Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1 | //===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements semantic analysis for Objective C declarations. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 13 | #include "TypeLocBuilder.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTConsumer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/ASTMutationListener.h" |
| 17 | #include "clang/AST/DeclObjC.h" |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprObjC.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecursiveASTVisitor.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Sema/DeclSpec.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Sema/Lookup.h" |
| 24 | #include "clang/Sema/Scope.h" |
| 25 | #include "clang/Sema/ScopeInfo.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 26 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/DenseMap.h" |
John McCall | a1e130b | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseSet.h" |
| 29 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 32 | /// Check whether the given method, which must be in the 'init' |
| 33 | /// family, is a valid member of that family. |
| 34 | /// |
| 35 | /// \param receiverTypeIfCall - if null, check this as if declaring it; |
| 36 | /// if non-null, check this as if making a call to it with the given |
| 37 | /// receiver type |
| 38 | /// |
| 39 | /// \return true to indicate that there was an error and appropriate |
| 40 | /// actions were taken |
| 41 | bool Sema::checkInitMethod(ObjCMethodDecl *method, |
| 42 | QualType receiverTypeIfCall) { |
| 43 | if (method->isInvalidDecl()) return true; |
| 44 | |
| 45 | // This castAs is safe: methods that don't return an object |
| 46 | // pointer won't be inferred as inits and will reject an explicit |
| 47 | // objc_method_family(init). |
| 48 | |
| 49 | // We ignore protocols here. Should we? What about Class? |
| 50 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 51 | const ObjCObjectType *result = |
| 52 | method->getReturnType()->castAs<ObjCObjectPointerType>()->getObjectType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 53 | |
| 54 | if (result->isObjCId()) { |
| 55 | return false; |
| 56 | } else if (result->isObjCClass()) { |
| 57 | // fall through: always an error |
| 58 | } else { |
| 59 | ObjCInterfaceDecl *resultClass = result->getInterface(); |
| 60 | assert(resultClass && "unexpected object type!"); |
| 61 | |
| 62 | // It's okay for the result type to still be a forward declaration |
| 63 | // if we're checking an interface declaration. |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 64 | if (!resultClass->hasDefinition()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 65 | if (receiverTypeIfCall.isNull() && |
| 66 | !isa<ObjCImplementationDecl>(method->getDeclContext())) |
| 67 | return false; |
| 68 | |
| 69 | // Otherwise, we try to compare class types. |
| 70 | } else { |
| 71 | // If this method was declared in a protocol, we can't check |
| 72 | // anything unless we have a receiver type that's an interface. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 73 | const ObjCInterfaceDecl *receiverClass = nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 74 | if (isa<ObjCProtocolDecl>(method->getDeclContext())) { |
| 75 | if (receiverTypeIfCall.isNull()) |
| 76 | return false; |
| 77 | |
| 78 | receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>() |
| 79 | ->getInterfaceDecl(); |
| 80 | |
| 81 | // This can be null for calls to e.g. id<Foo>. |
| 82 | if (!receiverClass) return false; |
| 83 | } else { |
| 84 | receiverClass = method->getClassInterface(); |
| 85 | assert(receiverClass && "method not associated with a class!"); |
| 86 | } |
| 87 | |
| 88 | // If either class is a subclass of the other, it's fine. |
| 89 | if (receiverClass->isSuperClassOf(resultClass) || |
| 90 | resultClass->isSuperClassOf(receiverClass)) |
| 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | SourceLocation loc = method->getLocation(); |
| 96 | |
| 97 | // If we're in a system header, and this is not a call, just make |
| 98 | // the method unusable. |
| 99 | if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 100 | method->addAttr(UnavailableAttr::CreateImplicit(Context, "", |
| 101 | UnavailableAttr::IR_ARCInitReturnsUnrelated, loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 102 | return true; |
| 103 | } |
| 104 | |
| 105 | // Otherwise, it's an error. |
| 106 | Diag(loc, diag::err_arc_init_method_unrelated_result_type); |
| 107 | method->setInvalidDecl(); |
| 108 | return true; |
| 109 | } |
| 110 | |
Akira Hatanaka | a6b5e00 | 2018-07-28 04:06:13 +0000 | [diff] [blame] | 111 | /// Issue a warning if the parameter of the overridden method is non-escaping |
| 112 | /// but the parameter of the overriding method is not. |
| 113 | static bool diagnoseNoescape(const ParmVarDecl *NewD, const ParmVarDecl *OldD, |
| 114 | Sema &S) { |
| 115 | if (OldD->hasAttr<NoEscapeAttr>() && !NewD->hasAttr<NoEscapeAttr>()) { |
| 116 | S.Diag(NewD->getLocation(), diag::warn_overriding_method_missing_noescape); |
| 117 | S.Diag(OldD->getLocation(), diag::note_overridden_marked_noescape); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | /// Produce additional diagnostics if a category conforms to a protocol that |
| 125 | /// defines a method taking a non-escaping parameter. |
| 126 | static void diagnoseNoescape(const ParmVarDecl *NewD, const ParmVarDecl *OldD, |
| 127 | const ObjCCategoryDecl *CD, |
| 128 | const ObjCProtocolDecl *PD, Sema &S) { |
| 129 | if (!diagnoseNoescape(NewD, OldD, S)) |
| 130 | S.Diag(CD->getLocation(), diag::note_cat_conform_to_noescape_prot) |
| 131 | << CD->IsClassExtension() << PD |
| 132 | << cast<ObjCMethodDecl>(NewD->getDeclContext()); |
| 133 | } |
| 134 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 135 | void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 136 | const ObjCMethodDecl *Overridden) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 137 | if (Overridden->hasRelatedResultType() && |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 138 | !NewMethod->hasRelatedResultType()) { |
| 139 | // This can only happen when the method follows a naming convention that |
| 140 | // implies a related result type, and the original (overridden) method has |
| 141 | // a suitable return type, but the new (overriding) method does not have |
| 142 | // a suitable return type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 143 | QualType ResultType = NewMethod->getReturnType(); |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 144 | SourceRange ResultTypeRange = NewMethod->getReturnTypeSourceRange(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 145 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 146 | // Figure out which class this method is part of, if any. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 147 | ObjCInterfaceDecl *CurrentClass |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 148 | = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext()); |
| 149 | if (!CurrentClass) { |
| 150 | DeclContext *DC = NewMethod->getDeclContext(); |
| 151 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC)) |
| 152 | CurrentClass = Cat->getClassInterface(); |
| 153 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC)) |
| 154 | CurrentClass = Impl->getClassInterface(); |
| 155 | else if (ObjCCategoryImplDecl *CatImpl |
| 156 | = dyn_cast<ObjCCategoryImplDecl>(DC)) |
| 157 | CurrentClass = CatImpl->getClassInterface(); |
| 158 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 159 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 160 | if (CurrentClass) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 161 | Diag(NewMethod->getLocation(), |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 162 | diag::warn_related_result_type_compatibility_class) |
| 163 | << Context.getObjCInterfaceType(CurrentClass) |
| 164 | << ResultType |
| 165 | << ResultTypeRange; |
| 166 | } else { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 167 | Diag(NewMethod->getLocation(), |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 168 | diag::warn_related_result_type_compatibility_protocol) |
| 169 | << ResultType |
| 170 | << ResultTypeRange; |
| 171 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 172 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 173 | if (ObjCMethodFamily Family = Overridden->getMethodFamily()) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 174 | Diag(Overridden->getLocation(), |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 175 | diag::note_related_result_type_family) |
| 176 | << /*overridden method*/ 0 |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 177 | << Family; |
| 178 | else |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 179 | Diag(Overridden->getLocation(), |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 180 | diag::note_related_result_type_overridden); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 181 | } |
Akira Hatanaka | 7d85b8f | 2017-09-20 05:39:18 +0000 | [diff] [blame] | 182 | |
| 183 | if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() != |
| 184 | Overridden->hasAttr<NSReturnsRetainedAttr>())) { |
| 185 | Diag(NewMethod->getLocation(), |
Alex Lorenz | 26d282f | 2018-01-03 23:52:42 +0000 | [diff] [blame] | 186 | getLangOpts().ObjCAutoRefCount |
| 187 | ? diag::err_nsreturns_retained_attribute_mismatch |
| 188 | : diag::warn_nsreturns_retained_attribute_mismatch) |
| 189 | << 1; |
Akira Hatanaka | 7d85b8f | 2017-09-20 05:39:18 +0000 | [diff] [blame] | 190 | Diag(Overridden->getLocation(), diag::note_previous_decl) << "method"; |
| 191 | } |
| 192 | if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() != |
| 193 | Overridden->hasAttr<NSReturnsNotRetainedAttr>())) { |
| 194 | Diag(NewMethod->getLocation(), |
Alex Lorenz | 26d282f | 2018-01-03 23:52:42 +0000 | [diff] [blame] | 195 | getLangOpts().ObjCAutoRefCount |
| 196 | ? diag::err_nsreturns_retained_attribute_mismatch |
| 197 | : diag::warn_nsreturns_retained_attribute_mismatch) |
| 198 | << 0; |
Akira Hatanaka | 7d85b8f | 2017-09-20 05:39:18 +0000 | [diff] [blame] | 199 | Diag(Overridden->getLocation(), diag::note_previous_decl) << "method"; |
| 200 | } |
| 201 | |
| 202 | ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(), |
| 203 | oe = Overridden->param_end(); |
| 204 | for (ObjCMethodDecl::param_iterator ni = NewMethod->param_begin(), |
| 205 | ne = NewMethod->param_end(); |
| 206 | ni != ne && oi != oe; ++ni, ++oi) { |
| 207 | const ParmVarDecl *oldDecl = (*oi); |
| 208 | ParmVarDecl *newDecl = (*ni); |
| 209 | if (newDecl->hasAttr<NSConsumedAttr>() != |
| 210 | oldDecl->hasAttr<NSConsumedAttr>()) { |
Alex Lorenz | 26d282f | 2018-01-03 23:52:42 +0000 | [diff] [blame] | 211 | Diag(newDecl->getLocation(), |
| 212 | getLangOpts().ObjCAutoRefCount |
| 213 | ? diag::err_nsconsumed_attribute_mismatch |
| 214 | : diag::warn_nsconsumed_attribute_mismatch); |
Akira Hatanaka | 7d85b8f | 2017-09-20 05:39:18 +0000 | [diff] [blame] | 215 | Diag(oldDecl->getLocation(), diag::note_previous_decl) << "parameter"; |
Fariborz Jahanian | ac8dbf0 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 216 | } |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 217 | |
Akira Hatanaka | a6b5e00 | 2018-07-28 04:06:13 +0000 | [diff] [blame] | 218 | diagnoseNoescape(newDecl, oldDecl, *this); |
Fariborz Jahanian | ac8dbf0 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 219 | } |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 222 | /// Check a method declaration for compatibility with the Objective-C |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 223 | /// ARC conventions. |
John McCall | e48f389 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 224 | bool Sema::CheckARCMethodDecl(ObjCMethodDecl *method) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 225 | ObjCMethodFamily family = method->getMethodFamily(); |
| 226 | switch (family) { |
| 227 | case OMF_None: |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 228 | case OMF_finalize: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 229 | case OMF_retain: |
| 230 | case OMF_release: |
| 231 | case OMF_autorelease: |
| 232 | case OMF_retainCount: |
| 233 | case OMF_self: |
Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 234 | case OMF_initialize: |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 235 | case OMF_performSelector: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 236 | return false; |
| 237 | |
Fariborz Jahanian | b7f03c1 | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 238 | case OMF_dealloc: |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 239 | if (!Context.hasSameType(method->getReturnType(), Context.VoidTy)) { |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 240 | SourceRange ResultTypeRange = method->getReturnTypeSourceRange(); |
Fariborz Jahanian | b7f03c1 | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 241 | if (ResultTypeRange.isInvalid()) |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 242 | Diag(method->getLocation(), diag::err_dealloc_bad_result_type) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 243 | << method->getReturnType() |
| 244 | << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)"); |
Fariborz Jahanian | b7f03c1 | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 245 | else |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 246 | Diag(method->getLocation(), diag::err_dealloc_bad_result_type) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 247 | << method->getReturnType() |
| 248 | << FixItHint::CreateReplacement(ResultTypeRange, "void"); |
Fariborz Jahanian | b7f03c1 | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 249 | return true; |
| 250 | } |
| 251 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 252 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 253 | case OMF_init: |
| 254 | // 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] | 255 | if (checkInitMethod(method, QualType())) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 256 | return true; |
| 257 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 258 | method->addAttr(NSConsumesSelfAttr::CreateImplicit(Context)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 259 | |
| 260 | // Don't add a second copy of this attribute, but otherwise don't |
| 261 | // let it be suppressed. |
| 262 | if (method->hasAttr<NSReturnsRetainedAttr>()) |
| 263 | return false; |
| 264 | break; |
| 265 | |
| 266 | case OMF_alloc: |
| 267 | case OMF_copy: |
| 268 | case OMF_mutableCopy: |
| 269 | case OMF_new: |
| 270 | if (method->hasAttr<NSReturnsRetainedAttr>() || |
| 271 | method->hasAttr<NSReturnsNotRetainedAttr>() || |
| 272 | method->hasAttr<NSReturnsAutoreleasedAttr>()) |
| 273 | return false; |
| 274 | break; |
| 275 | } |
| 276 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 277 | method->addAttr(NSReturnsRetainedAttr::CreateImplicit(Context)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 278 | return false; |
| 279 | } |
| 280 | |
Alex Lorenz | f81d97e | 2017-07-13 16:35:59 +0000 | [diff] [blame] | 281 | static void DiagnoseObjCImplementedDeprecations(Sema &S, const NamedDecl *ND, |
| 282 | SourceLocation ImplLoc) { |
| 283 | if (!ND) |
| 284 | return; |
| 285 | bool IsCategory = false; |
Alex Lorenz | f4d4cfb | 2018-05-03 01:12:06 +0000 | [diff] [blame] | 286 | StringRef RealizedPlatform; |
| 287 | AvailabilityResult Availability = ND->getAvailability( |
| 288 | /*Message=*/nullptr, /*EnclosingVersion=*/VersionTuple(), |
| 289 | &RealizedPlatform); |
Alex Lorenz | e1088dc | 2017-07-13 16:37:11 +0000 | [diff] [blame] | 290 | if (Availability != AR_Deprecated) { |
Eric Christopher | 7aba978 | 2017-07-14 01:42:57 +0000 | [diff] [blame] | 291 | if (isa<ObjCMethodDecl>(ND)) { |
Alex Lorenz | e1088dc | 2017-07-13 16:37:11 +0000 | [diff] [blame] | 292 | if (Availability != AR_Unavailable) |
| 293 | return; |
Alex Lorenz | f4d4cfb | 2018-05-03 01:12:06 +0000 | [diff] [blame] | 294 | if (RealizedPlatform.empty()) |
| 295 | RealizedPlatform = S.Context.getTargetInfo().getPlatformName(); |
| 296 | // Warn about implementing unavailable methods, unless the unavailable |
| 297 | // is for an app extension. |
| 298 | if (RealizedPlatform.endswith("_app_extension")) |
| 299 | return; |
Alex Lorenz | e1088dc | 2017-07-13 16:37:11 +0000 | [diff] [blame] | 300 | S.Diag(ImplLoc, diag::warn_unavailable_def); |
| 301 | S.Diag(ND->getLocation(), diag::note_method_declared_at) |
| 302 | << ND->getDeclName(); |
| 303 | return; |
| 304 | } |
Alex Lorenz | f81d97e | 2017-07-13 16:35:59 +0000 | [diff] [blame] | 305 | if (const auto *CD = dyn_cast<ObjCCategoryDecl>(ND)) { |
| 306 | if (!CD->getClassInterface()->isDeprecated()) |
| 307 | return; |
| 308 | ND = CD->getClassInterface(); |
| 309 | IsCategory = true; |
| 310 | } else |
| 311 | return; |
Fariborz Jahanian | d724a10 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 312 | } |
Alex Lorenz | f81d97e | 2017-07-13 16:35:59 +0000 | [diff] [blame] | 313 | S.Diag(ImplLoc, diag::warn_deprecated_def) |
| 314 | << (isa<ObjCMethodDecl>(ND) |
| 315 | ? /*Method*/ 0 |
| 316 | : isa<ObjCCategoryDecl>(ND) || IsCategory ? /*Category*/ 2 |
| 317 | : /*Class*/ 1); |
| 318 | if (isa<ObjCMethodDecl>(ND)) |
| 319 | S.Diag(ND->getLocation(), diag::note_method_declared_at) |
| 320 | << ND->getDeclName(); |
| 321 | else |
| 322 | S.Diag(ND->getLocation(), diag::note_previous_decl) |
| 323 | << (isa<ObjCCategoryDecl>(ND) ? "category" : "class"); |
Fariborz Jahanian | d724a10 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Fariborz Jahanian | bd0642f | 2011-08-31 17:37:55 +0000 | [diff] [blame] | 326 | /// AddAnyMethodToGlobalPool - Add any method, instance or factory to global |
| 327 | /// pool. |
| 328 | void Sema::AddAnyMethodToGlobalPool(Decl *D) { |
| 329 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 330 | |
Fariborz Jahanian | bd0642f | 2011-08-31 17:37:55 +0000 | [diff] [blame] | 331 | // If we don't have a valid method decl, simply return. |
| 332 | if (!MDecl) |
| 333 | return; |
| 334 | if (MDecl->isInstanceMethod()) |
| 335 | AddInstanceMethodToGlobalPool(MDecl, true); |
| 336 | else |
| 337 | AddFactoryMethodToGlobalPool(MDecl, true); |
| 338 | } |
| 339 | |
Fariborz Jahanian | 1dfeace | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 340 | /// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer |
| 341 | /// has explicit ownership attribute; false otherwise. |
| 342 | static bool |
| 343 | HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) { |
| 344 | QualType T = Param->getType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 345 | |
Fariborz Jahanian | 1dfeace | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 346 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 347 | T = PT->getPointeeType(); |
| 348 | } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) { |
| 349 | T = RT->getPointeeType(); |
| 350 | } else { |
| 351 | return true; |
| 352 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 353 | |
| 354 | // If we have a lifetime qualifier, but it's local, we must have |
Fariborz Jahanian | 1dfeace | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 355 | // inferred it. So, it is implicit. |
| 356 | return !T.getLocalQualifiers().hasObjCLifetime(); |
| 357 | } |
| 358 | |
Fariborz Jahanian | 18d0a5d | 2012-08-08 23:41:08 +0000 | [diff] [blame] | 359 | /// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible |
| 360 | /// and user declared, in the method definition's AST. |
| 361 | void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) { |
Akira Hatanaka | ac57af3 | 2019-04-17 23:14:44 +0000 | [diff] [blame] | 362 | ImplicitlyRetainedSelfLocs.clear(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 363 | assert((getCurMethodDecl() == nullptr) && "Methodparsing confused"); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 364 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 365 | |
Leonard Chan | bf5fe2d | 2018-12-06 00:10:36 +0000 | [diff] [blame] | 366 | PushExpressionEvaluationContext(ExprEvalContexts.back().Context); |
| 367 | |
Steve Naroff | 542cd5d | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 368 | // If we don't have a valid method decl, simply return. |
| 369 | if (!MDecl) |
| 370 | return; |
Steve Naroff | 1d2538c | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 371 | |
Akira Hatanaka | ff6c4f3 | 2018-04-12 06:01:41 +0000 | [diff] [blame] | 372 | QualType ResultType = MDecl->getReturnType(); |
| 373 | if (!ResultType->isDependentType() && !ResultType->isVoidType() && |
| 374 | !MDecl->isInvalidDecl() && |
| 375 | RequireCompleteType(MDecl->getLocation(), ResultType, |
| 376 | diag::err_func_def_incomplete_result)) |
| 377 | MDecl->setInvalidDecl(); |
| 378 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 379 | // 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] | 380 | PushDeclContext(FnBodyScope, MDecl); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 381 | PushFunctionScope(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 382 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 383 | // Create Decl objects for each parameter, entrring them in the scope for |
| 384 | // binding to their use. |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 385 | |
| 386 | // Insert the invisible arguments, self and _cmd! |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 387 | MDecl->createImplicitParams(Context, MDecl->getClassInterface()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | |
Daniel Dunbar | 279d1cc | 2008-08-26 06:07:48 +0000 | [diff] [blame] | 389 | PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); |
| 390 | PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 391 | |
Reid Kleckner | 5a11580 | 2013-06-24 14:38:26 +0000 | [diff] [blame] | 392 | // 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] | 393 | CheckParmsForFunctionDef(MDecl->parameters(), |
Reid Kleckner | 5a11580 | 2013-06-24 14:38:26 +0000 | [diff] [blame] | 394 | /*CheckParameterNames=*/false); |
| 395 | |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 396 | // Introduce all of the other parameters into this scope. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 397 | for (auto *Param : MDecl->parameters()) { |
Fariborz Jahanian | b3e8712 | 2010-09-17 22:07:07 +0000 | [diff] [blame] | 398 | if (!Param->isInvalidDecl() && |
Fariborz Jahanian | 1dfeace | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 399 | getLangOpts().ObjCAutoRefCount && |
| 400 | !HasExplicitOwnershipAttr(*this, Param)) |
| 401 | Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) << |
| 402 | Param->getType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 403 | |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 404 | if (Param->getIdentifier()) |
| 405 | PushOnScopeChains(Param, FnBodyScope); |
Fariborz Jahanian | b3e8712 | 2010-09-17 22:07:07 +0000 | [diff] [blame] | 406 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 407 | |
| 408 | // In ARC, disallow definition of retain/release/autorelease/retainCount |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 409 | if (getLangOpts().ObjCAutoRefCount) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 410 | switch (MDecl->getMethodFamily()) { |
| 411 | case OMF_retain: |
| 412 | case OMF_retainCount: |
| 413 | case OMF_release: |
| 414 | case OMF_autorelease: |
| 415 | Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def) |
Fariborz Jahanian | 39d1c42 | 2013-05-16 19:08:44 +0000 | [diff] [blame] | 416 | << 0 << MDecl->getSelector(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 417 | break; |
| 418 | |
| 419 | case OMF_None: |
| 420 | case OMF_dealloc: |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 421 | case OMF_finalize: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 422 | case OMF_alloc: |
| 423 | case OMF_init: |
| 424 | case OMF_mutableCopy: |
| 425 | case OMF_copy: |
| 426 | case OMF_new: |
| 427 | case OMF_self: |
Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 428 | case OMF_initialize: |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 429 | case OMF_performSelector: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 430 | break; |
| 431 | } |
| 432 | } |
| 433 | |
Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 434 | // Warn on deprecated methods under -Wdeprecated-implementations, |
| 435 | // and prepare for warning on missing super calls. |
| 436 | if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 437 | ObjCMethodDecl *IMD = |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 438 | IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 439 | |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 440 | if (IMD) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 441 | ObjCImplDecl *ImplDeclOfMethodDef = |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 442 | dyn_cast<ObjCImplDecl>(MDecl->getDeclContext()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 443 | ObjCContainerDecl *ContDeclOfMethodDecl = |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 444 | dyn_cast<ObjCContainerDecl>(IMD->getDeclContext()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 445 | ObjCImplDecl *ImplDeclOfMethodDecl = nullptr; |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 446 | if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl)) |
| 447 | ImplDeclOfMethodDecl = OID->getImplementation(); |
Fariborz Jahanian | ed39e7c | 2014-03-18 16:25:22 +0000 | [diff] [blame] | 448 | else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl)) { |
| 449 | if (CD->IsClassExtension()) { |
| 450 | if (ObjCInterfaceDecl *OID = CD->getClassInterface()) |
| 451 | ImplDeclOfMethodDecl = OID->getImplementation(); |
| 452 | } else |
| 453 | ImplDeclOfMethodDecl = CD->getImplementation(); |
Fariborz Jahanian | 19a08bb | 2014-03-18 00:10:37 +0000 | [diff] [blame] | 454 | } |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 455 | // No need to issue deprecated warning if deprecated mehod in class/category |
| 456 | // is being implemented in its own implementation (no overriding is involved). |
Fariborz Jahanian | ed39e7c | 2014-03-18 16:25:22 +0000 | [diff] [blame] | 457 | if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef) |
Alex Lorenz | f81d97e | 2017-07-13 16:35:59 +0000 | [diff] [blame] | 458 | DiagnoseObjCImplementedDeprecations(*this, IMD, MDecl->getLocation()); |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 459 | } |
Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 460 | |
Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 461 | if (MDecl->getMethodFamily() == OMF_init) { |
| 462 | if (MDecl->isDesignatedInitializerForTheInterface()) { |
| 463 | getCurFunction()->ObjCIsDesignatedInit = true; |
Fariborz Jahanian | e3b5c99 | 2014-03-14 23:30:18 +0000 | [diff] [blame] | 464 | getCurFunction()->ObjCWarnForNoDesignatedInitChain = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 465 | IC->getSuperClass() != nullptr; |
Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 466 | } else if (IC->hasDesignatedInitializers()) { |
| 467 | getCurFunction()->ObjCIsSecondaryInit = true; |
Fariborz Jahanian | e3b5c99 | 2014-03-14 23:30:18 +0000 | [diff] [blame] | 468 | getCurFunction()->ObjCWarnForNoInitDelegation = true; |
Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
Argyrios Kyrtzidis | 22bfa2c | 2013-12-03 21:11:36 +0000 | [diff] [blame] | 471 | |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 472 | // If this is "dealloc" or "finalize", set some bit here. |
Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 473 | // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false. |
| 474 | // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set. |
| 475 | // Only do this if the current class actually has a superclass. |
Jordan Rose | d03d99d | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 476 | if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) { |
Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 477 | ObjCMethodFamily Family = MDecl->getMethodFamily(); |
| 478 | if (Family == OMF_dealloc) { |
| 479 | if (!(getLangOpts().ObjCAutoRefCount || |
| 480 | getLangOpts().getGC() == LangOptions::GCOnly)) |
| 481 | getCurFunction()->ObjCShouldCallSuper = true; |
| 482 | |
| 483 | } else if (Family == OMF_finalize) { |
| 484 | if (Context.getLangOpts().getGC() != LangOptions::NonGC) |
| 485 | getCurFunction()->ObjCShouldCallSuper = true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 486 | |
Fariborz Jahanian | ce4bbb2 | 2013-11-05 00:28:21 +0000 | [diff] [blame] | 487 | } else { |
Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 488 | const ObjCMethodDecl *SuperMethod = |
Jordan Rose | d03d99d | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 489 | SuperClass->lookupMethod(MDecl->getSelector(), |
| 490 | MDecl->isInstanceMethod()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 491 | getCurFunction()->ObjCShouldCallSuper = |
Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 492 | (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>()); |
Fariborz Jahanian | d6876b2 | 2012-09-10 18:04:25 +0000 | [diff] [blame] | 493 | } |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 494 | } |
Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 495 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 498 | namespace { |
| 499 | |
| 500 | // Callback to only accept typo corrections that are Objective-C classes. |
| 501 | // If an ObjCInterfaceDecl* is given to the constructor, then the validation |
| 502 | // function will reject corrections to that class. |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 503 | class ObjCInterfaceValidatorCCC final : public CorrectionCandidateCallback { |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 504 | public: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 505 | ObjCInterfaceValidatorCCC() : CurrentIDecl(nullptr) {} |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 506 | explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl) |
| 507 | : CurrentIDecl(IDecl) {} |
| 508 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 509 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 510 | ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
| 511 | return ID && !declaresSameEntity(ID, CurrentIDecl); |
| 512 | } |
| 513 | |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 514 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 515 | return std::make_unique<ObjCInterfaceValidatorCCC>(*this); |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 518 | private: |
| 519 | ObjCInterfaceDecl *CurrentIDecl; |
| 520 | }; |
| 521 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 522 | } // end anonymous namespace |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 523 | |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 524 | static void diagnoseUseOfProtocols(Sema &TheSema, |
| 525 | ObjCContainerDecl *CD, |
| 526 | ObjCProtocolDecl *const *ProtoRefs, |
| 527 | unsigned NumProtoRefs, |
| 528 | const SourceLocation *ProtoLocs) { |
| 529 | assert(ProtoRefs); |
| 530 | // Diagnose availability in the context of the ObjC container. |
| 531 | Sema::ContextRAII SavedContext(TheSema, CD); |
| 532 | for (unsigned i = 0; i < NumProtoRefs; ++i) { |
Alex Lorenz | cdd596f | 2017-07-07 09:15:29 +0000 | [diff] [blame] | 533 | (void)TheSema.DiagnoseUseOfDecl(ProtoRefs[i], ProtoLocs[i], |
| 534 | /*UnknownObjCClass=*/nullptr, |
| 535 | /*ObjCPropertyAccess=*/false, |
| 536 | /*AvoidPartialAvailabilityChecks=*/true); |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 540 | void Sema:: |
| 541 | ActOnSuperClassOfClassInterface(Scope *S, |
| 542 | SourceLocation AtInterfaceLoc, |
| 543 | ObjCInterfaceDecl *IDecl, |
| 544 | IdentifierInfo *ClassName, |
| 545 | SourceLocation ClassLoc, |
| 546 | IdentifierInfo *SuperName, |
| 547 | SourceLocation SuperLoc, |
| 548 | ArrayRef<ParsedType> SuperTypeArgs, |
| 549 | SourceRange SuperTypeArgsRange) { |
| 550 | // Check if a different kind of symbol declared in this scope. |
| 551 | NamedDecl *PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
| 552 | LookupOrdinaryName); |
| 553 | |
| 554 | if (!PrevDecl) { |
| 555 | // Try to correct for a typo in the superclass name without correcting |
| 556 | // to the class we're defining. |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 557 | ObjCInterfaceValidatorCCC CCC(IDecl); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 558 | if (TypoCorrection Corrected = CorrectTypo( |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 559 | DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, |
| 560 | TUScope, nullptr, CCC, CTK_ErrorRecovery)) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 561 | diagnoseTypo(Corrected, PDiag(diag::err_undef_superclass_suggest) |
| 562 | << SuperName << ClassName); |
| 563 | PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | if (declaresSameEntity(PrevDecl, IDecl)) { |
| 568 | Diag(SuperLoc, diag::err_recursive_superclass) |
| 569 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
| 570 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
| 571 | } else { |
| 572 | ObjCInterfaceDecl *SuperClassDecl = |
| 573 | dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
| 574 | QualType SuperClassType; |
| 575 | |
| 576 | // Diagnose classes that inherit from deprecated classes. |
| 577 | if (SuperClassDecl) { |
| 578 | (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc); |
| 579 | SuperClassType = Context.getObjCInterfaceType(SuperClassDecl); |
| 580 | } |
| 581 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 582 | if (PrevDecl && !SuperClassDecl) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 583 | // The previous declaration was not a class decl. Check if we have a |
| 584 | // typedef. If we do, get the underlying class type. |
| 585 | if (const TypedefNameDecl *TDecl = |
| 586 | dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) { |
| 587 | QualType T = TDecl->getUnderlyingType(); |
| 588 | if (T->isObjCObjectType()) { |
Simon Pilgrim | a867cca | 2019-10-17 10:35:29 +0000 | [diff] [blame] | 589 | if (NamedDecl *IDecl = T->castAs<ObjCObjectType>()->getInterface()) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 590 | SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl); |
| 591 | SuperClassType = Context.getTypeDeclType(TDecl); |
| 592 | |
| 593 | // This handles the following case: |
| 594 | // @interface NewI @end |
| 595 | // typedef NewI DeprI __attribute__((deprecated("blah"))) |
| 596 | // @interface SI : DeprI /* warn here */ @end |
| 597 | (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc); |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | // This handles the following case: |
| 603 | // |
| 604 | // typedef int SuperClass; |
| 605 | // @interface MyClass : SuperClass {} @end |
| 606 | // |
| 607 | if (!SuperClassDecl) { |
| 608 | Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName; |
| 609 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) { |
| 614 | if (!SuperClassDecl) |
| 615 | Diag(SuperLoc, diag::err_undef_superclass) |
| 616 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
| 617 | else if (RequireCompleteType(SuperLoc, |
| 618 | SuperClassType, |
| 619 | diag::err_forward_superclass, |
| 620 | SuperClassDecl->getDeclName(), |
| 621 | ClassName, |
| 622 | SourceRange(AtInterfaceLoc, ClassLoc))) { |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 623 | SuperClassDecl = nullptr; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 624 | SuperClassType = QualType(); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | if (SuperClassType.isNull()) { |
| 629 | assert(!SuperClassDecl && "Failed to set SuperClassType?"); |
| 630 | return; |
| 631 | } |
| 632 | |
| 633 | // Handle type arguments on the superclass. |
| 634 | TypeSourceInfo *SuperClassTInfo = nullptr; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 635 | if (!SuperTypeArgs.empty()) { |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 636 | TypeResult fullSuperClassType = actOnObjCTypeArgsAndProtocolQualifiers( |
| 637 | S, |
| 638 | SuperLoc, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 639 | CreateParsedType(SuperClassType, |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 640 | nullptr), |
| 641 | SuperTypeArgsRange.getBegin(), |
| 642 | SuperTypeArgs, |
| 643 | SuperTypeArgsRange.getEnd(), |
| 644 | SourceLocation(), |
| 645 | { }, |
| 646 | { }, |
| 647 | SourceLocation()); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 648 | if (!fullSuperClassType.isUsable()) |
| 649 | return; |
| 650 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 651 | SuperClassType = GetTypeFromParser(fullSuperClassType.get(), |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 652 | &SuperClassTInfo); |
| 653 | } |
| 654 | |
| 655 | if (!SuperClassTInfo) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 656 | SuperClassTInfo = Context.getTrivialTypeSourceInfo(SuperClassType, |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 657 | SuperLoc); |
| 658 | } |
| 659 | |
| 660 | IDecl->setSuperClass(SuperClassTInfo); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 661 | IDecl->setEndOfDefinitionLoc(SuperClassTInfo->getTypeLoc().getEndLoc()); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 665 | DeclResult Sema::actOnObjCTypeParam(Scope *S, |
| 666 | ObjCTypeParamVariance variance, |
| 667 | SourceLocation varianceLoc, |
| 668 | unsigned index, |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 669 | IdentifierInfo *paramName, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 670 | SourceLocation paramLoc, |
| 671 | SourceLocation colonLoc, |
| 672 | ParsedType parsedTypeBound) { |
| 673 | // If there was an explicitly-provided type bound, check it. |
| 674 | TypeSourceInfo *typeBoundInfo = nullptr; |
| 675 | if (parsedTypeBound) { |
| 676 | // The type bound can be any Objective-C pointer type. |
| 677 | QualType typeBound = GetTypeFromParser(parsedTypeBound, &typeBoundInfo); |
| 678 | if (typeBound->isObjCObjectPointerType()) { |
| 679 | // okay |
| 680 | } else if (typeBound->isObjCObjectType()) { |
| 681 | // The user forgot the * on an Objective-C pointer type, e.g., |
| 682 | // "T : NSView". |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 683 | SourceLocation starLoc = getLocForEndOfToken( |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 684 | typeBoundInfo->getTypeLoc().getEndLoc()); |
| 685 | Diag(typeBoundInfo->getTypeLoc().getBeginLoc(), |
| 686 | diag::err_objc_type_param_bound_missing_pointer) |
| 687 | << typeBound << paramName |
| 688 | << FixItHint::CreateInsertion(starLoc, " *"); |
| 689 | |
| 690 | // Create a new type location builder so we can update the type |
| 691 | // location information we have. |
| 692 | TypeLocBuilder builder; |
| 693 | builder.pushFullCopy(typeBoundInfo->getTypeLoc()); |
| 694 | |
| 695 | // Create the Objective-C pointer type. |
| 696 | typeBound = Context.getObjCObjectPointerType(typeBound); |
| 697 | ObjCObjectPointerTypeLoc newT |
| 698 | = builder.push<ObjCObjectPointerTypeLoc>(typeBound); |
| 699 | newT.setStarLoc(starLoc); |
| 700 | |
| 701 | // Form the new type source information. |
| 702 | typeBoundInfo = builder.getTypeSourceInfo(Context, typeBound); |
| 703 | } else { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 704 | // Not a valid type bound. |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 705 | Diag(typeBoundInfo->getTypeLoc().getBeginLoc(), |
| 706 | diag::err_objc_type_param_bound_nonobject) |
| 707 | << typeBound << paramName; |
| 708 | |
| 709 | // Forget the bound; we'll default to id later. |
| 710 | typeBoundInfo = nullptr; |
| 711 | } |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 712 | |
John McCall | 6997525 | 2015-09-23 22:14:21 +0000 | [diff] [blame] | 713 | // Type bounds cannot have qualifiers (even indirectly) or explicit |
| 714 | // nullability. |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 715 | if (typeBoundInfo) { |
John McCall | 6997525 | 2015-09-23 22:14:21 +0000 | [diff] [blame] | 716 | QualType typeBound = typeBoundInfo->getType(); |
| 717 | TypeLoc qual = typeBoundInfo->getTypeLoc().findExplicitQualifierLoc(); |
| 718 | if (qual || typeBound.hasQualifiers()) { |
| 719 | bool diagnosed = false; |
| 720 | SourceRange rangeToRemove; |
| 721 | if (qual) { |
| 722 | if (auto attr = qual.getAs<AttributedTypeLoc>()) { |
| 723 | rangeToRemove = attr.getLocalSourceRange(); |
| 724 | if (attr.getTypePtr()->getImmediateNullability()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 725 | Diag(attr.getBeginLoc(), |
John McCall | 6997525 | 2015-09-23 22:14:21 +0000 | [diff] [blame] | 726 | diag::err_objc_type_param_bound_explicit_nullability) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 727 | << paramName << typeBound |
| 728 | << FixItHint::CreateRemoval(rangeToRemove); |
John McCall | 6997525 | 2015-09-23 22:14:21 +0000 | [diff] [blame] | 729 | diagnosed = true; |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | if (!diagnosed) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 735 | Diag(qual ? qual.getBeginLoc() |
| 736 | : typeBoundInfo->getTypeLoc().getBeginLoc(), |
| 737 | diag::err_objc_type_param_bound_qualified) |
| 738 | << paramName << typeBound |
| 739 | << typeBound.getQualifiers().getAsString() |
| 740 | << FixItHint::CreateRemoval(rangeToRemove); |
John McCall | 6997525 | 2015-09-23 22:14:21 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | // If the type bound has qualifiers other than CVR, we need to strip |
| 744 | // them or we'll probably assert later when trying to apply new |
| 745 | // qualifiers. |
| 746 | Qualifiers quals = typeBound.getQualifiers(); |
| 747 | quals.removeCVRQualifiers(); |
| 748 | if (!quals.empty()) { |
| 749 | typeBoundInfo = |
| 750 | Context.getTrivialTypeSourceInfo(typeBound.getUnqualifiedType()); |
| 751 | } |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 752 | } |
| 753 | } |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | // If there was no explicit type bound (or we removed it due to an error), |
| 757 | // use 'id' instead. |
| 758 | if (!typeBoundInfo) { |
| 759 | colonLoc = SourceLocation(); |
| 760 | typeBoundInfo = Context.getTrivialTypeSourceInfo(Context.getObjCIdType()); |
| 761 | } |
| 762 | |
| 763 | // Create the type parameter. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 764 | return ObjCTypeParamDecl::Create(Context, CurContext, variance, varianceLoc, |
| 765 | index, paramLoc, paramName, colonLoc, |
| 766 | typeBoundInfo); |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | ObjCTypeParamList *Sema::actOnObjCTypeParamList(Scope *S, |
| 770 | SourceLocation lAngleLoc, |
| 771 | ArrayRef<Decl *> typeParamsIn, |
| 772 | SourceLocation rAngleLoc) { |
| 773 | // We know that the array only contains Objective-C type parameters. |
| 774 | ArrayRef<ObjCTypeParamDecl *> |
| 775 | typeParams( |
| 776 | reinterpret_cast<ObjCTypeParamDecl * const *>(typeParamsIn.data()), |
| 777 | typeParamsIn.size()); |
| 778 | |
| 779 | // Diagnose redeclarations of type parameters. |
| 780 | // We do this now because Objective-C type parameters aren't pushed into |
| 781 | // scope until later (after the instance variable block), but we want the |
| 782 | // diagnostics to occur right after we parse the type parameter list. |
| 783 | llvm::SmallDenseMap<IdentifierInfo *, ObjCTypeParamDecl *> knownParams; |
| 784 | for (auto typeParam : typeParams) { |
| 785 | auto known = knownParams.find(typeParam->getIdentifier()); |
| 786 | if (known != knownParams.end()) { |
| 787 | Diag(typeParam->getLocation(), diag::err_objc_type_param_redecl) |
| 788 | << typeParam->getIdentifier() |
| 789 | << SourceRange(known->second->getLocation()); |
| 790 | |
| 791 | typeParam->setInvalidDecl(); |
| 792 | } else { |
| 793 | knownParams.insert(std::make_pair(typeParam->getIdentifier(), typeParam)); |
| 794 | |
| 795 | // Push the type parameter into scope. |
| 796 | PushOnScopeChains(typeParam, S, /*AddToContext=*/false); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | // Create the parameter list. |
| 801 | return ObjCTypeParamList::create(Context, lAngleLoc, typeParams, rAngleLoc); |
| 802 | } |
| 803 | |
| 804 | void Sema::popObjCTypeParamList(Scope *S, ObjCTypeParamList *typeParamList) { |
| 805 | for (auto typeParam : *typeParamList) { |
| 806 | if (!typeParam->isInvalidDecl()) { |
| 807 | S->RemoveDecl(typeParam); |
| 808 | IdResolver.RemoveDecl(typeParam); |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | namespace { |
| 814 | /// The context in which an Objective-C type parameter list occurs, for use |
| 815 | /// in diagnostics. |
| 816 | enum class TypeParamListContext { |
| 817 | ForwardDeclaration, |
| 818 | Definition, |
| 819 | Category, |
| 820 | Extension |
| 821 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 822 | } // end anonymous namespace |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 823 | |
| 824 | /// Check consistency between two Objective-C type parameter lists, e.g., |
NAKAMURA Takumi | 4c3ab45 | 2015-07-08 02:35:56 +0000 | [diff] [blame] | 825 | /// between a category/extension and an \@interface or between an \@class and an |
| 826 | /// \@interface. |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 827 | static bool checkTypeParamListConsistency(Sema &S, |
| 828 | ObjCTypeParamList *prevTypeParams, |
| 829 | ObjCTypeParamList *newTypeParams, |
| 830 | TypeParamListContext newContext) { |
| 831 | // If the sizes don't match, complain about that. |
| 832 | if (prevTypeParams->size() != newTypeParams->size()) { |
| 833 | SourceLocation diagLoc; |
| 834 | if (newTypeParams->size() > prevTypeParams->size()) { |
| 835 | diagLoc = newTypeParams->begin()[prevTypeParams->size()]->getLocation(); |
| 836 | } else { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 837 | diagLoc = S.getLocForEndOfToken(newTypeParams->back()->getEndLoc()); |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | S.Diag(diagLoc, diag::err_objc_type_param_arity_mismatch) |
| 841 | << static_cast<unsigned>(newContext) |
| 842 | << (newTypeParams->size() > prevTypeParams->size()) |
| 843 | << prevTypeParams->size() |
| 844 | << newTypeParams->size(); |
| 845 | |
| 846 | return true; |
| 847 | } |
| 848 | |
| 849 | // Match up the type parameters. |
| 850 | for (unsigned i = 0, n = prevTypeParams->size(); i != n; ++i) { |
| 851 | ObjCTypeParamDecl *prevTypeParam = prevTypeParams->begin()[i]; |
| 852 | ObjCTypeParamDecl *newTypeParam = newTypeParams->begin()[i]; |
| 853 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 854 | // Check for consistency of the variance. |
| 855 | if (newTypeParam->getVariance() != prevTypeParam->getVariance()) { |
| 856 | if (newTypeParam->getVariance() == ObjCTypeParamVariance::Invariant && |
| 857 | newContext != TypeParamListContext::Definition) { |
| 858 | // When the new type parameter is invariant and is not part |
| 859 | // of the definition, just propagate the variance. |
| 860 | newTypeParam->setVariance(prevTypeParam->getVariance()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 861 | } else if (prevTypeParam->getVariance() |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 862 | == ObjCTypeParamVariance::Invariant && |
| 863 | !(isa<ObjCInterfaceDecl>(prevTypeParam->getDeclContext()) && |
| 864 | cast<ObjCInterfaceDecl>(prevTypeParam->getDeclContext()) |
| 865 | ->getDefinition() == prevTypeParam->getDeclContext())) { |
| 866 | // When the old parameter is invariant and was not part of the |
| 867 | // definition, just ignore the difference because it doesn't |
| 868 | // matter. |
| 869 | } else { |
| 870 | { |
| 871 | // Diagnose the conflict and update the second declaration. |
| 872 | SourceLocation diagLoc = newTypeParam->getVarianceLoc(); |
| 873 | if (diagLoc.isInvalid()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 874 | diagLoc = newTypeParam->getBeginLoc(); |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 875 | |
| 876 | auto diag = S.Diag(diagLoc, |
| 877 | diag::err_objc_type_param_variance_conflict) |
| 878 | << static_cast<unsigned>(newTypeParam->getVariance()) |
| 879 | << newTypeParam->getDeclName() |
| 880 | << static_cast<unsigned>(prevTypeParam->getVariance()) |
| 881 | << prevTypeParam->getDeclName(); |
| 882 | switch (prevTypeParam->getVariance()) { |
| 883 | case ObjCTypeParamVariance::Invariant: |
| 884 | diag << FixItHint::CreateRemoval(newTypeParam->getVarianceLoc()); |
| 885 | break; |
| 886 | |
| 887 | case ObjCTypeParamVariance::Covariant: |
| 888 | case ObjCTypeParamVariance::Contravariant: { |
| 889 | StringRef newVarianceStr |
| 890 | = prevTypeParam->getVariance() == ObjCTypeParamVariance::Covariant |
| 891 | ? "__covariant" |
| 892 | : "__contravariant"; |
| 893 | if (newTypeParam->getVariance() |
| 894 | == ObjCTypeParamVariance::Invariant) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 895 | diag << FixItHint::CreateInsertion(newTypeParam->getBeginLoc(), |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 896 | (newVarianceStr + " ").str()); |
| 897 | } else { |
| 898 | diag << FixItHint::CreateReplacement(newTypeParam->getVarianceLoc(), |
| 899 | newVarianceStr); |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
| 906 | << prevTypeParam->getDeclName(); |
| 907 | |
| 908 | // Override the variance. |
| 909 | newTypeParam->setVariance(prevTypeParam->getVariance()); |
| 910 | } |
| 911 | } |
| 912 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 913 | // If the bound types match, there's nothing to do. |
| 914 | if (S.Context.hasSameType(prevTypeParam->getUnderlyingType(), |
| 915 | newTypeParam->getUnderlyingType())) |
| 916 | continue; |
| 917 | |
| 918 | // If the new type parameter's bound was explicit, complain about it being |
| 919 | // different from the original. |
| 920 | if (newTypeParam->hasExplicitBound()) { |
| 921 | SourceRange newBoundRange = newTypeParam->getTypeSourceInfo() |
| 922 | ->getTypeLoc().getSourceRange(); |
| 923 | S.Diag(newBoundRange.getBegin(), diag::err_objc_type_param_bound_conflict) |
| 924 | << newTypeParam->getUnderlyingType() |
| 925 | << newTypeParam->getDeclName() |
| 926 | << prevTypeParam->hasExplicitBound() |
| 927 | << prevTypeParam->getUnderlyingType() |
| 928 | << (newTypeParam->getDeclName() == prevTypeParam->getDeclName()) |
| 929 | << prevTypeParam->getDeclName() |
| 930 | << FixItHint::CreateReplacement( |
| 931 | newBoundRange, |
| 932 | prevTypeParam->getUnderlyingType().getAsString( |
| 933 | S.Context.getPrintingPolicy())); |
| 934 | |
| 935 | S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
| 936 | << prevTypeParam->getDeclName(); |
| 937 | |
| 938 | // Override the new type parameter's bound type with the previous type, |
| 939 | // so that it's consistent. |
| 940 | newTypeParam->setTypeSourceInfo( |
| 941 | S.Context.getTrivialTypeSourceInfo(prevTypeParam->getUnderlyingType())); |
| 942 | continue; |
| 943 | } |
| 944 | |
| 945 | // The new type parameter got the implicit bound of 'id'. That's okay for |
| 946 | // categories and extensions (overwrite it later), but not for forward |
| 947 | // declarations and @interfaces, because those must be standalone. |
| 948 | if (newContext == TypeParamListContext::ForwardDeclaration || |
| 949 | newContext == TypeParamListContext::Definition) { |
| 950 | // Diagnose this problem for forward declarations and definitions. |
| 951 | SourceLocation insertionLoc |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 952 | = S.getLocForEndOfToken(newTypeParam->getLocation()); |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 953 | std::string newCode |
| 954 | = " : " + prevTypeParam->getUnderlyingType().getAsString( |
| 955 | S.Context.getPrintingPolicy()); |
| 956 | S.Diag(newTypeParam->getLocation(), |
| 957 | diag::err_objc_type_param_bound_missing) |
| 958 | << prevTypeParam->getUnderlyingType() |
| 959 | << newTypeParam->getDeclName() |
| 960 | << (newContext == TypeParamListContext::ForwardDeclaration) |
| 961 | << FixItHint::CreateInsertion(insertionLoc, newCode); |
| 962 | |
| 963 | S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
| 964 | << prevTypeParam->getDeclName(); |
| 965 | } |
| 966 | |
| 967 | // Update the new type parameter's bound to match the previous one. |
| 968 | newTypeParam->setTypeSourceInfo( |
| 969 | S.Context.getTrivialTypeSourceInfo(prevTypeParam->getUnderlyingType())); |
| 970 | } |
| 971 | |
| 972 | return false; |
| 973 | } |
| 974 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 975 | Decl *Sema::ActOnStartClassInterface( |
| 976 | Scope *S, SourceLocation AtInterfaceLoc, IdentifierInfo *ClassName, |
| 977 | SourceLocation ClassLoc, ObjCTypeParamList *typeParamList, |
| 978 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
| 979 | ArrayRef<ParsedType> SuperTypeArgs, SourceRange SuperTypeArgsRange, |
| 980 | Decl *const *ProtoRefs, unsigned NumProtoRefs, |
| 981 | const SourceLocation *ProtoLocs, SourceLocation EndProtoLoc, |
| 982 | const ParsedAttributesView &AttrList) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 983 | assert(ClassName && "Missing class identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 985 | // Check for another declaration kind with the same name. |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 986 | NamedDecl *PrevDecl = |
| 987 | LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName, |
| 988 | forRedeclarationInCurContext()); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 989 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 990 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 991 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 992 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 993 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 994 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 995 | // Create a declaration to describe this @interface. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 996 | ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Argyrios Kyrtzidis | dd71063 | 2013-06-18 21:26:33 +0000 | [diff] [blame] | 997 | |
| 998 | if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) { |
| 999 | // A previous decl with a different name is because of |
| 1000 | // @compatibility_alias, for example: |
| 1001 | // \code |
| 1002 | // @class NewImage; |
| 1003 | // @compatibility_alias OldImage NewImage; |
| 1004 | // \endcode |
| 1005 | // A lookup for 'OldImage' will return the 'NewImage' decl. |
| 1006 | // |
| 1007 | // In such a case use the real declaration name, instead of the alias one, |
| 1008 | // otherwise we will break IdentifierResolver and redecls-chain invariants. |
| 1009 | // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl |
| 1010 | // has been aliased. |
| 1011 | ClassName = PrevIDecl->getIdentifier(); |
| 1012 | } |
| 1013 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1014 | // If there was a forward declaration with type parameters, check |
| 1015 | // for consistency. |
| 1016 | if (PrevIDecl) { |
| 1017 | if (ObjCTypeParamList *prevTypeParamList = PrevIDecl->getTypeParamList()) { |
| 1018 | if (typeParamList) { |
| 1019 | // Both have type parameter lists; check for consistency. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1020 | if (checkTypeParamListConsistency(*this, prevTypeParamList, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1021 | typeParamList, |
| 1022 | TypeParamListContext::Definition)) { |
| 1023 | typeParamList = nullptr; |
| 1024 | } |
| 1025 | } else { |
| 1026 | Diag(ClassLoc, diag::err_objc_parameterized_forward_class_first) |
| 1027 | << ClassName; |
| 1028 | Diag(prevTypeParamList->getLAngleLoc(), diag::note_previous_decl) |
| 1029 | << ClassName; |
| 1030 | |
| 1031 | // Clone the type parameter list. |
| 1032 | SmallVector<ObjCTypeParamDecl *, 4> clonedTypeParams; |
| 1033 | for (auto typeParam : *prevTypeParamList) { |
| 1034 | clonedTypeParams.push_back( |
| 1035 | ObjCTypeParamDecl::Create( |
| 1036 | Context, |
| 1037 | CurContext, |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 1038 | typeParam->getVariance(), |
| 1039 | SourceLocation(), |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1040 | typeParam->getIndex(), |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1041 | SourceLocation(), |
| 1042 | typeParam->getIdentifier(), |
| 1043 | SourceLocation(), |
| 1044 | Context.getTrivialTypeSourceInfo(typeParam->getUnderlyingType()))); |
| 1045 | } |
| 1046 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1047 | typeParamList = ObjCTypeParamList::create(Context, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1048 | SourceLocation(), |
| 1049 | clonedTypeParams, |
| 1050 | SourceLocation()); |
| 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1055 | ObjCInterfaceDecl *IDecl |
| 1056 | = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1057 | typeParamList, PrevIDecl, ClassLoc); |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1058 | if (PrevIDecl) { |
| 1059 | // Class already seen. Was it a definition? |
| 1060 | if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) { |
| 1061 | Diag(AtInterfaceLoc, diag::err_duplicate_class_def) |
| 1062 | << PrevIDecl->getDeclName(); |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 1063 | Diag(Def->getLocation(), diag::note_previous_definition); |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1064 | IDecl->setInvalidDecl(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1065 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1066 | } |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1067 | |
| 1068 | ProcessDeclAttributeList(TUScope, IDecl, AttrList); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1069 | AddPragmaAttributes(TUScope, IDecl); |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1070 | PushOnScopeChains(IDecl, TUScope); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1071 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1072 | // Start the definition of this class. If we're in a redefinition case, there |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1073 | // 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] | 1074 | if (!IDecl->hasDefinition()) |
| 1075 | IDecl->startDefinition(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1076 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1077 | if (SuperName) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1078 | // Diagnose availability in the context of the @interface. |
| 1079 | ContextRAII SavedContext(*this, IDecl); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1080 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1081 | ActOnSuperClassOfClassInterface(S, AtInterfaceLoc, IDecl, |
| 1082 | ClassName, ClassLoc, |
| 1083 | SuperName, SuperLoc, SuperTypeArgs, |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1084 | SuperTypeArgsRange); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1085 | } else { // we have a root class. |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 1086 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1087 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | |
Sebastian Redl | e7c1fe6 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 1089 | // Check then save referenced protocols. |
Chris Lattner | df59f5a | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 1090 | if (NumProtoRefs) { |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1091 | diagnoseUseOfProtocols(*this, IDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
| 1092 | NumProtoRefs, ProtoLocs); |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 1093 | IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 1094 | ProtoLocs, Context); |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 1095 | IDecl->setEndOfDefinitionLoc(EndProtoLoc); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1096 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1098 | CheckObjCDeclScope(IDecl); |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1099 | return ActOnObjCContainerStartDefinition(IDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Fariborz Jahanian | b7c5f74 | 2013-09-25 19:36:32 +0000 | [diff] [blame] | 1102 | /// ActOnTypedefedProtocols - this action finds protocol list as part of the |
| 1103 | /// typedef'ed use for a qualified super class and adds them to the list |
| 1104 | /// of the protocols. |
| 1105 | void Sema::ActOnTypedefedProtocols(SmallVectorImpl<Decl *> &ProtocolRefs, |
Argyrios Kyrtzidis | f95a000 | 2016-11-09 02:47:07 +0000 | [diff] [blame] | 1106 | SmallVectorImpl<SourceLocation> &ProtocolLocs, |
Fariborz Jahanian | b7c5f74 | 2013-09-25 19:36:32 +0000 | [diff] [blame] | 1107 | IdentifierInfo *SuperName, |
| 1108 | SourceLocation SuperLoc) { |
| 1109 | if (!SuperName) |
| 1110 | return; |
| 1111 | NamedDecl* IDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
| 1112 | LookupOrdinaryName); |
| 1113 | if (!IDecl) |
| 1114 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1115 | |
Fariborz Jahanian | b7c5f74 | 2013-09-25 19:36:32 +0000 | [diff] [blame] | 1116 | if (const TypedefNameDecl *TDecl = dyn_cast_or_null<TypedefNameDecl>(IDecl)) { |
| 1117 | QualType T = TDecl->getUnderlyingType(); |
| 1118 | if (T->isObjCObjectType()) |
Argyrios Kyrtzidis | f95a000 | 2016-11-09 02:47:07 +0000 | [diff] [blame] | 1119 | if (const ObjCObjectType *OPT = T->getAs<ObjCObjectType>()) { |
Benjamin Kramer | f989042 | 2015-02-17 16:48:30 +0000 | [diff] [blame] | 1120 | ProtocolRefs.append(OPT->qual_begin(), OPT->qual_end()); |
Argyrios Kyrtzidis | f95a000 | 2016-11-09 02:47:07 +0000 | [diff] [blame] | 1121 | // FIXME: Consider whether this should be an invalid loc since the loc |
| 1122 | // is not actually pointing to a protocol name reference but to the |
| 1123 | // typedef reference. Note that the base class name loc is also pointing |
| 1124 | // at the typedef. |
| 1125 | ProtocolLocs.append(OPT->getNumProtocols(), SuperLoc); |
| 1126 | } |
Fariborz Jahanian | b7c5f74 | 2013-09-25 19:36:32 +0000 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | |
Richard Smith | ac4e36d | 2012-08-08 23:32:13 +0000 | [diff] [blame] | 1130 | /// ActOnCompatibilityAlias - this action is called after complete parsing of |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 1131 | /// a \@compatibility_alias declaration. It sets up the alias relationships. |
Richard Smith | ac4e36d | 2012-08-08 23:32:13 +0000 | [diff] [blame] | 1132 | Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc, |
| 1133 | IdentifierInfo *AliasName, |
| 1134 | SourceLocation AliasLocation, |
| 1135 | IdentifierInfo *ClassName, |
| 1136 | SourceLocation ClassLocation) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1137 | // Look for previous declaration of alias name |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1138 | NamedDecl *ADecl = |
| 1139 | LookupSingleName(TUScope, AliasName, AliasLocation, LookupOrdinaryName, |
| 1140 | forRedeclarationInCurContext()); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1141 | if (ADecl) { |
Eli Friedman | fd6b3f8 | 2013-06-21 01:49:53 +0000 | [diff] [blame] | 1142 | Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; |
Chris Lattner | d068503 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 1143 | Diag(ADecl->getLocation(), diag::note_previous_declaration); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1144 | return nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1145 | } |
| 1146 | // Check for class declaration |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1147 | NamedDecl *CDeclU = |
| 1148 | LookupSingleName(TUScope, ClassName, ClassLocation, LookupOrdinaryName, |
| 1149 | forRedeclarationInCurContext()); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1150 | if (const TypedefNameDecl *TDecl = |
| 1151 | dyn_cast_or_null<TypedefNameDecl>(CDeclU)) { |
Fariborz Jahanian | 17290c3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 1152 | QualType T = TDecl->getUnderlyingType(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1153 | if (T->isObjCObjectType()) { |
Simon Pilgrim | a867cca | 2019-10-17 10:35:29 +0000 | [diff] [blame] | 1154 | if (NamedDecl *IDecl = T->castAs<ObjCObjectType>()->getInterface()) { |
Fariborz Jahanian | 17290c3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 1155 | ClassName = IDecl->getIdentifier(); |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1156 | CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1157 | LookupOrdinaryName, |
| 1158 | forRedeclarationInCurContext()); |
Fariborz Jahanian | 17290c3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | } |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 1162 | ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1163 | if (!CDecl) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1164 | Diag(ClassLocation, diag::warn_undef_interface) << ClassName; |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 1165 | if (CDeclU) |
Chris Lattner | d068503 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 1166 | Diag(CDeclU->getLocation(), diag::note_previous_declaration); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1167 | return nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1168 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1169 | |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 1170 | // Everything checked out, instantiate a new alias declaration AST. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1171 | ObjCCompatibleAliasDecl *AliasDecl = |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1172 | ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1173 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1174 | if (!CheckObjCDeclScope(AliasDecl)) |
Douglas Gregor | 38feed8 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 1175 | PushOnScopeChains(AliasDecl, TUScope); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1176 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1177 | return AliasDecl; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1180 | bool Sema::CheckForwardProtocolDeclarationForCircularDependency( |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1181 | IdentifierInfo *PName, |
| 1182 | SourceLocation &Ploc, SourceLocation PrevLoc, |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1183 | const ObjCList<ObjCProtocolDecl> &PList) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1184 | |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1185 | bool res = false; |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1186 | for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(), |
| 1187 | E = PList.end(); I != E; ++I) { |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1188 | if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(), |
| 1189 | Ploc)) { |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1190 | if (PDecl->getIdentifier() == PName) { |
| 1191 | Diag(Ploc, diag::err_protocol_has_circular_dependency); |
| 1192 | Diag(PrevLoc, diag::note_previous_definition); |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1193 | res = true; |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1194 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1195 | |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 1196 | if (!PDecl->hasDefinition()) |
| 1197 | continue; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1198 | |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1199 | if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc, |
| 1200 | PDecl->getLocation(), PDecl->getReferencedProtocols())) |
| 1201 | res = true; |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1202 | } |
| 1203 | } |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1204 | return res; |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1207 | Decl *Sema::ActOnStartProtocolInterface( |
| 1208 | SourceLocation AtProtoInterfaceLoc, IdentifierInfo *ProtocolName, |
| 1209 | SourceLocation ProtocolLoc, Decl *const *ProtoRefs, unsigned NumProtoRefs, |
| 1210 | const SourceLocation *ProtoLocs, SourceLocation EndProtoLoc, |
| 1211 | const ParsedAttributesView &AttrList) { |
Fariborz Jahanian | cadf7c5 | 2011-05-12 22:04:39 +0000 | [diff] [blame] | 1212 | bool err = false; |
Daniel Dunbar | 26e2ab4 | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 1213 | // FIXME: Deal with AttrList. |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1214 | assert(ProtocolName && "Missing protocol identifier"); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1215 | ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1216 | forRedeclarationInCurContext()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1217 | ObjCProtocolDecl *PDecl = nullptr; |
| 1218 | if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : nullptr) { |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1219 | // If we already have a definition, complain. |
| 1220 | Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName; |
| 1221 | Diag(Def->getLocation(), diag::note_previous_definition); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1222 | |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1223 | // Create a new protocol that is completely distinct from previous |
| 1224 | // declarations, and do not make this protocol available for name lookup. |
| 1225 | // That way, we'll end up completely ignoring the duplicate. |
| 1226 | // FIXME: Can we turn this into an error? |
| 1227 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName, |
| 1228 | ProtocolLoc, AtProtoInterfaceLoc, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1229 | /*PrevDecl=*/nullptr); |
Bruno Cardoso Lopes | 7dcf23e | 2018-06-30 00:49:27 +0000 | [diff] [blame] | 1230 | |
| 1231 | // If we are using modules, add the decl to the context in order to |
| 1232 | // serialize something meaningful. |
| 1233 | if (getLangOpts().Modules) |
| 1234 | PushOnScopeChains(PDecl, TUScope); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1235 | PDecl->startDefinition(); |
| 1236 | } else { |
| 1237 | if (PrevDecl) { |
| 1238 | // Check for circular dependencies among protocol declarations. This can |
| 1239 | // only happen if this protocol was forward-declared. |
Argyrios Kyrtzidis | 95dfc12 | 2011-11-13 22:08:30 +0000 | [diff] [blame] | 1240 | ObjCList<ObjCProtocolDecl> PList; |
| 1241 | PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context); |
| 1242 | err = CheckForwardProtocolDeclarationForCircularDependency( |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1243 | ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList); |
Argyrios Kyrtzidis | 95dfc12 | 2011-11-13 22:08:30 +0000 | [diff] [blame] | 1244 | } |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1245 | |
| 1246 | // Create the new declaration. |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 1247 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName, |
Argyrios Kyrtzidis | 1f4bee5 | 2011-10-17 19:48:06 +0000 | [diff] [blame] | 1248 | ProtocolLoc, AtProtoInterfaceLoc, |
Douglas Gregor | 05a1f4d | 2012-01-01 22:06:18 +0000 | [diff] [blame] | 1249 | /*PrevDecl=*/PrevDecl); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1250 | |
Douglas Gregor | de9f17e | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 1251 | PushOnScopeChains(PDecl, TUScope); |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 1252 | PDecl->startDefinition(); |
Chris Lattner | f87ca0a | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 1253 | } |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1254 | |
| 1255 | ProcessDeclAttributeList(TUScope, PDecl, AttrList); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1256 | AddPragmaAttributes(TUScope, PDecl); |
| 1257 | |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1258 | // Merge attributes from previous declarations. |
| 1259 | if (PrevDecl) |
| 1260 | mergeDeclAttributes(PDecl, PrevDecl); |
| 1261 | |
Fariborz Jahanian | cadf7c5 | 2011-05-12 22:04:39 +0000 | [diff] [blame] | 1262 | if (!err && NumProtoRefs ) { |
Chris Lattner | acc04a9 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 1263 | /// Check then save referenced protocols. |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1264 | diagnoseUseOfProtocols(*this, PDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
| 1265 | NumProtoRefs, ProtoLocs); |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 1266 | PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 1267 | ProtoLocs, Context); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1268 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1269 | |
| 1270 | CheckObjCDeclScope(PDecl); |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1271 | return ActOnObjCContainerStartDefinition(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1274 | static bool NestedProtocolHasNoDefinition(ObjCProtocolDecl *PDecl, |
| 1275 | ObjCProtocolDecl *&UndefinedProtocol) { |
| 1276 | if (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()) { |
| 1277 | UndefinedProtocol = PDecl; |
| 1278 | return true; |
| 1279 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1280 | |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 1281 | for (auto *PI : PDecl->protocols()) |
| 1282 | if (NestedProtocolHasNoDefinition(PI, UndefinedProtocol)) { |
| 1283 | UndefinedProtocol = PI; |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1284 | return true; |
| 1285 | } |
| 1286 | return false; |
| 1287 | } |
| 1288 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1289 | /// FindProtocolDeclaration - This routine looks up protocols and |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1290 | /// issues an error if they are not declared. It returns list of |
| 1291 | /// protocol declarations in its 'Protocols' argument. |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1292 | void |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1293 | Sema::FindProtocolDeclaration(bool WarnOnDeclarations, bool ForObjCContainer, |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1294 | ArrayRef<IdentifierLocPair> ProtocolId, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1295 | SmallVectorImpl<Decl *> &Protocols) { |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1296 | for (const IdentifierLocPair &Pair : ProtocolId) { |
| 1297 | ObjCProtocolDecl *PDecl = LookupProtocol(Pair.first, Pair.second); |
Chris Lattner | 9c1842b | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 1298 | if (!PDecl) { |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 1299 | DeclFilterCCC<ObjCProtocolDecl> CCC{}; |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1300 | TypoCorrection Corrected = CorrectTypo( |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 1301 | DeclarationNameInfo(Pair.first, Pair.second), LookupObjCProtocolName, |
| 1302 | TUScope, nullptr, CCC, CTK_ErrorRecovery); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1303 | if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) |
| 1304 | diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest) |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1305 | << Pair.first); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | if (!PDecl) { |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1309 | Diag(Pair.second, diag::err_undeclared_protocol) << Pair.first; |
Chris Lattner | 9c1842b | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 1310 | continue; |
| 1311 | } |
Fariborz Jahanian | ada44a2 | 2013-04-09 17:52:29 +0000 | [diff] [blame] | 1312 | // If this is a forward protocol declaration, get its definition. |
| 1313 | if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition()) |
| 1314 | PDecl = PDecl->getDefinition(); |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1315 | |
| 1316 | // For an objc container, delay protocol reference checking until after we |
| 1317 | // can set the objc decl as the availability context, otherwise check now. |
| 1318 | if (!ForObjCContainer) { |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1319 | (void)DiagnoseUseOfDecl(PDecl, Pair.second); |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1320 | } |
Chris Lattner | 9c1842b | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 1321 | |
| 1322 | // If this is a forward declaration and we are supposed to warn in this |
| 1323 | // case, do it. |
Douglas Gregor | eed4979 | 2013-01-17 00:38:46 +0000 | [diff] [blame] | 1324 | // FIXME: Recover nicely in the hidden case. |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1325 | ObjCProtocolDecl *UndefinedProtocol; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1326 | |
Douglas Gregor | eed4979 | 2013-01-17 00:38:46 +0000 | [diff] [blame] | 1327 | if (WarnOnDeclarations && |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1328 | NestedProtocolHasNoDefinition(PDecl, UndefinedProtocol)) { |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1329 | Diag(Pair.second, diag::warn_undef_protocolref) << Pair.first; |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1330 | Diag(UndefinedProtocol->getLocation(), diag::note_protocol_decl_undefined) |
| 1331 | << UndefinedProtocol; |
| 1332 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1333 | Protocols.push_back(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1334 | } |
| 1335 | } |
| 1336 | |
Benjamin Kramer | 8b851d0 | 2015-07-13 20:42:13 +0000 | [diff] [blame] | 1337 | namespace { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1338 | // Callback to only accept typo corrections that are either |
| 1339 | // Objective-C protocols or valid Objective-C type arguments. |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 1340 | class ObjCTypeArgOrProtocolValidatorCCC final |
| 1341 | : public CorrectionCandidateCallback { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1342 | ASTContext &Context; |
| 1343 | Sema::LookupNameKind LookupKind; |
| 1344 | public: |
| 1345 | ObjCTypeArgOrProtocolValidatorCCC(ASTContext &context, |
| 1346 | Sema::LookupNameKind lookupKind) |
| 1347 | : Context(context), LookupKind(lookupKind) { } |
| 1348 | |
| 1349 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
| 1350 | // If we're allowed to find protocols and we have a protocol, accept it. |
| 1351 | if (LookupKind != Sema::LookupOrdinaryName) { |
| 1352 | if (candidate.getCorrectionDeclAs<ObjCProtocolDecl>()) |
| 1353 | return true; |
| 1354 | } |
| 1355 | |
| 1356 | // If we're allowed to find type names and we have one, accept it. |
| 1357 | if (LookupKind != Sema::LookupObjCProtocolName) { |
| 1358 | // If we have a type declaration, we might accept this result. |
| 1359 | if (auto typeDecl = candidate.getCorrectionDeclAs<TypeDecl>()) { |
| 1360 | // If we found a tag declaration outside of C++, skip it. This |
| 1361 | // can happy because we look for any name when there is no |
| 1362 | // bias to protocol or type names. |
| 1363 | if (isa<RecordDecl>(typeDecl) && !Context.getLangOpts().CPlusPlus) |
| 1364 | return false; |
| 1365 | |
| 1366 | // Make sure the type is something we would accept as a type |
| 1367 | // argument. |
| 1368 | auto type = Context.getTypeDeclType(typeDecl); |
| 1369 | if (type->isObjCObjectPointerType() || |
| 1370 | type->isBlockPointerType() || |
| 1371 | type->isDependentType() || |
| 1372 | type->isObjCObjectType()) |
| 1373 | return true; |
| 1374 | |
| 1375 | return false; |
| 1376 | } |
| 1377 | |
| 1378 | // If we have an Objective-C class type, accept it; there will |
| 1379 | // be another fix to add the '*'. |
| 1380 | if (candidate.getCorrectionDeclAs<ObjCInterfaceDecl>()) |
| 1381 | return true; |
| 1382 | |
| 1383 | return false; |
| 1384 | } |
| 1385 | |
| 1386 | return false; |
| 1387 | } |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 1388 | |
| 1389 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 1390 | return std::make_unique<ObjCTypeArgOrProtocolValidatorCCC>(*this); |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 1391 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1392 | }; |
Benjamin Kramer | 8b851d0 | 2015-07-13 20:42:13 +0000 | [diff] [blame] | 1393 | } // end anonymous namespace |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1394 | |
Bruno Cardoso Lopes | c54768f | 2016-04-13 20:59:07 +0000 | [diff] [blame] | 1395 | void Sema::DiagnoseTypeArgsAndProtocols(IdentifierInfo *ProtocolId, |
| 1396 | SourceLocation ProtocolLoc, |
| 1397 | IdentifierInfo *TypeArgId, |
| 1398 | SourceLocation TypeArgLoc, |
| 1399 | bool SelectProtocolFirst) { |
| 1400 | Diag(TypeArgLoc, diag::err_objc_type_args_and_protocols) |
| 1401 | << SelectProtocolFirst << TypeArgId << ProtocolId |
| 1402 | << SourceRange(ProtocolLoc); |
| 1403 | } |
| 1404 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1405 | void Sema::actOnObjCTypeArgsOrProtocolQualifiers( |
| 1406 | Scope *S, |
Douglas Gregor | 10dc9d8 | 2015-07-07 03:58:28 +0000 | [diff] [blame] | 1407 | ParsedType baseType, |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1408 | SourceLocation lAngleLoc, |
| 1409 | ArrayRef<IdentifierInfo *> identifiers, |
| 1410 | ArrayRef<SourceLocation> identifierLocs, |
| 1411 | SourceLocation rAngleLoc, |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1412 | SourceLocation &typeArgsLAngleLoc, |
| 1413 | SmallVectorImpl<ParsedType> &typeArgs, |
| 1414 | SourceLocation &typeArgsRAngleLoc, |
| 1415 | SourceLocation &protocolLAngleLoc, |
| 1416 | SmallVectorImpl<Decl *> &protocols, |
| 1417 | SourceLocation &protocolRAngleLoc, |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1418 | bool warnOnIncompleteProtocols) { |
| 1419 | // Local function that updates the declaration specifiers with |
| 1420 | // protocol information. |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1421 | unsigned numProtocolsResolved = 0; |
| 1422 | auto resolvedAsProtocols = [&] { |
| 1423 | assert(numProtocolsResolved == identifiers.size() && "Unresolved protocols"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1424 | |
Douglas Gregor | 10dc9d8 | 2015-07-07 03:58:28 +0000 | [diff] [blame] | 1425 | // Determine whether the base type is a parameterized class, in |
| 1426 | // which case we want to warn about typos such as |
| 1427 | // "NSArray<NSObject>" (that should be NSArray<NSObject *>). |
| 1428 | ObjCInterfaceDecl *baseClass = nullptr; |
| 1429 | QualType base = GetTypeFromParser(baseType, nullptr); |
| 1430 | bool allAreTypeNames = false; |
| 1431 | SourceLocation firstClassNameLoc; |
| 1432 | if (!base.isNull()) { |
| 1433 | if (const auto *objcObjectType = base->getAs<ObjCObjectType>()) { |
| 1434 | baseClass = objcObjectType->getInterface(); |
| 1435 | if (baseClass) { |
| 1436 | if (auto typeParams = baseClass->getTypeParamList()) { |
| 1437 | if (typeParams->size() == numProtocolsResolved) { |
| 1438 | // Note that we should be looking for type names, too. |
| 1439 | allAreTypeNames = true; |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | } |
| 1445 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1446 | for (unsigned i = 0, n = protocols.size(); i != n; ++i) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1447 | ObjCProtocolDecl *&proto |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1448 | = reinterpret_cast<ObjCProtocolDecl *&>(protocols[i]); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1449 | // For an objc container, delay protocol reference checking until after we |
| 1450 | // can set the objc decl as the availability context, otherwise check now. |
| 1451 | if (!warnOnIncompleteProtocols) { |
| 1452 | (void)DiagnoseUseOfDecl(proto, identifierLocs[i]); |
| 1453 | } |
| 1454 | |
| 1455 | // If this is a forward protocol declaration, get its definition. |
| 1456 | if (!proto->isThisDeclarationADefinition() && proto->getDefinition()) |
| 1457 | proto = proto->getDefinition(); |
| 1458 | |
| 1459 | // If this is a forward declaration and we are supposed to warn in this |
| 1460 | // case, do it. |
| 1461 | // FIXME: Recover nicely in the hidden case. |
| 1462 | ObjCProtocolDecl *forwardDecl = nullptr; |
| 1463 | if (warnOnIncompleteProtocols && |
| 1464 | NestedProtocolHasNoDefinition(proto, forwardDecl)) { |
| 1465 | Diag(identifierLocs[i], diag::warn_undef_protocolref) |
| 1466 | << proto->getDeclName(); |
| 1467 | Diag(forwardDecl->getLocation(), diag::note_protocol_decl_undefined) |
| 1468 | << forwardDecl; |
| 1469 | } |
Douglas Gregor | 10dc9d8 | 2015-07-07 03:58:28 +0000 | [diff] [blame] | 1470 | |
| 1471 | // If everything this far has been a type name (and we care |
| 1472 | // about such things), check whether this name refers to a type |
| 1473 | // as well. |
| 1474 | if (allAreTypeNames) { |
| 1475 | if (auto *decl = LookupSingleName(S, identifiers[i], identifierLocs[i], |
| 1476 | LookupOrdinaryName)) { |
| 1477 | if (isa<ObjCInterfaceDecl>(decl)) { |
| 1478 | if (firstClassNameLoc.isInvalid()) |
| 1479 | firstClassNameLoc = identifierLocs[i]; |
| 1480 | } else if (!isa<TypeDecl>(decl)) { |
| 1481 | // Not a type. |
| 1482 | allAreTypeNames = false; |
| 1483 | } |
| 1484 | } else { |
| 1485 | allAreTypeNames = false; |
| 1486 | } |
| 1487 | } |
| 1488 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1489 | |
Douglas Gregor | 10dc9d8 | 2015-07-07 03:58:28 +0000 | [diff] [blame] | 1490 | // All of the protocols listed also have type names, and at least |
| 1491 | // one is an Objective-C class name. Check whether all of the |
| 1492 | // protocol conformances are declared by the base class itself, in |
| 1493 | // which case we warn. |
| 1494 | if (allAreTypeNames && firstClassNameLoc.isValid()) { |
| 1495 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> knownProtocols; |
| 1496 | Context.CollectInheritedProtocols(baseClass, knownProtocols); |
| 1497 | bool allProtocolsDeclared = true; |
| 1498 | for (auto proto : protocols) { |
| 1499 | if (knownProtocols.count(static_cast<ObjCProtocolDecl *>(proto)) == 0) { |
| 1500 | allProtocolsDeclared = false; |
| 1501 | break; |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | if (allProtocolsDeclared) { |
| 1506 | Diag(firstClassNameLoc, diag::warn_objc_redundant_qualified_class_type) |
| 1507 | << baseClass->getDeclName() << SourceRange(lAngleLoc, rAngleLoc) |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 1508 | << FixItHint::CreateInsertion(getLocForEndOfToken(firstClassNameLoc), |
| 1509 | " *"); |
Douglas Gregor | 10dc9d8 | 2015-07-07 03:58:28 +0000 | [diff] [blame] | 1510 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1513 | protocolLAngleLoc = lAngleLoc; |
| 1514 | protocolRAngleLoc = rAngleLoc; |
| 1515 | assert(protocols.size() == identifierLocs.size()); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1516 | }; |
| 1517 | |
| 1518 | // Attempt to resolve all of the identifiers as protocols. |
| 1519 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1520 | ObjCProtocolDecl *proto = LookupProtocol(identifiers[i], identifierLocs[i]); |
| 1521 | protocols.push_back(proto); |
| 1522 | if (proto) |
| 1523 | ++numProtocolsResolved; |
| 1524 | } |
| 1525 | |
| 1526 | // If all of the names were protocols, these were protocol qualifiers. |
| 1527 | if (numProtocolsResolved == identifiers.size()) |
| 1528 | return resolvedAsProtocols(); |
| 1529 | |
| 1530 | // Attempt to resolve all of the identifiers as type names or |
| 1531 | // Objective-C class names. The latter is technically ill-formed, |
| 1532 | // but is probably something like \c NSArray<NSView *> missing the |
| 1533 | // \c*. |
| 1534 | typedef llvm::PointerUnion<TypeDecl *, ObjCInterfaceDecl *> TypeOrClassDecl; |
| 1535 | SmallVector<TypeOrClassDecl, 4> typeDecls; |
| 1536 | unsigned numTypeDeclsResolved = 0; |
| 1537 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1538 | NamedDecl *decl = LookupSingleName(S, identifiers[i], identifierLocs[i], |
| 1539 | LookupOrdinaryName); |
| 1540 | if (!decl) { |
| 1541 | typeDecls.push_back(TypeOrClassDecl()); |
| 1542 | continue; |
| 1543 | } |
| 1544 | |
| 1545 | if (auto typeDecl = dyn_cast<TypeDecl>(decl)) { |
| 1546 | typeDecls.push_back(typeDecl); |
| 1547 | ++numTypeDeclsResolved; |
| 1548 | continue; |
| 1549 | } |
| 1550 | |
| 1551 | if (auto objcClass = dyn_cast<ObjCInterfaceDecl>(decl)) { |
| 1552 | typeDecls.push_back(objcClass); |
| 1553 | ++numTypeDeclsResolved; |
| 1554 | continue; |
| 1555 | } |
| 1556 | |
| 1557 | typeDecls.push_back(TypeOrClassDecl()); |
| 1558 | } |
| 1559 | |
| 1560 | AttributeFactory attrFactory; |
| 1561 | |
| 1562 | // Local function that forms a reference to the given type or |
| 1563 | // Objective-C class declaration. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1564 | auto resolveTypeReference = [&](TypeOrClassDecl typeDecl, SourceLocation loc) |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1565 | -> TypeResult { |
| 1566 | // Form declaration specifiers. They simply refer to the type. |
| 1567 | DeclSpec DS(attrFactory); |
| 1568 | const char* prevSpec; // unused |
| 1569 | unsigned diagID; // unused |
| 1570 | QualType type; |
| 1571 | if (auto *actualTypeDecl = typeDecl.dyn_cast<TypeDecl *>()) |
| 1572 | type = Context.getTypeDeclType(actualTypeDecl); |
| 1573 | else |
| 1574 | type = Context.getObjCInterfaceType(typeDecl.get<ObjCInterfaceDecl *>()); |
| 1575 | TypeSourceInfo *parsedTSInfo = Context.getTrivialTypeSourceInfo(type, loc); |
| 1576 | ParsedType parsedType = CreateParsedType(type, parsedTSInfo); |
| 1577 | DS.SetTypeSpecType(DeclSpec::TST_typename, loc, prevSpec, diagID, |
| 1578 | parsedType, Context.getPrintingPolicy()); |
| 1579 | // Use the identifier location for the type source range. |
| 1580 | DS.SetRangeStart(loc); |
| 1581 | DS.SetRangeEnd(loc); |
| 1582 | |
| 1583 | // Form the declarator. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1584 | Declarator D(DS, DeclaratorContext::TypeNameContext); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1585 | |
| 1586 | // If we have a typedef of an Objective-C class type that is missing a '*', |
| 1587 | // add the '*'. |
| 1588 | if (type->getAs<ObjCInterfaceType>()) { |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 1589 | SourceLocation starLoc = getLocForEndOfToken(loc); |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 1590 | D.AddTypeInfo(DeclaratorChunk::getPointer(/*TypeQuals=*/0, starLoc, |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1591 | SourceLocation(), |
| 1592 | SourceLocation(), |
| 1593 | SourceLocation(), |
Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 1594 | SourceLocation(), |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1595 | SourceLocation()), |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1596 | starLoc); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1597 | |
| 1598 | // Diagnose the missing '*'. |
| 1599 | Diag(loc, diag::err_objc_type_arg_missing_star) |
| 1600 | << type |
| 1601 | << FixItHint::CreateInsertion(starLoc, " *"); |
| 1602 | } |
| 1603 | |
| 1604 | // Convert this to a type. |
| 1605 | return ActOnTypeName(S, D); |
| 1606 | }; |
| 1607 | |
| 1608 | // Local function that updates the declaration specifiers with |
| 1609 | // type argument information. |
| 1610 | auto resolvedAsTypeDecls = [&] { |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1611 | // We did not resolve these as protocols. |
| 1612 | protocols.clear(); |
| 1613 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1614 | assert(numTypeDeclsResolved == identifiers.size() && "Unresolved type decl"); |
| 1615 | // Map type declarations to type arguments. |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1616 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1617 | // Map type reference to a type. |
| 1618 | TypeResult type = resolveTypeReference(typeDecls[i], identifierLocs[i]); |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1619 | if (!type.isUsable()) { |
| 1620 | typeArgs.clear(); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1621 | return; |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1622 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1623 | |
| 1624 | typeArgs.push_back(type.get()); |
| 1625 | } |
| 1626 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1627 | typeArgsLAngleLoc = lAngleLoc; |
| 1628 | typeArgsRAngleLoc = rAngleLoc; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1629 | }; |
| 1630 | |
| 1631 | // If all of the identifiers can be resolved as type names or |
| 1632 | // Objective-C class names, we have type arguments. |
| 1633 | if (numTypeDeclsResolved == identifiers.size()) |
| 1634 | return resolvedAsTypeDecls(); |
| 1635 | |
| 1636 | // Error recovery: some names weren't found, or we have a mix of |
| 1637 | // type and protocol names. Go resolve all of the unresolved names |
| 1638 | // and complain if we can't find a consistent answer. |
| 1639 | LookupNameKind lookupKind = LookupAnyName; |
| 1640 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1641 | // If we already have a protocol or type. Check whether it is the |
| 1642 | // right thing. |
| 1643 | if (protocols[i] || typeDecls[i]) { |
| 1644 | // If we haven't figured out whether we want types or protocols |
| 1645 | // yet, try to figure it out from this name. |
| 1646 | if (lookupKind == LookupAnyName) { |
| 1647 | // If this name refers to both a protocol and a type (e.g., \c |
| 1648 | // NSObject), don't conclude anything yet. |
| 1649 | if (protocols[i] && typeDecls[i]) |
| 1650 | continue; |
| 1651 | |
| 1652 | // Otherwise, let this name decide whether we'll be correcting |
| 1653 | // toward types or protocols. |
| 1654 | lookupKind = protocols[i] ? LookupObjCProtocolName |
| 1655 | : LookupOrdinaryName; |
| 1656 | continue; |
| 1657 | } |
| 1658 | |
| 1659 | // If we want protocols and we have a protocol, there's nothing |
| 1660 | // more to do. |
| 1661 | if (lookupKind == LookupObjCProtocolName && protocols[i]) |
| 1662 | continue; |
| 1663 | |
| 1664 | // If we want types and we have a type declaration, there's |
| 1665 | // nothing more to do. |
| 1666 | if (lookupKind == LookupOrdinaryName && typeDecls[i]) |
| 1667 | continue; |
| 1668 | |
| 1669 | // We have a conflict: some names refer to protocols and others |
| 1670 | // refer to types. |
Bruno Cardoso Lopes | c54768f | 2016-04-13 20:59:07 +0000 | [diff] [blame] | 1671 | DiagnoseTypeArgsAndProtocols(identifiers[0], identifierLocs[0], |
| 1672 | identifiers[i], identifierLocs[i], |
| 1673 | protocols[i] != nullptr); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1674 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1675 | protocols.clear(); |
| 1676 | typeArgs.clear(); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1677 | return; |
| 1678 | } |
| 1679 | |
| 1680 | // Perform typo correction on the name. |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 1681 | ObjCTypeArgOrProtocolValidatorCCC CCC(Context, lookupKind); |
| 1682 | TypoCorrection corrected = |
| 1683 | CorrectTypo(DeclarationNameInfo(identifiers[i], identifierLocs[i]), |
| 1684 | lookupKind, S, nullptr, CCC, CTK_ErrorRecovery); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1685 | if (corrected) { |
| 1686 | // Did we find a protocol? |
| 1687 | if (auto proto = corrected.getCorrectionDeclAs<ObjCProtocolDecl>()) { |
| 1688 | diagnoseTypo(corrected, |
| 1689 | PDiag(diag::err_undeclared_protocol_suggest) |
| 1690 | << identifiers[i]); |
| 1691 | lookupKind = LookupObjCProtocolName; |
| 1692 | protocols[i] = proto; |
| 1693 | ++numProtocolsResolved; |
| 1694 | continue; |
| 1695 | } |
| 1696 | |
| 1697 | // Did we find a type? |
| 1698 | if (auto typeDecl = corrected.getCorrectionDeclAs<TypeDecl>()) { |
| 1699 | diagnoseTypo(corrected, |
| 1700 | PDiag(diag::err_unknown_typename_suggest) |
| 1701 | << identifiers[i]); |
| 1702 | lookupKind = LookupOrdinaryName; |
| 1703 | typeDecls[i] = typeDecl; |
| 1704 | ++numTypeDeclsResolved; |
| 1705 | continue; |
| 1706 | } |
| 1707 | |
| 1708 | // Did we find an Objective-C class? |
| 1709 | if (auto objcClass = corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
| 1710 | diagnoseTypo(corrected, |
| 1711 | PDiag(diag::err_unknown_type_or_class_name_suggest) |
| 1712 | << identifiers[i] << true); |
| 1713 | lookupKind = LookupOrdinaryName; |
| 1714 | typeDecls[i] = objcClass; |
| 1715 | ++numTypeDeclsResolved; |
| 1716 | continue; |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | // We couldn't find anything. |
| 1721 | Diag(identifierLocs[i], |
| 1722 | (lookupKind == LookupAnyName ? diag::err_objc_type_arg_missing |
| 1723 | : lookupKind == LookupObjCProtocolName ? diag::err_undeclared_protocol |
| 1724 | : diag::err_unknown_typename)) |
| 1725 | << identifiers[i]; |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1726 | protocols.clear(); |
| 1727 | typeArgs.clear(); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1728 | return; |
| 1729 | } |
| 1730 | |
| 1731 | // If all of the names were (corrected to) protocols, these were |
| 1732 | // protocol qualifiers. |
| 1733 | if (numProtocolsResolved == identifiers.size()) |
| 1734 | return resolvedAsProtocols(); |
| 1735 | |
| 1736 | // Otherwise, all of the names were (corrected to) types. |
| 1737 | assert(numTypeDeclsResolved == identifiers.size() && "Not all types?"); |
| 1738 | return resolvedAsTypeDecls(); |
| 1739 | } |
| 1740 | |
Fariborz Jahanian | abf63e7b | 2009-03-02 19:06:08 +0000 | [diff] [blame] | 1741 | /// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1742 | /// a class method in its extension. |
| 1743 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1744 | void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1745 | ObjCInterfaceDecl *ID) { |
| 1746 | if (!ID) |
| 1747 | return; // Possibly due to previous error |
| 1748 | |
| 1749 | llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap; |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 1750 | for (auto *MD : ID->methods()) |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1751 | MethodMap[MD->getSelector()] = MD; |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1752 | |
| 1753 | if (MethodMap.empty()) |
| 1754 | return; |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 1755 | for (const auto *Method : CAT->methods()) { |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1756 | const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()]; |
Fariborz Jahanian | 83d674e | 2014-03-17 17:46:10 +0000 | [diff] [blame] | 1757 | if (PrevMethod && |
| 1758 | (PrevMethod->isInstanceMethod() == Method->isInstanceMethod()) && |
| 1759 | !MatchTwoMethodDeclarations(Method, PrevMethod)) { |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1760 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
| 1761 | << Method->getDeclName(); |
| 1762 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 1763 | } |
| 1764 | } |
| 1765 | } |
| 1766 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 1767 | /// ActOnForwardProtocolDeclaration - Handle \@protocol foo; |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1768 | Sema::DeclGroupPtrTy |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1769 | Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
Craig Topper | 0f723bb | 2015-10-22 05:00:01 +0000 | [diff] [blame] | 1770 | ArrayRef<IdentifierLocPair> IdentList, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1771 | const ParsedAttributesView &attrList) { |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1772 | SmallVector<Decl *, 8> DeclsInGroup; |
Craig Topper | 0f723bb | 2015-10-22 05:00:01 +0000 | [diff] [blame] | 1773 | for (const IdentifierLocPair &IdentPair : IdentList) { |
| 1774 | IdentifierInfo *Ident = IdentPair.first; |
| 1775 | ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentPair.second, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1776 | forRedeclarationInCurContext()); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1777 | ObjCProtocolDecl *PDecl |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1778 | = ObjCProtocolDecl::Create(Context, CurContext, Ident, |
Craig Topper | 0f723bb | 2015-10-22 05:00:01 +0000 | [diff] [blame] | 1779 | IdentPair.second, AtProtocolLoc, |
Douglas Gregor | 05a1f4d | 2012-01-01 22:06:18 +0000 | [diff] [blame] | 1780 | PrevDecl); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1781 | |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1782 | PushOnScopeChains(PDecl, TUScope); |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1783 | CheckObjCDeclScope(PDecl); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1784 | |
| 1785 | ProcessDeclAttributeList(TUScope, PDecl, attrList); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1786 | AddPragmaAttributes(TUScope, PDecl); |
| 1787 | |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1788 | if (PrevDecl) |
| 1789 | mergeDeclAttributes(PDecl, PrevDecl); |
| 1790 | |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1791 | DeclsInGroup.push_back(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1792 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1793 | |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1794 | return BuildDeclaratorGroup(DeclsInGroup); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1797 | Decl *Sema::ActOnStartCategoryInterface( |
| 1798 | SourceLocation AtInterfaceLoc, IdentifierInfo *ClassName, |
| 1799 | SourceLocation ClassLoc, ObjCTypeParamList *typeParamList, |
| 1800 | IdentifierInfo *CategoryName, SourceLocation CategoryLoc, |
| 1801 | Decl *const *ProtoRefs, unsigned NumProtoRefs, |
| 1802 | const SourceLocation *ProtoLocs, SourceLocation EndProtoLoc, |
| 1803 | const ParsedAttributesView &AttrList) { |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1804 | ObjCCategoryDecl *CDecl; |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1805 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1806 | |
| 1807 | /// Check that class of this category is already completely declared. |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1808 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1809 | if (!IDecl |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1810 | || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1811 | diag::err_category_forward_interface, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1812 | CategoryName == nullptr)) { |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1813 | // Create an invalid ObjCCategoryDecl to serve as context for |
| 1814 | // the enclosing method declarations. We mark the decl invalid |
| 1815 | // to make it clear that this isn't a valid AST. |
| 1816 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1817 | ClassLoc, CategoryLoc, CategoryName, |
| 1818 | IDecl, typeParamList); |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1819 | CDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | b15def2 | 2012-03-12 18:34:26 +0000 | [diff] [blame] | 1820 | CurContext->addDecl(CDecl); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1821 | |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1822 | if (!IDecl) |
| 1823 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1824 | return ActOnObjCContainerStartDefinition(CDecl); |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1827 | if (!CategoryName && IDecl->getImplementation()) { |
| 1828 | Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1829 | Diag(IDecl->getImplementation()->getLocation(), |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1830 | diag::note_implementation_declared); |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1833 | if (CategoryName) { |
| 1834 | /// Check for duplicate interface declaration for this category |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1835 | if (ObjCCategoryDecl *Previous |
| 1836 | = IDecl->FindCategoryDeclaration(CategoryName)) { |
| 1837 | // Class extensions can be declared multiple times, categories cannot. |
| 1838 | Diag(CategoryLoc, diag::warn_dup_category_def) |
| 1839 | << ClassName << CategoryName; |
| 1840 | Diag(Previous->getLocation(), diag::note_previous_definition); |
Chris Lattner | 9018ca8 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 1841 | } |
| 1842 | } |
Chris Lattner | 9018ca8 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 1843 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1844 | // If we have a type parameter list, check it. |
| 1845 | if (typeParamList) { |
| 1846 | if (auto prevTypeParamList = IDecl->getTypeParamList()) { |
| 1847 | if (checkTypeParamListConsistency(*this, prevTypeParamList, typeParamList, |
| 1848 | CategoryName |
| 1849 | ? TypeParamListContext::Category |
| 1850 | : TypeParamListContext::Extension)) |
| 1851 | typeParamList = nullptr; |
| 1852 | } else { |
| 1853 | Diag(typeParamList->getLAngleLoc(), |
| 1854 | diag::err_objc_parameterized_category_nonclass) |
| 1855 | << (CategoryName != nullptr) |
| 1856 | << ClassName |
| 1857 | << typeParamList->getSourceRange(); |
| 1858 | |
| 1859 | typeParamList = nullptr; |
| 1860 | } |
| 1861 | } |
| 1862 | |
Argyrios Kyrtzidis | 3a5094b | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 1863 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1864 | ClassLoc, CategoryLoc, CategoryName, IDecl, |
| 1865 | typeParamList); |
Argyrios Kyrtzidis | 3a5094b | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 1866 | // FIXME: PushOnScopeChains? |
| 1867 | CurContext->addDecl(CDecl); |
| 1868 | |
Alex Lorenz | a9c966d | 2018-02-23 23:49:43 +0000 | [diff] [blame] | 1869 | // Process the attributes before looking at protocols to ensure that the |
| 1870 | // availability attribute is attached to the category to provide availability |
| 1871 | // checking for protocol uses. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1872 | ProcessDeclAttributeList(TUScope, CDecl, AttrList); |
Alex Lorenz | a9c966d | 2018-02-23 23:49:43 +0000 | [diff] [blame] | 1873 | AddPragmaAttributes(TUScope, CDecl); |
| 1874 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1875 | if (NumProtoRefs) { |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1876 | diagnoseUseOfProtocols(*this, CDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
| 1877 | NumProtoRefs, ProtoLocs); |
| 1878 | CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 1879 | ProtoLocs, Context); |
Fariborz Jahanian | 092cd6e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 1880 | // Protocols in the class extension belong to the class. |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1881 | if (CDecl->IsClassExtension()) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1882 | IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs, |
| 1883 | NumProtoRefs, Context); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1884 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1885 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1886 | CheckObjCDeclScope(CDecl); |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1887 | return ActOnObjCContainerStartDefinition(CDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | /// ActOnStartCategoryImplementation - Perform semantic checks on the |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1891 | /// category implementation declaration and build an ObjCCategoryImplDecl |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1892 | /// object. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1893 | Decl *Sema::ActOnStartCategoryImplementation( |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1894 | SourceLocation AtCatImplLoc, |
| 1895 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
Erik Pilkington | c5a0583 | 2019-04-11 17:55:30 +0000 | [diff] [blame] | 1896 | IdentifierInfo *CatName, SourceLocation CatLoc, |
| 1897 | const ParsedAttributesView &Attrs) { |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1898 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1899 | ObjCCategoryDecl *CatIDecl = nullptr; |
Argyrios Kyrtzidis | 4af2cb3 | 2012-03-02 19:14:29 +0000 | [diff] [blame] | 1900 | if (IDecl && IDecl->hasDefinition()) { |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1901 | CatIDecl = IDecl->FindCategoryDeclaration(CatName); |
| 1902 | if (!CatIDecl) { |
| 1903 | // Category @implementation with no corresponding @interface. |
| 1904 | // Create and install one. |
Argyrios Kyrtzidis | 41fc05c | 2011-11-23 20:27:26 +0000 | [diff] [blame] | 1905 | CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc, |
| 1906 | ClassLoc, CatLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1907 | CatName, IDecl, |
| 1908 | /*typeParamList=*/nullptr); |
Argyrios Kyrtzidis | 41fc05c | 2011-11-23 20:27:26 +0000 | [diff] [blame] | 1909 | CatIDecl->setImplicit(); |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1910 | } |
| 1911 | } |
| 1912 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1913 | ObjCCategoryImplDecl *CDecl = |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 1914 | ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl, |
Argyrios Kyrtzidis | 4996f5f | 2011-12-09 00:31:40 +0000 | [diff] [blame] | 1915 | ClassLoc, AtCatImplLoc, CatLoc); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1916 | /// Check that class of this category is already completely declared. |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1917 | if (!IDecl) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1918 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 1919 | CDecl->setInvalidDecl(); |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1920 | } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
| 1921 | diag::err_undef_interface)) { |
| 1922 | CDecl->setInvalidDecl(); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 1923 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1924 | |
Erik Pilkington | c5a0583 | 2019-04-11 17:55:30 +0000 | [diff] [blame] | 1925 | ProcessDeclAttributeList(TUScope, CDecl, Attrs); |
| 1926 | AddPragmaAttributes(TUScope, CDecl); |
| 1927 | |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1928 | // FIXME: PushOnScopeChains? |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1929 | CurContext->addDecl(CDecl); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1930 | |
Douglas Gregor | 24ae22c | 2016-04-01 23:23:52 +0000 | [diff] [blame] | 1931 | // If the interface has the objc_runtime_visible attribute, we |
| 1932 | // cannot implement a category for it. |
| 1933 | if (IDecl && IDecl->hasAttr<ObjCRuntimeVisibleAttr>()) { |
| 1934 | Diag(ClassLoc, diag::err_objc_runtime_visible_category) |
| 1935 | << IDecl->getDeclName(); |
| 1936 | } |
| 1937 | |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1938 | /// Check that CatName, category name, is not used in another implementation. |
| 1939 | if (CatIDecl) { |
| 1940 | if (CatIDecl->getImplementation()) { |
| 1941 | Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName |
| 1942 | << CatName; |
| 1943 | Diag(CatIDecl->getImplementation()->getLocation(), |
| 1944 | diag::note_previous_definition); |
Argyrios Kyrtzidis | 0f6d5ca | 2013-05-30 18:53:21 +0000 | [diff] [blame] | 1945 | CDecl->setInvalidDecl(); |
Fariborz Jahanian | d33ab8c | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 1946 | } else { |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1947 | CatIDecl->setImplementation(CDecl); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1948 | // Warn on implementating category of deprecated class under |
Fariborz Jahanian | d33ab8c | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 1949 | // -Wdeprecated-implementations flag. |
Alex Lorenz | f81d97e | 2017-07-13 16:35:59 +0000 | [diff] [blame] | 1950 | DiagnoseObjCImplementedDeprecations(*this, CatIDecl, |
| 1951 | CDecl->getLocation()); |
Fariborz Jahanian | d33ab8c | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 1952 | } |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1953 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1954 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1955 | CheckObjCDeclScope(CDecl); |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1956 | return ActOnObjCContainerStartDefinition(CDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1959 | Decl *Sema::ActOnStartClassImplementation( |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1960 | SourceLocation AtClassImplLoc, |
| 1961 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1962 | IdentifierInfo *SuperClassname, |
Erik Pilkington | c5a0583 | 2019-04-11 17:55:30 +0000 | [diff] [blame] | 1963 | SourceLocation SuperClassLoc, |
| 1964 | const ParsedAttributesView &Attrs) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1965 | ObjCInterfaceDecl *IDecl = nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1966 | // Check for another declaration kind with the same name. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1967 | NamedDecl *PrevDecl |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 1968 | = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1969 | forRedeclarationInCurContext()); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1970 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1971 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1972 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1973 | } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) { |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 1974 | // FIXME: This will produce an error if the definition of the interface has |
| 1975 | // been imported from a module but is not visible. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 1976 | RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
| 1977 | diag::warn_undef_interface); |
Douglas Gregor | 40f7a00 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 1978 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1979 | // 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] | 1980 | // typos in the class name. |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 1981 | ObjCInterfaceValidatorCCC CCC{}; |
| 1982 | TypoCorrection Corrected = |
| 1983 | CorrectTypo(DeclarationNameInfo(ClassName, ClassLoc), |
| 1984 | LookupOrdinaryName, TUScope, nullptr, CCC, CTK_NonError); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1985 | if (Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
| 1986 | // Suggest the (potentially) correct interface name. Don't provide a |
| 1987 | // code-modification hint or use the typo name for recovery, because |
| 1988 | // this is just a warning. The program may actually be correct. |
| 1989 | diagnoseTypo(Corrected, |
| 1990 | PDiag(diag::warn_undef_interface_suggest) << ClassName, |
| 1991 | /*ErrorRecovery*/false); |
Douglas Gregor | 40f7a00 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 1992 | } else { |
| 1993 | Diag(ClassLoc, diag::warn_undef_interface) << ClassName; |
| 1994 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1995 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1996 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1997 | // Check that super class name is valid class name |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1998 | ObjCInterfaceDecl *SDecl = nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1999 | if (SuperClassname) { |
| 2000 | // Check if a different kind of symbol declared in this scope. |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2001 | PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc, |
| 2002 | LookupOrdinaryName); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2003 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2004 | Diag(SuperClassLoc, diag::err_redefinition_different_kind) |
| 2005 | << SuperClassname; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2006 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2007 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2008 | SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Argyrios Kyrtzidis | 3b60cff | 2012-03-13 01:09:36 +0000 | [diff] [blame] | 2009 | if (SDecl && !SDecl->hasDefinition()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2010 | SDecl = nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2011 | if (!SDecl) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2012 | Diag(SuperClassLoc, diag::err_undef_superclass) |
| 2013 | << SuperClassname << ClassName; |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 2014 | else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2015 | // This implementation and its interface do not have the same |
| 2016 | // super class. |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2017 | Diag(SuperClassLoc, diag::err_conflicting_super_class) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2018 | << SDecl->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2019 | Diag(SDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2020 | } |
| 2021 | } |
| 2022 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2023 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2024 | if (!IDecl) { |
| 2025 | // Legacy case of @implementation with no corresponding @interface. |
| 2026 | // Build, chain & install the interface decl into the identifier. |
Daniel Dunbar | 73a73f5 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 2027 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2028 | // FIXME: Do we support attributes on the @implementation? If so we should |
| 2029 | // copy them over. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2030 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 2031 | ClassName, /*typeParamList=*/nullptr, |
| 2032 | /*PrevDecl=*/nullptr, ClassLoc, |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 2033 | true); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 2034 | AddPragmaAttributes(TUScope, IDecl); |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 2035 | IDecl->startDefinition(); |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 2036 | if (SDecl) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 2037 | IDecl->setSuperClass(Context.getTrivialTypeSourceInfo( |
| 2038 | Context.getObjCInterfaceType(SDecl), |
| 2039 | SuperClassLoc)); |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 2040 | IDecl->setEndOfDefinitionLoc(SuperClassLoc); |
| 2041 | } else { |
| 2042 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
| 2043 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2044 | |
Douglas Gregor | ac345a3 | 2009-04-24 00:16:12 +0000 | [diff] [blame] | 2045 | PushOnScopeChains(IDecl, TUScope); |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2046 | } else { |
| 2047 | // Mark the interface as being completed, even if it was just as |
| 2048 | // @class ....; |
| 2049 | // declaration; the user cannot reopen it. |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 2050 | if (!IDecl->hasDefinition()) |
| 2051 | IDecl->startDefinition(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2052 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2053 | |
| 2054 | ObjCImplementationDecl* IMPDecl = |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 2055 | ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl, |
Argyrios Kyrtzidis | fac3162 | 2013-05-03 18:05:44 +0000 | [diff] [blame] | 2056 | ClassLoc, AtClassImplLoc, SuperClassLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2057 | |
Erik Pilkington | c5a0583 | 2019-04-11 17:55:30 +0000 | [diff] [blame] | 2058 | ProcessDeclAttributeList(TUScope, IMPDecl, Attrs); |
| 2059 | AddPragmaAttributes(TUScope, IMPDecl); |
| 2060 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 2061 | if (CheckObjCDeclScope(IMPDecl)) |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 2062 | return ActOnObjCContainerStartDefinition(IMPDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2063 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2064 | // Check that there is no duplicate implementation of this class. |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2065 | if (IDecl->getImplementation()) { |
| 2066 | // FIXME: Don't leak everything! |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2067 | Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; |
Argyrios Kyrtzidis | 43cee935 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 2068 | Diag(IDecl->getImplementation()->getLocation(), |
| 2069 | diag::note_previous_definition); |
Argyrios Kyrtzidis | 0f6d5ca | 2013-05-30 18:53:21 +0000 | [diff] [blame] | 2070 | IMPDecl->setInvalidDecl(); |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2071 | } else { // add it to the list. |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2072 | IDecl->setImplementation(IMPDecl); |
Douglas Gregor | 79947a2 | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 2073 | PushOnScopeChains(IMPDecl, TUScope); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2074 | // Warn on implementating deprecated class under |
Fariborz Jahanian | d33ab8c | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 2075 | // -Wdeprecated-implementations flag. |
Alex Lorenz | f81d97e | 2017-07-13 16:35:59 +0000 | [diff] [blame] | 2076 | DiagnoseObjCImplementedDeprecations(*this, IDecl, IMPDecl->getLocation()); |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2077 | } |
Douglas Gregor | 24ae22c | 2016-04-01 23:23:52 +0000 | [diff] [blame] | 2078 | |
| 2079 | // If the superclass has the objc_runtime_visible attribute, we |
| 2080 | // cannot implement a subclass of it. |
| 2081 | if (IDecl->getSuperClass() && |
| 2082 | IDecl->getSuperClass()->hasAttr<ObjCRuntimeVisibleAttr>()) { |
| 2083 | Diag(ClassLoc, diag::err_objc_runtime_visible_subclass) |
| 2084 | << IDecl->getDeclName() |
| 2085 | << IDecl->getSuperClass()->getDeclName(); |
| 2086 | } |
| 2087 | |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 2088 | return ActOnObjCContainerStartDefinition(IMPDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
Argyrios Kyrtzidis | 2e85c5f | 2012-02-23 21:11:20 +0000 | [diff] [blame] | 2091 | Sema::DeclGroupPtrTy |
| 2092 | Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) { |
| 2093 | SmallVector<Decl *, 64> DeclsInGroup; |
| 2094 | DeclsInGroup.reserve(Decls.size() + 1); |
| 2095 | |
| 2096 | for (unsigned i = 0, e = Decls.size(); i != e; ++i) { |
| 2097 | Decl *Dcl = Decls[i]; |
| 2098 | if (!Dcl) |
| 2099 | continue; |
| 2100 | if (Dcl->getDeclContext()->isFileContext()) |
| 2101 | Dcl->setTopLevelDeclInObjCContainer(); |
| 2102 | DeclsInGroup.push_back(Dcl); |
| 2103 | } |
| 2104 | |
| 2105 | DeclsInGroup.push_back(ObjCImpDecl); |
| 2106 | |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2107 | return BuildDeclaratorGroup(DeclsInGroup); |
Argyrios Kyrtzidis | 2e85c5f | 2012-02-23 21:11:20 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2110 | void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, |
| 2111 | ObjCIvarDecl **ivars, unsigned numIvars, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2112 | SourceLocation RBrace) { |
| 2113 | assert(ImpDecl && "missing implementation decl"); |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2114 | ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2115 | if (!IDecl) |
| 2116 | return; |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2117 | /// Check case of non-existing \@interface decl. |
| 2118 | /// (legacy objective-c \@implementation decl without an \@interface decl). |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2119 | /// Add implementations's ivar to the synthesize class's ivar list. |
Steve Naroff | aac654a | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 2120 | if (IDecl->isImplicitInterfaceDecl()) { |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 2121 | IDecl->setEndOfDefinitionLoc(RBrace); |
Fariborz Jahanian | 20912d6 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 2122 | // Add ivar's to class's DeclContext. |
| 2123 | for (unsigned i = 0, e = numIvars; i != e; ++i) { |
Fariborz Jahanian | c0309cd | 2010-02-17 18:10:54 +0000 | [diff] [blame] | 2124 | ivars[i]->setLexicalDeclContext(ImpDecl); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 2125 | IDecl->makeDeclVisibleInContext(ivars[i]); |
Fariborz Jahanian | aef6622 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 2126 | ImpDecl->addDecl(ivars[i]); |
Fariborz Jahanian | 20912d6 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 2127 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2128 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2129 | return; |
| 2130 | } |
| 2131 | // If implementation has empty ivar list, just return. |
| 2132 | if (numIvars == 0) |
| 2133 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2134 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2135 | assert(ivars && "missing @implementation ivars"); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2136 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | 34e3cef | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 2137 | if (ImpDecl->getSuperClass()) |
| 2138 | Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use); |
| 2139 | for (unsigned i = 0; i < numIvars; i++) { |
| 2140 | ObjCIvarDecl* ImplIvar = ivars[i]; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2141 | if (const ObjCIvarDecl *ClsIvar = |
Fariborz Jahanian | 34e3cef | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 2142 | IDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2143 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
Fariborz Jahanian | 34e3cef | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 2144 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
| 2145 | continue; |
| 2146 | } |
Fariborz Jahanian | e23f26b | 2013-06-26 22:10:27 +0000 | [diff] [blame] | 2147 | // Check class extensions (unnamed categories) for duplicate ivars. |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 2148 | for (const auto *CDecl : IDecl->visible_extensions()) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2149 | if (const ObjCIvarDecl *ClsExtIvar = |
Fariborz Jahanian | e23f26b | 2013-06-26 22:10:27 +0000 | [diff] [blame] | 2150 | CDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2151 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
Fariborz Jahanian | e23f26b | 2013-06-26 22:10:27 +0000 | [diff] [blame] | 2152 | Diag(ClsExtIvar->getLocation(), diag::note_previous_definition); |
| 2153 | continue; |
| 2154 | } |
| 2155 | } |
Fariborz Jahanian | 34e3cef | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 2156 | // Instance ivar to Implementation's DeclContext. |
| 2157 | ImplIvar->setLexicalDeclContext(ImpDecl); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 2158 | IDecl->makeDeclVisibleInContext(ImplIvar); |
Fariborz Jahanian | 34e3cef | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 2159 | ImpDecl->addDecl(ImplIvar); |
| 2160 | } |
| 2161 | return; |
| 2162 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2163 | // Check interface's Ivar list against those in the implementation. |
| 2164 | // names and types must match. |
| 2165 | // |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2166 | unsigned j = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2167 | ObjCInterfaceDecl::ivar_iterator |
Chris Lattner | 061227a | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 2168 | IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); |
| 2169 | for (; numIvars > 0 && IVI != IVE; ++IVI) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2170 | ObjCIvarDecl* ImplIvar = ivars[j++]; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2171 | ObjCIvarDecl* ClsIvar = *IVI; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2172 | assert (ImplIvar && "missing implementation ivar"); |
| 2173 | assert (ClsIvar && "missing class ivar"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2174 | |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 2175 | // First, make sure the types match. |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2176 | if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2177 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2178 | << ImplIvar->getIdentifier() |
| 2179 | << ImplIvar->getType() << ClsIvar->getType(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2180 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2181 | } else if (ImplIvar->isBitField() && ClsIvar->isBitField() && |
| 2182 | ImplIvar->getBitWidthValue(Context) != |
| 2183 | ClsIvar->getBitWidthValue(Context)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2184 | Diag(ImplIvar->getBitWidth()->getBeginLoc(), |
| 2185 | diag::err_conflicting_ivar_bitwidth) |
| 2186 | << ImplIvar->getIdentifier(); |
| 2187 | Diag(ClsIvar->getBitWidth()->getBeginLoc(), |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2188 | diag::note_previous_definition); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2189 | } |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 2190 | // Make sure the names are identical. |
| 2191 | if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2192 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2193 | << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2194 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2195 | } |
| 2196 | --numIvars; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2197 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2198 | |
Chris Lattner | 0f29d98 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 2199 | if (numIvars > 0) |
Alp Toker | fff0674 | 2013-12-02 03:50:21 +0000 | [diff] [blame] | 2200 | Diag(ivars[j]->getLocation(), diag::err_inconsistent_ivar_count); |
Chris Lattner | 0f29d98 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 2201 | else if (IVI != IVE) |
Alp Toker | fff0674 | 2013-12-02 03:50:21 +0000 | [diff] [blame] | 2202 | Diag(IVI->getLocation(), diag::err_inconsistent_ivar_count); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
Ted Kremenek | f87decd | 2013-12-13 05:58:44 +0000 | [diff] [blame] | 2205 | static void WarnUndefinedMethod(Sema &S, SourceLocation ImpLoc, |
| 2206 | ObjCMethodDecl *method, |
| 2207 | bool &IncompleteImpl, |
Ted Kremenek | 2ccf19e | 2013-12-13 05:58:51 +0000 | [diff] [blame] | 2208 | unsigned DiagID, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2209 | NamedDecl *NeededFor = nullptr) { |
Fariborz Jahanian | 9fc39c4 | 2011-06-24 20:31:37 +0000 | [diff] [blame] | 2210 | // No point warning no definition of method which is 'unavailable'. |
Erik Pilkington | ecce5c9 | 2018-07-07 01:50:20 +0000 | [diff] [blame] | 2211 | if (method->getAvailability() == AR_Unavailable) |
Fariborz Jahanian | 9fc39c4 | 2011-06-24 20:31:37 +0000 | [diff] [blame] | 2212 | return; |
Erik Pilkington | ecce5c9 | 2018-07-07 01:50:20 +0000 | [diff] [blame] | 2213 | |
Ted Kremenek | 65d6357 | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 2214 | // FIXME: For now ignore 'IncompleteImpl'. |
| 2215 | // Previously we grouped all unimplemented methods under a single |
| 2216 | // warning, but some users strongly voiced that they would prefer |
| 2217 | // separate warnings. We will give that approach a try, as that |
| 2218 | // matches what we do with protocols. |
Ted Kremenek | 2ccf19e | 2013-12-13 05:58:51 +0000 | [diff] [blame] | 2219 | { |
| 2220 | const Sema::SemaDiagnosticBuilder &B = S.Diag(ImpLoc, DiagID); |
| 2221 | B << method; |
| 2222 | if (NeededFor) |
| 2223 | B << NeededFor; |
| 2224 | } |
Ted Kremenek | 65d6357 | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 2225 | |
| 2226 | // Issue a note to the original declaration. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2227 | SourceLocation MethodLoc = method->getBeginLoc(); |
Ted Kremenek | 65d6357 | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 2228 | if (MethodLoc.isValid()) |
Ted Kremenek | f87decd | 2013-12-13 05:58:44 +0000 | [diff] [blame] | 2229 | S.Diag(MethodLoc, diag::note_method_declared_at) << method; |
Steve Naroff | 15833ed | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2232 | /// Determines if type B can be substituted for type A. Returns true if we can |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2233 | /// guarantee that anything that the user will do to an object of type A can |
| 2234 | /// also be done to an object of type B. This is trivially true if the two |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2235 | /// types are the same, or if B is a subclass of A. It becomes more complex |
| 2236 | /// in cases where protocols are involved. |
| 2237 | /// |
| 2238 | /// Object types in Objective-C describe the minimum requirements for an |
| 2239 | /// object, rather than providing a complete description of a type. For |
| 2240 | /// example, if A is a subclass of B, then B* may refer to an instance of A. |
| 2241 | /// The principle of substitutability means that we may use an instance of A |
| 2242 | /// anywhere that we may use an instance of B - it will implement all of the |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2243 | /// ivars of B and all of the methods of B. |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2244 | /// |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2245 | /// This substitutability is important when type checking methods, because |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2246 | /// the implementation may have stricter type definitions than the interface. |
| 2247 | /// The interface specifies minimum requirements, but the implementation may |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2248 | /// have more accurate ones. For example, a method may privately accept |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2249 | /// instances of B, but only publish that it accepts instances of A. Any |
| 2250 | /// object passed to it will be type checked against B, and so will implicitly |
| 2251 | /// by a valid A*. Similarly, a method may return a subclass of the class that |
| 2252 | /// it is declared as returning. |
| 2253 | /// |
| 2254 | /// This is most important when considering subclassing. A method in a |
| 2255 | /// subclass must accept any object as an argument that its superclass's |
| 2256 | /// implementation accepts. It may, however, accept a more general type |
| 2257 | /// without breaking substitutability (i.e. you can still use the subclass |
| 2258 | /// anywhere that you can use the superclass, but not vice versa). The |
| 2259 | /// converse requirement applies to return types: the return type for a |
| 2260 | /// subclass method must be a valid object of the kind that the superclass |
| 2261 | /// advertises, but it may be specified more accurately. This avoids the need |
| 2262 | /// for explicit down-casting by callers. |
| 2263 | /// |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2264 | /// Note: This is a stricter requirement than for assignment. |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2265 | static bool isObjCTypeSubstitutable(ASTContext &Context, |
| 2266 | const ObjCObjectPointerType *A, |
| 2267 | const ObjCObjectPointerType *B, |
| 2268 | bool rejectId) { |
| 2269 | // Reject a protocol-unqualified id. |
| 2270 | if (rejectId && B->isObjCIdType()) return false; |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2271 | |
| 2272 | // If B is a qualified id, then A must also be a qualified id and it must |
| 2273 | // implement all of the protocols in B. It may not be a qualified class. |
| 2274 | // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a |
| 2275 | // stricter definition so it is not substitutable for id<A>. |
| 2276 | if (B->isObjCQualifiedIdType()) { |
| 2277 | return A->isObjCQualifiedIdType() && |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 2278 | Context.ObjCQualifiedIdTypesAreCompatible(A, B, false); |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | /* |
| 2282 | // id is a special type that bypasses type checking completely. We want a |
| 2283 | // warning when it is used in one place but not another. |
| 2284 | if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false; |
| 2285 | |
| 2286 | |
| 2287 | // If B is a qualified id, then A must also be a qualified id (which it isn't |
| 2288 | // if we've got this far) |
| 2289 | if (B->isObjCQualifiedIdType()) return false; |
| 2290 | */ |
| 2291 | |
| 2292 | // Now we know that A and B are (potentially-qualified) class types. The |
| 2293 | // normal rules for assignment apply. |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2294 | return Context.canAssignObjCInterfaces(A, B); |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2297 | static SourceRange getTypeRange(TypeSourceInfo *TSI) { |
| 2298 | return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange()); |
| 2299 | } |
| 2300 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2301 | /// Determine whether two set of Objective-C declaration qualifiers conflict. |
| 2302 | static bool objcModifiersConflict(Decl::ObjCDeclQualifier x, |
| 2303 | Decl::ObjCDeclQualifier y) { |
| 2304 | return (x & ~Decl::OBJC_TQ_CSNullability) != |
| 2305 | (y & ~Decl::OBJC_TQ_CSNullability); |
| 2306 | } |
| 2307 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2308 | static bool CheckMethodOverrideReturn(Sema &S, |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2309 | ObjCMethodDecl *MethodImpl, |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2310 | ObjCMethodDecl *MethodDecl, |
Fariborz Jahanian | 4ceec3f | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 2311 | bool IsProtocolMethodDecl, |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2312 | bool IsOverridingMode, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2313 | bool Warn) { |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2314 | if (IsProtocolMethodDecl && |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2315 | objcModifiersConflict(MethodDecl->getObjCDeclQualifier(), |
| 2316 | MethodImpl->getObjCDeclQualifier())) { |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2317 | if (Warn) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2318 | S.Diag(MethodImpl->getLocation(), |
| 2319 | (IsOverridingMode |
| 2320 | ? diag::warn_conflicting_overriding_ret_type_modifiers |
| 2321 | : diag::warn_conflicting_ret_type_modifiers)) |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2322 | << MethodImpl->getDeclName() |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 2323 | << MethodImpl->getReturnTypeSourceRange(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2324 | S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration) |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 2325 | << MethodDecl->getReturnTypeSourceRange(); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2326 | } |
| 2327 | else |
| 2328 | return false; |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2329 | } |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2330 | if (Warn && IsOverridingMode && |
| 2331 | !isa<ObjCImplementationDecl>(MethodImpl->getDeclContext()) && |
| 2332 | !S.Context.hasSameNullabilityTypeQualifier(MethodImpl->getReturnType(), |
| 2333 | MethodDecl->getReturnType(), |
| 2334 | false)) { |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 2335 | auto nullabilityMethodImpl = |
| 2336 | *MethodImpl->getReturnType()->getNullability(S.Context); |
| 2337 | auto nullabilityMethodDecl = |
| 2338 | *MethodDecl->getReturnType()->getNullability(S.Context); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2339 | S.Diag(MethodImpl->getLocation(), |
| 2340 | diag::warn_conflicting_nullability_attr_overriding_ret_types) |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 2341 | << DiagNullabilityKind( |
| 2342 | nullabilityMethodImpl, |
| 2343 | ((MethodImpl->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2344 | != 0)) |
| 2345 | << DiagNullabilityKind( |
| 2346 | nullabilityMethodDecl, |
| 2347 | ((MethodDecl->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2348 | != 0)); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2349 | S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration); |
| 2350 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2351 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2352 | if (S.Context.hasSameUnqualifiedType(MethodImpl->getReturnType(), |
| 2353 | MethodDecl->getReturnType())) |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2354 | return true; |
| 2355 | if (!Warn) |
| 2356 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2357 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2358 | unsigned DiagID = |
| 2359 | IsOverridingMode ? diag::warn_conflicting_overriding_ret_types |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2360 | : diag::warn_conflicting_ret_types; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2361 | |
| 2362 | // Mismatches between ObjC pointers go into a different warning |
| 2363 | // category, and sometimes they're even completely whitelisted. |
| 2364 | if (const ObjCObjectPointerType *ImplPtrTy = |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2365 | MethodImpl->getReturnType()->getAs<ObjCObjectPointerType>()) { |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2366 | if (const ObjCObjectPointerType *IfacePtrTy = |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2367 | MethodDecl->getReturnType()->getAs<ObjCObjectPointerType>()) { |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2368 | // Allow non-matching return types as long as they don't violate |
| 2369 | // the principle of substitutability. Specifically, we permit |
| 2370 | // return types that are subclasses of the declared return type, |
| 2371 | // or that are more-qualified versions of the declared type. |
| 2372 | if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false)) |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2373 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2374 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2375 | DiagID = |
| 2376 | IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types |
Craig Topper | 8f7f3ea | 2015-11-17 05:40:05 +0000 | [diff] [blame] | 2377 | : diag::warn_non_covariant_ret_types; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | S.Diag(MethodImpl->getLocation(), DiagID) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2382 | << MethodImpl->getDeclName() << MethodDecl->getReturnType() |
| 2383 | << MethodImpl->getReturnType() |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 2384 | << MethodImpl->getReturnTypeSourceRange(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2385 | S.Diag(MethodDecl->getLocation(), IsOverridingMode |
| 2386 | ? diag::note_previous_declaration |
| 2387 | : diag::note_previous_definition) |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 2388 | << MethodDecl->getReturnTypeSourceRange(); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2389 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2390 | } |
| 2391 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2392 | static bool CheckMethodOverrideParam(Sema &S, |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2393 | ObjCMethodDecl *MethodImpl, |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2394 | ObjCMethodDecl *MethodDecl, |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2395 | ParmVarDecl *ImplVar, |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2396 | ParmVarDecl *IfaceVar, |
Fariborz Jahanian | 4ceec3f | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 2397 | bool IsProtocolMethodDecl, |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2398 | bool IsOverridingMode, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2399 | bool Warn) { |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2400 | if (IsProtocolMethodDecl && |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2401 | objcModifiersConflict(ImplVar->getObjCDeclQualifier(), |
| 2402 | IfaceVar->getObjCDeclQualifier())) { |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2403 | if (Warn) { |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2404 | if (IsOverridingMode) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2405 | S.Diag(ImplVar->getLocation(), |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2406 | diag::warn_conflicting_overriding_param_modifiers) |
| 2407 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
| 2408 | << MethodImpl->getDeclName(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2409 | else S.Diag(ImplVar->getLocation(), |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2410 | diag::warn_conflicting_param_modifiers) |
| 2411 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2412 | << MethodImpl->getDeclName(); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2413 | S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2414 | << getTypeRange(IfaceVar->getTypeSourceInfo()); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2415 | } |
| 2416 | else |
| 2417 | return false; |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2418 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2419 | |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2420 | QualType ImplTy = ImplVar->getType(); |
| 2421 | QualType IfaceTy = IfaceVar->getType(); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2422 | if (Warn && IsOverridingMode && |
| 2423 | !isa<ObjCImplementationDecl>(MethodImpl->getDeclContext()) && |
| 2424 | !S.Context.hasSameNullabilityTypeQualifier(ImplTy, IfaceTy, true)) { |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2425 | S.Diag(ImplVar->getLocation(), |
| 2426 | diag::warn_conflicting_nullability_attr_overriding_param_types) |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 2427 | << DiagNullabilityKind( |
| 2428 | *ImplTy->getNullability(S.Context), |
| 2429 | ((ImplVar->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2430 | != 0)) |
| 2431 | << DiagNullabilityKind( |
| 2432 | *IfaceTy->getNullability(S.Context), |
| 2433 | ((IfaceVar->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2434 | != 0)); |
| 2435 | S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2436 | } |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2437 | if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy)) |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2438 | return true; |
Manman Ren | c5705ba | 2016-09-13 17:41:05 +0000 | [diff] [blame] | 2439 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2440 | if (!Warn) |
| 2441 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2442 | unsigned DiagID = |
| 2443 | IsOverridingMode ? diag::warn_conflicting_overriding_param_types |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2444 | : diag::warn_conflicting_param_types; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2445 | |
| 2446 | // Mismatches between ObjC pointers go into a different warning |
| 2447 | // category, and sometimes they're even completely whitelisted. |
| 2448 | if (const ObjCObjectPointerType *ImplPtrTy = |
| 2449 | ImplTy->getAs<ObjCObjectPointerType>()) { |
| 2450 | if (const ObjCObjectPointerType *IfacePtrTy = |
| 2451 | IfaceTy->getAs<ObjCObjectPointerType>()) { |
| 2452 | // Allow non-matching argument types as long as they don't |
| 2453 | // violate the principle of substitutability. Specifically, the |
| 2454 | // implementation must accept any objects that the superclass |
| 2455 | // accepts, however it may also accept others. |
| 2456 | if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true)) |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2457 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2458 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2459 | DiagID = |
| 2460 | IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types |
Craig Topper | 8f7f3ea | 2015-11-17 05:40:05 +0000 | [diff] [blame] | 2461 | : diag::warn_non_contravariant_param_types; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2462 | } |
| 2463 | } |
| 2464 | |
| 2465 | S.Diag(ImplVar->getLocation(), DiagID) |
| 2466 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2467 | << MethodImpl->getDeclName() << IfaceTy << ImplTy; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2468 | S.Diag(IfaceVar->getLocation(), |
| 2469 | (IsOverridingMode ? diag::note_previous_declaration |
Craig Topper | 8f7f3ea | 2015-11-17 05:40:05 +0000 | [diff] [blame] | 2470 | : diag::note_previous_definition)) |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2471 | << getTypeRange(IfaceVar->getTypeSourceInfo()); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2472 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2473 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2474 | |
| 2475 | /// In ARC, check whether the conventional meanings of the two methods |
| 2476 | /// match. If they don't, it's a hard error. |
| 2477 | static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl, |
| 2478 | ObjCMethodDecl *decl) { |
| 2479 | ObjCMethodFamily implFamily = impl->getMethodFamily(); |
| 2480 | ObjCMethodFamily declFamily = decl->getMethodFamily(); |
| 2481 | if (implFamily == declFamily) return false; |
| 2482 | |
| 2483 | // Since conventions are sorted by selector, the only possibility is |
| 2484 | // that the types differ enough to cause one selector or the other |
| 2485 | // to fall out of the family. |
| 2486 | assert(implFamily == OMF_None || declFamily == OMF_None); |
| 2487 | |
| 2488 | // No further diagnostics required on invalid declarations. |
| 2489 | if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true; |
| 2490 | |
| 2491 | const ObjCMethodDecl *unmatched = impl; |
| 2492 | ObjCMethodFamily family = declFamily; |
| 2493 | unsigned errorID = diag::err_arc_lost_method_convention; |
| 2494 | unsigned noteID = diag::note_arc_lost_method_convention; |
| 2495 | if (declFamily == OMF_None) { |
| 2496 | unmatched = decl; |
| 2497 | family = implFamily; |
| 2498 | errorID = diag::err_arc_gained_method_convention; |
| 2499 | noteID = diag::note_arc_gained_method_convention; |
| 2500 | } |
| 2501 | |
| 2502 | // Indexes into a %select clause in the diagnostic. |
| 2503 | enum FamilySelector { |
| 2504 | F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new |
| 2505 | }; |
| 2506 | FamilySelector familySelector = FamilySelector(); |
| 2507 | |
| 2508 | switch (family) { |
| 2509 | case OMF_None: llvm_unreachable("logic error, no method convention"); |
| 2510 | case OMF_retain: |
| 2511 | case OMF_release: |
| 2512 | case OMF_autorelease: |
| 2513 | case OMF_dealloc: |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 2514 | case OMF_finalize: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2515 | case OMF_retainCount: |
| 2516 | case OMF_self: |
Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 2517 | case OMF_initialize: |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 2518 | case OMF_performSelector: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2519 | // Mismatches for these methods don't change ownership |
| 2520 | // conventions, so we don't care. |
| 2521 | return false; |
| 2522 | |
| 2523 | case OMF_init: familySelector = F_init; break; |
| 2524 | case OMF_alloc: familySelector = F_alloc; break; |
| 2525 | case OMF_copy: familySelector = F_copy; break; |
| 2526 | case OMF_mutableCopy: familySelector = F_mutableCopy; break; |
| 2527 | case OMF_new: familySelector = F_new; break; |
| 2528 | } |
| 2529 | |
| 2530 | enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn }; |
| 2531 | ReasonSelector reasonSelector; |
| 2532 | |
| 2533 | // The only reason these methods don't fall within their families is |
| 2534 | // due to unusual result types. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2535 | if (unmatched->getReturnType()->isObjCObjectPointerType()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2536 | reasonSelector = R_UnrelatedReturn; |
| 2537 | } else { |
| 2538 | reasonSelector = R_NonObjectReturn; |
| 2539 | } |
| 2540 | |
Joerg Sonnenberger | ffc6d49 | 2013-06-26 21:31:47 +0000 | [diff] [blame] | 2541 | S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector); |
| 2542 | S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2543 | |
| 2544 | return true; |
| 2545 | } |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2546 | |
Fariborz Jahanian | 7988d7d | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 2547 | void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2548 | ObjCMethodDecl *MethodDecl, |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2549 | bool IsProtocolMethodDecl) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2550 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2551 | checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl)) |
| 2552 | return; |
| 2553 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2554 | CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
| 2555 | IsProtocolMethodDecl, false, |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2556 | true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2557 | |
Chris Lattner | 67f35b0 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 2558 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2559 | IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(), |
| 2560 | EF = MethodDecl->param_end(); |
| 2561 | IM != EM && IF != EF; ++IM, ++IF) { |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2562 | CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF, |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2563 | IsProtocolMethodDecl, false, true); |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2564 | } |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2565 | |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2566 | if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2567 | Diag(ImpMethodDecl->getLocation(), |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2568 | diag::warn_conflicting_variadic); |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2569 | Diag(MethodDecl->getLocation(), diag::note_previous_declaration); |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2570 | } |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2573 | void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method, |
| 2574 | ObjCMethodDecl *Overridden, |
| 2575 | bool IsProtocolMethodDecl) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2576 | |
| 2577 | CheckMethodOverrideReturn(*this, Method, Overridden, |
| 2578 | IsProtocolMethodDecl, true, |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2579 | true); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2580 | |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2581 | for (ObjCMethodDecl::param_iterator IM = Method->param_begin(), |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2582 | IF = Overridden->param_begin(), EM = Method->param_end(), |
| 2583 | EF = Overridden->param_end(); |
| 2584 | IM != EM && IF != EF; ++IM, ++IF) { |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2585 | CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF, |
| 2586 | IsProtocolMethodDecl, true, true); |
| 2587 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2588 | |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2589 | if (Method->isVariadic() != Overridden->isVariadic()) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2590 | Diag(Method->getLocation(), |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2591 | diag::warn_conflicting_overriding_variadic); |
| 2592 | Diag(Overridden->getLocation(), diag::note_previous_declaration); |
| 2593 | } |
| 2594 | } |
| 2595 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2596 | /// WarnExactTypedMethods - This routine issues a warning if method |
| 2597 | /// implementation declaration matches exactly that of its declaration. |
| 2598 | void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
| 2599 | ObjCMethodDecl *MethodDecl, |
| 2600 | bool IsProtocolMethodDecl) { |
| 2601 | // don't issue warning when protocol method is optional because primary |
| 2602 | // class is not required to implement it and it is safe for protocol |
| 2603 | // to implement it. |
| 2604 | if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional) |
| 2605 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2606 | // don't issue warning when primary class's method is |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2607 | // depecated/unavailable. |
| 2608 | if (MethodDecl->hasAttr<UnavailableAttr>() || |
| 2609 | MethodDecl->hasAttr<DeprecatedAttr>()) |
| 2610 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2611 | |
| 2612 | bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2613 | IsProtocolMethodDecl, false, false); |
| 2614 | if (match) |
| 2615 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2616 | IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(), |
| 2617 | EF = MethodDecl->param_end(); |
| 2618 | IM != EM && IF != EF; ++IM, ++IF) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2619 | match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2620 | *IM, *IF, |
| 2621 | IsProtocolMethodDecl, false, false); |
| 2622 | if (!match) |
| 2623 | break; |
| 2624 | } |
| 2625 | if (match) |
| 2626 | match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic()); |
David Chisnall | 9291851 | 2011-08-08 17:32:19 +0000 | [diff] [blame] | 2627 | if (match) |
| 2628 | match = !(MethodDecl->isClassMethod() && |
| 2629 | MethodDecl->getSelector() == GetNullarySelector("load", Context)); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2630 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2631 | if (match) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2632 | Diag(ImpMethodDecl->getLocation(), |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2633 | diag::warn_category_method_impl_match); |
Ted Kremenek | 59b10db | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 2634 | Diag(MethodDecl->getLocation(), diag::note_method_declared_at) |
| 2635 | << MethodDecl->getDeclName(); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2636 | } |
| 2637 | } |
| 2638 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2639 | /// FIXME: Type hierarchies in Objective-C can be deep. We could most likely |
| 2640 | /// improve the efficiency of selector lookups and type checking by associating |
| 2641 | /// with each protocol / interface / category the flattened instance tables. If |
| 2642 | /// we used an immutable set to keep the table then it wouldn't add significant |
| 2643 | /// memory cost and it would be handy for lookups. |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 2644 | |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2645 | typedef llvm::DenseSet<IdentifierInfo*> ProtocolNameSet; |
Ahmed Charles | af94d56 | 2014-03-09 11:34:25 +0000 | [diff] [blame] | 2646 | typedef std::unique_ptr<ProtocolNameSet> LazyProtocolNameSet; |
Ted Kremenek | 760a2ac | 2014-03-05 23:18:22 +0000 | [diff] [blame] | 2647 | |
| 2648 | static void findProtocolsWithExplicitImpls(const ObjCProtocolDecl *PDecl, |
| 2649 | ProtocolNameSet &PNS) { |
| 2650 | if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) |
| 2651 | PNS.insert(PDecl->getIdentifier()); |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 2652 | for (const auto *PI : PDecl->protocols()) |
| 2653 | findProtocolsWithExplicitImpls(PI, PNS); |
Ted Kremenek | 760a2ac | 2014-03-05 23:18:22 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2656 | /// Recursively populates a set with all conformed protocols in a class |
| 2657 | /// hierarchy that have the 'objc_protocol_requires_explicit_implementation' |
| 2658 | /// attribute. |
| 2659 | static void findProtocolsWithExplicitImpls(const ObjCInterfaceDecl *Super, |
| 2660 | ProtocolNameSet &PNS) { |
| 2661 | if (!Super) |
| 2662 | return; |
| 2663 | |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2664 | for (const auto *I : Super->all_referenced_protocols()) |
| 2665 | findProtocolsWithExplicitImpls(I, PNS); |
Ted Kremenek | 760a2ac | 2014-03-05 23:18:22 +0000 | [diff] [blame] | 2666 | |
| 2667 | findProtocolsWithExplicitImpls(Super->getSuperClass(), PNS); |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
Steve Naroff | a3699224 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 2670 | /// CheckProtocolMethodDefs - This routine checks unimplemented methods |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2671 | /// Declared in protocol, and those referenced by it. |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2672 | static void CheckProtocolMethodDefs(Sema &S, |
| 2673 | SourceLocation ImpLoc, |
| 2674 | ObjCProtocolDecl *PDecl, |
| 2675 | bool& IncompleteImpl, |
| 2676 | const Sema::SelectorSet &InsMap, |
| 2677 | const Sema::SelectorSet &ClsMap, |
Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 2678 | ObjCContainerDecl *CDecl, |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2679 | LazyProtocolNameSet &ProtocolsExplictImpl) { |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2680 | ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2681 | ObjCInterfaceDecl *IDecl = C ? C->getClassInterface() |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2682 | : dyn_cast<ObjCInterfaceDecl>(CDecl); |
Fariborz Jahanian | 2e8074b | 2010-03-27 21:10:05 +0000 | [diff] [blame] | 2683 | assert (IDecl && "CheckProtocolMethodDefs - IDecl is null"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2684 | |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 2685 | ObjCInterfaceDecl *Super = IDecl->getSuperClass(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2686 | ObjCInterfaceDecl *NSIDecl = nullptr; |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2687 | |
| 2688 | // If this protocol is marked 'objc_protocol_requires_explicit_implementation' |
| 2689 | // then we should check if any class in the super class hierarchy also |
| 2690 | // conforms to this protocol, either directly or via protocol inheritance. |
| 2691 | // If so, we can skip checking this protocol completely because we |
| 2692 | // know that a parent class already satisfies this protocol. |
| 2693 | // |
| 2694 | // Note: we could generalize this logic for all protocols, and merely |
| 2695 | // add the limit on looking at the super class chain for just |
| 2696 | // specially marked protocols. This may be a good optimization. This |
| 2697 | // change is restricted to 'objc_protocol_requires_explicit_implementation' |
| 2698 | // protocols for now for controlled evaluation. |
| 2699 | if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) { |
Ahmed Charles | af94d56 | 2014-03-09 11:34:25 +0000 | [diff] [blame] | 2700 | if (!ProtocolsExplictImpl) { |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2701 | ProtocolsExplictImpl.reset(new ProtocolNameSet); |
| 2702 | findProtocolsWithExplicitImpls(Super, *ProtocolsExplictImpl); |
| 2703 | } |
| 2704 | if (ProtocolsExplictImpl->find(PDecl->getIdentifier()) != |
| 2705 | ProtocolsExplictImpl->end()) |
| 2706 | return; |
| 2707 | |
| 2708 | // If no super class conforms to the protocol, we should not search |
| 2709 | // for methods in the super class to implicitly satisfy the protocol. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2710 | Super = nullptr; |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2713 | if (S.getLangOpts().ObjCRuntime.isNeXTFamily()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | // check to see if class implements forwardInvocation method and objects |
| 2715 | // of this class are derived from 'NSProxy' so that to forward requests |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2716 | // from one object to another. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2717 | // Under such conditions, which means that every method possible is |
| 2718 | // implemented in the class, we should not issue "Method definition not |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2719 | // found" warnings. |
| 2720 | // FIXME: Use a general GetUnarySelector method for this. |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2721 | IdentifierInfo* II = &S.Context.Idents.get("forwardInvocation"); |
| 2722 | Selector fISelector = S.Context.Selectors.getSelector(1, &II); |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2723 | if (InsMap.count(fISelector)) |
| 2724 | // Is IDecl derived from 'NSProxy'? If so, no instance methods |
| 2725 | // need be implemented in the implementation. |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2726 | NSIDecl = IDecl->lookupInheritedClass(&S.Context.Idents.get("NSProxy")); |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2727 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2728 | |
Fariborz Jahanian | c41cf05 | 2013-01-07 19:21:03 +0000 | [diff] [blame] | 2729 | // If this is a forward protocol declaration, get its definition. |
| 2730 | if (!PDecl->isThisDeclarationADefinition() && |
| 2731 | PDecl->getDefinition()) |
| 2732 | PDecl = PDecl->getDefinition(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2733 | |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 2734 | // If a method lookup fails locally we still need to look and see if |
| 2735 | // the method was implemented by a base class or an inherited |
| 2736 | // protocol. This lookup is slow, but occurs rarely in correct code |
| 2737 | // and otherwise would terminate in a warning. |
| 2738 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2739 | // check unimplemented instance methods. |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2740 | if (!NSIDecl) |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2741 | for (auto *method : PDecl->instance_methods()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2742 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2743 | !method->isPropertyAccessor() && |
| 2744 | !InsMap.count(method->getSelector()) && |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2745 | (!Super || !Super->lookupMethod(method->getSelector(), |
| 2746 | true /* instance */, |
| 2747 | false /* shallowCategory */, |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2748 | true /* followsSuper */, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2749 | nullptr /* category */))) { |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2750 | // If a method is not implemented in the category implementation but |
| 2751 | // has been declared in its primary class, superclass, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2752 | // or in one of their protocols, no need to issue the warning. |
| 2753 | // This is because method will be implemented in the primary class |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2754 | // or one of its super class implementation. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2755 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 2756 | // Ugly, but necessary. Method declared in protocol might have |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2757 | // have been synthesized due to a property declared in the class which |
| 2758 | // uses the protocol. |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2759 | if (ObjCMethodDecl *MethodInClass = |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2760 | IDecl->lookupMethod(method->getSelector(), |
| 2761 | true /* instance */, |
| 2762 | true /* shallowCategoryLookup */, |
| 2763 | false /* followSuper */)) |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2764 | if (C || MethodInClass->isPropertyAccessor()) |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2765 | continue; |
| 2766 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 2767 | if (!S.Diags.isIgnored(DIAG, ImpLoc)) { |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2768 | WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, |
Ted Kremenek | 2ccf19e | 2013-12-13 05:58:51 +0000 | [diff] [blame] | 2769 | PDecl); |
Fariborz Jahanian | 97752f7 | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 2770 | } |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2771 | } |
| 2772 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2773 | // check unimplemented class methods |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2774 | for (auto *method : PDecl->class_methods()) { |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 2775 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
| 2776 | !ClsMap.count(method->getSelector()) && |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2777 | (!Super || !Super->lookupMethod(method->getSelector(), |
| 2778 | false /* class method */, |
| 2779 | false /* shallowCategoryLookup */, |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2780 | true /* followSuper */, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2781 | nullptr /* category */))) { |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2782 | // See above comment for instance method lookups. |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2783 | if (C && IDecl->lookupMethod(method->getSelector(), |
| 2784 | false /* class */, |
| 2785 | true /* shallowCategoryLookup */, |
| 2786 | false /* followSuper */)) |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2787 | continue; |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2788 | |
Fariborz Jahanian | c1fb862 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 2789 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 2790 | if (!S.Diags.isIgnored(DIAG, ImpLoc)) { |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2791 | WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, PDecl); |
Fariborz Jahanian | c1fb862 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 2792 | } |
Fariborz Jahanian | 97752f7 | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 2793 | } |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 2794 | } |
Chris Lattner | 390d39a | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 2795 | // Check on this protocols's referenced protocols, recursively. |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 2796 | for (auto *PI : PDecl->protocols()) |
| 2797 | CheckProtocolMethodDefs(S, ImpLoc, PI, IncompleteImpl, InsMap, ClsMap, |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2798 | CDecl, ProtocolsExplictImpl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
Fariborz Jahanian | f9ae68a | 2011-07-16 00:08:33 +0000 | [diff] [blame] | 2801 | /// MatchAllMethodDeclarations - Check methods declared in interface |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2802 | /// or protocol against those declared in their implementations. |
| 2803 | /// |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2804 | void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap, |
| 2805 | const SelectorSet &ClsMap, |
| 2806 | SelectorSet &InsMapSeen, |
| 2807 | SelectorSet &ClsMapSeen, |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2808 | ObjCImplDecl* IMPDecl, |
| 2809 | ObjCContainerDecl* CDecl, |
| 2810 | bool &IncompleteImpl, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2811 | bool ImmediateClass, |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2812 | bool WarnCategoryMethodImpl) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2813 | // Check and see if instance methods in class interface have been |
| 2814 | // implemented in the implementation class. If so, their types match. |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2815 | for (auto *I : CDecl->instance_methods()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 2816 | if (!InsMapSeen.insert(I->getSelector()).second) |
Benjamin Kramer | 9f8e2d7 | 2013-10-14 15:16:10 +0000 | [diff] [blame] | 2817 | continue; |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2818 | if (!I->isPropertyAccessor() && |
| 2819 | !InsMap.count(I->getSelector())) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2820 | if (ImmediateClass) |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2821 | WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl, |
Ted Kremenek | 65d6357 | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 2822 | diag::warn_undef_method_impl); |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2823 | continue; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2824 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2825 | ObjCMethodDecl *ImpMethodDecl = |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2826 | IMPDecl->getInstanceMethod(I->getSelector()); |
Manman Ren | a58f92f | 2016-10-11 21:18:20 +0000 | [diff] [blame] | 2827 | assert(CDecl->getInstanceMethod(I->getSelector(), true/*AllowHidden*/) && |
Argyrios Kyrtzidis | 342e08f | 2011-08-30 19:43:21 +0000 | [diff] [blame] | 2828 | "Expected to find the method through lookup as well"); |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2829 | // ImpMethodDecl may be null as in a @dynamic property. |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2830 | if (ImpMethodDecl) { |
Adrian Prantl | 2073dd2 | 2019-11-04 14:28:14 -0800 | [diff] [blame] | 2831 | // Skip property accessor function stubs. |
| 2832 | if (ImpMethodDecl->isSynthesizedAccessorStub()) |
| 2833 | continue; |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2834 | if (!WarnCategoryMethodImpl) |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2835 | WarnConflictingTypedMethods(ImpMethodDecl, I, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2836 | isa<ObjCProtocolDecl>(CDecl)); |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2837 | else if (!I->isPropertyAccessor()) |
| 2838 | WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl)); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2839 | } |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2840 | } |
| 2841 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2842 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2843 | // Check and see if class methods in class interface have been |
| 2844 | // implemented in the implementation class. If so, their types match. |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2845 | for (auto *I : CDecl->class_methods()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 2846 | if (!ClsMapSeen.insert(I->getSelector()).second) |
Benjamin Kramer | 9f8e2d7 | 2013-10-14 15:16:10 +0000 | [diff] [blame] | 2847 | continue; |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2848 | if (!I->isPropertyAccessor() && |
| 2849 | !ClsMap.count(I->getSelector())) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2850 | if (ImmediateClass) |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2851 | WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl, |
Ted Kremenek | 65d6357 | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 2852 | diag::warn_undef_method_impl); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2853 | } else { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2854 | ObjCMethodDecl *ImpMethodDecl = |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2855 | IMPDecl->getClassMethod(I->getSelector()); |
Manman Ren | a58f92f | 2016-10-11 21:18:20 +0000 | [diff] [blame] | 2856 | assert(CDecl->getClassMethod(I->getSelector(), true/*AllowHidden*/) && |
Argyrios Kyrtzidis | 342e08f | 2011-08-30 19:43:21 +0000 | [diff] [blame] | 2857 | "Expected to find the method through lookup as well"); |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2858 | // ImpMethodDecl may be null as in a @dynamic property. |
| 2859 | if (ImpMethodDecl) { |
Adrian Prantl | 2073dd2 | 2019-11-04 14:28:14 -0800 | [diff] [blame] | 2860 | // Skip property accessor function stubs. |
| 2861 | if (ImpMethodDecl->isSynthesizedAccessorStub()) |
| 2862 | continue; |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2863 | if (!WarnCategoryMethodImpl) |
| 2864 | WarnConflictingTypedMethods(ImpMethodDecl, I, |
| 2865 | isa<ObjCProtocolDecl>(CDecl)); |
| 2866 | else if (!I->isPropertyAccessor()) |
| 2867 | WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl)); |
| 2868 | } |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2869 | } |
| 2870 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2871 | |
Fariborz Jahanian | 8181caa | 2013-08-14 23:58:55 +0000 | [diff] [blame] | 2872 | if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) { |
| 2873 | // Also, check for methods declared in protocols inherited by |
| 2874 | // this protocol. |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 2875 | for (auto *PI : PD->protocols()) |
Fariborz Jahanian | 8181caa | 2013-08-14 23:58:55 +0000 | [diff] [blame] | 2876 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 2877 | IMPDecl, PI, IncompleteImpl, false, |
Fariborz Jahanian | 8181caa | 2013-08-14 23:58:55 +0000 | [diff] [blame] | 2878 | WarnCategoryMethodImpl); |
| 2879 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2880 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2881 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2882 | // when checking that methods in implementation match their declaration, |
| 2883 | // i.e. when WarnCategoryMethodImpl is false, check declarations in class |
| 2884 | // extension; as well as those in categories. |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2885 | if (!WarnCategoryMethodImpl) { |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 2886 | for (auto *Cat : I->visible_categories()) |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2887 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Argyrios Kyrtzidis | 3a43754 | 2015-10-13 23:27:34 +0000 | [diff] [blame] | 2888 | IMPDecl, Cat, IncompleteImpl, |
| 2889 | ImmediateClass && Cat->IsClassExtension(), |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2890 | WarnCategoryMethodImpl); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2891 | } else { |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2892 | // Also methods in class extensions need be looked at next. |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 2893 | for (auto *Ext : I->visible_extensions()) |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2894 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 2895 | IMPDecl, Ext, IncompleteImpl, false, |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2896 | WarnCategoryMethodImpl); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2899 | // Check for any implementation of a methods declared in protocol. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2900 | for (auto *PI : I->all_referenced_protocols()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2901 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2902 | IMPDecl, PI, IncompleteImpl, false, |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2903 | WarnCategoryMethodImpl); |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2904 | |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 2905 | // FIXME. For now, we are not checking for exact match of methods |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2906 | // in category implementation and its primary class's super class. |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2907 | if (!WarnCategoryMethodImpl && I->getSuperClass()) |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2908 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2909 | IMPDecl, |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2910 | I->getSuperClass(), IncompleteImpl, false); |
| 2911 | } |
| 2912 | } |
| 2913 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2914 | /// CheckCategoryVsClassMethodMatches - Checks that methods implemented in |
| 2915 | /// category matches with those implemented in its primary class and |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2916 | /// warns each time an exact match is found. |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2917 | void Sema::CheckCategoryVsClassMethodMatches( |
| 2918 | ObjCCategoryImplDecl *CatIMPDecl) { |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2919 | // Get category's primary class. |
| 2920 | ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl(); |
| 2921 | if (!CatDecl) |
| 2922 | return; |
| 2923 | ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface(); |
| 2924 | if (!IDecl) |
| 2925 | return; |
Fariborz Jahanian | f3077a2 | 2013-12-05 20:52:31 +0000 | [diff] [blame] | 2926 | ObjCInterfaceDecl *SuperIDecl = IDecl->getSuperClass(); |
| 2927 | SelectorSet InsMap, ClsMap; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2928 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2929 | for (const auto *I : CatIMPDecl->instance_methods()) { |
| 2930 | Selector Sel = I->getSelector(); |
Fariborz Jahanian | f3077a2 | 2013-12-05 20:52:31 +0000 | [diff] [blame] | 2931 | // When checking for methods implemented in the category, skip over |
| 2932 | // those declared in category class's super class. This is because |
| 2933 | // the super class must implement the method. |
| 2934 | if (SuperIDecl && SuperIDecl->lookupMethod(Sel, true)) |
| 2935 | continue; |
| 2936 | InsMap.insert(Sel); |
| 2937 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2938 | |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2939 | for (const auto *I : CatIMPDecl->class_methods()) { |
| 2940 | Selector Sel = I->getSelector(); |
Fariborz Jahanian | f3077a2 | 2013-12-05 20:52:31 +0000 | [diff] [blame] | 2941 | if (SuperIDecl && SuperIDecl->lookupMethod(Sel, false)) |
| 2942 | continue; |
| 2943 | ClsMap.insert(Sel); |
| 2944 | } |
| 2945 | if (InsMap.empty() && ClsMap.empty()) |
| 2946 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2947 | |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2948 | SelectorSet InsMapSeen, ClsMapSeen; |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2949 | bool IncompleteImpl = false; |
| 2950 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 2951 | CatIMPDecl, IDecl, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2952 | IncompleteImpl, false, |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2953 | true /*WarnCategoryMethodImpl*/); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2954 | } |
Fariborz Jahanian | 4ceec3f | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 2955 | |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 2956 | void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2957 | ObjCContainerDecl* CDecl, |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 2958 | bool IncompleteImpl) { |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2959 | SelectorSet InsMap; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2960 | // Check and see if instance methods in class interface have been |
| 2961 | // implemented in the implementation class. |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2962 | for (const auto *I : IMPDecl->instance_methods()) |
| 2963 | InsMap.insert(I->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2964 | |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 2965 | // Add the selectors for getters/setters of @dynamic properties. |
| 2966 | for (const auto *PImpl : IMPDecl->property_impls()) { |
| 2967 | // We only care about @dynamic implementations. |
| 2968 | if (PImpl->getPropertyImplementation() != ObjCPropertyImplDecl::Dynamic) |
| 2969 | continue; |
| 2970 | |
| 2971 | const auto *P = PImpl->getPropertyDecl(); |
| 2972 | if (!P) continue; |
| 2973 | |
| 2974 | InsMap.insert(P->getGetterName()); |
| 2975 | if (!P->getSetterName().isNull()) |
| 2976 | InsMap.insert(P->getSetterName()); |
| 2977 | } |
| 2978 | |
Fariborz Jahanian | 6c6aea9 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 2979 | // Check and see if properties declared in the interface have either 1) |
| 2980 | // an implementation or 2) there is a @synthesize/@dynamic implementation |
| 2981 | // of the property in the @implementation. |
Ted Kremenek | 348e88c | 2014-02-21 19:41:34 +0000 | [diff] [blame] | 2982 | if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 2983 | bool SynthesizeProperties = LangOpts.ObjCDefaultSynthProperties && |
| 2984 | LangOpts.ObjCRuntime.isNonFragile() && |
| 2985 | !IDecl->isObjCRequiresPropertyDefs(); |
| 2986 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, SynthesizeProperties); |
| 2987 | } |
| 2988 | |
Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 2989 | // Diagnose null-resettable synthesized setters. |
| 2990 | diagnoseNullResettableSynthesizedSetters(IMPDecl); |
| 2991 | |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2992 | SelectorSet ClsMap; |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2993 | for (const auto *I : IMPDecl->class_methods()) |
| 2994 | ClsMap.insert(I->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2995 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2996 | // Check for type conflict of methods declared in a class/protocol and |
| 2997 | // its implementation; if any. |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2998 | SelectorSet InsMapSeen, ClsMapSeen; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2999 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 3000 | IMPDecl, CDecl, |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 3001 | IncompleteImpl, true); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3002 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 3003 | // check all methods implemented in category against those declared |
| 3004 | // in its primary class. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3005 | if (ObjCCategoryImplDecl *CatDecl = |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 3006 | dyn_cast<ObjCCategoryImplDecl>(IMPDecl)) |
| 3007 | CheckCategoryVsClassMethodMatches(CatDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3008 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3009 | // Check the protocol list for unimplemented methods in the @implementation |
| 3010 | // class. |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 3011 | // Check and see if class methods in class interface have been |
| 3012 | // implemented in the implementation class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3013 | |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 3014 | LazyProtocolNameSet ExplicitImplProtocols; |
| 3015 | |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 3016 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 3017 | for (auto *PI : I->all_referenced_protocols()) |
| 3018 | CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), PI, IncompleteImpl, |
| 3019 | InsMap, ClsMap, I, ExplicitImplProtocols); |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 3020 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Fariborz Jahanian | 8764c74 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 3021 | // For extended class, unimplemented methods in its protocols will |
| 3022 | // be reported in the primary class. |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 3023 | if (!C->IsClassExtension()) { |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 3024 | for (auto *P : C->protocols()) |
| 3025 | CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), P, |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 3026 | IncompleteImpl, InsMap, ClsMap, CDecl, |
| 3027 | ExplicitImplProtocols); |
Ted Kremenek | 348e88c | 2014-02-21 19:41:34 +0000 | [diff] [blame] | 3028 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3029 | /*SynthesizeProperties=*/false); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3030 | } |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 3031 | } else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3032 | llvm_unreachable("invalid ObjCContainerDecl type."); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3033 | } |
| 3034 | |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 3035 | Sema::DeclGroupPtrTy |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3036 | Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
Chris Lattner | 99a8331 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 3037 | IdentifierInfo **IdentList, |
Ted Kremenek | a26da85 | 2009-11-17 23:12:20 +0000 | [diff] [blame] | 3038 | SourceLocation *IdentLocs, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3039 | ArrayRef<ObjCTypeParamList *> TypeParamLists, |
Chris Lattner | 99a8331 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 3040 | unsigned NumElts) { |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 3041 | SmallVector<Decl *, 8> DeclsInGroup; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3042 | for (unsigned i = 0; i != NumElts; ++i) { |
| 3043 | // Check for another declaration kind with the same name. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3044 | NamedDecl *PrevDecl |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3045 | = LookupSingleName(TUScope, IdentList[i], IdentLocs[i], |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 3046 | LookupOrdinaryName, forRedeclarationInCurContext()); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3047 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Steve Naroff | 946166f | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 3048 | // GCC apparently allows the following idiom: |
| 3049 | // |
| 3050 | // typedef NSObject < XCElementTogglerP > XCElementToggler; |
| 3051 | // @class XCElementToggler; |
| 3052 | // |
Fariborz Jahanian | 04c4455 | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 3053 | // Here we have chosen to ignore the forward class declaration |
| 3054 | // with a warning. Since this is the implied behavior. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3055 | TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3056 | if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3057 | Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i]; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3058 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3059 | } else { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3060 | // a forward class declaration matching a typedef name of a class refers |
Fariborz Jahanian | 04c4455 | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 3061 | // to the underlying class. Just ignore the forward class with a warning |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3062 | // as this will force the intended behavior which is to lookup the |
| 3063 | // typedef name. |
Fariborz Jahanian | 04c4455 | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 3064 | if (isa<ObjCObjectType>(TDD->getUnderlyingType())) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3065 | Diag(AtClassLoc, diag::warn_forward_class_redefinition) |
| 3066 | << IdentList[i]; |
Fariborz Jahanian | 04c4455 | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 3067 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3068 | continue; |
| 3069 | } |
Fariborz Jahanian | 0d45181 | 2009-05-07 21:49:26 +0000 | [diff] [blame] | 3070 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3071 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3072 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 3073 | // Create a declaration to describe this forward declaration. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3074 | ObjCInterfaceDecl *PrevIDecl |
| 3075 | = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Argyrios Kyrtzidis | dd71063 | 2013-06-18 21:26:33 +0000 | [diff] [blame] | 3076 | |
| 3077 | IdentifierInfo *ClassName = IdentList[i]; |
| 3078 | if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) { |
| 3079 | // A previous decl with a different name is because of |
| 3080 | // @compatibility_alias, for example: |
| 3081 | // \code |
| 3082 | // @class NewImage; |
| 3083 | // @compatibility_alias OldImage NewImage; |
| 3084 | // \endcode |
| 3085 | // A lookup for 'OldImage' will return the 'NewImage' decl. |
| 3086 | // |
| 3087 | // In such a case use the real declaration name, instead of the alias one, |
| 3088 | // otherwise we will break IdentifierResolver and redecls-chain invariants. |
| 3089 | // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl |
| 3090 | // has been aliased. |
| 3091 | ClassName = PrevIDecl->getIdentifier(); |
| 3092 | } |
| 3093 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3094 | // If this forward declaration has type parameters, compare them with the |
| 3095 | // type parameters of the previous declaration. |
| 3096 | ObjCTypeParamList *TypeParams = TypeParamLists[i]; |
| 3097 | if (PrevIDecl && TypeParams) { |
| 3098 | if (ObjCTypeParamList *PrevTypeParams = PrevIDecl->getTypeParamList()) { |
| 3099 | // Check for consistency with the previous declaration. |
| 3100 | if (checkTypeParamListConsistency( |
| 3101 | *this, PrevTypeParams, TypeParams, |
| 3102 | TypeParamListContext::ForwardDeclaration)) { |
| 3103 | TypeParams = nullptr; |
| 3104 | } |
| 3105 | } else if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) { |
| 3106 | // The @interface does not have type parameters. Complain. |
| 3107 | Diag(IdentLocs[i], diag::err_objc_parameterized_forward_class) |
| 3108 | << ClassName |
| 3109 | << TypeParams->getSourceRange(); |
| 3110 | Diag(Def->getLocation(), diag::note_defined_here) |
| 3111 | << ClassName; |
| 3112 | |
| 3113 | TypeParams = nullptr; |
| 3114 | } |
| 3115 | } |
| 3116 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 3117 | ObjCInterfaceDecl *IDecl |
| 3118 | = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3119 | ClassName, TypeParams, PrevIDecl, |
| 3120 | IdentLocs[i]); |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 3121 | IDecl->setAtEndRange(IdentLocs[i]); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3122 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 3123 | PushOnScopeChains(IDecl, TUScope); |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 3124 | CheckObjCDeclScope(IDecl); |
| 3125 | DeclsInGroup.push_back(IDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3126 | } |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 3127 | |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 3128 | return BuildDeclaratorGroup(DeclsInGroup); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3131 | static bool tryMatchRecordTypes(ASTContext &Context, |
| 3132 | Sema::MethodMatchStrategy strategy, |
| 3133 | const Type *left, const Type *right); |
| 3134 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3135 | static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy, |
| 3136 | QualType leftQT, QualType rightQT) { |
| 3137 | const Type *left = |
| 3138 | Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr(); |
| 3139 | const Type *right = |
| 3140 | Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr(); |
| 3141 | |
| 3142 | if (left == right) return true; |
| 3143 | |
| 3144 | // If we're doing a strict match, the types have to match exactly. |
| 3145 | if (strategy == Sema::MMS_strict) return false; |
| 3146 | |
| 3147 | if (left->isIncompleteType() || right->isIncompleteType()) return false; |
| 3148 | |
| 3149 | // Otherwise, use this absurdly complicated algorithm to try to |
| 3150 | // validate the basic, low-level compatibility of the two types. |
| 3151 | |
| 3152 | // As a minimum, require the sizes and alignments to match. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 3153 | TypeInfo LeftTI = Context.getTypeInfo(left); |
| 3154 | TypeInfo RightTI = Context.getTypeInfo(right); |
| 3155 | if (LeftTI.Width != RightTI.Width) |
| 3156 | return false; |
| 3157 | |
| 3158 | if (LeftTI.Align != RightTI.Align) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3159 | return false; |
| 3160 | |
| 3161 | // Consider all the kinds of non-dependent canonical types: |
| 3162 | // - functions and arrays aren't possible as return and parameter types |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3163 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3164 | // - vector types of equal size can be arbitrarily mixed |
| 3165 | if (isa<VectorType>(left)) return isa<VectorType>(right); |
| 3166 | if (isa<VectorType>(right)) return false; |
| 3167 | |
| 3168 | // - references should only match references of identical type |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3169 | // - structs, unions, and Objective-C objects must match more-or-less |
| 3170 | // exactly |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3171 | // - everything else should be a scalar |
| 3172 | if (!left->isScalarType() || !right->isScalarType()) |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3173 | return tryMatchRecordTypes(Context, strategy, left, right); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3174 | |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3175 | // Make scalars agree in kind, except count bools as chars, and group |
| 3176 | // all non-member pointers together. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3177 | Type::ScalarTypeKind leftSK = left->getScalarTypeKind(); |
| 3178 | Type::ScalarTypeKind rightSK = right->getScalarTypeKind(); |
| 3179 | if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral; |
| 3180 | if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral; |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3181 | if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer) |
| 3182 | leftSK = Type::STK_ObjCObjectPointer; |
| 3183 | if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer) |
| 3184 | rightSK = Type::STK_ObjCObjectPointer; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3185 | |
| 3186 | // Note that data member pointers and function member pointers don't |
| 3187 | // intermix because of the size differences. |
| 3188 | |
| 3189 | return (leftSK == rightSK); |
| 3190 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3191 | |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3192 | static bool tryMatchRecordTypes(ASTContext &Context, |
| 3193 | Sema::MethodMatchStrategy strategy, |
| 3194 | const Type *lt, const Type *rt) { |
| 3195 | assert(lt && rt && lt != rt); |
| 3196 | |
| 3197 | if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false; |
| 3198 | RecordDecl *left = cast<RecordType>(lt)->getDecl(); |
| 3199 | RecordDecl *right = cast<RecordType>(rt)->getDecl(); |
| 3200 | |
| 3201 | // Require union-hood to match. |
| 3202 | if (left->isUnion() != right->isUnion()) return false; |
| 3203 | |
| 3204 | // Require an exact match if either is non-POD. |
| 3205 | if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) || |
| 3206 | (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD())) |
| 3207 | return false; |
| 3208 | |
| 3209 | // Require size and alignment to match. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 3210 | TypeInfo LeftTI = Context.getTypeInfo(lt); |
| 3211 | TypeInfo RightTI = Context.getTypeInfo(rt); |
| 3212 | if (LeftTI.Width != RightTI.Width) |
| 3213 | return false; |
| 3214 | |
| 3215 | if (LeftTI.Align != RightTI.Align) |
| 3216 | return false; |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3217 | |
| 3218 | // Require fields to match. |
| 3219 | RecordDecl::field_iterator li = left->field_begin(), le = left->field_end(); |
| 3220 | RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end(); |
| 3221 | for (; li != le && ri != re; ++li, ++ri) { |
| 3222 | if (!matchTypes(Context, strategy, li->getType(), ri->getType())) |
| 3223 | return false; |
| 3224 | } |
| 3225 | return (li == le && ri == re); |
| 3226 | } |
| 3227 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3228 | /// MatchTwoMethodDeclarations - Checks that two methods have matching type and |
| 3229 | /// returns true, or false, accordingly. |
| 3230 | /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3231 | bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left, |
| 3232 | const ObjCMethodDecl *right, |
| 3233 | MethodMatchStrategy strategy) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3234 | if (!matchTypes(Context, strategy, left->getReturnType(), |
| 3235 | right->getReturnType())) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3236 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3237 | |
Douglas Gregor | 560b7fa | 2013-02-07 19:13:24 +0000 | [diff] [blame] | 3238 | // If either is hidden, it is not considered to match. |
| 3239 | if (left->isHidden() || right->isHidden()) |
| 3240 | return false; |
| 3241 | |
Pierre Habouzit | d4e1ba3 | 2019-11-07 23:14:58 -0800 | [diff] [blame^] | 3242 | if (left->isDirectMethod() != right->isDirectMethod()) |
| 3243 | return false; |
| 3244 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3245 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3246 | (left->hasAttr<NSReturnsRetainedAttr>() |
| 3247 | != right->hasAttr<NSReturnsRetainedAttr>() || |
| 3248 | left->hasAttr<NSConsumesSelfAttr>() |
| 3249 | != right->hasAttr<NSConsumesSelfAttr>())) |
| 3250 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3251 | |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 3252 | ObjCMethodDecl::param_const_iterator |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 3253 | li = left->param_begin(), le = left->param_end(), ri = right->param_begin(), |
| 3254 | re = right->param_end(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3255 | |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 3256 | for (; li != le && ri != re; ++li, ++ri) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3257 | assert(ri != right->param_end() && "Param mismatch"); |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 3258 | const ParmVarDecl *lparm = *li, *rparm = *ri; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3259 | |
| 3260 | if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType())) |
| 3261 | return false; |
| 3262 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3263 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3264 | lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>()) |
| 3265 | return false; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3266 | } |
| 3267 | return true; |
| 3268 | } |
| 3269 | |
Manman Ren | 7122453 | 2016-04-09 18:59:48 +0000 | [diff] [blame] | 3270 | static bool isMethodContextSameForKindofLookup(ObjCMethodDecl *Method, |
| 3271 | ObjCMethodDecl *MethodInList) { |
| 3272 | auto *MethodProtocol = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext()); |
| 3273 | auto *MethodInListProtocol = |
| 3274 | dyn_cast<ObjCProtocolDecl>(MethodInList->getDeclContext()); |
| 3275 | // If this method belongs to a protocol but the method in list does not, or |
| 3276 | // vice versa, we say the context is not the same. |
| 3277 | if ((MethodProtocol && !MethodInListProtocol) || |
| 3278 | (!MethodProtocol && MethodInListProtocol)) |
| 3279 | return false; |
| 3280 | |
| 3281 | if (MethodProtocol && MethodInListProtocol) |
| 3282 | return true; |
| 3283 | |
| 3284 | ObjCInterfaceDecl *MethodInterface = Method->getClassInterface(); |
| 3285 | ObjCInterfaceDecl *MethodInListInterface = |
| 3286 | MethodInList->getClassInterface(); |
| 3287 | return MethodInterface == MethodInListInterface; |
| 3288 | } |
| 3289 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3290 | void Sema::addMethodToGlobalList(ObjCMethodList *List, |
| 3291 | ObjCMethodDecl *Method) { |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 3292 | // Record at the head of the list whether there were 0, 1, or >= 2 methods |
| 3293 | // inside categories. |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3294 | if (ObjCCategoryDecl *CD = |
| 3295 | dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) |
Argyrios Kyrtzidis | 04703a6 | 2013-04-27 00:10:12 +0000 | [diff] [blame] | 3296 | if (!CD->IsClassExtension() && List->getBits() < 2) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3297 | List->setBits(List->getBits() + 1); |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 3298 | |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3299 | // If the list is empty, make it a singleton list. |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3300 | if (List->getMethod() == nullptr) { |
| 3301 | List->setMethod(Method); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3302 | List->setNext(nullptr); |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3303 | return; |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3304 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3305 | |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3306 | // We've seen a method with this name, see if we have already seen this type |
| 3307 | // signature. |
| 3308 | ObjCMethodList *Previous = List; |
Manman Ren | 051d0b6 | 2016-04-13 23:43:56 +0000 | [diff] [blame] | 3309 | ObjCMethodList *ListWithSameDeclaration = nullptr; |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 3310 | for (; List; Previous = List, List = List->getNext()) { |
Douglas Gregor | 600a2f5 | 2013-06-21 00:20:25 +0000 | [diff] [blame] | 3311 | // If we are building a module, keep all of the methods. |
Richard Smith | bbcc9f0 | 2016-08-26 00:14:38 +0000 | [diff] [blame] | 3312 | if (getLangOpts().isCompilingModule()) |
Douglas Gregor | 600a2f5 | 2013-06-21 00:20:25 +0000 | [diff] [blame] | 3313 | continue; |
| 3314 | |
Manman Ren | 051d0b6 | 2016-04-13 23:43:56 +0000 | [diff] [blame] | 3315 | bool SameDeclaration = MatchTwoMethodDeclarations(Method, |
| 3316 | List->getMethod()); |
Manman Ren | 7122453 | 2016-04-09 18:59:48 +0000 | [diff] [blame] | 3317 | // Looking for method with a type bound requires the correct context exists. |
Manman Ren | 051d0b6 | 2016-04-13 23:43:56 +0000 | [diff] [blame] | 3318 | // We need to insert a method into the list if the context is different. |
| 3319 | // If the method's declaration matches the list |
| 3320 | // a> the method belongs to a different context: we need to insert it, in |
| 3321 | // order to emit the availability message, we need to prioritize over |
| 3322 | // availability among the methods with the same declaration. |
| 3323 | // b> the method belongs to the same context: there is no need to insert a |
| 3324 | // new entry. |
| 3325 | // If the method's declaration does not match the list, we insert it to the |
| 3326 | // end. |
| 3327 | if (!SameDeclaration || |
Manman Ren | 7122453 | 2016-04-09 18:59:48 +0000 | [diff] [blame] | 3328 | !isMethodContextSameForKindofLookup(Method, List->getMethod())) { |
Fariborz Jahanian | d436b2a | 2015-04-07 16:56:27 +0000 | [diff] [blame] | 3329 | // Even if two method types do not match, we would like to say |
| 3330 | // there is more than one declaration so unavailability/deprecated |
| 3331 | // warning is not too noisy. |
| 3332 | if (!Method->isDefined()) |
| 3333 | List->setHasMoreThanOneDecl(true); |
Manman Ren | 051d0b6 | 2016-04-13 23:43:56 +0000 | [diff] [blame] | 3334 | |
| 3335 | // For methods with the same declaration, the one that is deprecated |
| 3336 | // should be put in the front for better diagnostics. |
| 3337 | if (Method->isDeprecated() && SameDeclaration && |
| 3338 | !ListWithSameDeclaration && !List->getMethod()->isDeprecated()) |
| 3339 | ListWithSameDeclaration = List; |
| 3340 | |
| 3341 | if (Method->isUnavailable() && SameDeclaration && |
| 3342 | !ListWithSameDeclaration && |
| 3343 | List->getMethod()->getAvailability() < AR_Deprecated) |
| 3344 | ListWithSameDeclaration = List; |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3345 | continue; |
Fariborz Jahanian | d436b2a | 2015-04-07 16:56:27 +0000 | [diff] [blame] | 3346 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3347 | |
| 3348 | ObjCMethodDecl *PrevObjCMethod = List->getMethod(); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3349 | |
| 3350 | // Propagate the 'defined' bit. |
| 3351 | if (Method->isDefined()) |
| 3352 | PrevObjCMethod->setDefined(true); |
Nico Weber | e3b1104 | 2014-12-27 07:09:37 +0000 | [diff] [blame] | 3353 | else { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3354 | // Objective-C doesn't allow an @interface for a class after its |
| 3355 | // @implementation. So if Method is not defined and there already is |
| 3356 | // an entry for this type signature, Method has to be for a different |
| 3357 | // class than PrevObjCMethod. |
| 3358 | List->setHasMoreThanOneDecl(true); |
| 3359 | } |
| 3360 | |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3361 | // If a method is deprecated, push it in the global pool. |
| 3362 | // This is used for better diagnostics. |
| 3363 | if (Method->isDeprecated()) { |
| 3364 | if (!PrevObjCMethod->isDeprecated()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3365 | List->setMethod(Method); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3366 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3367 | // If the new method is unavailable, push it into global pool |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3368 | // unless previous one is deprecated. |
| 3369 | if (Method->isUnavailable()) { |
| 3370 | if (PrevObjCMethod->getAvailability() < AR_Deprecated) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3371 | List->setMethod(Method); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3372 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3373 | |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3374 | return; |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3375 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3376 | |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3377 | // We have a new signature for an existing method - add it. |
| 3378 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 3379 | ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>(); |
Manman Ren | 7122453 | 2016-04-09 18:59:48 +0000 | [diff] [blame] | 3380 | |
Manman Ren | 051d0b6 | 2016-04-13 23:43:56 +0000 | [diff] [blame] | 3381 | // We insert it right before ListWithSameDeclaration. |
| 3382 | if (ListWithSameDeclaration) { |
| 3383 | auto *List = new (Mem) ObjCMethodList(*ListWithSameDeclaration); |
| 3384 | // FIXME: should we clear the other bits in ListWithSameDeclaration? |
| 3385 | ListWithSameDeclaration->setMethod(Method); |
| 3386 | ListWithSameDeclaration->setNext(List); |
Manman Ren | 7122453 | 2016-04-09 18:59:48 +0000 | [diff] [blame] | 3387 | return; |
| 3388 | } |
| 3389 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3390 | Previous->setNext(new (Mem) ObjCMethodList(Method)); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3393 | /// Read the contents of the method pool for a given selector from |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3394 | /// external storage. |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 3395 | void Sema::ReadMethodPool(Selector Sel) { |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3396 | assert(ExternalSource && "We need an external AST source"); |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 3397 | ExternalSource->ReadMethodPool(Sel); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3398 | } |
| 3399 | |
Manman Ren | a0f31a0 | 2016-04-29 19:04:05 +0000 | [diff] [blame] | 3400 | void Sema::updateOutOfDateSelector(Selector Sel) { |
| 3401 | if (!ExternalSource) |
| 3402 | return; |
| 3403 | ExternalSource->updateOutOfDateSelector(Sel); |
| 3404 | } |
| 3405 | |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3406 | void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl, |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3407 | bool instance) { |
Argyrios Kyrtzidis | b15def2 | 2012-03-12 18:34:26 +0000 | [diff] [blame] | 3408 | // Ignore methods of invalid containers. |
| 3409 | if (cast<Decl>(Method->getDeclContext())->isInvalidDecl()) |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3410 | return; |
Argyrios Kyrtzidis | b15def2 | 2012-03-12 18:34:26 +0000 | [diff] [blame] | 3411 | |
Douglas Gregor | 70f449b | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 3412 | if (ExternalSource) |
| 3413 | ReadMethodPool(Method->getSelector()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3414 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3415 | GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector()); |
Douglas Gregor | 70f449b | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 3416 | if (Pos == MethodPool.end()) |
| 3417 | Pos = MethodPool.insert(std::make_pair(Method->getSelector(), |
| 3418 | GlobalMethods())).first; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3419 | |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 3420 | Method->setDefined(impl); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3421 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3422 | ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second; |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3423 | addMethodToGlobalList(&Entry, Method); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3426 | /// Determines if this is an "acceptable" loose mismatch in the global |
| 3427 | /// method pool. This exists mostly as a hack to get around certain |
| 3428 | /// global mismatches which we can't afford to make warnings / errors. |
| 3429 | /// Really, what we want is a way to take a method out of the global |
| 3430 | /// method pool. |
| 3431 | static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen, |
| 3432 | ObjCMethodDecl *other) { |
| 3433 | if (!chosen->isInstanceMethod()) |
| 3434 | return false; |
| 3435 | |
Pierre Habouzit | d4e1ba3 | 2019-11-07 23:14:58 -0800 | [diff] [blame^] | 3436 | if (chosen->isDirectMethod() != other->isDirectMethod()) |
| 3437 | return false; |
| 3438 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3439 | Selector sel = chosen->getSelector(); |
| 3440 | if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length") |
| 3441 | return false; |
| 3442 | |
| 3443 | // Don't complain about mismatches for -length if the method we |
| 3444 | // chose has an integral result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3445 | return (chosen->getReturnType()->isIntegerType()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3446 | } |
| 3447 | |
Manman Ren | 7ed4f98 | 2016-04-07 19:32:24 +0000 | [diff] [blame] | 3448 | /// Return true if the given method is wthin the type bound. |
| 3449 | static bool FilterMethodsByTypeBound(ObjCMethodDecl *Method, |
| 3450 | const ObjCObjectType *TypeBound) { |
| 3451 | if (!TypeBound) |
| 3452 | return true; |
| 3453 | |
| 3454 | if (TypeBound->isObjCId()) |
| 3455 | // FIXME: should we handle the case of bounding to id<A, B> differently? |
| 3456 | return true; |
| 3457 | |
| 3458 | auto *BoundInterface = TypeBound->getInterface(); |
| 3459 | assert(BoundInterface && "unexpected object type!"); |
| 3460 | |
| 3461 | // Check if the Method belongs to a protocol. We should allow any method |
| 3462 | // defined in any protocol, because any subclass could adopt the protocol. |
| 3463 | auto *MethodProtocol = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext()); |
| 3464 | if (MethodProtocol) { |
| 3465 | return true; |
| 3466 | } |
| 3467 | |
| 3468 | // If the Method belongs to a class, check if it belongs to the class |
| 3469 | // hierarchy of the class bound. |
| 3470 | if (ObjCInterfaceDecl *MethodInterface = Method->getClassInterface()) { |
| 3471 | // We allow methods declared within classes that are part of the hierarchy |
| 3472 | // of the class bound (superclass of, subclass of, or the same as the class |
| 3473 | // bound). |
| 3474 | return MethodInterface == BoundInterface || |
| 3475 | MethodInterface->isSuperClassOf(BoundInterface) || |
| 3476 | BoundInterface->isSuperClassOf(MethodInterface); |
| 3477 | } |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 3478 | llvm_unreachable("unknown method context"); |
Manman Ren | 7ed4f98 | 2016-04-07 19:32:24 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3481 | /// We first select the type of the method: Instance or Factory, then collect |
| 3482 | /// all methods with that type. |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3483 | bool Sema::CollectMultipleMethodsInGlobalPool( |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3484 | Selector Sel, SmallVectorImpl<ObjCMethodDecl *> &Methods, |
Manman Ren | 7ed4f98 | 2016-04-07 19:32:24 +0000 | [diff] [blame] | 3485 | bool InstanceFirst, bool CheckTheOther, |
| 3486 | const ObjCObjectType *TypeBound) { |
Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 3487 | if (ExternalSource) |
| 3488 | ReadMethodPool(Sel); |
| 3489 | |
| 3490 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
| 3491 | if (Pos == MethodPool.end()) |
| 3492 | return false; |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3493 | |
Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 3494 | // Gather the non-hidden methods. |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3495 | ObjCMethodList &MethList = InstanceFirst ? Pos->second.first : |
| 3496 | Pos->second.second; |
Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 3497 | for (ObjCMethodList *M = &MethList; M; M = M->getNext()) |
Manman Ren | 7ed4f98 | 2016-04-07 19:32:24 +0000 | [diff] [blame] | 3498 | if (M->getMethod() && !M->getMethod()->isHidden()) { |
| 3499 | if (FilterMethodsByTypeBound(M->getMethod(), TypeBound)) |
| 3500 | Methods.push_back(M->getMethod()); |
| 3501 | } |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3502 | |
| 3503 | // Return if we find any method with the desired kind. |
| 3504 | if (!Methods.empty()) |
| 3505 | return Methods.size() > 1; |
| 3506 | |
| 3507 | if (!CheckTheOther) |
| 3508 | return false; |
| 3509 | |
| 3510 | // Gather the other kind. |
| 3511 | ObjCMethodList &MethList2 = InstanceFirst ? Pos->second.second : |
| 3512 | Pos->second.first; |
| 3513 | for (ObjCMethodList *M = &MethList2; M; M = M->getNext()) |
Manman Ren | 7ed4f98 | 2016-04-07 19:32:24 +0000 | [diff] [blame] | 3514 | if (M->getMethod() && !M->getMethod()->isHidden()) { |
| 3515 | if (FilterMethodsByTypeBound(M->getMethod(), TypeBound)) |
| 3516 | Methods.push_back(M->getMethod()); |
| 3517 | } |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3518 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3519 | return Methods.size() > 1; |
Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 3520 | } |
| 3521 | |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3522 | bool Sema::AreMultipleMethodsInGlobalPool( |
| 3523 | Selector Sel, ObjCMethodDecl *BestMethod, SourceRange R, |
| 3524 | bool receiverIdOrClass, SmallVectorImpl<ObjCMethodDecl *> &Methods) { |
| 3525 | // Diagnose finding more than one method in global pool. |
| 3526 | SmallVector<ObjCMethodDecl *, 4> FilteredMethods; |
| 3527 | FilteredMethods.push_back(BestMethod); |
| 3528 | |
| 3529 | for (auto *M : Methods) |
| 3530 | if (M != BestMethod && !M->hasAttr<UnavailableAttr>()) |
| 3531 | FilteredMethods.push_back(M); |
| 3532 | |
| 3533 | if (FilteredMethods.size() > 1) |
| 3534 | DiagnoseMultipleMethodInGlobalPool(FilteredMethods, Sel, R, |
| 3535 | receiverIdOrClass); |
| 3536 | |
Fariborz Jahanian | c62d16f | 2014-11-13 22:27:05 +0000 | [diff] [blame] | 3537 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3538 | // Test for no method in the pool which should not trigger any warning by |
| 3539 | // caller. |
Fariborz Jahanian | c62d16f | 2014-11-13 22:27:05 +0000 | [diff] [blame] | 3540 | if (Pos == MethodPool.end()) |
| 3541 | return true; |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3542 | ObjCMethodList &MethList = |
| 3543 | BestMethod->isInstanceMethod() ? Pos->second.first : Pos->second.second; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3544 | return MethList.hasMoreThanOneDecl(); |
Fariborz Jahanian | c62d16f | 2014-11-13 22:27:05 +0000 | [diff] [blame] | 3545 | } |
| 3546 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3547 | ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R, |
Fariborz Jahanian | 3337b2e | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 3548 | bool receiverIdOrClass, |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3549 | bool instance) { |
Douglas Gregor | 70f449b | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 3550 | if (ExternalSource) |
| 3551 | ReadMethodPool(Sel); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3552 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3553 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
Douglas Gregor | 70f449b | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 3554 | if (Pos == MethodPool.end()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3555 | return nullptr; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3556 | |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3557 | // Gather the non-hidden methods. |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3558 | ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second; |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 3559 | SmallVector<ObjCMethodDecl *, 4> Methods; |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 3560 | for (ObjCMethodList *M = &MethList; M; M = M->getNext()) { |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3561 | if (M->getMethod() && !M->getMethod()->isHidden()) |
| 3562 | return M->getMethod(); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3563 | } |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3564 | return nullptr; |
| 3565 | } |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3566 | |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3567 | void Sema::DiagnoseMultipleMethodInGlobalPool(SmallVectorImpl<ObjCMethodDecl*> &Methods, |
| 3568 | Selector Sel, SourceRange R, |
| 3569 | bool receiverIdOrClass) { |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3570 | // We found multiple methods, so we may have to complain. |
| 3571 | bool issueDiagnostic = false, issueError = false; |
Jonathan Roelofs | 7441136 | 2015-04-28 18:04:44 +0000 | [diff] [blame] | 3572 | |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3573 | // We support a warning which complains about *any* difference in |
| 3574 | // method signature. |
| 3575 | bool strictSelectorMatch = |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3576 | receiverIdOrClass && |
| 3577 | !Diags.isIgnored(diag::warn_strict_multiple_method_decl, R.getBegin()); |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3578 | if (strictSelectorMatch) { |
| 3579 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 3580 | if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) { |
| 3581 | issueDiagnostic = true; |
| 3582 | break; |
| 3583 | } |
| 3584 | } |
| 3585 | } |
Jonathan Roelofs | 7441136 | 2015-04-28 18:04:44 +0000 | [diff] [blame] | 3586 | |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3587 | // If we didn't see any strict differences, we won't see any loose |
| 3588 | // differences. In ARC, however, we also need to check for loose |
| 3589 | // mismatches, because most of them are errors. |
| 3590 | if (!strictSelectorMatch || |
| 3591 | (issueDiagnostic && getLangOpts().ObjCAutoRefCount)) |
| 3592 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 3593 | // This checks if the methods differ in type mismatch. |
| 3594 | if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) && |
| 3595 | !isAcceptableMethodMismatch(Methods[0], Methods[I])) { |
| 3596 | issueDiagnostic = true; |
| 3597 | if (getLangOpts().ObjCAutoRefCount) |
| 3598 | issueError = true; |
| 3599 | break; |
| 3600 | } |
| 3601 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3602 | |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3603 | if (issueDiagnostic) { |
| 3604 | if (issueError) |
| 3605 | Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R; |
| 3606 | else if (strictSelectorMatch) |
| 3607 | Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R; |
| 3608 | else |
| 3609 | Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3610 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3611 | Diag(Methods[0]->getBeginLoc(), |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3612 | issueError ? diag::note_possibility : diag::note_using) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3613 | << Methods[0]->getSourceRange(); |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3614 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3615 | Diag(Methods[I]->getBeginLoc(), diag::note_also_found) |
| 3616 | << Methods[I]->getSourceRange(); |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3617 | } |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3618 | } |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3619 | } |
| 3620 | |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 3621 | ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) { |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3622 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
| 3623 | if (Pos == MethodPool.end()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3624 | return nullptr; |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3625 | |
| 3626 | GlobalMethods &Methods = Pos->second; |
Fariborz Jahanian | ec762bd | 2014-03-26 20:59:26 +0000 | [diff] [blame] | 3627 | for (const ObjCMethodList *Method = &Methods.first; Method; |
| 3628 | Method = Method->getNext()) |
Fariborz Jahanian | 4019c7f | 2015-02-19 21:52:41 +0000 | [diff] [blame] | 3629 | if (Method->getMethod() && |
| 3630 | (Method->getMethod()->isDefined() || |
| 3631 | Method->getMethod()->isPropertyAccessor())) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3632 | return Method->getMethod(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3633 | |
Fariborz Jahanian | ec762bd | 2014-03-26 20:59:26 +0000 | [diff] [blame] | 3634 | for (const ObjCMethodList *Method = &Methods.second; Method; |
| 3635 | Method = Method->getNext()) |
Fariborz Jahanian | 4019c7f | 2015-02-19 21:52:41 +0000 | [diff] [blame] | 3636 | if (Method->getMethod() && |
| 3637 | (Method->getMethod()->isDefined() || |
| 3638 | Method->getMethod()->isPropertyAccessor())) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3639 | return Method->getMethod(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3640 | return nullptr; |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 3641 | } |
| 3642 | |
Fariborz Jahanian | 42f8938 | 2013-05-30 21:48:58 +0000 | [diff] [blame] | 3643 | static void |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3644 | HelperSelectorsForTypoCorrection( |
| 3645 | SmallVectorImpl<const ObjCMethodDecl *> &BestMethod, |
| 3646 | StringRef Typo, const ObjCMethodDecl * Method) { |
| 3647 | const unsigned MaxEditDistance = 1; |
| 3648 | unsigned BestEditDistance = MaxEditDistance + 1; |
Richard Trieu | ea8d370 | 2013-06-06 02:22:29 +0000 | [diff] [blame] | 3649 | std::string MethodName = Method->getSelector().getAsString(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3650 | |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3651 | unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size()); |
| 3652 | if (MinPossibleEditDistance > 0 && |
| 3653 | Typo.size() / MinPossibleEditDistance < 1) |
| 3654 | return; |
| 3655 | unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance); |
| 3656 | if (EditDistance > MaxEditDistance) |
| 3657 | return; |
| 3658 | if (EditDistance == BestEditDistance) |
| 3659 | BestMethod.push_back(Method); |
| 3660 | else if (EditDistance < BestEditDistance) { |
| 3661 | BestMethod.clear(); |
| 3662 | BestMethod.push_back(Method); |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3663 | } |
| 3664 | } |
| 3665 | |
Fariborz Jahanian | 7548167 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 3666 | static bool HelperIsMethodInObjCType(Sema &S, Selector Sel, |
| 3667 | QualType ObjectType) { |
| 3668 | if (ObjectType.isNull()) |
| 3669 | return true; |
| 3670 | if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/)) |
| 3671 | return true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3672 | return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) != |
| 3673 | nullptr; |
Fariborz Jahanian | 7548167 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 3674 | } |
| 3675 | |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3676 | const ObjCMethodDecl * |
Fariborz Jahanian | 7548167 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 3677 | Sema::SelectorsForTypoCorrection(Selector Sel, |
| 3678 | QualType ObjectType) { |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3679 | unsigned NumArgs = Sel.getNumArgs(); |
| 3680 | SmallVector<const ObjCMethodDecl *, 8> Methods; |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3681 | bool ObjectIsId = true, ObjectIsClass = true; |
| 3682 | if (ObjectType.isNull()) |
| 3683 | ObjectIsId = ObjectIsClass = false; |
| 3684 | else if (!ObjectType->isObjCObjectPointerType()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3685 | return nullptr; |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3686 | else if (const ObjCObjectPointerType *ObjCPtr = |
| 3687 | ObjectType->getAsObjCInterfacePointerType()) { |
| 3688 | ObjectType = QualType(ObjCPtr->getInterfaceType(), 0); |
| 3689 | ObjectIsId = ObjectIsClass = false; |
| 3690 | } |
| 3691 | else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType()) |
| 3692 | ObjectIsClass = false; |
| 3693 | else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType()) |
| 3694 | ObjectIsId = false; |
| 3695 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3696 | return nullptr; |
| 3697 | |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3698 | for (GlobalMethodPool::iterator b = MethodPool.begin(), |
| 3699 | e = MethodPool.end(); b != e; b++) { |
| 3700 | // instance methods |
| 3701 | for (ObjCMethodList *M = &b->second.first; M; M=M->getNext()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3702 | if (M->getMethod() && |
| 3703 | (M->getMethod()->getSelector().getNumArgs() == NumArgs) && |
| 3704 | (M->getMethod()->getSelector() != Sel)) { |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3705 | if (ObjectIsId) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3706 | Methods.push_back(M->getMethod()); |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3707 | else if (!ObjectIsClass && |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3708 | HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(), |
| 3709 | ObjectType)) |
| 3710 | Methods.push_back(M->getMethod()); |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3711 | } |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3712 | // class methods |
| 3713 | for (ObjCMethodList *M = &b->second.second; M; M=M->getNext()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3714 | if (M->getMethod() && |
| 3715 | (M->getMethod()->getSelector().getNumArgs() == NumArgs) && |
| 3716 | (M->getMethod()->getSelector() != Sel)) { |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3717 | if (ObjectIsClass) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3718 | Methods.push_back(M->getMethod()); |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3719 | else if (!ObjectIsId && |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3720 | HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(), |
| 3721 | ObjectType)) |
| 3722 | Methods.push_back(M->getMethod()); |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3723 | } |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3724 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3725 | |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3726 | SmallVector<const ObjCMethodDecl *, 8> SelectedMethods; |
| 3727 | for (unsigned i = 0, e = Methods.size(); i < e; i++) { |
| 3728 | HelperSelectorsForTypoCorrection(SelectedMethods, |
| 3729 | Sel.getAsString(), Methods[i]); |
| 3730 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3731 | return (SelectedMethods.size() == 1) ? SelectedMethods[0] : nullptr; |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3732 | } |
| 3733 | |
Fariborz Jahanian | 42f8938 | 2013-05-30 21:48:58 +0000 | [diff] [blame] | 3734 | /// DiagnoseDuplicateIvars - |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3735 | /// Check for duplicate ivars in the entire class at the start of |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 3736 | /// \@implementation. This becomes necesssary because class extension can |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 3737 | /// 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] | 3738 | /// class's \@implementation is seen. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3739 | void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 3740 | ObjCInterfaceDecl *SID) { |
Aaron Ballman | 59abbd4 | 2014-03-13 21:09:43 +0000 | [diff] [blame] | 3741 | for (auto *Ivar : ID->ivars()) { |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 3742 | if (Ivar->isInvalidDecl()) |
| 3743 | continue; |
| 3744 | if (IdentifierInfo *II = Ivar->getIdentifier()) { |
| 3745 | ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II); |
| 3746 | if (prevIvar) { |
| 3747 | Diag(Ivar->getLocation(), diag::err_duplicate_member) << II; |
| 3748 | Diag(prevIvar->getLocation(), diag::note_previous_declaration); |
| 3749 | Ivar->setInvalidDecl(); |
| 3750 | } |
| 3751 | } |
| 3752 | } |
| 3753 | } |
| 3754 | |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 3755 | /// Diagnose attempts to define ARC-__weak ivars when __weak is disabled. |
| 3756 | static void DiagnoseWeakIvars(Sema &S, ObjCImplementationDecl *ID) { |
| 3757 | if (S.getLangOpts().ObjCWeak) return; |
| 3758 | |
| 3759 | for (auto ivar = ID->getClassInterface()->all_declared_ivar_begin(); |
| 3760 | ivar; ivar = ivar->getNextIvar()) { |
| 3761 | if (ivar->isInvalidDecl()) continue; |
| 3762 | if (ivar->getType().getObjCLifetime() == Qualifiers::OCL_Weak) { |
| 3763 | if (S.getLangOpts().ObjCWeakRuntime) { |
| 3764 | S.Diag(ivar->getLocation(), diag::err_arc_weak_disabled); |
| 3765 | } else { |
| 3766 | S.Diag(ivar->getLocation(), diag::err_arc_weak_no_runtime); |
| 3767 | } |
| 3768 | } |
| 3769 | } |
| 3770 | } |
| 3771 | |
Volodymyr Sapsai | 30680e9 | 2017-10-23 22:01:41 +0000 | [diff] [blame] | 3772 | /// Diagnose attempts to use flexible array member with retainable object type. |
| 3773 | static void DiagnoseRetainableFlexibleArrayMember(Sema &S, |
| 3774 | ObjCInterfaceDecl *ID) { |
| 3775 | if (!S.getLangOpts().ObjCAutoRefCount) |
| 3776 | return; |
| 3777 | |
| 3778 | for (auto ivar = ID->all_declared_ivar_begin(); ivar; |
| 3779 | ivar = ivar->getNextIvar()) { |
| 3780 | if (ivar->isInvalidDecl()) |
| 3781 | continue; |
| 3782 | QualType IvarTy = ivar->getType(); |
| 3783 | if (IvarTy->isIncompleteArrayType() && |
| 3784 | (IvarTy.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) && |
| 3785 | IvarTy->isObjCLifetimeType()) { |
| 3786 | S.Diag(ivar->getLocation(), diag::err_flexible_array_arc_retainable); |
| 3787 | ivar->setInvalidDecl(); |
| 3788 | } |
| 3789 | } |
| 3790 | } |
| 3791 | |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3792 | Sema::ObjCContainerKind Sema::getObjCContainerKind() const { |
| 3793 | switch (CurContext->getDeclKind()) { |
| 3794 | case Decl::ObjCInterface: |
| 3795 | return Sema::OCK_Interface; |
| 3796 | case Decl::ObjCProtocol: |
| 3797 | return Sema::OCK_Protocol; |
| 3798 | case Decl::ObjCCategory: |
Benjamin Kramer | a008d3a | 2015-04-10 11:37:55 +0000 | [diff] [blame] | 3799 | if (cast<ObjCCategoryDecl>(CurContext)->IsClassExtension()) |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3800 | return Sema::OCK_ClassExtension; |
Benjamin Kramer | a008d3a | 2015-04-10 11:37:55 +0000 | [diff] [blame] | 3801 | return Sema::OCK_Category; |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3802 | case Decl::ObjCImplementation: |
| 3803 | return Sema::OCK_Implementation; |
| 3804 | case Decl::ObjCCategoryImpl: |
| 3805 | return Sema::OCK_CategoryImplementation; |
| 3806 | |
| 3807 | default: |
| 3808 | return Sema::OCK_None; |
| 3809 | } |
| 3810 | } |
| 3811 | |
Volodymyr Sapsai | 30680e9 | 2017-10-23 22:01:41 +0000 | [diff] [blame] | 3812 | static bool IsVariableSizedType(QualType T) { |
| 3813 | if (T->isIncompleteArrayType()) |
| 3814 | return true; |
| 3815 | const auto *RecordTy = T->getAs<RecordType>(); |
| 3816 | return (RecordTy && RecordTy->getDecl()->hasFlexibleArrayMember()); |
| 3817 | } |
| 3818 | |
| 3819 | static void DiagnoseVariableSizedIvars(Sema &S, ObjCContainerDecl *OCD) { |
| 3820 | ObjCInterfaceDecl *IntfDecl = nullptr; |
| 3821 | ObjCInterfaceDecl::ivar_range Ivars = llvm::make_range( |
| 3822 | ObjCInterfaceDecl::ivar_iterator(), ObjCInterfaceDecl::ivar_iterator()); |
| 3823 | if ((IntfDecl = dyn_cast<ObjCInterfaceDecl>(OCD))) { |
| 3824 | Ivars = IntfDecl->ivars(); |
| 3825 | } else if (auto *ImplDecl = dyn_cast<ObjCImplementationDecl>(OCD)) { |
| 3826 | IntfDecl = ImplDecl->getClassInterface(); |
| 3827 | Ivars = ImplDecl->ivars(); |
| 3828 | } else if (auto *CategoryDecl = dyn_cast<ObjCCategoryDecl>(OCD)) { |
| 3829 | if (CategoryDecl->IsClassExtension()) { |
| 3830 | IntfDecl = CategoryDecl->getClassInterface(); |
| 3831 | Ivars = CategoryDecl->ivars(); |
| 3832 | } |
| 3833 | } |
| 3834 | |
| 3835 | // Check if variable sized ivar is in interface and visible to subclasses. |
| 3836 | if (!isa<ObjCInterfaceDecl>(OCD)) { |
| 3837 | for (auto ivar : Ivars) { |
| 3838 | if (!ivar->isInvalidDecl() && IsVariableSizedType(ivar->getType())) { |
| 3839 | S.Diag(ivar->getLocation(), diag::warn_variable_sized_ivar_visibility) |
| 3840 | << ivar->getDeclName() << ivar->getType(); |
| 3841 | } |
| 3842 | } |
| 3843 | } |
| 3844 | |
| 3845 | // Subsequent checks require interface decl. |
| 3846 | if (!IntfDecl) |
| 3847 | return; |
| 3848 | |
| 3849 | // Check if variable sized ivar is followed by another ivar. |
| 3850 | for (ObjCIvarDecl *ivar = IntfDecl->all_declared_ivar_begin(); ivar; |
| 3851 | ivar = ivar->getNextIvar()) { |
| 3852 | if (ivar->isInvalidDecl() || !ivar->getNextIvar()) |
| 3853 | continue; |
| 3854 | QualType IvarTy = ivar->getType(); |
| 3855 | bool IsInvalidIvar = false; |
| 3856 | if (IvarTy->isIncompleteArrayType()) { |
| 3857 | S.Diag(ivar->getLocation(), diag::err_flexible_array_not_at_end) |
| 3858 | << ivar->getDeclName() << IvarTy |
| 3859 | << TTK_Class; // Use "class" for Obj-C. |
| 3860 | IsInvalidIvar = true; |
| 3861 | } else if (const RecordType *RecordTy = IvarTy->getAs<RecordType>()) { |
| 3862 | if (RecordTy->getDecl()->hasFlexibleArrayMember()) { |
| 3863 | S.Diag(ivar->getLocation(), |
| 3864 | diag::err_objc_variable_sized_type_not_at_end) |
| 3865 | << ivar->getDeclName() << IvarTy; |
| 3866 | IsInvalidIvar = true; |
| 3867 | } |
| 3868 | } |
| 3869 | if (IsInvalidIvar) { |
| 3870 | S.Diag(ivar->getNextIvar()->getLocation(), |
| 3871 | diag::note_next_ivar_declaration) |
| 3872 | << ivar->getNextIvar()->getSynthesize(); |
| 3873 | ivar->setInvalidDecl(); |
| 3874 | } |
| 3875 | } |
| 3876 | |
| 3877 | // Check if ObjC container adds ivars after variable sized ivar in superclass. |
| 3878 | // Perform the check only if OCD is the first container to declare ivars to |
| 3879 | // avoid multiple warnings for the same ivar. |
| 3880 | ObjCIvarDecl *FirstIvar = |
| 3881 | (Ivars.begin() == Ivars.end()) ? nullptr : *Ivars.begin(); |
| 3882 | if (FirstIvar && (FirstIvar == IntfDecl->all_declared_ivar_begin())) { |
| 3883 | const ObjCInterfaceDecl *SuperClass = IntfDecl->getSuperClass(); |
| 3884 | while (SuperClass && SuperClass->ivar_empty()) |
| 3885 | SuperClass = SuperClass->getSuperClass(); |
| 3886 | if (SuperClass) { |
| 3887 | auto IvarIter = SuperClass->ivar_begin(); |
| 3888 | std::advance(IvarIter, SuperClass->ivar_size() - 1); |
| 3889 | const ObjCIvarDecl *LastIvar = *IvarIter; |
| 3890 | if (IsVariableSizedType(LastIvar->getType())) { |
| 3891 | S.Diag(FirstIvar->getLocation(), |
| 3892 | diag::warn_superclass_variable_sized_type_not_at_end) |
| 3893 | << FirstIvar->getDeclName() << LastIvar->getDeclName() |
| 3894 | << LastIvar->getType() << SuperClass->getDeclName(); |
| 3895 | S.Diag(LastIvar->getLocation(), diag::note_entity_declared_at) |
| 3896 | << LastIvar->getDeclName(); |
| 3897 | } |
| 3898 | } |
| 3899 | } |
| 3900 | } |
| 3901 | |
Fariborz Jahanian | 0080fb5 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 3902 | // Note: For class/category implementations, allMethods is always null. |
Robert Wilhelm | 57c6711 | 2013-07-17 21:14:35 +0000 | [diff] [blame] | 3903 | Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods, |
Fariborz Jahanian | dfb7687 | 2013-07-17 00:05:08 +0000 | [diff] [blame] | 3904 | ArrayRef<DeclGroupPtrTy> allTUVars) { |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3905 | if (getObjCContainerKind() == Sema::OCK_None) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3906 | return nullptr; |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3907 | |
| 3908 | assert(AtEnd.isValid() && "Invalid location for '@end'"); |
| 3909 | |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 3910 | auto *OCD = cast<ObjCContainerDecl>(CurContext); |
| 3911 | Decl *ClassDecl = OCD; |
| 3912 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3913 | bool isInterfaceDeclKind = |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 3914 | isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) |
| 3915 | || isa<ObjCProtocolDecl>(ClassDecl); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3916 | bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 3917 | |
Adrian Prantl | 2073dd2 | 2019-11-04 14:28:14 -0800 | [diff] [blame] | 3918 | // Make synthesized accessor stub functions visible. |
| 3919 | // ActOnPropertyImplDecl() creates them as not visible in case |
| 3920 | // they are overridden by an explicit method that is encountered |
| 3921 | // later. |
| 3922 | if (auto *OID = dyn_cast<ObjCImplementationDecl>(CurContext)) { |
| 3923 | for (auto PropImpl : OID->property_impls()) { |
| 3924 | if (auto *Getter = PropImpl->getGetterMethodDecl()) |
| 3925 | if (Getter->isSynthesizedAccessorStub()) { |
| 3926 | OID->makeDeclVisibleInContext(Getter); |
| 3927 | OID->addDecl(Getter); |
| 3928 | } |
| 3929 | if (auto *Setter = PropImpl->getSetterMethodDecl()) |
| 3930 | if (Setter->isSynthesizedAccessorStub()) { |
| 3931 | OID->makeDeclVisibleInContext(Setter); |
| 3932 | OID->addDecl(Setter); |
| 3933 | } |
| 3934 | } |
| 3935 | } |
| 3936 | |
Steve Naroff | 35c62ae | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 3937 | // FIXME: Remove these and use the ObjCContainerDecl/DeclContext. |
| 3938 | llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; |
| 3939 | llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; |
| 3940 | |
Fariborz Jahanian | 0080fb5 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 3941 | for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3942 | ObjCMethodDecl *Method = |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3943 | cast_or_null<ObjCMethodDecl>(allMethods[i]); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3944 | |
| 3945 | if (!Method) continue; // Already issued a diagnostic. |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 3946 | if (Method->isInstanceMethod()) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3947 | /// Check for instance method of the same name with incompatible types |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3948 | const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3949 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3950 | : false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3951 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 42b1e9e | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 3952 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3953 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3954 | << Method->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3955 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | 87e9275 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 3956 | Method->setInvalidDecl(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3957 | } else { |
Fariborz Jahanian | c17c86b | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 3958 | if (PrevMethod) { |
Argyrios Kyrtzidis | dcaaa21 | 2011-10-14 08:02:31 +0000 | [diff] [blame] | 3959 | Method->setAsRedeclaration(PrevMethod); |
Fariborz Jahanian | c17c86b | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 3960 | if (!Context.getSourceManager().isInSystemHeader( |
| 3961 | Method->getLocation())) |
| 3962 | Diag(Method->getLocation(), diag::warn_duplicate_method_decl) |
| 3963 | << Method->getDeclName(); |
| 3964 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 3965 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3966 | InsMap[Method->getSelector()] = Method; |
| 3967 | /// The following allows us to typecheck messages to "id". |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3968 | AddInstanceMethodToGlobalPool(Method); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3969 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3970 | } else { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3971 | /// Check for class method of the same name with incompatible types |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3972 | const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3973 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3974 | : false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3975 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 42b1e9e | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 3976 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3977 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3978 | << Method->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3979 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | 87e9275 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 3980 | Method->setInvalidDecl(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3981 | } else { |
Fariborz Jahanian | c17c86b | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 3982 | if (PrevMethod) { |
Argyrios Kyrtzidis | dcaaa21 | 2011-10-14 08:02:31 +0000 | [diff] [blame] | 3983 | Method->setAsRedeclaration(PrevMethod); |
Fariborz Jahanian | c17c86b | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 3984 | if (!Context.getSourceManager().isInSystemHeader( |
| 3985 | Method->getLocation())) |
| 3986 | Diag(Method->getLocation(), diag::warn_duplicate_method_decl) |
| 3987 | << Method->getDeclName(); |
| 3988 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 3989 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3990 | ClsMap[Method->getSelector()] = Method; |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3991 | AddFactoryMethodToGlobalPool(Method); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3992 | } |
| 3993 | } |
| 3994 | } |
Douglas Gregor | b898209 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 3995 | if (isa<ObjCInterfaceDecl>(ClassDecl)) { |
| 3996 | // Nothing to do here. |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 3997 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Fariborz Jahanian | 62293f4 | 2008-12-06 19:59:02 +0000 | [diff] [blame] | 3998 | // Categories are used to extend the class by declaring new methods. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3999 | // 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] | 4000 | // need to compare the added property to those in the class. |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 4001 | |
Fariborz Jahanian | c21f543 | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 4002 | if (C->IsClassExtension()) { |
| 4003 | ObjCInterfaceDecl *CCPrimary = C->getClassInterface(); |
| 4004 | DiagnoseClassExtensionDupMethods(C, CCPrimary); |
Fariborz Jahanian | c21f543 | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 4005 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4006 | } |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 4007 | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) { |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 4008 | if (CDecl->getIdentifier()) |
| 4009 | // ProcessPropertyDecl is responsible for diagnosing conflicts with any |
| 4010 | // user-defined setter/getter. It also synthesizes setter/getter methods |
| 4011 | // and adds them to the DeclContext and global method pools. |
Manman Ren | efe1bac | 2016-01-27 20:00:32 +0000 | [diff] [blame] | 4012 | for (auto *I : CDecl->properties()) |
Douglas Gregor | e17765e | 2015-11-03 17:02:34 +0000 | [diff] [blame] | 4013 | ProcessPropertyDecl(I); |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 4014 | CDecl->setAtEndRange(AtEnd); |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 4015 | } |
| 4016 | if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 4017 | IC->setAtEndRange(AtEnd); |
Fariborz Jahanian | 13e0c90 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 4018 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) { |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 4019 | // Any property declared in a class extension might have user |
| 4020 | // declared setter or getter in current class extension or one |
| 4021 | // of the other class extensions. Mark them as synthesized as |
| 4022 | // property will be synthesized when property with same name is |
| 4023 | // seen in the @implementation. |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 4024 | for (const auto *Ext : IDecl->visible_extensions()) { |
Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame] | 4025 | for (const auto *Property : Ext->instance_properties()) { |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 4026 | // Skip over properties declared @dynamic |
| 4027 | if (const ObjCPropertyImplDecl *PIDecl |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 4028 | = IC->FindPropertyImplDecl(Property->getIdentifier(), |
| 4029 | Property->getQueryKind())) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4030 | if (PIDecl->getPropertyImplementation() |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 4031 | == ObjCPropertyImplDecl::Dynamic) |
| 4032 | continue; |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 4033 | |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 4034 | for (const auto *Ext : IDecl->visible_extensions()) { |
Adrian Prantl | 2073dd2 | 2019-11-04 14:28:14 -0800 | [diff] [blame] | 4035 | if (ObjCMethodDecl *GetterMethod = |
| 4036 | Ext->getInstanceMethod(Property->getGetterName())) |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 4037 | GetterMethod->setPropertyAccessor(true); |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 4038 | if (!Property->isReadOnly()) |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 4039 | if (ObjCMethodDecl *SetterMethod |
| 4040 | = Ext->getInstanceMethod(Property->getSetterName())) |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 4041 | SetterMethod->setPropertyAccessor(true); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 4042 | } |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 4043 | } |
| 4044 | } |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 4045 | ImplMethodsVsClassMethods(S, IC, IDecl); |
Fariborz Jahanian | 13e0c90 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 4046 | AtomicPropertySetterGetterRules(IC, IDecl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4047 | DiagnoseOwningPropertyGetterSynthesis(IC); |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4048 | DiagnoseUnusedBackingIvarInAccessor(S, IC); |
Fariborz Jahanian | 20cfff3 | 2015-03-11 16:59:48 +0000 | [diff] [blame] | 4049 | if (IDecl->hasDesignatedInitializers()) |
| 4050 | DiagnoseMissingDesignatedInitOverrides(IC, IDecl); |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 4051 | DiagnoseWeakIvars(*this, IC); |
Volodymyr Sapsai | 30680e9 | 2017-10-23 22:01:41 +0000 | [diff] [blame] | 4052 | DiagnoseRetainableFlexibleArrayMember(*this, IDecl); |
Argyrios Kyrtzidis | db5ce0f | 2013-12-03 21:11:54 +0000 | [diff] [blame] | 4053 | |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4054 | bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4055 | if (IDecl->getSuperClass() == nullptr) { |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4056 | // This class has no superclass, so check that it has been marked with |
| 4057 | // __attribute((objc_root_class)). |
| 4058 | if (!HasRootClassAttr) { |
| 4059 | SourceLocation DeclLoc(IDecl->getLocation()); |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4060 | SourceLocation SuperClassLoc(getLocForEndOfToken(DeclLoc)); |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4061 | Diag(DeclLoc, diag::warn_objc_root_class_missing) |
| 4062 | << IDecl->getIdentifier(); |
| 4063 | // See if NSObject is in the current scope, and if it is, suggest |
| 4064 | // adding " : NSObject " to the class declaration. |
| 4065 | NamedDecl *IF = LookupSingleName(TUScope, |
| 4066 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject), |
| 4067 | DeclLoc, LookupOrdinaryName); |
| 4068 | ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
| 4069 | if (NSObjectDecl && NSObjectDecl->getDefinition()) { |
| 4070 | Diag(SuperClassLoc, diag::note_objc_needs_superclass) |
| 4071 | << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject "); |
| 4072 | } else { |
| 4073 | Diag(SuperClassLoc, diag::note_objc_needs_superclass); |
| 4074 | } |
| 4075 | } |
| 4076 | } else if (HasRootClassAttr) { |
| 4077 | // Complain that only root classes may have this attribute. |
| 4078 | Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass); |
| 4079 | } |
| 4080 | |
Alex Lorenz | a8c44ba | 2016-10-28 10:25:10 +0000 | [diff] [blame] | 4081 | if (const ObjCInterfaceDecl *Super = IDecl->getSuperClass()) { |
| 4082 | // An interface can subclass another interface with a |
| 4083 | // objc_subclassing_restricted attribute when it has that attribute as |
| 4084 | // well (because of interfaces imported from Swift). Therefore we have |
| 4085 | // to check if we can subclass in the implementation as well. |
| 4086 | if (IDecl->hasAttr<ObjCSubclassingRestrictedAttr>() && |
| 4087 | Super->hasAttr<ObjCSubclassingRestrictedAttr>()) { |
| 4088 | Diag(IC->getLocation(), diag::err_restricted_superclass_mismatch); |
| 4089 | Diag(Super->getLocation(), diag::note_class_declared); |
| 4090 | } |
| 4091 | } |
| 4092 | |
John McCall | 2c91c3b | 2019-05-30 04:09:01 +0000 | [diff] [blame] | 4093 | if (IDecl->hasAttr<ObjCClassStubAttr>()) |
| 4094 | Diag(IC->getLocation(), diag::err_implementation_of_class_stub); |
| 4095 | |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 4096 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 4097 | while (IDecl->getSuperClass()) { |
| 4098 | DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass()); |
| 4099 | IDecl = IDecl->getSuperClass(); |
| 4100 | } |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4101 | } |
Fariborz Jahanian | 13e0c90 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 4102 | } |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 4103 | SetIvarInitializers(IC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4104 | } else if (ObjCCategoryImplDecl* CatImplClass = |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 4105 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 4106 | CatImplClass->setAtEndRange(AtEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4107 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4108 | // Find category interface decl and then check that all methods declared |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 4109 | // in this interface are implemented in the category @implementation. |
Chris Lattner | 41fd42e | 2009-02-16 18:32:47 +0000 | [diff] [blame] | 4110 | if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 4111 | if (ObjCCategoryDecl *Cat |
| 4112 | = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) { |
| 4113 | ImplMethodsVsClassMethods(S, CatImplClass, Cat); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4114 | } |
| 4115 | } |
Alex Lorenz | a8c44ba | 2016-10-28 10:25:10 +0000 | [diff] [blame] | 4116 | } else if (const auto *IntfDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
| 4117 | if (const ObjCInterfaceDecl *Super = IntfDecl->getSuperClass()) { |
| 4118 | if (!IntfDecl->hasAttr<ObjCSubclassingRestrictedAttr>() && |
| 4119 | Super->hasAttr<ObjCSubclassingRestrictedAttr>()) { |
| 4120 | Diag(IntfDecl->getLocation(), diag::err_restricted_superclass_mismatch); |
| 4121 | Diag(Super->getLocation(), diag::note_class_declared); |
| 4122 | } |
| 4123 | } |
John McCall | 2c91c3b | 2019-05-30 04:09:01 +0000 | [diff] [blame] | 4124 | |
| 4125 | if (IntfDecl->hasAttr<ObjCClassStubAttr>() && |
| 4126 | !IntfDecl->hasAttr<ObjCSubclassingRestrictedAttr>()) |
| 4127 | Diag(IntfDecl->getLocation(), diag::err_class_stub_subclassing_mismatch); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4128 | } |
Volodymyr Sapsai | 30680e9 | 2017-10-23 22:01:41 +0000 | [diff] [blame] | 4129 | DiagnoseVariableSizedIvars(*this, OCD); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 4130 | if (isInterfaceDeclKind) { |
| 4131 | // Reject invalid vardecls. |
Fariborz Jahanian | 0080fb5 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 4132 | for (unsigned i = 0, e = allTUVars.size(); i != e; i++) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 4133 | DeclGroupRef DG = allTUVars[i].get(); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 4134 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 4135 | if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) { |
Daniel Dunbar | 0ca1660 | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 4136 | if (!VDecl->hasExternalStorage()) |
Steve Naroff | 42959b2 | 2009-04-13 17:58:46 +0000 | [diff] [blame] | 4137 | Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass); |
Fariborz Jahanian | 629aed9 | 2009-03-21 18:06:45 +0000 | [diff] [blame] | 4138 | } |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 4139 | } |
Fariborz Jahanian | 3654e65 | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 4140 | } |
Fariborz Jahanian | 4327b32 | 2011-08-29 17:33:12 +0000 | [diff] [blame] | 4141 | ActOnObjCContainerFinishDefinition(); |
Argyrios Kyrtzidis | bd8b150 | 2011-10-17 19:48:13 +0000 | [diff] [blame] | 4142 | |
Fariborz Jahanian | 0080fb5 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 4143 | for (unsigned i = 0, e = allTUVars.size(); i != e; i++) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 4144 | DeclGroupRef DG = allTUVars[i].get(); |
Argyrios Kyrtzidis | 8ad3bab | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 4145 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 4146 | (*I)->setTopLevelDeclInObjCContainer(); |
Argyrios Kyrtzidis | bd8b150 | 2011-10-17 19:48:13 +0000 | [diff] [blame] | 4147 | Consumer.HandleTopLevelDeclInObjCContainer(DG); |
| 4148 | } |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 4149 | |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 4150 | ActOnDocumentableDecl(ClassDecl); |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 4151 | return ClassDecl; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4154 | /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for |
| 4155 | /// 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] | 4156 | static Decl::ObjCDeclQualifier |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4157 | CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { |
John McCall | ca87290 | 2011-05-01 03:04:29 +0000 | [diff] [blame] | 4158 | return (Decl::ObjCDeclQualifier) (unsigned) PQTVal; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4159 | } |
| 4160 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4161 | /// Check whether the declared result type of the given Objective-C |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4162 | /// method declaration is compatible with the method's class. |
| 4163 | /// |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4164 | static Sema::ResultTypeCompatibilityKind |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4165 | CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method, |
| 4166 | ObjCInterfaceDecl *CurrentClass) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4167 | QualType ResultType = Method->getReturnType(); |
| 4168 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4169 | // If an Objective-C method inherits its related result type, then its |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4170 | // declared result type must be compatible with its own class type. The |
| 4171 | // declared result type is compatible if: |
| 4172 | if (const ObjCObjectPointerType *ResultObjectType |
| 4173 | = ResultType->getAs<ObjCObjectPointerType>()) { |
| 4174 | // - it is id or qualified id, or |
| 4175 | if (ResultObjectType->isObjCIdType() || |
| 4176 | ResultObjectType->isObjCQualifiedIdType()) |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4177 | return Sema::RTC_Compatible; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4178 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4179 | if (CurrentClass) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4180 | if (ObjCInterfaceDecl *ResultClass |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4181 | = ResultObjectType->getInterfaceDecl()) { |
| 4182 | // - it is the same as the method's class type, or |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 4183 | if (declaresSameEntity(CurrentClass, ResultClass)) |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4184 | return Sema::RTC_Compatible; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4185 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4186 | // - it is a superclass of the method's class type |
| 4187 | if (ResultClass->isSuperClassOf(CurrentClass)) |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4188 | return Sema::RTC_Compatible; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4189 | } |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4190 | } else { |
| 4191 | // Any Objective-C pointer type might be acceptable for a protocol |
| 4192 | // method; we just don't know. |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4193 | return Sema::RTC_Unknown; |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4194 | } |
| 4195 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4196 | |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4197 | return Sema::RTC_Incompatible; |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4198 | } |
| 4199 | |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4200 | namespace { |
| 4201 | /// A helper class for searching for methods which a particular method |
| 4202 | /// overrides. |
| 4203 | class OverrideSearch { |
Daniel Dunbar | d6d74c3 | 2012-02-29 03:04:05 +0000 | [diff] [blame] | 4204 | public: |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4205 | const ObjCMethodDecl *Method; |
Akira Hatanaka | 4c687f3 | 2018-02-06 23:44:40 +0000 | [diff] [blame] | 4206 | llvm::SmallSetVector<ObjCMethodDecl*, 4> Overridden; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4207 | bool Recursive; |
| 4208 | |
| 4209 | public: |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4210 | OverrideSearch(Sema &S, const ObjCMethodDecl *method) : Method(method) { |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4211 | Selector selector = method->getSelector(); |
| 4212 | |
| 4213 | // Bypass this search if we've never seen an instance/class method |
| 4214 | // with this selector before. |
| 4215 | Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector); |
| 4216 | if (it == S.MethodPool.end()) { |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 4217 | if (!S.getExternalSource()) return; |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 4218 | S.ReadMethodPool(selector); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4219 | |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 4220 | it = S.MethodPool.find(selector); |
| 4221 | if (it == S.MethodPool.end()) |
| 4222 | return; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4223 | } |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4224 | const ObjCMethodList &list = |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4225 | method->isInstanceMethod() ? it->second.first : it->second.second; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 4226 | if (!list.getMethod()) return; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4227 | |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4228 | const ObjCContainerDecl *container |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4229 | = cast<ObjCContainerDecl>(method->getDeclContext()); |
| 4230 | |
| 4231 | // Prevent the search from reaching this container again. This is |
| 4232 | // important with categories, which override methods from the |
| 4233 | // interface and each other. |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4234 | if (const ObjCCategoryDecl *Category = |
| 4235 | dyn_cast<ObjCCategoryDecl>(container)) { |
Douglas Gregor | cf4ac44 | 2012-05-03 21:25:24 +0000 | [diff] [blame] | 4236 | searchFromContainer(container); |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4237 | if (const ObjCInterfaceDecl *Interface = Category->getClassInterface()) |
Douglas Gregor | c5928af | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 4238 | searchFromContainer(Interface); |
Douglas Gregor | cf4ac44 | 2012-05-03 21:25:24 +0000 | [diff] [blame] | 4239 | } else { |
| 4240 | searchFromContainer(container); |
| 4241 | } |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4242 | } |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4243 | |
Akira Hatanaka | 4c687f3 | 2018-02-06 23:44:40 +0000 | [diff] [blame] | 4244 | typedef decltype(Overridden)::iterator iterator; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4245 | iterator begin() const { return Overridden.begin(); } |
| 4246 | iterator end() const { return Overridden.end(); } |
| 4247 | |
| 4248 | private: |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4249 | void searchFromContainer(const ObjCContainerDecl *container) { |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4250 | if (container->isInvalidDecl()) return; |
| 4251 | |
| 4252 | switch (container->getDeclKind()) { |
| 4253 | #define OBJCCONTAINER(type, base) \ |
| 4254 | case Decl::type: \ |
| 4255 | searchFrom(cast<type##Decl>(container)); \ |
| 4256 | break; |
| 4257 | #define ABSTRACT_DECL(expansion) |
| 4258 | #define DECL(type, base) \ |
| 4259 | case Decl::type: |
| 4260 | #include "clang/AST/DeclNodes.inc" |
| 4261 | llvm_unreachable("not an ObjC container!"); |
| 4262 | } |
| 4263 | } |
| 4264 | |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4265 | void searchFrom(const ObjCProtocolDecl *protocol) { |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 4266 | if (!protocol->hasDefinition()) |
| 4267 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4268 | |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4269 | // A method in a protocol declaration overrides declarations from |
| 4270 | // referenced ("parent") protocols. |
| 4271 | search(protocol->getReferencedProtocols()); |
| 4272 | } |
| 4273 | |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4274 | void searchFrom(const ObjCCategoryDecl *category) { |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4275 | // A method in a category declaration overrides declarations from |
| 4276 | // the main class and from protocols the category references. |
Douglas Gregor | cf4ac44 | 2012-05-03 21:25:24 +0000 | [diff] [blame] | 4277 | // The main class is handled in the constructor. |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4278 | search(category->getReferencedProtocols()); |
| 4279 | } |
| 4280 | |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4281 | void searchFrom(const ObjCCategoryImplDecl *impl) { |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4282 | // A method in a category definition that has a category |
| 4283 | // declaration overrides declarations from the category |
| 4284 | // declaration. |
| 4285 | if (ObjCCategoryDecl *category = impl->getCategoryDecl()) { |
| 4286 | search(category); |
Douglas Gregor | c5928af | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 4287 | if (ObjCInterfaceDecl *Interface = category->getClassInterface()) |
| 4288 | search(Interface); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4289 | |
| 4290 | // Otherwise it overrides declarations from the class. |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4291 | } else if (const auto *Interface = impl->getClassInterface()) { |
Douglas Gregor | c5928af | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 4292 | search(Interface); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4293 | } |
| 4294 | } |
| 4295 | |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4296 | void searchFrom(const ObjCInterfaceDecl *iface) { |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4297 | // A method in a class declaration overrides declarations from |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 4298 | if (!iface->hasDefinition()) |
| 4299 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4300 | |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4301 | // - categories, |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 4302 | for (auto *Cat : iface->known_categories()) |
| 4303 | search(Cat); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4304 | |
| 4305 | // - the super class, and |
| 4306 | if (ObjCInterfaceDecl *super = iface->getSuperClass()) |
| 4307 | search(super); |
| 4308 | |
| 4309 | // - any referenced protocols. |
| 4310 | search(iface->getReferencedProtocols()); |
| 4311 | } |
| 4312 | |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4313 | void searchFrom(const ObjCImplementationDecl *impl) { |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4314 | // A method in a class implementation overrides declarations from |
| 4315 | // the class interface. |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4316 | if (const auto *Interface = impl->getClassInterface()) |
Douglas Gregor | c5928af | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 4317 | search(Interface); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4318 | } |
| 4319 | |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4320 | void search(const ObjCProtocolList &protocols) { |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4321 | for (const auto *Proto : protocols) |
| 4322 | search(Proto); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4325 | void search(const ObjCContainerDecl *container) { |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4326 | // Check for a method in this container which matches this selector. |
| 4327 | ObjCMethodDecl *meth = container->getMethod(Method->getSelector(), |
Argyrios Kyrtzidis | bd8cd3e | 2013-03-29 21:51:48 +0000 | [diff] [blame] | 4328 | Method->isInstanceMethod(), |
| 4329 | /*AllowHidden=*/true); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4330 | |
| 4331 | // If we find one, record it and bail out. |
| 4332 | if (meth) { |
| 4333 | Overridden.insert(meth); |
| 4334 | return; |
| 4335 | } |
| 4336 | |
| 4337 | // Otherwise, search for methods that a hypothetical method here |
| 4338 | // would have overridden. |
| 4339 | |
| 4340 | // Note that we're now in a recursive case. |
| 4341 | Recursive = true; |
| 4342 | |
| 4343 | searchFromContainer(container); |
| 4344 | } |
| 4345 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 4346 | } // end anonymous namespace |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4347 | |
Pierre Habouzit | d4e1ba3 | 2019-11-07 23:14:58 -0800 | [diff] [blame^] | 4348 | void Sema::CheckObjCMethodDirectOverrides(ObjCMethodDecl *method, |
| 4349 | ObjCMethodDecl *overridden) { |
| 4350 | if (const auto *attr = overridden->getAttr<ObjCDirectAttr>()) { |
| 4351 | Diag(method->getLocation(), diag::err_objc_override_direct_method); |
| 4352 | Diag(attr->getLocation(), diag::note_previous_declaration); |
| 4353 | } else if (const auto *attr = method->getAttr<ObjCDirectAttr>()) { |
| 4354 | Diag(attr->getLocation(), diag::err_objc_direct_on_override) |
| 4355 | << isa<ObjCProtocolDecl>(overridden->getDeclContext()); |
| 4356 | Diag(overridden->getLocation(), diag::note_previous_declaration); |
| 4357 | } |
| 4358 | } |
| 4359 | |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4360 | void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod, |
| 4361 | ObjCInterfaceDecl *CurrentClass, |
| 4362 | ResultTypeCompatibilityKind RTC) { |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4363 | if (!ObjCMethod) |
| 4364 | return; |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4365 | // Search for overridden methods and merge information down from them. |
| 4366 | OverrideSearch overrides(*this, ObjCMethod); |
| 4367 | // Keep track if the method overrides any method in the class's base classes, |
| 4368 | // its protocols, or its categories' protocols; we will keep that info |
| 4369 | // in the ObjCMethodDecl. |
| 4370 | // For this info, a method in an implementation is not considered as |
| 4371 | // overriding the same method in the interface or its categories. |
| 4372 | bool hasOverriddenMethodsInBaseOrProtocol = false; |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4373 | for (ObjCMethodDecl *overridden : overrides) { |
Argyrios Kyrtzidis | c2091d5 | 2013-04-17 00:09:08 +0000 | [diff] [blame] | 4374 | if (!hasOverriddenMethodsInBaseOrProtocol) { |
| 4375 | if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) || |
| 4376 | CurrentClass != overridden->getClassInterface() || |
| 4377 | overridden->isOverriding()) { |
Pierre Habouzit | d4e1ba3 | 2019-11-07 23:14:58 -0800 | [diff] [blame^] | 4378 | CheckObjCMethodDirectOverrides(ObjCMethod, overridden); |
Argyrios Kyrtzidis | c2091d5 | 2013-04-17 00:09:08 +0000 | [diff] [blame] | 4379 | hasOverriddenMethodsInBaseOrProtocol = true; |
Argyrios Kyrtzidis | c2091d5 | 2013-04-17 00:09:08 +0000 | [diff] [blame] | 4380 | } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) { |
| 4381 | // OverrideSearch will return as "overridden" the same method in the |
| 4382 | // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to |
| 4383 | // check whether a category of a base class introduced a method with the |
| 4384 | // same selector, after the interface method declaration. |
| 4385 | // To avoid unnecessary lookups in the majority of cases, we use the |
| 4386 | // extra info bits in GlobalMethodPool to check whether there were any |
| 4387 | // category methods with this selector. |
| 4388 | GlobalMethodPool::iterator It = |
| 4389 | MethodPool.find(ObjCMethod->getSelector()); |
| 4390 | if (It != MethodPool.end()) { |
| 4391 | ObjCMethodList &List = |
| 4392 | ObjCMethod->isInstanceMethod()? It->second.first: It->second.second; |
| 4393 | unsigned CategCount = List.getBits(); |
| 4394 | if (CategCount > 0) { |
| 4395 | // If the method is in a category we'll do lookup if there were at |
| 4396 | // least 2 category methods recorded, otherwise only one will do. |
| 4397 | if (CategCount > 1 || |
| 4398 | !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) { |
| 4399 | OverrideSearch overrides(*this, overridden); |
Matt Davis | 55043e2 | 2019-04-22 16:04:44 +0000 | [diff] [blame] | 4400 | for (ObjCMethodDecl *SuperOverridden : overrides) { |
Argyrios Kyrtzidis | 04703a6 | 2013-04-27 00:10:12 +0000 | [diff] [blame] | 4401 | if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) || |
| 4402 | CurrentClass != SuperOverridden->getClassInterface()) { |
Pierre Habouzit | d4e1ba3 | 2019-11-07 23:14:58 -0800 | [diff] [blame^] | 4403 | CheckObjCMethodDirectOverrides(ObjCMethod, SuperOverridden); |
Argyrios Kyrtzidis | c2091d5 | 2013-04-17 00:09:08 +0000 | [diff] [blame] | 4404 | hasOverriddenMethodsInBaseOrProtocol = true; |
| 4405 | overridden->setOverriding(true); |
| 4406 | break; |
| 4407 | } |
| 4408 | } |
| 4409 | } |
| 4410 | } |
| 4411 | } |
| 4412 | } |
| 4413 | } |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4414 | |
| 4415 | // Propagate down the 'related result type' bit from overridden methods. |
| 4416 | if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType()) |
Erich Keane | 9b18eca | 2018-08-01 21:31:08 +0000 | [diff] [blame] | 4417 | ObjCMethod->setRelatedResultType(); |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4418 | |
| 4419 | // Then merge the declarations. |
| 4420 | mergeObjCMethodDecls(ObjCMethod, overridden); |
| 4421 | |
| 4422 | if (ObjCMethod->isImplicit() && overridden->isImplicit()) |
| 4423 | continue; // Conflicting properties are detected elsewhere. |
| 4424 | |
| 4425 | // Check for overriding methods |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4426 | if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) || |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4427 | isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) |
| 4428 | CheckConflictingOverridingMethod(ObjCMethod, overridden, |
| 4429 | isa<ObjCProtocolDecl>(overridden->getDeclContext())); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4430 | |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4431 | if (CurrentClass && overridden->getDeclContext() != CurrentClass && |
Fariborz Jahanian | 31a2568 | 2012-07-05 22:26:07 +0000 | [diff] [blame] | 4432 | isa<ObjCInterfaceDecl>(overridden->getDeclContext()) && |
| 4433 | !overridden->isImplicit() /* not meant for properties */) { |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4434 | ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(), |
| 4435 | E = ObjCMethod->param_end(); |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 4436 | ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(), |
| 4437 | PrevE = overridden->param_end(); |
| 4438 | for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) { |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4439 | assert(PrevI != overridden->param_end() && "Param mismatch"); |
| 4440 | QualType T1 = Context.getCanonicalType((*ParamI)->getType()); |
| 4441 | QualType T2 = Context.getCanonicalType((*PrevI)->getType()); |
| 4442 | // If type of argument of method in this class does not match its |
| 4443 | // respective argument type in the super class method, issue warning; |
| 4444 | if (!Context.typesAreCompatible(T1, T2)) { |
| 4445 | Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super) |
| 4446 | << T1 << T2; |
| 4447 | Diag(overridden->getLocation(), diag::note_previous_declaration); |
| 4448 | break; |
| 4449 | } |
| 4450 | } |
| 4451 | } |
| 4452 | } |
| 4453 | |
| 4454 | ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol); |
| 4455 | } |
| 4456 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4457 | /// Merge type nullability from for a redeclaration of the same entity, |
| 4458 | /// producing the updated type of the redeclared entity. |
| 4459 | static QualType mergeTypeNullabilityForRedecl(Sema &S, SourceLocation loc, |
| 4460 | QualType type, |
| 4461 | bool usesCSKeyword, |
| 4462 | SourceLocation prevLoc, |
| 4463 | QualType prevType, |
| 4464 | bool prevUsesCSKeyword) { |
| 4465 | // Determine the nullability of both types. |
| 4466 | auto nullability = type->getNullability(S.Context); |
| 4467 | auto prevNullability = prevType->getNullability(S.Context); |
| 4468 | |
| 4469 | // Easy case: both have nullability. |
| 4470 | if (nullability.hasValue() == prevNullability.hasValue()) { |
| 4471 | // Neither has nullability; continue. |
| 4472 | if (!nullability) |
| 4473 | return type; |
| 4474 | |
| 4475 | // The nullabilities are equivalent; do nothing. |
| 4476 | if (*nullability == *prevNullability) |
| 4477 | return type; |
| 4478 | |
| 4479 | // Complain about mismatched nullability. |
| 4480 | S.Diag(loc, diag::err_nullability_conflicting) |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 4481 | << DiagNullabilityKind(*nullability, usesCSKeyword) |
| 4482 | << DiagNullabilityKind(*prevNullability, prevUsesCSKeyword); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4483 | return type; |
| 4484 | } |
| 4485 | |
| 4486 | // If it's the redeclaration that has nullability, don't change anything. |
| 4487 | if (nullability) |
| 4488 | return type; |
| 4489 | |
| 4490 | // Otherwise, provide the result with the same nullability. |
| 4491 | return S.Context.getAttributedType( |
| 4492 | AttributedType::getNullabilityAttrKind(*prevNullability), |
| 4493 | type, type); |
| 4494 | } |
| 4495 | |
NAKAMURA Takumi | 2df5c3c | 2015-06-20 03:52:52 +0000 | [diff] [blame] | 4496 | /// Merge information from the declaration of a method in the \@interface |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4497 | /// (or a category/extension) into the corresponding method in the |
| 4498 | /// @implementation (for a class or category). |
| 4499 | static void mergeInterfaceMethodToImpl(Sema &S, |
| 4500 | ObjCMethodDecl *method, |
| 4501 | ObjCMethodDecl *prevMethod) { |
| 4502 | // Merge the objc_requires_super attribute. |
| 4503 | if (prevMethod->hasAttr<ObjCRequiresSuperAttr>() && |
| 4504 | !method->hasAttr<ObjCRequiresSuperAttr>()) { |
| 4505 | // merge the attribute into implementation. |
| 4506 | method->addAttr( |
| 4507 | ObjCRequiresSuperAttr::CreateImplicit(S.Context, |
| 4508 | method->getLocation())); |
| 4509 | } |
| 4510 | |
Pierre Habouzit | d4e1ba3 | 2019-11-07 23:14:58 -0800 | [diff] [blame^] | 4511 | if (!method->isDirectMethod()) |
| 4512 | if (const auto *attr = prevMethod->getAttr<ObjCDirectAttr>()) { |
| 4513 | method->addAttr( |
| 4514 | ObjCDirectAttr::CreateImplicit(S.Context, attr->getLocation())); |
| 4515 | } |
| 4516 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4517 | // Merge nullability of the result type. |
| 4518 | QualType newReturnType |
| 4519 | = mergeTypeNullabilityForRedecl( |
| 4520 | S, method->getReturnTypeSourceRange().getBegin(), |
| 4521 | method->getReturnType(), |
| 4522 | method->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability, |
| 4523 | prevMethod->getReturnTypeSourceRange().getBegin(), |
| 4524 | prevMethod->getReturnType(), |
| 4525 | prevMethod->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability); |
| 4526 | method->setReturnType(newReturnType); |
| 4527 | |
| 4528 | // Handle each of the parameters. |
| 4529 | unsigned numParams = method->param_size(); |
| 4530 | unsigned numPrevParams = prevMethod->param_size(); |
| 4531 | for (unsigned i = 0, n = std::min(numParams, numPrevParams); i != n; ++i) { |
| 4532 | ParmVarDecl *param = method->param_begin()[i]; |
| 4533 | ParmVarDecl *prevParam = prevMethod->param_begin()[i]; |
| 4534 | |
| 4535 | // Merge nullability. |
| 4536 | QualType newParamType |
| 4537 | = mergeTypeNullabilityForRedecl( |
| 4538 | S, param->getLocation(), param->getType(), |
| 4539 | param->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability, |
| 4540 | prevParam->getLocation(), prevParam->getType(), |
| 4541 | prevParam->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability); |
| 4542 | param->setType(newParamType); |
| 4543 | } |
| 4544 | } |
| 4545 | |
Alex Lorenz | a8a372d | 2017-04-27 10:43:48 +0000 | [diff] [blame] | 4546 | /// Verify that the method parameters/return value have types that are supported |
| 4547 | /// by the x86 target. |
| 4548 | static void checkObjCMethodX86VectorTypes(Sema &SemaRef, |
| 4549 | const ObjCMethodDecl *Method) { |
| 4550 | assert(SemaRef.getASTContext().getTargetInfo().getTriple().getArch() == |
| 4551 | llvm::Triple::x86 && |
| 4552 | "x86-specific check invoked for a different target"); |
| 4553 | SourceLocation Loc; |
| 4554 | QualType T; |
| 4555 | for (const ParmVarDecl *P : Method->parameters()) { |
| 4556 | if (P->getType()->isVectorType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4557 | Loc = P->getBeginLoc(); |
Alex Lorenz | a8a372d | 2017-04-27 10:43:48 +0000 | [diff] [blame] | 4558 | T = P->getType(); |
| 4559 | break; |
| 4560 | } |
| 4561 | } |
| 4562 | if (Loc.isInvalid()) { |
| 4563 | if (Method->getReturnType()->isVectorType()) { |
| 4564 | Loc = Method->getReturnTypeSourceRange().getBegin(); |
| 4565 | T = Method->getReturnType(); |
| 4566 | } else |
| 4567 | return; |
| 4568 | } |
| 4569 | |
| 4570 | // Vector parameters/return values are not supported by objc_msgSend on x86 in |
| 4571 | // iOS < 9 and macOS < 10.11. |
| 4572 | const auto &Triple = SemaRef.getASTContext().getTargetInfo().getTriple(); |
| 4573 | VersionTuple AcceptedInVersion; |
| 4574 | if (Triple.getOS() == llvm::Triple::IOS) |
| 4575 | AcceptedInVersion = VersionTuple(/*Major=*/9); |
| 4576 | else if (Triple.isMacOSX()) |
| 4577 | AcceptedInVersion = VersionTuple(/*Major=*/10, /*Minor=*/11); |
| 4578 | else |
| 4579 | return; |
Alex Lorenz | a8a372d | 2017-04-27 10:43:48 +0000 | [diff] [blame] | 4580 | if (SemaRef.getASTContext().getTargetInfo().getPlatformMinVersion() >= |
Alex Lorenz | 9282483 | 2017-05-05 16:15:17 +0000 | [diff] [blame] | 4581 | AcceptedInVersion) |
Alex Lorenz | a8a372d | 2017-04-27 10:43:48 +0000 | [diff] [blame] | 4582 | return; |
| 4583 | SemaRef.Diag(Loc, diag::err_objc_method_unsupported_param_ret_type) |
| 4584 | << T << (Method->getReturnType()->isVectorType() ? /*return value*/ 1 |
| 4585 | : /*parameter*/ 0) |
| 4586 | << (Triple.isMacOSX() ? "macOS 10.11" : "iOS 9"); |
| 4587 | } |
| 4588 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4589 | Decl *Sema::ActOnMethodDeclaration( |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4590 | Scope *S, SourceLocation MethodLoc, SourceLocation EndLoc, |
| 4591 | tok::TokenKind MethodType, ObjCDeclSpec &ReturnQT, ParsedType ReturnType, |
| 4592 | ArrayRef<SourceLocation> SelectorLocs, Selector Sel, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4593 | // optional arguments. The number of types/arguments is obtained |
| 4594 | // from the Sel.getNumArgs(). |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4595 | ObjCArgInfo *ArgInfo, DeclaratorChunk::ParamInfo *CParamInfo, |
| 4596 | unsigned CNumArgs, // c-style args |
| 4597 | const ParsedAttributesView &AttrList, tok::ObjCKeywordKind MethodDeclKind, |
Fariborz Jahanian | c677f69 | 2011-03-12 18:54:30 +0000 | [diff] [blame] | 4598 | bool isVariadic, bool MethodDefinition) { |
Steve Naroff | 83777fe | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 4599 | // Make sure we can establish a context for the method. |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4600 | if (!CurContext->isObjCContainer()) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 4601 | Diag(MethodLoc, diag::err_missing_method_context); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4602 | return nullptr; |
Steve Naroff | 83777fe | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 4603 | } |
Adrian Prantl | 2073dd2 | 2019-11-04 14:28:14 -0800 | [diff] [blame] | 4604 | |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 4605 | Decl *ClassDecl = cast<ObjCContainerDecl>(CurContext); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4606 | QualType resultDeclType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4607 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4608 | bool HasRelatedResultType = false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4609 | TypeSourceInfo *ReturnTInfo = nullptr; |
Steve Naroff | 3260641 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 4610 | if (ReturnType) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4611 | resultDeclType = GetTypeFromParser(ReturnType, &ReturnTInfo); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4612 | |
Eli Friedman | 31a5bcc | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 4613 | if (CheckFunctionReturnType(resultDeclType, MethodLoc)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4614 | return nullptr; |
Eli Friedman | 31a5bcc | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 4615 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4616 | QualType bareResultType = resultDeclType; |
| 4617 | (void)AttributedType::stripOuterNullability(bareResultType); |
| 4618 | HasRelatedResultType = (bareResultType == Context.getObjCInstanceType()); |
Fariborz Jahanian | b5a52ca | 2011-07-21 17:00:47 +0000 | [diff] [blame] | 4619 | } else { // get the type for "id". |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4620 | resultDeclType = Context.getObjCIdType(); |
Fariborz Jahanian | b21138f | 2011-07-21 17:38:14 +0000 | [diff] [blame] | 4621 | Diag(MethodLoc, diag::warn_missing_method_return_type) |
Argyrios Kyrtzidis | dfd6570 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 4622 | << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)"); |
Fariborz Jahanian | b5a52ca | 2011-07-21 17:00:47 +0000 | [diff] [blame] | 4623 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4624 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4625 | ObjCMethodDecl *ObjCMethod = ObjCMethodDecl::Create( |
| 4626 | Context, MethodLoc, EndLoc, Sel, resultDeclType, ReturnTInfo, CurContext, |
| 4627 | MethodType == tok::minus, isVariadic, |
Adrian Prantl | 2073dd2 | 2019-11-04 14:28:14 -0800 | [diff] [blame] | 4628 | /*isPropertyAccessor=*/false, /*isSynthesizedAccessorStub=*/false, |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4629 | /*isImplicitlyDeclared=*/false, /*isDefined=*/false, |
| 4630 | MethodDeclKind == tok::objc_optional ? ObjCMethodDecl::Optional |
| 4631 | : ObjCMethodDecl::Required, |
| 4632 | HasRelatedResultType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4633 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4634 | SmallVector<ParmVarDecl*, 16> Params; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4635 | |
Chris Lattner | 23b0faf | 2009-04-11 19:42:43 +0000 | [diff] [blame] | 4636 | for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) { |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 4637 | QualType ArgType; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4638 | TypeSourceInfo *DI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4639 | |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 4640 | if (!ArgInfo[i].Type) { |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 4641 | ArgType = Context.getObjCIdType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4642 | DI = nullptr; |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 4643 | } else { |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 4644 | ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI); |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 4645 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4646 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4647 | LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 4648 | LookupOrdinaryName, forRedeclarationInCurContext()); |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4649 | LookupName(R, S); |
| 4650 | if (R.isSingleResult()) { |
| 4651 | NamedDecl *PrevDecl = R.getFoundDecl(); |
| 4652 | if (S->isDeclScope(PrevDecl)) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4653 | Diag(ArgInfo[i].NameLoc, |
| 4654 | (MethodDefinition ? diag::warn_method_param_redefinition |
| 4655 | : diag::warn_method_param_declaration)) |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4656 | << ArgInfo[i].Name; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4657 | Diag(PrevDecl->getLocation(), |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4658 | diag::note_previous_declaration); |
| 4659 | } |
| 4660 | } |
| 4661 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4662 | SourceLocation StartLoc = DI |
| 4663 | ? DI->getTypeLoc().getBeginLoc() |
| 4664 | : ArgInfo[i].NameLoc; |
| 4665 | |
John McCall | d44f4d7 | 2011-04-23 02:46:06 +0000 | [diff] [blame] | 4666 | ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc, |
| 4667 | ArgInfo[i].NameLoc, ArgInfo[i].Name, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4668 | ArgType, DI, SC_None); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4669 | |
John McCall | 8249083 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 4670 | Param->setObjCMethodScopeInfo(i); |
| 4671 | |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 4672 | Param->setObjCDeclQualifier( |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 4673 | CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4674 | |
Chris Lattner | 9713a1c | 2009-04-11 19:34:56 +0000 | [diff] [blame] | 4675 | // Apply the attributes to the parameter. |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4676 | ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 4677 | AddPragmaAttributes(TUScope, Param); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4678 | |
Fariborz Jahanian | 52d02f6 | 2012-01-14 18:44:35 +0000 | [diff] [blame] | 4679 | if (Param->hasAttr<BlocksAttr>()) { |
| 4680 | Diag(Param->getLocation(), diag::err_block_on_nonlocal); |
| 4681 | Param->setInvalidDecl(); |
| 4682 | } |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4683 | S->AddDecl(Param); |
| 4684 | IdResolver.AddDecl(Param); |
| 4685 | |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 4686 | Params.push_back(Param); |
| 4687 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4688 | |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 4689 | for (unsigned i = 0, e = CNumArgs; i != e; ++i) { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4690 | ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param); |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 4691 | QualType ArgType = Param->getType(); |
| 4692 | if (ArgType.isNull()) |
| 4693 | ArgType = Context.getObjCIdType(); |
| 4694 | else |
| 4695 | // 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] | 4696 | ArgType = Context.getAdjustedParameterType(ArgType); |
Eli Friedman | 31a5bcc | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 4697 | |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 4698 | Param->setDeclContext(ObjCMethod); |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 4699 | Params.push_back(Param); |
| 4700 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4701 | |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4702 | ObjCMethod->setMethodParams(Context, Params, SelectorLocs); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4703 | ObjCMethod->setObjCDeclQualifier( |
| 4704 | CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 4705 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4706 | ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 4707 | AddPragmaAttributes(TUScope, ObjCMethod); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4708 | |
Douglas Gregor | 87e9275 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 4709 | // Add the method now. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4710 | const ObjCMethodDecl *PrevMethod = nullptr; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4711 | if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4712 | if (MethodType == tok::minus) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4713 | PrevMethod = ImpDecl->getInstanceMethod(Sel); |
| 4714 | ImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4715 | } else { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4716 | PrevMethod = ImpDecl->getClassMethod(Sel); |
| 4717 | ImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4718 | } |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4719 | |
Adrian Prantl | 2073dd2 | 2019-11-04 14:28:14 -0800 | [diff] [blame] | 4720 | // If this method overrides a previous @synthesize declaration, |
| 4721 | // register it with the property. Linear search through all |
| 4722 | // properties here, because the autosynthesized stub hasn't been |
| 4723 | // made visible yet, so it can be overriden by a later |
| 4724 | // user-specified implementation. |
| 4725 | for (ObjCPropertyImplDecl *PropertyImpl : ImpDecl->property_impls()) { |
| 4726 | if (auto *Setter = PropertyImpl->getSetterMethodDecl()) |
| 4727 | if (Setter->getSelector() == Sel && |
| 4728 | Setter->isInstanceMethod() == ObjCMethod->isInstanceMethod()) { |
| 4729 | assert(Setter->isSynthesizedAccessorStub() && "autosynth stub expected"); |
| 4730 | PropertyImpl->setSetterMethodDecl(ObjCMethod); |
| 4731 | } |
| 4732 | if (auto *Getter = PropertyImpl->getGetterMethodDecl()) |
| 4733 | if (Getter->getSelector() == Sel && |
| 4734 | Getter->isInstanceMethod() == ObjCMethod->isInstanceMethod()) { |
| 4735 | assert(Getter->isSynthesizedAccessorStub() && "autosynth stub expected"); |
| 4736 | PropertyImpl->setGetterMethodDecl(ObjCMethod); |
| 4737 | break; |
| 4738 | } |
| 4739 | } |
| 4740 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4741 | // Merge information from the @interface declaration into the |
| 4742 | // @implementation. |
| 4743 | if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface()) { |
| 4744 | if (auto *IMD = IDecl->lookupMethod(ObjCMethod->getSelector(), |
| 4745 | ObjCMethod->isInstanceMethod())) { |
| 4746 | mergeInterfaceMethodToImpl(*this, ObjCMethod, IMD); |
Pierre Habouzit | d4e1ba3 | 2019-11-07 23:14:58 -0800 | [diff] [blame^] | 4747 | if (const auto *attr = ObjCMethod->getAttr<ObjCDirectAttr>()) { |
| 4748 | if (!IMD->isDirectMethod()) { |
| 4749 | Diag(attr->getLocation(), diag::err_objc_direct_missing_on_decl); |
| 4750 | Diag(IMD->getLocation(), diag::note_previous_declaration); |
| 4751 | } |
| 4752 | } |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4753 | |
| 4754 | // Warn about defining -dealloc in a category. |
| 4755 | if (isa<ObjCCategoryImplDecl>(ImpDecl) && IMD->isOverriding() && |
| 4756 | ObjCMethod->getSelector().getMethodFamily() == OMF_dealloc) { |
| 4757 | Diag(ObjCMethod->getLocation(), diag::warn_dealloc_in_category) |
| 4758 | << ObjCMethod->getDeclName(); |
| 4759 | } |
Pierre Habouzit | d4e1ba3 | 2019-11-07 23:14:58 -0800 | [diff] [blame^] | 4760 | } else if (ImpDecl->hasAttr<ObjCDirectMembersAttr>()) { |
| 4761 | ObjCMethod->addAttr( |
| 4762 | ObjCDirectAttr::CreateImplicit(Context, ObjCMethod->getLocation())); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4763 | } |
Akira Hatanaka | a6b5e00 | 2018-07-28 04:06:13 +0000 | [diff] [blame] | 4764 | |
| 4765 | // Warn if a method declared in a protocol to which a category or |
| 4766 | // extension conforms is non-escaping and the implementation's method is |
| 4767 | // escaping. |
| 4768 | for (auto *C : IDecl->visible_categories()) |
| 4769 | for (auto &P : C->protocols()) |
| 4770 | if (auto *IMD = P->lookupMethod(ObjCMethod->getSelector(), |
| 4771 | ObjCMethod->isInstanceMethod())) { |
| 4772 | assert(ObjCMethod->parameters().size() == |
| 4773 | IMD->parameters().size() && |
| 4774 | "Methods have different number of parameters"); |
| 4775 | auto OI = IMD->param_begin(), OE = IMD->param_end(); |
| 4776 | auto NI = ObjCMethod->param_begin(); |
| 4777 | for (; OI != OE; ++OI, ++NI) |
| 4778 | diagnoseNoescape(*NI, *OI, C, P, *this); |
| 4779 | } |
Fariborz Jahanian | 7e350d2 | 2013-12-17 22:44:28 +0000 | [diff] [blame] | 4780 | } |
Douglas Gregor | 87e9275 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 4781 | } else { |
Pierre Habouzit | d4e1ba3 | 2019-11-07 23:14:58 -0800 | [diff] [blame^] | 4782 | if (!ObjCMethod->isDirectMethod() && |
| 4783 | ClassDecl->hasAttr<ObjCDirectMembersAttr>()) { |
| 4784 | ObjCMethod->addAttr( |
| 4785 | ObjCDirectAttr::CreateImplicit(Context, ObjCMethod->getLocation())); |
| 4786 | } |
Douglas Gregor | 87e9275 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 4787 | cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4788 | } |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4789 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4790 | if (PrevMethod) { |
| 4791 | // You can never have two method definitions with the same name. |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 4792 | Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4793 | << ObjCMethod->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 4794 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Fariborz Jahanian | 096f7c1 | 2013-05-13 17:27:00 +0000 | [diff] [blame] | 4795 | ObjCMethod->setInvalidDecl(); |
| 4796 | return ObjCMethod; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4797 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4798 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4799 | // If this Objective-C method does not have a related result type, but we |
| 4800 | // are allowed to infer related result types, try to do so based on the |
| 4801 | // method family. |
| 4802 | ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl); |
| 4803 | if (!CurrentClass) { |
| 4804 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) |
| 4805 | CurrentClass = Cat->getClassInterface(); |
| 4806 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl)) |
| 4807 | CurrentClass = Impl->getClassInterface(); |
| 4808 | else if (ObjCCategoryImplDecl *CatImpl |
| 4809 | = dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) |
| 4810 | CurrentClass = CatImpl->getClassInterface(); |
| 4811 | } |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4812 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4813 | ResultTypeCompatibilityKind RTC |
| 4814 | = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4815 | |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4816 | CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4817 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4818 | bool ARCError = false; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4819 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | e48f389 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 4820 | ARCError = CheckARCMethodDecl(ObjCMethod); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4821 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4822 | // Infer the related result type when possible. |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4823 | if (!ARCError && RTC == Sema::RTC_Compatible && |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4824 | !ObjCMethod->hasRelatedResultType() && |
| 4825 | LangOpts.ObjCInferRelatedResultType) { |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4826 | bool InferRelatedResultType = false; |
| 4827 | switch (ObjCMethod->getMethodFamily()) { |
| 4828 | case OMF_None: |
| 4829 | case OMF_copy: |
| 4830 | case OMF_dealloc: |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 4831 | case OMF_finalize: |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4832 | case OMF_mutableCopy: |
| 4833 | case OMF_release: |
| 4834 | case OMF_retainCount: |
Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 4835 | case OMF_initialize: |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 4836 | case OMF_performSelector: |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4837 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4838 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4839 | case OMF_alloc: |
| 4840 | case OMF_new: |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 4841 | InferRelatedResultType = ObjCMethod->isClassMethod(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4842 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4843 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4844 | case OMF_init: |
| 4845 | case OMF_autorelease: |
| 4846 | case OMF_retain: |
| 4847 | case OMF_self: |
| 4848 | InferRelatedResultType = ObjCMethod->isInstanceMethod(); |
| 4849 | break; |
| 4850 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4851 | |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 4852 | if (InferRelatedResultType && |
| 4853 | !ObjCMethod->getReturnType()->isObjCIndependentClassType()) |
Erich Keane | 9b18eca | 2018-08-01 21:31:08 +0000 | [diff] [blame] | 4854 | ObjCMethod->setRelatedResultType(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4855 | } |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 4856 | |
Alex Lorenz | a8a372d | 2017-04-27 10:43:48 +0000 | [diff] [blame] | 4857 | if (MethodDefinition && |
| 4858 | Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86) |
| 4859 | checkObjCMethodX86VectorTypes(*this, ObjCMethod); |
| 4860 | |
Steven Wu | 3bb4aa5 | 2018-04-16 23:34:18 +0000 | [diff] [blame] | 4861 | // + load method cannot have availability attributes. It get called on |
| 4862 | // startup, so it has to have the availability of the deployment target. |
| 4863 | if (const auto *attr = ObjCMethod->getAttr<AvailabilityAttr>()) { |
| 4864 | if (ObjCMethod->isClassMethod() && |
| 4865 | ObjCMethod->getSelector().getAsString() == "load") { |
| 4866 | Diag(attr->getLocation(), diag::warn_availability_on_static_initializer) |
| 4867 | << 0; |
| 4868 | ObjCMethod->dropAttr<AvailabilityAttr>(); |
| 4869 | } |
| 4870 | } |
| 4871 | |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 4872 | ActOnDocumentableDecl(ObjCMethod); |
| 4873 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4874 | return ObjCMethod; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4875 | } |
| 4876 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4877 | bool Sema::CheckObjCDeclScope(Decl *D) { |
Fariborz Jahanian | f36734d | 2011-08-22 18:34:22 +0000 | [diff] [blame] | 4878 | // Following is also an error. But it is caused by a missing @end |
| 4879 | // and diagnostic is issued elsewhere. |
Argyrios Kyrtzidis | 822c433 | 2012-03-23 23:24:23 +0000 | [diff] [blame] | 4880 | if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4881 | return false; |
Argyrios Kyrtzidis | 822c433 | 2012-03-23 23:24:23 +0000 | [diff] [blame] | 4882 | |
| 4883 | // If we switched context to translation unit while we are still lexically in |
| 4884 | // an objc container, it means the parser missed emitting an error. |
| 4885 | if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext())) |
| 4886 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4887 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 4888 | Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); |
| 4889 | D->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4890 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 4891 | return true; |
| 4892 | } |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4893 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4894 | /// Called whenever \@defs(ClassName) is encountered in the source. Inserts the |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4895 | /// instance variables of ClassName into Decls. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4896 | void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart, |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4897 | IdentifierInfo *ClassName, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4898 | SmallVectorImpl<Decl*> &Decls) { |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4899 | // Check that ClassName is a valid class |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4900 | ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4901 | if (!Class) { |
| 4902 | Diag(DeclStart, diag::err_undef_interface) << ClassName; |
| 4903 | return; |
| 4904 | } |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 4905 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | ece1b2b | 2009-04-21 20:28:41 +0000 | [diff] [blame] | 4906 | Diag(DeclStart, diag::err_atdef_nonfragile_interface); |
| 4907 | return; |
| 4908 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4909 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4910 | // Collect the instance variables |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4911 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4912 | Context.DeepCollectObjCIvars(Class, true, Ivars); |
Fariborz Jahanian | 7dae114 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 4913 | // For each ivar, create a fresh ObjCAtDefsFieldDecl. |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4914 | for (unsigned i = 0; i < Ivars.size(); i++) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 4915 | const FieldDecl* ID = Ivars[i]; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4916 | RecordDecl *Record = dyn_cast<RecordDecl>(TagD); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4917 | Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, |
| 4918 | /*FIXME: StartL=*/ID->getLocation(), |
| 4919 | ID->getLocation(), |
Fariborz Jahanian | 7dae114 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 4920 | ID->getIdentifier(), ID->getType(), |
| 4921 | ID->getBitWidth()); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4922 | Decls.push_back(FD); |
Fariborz Jahanian | 7dae114 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 4923 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4924 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4925 | // Introduce all of these fields into the appropriate scope. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4926 | for (SmallVectorImpl<Decl*>::iterator D = Decls.begin(); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4927 | D != Decls.end(); ++D) { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4928 | FieldDecl *FD = cast<FieldDecl>(*D); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4929 | if (getLangOpts().CPlusPlus) |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 4930 | PushOnScopeChains(FD, S); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4931 | else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD)) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4932 | Record->addDecl(FD); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4933 | } |
| 4934 | } |
| 4935 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4936 | /// Build a type-check a new Objective-C exception variable declaration. |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4937 | VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T, |
| 4938 | SourceLocation StartLoc, |
| 4939 | SourceLocation IdLoc, |
| 4940 | IdentifierInfo *Id, |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4941 | bool Invalid) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4942 | // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4943 | // duration shall not be qualified by an address-space qualifier." |
| 4944 | // Since all parameters have automatic store duration, they can not have |
| 4945 | // an address space. |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 4946 | if (T.getAddressSpace() != LangAS::Default) { |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4947 | Diag(IdLoc, diag::err_arg_with_address_space); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4948 | Invalid = true; |
| 4949 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4950 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4951 | // An @catch parameter must be an unqualified object pointer type; |
| 4952 | // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"? |
| 4953 | if (Invalid) { |
| 4954 | // Don't do any further checking. |
Douglas Gregor | f4e837f | 2010-04-26 17:57:08 +0000 | [diff] [blame] | 4955 | } else if (T->isDependentType()) { |
| 4956 | // Okay: we don't know what this type will instantiate to. |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4957 | } else if (T->isObjCQualifiedIdType()) { |
| 4958 | Invalid = true; |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4959 | Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm); |
Saleem Abdulrasool | 278e1c4 | 2018-05-20 19:26:44 +0000 | [diff] [blame] | 4960 | } else if (T->isObjCIdType()) { |
| 4961 | // Okay: we don't know what this type will instantiate to. |
| 4962 | } else if (!T->isObjCObjectPointerType()) { |
| 4963 | Invalid = true; |
| 4964 | Diag(IdLoc, diag::err_catch_param_not_objc_type); |
Simon Pilgrim | a867cca | 2019-10-17 10:35:29 +0000 | [diff] [blame] | 4965 | } else if (!T->castAs<ObjCObjectPointerType>()->getInterfaceType()) { |
Saleem Abdulrasool | 278e1c4 | 2018-05-20 19:26:44 +0000 | [diff] [blame] | 4966 | Invalid = true; |
| 4967 | Diag(IdLoc, diag::err_catch_param_not_objc_type); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4968 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4969 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4970 | VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4971 | T, TInfo, SC_None); |
Douglas Gregor | 3f324d56 | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 4972 | New->setExceptionVariable(true); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4973 | |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 4974 | // In ARC, infer 'retaining' for variables of retainable type. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4975 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New)) |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 4976 | Invalid = true; |
| 4977 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4978 | if (Invalid) |
| 4979 | New->setInvalidDecl(); |
| 4980 | return New; |
| 4981 | } |
| 4982 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4983 | Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) { |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4984 | const DeclSpec &DS = D.getDeclSpec(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4985 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4986 | // We allow the "register" storage class on exception variables because |
| 4987 | // GCC did, but we drop it completely. Any other storage class is an error. |
| 4988 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
| 4989 | Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm) |
| 4990 | << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc())); |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4991 | } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) { |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4992 | Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm) |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4993 | << DeclSpec::getSpecifierName(SCS); |
| 4994 | } |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4995 | if (DS.isInlineSpecified()) |
| 4996 | Diag(DS.getInlineSpecLoc(), diag::err_inline_non_function) |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4997 | << getLangOpts().CPlusPlus17; |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4998 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
| 4999 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 5000 | diag::err_invalid_thread) |
| 5001 | << DeclSpec::getSpecifierName(TSCS); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 5002 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 5003 | |
Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 5004 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5005 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 5006 | // Check that there are no default arguments inside the type of this |
| 5007 | // exception object (C++ only). |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5008 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 5009 | CheckExtraCXXDefaultArguments(D); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5010 | |
Argyrios Kyrtzidis | ef7022f | 2011-06-28 03:01:15 +0000 | [diff] [blame] | 5011 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 5012 | QualType ExceptionType = TInfo->getType(); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 5013 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5014 | VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, |
| 5015 | D.getSourceRange().getBegin(), |
| 5016 | D.getIdentifierLoc(), |
| 5017 | D.getIdentifier(), |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 5018 | D.isInvalidType()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5019 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 5020 | // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1). |
| 5021 | if (D.getCXXScopeSpec().isSet()) { |
| 5022 | Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm) |
| 5023 | << D.getCXXScopeSpec().getRange(); |
| 5024 | New->setInvalidDecl(); |
| 5025 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5026 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 5027 | // Add the parameter declaration into this scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5028 | S->AddDecl(New); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 5029 | if (D.getIdentifier()) |
| 5030 | IdResolver.AddDecl(New); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5031 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 5032 | ProcessDeclAttributes(S, New, D); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5033 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 5034 | if (New->hasAttr<BlocksAttr>()) |
| 5035 | Diag(New->getLocation(), diag::err_block_on_nonlocal); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5036 | return New; |
Douglas Gregor | e11ee11 | 2010-04-23 23:01:43 +0000 | [diff] [blame] | 5037 | } |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 5038 | |
| 5039 | /// CollectIvarsToConstructOrDestruct - Collect those ivars which require |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 5040 | /// initialization. |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 5041 | void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5042 | SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5043 | for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 5044 | Iv= Iv->getNextIvar()) { |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 5045 | QualType QT = Context.getBaseElementType(Iv->getType()); |
Douglas Gregor | 527786e | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 5046 | if (QT->isRecordType()) |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 5047 | Ivars.push_back(Iv); |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 5048 | } |
| 5049 | } |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 5050 | |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 5051 | void Sema::DiagnoseUseOfUnimplementedSelectors() { |
Douglas Gregor | 72e357f | 2011-07-28 14:54:22 +0000 | [diff] [blame] | 5052 | // Load referenced selectors from the external source. |
| 5053 | if (ExternalSource) { |
| 5054 | SmallVector<std::pair<Selector, SourceLocation>, 4> Sels; |
| 5055 | ExternalSource->ReadReferencedSelectors(Sels); |
| 5056 | for (unsigned I = 0, N = Sels.size(); I != N; ++I) |
| 5057 | ReferencedSelectors[Sels[I].first] = Sels[I].second; |
| 5058 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5059 | |
Fariborz Jahanian | c9b7c20 | 2011-02-04 23:19:27 +0000 | [diff] [blame] | 5060 | // Warning will be issued only when selector table is |
| 5061 | // generated (which means there is at lease one implementation |
| 5062 | // in the TU). This is to match gcc's behavior. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5063 | if (ReferencedSelectors.empty() || |
Fariborz Jahanian | c9b7c20 | 2011-02-04 23:19:27 +0000 | [diff] [blame] | 5064 | !Context.AnyObjCImplementation()) |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 5065 | return; |
Chandler Carruth | 12c8f65 | 2015-03-27 00:55:05 +0000 | [diff] [blame] | 5066 | for (auto &SelectorAndLocation : ReferencedSelectors) { |
| 5067 | Selector Sel = SelectorAndLocation.first; |
| 5068 | SourceLocation Loc = SelectorAndLocation.second; |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 5069 | if (!LookupImplementedMethodInGlobalPool(Sel)) |
Chandler Carruth | 12c8f65 | 2015-03-27 00:55:05 +0000 | [diff] [blame] | 5070 | Diag(Loc, diag::warn_unimplemented_selector) << Sel; |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 5071 | } |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 5072 | } |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 5073 | |
| 5074 | ObjCIvarDecl * |
| 5075 | Sema::GetIvarBackingPropertyAccessor(const ObjCMethodDecl *Method, |
| 5076 | const ObjCPropertyDecl *&PDecl) const { |
Fariborz Jahanian | 1cc7ae1 | 2014-01-02 17:24:32 +0000 | [diff] [blame] | 5077 | if (Method->isClassMethod()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5078 | return nullptr; |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 5079 | const ObjCInterfaceDecl *IDecl = Method->getClassInterface(); |
| 5080 | if (!IDecl) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5081 | return nullptr; |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 5082 | Method = IDecl->lookupMethod(Method->getSelector(), /*isInstance=*/true, |
| 5083 | /*shallowCategoryLookup=*/false, |
| 5084 | /*followSuper=*/false); |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 5085 | if (!Method || !Method->isPropertyAccessor()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5086 | return nullptr; |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 5087 | if ((PDecl = Method->findPropertyDecl())) |
Fariborz Jahanian | 122d94f | 2014-01-27 22:27:43 +0000 | [diff] [blame] | 5088 | if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) { |
| 5089 | // property backing ivar must belong to property's class |
| 5090 | // or be a private ivar in class's implementation. |
| 5091 | // FIXME. fix the const-ness issue. |
| 5092 | IV = const_cast<ObjCInterfaceDecl *>(IDecl)->lookupInstanceVariable( |
| 5093 | IV->getIdentifier()); |
| 5094 | return IV; |
| 5095 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5096 | return nullptr; |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 5097 | } |
| 5098 | |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 5099 | namespace { |
| 5100 | /// Used by Sema::DiagnoseUnusedBackingIvarInAccessor to check if a property |
| 5101 | /// accessor references the backing ivar. |
Argyrios Kyrtzidis | 98045c1 | 2014-01-03 19:53:09 +0000 | [diff] [blame] | 5102 | class UnusedBackingIvarChecker : |
Richard Smith | 5066845 | 2015-11-24 03:55:01 +0000 | [diff] [blame] | 5103 | public RecursiveASTVisitor<UnusedBackingIvarChecker> { |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 5104 | public: |
| 5105 | Sema &S; |
| 5106 | const ObjCMethodDecl *Method; |
| 5107 | const ObjCIvarDecl *IvarD; |
| 5108 | bool AccessedIvar; |
| 5109 | bool InvokedSelfMethod; |
| 5110 | |
| 5111 | UnusedBackingIvarChecker(Sema &S, const ObjCMethodDecl *Method, |
| 5112 | const ObjCIvarDecl *IvarD) |
| 5113 | : S(S), Method(Method), IvarD(IvarD), |
| 5114 | AccessedIvar(false), InvokedSelfMethod(false) { |
| 5115 | assert(IvarD); |
| 5116 | } |
| 5117 | |
| 5118 | bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { |
| 5119 | if (E->getDecl() == IvarD) { |
| 5120 | AccessedIvar = true; |
| 5121 | return false; |
| 5122 | } |
| 5123 | return true; |
| 5124 | } |
| 5125 | |
| 5126 | bool VisitObjCMessageExpr(ObjCMessageExpr *E) { |
| 5127 | if (E->getReceiverKind() == ObjCMessageExpr::Instance && |
| 5128 | S.isSelfExpr(E->getInstanceReceiver(), Method)) { |
| 5129 | InvokedSelfMethod = true; |
| 5130 | } |
| 5131 | return true; |
| 5132 | } |
| 5133 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 5134 | } // end anonymous namespace |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 5135 | |
| 5136 | void Sema::DiagnoseUnusedBackingIvarInAccessor(Scope *S, |
| 5137 | const ObjCImplementationDecl *ImplD) { |
| 5138 | if (S->hasUnrecoverableErrorOccurred()) |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 5139 | return; |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 5140 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 5141 | for (const auto *CurMethod : ImplD->instance_methods()) { |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 5142 | unsigned DIAG = diag::warn_unused_property_backing_ivar; |
| 5143 | SourceLocation Loc = CurMethod->getLocation(); |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 5144 | if (Diags.isIgnored(DIAG, Loc)) |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 5145 | continue; |
| 5146 | |
| 5147 | const ObjCPropertyDecl *PDecl; |
| 5148 | const ObjCIvarDecl *IV = GetIvarBackingPropertyAccessor(CurMethod, PDecl); |
| 5149 | if (!IV) |
| 5150 | continue; |
| 5151 | |
Adrian Prantl | 2073dd2 | 2019-11-04 14:28:14 -0800 | [diff] [blame] | 5152 | if (CurMethod->isSynthesizedAccessorStub()) |
| 5153 | continue; |
| 5154 | |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 5155 | UnusedBackingIvarChecker Checker(*this, CurMethod, IV); |
| 5156 | Checker.TraverseStmt(CurMethod->getBody()); |
| 5157 | if (Checker.AccessedIvar) |
| 5158 | continue; |
| 5159 | |
Fariborz Jahanian | 5b3105d | 2014-01-02 22:42:09 +0000 | [diff] [blame] | 5160 | // 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] | 5161 | // implementation makes a self call. This is to prevent false positive in |
| 5162 | // cases where the ivar is accessed by another method that the accessor |
| 5163 | // delegates to. |
| 5164 | if (!IV->isReferenced() || !Checker.InvokedSelfMethod) { |
Argyrios Kyrtzidis | d8a3532 | 2014-01-03 19:39:23 +0000 | [diff] [blame] | 5165 | Diag(Loc, DIAG) << IV; |
Fariborz Jahanian | 5b3105d | 2014-01-02 22:42:09 +0000 | [diff] [blame] | 5166 | Diag(PDecl->getLocation(), diag::note_property_declare); |
| 5167 | } |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 5168 | } |
| 5169 | } |