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