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" |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +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" |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseMap.h" |
John McCall | a1e130b | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/DenseSet.h" |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 31 | #include "TypeLocBuilder.h" |
John McCall | a1e130b | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 32 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 33 | using namespace clang; |
| 34 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 35 | /// Check whether the given method, which must be in the 'init' |
| 36 | /// family, is a valid member of that family. |
| 37 | /// |
| 38 | /// \param receiverTypeIfCall - if null, check this as if declaring it; |
| 39 | /// if non-null, check this as if making a call to it with the given |
| 40 | /// receiver type |
| 41 | /// |
| 42 | /// \return true to indicate that there was an error and appropriate |
| 43 | /// actions were taken |
| 44 | bool Sema::checkInitMethod(ObjCMethodDecl *method, |
| 45 | QualType receiverTypeIfCall) { |
| 46 | if (method->isInvalidDecl()) return true; |
| 47 | |
| 48 | // This castAs is safe: methods that don't return an object |
| 49 | // pointer won't be inferred as inits and will reject an explicit |
| 50 | // objc_method_family(init). |
| 51 | |
| 52 | // We ignore protocols here. Should we? What about Class? |
| 53 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 54 | const ObjCObjectType *result = |
| 55 | method->getReturnType()->castAs<ObjCObjectPointerType>()->getObjectType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 56 | |
| 57 | if (result->isObjCId()) { |
| 58 | return false; |
| 59 | } else if (result->isObjCClass()) { |
| 60 | // fall through: always an error |
| 61 | } else { |
| 62 | ObjCInterfaceDecl *resultClass = result->getInterface(); |
| 63 | assert(resultClass && "unexpected object type!"); |
| 64 | |
| 65 | // It's okay for the result type to still be a forward declaration |
| 66 | // if we're checking an interface declaration. |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 67 | if (!resultClass->hasDefinition()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 68 | if (receiverTypeIfCall.isNull() && |
| 69 | !isa<ObjCImplementationDecl>(method->getDeclContext())) |
| 70 | return false; |
| 71 | |
| 72 | // Otherwise, we try to compare class types. |
| 73 | } else { |
| 74 | // If this method was declared in a protocol, we can't check |
| 75 | // anything unless we have a receiver type that's an interface. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 76 | const ObjCInterfaceDecl *receiverClass = nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 77 | if (isa<ObjCProtocolDecl>(method->getDeclContext())) { |
| 78 | if (receiverTypeIfCall.isNull()) |
| 79 | return false; |
| 80 | |
| 81 | receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>() |
| 82 | ->getInterfaceDecl(); |
| 83 | |
| 84 | // This can be null for calls to e.g. id<Foo>. |
| 85 | if (!receiverClass) return false; |
| 86 | } else { |
| 87 | receiverClass = method->getClassInterface(); |
| 88 | assert(receiverClass && "method not associated with a class!"); |
| 89 | } |
| 90 | |
| 91 | // If either class is a subclass of the other, it's fine. |
| 92 | if (receiverClass->isSuperClassOf(resultClass) || |
| 93 | resultClass->isSuperClassOf(receiverClass)) |
| 94 | return false; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | SourceLocation loc = method->getLocation(); |
| 99 | |
| 100 | // If we're in a system header, and this is not a call, just make |
| 101 | // the method unusable. |
| 102 | if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) { |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 103 | method->addAttr(UnavailableAttr::CreateImplicit(Context, |
| 104 | "init method returns a type unrelated to its receiver type", |
| 105 | loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | |
| 109 | // Otherwise, it's an error. |
| 110 | Diag(loc, diag::err_arc_init_method_unrelated_result_type); |
| 111 | method->setInvalidDecl(); |
| 112 | return true; |
| 113 | } |
| 114 | |
Fariborz Jahanian | ac8dbf0 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 115 | void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 116 | const ObjCMethodDecl *Overridden) { |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 117 | if (Overridden->hasRelatedResultType() && |
| 118 | !NewMethod->hasRelatedResultType()) { |
| 119 | // This can only happen when the method follows a naming convention that |
| 120 | // implies a related result type, and the original (overridden) method has |
| 121 | // a suitable return type, but the new (overriding) method does not have |
| 122 | // a suitable return type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 123 | QualType ResultType = NewMethod->getReturnType(); |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 124 | SourceRange ResultTypeRange = NewMethod->getReturnTypeSourceRange(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 125 | |
| 126 | // Figure out which class this method is part of, if any. |
| 127 | ObjCInterfaceDecl *CurrentClass |
| 128 | = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext()); |
| 129 | if (!CurrentClass) { |
| 130 | DeclContext *DC = NewMethod->getDeclContext(); |
| 131 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC)) |
| 132 | CurrentClass = Cat->getClassInterface(); |
| 133 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC)) |
| 134 | CurrentClass = Impl->getClassInterface(); |
| 135 | else if (ObjCCategoryImplDecl *CatImpl |
| 136 | = dyn_cast<ObjCCategoryImplDecl>(DC)) |
| 137 | CurrentClass = CatImpl->getClassInterface(); |
| 138 | } |
| 139 | |
| 140 | if (CurrentClass) { |
| 141 | Diag(NewMethod->getLocation(), |
| 142 | diag::warn_related_result_type_compatibility_class) |
| 143 | << Context.getObjCInterfaceType(CurrentClass) |
| 144 | << ResultType |
| 145 | << ResultTypeRange; |
| 146 | } else { |
| 147 | Diag(NewMethod->getLocation(), |
| 148 | diag::warn_related_result_type_compatibility_protocol) |
| 149 | << ResultType |
| 150 | << ResultTypeRange; |
| 151 | } |
| 152 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 153 | if (ObjCMethodFamily Family = Overridden->getMethodFamily()) |
| 154 | Diag(Overridden->getLocation(), |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 155 | diag::note_related_result_type_family) |
| 156 | << /*overridden method*/ 0 |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 157 | << Family; |
| 158 | else |
| 159 | Diag(Overridden->getLocation(), |
| 160 | diag::note_related_result_type_overridden); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 161 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 162 | if (getLangOpts().ObjCAutoRefCount) { |
Fariborz Jahanian | ac8dbf0 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 163 | if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() != |
| 164 | Overridden->hasAttr<NSReturnsRetainedAttr>())) { |
| 165 | Diag(NewMethod->getLocation(), |
| 166 | diag::err_nsreturns_retained_attribute_mismatch) << 1; |
| 167 | Diag(Overridden->getLocation(), diag::note_previous_decl) |
| 168 | << "method"; |
| 169 | } |
| 170 | if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() != |
| 171 | Overridden->hasAttr<NSReturnsNotRetainedAttr>())) { |
| 172 | Diag(NewMethod->getLocation(), |
| 173 | diag::err_nsreturns_retained_attribute_mismatch) << 0; |
| 174 | Diag(Overridden->getLocation(), diag::note_previous_decl) |
| 175 | << "method"; |
| 176 | } |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 177 | ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(), |
| 178 | oe = Overridden->param_end(); |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 179 | for (ObjCMethodDecl::param_iterator |
| 180 | ni = NewMethod->param_begin(), ne = NewMethod->param_end(); |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 181 | ni != ne && oi != oe; ++ni, ++oi) { |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 182 | const ParmVarDecl *oldDecl = (*oi); |
Fariborz Jahanian | ac8dbf0 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 183 | ParmVarDecl *newDecl = (*ni); |
| 184 | if (newDecl->hasAttr<NSConsumedAttr>() != |
| 185 | oldDecl->hasAttr<NSConsumedAttr>()) { |
| 186 | Diag(newDecl->getLocation(), |
| 187 | diag::err_nsconsumed_attribute_mismatch); |
| 188 | Diag(oldDecl->getLocation(), diag::note_previous_decl) |
| 189 | << "parameter"; |
| 190 | } |
| 191 | } |
| 192 | } |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 193 | } |
| 194 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 195 | /// \brief Check a method declaration for compatibility with the Objective-C |
| 196 | /// ARC conventions. |
John McCall | e48f389 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 197 | bool Sema::CheckARCMethodDecl(ObjCMethodDecl *method) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 198 | ObjCMethodFamily family = method->getMethodFamily(); |
| 199 | switch (family) { |
| 200 | case OMF_None: |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 201 | case OMF_finalize: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 202 | case OMF_retain: |
| 203 | case OMF_release: |
| 204 | case OMF_autorelease: |
| 205 | case OMF_retainCount: |
| 206 | case OMF_self: |
Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 207 | case OMF_initialize: |
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)) { |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 213 | SourceRange ResultTypeRange = method->getReturnTypeSourceRange(); |
Fariborz Jahanian | b7f03c1 | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 214 | if (ResultTypeRange.isInvalid()) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 215 | Diag(method->getLocation(), diag::error_dealloc_bad_result_type) |
| 216 | << method->getReturnType() |
| 217 | << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)"); |
Fariborz Jahanian | b7f03c1 | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 218 | else |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 219 | Diag(method->getLocation(), diag::error_dealloc_bad_result_type) |
| 220 | << method->getReturnType() |
| 221 | << FixItHint::CreateReplacement(ResultTypeRange, "void"); |
Fariborz Jahanian | b7f03c1 | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 222 | return true; |
| 223 | } |
| 224 | return false; |
| 225 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 226 | case OMF_init: |
| 227 | // 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] | 228 | if (checkInitMethod(method, QualType())) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 229 | return true; |
| 230 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 231 | method->addAttr(NSConsumesSelfAttr::CreateImplicit(Context)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 232 | |
| 233 | // Don't add a second copy of this attribute, but otherwise don't |
| 234 | // let it be suppressed. |
| 235 | if (method->hasAttr<NSReturnsRetainedAttr>()) |
| 236 | return false; |
| 237 | break; |
| 238 | |
| 239 | case OMF_alloc: |
| 240 | case OMF_copy: |
| 241 | case OMF_mutableCopy: |
| 242 | case OMF_new: |
| 243 | if (method->hasAttr<NSReturnsRetainedAttr>() || |
| 244 | method->hasAttr<NSReturnsNotRetainedAttr>() || |
| 245 | method->hasAttr<NSReturnsAutoreleasedAttr>()) |
| 246 | return false; |
| 247 | break; |
| 248 | } |
| 249 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 250 | method->addAttr(NSReturnsRetainedAttr::CreateImplicit(Context)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 251 | return false; |
| 252 | } |
| 253 | |
Fariborz Jahanian | d724a10 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 254 | static void DiagnoseObjCImplementedDeprecations(Sema &S, |
| 255 | NamedDecl *ND, |
| 256 | SourceLocation ImplLoc, |
| 257 | int select) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 258 | if (ND && ND->isDeprecated()) { |
Fariborz Jahanian | 6fd9435 | 2011-02-16 00:30:31 +0000 | [diff] [blame] | 259 | S.Diag(ImplLoc, diag::warn_deprecated_def) << select; |
Fariborz Jahanian | d724a10 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 260 | if (select == 0) |
Ted Kremenek | 59b10db | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 261 | S.Diag(ND->getLocation(), diag::note_method_declared_at) |
| 262 | << ND->getDeclName(); |
Fariborz Jahanian | d724a10 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 263 | else |
| 264 | S.Diag(ND->getLocation(), diag::note_previous_decl) << "class"; |
| 265 | } |
| 266 | } |
| 267 | |
Fariborz Jahanian | bd0642f | 2011-08-31 17:37:55 +0000 | [diff] [blame] | 268 | /// AddAnyMethodToGlobalPool - Add any method, instance or factory to global |
| 269 | /// pool. |
| 270 | void Sema::AddAnyMethodToGlobalPool(Decl *D) { |
| 271 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); |
| 272 | |
| 273 | // If we don't have a valid method decl, simply return. |
| 274 | if (!MDecl) |
| 275 | return; |
| 276 | if (MDecl->isInstanceMethod()) |
| 277 | AddInstanceMethodToGlobalPool(MDecl, true); |
| 278 | else |
| 279 | AddFactoryMethodToGlobalPool(MDecl, true); |
| 280 | } |
| 281 | |
Fariborz Jahanian | 1dfeace | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 282 | /// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer |
| 283 | /// has explicit ownership attribute; false otherwise. |
| 284 | static bool |
| 285 | HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) { |
| 286 | QualType T = Param->getType(); |
| 287 | |
Fariborz Jahanian | 1dfeace | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 288 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 289 | T = PT->getPointeeType(); |
| 290 | } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) { |
| 291 | T = RT->getPointeeType(); |
| 292 | } else { |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | // If we have a lifetime qualifier, but it's local, we must have |
| 297 | // inferred it. So, it is implicit. |
| 298 | return !T.getLocalQualifiers().hasObjCLifetime(); |
| 299 | } |
| 300 | |
Fariborz Jahanian | 18d0a5d | 2012-08-08 23:41:08 +0000 | [diff] [blame] | 301 | /// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible |
| 302 | /// and user declared, in the method definition's AST. |
| 303 | void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 304 | assert((getCurMethodDecl() == nullptr) && "Methodparsing confused"); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 305 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); |
Fariborz Jahanian | 577574a | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 306 | |
Steve Naroff | 542cd5d | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 307 | // If we don't have a valid method decl, simply return. |
| 308 | if (!MDecl) |
| 309 | return; |
Steve Naroff | 1d2538c | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 310 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 311 | // 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] | 312 | PushDeclContext(FnBodyScope, MDecl); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 313 | PushFunctionScope(); |
| 314 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 315 | // Create Decl objects for each parameter, entrring them in the scope for |
| 316 | // binding to their use. |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 317 | |
| 318 | // Insert the invisible arguments, self and _cmd! |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 319 | MDecl->createImplicitParams(Context, MDecl->getClassInterface()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | |
Daniel Dunbar | 279d1cc | 2008-08-26 06:07:48 +0000 | [diff] [blame] | 321 | PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); |
| 322 | PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 323 | |
Reid Kleckner | 5a11580 | 2013-06-24 14:38:26 +0000 | [diff] [blame] | 324 | // The ObjC parser requires parameter names so there's no need to check. |
| 325 | CheckParmsForFunctionDef(MDecl->param_begin(), MDecl->param_end(), |
| 326 | /*CheckParameterNames=*/false); |
| 327 | |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 328 | // Introduce all of the other parameters into this scope. |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 329 | for (auto *Param : MDecl->params()) { |
Fariborz Jahanian | b3e8712 | 2010-09-17 22:07:07 +0000 | [diff] [blame] | 330 | if (!Param->isInvalidDecl() && |
Fariborz Jahanian | 1dfeace | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 331 | getLangOpts().ObjCAutoRefCount && |
| 332 | !HasExplicitOwnershipAttr(*this, Param)) |
| 333 | Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) << |
| 334 | Param->getType(); |
Fariborz Jahanian | cd278ff | 2012-08-30 23:56:02 +0000 | [diff] [blame] | 335 | |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 336 | if (Param->getIdentifier()) |
| 337 | PushOnScopeChains(Param, FnBodyScope); |
Fariborz Jahanian | b3e8712 | 2010-09-17 22:07:07 +0000 | [diff] [blame] | 338 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 339 | |
| 340 | // In ARC, disallow definition of retain/release/autorelease/retainCount |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 341 | if (getLangOpts().ObjCAutoRefCount) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 342 | switch (MDecl->getMethodFamily()) { |
| 343 | case OMF_retain: |
| 344 | case OMF_retainCount: |
| 345 | case OMF_release: |
| 346 | case OMF_autorelease: |
| 347 | Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def) |
Fariborz Jahanian | 39d1c42 | 2013-05-16 19:08:44 +0000 | [diff] [blame] | 348 | << 0 << MDecl->getSelector(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 349 | break; |
| 350 | |
| 351 | case OMF_None: |
| 352 | case OMF_dealloc: |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 353 | case OMF_finalize: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 354 | case OMF_alloc: |
| 355 | case OMF_init: |
| 356 | case OMF_mutableCopy: |
| 357 | case OMF_copy: |
| 358 | case OMF_new: |
| 359 | case OMF_self: |
Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 360 | case OMF_initialize: |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 361 | case OMF_performSelector: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 362 | break; |
| 363 | } |
| 364 | } |
| 365 | |
Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 366 | // Warn on deprecated methods under -Wdeprecated-implementations, |
| 367 | // and prepare for warning on missing super calls. |
| 368 | if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) { |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 369 | ObjCMethodDecl *IMD = |
| 370 | IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()); |
| 371 | |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 372 | if (IMD) { |
| 373 | ObjCImplDecl *ImplDeclOfMethodDef = |
| 374 | dyn_cast<ObjCImplDecl>(MDecl->getDeclContext()); |
| 375 | ObjCContainerDecl *ContDeclOfMethodDecl = |
| 376 | dyn_cast<ObjCContainerDecl>(IMD->getDeclContext()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 377 | ObjCImplDecl *ImplDeclOfMethodDecl = nullptr; |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 378 | if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl)) |
| 379 | ImplDeclOfMethodDecl = OID->getImplementation(); |
Fariborz Jahanian | ed39e7c | 2014-03-18 16:25:22 +0000 | [diff] [blame] | 380 | else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl)) { |
| 381 | if (CD->IsClassExtension()) { |
| 382 | if (ObjCInterfaceDecl *OID = CD->getClassInterface()) |
| 383 | ImplDeclOfMethodDecl = OID->getImplementation(); |
| 384 | } else |
| 385 | ImplDeclOfMethodDecl = CD->getImplementation(); |
Fariborz Jahanian | 19a08bb | 2014-03-18 00:10:37 +0000 | [diff] [blame] | 386 | } |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 387 | // No need to issue deprecated warning if deprecated mehod in class/category |
| 388 | // is being implemented in its own implementation (no overriding is involved). |
Fariborz Jahanian | ed39e7c | 2014-03-18 16:25:22 +0000 | [diff] [blame] | 389 | if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef) |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 390 | DiagnoseObjCImplementedDeprecations(*this, |
Fariborz Jahanian | d724a10 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 391 | dyn_cast<NamedDecl>(IMD), |
| 392 | MDecl->getLocation(), 0); |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 393 | } |
Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 394 | |
Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 395 | if (MDecl->getMethodFamily() == OMF_init) { |
| 396 | if (MDecl->isDesignatedInitializerForTheInterface()) { |
| 397 | getCurFunction()->ObjCIsDesignatedInit = true; |
Fariborz Jahanian | e3b5c99 | 2014-03-14 23:30:18 +0000 | [diff] [blame] | 398 | getCurFunction()->ObjCWarnForNoDesignatedInitChain = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 399 | IC->getSuperClass() != nullptr; |
Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 400 | } else if (IC->hasDesignatedInitializers()) { |
| 401 | getCurFunction()->ObjCIsSecondaryInit = true; |
Fariborz Jahanian | e3b5c99 | 2014-03-14 23:30:18 +0000 | [diff] [blame] | 402 | getCurFunction()->ObjCWarnForNoInitDelegation = true; |
Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
Argyrios Kyrtzidis | 22bfa2c | 2013-12-03 21:11:36 +0000 | [diff] [blame] | 405 | |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 406 | // If this is "dealloc" or "finalize", set some bit here. |
Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 407 | // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false. |
| 408 | // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set. |
| 409 | // Only do this if the current class actually has a superclass. |
Jordan Rose | d03d99d | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 410 | if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) { |
Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 411 | ObjCMethodFamily Family = MDecl->getMethodFamily(); |
| 412 | if (Family == OMF_dealloc) { |
| 413 | if (!(getLangOpts().ObjCAutoRefCount || |
| 414 | getLangOpts().getGC() == LangOptions::GCOnly)) |
| 415 | getCurFunction()->ObjCShouldCallSuper = true; |
| 416 | |
| 417 | } else if (Family == OMF_finalize) { |
| 418 | if (Context.getLangOpts().getGC() != LangOptions::NonGC) |
| 419 | getCurFunction()->ObjCShouldCallSuper = true; |
| 420 | |
Fariborz Jahanian | ce4bbb2 | 2013-11-05 00:28:21 +0000 | [diff] [blame] | 421 | } else { |
Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 422 | const ObjCMethodDecl *SuperMethod = |
Jordan Rose | d03d99d | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 423 | SuperClass->lookupMethod(MDecl->getSelector(), |
| 424 | MDecl->isInstanceMethod()); |
Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 425 | getCurFunction()->ObjCShouldCallSuper = |
| 426 | (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>()); |
Fariborz Jahanian | d6876b2 | 2012-09-10 18:04:25 +0000 | [diff] [blame] | 427 | } |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 428 | } |
Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 429 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 432 | namespace { |
| 433 | |
| 434 | // Callback to only accept typo corrections that are Objective-C classes. |
| 435 | // If an ObjCInterfaceDecl* is given to the constructor, then the validation |
| 436 | // function will reject corrections to that class. |
| 437 | class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback { |
| 438 | public: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 439 | ObjCInterfaceValidatorCCC() : CurrentIDecl(nullptr) {} |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 440 | explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl) |
| 441 | : CurrentIDecl(IDecl) {} |
| 442 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 443 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 444 | ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
| 445 | return ID && !declaresSameEntity(ID, CurrentIDecl); |
| 446 | } |
| 447 | |
| 448 | private: |
| 449 | ObjCInterfaceDecl *CurrentIDecl; |
| 450 | }; |
| 451 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 452 | } |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 453 | |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 454 | static void diagnoseUseOfProtocols(Sema &TheSema, |
| 455 | ObjCContainerDecl *CD, |
| 456 | ObjCProtocolDecl *const *ProtoRefs, |
| 457 | unsigned NumProtoRefs, |
| 458 | const SourceLocation *ProtoLocs) { |
| 459 | assert(ProtoRefs); |
| 460 | // Diagnose availability in the context of the ObjC container. |
| 461 | Sema::ContextRAII SavedContext(TheSema, CD); |
| 462 | for (unsigned i = 0; i < NumProtoRefs; ++i) { |
| 463 | (void)TheSema.DiagnoseUseOfDecl(ProtoRefs[i], ProtoLocs[i]); |
| 464 | } |
| 465 | } |
| 466 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 467 | void Sema:: |
| 468 | ActOnSuperClassOfClassInterface(Scope *S, |
| 469 | SourceLocation AtInterfaceLoc, |
| 470 | ObjCInterfaceDecl *IDecl, |
| 471 | IdentifierInfo *ClassName, |
| 472 | SourceLocation ClassLoc, |
| 473 | IdentifierInfo *SuperName, |
| 474 | SourceLocation SuperLoc, |
| 475 | ArrayRef<ParsedType> SuperTypeArgs, |
| 476 | SourceRange SuperTypeArgsRange) { |
| 477 | // Check if a different kind of symbol declared in this scope. |
| 478 | NamedDecl *PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
| 479 | LookupOrdinaryName); |
| 480 | |
| 481 | if (!PrevDecl) { |
| 482 | // Try to correct for a typo in the superclass name without correcting |
| 483 | // to the class we're defining. |
| 484 | if (TypoCorrection Corrected = CorrectTypo( |
| 485 | DeclarationNameInfo(SuperName, SuperLoc), |
| 486 | LookupOrdinaryName, TUScope, |
| 487 | NULL, llvm::make_unique<ObjCInterfaceValidatorCCC>(IDecl), |
| 488 | CTK_ErrorRecovery)) { |
| 489 | diagnoseTypo(Corrected, PDiag(diag::err_undef_superclass_suggest) |
| 490 | << SuperName << ClassName); |
| 491 | PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | if (declaresSameEntity(PrevDecl, IDecl)) { |
| 496 | Diag(SuperLoc, diag::err_recursive_superclass) |
| 497 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
| 498 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
| 499 | } else { |
| 500 | ObjCInterfaceDecl *SuperClassDecl = |
| 501 | dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
| 502 | QualType SuperClassType; |
| 503 | |
| 504 | // Diagnose classes that inherit from deprecated classes. |
| 505 | if (SuperClassDecl) { |
| 506 | (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc); |
| 507 | SuperClassType = Context.getObjCInterfaceType(SuperClassDecl); |
| 508 | } |
| 509 | |
| 510 | if (PrevDecl && SuperClassDecl == 0) { |
| 511 | // The previous declaration was not a class decl. Check if we have a |
| 512 | // typedef. If we do, get the underlying class type. |
| 513 | if (const TypedefNameDecl *TDecl = |
| 514 | dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) { |
| 515 | QualType T = TDecl->getUnderlyingType(); |
| 516 | if (T->isObjCObjectType()) { |
| 517 | if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) { |
| 518 | SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl); |
| 519 | SuperClassType = Context.getTypeDeclType(TDecl); |
| 520 | |
| 521 | // This handles the following case: |
| 522 | // @interface NewI @end |
| 523 | // typedef NewI DeprI __attribute__((deprecated("blah"))) |
| 524 | // @interface SI : DeprI /* warn here */ @end |
| 525 | (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc); |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | // This handles the following case: |
| 531 | // |
| 532 | // typedef int SuperClass; |
| 533 | // @interface MyClass : SuperClass {} @end |
| 534 | // |
| 535 | if (!SuperClassDecl) { |
| 536 | Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName; |
| 537 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) { |
| 542 | if (!SuperClassDecl) |
| 543 | Diag(SuperLoc, diag::err_undef_superclass) |
| 544 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
| 545 | else if (RequireCompleteType(SuperLoc, |
| 546 | SuperClassType, |
| 547 | diag::err_forward_superclass, |
| 548 | SuperClassDecl->getDeclName(), |
| 549 | ClassName, |
| 550 | SourceRange(AtInterfaceLoc, ClassLoc))) { |
| 551 | SuperClassDecl = 0; |
| 552 | SuperClassType = QualType(); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | if (SuperClassType.isNull()) { |
| 557 | assert(!SuperClassDecl && "Failed to set SuperClassType?"); |
| 558 | return; |
| 559 | } |
| 560 | |
| 561 | // Handle type arguments on the superclass. |
| 562 | TypeSourceInfo *SuperClassTInfo = nullptr; |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 563 | if (!SuperTypeArgs.empty()) { |
| 564 | TypeResult fullSuperClassType = actOnObjCTypeArgsAndProtocolQualifiers( |
| 565 | S, |
| 566 | SuperLoc, |
| 567 | CreateParsedType(SuperClassType, |
| 568 | nullptr), |
| 569 | SuperTypeArgsRange.getBegin(), |
| 570 | SuperTypeArgs, |
| 571 | SuperTypeArgsRange.getEnd(), |
| 572 | SourceLocation(), |
| 573 | { }, |
| 574 | { }, |
| 575 | SourceLocation()); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 576 | if (!fullSuperClassType.isUsable()) |
| 577 | return; |
| 578 | |
| 579 | SuperClassType = GetTypeFromParser(fullSuperClassType.get(), |
| 580 | &SuperClassTInfo); |
| 581 | } |
| 582 | |
| 583 | if (!SuperClassTInfo) { |
| 584 | SuperClassTInfo = Context.getTrivialTypeSourceInfo(SuperClassType, |
| 585 | SuperLoc); |
| 586 | } |
| 587 | |
| 588 | IDecl->setSuperClass(SuperClassTInfo); |
| 589 | IDecl->setEndOfDefinitionLoc(SuperClassTInfo->getTypeLoc().getLocEnd()); |
| 590 | } |
| 591 | } |
| 592 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame^] | 593 | DeclResult Sema::actOnObjCTypeParam(Scope *S, |
| 594 | ObjCTypeParamVariance variance, |
| 595 | SourceLocation varianceLoc, |
| 596 | unsigned index, |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 597 | IdentifierInfo *paramName, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 598 | SourceLocation paramLoc, |
| 599 | SourceLocation colonLoc, |
| 600 | ParsedType parsedTypeBound) { |
| 601 | // If there was an explicitly-provided type bound, check it. |
| 602 | TypeSourceInfo *typeBoundInfo = nullptr; |
| 603 | if (parsedTypeBound) { |
| 604 | // The type bound can be any Objective-C pointer type. |
| 605 | QualType typeBound = GetTypeFromParser(parsedTypeBound, &typeBoundInfo); |
| 606 | if (typeBound->isObjCObjectPointerType()) { |
| 607 | // okay |
| 608 | } else if (typeBound->isObjCObjectType()) { |
| 609 | // The user forgot the * on an Objective-C pointer type, e.g., |
| 610 | // "T : NSView". |
| 611 | SourceLocation starLoc = PP.getLocForEndOfToken( |
| 612 | typeBoundInfo->getTypeLoc().getEndLoc()); |
| 613 | Diag(typeBoundInfo->getTypeLoc().getBeginLoc(), |
| 614 | diag::err_objc_type_param_bound_missing_pointer) |
| 615 | << typeBound << paramName |
| 616 | << FixItHint::CreateInsertion(starLoc, " *"); |
| 617 | |
| 618 | // Create a new type location builder so we can update the type |
| 619 | // location information we have. |
| 620 | TypeLocBuilder builder; |
| 621 | builder.pushFullCopy(typeBoundInfo->getTypeLoc()); |
| 622 | |
| 623 | // Create the Objective-C pointer type. |
| 624 | typeBound = Context.getObjCObjectPointerType(typeBound); |
| 625 | ObjCObjectPointerTypeLoc newT |
| 626 | = builder.push<ObjCObjectPointerTypeLoc>(typeBound); |
| 627 | newT.setStarLoc(starLoc); |
| 628 | |
| 629 | // Form the new type source information. |
| 630 | typeBoundInfo = builder.getTypeSourceInfo(Context, typeBound); |
| 631 | } else { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 632 | // Not a valid type bound. |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 633 | Diag(typeBoundInfo->getTypeLoc().getBeginLoc(), |
| 634 | diag::err_objc_type_param_bound_nonobject) |
| 635 | << typeBound << paramName; |
| 636 | |
| 637 | // Forget the bound; we'll default to id later. |
| 638 | typeBoundInfo = nullptr; |
| 639 | } |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 640 | |
| 641 | // Type bounds cannot have explicit nullability. |
| 642 | if (typeBoundInfo) { |
| 643 | // Type arguments cannot explicitly specify nullability. |
| 644 | if (auto nullability = AttributedType::stripOuterNullability(typeBound)) { |
| 645 | // Look at the type location information to find the nullability |
| 646 | // specifier so we can zap it. |
| 647 | SourceLocation nullabilityLoc |
| 648 | = typeBoundInfo->getTypeLoc().findNullabilityLoc(); |
| 649 | SourceLocation diagLoc |
| 650 | = nullabilityLoc.isValid()? nullabilityLoc |
| 651 | : typeBoundInfo->getTypeLoc().getLocStart(); |
| 652 | Diag(diagLoc, diag::err_type_param_bound_explicit_nullability) |
| 653 | << paramName << typeBoundInfo->getType() |
| 654 | << FixItHint::CreateRemoval(nullabilityLoc); |
| 655 | } |
| 656 | } |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | // If there was no explicit type bound (or we removed it due to an error), |
| 660 | // use 'id' instead. |
| 661 | if (!typeBoundInfo) { |
| 662 | colonLoc = SourceLocation(); |
| 663 | typeBoundInfo = Context.getTrivialTypeSourceInfo(Context.getObjCIdType()); |
| 664 | } |
| 665 | |
| 666 | // Create the type parameter. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame^] | 667 | return ObjCTypeParamDecl::Create(Context, CurContext, variance, varianceLoc, |
| 668 | index, paramLoc, paramName, colonLoc, |
| 669 | typeBoundInfo); |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | ObjCTypeParamList *Sema::actOnObjCTypeParamList(Scope *S, |
| 673 | SourceLocation lAngleLoc, |
| 674 | ArrayRef<Decl *> typeParamsIn, |
| 675 | SourceLocation rAngleLoc) { |
| 676 | // We know that the array only contains Objective-C type parameters. |
| 677 | ArrayRef<ObjCTypeParamDecl *> |
| 678 | typeParams( |
| 679 | reinterpret_cast<ObjCTypeParamDecl * const *>(typeParamsIn.data()), |
| 680 | typeParamsIn.size()); |
| 681 | |
| 682 | // Diagnose redeclarations of type parameters. |
| 683 | // We do this now because Objective-C type parameters aren't pushed into |
| 684 | // scope until later (after the instance variable block), but we want the |
| 685 | // diagnostics to occur right after we parse the type parameter list. |
| 686 | llvm::SmallDenseMap<IdentifierInfo *, ObjCTypeParamDecl *> knownParams; |
| 687 | for (auto typeParam : typeParams) { |
| 688 | auto known = knownParams.find(typeParam->getIdentifier()); |
| 689 | if (known != knownParams.end()) { |
| 690 | Diag(typeParam->getLocation(), diag::err_objc_type_param_redecl) |
| 691 | << typeParam->getIdentifier() |
| 692 | << SourceRange(known->second->getLocation()); |
| 693 | |
| 694 | typeParam->setInvalidDecl(); |
| 695 | } else { |
| 696 | knownParams.insert(std::make_pair(typeParam->getIdentifier(), typeParam)); |
| 697 | |
| 698 | // Push the type parameter into scope. |
| 699 | PushOnScopeChains(typeParam, S, /*AddToContext=*/false); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | // Create the parameter list. |
| 704 | return ObjCTypeParamList::create(Context, lAngleLoc, typeParams, rAngleLoc); |
| 705 | } |
| 706 | |
| 707 | void Sema::popObjCTypeParamList(Scope *S, ObjCTypeParamList *typeParamList) { |
| 708 | for (auto typeParam : *typeParamList) { |
| 709 | if (!typeParam->isInvalidDecl()) { |
| 710 | S->RemoveDecl(typeParam); |
| 711 | IdResolver.RemoveDecl(typeParam); |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | namespace { |
| 717 | /// The context in which an Objective-C type parameter list occurs, for use |
| 718 | /// in diagnostics. |
| 719 | enum class TypeParamListContext { |
| 720 | ForwardDeclaration, |
| 721 | Definition, |
| 722 | Category, |
| 723 | Extension |
| 724 | }; |
| 725 | } |
| 726 | |
| 727 | /// Check consistency between two Objective-C type parameter lists, e.g., |
| 728 | /// between a category/extension and an @interface or between an @class and an |
| 729 | /// @interface. |
| 730 | static bool checkTypeParamListConsistency(Sema &S, |
| 731 | ObjCTypeParamList *prevTypeParams, |
| 732 | ObjCTypeParamList *newTypeParams, |
| 733 | TypeParamListContext newContext) { |
| 734 | // If the sizes don't match, complain about that. |
| 735 | if (prevTypeParams->size() != newTypeParams->size()) { |
| 736 | SourceLocation diagLoc; |
| 737 | if (newTypeParams->size() > prevTypeParams->size()) { |
| 738 | diagLoc = newTypeParams->begin()[prevTypeParams->size()]->getLocation(); |
| 739 | } else { |
| 740 | diagLoc = S.PP.getLocForEndOfToken(newTypeParams->back()->getLocEnd()); |
| 741 | } |
| 742 | |
| 743 | S.Diag(diagLoc, diag::err_objc_type_param_arity_mismatch) |
| 744 | << static_cast<unsigned>(newContext) |
| 745 | << (newTypeParams->size() > prevTypeParams->size()) |
| 746 | << prevTypeParams->size() |
| 747 | << newTypeParams->size(); |
| 748 | |
| 749 | return true; |
| 750 | } |
| 751 | |
| 752 | // Match up the type parameters. |
| 753 | for (unsigned i = 0, n = prevTypeParams->size(); i != n; ++i) { |
| 754 | ObjCTypeParamDecl *prevTypeParam = prevTypeParams->begin()[i]; |
| 755 | ObjCTypeParamDecl *newTypeParam = newTypeParams->begin()[i]; |
| 756 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame^] | 757 | // Check for consistency of the variance. |
| 758 | if (newTypeParam->getVariance() != prevTypeParam->getVariance()) { |
| 759 | if (newTypeParam->getVariance() == ObjCTypeParamVariance::Invariant && |
| 760 | newContext != TypeParamListContext::Definition) { |
| 761 | // When the new type parameter is invariant and is not part |
| 762 | // of the definition, just propagate the variance. |
| 763 | newTypeParam->setVariance(prevTypeParam->getVariance()); |
| 764 | } else if (prevTypeParam->getVariance() |
| 765 | == ObjCTypeParamVariance::Invariant && |
| 766 | !(isa<ObjCInterfaceDecl>(prevTypeParam->getDeclContext()) && |
| 767 | cast<ObjCInterfaceDecl>(prevTypeParam->getDeclContext()) |
| 768 | ->getDefinition() == prevTypeParam->getDeclContext())) { |
| 769 | // When the old parameter is invariant and was not part of the |
| 770 | // definition, just ignore the difference because it doesn't |
| 771 | // matter. |
| 772 | } else { |
| 773 | { |
| 774 | // Diagnose the conflict and update the second declaration. |
| 775 | SourceLocation diagLoc = newTypeParam->getVarianceLoc(); |
| 776 | if (diagLoc.isInvalid()) |
| 777 | diagLoc = newTypeParam->getLocStart(); |
| 778 | |
| 779 | auto diag = S.Diag(diagLoc, |
| 780 | diag::err_objc_type_param_variance_conflict) |
| 781 | << static_cast<unsigned>(newTypeParam->getVariance()) |
| 782 | << newTypeParam->getDeclName() |
| 783 | << static_cast<unsigned>(prevTypeParam->getVariance()) |
| 784 | << prevTypeParam->getDeclName(); |
| 785 | switch (prevTypeParam->getVariance()) { |
| 786 | case ObjCTypeParamVariance::Invariant: |
| 787 | diag << FixItHint::CreateRemoval(newTypeParam->getVarianceLoc()); |
| 788 | break; |
| 789 | |
| 790 | case ObjCTypeParamVariance::Covariant: |
| 791 | case ObjCTypeParamVariance::Contravariant: { |
| 792 | StringRef newVarianceStr |
| 793 | = prevTypeParam->getVariance() == ObjCTypeParamVariance::Covariant |
| 794 | ? "__covariant" |
| 795 | : "__contravariant"; |
| 796 | if (newTypeParam->getVariance() |
| 797 | == ObjCTypeParamVariance::Invariant) { |
| 798 | diag << FixItHint::CreateInsertion(newTypeParam->getLocStart(), |
| 799 | (newVarianceStr + " ").str()); |
| 800 | } else { |
| 801 | diag << FixItHint::CreateReplacement(newTypeParam->getVarianceLoc(), |
| 802 | newVarianceStr); |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
| 809 | << prevTypeParam->getDeclName(); |
| 810 | |
| 811 | // Override the variance. |
| 812 | newTypeParam->setVariance(prevTypeParam->getVariance()); |
| 813 | } |
| 814 | } |
| 815 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 816 | // If the bound types match, there's nothing to do. |
| 817 | if (S.Context.hasSameType(prevTypeParam->getUnderlyingType(), |
| 818 | newTypeParam->getUnderlyingType())) |
| 819 | continue; |
| 820 | |
| 821 | // If the new type parameter's bound was explicit, complain about it being |
| 822 | // different from the original. |
| 823 | if (newTypeParam->hasExplicitBound()) { |
| 824 | SourceRange newBoundRange = newTypeParam->getTypeSourceInfo() |
| 825 | ->getTypeLoc().getSourceRange(); |
| 826 | S.Diag(newBoundRange.getBegin(), diag::err_objc_type_param_bound_conflict) |
| 827 | << newTypeParam->getUnderlyingType() |
| 828 | << newTypeParam->getDeclName() |
| 829 | << prevTypeParam->hasExplicitBound() |
| 830 | << prevTypeParam->getUnderlyingType() |
| 831 | << (newTypeParam->getDeclName() == prevTypeParam->getDeclName()) |
| 832 | << prevTypeParam->getDeclName() |
| 833 | << FixItHint::CreateReplacement( |
| 834 | newBoundRange, |
| 835 | prevTypeParam->getUnderlyingType().getAsString( |
| 836 | S.Context.getPrintingPolicy())); |
| 837 | |
| 838 | S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
| 839 | << prevTypeParam->getDeclName(); |
| 840 | |
| 841 | // Override the new type parameter's bound type with the previous type, |
| 842 | // so that it's consistent. |
| 843 | newTypeParam->setTypeSourceInfo( |
| 844 | S.Context.getTrivialTypeSourceInfo(prevTypeParam->getUnderlyingType())); |
| 845 | continue; |
| 846 | } |
| 847 | |
| 848 | // The new type parameter got the implicit bound of 'id'. That's okay for |
| 849 | // categories and extensions (overwrite it later), but not for forward |
| 850 | // declarations and @interfaces, because those must be standalone. |
| 851 | if (newContext == TypeParamListContext::ForwardDeclaration || |
| 852 | newContext == TypeParamListContext::Definition) { |
| 853 | // Diagnose this problem for forward declarations and definitions. |
| 854 | SourceLocation insertionLoc |
| 855 | = S.PP.getLocForEndOfToken(newTypeParam->getLocation()); |
| 856 | std::string newCode |
| 857 | = " : " + prevTypeParam->getUnderlyingType().getAsString( |
| 858 | S.Context.getPrintingPolicy()); |
| 859 | S.Diag(newTypeParam->getLocation(), |
| 860 | diag::err_objc_type_param_bound_missing) |
| 861 | << prevTypeParam->getUnderlyingType() |
| 862 | << newTypeParam->getDeclName() |
| 863 | << (newContext == TypeParamListContext::ForwardDeclaration) |
| 864 | << FixItHint::CreateInsertion(insertionLoc, newCode); |
| 865 | |
| 866 | S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
| 867 | << prevTypeParam->getDeclName(); |
| 868 | } |
| 869 | |
| 870 | // Update the new type parameter's bound to match the previous one. |
| 871 | newTypeParam->setTypeSourceInfo( |
| 872 | S.Context.getTrivialTypeSourceInfo(prevTypeParam->getUnderlyingType())); |
| 873 | } |
| 874 | |
| 875 | return false; |
| 876 | } |
| 877 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 878 | Decl *Sema:: |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 879 | ActOnStartClassInterface(Scope *S, SourceLocation AtInterfaceLoc, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 880 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 881 | ObjCTypeParamList *typeParamList, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 882 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 883 | ArrayRef<ParsedType> SuperTypeArgs, |
| 884 | SourceRange SuperTypeArgsRange, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 885 | Decl * const *ProtoRefs, unsigned NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 886 | const SourceLocation *ProtoLocs, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 887 | SourceLocation EndProtoLoc, AttributeList *AttrList) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 888 | assert(ClassName && "Missing class identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 889 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 890 | // Check for another declaration kind with the same name. |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 891 | NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc, |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 892 | LookupOrdinaryName, ForRedeclaration); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 893 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 894 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 895 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 896 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 897 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 898 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 899 | // Create a declaration to describe this @interface. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 900 | ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Argyrios Kyrtzidis | dd71063 | 2013-06-18 21:26:33 +0000 | [diff] [blame] | 901 | |
| 902 | if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) { |
| 903 | // A previous decl with a different name is because of |
| 904 | // @compatibility_alias, for example: |
| 905 | // \code |
| 906 | // @class NewImage; |
| 907 | // @compatibility_alias OldImage NewImage; |
| 908 | // \endcode |
| 909 | // A lookup for 'OldImage' will return the 'NewImage' decl. |
| 910 | // |
| 911 | // In such a case use the real declaration name, instead of the alias one, |
| 912 | // otherwise we will break IdentifierResolver and redecls-chain invariants. |
| 913 | // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl |
| 914 | // has been aliased. |
| 915 | ClassName = PrevIDecl->getIdentifier(); |
| 916 | } |
| 917 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 918 | // If there was a forward declaration with type parameters, check |
| 919 | // for consistency. |
| 920 | if (PrevIDecl) { |
| 921 | if (ObjCTypeParamList *prevTypeParamList = PrevIDecl->getTypeParamList()) { |
| 922 | if (typeParamList) { |
| 923 | // Both have type parameter lists; check for consistency. |
| 924 | if (checkTypeParamListConsistency(*this, prevTypeParamList, |
| 925 | typeParamList, |
| 926 | TypeParamListContext::Definition)) { |
| 927 | typeParamList = nullptr; |
| 928 | } |
| 929 | } else { |
| 930 | Diag(ClassLoc, diag::err_objc_parameterized_forward_class_first) |
| 931 | << ClassName; |
| 932 | Diag(prevTypeParamList->getLAngleLoc(), diag::note_previous_decl) |
| 933 | << ClassName; |
| 934 | |
| 935 | // Clone the type parameter list. |
| 936 | SmallVector<ObjCTypeParamDecl *, 4> clonedTypeParams; |
| 937 | for (auto typeParam : *prevTypeParamList) { |
| 938 | clonedTypeParams.push_back( |
| 939 | ObjCTypeParamDecl::Create( |
| 940 | Context, |
| 941 | CurContext, |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame^] | 942 | typeParam->getVariance(), |
| 943 | SourceLocation(), |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 944 | typeParam->getIndex(), |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 945 | SourceLocation(), |
| 946 | typeParam->getIdentifier(), |
| 947 | SourceLocation(), |
| 948 | Context.getTrivialTypeSourceInfo(typeParam->getUnderlyingType()))); |
| 949 | } |
| 950 | |
| 951 | typeParamList = ObjCTypeParamList::create(Context, |
| 952 | SourceLocation(), |
| 953 | clonedTypeParams, |
| 954 | SourceLocation()); |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 959 | ObjCInterfaceDecl *IDecl |
| 960 | = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 961 | typeParamList, PrevIDecl, ClassLoc); |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 962 | if (PrevIDecl) { |
| 963 | // Class already seen. Was it a definition? |
| 964 | if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) { |
| 965 | Diag(AtInterfaceLoc, diag::err_duplicate_class_def) |
| 966 | << PrevIDecl->getDeclName(); |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 967 | Diag(Def->getLocation(), diag::note_previous_definition); |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 968 | IDecl->setInvalidDecl(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 969 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 970 | } |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 971 | |
| 972 | if (AttrList) |
| 973 | ProcessDeclAttributeList(TUScope, IDecl, AttrList); |
| 974 | PushOnScopeChains(IDecl, TUScope); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 976 | // Start the definition of this class. If we're in a redefinition case, there |
| 977 | // 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] | 978 | if (!IDecl->hasDefinition()) |
| 979 | IDecl->startDefinition(); |
| 980 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 981 | if (SuperName) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 982 | // Diagnose availability in the context of the @interface. |
| 983 | ContextRAII SavedContext(*this, IDecl); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 984 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 985 | ActOnSuperClassOfClassInterface(S, AtInterfaceLoc, IDecl, |
| 986 | ClassName, ClassLoc, |
| 987 | SuperName, SuperLoc, SuperTypeArgs, |
| 988 | SuperTypeArgsRange); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 989 | } else { // we have a root class. |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 990 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 991 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 992 | |
Sebastian Redl | e7c1fe6 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 993 | // Check then save referenced protocols. |
Chris Lattner | df59f5a | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 994 | if (NumProtoRefs) { |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 995 | diagnoseUseOfProtocols(*this, IDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
| 996 | NumProtoRefs, ProtoLocs); |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 997 | IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 998 | ProtoLocs, Context); |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 999 | IDecl->setEndOfDefinitionLoc(EndProtoLoc); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1000 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1002 | CheckObjCDeclScope(IDecl); |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1003 | return ActOnObjCContainerStartDefinition(IDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Fariborz Jahanian | b7c5f74 | 2013-09-25 19:36:32 +0000 | [diff] [blame] | 1006 | /// ActOnTypedefedProtocols - this action finds protocol list as part of the |
| 1007 | /// typedef'ed use for a qualified super class and adds them to the list |
| 1008 | /// of the protocols. |
| 1009 | void Sema::ActOnTypedefedProtocols(SmallVectorImpl<Decl *> &ProtocolRefs, |
| 1010 | IdentifierInfo *SuperName, |
| 1011 | SourceLocation SuperLoc) { |
| 1012 | if (!SuperName) |
| 1013 | return; |
| 1014 | NamedDecl* IDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
| 1015 | LookupOrdinaryName); |
| 1016 | if (!IDecl) |
| 1017 | return; |
| 1018 | |
| 1019 | if (const TypedefNameDecl *TDecl = dyn_cast_or_null<TypedefNameDecl>(IDecl)) { |
| 1020 | QualType T = TDecl->getUnderlyingType(); |
| 1021 | if (T->isObjCObjectType()) |
| 1022 | if (const ObjCObjectType *OPT = T->getAs<ObjCObjectType>()) |
Benjamin Kramer | f989042 | 2015-02-17 16:48:30 +0000 | [diff] [blame] | 1023 | ProtocolRefs.append(OPT->qual_begin(), OPT->qual_end()); |
Fariborz Jahanian | b7c5f74 | 2013-09-25 19:36:32 +0000 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | |
Richard Smith | ac4e36d | 2012-08-08 23:32:13 +0000 | [diff] [blame] | 1027 | /// ActOnCompatibilityAlias - this action is called after complete parsing of |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 1028 | /// a \@compatibility_alias declaration. It sets up the alias relationships. |
Richard Smith | ac4e36d | 2012-08-08 23:32:13 +0000 | [diff] [blame] | 1029 | Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc, |
| 1030 | IdentifierInfo *AliasName, |
| 1031 | SourceLocation AliasLocation, |
| 1032 | IdentifierInfo *ClassName, |
| 1033 | SourceLocation ClassLocation) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1034 | // Look for previous declaration of alias name |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1035 | NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation, |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 1036 | LookupOrdinaryName, ForRedeclaration); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1037 | if (ADecl) { |
Eli Friedman | fd6b3f8 | 2013-06-21 01:49:53 +0000 | [diff] [blame] | 1038 | Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; |
Chris Lattner | d068503 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 1039 | Diag(ADecl->getLocation(), diag::note_previous_declaration); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1040 | return nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1041 | } |
| 1042 | // Check for class declaration |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1043 | NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation, |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 1044 | LookupOrdinaryName, ForRedeclaration); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1045 | if (const TypedefNameDecl *TDecl = |
| 1046 | dyn_cast_or_null<TypedefNameDecl>(CDeclU)) { |
Fariborz Jahanian | 17290c3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 1047 | QualType T = TDecl->getUnderlyingType(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1048 | if (T->isObjCObjectType()) { |
| 1049 | if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) { |
Fariborz Jahanian | 17290c3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 1050 | ClassName = IDecl->getIdentifier(); |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1051 | CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation, |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 1052 | LookupOrdinaryName, ForRedeclaration); |
Fariborz Jahanian | 17290c3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 1053 | } |
| 1054 | } |
| 1055 | } |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 1056 | ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1057 | if (!CDecl) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1058 | Diag(ClassLocation, diag::warn_undef_interface) << ClassName; |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 1059 | if (CDeclU) |
Chris Lattner | d068503 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 1060 | Diag(CDeclU->getLocation(), diag::note_previous_declaration); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1061 | return nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1062 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1063 | |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 1064 | // Everything checked out, instantiate a new alias declaration AST. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1065 | ObjCCompatibleAliasDecl *AliasDecl = |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1066 | ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1068 | if (!CheckObjCDeclScope(AliasDecl)) |
Douglas Gregor | 38feed8 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 1069 | PushOnScopeChains(AliasDecl, TUScope); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1070 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1071 | return AliasDecl; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1074 | bool Sema::CheckForwardProtocolDeclarationForCircularDependency( |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1075 | IdentifierInfo *PName, |
| 1076 | SourceLocation &Ploc, SourceLocation PrevLoc, |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1077 | const ObjCList<ObjCProtocolDecl> &PList) { |
| 1078 | |
| 1079 | bool res = false; |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1080 | for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(), |
| 1081 | E = PList.end(); I != E; ++I) { |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1082 | if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(), |
| 1083 | Ploc)) { |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1084 | if (PDecl->getIdentifier() == PName) { |
| 1085 | Diag(Ploc, diag::err_protocol_has_circular_dependency); |
| 1086 | Diag(PrevLoc, diag::note_previous_definition); |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1087 | res = true; |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1088 | } |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 1089 | |
| 1090 | if (!PDecl->hasDefinition()) |
| 1091 | continue; |
| 1092 | |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1093 | if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc, |
| 1094 | PDecl->getLocation(), PDecl->getReferencedProtocols())) |
| 1095 | res = true; |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1096 | } |
| 1097 | } |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1098 | return res; |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1101 | Decl * |
Chris Lattner | 3bbae00 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 1102 | Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc, |
| 1103 | IdentifierInfo *ProtocolName, |
| 1104 | SourceLocation ProtocolLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1105 | Decl * const *ProtoRefs, |
Chris Lattner | 3bbae00 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 1106 | unsigned NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 1107 | const SourceLocation *ProtoLocs, |
Daniel Dunbar | 26e2ab4 | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 1108 | SourceLocation EndProtoLoc, |
| 1109 | AttributeList *AttrList) { |
Fariborz Jahanian | cadf7c5 | 2011-05-12 22:04:39 +0000 | [diff] [blame] | 1110 | bool err = false; |
Daniel Dunbar | 26e2ab4 | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 1111 | // FIXME: Deal with AttrList. |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1112 | assert(ProtocolName && "Missing protocol identifier"); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1113 | ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc, |
| 1114 | ForRedeclaration); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1115 | ObjCProtocolDecl *PDecl = nullptr; |
| 1116 | if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : nullptr) { |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1117 | // If we already have a definition, complain. |
| 1118 | Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName; |
| 1119 | Diag(Def->getLocation(), diag::note_previous_definition); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1120 | |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1121 | // Create a new protocol that is completely distinct from previous |
| 1122 | // declarations, and do not make this protocol available for name lookup. |
| 1123 | // That way, we'll end up completely ignoring the duplicate. |
| 1124 | // FIXME: Can we turn this into an error? |
| 1125 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName, |
| 1126 | ProtocolLoc, AtProtoInterfaceLoc, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1127 | /*PrevDecl=*/nullptr); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1128 | PDecl->startDefinition(); |
| 1129 | } else { |
| 1130 | if (PrevDecl) { |
| 1131 | // Check for circular dependencies among protocol declarations. This can |
| 1132 | // only happen if this protocol was forward-declared. |
Argyrios Kyrtzidis | 95dfc12 | 2011-11-13 22:08:30 +0000 | [diff] [blame] | 1133 | ObjCList<ObjCProtocolDecl> PList; |
| 1134 | PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context); |
| 1135 | err = CheckForwardProtocolDeclarationForCircularDependency( |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1136 | ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList); |
Argyrios Kyrtzidis | 95dfc12 | 2011-11-13 22:08:30 +0000 | [diff] [blame] | 1137 | } |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1138 | |
| 1139 | // Create the new declaration. |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 1140 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName, |
Argyrios Kyrtzidis | 1f4bee5 | 2011-10-17 19:48:06 +0000 | [diff] [blame] | 1141 | ProtocolLoc, AtProtoInterfaceLoc, |
Douglas Gregor | 05a1f4d | 2012-01-01 22:06:18 +0000 | [diff] [blame] | 1142 | /*PrevDecl=*/PrevDecl); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1143 | |
Douglas Gregor | de9f17e | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 1144 | PushOnScopeChains(PDecl, TUScope); |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 1145 | PDecl->startDefinition(); |
Chris Lattner | f87ca0a | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 1146 | } |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 1147 | |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1148 | if (AttrList) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1149 | ProcessDeclAttributeList(TUScope, PDecl, AttrList); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1150 | |
| 1151 | // Merge attributes from previous declarations. |
| 1152 | if (PrevDecl) |
| 1153 | mergeDeclAttributes(PDecl, PrevDecl); |
| 1154 | |
Fariborz Jahanian | cadf7c5 | 2011-05-12 22:04:39 +0000 | [diff] [blame] | 1155 | if (!err && NumProtoRefs ) { |
Chris Lattner | acc04a9 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 1156 | /// Check then save referenced protocols. |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1157 | diagnoseUseOfProtocols(*this, PDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
| 1158 | NumProtoRefs, ProtoLocs); |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 1159 | PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 1160 | ProtoLocs, Context); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1161 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1162 | |
| 1163 | CheckObjCDeclScope(PDecl); |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1164 | return ActOnObjCContainerStartDefinition(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1167 | static bool NestedProtocolHasNoDefinition(ObjCProtocolDecl *PDecl, |
| 1168 | ObjCProtocolDecl *&UndefinedProtocol) { |
| 1169 | if (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()) { |
| 1170 | UndefinedProtocol = PDecl; |
| 1171 | return true; |
| 1172 | } |
| 1173 | |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 1174 | for (auto *PI : PDecl->protocols()) |
| 1175 | if (NestedProtocolHasNoDefinition(PI, UndefinedProtocol)) { |
| 1176 | UndefinedProtocol = PI; |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1177 | return true; |
| 1178 | } |
| 1179 | return false; |
| 1180 | } |
| 1181 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1182 | /// FindProtocolDeclaration - This routine looks up protocols and |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1183 | /// issues an error if they are not declared. It returns list of |
| 1184 | /// protocol declarations in its 'Protocols' argument. |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1185 | void |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1186 | Sema::FindProtocolDeclaration(bool WarnOnDeclarations, bool ForObjCContainer, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1187 | const IdentifierLocPair *ProtocolId, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1188 | unsigned NumProtocols, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1189 | SmallVectorImpl<Decl *> &Protocols) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1190 | for (unsigned i = 0; i != NumProtocols; ++i) { |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1191 | ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first, |
| 1192 | ProtocolId[i].second); |
Chris Lattner | 9c1842b | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 1193 | if (!PDecl) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1194 | TypoCorrection Corrected = CorrectTypo( |
| 1195 | DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second), |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 1196 | LookupObjCProtocolName, TUScope, nullptr, |
| 1197 | llvm::make_unique<DeclFilterCCC<ObjCProtocolDecl>>(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1198 | CTK_ErrorRecovery); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1199 | if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) |
| 1200 | diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest) |
| 1201 | << ProtocolId[i].first); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | if (!PDecl) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1205 | Diag(ProtocolId[i].second, diag::err_undeclared_protocol) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1206 | << ProtocolId[i].first; |
Chris Lattner | 9c1842b | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 1207 | continue; |
| 1208 | } |
Fariborz Jahanian | ada44a2 | 2013-04-09 17:52:29 +0000 | [diff] [blame] | 1209 | // If this is a forward protocol declaration, get its definition. |
| 1210 | if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition()) |
| 1211 | PDecl = PDecl->getDefinition(); |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1212 | |
| 1213 | // For an objc container, delay protocol reference checking until after we |
| 1214 | // can set the objc decl as the availability context, otherwise check now. |
| 1215 | if (!ForObjCContainer) { |
| 1216 | (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second); |
| 1217 | } |
Chris Lattner | 9c1842b | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 1218 | |
| 1219 | // If this is a forward declaration and we are supposed to warn in this |
| 1220 | // case, do it. |
Douglas Gregor | eed4979 | 2013-01-17 00:38:46 +0000 | [diff] [blame] | 1221 | // FIXME: Recover nicely in the hidden case. |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1222 | ObjCProtocolDecl *UndefinedProtocol; |
| 1223 | |
Douglas Gregor | eed4979 | 2013-01-17 00:38:46 +0000 | [diff] [blame] | 1224 | if (WarnOnDeclarations && |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1225 | NestedProtocolHasNoDefinition(PDecl, UndefinedProtocol)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1226 | Diag(ProtocolId[i].second, diag::warn_undef_protocolref) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1227 | << ProtocolId[i].first; |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1228 | Diag(UndefinedProtocol->getLocation(), diag::note_protocol_decl_undefined) |
| 1229 | << UndefinedProtocol; |
| 1230 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1231 | Protocols.push_back(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1235 | // Callback to only accept typo corrections that are either |
| 1236 | // Objective-C protocols or valid Objective-C type arguments. |
| 1237 | class ObjCTypeArgOrProtocolValidatorCCC : public CorrectionCandidateCallback { |
| 1238 | ASTContext &Context; |
| 1239 | Sema::LookupNameKind LookupKind; |
| 1240 | public: |
| 1241 | ObjCTypeArgOrProtocolValidatorCCC(ASTContext &context, |
| 1242 | Sema::LookupNameKind lookupKind) |
| 1243 | : Context(context), LookupKind(lookupKind) { } |
| 1244 | |
| 1245 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
| 1246 | // If we're allowed to find protocols and we have a protocol, accept it. |
| 1247 | if (LookupKind != Sema::LookupOrdinaryName) { |
| 1248 | if (candidate.getCorrectionDeclAs<ObjCProtocolDecl>()) |
| 1249 | return true; |
| 1250 | } |
| 1251 | |
| 1252 | // If we're allowed to find type names and we have one, accept it. |
| 1253 | if (LookupKind != Sema::LookupObjCProtocolName) { |
| 1254 | // If we have a type declaration, we might accept this result. |
| 1255 | if (auto typeDecl = candidate.getCorrectionDeclAs<TypeDecl>()) { |
| 1256 | // If we found a tag declaration outside of C++, skip it. This |
| 1257 | // can happy because we look for any name when there is no |
| 1258 | // bias to protocol or type names. |
| 1259 | if (isa<RecordDecl>(typeDecl) && !Context.getLangOpts().CPlusPlus) |
| 1260 | return false; |
| 1261 | |
| 1262 | // Make sure the type is something we would accept as a type |
| 1263 | // argument. |
| 1264 | auto type = Context.getTypeDeclType(typeDecl); |
| 1265 | if (type->isObjCObjectPointerType() || |
| 1266 | type->isBlockPointerType() || |
| 1267 | type->isDependentType() || |
| 1268 | type->isObjCObjectType()) |
| 1269 | return true; |
| 1270 | |
| 1271 | return false; |
| 1272 | } |
| 1273 | |
| 1274 | // If we have an Objective-C class type, accept it; there will |
| 1275 | // be another fix to add the '*'. |
| 1276 | if (candidate.getCorrectionDeclAs<ObjCInterfaceDecl>()) |
| 1277 | return true; |
| 1278 | |
| 1279 | return false; |
| 1280 | } |
| 1281 | |
| 1282 | return false; |
| 1283 | } |
| 1284 | }; |
| 1285 | |
| 1286 | void Sema::actOnObjCTypeArgsOrProtocolQualifiers( |
| 1287 | Scope *S, |
Douglas Gregor | 10dc9d8 | 2015-07-07 03:58:28 +0000 | [diff] [blame] | 1288 | ParsedType baseType, |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1289 | SourceLocation lAngleLoc, |
| 1290 | ArrayRef<IdentifierInfo *> identifiers, |
| 1291 | ArrayRef<SourceLocation> identifierLocs, |
| 1292 | SourceLocation rAngleLoc, |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1293 | SourceLocation &typeArgsLAngleLoc, |
| 1294 | SmallVectorImpl<ParsedType> &typeArgs, |
| 1295 | SourceLocation &typeArgsRAngleLoc, |
| 1296 | SourceLocation &protocolLAngleLoc, |
| 1297 | SmallVectorImpl<Decl *> &protocols, |
| 1298 | SourceLocation &protocolRAngleLoc, |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1299 | bool warnOnIncompleteProtocols) { |
| 1300 | // Local function that updates the declaration specifiers with |
| 1301 | // protocol information. |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1302 | unsigned numProtocolsResolved = 0; |
| 1303 | auto resolvedAsProtocols = [&] { |
| 1304 | assert(numProtocolsResolved == identifiers.size() && "Unresolved protocols"); |
| 1305 | |
Douglas Gregor | 10dc9d8 | 2015-07-07 03:58:28 +0000 | [diff] [blame] | 1306 | // Determine whether the base type is a parameterized class, in |
| 1307 | // which case we want to warn about typos such as |
| 1308 | // "NSArray<NSObject>" (that should be NSArray<NSObject *>). |
| 1309 | ObjCInterfaceDecl *baseClass = nullptr; |
| 1310 | QualType base = GetTypeFromParser(baseType, nullptr); |
| 1311 | bool allAreTypeNames = false; |
| 1312 | SourceLocation firstClassNameLoc; |
| 1313 | if (!base.isNull()) { |
| 1314 | if (const auto *objcObjectType = base->getAs<ObjCObjectType>()) { |
| 1315 | baseClass = objcObjectType->getInterface(); |
| 1316 | if (baseClass) { |
| 1317 | if (auto typeParams = baseClass->getTypeParamList()) { |
| 1318 | if (typeParams->size() == numProtocolsResolved) { |
| 1319 | // Note that we should be looking for type names, too. |
| 1320 | allAreTypeNames = true; |
| 1321 | } |
| 1322 | } |
| 1323 | } |
| 1324 | } |
| 1325 | } |
| 1326 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1327 | for (unsigned i = 0, n = protocols.size(); i != n; ++i) { |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1328 | ObjCProtocolDecl *&proto |
| 1329 | = reinterpret_cast<ObjCProtocolDecl *&>(protocols[i]); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1330 | // For an objc container, delay protocol reference checking until after we |
| 1331 | // can set the objc decl as the availability context, otherwise check now. |
| 1332 | if (!warnOnIncompleteProtocols) { |
| 1333 | (void)DiagnoseUseOfDecl(proto, identifierLocs[i]); |
| 1334 | } |
| 1335 | |
| 1336 | // If this is a forward protocol declaration, get its definition. |
| 1337 | if (!proto->isThisDeclarationADefinition() && proto->getDefinition()) |
| 1338 | proto = proto->getDefinition(); |
| 1339 | |
| 1340 | // If this is a forward declaration and we are supposed to warn in this |
| 1341 | // case, do it. |
| 1342 | // FIXME: Recover nicely in the hidden case. |
| 1343 | ObjCProtocolDecl *forwardDecl = nullptr; |
| 1344 | if (warnOnIncompleteProtocols && |
| 1345 | NestedProtocolHasNoDefinition(proto, forwardDecl)) { |
| 1346 | Diag(identifierLocs[i], diag::warn_undef_protocolref) |
| 1347 | << proto->getDeclName(); |
| 1348 | Diag(forwardDecl->getLocation(), diag::note_protocol_decl_undefined) |
| 1349 | << forwardDecl; |
| 1350 | } |
Douglas Gregor | 10dc9d8 | 2015-07-07 03:58:28 +0000 | [diff] [blame] | 1351 | |
| 1352 | // If everything this far has been a type name (and we care |
| 1353 | // about such things), check whether this name refers to a type |
| 1354 | // as well. |
| 1355 | if (allAreTypeNames) { |
| 1356 | if (auto *decl = LookupSingleName(S, identifiers[i], identifierLocs[i], |
| 1357 | LookupOrdinaryName)) { |
| 1358 | if (isa<ObjCInterfaceDecl>(decl)) { |
| 1359 | if (firstClassNameLoc.isInvalid()) |
| 1360 | firstClassNameLoc = identifierLocs[i]; |
| 1361 | } else if (!isa<TypeDecl>(decl)) { |
| 1362 | // Not a type. |
| 1363 | allAreTypeNames = false; |
| 1364 | } |
| 1365 | } else { |
| 1366 | allAreTypeNames = false; |
| 1367 | } |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | // All of the protocols listed also have type names, and at least |
| 1372 | // one is an Objective-C class name. Check whether all of the |
| 1373 | // protocol conformances are declared by the base class itself, in |
| 1374 | // which case we warn. |
| 1375 | if (allAreTypeNames && firstClassNameLoc.isValid()) { |
| 1376 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> knownProtocols; |
| 1377 | Context.CollectInheritedProtocols(baseClass, knownProtocols); |
| 1378 | bool allProtocolsDeclared = true; |
| 1379 | for (auto proto : protocols) { |
| 1380 | if (knownProtocols.count(static_cast<ObjCProtocolDecl *>(proto)) == 0) { |
| 1381 | allProtocolsDeclared = false; |
| 1382 | break; |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | if (allProtocolsDeclared) { |
| 1387 | Diag(firstClassNameLoc, diag::warn_objc_redundant_qualified_class_type) |
| 1388 | << baseClass->getDeclName() << SourceRange(lAngleLoc, rAngleLoc) |
| 1389 | << FixItHint::CreateInsertion( |
| 1390 | PP.getLocForEndOfToken(firstClassNameLoc), " *"); |
| 1391 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1394 | protocolLAngleLoc = lAngleLoc; |
| 1395 | protocolRAngleLoc = rAngleLoc; |
| 1396 | assert(protocols.size() == identifierLocs.size()); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1397 | }; |
| 1398 | |
| 1399 | // Attempt to resolve all of the identifiers as protocols. |
| 1400 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1401 | ObjCProtocolDecl *proto = LookupProtocol(identifiers[i], identifierLocs[i]); |
| 1402 | protocols.push_back(proto); |
| 1403 | if (proto) |
| 1404 | ++numProtocolsResolved; |
| 1405 | } |
| 1406 | |
| 1407 | // If all of the names were protocols, these were protocol qualifiers. |
| 1408 | if (numProtocolsResolved == identifiers.size()) |
| 1409 | return resolvedAsProtocols(); |
| 1410 | |
| 1411 | // Attempt to resolve all of the identifiers as type names or |
| 1412 | // Objective-C class names. The latter is technically ill-formed, |
| 1413 | // but is probably something like \c NSArray<NSView *> missing the |
| 1414 | // \c*. |
| 1415 | typedef llvm::PointerUnion<TypeDecl *, ObjCInterfaceDecl *> TypeOrClassDecl; |
| 1416 | SmallVector<TypeOrClassDecl, 4> typeDecls; |
| 1417 | unsigned numTypeDeclsResolved = 0; |
| 1418 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1419 | NamedDecl *decl = LookupSingleName(S, identifiers[i], identifierLocs[i], |
| 1420 | LookupOrdinaryName); |
| 1421 | if (!decl) { |
| 1422 | typeDecls.push_back(TypeOrClassDecl()); |
| 1423 | continue; |
| 1424 | } |
| 1425 | |
| 1426 | if (auto typeDecl = dyn_cast<TypeDecl>(decl)) { |
| 1427 | typeDecls.push_back(typeDecl); |
| 1428 | ++numTypeDeclsResolved; |
| 1429 | continue; |
| 1430 | } |
| 1431 | |
| 1432 | if (auto objcClass = dyn_cast<ObjCInterfaceDecl>(decl)) { |
| 1433 | typeDecls.push_back(objcClass); |
| 1434 | ++numTypeDeclsResolved; |
| 1435 | continue; |
| 1436 | } |
| 1437 | |
| 1438 | typeDecls.push_back(TypeOrClassDecl()); |
| 1439 | } |
| 1440 | |
| 1441 | AttributeFactory attrFactory; |
| 1442 | |
| 1443 | // Local function that forms a reference to the given type or |
| 1444 | // Objective-C class declaration. |
| 1445 | auto resolveTypeReference = [&](TypeOrClassDecl typeDecl, SourceLocation loc) |
| 1446 | -> TypeResult { |
| 1447 | // Form declaration specifiers. They simply refer to the type. |
| 1448 | DeclSpec DS(attrFactory); |
| 1449 | const char* prevSpec; // unused |
| 1450 | unsigned diagID; // unused |
| 1451 | QualType type; |
| 1452 | if (auto *actualTypeDecl = typeDecl.dyn_cast<TypeDecl *>()) |
| 1453 | type = Context.getTypeDeclType(actualTypeDecl); |
| 1454 | else |
| 1455 | type = Context.getObjCInterfaceType(typeDecl.get<ObjCInterfaceDecl *>()); |
| 1456 | TypeSourceInfo *parsedTSInfo = Context.getTrivialTypeSourceInfo(type, loc); |
| 1457 | ParsedType parsedType = CreateParsedType(type, parsedTSInfo); |
| 1458 | DS.SetTypeSpecType(DeclSpec::TST_typename, loc, prevSpec, diagID, |
| 1459 | parsedType, Context.getPrintingPolicy()); |
| 1460 | // Use the identifier location for the type source range. |
| 1461 | DS.SetRangeStart(loc); |
| 1462 | DS.SetRangeEnd(loc); |
| 1463 | |
| 1464 | // Form the declarator. |
| 1465 | Declarator D(DS, Declarator::TypeNameContext); |
| 1466 | |
| 1467 | // If we have a typedef of an Objective-C class type that is missing a '*', |
| 1468 | // add the '*'. |
| 1469 | if (type->getAs<ObjCInterfaceType>()) { |
| 1470 | SourceLocation starLoc = PP.getLocForEndOfToken(loc); |
| 1471 | ParsedAttributes parsedAttrs(attrFactory); |
| 1472 | D.AddTypeInfo(DeclaratorChunk::getPointer(/*typeQuals=*/0, starLoc, |
| 1473 | SourceLocation(), |
| 1474 | SourceLocation(), |
| 1475 | SourceLocation(), |
| 1476 | SourceLocation()), |
| 1477 | parsedAttrs, |
| 1478 | starLoc); |
| 1479 | |
| 1480 | // Diagnose the missing '*'. |
| 1481 | Diag(loc, diag::err_objc_type_arg_missing_star) |
| 1482 | << type |
| 1483 | << FixItHint::CreateInsertion(starLoc, " *"); |
| 1484 | } |
| 1485 | |
| 1486 | // Convert this to a type. |
| 1487 | return ActOnTypeName(S, D); |
| 1488 | }; |
| 1489 | |
| 1490 | // Local function that updates the declaration specifiers with |
| 1491 | // type argument information. |
| 1492 | auto resolvedAsTypeDecls = [&] { |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1493 | // We did not resolve these as protocols. |
| 1494 | protocols.clear(); |
| 1495 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1496 | assert(numTypeDeclsResolved == identifiers.size() && "Unresolved type decl"); |
| 1497 | // Map type declarations to type arguments. |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1498 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1499 | // Map type reference to a type. |
| 1500 | TypeResult type = resolveTypeReference(typeDecls[i], identifierLocs[i]); |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1501 | if (!type.isUsable()) { |
| 1502 | typeArgs.clear(); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1503 | return; |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1504 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1505 | |
| 1506 | typeArgs.push_back(type.get()); |
| 1507 | } |
| 1508 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1509 | typeArgsLAngleLoc = lAngleLoc; |
| 1510 | typeArgsRAngleLoc = rAngleLoc; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1511 | }; |
| 1512 | |
| 1513 | // If all of the identifiers can be resolved as type names or |
| 1514 | // Objective-C class names, we have type arguments. |
| 1515 | if (numTypeDeclsResolved == identifiers.size()) |
| 1516 | return resolvedAsTypeDecls(); |
| 1517 | |
| 1518 | // Error recovery: some names weren't found, or we have a mix of |
| 1519 | // type and protocol names. Go resolve all of the unresolved names |
| 1520 | // and complain if we can't find a consistent answer. |
| 1521 | LookupNameKind lookupKind = LookupAnyName; |
| 1522 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1523 | // If we already have a protocol or type. Check whether it is the |
| 1524 | // right thing. |
| 1525 | if (protocols[i] || typeDecls[i]) { |
| 1526 | // If we haven't figured out whether we want types or protocols |
| 1527 | // yet, try to figure it out from this name. |
| 1528 | if (lookupKind == LookupAnyName) { |
| 1529 | // If this name refers to both a protocol and a type (e.g., \c |
| 1530 | // NSObject), don't conclude anything yet. |
| 1531 | if (protocols[i] && typeDecls[i]) |
| 1532 | continue; |
| 1533 | |
| 1534 | // Otherwise, let this name decide whether we'll be correcting |
| 1535 | // toward types or protocols. |
| 1536 | lookupKind = protocols[i] ? LookupObjCProtocolName |
| 1537 | : LookupOrdinaryName; |
| 1538 | continue; |
| 1539 | } |
| 1540 | |
| 1541 | // If we want protocols and we have a protocol, there's nothing |
| 1542 | // more to do. |
| 1543 | if (lookupKind == LookupObjCProtocolName && protocols[i]) |
| 1544 | continue; |
| 1545 | |
| 1546 | // If we want types and we have a type declaration, there's |
| 1547 | // nothing more to do. |
| 1548 | if (lookupKind == LookupOrdinaryName && typeDecls[i]) |
| 1549 | continue; |
| 1550 | |
| 1551 | // We have a conflict: some names refer to protocols and others |
| 1552 | // refer to types. |
| 1553 | Diag(identifierLocs[i], diag::err_objc_type_args_and_protocols) |
| 1554 | << (protocols[i] != nullptr) |
| 1555 | << identifiers[i] |
| 1556 | << identifiers[0] |
| 1557 | << SourceRange(identifierLocs[0]); |
| 1558 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1559 | protocols.clear(); |
| 1560 | typeArgs.clear(); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1561 | return; |
| 1562 | } |
| 1563 | |
| 1564 | // Perform typo correction on the name. |
| 1565 | TypoCorrection corrected = CorrectTypo( |
| 1566 | DeclarationNameInfo(identifiers[i], identifierLocs[i]), lookupKind, S, |
| 1567 | nullptr, |
| 1568 | llvm::make_unique<ObjCTypeArgOrProtocolValidatorCCC>(Context, |
| 1569 | lookupKind), |
| 1570 | CTK_ErrorRecovery); |
| 1571 | if (corrected) { |
| 1572 | // Did we find a protocol? |
| 1573 | if (auto proto = corrected.getCorrectionDeclAs<ObjCProtocolDecl>()) { |
| 1574 | diagnoseTypo(corrected, |
| 1575 | PDiag(diag::err_undeclared_protocol_suggest) |
| 1576 | << identifiers[i]); |
| 1577 | lookupKind = LookupObjCProtocolName; |
| 1578 | protocols[i] = proto; |
| 1579 | ++numProtocolsResolved; |
| 1580 | continue; |
| 1581 | } |
| 1582 | |
| 1583 | // Did we find a type? |
| 1584 | if (auto typeDecl = corrected.getCorrectionDeclAs<TypeDecl>()) { |
| 1585 | diagnoseTypo(corrected, |
| 1586 | PDiag(diag::err_unknown_typename_suggest) |
| 1587 | << identifiers[i]); |
| 1588 | lookupKind = LookupOrdinaryName; |
| 1589 | typeDecls[i] = typeDecl; |
| 1590 | ++numTypeDeclsResolved; |
| 1591 | continue; |
| 1592 | } |
| 1593 | |
| 1594 | // Did we find an Objective-C class? |
| 1595 | if (auto objcClass = corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
| 1596 | diagnoseTypo(corrected, |
| 1597 | PDiag(diag::err_unknown_type_or_class_name_suggest) |
| 1598 | << identifiers[i] << true); |
| 1599 | lookupKind = LookupOrdinaryName; |
| 1600 | typeDecls[i] = objcClass; |
| 1601 | ++numTypeDeclsResolved; |
| 1602 | continue; |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | // We couldn't find anything. |
| 1607 | Diag(identifierLocs[i], |
| 1608 | (lookupKind == LookupAnyName ? diag::err_objc_type_arg_missing |
| 1609 | : lookupKind == LookupObjCProtocolName ? diag::err_undeclared_protocol |
| 1610 | : diag::err_unknown_typename)) |
| 1611 | << identifiers[i]; |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1612 | protocols.clear(); |
| 1613 | typeArgs.clear(); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1614 | return; |
| 1615 | } |
| 1616 | |
| 1617 | // If all of the names were (corrected to) protocols, these were |
| 1618 | // protocol qualifiers. |
| 1619 | if (numProtocolsResolved == identifiers.size()) |
| 1620 | return resolvedAsProtocols(); |
| 1621 | |
| 1622 | // Otherwise, all of the names were (corrected to) types. |
| 1623 | assert(numTypeDeclsResolved == identifiers.size() && "Not all types?"); |
| 1624 | return resolvedAsTypeDecls(); |
| 1625 | } |
| 1626 | |
Fariborz Jahanian | abf63e7b | 2009-03-02 19:06:08 +0000 | [diff] [blame] | 1627 | /// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1628 | /// a class method in its extension. |
| 1629 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1630 | void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1631 | ObjCInterfaceDecl *ID) { |
| 1632 | if (!ID) |
| 1633 | return; // Possibly due to previous error |
| 1634 | |
| 1635 | llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap; |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 1636 | for (auto *MD : ID->methods()) |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1637 | MethodMap[MD->getSelector()] = MD; |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1638 | |
| 1639 | if (MethodMap.empty()) |
| 1640 | return; |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 1641 | for (const auto *Method : CAT->methods()) { |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1642 | const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()]; |
Fariborz Jahanian | 83d674e | 2014-03-17 17:46:10 +0000 | [diff] [blame] | 1643 | if (PrevMethod && |
| 1644 | (PrevMethod->isInstanceMethod() == Method->isInstanceMethod()) && |
| 1645 | !MatchTwoMethodDeclarations(Method, PrevMethod)) { |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1646 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
| 1647 | << Method->getDeclName(); |
| 1648 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 1649 | } |
| 1650 | } |
| 1651 | } |
| 1652 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 1653 | /// ActOnForwardProtocolDeclaration - Handle \@protocol foo; |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1654 | Sema::DeclGroupPtrTy |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1655 | Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1656 | const IdentifierLocPair *IdentList, |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1657 | unsigned NumElts, |
| 1658 | AttributeList *attrList) { |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1659 | SmallVector<Decl *, 8> DeclsInGroup; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1660 | for (unsigned i = 0; i != NumElts; ++i) { |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1661 | IdentifierInfo *Ident = IdentList[i].first; |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1662 | ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second, |
| 1663 | ForRedeclaration); |
| 1664 | ObjCProtocolDecl *PDecl |
| 1665 | = ObjCProtocolDecl::Create(Context, CurContext, Ident, |
| 1666 | IdentList[i].second, AtProtocolLoc, |
Douglas Gregor | 05a1f4d | 2012-01-01 22:06:18 +0000 | [diff] [blame] | 1667 | PrevDecl); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1668 | |
| 1669 | PushOnScopeChains(PDecl, TUScope); |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1670 | CheckObjCDeclScope(PDecl); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1671 | |
Douglas Gregor | 42ff1bb | 2012-01-01 20:33:24 +0000 | [diff] [blame] | 1672 | if (attrList) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1673 | ProcessDeclAttributeList(TUScope, PDecl, attrList); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1674 | |
| 1675 | if (PrevDecl) |
| 1676 | mergeDeclAttributes(PDecl, PrevDecl); |
| 1677 | |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1678 | DeclsInGroup.push_back(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1679 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1680 | |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1681 | return BuildDeclaratorGroup(DeclsInGroup, false); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1684 | Decl *Sema:: |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1685 | ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc, |
| 1686 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1687 | ObjCTypeParamList *typeParamList, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1688 | IdentifierInfo *CategoryName, |
| 1689 | SourceLocation CategoryLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1690 | Decl * const *ProtoRefs, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1691 | unsigned NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 1692 | const SourceLocation *ProtoLocs, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1693 | SourceLocation EndProtoLoc) { |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1694 | ObjCCategoryDecl *CDecl; |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1695 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1696 | |
| 1697 | /// Check that class of this category is already completely declared. |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1698 | |
| 1699 | if (!IDecl |
| 1700 | || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1701 | diag::err_category_forward_interface, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1702 | CategoryName == nullptr)) { |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1703 | // Create an invalid ObjCCategoryDecl to serve as context for |
| 1704 | // the enclosing method declarations. We mark the decl invalid |
| 1705 | // to make it clear that this isn't a valid AST. |
| 1706 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1707 | ClassLoc, CategoryLoc, CategoryName, |
| 1708 | IDecl, typeParamList); |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1709 | CDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | b15def2 | 2012-03-12 18:34:26 +0000 | [diff] [blame] | 1710 | CurContext->addDecl(CDecl); |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1711 | |
| 1712 | if (!IDecl) |
| 1713 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1714 | return ActOnObjCContainerStartDefinition(CDecl); |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1717 | if (!CategoryName && IDecl->getImplementation()) { |
| 1718 | Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName; |
| 1719 | Diag(IDecl->getImplementation()->getLocation(), |
| 1720 | diag::note_implementation_declared); |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1723 | if (CategoryName) { |
| 1724 | /// Check for duplicate interface declaration for this category |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1725 | if (ObjCCategoryDecl *Previous |
| 1726 | = IDecl->FindCategoryDeclaration(CategoryName)) { |
| 1727 | // Class extensions can be declared multiple times, categories cannot. |
| 1728 | Diag(CategoryLoc, diag::warn_dup_category_def) |
| 1729 | << ClassName << CategoryName; |
| 1730 | Diag(Previous->getLocation(), diag::note_previous_definition); |
Chris Lattner | 9018ca8 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 1731 | } |
| 1732 | } |
Chris Lattner | 9018ca8 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 1733 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1734 | // If we have a type parameter list, check it. |
| 1735 | if (typeParamList) { |
| 1736 | if (auto prevTypeParamList = IDecl->getTypeParamList()) { |
| 1737 | if (checkTypeParamListConsistency(*this, prevTypeParamList, typeParamList, |
| 1738 | CategoryName |
| 1739 | ? TypeParamListContext::Category |
| 1740 | : TypeParamListContext::Extension)) |
| 1741 | typeParamList = nullptr; |
| 1742 | } else { |
| 1743 | Diag(typeParamList->getLAngleLoc(), |
| 1744 | diag::err_objc_parameterized_category_nonclass) |
| 1745 | << (CategoryName != nullptr) |
| 1746 | << ClassName |
| 1747 | << typeParamList->getSourceRange(); |
| 1748 | |
| 1749 | typeParamList = nullptr; |
| 1750 | } |
| 1751 | } |
| 1752 | |
Argyrios Kyrtzidis | 3a5094b | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 1753 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1754 | ClassLoc, CategoryLoc, CategoryName, IDecl, |
| 1755 | typeParamList); |
Argyrios Kyrtzidis | 3a5094b | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 1756 | // FIXME: PushOnScopeChains? |
| 1757 | CurContext->addDecl(CDecl); |
| 1758 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1759 | if (NumProtoRefs) { |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1760 | diagnoseUseOfProtocols(*this, CDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
| 1761 | NumProtoRefs, ProtoLocs); |
| 1762 | CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 1763 | ProtoLocs, Context); |
Fariborz Jahanian | 092cd6e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 1764 | // Protocols in the class extension belong to the class. |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1765 | if (CDecl->IsClassExtension()) |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 1766 | IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs, |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1767 | NumProtoRefs, Context); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1768 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1769 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1770 | CheckObjCDeclScope(CDecl); |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1771 | return ActOnObjCContainerStartDefinition(CDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | /// ActOnStartCategoryImplementation - Perform semantic checks on the |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1775 | /// category implementation declaration and build an ObjCCategoryImplDecl |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1776 | /// object. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1777 | Decl *Sema::ActOnStartCategoryImplementation( |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1778 | SourceLocation AtCatImplLoc, |
| 1779 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 1780 | IdentifierInfo *CatName, SourceLocation CatLoc) { |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1781 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1782 | ObjCCategoryDecl *CatIDecl = nullptr; |
Argyrios Kyrtzidis | 4af2cb3 | 2012-03-02 19:14:29 +0000 | [diff] [blame] | 1783 | if (IDecl && IDecl->hasDefinition()) { |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1784 | CatIDecl = IDecl->FindCategoryDeclaration(CatName); |
| 1785 | if (!CatIDecl) { |
| 1786 | // Category @implementation with no corresponding @interface. |
| 1787 | // Create and install one. |
Argyrios Kyrtzidis | 41fc05c | 2011-11-23 20:27:26 +0000 | [diff] [blame] | 1788 | CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc, |
| 1789 | ClassLoc, CatLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1790 | CatName, IDecl, |
| 1791 | /*typeParamList=*/nullptr); |
Argyrios Kyrtzidis | 41fc05c | 2011-11-23 20:27:26 +0000 | [diff] [blame] | 1792 | CatIDecl->setImplicit(); |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1793 | } |
| 1794 | } |
| 1795 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1796 | ObjCCategoryImplDecl *CDecl = |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 1797 | ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl, |
Argyrios Kyrtzidis | 4996f5f | 2011-12-09 00:31:40 +0000 | [diff] [blame] | 1798 | ClassLoc, AtCatImplLoc, CatLoc); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1799 | /// Check that class of this category is already completely declared. |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1800 | if (!IDecl) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1801 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 1802 | CDecl->setInvalidDecl(); |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1803 | } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
| 1804 | diag::err_undef_interface)) { |
| 1805 | CDecl->setInvalidDecl(); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 1806 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1807 | |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1808 | // FIXME: PushOnScopeChains? |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1809 | CurContext->addDecl(CDecl); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1810 | |
Argyrios Kyrtzidis | c281c96 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 1811 | // If the interface is deprecated/unavailable, warn/error about it. |
| 1812 | if (IDecl) |
| 1813 | DiagnoseUseOfDecl(IDecl, ClassLoc); |
| 1814 | |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1815 | /// Check that CatName, category name, is not used in another implementation. |
| 1816 | if (CatIDecl) { |
| 1817 | if (CatIDecl->getImplementation()) { |
| 1818 | Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName |
| 1819 | << CatName; |
| 1820 | Diag(CatIDecl->getImplementation()->getLocation(), |
| 1821 | diag::note_previous_definition); |
Argyrios Kyrtzidis | 0f6d5ca | 2013-05-30 18:53:21 +0000 | [diff] [blame] | 1822 | CDecl->setInvalidDecl(); |
Fariborz Jahanian | d33ab8c | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 1823 | } else { |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1824 | CatIDecl->setImplementation(CDecl); |
Fariborz Jahanian | d33ab8c | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 1825 | // Warn on implementating category of deprecated class under |
| 1826 | // -Wdeprecated-implementations flag. |
Fariborz Jahanian | d724a10 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 1827 | DiagnoseObjCImplementedDeprecations(*this, |
| 1828 | dyn_cast<NamedDecl>(IDecl), |
| 1829 | CDecl->getLocation(), 2); |
Fariborz Jahanian | d33ab8c | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 1830 | } |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1831 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1832 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1833 | CheckObjCDeclScope(CDecl); |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1834 | return ActOnObjCContainerStartDefinition(CDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1837 | Decl *Sema::ActOnStartClassImplementation( |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1838 | SourceLocation AtClassImplLoc, |
| 1839 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1840 | IdentifierInfo *SuperClassname, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1841 | SourceLocation SuperClassLoc) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1842 | ObjCInterfaceDecl *IDecl = nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1843 | // Check for another declaration kind with the same name. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1844 | NamedDecl *PrevDecl |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 1845 | = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName, |
| 1846 | ForRedeclaration); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1847 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1848 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1849 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1850 | } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) { |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 1851 | RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
| 1852 | diag::warn_undef_interface); |
Douglas Gregor | 40f7a00 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 1853 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1854 | // 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] | 1855 | // typos in the class name. |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 1856 | TypoCorrection Corrected = CorrectTypo( |
| 1857 | DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope, |
| 1858 | nullptr, llvm::make_unique<ObjCInterfaceValidatorCCC>(), CTK_NonError); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1859 | if (Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
| 1860 | // Suggest the (potentially) correct interface name. Don't provide a |
| 1861 | // code-modification hint or use the typo name for recovery, because |
| 1862 | // this is just a warning. The program may actually be correct. |
| 1863 | diagnoseTypo(Corrected, |
| 1864 | PDiag(diag::warn_undef_interface_suggest) << ClassName, |
| 1865 | /*ErrorRecovery*/false); |
Douglas Gregor | 40f7a00 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 1866 | } else { |
| 1867 | Diag(ClassLoc, diag::warn_undef_interface) << ClassName; |
| 1868 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1869 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1870 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1871 | // Check that super class name is valid class name |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1872 | ObjCInterfaceDecl *SDecl = nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1873 | if (SuperClassname) { |
| 1874 | // Check if a different kind of symbol declared in this scope. |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1875 | PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc, |
| 1876 | LookupOrdinaryName); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1877 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1878 | Diag(SuperClassLoc, diag::err_redefinition_different_kind) |
| 1879 | << SuperClassname; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1880 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1881 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1882 | SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Argyrios Kyrtzidis | 3b60cff | 2012-03-13 01:09:36 +0000 | [diff] [blame] | 1883 | if (SDecl && !SDecl->hasDefinition()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1884 | SDecl = nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1885 | if (!SDecl) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1886 | Diag(SuperClassLoc, diag::err_undef_superclass) |
| 1887 | << SuperClassname << ClassName; |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 1888 | else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1889 | // This implementation and its interface do not have the same |
| 1890 | // super class. |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1891 | Diag(SuperClassLoc, diag::err_conflicting_super_class) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1892 | << SDecl->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1893 | Diag(SDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1894 | } |
| 1895 | } |
| 1896 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1897 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1898 | if (!IDecl) { |
| 1899 | // Legacy case of @implementation with no corresponding @interface. |
| 1900 | // Build, chain & install the interface decl into the identifier. |
Daniel Dunbar | 73a73f5 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 1901 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1902 | // FIXME: Do we support attributes on the @implementation? If so we should |
| 1903 | // copy them over. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1904 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1905 | ClassName, /*typeParamList=*/nullptr, |
| 1906 | /*PrevDecl=*/nullptr, ClassLoc, |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 1907 | true); |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 1908 | IDecl->startDefinition(); |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 1909 | if (SDecl) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1910 | IDecl->setSuperClass(Context.getTrivialTypeSourceInfo( |
| 1911 | Context.getObjCInterfaceType(SDecl), |
| 1912 | SuperClassLoc)); |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 1913 | IDecl->setEndOfDefinitionLoc(SuperClassLoc); |
| 1914 | } else { |
| 1915 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
| 1916 | } |
| 1917 | |
Douglas Gregor | ac345a3 | 2009-04-24 00:16:12 +0000 | [diff] [blame] | 1918 | PushOnScopeChains(IDecl, TUScope); |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1919 | } else { |
| 1920 | // Mark the interface as being completed, even if it was just as |
| 1921 | // @class ....; |
| 1922 | // declaration; the user cannot reopen it. |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 1923 | if (!IDecl->hasDefinition()) |
| 1924 | IDecl->startDefinition(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1925 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1926 | |
| 1927 | ObjCImplementationDecl* IMPDecl = |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 1928 | ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl, |
Argyrios Kyrtzidis | fac3162 | 2013-05-03 18:05:44 +0000 | [diff] [blame] | 1929 | ClassLoc, AtClassImplLoc, SuperClassLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1930 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1931 | if (CheckObjCDeclScope(IMPDecl)) |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1932 | return ActOnObjCContainerStartDefinition(IMPDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1933 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1934 | // Check that there is no duplicate implementation of this class. |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1935 | if (IDecl->getImplementation()) { |
| 1936 | // FIXME: Don't leak everything! |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1937 | Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; |
Argyrios Kyrtzidis | 43cee935 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 1938 | Diag(IDecl->getImplementation()->getLocation(), |
| 1939 | diag::note_previous_definition); |
Argyrios Kyrtzidis | 0f6d5ca | 2013-05-30 18:53:21 +0000 | [diff] [blame] | 1940 | IMPDecl->setInvalidDecl(); |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1941 | } else { // add it to the list. |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1942 | IDecl->setImplementation(IMPDecl); |
Douglas Gregor | 79947a2 | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 1943 | PushOnScopeChains(IMPDecl, TUScope); |
Fariborz Jahanian | d33ab8c | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 1944 | // Warn on implementating deprecated class under |
| 1945 | // -Wdeprecated-implementations flag. |
Fariborz Jahanian | d724a10 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 1946 | DiagnoseObjCImplementedDeprecations(*this, |
| 1947 | dyn_cast<NamedDecl>(IDecl), |
| 1948 | IMPDecl->getLocation(), 1); |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1949 | } |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1950 | return ActOnObjCContainerStartDefinition(IMPDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Argyrios Kyrtzidis | 2e85c5f | 2012-02-23 21:11:20 +0000 | [diff] [blame] | 1953 | Sema::DeclGroupPtrTy |
| 1954 | Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) { |
| 1955 | SmallVector<Decl *, 64> DeclsInGroup; |
| 1956 | DeclsInGroup.reserve(Decls.size() + 1); |
| 1957 | |
| 1958 | for (unsigned i = 0, e = Decls.size(); i != e; ++i) { |
| 1959 | Decl *Dcl = Decls[i]; |
| 1960 | if (!Dcl) |
| 1961 | continue; |
| 1962 | if (Dcl->getDeclContext()->isFileContext()) |
| 1963 | Dcl->setTopLevelDeclInObjCContainer(); |
| 1964 | DeclsInGroup.push_back(Dcl); |
| 1965 | } |
| 1966 | |
| 1967 | DeclsInGroup.push_back(ObjCImpDecl); |
| 1968 | |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1969 | return BuildDeclaratorGroup(DeclsInGroup, false); |
Argyrios Kyrtzidis | 2e85c5f | 2012-02-23 21:11:20 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1972 | void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, |
| 1973 | ObjCIvarDecl **ivars, unsigned numIvars, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1974 | SourceLocation RBrace) { |
| 1975 | assert(ImpDecl && "missing implementation decl"); |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1976 | ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1977 | if (!IDecl) |
| 1978 | return; |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 1979 | /// Check case of non-existing \@interface decl. |
| 1980 | /// (legacy objective-c \@implementation decl without an \@interface decl). |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1981 | /// Add implementations's ivar to the synthesize class's ivar list. |
Steve Naroff | aac654a | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 1982 | if (IDecl->isImplicitInterfaceDecl()) { |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 1983 | IDecl->setEndOfDefinitionLoc(RBrace); |
Fariborz Jahanian | 20912d6 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 1984 | // Add ivar's to class's DeclContext. |
| 1985 | for (unsigned i = 0, e = numIvars; i != e; ++i) { |
Fariborz Jahanian | c0309cd | 2010-02-17 18:10:54 +0000 | [diff] [blame] | 1986 | ivars[i]->setLexicalDeclContext(ImpDecl); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1987 | IDecl->makeDeclVisibleInContext(ivars[i]); |
Fariborz Jahanian | aef6622 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 1988 | ImpDecl->addDecl(ivars[i]); |
Fariborz Jahanian | 20912d6 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1991 | return; |
| 1992 | } |
| 1993 | // If implementation has empty ivar list, just return. |
| 1994 | if (numIvars == 0) |
| 1995 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1996 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1997 | assert(ivars && "missing @implementation ivars"); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1998 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | 34e3cef | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 1999 | if (ImpDecl->getSuperClass()) |
| 2000 | Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use); |
| 2001 | for (unsigned i = 0; i < numIvars; i++) { |
| 2002 | ObjCIvarDecl* ImplIvar = ivars[i]; |
| 2003 | if (const ObjCIvarDecl *ClsIvar = |
| 2004 | IDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
| 2005 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
| 2006 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
| 2007 | continue; |
| 2008 | } |
Fariborz Jahanian | e23f26b | 2013-06-26 22:10:27 +0000 | [diff] [blame] | 2009 | // Check class extensions (unnamed categories) for duplicate ivars. |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 2010 | for (const auto *CDecl : IDecl->visible_extensions()) { |
Fariborz Jahanian | e23f26b | 2013-06-26 22:10:27 +0000 | [diff] [blame] | 2011 | if (const ObjCIvarDecl *ClsExtIvar = |
| 2012 | CDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
| 2013 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
| 2014 | Diag(ClsExtIvar->getLocation(), diag::note_previous_definition); |
| 2015 | continue; |
| 2016 | } |
| 2017 | } |
Fariborz Jahanian | 34e3cef | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 2018 | // Instance ivar to Implementation's DeclContext. |
| 2019 | ImplIvar->setLexicalDeclContext(ImpDecl); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 2020 | IDecl->makeDeclVisibleInContext(ImplIvar); |
Fariborz Jahanian | 34e3cef | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 2021 | ImpDecl->addDecl(ImplIvar); |
| 2022 | } |
| 2023 | return; |
| 2024 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2025 | // Check interface's Ivar list against those in the implementation. |
| 2026 | // names and types must match. |
| 2027 | // |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2028 | unsigned j = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2029 | ObjCInterfaceDecl::ivar_iterator |
Chris Lattner | 061227a | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 2030 | IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); |
| 2031 | for (; numIvars > 0 && IVI != IVE; ++IVI) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2032 | ObjCIvarDecl* ImplIvar = ivars[j++]; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2033 | ObjCIvarDecl* ClsIvar = *IVI; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2034 | assert (ImplIvar && "missing implementation ivar"); |
| 2035 | assert (ClsIvar && "missing class ivar"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2036 | |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 2037 | // First, make sure the types match. |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2038 | if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2039 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2040 | << ImplIvar->getIdentifier() |
| 2041 | << ImplIvar->getType() << ClsIvar->getType(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2042 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2043 | } else if (ImplIvar->isBitField() && ClsIvar->isBitField() && |
| 2044 | ImplIvar->getBitWidthValue(Context) != |
| 2045 | ClsIvar->getBitWidthValue(Context)) { |
| 2046 | Diag(ImplIvar->getBitWidth()->getLocStart(), |
| 2047 | diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier(); |
| 2048 | Diag(ClsIvar->getBitWidth()->getLocStart(), |
| 2049 | diag::note_previous_definition); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2050 | } |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 2051 | // Make sure the names are identical. |
| 2052 | if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2053 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2054 | << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2055 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2056 | } |
| 2057 | --numIvars; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2058 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2059 | |
Chris Lattner | 0f29d98 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 2060 | if (numIvars > 0) |
Alp Toker | fff0674 | 2013-12-02 03:50:21 +0000 | [diff] [blame] | 2061 | Diag(ivars[j]->getLocation(), diag::err_inconsistent_ivar_count); |
Chris Lattner | 0f29d98 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 2062 | else if (IVI != IVE) |
Alp Toker | fff0674 | 2013-12-02 03:50:21 +0000 | [diff] [blame] | 2063 | Diag(IVI->getLocation(), diag::err_inconsistent_ivar_count); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Ted Kremenek | f87decd | 2013-12-13 05:58:44 +0000 | [diff] [blame] | 2066 | static void WarnUndefinedMethod(Sema &S, SourceLocation ImpLoc, |
| 2067 | ObjCMethodDecl *method, |
| 2068 | bool &IncompleteImpl, |
Ted Kremenek | 2ccf19e | 2013-12-13 05:58:51 +0000 | [diff] [blame] | 2069 | unsigned DiagID, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2070 | NamedDecl *NeededFor = nullptr) { |
Fariborz Jahanian | 9fc39c4 | 2011-06-24 20:31:37 +0000 | [diff] [blame] | 2071 | // No point warning no definition of method which is 'unavailable'. |
Douglas Gregor | c2e3d5c | 2012-12-11 18:53:07 +0000 | [diff] [blame] | 2072 | switch (method->getAvailability()) { |
| 2073 | case AR_Available: |
| 2074 | case AR_Deprecated: |
| 2075 | break; |
| 2076 | |
| 2077 | // Don't warn about unavailable or not-yet-introduced methods. |
| 2078 | case AR_NotYetIntroduced: |
| 2079 | case AR_Unavailable: |
Fariborz Jahanian | 9fc39c4 | 2011-06-24 20:31:37 +0000 | [diff] [blame] | 2080 | return; |
Douglas Gregor | c2e3d5c | 2012-12-11 18:53:07 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
Ted Kremenek | 65d6357 | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 2083 | // FIXME: For now ignore 'IncompleteImpl'. |
| 2084 | // Previously we grouped all unimplemented methods under a single |
| 2085 | // warning, but some users strongly voiced that they would prefer |
| 2086 | // separate warnings. We will give that approach a try, as that |
| 2087 | // matches what we do with protocols. |
Ted Kremenek | 2ccf19e | 2013-12-13 05:58:51 +0000 | [diff] [blame] | 2088 | { |
| 2089 | const Sema::SemaDiagnosticBuilder &B = S.Diag(ImpLoc, DiagID); |
| 2090 | B << method; |
| 2091 | if (NeededFor) |
| 2092 | B << NeededFor; |
| 2093 | } |
Ted Kremenek | 65d6357 | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 2094 | |
| 2095 | // Issue a note to the original declaration. |
| 2096 | SourceLocation MethodLoc = method->getLocStart(); |
| 2097 | if (MethodLoc.isValid()) |
Ted Kremenek | f87decd | 2013-12-13 05:58:44 +0000 | [diff] [blame] | 2098 | S.Diag(MethodLoc, diag::note_method_declared_at) << method; |
Steve Naroff | 15833ed | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2101 | /// Determines if type B can be substituted for type A. Returns true if we can |
| 2102 | /// guarantee that anything that the user will do to an object of type A can |
| 2103 | /// also be done to an object of type B. This is trivially true if the two |
| 2104 | /// types are the same, or if B is a subclass of A. It becomes more complex |
| 2105 | /// in cases where protocols are involved. |
| 2106 | /// |
| 2107 | /// Object types in Objective-C describe the minimum requirements for an |
| 2108 | /// object, rather than providing a complete description of a type. For |
| 2109 | /// example, if A is a subclass of B, then B* may refer to an instance of A. |
| 2110 | /// The principle of substitutability means that we may use an instance of A |
| 2111 | /// anywhere that we may use an instance of B - it will implement all of the |
| 2112 | /// ivars of B and all of the methods of B. |
| 2113 | /// |
| 2114 | /// This substitutability is important when type checking methods, because |
| 2115 | /// the implementation may have stricter type definitions than the interface. |
| 2116 | /// The interface specifies minimum requirements, but the implementation may |
| 2117 | /// have more accurate ones. For example, a method may privately accept |
| 2118 | /// instances of B, but only publish that it accepts instances of A. Any |
| 2119 | /// object passed to it will be type checked against B, and so will implicitly |
| 2120 | /// by a valid A*. Similarly, a method may return a subclass of the class that |
| 2121 | /// it is declared as returning. |
| 2122 | /// |
| 2123 | /// This is most important when considering subclassing. A method in a |
| 2124 | /// subclass must accept any object as an argument that its superclass's |
| 2125 | /// implementation accepts. It may, however, accept a more general type |
| 2126 | /// without breaking substitutability (i.e. you can still use the subclass |
| 2127 | /// anywhere that you can use the superclass, but not vice versa). The |
| 2128 | /// converse requirement applies to return types: the return type for a |
| 2129 | /// subclass method must be a valid object of the kind that the superclass |
| 2130 | /// advertises, but it may be specified more accurately. This avoids the need |
| 2131 | /// for explicit down-casting by callers. |
| 2132 | /// |
| 2133 | /// Note: This is a stricter requirement than for assignment. |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2134 | static bool isObjCTypeSubstitutable(ASTContext &Context, |
| 2135 | const ObjCObjectPointerType *A, |
| 2136 | const ObjCObjectPointerType *B, |
| 2137 | bool rejectId) { |
| 2138 | // Reject a protocol-unqualified id. |
| 2139 | if (rejectId && B->isObjCIdType()) return false; |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2140 | |
| 2141 | // If B is a qualified id, then A must also be a qualified id and it must |
| 2142 | // implement all of the protocols in B. It may not be a qualified class. |
| 2143 | // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a |
| 2144 | // stricter definition so it is not substitutable for id<A>. |
| 2145 | if (B->isObjCQualifiedIdType()) { |
| 2146 | return A->isObjCQualifiedIdType() && |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2147 | Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0), |
| 2148 | QualType(B,0), |
| 2149 | false); |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | /* |
| 2153 | // id is a special type that bypasses type checking completely. We want a |
| 2154 | // warning when it is used in one place but not another. |
| 2155 | if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false; |
| 2156 | |
| 2157 | |
| 2158 | // If B is a qualified id, then A must also be a qualified id (which it isn't |
| 2159 | // if we've got this far) |
| 2160 | if (B->isObjCQualifiedIdType()) return false; |
| 2161 | */ |
| 2162 | |
| 2163 | // Now we know that A and B are (potentially-qualified) class types. The |
| 2164 | // normal rules for assignment apply. |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2165 | return Context.canAssignObjCInterfaces(A, B); |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2168 | static SourceRange getTypeRange(TypeSourceInfo *TSI) { |
| 2169 | return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange()); |
| 2170 | } |
| 2171 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2172 | /// Determine whether two set of Objective-C declaration qualifiers conflict. |
| 2173 | static bool objcModifiersConflict(Decl::ObjCDeclQualifier x, |
| 2174 | Decl::ObjCDeclQualifier y) { |
| 2175 | return (x & ~Decl::OBJC_TQ_CSNullability) != |
| 2176 | (y & ~Decl::OBJC_TQ_CSNullability); |
| 2177 | } |
| 2178 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2179 | static bool CheckMethodOverrideReturn(Sema &S, |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2180 | ObjCMethodDecl *MethodImpl, |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2181 | ObjCMethodDecl *MethodDecl, |
Fariborz Jahanian | 4ceec3f | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 2182 | bool IsProtocolMethodDecl, |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2183 | bool IsOverridingMode, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2184 | bool Warn) { |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2185 | if (IsProtocolMethodDecl && |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2186 | objcModifiersConflict(MethodDecl->getObjCDeclQualifier(), |
| 2187 | MethodImpl->getObjCDeclQualifier())) { |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2188 | if (Warn) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2189 | S.Diag(MethodImpl->getLocation(), |
| 2190 | (IsOverridingMode |
| 2191 | ? diag::warn_conflicting_overriding_ret_type_modifiers |
| 2192 | : diag::warn_conflicting_ret_type_modifiers)) |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2193 | << MethodImpl->getDeclName() |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 2194 | << MethodImpl->getReturnTypeSourceRange(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2195 | S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration) |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 2196 | << MethodDecl->getReturnTypeSourceRange(); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2197 | } |
| 2198 | else |
| 2199 | return false; |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2200 | } |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2201 | if (Warn && IsOverridingMode && |
| 2202 | !isa<ObjCImplementationDecl>(MethodImpl->getDeclContext()) && |
| 2203 | !S.Context.hasSameNullabilityTypeQualifier(MethodImpl->getReturnType(), |
| 2204 | MethodDecl->getReturnType(), |
| 2205 | false)) { |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 2206 | auto nullabilityMethodImpl = |
| 2207 | *MethodImpl->getReturnType()->getNullability(S.Context); |
| 2208 | auto nullabilityMethodDecl = |
| 2209 | *MethodDecl->getReturnType()->getNullability(S.Context); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2210 | S.Diag(MethodImpl->getLocation(), |
| 2211 | diag::warn_conflicting_nullability_attr_overriding_ret_types) |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 2212 | << DiagNullabilityKind( |
| 2213 | nullabilityMethodImpl, |
| 2214 | ((MethodImpl->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2215 | != 0)) |
| 2216 | << DiagNullabilityKind( |
| 2217 | nullabilityMethodDecl, |
| 2218 | ((MethodDecl->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2219 | != 0)); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2220 | S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration); |
| 2221 | } |
| 2222 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2223 | if (S.Context.hasSameUnqualifiedType(MethodImpl->getReturnType(), |
| 2224 | MethodDecl->getReturnType())) |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2225 | return true; |
| 2226 | if (!Warn) |
| 2227 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2228 | |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2229 | unsigned DiagID = |
| 2230 | IsOverridingMode ? diag::warn_conflicting_overriding_ret_types |
| 2231 | : diag::warn_conflicting_ret_types; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2232 | |
| 2233 | // Mismatches between ObjC pointers go into a different warning |
| 2234 | // category, and sometimes they're even completely whitelisted. |
| 2235 | if (const ObjCObjectPointerType *ImplPtrTy = |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2236 | MethodImpl->getReturnType()->getAs<ObjCObjectPointerType>()) { |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2237 | if (const ObjCObjectPointerType *IfacePtrTy = |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2238 | MethodDecl->getReturnType()->getAs<ObjCObjectPointerType>()) { |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2239 | // Allow non-matching return types as long as they don't violate |
| 2240 | // the principle of substitutability. Specifically, we permit |
| 2241 | // return types that are subclasses of the declared return type, |
| 2242 | // or that are more-qualified versions of the declared type. |
| 2243 | if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false)) |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2244 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2245 | |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2246 | DiagID = |
| 2247 | IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types |
| 2248 | : diag::warn_non_covariant_ret_types; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2249 | } |
| 2250 | } |
| 2251 | |
| 2252 | S.Diag(MethodImpl->getLocation(), DiagID) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2253 | << MethodImpl->getDeclName() << MethodDecl->getReturnType() |
| 2254 | << MethodImpl->getReturnType() |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 2255 | << MethodImpl->getReturnTypeSourceRange(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2256 | S.Diag(MethodDecl->getLocation(), IsOverridingMode |
| 2257 | ? diag::note_previous_declaration |
| 2258 | : diag::note_previous_definition) |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 2259 | << MethodDecl->getReturnTypeSourceRange(); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2260 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2263 | static bool CheckMethodOverrideParam(Sema &S, |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2264 | ObjCMethodDecl *MethodImpl, |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2265 | ObjCMethodDecl *MethodDecl, |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2266 | ParmVarDecl *ImplVar, |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2267 | ParmVarDecl *IfaceVar, |
Fariborz Jahanian | 4ceec3f | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 2268 | bool IsProtocolMethodDecl, |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2269 | bool IsOverridingMode, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2270 | bool Warn) { |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2271 | if (IsProtocolMethodDecl && |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2272 | objcModifiersConflict(ImplVar->getObjCDeclQualifier(), |
| 2273 | IfaceVar->getObjCDeclQualifier())) { |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2274 | if (Warn) { |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2275 | if (IsOverridingMode) |
| 2276 | S.Diag(ImplVar->getLocation(), |
| 2277 | diag::warn_conflicting_overriding_param_modifiers) |
| 2278 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
| 2279 | << MethodImpl->getDeclName(); |
| 2280 | else S.Diag(ImplVar->getLocation(), |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2281 | diag::warn_conflicting_param_modifiers) |
| 2282 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2283 | << MethodImpl->getDeclName(); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2284 | S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration) |
| 2285 | << getTypeRange(IfaceVar->getTypeSourceInfo()); |
| 2286 | } |
| 2287 | else |
| 2288 | return false; |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2291 | QualType ImplTy = ImplVar->getType(); |
| 2292 | QualType IfaceTy = IfaceVar->getType(); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2293 | if (Warn && IsOverridingMode && |
| 2294 | !isa<ObjCImplementationDecl>(MethodImpl->getDeclContext()) && |
| 2295 | !S.Context.hasSameNullabilityTypeQualifier(ImplTy, IfaceTy, true)) { |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2296 | S.Diag(ImplVar->getLocation(), |
| 2297 | diag::warn_conflicting_nullability_attr_overriding_param_types) |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 2298 | << DiagNullabilityKind( |
| 2299 | *ImplTy->getNullability(S.Context), |
| 2300 | ((ImplVar->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2301 | != 0)) |
| 2302 | << DiagNullabilityKind( |
| 2303 | *IfaceTy->getNullability(S.Context), |
| 2304 | ((IfaceVar->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2305 | != 0)); |
| 2306 | S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2307 | } |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2308 | if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy)) |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2309 | return true; |
| 2310 | |
| 2311 | if (!Warn) |
| 2312 | return false; |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2313 | unsigned DiagID = |
| 2314 | IsOverridingMode ? diag::warn_conflicting_overriding_param_types |
| 2315 | : diag::warn_conflicting_param_types; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2316 | |
| 2317 | // Mismatches between ObjC pointers go into a different warning |
| 2318 | // category, and sometimes they're even completely whitelisted. |
| 2319 | if (const ObjCObjectPointerType *ImplPtrTy = |
| 2320 | ImplTy->getAs<ObjCObjectPointerType>()) { |
| 2321 | if (const ObjCObjectPointerType *IfacePtrTy = |
| 2322 | IfaceTy->getAs<ObjCObjectPointerType>()) { |
| 2323 | // Allow non-matching argument types as long as they don't |
| 2324 | // violate the principle of substitutability. Specifically, the |
| 2325 | // implementation must accept any objects that the superclass |
| 2326 | // accepts, however it may also accept others. |
| 2327 | if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true)) |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2328 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2329 | |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2330 | DiagID = |
| 2331 | IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types |
| 2332 | : diag::warn_non_contravariant_param_types; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2333 | } |
| 2334 | } |
| 2335 | |
| 2336 | S.Diag(ImplVar->getLocation(), DiagID) |
| 2337 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2338 | << MethodImpl->getDeclName() << IfaceTy << ImplTy; |
| 2339 | S.Diag(IfaceVar->getLocation(), |
| 2340 | (IsOverridingMode ? diag::note_previous_declaration |
| 2341 | : diag::note_previous_definition)) |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2342 | << getTypeRange(IfaceVar->getTypeSourceInfo()); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2343 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2344 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2345 | |
| 2346 | /// In ARC, check whether the conventional meanings of the two methods |
| 2347 | /// match. If they don't, it's a hard error. |
| 2348 | static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl, |
| 2349 | ObjCMethodDecl *decl) { |
| 2350 | ObjCMethodFamily implFamily = impl->getMethodFamily(); |
| 2351 | ObjCMethodFamily declFamily = decl->getMethodFamily(); |
| 2352 | if (implFamily == declFamily) return false; |
| 2353 | |
| 2354 | // Since conventions are sorted by selector, the only possibility is |
| 2355 | // that the types differ enough to cause one selector or the other |
| 2356 | // to fall out of the family. |
| 2357 | assert(implFamily == OMF_None || declFamily == OMF_None); |
| 2358 | |
| 2359 | // No further diagnostics required on invalid declarations. |
| 2360 | if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true; |
| 2361 | |
| 2362 | const ObjCMethodDecl *unmatched = impl; |
| 2363 | ObjCMethodFamily family = declFamily; |
| 2364 | unsigned errorID = diag::err_arc_lost_method_convention; |
| 2365 | unsigned noteID = diag::note_arc_lost_method_convention; |
| 2366 | if (declFamily == OMF_None) { |
| 2367 | unmatched = decl; |
| 2368 | family = implFamily; |
| 2369 | errorID = diag::err_arc_gained_method_convention; |
| 2370 | noteID = diag::note_arc_gained_method_convention; |
| 2371 | } |
| 2372 | |
| 2373 | // Indexes into a %select clause in the diagnostic. |
| 2374 | enum FamilySelector { |
| 2375 | F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new |
| 2376 | }; |
| 2377 | FamilySelector familySelector = FamilySelector(); |
| 2378 | |
| 2379 | switch (family) { |
| 2380 | case OMF_None: llvm_unreachable("logic error, no method convention"); |
| 2381 | case OMF_retain: |
| 2382 | case OMF_release: |
| 2383 | case OMF_autorelease: |
| 2384 | case OMF_dealloc: |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 2385 | case OMF_finalize: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2386 | case OMF_retainCount: |
| 2387 | case OMF_self: |
Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 2388 | case OMF_initialize: |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 2389 | case OMF_performSelector: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2390 | // Mismatches for these methods don't change ownership |
| 2391 | // conventions, so we don't care. |
| 2392 | return false; |
| 2393 | |
| 2394 | case OMF_init: familySelector = F_init; break; |
| 2395 | case OMF_alloc: familySelector = F_alloc; break; |
| 2396 | case OMF_copy: familySelector = F_copy; break; |
| 2397 | case OMF_mutableCopy: familySelector = F_mutableCopy; break; |
| 2398 | case OMF_new: familySelector = F_new; break; |
| 2399 | } |
| 2400 | |
| 2401 | enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn }; |
| 2402 | ReasonSelector reasonSelector; |
| 2403 | |
| 2404 | // The only reason these methods don't fall within their families is |
| 2405 | // due to unusual result types. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2406 | if (unmatched->getReturnType()->isObjCObjectPointerType()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2407 | reasonSelector = R_UnrelatedReturn; |
| 2408 | } else { |
| 2409 | reasonSelector = R_NonObjectReturn; |
| 2410 | } |
| 2411 | |
Joerg Sonnenberger | ffc6d49 | 2013-06-26 21:31:47 +0000 | [diff] [blame] | 2412 | S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector); |
| 2413 | S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2414 | |
| 2415 | return true; |
| 2416 | } |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2417 | |
Fariborz Jahanian | 7988d7d | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 2418 | void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2419 | ObjCMethodDecl *MethodDecl, |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2420 | bool IsProtocolMethodDecl) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2421 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2422 | checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl)) |
| 2423 | return; |
| 2424 | |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2425 | CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2426 | IsProtocolMethodDecl, false, |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2427 | true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2428 | |
Chris Lattner | 67f35b0 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 2429 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2430 | IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(), |
| 2431 | EF = MethodDecl->param_end(); |
| 2432 | IM != EM && IF != EF; ++IM, ++IF) { |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2433 | CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF, |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2434 | IsProtocolMethodDecl, false, true); |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2435 | } |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2436 | |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2437 | if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) { |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2438 | Diag(ImpMethodDecl->getLocation(), |
| 2439 | diag::warn_conflicting_variadic); |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2440 | Diag(MethodDecl->getLocation(), diag::note_previous_declaration); |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2441 | } |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2444 | void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method, |
| 2445 | ObjCMethodDecl *Overridden, |
| 2446 | bool IsProtocolMethodDecl) { |
| 2447 | |
| 2448 | CheckMethodOverrideReturn(*this, Method, Overridden, |
| 2449 | IsProtocolMethodDecl, true, |
| 2450 | true); |
| 2451 | |
| 2452 | for (ObjCMethodDecl::param_iterator IM = Method->param_begin(), |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2453 | IF = Overridden->param_begin(), EM = Method->param_end(), |
| 2454 | EF = Overridden->param_end(); |
| 2455 | IM != EM && IF != EF; ++IM, ++IF) { |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2456 | CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF, |
| 2457 | IsProtocolMethodDecl, true, true); |
| 2458 | } |
| 2459 | |
| 2460 | if (Method->isVariadic() != Overridden->isVariadic()) { |
| 2461 | Diag(Method->getLocation(), |
| 2462 | diag::warn_conflicting_overriding_variadic); |
| 2463 | Diag(Overridden->getLocation(), diag::note_previous_declaration); |
| 2464 | } |
| 2465 | } |
| 2466 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2467 | /// WarnExactTypedMethods - This routine issues a warning if method |
| 2468 | /// implementation declaration matches exactly that of its declaration. |
| 2469 | void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
| 2470 | ObjCMethodDecl *MethodDecl, |
| 2471 | bool IsProtocolMethodDecl) { |
| 2472 | // don't issue warning when protocol method is optional because primary |
| 2473 | // class is not required to implement it and it is safe for protocol |
| 2474 | // to implement it. |
| 2475 | if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional) |
| 2476 | return; |
| 2477 | // don't issue warning when primary class's method is |
| 2478 | // depecated/unavailable. |
| 2479 | if (MethodDecl->hasAttr<UnavailableAttr>() || |
| 2480 | MethodDecl->hasAttr<DeprecatedAttr>()) |
| 2481 | return; |
| 2482 | |
| 2483 | bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
| 2484 | IsProtocolMethodDecl, false, false); |
| 2485 | if (match) |
| 2486 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2487 | IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(), |
| 2488 | EF = MethodDecl->param_end(); |
| 2489 | IM != EM && IF != EF; ++IM, ++IF) { |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2490 | match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, |
| 2491 | *IM, *IF, |
| 2492 | IsProtocolMethodDecl, false, false); |
| 2493 | if (!match) |
| 2494 | break; |
| 2495 | } |
| 2496 | if (match) |
| 2497 | match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic()); |
David Chisnall | 9291851 | 2011-08-08 17:32:19 +0000 | [diff] [blame] | 2498 | if (match) |
| 2499 | match = !(MethodDecl->isClassMethod() && |
| 2500 | MethodDecl->getSelector() == GetNullarySelector("load", Context)); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2501 | |
| 2502 | if (match) { |
| 2503 | Diag(ImpMethodDecl->getLocation(), |
| 2504 | diag::warn_category_method_impl_match); |
Ted Kremenek | 59b10db | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 2505 | Diag(MethodDecl->getLocation(), diag::note_method_declared_at) |
| 2506 | << MethodDecl->getDeclName(); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2507 | } |
| 2508 | } |
| 2509 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2510 | /// FIXME: Type hierarchies in Objective-C can be deep. We could most likely |
| 2511 | /// improve the efficiency of selector lookups and type checking by associating |
| 2512 | /// with each protocol / interface / category the flattened instance tables. If |
| 2513 | /// we used an immutable set to keep the table then it wouldn't add significant |
| 2514 | /// memory cost and it would be handy for lookups. |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 2515 | |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2516 | typedef llvm::DenseSet<IdentifierInfo*> ProtocolNameSet; |
Ahmed Charles | af94d56 | 2014-03-09 11:34:25 +0000 | [diff] [blame] | 2517 | typedef std::unique_ptr<ProtocolNameSet> LazyProtocolNameSet; |
Ted Kremenek | 760a2ac | 2014-03-05 23:18:22 +0000 | [diff] [blame] | 2518 | |
| 2519 | static void findProtocolsWithExplicitImpls(const ObjCProtocolDecl *PDecl, |
| 2520 | ProtocolNameSet &PNS) { |
| 2521 | if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) |
| 2522 | PNS.insert(PDecl->getIdentifier()); |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 2523 | for (const auto *PI : PDecl->protocols()) |
| 2524 | findProtocolsWithExplicitImpls(PI, PNS); |
Ted Kremenek | 760a2ac | 2014-03-05 23:18:22 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2527 | /// Recursively populates a set with all conformed protocols in a class |
| 2528 | /// hierarchy that have the 'objc_protocol_requires_explicit_implementation' |
| 2529 | /// attribute. |
| 2530 | static void findProtocolsWithExplicitImpls(const ObjCInterfaceDecl *Super, |
| 2531 | ProtocolNameSet &PNS) { |
| 2532 | if (!Super) |
| 2533 | return; |
| 2534 | |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2535 | for (const auto *I : Super->all_referenced_protocols()) |
| 2536 | findProtocolsWithExplicitImpls(I, PNS); |
Ted Kremenek | 760a2ac | 2014-03-05 23:18:22 +0000 | [diff] [blame] | 2537 | |
| 2538 | findProtocolsWithExplicitImpls(Super->getSuperClass(), PNS); |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Steve Naroff | a3699224 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 2541 | /// CheckProtocolMethodDefs - This routine checks unimplemented methods |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2542 | /// Declared in protocol, and those referenced by it. |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2543 | static void CheckProtocolMethodDefs(Sema &S, |
| 2544 | SourceLocation ImpLoc, |
| 2545 | ObjCProtocolDecl *PDecl, |
| 2546 | bool& IncompleteImpl, |
| 2547 | const Sema::SelectorSet &InsMap, |
| 2548 | const Sema::SelectorSet &ClsMap, |
Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 2549 | ObjCContainerDecl *CDecl, |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2550 | LazyProtocolNameSet &ProtocolsExplictImpl) { |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2551 | ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl); |
| 2552 | ObjCInterfaceDecl *IDecl = C ? C->getClassInterface() |
| 2553 | : dyn_cast<ObjCInterfaceDecl>(CDecl); |
Fariborz Jahanian | 2e8074b | 2010-03-27 21:10:05 +0000 | [diff] [blame] | 2554 | assert (IDecl && "CheckProtocolMethodDefs - IDecl is null"); |
| 2555 | |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 2556 | ObjCInterfaceDecl *Super = IDecl->getSuperClass(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2557 | ObjCInterfaceDecl *NSIDecl = nullptr; |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2558 | |
| 2559 | // If this protocol is marked 'objc_protocol_requires_explicit_implementation' |
| 2560 | // then we should check if any class in the super class hierarchy also |
| 2561 | // conforms to this protocol, either directly or via protocol inheritance. |
| 2562 | // If so, we can skip checking this protocol completely because we |
| 2563 | // know that a parent class already satisfies this protocol. |
| 2564 | // |
| 2565 | // Note: we could generalize this logic for all protocols, and merely |
| 2566 | // add the limit on looking at the super class chain for just |
| 2567 | // specially marked protocols. This may be a good optimization. This |
| 2568 | // change is restricted to 'objc_protocol_requires_explicit_implementation' |
| 2569 | // protocols for now for controlled evaluation. |
| 2570 | if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) { |
Ahmed Charles | af94d56 | 2014-03-09 11:34:25 +0000 | [diff] [blame] | 2571 | if (!ProtocolsExplictImpl) { |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2572 | ProtocolsExplictImpl.reset(new ProtocolNameSet); |
| 2573 | findProtocolsWithExplicitImpls(Super, *ProtocolsExplictImpl); |
| 2574 | } |
| 2575 | if (ProtocolsExplictImpl->find(PDecl->getIdentifier()) != |
| 2576 | ProtocolsExplictImpl->end()) |
| 2577 | return; |
| 2578 | |
| 2579 | // If no super class conforms to the protocol, we should not search |
| 2580 | // for methods in the super class to implicitly satisfy the protocol. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2581 | Super = nullptr; |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2584 | if (S.getLangOpts().ObjCRuntime.isNeXTFamily()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2585 | // check to see if class implements forwardInvocation method and objects |
| 2586 | // of this class are derived from 'NSProxy' so that to forward requests |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2587 | // from one object to another. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2588 | // Under such conditions, which means that every method possible is |
| 2589 | // implemented in the class, we should not issue "Method definition not |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2590 | // found" warnings. |
| 2591 | // FIXME: Use a general GetUnarySelector method for this. |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2592 | IdentifierInfo* II = &S.Context.Idents.get("forwardInvocation"); |
| 2593 | Selector fISelector = S.Context.Selectors.getSelector(1, &II); |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2594 | if (InsMap.count(fISelector)) |
| 2595 | // Is IDecl derived from 'NSProxy'? If so, no instance methods |
| 2596 | // need be implemented in the implementation. |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2597 | NSIDecl = IDecl->lookupInheritedClass(&S.Context.Idents.get("NSProxy")); |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2598 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2599 | |
Fariborz Jahanian | c41cf05 | 2013-01-07 19:21:03 +0000 | [diff] [blame] | 2600 | // If this is a forward protocol declaration, get its definition. |
| 2601 | if (!PDecl->isThisDeclarationADefinition() && |
| 2602 | PDecl->getDefinition()) |
| 2603 | PDecl = PDecl->getDefinition(); |
| 2604 | |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 2605 | // If a method lookup fails locally we still need to look and see if |
| 2606 | // the method was implemented by a base class or an inherited |
| 2607 | // protocol. This lookup is slow, but occurs rarely in correct code |
| 2608 | // and otherwise would terminate in a warning. |
| 2609 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2610 | // check unimplemented instance methods. |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2611 | if (!NSIDecl) |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2612 | for (auto *method : PDecl->instance_methods()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2613 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2614 | !method->isPropertyAccessor() && |
| 2615 | !InsMap.count(method->getSelector()) && |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2616 | (!Super || !Super->lookupMethod(method->getSelector(), |
| 2617 | true /* instance */, |
| 2618 | false /* shallowCategory */, |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2619 | true /* followsSuper */, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2620 | nullptr /* category */))) { |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2621 | // If a method is not implemented in the category implementation but |
| 2622 | // has been declared in its primary class, superclass, |
| 2623 | // or in one of their protocols, no need to issue the warning. |
| 2624 | // This is because method will be implemented in the primary class |
| 2625 | // or one of its super class implementation. |
| 2626 | |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2627 | // Ugly, but necessary. Method declared in protcol might have |
| 2628 | // have been synthesized due to a property declared in the class which |
| 2629 | // uses the protocol. |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2630 | if (ObjCMethodDecl *MethodInClass = |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2631 | IDecl->lookupMethod(method->getSelector(), |
| 2632 | true /* instance */, |
| 2633 | true /* shallowCategoryLookup */, |
| 2634 | false /* followSuper */)) |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2635 | if (C || MethodInClass->isPropertyAccessor()) |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2636 | continue; |
| 2637 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 2638 | if (!S.Diags.isIgnored(DIAG, ImpLoc)) { |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2639 | WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, |
Ted Kremenek | 2ccf19e | 2013-12-13 05:58:51 +0000 | [diff] [blame] | 2640 | PDecl); |
Fariborz Jahanian | 97752f7 | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 2641 | } |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2642 | } |
| 2643 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2644 | // check unimplemented class methods |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2645 | for (auto *method : PDecl->class_methods()) { |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 2646 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
| 2647 | !ClsMap.count(method->getSelector()) && |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2648 | (!Super || !Super->lookupMethod(method->getSelector(), |
| 2649 | false /* class method */, |
| 2650 | false /* shallowCategoryLookup */, |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2651 | true /* followSuper */, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2652 | nullptr /* category */))) { |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2653 | // See above comment for instance method lookups. |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2654 | if (C && IDecl->lookupMethod(method->getSelector(), |
| 2655 | false /* class */, |
| 2656 | true /* shallowCategoryLookup */, |
| 2657 | false /* followSuper */)) |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2658 | continue; |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2659 | |
Fariborz Jahanian | c1fb862 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 2660 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 2661 | if (!S.Diags.isIgnored(DIAG, ImpLoc)) { |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2662 | WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, PDecl); |
Fariborz Jahanian | c1fb862 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 2663 | } |
Fariborz Jahanian | 97752f7 | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 2664 | } |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 2665 | } |
Chris Lattner | 390d39a | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 2666 | // Check on this protocols's referenced protocols, recursively. |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 2667 | for (auto *PI : PDecl->protocols()) |
| 2668 | CheckProtocolMethodDefs(S, ImpLoc, PI, IncompleteImpl, InsMap, ClsMap, |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2669 | CDecl, ProtocolsExplictImpl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
Fariborz Jahanian | f9ae68a | 2011-07-16 00:08:33 +0000 | [diff] [blame] | 2672 | /// MatchAllMethodDeclarations - Check methods declared in interface |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2673 | /// or protocol against those declared in their implementations. |
| 2674 | /// |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2675 | void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap, |
| 2676 | const SelectorSet &ClsMap, |
| 2677 | SelectorSet &InsMapSeen, |
| 2678 | SelectorSet &ClsMapSeen, |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2679 | ObjCImplDecl* IMPDecl, |
| 2680 | ObjCContainerDecl* CDecl, |
| 2681 | bool &IncompleteImpl, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2682 | bool ImmediateClass, |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2683 | bool WarnCategoryMethodImpl) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2684 | // Check and see if instance methods in class interface have been |
| 2685 | // implemented in the implementation class. If so, their types match. |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2686 | for (auto *I : CDecl->instance_methods()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 2687 | if (!InsMapSeen.insert(I->getSelector()).second) |
Benjamin Kramer | 9f8e2d7 | 2013-10-14 15:16:10 +0000 | [diff] [blame] | 2688 | continue; |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2689 | if (!I->isPropertyAccessor() && |
| 2690 | !InsMap.count(I->getSelector())) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2691 | if (ImmediateClass) |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2692 | WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl, |
Ted Kremenek | 65d6357 | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 2693 | diag::warn_undef_method_impl); |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2694 | continue; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2695 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2696 | ObjCMethodDecl *ImpMethodDecl = |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2697 | IMPDecl->getInstanceMethod(I->getSelector()); |
| 2698 | assert(CDecl->getInstanceMethod(I->getSelector()) && |
Argyrios Kyrtzidis | 342e08f | 2011-08-30 19:43:21 +0000 | [diff] [blame] | 2699 | "Expected to find the method through lookup as well"); |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2700 | // ImpMethodDecl may be null as in a @dynamic property. |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2701 | if (ImpMethodDecl) { |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2702 | if (!WarnCategoryMethodImpl) |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2703 | WarnConflictingTypedMethods(ImpMethodDecl, I, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2704 | isa<ObjCProtocolDecl>(CDecl)); |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2705 | else if (!I->isPropertyAccessor()) |
| 2706 | WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl)); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2707 | } |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2708 | } |
| 2709 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2710 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2711 | // Check and see if class methods in class interface have been |
| 2712 | // implemented in the implementation class. If so, their types match. |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2713 | for (auto *I : CDecl->class_methods()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 2714 | if (!ClsMapSeen.insert(I->getSelector()).second) |
Benjamin Kramer | 9f8e2d7 | 2013-10-14 15:16:10 +0000 | [diff] [blame] | 2715 | continue; |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2716 | if (!ClsMap.count(I->getSelector())) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2717 | if (ImmediateClass) |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2718 | WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl, |
Ted Kremenek | 65d6357 | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 2719 | diag::warn_undef_method_impl); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2720 | } else { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2721 | ObjCMethodDecl *ImpMethodDecl = |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2722 | IMPDecl->getClassMethod(I->getSelector()); |
| 2723 | assert(CDecl->getClassMethod(I->getSelector()) && |
Argyrios Kyrtzidis | 342e08f | 2011-08-30 19:43:21 +0000 | [diff] [blame] | 2724 | "Expected to find the method through lookup as well"); |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2725 | if (!WarnCategoryMethodImpl) |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2726 | WarnConflictingTypedMethods(ImpMethodDecl, I, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2727 | isa<ObjCProtocolDecl>(CDecl)); |
| 2728 | else |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2729 | WarnExactTypedMethods(ImpMethodDecl, I, |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2730 | isa<ObjCProtocolDecl>(CDecl)); |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2731 | } |
| 2732 | } |
Fariborz Jahanian | 73853e5 | 2010-10-08 22:59:25 +0000 | [diff] [blame] | 2733 | |
Fariborz Jahanian | 8181caa | 2013-08-14 23:58:55 +0000 | [diff] [blame] | 2734 | if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) { |
| 2735 | // Also, check for methods declared in protocols inherited by |
| 2736 | // this protocol. |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 2737 | for (auto *PI : PD->protocols()) |
Fariborz Jahanian | 8181caa | 2013-08-14 23:58:55 +0000 | [diff] [blame] | 2738 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 2739 | IMPDecl, PI, IncompleteImpl, false, |
Fariborz Jahanian | 8181caa | 2013-08-14 23:58:55 +0000 | [diff] [blame] | 2740 | WarnCategoryMethodImpl); |
| 2741 | } |
| 2742 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2743 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2744 | // when checking that methods in implementation match their declaration, |
| 2745 | // i.e. when WarnCategoryMethodImpl is false, check declarations in class |
| 2746 | // extension; as well as those in categories. |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2747 | if (!WarnCategoryMethodImpl) { |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 2748 | for (auto *Cat : I->visible_categories()) |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2749 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 2750 | IMPDecl, Cat, IncompleteImpl, false, |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2751 | WarnCategoryMethodImpl); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2752 | } else { |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2753 | // Also methods in class extensions need be looked at next. |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 2754 | for (auto *Ext : I->visible_extensions()) |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2755 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 2756 | IMPDecl, Ext, IncompleteImpl, false, |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2757 | WarnCategoryMethodImpl); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2760 | // Check for any implementation of a methods declared in protocol. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2761 | for (auto *PI : I->all_referenced_protocols()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2762 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2763 | IMPDecl, PI, IncompleteImpl, false, |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2764 | WarnCategoryMethodImpl); |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2765 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2766 | // FIXME. For now, we are not checking for extact match of methods |
| 2767 | // in category implementation and its primary class's super class. |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2768 | if (!WarnCategoryMethodImpl && I->getSuperClass()) |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2769 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2770 | IMPDecl, |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2771 | I->getSuperClass(), IncompleteImpl, false); |
| 2772 | } |
| 2773 | } |
| 2774 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2775 | /// CheckCategoryVsClassMethodMatches - Checks that methods implemented in |
| 2776 | /// category matches with those implemented in its primary class and |
| 2777 | /// warns each time an exact match is found. |
| 2778 | void Sema::CheckCategoryVsClassMethodMatches( |
| 2779 | ObjCCategoryImplDecl *CatIMPDecl) { |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2780 | // Get category's primary class. |
| 2781 | ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl(); |
| 2782 | if (!CatDecl) |
| 2783 | return; |
| 2784 | ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface(); |
| 2785 | if (!IDecl) |
| 2786 | return; |
Fariborz Jahanian | f3077a2 | 2013-12-05 20:52:31 +0000 | [diff] [blame] | 2787 | ObjCInterfaceDecl *SuperIDecl = IDecl->getSuperClass(); |
| 2788 | SelectorSet InsMap, ClsMap; |
| 2789 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2790 | for (const auto *I : CatIMPDecl->instance_methods()) { |
| 2791 | Selector Sel = I->getSelector(); |
Fariborz Jahanian | f3077a2 | 2013-12-05 20:52:31 +0000 | [diff] [blame] | 2792 | // When checking for methods implemented in the category, skip over |
| 2793 | // those declared in category class's super class. This is because |
| 2794 | // the super class must implement the method. |
| 2795 | if (SuperIDecl && SuperIDecl->lookupMethod(Sel, true)) |
| 2796 | continue; |
| 2797 | InsMap.insert(Sel); |
| 2798 | } |
| 2799 | |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2800 | for (const auto *I : CatIMPDecl->class_methods()) { |
| 2801 | Selector Sel = I->getSelector(); |
Fariborz Jahanian | f3077a2 | 2013-12-05 20:52:31 +0000 | [diff] [blame] | 2802 | if (SuperIDecl && SuperIDecl->lookupMethod(Sel, false)) |
| 2803 | continue; |
| 2804 | ClsMap.insert(Sel); |
| 2805 | } |
| 2806 | if (InsMap.empty() && ClsMap.empty()) |
| 2807 | return; |
| 2808 | |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2809 | SelectorSet InsMapSeen, ClsMapSeen; |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2810 | bool IncompleteImpl = false; |
| 2811 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 2812 | CatIMPDecl, IDecl, |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2813 | IncompleteImpl, false, |
| 2814 | true /*WarnCategoryMethodImpl*/); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2815 | } |
Fariborz Jahanian | 4ceec3f | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 2816 | |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 2817 | void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2818 | ObjCContainerDecl* CDecl, |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 2819 | bool IncompleteImpl) { |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2820 | SelectorSet InsMap; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2821 | // Check and see if instance methods in class interface have been |
| 2822 | // implemented in the implementation class. |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2823 | for (const auto *I : IMPDecl->instance_methods()) |
| 2824 | InsMap.insert(I->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2825 | |
Fariborz Jahanian | 6c6aea9 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 2826 | // Check and see if properties declared in the interface have either 1) |
| 2827 | // an implementation or 2) there is a @synthesize/@dynamic implementation |
| 2828 | // of the property in the @implementation. |
Ted Kremenek | 348e88c | 2014-02-21 19:41:34 +0000 | [diff] [blame] | 2829 | if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 2830 | bool SynthesizeProperties = LangOpts.ObjCDefaultSynthProperties && |
| 2831 | LangOpts.ObjCRuntime.isNonFragile() && |
| 2832 | !IDecl->isObjCRequiresPropertyDefs(); |
| 2833 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, SynthesizeProperties); |
| 2834 | } |
| 2835 | |
Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 2836 | // Diagnose null-resettable synthesized setters. |
| 2837 | diagnoseNullResettableSynthesizedSetters(IMPDecl); |
| 2838 | |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2839 | SelectorSet ClsMap; |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2840 | for (const auto *I : IMPDecl->class_methods()) |
| 2841 | ClsMap.insert(I->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2842 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2843 | // Check for type conflict of methods declared in a class/protocol and |
| 2844 | // its implementation; if any. |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2845 | SelectorSet InsMapSeen, ClsMapSeen; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2846 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 2847 | IMPDecl, CDecl, |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2848 | IncompleteImpl, true); |
Fariborz Jahanian | 2bda1b6 | 2011-08-03 18:21:12 +0000 | [diff] [blame] | 2849 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2850 | // check all methods implemented in category against those declared |
| 2851 | // in its primary class. |
| 2852 | if (ObjCCategoryImplDecl *CatDecl = |
| 2853 | dyn_cast<ObjCCategoryImplDecl>(IMPDecl)) |
| 2854 | CheckCategoryVsClassMethodMatches(CatDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2855 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2856 | // Check the protocol list for unimplemented methods in the @implementation |
| 2857 | // class. |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2858 | // Check and see if class methods in class interface have been |
| 2859 | // implemented in the implementation class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2860 | |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2861 | LazyProtocolNameSet ExplicitImplProtocols; |
| 2862 | |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 2863 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2864 | for (auto *PI : I->all_referenced_protocols()) |
| 2865 | CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), PI, IncompleteImpl, |
| 2866 | InsMap, ClsMap, I, ExplicitImplProtocols); |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 2867 | // Check class extensions (unnamed categories) |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 2868 | for (auto *Ext : I->visible_extensions()) |
| 2869 | ImplMethodsVsClassMethods(S, IMPDecl, Ext, IncompleteImpl); |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 2870 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Fariborz Jahanian | 8764c74 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 2871 | // For extended class, unimplemented methods in its protocols will |
| 2872 | // be reported in the primary class. |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 2873 | if (!C->IsClassExtension()) { |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 2874 | for (auto *P : C->protocols()) |
| 2875 | CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), P, |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2876 | IncompleteImpl, InsMap, ClsMap, CDecl, |
| 2877 | ExplicitImplProtocols); |
Ted Kremenek | 348e88c | 2014-02-21 19:41:34 +0000 | [diff] [blame] | 2878 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 2879 | /*SynthesizeProperties=*/false); |
Fariborz Jahanian | 4f8a571 | 2010-01-20 19:36:21 +0000 | [diff] [blame] | 2880 | } |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 2881 | } else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2882 | llvm_unreachable("invalid ObjCContainerDecl type."); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 2885 | Sema::DeclGroupPtrTy |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2886 | Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
Chris Lattner | 99a8331 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 2887 | IdentifierInfo **IdentList, |
Ted Kremenek | a26da85 | 2009-11-17 23:12:20 +0000 | [diff] [blame] | 2888 | SourceLocation *IdentLocs, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 2889 | ArrayRef<ObjCTypeParamList *> TypeParamLists, |
Chris Lattner | 99a8331 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 2890 | unsigned NumElts) { |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 2891 | SmallVector<Decl *, 8> DeclsInGroup; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2892 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2893 | // Check for another declaration kind with the same name. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2894 | NamedDecl *PrevDecl |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2895 | = LookupSingleName(TUScope, IdentList[i], IdentLocs[i], |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 2896 | LookupOrdinaryName, ForRedeclaration); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2897 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Steve Naroff | 946166f | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 2898 | // GCC apparently allows the following idiom: |
| 2899 | // |
| 2900 | // typedef NSObject < XCElementTogglerP > XCElementToggler; |
| 2901 | // @class XCElementToggler; |
| 2902 | // |
Fariborz Jahanian | 04c4455 | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 2903 | // Here we have chosen to ignore the forward class declaration |
| 2904 | // with a warning. Since this is the implied behavior. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2905 | TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2906 | if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2907 | Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i]; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2908 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2909 | } else { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2910 | // a forward class declaration matching a typedef name of a class refers |
Fariborz Jahanian | 04c4455 | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 2911 | // to the underlying class. Just ignore the forward class with a warning |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 2912 | // as this will force the intended behavior which is to lookup the |
| 2913 | // typedef name. |
Fariborz Jahanian | 04c4455 | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 2914 | if (isa<ObjCObjectType>(TDD->getUnderlyingType())) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 2915 | Diag(AtClassLoc, diag::warn_forward_class_redefinition) |
| 2916 | << IdentList[i]; |
Fariborz Jahanian | 04c4455 | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 2917 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 2918 | continue; |
| 2919 | } |
Fariborz Jahanian | 0d45181 | 2009-05-07 21:49:26 +0000 | [diff] [blame] | 2920 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2921 | } |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 2922 | |
| 2923 | // Create a declaration to describe this forward declaration. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 2924 | ObjCInterfaceDecl *PrevIDecl |
| 2925 | = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Argyrios Kyrtzidis | dd71063 | 2013-06-18 21:26:33 +0000 | [diff] [blame] | 2926 | |
| 2927 | IdentifierInfo *ClassName = IdentList[i]; |
| 2928 | if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) { |
| 2929 | // A previous decl with a different name is because of |
| 2930 | // @compatibility_alias, for example: |
| 2931 | // \code |
| 2932 | // @class NewImage; |
| 2933 | // @compatibility_alias OldImage NewImage; |
| 2934 | // \endcode |
| 2935 | // A lookup for 'OldImage' will return the 'NewImage' decl. |
| 2936 | // |
| 2937 | // In such a case use the real declaration name, instead of the alias one, |
| 2938 | // otherwise we will break IdentifierResolver and redecls-chain invariants. |
| 2939 | // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl |
| 2940 | // has been aliased. |
| 2941 | ClassName = PrevIDecl->getIdentifier(); |
| 2942 | } |
| 2943 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 2944 | // If this forward declaration has type parameters, compare them with the |
| 2945 | // type parameters of the previous declaration. |
| 2946 | ObjCTypeParamList *TypeParams = TypeParamLists[i]; |
| 2947 | if (PrevIDecl && TypeParams) { |
| 2948 | if (ObjCTypeParamList *PrevTypeParams = PrevIDecl->getTypeParamList()) { |
| 2949 | // Check for consistency with the previous declaration. |
| 2950 | if (checkTypeParamListConsistency( |
| 2951 | *this, PrevTypeParams, TypeParams, |
| 2952 | TypeParamListContext::ForwardDeclaration)) { |
| 2953 | TypeParams = nullptr; |
| 2954 | } |
| 2955 | } else if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) { |
| 2956 | // The @interface does not have type parameters. Complain. |
| 2957 | Diag(IdentLocs[i], diag::err_objc_parameterized_forward_class) |
| 2958 | << ClassName |
| 2959 | << TypeParams->getSourceRange(); |
| 2960 | Diag(Def->getLocation(), diag::note_defined_here) |
| 2961 | << ClassName; |
| 2962 | |
| 2963 | TypeParams = nullptr; |
| 2964 | } |
| 2965 | } |
| 2966 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 2967 | ObjCInterfaceDecl *IDecl |
| 2968 | = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 2969 | ClassName, TypeParams, PrevIDecl, |
| 2970 | IdentLocs[i]); |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 2971 | IDecl->setAtEndRange(IdentLocs[i]); |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 2972 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 2973 | PushOnScopeChains(IDecl, TUScope); |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 2974 | CheckObjCDeclScope(IDecl); |
| 2975 | DeclsInGroup.push_back(IDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2976 | } |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 2977 | |
| 2978 | return BuildDeclaratorGroup(DeclsInGroup, false); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 2981 | static bool tryMatchRecordTypes(ASTContext &Context, |
| 2982 | Sema::MethodMatchStrategy strategy, |
| 2983 | const Type *left, const Type *right); |
| 2984 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2985 | static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy, |
| 2986 | QualType leftQT, QualType rightQT) { |
| 2987 | const Type *left = |
| 2988 | Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr(); |
| 2989 | const Type *right = |
| 2990 | Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr(); |
| 2991 | |
| 2992 | if (left == right) return true; |
| 2993 | |
| 2994 | // If we're doing a strict match, the types have to match exactly. |
| 2995 | if (strategy == Sema::MMS_strict) return false; |
| 2996 | |
| 2997 | if (left->isIncompleteType() || right->isIncompleteType()) return false; |
| 2998 | |
| 2999 | // Otherwise, use this absurdly complicated algorithm to try to |
| 3000 | // validate the basic, low-level compatibility of the two types. |
| 3001 | |
| 3002 | // As a minimum, require the sizes and alignments to match. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 3003 | TypeInfo LeftTI = Context.getTypeInfo(left); |
| 3004 | TypeInfo RightTI = Context.getTypeInfo(right); |
| 3005 | if (LeftTI.Width != RightTI.Width) |
| 3006 | return false; |
| 3007 | |
| 3008 | if (LeftTI.Align != RightTI.Align) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3009 | return false; |
| 3010 | |
| 3011 | // Consider all the kinds of non-dependent canonical types: |
| 3012 | // - functions and arrays aren't possible as return and parameter types |
| 3013 | |
| 3014 | // - vector types of equal size can be arbitrarily mixed |
| 3015 | if (isa<VectorType>(left)) return isa<VectorType>(right); |
| 3016 | if (isa<VectorType>(right)) return false; |
| 3017 | |
| 3018 | // - references should only match references of identical type |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3019 | // - structs, unions, and Objective-C objects must match more-or-less |
| 3020 | // exactly |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3021 | // - everything else should be a scalar |
| 3022 | if (!left->isScalarType() || !right->isScalarType()) |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3023 | return tryMatchRecordTypes(Context, strategy, left, right); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3024 | |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3025 | // Make scalars agree in kind, except count bools as chars, and group |
| 3026 | // all non-member pointers together. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3027 | Type::ScalarTypeKind leftSK = left->getScalarTypeKind(); |
| 3028 | Type::ScalarTypeKind rightSK = right->getScalarTypeKind(); |
| 3029 | if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral; |
| 3030 | if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral; |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3031 | if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer) |
| 3032 | leftSK = Type::STK_ObjCObjectPointer; |
| 3033 | if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer) |
| 3034 | rightSK = Type::STK_ObjCObjectPointer; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3035 | |
| 3036 | // Note that data member pointers and function member pointers don't |
| 3037 | // intermix because of the size differences. |
| 3038 | |
| 3039 | return (leftSK == rightSK); |
| 3040 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3041 | |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3042 | static bool tryMatchRecordTypes(ASTContext &Context, |
| 3043 | Sema::MethodMatchStrategy strategy, |
| 3044 | const Type *lt, const Type *rt) { |
| 3045 | assert(lt && rt && lt != rt); |
| 3046 | |
| 3047 | if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false; |
| 3048 | RecordDecl *left = cast<RecordType>(lt)->getDecl(); |
| 3049 | RecordDecl *right = cast<RecordType>(rt)->getDecl(); |
| 3050 | |
| 3051 | // Require union-hood to match. |
| 3052 | if (left->isUnion() != right->isUnion()) return false; |
| 3053 | |
| 3054 | // Require an exact match if either is non-POD. |
| 3055 | if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) || |
| 3056 | (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD())) |
| 3057 | return false; |
| 3058 | |
| 3059 | // Require size and alignment to match. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 3060 | TypeInfo LeftTI = Context.getTypeInfo(lt); |
| 3061 | TypeInfo RightTI = Context.getTypeInfo(rt); |
| 3062 | if (LeftTI.Width != RightTI.Width) |
| 3063 | return false; |
| 3064 | |
| 3065 | if (LeftTI.Align != RightTI.Align) |
| 3066 | return false; |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3067 | |
| 3068 | // Require fields to match. |
| 3069 | RecordDecl::field_iterator li = left->field_begin(), le = left->field_end(); |
| 3070 | RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end(); |
| 3071 | for (; li != le && ri != re; ++li, ++ri) { |
| 3072 | if (!matchTypes(Context, strategy, li->getType(), ri->getType())) |
| 3073 | return false; |
| 3074 | } |
| 3075 | return (li == le && ri == re); |
| 3076 | } |
| 3077 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3078 | /// MatchTwoMethodDeclarations - Checks that two methods have matching type and |
| 3079 | /// returns true, or false, accordingly. |
| 3080 | /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3081 | bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left, |
| 3082 | const ObjCMethodDecl *right, |
| 3083 | MethodMatchStrategy strategy) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3084 | if (!matchTypes(Context, strategy, left->getReturnType(), |
| 3085 | right->getReturnType())) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3086 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3087 | |
Douglas Gregor | 560b7fa | 2013-02-07 19:13:24 +0000 | [diff] [blame] | 3088 | // If either is hidden, it is not considered to match. |
| 3089 | if (left->isHidden() || right->isHidden()) |
| 3090 | return false; |
| 3091 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3092 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3093 | (left->hasAttr<NSReturnsRetainedAttr>() |
| 3094 | != right->hasAttr<NSReturnsRetainedAttr>() || |
| 3095 | left->hasAttr<NSConsumesSelfAttr>() |
| 3096 | != right->hasAttr<NSConsumesSelfAttr>())) |
| 3097 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3098 | |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 3099 | ObjCMethodDecl::param_const_iterator |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 3100 | li = left->param_begin(), le = left->param_end(), ri = right->param_begin(), |
| 3101 | re = right->param_end(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3102 | |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 3103 | for (; li != le && ri != re; ++li, ++ri) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3104 | assert(ri != right->param_end() && "Param mismatch"); |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 3105 | const ParmVarDecl *lparm = *li, *rparm = *ri; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3106 | |
| 3107 | if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType())) |
| 3108 | return false; |
| 3109 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3110 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3111 | lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>()) |
| 3112 | return false; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3113 | } |
| 3114 | return true; |
| 3115 | } |
| 3116 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3117 | void Sema::addMethodToGlobalList(ObjCMethodList *List, |
| 3118 | ObjCMethodDecl *Method) { |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 3119 | // Record at the head of the list whether there were 0, 1, or >= 2 methods |
| 3120 | // inside categories. |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3121 | if (ObjCCategoryDecl *CD = |
| 3122 | dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) |
Argyrios Kyrtzidis | 04703a6 | 2013-04-27 00:10:12 +0000 | [diff] [blame] | 3123 | if (!CD->IsClassExtension() && List->getBits() < 2) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3124 | List->setBits(List->getBits() + 1); |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 3125 | |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3126 | // If the list is empty, make it a singleton list. |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3127 | if (List->getMethod() == nullptr) { |
| 3128 | List->setMethod(Method); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3129 | List->setNext(nullptr); |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3130 | return; |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3131 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3132 | |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3133 | // We've seen a method with this name, see if we have already seen this type |
| 3134 | // signature. |
| 3135 | ObjCMethodList *Previous = List; |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 3136 | for (; List; Previous = List, List = List->getNext()) { |
Douglas Gregor | 600a2f5 | 2013-06-21 00:20:25 +0000 | [diff] [blame] | 3137 | // If we are building a module, keep all of the methods. |
| 3138 | if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty()) |
| 3139 | continue; |
| 3140 | |
Fariborz Jahanian | d436b2a | 2015-04-07 16:56:27 +0000 | [diff] [blame] | 3141 | if (!MatchTwoMethodDeclarations(Method, List->getMethod())) { |
| 3142 | // Even if two method types do not match, we would like to say |
| 3143 | // there is more than one declaration so unavailability/deprecated |
| 3144 | // warning is not too noisy. |
| 3145 | if (!Method->isDefined()) |
| 3146 | List->setHasMoreThanOneDecl(true); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3147 | continue; |
Fariborz Jahanian | d436b2a | 2015-04-07 16:56:27 +0000 | [diff] [blame] | 3148 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3149 | |
| 3150 | ObjCMethodDecl *PrevObjCMethod = List->getMethod(); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3151 | |
| 3152 | // Propagate the 'defined' bit. |
| 3153 | if (Method->isDefined()) |
| 3154 | PrevObjCMethod->setDefined(true); |
Nico Weber | e3b1104 | 2014-12-27 07:09:37 +0000 | [diff] [blame] | 3155 | else { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3156 | // Objective-C doesn't allow an @interface for a class after its |
| 3157 | // @implementation. So if Method is not defined and there already is |
| 3158 | // an entry for this type signature, Method has to be for a different |
| 3159 | // class than PrevObjCMethod. |
| 3160 | List->setHasMoreThanOneDecl(true); |
| 3161 | } |
| 3162 | |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3163 | // If a method is deprecated, push it in the global pool. |
| 3164 | // This is used for better diagnostics. |
| 3165 | if (Method->isDeprecated()) { |
| 3166 | if (!PrevObjCMethod->isDeprecated()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3167 | List->setMethod(Method); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3168 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3169 | // If the new method is unavailable, push it into global pool |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3170 | // unless previous one is deprecated. |
| 3171 | if (Method->isUnavailable()) { |
| 3172 | if (PrevObjCMethod->getAvailability() < AR_Deprecated) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3173 | List->setMethod(Method); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3174 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3175 | |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3176 | return; |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3177 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3178 | |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3179 | // We have a new signature for an existing method - add it. |
| 3180 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 3181 | ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>(); |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3182 | Previous->setNext(new (Mem) ObjCMethodList(Method)); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3183 | } |
| 3184 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3185 | /// \brief Read the contents of the method pool for a given selector from |
| 3186 | /// external storage. |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 3187 | void Sema::ReadMethodPool(Selector Sel) { |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3188 | assert(ExternalSource && "We need an external AST source"); |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 3189 | ExternalSource->ReadMethodPool(Sel); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3190 | } |
| 3191 | |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3192 | void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl, |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3193 | bool instance) { |
Argyrios Kyrtzidis | b15def2 | 2012-03-12 18:34:26 +0000 | [diff] [blame] | 3194 | // Ignore methods of invalid containers. |
| 3195 | if (cast<Decl>(Method->getDeclContext())->isInvalidDecl()) |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3196 | return; |
Argyrios Kyrtzidis | b15def2 | 2012-03-12 18:34:26 +0000 | [diff] [blame] | 3197 | |
Douglas Gregor | 70f449b | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 3198 | if (ExternalSource) |
| 3199 | ReadMethodPool(Method->getSelector()); |
| 3200 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3201 | GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector()); |
Douglas Gregor | 70f449b | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 3202 | if (Pos == MethodPool.end()) |
| 3203 | Pos = MethodPool.insert(std::make_pair(Method->getSelector(), |
| 3204 | GlobalMethods())).first; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3205 | |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 3206 | Method->setDefined(impl); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3207 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3208 | ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second; |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3209 | addMethodToGlobalList(&Entry, Method); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3210 | } |
| 3211 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3212 | /// Determines if this is an "acceptable" loose mismatch in the global |
| 3213 | /// method pool. This exists mostly as a hack to get around certain |
| 3214 | /// global mismatches which we can't afford to make warnings / errors. |
| 3215 | /// Really, what we want is a way to take a method out of the global |
| 3216 | /// method pool. |
| 3217 | static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen, |
| 3218 | ObjCMethodDecl *other) { |
| 3219 | if (!chosen->isInstanceMethod()) |
| 3220 | return false; |
| 3221 | |
| 3222 | Selector sel = chosen->getSelector(); |
| 3223 | if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length") |
| 3224 | return false; |
| 3225 | |
| 3226 | // Don't complain about mismatches for -length if the method we |
| 3227 | // chose has an integral result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3228 | return (chosen->getReturnType()->isIntegerType()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3231 | bool Sema::CollectMultipleMethodsInGlobalPool( |
| 3232 | Selector Sel, SmallVectorImpl<ObjCMethodDecl *> &Methods, bool instance) { |
Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 3233 | if (ExternalSource) |
| 3234 | ReadMethodPool(Sel); |
| 3235 | |
| 3236 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
| 3237 | if (Pos == MethodPool.end()) |
| 3238 | return false; |
| 3239 | // Gather the non-hidden methods. |
| 3240 | ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second; |
| 3241 | for (ObjCMethodList *M = &MethList; M; M = M->getNext()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3242 | if (M->getMethod() && !M->getMethod()->isHidden()) |
| 3243 | Methods.push_back(M->getMethod()); |
| 3244 | return Methods.size() > 1; |
Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3247 | bool Sema::AreMultipleMethodsInGlobalPool(Selector Sel, ObjCMethodDecl *BestMethod, |
| 3248 | SourceRange R, |
| 3249 | bool receiverIdOrClass) { |
Fariborz Jahanian | c62d16f | 2014-11-13 22:27:05 +0000 | [diff] [blame] | 3250 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3251 | // Test for no method in the pool which should not trigger any warning by |
| 3252 | // caller. |
Fariborz Jahanian | c62d16f | 2014-11-13 22:27:05 +0000 | [diff] [blame] | 3253 | if (Pos == MethodPool.end()) |
| 3254 | return true; |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3255 | ObjCMethodList &MethList = |
| 3256 | BestMethod->isInstanceMethod() ? Pos->second.first : Pos->second.second; |
| 3257 | |
| 3258 | // Diagnose finding more than one method in global pool |
| 3259 | SmallVector<ObjCMethodDecl *, 4> Methods; |
| 3260 | Methods.push_back(BestMethod); |
Jonathan Roelofs | 7441136 | 2015-04-28 18:04:44 +0000 | [diff] [blame] | 3261 | for (ObjCMethodList *ML = &MethList; ML; ML = ML->getNext()) |
| 3262 | if (ObjCMethodDecl *M = ML->getMethod()) |
| 3263 | if (!M->isHidden() && M != BestMethod && !M->hasAttr<UnavailableAttr>()) |
| 3264 | Methods.push_back(M); |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3265 | if (Methods.size() > 1) |
| 3266 | DiagnoseMultipleMethodInGlobalPool(Methods, Sel, R, receiverIdOrClass); |
| 3267 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3268 | return MethList.hasMoreThanOneDecl(); |
Fariborz Jahanian | c62d16f | 2014-11-13 22:27:05 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3271 | ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R, |
Fariborz Jahanian | 3337b2e | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 3272 | bool receiverIdOrClass, |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3273 | bool instance) { |
Douglas Gregor | 70f449b | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 3274 | if (ExternalSource) |
| 3275 | ReadMethodPool(Sel); |
| 3276 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3277 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
Douglas Gregor | 70f449b | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 3278 | if (Pos == MethodPool.end()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3279 | return nullptr; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3280 | |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3281 | // Gather the non-hidden methods. |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3282 | ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second; |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 3283 | SmallVector<ObjCMethodDecl *, 4> Methods; |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 3284 | for (ObjCMethodList *M = &MethList; M; M = M->getNext()) { |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3285 | if (M->getMethod() && !M->getMethod()->isHidden()) |
| 3286 | return M->getMethod(); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3287 | } |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3288 | return nullptr; |
| 3289 | } |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3290 | |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3291 | void Sema::DiagnoseMultipleMethodInGlobalPool(SmallVectorImpl<ObjCMethodDecl*> &Methods, |
| 3292 | Selector Sel, SourceRange R, |
| 3293 | bool receiverIdOrClass) { |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3294 | // We found multiple methods, so we may have to complain. |
| 3295 | bool issueDiagnostic = false, issueError = false; |
Jonathan Roelofs | 7441136 | 2015-04-28 18:04:44 +0000 | [diff] [blame] | 3296 | |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3297 | // We support a warning which complains about *any* difference in |
| 3298 | // method signature. |
| 3299 | bool strictSelectorMatch = |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3300 | receiverIdOrClass && |
| 3301 | !Diags.isIgnored(diag::warn_strict_multiple_method_decl, R.getBegin()); |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3302 | if (strictSelectorMatch) { |
| 3303 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 3304 | if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) { |
| 3305 | issueDiagnostic = true; |
| 3306 | break; |
| 3307 | } |
| 3308 | } |
| 3309 | } |
Jonathan Roelofs | 7441136 | 2015-04-28 18:04:44 +0000 | [diff] [blame] | 3310 | |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3311 | // If we didn't see any strict differences, we won't see any loose |
| 3312 | // differences. In ARC, however, we also need to check for loose |
| 3313 | // mismatches, because most of them are errors. |
| 3314 | if (!strictSelectorMatch || |
| 3315 | (issueDiagnostic && getLangOpts().ObjCAutoRefCount)) |
| 3316 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 3317 | // This checks if the methods differ in type mismatch. |
| 3318 | if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) && |
| 3319 | !isAcceptableMethodMismatch(Methods[0], Methods[I])) { |
| 3320 | issueDiagnostic = true; |
| 3321 | if (getLangOpts().ObjCAutoRefCount) |
| 3322 | issueError = true; |
| 3323 | break; |
| 3324 | } |
| 3325 | } |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3326 | |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3327 | if (issueDiagnostic) { |
| 3328 | if (issueError) |
| 3329 | Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R; |
| 3330 | else if (strictSelectorMatch) |
| 3331 | Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R; |
| 3332 | else |
| 3333 | Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3334 | |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3335 | Diag(Methods[0]->getLocStart(), |
| 3336 | issueError ? diag::note_possibility : diag::note_using) |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3337 | << Methods[0]->getSourceRange(); |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3338 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 3339 | Diag(Methods[I]->getLocStart(), diag::note_also_found) |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3340 | << Methods[I]->getSourceRange(); |
| 3341 | } |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3342 | } |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3343 | } |
| 3344 | |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 3345 | ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) { |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3346 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
| 3347 | if (Pos == MethodPool.end()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3348 | return nullptr; |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3349 | |
| 3350 | GlobalMethods &Methods = Pos->second; |
Fariborz Jahanian | ec762bd | 2014-03-26 20:59:26 +0000 | [diff] [blame] | 3351 | for (const ObjCMethodList *Method = &Methods.first; Method; |
| 3352 | Method = Method->getNext()) |
Fariborz Jahanian | 4019c7f | 2015-02-19 21:52:41 +0000 | [diff] [blame] | 3353 | if (Method->getMethod() && |
| 3354 | (Method->getMethod()->isDefined() || |
| 3355 | Method->getMethod()->isPropertyAccessor())) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3356 | return Method->getMethod(); |
Fariborz Jahanian | ec762bd | 2014-03-26 20:59:26 +0000 | [diff] [blame] | 3357 | |
| 3358 | for (const ObjCMethodList *Method = &Methods.second; Method; |
| 3359 | Method = Method->getNext()) |
Fariborz Jahanian | 4019c7f | 2015-02-19 21:52:41 +0000 | [diff] [blame] | 3360 | if (Method->getMethod() && |
| 3361 | (Method->getMethod()->isDefined() || |
| 3362 | Method->getMethod()->isPropertyAccessor())) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3363 | return Method->getMethod(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3364 | return nullptr; |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 3365 | } |
| 3366 | |
Fariborz Jahanian | 42f8938 | 2013-05-30 21:48:58 +0000 | [diff] [blame] | 3367 | static void |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3368 | HelperSelectorsForTypoCorrection( |
| 3369 | SmallVectorImpl<const ObjCMethodDecl *> &BestMethod, |
| 3370 | StringRef Typo, const ObjCMethodDecl * Method) { |
| 3371 | const unsigned MaxEditDistance = 1; |
| 3372 | unsigned BestEditDistance = MaxEditDistance + 1; |
Richard Trieu | ea8d370 | 2013-06-06 02:22:29 +0000 | [diff] [blame] | 3373 | std::string MethodName = Method->getSelector().getAsString(); |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3374 | |
| 3375 | unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size()); |
| 3376 | if (MinPossibleEditDistance > 0 && |
| 3377 | Typo.size() / MinPossibleEditDistance < 1) |
| 3378 | return; |
| 3379 | unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance); |
| 3380 | if (EditDistance > MaxEditDistance) |
| 3381 | return; |
| 3382 | if (EditDistance == BestEditDistance) |
| 3383 | BestMethod.push_back(Method); |
| 3384 | else if (EditDistance < BestEditDistance) { |
| 3385 | BestMethod.clear(); |
| 3386 | BestMethod.push_back(Method); |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3387 | } |
| 3388 | } |
| 3389 | |
Fariborz Jahanian | 7548167 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 3390 | static bool HelperIsMethodInObjCType(Sema &S, Selector Sel, |
| 3391 | QualType ObjectType) { |
| 3392 | if (ObjectType.isNull()) |
| 3393 | return true; |
| 3394 | if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/)) |
| 3395 | return true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3396 | return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) != |
| 3397 | nullptr; |
Fariborz Jahanian | 7548167 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 3398 | } |
| 3399 | |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3400 | const ObjCMethodDecl * |
Fariborz Jahanian | 7548167 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 3401 | Sema::SelectorsForTypoCorrection(Selector Sel, |
| 3402 | QualType ObjectType) { |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3403 | unsigned NumArgs = Sel.getNumArgs(); |
| 3404 | SmallVector<const ObjCMethodDecl *, 8> Methods; |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3405 | bool ObjectIsId = true, ObjectIsClass = true; |
| 3406 | if (ObjectType.isNull()) |
| 3407 | ObjectIsId = ObjectIsClass = false; |
| 3408 | else if (!ObjectType->isObjCObjectPointerType()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3409 | return nullptr; |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3410 | else if (const ObjCObjectPointerType *ObjCPtr = |
| 3411 | ObjectType->getAsObjCInterfacePointerType()) { |
| 3412 | ObjectType = QualType(ObjCPtr->getInterfaceType(), 0); |
| 3413 | ObjectIsId = ObjectIsClass = false; |
| 3414 | } |
| 3415 | else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType()) |
| 3416 | ObjectIsClass = false; |
| 3417 | else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType()) |
| 3418 | ObjectIsId = false; |
| 3419 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3420 | return nullptr; |
| 3421 | |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3422 | for (GlobalMethodPool::iterator b = MethodPool.begin(), |
| 3423 | e = MethodPool.end(); b != e; b++) { |
| 3424 | // instance methods |
| 3425 | for (ObjCMethodList *M = &b->second.first; M; M=M->getNext()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3426 | if (M->getMethod() && |
| 3427 | (M->getMethod()->getSelector().getNumArgs() == NumArgs) && |
| 3428 | (M->getMethod()->getSelector() != Sel)) { |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3429 | if (ObjectIsId) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3430 | Methods.push_back(M->getMethod()); |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3431 | else if (!ObjectIsClass && |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3432 | HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(), |
| 3433 | ObjectType)) |
| 3434 | Methods.push_back(M->getMethod()); |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3435 | } |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3436 | // class methods |
| 3437 | for (ObjCMethodList *M = &b->second.second; M; M=M->getNext()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3438 | if (M->getMethod() && |
| 3439 | (M->getMethod()->getSelector().getNumArgs() == NumArgs) && |
| 3440 | (M->getMethod()->getSelector() != Sel)) { |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3441 | if (ObjectIsClass) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3442 | Methods.push_back(M->getMethod()); |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3443 | else if (!ObjectIsId && |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3444 | HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(), |
| 3445 | ObjectType)) |
| 3446 | Methods.push_back(M->getMethod()); |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3447 | } |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3448 | } |
| 3449 | |
| 3450 | SmallVector<const ObjCMethodDecl *, 8> SelectedMethods; |
| 3451 | for (unsigned i = 0, e = Methods.size(); i < e; i++) { |
| 3452 | HelperSelectorsForTypoCorrection(SelectedMethods, |
| 3453 | Sel.getAsString(), Methods[i]); |
| 3454 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3455 | return (SelectedMethods.size() == 1) ? SelectedMethods[0] : nullptr; |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3456 | } |
| 3457 | |
Fariborz Jahanian | 42f8938 | 2013-05-30 21:48:58 +0000 | [diff] [blame] | 3458 | /// DiagnoseDuplicateIvars - |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 3459 | /// Check for duplicate ivars in the entire class at the start of |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 3460 | /// \@implementation. This becomes necesssary because class extension can |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 3461 | /// 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] | 3462 | /// class's \@implementation is seen. |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 3463 | void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID, |
| 3464 | ObjCInterfaceDecl *SID) { |
Aaron Ballman | 59abbd4 | 2014-03-13 21:09:43 +0000 | [diff] [blame] | 3465 | for (auto *Ivar : ID->ivars()) { |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 3466 | if (Ivar->isInvalidDecl()) |
| 3467 | continue; |
| 3468 | if (IdentifierInfo *II = Ivar->getIdentifier()) { |
| 3469 | ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II); |
| 3470 | if (prevIvar) { |
| 3471 | Diag(Ivar->getLocation(), diag::err_duplicate_member) << II; |
| 3472 | Diag(prevIvar->getLocation(), diag::note_previous_declaration); |
| 3473 | Ivar->setInvalidDecl(); |
| 3474 | } |
| 3475 | } |
| 3476 | } |
| 3477 | } |
| 3478 | |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3479 | Sema::ObjCContainerKind Sema::getObjCContainerKind() const { |
| 3480 | switch (CurContext->getDeclKind()) { |
| 3481 | case Decl::ObjCInterface: |
| 3482 | return Sema::OCK_Interface; |
| 3483 | case Decl::ObjCProtocol: |
| 3484 | return Sema::OCK_Protocol; |
| 3485 | case Decl::ObjCCategory: |
Benjamin Kramer | a008d3a | 2015-04-10 11:37:55 +0000 | [diff] [blame] | 3486 | if (cast<ObjCCategoryDecl>(CurContext)->IsClassExtension()) |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3487 | return Sema::OCK_ClassExtension; |
Benjamin Kramer | a008d3a | 2015-04-10 11:37:55 +0000 | [diff] [blame] | 3488 | return Sema::OCK_Category; |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3489 | case Decl::ObjCImplementation: |
| 3490 | return Sema::OCK_Implementation; |
| 3491 | case Decl::ObjCCategoryImpl: |
| 3492 | return Sema::OCK_CategoryImplementation; |
| 3493 | |
| 3494 | default: |
| 3495 | return Sema::OCK_None; |
| 3496 | } |
| 3497 | } |
| 3498 | |
Fariborz Jahanian | 0080fb5 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 3499 | // Note: For class/category implementations, allMethods is always null. |
Robert Wilhelm | 57c6711 | 2013-07-17 21:14:35 +0000 | [diff] [blame] | 3500 | Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods, |
Fariborz Jahanian | dfb7687 | 2013-07-17 00:05:08 +0000 | [diff] [blame] | 3501 | ArrayRef<DeclGroupPtrTy> allTUVars) { |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3502 | if (getObjCContainerKind() == Sema::OCK_None) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3503 | return nullptr; |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3504 | |
| 3505 | assert(AtEnd.isValid() && "Invalid location for '@end'"); |
| 3506 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 3507 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); |
| 3508 | Decl *ClassDecl = cast<Decl>(OCD); |
Fariborz Jahanian | 9290ede | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 3509 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3510 | bool isInterfaceDeclKind = |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 3511 | isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) |
| 3512 | || isa<ObjCProtocolDecl>(ClassDecl); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3513 | bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 3514 | |
Steve Naroff | 35c62ae | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 3515 | // FIXME: Remove these and use the ObjCContainerDecl/DeclContext. |
| 3516 | llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; |
| 3517 | llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; |
| 3518 | |
Fariborz Jahanian | 0080fb5 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 3519 | for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3520 | ObjCMethodDecl *Method = |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3521 | cast_or_null<ObjCMethodDecl>(allMethods[i]); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3522 | |
| 3523 | if (!Method) continue; // Already issued a diagnostic. |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 3524 | if (Method->isInstanceMethod()) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3525 | /// Check for instance method of the same name with incompatible types |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3526 | const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3527 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3528 | : false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3529 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 42b1e9e | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 3530 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3531 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3532 | << Method->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3533 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | 87e9275 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 3534 | Method->setInvalidDecl(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3535 | } else { |
Fariborz Jahanian | c17c86b | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 3536 | if (PrevMethod) { |
Argyrios Kyrtzidis | dcaaa21 | 2011-10-14 08:02:31 +0000 | [diff] [blame] | 3537 | Method->setAsRedeclaration(PrevMethod); |
Fariborz Jahanian | c17c86b | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 3538 | if (!Context.getSourceManager().isInSystemHeader( |
| 3539 | Method->getLocation())) |
| 3540 | Diag(Method->getLocation(), diag::warn_duplicate_method_decl) |
| 3541 | << Method->getDeclName(); |
| 3542 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 3543 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3544 | InsMap[Method->getSelector()] = Method; |
| 3545 | /// The following allows us to typecheck messages to "id". |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3546 | AddInstanceMethodToGlobalPool(Method); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3547 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3548 | } else { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3549 | /// Check for class method of the same name with incompatible types |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3550 | const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3551 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3552 | : false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3553 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 42b1e9e | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 3554 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3555 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3556 | << Method->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3557 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | 87e9275 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 3558 | Method->setInvalidDecl(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3559 | } else { |
Fariborz Jahanian | c17c86b | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 3560 | if (PrevMethod) { |
Argyrios Kyrtzidis | dcaaa21 | 2011-10-14 08:02:31 +0000 | [diff] [blame] | 3561 | Method->setAsRedeclaration(PrevMethod); |
Fariborz Jahanian | c17c86b | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 3562 | if (!Context.getSourceManager().isInSystemHeader( |
| 3563 | Method->getLocation())) |
| 3564 | Diag(Method->getLocation(), diag::warn_duplicate_method_decl) |
| 3565 | << Method->getDeclName(); |
| 3566 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 3567 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3568 | ClsMap[Method->getSelector()] = Method; |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3569 | AddFactoryMethodToGlobalPool(Method); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3570 | } |
| 3571 | } |
| 3572 | } |
Douglas Gregor | b898209 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 3573 | if (isa<ObjCInterfaceDecl>(ClassDecl)) { |
| 3574 | // Nothing to do here. |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 3575 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Fariborz Jahanian | 62293f4 | 2008-12-06 19:59:02 +0000 | [diff] [blame] | 3576 | // Categories are used to extend the class by declaring new methods. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3577 | // 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] | 3578 | // need to compare the added property to those in the class. |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 3579 | |
Fariborz Jahanian | c21f543 | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 3580 | if (C->IsClassExtension()) { |
| 3581 | ObjCInterfaceDecl *CCPrimary = C->getClassInterface(); |
| 3582 | DiagnoseClassExtensionDupMethods(C, CCPrimary); |
Fariborz Jahanian | c21f543 | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 3583 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3584 | } |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 3585 | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) { |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 3586 | if (CDecl->getIdentifier()) |
| 3587 | // ProcessPropertyDecl is responsible for diagnosing conflicts with any |
| 3588 | // user-defined setter/getter. It also synthesizes setter/getter methods |
| 3589 | // and adds them to the DeclContext and global method pools. |
Aaron Ballman | d174edf | 2014-03-13 19:11:50 +0000 | [diff] [blame] | 3590 | for (auto *I : CDecl->properties()) |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 3591 | ProcessPropertyDecl(I, CDecl); |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 3592 | CDecl->setAtEndRange(AtEnd); |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 3593 | } |
| 3594 | if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 3595 | IC->setAtEndRange(AtEnd); |
Fariborz Jahanian | 13e0c90 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 3596 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) { |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 3597 | // Any property declared in a class extension might have user |
| 3598 | // declared setter or getter in current class extension or one |
| 3599 | // of the other class extensions. Mark them as synthesized as |
| 3600 | // property will be synthesized when property with same name is |
| 3601 | // seen in the @implementation. |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 3602 | for (const auto *Ext : IDecl->visible_extensions()) { |
Aaron Ballman | d174edf | 2014-03-13 19:11:50 +0000 | [diff] [blame] | 3603 | for (const auto *Property : Ext->properties()) { |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 3604 | // Skip over properties declared @dynamic |
| 3605 | if (const ObjCPropertyImplDecl *PIDecl |
| 3606 | = IC->FindPropertyImplDecl(Property->getIdentifier())) |
| 3607 | if (PIDecl->getPropertyImplementation() |
| 3608 | == ObjCPropertyImplDecl::Dynamic) |
| 3609 | continue; |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 3610 | |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 3611 | for (const auto *Ext : IDecl->visible_extensions()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 3612 | if (ObjCMethodDecl *GetterMethod |
| 3613 | = Ext->getInstanceMethod(Property->getGetterName())) |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 3614 | GetterMethod->setPropertyAccessor(true); |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 3615 | if (!Property->isReadOnly()) |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 3616 | if (ObjCMethodDecl *SetterMethod |
| 3617 | = Ext->getInstanceMethod(Property->getSetterName())) |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 3618 | SetterMethod->setPropertyAccessor(true); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 3619 | } |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 3620 | } |
| 3621 | } |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 3622 | ImplMethodsVsClassMethods(S, IC, IDecl); |
Fariborz Jahanian | 13e0c90 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 3623 | AtomicPropertySetterGetterRules(IC, IDecl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3624 | DiagnoseOwningPropertyGetterSynthesis(IC); |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 3625 | DiagnoseUnusedBackingIvarInAccessor(S, IC); |
Fariborz Jahanian | 20cfff3 | 2015-03-11 16:59:48 +0000 | [diff] [blame] | 3626 | if (IDecl->hasDesignatedInitializers()) |
| 3627 | DiagnoseMissingDesignatedInitOverrides(IC, IDecl); |
Argyrios Kyrtzidis | db5ce0f | 2013-12-03 21:11:54 +0000 | [diff] [blame] | 3628 | |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 3629 | bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3630 | if (IDecl->getSuperClass() == nullptr) { |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 3631 | // This class has no superclass, so check that it has been marked with |
| 3632 | // __attribute((objc_root_class)). |
| 3633 | if (!HasRootClassAttr) { |
| 3634 | SourceLocation DeclLoc(IDecl->getLocation()); |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 3635 | SourceLocation SuperClassLoc(getLocForEndOfToken(DeclLoc)); |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 3636 | Diag(DeclLoc, diag::warn_objc_root_class_missing) |
| 3637 | << IDecl->getIdentifier(); |
| 3638 | // See if NSObject is in the current scope, and if it is, suggest |
| 3639 | // adding " : NSObject " to the class declaration. |
| 3640 | NamedDecl *IF = LookupSingleName(TUScope, |
| 3641 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject), |
| 3642 | DeclLoc, LookupOrdinaryName); |
| 3643 | ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
| 3644 | if (NSObjectDecl && NSObjectDecl->getDefinition()) { |
| 3645 | Diag(SuperClassLoc, diag::note_objc_needs_superclass) |
| 3646 | << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject "); |
| 3647 | } else { |
| 3648 | Diag(SuperClassLoc, diag::note_objc_needs_superclass); |
| 3649 | } |
| 3650 | } |
| 3651 | } else if (HasRootClassAttr) { |
| 3652 | // Complain that only root classes may have this attribute. |
| 3653 | Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass); |
| 3654 | } |
| 3655 | |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 3656 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 3657 | while (IDecl->getSuperClass()) { |
| 3658 | DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass()); |
| 3659 | IDecl = IDecl->getSuperClass(); |
| 3660 | } |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 3661 | } |
Fariborz Jahanian | 13e0c90 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 3662 | } |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 3663 | SetIvarInitializers(IC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3664 | } else if (ObjCCategoryImplDecl* CatImplClass = |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 3665 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 3666 | CatImplClass->setAtEndRange(AtEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3667 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3668 | // Find category interface decl and then check that all methods declared |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 3669 | // in this interface are implemented in the category @implementation. |
Chris Lattner | 41fd42e | 2009-02-16 18:32:47 +0000 | [diff] [blame] | 3670 | if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 3671 | if (ObjCCategoryDecl *Cat |
| 3672 | = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) { |
| 3673 | ImplMethodsVsClassMethods(S, CatImplClass, Cat); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3674 | } |
| 3675 | } |
| 3676 | } |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 3677 | if (isInterfaceDeclKind) { |
| 3678 | // Reject invalid vardecls. |
Fariborz Jahanian | 0080fb5 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 3679 | for (unsigned i = 0, e = allTUVars.size(); i != e; i++) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 3680 | DeclGroupRef DG = allTUVars[i].get(); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 3681 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 3682 | if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) { |
Daniel Dunbar | 0ca1660 | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 3683 | if (!VDecl->hasExternalStorage()) |
Steve Naroff | 42959b2 | 2009-04-13 17:58:46 +0000 | [diff] [blame] | 3684 | Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass); |
Fariborz Jahanian | 629aed9 | 2009-03-21 18:06:45 +0000 | [diff] [blame] | 3685 | } |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 3686 | } |
Fariborz Jahanian | 3654e65 | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 3687 | } |
Fariborz Jahanian | 4327b32 | 2011-08-29 17:33:12 +0000 | [diff] [blame] | 3688 | ActOnObjCContainerFinishDefinition(); |
Argyrios Kyrtzidis | bd8b150 | 2011-10-17 19:48:13 +0000 | [diff] [blame] | 3689 | |
Fariborz Jahanian | 0080fb5 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 3690 | for (unsigned i = 0, e = allTUVars.size(); i != e; i++) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 3691 | DeclGroupRef DG = allTUVars[i].get(); |
Argyrios Kyrtzidis | 8ad3bab | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 3692 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 3693 | (*I)->setTopLevelDeclInObjCContainer(); |
Argyrios Kyrtzidis | bd8b150 | 2011-10-17 19:48:13 +0000 | [diff] [blame] | 3694 | Consumer.HandleTopLevelDeclInObjCContainer(DG); |
| 3695 | } |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3696 | |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 3697 | ActOnDocumentableDecl(ClassDecl); |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3698 | return ClassDecl; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3699 | } |
| 3700 | |
| 3701 | |
| 3702 | /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for |
| 3703 | /// 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] | 3704 | static Decl::ObjCDeclQualifier |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3705 | CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { |
John McCall | ca87290 | 2011-05-01 03:04:29 +0000 | [diff] [blame] | 3706 | return (Decl::ObjCDeclQualifier) (unsigned) PQTVal; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3707 | } |
| 3708 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3709 | /// \brief Check whether the declared result type of the given Objective-C |
| 3710 | /// method declaration is compatible with the method's class. |
| 3711 | /// |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3712 | static Sema::ResultTypeCompatibilityKind |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3713 | CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method, |
| 3714 | ObjCInterfaceDecl *CurrentClass) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3715 | QualType ResultType = Method->getReturnType(); |
| 3716 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3717 | // If an Objective-C method inherits its related result type, then its |
| 3718 | // declared result type must be compatible with its own class type. The |
| 3719 | // declared result type is compatible if: |
| 3720 | if (const ObjCObjectPointerType *ResultObjectType |
| 3721 | = ResultType->getAs<ObjCObjectPointerType>()) { |
| 3722 | // - it is id or qualified id, or |
| 3723 | if (ResultObjectType->isObjCIdType() || |
| 3724 | ResultObjectType->isObjCQualifiedIdType()) |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3725 | return Sema::RTC_Compatible; |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3726 | |
| 3727 | if (CurrentClass) { |
| 3728 | if (ObjCInterfaceDecl *ResultClass |
| 3729 | = ResultObjectType->getInterfaceDecl()) { |
| 3730 | // - it is the same as the method's class type, or |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 3731 | if (declaresSameEntity(CurrentClass, ResultClass)) |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3732 | return Sema::RTC_Compatible; |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3733 | |
| 3734 | // - it is a superclass of the method's class type |
| 3735 | if (ResultClass->isSuperClassOf(CurrentClass)) |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3736 | return Sema::RTC_Compatible; |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3737 | } |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3738 | } else { |
| 3739 | // Any Objective-C pointer type might be acceptable for a protocol |
| 3740 | // method; we just don't know. |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3741 | return Sema::RTC_Unknown; |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3742 | } |
| 3743 | } |
| 3744 | |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3745 | return Sema::RTC_Incompatible; |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3746 | } |
| 3747 | |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3748 | namespace { |
| 3749 | /// A helper class for searching for methods which a particular method |
| 3750 | /// overrides. |
| 3751 | class OverrideSearch { |
Daniel Dunbar | d6d74c3 | 2012-02-29 03:04:05 +0000 | [diff] [blame] | 3752 | public: |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3753 | Sema &S; |
| 3754 | ObjCMethodDecl *Method; |
Daniel Dunbar | d6d74c3 | 2012-02-29 03:04:05 +0000 | [diff] [blame] | 3755 | llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3756 | bool Recursive; |
| 3757 | |
| 3758 | public: |
| 3759 | OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) { |
| 3760 | Selector selector = method->getSelector(); |
| 3761 | |
| 3762 | // Bypass this search if we've never seen an instance/class method |
| 3763 | // with this selector before. |
| 3764 | Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector); |
| 3765 | if (it == S.MethodPool.end()) { |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 3766 | if (!S.getExternalSource()) return; |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 3767 | S.ReadMethodPool(selector); |
| 3768 | |
| 3769 | it = S.MethodPool.find(selector); |
| 3770 | if (it == S.MethodPool.end()) |
| 3771 | return; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3772 | } |
| 3773 | ObjCMethodList &list = |
| 3774 | method->isInstanceMethod() ? it->second.first : it->second.second; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3775 | if (!list.getMethod()) return; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3776 | |
| 3777 | ObjCContainerDecl *container |
| 3778 | = cast<ObjCContainerDecl>(method->getDeclContext()); |
| 3779 | |
| 3780 | // Prevent the search from reaching this container again. This is |
| 3781 | // important with categories, which override methods from the |
| 3782 | // interface and each other. |
Douglas Gregor | cf4ac44 | 2012-05-03 21:25:24 +0000 | [diff] [blame] | 3783 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) { |
| 3784 | searchFromContainer(container); |
Douglas Gregor | c5928af | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 3785 | if (ObjCInterfaceDecl *Interface = Category->getClassInterface()) |
| 3786 | searchFromContainer(Interface); |
Douglas Gregor | cf4ac44 | 2012-05-03 21:25:24 +0000 | [diff] [blame] | 3787 | } else { |
| 3788 | searchFromContainer(container); |
| 3789 | } |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3790 | } |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3791 | |
Daniel Dunbar | d6d74c3 | 2012-02-29 03:04:05 +0000 | [diff] [blame] | 3792 | typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3793 | iterator begin() const { return Overridden.begin(); } |
| 3794 | iterator end() const { return Overridden.end(); } |
| 3795 | |
| 3796 | private: |
| 3797 | void searchFromContainer(ObjCContainerDecl *container) { |
| 3798 | if (container->isInvalidDecl()) return; |
| 3799 | |
| 3800 | switch (container->getDeclKind()) { |
| 3801 | #define OBJCCONTAINER(type, base) \ |
| 3802 | case Decl::type: \ |
| 3803 | searchFrom(cast<type##Decl>(container)); \ |
| 3804 | break; |
| 3805 | #define ABSTRACT_DECL(expansion) |
| 3806 | #define DECL(type, base) \ |
| 3807 | case Decl::type: |
| 3808 | #include "clang/AST/DeclNodes.inc" |
| 3809 | llvm_unreachable("not an ObjC container!"); |
| 3810 | } |
| 3811 | } |
| 3812 | |
| 3813 | void searchFrom(ObjCProtocolDecl *protocol) { |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 3814 | if (!protocol->hasDefinition()) |
| 3815 | return; |
| 3816 | |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3817 | // A method in a protocol declaration overrides declarations from |
| 3818 | // referenced ("parent") protocols. |
| 3819 | search(protocol->getReferencedProtocols()); |
| 3820 | } |
| 3821 | |
| 3822 | void searchFrom(ObjCCategoryDecl *category) { |
| 3823 | // A method in a category declaration overrides declarations from |
| 3824 | // the main class and from protocols the category references. |
Douglas Gregor | cf4ac44 | 2012-05-03 21:25:24 +0000 | [diff] [blame] | 3825 | // The main class is handled in the constructor. |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3826 | search(category->getReferencedProtocols()); |
| 3827 | } |
| 3828 | |
| 3829 | void searchFrom(ObjCCategoryImplDecl *impl) { |
| 3830 | // A method in a category definition that has a category |
| 3831 | // declaration overrides declarations from the category |
| 3832 | // declaration. |
| 3833 | if (ObjCCategoryDecl *category = impl->getCategoryDecl()) { |
| 3834 | search(category); |
Douglas Gregor | c5928af | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 3835 | if (ObjCInterfaceDecl *Interface = category->getClassInterface()) |
| 3836 | search(Interface); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3837 | |
| 3838 | // Otherwise it overrides declarations from the class. |
Douglas Gregor | c5928af | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 3839 | } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) { |
| 3840 | search(Interface); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3841 | } |
| 3842 | } |
| 3843 | |
| 3844 | void searchFrom(ObjCInterfaceDecl *iface) { |
| 3845 | // A method in a class declaration overrides declarations from |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 3846 | if (!iface->hasDefinition()) |
| 3847 | return; |
| 3848 | |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3849 | // - categories, |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 3850 | for (auto *Cat : iface->known_categories()) |
| 3851 | search(Cat); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3852 | |
| 3853 | // - the super class, and |
| 3854 | if (ObjCInterfaceDecl *super = iface->getSuperClass()) |
| 3855 | search(super); |
| 3856 | |
| 3857 | // - any referenced protocols. |
| 3858 | search(iface->getReferencedProtocols()); |
| 3859 | } |
| 3860 | |
| 3861 | void searchFrom(ObjCImplementationDecl *impl) { |
| 3862 | // A method in a class implementation overrides declarations from |
| 3863 | // the class interface. |
Douglas Gregor | c5928af | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 3864 | if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) |
| 3865 | search(Interface); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3866 | } |
| 3867 | |
| 3868 | |
| 3869 | void search(const ObjCProtocolList &protocols) { |
| 3870 | for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end(); |
| 3871 | i != e; ++i) |
| 3872 | search(*i); |
| 3873 | } |
| 3874 | |
| 3875 | void search(ObjCContainerDecl *container) { |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3876 | // Check for a method in this container which matches this selector. |
| 3877 | ObjCMethodDecl *meth = container->getMethod(Method->getSelector(), |
Argyrios Kyrtzidis | bd8cd3e | 2013-03-29 21:51:48 +0000 | [diff] [blame] | 3878 | Method->isInstanceMethod(), |
| 3879 | /*AllowHidden=*/true); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3880 | |
| 3881 | // If we find one, record it and bail out. |
| 3882 | if (meth) { |
| 3883 | Overridden.insert(meth); |
| 3884 | return; |
| 3885 | } |
| 3886 | |
| 3887 | // Otherwise, search for methods that a hypothetical method here |
| 3888 | // would have overridden. |
| 3889 | |
| 3890 | // Note that we're now in a recursive case. |
| 3891 | Recursive = true; |
| 3892 | |
| 3893 | searchFromContainer(container); |
| 3894 | } |
| 3895 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3896 | } |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3897 | |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3898 | void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod, |
| 3899 | ObjCInterfaceDecl *CurrentClass, |
| 3900 | ResultTypeCompatibilityKind RTC) { |
| 3901 | // Search for overridden methods and merge information down from them. |
| 3902 | OverrideSearch overrides(*this, ObjCMethod); |
| 3903 | // Keep track if the method overrides any method in the class's base classes, |
| 3904 | // its protocols, or its categories' protocols; we will keep that info |
| 3905 | // in the ObjCMethodDecl. |
| 3906 | // For this info, a method in an implementation is not considered as |
| 3907 | // overriding the same method in the interface or its categories. |
| 3908 | bool hasOverriddenMethodsInBaseOrProtocol = false; |
| 3909 | for (OverrideSearch::iterator |
| 3910 | i = overrides.begin(), e = overrides.end(); i != e; ++i) { |
| 3911 | ObjCMethodDecl *overridden = *i; |
| 3912 | |
Argyrios Kyrtzidis | c2091d5 | 2013-04-17 00:09:08 +0000 | [diff] [blame] | 3913 | if (!hasOverriddenMethodsInBaseOrProtocol) { |
| 3914 | if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) || |
| 3915 | CurrentClass != overridden->getClassInterface() || |
| 3916 | overridden->isOverriding()) { |
| 3917 | hasOverriddenMethodsInBaseOrProtocol = true; |
| 3918 | |
| 3919 | } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) { |
| 3920 | // OverrideSearch will return as "overridden" the same method in the |
| 3921 | // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to |
| 3922 | // check whether a category of a base class introduced a method with the |
| 3923 | // same selector, after the interface method declaration. |
| 3924 | // To avoid unnecessary lookups in the majority of cases, we use the |
| 3925 | // extra info bits in GlobalMethodPool to check whether there were any |
| 3926 | // category methods with this selector. |
| 3927 | GlobalMethodPool::iterator It = |
| 3928 | MethodPool.find(ObjCMethod->getSelector()); |
| 3929 | if (It != MethodPool.end()) { |
| 3930 | ObjCMethodList &List = |
| 3931 | ObjCMethod->isInstanceMethod()? It->second.first: It->second.second; |
| 3932 | unsigned CategCount = List.getBits(); |
| 3933 | if (CategCount > 0) { |
| 3934 | // If the method is in a category we'll do lookup if there were at |
| 3935 | // least 2 category methods recorded, otherwise only one will do. |
| 3936 | if (CategCount > 1 || |
| 3937 | !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) { |
| 3938 | OverrideSearch overrides(*this, overridden); |
| 3939 | for (OverrideSearch::iterator |
| 3940 | OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) { |
| 3941 | ObjCMethodDecl *SuperOverridden = *OI; |
Argyrios Kyrtzidis | 04703a6 | 2013-04-27 00:10:12 +0000 | [diff] [blame] | 3942 | if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) || |
| 3943 | CurrentClass != SuperOverridden->getClassInterface()) { |
Argyrios Kyrtzidis | c2091d5 | 2013-04-17 00:09:08 +0000 | [diff] [blame] | 3944 | hasOverriddenMethodsInBaseOrProtocol = true; |
| 3945 | overridden->setOverriding(true); |
| 3946 | break; |
| 3947 | } |
| 3948 | } |
| 3949 | } |
| 3950 | } |
| 3951 | } |
| 3952 | } |
| 3953 | } |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3954 | |
| 3955 | // Propagate down the 'related result type' bit from overridden methods. |
| 3956 | if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType()) |
| 3957 | ObjCMethod->SetRelatedResultType(); |
| 3958 | |
| 3959 | // Then merge the declarations. |
| 3960 | mergeObjCMethodDecls(ObjCMethod, overridden); |
| 3961 | |
| 3962 | if (ObjCMethod->isImplicit() && overridden->isImplicit()) |
| 3963 | continue; // Conflicting properties are detected elsewhere. |
| 3964 | |
| 3965 | // Check for overriding methods |
| 3966 | if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) || |
| 3967 | isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) |
| 3968 | CheckConflictingOverridingMethod(ObjCMethod, overridden, |
| 3969 | isa<ObjCProtocolDecl>(overridden->getDeclContext())); |
| 3970 | |
| 3971 | if (CurrentClass && overridden->getDeclContext() != CurrentClass && |
Fariborz Jahanian | 31a2568 | 2012-07-05 22:26:07 +0000 | [diff] [blame] | 3972 | isa<ObjCInterfaceDecl>(overridden->getDeclContext()) && |
| 3973 | !overridden->isImplicit() /* not meant for properties */) { |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3974 | ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(), |
| 3975 | E = ObjCMethod->param_end(); |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 3976 | ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(), |
| 3977 | PrevE = overridden->param_end(); |
| 3978 | for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) { |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3979 | assert(PrevI != overridden->param_end() && "Param mismatch"); |
| 3980 | QualType T1 = Context.getCanonicalType((*ParamI)->getType()); |
| 3981 | QualType T2 = Context.getCanonicalType((*PrevI)->getType()); |
| 3982 | // If type of argument of method in this class does not match its |
| 3983 | // respective argument type in the super class method, issue warning; |
| 3984 | if (!Context.typesAreCompatible(T1, T2)) { |
| 3985 | Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super) |
| 3986 | << T1 << T2; |
| 3987 | Diag(overridden->getLocation(), diag::note_previous_declaration); |
| 3988 | break; |
| 3989 | } |
| 3990 | } |
| 3991 | } |
| 3992 | } |
| 3993 | |
| 3994 | ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol); |
| 3995 | } |
| 3996 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 3997 | /// Merge type nullability from for a redeclaration of the same entity, |
| 3998 | /// producing the updated type of the redeclared entity. |
| 3999 | static QualType mergeTypeNullabilityForRedecl(Sema &S, SourceLocation loc, |
| 4000 | QualType type, |
| 4001 | bool usesCSKeyword, |
| 4002 | SourceLocation prevLoc, |
| 4003 | QualType prevType, |
| 4004 | bool prevUsesCSKeyword) { |
| 4005 | // Determine the nullability of both types. |
| 4006 | auto nullability = type->getNullability(S.Context); |
| 4007 | auto prevNullability = prevType->getNullability(S.Context); |
| 4008 | |
| 4009 | // Easy case: both have nullability. |
| 4010 | if (nullability.hasValue() == prevNullability.hasValue()) { |
| 4011 | // Neither has nullability; continue. |
| 4012 | if (!nullability) |
| 4013 | return type; |
| 4014 | |
| 4015 | // The nullabilities are equivalent; do nothing. |
| 4016 | if (*nullability == *prevNullability) |
| 4017 | return type; |
| 4018 | |
| 4019 | // Complain about mismatched nullability. |
| 4020 | S.Diag(loc, diag::err_nullability_conflicting) |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 4021 | << DiagNullabilityKind(*nullability, usesCSKeyword) |
| 4022 | << DiagNullabilityKind(*prevNullability, prevUsesCSKeyword); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4023 | return type; |
| 4024 | } |
| 4025 | |
| 4026 | // If it's the redeclaration that has nullability, don't change anything. |
| 4027 | if (nullability) |
| 4028 | return type; |
| 4029 | |
| 4030 | // Otherwise, provide the result with the same nullability. |
| 4031 | return S.Context.getAttributedType( |
| 4032 | AttributedType::getNullabilityAttrKind(*prevNullability), |
| 4033 | type, type); |
| 4034 | } |
| 4035 | |
NAKAMURA Takumi | 2df5c3c | 2015-06-20 03:52:52 +0000 | [diff] [blame] | 4036 | /// Merge information from the declaration of a method in the \@interface |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4037 | /// (or a category/extension) into the corresponding method in the |
| 4038 | /// @implementation (for a class or category). |
| 4039 | static void mergeInterfaceMethodToImpl(Sema &S, |
| 4040 | ObjCMethodDecl *method, |
| 4041 | ObjCMethodDecl *prevMethod) { |
| 4042 | // Merge the objc_requires_super attribute. |
| 4043 | if (prevMethod->hasAttr<ObjCRequiresSuperAttr>() && |
| 4044 | !method->hasAttr<ObjCRequiresSuperAttr>()) { |
| 4045 | // merge the attribute into implementation. |
| 4046 | method->addAttr( |
| 4047 | ObjCRequiresSuperAttr::CreateImplicit(S.Context, |
| 4048 | method->getLocation())); |
| 4049 | } |
| 4050 | |
| 4051 | // Merge nullability of the result type. |
| 4052 | QualType newReturnType |
| 4053 | = mergeTypeNullabilityForRedecl( |
| 4054 | S, method->getReturnTypeSourceRange().getBegin(), |
| 4055 | method->getReturnType(), |
| 4056 | method->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability, |
| 4057 | prevMethod->getReturnTypeSourceRange().getBegin(), |
| 4058 | prevMethod->getReturnType(), |
| 4059 | prevMethod->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability); |
| 4060 | method->setReturnType(newReturnType); |
| 4061 | |
| 4062 | // Handle each of the parameters. |
| 4063 | unsigned numParams = method->param_size(); |
| 4064 | unsigned numPrevParams = prevMethod->param_size(); |
| 4065 | for (unsigned i = 0, n = std::min(numParams, numPrevParams); i != n; ++i) { |
| 4066 | ParmVarDecl *param = method->param_begin()[i]; |
| 4067 | ParmVarDecl *prevParam = prevMethod->param_begin()[i]; |
| 4068 | |
| 4069 | // Merge nullability. |
| 4070 | QualType newParamType |
| 4071 | = mergeTypeNullabilityForRedecl( |
| 4072 | S, param->getLocation(), param->getType(), |
| 4073 | param->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability, |
| 4074 | prevParam->getLocation(), prevParam->getType(), |
| 4075 | prevParam->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability); |
| 4076 | param->setType(newParamType); |
| 4077 | } |
| 4078 | } |
| 4079 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4080 | Decl *Sema::ActOnMethodDeclaration( |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4081 | Scope *S, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4082 | SourceLocation MethodLoc, SourceLocation EndLoc, |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4083 | tok::TokenKind MethodType, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4084 | ObjCDeclSpec &ReturnQT, ParsedType ReturnType, |
Argyrios Kyrtzidis | dfd6570 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 4085 | ArrayRef<SourceLocation> SelectorLocs, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4086 | Selector Sel, |
| 4087 | // optional arguments. The number of types/arguments is obtained |
| 4088 | // from the Sel.getNumArgs(). |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 4089 | ObjCArgInfo *ArgInfo, |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 4090 | DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4091 | AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, |
Fariborz Jahanian | c677f69 | 2011-03-12 18:54:30 +0000 | [diff] [blame] | 4092 | bool isVariadic, bool MethodDefinition) { |
Steve Naroff | 83777fe | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 4093 | // Make sure we can establish a context for the method. |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4094 | if (!CurContext->isObjCContainer()) { |
Steve Naroff | 83777fe | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 4095 | Diag(MethodLoc, diag::error_missing_method_context); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4096 | return nullptr; |
Steve Naroff | 83777fe | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 4097 | } |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4098 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); |
| 4099 | Decl *ClassDecl = cast<Decl>(OCD); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4100 | QualType resultDeclType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4101 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4102 | bool HasRelatedResultType = false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4103 | TypeSourceInfo *ReturnTInfo = nullptr; |
Steve Naroff | 3260641 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 4104 | if (ReturnType) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4105 | resultDeclType = GetTypeFromParser(ReturnType, &ReturnTInfo); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4106 | |
Eli Friedman | 31a5bcc | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 4107 | if (CheckFunctionReturnType(resultDeclType, MethodLoc)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4108 | return nullptr; |
Eli Friedman | 31a5bcc | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 4109 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4110 | QualType bareResultType = resultDeclType; |
| 4111 | (void)AttributedType::stripOuterNullability(bareResultType); |
| 4112 | HasRelatedResultType = (bareResultType == Context.getObjCInstanceType()); |
Fariborz Jahanian | b5a52ca | 2011-07-21 17:00:47 +0000 | [diff] [blame] | 4113 | } else { // get the type for "id". |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4114 | resultDeclType = Context.getObjCIdType(); |
Fariborz Jahanian | b21138f | 2011-07-21 17:38:14 +0000 | [diff] [blame] | 4115 | Diag(MethodLoc, diag::warn_missing_method_return_type) |
Argyrios Kyrtzidis | dfd6570 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 4116 | << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)"); |
Fariborz Jahanian | b5a52ca | 2011-07-21 17:00:47 +0000 | [diff] [blame] | 4117 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4118 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4119 | ObjCMethodDecl *ObjCMethod = ObjCMethodDecl::Create( |
| 4120 | Context, MethodLoc, EndLoc, Sel, resultDeclType, ReturnTInfo, CurContext, |
| 4121 | MethodType == tok::minus, isVariadic, |
| 4122 | /*isPropertyAccessor=*/false, |
| 4123 | /*isImplicitlyDeclared=*/false, /*isDefined=*/false, |
| 4124 | MethodDeclKind == tok::objc_optional ? ObjCMethodDecl::Optional |
| 4125 | : ObjCMethodDecl::Required, |
| 4126 | HasRelatedResultType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4127 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4128 | SmallVector<ParmVarDecl*, 16> Params; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4129 | |
Chris Lattner | 23b0faf | 2009-04-11 19:42:43 +0000 | [diff] [blame] | 4130 | for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) { |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 4131 | QualType ArgType; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4132 | TypeSourceInfo *DI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4133 | |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 4134 | if (!ArgInfo[i].Type) { |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 4135 | ArgType = Context.getObjCIdType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4136 | DI = nullptr; |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 4137 | } else { |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 4138 | ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI); |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 4139 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4140 | |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4141 | LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc, |
| 4142 | LookupOrdinaryName, ForRedeclaration); |
| 4143 | LookupName(R, S); |
| 4144 | if (R.isSingleResult()) { |
| 4145 | NamedDecl *PrevDecl = R.getFoundDecl(); |
| 4146 | if (S->isDeclScope(PrevDecl)) { |
Fariborz Jahanian | c677f69 | 2011-03-12 18:54:30 +0000 | [diff] [blame] | 4147 | Diag(ArgInfo[i].NameLoc, |
| 4148 | (MethodDefinition ? diag::warn_method_param_redefinition |
| 4149 | : diag::warn_method_param_declaration)) |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4150 | << ArgInfo[i].Name; |
| 4151 | Diag(PrevDecl->getLocation(), |
| 4152 | diag::note_previous_declaration); |
| 4153 | } |
| 4154 | } |
| 4155 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4156 | SourceLocation StartLoc = DI |
| 4157 | ? DI->getTypeLoc().getBeginLoc() |
| 4158 | : ArgInfo[i].NameLoc; |
| 4159 | |
John McCall | d44f4d7 | 2011-04-23 02:46:06 +0000 | [diff] [blame] | 4160 | ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc, |
| 4161 | ArgInfo[i].NameLoc, ArgInfo[i].Name, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4162 | ArgType, DI, SC_None); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4163 | |
John McCall | 8249083 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 4164 | Param->setObjCMethodScopeInfo(i); |
| 4165 | |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 4166 | Param->setObjCDeclQualifier( |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 4167 | CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4168 | |
Chris Lattner | 9713a1c | 2009-04-11 19:34:56 +0000 | [diff] [blame] | 4169 | // Apply the attributes to the parameter. |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4170 | ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4171 | |
Fariborz Jahanian | 52d02f6 | 2012-01-14 18:44:35 +0000 | [diff] [blame] | 4172 | if (Param->hasAttr<BlocksAttr>()) { |
| 4173 | Diag(Param->getLocation(), diag::err_block_on_nonlocal); |
| 4174 | Param->setInvalidDecl(); |
| 4175 | } |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4176 | S->AddDecl(Param); |
| 4177 | IdResolver.AddDecl(Param); |
| 4178 | |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 4179 | Params.push_back(Param); |
| 4180 | } |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4181 | |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 4182 | for (unsigned i = 0, e = CNumArgs; i != e; ++i) { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4183 | ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param); |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 4184 | QualType ArgType = Param->getType(); |
| 4185 | if (ArgType.isNull()) |
| 4186 | ArgType = Context.getObjCIdType(); |
| 4187 | else |
| 4188 | // 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] | 4189 | ArgType = Context.getAdjustedParameterType(ArgType); |
Eli Friedman | 31a5bcc | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 4190 | |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 4191 | Param->setDeclContext(ObjCMethod); |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 4192 | Params.push_back(Param); |
| 4193 | } |
| 4194 | |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4195 | ObjCMethod->setMethodParams(Context, Params, SelectorLocs); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4196 | ObjCMethod->setObjCDeclQualifier( |
| 4197 | CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 4198 | |
| 4199 | if (AttrList) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4200 | ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4201 | |
Douglas Gregor | 87e9275 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 4202 | // Add the method now. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4203 | const ObjCMethodDecl *PrevMethod = nullptr; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4204 | if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4205 | if (MethodType == tok::minus) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4206 | PrevMethod = ImpDecl->getInstanceMethod(Sel); |
| 4207 | ImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4208 | } else { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4209 | PrevMethod = ImpDecl->getClassMethod(Sel); |
| 4210 | ImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4211 | } |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4212 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4213 | // Merge information from the @interface declaration into the |
| 4214 | // @implementation. |
| 4215 | if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface()) { |
| 4216 | if (auto *IMD = IDecl->lookupMethod(ObjCMethod->getSelector(), |
| 4217 | ObjCMethod->isInstanceMethod())) { |
| 4218 | mergeInterfaceMethodToImpl(*this, ObjCMethod, IMD); |
| 4219 | |
| 4220 | // Warn about defining -dealloc in a category. |
| 4221 | if (isa<ObjCCategoryImplDecl>(ImpDecl) && IMD->isOverriding() && |
| 4222 | ObjCMethod->getSelector().getMethodFamily() == OMF_dealloc) { |
| 4223 | Diag(ObjCMethod->getLocation(), diag::warn_dealloc_in_category) |
| 4224 | << ObjCMethod->getDeclName(); |
| 4225 | } |
| 4226 | } |
Fariborz Jahanian | 7e350d2 | 2013-12-17 22:44:28 +0000 | [diff] [blame] | 4227 | } |
Douglas Gregor | 87e9275 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 4228 | } else { |
| 4229 | cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4230 | } |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4231 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4232 | if (PrevMethod) { |
| 4233 | // You can never have two method definitions with the same name. |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 4234 | Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4235 | << ObjCMethod->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 4236 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Fariborz Jahanian | 096f7c1 | 2013-05-13 17:27:00 +0000 | [diff] [blame] | 4237 | ObjCMethod->setInvalidDecl(); |
| 4238 | return ObjCMethod; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4239 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4240 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4241 | // If this Objective-C method does not have a related result type, but we |
| 4242 | // are allowed to infer related result types, try to do so based on the |
| 4243 | // method family. |
| 4244 | ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl); |
| 4245 | if (!CurrentClass) { |
| 4246 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) |
| 4247 | CurrentClass = Cat->getClassInterface(); |
| 4248 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl)) |
| 4249 | CurrentClass = Impl->getClassInterface(); |
| 4250 | else if (ObjCCategoryImplDecl *CatImpl |
| 4251 | = dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) |
| 4252 | CurrentClass = CatImpl->getClassInterface(); |
| 4253 | } |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4254 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4255 | ResultTypeCompatibilityKind RTC |
| 4256 | = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4257 | |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4258 | CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4259 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4260 | bool ARCError = false; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4261 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | e48f389 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 4262 | ARCError = CheckARCMethodDecl(ObjCMethod); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4263 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4264 | // Infer the related result type when possible. |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4265 | if (!ARCError && RTC == Sema::RTC_Compatible && |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4266 | !ObjCMethod->hasRelatedResultType() && |
| 4267 | LangOpts.ObjCInferRelatedResultType) { |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4268 | bool InferRelatedResultType = false; |
| 4269 | switch (ObjCMethod->getMethodFamily()) { |
| 4270 | case OMF_None: |
| 4271 | case OMF_copy: |
| 4272 | case OMF_dealloc: |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 4273 | case OMF_finalize: |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4274 | case OMF_mutableCopy: |
| 4275 | case OMF_release: |
| 4276 | case OMF_retainCount: |
Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 4277 | case OMF_initialize: |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 4278 | case OMF_performSelector: |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4279 | break; |
| 4280 | |
| 4281 | case OMF_alloc: |
| 4282 | case OMF_new: |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 4283 | InferRelatedResultType = ObjCMethod->isClassMethod(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4284 | break; |
| 4285 | |
| 4286 | case OMF_init: |
| 4287 | case OMF_autorelease: |
| 4288 | case OMF_retain: |
| 4289 | case OMF_self: |
| 4290 | InferRelatedResultType = ObjCMethod->isInstanceMethod(); |
| 4291 | break; |
| 4292 | } |
| 4293 | |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 4294 | if (InferRelatedResultType && |
| 4295 | !ObjCMethod->getReturnType()->isObjCIndependentClassType()) |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4296 | ObjCMethod->SetRelatedResultType(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4297 | } |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 4298 | |
| 4299 | ActOnDocumentableDecl(ObjCMethod); |
| 4300 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4301 | return ObjCMethod; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4302 | } |
| 4303 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4304 | bool Sema::CheckObjCDeclScope(Decl *D) { |
Fariborz Jahanian | f36734d | 2011-08-22 18:34:22 +0000 | [diff] [blame] | 4305 | // Following is also an error. But it is caused by a missing @end |
| 4306 | // and diagnostic is issued elsewhere. |
Argyrios Kyrtzidis | 822c433 | 2012-03-23 23:24:23 +0000 | [diff] [blame] | 4307 | if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4308 | return false; |
Argyrios Kyrtzidis | 822c433 | 2012-03-23 23:24:23 +0000 | [diff] [blame] | 4309 | |
| 4310 | // If we switched context to translation unit while we are still lexically in |
| 4311 | // an objc container, it means the parser missed emitting an error. |
| 4312 | if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext())) |
| 4313 | return false; |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4314 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 4315 | Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); |
| 4316 | D->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4317 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 4318 | return true; |
| 4319 | } |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4320 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4321 | /// Called whenever \@defs(ClassName) is encountered in the source. Inserts the |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4322 | /// instance variables of ClassName into Decls. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4323 | void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart, |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4324 | IdentifierInfo *ClassName, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4325 | SmallVectorImpl<Decl*> &Decls) { |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4326 | // Check that ClassName is a valid class |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4327 | ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4328 | if (!Class) { |
| 4329 | Diag(DeclStart, diag::err_undef_interface) << ClassName; |
| 4330 | return; |
| 4331 | } |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 4332 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | ece1b2b | 2009-04-21 20:28:41 +0000 | [diff] [blame] | 4333 | Diag(DeclStart, diag::err_atdef_nonfragile_interface); |
| 4334 | return; |
| 4335 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4336 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4337 | // Collect the instance variables |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4338 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4339 | Context.DeepCollectObjCIvars(Class, true, Ivars); |
Fariborz Jahanian | 7dae114 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 4340 | // For each ivar, create a fresh ObjCAtDefsFieldDecl. |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4341 | for (unsigned i = 0; i < Ivars.size(); i++) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4342 | const FieldDecl* ID = cast<FieldDecl>(Ivars[i]); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4343 | RecordDecl *Record = dyn_cast<RecordDecl>(TagD); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4344 | Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, |
| 4345 | /*FIXME: StartL=*/ID->getLocation(), |
| 4346 | ID->getLocation(), |
Fariborz Jahanian | 7dae114 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 4347 | ID->getIdentifier(), ID->getType(), |
| 4348 | ID->getBitWidth()); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4349 | Decls.push_back(FD); |
Fariborz Jahanian | 7dae114 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 4350 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4351 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4352 | // Introduce all of these fields into the appropriate scope. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4353 | for (SmallVectorImpl<Decl*>::iterator D = Decls.begin(); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4354 | D != Decls.end(); ++D) { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4355 | FieldDecl *FD = cast<FieldDecl>(*D); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4356 | if (getLangOpts().CPlusPlus) |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4357 | PushOnScopeChains(cast<FieldDecl>(FD), S); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4358 | else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD)) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4359 | Record->addDecl(FD); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4360 | } |
| 4361 | } |
| 4362 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4363 | /// \brief Build a type-check a new Objective-C exception variable declaration. |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4364 | VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T, |
| 4365 | SourceLocation StartLoc, |
| 4366 | SourceLocation IdLoc, |
| 4367 | IdentifierInfo *Id, |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4368 | bool Invalid) { |
| 4369 | // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage |
| 4370 | // duration shall not be qualified by an address-space qualifier." |
| 4371 | // Since all parameters have automatic store duration, they can not have |
| 4372 | // an address space. |
| 4373 | if (T.getAddressSpace() != 0) { |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4374 | Diag(IdLoc, diag::err_arg_with_address_space); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4375 | Invalid = true; |
| 4376 | } |
| 4377 | |
| 4378 | // An @catch parameter must be an unqualified object pointer type; |
| 4379 | // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"? |
| 4380 | if (Invalid) { |
| 4381 | // Don't do any further checking. |
Douglas Gregor | f4e837f | 2010-04-26 17:57:08 +0000 | [diff] [blame] | 4382 | } else if (T->isDependentType()) { |
| 4383 | // Okay: we don't know what this type will instantiate to. |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4384 | } else if (!T->isObjCObjectPointerType()) { |
| 4385 | Invalid = true; |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4386 | Diag(IdLoc ,diag::err_catch_param_not_objc_type); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4387 | } else if (T->isObjCQualifiedIdType()) { |
| 4388 | Invalid = true; |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4389 | Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4390 | } |
| 4391 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4392 | VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4393 | T, TInfo, SC_None); |
Douglas Gregor | 3f324d56 | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 4394 | New->setExceptionVariable(true); |
| 4395 | |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 4396 | // In ARC, infer 'retaining' for variables of retainable type. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4397 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New)) |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 4398 | Invalid = true; |
| 4399 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4400 | if (Invalid) |
| 4401 | New->setInvalidDecl(); |
| 4402 | return New; |
| 4403 | } |
| 4404 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4405 | Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) { |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4406 | const DeclSpec &DS = D.getDeclSpec(); |
| 4407 | |
| 4408 | // We allow the "register" storage class on exception variables because |
| 4409 | // GCC did, but we drop it completely. Any other storage class is an error. |
| 4410 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
| 4411 | Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm) |
| 4412 | << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc())); |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4413 | } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) { |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4414 | Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm) |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4415 | << DeclSpec::getSpecifierName(SCS); |
| 4416 | } |
| 4417 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
| 4418 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 4419 | diag::err_invalid_thread) |
| 4420 | << DeclSpec::getSpecifierName(TSCS); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4421 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 4422 | |
Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4423 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4424 | |
| 4425 | // Check that there are no default arguments inside the type of this |
| 4426 | // exception object (C++ only). |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4427 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4428 | CheckExtraCXXDefaultArguments(D); |
| 4429 | |
Argyrios Kyrtzidis | ef7022f | 2011-06-28 03:01:15 +0000 | [diff] [blame] | 4430 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 4431 | QualType ExceptionType = TInfo->getType(); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4432 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4433 | VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, |
| 4434 | D.getSourceRange().getBegin(), |
| 4435 | D.getIdentifierLoc(), |
| 4436 | D.getIdentifier(), |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4437 | D.isInvalidType()); |
| 4438 | |
| 4439 | // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1). |
| 4440 | if (D.getCXXScopeSpec().isSet()) { |
| 4441 | Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm) |
| 4442 | << D.getCXXScopeSpec().getRange(); |
| 4443 | New->setInvalidDecl(); |
| 4444 | } |
| 4445 | |
| 4446 | // Add the parameter declaration into this scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4447 | S->AddDecl(New); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4448 | if (D.getIdentifier()) |
| 4449 | IdResolver.AddDecl(New); |
| 4450 | |
| 4451 | ProcessDeclAttributes(S, New, D); |
| 4452 | |
| 4453 | if (New->hasAttr<BlocksAttr>()) |
| 4454 | Diag(New->getLocation(), diag::err_block_on_nonlocal); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4455 | return New; |
Douglas Gregor | e11ee11 | 2010-04-23 23:01:43 +0000 | [diff] [blame] | 4456 | } |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 4457 | |
| 4458 | /// CollectIvarsToConstructOrDestruct - Collect those ivars which require |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 4459 | /// initialization. |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4460 | void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4461 | SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4462 | for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv; |
| 4463 | Iv= Iv->getNextIvar()) { |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 4464 | QualType QT = Context.getBaseElementType(Iv->getType()); |
Douglas Gregor | 527786e | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 4465 | if (QT->isRecordType()) |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4466 | Ivars.push_back(Iv); |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 4467 | } |
| 4468 | } |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 4469 | |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 4470 | void Sema::DiagnoseUseOfUnimplementedSelectors() { |
Douglas Gregor | 72e357f | 2011-07-28 14:54:22 +0000 | [diff] [blame] | 4471 | // Load referenced selectors from the external source. |
| 4472 | if (ExternalSource) { |
| 4473 | SmallVector<std::pair<Selector, SourceLocation>, 4> Sels; |
| 4474 | ExternalSource->ReadReferencedSelectors(Sels); |
| 4475 | for (unsigned I = 0, N = Sels.size(); I != N; ++I) |
| 4476 | ReferencedSelectors[Sels[I].first] = Sels[I].second; |
| 4477 | } |
| 4478 | |
Fariborz Jahanian | c9b7c20 | 2011-02-04 23:19:27 +0000 | [diff] [blame] | 4479 | // Warning will be issued only when selector table is |
| 4480 | // generated (which means there is at lease one implementation |
| 4481 | // in the TU). This is to match gcc's behavior. |
| 4482 | if (ReferencedSelectors.empty() || |
| 4483 | !Context.AnyObjCImplementation()) |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 4484 | return; |
Chandler Carruth | 12c8f65 | 2015-03-27 00:55:05 +0000 | [diff] [blame] | 4485 | for (auto &SelectorAndLocation : ReferencedSelectors) { |
| 4486 | Selector Sel = SelectorAndLocation.first; |
| 4487 | SourceLocation Loc = SelectorAndLocation.second; |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 4488 | if (!LookupImplementedMethodInGlobalPool(Sel)) |
Chandler Carruth | 12c8f65 | 2015-03-27 00:55:05 +0000 | [diff] [blame] | 4489 | Diag(Loc, diag::warn_unimplemented_selector) << Sel; |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 4490 | } |
| 4491 | return; |
| 4492 | } |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 4493 | |
| 4494 | ObjCIvarDecl * |
| 4495 | Sema::GetIvarBackingPropertyAccessor(const ObjCMethodDecl *Method, |
| 4496 | const ObjCPropertyDecl *&PDecl) const { |
Fariborz Jahanian | 1cc7ae1 | 2014-01-02 17:24:32 +0000 | [diff] [blame] | 4497 | if (Method->isClassMethod()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4498 | return nullptr; |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 4499 | const ObjCInterfaceDecl *IDecl = Method->getClassInterface(); |
| 4500 | if (!IDecl) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4501 | return nullptr; |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4502 | Method = IDecl->lookupMethod(Method->getSelector(), /*isInstance=*/true, |
| 4503 | /*shallowCategoryLookup=*/false, |
| 4504 | /*followSuper=*/false); |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 4505 | if (!Method || !Method->isPropertyAccessor()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4506 | return nullptr; |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4507 | if ((PDecl = Method->findPropertyDecl())) |
Fariborz Jahanian | 122d94f | 2014-01-27 22:27:43 +0000 | [diff] [blame] | 4508 | if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) { |
| 4509 | // property backing ivar must belong to property's class |
| 4510 | // or be a private ivar in class's implementation. |
| 4511 | // FIXME. fix the const-ness issue. |
| 4512 | IV = const_cast<ObjCInterfaceDecl *>(IDecl)->lookupInstanceVariable( |
| 4513 | IV->getIdentifier()); |
| 4514 | return IV; |
| 4515 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4516 | return nullptr; |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 4517 | } |
| 4518 | |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4519 | namespace { |
| 4520 | /// Used by Sema::DiagnoseUnusedBackingIvarInAccessor to check if a property |
| 4521 | /// accessor references the backing ivar. |
Argyrios Kyrtzidis | 98045c1 | 2014-01-03 19:53:09 +0000 | [diff] [blame] | 4522 | class UnusedBackingIvarChecker : |
| 4523 | public DataRecursiveASTVisitor<UnusedBackingIvarChecker> { |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4524 | public: |
| 4525 | Sema &S; |
| 4526 | const ObjCMethodDecl *Method; |
| 4527 | const ObjCIvarDecl *IvarD; |
| 4528 | bool AccessedIvar; |
| 4529 | bool InvokedSelfMethod; |
| 4530 | |
| 4531 | UnusedBackingIvarChecker(Sema &S, const ObjCMethodDecl *Method, |
| 4532 | const ObjCIvarDecl *IvarD) |
| 4533 | : S(S), Method(Method), IvarD(IvarD), |
| 4534 | AccessedIvar(false), InvokedSelfMethod(false) { |
| 4535 | assert(IvarD); |
| 4536 | } |
| 4537 | |
| 4538 | bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { |
| 4539 | if (E->getDecl() == IvarD) { |
| 4540 | AccessedIvar = true; |
| 4541 | return false; |
| 4542 | } |
| 4543 | return true; |
| 4544 | } |
| 4545 | |
| 4546 | bool VisitObjCMessageExpr(ObjCMessageExpr *E) { |
| 4547 | if (E->getReceiverKind() == ObjCMessageExpr::Instance && |
| 4548 | S.isSelfExpr(E->getInstanceReceiver(), Method)) { |
| 4549 | InvokedSelfMethod = true; |
| 4550 | } |
| 4551 | return true; |
| 4552 | } |
| 4553 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4554 | } |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4555 | |
| 4556 | void Sema::DiagnoseUnusedBackingIvarInAccessor(Scope *S, |
| 4557 | const ObjCImplementationDecl *ImplD) { |
| 4558 | if (S->hasUnrecoverableErrorOccurred()) |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 4559 | return; |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4560 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 4561 | for (const auto *CurMethod : ImplD->instance_methods()) { |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4562 | unsigned DIAG = diag::warn_unused_property_backing_ivar; |
| 4563 | SourceLocation Loc = CurMethod->getLocation(); |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 4564 | if (Diags.isIgnored(DIAG, Loc)) |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4565 | continue; |
| 4566 | |
| 4567 | const ObjCPropertyDecl *PDecl; |
| 4568 | const ObjCIvarDecl *IV = GetIvarBackingPropertyAccessor(CurMethod, PDecl); |
| 4569 | if (!IV) |
| 4570 | continue; |
| 4571 | |
| 4572 | UnusedBackingIvarChecker Checker(*this, CurMethod, IV); |
| 4573 | Checker.TraverseStmt(CurMethod->getBody()); |
| 4574 | if (Checker.AccessedIvar) |
| 4575 | continue; |
| 4576 | |
Fariborz Jahanian | 5b3105d | 2014-01-02 22:42:09 +0000 | [diff] [blame] | 4577 | // 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] | 4578 | // implementation makes a self call. This is to prevent false positive in |
| 4579 | // cases where the ivar is accessed by another method that the accessor |
| 4580 | // delegates to. |
| 4581 | if (!IV->isReferenced() || !Checker.InvokedSelfMethod) { |
Argyrios Kyrtzidis | d8a3532 | 2014-01-03 19:39:23 +0000 | [diff] [blame] | 4582 | Diag(Loc, DIAG) << IV; |
Fariborz Jahanian | 5b3105d | 2014-01-02 22:42:09 +0000 | [diff] [blame] | 4583 | Diag(PDecl->getLocation(), diag::note_property_declare); |
| 4584 | } |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 4585 | } |
| 4586 | } |