Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1 | //===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for Objective C declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 14 | #include "TypeLocBuilder.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/ASTMutationListener.h" |
| 18 | #include "clang/AST/DeclObjC.h" |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprObjC.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 21 | #include "clang/AST/RecursiveASTVisitor.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Sema/DeclSpec.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Sema/Lookup.h" |
| 25 | #include "clang/Sema/Scope.h" |
| 26 | #include "clang/Sema/ScopeInfo.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 27 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseMap.h" |
John McCall | a1e130b | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseSet.h" |
| 30 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 33 | /// Check whether the given method, which must be in the 'init' |
| 34 | /// family, is a valid member of that family. |
| 35 | /// |
| 36 | /// \param receiverTypeIfCall - if null, check this as if declaring it; |
| 37 | /// if non-null, check this as if making a call to it with the given |
| 38 | /// receiver type |
| 39 | /// |
| 40 | /// \return true to indicate that there was an error and appropriate |
| 41 | /// actions were taken |
| 42 | bool Sema::checkInitMethod(ObjCMethodDecl *method, |
| 43 | QualType receiverTypeIfCall) { |
| 44 | if (method->isInvalidDecl()) return true; |
| 45 | |
| 46 | // This castAs is safe: methods that don't return an object |
| 47 | // pointer won't be inferred as inits and will reject an explicit |
| 48 | // objc_method_family(init). |
| 49 | |
| 50 | // We ignore protocols here. Should we? What about Class? |
| 51 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 52 | const ObjCObjectType *result = |
| 53 | method->getReturnType()->castAs<ObjCObjectPointerType>()->getObjectType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 54 | |
| 55 | if (result->isObjCId()) { |
| 56 | return false; |
| 57 | } else if (result->isObjCClass()) { |
| 58 | // fall through: always an error |
| 59 | } else { |
| 60 | ObjCInterfaceDecl *resultClass = result->getInterface(); |
| 61 | assert(resultClass && "unexpected object type!"); |
| 62 | |
| 63 | // It's okay for the result type to still be a forward declaration |
| 64 | // if we're checking an interface declaration. |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 65 | if (!resultClass->hasDefinition()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 66 | if (receiverTypeIfCall.isNull() && |
| 67 | !isa<ObjCImplementationDecl>(method->getDeclContext())) |
| 68 | return false; |
| 69 | |
| 70 | // Otherwise, we try to compare class types. |
| 71 | } else { |
| 72 | // If this method was declared in a protocol, we can't check |
| 73 | // anything unless we have a receiver type that's an interface. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 74 | const ObjCInterfaceDecl *receiverClass = nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 75 | if (isa<ObjCProtocolDecl>(method->getDeclContext())) { |
| 76 | if (receiverTypeIfCall.isNull()) |
| 77 | return false; |
| 78 | |
| 79 | receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>() |
| 80 | ->getInterfaceDecl(); |
| 81 | |
| 82 | // This can be null for calls to e.g. id<Foo>. |
| 83 | if (!receiverClass) return false; |
| 84 | } else { |
| 85 | receiverClass = method->getClassInterface(); |
| 86 | assert(receiverClass && "method not associated with a class!"); |
| 87 | } |
| 88 | |
| 89 | // If either class is a subclass of the other, it's fine. |
| 90 | if (receiverClass->isSuperClassOf(resultClass) || |
| 91 | resultClass->isSuperClassOf(receiverClass)) |
| 92 | return false; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | SourceLocation loc = method->getLocation(); |
| 97 | |
| 98 | // If we're in a system header, and this is not a call, just make |
| 99 | // the method unusable. |
| 100 | if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 101 | method->addAttr(UnavailableAttr::CreateImplicit(Context, "", |
| 102 | UnavailableAttr::IR_ARCInitReturnsUnrelated, loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
| 106 | // Otherwise, it's an error. |
| 107 | Diag(loc, diag::err_arc_init_method_unrelated_result_type); |
| 108 | method->setInvalidDecl(); |
| 109 | return true; |
| 110 | } |
| 111 | |
Akira Hatanaka | a6b5e00 | 2018-07-28 04:06:13 +0000 | [diff] [blame] | 112 | /// Issue a warning if the parameter of the overridden method is non-escaping |
| 113 | /// but the parameter of the overriding method is not. |
| 114 | static bool diagnoseNoescape(const ParmVarDecl *NewD, const ParmVarDecl *OldD, |
| 115 | Sema &S) { |
| 116 | if (OldD->hasAttr<NoEscapeAttr>() && !NewD->hasAttr<NoEscapeAttr>()) { |
| 117 | S.Diag(NewD->getLocation(), diag::warn_overriding_method_missing_noescape); |
| 118 | S.Diag(OldD->getLocation(), diag::note_overridden_marked_noescape); |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | /// Produce additional diagnostics if a category conforms to a protocol that |
| 126 | /// defines a method taking a non-escaping parameter. |
| 127 | static void diagnoseNoescape(const ParmVarDecl *NewD, const ParmVarDecl *OldD, |
| 128 | const ObjCCategoryDecl *CD, |
| 129 | const ObjCProtocolDecl *PD, Sema &S) { |
| 130 | if (!diagnoseNoescape(NewD, OldD, S)) |
| 131 | S.Diag(CD->getLocation(), diag::note_cat_conform_to_noescape_prot) |
| 132 | << CD->IsClassExtension() << PD |
| 133 | << cast<ObjCMethodDecl>(NewD->getDeclContext()); |
| 134 | } |
| 135 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 136 | void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 137 | const ObjCMethodDecl *Overridden) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 138 | if (Overridden->hasRelatedResultType() && |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 139 | !NewMethod->hasRelatedResultType()) { |
| 140 | // This can only happen when the method follows a naming convention that |
| 141 | // implies a related result type, and the original (overridden) method has |
| 142 | // a suitable return type, but the new (overriding) method does not have |
| 143 | // a suitable return type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 144 | QualType ResultType = NewMethod->getReturnType(); |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 145 | SourceRange ResultTypeRange = NewMethod->getReturnTypeSourceRange(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 146 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 147 | // Figure out which class this method is part of, if any. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 148 | ObjCInterfaceDecl *CurrentClass |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 149 | = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext()); |
| 150 | if (!CurrentClass) { |
| 151 | DeclContext *DC = NewMethod->getDeclContext(); |
| 152 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC)) |
| 153 | CurrentClass = Cat->getClassInterface(); |
| 154 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC)) |
| 155 | CurrentClass = Impl->getClassInterface(); |
| 156 | else if (ObjCCategoryImplDecl *CatImpl |
| 157 | = dyn_cast<ObjCCategoryImplDecl>(DC)) |
| 158 | CurrentClass = CatImpl->getClassInterface(); |
| 159 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 160 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 161 | if (CurrentClass) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 162 | Diag(NewMethod->getLocation(), |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 163 | diag::warn_related_result_type_compatibility_class) |
| 164 | << Context.getObjCInterfaceType(CurrentClass) |
| 165 | << ResultType |
| 166 | << ResultTypeRange; |
| 167 | } else { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 168 | Diag(NewMethod->getLocation(), |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 169 | diag::warn_related_result_type_compatibility_protocol) |
| 170 | << ResultType |
| 171 | << ResultTypeRange; |
| 172 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 173 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 174 | if (ObjCMethodFamily Family = Overridden->getMethodFamily()) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 175 | Diag(Overridden->getLocation(), |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 176 | diag::note_related_result_type_family) |
| 177 | << /*overridden method*/ 0 |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 178 | << Family; |
| 179 | else |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 180 | Diag(Overridden->getLocation(), |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 181 | diag::note_related_result_type_overridden); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 182 | } |
Akira Hatanaka | 7d85b8f | 2017-09-20 05:39:18 +0000 | [diff] [blame] | 183 | |
| 184 | if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() != |
| 185 | Overridden->hasAttr<NSReturnsRetainedAttr>())) { |
| 186 | Diag(NewMethod->getLocation(), |
Alex Lorenz | 26d282f | 2018-01-03 23:52:42 +0000 | [diff] [blame] | 187 | getLangOpts().ObjCAutoRefCount |
| 188 | ? diag::err_nsreturns_retained_attribute_mismatch |
| 189 | : diag::warn_nsreturns_retained_attribute_mismatch) |
| 190 | << 1; |
Akira Hatanaka | 7d85b8f | 2017-09-20 05:39:18 +0000 | [diff] [blame] | 191 | Diag(Overridden->getLocation(), diag::note_previous_decl) << "method"; |
| 192 | } |
| 193 | if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() != |
| 194 | Overridden->hasAttr<NSReturnsNotRetainedAttr>())) { |
| 195 | Diag(NewMethod->getLocation(), |
Alex Lorenz | 26d282f | 2018-01-03 23:52:42 +0000 | [diff] [blame] | 196 | getLangOpts().ObjCAutoRefCount |
| 197 | ? diag::err_nsreturns_retained_attribute_mismatch |
| 198 | : diag::warn_nsreturns_retained_attribute_mismatch) |
| 199 | << 0; |
Akira Hatanaka | 7d85b8f | 2017-09-20 05:39:18 +0000 | [diff] [blame] | 200 | Diag(Overridden->getLocation(), diag::note_previous_decl) << "method"; |
| 201 | } |
| 202 | |
| 203 | ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(), |
| 204 | oe = Overridden->param_end(); |
| 205 | for (ObjCMethodDecl::param_iterator ni = NewMethod->param_begin(), |
| 206 | ne = NewMethod->param_end(); |
| 207 | ni != ne && oi != oe; ++ni, ++oi) { |
| 208 | const ParmVarDecl *oldDecl = (*oi); |
| 209 | ParmVarDecl *newDecl = (*ni); |
| 210 | if (newDecl->hasAttr<NSConsumedAttr>() != |
| 211 | oldDecl->hasAttr<NSConsumedAttr>()) { |
Alex Lorenz | 26d282f | 2018-01-03 23:52:42 +0000 | [diff] [blame] | 212 | Diag(newDecl->getLocation(), |
| 213 | getLangOpts().ObjCAutoRefCount |
| 214 | ? diag::err_nsconsumed_attribute_mismatch |
| 215 | : diag::warn_nsconsumed_attribute_mismatch); |
Akira Hatanaka | 7d85b8f | 2017-09-20 05:39:18 +0000 | [diff] [blame] | 216 | Diag(oldDecl->getLocation(), diag::note_previous_decl) << "parameter"; |
Fariborz Jahanian | ac8dbf0 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 217 | } |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 218 | |
Akira Hatanaka | a6b5e00 | 2018-07-28 04:06:13 +0000 | [diff] [blame] | 219 | diagnoseNoescape(newDecl, oldDecl, *this); |
Fariborz Jahanian | ac8dbf0 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 220 | } |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 223 | /// Check a method declaration for compatibility with the Objective-C |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 224 | /// ARC conventions. |
John McCall | e48f389 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 225 | bool Sema::CheckARCMethodDecl(ObjCMethodDecl *method) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 226 | ObjCMethodFamily family = method->getMethodFamily(); |
| 227 | switch (family) { |
| 228 | case OMF_None: |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 229 | case OMF_finalize: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 230 | case OMF_retain: |
| 231 | case OMF_release: |
| 232 | case OMF_autorelease: |
| 233 | case OMF_retainCount: |
| 234 | case OMF_self: |
Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 235 | case OMF_initialize: |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 236 | case OMF_performSelector: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 237 | return false; |
| 238 | |
Fariborz Jahanian | b7f03c1 | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 239 | case OMF_dealloc: |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 240 | if (!Context.hasSameType(method->getReturnType(), Context.VoidTy)) { |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 241 | SourceRange ResultTypeRange = method->getReturnTypeSourceRange(); |
Fariborz Jahanian | b7f03c1 | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 242 | if (ResultTypeRange.isInvalid()) |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 243 | Diag(method->getLocation(), diag::err_dealloc_bad_result_type) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 244 | << method->getReturnType() |
| 245 | << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)"); |
Fariborz Jahanian | b7f03c1 | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 246 | else |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 247 | Diag(method->getLocation(), diag::err_dealloc_bad_result_type) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 248 | << method->getReturnType() |
| 249 | << FixItHint::CreateReplacement(ResultTypeRange, "void"); |
Fariborz Jahanian | b7f03c1 | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 250 | return true; |
| 251 | } |
| 252 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 253 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 254 | case OMF_init: |
| 255 | // 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] | 256 | if (checkInitMethod(method, QualType())) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 257 | return true; |
| 258 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 259 | method->addAttr(NSConsumesSelfAttr::CreateImplicit(Context)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 260 | |
| 261 | // Don't add a second copy of this attribute, but otherwise don't |
| 262 | // let it be suppressed. |
| 263 | if (method->hasAttr<NSReturnsRetainedAttr>()) |
| 264 | return false; |
| 265 | break; |
| 266 | |
| 267 | case OMF_alloc: |
| 268 | case OMF_copy: |
| 269 | case OMF_mutableCopy: |
| 270 | case OMF_new: |
| 271 | if (method->hasAttr<NSReturnsRetainedAttr>() || |
| 272 | method->hasAttr<NSReturnsNotRetainedAttr>() || |
| 273 | method->hasAttr<NSReturnsAutoreleasedAttr>()) |
| 274 | return false; |
| 275 | break; |
| 276 | } |
| 277 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 278 | method->addAttr(NSReturnsRetainedAttr::CreateImplicit(Context)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 279 | return false; |
| 280 | } |
| 281 | |
Alex Lorenz | f81d97e | 2017-07-13 16:35:59 +0000 | [diff] [blame] | 282 | static void DiagnoseObjCImplementedDeprecations(Sema &S, const NamedDecl *ND, |
| 283 | SourceLocation ImplLoc) { |
| 284 | if (!ND) |
| 285 | return; |
| 286 | bool IsCategory = false; |
Alex Lorenz | f4d4cfb | 2018-05-03 01:12:06 +0000 | [diff] [blame] | 287 | StringRef RealizedPlatform; |
| 288 | AvailabilityResult Availability = ND->getAvailability( |
| 289 | /*Message=*/nullptr, /*EnclosingVersion=*/VersionTuple(), |
| 290 | &RealizedPlatform); |
Alex Lorenz | e1088dc | 2017-07-13 16:37:11 +0000 | [diff] [blame] | 291 | if (Availability != AR_Deprecated) { |
Eric Christopher | 7aba978 | 2017-07-14 01:42:57 +0000 | [diff] [blame] | 292 | if (isa<ObjCMethodDecl>(ND)) { |
Alex Lorenz | e1088dc | 2017-07-13 16:37:11 +0000 | [diff] [blame] | 293 | if (Availability != AR_Unavailable) |
| 294 | return; |
Alex Lorenz | f4d4cfb | 2018-05-03 01:12:06 +0000 | [diff] [blame] | 295 | if (RealizedPlatform.empty()) |
| 296 | RealizedPlatform = S.Context.getTargetInfo().getPlatformName(); |
| 297 | // Warn about implementing unavailable methods, unless the unavailable |
| 298 | // is for an app extension. |
| 299 | if (RealizedPlatform.endswith("_app_extension")) |
| 300 | return; |
Alex Lorenz | e1088dc | 2017-07-13 16:37:11 +0000 | [diff] [blame] | 301 | S.Diag(ImplLoc, diag::warn_unavailable_def); |
| 302 | S.Diag(ND->getLocation(), diag::note_method_declared_at) |
| 303 | << ND->getDeclName(); |
| 304 | return; |
| 305 | } |
Alex Lorenz | f81d97e | 2017-07-13 16:35:59 +0000 | [diff] [blame] | 306 | if (const auto *CD = dyn_cast<ObjCCategoryDecl>(ND)) { |
| 307 | if (!CD->getClassInterface()->isDeprecated()) |
| 308 | return; |
| 309 | ND = CD->getClassInterface(); |
| 310 | IsCategory = true; |
| 311 | } else |
| 312 | return; |
Fariborz Jahanian | d724a10 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 313 | } |
Alex Lorenz | f81d97e | 2017-07-13 16:35:59 +0000 | [diff] [blame] | 314 | S.Diag(ImplLoc, diag::warn_deprecated_def) |
| 315 | << (isa<ObjCMethodDecl>(ND) |
| 316 | ? /*Method*/ 0 |
| 317 | : isa<ObjCCategoryDecl>(ND) || IsCategory ? /*Category*/ 2 |
| 318 | : /*Class*/ 1); |
| 319 | if (isa<ObjCMethodDecl>(ND)) |
| 320 | S.Diag(ND->getLocation(), diag::note_method_declared_at) |
| 321 | << ND->getDeclName(); |
| 322 | else |
| 323 | S.Diag(ND->getLocation(), diag::note_previous_decl) |
| 324 | << (isa<ObjCCategoryDecl>(ND) ? "category" : "class"); |
Fariborz Jahanian | d724a10 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Fariborz Jahanian | bd0642f | 2011-08-31 17:37:55 +0000 | [diff] [blame] | 327 | /// AddAnyMethodToGlobalPool - Add any method, instance or factory to global |
| 328 | /// pool. |
| 329 | void Sema::AddAnyMethodToGlobalPool(Decl *D) { |
| 330 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 331 | |
Fariborz Jahanian | bd0642f | 2011-08-31 17:37:55 +0000 | [diff] [blame] | 332 | // If we don't have a valid method decl, simply return. |
| 333 | if (!MDecl) |
| 334 | return; |
| 335 | if (MDecl->isInstanceMethod()) |
| 336 | AddInstanceMethodToGlobalPool(MDecl, true); |
| 337 | else |
| 338 | AddFactoryMethodToGlobalPool(MDecl, true); |
| 339 | } |
| 340 | |
Fariborz Jahanian | 1dfeace | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 341 | /// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer |
| 342 | /// has explicit ownership attribute; false otherwise. |
| 343 | static bool |
| 344 | HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) { |
| 345 | QualType T = Param->getType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 346 | |
Fariborz Jahanian | 1dfeace | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 347 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 348 | T = PT->getPointeeType(); |
| 349 | } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) { |
| 350 | T = RT->getPointeeType(); |
| 351 | } else { |
| 352 | return true; |
| 353 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 354 | |
| 355 | // 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] | 356 | // inferred it. So, it is implicit. |
| 357 | return !T.getLocalQualifiers().hasObjCLifetime(); |
| 358 | } |
| 359 | |
Fariborz Jahanian | 18d0a5d | 2012-08-08 23:41:08 +0000 | [diff] [blame] | 360 | /// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible |
| 361 | /// and user declared, in the method definition's AST. |
| 362 | void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) { |
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 | |
Steve Naroff | 542cd5d | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 366 | // If we don't have a valid method decl, simply return. |
| 367 | if (!MDecl) |
| 368 | return; |
Steve Naroff | 1d2538c | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 369 | |
Akira Hatanaka | ff6c4f3 | 2018-04-12 06:01:41 +0000 | [diff] [blame] | 370 | QualType ResultType = MDecl->getReturnType(); |
| 371 | if (!ResultType->isDependentType() && !ResultType->isVoidType() && |
| 372 | !MDecl->isInvalidDecl() && |
| 373 | RequireCompleteType(MDecl->getLocation(), ResultType, |
| 374 | diag::err_func_def_incomplete_result)) |
| 375 | MDecl->setInvalidDecl(); |
| 376 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 377 | // 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] | 378 | PushDeclContext(FnBodyScope, MDecl); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 379 | PushFunctionScope(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 380 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 381 | // Create Decl objects for each parameter, entrring them in the scope for |
| 382 | // binding to their use. |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 383 | |
| 384 | // Insert the invisible arguments, self and _cmd! |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 385 | MDecl->createImplicitParams(Context, MDecl->getClassInterface()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | |
Daniel Dunbar | 279d1cc | 2008-08-26 06:07:48 +0000 | [diff] [blame] | 387 | PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); |
| 388 | PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 389 | |
Reid Kleckner | 5a11580 | 2013-06-24 14:38:26 +0000 | [diff] [blame] | 390 | // 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] | 391 | CheckParmsForFunctionDef(MDecl->parameters(), |
Reid Kleckner | 5a11580 | 2013-06-24 14:38:26 +0000 | [diff] [blame] | 392 | /*CheckParameterNames=*/false); |
| 393 | |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 394 | // Introduce all of the other parameters into this scope. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 395 | for (auto *Param : MDecl->parameters()) { |
Fariborz Jahanian | b3e8712 | 2010-09-17 22:07:07 +0000 | [diff] [blame] | 396 | if (!Param->isInvalidDecl() && |
Fariborz Jahanian | 1dfeace | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 397 | getLangOpts().ObjCAutoRefCount && |
| 398 | !HasExplicitOwnershipAttr(*this, Param)) |
| 399 | Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) << |
| 400 | Param->getType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 401 | |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 402 | if (Param->getIdentifier()) |
| 403 | PushOnScopeChains(Param, FnBodyScope); |
Fariborz Jahanian | b3e8712 | 2010-09-17 22:07:07 +0000 | [diff] [blame] | 404 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 405 | |
| 406 | // In ARC, disallow definition of retain/release/autorelease/retainCount |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 407 | if (getLangOpts().ObjCAutoRefCount) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 408 | switch (MDecl->getMethodFamily()) { |
| 409 | case OMF_retain: |
| 410 | case OMF_retainCount: |
| 411 | case OMF_release: |
| 412 | case OMF_autorelease: |
| 413 | Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def) |
Fariborz Jahanian | 39d1c42 | 2013-05-16 19:08:44 +0000 | [diff] [blame] | 414 | << 0 << MDecl->getSelector(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 415 | break; |
| 416 | |
| 417 | case OMF_None: |
| 418 | case OMF_dealloc: |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 419 | case OMF_finalize: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 420 | case OMF_alloc: |
| 421 | case OMF_init: |
| 422 | case OMF_mutableCopy: |
| 423 | case OMF_copy: |
| 424 | case OMF_new: |
| 425 | case OMF_self: |
Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 426 | case OMF_initialize: |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 427 | case OMF_performSelector: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 428 | break; |
| 429 | } |
| 430 | } |
| 431 | |
Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 432 | // Warn on deprecated methods under -Wdeprecated-implementations, |
| 433 | // and prepare for warning on missing super calls. |
| 434 | if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 435 | ObjCMethodDecl *IMD = |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 436 | IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 437 | |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 438 | if (IMD) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 439 | ObjCImplDecl *ImplDeclOfMethodDef = |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 440 | dyn_cast<ObjCImplDecl>(MDecl->getDeclContext()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 441 | ObjCContainerDecl *ContDeclOfMethodDecl = |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 442 | dyn_cast<ObjCContainerDecl>(IMD->getDeclContext()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 443 | ObjCImplDecl *ImplDeclOfMethodDecl = nullptr; |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 444 | if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl)) |
| 445 | ImplDeclOfMethodDecl = OID->getImplementation(); |
Fariborz Jahanian | ed39e7c | 2014-03-18 16:25:22 +0000 | [diff] [blame] | 446 | else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl)) { |
| 447 | if (CD->IsClassExtension()) { |
| 448 | if (ObjCInterfaceDecl *OID = CD->getClassInterface()) |
| 449 | ImplDeclOfMethodDecl = OID->getImplementation(); |
| 450 | } else |
| 451 | ImplDeclOfMethodDecl = CD->getImplementation(); |
Fariborz Jahanian | 19a08bb | 2014-03-18 00:10:37 +0000 | [diff] [blame] | 452 | } |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 453 | // No need to issue deprecated warning if deprecated mehod in class/category |
| 454 | // is being implemented in its own implementation (no overriding is involved). |
Fariborz Jahanian | ed39e7c | 2014-03-18 16:25:22 +0000 | [diff] [blame] | 455 | if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef) |
Alex Lorenz | f81d97e | 2017-07-13 16:35:59 +0000 | [diff] [blame] | 456 | DiagnoseObjCImplementedDeprecations(*this, IMD, MDecl->getLocation()); |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 457 | } |
Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 458 | |
Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 459 | if (MDecl->getMethodFamily() == OMF_init) { |
| 460 | if (MDecl->isDesignatedInitializerForTheInterface()) { |
| 461 | getCurFunction()->ObjCIsDesignatedInit = true; |
Fariborz Jahanian | e3b5c99 | 2014-03-14 23:30:18 +0000 | [diff] [blame] | 462 | getCurFunction()->ObjCWarnForNoDesignatedInitChain = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 463 | IC->getSuperClass() != nullptr; |
Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 464 | } else if (IC->hasDesignatedInitializers()) { |
| 465 | getCurFunction()->ObjCIsSecondaryInit = true; |
Fariborz Jahanian | e3b5c99 | 2014-03-14 23:30:18 +0000 | [diff] [blame] | 466 | getCurFunction()->ObjCWarnForNoInitDelegation = true; |
Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 467 | } |
| 468 | } |
Argyrios Kyrtzidis | 22bfa2c | 2013-12-03 21:11:36 +0000 | [diff] [blame] | 469 | |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 470 | // If this is "dealloc" or "finalize", set some bit here. |
Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 471 | // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false. |
| 472 | // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set. |
| 473 | // Only do this if the current class actually has a superclass. |
Jordan Rose | d03d99d | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 474 | if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) { |
Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 475 | ObjCMethodFamily Family = MDecl->getMethodFamily(); |
| 476 | if (Family == OMF_dealloc) { |
| 477 | if (!(getLangOpts().ObjCAutoRefCount || |
| 478 | getLangOpts().getGC() == LangOptions::GCOnly)) |
| 479 | getCurFunction()->ObjCShouldCallSuper = true; |
| 480 | |
| 481 | } else if (Family == OMF_finalize) { |
| 482 | if (Context.getLangOpts().getGC() != LangOptions::NonGC) |
| 483 | getCurFunction()->ObjCShouldCallSuper = true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 484 | |
Fariborz Jahanian | ce4bbb2 | 2013-11-05 00:28:21 +0000 | [diff] [blame] | 485 | } else { |
Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 486 | const ObjCMethodDecl *SuperMethod = |
Jordan Rose | d03d99d | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 487 | SuperClass->lookupMethod(MDecl->getSelector(), |
| 488 | MDecl->isInstanceMethod()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 489 | getCurFunction()->ObjCShouldCallSuper = |
Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 490 | (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>()); |
Fariborz Jahanian | d6876b2 | 2012-09-10 18:04:25 +0000 | [diff] [blame] | 491 | } |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 492 | } |
Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 493 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 496 | namespace { |
| 497 | |
| 498 | // Callback to only accept typo corrections that are Objective-C classes. |
| 499 | // If an ObjCInterfaceDecl* is given to the constructor, then the validation |
| 500 | // function will reject corrections to that class. |
| 501 | class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback { |
| 502 | public: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 503 | ObjCInterfaceValidatorCCC() : CurrentIDecl(nullptr) {} |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 504 | explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl) |
| 505 | : CurrentIDecl(IDecl) {} |
| 506 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 507 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 508 | ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
| 509 | return ID && !declaresSameEntity(ID, CurrentIDecl); |
| 510 | } |
| 511 | |
| 512 | private: |
| 513 | ObjCInterfaceDecl *CurrentIDecl; |
| 514 | }; |
| 515 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 516 | } // end anonymous namespace |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 517 | |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 518 | static void diagnoseUseOfProtocols(Sema &TheSema, |
| 519 | ObjCContainerDecl *CD, |
| 520 | ObjCProtocolDecl *const *ProtoRefs, |
| 521 | unsigned NumProtoRefs, |
| 522 | const SourceLocation *ProtoLocs) { |
| 523 | assert(ProtoRefs); |
| 524 | // Diagnose availability in the context of the ObjC container. |
| 525 | Sema::ContextRAII SavedContext(TheSema, CD); |
| 526 | for (unsigned i = 0; i < NumProtoRefs; ++i) { |
Alex Lorenz | cdd596f | 2017-07-07 09:15:29 +0000 | [diff] [blame] | 527 | (void)TheSema.DiagnoseUseOfDecl(ProtoRefs[i], ProtoLocs[i], |
| 528 | /*UnknownObjCClass=*/nullptr, |
| 529 | /*ObjCPropertyAccess=*/false, |
| 530 | /*AvoidPartialAvailabilityChecks=*/true); |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 534 | void Sema:: |
| 535 | ActOnSuperClassOfClassInterface(Scope *S, |
| 536 | SourceLocation AtInterfaceLoc, |
| 537 | ObjCInterfaceDecl *IDecl, |
| 538 | IdentifierInfo *ClassName, |
| 539 | SourceLocation ClassLoc, |
| 540 | IdentifierInfo *SuperName, |
| 541 | SourceLocation SuperLoc, |
| 542 | ArrayRef<ParsedType> SuperTypeArgs, |
| 543 | SourceRange SuperTypeArgsRange) { |
| 544 | // Check if a different kind of symbol declared in this scope. |
| 545 | NamedDecl *PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
| 546 | LookupOrdinaryName); |
| 547 | |
| 548 | if (!PrevDecl) { |
| 549 | // Try to correct for a typo in the superclass name without correcting |
| 550 | // to the class we're defining. |
| 551 | if (TypoCorrection Corrected = CorrectTypo( |
| 552 | DeclarationNameInfo(SuperName, SuperLoc), |
| 553 | LookupOrdinaryName, TUScope, |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 554 | nullptr, llvm::make_unique<ObjCInterfaceValidatorCCC>(IDecl), |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 555 | CTK_ErrorRecovery)) { |
| 556 | diagnoseTypo(Corrected, PDiag(diag::err_undef_superclass_suggest) |
| 557 | << SuperName << ClassName); |
| 558 | PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | if (declaresSameEntity(PrevDecl, IDecl)) { |
| 563 | Diag(SuperLoc, diag::err_recursive_superclass) |
| 564 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
| 565 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
| 566 | } else { |
| 567 | ObjCInterfaceDecl *SuperClassDecl = |
| 568 | dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
| 569 | QualType SuperClassType; |
| 570 | |
| 571 | // Diagnose classes that inherit from deprecated classes. |
| 572 | if (SuperClassDecl) { |
| 573 | (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc); |
| 574 | SuperClassType = Context.getObjCInterfaceType(SuperClassDecl); |
| 575 | } |
| 576 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 577 | if (PrevDecl && !SuperClassDecl) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 578 | // The previous declaration was not a class decl. Check if we have a |
| 579 | // typedef. If we do, get the underlying class type. |
| 580 | if (const TypedefNameDecl *TDecl = |
| 581 | dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) { |
| 582 | QualType T = TDecl->getUnderlyingType(); |
| 583 | if (T->isObjCObjectType()) { |
| 584 | if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) { |
| 585 | SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl); |
| 586 | SuperClassType = Context.getTypeDeclType(TDecl); |
| 587 | |
| 588 | // This handles the following case: |
| 589 | // @interface NewI @end |
| 590 | // typedef NewI DeprI __attribute__((deprecated("blah"))) |
| 591 | // @interface SI : DeprI /* warn here */ @end |
| 592 | (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc); |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | // This handles the following case: |
| 598 | // |
| 599 | // typedef int SuperClass; |
| 600 | // @interface MyClass : SuperClass {} @end |
| 601 | // |
| 602 | if (!SuperClassDecl) { |
| 603 | Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName; |
| 604 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) { |
| 609 | if (!SuperClassDecl) |
| 610 | Diag(SuperLoc, diag::err_undef_superclass) |
| 611 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
| 612 | else if (RequireCompleteType(SuperLoc, |
| 613 | SuperClassType, |
| 614 | diag::err_forward_superclass, |
| 615 | SuperClassDecl->getDeclName(), |
| 616 | ClassName, |
| 617 | SourceRange(AtInterfaceLoc, ClassLoc))) { |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 618 | SuperClassDecl = nullptr; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 619 | SuperClassType = QualType(); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | if (SuperClassType.isNull()) { |
| 624 | assert(!SuperClassDecl && "Failed to set SuperClassType?"); |
| 625 | return; |
| 626 | } |
| 627 | |
| 628 | // Handle type arguments on the superclass. |
| 629 | TypeSourceInfo *SuperClassTInfo = nullptr; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 630 | if (!SuperTypeArgs.empty()) { |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 631 | TypeResult fullSuperClassType = actOnObjCTypeArgsAndProtocolQualifiers( |
| 632 | S, |
| 633 | SuperLoc, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 634 | CreateParsedType(SuperClassType, |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 635 | nullptr), |
| 636 | SuperTypeArgsRange.getBegin(), |
| 637 | SuperTypeArgs, |
| 638 | SuperTypeArgsRange.getEnd(), |
| 639 | SourceLocation(), |
| 640 | { }, |
| 641 | { }, |
| 642 | SourceLocation()); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 643 | if (!fullSuperClassType.isUsable()) |
| 644 | return; |
| 645 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 646 | SuperClassType = GetTypeFromParser(fullSuperClassType.get(), |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 647 | &SuperClassTInfo); |
| 648 | } |
| 649 | |
| 650 | if (!SuperClassTInfo) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 651 | SuperClassTInfo = Context.getTrivialTypeSourceInfo(SuperClassType, |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 652 | SuperLoc); |
| 653 | } |
| 654 | |
| 655 | IDecl->setSuperClass(SuperClassTInfo); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 656 | IDecl->setEndOfDefinitionLoc(SuperClassTInfo->getTypeLoc().getEndLoc()); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 660 | DeclResult Sema::actOnObjCTypeParam(Scope *S, |
| 661 | ObjCTypeParamVariance variance, |
| 662 | SourceLocation varianceLoc, |
| 663 | unsigned index, |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 664 | IdentifierInfo *paramName, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 665 | SourceLocation paramLoc, |
| 666 | SourceLocation colonLoc, |
| 667 | ParsedType parsedTypeBound) { |
| 668 | // If there was an explicitly-provided type bound, check it. |
| 669 | TypeSourceInfo *typeBoundInfo = nullptr; |
| 670 | if (parsedTypeBound) { |
| 671 | // The type bound can be any Objective-C pointer type. |
| 672 | QualType typeBound = GetTypeFromParser(parsedTypeBound, &typeBoundInfo); |
| 673 | if (typeBound->isObjCObjectPointerType()) { |
| 674 | // okay |
| 675 | } else if (typeBound->isObjCObjectType()) { |
| 676 | // The user forgot the * on an Objective-C pointer type, e.g., |
| 677 | // "T : NSView". |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 678 | SourceLocation starLoc = getLocForEndOfToken( |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 679 | typeBoundInfo->getTypeLoc().getEndLoc()); |
| 680 | Diag(typeBoundInfo->getTypeLoc().getBeginLoc(), |
| 681 | diag::err_objc_type_param_bound_missing_pointer) |
| 682 | << typeBound << paramName |
| 683 | << FixItHint::CreateInsertion(starLoc, " *"); |
| 684 | |
| 685 | // Create a new type location builder so we can update the type |
| 686 | // location information we have. |
| 687 | TypeLocBuilder builder; |
| 688 | builder.pushFullCopy(typeBoundInfo->getTypeLoc()); |
| 689 | |
| 690 | // Create the Objective-C pointer type. |
| 691 | typeBound = Context.getObjCObjectPointerType(typeBound); |
| 692 | ObjCObjectPointerTypeLoc newT |
| 693 | = builder.push<ObjCObjectPointerTypeLoc>(typeBound); |
| 694 | newT.setStarLoc(starLoc); |
| 695 | |
| 696 | // Form the new type source information. |
| 697 | typeBoundInfo = builder.getTypeSourceInfo(Context, typeBound); |
| 698 | } else { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 699 | // Not a valid type bound. |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 700 | Diag(typeBoundInfo->getTypeLoc().getBeginLoc(), |
| 701 | diag::err_objc_type_param_bound_nonobject) |
| 702 | << typeBound << paramName; |
| 703 | |
| 704 | // Forget the bound; we'll default to id later. |
| 705 | typeBoundInfo = nullptr; |
| 706 | } |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 707 | |
John McCall | 6997525 | 2015-09-23 22:14:21 +0000 | [diff] [blame] | 708 | // Type bounds cannot have qualifiers (even indirectly) or explicit |
| 709 | // nullability. |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 710 | if (typeBoundInfo) { |
John McCall | 6997525 | 2015-09-23 22:14:21 +0000 | [diff] [blame] | 711 | QualType typeBound = typeBoundInfo->getType(); |
| 712 | TypeLoc qual = typeBoundInfo->getTypeLoc().findExplicitQualifierLoc(); |
| 713 | if (qual || typeBound.hasQualifiers()) { |
| 714 | bool diagnosed = false; |
| 715 | SourceRange rangeToRemove; |
| 716 | if (qual) { |
| 717 | if (auto attr = qual.getAs<AttributedTypeLoc>()) { |
| 718 | rangeToRemove = attr.getLocalSourceRange(); |
| 719 | if (attr.getTypePtr()->getImmediateNullability()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 720 | Diag(attr.getBeginLoc(), |
John McCall | 6997525 | 2015-09-23 22:14:21 +0000 | [diff] [blame] | 721 | diag::err_objc_type_param_bound_explicit_nullability) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 722 | << paramName << typeBound |
| 723 | << FixItHint::CreateRemoval(rangeToRemove); |
John McCall | 6997525 | 2015-09-23 22:14:21 +0000 | [diff] [blame] | 724 | diagnosed = true; |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | if (!diagnosed) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 730 | Diag(qual ? qual.getBeginLoc() |
| 731 | : typeBoundInfo->getTypeLoc().getBeginLoc(), |
| 732 | diag::err_objc_type_param_bound_qualified) |
| 733 | << paramName << typeBound |
| 734 | << typeBound.getQualifiers().getAsString() |
| 735 | << FixItHint::CreateRemoval(rangeToRemove); |
John McCall | 6997525 | 2015-09-23 22:14:21 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | // If the type bound has qualifiers other than CVR, we need to strip |
| 739 | // them or we'll probably assert later when trying to apply new |
| 740 | // qualifiers. |
| 741 | Qualifiers quals = typeBound.getQualifiers(); |
| 742 | quals.removeCVRQualifiers(); |
| 743 | if (!quals.empty()) { |
| 744 | typeBoundInfo = |
| 745 | Context.getTrivialTypeSourceInfo(typeBound.getUnqualifiedType()); |
| 746 | } |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 747 | } |
| 748 | } |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | // If there was no explicit type bound (or we removed it due to an error), |
| 752 | // use 'id' instead. |
| 753 | if (!typeBoundInfo) { |
| 754 | colonLoc = SourceLocation(); |
| 755 | typeBoundInfo = Context.getTrivialTypeSourceInfo(Context.getObjCIdType()); |
| 756 | } |
| 757 | |
| 758 | // Create the type parameter. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 759 | return ObjCTypeParamDecl::Create(Context, CurContext, variance, varianceLoc, |
| 760 | index, paramLoc, paramName, colonLoc, |
| 761 | typeBoundInfo); |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | ObjCTypeParamList *Sema::actOnObjCTypeParamList(Scope *S, |
| 765 | SourceLocation lAngleLoc, |
| 766 | ArrayRef<Decl *> typeParamsIn, |
| 767 | SourceLocation rAngleLoc) { |
| 768 | // We know that the array only contains Objective-C type parameters. |
| 769 | ArrayRef<ObjCTypeParamDecl *> |
| 770 | typeParams( |
| 771 | reinterpret_cast<ObjCTypeParamDecl * const *>(typeParamsIn.data()), |
| 772 | typeParamsIn.size()); |
| 773 | |
| 774 | // Diagnose redeclarations of type parameters. |
| 775 | // We do this now because Objective-C type parameters aren't pushed into |
| 776 | // scope until later (after the instance variable block), but we want the |
| 777 | // diagnostics to occur right after we parse the type parameter list. |
| 778 | llvm::SmallDenseMap<IdentifierInfo *, ObjCTypeParamDecl *> knownParams; |
| 779 | for (auto typeParam : typeParams) { |
| 780 | auto known = knownParams.find(typeParam->getIdentifier()); |
| 781 | if (known != knownParams.end()) { |
| 782 | Diag(typeParam->getLocation(), diag::err_objc_type_param_redecl) |
| 783 | << typeParam->getIdentifier() |
| 784 | << SourceRange(known->second->getLocation()); |
| 785 | |
| 786 | typeParam->setInvalidDecl(); |
| 787 | } else { |
| 788 | knownParams.insert(std::make_pair(typeParam->getIdentifier(), typeParam)); |
| 789 | |
| 790 | // Push the type parameter into scope. |
| 791 | PushOnScopeChains(typeParam, S, /*AddToContext=*/false); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | // Create the parameter list. |
| 796 | return ObjCTypeParamList::create(Context, lAngleLoc, typeParams, rAngleLoc); |
| 797 | } |
| 798 | |
| 799 | void Sema::popObjCTypeParamList(Scope *S, ObjCTypeParamList *typeParamList) { |
| 800 | for (auto typeParam : *typeParamList) { |
| 801 | if (!typeParam->isInvalidDecl()) { |
| 802 | S->RemoveDecl(typeParam); |
| 803 | IdResolver.RemoveDecl(typeParam); |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | namespace { |
| 809 | /// The context in which an Objective-C type parameter list occurs, for use |
| 810 | /// in diagnostics. |
| 811 | enum class TypeParamListContext { |
| 812 | ForwardDeclaration, |
| 813 | Definition, |
| 814 | Category, |
| 815 | Extension |
| 816 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 817 | } // end anonymous namespace |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 818 | |
| 819 | /// Check consistency between two Objective-C type parameter lists, e.g., |
NAKAMURA Takumi | 4c3ab45 | 2015-07-08 02:35:56 +0000 | [diff] [blame] | 820 | /// between a category/extension and an \@interface or between an \@class and an |
| 821 | /// \@interface. |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 822 | static bool checkTypeParamListConsistency(Sema &S, |
| 823 | ObjCTypeParamList *prevTypeParams, |
| 824 | ObjCTypeParamList *newTypeParams, |
| 825 | TypeParamListContext newContext) { |
| 826 | // If the sizes don't match, complain about that. |
| 827 | if (prevTypeParams->size() != newTypeParams->size()) { |
| 828 | SourceLocation diagLoc; |
| 829 | if (newTypeParams->size() > prevTypeParams->size()) { |
| 830 | diagLoc = newTypeParams->begin()[prevTypeParams->size()]->getLocation(); |
| 831 | } else { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 832 | diagLoc = S.getLocForEndOfToken(newTypeParams->back()->getEndLoc()); |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | S.Diag(diagLoc, diag::err_objc_type_param_arity_mismatch) |
| 836 | << static_cast<unsigned>(newContext) |
| 837 | << (newTypeParams->size() > prevTypeParams->size()) |
| 838 | << prevTypeParams->size() |
| 839 | << newTypeParams->size(); |
| 840 | |
| 841 | return true; |
| 842 | } |
| 843 | |
| 844 | // Match up the type parameters. |
| 845 | for (unsigned i = 0, n = prevTypeParams->size(); i != n; ++i) { |
| 846 | ObjCTypeParamDecl *prevTypeParam = prevTypeParams->begin()[i]; |
| 847 | ObjCTypeParamDecl *newTypeParam = newTypeParams->begin()[i]; |
| 848 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 849 | // Check for consistency of the variance. |
| 850 | if (newTypeParam->getVariance() != prevTypeParam->getVariance()) { |
| 851 | if (newTypeParam->getVariance() == ObjCTypeParamVariance::Invariant && |
| 852 | newContext != TypeParamListContext::Definition) { |
| 853 | // When the new type parameter is invariant and is not part |
| 854 | // of the definition, just propagate the variance. |
| 855 | newTypeParam->setVariance(prevTypeParam->getVariance()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 856 | } else if (prevTypeParam->getVariance() |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 857 | == ObjCTypeParamVariance::Invariant && |
| 858 | !(isa<ObjCInterfaceDecl>(prevTypeParam->getDeclContext()) && |
| 859 | cast<ObjCInterfaceDecl>(prevTypeParam->getDeclContext()) |
| 860 | ->getDefinition() == prevTypeParam->getDeclContext())) { |
| 861 | // When the old parameter is invariant and was not part of the |
| 862 | // definition, just ignore the difference because it doesn't |
| 863 | // matter. |
| 864 | } else { |
| 865 | { |
| 866 | // Diagnose the conflict and update the second declaration. |
| 867 | SourceLocation diagLoc = newTypeParam->getVarianceLoc(); |
| 868 | if (diagLoc.isInvalid()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 869 | diagLoc = newTypeParam->getBeginLoc(); |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 870 | |
| 871 | auto diag = S.Diag(diagLoc, |
| 872 | diag::err_objc_type_param_variance_conflict) |
| 873 | << static_cast<unsigned>(newTypeParam->getVariance()) |
| 874 | << newTypeParam->getDeclName() |
| 875 | << static_cast<unsigned>(prevTypeParam->getVariance()) |
| 876 | << prevTypeParam->getDeclName(); |
| 877 | switch (prevTypeParam->getVariance()) { |
| 878 | case ObjCTypeParamVariance::Invariant: |
| 879 | diag << FixItHint::CreateRemoval(newTypeParam->getVarianceLoc()); |
| 880 | break; |
| 881 | |
| 882 | case ObjCTypeParamVariance::Covariant: |
| 883 | case ObjCTypeParamVariance::Contravariant: { |
| 884 | StringRef newVarianceStr |
| 885 | = prevTypeParam->getVariance() == ObjCTypeParamVariance::Covariant |
| 886 | ? "__covariant" |
| 887 | : "__contravariant"; |
| 888 | if (newTypeParam->getVariance() |
| 889 | == ObjCTypeParamVariance::Invariant) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 890 | diag << FixItHint::CreateInsertion(newTypeParam->getBeginLoc(), |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 891 | (newVarianceStr + " ").str()); |
| 892 | } else { |
| 893 | diag << FixItHint::CreateReplacement(newTypeParam->getVarianceLoc(), |
| 894 | newVarianceStr); |
| 895 | } |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
| 901 | << prevTypeParam->getDeclName(); |
| 902 | |
| 903 | // Override the variance. |
| 904 | newTypeParam->setVariance(prevTypeParam->getVariance()); |
| 905 | } |
| 906 | } |
| 907 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 908 | // If the bound types match, there's nothing to do. |
| 909 | if (S.Context.hasSameType(prevTypeParam->getUnderlyingType(), |
| 910 | newTypeParam->getUnderlyingType())) |
| 911 | continue; |
| 912 | |
| 913 | // If the new type parameter's bound was explicit, complain about it being |
| 914 | // different from the original. |
| 915 | if (newTypeParam->hasExplicitBound()) { |
| 916 | SourceRange newBoundRange = newTypeParam->getTypeSourceInfo() |
| 917 | ->getTypeLoc().getSourceRange(); |
| 918 | S.Diag(newBoundRange.getBegin(), diag::err_objc_type_param_bound_conflict) |
| 919 | << newTypeParam->getUnderlyingType() |
| 920 | << newTypeParam->getDeclName() |
| 921 | << prevTypeParam->hasExplicitBound() |
| 922 | << prevTypeParam->getUnderlyingType() |
| 923 | << (newTypeParam->getDeclName() == prevTypeParam->getDeclName()) |
| 924 | << prevTypeParam->getDeclName() |
| 925 | << FixItHint::CreateReplacement( |
| 926 | newBoundRange, |
| 927 | prevTypeParam->getUnderlyingType().getAsString( |
| 928 | S.Context.getPrintingPolicy())); |
| 929 | |
| 930 | S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
| 931 | << prevTypeParam->getDeclName(); |
| 932 | |
| 933 | // Override the new type parameter's bound type with the previous type, |
| 934 | // so that it's consistent. |
| 935 | newTypeParam->setTypeSourceInfo( |
| 936 | S.Context.getTrivialTypeSourceInfo(prevTypeParam->getUnderlyingType())); |
| 937 | continue; |
| 938 | } |
| 939 | |
| 940 | // The new type parameter got the implicit bound of 'id'. That's okay for |
| 941 | // categories and extensions (overwrite it later), but not for forward |
| 942 | // declarations and @interfaces, because those must be standalone. |
| 943 | if (newContext == TypeParamListContext::ForwardDeclaration || |
| 944 | newContext == TypeParamListContext::Definition) { |
| 945 | // Diagnose this problem for forward declarations and definitions. |
| 946 | SourceLocation insertionLoc |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 947 | = S.getLocForEndOfToken(newTypeParam->getLocation()); |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 948 | std::string newCode |
| 949 | = " : " + prevTypeParam->getUnderlyingType().getAsString( |
| 950 | S.Context.getPrintingPolicy()); |
| 951 | S.Diag(newTypeParam->getLocation(), |
| 952 | diag::err_objc_type_param_bound_missing) |
| 953 | << prevTypeParam->getUnderlyingType() |
| 954 | << newTypeParam->getDeclName() |
| 955 | << (newContext == TypeParamListContext::ForwardDeclaration) |
| 956 | << FixItHint::CreateInsertion(insertionLoc, newCode); |
| 957 | |
| 958 | S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
| 959 | << prevTypeParam->getDeclName(); |
| 960 | } |
| 961 | |
| 962 | // Update the new type parameter's bound to match the previous one. |
| 963 | newTypeParam->setTypeSourceInfo( |
| 964 | S.Context.getTrivialTypeSourceInfo(prevTypeParam->getUnderlyingType())); |
| 965 | } |
| 966 | |
| 967 | return false; |
| 968 | } |
| 969 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 970 | Decl *Sema::ActOnStartClassInterface( |
| 971 | Scope *S, SourceLocation AtInterfaceLoc, IdentifierInfo *ClassName, |
| 972 | SourceLocation ClassLoc, ObjCTypeParamList *typeParamList, |
| 973 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
| 974 | ArrayRef<ParsedType> SuperTypeArgs, SourceRange SuperTypeArgsRange, |
| 975 | Decl *const *ProtoRefs, unsigned NumProtoRefs, |
| 976 | const SourceLocation *ProtoLocs, SourceLocation EndProtoLoc, |
| 977 | const ParsedAttributesView &AttrList) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 978 | assert(ClassName && "Missing class identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 980 | // Check for another declaration kind with the same name. |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 981 | NamedDecl *PrevDecl = |
| 982 | LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName, |
| 983 | forRedeclarationInCurContext()); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 984 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 985 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 986 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 987 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 988 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 990 | // Create a declaration to describe this @interface. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 991 | ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Argyrios Kyrtzidis | dd71063 | 2013-06-18 21:26:33 +0000 | [diff] [blame] | 992 | |
| 993 | if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) { |
| 994 | // A previous decl with a different name is because of |
| 995 | // @compatibility_alias, for example: |
| 996 | // \code |
| 997 | // @class NewImage; |
| 998 | // @compatibility_alias OldImage NewImage; |
| 999 | // \endcode |
| 1000 | // A lookup for 'OldImage' will return the 'NewImage' decl. |
| 1001 | // |
| 1002 | // In such a case use the real declaration name, instead of the alias one, |
| 1003 | // otherwise we will break IdentifierResolver and redecls-chain invariants. |
| 1004 | // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl |
| 1005 | // has been aliased. |
| 1006 | ClassName = PrevIDecl->getIdentifier(); |
| 1007 | } |
| 1008 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1009 | // If there was a forward declaration with type parameters, check |
| 1010 | // for consistency. |
| 1011 | if (PrevIDecl) { |
| 1012 | if (ObjCTypeParamList *prevTypeParamList = PrevIDecl->getTypeParamList()) { |
| 1013 | if (typeParamList) { |
| 1014 | // Both have type parameter lists; check for consistency. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1015 | if (checkTypeParamListConsistency(*this, prevTypeParamList, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1016 | typeParamList, |
| 1017 | TypeParamListContext::Definition)) { |
| 1018 | typeParamList = nullptr; |
| 1019 | } |
| 1020 | } else { |
| 1021 | Diag(ClassLoc, diag::err_objc_parameterized_forward_class_first) |
| 1022 | << ClassName; |
| 1023 | Diag(prevTypeParamList->getLAngleLoc(), diag::note_previous_decl) |
| 1024 | << ClassName; |
| 1025 | |
| 1026 | // Clone the type parameter list. |
| 1027 | SmallVector<ObjCTypeParamDecl *, 4> clonedTypeParams; |
| 1028 | for (auto typeParam : *prevTypeParamList) { |
| 1029 | clonedTypeParams.push_back( |
| 1030 | ObjCTypeParamDecl::Create( |
| 1031 | Context, |
| 1032 | CurContext, |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 1033 | typeParam->getVariance(), |
| 1034 | SourceLocation(), |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1035 | typeParam->getIndex(), |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1036 | SourceLocation(), |
| 1037 | typeParam->getIdentifier(), |
| 1038 | SourceLocation(), |
| 1039 | Context.getTrivialTypeSourceInfo(typeParam->getUnderlyingType()))); |
| 1040 | } |
| 1041 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1042 | typeParamList = ObjCTypeParamList::create(Context, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1043 | SourceLocation(), |
| 1044 | clonedTypeParams, |
| 1045 | SourceLocation()); |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1050 | ObjCInterfaceDecl *IDecl |
| 1051 | = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1052 | typeParamList, PrevIDecl, ClassLoc); |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1053 | if (PrevIDecl) { |
| 1054 | // Class already seen. Was it a definition? |
| 1055 | if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) { |
| 1056 | Diag(AtInterfaceLoc, diag::err_duplicate_class_def) |
| 1057 | << PrevIDecl->getDeclName(); |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 1058 | Diag(Def->getLocation(), diag::note_previous_definition); |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1059 | IDecl->setInvalidDecl(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1060 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1061 | } |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1062 | |
| 1063 | ProcessDeclAttributeList(TUScope, IDecl, AttrList); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1064 | AddPragmaAttributes(TUScope, IDecl); |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1065 | PushOnScopeChains(IDecl, TUScope); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1067 | // 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] | 1068 | // 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] | 1069 | if (!IDecl->hasDefinition()) |
| 1070 | IDecl->startDefinition(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1071 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1072 | if (SuperName) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1073 | // Diagnose availability in the context of the @interface. |
| 1074 | ContextRAII SavedContext(*this, IDecl); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1075 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1076 | ActOnSuperClassOfClassInterface(S, AtInterfaceLoc, IDecl, |
| 1077 | ClassName, ClassLoc, |
| 1078 | SuperName, SuperLoc, SuperTypeArgs, |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1079 | SuperTypeArgsRange); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1080 | } else { // we have a root class. |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 1081 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1082 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | |
Sebastian Redl | e7c1fe6 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 1084 | // Check then save referenced protocols. |
Chris Lattner | df59f5a | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 1085 | if (NumProtoRefs) { |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1086 | diagnoseUseOfProtocols(*this, IDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
| 1087 | NumProtoRefs, ProtoLocs); |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 1088 | IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 1089 | ProtoLocs, Context); |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 1090 | IDecl->setEndOfDefinitionLoc(EndProtoLoc); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1091 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1093 | CheckObjCDeclScope(IDecl); |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1094 | return ActOnObjCContainerStartDefinition(IDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Fariborz Jahanian | b7c5f74 | 2013-09-25 19:36:32 +0000 | [diff] [blame] | 1097 | /// ActOnTypedefedProtocols - this action finds protocol list as part of the |
| 1098 | /// typedef'ed use for a qualified super class and adds them to the list |
| 1099 | /// of the protocols. |
| 1100 | void Sema::ActOnTypedefedProtocols(SmallVectorImpl<Decl *> &ProtocolRefs, |
Argyrios Kyrtzidis | f95a000 | 2016-11-09 02:47:07 +0000 | [diff] [blame] | 1101 | SmallVectorImpl<SourceLocation> &ProtocolLocs, |
Fariborz Jahanian | b7c5f74 | 2013-09-25 19:36:32 +0000 | [diff] [blame] | 1102 | IdentifierInfo *SuperName, |
| 1103 | SourceLocation SuperLoc) { |
| 1104 | if (!SuperName) |
| 1105 | return; |
| 1106 | NamedDecl* IDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
| 1107 | LookupOrdinaryName); |
| 1108 | if (!IDecl) |
| 1109 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1110 | |
Fariborz Jahanian | b7c5f74 | 2013-09-25 19:36:32 +0000 | [diff] [blame] | 1111 | if (const TypedefNameDecl *TDecl = dyn_cast_or_null<TypedefNameDecl>(IDecl)) { |
| 1112 | QualType T = TDecl->getUnderlyingType(); |
| 1113 | if (T->isObjCObjectType()) |
Argyrios Kyrtzidis | f95a000 | 2016-11-09 02:47:07 +0000 | [diff] [blame] | 1114 | if (const ObjCObjectType *OPT = T->getAs<ObjCObjectType>()) { |
Benjamin Kramer | f989042 | 2015-02-17 16:48:30 +0000 | [diff] [blame] | 1115 | ProtocolRefs.append(OPT->qual_begin(), OPT->qual_end()); |
Argyrios Kyrtzidis | f95a000 | 2016-11-09 02:47:07 +0000 | [diff] [blame] | 1116 | // FIXME: Consider whether this should be an invalid loc since the loc |
| 1117 | // is not actually pointing to a protocol name reference but to the |
| 1118 | // typedef reference. Note that the base class name loc is also pointing |
| 1119 | // at the typedef. |
| 1120 | ProtocolLocs.append(OPT->getNumProtocols(), SuperLoc); |
| 1121 | } |
Fariborz Jahanian | b7c5f74 | 2013-09-25 19:36:32 +0000 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | |
Richard Smith | ac4e36d | 2012-08-08 23:32:13 +0000 | [diff] [blame] | 1125 | /// ActOnCompatibilityAlias - this action is called after complete parsing of |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 1126 | /// a \@compatibility_alias declaration. It sets up the alias relationships. |
Richard Smith | ac4e36d | 2012-08-08 23:32:13 +0000 | [diff] [blame] | 1127 | Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc, |
| 1128 | IdentifierInfo *AliasName, |
| 1129 | SourceLocation AliasLocation, |
| 1130 | IdentifierInfo *ClassName, |
| 1131 | SourceLocation ClassLocation) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1132 | // Look for previous declaration of alias name |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1133 | NamedDecl *ADecl = |
| 1134 | LookupSingleName(TUScope, AliasName, AliasLocation, LookupOrdinaryName, |
| 1135 | forRedeclarationInCurContext()); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1136 | if (ADecl) { |
Eli Friedman | fd6b3f8 | 2013-06-21 01:49:53 +0000 | [diff] [blame] | 1137 | Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; |
Chris Lattner | d068503 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 1138 | Diag(ADecl->getLocation(), diag::note_previous_declaration); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1139 | return nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1140 | } |
| 1141 | // Check for class declaration |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1142 | NamedDecl *CDeclU = |
| 1143 | LookupSingleName(TUScope, ClassName, ClassLocation, LookupOrdinaryName, |
| 1144 | forRedeclarationInCurContext()); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1145 | if (const TypedefNameDecl *TDecl = |
| 1146 | dyn_cast_or_null<TypedefNameDecl>(CDeclU)) { |
Fariborz Jahanian | 17290c3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 1147 | QualType T = TDecl->getUnderlyingType(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1148 | if (T->isObjCObjectType()) { |
| 1149 | if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) { |
Fariborz Jahanian | 17290c3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 1150 | ClassName = IDecl->getIdentifier(); |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1151 | CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1152 | LookupOrdinaryName, |
| 1153 | forRedeclarationInCurContext()); |
Fariborz Jahanian | 17290c3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | } |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 1157 | ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1158 | if (!CDecl) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1159 | Diag(ClassLocation, diag::warn_undef_interface) << ClassName; |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 1160 | if (CDeclU) |
Chris Lattner | d068503 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 1161 | Diag(CDeclU->getLocation(), diag::note_previous_declaration); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1162 | return nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1163 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1164 | |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 1165 | // Everything checked out, instantiate a new alias declaration AST. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1166 | ObjCCompatibleAliasDecl *AliasDecl = |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1167 | ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1169 | if (!CheckObjCDeclScope(AliasDecl)) |
Douglas Gregor | 38feed8 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 1170 | PushOnScopeChains(AliasDecl, TUScope); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1171 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1172 | return AliasDecl; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1175 | bool Sema::CheckForwardProtocolDeclarationForCircularDependency( |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1176 | IdentifierInfo *PName, |
| 1177 | SourceLocation &Ploc, SourceLocation PrevLoc, |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1178 | const ObjCList<ObjCProtocolDecl> &PList) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1179 | |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1180 | bool res = false; |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1181 | for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(), |
| 1182 | E = PList.end(); I != E; ++I) { |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1183 | if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(), |
| 1184 | Ploc)) { |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1185 | if (PDecl->getIdentifier() == PName) { |
| 1186 | Diag(Ploc, diag::err_protocol_has_circular_dependency); |
| 1187 | Diag(PrevLoc, diag::note_previous_definition); |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1188 | res = true; |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1189 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1190 | |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 1191 | if (!PDecl->hasDefinition()) |
| 1192 | continue; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1193 | |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1194 | if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc, |
| 1195 | PDecl->getLocation(), PDecl->getReferencedProtocols())) |
| 1196 | res = true; |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1197 | } |
| 1198 | } |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1199 | return res; |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1202 | Decl *Sema::ActOnStartProtocolInterface( |
| 1203 | SourceLocation AtProtoInterfaceLoc, IdentifierInfo *ProtocolName, |
| 1204 | SourceLocation ProtocolLoc, Decl *const *ProtoRefs, unsigned NumProtoRefs, |
| 1205 | const SourceLocation *ProtoLocs, SourceLocation EndProtoLoc, |
| 1206 | const ParsedAttributesView &AttrList) { |
Fariborz Jahanian | cadf7c5 | 2011-05-12 22:04:39 +0000 | [diff] [blame] | 1207 | bool err = false; |
Daniel Dunbar | 26e2ab4 | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 1208 | // FIXME: Deal with AttrList. |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1209 | assert(ProtocolName && "Missing protocol identifier"); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1210 | ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1211 | forRedeclarationInCurContext()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1212 | ObjCProtocolDecl *PDecl = nullptr; |
| 1213 | if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : nullptr) { |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1214 | // If we already have a definition, complain. |
| 1215 | Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName; |
| 1216 | Diag(Def->getLocation(), diag::note_previous_definition); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1217 | |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1218 | // Create a new protocol that is completely distinct from previous |
| 1219 | // declarations, and do not make this protocol available for name lookup. |
| 1220 | // That way, we'll end up completely ignoring the duplicate. |
| 1221 | // FIXME: Can we turn this into an error? |
| 1222 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName, |
| 1223 | ProtocolLoc, AtProtoInterfaceLoc, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1224 | /*PrevDecl=*/nullptr); |
Bruno Cardoso Lopes | 7dcf23e | 2018-06-30 00:49:27 +0000 | [diff] [blame] | 1225 | |
| 1226 | // If we are using modules, add the decl to the context in order to |
| 1227 | // serialize something meaningful. |
| 1228 | if (getLangOpts().Modules) |
| 1229 | PushOnScopeChains(PDecl, TUScope); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1230 | PDecl->startDefinition(); |
| 1231 | } else { |
| 1232 | if (PrevDecl) { |
| 1233 | // Check for circular dependencies among protocol declarations. This can |
| 1234 | // only happen if this protocol was forward-declared. |
Argyrios Kyrtzidis | 95dfc12 | 2011-11-13 22:08:30 +0000 | [diff] [blame] | 1235 | ObjCList<ObjCProtocolDecl> PList; |
| 1236 | PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context); |
| 1237 | err = CheckForwardProtocolDeclarationForCircularDependency( |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1238 | ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList); |
Argyrios Kyrtzidis | 95dfc12 | 2011-11-13 22:08:30 +0000 | [diff] [blame] | 1239 | } |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1240 | |
| 1241 | // Create the new declaration. |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 1242 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName, |
Argyrios Kyrtzidis | 1f4bee5 | 2011-10-17 19:48:06 +0000 | [diff] [blame] | 1243 | ProtocolLoc, AtProtoInterfaceLoc, |
Douglas Gregor | 05a1f4d | 2012-01-01 22:06:18 +0000 | [diff] [blame] | 1244 | /*PrevDecl=*/PrevDecl); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1245 | |
Douglas Gregor | de9f17e | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 1246 | PushOnScopeChains(PDecl, TUScope); |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 1247 | PDecl->startDefinition(); |
Chris Lattner | f87ca0a | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 1248 | } |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1249 | |
| 1250 | ProcessDeclAttributeList(TUScope, PDecl, AttrList); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1251 | AddPragmaAttributes(TUScope, PDecl); |
| 1252 | |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1253 | // Merge attributes from previous declarations. |
| 1254 | if (PrevDecl) |
| 1255 | mergeDeclAttributes(PDecl, PrevDecl); |
| 1256 | |
Fariborz Jahanian | cadf7c5 | 2011-05-12 22:04:39 +0000 | [diff] [blame] | 1257 | if (!err && NumProtoRefs ) { |
Chris Lattner | acc04a9 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 1258 | /// Check then save referenced protocols. |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1259 | diagnoseUseOfProtocols(*this, PDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
| 1260 | NumProtoRefs, ProtoLocs); |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 1261 | PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 1262 | ProtoLocs, Context); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1263 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1264 | |
| 1265 | CheckObjCDeclScope(PDecl); |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1266 | return ActOnObjCContainerStartDefinition(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1269 | static bool NestedProtocolHasNoDefinition(ObjCProtocolDecl *PDecl, |
| 1270 | ObjCProtocolDecl *&UndefinedProtocol) { |
| 1271 | if (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()) { |
| 1272 | UndefinedProtocol = PDecl; |
| 1273 | return true; |
| 1274 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1275 | |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 1276 | for (auto *PI : PDecl->protocols()) |
| 1277 | if (NestedProtocolHasNoDefinition(PI, UndefinedProtocol)) { |
| 1278 | UndefinedProtocol = PI; |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1279 | return true; |
| 1280 | } |
| 1281 | return false; |
| 1282 | } |
| 1283 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1284 | /// FindProtocolDeclaration - This routine looks up protocols and |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1285 | /// issues an error if they are not declared. It returns list of |
| 1286 | /// protocol declarations in its 'Protocols' argument. |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1287 | void |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1288 | Sema::FindProtocolDeclaration(bool WarnOnDeclarations, bool ForObjCContainer, |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1289 | ArrayRef<IdentifierLocPair> ProtocolId, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1290 | SmallVectorImpl<Decl *> &Protocols) { |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1291 | for (const IdentifierLocPair &Pair : ProtocolId) { |
| 1292 | ObjCProtocolDecl *PDecl = LookupProtocol(Pair.first, Pair.second); |
Chris Lattner | 9c1842b | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 1293 | if (!PDecl) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1294 | TypoCorrection Corrected = CorrectTypo( |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1295 | DeclarationNameInfo(Pair.first, Pair.second), |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 1296 | LookupObjCProtocolName, TUScope, nullptr, |
| 1297 | llvm::make_unique<DeclFilterCCC<ObjCProtocolDecl>>(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1298 | CTK_ErrorRecovery); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1299 | if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) |
| 1300 | diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest) |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1301 | << Pair.first); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | if (!PDecl) { |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1305 | Diag(Pair.second, diag::err_undeclared_protocol) << Pair.first; |
Chris Lattner | 9c1842b | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 1306 | continue; |
| 1307 | } |
Fariborz Jahanian | ada44a2 | 2013-04-09 17:52:29 +0000 | [diff] [blame] | 1308 | // If this is a forward protocol declaration, get its definition. |
| 1309 | if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition()) |
| 1310 | PDecl = PDecl->getDefinition(); |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1311 | |
| 1312 | // For an objc container, delay protocol reference checking until after we |
| 1313 | // can set the objc decl as the availability context, otherwise check now. |
| 1314 | if (!ForObjCContainer) { |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1315 | (void)DiagnoseUseOfDecl(PDecl, Pair.second); |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1316 | } |
Chris Lattner | 9c1842b | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 1317 | |
| 1318 | // If this is a forward declaration and we are supposed to warn in this |
| 1319 | // case, do it. |
Douglas Gregor | eed4979 | 2013-01-17 00:38:46 +0000 | [diff] [blame] | 1320 | // FIXME: Recover nicely in the hidden case. |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1321 | ObjCProtocolDecl *UndefinedProtocol; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1322 | |
Douglas Gregor | eed4979 | 2013-01-17 00:38:46 +0000 | [diff] [blame] | 1323 | if (WarnOnDeclarations && |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1324 | NestedProtocolHasNoDefinition(PDecl, UndefinedProtocol)) { |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1325 | Diag(Pair.second, diag::warn_undef_protocolref) << Pair.first; |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1326 | Diag(UndefinedProtocol->getLocation(), diag::note_protocol_decl_undefined) |
| 1327 | << UndefinedProtocol; |
| 1328 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1329 | Protocols.push_back(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1330 | } |
| 1331 | } |
| 1332 | |
Benjamin Kramer | 8b851d0 | 2015-07-13 20:42:13 +0000 | [diff] [blame] | 1333 | namespace { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1334 | // Callback to only accept typo corrections that are either |
| 1335 | // Objective-C protocols or valid Objective-C type arguments. |
| 1336 | class ObjCTypeArgOrProtocolValidatorCCC : public CorrectionCandidateCallback { |
| 1337 | ASTContext &Context; |
| 1338 | Sema::LookupNameKind LookupKind; |
| 1339 | public: |
| 1340 | ObjCTypeArgOrProtocolValidatorCCC(ASTContext &context, |
| 1341 | Sema::LookupNameKind lookupKind) |
| 1342 | : Context(context), LookupKind(lookupKind) { } |
| 1343 | |
| 1344 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
| 1345 | // If we're allowed to find protocols and we have a protocol, accept it. |
| 1346 | if (LookupKind != Sema::LookupOrdinaryName) { |
| 1347 | if (candidate.getCorrectionDeclAs<ObjCProtocolDecl>()) |
| 1348 | return true; |
| 1349 | } |
| 1350 | |
| 1351 | // If we're allowed to find type names and we have one, accept it. |
| 1352 | if (LookupKind != Sema::LookupObjCProtocolName) { |
| 1353 | // If we have a type declaration, we might accept this result. |
| 1354 | if (auto typeDecl = candidate.getCorrectionDeclAs<TypeDecl>()) { |
| 1355 | // If we found a tag declaration outside of C++, skip it. This |
| 1356 | // can happy because we look for any name when there is no |
| 1357 | // bias to protocol or type names. |
| 1358 | if (isa<RecordDecl>(typeDecl) && !Context.getLangOpts().CPlusPlus) |
| 1359 | return false; |
| 1360 | |
| 1361 | // Make sure the type is something we would accept as a type |
| 1362 | // argument. |
| 1363 | auto type = Context.getTypeDeclType(typeDecl); |
| 1364 | if (type->isObjCObjectPointerType() || |
| 1365 | type->isBlockPointerType() || |
| 1366 | type->isDependentType() || |
| 1367 | type->isObjCObjectType()) |
| 1368 | return true; |
| 1369 | |
| 1370 | return false; |
| 1371 | } |
| 1372 | |
| 1373 | // If we have an Objective-C class type, accept it; there will |
| 1374 | // be another fix to add the '*'. |
| 1375 | if (candidate.getCorrectionDeclAs<ObjCInterfaceDecl>()) |
| 1376 | return true; |
| 1377 | |
| 1378 | return false; |
| 1379 | } |
| 1380 | |
| 1381 | return false; |
| 1382 | } |
| 1383 | }; |
Benjamin Kramer | 8b851d0 | 2015-07-13 20:42:13 +0000 | [diff] [blame] | 1384 | } // end anonymous namespace |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1385 | |
Bruno Cardoso Lopes | c54768f | 2016-04-13 20:59:07 +0000 | [diff] [blame] | 1386 | void Sema::DiagnoseTypeArgsAndProtocols(IdentifierInfo *ProtocolId, |
| 1387 | SourceLocation ProtocolLoc, |
| 1388 | IdentifierInfo *TypeArgId, |
| 1389 | SourceLocation TypeArgLoc, |
| 1390 | bool SelectProtocolFirst) { |
| 1391 | Diag(TypeArgLoc, diag::err_objc_type_args_and_protocols) |
| 1392 | << SelectProtocolFirst << TypeArgId << ProtocolId |
| 1393 | << SourceRange(ProtocolLoc); |
| 1394 | } |
| 1395 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1396 | void Sema::actOnObjCTypeArgsOrProtocolQualifiers( |
| 1397 | Scope *S, |
Douglas Gregor | 10dc9d8 | 2015-07-07 03:58:28 +0000 | [diff] [blame] | 1398 | ParsedType baseType, |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1399 | SourceLocation lAngleLoc, |
| 1400 | ArrayRef<IdentifierInfo *> identifiers, |
| 1401 | ArrayRef<SourceLocation> identifierLocs, |
| 1402 | SourceLocation rAngleLoc, |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1403 | SourceLocation &typeArgsLAngleLoc, |
| 1404 | SmallVectorImpl<ParsedType> &typeArgs, |
| 1405 | SourceLocation &typeArgsRAngleLoc, |
| 1406 | SourceLocation &protocolLAngleLoc, |
| 1407 | SmallVectorImpl<Decl *> &protocols, |
| 1408 | SourceLocation &protocolRAngleLoc, |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1409 | bool warnOnIncompleteProtocols) { |
| 1410 | // Local function that updates the declaration specifiers with |
| 1411 | // protocol information. |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1412 | unsigned numProtocolsResolved = 0; |
| 1413 | auto resolvedAsProtocols = [&] { |
| 1414 | assert(numProtocolsResolved == identifiers.size() && "Unresolved protocols"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1415 | |
Douglas Gregor | 10dc9d8 | 2015-07-07 03:58:28 +0000 | [diff] [blame] | 1416 | // Determine whether the base type is a parameterized class, in |
| 1417 | // which case we want to warn about typos such as |
| 1418 | // "NSArray<NSObject>" (that should be NSArray<NSObject *>). |
| 1419 | ObjCInterfaceDecl *baseClass = nullptr; |
| 1420 | QualType base = GetTypeFromParser(baseType, nullptr); |
| 1421 | bool allAreTypeNames = false; |
| 1422 | SourceLocation firstClassNameLoc; |
| 1423 | if (!base.isNull()) { |
| 1424 | if (const auto *objcObjectType = base->getAs<ObjCObjectType>()) { |
| 1425 | baseClass = objcObjectType->getInterface(); |
| 1426 | if (baseClass) { |
| 1427 | if (auto typeParams = baseClass->getTypeParamList()) { |
| 1428 | if (typeParams->size() == numProtocolsResolved) { |
| 1429 | // Note that we should be looking for type names, too. |
| 1430 | allAreTypeNames = true; |
| 1431 | } |
| 1432 | } |
| 1433 | } |
| 1434 | } |
| 1435 | } |
| 1436 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1437 | for (unsigned i = 0, n = protocols.size(); i != n; ++i) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1438 | ObjCProtocolDecl *&proto |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1439 | = reinterpret_cast<ObjCProtocolDecl *&>(protocols[i]); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1440 | // For an objc container, delay protocol reference checking until after we |
| 1441 | // can set the objc decl as the availability context, otherwise check now. |
| 1442 | if (!warnOnIncompleteProtocols) { |
| 1443 | (void)DiagnoseUseOfDecl(proto, identifierLocs[i]); |
| 1444 | } |
| 1445 | |
| 1446 | // If this is a forward protocol declaration, get its definition. |
| 1447 | if (!proto->isThisDeclarationADefinition() && proto->getDefinition()) |
| 1448 | proto = proto->getDefinition(); |
| 1449 | |
| 1450 | // If this is a forward declaration and we are supposed to warn in this |
| 1451 | // case, do it. |
| 1452 | // FIXME: Recover nicely in the hidden case. |
| 1453 | ObjCProtocolDecl *forwardDecl = nullptr; |
| 1454 | if (warnOnIncompleteProtocols && |
| 1455 | NestedProtocolHasNoDefinition(proto, forwardDecl)) { |
| 1456 | Diag(identifierLocs[i], diag::warn_undef_protocolref) |
| 1457 | << proto->getDeclName(); |
| 1458 | Diag(forwardDecl->getLocation(), diag::note_protocol_decl_undefined) |
| 1459 | << forwardDecl; |
| 1460 | } |
Douglas Gregor | 10dc9d8 | 2015-07-07 03:58:28 +0000 | [diff] [blame] | 1461 | |
| 1462 | // If everything this far has been a type name (and we care |
| 1463 | // about such things), check whether this name refers to a type |
| 1464 | // as well. |
| 1465 | if (allAreTypeNames) { |
| 1466 | if (auto *decl = LookupSingleName(S, identifiers[i], identifierLocs[i], |
| 1467 | LookupOrdinaryName)) { |
| 1468 | if (isa<ObjCInterfaceDecl>(decl)) { |
| 1469 | if (firstClassNameLoc.isInvalid()) |
| 1470 | firstClassNameLoc = identifierLocs[i]; |
| 1471 | } else if (!isa<TypeDecl>(decl)) { |
| 1472 | // Not a type. |
| 1473 | allAreTypeNames = false; |
| 1474 | } |
| 1475 | } else { |
| 1476 | allAreTypeNames = false; |
| 1477 | } |
| 1478 | } |
| 1479 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1480 | |
Douglas Gregor | 10dc9d8 | 2015-07-07 03:58:28 +0000 | [diff] [blame] | 1481 | // All of the protocols listed also have type names, and at least |
| 1482 | // one is an Objective-C class name. Check whether all of the |
| 1483 | // protocol conformances are declared by the base class itself, in |
| 1484 | // which case we warn. |
| 1485 | if (allAreTypeNames && firstClassNameLoc.isValid()) { |
| 1486 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> knownProtocols; |
| 1487 | Context.CollectInheritedProtocols(baseClass, knownProtocols); |
| 1488 | bool allProtocolsDeclared = true; |
| 1489 | for (auto proto : protocols) { |
| 1490 | if (knownProtocols.count(static_cast<ObjCProtocolDecl *>(proto)) == 0) { |
| 1491 | allProtocolsDeclared = false; |
| 1492 | break; |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | if (allProtocolsDeclared) { |
| 1497 | Diag(firstClassNameLoc, diag::warn_objc_redundant_qualified_class_type) |
| 1498 | << baseClass->getDeclName() << SourceRange(lAngleLoc, rAngleLoc) |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 1499 | << FixItHint::CreateInsertion(getLocForEndOfToken(firstClassNameLoc), |
| 1500 | " *"); |
Douglas Gregor | 10dc9d8 | 2015-07-07 03:58:28 +0000 | [diff] [blame] | 1501 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1504 | protocolLAngleLoc = lAngleLoc; |
| 1505 | protocolRAngleLoc = rAngleLoc; |
| 1506 | assert(protocols.size() == identifierLocs.size()); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1507 | }; |
| 1508 | |
| 1509 | // Attempt to resolve all of the identifiers as protocols. |
| 1510 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1511 | ObjCProtocolDecl *proto = LookupProtocol(identifiers[i], identifierLocs[i]); |
| 1512 | protocols.push_back(proto); |
| 1513 | if (proto) |
| 1514 | ++numProtocolsResolved; |
| 1515 | } |
| 1516 | |
| 1517 | // If all of the names were protocols, these were protocol qualifiers. |
| 1518 | if (numProtocolsResolved == identifiers.size()) |
| 1519 | return resolvedAsProtocols(); |
| 1520 | |
| 1521 | // Attempt to resolve all of the identifiers as type names or |
| 1522 | // Objective-C class names. The latter is technically ill-formed, |
| 1523 | // but is probably something like \c NSArray<NSView *> missing the |
| 1524 | // \c*. |
| 1525 | typedef llvm::PointerUnion<TypeDecl *, ObjCInterfaceDecl *> TypeOrClassDecl; |
| 1526 | SmallVector<TypeOrClassDecl, 4> typeDecls; |
| 1527 | unsigned numTypeDeclsResolved = 0; |
| 1528 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1529 | NamedDecl *decl = LookupSingleName(S, identifiers[i], identifierLocs[i], |
| 1530 | LookupOrdinaryName); |
| 1531 | if (!decl) { |
| 1532 | typeDecls.push_back(TypeOrClassDecl()); |
| 1533 | continue; |
| 1534 | } |
| 1535 | |
| 1536 | if (auto typeDecl = dyn_cast<TypeDecl>(decl)) { |
| 1537 | typeDecls.push_back(typeDecl); |
| 1538 | ++numTypeDeclsResolved; |
| 1539 | continue; |
| 1540 | } |
| 1541 | |
| 1542 | if (auto objcClass = dyn_cast<ObjCInterfaceDecl>(decl)) { |
| 1543 | typeDecls.push_back(objcClass); |
| 1544 | ++numTypeDeclsResolved; |
| 1545 | continue; |
| 1546 | } |
| 1547 | |
| 1548 | typeDecls.push_back(TypeOrClassDecl()); |
| 1549 | } |
| 1550 | |
| 1551 | AttributeFactory attrFactory; |
| 1552 | |
| 1553 | // Local function that forms a reference to the given type or |
| 1554 | // Objective-C class declaration. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1555 | auto resolveTypeReference = [&](TypeOrClassDecl typeDecl, SourceLocation loc) |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1556 | -> TypeResult { |
| 1557 | // Form declaration specifiers. They simply refer to the type. |
| 1558 | DeclSpec DS(attrFactory); |
| 1559 | const char* prevSpec; // unused |
| 1560 | unsigned diagID; // unused |
| 1561 | QualType type; |
| 1562 | if (auto *actualTypeDecl = typeDecl.dyn_cast<TypeDecl *>()) |
| 1563 | type = Context.getTypeDeclType(actualTypeDecl); |
| 1564 | else |
| 1565 | type = Context.getObjCInterfaceType(typeDecl.get<ObjCInterfaceDecl *>()); |
| 1566 | TypeSourceInfo *parsedTSInfo = Context.getTrivialTypeSourceInfo(type, loc); |
| 1567 | ParsedType parsedType = CreateParsedType(type, parsedTSInfo); |
| 1568 | DS.SetTypeSpecType(DeclSpec::TST_typename, loc, prevSpec, diagID, |
| 1569 | parsedType, Context.getPrintingPolicy()); |
| 1570 | // Use the identifier location for the type source range. |
| 1571 | DS.SetRangeStart(loc); |
| 1572 | DS.SetRangeEnd(loc); |
| 1573 | |
| 1574 | // Form the declarator. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1575 | Declarator D(DS, DeclaratorContext::TypeNameContext); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1576 | |
| 1577 | // If we have a typedef of an Objective-C class type that is missing a '*', |
| 1578 | // add the '*'. |
| 1579 | if (type->getAs<ObjCInterfaceType>()) { |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 1580 | SourceLocation starLoc = getLocForEndOfToken(loc); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1581 | D.AddTypeInfo(DeclaratorChunk::getPointer(/*typeQuals=*/0, starLoc, |
| 1582 | SourceLocation(), |
| 1583 | SourceLocation(), |
| 1584 | SourceLocation(), |
Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 1585 | SourceLocation(), |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1586 | SourceLocation()), |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1587 | starLoc); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1588 | |
| 1589 | // Diagnose the missing '*'. |
| 1590 | Diag(loc, diag::err_objc_type_arg_missing_star) |
| 1591 | << type |
| 1592 | << FixItHint::CreateInsertion(starLoc, " *"); |
| 1593 | } |
| 1594 | |
| 1595 | // Convert this to a type. |
| 1596 | return ActOnTypeName(S, D); |
| 1597 | }; |
| 1598 | |
| 1599 | // Local function that updates the declaration specifiers with |
| 1600 | // type argument information. |
| 1601 | auto resolvedAsTypeDecls = [&] { |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1602 | // We did not resolve these as protocols. |
| 1603 | protocols.clear(); |
| 1604 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1605 | assert(numTypeDeclsResolved == identifiers.size() && "Unresolved type decl"); |
| 1606 | // Map type declarations to type arguments. |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1607 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1608 | // Map type reference to a type. |
| 1609 | TypeResult type = resolveTypeReference(typeDecls[i], identifierLocs[i]); |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1610 | if (!type.isUsable()) { |
| 1611 | typeArgs.clear(); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1612 | return; |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1613 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1614 | |
| 1615 | typeArgs.push_back(type.get()); |
| 1616 | } |
| 1617 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1618 | typeArgsLAngleLoc = lAngleLoc; |
| 1619 | typeArgsRAngleLoc = rAngleLoc; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1620 | }; |
| 1621 | |
| 1622 | // If all of the identifiers can be resolved as type names or |
| 1623 | // Objective-C class names, we have type arguments. |
| 1624 | if (numTypeDeclsResolved == identifiers.size()) |
| 1625 | return resolvedAsTypeDecls(); |
| 1626 | |
| 1627 | // Error recovery: some names weren't found, or we have a mix of |
| 1628 | // type and protocol names. Go resolve all of the unresolved names |
| 1629 | // and complain if we can't find a consistent answer. |
| 1630 | LookupNameKind lookupKind = LookupAnyName; |
| 1631 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1632 | // If we already have a protocol or type. Check whether it is the |
| 1633 | // right thing. |
| 1634 | if (protocols[i] || typeDecls[i]) { |
| 1635 | // If we haven't figured out whether we want types or protocols |
| 1636 | // yet, try to figure it out from this name. |
| 1637 | if (lookupKind == LookupAnyName) { |
| 1638 | // If this name refers to both a protocol and a type (e.g., \c |
| 1639 | // NSObject), don't conclude anything yet. |
| 1640 | if (protocols[i] && typeDecls[i]) |
| 1641 | continue; |
| 1642 | |
| 1643 | // Otherwise, let this name decide whether we'll be correcting |
| 1644 | // toward types or protocols. |
| 1645 | lookupKind = protocols[i] ? LookupObjCProtocolName |
| 1646 | : LookupOrdinaryName; |
| 1647 | continue; |
| 1648 | } |
| 1649 | |
| 1650 | // If we want protocols and we have a protocol, there's nothing |
| 1651 | // more to do. |
| 1652 | if (lookupKind == LookupObjCProtocolName && protocols[i]) |
| 1653 | continue; |
| 1654 | |
| 1655 | // If we want types and we have a type declaration, there's |
| 1656 | // nothing more to do. |
| 1657 | if (lookupKind == LookupOrdinaryName && typeDecls[i]) |
| 1658 | continue; |
| 1659 | |
| 1660 | // We have a conflict: some names refer to protocols and others |
| 1661 | // refer to types. |
Bruno Cardoso Lopes | c54768f | 2016-04-13 20:59:07 +0000 | [diff] [blame] | 1662 | DiagnoseTypeArgsAndProtocols(identifiers[0], identifierLocs[0], |
| 1663 | identifiers[i], identifierLocs[i], |
| 1664 | protocols[i] != nullptr); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1665 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1666 | protocols.clear(); |
| 1667 | typeArgs.clear(); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1668 | return; |
| 1669 | } |
| 1670 | |
| 1671 | // Perform typo correction on the name. |
| 1672 | TypoCorrection corrected = CorrectTypo( |
| 1673 | DeclarationNameInfo(identifiers[i], identifierLocs[i]), lookupKind, S, |
| 1674 | nullptr, |
| 1675 | llvm::make_unique<ObjCTypeArgOrProtocolValidatorCCC>(Context, |
| 1676 | lookupKind), |
| 1677 | CTK_ErrorRecovery); |
| 1678 | if (corrected) { |
| 1679 | // Did we find a protocol? |
| 1680 | if (auto proto = corrected.getCorrectionDeclAs<ObjCProtocolDecl>()) { |
| 1681 | diagnoseTypo(corrected, |
| 1682 | PDiag(diag::err_undeclared_protocol_suggest) |
| 1683 | << identifiers[i]); |
| 1684 | lookupKind = LookupObjCProtocolName; |
| 1685 | protocols[i] = proto; |
| 1686 | ++numProtocolsResolved; |
| 1687 | continue; |
| 1688 | } |
| 1689 | |
| 1690 | // Did we find a type? |
| 1691 | if (auto typeDecl = corrected.getCorrectionDeclAs<TypeDecl>()) { |
| 1692 | diagnoseTypo(corrected, |
| 1693 | PDiag(diag::err_unknown_typename_suggest) |
| 1694 | << identifiers[i]); |
| 1695 | lookupKind = LookupOrdinaryName; |
| 1696 | typeDecls[i] = typeDecl; |
| 1697 | ++numTypeDeclsResolved; |
| 1698 | continue; |
| 1699 | } |
| 1700 | |
| 1701 | // Did we find an Objective-C class? |
| 1702 | if (auto objcClass = corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
| 1703 | diagnoseTypo(corrected, |
| 1704 | PDiag(diag::err_unknown_type_or_class_name_suggest) |
| 1705 | << identifiers[i] << true); |
| 1706 | lookupKind = LookupOrdinaryName; |
| 1707 | typeDecls[i] = objcClass; |
| 1708 | ++numTypeDeclsResolved; |
| 1709 | continue; |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | // We couldn't find anything. |
| 1714 | Diag(identifierLocs[i], |
| 1715 | (lookupKind == LookupAnyName ? diag::err_objc_type_arg_missing |
| 1716 | : lookupKind == LookupObjCProtocolName ? diag::err_undeclared_protocol |
| 1717 | : diag::err_unknown_typename)) |
| 1718 | << identifiers[i]; |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1719 | protocols.clear(); |
| 1720 | typeArgs.clear(); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1721 | return; |
| 1722 | } |
| 1723 | |
| 1724 | // If all of the names were (corrected to) protocols, these were |
| 1725 | // protocol qualifiers. |
| 1726 | if (numProtocolsResolved == identifiers.size()) |
| 1727 | return resolvedAsProtocols(); |
| 1728 | |
| 1729 | // Otherwise, all of the names were (corrected to) types. |
| 1730 | assert(numTypeDeclsResolved == identifiers.size() && "Not all types?"); |
| 1731 | return resolvedAsTypeDecls(); |
| 1732 | } |
| 1733 | |
Fariborz Jahanian | abf63e7b | 2009-03-02 19:06:08 +0000 | [diff] [blame] | 1734 | /// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1735 | /// a class method in its extension. |
| 1736 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1737 | void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1738 | ObjCInterfaceDecl *ID) { |
| 1739 | if (!ID) |
| 1740 | return; // Possibly due to previous error |
| 1741 | |
| 1742 | llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap; |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 1743 | for (auto *MD : ID->methods()) |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1744 | MethodMap[MD->getSelector()] = MD; |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1745 | |
| 1746 | if (MethodMap.empty()) |
| 1747 | return; |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 1748 | for (const auto *Method : CAT->methods()) { |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1749 | const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()]; |
Fariborz Jahanian | 83d674e | 2014-03-17 17:46:10 +0000 | [diff] [blame] | 1750 | if (PrevMethod && |
| 1751 | (PrevMethod->isInstanceMethod() == Method->isInstanceMethod()) && |
| 1752 | !MatchTwoMethodDeclarations(Method, PrevMethod)) { |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1753 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
| 1754 | << Method->getDeclName(); |
| 1755 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 1756 | } |
| 1757 | } |
| 1758 | } |
| 1759 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 1760 | /// ActOnForwardProtocolDeclaration - Handle \@protocol foo; |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1761 | Sema::DeclGroupPtrTy |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1762 | Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
Craig Topper | 0f723bb | 2015-10-22 05:00:01 +0000 | [diff] [blame] | 1763 | ArrayRef<IdentifierLocPair> IdentList, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1764 | const ParsedAttributesView &attrList) { |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1765 | SmallVector<Decl *, 8> DeclsInGroup; |
Craig Topper | 0f723bb | 2015-10-22 05:00:01 +0000 | [diff] [blame] | 1766 | for (const IdentifierLocPair &IdentPair : IdentList) { |
| 1767 | IdentifierInfo *Ident = IdentPair.first; |
| 1768 | ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentPair.second, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1769 | forRedeclarationInCurContext()); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1770 | ObjCProtocolDecl *PDecl |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1771 | = ObjCProtocolDecl::Create(Context, CurContext, Ident, |
Craig Topper | 0f723bb | 2015-10-22 05:00:01 +0000 | [diff] [blame] | 1772 | IdentPair.second, AtProtocolLoc, |
Douglas Gregor | 05a1f4d | 2012-01-01 22:06:18 +0000 | [diff] [blame] | 1773 | PrevDecl); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1774 | |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1775 | PushOnScopeChains(PDecl, TUScope); |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1776 | CheckObjCDeclScope(PDecl); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1777 | |
| 1778 | ProcessDeclAttributeList(TUScope, PDecl, attrList); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1779 | AddPragmaAttributes(TUScope, PDecl); |
| 1780 | |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1781 | if (PrevDecl) |
| 1782 | mergeDeclAttributes(PDecl, PrevDecl); |
| 1783 | |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1784 | DeclsInGroup.push_back(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1785 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1786 | |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1787 | return BuildDeclaratorGroup(DeclsInGroup); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1790 | Decl *Sema::ActOnStartCategoryInterface( |
| 1791 | SourceLocation AtInterfaceLoc, IdentifierInfo *ClassName, |
| 1792 | SourceLocation ClassLoc, ObjCTypeParamList *typeParamList, |
| 1793 | IdentifierInfo *CategoryName, SourceLocation CategoryLoc, |
| 1794 | Decl *const *ProtoRefs, unsigned NumProtoRefs, |
| 1795 | const SourceLocation *ProtoLocs, SourceLocation EndProtoLoc, |
| 1796 | const ParsedAttributesView &AttrList) { |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1797 | ObjCCategoryDecl *CDecl; |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1798 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1799 | |
| 1800 | /// Check that class of this category is already completely declared. |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1801 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1802 | if (!IDecl |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1803 | || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1804 | diag::err_category_forward_interface, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1805 | CategoryName == nullptr)) { |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1806 | // Create an invalid ObjCCategoryDecl to serve as context for |
| 1807 | // the enclosing method declarations. We mark the decl invalid |
| 1808 | // to make it clear that this isn't a valid AST. |
| 1809 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1810 | ClassLoc, CategoryLoc, CategoryName, |
| 1811 | IDecl, typeParamList); |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1812 | CDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | b15def2 | 2012-03-12 18:34:26 +0000 | [diff] [blame] | 1813 | CurContext->addDecl(CDecl); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1814 | |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1815 | if (!IDecl) |
| 1816 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1817 | return ActOnObjCContainerStartDefinition(CDecl); |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1820 | if (!CategoryName && IDecl->getImplementation()) { |
| 1821 | Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1822 | Diag(IDecl->getImplementation()->getLocation(), |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1823 | diag::note_implementation_declared); |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1826 | if (CategoryName) { |
| 1827 | /// Check for duplicate interface declaration for this category |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1828 | if (ObjCCategoryDecl *Previous |
| 1829 | = IDecl->FindCategoryDeclaration(CategoryName)) { |
| 1830 | // Class extensions can be declared multiple times, categories cannot. |
| 1831 | Diag(CategoryLoc, diag::warn_dup_category_def) |
| 1832 | << ClassName << CategoryName; |
| 1833 | Diag(Previous->getLocation(), diag::note_previous_definition); |
Chris Lattner | 9018ca8 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 1834 | } |
| 1835 | } |
Chris Lattner | 9018ca8 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 1836 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1837 | // If we have a type parameter list, check it. |
| 1838 | if (typeParamList) { |
| 1839 | if (auto prevTypeParamList = IDecl->getTypeParamList()) { |
| 1840 | if (checkTypeParamListConsistency(*this, prevTypeParamList, typeParamList, |
| 1841 | CategoryName |
| 1842 | ? TypeParamListContext::Category |
| 1843 | : TypeParamListContext::Extension)) |
| 1844 | typeParamList = nullptr; |
| 1845 | } else { |
| 1846 | Diag(typeParamList->getLAngleLoc(), |
| 1847 | diag::err_objc_parameterized_category_nonclass) |
| 1848 | << (CategoryName != nullptr) |
| 1849 | << ClassName |
| 1850 | << typeParamList->getSourceRange(); |
| 1851 | |
| 1852 | typeParamList = nullptr; |
| 1853 | } |
| 1854 | } |
| 1855 | |
Argyrios Kyrtzidis | 3a5094b | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 1856 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1857 | ClassLoc, CategoryLoc, CategoryName, IDecl, |
| 1858 | typeParamList); |
Argyrios Kyrtzidis | 3a5094b | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 1859 | // FIXME: PushOnScopeChains? |
| 1860 | CurContext->addDecl(CDecl); |
| 1861 | |
Alex Lorenz | a9c966d | 2018-02-23 23:49:43 +0000 | [diff] [blame] | 1862 | // Process the attributes before looking at protocols to ensure that the |
| 1863 | // availability attribute is attached to the category to provide availability |
| 1864 | // checking for protocol uses. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1865 | ProcessDeclAttributeList(TUScope, CDecl, AttrList); |
Alex Lorenz | a9c966d | 2018-02-23 23:49:43 +0000 | [diff] [blame] | 1866 | AddPragmaAttributes(TUScope, CDecl); |
| 1867 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1868 | if (NumProtoRefs) { |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1869 | diagnoseUseOfProtocols(*this, CDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
| 1870 | NumProtoRefs, ProtoLocs); |
| 1871 | CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 1872 | ProtoLocs, Context); |
Fariborz Jahanian | 092cd6e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 1873 | // Protocols in the class extension belong to the class. |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1874 | if (CDecl->IsClassExtension()) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1875 | IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs, |
| 1876 | NumProtoRefs, Context); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1877 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1878 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1879 | CheckObjCDeclScope(CDecl); |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1880 | return ActOnObjCContainerStartDefinition(CDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | /// ActOnStartCategoryImplementation - Perform semantic checks on the |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1884 | /// category implementation declaration and build an ObjCCategoryImplDecl |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1885 | /// object. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1886 | Decl *Sema::ActOnStartCategoryImplementation( |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1887 | SourceLocation AtCatImplLoc, |
| 1888 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 1889 | IdentifierInfo *CatName, SourceLocation CatLoc) { |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1890 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1891 | ObjCCategoryDecl *CatIDecl = nullptr; |
Argyrios Kyrtzidis | 4af2cb3 | 2012-03-02 19:14:29 +0000 | [diff] [blame] | 1892 | if (IDecl && IDecl->hasDefinition()) { |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1893 | CatIDecl = IDecl->FindCategoryDeclaration(CatName); |
| 1894 | if (!CatIDecl) { |
| 1895 | // Category @implementation with no corresponding @interface. |
| 1896 | // Create and install one. |
Argyrios Kyrtzidis | 41fc05c | 2011-11-23 20:27:26 +0000 | [diff] [blame] | 1897 | CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc, |
| 1898 | ClassLoc, CatLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1899 | CatName, IDecl, |
| 1900 | /*typeParamList=*/nullptr); |
Argyrios Kyrtzidis | 41fc05c | 2011-11-23 20:27:26 +0000 | [diff] [blame] | 1901 | CatIDecl->setImplicit(); |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1902 | } |
| 1903 | } |
| 1904 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1905 | ObjCCategoryImplDecl *CDecl = |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 1906 | ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl, |
Argyrios Kyrtzidis | 4996f5f | 2011-12-09 00:31:40 +0000 | [diff] [blame] | 1907 | ClassLoc, AtCatImplLoc, CatLoc); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1908 | /// Check that class of this category is already completely declared. |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1909 | if (!IDecl) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1910 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 1911 | CDecl->setInvalidDecl(); |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1912 | } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
| 1913 | diag::err_undef_interface)) { |
| 1914 | CDecl->setInvalidDecl(); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 1915 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1916 | |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1917 | // FIXME: PushOnScopeChains? |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1918 | CurContext->addDecl(CDecl); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1919 | |
Douglas Gregor | 24ae22c | 2016-04-01 23:23:52 +0000 | [diff] [blame] | 1920 | // If the interface has the objc_runtime_visible attribute, we |
| 1921 | // cannot implement a category for it. |
| 1922 | if (IDecl && IDecl->hasAttr<ObjCRuntimeVisibleAttr>()) { |
| 1923 | Diag(ClassLoc, diag::err_objc_runtime_visible_category) |
| 1924 | << IDecl->getDeclName(); |
| 1925 | } |
| 1926 | |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1927 | /// Check that CatName, category name, is not used in another implementation. |
| 1928 | if (CatIDecl) { |
| 1929 | if (CatIDecl->getImplementation()) { |
| 1930 | Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName |
| 1931 | << CatName; |
| 1932 | Diag(CatIDecl->getImplementation()->getLocation(), |
| 1933 | diag::note_previous_definition); |
Argyrios Kyrtzidis | 0f6d5ca | 2013-05-30 18:53:21 +0000 | [diff] [blame] | 1934 | CDecl->setInvalidDecl(); |
Fariborz Jahanian | d33ab8c | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 1935 | } else { |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1936 | CatIDecl->setImplementation(CDecl); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1937 | // Warn on implementating category of deprecated class under |
Fariborz Jahanian | d33ab8c | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 1938 | // -Wdeprecated-implementations flag. |
Alex Lorenz | f81d97e | 2017-07-13 16:35:59 +0000 | [diff] [blame] | 1939 | DiagnoseObjCImplementedDeprecations(*this, CatIDecl, |
| 1940 | CDecl->getLocation()); |
Fariborz Jahanian | d33ab8c | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 1941 | } |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1942 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1943 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1944 | CheckObjCDeclScope(CDecl); |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1945 | return ActOnObjCContainerStartDefinition(CDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1948 | Decl *Sema::ActOnStartClassImplementation( |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1949 | SourceLocation AtClassImplLoc, |
| 1950 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1951 | IdentifierInfo *SuperClassname, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1952 | SourceLocation SuperClassLoc) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1953 | ObjCInterfaceDecl *IDecl = nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1954 | // Check for another declaration kind with the same name. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1955 | NamedDecl *PrevDecl |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 1956 | = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1957 | forRedeclarationInCurContext()); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1958 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1959 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1960 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1961 | } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) { |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 1962 | // FIXME: This will produce an error if the definition of the interface has |
| 1963 | // been imported from a module but is not visible. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 1964 | RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
| 1965 | diag::warn_undef_interface); |
Douglas Gregor | 40f7a00 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 1966 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1967 | // 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] | 1968 | // typos in the class name. |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 1969 | TypoCorrection Corrected = CorrectTypo( |
| 1970 | DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope, |
| 1971 | nullptr, llvm::make_unique<ObjCInterfaceValidatorCCC>(), CTK_NonError); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1972 | if (Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
| 1973 | // Suggest the (potentially) correct interface name. Don't provide a |
| 1974 | // code-modification hint or use the typo name for recovery, because |
| 1975 | // this is just a warning. The program may actually be correct. |
| 1976 | diagnoseTypo(Corrected, |
| 1977 | PDiag(diag::warn_undef_interface_suggest) << ClassName, |
| 1978 | /*ErrorRecovery*/false); |
Douglas Gregor | 40f7a00 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 1979 | } else { |
| 1980 | Diag(ClassLoc, diag::warn_undef_interface) << ClassName; |
| 1981 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1982 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1983 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1984 | // Check that super class name is valid class name |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1985 | ObjCInterfaceDecl *SDecl = nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1986 | if (SuperClassname) { |
| 1987 | // Check if a different kind of symbol declared in this scope. |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1988 | PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc, |
| 1989 | LookupOrdinaryName); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1990 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1991 | Diag(SuperClassLoc, diag::err_redefinition_different_kind) |
| 1992 | << SuperClassname; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1993 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1994 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1995 | SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Argyrios Kyrtzidis | 3b60cff | 2012-03-13 01:09:36 +0000 | [diff] [blame] | 1996 | if (SDecl && !SDecl->hasDefinition()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1997 | SDecl = nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1998 | if (!SDecl) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1999 | Diag(SuperClassLoc, diag::err_undef_superclass) |
| 2000 | << SuperClassname << ClassName; |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 2001 | else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2002 | // This implementation and its interface do not have the same |
| 2003 | // super class. |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2004 | Diag(SuperClassLoc, diag::err_conflicting_super_class) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2005 | << SDecl->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2006 | Diag(SDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2007 | } |
| 2008 | } |
| 2009 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2010 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2011 | if (!IDecl) { |
| 2012 | // Legacy case of @implementation with no corresponding @interface. |
| 2013 | // Build, chain & install the interface decl into the identifier. |
Daniel Dunbar | 73a73f5 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 2014 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2015 | // FIXME: Do we support attributes on the @implementation? If so we should |
| 2016 | // copy them over. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2017 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 2018 | ClassName, /*typeParamList=*/nullptr, |
| 2019 | /*PrevDecl=*/nullptr, ClassLoc, |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 2020 | true); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 2021 | AddPragmaAttributes(TUScope, IDecl); |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 2022 | IDecl->startDefinition(); |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 2023 | if (SDecl) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 2024 | IDecl->setSuperClass(Context.getTrivialTypeSourceInfo( |
| 2025 | Context.getObjCInterfaceType(SDecl), |
| 2026 | SuperClassLoc)); |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 2027 | IDecl->setEndOfDefinitionLoc(SuperClassLoc); |
| 2028 | } else { |
| 2029 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
| 2030 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2031 | |
Douglas Gregor | ac345a3 | 2009-04-24 00:16:12 +0000 | [diff] [blame] | 2032 | PushOnScopeChains(IDecl, TUScope); |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2033 | } else { |
| 2034 | // Mark the interface as being completed, even if it was just as |
| 2035 | // @class ....; |
| 2036 | // declaration; the user cannot reopen it. |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 2037 | if (!IDecl->hasDefinition()) |
| 2038 | IDecl->startDefinition(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2039 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2040 | |
| 2041 | ObjCImplementationDecl* IMPDecl = |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 2042 | ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl, |
Argyrios Kyrtzidis | fac3162 | 2013-05-03 18:05:44 +0000 | [diff] [blame] | 2043 | ClassLoc, AtClassImplLoc, SuperClassLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2044 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 2045 | if (CheckObjCDeclScope(IMPDecl)) |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 2046 | return ActOnObjCContainerStartDefinition(IMPDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2047 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2048 | // Check that there is no duplicate implementation of this class. |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2049 | if (IDecl->getImplementation()) { |
| 2050 | // FIXME: Don't leak everything! |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2051 | Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; |
Argyrios Kyrtzidis | 43cee935 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 2052 | Diag(IDecl->getImplementation()->getLocation(), |
| 2053 | diag::note_previous_definition); |
Argyrios Kyrtzidis | 0f6d5ca | 2013-05-30 18:53:21 +0000 | [diff] [blame] | 2054 | IMPDecl->setInvalidDecl(); |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2055 | } else { // add it to the list. |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2056 | IDecl->setImplementation(IMPDecl); |
Douglas Gregor | 79947a2 | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 2057 | PushOnScopeChains(IMPDecl, TUScope); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2058 | // Warn on implementating deprecated class under |
Fariborz Jahanian | d33ab8c | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 2059 | // -Wdeprecated-implementations flag. |
Alex Lorenz | f81d97e | 2017-07-13 16:35:59 +0000 | [diff] [blame] | 2060 | DiagnoseObjCImplementedDeprecations(*this, IDecl, IMPDecl->getLocation()); |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2061 | } |
Douglas Gregor | 24ae22c | 2016-04-01 23:23:52 +0000 | [diff] [blame] | 2062 | |
| 2063 | // If the superclass has the objc_runtime_visible attribute, we |
| 2064 | // cannot implement a subclass of it. |
| 2065 | if (IDecl->getSuperClass() && |
| 2066 | IDecl->getSuperClass()->hasAttr<ObjCRuntimeVisibleAttr>()) { |
| 2067 | Diag(ClassLoc, diag::err_objc_runtime_visible_subclass) |
| 2068 | << IDecl->getDeclName() |
| 2069 | << IDecl->getSuperClass()->getDeclName(); |
| 2070 | } |
| 2071 | |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 2072 | return ActOnObjCContainerStartDefinition(IMPDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
Argyrios Kyrtzidis | 2e85c5f | 2012-02-23 21:11:20 +0000 | [diff] [blame] | 2075 | Sema::DeclGroupPtrTy |
| 2076 | Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) { |
| 2077 | SmallVector<Decl *, 64> DeclsInGroup; |
| 2078 | DeclsInGroup.reserve(Decls.size() + 1); |
| 2079 | |
| 2080 | for (unsigned i = 0, e = Decls.size(); i != e; ++i) { |
| 2081 | Decl *Dcl = Decls[i]; |
| 2082 | if (!Dcl) |
| 2083 | continue; |
| 2084 | if (Dcl->getDeclContext()->isFileContext()) |
| 2085 | Dcl->setTopLevelDeclInObjCContainer(); |
| 2086 | DeclsInGroup.push_back(Dcl); |
| 2087 | } |
| 2088 | |
| 2089 | DeclsInGroup.push_back(ObjCImpDecl); |
| 2090 | |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2091 | return BuildDeclaratorGroup(DeclsInGroup); |
Argyrios Kyrtzidis | 2e85c5f | 2012-02-23 21:11:20 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2094 | void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, |
| 2095 | ObjCIvarDecl **ivars, unsigned numIvars, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2096 | SourceLocation RBrace) { |
| 2097 | assert(ImpDecl && "missing implementation decl"); |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2098 | ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2099 | if (!IDecl) |
| 2100 | return; |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2101 | /// Check case of non-existing \@interface decl. |
| 2102 | /// (legacy objective-c \@implementation decl without an \@interface decl). |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2103 | /// Add implementations's ivar to the synthesize class's ivar list. |
Steve Naroff | aac654a | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 2104 | if (IDecl->isImplicitInterfaceDecl()) { |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 2105 | IDecl->setEndOfDefinitionLoc(RBrace); |
Fariborz Jahanian | 20912d6 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 2106 | // Add ivar's to class's DeclContext. |
| 2107 | for (unsigned i = 0, e = numIvars; i != e; ++i) { |
Fariborz Jahanian | c0309cd | 2010-02-17 18:10:54 +0000 | [diff] [blame] | 2108 | ivars[i]->setLexicalDeclContext(ImpDecl); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 2109 | IDecl->makeDeclVisibleInContext(ivars[i]); |
Fariborz Jahanian | aef6622 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 2110 | ImpDecl->addDecl(ivars[i]); |
Fariborz Jahanian | 20912d6 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 2111 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2112 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2113 | return; |
| 2114 | } |
| 2115 | // If implementation has empty ivar list, just return. |
| 2116 | if (numIvars == 0) |
| 2117 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2118 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2119 | assert(ivars && "missing @implementation ivars"); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2120 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | 34e3cef | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 2121 | if (ImpDecl->getSuperClass()) |
| 2122 | Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use); |
| 2123 | for (unsigned i = 0; i < numIvars; i++) { |
| 2124 | ObjCIvarDecl* ImplIvar = ivars[i]; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2125 | if (const ObjCIvarDecl *ClsIvar = |
Fariborz Jahanian | 34e3cef | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 2126 | IDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2127 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
Fariborz Jahanian | 34e3cef | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 2128 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
| 2129 | continue; |
| 2130 | } |
Fariborz Jahanian | e23f26b | 2013-06-26 22:10:27 +0000 | [diff] [blame] | 2131 | // Check class extensions (unnamed categories) for duplicate ivars. |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 2132 | for (const auto *CDecl : IDecl->visible_extensions()) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2133 | if (const ObjCIvarDecl *ClsExtIvar = |
Fariborz Jahanian | e23f26b | 2013-06-26 22:10:27 +0000 | [diff] [blame] | 2134 | CDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2135 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
Fariborz Jahanian | e23f26b | 2013-06-26 22:10:27 +0000 | [diff] [blame] | 2136 | Diag(ClsExtIvar->getLocation(), diag::note_previous_definition); |
| 2137 | continue; |
| 2138 | } |
| 2139 | } |
Fariborz Jahanian | 34e3cef | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 2140 | // Instance ivar to Implementation's DeclContext. |
| 2141 | ImplIvar->setLexicalDeclContext(ImpDecl); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 2142 | IDecl->makeDeclVisibleInContext(ImplIvar); |
Fariborz Jahanian | 34e3cef | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 2143 | ImpDecl->addDecl(ImplIvar); |
| 2144 | } |
| 2145 | return; |
| 2146 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2147 | // Check interface's Ivar list against those in the implementation. |
| 2148 | // names and types must match. |
| 2149 | // |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2150 | unsigned j = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2151 | ObjCInterfaceDecl::ivar_iterator |
Chris Lattner | 061227a | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 2152 | IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); |
| 2153 | for (; numIvars > 0 && IVI != IVE; ++IVI) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2154 | ObjCIvarDecl* ImplIvar = ivars[j++]; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2155 | ObjCIvarDecl* ClsIvar = *IVI; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2156 | assert (ImplIvar && "missing implementation ivar"); |
| 2157 | assert (ClsIvar && "missing class ivar"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2158 | |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 2159 | // First, make sure the types match. |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2160 | if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2161 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2162 | << ImplIvar->getIdentifier() |
| 2163 | << ImplIvar->getType() << ClsIvar->getType(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2164 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2165 | } else if (ImplIvar->isBitField() && ClsIvar->isBitField() && |
| 2166 | ImplIvar->getBitWidthValue(Context) != |
| 2167 | ClsIvar->getBitWidthValue(Context)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2168 | Diag(ImplIvar->getBitWidth()->getBeginLoc(), |
| 2169 | diag::err_conflicting_ivar_bitwidth) |
| 2170 | << ImplIvar->getIdentifier(); |
| 2171 | Diag(ClsIvar->getBitWidth()->getBeginLoc(), |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2172 | diag::note_previous_definition); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2173 | } |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 2174 | // Make sure the names are identical. |
| 2175 | if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2176 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2177 | << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2178 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2179 | } |
| 2180 | --numIvars; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2181 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2182 | |
Chris Lattner | 0f29d98 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 2183 | if (numIvars > 0) |
Alp Toker | fff0674 | 2013-12-02 03:50:21 +0000 | [diff] [blame] | 2184 | Diag(ivars[j]->getLocation(), diag::err_inconsistent_ivar_count); |
Chris Lattner | 0f29d98 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 2185 | else if (IVI != IVE) |
Alp Toker | fff0674 | 2013-12-02 03:50:21 +0000 | [diff] [blame] | 2186 | Diag(IVI->getLocation(), diag::err_inconsistent_ivar_count); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
Ted Kremenek | f87decd | 2013-12-13 05:58:44 +0000 | [diff] [blame] | 2189 | static void WarnUndefinedMethod(Sema &S, SourceLocation ImpLoc, |
| 2190 | ObjCMethodDecl *method, |
| 2191 | bool &IncompleteImpl, |
Ted Kremenek | 2ccf19e | 2013-12-13 05:58:51 +0000 | [diff] [blame] | 2192 | unsigned DiagID, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2193 | NamedDecl *NeededFor = nullptr) { |
Fariborz Jahanian | 9fc39c4 | 2011-06-24 20:31:37 +0000 | [diff] [blame] | 2194 | // No point warning no definition of method which is 'unavailable'. |
Erik Pilkington | ecce5c9 | 2018-07-07 01:50:20 +0000 | [diff] [blame] | 2195 | if (method->getAvailability() == AR_Unavailable) |
Fariborz Jahanian | 9fc39c4 | 2011-06-24 20:31:37 +0000 | [diff] [blame] | 2196 | return; |
Erik Pilkington | ecce5c9 | 2018-07-07 01:50:20 +0000 | [diff] [blame] | 2197 | |
Ted Kremenek | 65d6357 | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 2198 | // FIXME: For now ignore 'IncompleteImpl'. |
| 2199 | // Previously we grouped all unimplemented methods under a single |
| 2200 | // warning, but some users strongly voiced that they would prefer |
| 2201 | // separate warnings. We will give that approach a try, as that |
| 2202 | // matches what we do with protocols. |
Ted Kremenek | 2ccf19e | 2013-12-13 05:58:51 +0000 | [diff] [blame] | 2203 | { |
| 2204 | const Sema::SemaDiagnosticBuilder &B = S.Diag(ImpLoc, DiagID); |
| 2205 | B << method; |
| 2206 | if (NeededFor) |
| 2207 | B << NeededFor; |
| 2208 | } |
Ted Kremenek | 65d6357 | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 2209 | |
| 2210 | // Issue a note to the original declaration. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2211 | SourceLocation MethodLoc = method->getBeginLoc(); |
Ted Kremenek | 65d6357 | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 2212 | if (MethodLoc.isValid()) |
Ted Kremenek | f87decd | 2013-12-13 05:58:44 +0000 | [diff] [blame] | 2213 | S.Diag(MethodLoc, diag::note_method_declared_at) << method; |
Steve Naroff | 15833ed | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2216 | /// 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] | 2217 | /// guarantee that anything that the user will do to an object of type A can |
| 2218 | /// 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] | 2219 | /// types are the same, or if B is a subclass of A. It becomes more complex |
| 2220 | /// in cases where protocols are involved. |
| 2221 | /// |
| 2222 | /// Object types in Objective-C describe the minimum requirements for an |
| 2223 | /// object, rather than providing a complete description of a type. For |
| 2224 | /// example, if A is a subclass of B, then B* may refer to an instance of A. |
| 2225 | /// The principle of substitutability means that we may use an instance of A |
| 2226 | /// 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] | 2227 | /// ivars of B and all of the methods of B. |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2228 | /// |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2229 | /// This substitutability is important when type checking methods, because |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2230 | /// the implementation may have stricter type definitions than the interface. |
| 2231 | /// The interface specifies minimum requirements, but the implementation may |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2232 | /// have more accurate ones. For example, a method may privately accept |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2233 | /// instances of B, but only publish that it accepts instances of A. Any |
| 2234 | /// object passed to it will be type checked against B, and so will implicitly |
| 2235 | /// by a valid A*. Similarly, a method may return a subclass of the class that |
| 2236 | /// it is declared as returning. |
| 2237 | /// |
| 2238 | /// This is most important when considering subclassing. A method in a |
| 2239 | /// subclass must accept any object as an argument that its superclass's |
| 2240 | /// implementation accepts. It may, however, accept a more general type |
| 2241 | /// without breaking substitutability (i.e. you can still use the subclass |
| 2242 | /// anywhere that you can use the superclass, but not vice versa). The |
| 2243 | /// converse requirement applies to return types: the return type for a |
| 2244 | /// subclass method must be a valid object of the kind that the superclass |
| 2245 | /// advertises, but it may be specified more accurately. This avoids the need |
| 2246 | /// for explicit down-casting by callers. |
| 2247 | /// |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2248 | /// Note: This is a stricter requirement than for assignment. |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2249 | static bool isObjCTypeSubstitutable(ASTContext &Context, |
| 2250 | const ObjCObjectPointerType *A, |
| 2251 | const ObjCObjectPointerType *B, |
| 2252 | bool rejectId) { |
| 2253 | // Reject a protocol-unqualified id. |
| 2254 | if (rejectId && B->isObjCIdType()) return false; |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2255 | |
| 2256 | // If B is a qualified id, then A must also be a qualified id and it must |
| 2257 | // implement all of the protocols in B. It may not be a qualified class. |
| 2258 | // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a |
| 2259 | // stricter definition so it is not substitutable for id<A>. |
| 2260 | if (B->isObjCQualifiedIdType()) { |
| 2261 | return A->isObjCQualifiedIdType() && |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2262 | Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0), |
| 2263 | QualType(B,0), |
| 2264 | false); |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | /* |
| 2268 | // id is a special type that bypasses type checking completely. We want a |
| 2269 | // warning when it is used in one place but not another. |
| 2270 | if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false; |
| 2271 | |
| 2272 | |
| 2273 | // If B is a qualified id, then A must also be a qualified id (which it isn't |
| 2274 | // if we've got this far) |
| 2275 | if (B->isObjCQualifiedIdType()) return false; |
| 2276 | */ |
| 2277 | |
| 2278 | // Now we know that A and B are (potentially-qualified) class types. The |
| 2279 | // normal rules for assignment apply. |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2280 | return Context.canAssignObjCInterfaces(A, B); |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2283 | static SourceRange getTypeRange(TypeSourceInfo *TSI) { |
| 2284 | return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange()); |
| 2285 | } |
| 2286 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2287 | /// Determine whether two set of Objective-C declaration qualifiers conflict. |
| 2288 | static bool objcModifiersConflict(Decl::ObjCDeclQualifier x, |
| 2289 | Decl::ObjCDeclQualifier y) { |
| 2290 | return (x & ~Decl::OBJC_TQ_CSNullability) != |
| 2291 | (y & ~Decl::OBJC_TQ_CSNullability); |
| 2292 | } |
| 2293 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2294 | static bool CheckMethodOverrideReturn(Sema &S, |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2295 | ObjCMethodDecl *MethodImpl, |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2296 | ObjCMethodDecl *MethodDecl, |
Fariborz Jahanian | 4ceec3f | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 2297 | bool IsProtocolMethodDecl, |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2298 | bool IsOverridingMode, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2299 | bool Warn) { |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2300 | if (IsProtocolMethodDecl && |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2301 | objcModifiersConflict(MethodDecl->getObjCDeclQualifier(), |
| 2302 | MethodImpl->getObjCDeclQualifier())) { |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2303 | if (Warn) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2304 | S.Diag(MethodImpl->getLocation(), |
| 2305 | (IsOverridingMode |
| 2306 | ? diag::warn_conflicting_overriding_ret_type_modifiers |
| 2307 | : diag::warn_conflicting_ret_type_modifiers)) |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2308 | << MethodImpl->getDeclName() |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 2309 | << MethodImpl->getReturnTypeSourceRange(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2310 | S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration) |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 2311 | << MethodDecl->getReturnTypeSourceRange(); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2312 | } |
| 2313 | else |
| 2314 | return false; |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2315 | } |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2316 | if (Warn && IsOverridingMode && |
| 2317 | !isa<ObjCImplementationDecl>(MethodImpl->getDeclContext()) && |
| 2318 | !S.Context.hasSameNullabilityTypeQualifier(MethodImpl->getReturnType(), |
| 2319 | MethodDecl->getReturnType(), |
| 2320 | false)) { |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 2321 | auto nullabilityMethodImpl = |
| 2322 | *MethodImpl->getReturnType()->getNullability(S.Context); |
| 2323 | auto nullabilityMethodDecl = |
| 2324 | *MethodDecl->getReturnType()->getNullability(S.Context); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2325 | S.Diag(MethodImpl->getLocation(), |
| 2326 | diag::warn_conflicting_nullability_attr_overriding_ret_types) |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 2327 | << DiagNullabilityKind( |
| 2328 | nullabilityMethodImpl, |
| 2329 | ((MethodImpl->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2330 | != 0)) |
| 2331 | << DiagNullabilityKind( |
| 2332 | nullabilityMethodDecl, |
| 2333 | ((MethodDecl->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2334 | != 0)); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2335 | S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration); |
| 2336 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2337 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2338 | if (S.Context.hasSameUnqualifiedType(MethodImpl->getReturnType(), |
| 2339 | MethodDecl->getReturnType())) |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2340 | return true; |
| 2341 | if (!Warn) |
| 2342 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2343 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2344 | unsigned DiagID = |
| 2345 | IsOverridingMode ? diag::warn_conflicting_overriding_ret_types |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2346 | : diag::warn_conflicting_ret_types; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2347 | |
| 2348 | // Mismatches between ObjC pointers go into a different warning |
| 2349 | // category, and sometimes they're even completely whitelisted. |
| 2350 | if (const ObjCObjectPointerType *ImplPtrTy = |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2351 | MethodImpl->getReturnType()->getAs<ObjCObjectPointerType>()) { |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2352 | if (const ObjCObjectPointerType *IfacePtrTy = |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2353 | MethodDecl->getReturnType()->getAs<ObjCObjectPointerType>()) { |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2354 | // Allow non-matching return types as long as they don't violate |
| 2355 | // the principle of substitutability. Specifically, we permit |
| 2356 | // return types that are subclasses of the declared return type, |
| 2357 | // or that are more-qualified versions of the declared type. |
| 2358 | if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false)) |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2359 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2360 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2361 | DiagID = |
| 2362 | IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types |
Craig Topper | 8f7f3ea | 2015-11-17 05:40:05 +0000 | [diff] [blame] | 2363 | : diag::warn_non_covariant_ret_types; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | S.Diag(MethodImpl->getLocation(), DiagID) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2368 | << MethodImpl->getDeclName() << MethodDecl->getReturnType() |
| 2369 | << MethodImpl->getReturnType() |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 2370 | << MethodImpl->getReturnTypeSourceRange(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2371 | S.Diag(MethodDecl->getLocation(), IsOverridingMode |
| 2372 | ? diag::note_previous_declaration |
| 2373 | : diag::note_previous_definition) |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 2374 | << MethodDecl->getReturnTypeSourceRange(); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2375 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2376 | } |
| 2377 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2378 | static bool CheckMethodOverrideParam(Sema &S, |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2379 | ObjCMethodDecl *MethodImpl, |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2380 | ObjCMethodDecl *MethodDecl, |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2381 | ParmVarDecl *ImplVar, |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2382 | ParmVarDecl *IfaceVar, |
Fariborz Jahanian | 4ceec3f | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 2383 | bool IsProtocolMethodDecl, |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2384 | bool IsOverridingMode, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2385 | bool Warn) { |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2386 | if (IsProtocolMethodDecl && |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2387 | objcModifiersConflict(ImplVar->getObjCDeclQualifier(), |
| 2388 | IfaceVar->getObjCDeclQualifier())) { |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2389 | if (Warn) { |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2390 | if (IsOverridingMode) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2391 | S.Diag(ImplVar->getLocation(), |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2392 | diag::warn_conflicting_overriding_param_modifiers) |
| 2393 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
| 2394 | << MethodImpl->getDeclName(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2395 | else S.Diag(ImplVar->getLocation(), |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2396 | diag::warn_conflicting_param_modifiers) |
| 2397 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2398 | << MethodImpl->getDeclName(); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2399 | S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2400 | << getTypeRange(IfaceVar->getTypeSourceInfo()); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2401 | } |
| 2402 | else |
| 2403 | return false; |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2404 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2405 | |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2406 | QualType ImplTy = ImplVar->getType(); |
| 2407 | QualType IfaceTy = IfaceVar->getType(); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2408 | if (Warn && IsOverridingMode && |
| 2409 | !isa<ObjCImplementationDecl>(MethodImpl->getDeclContext()) && |
| 2410 | !S.Context.hasSameNullabilityTypeQualifier(ImplTy, IfaceTy, true)) { |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2411 | S.Diag(ImplVar->getLocation(), |
| 2412 | diag::warn_conflicting_nullability_attr_overriding_param_types) |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 2413 | << DiagNullabilityKind( |
| 2414 | *ImplTy->getNullability(S.Context), |
| 2415 | ((ImplVar->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2416 | != 0)) |
| 2417 | << DiagNullabilityKind( |
| 2418 | *IfaceTy->getNullability(S.Context), |
| 2419 | ((IfaceVar->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2420 | != 0)); |
| 2421 | S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2422 | } |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2423 | if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy)) |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2424 | return true; |
Manman Ren | c5705ba | 2016-09-13 17:41:05 +0000 | [diff] [blame] | 2425 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2426 | if (!Warn) |
| 2427 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2428 | unsigned DiagID = |
| 2429 | IsOverridingMode ? diag::warn_conflicting_overriding_param_types |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2430 | : diag::warn_conflicting_param_types; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2431 | |
| 2432 | // Mismatches between ObjC pointers go into a different warning |
| 2433 | // category, and sometimes they're even completely whitelisted. |
| 2434 | if (const ObjCObjectPointerType *ImplPtrTy = |
| 2435 | ImplTy->getAs<ObjCObjectPointerType>()) { |
| 2436 | if (const ObjCObjectPointerType *IfacePtrTy = |
| 2437 | IfaceTy->getAs<ObjCObjectPointerType>()) { |
| 2438 | // Allow non-matching argument types as long as they don't |
| 2439 | // violate the principle of substitutability. Specifically, the |
| 2440 | // implementation must accept any objects that the superclass |
| 2441 | // accepts, however it may also accept others. |
| 2442 | if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true)) |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2443 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2444 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2445 | DiagID = |
| 2446 | IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types |
Craig Topper | 8f7f3ea | 2015-11-17 05:40:05 +0000 | [diff] [blame] | 2447 | : diag::warn_non_contravariant_param_types; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2448 | } |
| 2449 | } |
| 2450 | |
| 2451 | S.Diag(ImplVar->getLocation(), DiagID) |
| 2452 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2453 | << MethodImpl->getDeclName() << IfaceTy << ImplTy; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2454 | S.Diag(IfaceVar->getLocation(), |
| 2455 | (IsOverridingMode ? diag::note_previous_declaration |
Craig Topper | 8f7f3ea | 2015-11-17 05:40:05 +0000 | [diff] [blame] | 2456 | : diag::note_previous_definition)) |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2457 | << getTypeRange(IfaceVar->getTypeSourceInfo()); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2458 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2459 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2460 | |
| 2461 | /// In ARC, check whether the conventional meanings of the two methods |
| 2462 | /// match. If they don't, it's a hard error. |
| 2463 | static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl, |
| 2464 | ObjCMethodDecl *decl) { |
| 2465 | ObjCMethodFamily implFamily = impl->getMethodFamily(); |
| 2466 | ObjCMethodFamily declFamily = decl->getMethodFamily(); |
| 2467 | if (implFamily == declFamily) return false; |
| 2468 | |
| 2469 | // Since conventions are sorted by selector, the only possibility is |
| 2470 | // that the types differ enough to cause one selector or the other |
| 2471 | // to fall out of the family. |
| 2472 | assert(implFamily == OMF_None || declFamily == OMF_None); |
| 2473 | |
| 2474 | // No further diagnostics required on invalid declarations. |
| 2475 | if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true; |
| 2476 | |
| 2477 | const ObjCMethodDecl *unmatched = impl; |
| 2478 | ObjCMethodFamily family = declFamily; |
| 2479 | unsigned errorID = diag::err_arc_lost_method_convention; |
| 2480 | unsigned noteID = diag::note_arc_lost_method_convention; |
| 2481 | if (declFamily == OMF_None) { |
| 2482 | unmatched = decl; |
| 2483 | family = implFamily; |
| 2484 | errorID = diag::err_arc_gained_method_convention; |
| 2485 | noteID = diag::note_arc_gained_method_convention; |
| 2486 | } |
| 2487 | |
| 2488 | // Indexes into a %select clause in the diagnostic. |
| 2489 | enum FamilySelector { |
| 2490 | F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new |
| 2491 | }; |
| 2492 | FamilySelector familySelector = FamilySelector(); |
| 2493 | |
| 2494 | switch (family) { |
| 2495 | case OMF_None: llvm_unreachable("logic error, no method convention"); |
| 2496 | case OMF_retain: |
| 2497 | case OMF_release: |
| 2498 | case OMF_autorelease: |
| 2499 | case OMF_dealloc: |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 2500 | case OMF_finalize: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2501 | case OMF_retainCount: |
| 2502 | case OMF_self: |
Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 2503 | case OMF_initialize: |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 2504 | case OMF_performSelector: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2505 | // Mismatches for these methods don't change ownership |
| 2506 | // conventions, so we don't care. |
| 2507 | return false; |
| 2508 | |
| 2509 | case OMF_init: familySelector = F_init; break; |
| 2510 | case OMF_alloc: familySelector = F_alloc; break; |
| 2511 | case OMF_copy: familySelector = F_copy; break; |
| 2512 | case OMF_mutableCopy: familySelector = F_mutableCopy; break; |
| 2513 | case OMF_new: familySelector = F_new; break; |
| 2514 | } |
| 2515 | |
| 2516 | enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn }; |
| 2517 | ReasonSelector reasonSelector; |
| 2518 | |
| 2519 | // The only reason these methods don't fall within their families is |
| 2520 | // due to unusual result types. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2521 | if (unmatched->getReturnType()->isObjCObjectPointerType()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2522 | reasonSelector = R_UnrelatedReturn; |
| 2523 | } else { |
| 2524 | reasonSelector = R_NonObjectReturn; |
| 2525 | } |
| 2526 | |
Joerg Sonnenberger | ffc6d49 | 2013-06-26 21:31:47 +0000 | [diff] [blame] | 2527 | S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector); |
| 2528 | S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2529 | |
| 2530 | return true; |
| 2531 | } |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2532 | |
Fariborz Jahanian | 7988d7d | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 2533 | void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2534 | ObjCMethodDecl *MethodDecl, |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2535 | bool IsProtocolMethodDecl) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2536 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2537 | checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl)) |
| 2538 | return; |
| 2539 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2540 | CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
| 2541 | IsProtocolMethodDecl, false, |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2542 | true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2543 | |
Chris Lattner | 67f35b0 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 2544 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2545 | IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(), |
| 2546 | EF = MethodDecl->param_end(); |
| 2547 | IM != EM && IF != EF; ++IM, ++IF) { |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2548 | CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF, |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2549 | IsProtocolMethodDecl, false, true); |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2550 | } |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2551 | |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2552 | if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2553 | Diag(ImpMethodDecl->getLocation(), |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2554 | diag::warn_conflicting_variadic); |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2555 | Diag(MethodDecl->getLocation(), diag::note_previous_declaration); |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2556 | } |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2557 | } |
| 2558 | |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2559 | void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method, |
| 2560 | ObjCMethodDecl *Overridden, |
| 2561 | bool IsProtocolMethodDecl) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2562 | |
| 2563 | CheckMethodOverrideReturn(*this, Method, Overridden, |
| 2564 | IsProtocolMethodDecl, true, |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2565 | true); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2566 | |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2567 | for (ObjCMethodDecl::param_iterator IM = Method->param_begin(), |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2568 | IF = Overridden->param_begin(), EM = Method->param_end(), |
| 2569 | EF = Overridden->param_end(); |
| 2570 | IM != EM && IF != EF; ++IM, ++IF) { |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2571 | CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF, |
| 2572 | IsProtocolMethodDecl, true, true); |
| 2573 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2574 | |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2575 | if (Method->isVariadic() != Overridden->isVariadic()) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2576 | Diag(Method->getLocation(), |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2577 | diag::warn_conflicting_overriding_variadic); |
| 2578 | Diag(Overridden->getLocation(), diag::note_previous_declaration); |
| 2579 | } |
| 2580 | } |
| 2581 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2582 | /// WarnExactTypedMethods - This routine issues a warning if method |
| 2583 | /// implementation declaration matches exactly that of its declaration. |
| 2584 | void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
| 2585 | ObjCMethodDecl *MethodDecl, |
| 2586 | bool IsProtocolMethodDecl) { |
| 2587 | // don't issue warning when protocol method is optional because primary |
| 2588 | // class is not required to implement it and it is safe for protocol |
| 2589 | // to implement it. |
| 2590 | if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional) |
| 2591 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2592 | // don't issue warning when primary class's method is |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2593 | // depecated/unavailable. |
| 2594 | if (MethodDecl->hasAttr<UnavailableAttr>() || |
| 2595 | MethodDecl->hasAttr<DeprecatedAttr>()) |
| 2596 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2597 | |
| 2598 | bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2599 | IsProtocolMethodDecl, false, false); |
| 2600 | if (match) |
| 2601 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2602 | IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(), |
| 2603 | EF = MethodDecl->param_end(); |
| 2604 | IM != EM && IF != EF; ++IM, ++IF) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2605 | match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2606 | *IM, *IF, |
| 2607 | IsProtocolMethodDecl, false, false); |
| 2608 | if (!match) |
| 2609 | break; |
| 2610 | } |
| 2611 | if (match) |
| 2612 | match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic()); |
David Chisnall | 9291851 | 2011-08-08 17:32:19 +0000 | [diff] [blame] | 2613 | if (match) |
| 2614 | match = !(MethodDecl->isClassMethod() && |
| 2615 | MethodDecl->getSelector() == GetNullarySelector("load", Context)); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2616 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2617 | if (match) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2618 | Diag(ImpMethodDecl->getLocation(), |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2619 | diag::warn_category_method_impl_match); |
Ted Kremenek | 59b10db | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 2620 | Diag(MethodDecl->getLocation(), diag::note_method_declared_at) |
| 2621 | << MethodDecl->getDeclName(); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2622 | } |
| 2623 | } |
| 2624 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2625 | /// FIXME: Type hierarchies in Objective-C can be deep. We could most likely |
| 2626 | /// improve the efficiency of selector lookups and type checking by associating |
| 2627 | /// with each protocol / interface / category the flattened instance tables. If |
| 2628 | /// we used an immutable set to keep the table then it wouldn't add significant |
| 2629 | /// memory cost and it would be handy for lookups. |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 2630 | |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2631 | typedef llvm::DenseSet<IdentifierInfo*> ProtocolNameSet; |
Ahmed Charles | af94d56 | 2014-03-09 11:34:25 +0000 | [diff] [blame] | 2632 | typedef std::unique_ptr<ProtocolNameSet> LazyProtocolNameSet; |
Ted Kremenek | 760a2ac | 2014-03-05 23:18:22 +0000 | [diff] [blame] | 2633 | |
| 2634 | static void findProtocolsWithExplicitImpls(const ObjCProtocolDecl *PDecl, |
| 2635 | ProtocolNameSet &PNS) { |
| 2636 | if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) |
| 2637 | PNS.insert(PDecl->getIdentifier()); |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 2638 | for (const auto *PI : PDecl->protocols()) |
| 2639 | findProtocolsWithExplicitImpls(PI, PNS); |
Ted Kremenek | 760a2ac | 2014-03-05 23:18:22 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2642 | /// Recursively populates a set with all conformed protocols in a class |
| 2643 | /// hierarchy that have the 'objc_protocol_requires_explicit_implementation' |
| 2644 | /// attribute. |
| 2645 | static void findProtocolsWithExplicitImpls(const ObjCInterfaceDecl *Super, |
| 2646 | ProtocolNameSet &PNS) { |
| 2647 | if (!Super) |
| 2648 | return; |
| 2649 | |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2650 | for (const auto *I : Super->all_referenced_protocols()) |
| 2651 | findProtocolsWithExplicitImpls(I, PNS); |
Ted Kremenek | 760a2ac | 2014-03-05 23:18:22 +0000 | [diff] [blame] | 2652 | |
| 2653 | findProtocolsWithExplicitImpls(Super->getSuperClass(), PNS); |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Steve Naroff | a3699224 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 2656 | /// CheckProtocolMethodDefs - This routine checks unimplemented methods |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2657 | /// Declared in protocol, and those referenced by it. |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2658 | static void CheckProtocolMethodDefs(Sema &S, |
| 2659 | SourceLocation ImpLoc, |
| 2660 | ObjCProtocolDecl *PDecl, |
| 2661 | bool& IncompleteImpl, |
| 2662 | const Sema::SelectorSet &InsMap, |
| 2663 | const Sema::SelectorSet &ClsMap, |
Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 2664 | ObjCContainerDecl *CDecl, |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2665 | LazyProtocolNameSet &ProtocolsExplictImpl) { |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2666 | ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2667 | ObjCInterfaceDecl *IDecl = C ? C->getClassInterface() |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2668 | : dyn_cast<ObjCInterfaceDecl>(CDecl); |
Fariborz Jahanian | 2e8074b | 2010-03-27 21:10:05 +0000 | [diff] [blame] | 2669 | assert (IDecl && "CheckProtocolMethodDefs - IDecl is null"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2670 | |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 2671 | ObjCInterfaceDecl *Super = IDecl->getSuperClass(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2672 | ObjCInterfaceDecl *NSIDecl = nullptr; |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2673 | |
| 2674 | // If this protocol is marked 'objc_protocol_requires_explicit_implementation' |
| 2675 | // then we should check if any class in the super class hierarchy also |
| 2676 | // conforms to this protocol, either directly or via protocol inheritance. |
| 2677 | // If so, we can skip checking this protocol completely because we |
| 2678 | // know that a parent class already satisfies this protocol. |
| 2679 | // |
| 2680 | // Note: we could generalize this logic for all protocols, and merely |
| 2681 | // add the limit on looking at the super class chain for just |
| 2682 | // specially marked protocols. This may be a good optimization. This |
| 2683 | // change is restricted to 'objc_protocol_requires_explicit_implementation' |
| 2684 | // protocols for now for controlled evaluation. |
| 2685 | if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) { |
Ahmed Charles | af94d56 | 2014-03-09 11:34:25 +0000 | [diff] [blame] | 2686 | if (!ProtocolsExplictImpl) { |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2687 | ProtocolsExplictImpl.reset(new ProtocolNameSet); |
| 2688 | findProtocolsWithExplicitImpls(Super, *ProtocolsExplictImpl); |
| 2689 | } |
| 2690 | if (ProtocolsExplictImpl->find(PDecl->getIdentifier()) != |
| 2691 | ProtocolsExplictImpl->end()) |
| 2692 | return; |
| 2693 | |
| 2694 | // If no super class conforms to the protocol, we should not search |
| 2695 | // for methods in the super class to implicitly satisfy the protocol. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2696 | Super = nullptr; |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2699 | if (S.getLangOpts().ObjCRuntime.isNeXTFamily()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2700 | // check to see if class implements forwardInvocation method and objects |
| 2701 | // of this class are derived from 'NSProxy' so that to forward requests |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2702 | // from one object to another. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2703 | // Under such conditions, which means that every method possible is |
| 2704 | // implemented in the class, we should not issue "Method definition not |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2705 | // found" warnings. |
| 2706 | // FIXME: Use a general GetUnarySelector method for this. |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2707 | IdentifierInfo* II = &S.Context.Idents.get("forwardInvocation"); |
| 2708 | Selector fISelector = S.Context.Selectors.getSelector(1, &II); |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2709 | if (InsMap.count(fISelector)) |
| 2710 | // Is IDecl derived from 'NSProxy'? If so, no instance methods |
| 2711 | // need be implemented in the implementation. |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2712 | NSIDecl = IDecl->lookupInheritedClass(&S.Context.Idents.get("NSProxy")); |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2713 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | |
Fariborz Jahanian | c41cf05 | 2013-01-07 19:21:03 +0000 | [diff] [blame] | 2715 | // If this is a forward protocol declaration, get its definition. |
| 2716 | if (!PDecl->isThisDeclarationADefinition() && |
| 2717 | PDecl->getDefinition()) |
| 2718 | PDecl = PDecl->getDefinition(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2719 | |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 2720 | // If a method lookup fails locally we still need to look and see if |
| 2721 | // the method was implemented by a base class or an inherited |
| 2722 | // protocol. This lookup is slow, but occurs rarely in correct code |
| 2723 | // and otherwise would terminate in a warning. |
| 2724 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2725 | // check unimplemented instance methods. |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2726 | if (!NSIDecl) |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2727 | for (auto *method : PDecl->instance_methods()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2728 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2729 | !method->isPropertyAccessor() && |
| 2730 | !InsMap.count(method->getSelector()) && |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2731 | (!Super || !Super->lookupMethod(method->getSelector(), |
| 2732 | true /* instance */, |
| 2733 | false /* shallowCategory */, |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2734 | true /* followsSuper */, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2735 | nullptr /* category */))) { |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2736 | // If a method is not implemented in the category implementation but |
| 2737 | // has been declared in its primary class, superclass, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2738 | // or in one of their protocols, no need to issue the warning. |
| 2739 | // This is because method will be implemented in the primary class |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2740 | // or one of its super class implementation. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2741 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 2742 | // Ugly, but necessary. Method declared in protocol might have |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2743 | // have been synthesized due to a property declared in the class which |
| 2744 | // uses the protocol. |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2745 | if (ObjCMethodDecl *MethodInClass = |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2746 | IDecl->lookupMethod(method->getSelector(), |
| 2747 | true /* instance */, |
| 2748 | true /* shallowCategoryLookup */, |
| 2749 | false /* followSuper */)) |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2750 | if (C || MethodInClass->isPropertyAccessor()) |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2751 | continue; |
| 2752 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 2753 | if (!S.Diags.isIgnored(DIAG, ImpLoc)) { |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2754 | WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, |
Ted Kremenek | 2ccf19e | 2013-12-13 05:58:51 +0000 | [diff] [blame] | 2755 | PDecl); |
Fariborz Jahanian | 97752f7 | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 2756 | } |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2757 | } |
| 2758 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2759 | // check unimplemented class methods |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2760 | for (auto *method : PDecl->class_methods()) { |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 2761 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
| 2762 | !ClsMap.count(method->getSelector()) && |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2763 | (!Super || !Super->lookupMethod(method->getSelector(), |
| 2764 | false /* class method */, |
| 2765 | false /* shallowCategoryLookup */, |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2766 | true /* followSuper */, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2767 | nullptr /* category */))) { |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2768 | // See above comment for instance method lookups. |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2769 | if (C && IDecl->lookupMethod(method->getSelector(), |
| 2770 | false /* class */, |
| 2771 | true /* shallowCategoryLookup */, |
| 2772 | false /* followSuper */)) |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2773 | continue; |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2774 | |
Fariborz Jahanian | c1fb862 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 2775 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 2776 | if (!S.Diags.isIgnored(DIAG, ImpLoc)) { |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2777 | WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, PDecl); |
Fariborz Jahanian | c1fb862 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 2778 | } |
Fariborz Jahanian | 97752f7 | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 2779 | } |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 2780 | } |
Chris Lattner | 390d39a | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 2781 | // Check on this protocols's referenced protocols, recursively. |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 2782 | for (auto *PI : PDecl->protocols()) |
| 2783 | CheckProtocolMethodDefs(S, ImpLoc, PI, IncompleteImpl, InsMap, ClsMap, |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2784 | CDecl, ProtocolsExplictImpl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
Fariborz Jahanian | f9ae68a | 2011-07-16 00:08:33 +0000 | [diff] [blame] | 2787 | /// MatchAllMethodDeclarations - Check methods declared in interface |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2788 | /// or protocol against those declared in their implementations. |
| 2789 | /// |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2790 | void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap, |
| 2791 | const SelectorSet &ClsMap, |
| 2792 | SelectorSet &InsMapSeen, |
| 2793 | SelectorSet &ClsMapSeen, |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2794 | ObjCImplDecl* IMPDecl, |
| 2795 | ObjCContainerDecl* CDecl, |
| 2796 | bool &IncompleteImpl, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2797 | bool ImmediateClass, |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2798 | bool WarnCategoryMethodImpl) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2799 | // Check and see if instance methods in class interface have been |
| 2800 | // implemented in the implementation class. If so, their types match. |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2801 | for (auto *I : CDecl->instance_methods()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 2802 | if (!InsMapSeen.insert(I->getSelector()).second) |
Benjamin Kramer | 9f8e2d7 | 2013-10-14 15:16:10 +0000 | [diff] [blame] | 2803 | continue; |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2804 | if (!I->isPropertyAccessor() && |
| 2805 | !InsMap.count(I->getSelector())) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2806 | if (ImmediateClass) |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2807 | WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl, |
Ted Kremenek | 65d6357 | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 2808 | diag::warn_undef_method_impl); |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2809 | continue; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2810 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2811 | ObjCMethodDecl *ImpMethodDecl = |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2812 | IMPDecl->getInstanceMethod(I->getSelector()); |
Manman Ren | a58f92f | 2016-10-11 21:18:20 +0000 | [diff] [blame] | 2813 | assert(CDecl->getInstanceMethod(I->getSelector(), true/*AllowHidden*/) && |
Argyrios Kyrtzidis | 342e08f | 2011-08-30 19:43:21 +0000 | [diff] [blame] | 2814 | "Expected to find the method through lookup as well"); |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2815 | // ImpMethodDecl may be null as in a @dynamic property. |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2816 | if (ImpMethodDecl) { |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2817 | if (!WarnCategoryMethodImpl) |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2818 | WarnConflictingTypedMethods(ImpMethodDecl, I, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2819 | isa<ObjCProtocolDecl>(CDecl)); |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2820 | else if (!I->isPropertyAccessor()) |
| 2821 | WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl)); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2822 | } |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2823 | } |
| 2824 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2825 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2826 | // Check and see if class methods in class interface have been |
| 2827 | // implemented in the implementation class. If so, their types match. |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2828 | for (auto *I : CDecl->class_methods()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 2829 | if (!ClsMapSeen.insert(I->getSelector()).second) |
Benjamin Kramer | 9f8e2d7 | 2013-10-14 15:16:10 +0000 | [diff] [blame] | 2830 | continue; |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2831 | if (!I->isPropertyAccessor() && |
| 2832 | !ClsMap.count(I->getSelector())) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2833 | if (ImmediateClass) |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2834 | WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl, |
Ted Kremenek | 65d6357 | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 2835 | diag::warn_undef_method_impl); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2836 | } else { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2837 | ObjCMethodDecl *ImpMethodDecl = |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2838 | IMPDecl->getClassMethod(I->getSelector()); |
Manman Ren | a58f92f | 2016-10-11 21:18:20 +0000 | [diff] [blame] | 2839 | assert(CDecl->getClassMethod(I->getSelector(), true/*AllowHidden*/) && |
Argyrios Kyrtzidis | 342e08f | 2011-08-30 19:43:21 +0000 | [diff] [blame] | 2840 | "Expected to find the method through lookup as well"); |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2841 | // ImpMethodDecl may be null as in a @dynamic property. |
| 2842 | if (ImpMethodDecl) { |
| 2843 | if (!WarnCategoryMethodImpl) |
| 2844 | WarnConflictingTypedMethods(ImpMethodDecl, I, |
| 2845 | isa<ObjCProtocolDecl>(CDecl)); |
| 2846 | else if (!I->isPropertyAccessor()) |
| 2847 | WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl)); |
| 2848 | } |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2849 | } |
| 2850 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2851 | |
Fariborz Jahanian | 8181caa | 2013-08-14 23:58:55 +0000 | [diff] [blame] | 2852 | if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) { |
| 2853 | // Also, check for methods declared in protocols inherited by |
| 2854 | // this protocol. |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 2855 | for (auto *PI : PD->protocols()) |
Fariborz Jahanian | 8181caa | 2013-08-14 23:58:55 +0000 | [diff] [blame] | 2856 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 2857 | IMPDecl, PI, IncompleteImpl, false, |
Fariborz Jahanian | 8181caa | 2013-08-14 23:58:55 +0000 | [diff] [blame] | 2858 | WarnCategoryMethodImpl); |
| 2859 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2860 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2861 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2862 | // when checking that methods in implementation match their declaration, |
| 2863 | // i.e. when WarnCategoryMethodImpl is false, check declarations in class |
| 2864 | // extension; as well as those in categories. |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2865 | if (!WarnCategoryMethodImpl) { |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 2866 | for (auto *Cat : I->visible_categories()) |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2867 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Argyrios Kyrtzidis | 3a43754 | 2015-10-13 23:27:34 +0000 | [diff] [blame] | 2868 | IMPDecl, Cat, IncompleteImpl, |
| 2869 | ImmediateClass && Cat->IsClassExtension(), |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2870 | WarnCategoryMethodImpl); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2871 | } else { |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2872 | // Also methods in class extensions need be looked at next. |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 2873 | for (auto *Ext : I->visible_extensions()) |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2874 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 2875 | IMPDecl, Ext, IncompleteImpl, false, |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2876 | WarnCategoryMethodImpl); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2879 | // Check for any implementation of a methods declared in protocol. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2880 | for (auto *PI : I->all_referenced_protocols()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2881 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2882 | IMPDecl, PI, IncompleteImpl, false, |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2883 | WarnCategoryMethodImpl); |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2884 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2885 | // FIXME. For now, we are not checking for extact match of methods |
| 2886 | // in category implementation and its primary class's super class. |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2887 | if (!WarnCategoryMethodImpl && I->getSuperClass()) |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2888 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2889 | IMPDecl, |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2890 | I->getSuperClass(), IncompleteImpl, false); |
| 2891 | } |
| 2892 | } |
| 2893 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2894 | /// CheckCategoryVsClassMethodMatches - Checks that methods implemented in |
| 2895 | /// category matches with those implemented in its primary class and |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2896 | /// warns each time an exact match is found. |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2897 | void Sema::CheckCategoryVsClassMethodMatches( |
| 2898 | ObjCCategoryImplDecl *CatIMPDecl) { |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2899 | // Get category's primary class. |
| 2900 | ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl(); |
| 2901 | if (!CatDecl) |
| 2902 | return; |
| 2903 | ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface(); |
| 2904 | if (!IDecl) |
| 2905 | return; |
Fariborz Jahanian | f3077a2 | 2013-12-05 20:52:31 +0000 | [diff] [blame] | 2906 | ObjCInterfaceDecl *SuperIDecl = IDecl->getSuperClass(); |
| 2907 | SelectorSet InsMap, ClsMap; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2908 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2909 | for (const auto *I : CatIMPDecl->instance_methods()) { |
| 2910 | Selector Sel = I->getSelector(); |
Fariborz Jahanian | f3077a2 | 2013-12-05 20:52:31 +0000 | [diff] [blame] | 2911 | // When checking for methods implemented in the category, skip over |
| 2912 | // those declared in category class's super class. This is because |
| 2913 | // the super class must implement the method. |
| 2914 | if (SuperIDecl && SuperIDecl->lookupMethod(Sel, true)) |
| 2915 | continue; |
| 2916 | InsMap.insert(Sel); |
| 2917 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2918 | |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2919 | for (const auto *I : CatIMPDecl->class_methods()) { |
| 2920 | Selector Sel = I->getSelector(); |
Fariborz Jahanian | f3077a2 | 2013-12-05 20:52:31 +0000 | [diff] [blame] | 2921 | if (SuperIDecl && SuperIDecl->lookupMethod(Sel, false)) |
| 2922 | continue; |
| 2923 | ClsMap.insert(Sel); |
| 2924 | } |
| 2925 | if (InsMap.empty() && ClsMap.empty()) |
| 2926 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2927 | |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2928 | SelectorSet InsMapSeen, ClsMapSeen; |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2929 | bool IncompleteImpl = false; |
| 2930 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 2931 | CatIMPDecl, IDecl, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2932 | IncompleteImpl, false, |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2933 | true /*WarnCategoryMethodImpl*/); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2934 | } |
Fariborz Jahanian | 4ceec3f | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 2935 | |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 2936 | void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2937 | ObjCContainerDecl* CDecl, |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 2938 | bool IncompleteImpl) { |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2939 | SelectorSet InsMap; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2940 | // Check and see if instance methods in class interface have been |
| 2941 | // implemented in the implementation class. |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2942 | for (const auto *I : IMPDecl->instance_methods()) |
| 2943 | InsMap.insert(I->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2944 | |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 2945 | // Add the selectors for getters/setters of @dynamic properties. |
| 2946 | for (const auto *PImpl : IMPDecl->property_impls()) { |
| 2947 | // We only care about @dynamic implementations. |
| 2948 | if (PImpl->getPropertyImplementation() != ObjCPropertyImplDecl::Dynamic) |
| 2949 | continue; |
| 2950 | |
| 2951 | const auto *P = PImpl->getPropertyDecl(); |
| 2952 | if (!P) continue; |
| 2953 | |
| 2954 | InsMap.insert(P->getGetterName()); |
| 2955 | if (!P->getSetterName().isNull()) |
| 2956 | InsMap.insert(P->getSetterName()); |
| 2957 | } |
| 2958 | |
Fariborz Jahanian | 6c6aea9 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 2959 | // Check and see if properties declared in the interface have either 1) |
| 2960 | // an implementation or 2) there is a @synthesize/@dynamic implementation |
| 2961 | // of the property in the @implementation. |
Ted Kremenek | 348e88c | 2014-02-21 19:41:34 +0000 | [diff] [blame] | 2962 | if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 2963 | bool SynthesizeProperties = LangOpts.ObjCDefaultSynthProperties && |
| 2964 | LangOpts.ObjCRuntime.isNonFragile() && |
| 2965 | !IDecl->isObjCRequiresPropertyDefs(); |
| 2966 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, SynthesizeProperties); |
| 2967 | } |
| 2968 | |
Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 2969 | // Diagnose null-resettable synthesized setters. |
| 2970 | diagnoseNullResettableSynthesizedSetters(IMPDecl); |
| 2971 | |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2972 | SelectorSet ClsMap; |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2973 | for (const auto *I : IMPDecl->class_methods()) |
| 2974 | ClsMap.insert(I->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2975 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2976 | // Check for type conflict of methods declared in a class/protocol and |
| 2977 | // its implementation; if any. |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2978 | SelectorSet InsMapSeen, ClsMapSeen; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2979 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 2980 | IMPDecl, CDecl, |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2981 | IncompleteImpl, true); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2982 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2983 | // check all methods implemented in category against those declared |
| 2984 | // in its primary class. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2985 | if (ObjCCategoryImplDecl *CatDecl = |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2986 | dyn_cast<ObjCCategoryImplDecl>(IMPDecl)) |
| 2987 | CheckCategoryVsClassMethodMatches(CatDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2988 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2989 | // Check the protocol list for unimplemented methods in the @implementation |
| 2990 | // class. |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2991 | // Check and see if class methods in class interface have been |
| 2992 | // implemented in the implementation class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2993 | |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2994 | LazyProtocolNameSet ExplicitImplProtocols; |
| 2995 | |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 2996 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2997 | for (auto *PI : I->all_referenced_protocols()) |
| 2998 | CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), PI, IncompleteImpl, |
| 2999 | InsMap, ClsMap, I, ExplicitImplProtocols); |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 3000 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Fariborz Jahanian | 8764c74 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 3001 | // For extended class, unimplemented methods in its protocols will |
| 3002 | // be reported in the primary class. |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 3003 | if (!C->IsClassExtension()) { |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 3004 | for (auto *P : C->protocols()) |
| 3005 | CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), P, |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 3006 | IncompleteImpl, InsMap, ClsMap, CDecl, |
| 3007 | ExplicitImplProtocols); |
Ted Kremenek | 348e88c | 2014-02-21 19:41:34 +0000 | [diff] [blame] | 3008 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3009 | /*SynthesizeProperties=*/false); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3010 | } |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 3011 | } else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3012 | llvm_unreachable("invalid ObjCContainerDecl type."); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 3015 | Sema::DeclGroupPtrTy |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3016 | Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
Chris Lattner | 99a8331 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 3017 | IdentifierInfo **IdentList, |
Ted Kremenek | a26da85 | 2009-11-17 23:12:20 +0000 | [diff] [blame] | 3018 | SourceLocation *IdentLocs, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3019 | ArrayRef<ObjCTypeParamList *> TypeParamLists, |
Chris Lattner | 99a8331 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 3020 | unsigned NumElts) { |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 3021 | SmallVector<Decl *, 8> DeclsInGroup; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3022 | for (unsigned i = 0; i != NumElts; ++i) { |
| 3023 | // Check for another declaration kind with the same name. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3024 | NamedDecl *PrevDecl |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3025 | = LookupSingleName(TUScope, IdentList[i], IdentLocs[i], |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 3026 | LookupOrdinaryName, forRedeclarationInCurContext()); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3027 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Steve Naroff | 946166f | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 3028 | // GCC apparently allows the following idiom: |
| 3029 | // |
| 3030 | // typedef NSObject < XCElementTogglerP > XCElementToggler; |
| 3031 | // @class XCElementToggler; |
| 3032 | // |
Fariborz Jahanian | 04c4455 | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 3033 | // Here we have chosen to ignore the forward class declaration |
| 3034 | // with a warning. Since this is the implied behavior. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3035 | TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3036 | if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3037 | Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i]; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3038 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3039 | } else { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3040 | // a forward class declaration matching a typedef name of a class refers |
Fariborz Jahanian | 04c4455 | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 3041 | // to the underlying class. Just ignore the forward class with a warning |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3042 | // as this will force the intended behavior which is to lookup the |
| 3043 | // typedef name. |
Fariborz Jahanian | 04c4455 | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 3044 | if (isa<ObjCObjectType>(TDD->getUnderlyingType())) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3045 | Diag(AtClassLoc, diag::warn_forward_class_redefinition) |
| 3046 | << IdentList[i]; |
Fariborz Jahanian | 04c4455 | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 3047 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3048 | continue; |
| 3049 | } |
Fariborz Jahanian | 0d45181 | 2009-05-07 21:49:26 +0000 | [diff] [blame] | 3050 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3051 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3052 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 3053 | // Create a declaration to describe this forward declaration. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3054 | ObjCInterfaceDecl *PrevIDecl |
| 3055 | = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Argyrios Kyrtzidis | dd71063 | 2013-06-18 21:26:33 +0000 | [diff] [blame] | 3056 | |
| 3057 | IdentifierInfo *ClassName = IdentList[i]; |
| 3058 | if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) { |
| 3059 | // A previous decl with a different name is because of |
| 3060 | // @compatibility_alias, for example: |
| 3061 | // \code |
| 3062 | // @class NewImage; |
| 3063 | // @compatibility_alias OldImage NewImage; |
| 3064 | // \endcode |
| 3065 | // A lookup for 'OldImage' will return the 'NewImage' decl. |
| 3066 | // |
| 3067 | // In such a case use the real declaration name, instead of the alias one, |
| 3068 | // otherwise we will break IdentifierResolver and redecls-chain invariants. |
| 3069 | // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl |
| 3070 | // has been aliased. |
| 3071 | ClassName = PrevIDecl->getIdentifier(); |
| 3072 | } |
| 3073 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3074 | // If this forward declaration has type parameters, compare them with the |
| 3075 | // type parameters of the previous declaration. |
| 3076 | ObjCTypeParamList *TypeParams = TypeParamLists[i]; |
| 3077 | if (PrevIDecl && TypeParams) { |
| 3078 | if (ObjCTypeParamList *PrevTypeParams = PrevIDecl->getTypeParamList()) { |
| 3079 | // Check for consistency with the previous declaration. |
| 3080 | if (checkTypeParamListConsistency( |
| 3081 | *this, PrevTypeParams, TypeParams, |
| 3082 | TypeParamListContext::ForwardDeclaration)) { |
| 3083 | TypeParams = nullptr; |
| 3084 | } |
| 3085 | } else if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) { |
| 3086 | // The @interface does not have type parameters. Complain. |
| 3087 | Diag(IdentLocs[i], diag::err_objc_parameterized_forward_class) |
| 3088 | << ClassName |
| 3089 | << TypeParams->getSourceRange(); |
| 3090 | Diag(Def->getLocation(), diag::note_defined_here) |
| 3091 | << ClassName; |
| 3092 | |
| 3093 | TypeParams = nullptr; |
| 3094 | } |
| 3095 | } |
| 3096 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 3097 | ObjCInterfaceDecl *IDecl |
| 3098 | = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3099 | ClassName, TypeParams, PrevIDecl, |
| 3100 | IdentLocs[i]); |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 3101 | IDecl->setAtEndRange(IdentLocs[i]); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3102 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 3103 | PushOnScopeChains(IDecl, TUScope); |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 3104 | CheckObjCDeclScope(IDecl); |
| 3105 | DeclsInGroup.push_back(IDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3106 | } |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 3107 | |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 3108 | return BuildDeclaratorGroup(DeclsInGroup); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3111 | static bool tryMatchRecordTypes(ASTContext &Context, |
| 3112 | Sema::MethodMatchStrategy strategy, |
| 3113 | const Type *left, const Type *right); |
| 3114 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3115 | static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy, |
| 3116 | QualType leftQT, QualType rightQT) { |
| 3117 | const Type *left = |
| 3118 | Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr(); |
| 3119 | const Type *right = |
| 3120 | Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr(); |
| 3121 | |
| 3122 | if (left == right) return true; |
| 3123 | |
| 3124 | // If we're doing a strict match, the types have to match exactly. |
| 3125 | if (strategy == Sema::MMS_strict) return false; |
| 3126 | |
| 3127 | if (left->isIncompleteType() || right->isIncompleteType()) return false; |
| 3128 | |
| 3129 | // Otherwise, use this absurdly complicated algorithm to try to |
| 3130 | // validate the basic, low-level compatibility of the two types. |
| 3131 | |
| 3132 | // As a minimum, require the sizes and alignments to match. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 3133 | TypeInfo LeftTI = Context.getTypeInfo(left); |
| 3134 | TypeInfo RightTI = Context.getTypeInfo(right); |
| 3135 | if (LeftTI.Width != RightTI.Width) |
| 3136 | return false; |
| 3137 | |
| 3138 | if (LeftTI.Align != RightTI.Align) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3139 | return false; |
| 3140 | |
| 3141 | // Consider all the kinds of non-dependent canonical types: |
| 3142 | // - functions and arrays aren't possible as return and parameter types |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3143 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3144 | // - vector types of equal size can be arbitrarily mixed |
| 3145 | if (isa<VectorType>(left)) return isa<VectorType>(right); |
| 3146 | if (isa<VectorType>(right)) return false; |
| 3147 | |
| 3148 | // - references should only match references of identical type |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3149 | // - structs, unions, and Objective-C objects must match more-or-less |
| 3150 | // exactly |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3151 | // - everything else should be a scalar |
| 3152 | if (!left->isScalarType() || !right->isScalarType()) |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3153 | return tryMatchRecordTypes(Context, strategy, left, right); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3154 | |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3155 | // Make scalars agree in kind, except count bools as chars, and group |
| 3156 | // all non-member pointers together. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3157 | Type::ScalarTypeKind leftSK = left->getScalarTypeKind(); |
| 3158 | Type::ScalarTypeKind rightSK = right->getScalarTypeKind(); |
| 3159 | if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral; |
| 3160 | if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral; |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3161 | if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer) |
| 3162 | leftSK = Type::STK_ObjCObjectPointer; |
| 3163 | if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer) |
| 3164 | rightSK = Type::STK_ObjCObjectPointer; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3165 | |
| 3166 | // Note that data member pointers and function member pointers don't |
| 3167 | // intermix because of the size differences. |
| 3168 | |
| 3169 | return (leftSK == rightSK); |
| 3170 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3171 | |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3172 | static bool tryMatchRecordTypes(ASTContext &Context, |
| 3173 | Sema::MethodMatchStrategy strategy, |
| 3174 | const Type *lt, const Type *rt) { |
| 3175 | assert(lt && rt && lt != rt); |
| 3176 | |
| 3177 | if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false; |
| 3178 | RecordDecl *left = cast<RecordType>(lt)->getDecl(); |
| 3179 | RecordDecl *right = cast<RecordType>(rt)->getDecl(); |
| 3180 | |
| 3181 | // Require union-hood to match. |
| 3182 | if (left->isUnion() != right->isUnion()) return false; |
| 3183 | |
| 3184 | // Require an exact match if either is non-POD. |
| 3185 | if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) || |
| 3186 | (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD())) |
| 3187 | return false; |
| 3188 | |
| 3189 | // Require size and alignment to match. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 3190 | TypeInfo LeftTI = Context.getTypeInfo(lt); |
| 3191 | TypeInfo RightTI = Context.getTypeInfo(rt); |
| 3192 | if (LeftTI.Width != RightTI.Width) |
| 3193 | return false; |
| 3194 | |
| 3195 | if (LeftTI.Align != RightTI.Align) |
| 3196 | return false; |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3197 | |
| 3198 | // Require fields to match. |
| 3199 | RecordDecl::field_iterator li = left->field_begin(), le = left->field_end(); |
| 3200 | RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end(); |
| 3201 | for (; li != le && ri != re; ++li, ++ri) { |
| 3202 | if (!matchTypes(Context, strategy, li->getType(), ri->getType())) |
| 3203 | return false; |
| 3204 | } |
| 3205 | return (li == le && ri == re); |
| 3206 | } |
| 3207 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3208 | /// MatchTwoMethodDeclarations - Checks that two methods have matching type and |
| 3209 | /// returns true, or false, accordingly. |
| 3210 | /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3211 | bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left, |
| 3212 | const ObjCMethodDecl *right, |
| 3213 | MethodMatchStrategy strategy) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3214 | if (!matchTypes(Context, strategy, left->getReturnType(), |
| 3215 | right->getReturnType())) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3216 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3217 | |
Douglas Gregor | 560b7fa | 2013-02-07 19:13:24 +0000 | [diff] [blame] | 3218 | // If either is hidden, it is not considered to match. |
| 3219 | if (left->isHidden() || right->isHidden()) |
| 3220 | return false; |
| 3221 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3222 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3223 | (left->hasAttr<NSReturnsRetainedAttr>() |
| 3224 | != right->hasAttr<NSReturnsRetainedAttr>() || |
| 3225 | left->hasAttr<NSConsumesSelfAttr>() |
| 3226 | != right->hasAttr<NSConsumesSelfAttr>())) |
| 3227 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3228 | |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 3229 | ObjCMethodDecl::param_const_iterator |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 3230 | li = left->param_begin(), le = left->param_end(), ri = right->param_begin(), |
| 3231 | re = right->param_end(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3232 | |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 3233 | for (; li != le && ri != re; ++li, ++ri) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3234 | assert(ri != right->param_end() && "Param mismatch"); |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 3235 | const ParmVarDecl *lparm = *li, *rparm = *ri; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3236 | |
| 3237 | if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType())) |
| 3238 | return false; |
| 3239 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3240 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3241 | lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>()) |
| 3242 | return false; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3243 | } |
| 3244 | return true; |
| 3245 | } |
| 3246 | |
Manman Ren | 7122453 | 2016-04-09 18:59:48 +0000 | [diff] [blame] | 3247 | static bool isMethodContextSameForKindofLookup(ObjCMethodDecl *Method, |
| 3248 | ObjCMethodDecl *MethodInList) { |
| 3249 | auto *MethodProtocol = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext()); |
| 3250 | auto *MethodInListProtocol = |
| 3251 | dyn_cast<ObjCProtocolDecl>(MethodInList->getDeclContext()); |
| 3252 | // If this method belongs to a protocol but the method in list does not, or |
| 3253 | // vice versa, we say the context is not the same. |
| 3254 | if ((MethodProtocol && !MethodInListProtocol) || |
| 3255 | (!MethodProtocol && MethodInListProtocol)) |
| 3256 | return false; |
| 3257 | |
| 3258 | if (MethodProtocol && MethodInListProtocol) |
| 3259 | return true; |
| 3260 | |
| 3261 | ObjCInterfaceDecl *MethodInterface = Method->getClassInterface(); |
| 3262 | ObjCInterfaceDecl *MethodInListInterface = |
| 3263 | MethodInList->getClassInterface(); |
| 3264 | return MethodInterface == MethodInListInterface; |
| 3265 | } |
| 3266 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3267 | void Sema::addMethodToGlobalList(ObjCMethodList *List, |
| 3268 | ObjCMethodDecl *Method) { |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 3269 | // Record at the head of the list whether there were 0, 1, or >= 2 methods |
| 3270 | // inside categories. |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3271 | if (ObjCCategoryDecl *CD = |
| 3272 | dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) |
Argyrios Kyrtzidis | 04703a6 | 2013-04-27 00:10:12 +0000 | [diff] [blame] | 3273 | if (!CD->IsClassExtension() && List->getBits() < 2) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3274 | List->setBits(List->getBits() + 1); |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 3275 | |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3276 | // If the list is empty, make it a singleton list. |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3277 | if (List->getMethod() == nullptr) { |
| 3278 | List->setMethod(Method); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3279 | List->setNext(nullptr); |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3280 | return; |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3281 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3282 | |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3283 | // We've seen a method with this name, see if we have already seen this type |
| 3284 | // signature. |
| 3285 | ObjCMethodList *Previous = List; |
Manman Ren | 051d0b6 | 2016-04-13 23:43:56 +0000 | [diff] [blame] | 3286 | ObjCMethodList *ListWithSameDeclaration = nullptr; |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 3287 | for (; List; Previous = List, List = List->getNext()) { |
Douglas Gregor | 600a2f5 | 2013-06-21 00:20:25 +0000 | [diff] [blame] | 3288 | // If we are building a module, keep all of the methods. |
Richard Smith | bbcc9f0 | 2016-08-26 00:14:38 +0000 | [diff] [blame] | 3289 | if (getLangOpts().isCompilingModule()) |
Douglas Gregor | 600a2f5 | 2013-06-21 00:20:25 +0000 | [diff] [blame] | 3290 | continue; |
| 3291 | |
Manman Ren | 051d0b6 | 2016-04-13 23:43:56 +0000 | [diff] [blame] | 3292 | bool SameDeclaration = MatchTwoMethodDeclarations(Method, |
| 3293 | List->getMethod()); |
Manman Ren | 7122453 | 2016-04-09 18:59:48 +0000 | [diff] [blame] | 3294 | // Looking for method with a type bound requires the correct context exists. |
Manman Ren | 051d0b6 | 2016-04-13 23:43:56 +0000 | [diff] [blame] | 3295 | // We need to insert a method into the list if the context is different. |
| 3296 | // If the method's declaration matches the list |
| 3297 | // a> the method belongs to a different context: we need to insert it, in |
| 3298 | // order to emit the availability message, we need to prioritize over |
| 3299 | // availability among the methods with the same declaration. |
| 3300 | // b> the method belongs to the same context: there is no need to insert a |
| 3301 | // new entry. |
| 3302 | // If the method's declaration does not match the list, we insert it to the |
| 3303 | // end. |
| 3304 | if (!SameDeclaration || |
Manman Ren | 7122453 | 2016-04-09 18:59:48 +0000 | [diff] [blame] | 3305 | !isMethodContextSameForKindofLookup(Method, List->getMethod())) { |
Fariborz Jahanian | d436b2a | 2015-04-07 16:56:27 +0000 | [diff] [blame] | 3306 | // Even if two method types do not match, we would like to say |
| 3307 | // there is more than one declaration so unavailability/deprecated |
| 3308 | // warning is not too noisy. |
| 3309 | if (!Method->isDefined()) |
| 3310 | List->setHasMoreThanOneDecl(true); |
Manman Ren | 051d0b6 | 2016-04-13 23:43:56 +0000 | [diff] [blame] | 3311 | |
| 3312 | // For methods with the same declaration, the one that is deprecated |
| 3313 | // should be put in the front for better diagnostics. |
| 3314 | if (Method->isDeprecated() && SameDeclaration && |
| 3315 | !ListWithSameDeclaration && !List->getMethod()->isDeprecated()) |
| 3316 | ListWithSameDeclaration = List; |
| 3317 | |
| 3318 | if (Method->isUnavailable() && SameDeclaration && |
| 3319 | !ListWithSameDeclaration && |
| 3320 | List->getMethod()->getAvailability() < AR_Deprecated) |
| 3321 | ListWithSameDeclaration = List; |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3322 | continue; |
Fariborz Jahanian | d436b2a | 2015-04-07 16:56:27 +0000 | [diff] [blame] | 3323 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3324 | |
| 3325 | ObjCMethodDecl *PrevObjCMethod = List->getMethod(); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3326 | |
| 3327 | // Propagate the 'defined' bit. |
| 3328 | if (Method->isDefined()) |
| 3329 | PrevObjCMethod->setDefined(true); |
Nico Weber | e3b1104 | 2014-12-27 07:09:37 +0000 | [diff] [blame] | 3330 | else { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3331 | // Objective-C doesn't allow an @interface for a class after its |
| 3332 | // @implementation. So if Method is not defined and there already is |
| 3333 | // an entry for this type signature, Method has to be for a different |
| 3334 | // class than PrevObjCMethod. |
| 3335 | List->setHasMoreThanOneDecl(true); |
| 3336 | } |
| 3337 | |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3338 | // If a method is deprecated, push it in the global pool. |
| 3339 | // This is used for better diagnostics. |
| 3340 | if (Method->isDeprecated()) { |
| 3341 | if (!PrevObjCMethod->isDeprecated()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3342 | List->setMethod(Method); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3343 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3344 | // If the new method is unavailable, push it into global pool |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3345 | // unless previous one is deprecated. |
| 3346 | if (Method->isUnavailable()) { |
| 3347 | if (PrevObjCMethod->getAvailability() < AR_Deprecated) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3348 | List->setMethod(Method); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3349 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3350 | |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3351 | return; |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3352 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3353 | |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3354 | // We have a new signature for an existing method - add it. |
| 3355 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 3356 | ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>(); |
Manman Ren | 7122453 | 2016-04-09 18:59:48 +0000 | [diff] [blame] | 3357 | |
Manman Ren | 051d0b6 | 2016-04-13 23:43:56 +0000 | [diff] [blame] | 3358 | // We insert it right before ListWithSameDeclaration. |
| 3359 | if (ListWithSameDeclaration) { |
| 3360 | auto *List = new (Mem) ObjCMethodList(*ListWithSameDeclaration); |
| 3361 | // FIXME: should we clear the other bits in ListWithSameDeclaration? |
| 3362 | ListWithSameDeclaration->setMethod(Method); |
| 3363 | ListWithSameDeclaration->setNext(List); |
Manman Ren | 7122453 | 2016-04-09 18:59:48 +0000 | [diff] [blame] | 3364 | return; |
| 3365 | } |
| 3366 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3367 | Previous->setNext(new (Mem) ObjCMethodList(Method)); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3368 | } |
| 3369 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3370 | /// Read the contents of the method pool for a given selector from |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3371 | /// external storage. |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 3372 | void Sema::ReadMethodPool(Selector Sel) { |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3373 | assert(ExternalSource && "We need an external AST source"); |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 3374 | ExternalSource->ReadMethodPool(Sel); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
Manman Ren | a0f31a0 | 2016-04-29 19:04:05 +0000 | [diff] [blame] | 3377 | void Sema::updateOutOfDateSelector(Selector Sel) { |
| 3378 | if (!ExternalSource) |
| 3379 | return; |
| 3380 | ExternalSource->updateOutOfDateSelector(Sel); |
| 3381 | } |
| 3382 | |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3383 | void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl, |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3384 | bool instance) { |
Argyrios Kyrtzidis | b15def2 | 2012-03-12 18:34:26 +0000 | [diff] [blame] | 3385 | // Ignore methods of invalid containers. |
| 3386 | if (cast<Decl>(Method->getDeclContext())->isInvalidDecl()) |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3387 | return; |
Argyrios Kyrtzidis | b15def2 | 2012-03-12 18:34:26 +0000 | [diff] [blame] | 3388 | |
Douglas Gregor | 70f449b | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 3389 | if (ExternalSource) |
| 3390 | ReadMethodPool(Method->getSelector()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3391 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3392 | GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector()); |
Douglas Gregor | 70f449b | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 3393 | if (Pos == MethodPool.end()) |
| 3394 | Pos = MethodPool.insert(std::make_pair(Method->getSelector(), |
| 3395 | GlobalMethods())).first; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3396 | |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 3397 | Method->setDefined(impl); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3398 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3399 | ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second; |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3400 | addMethodToGlobalList(&Entry, Method); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3401 | } |
| 3402 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3403 | /// Determines if this is an "acceptable" loose mismatch in the global |
| 3404 | /// method pool. This exists mostly as a hack to get around certain |
| 3405 | /// global mismatches which we can't afford to make warnings / errors. |
| 3406 | /// Really, what we want is a way to take a method out of the global |
| 3407 | /// method pool. |
| 3408 | static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen, |
| 3409 | ObjCMethodDecl *other) { |
| 3410 | if (!chosen->isInstanceMethod()) |
| 3411 | return false; |
| 3412 | |
| 3413 | Selector sel = chosen->getSelector(); |
| 3414 | if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length") |
| 3415 | return false; |
| 3416 | |
| 3417 | // Don't complain about mismatches for -length if the method we |
| 3418 | // chose has an integral result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3419 | return (chosen->getReturnType()->isIntegerType()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3420 | } |
| 3421 | |
Manman Ren | 7ed4f98 | 2016-04-07 19:32:24 +0000 | [diff] [blame] | 3422 | /// Return true if the given method is wthin the type bound. |
| 3423 | static bool FilterMethodsByTypeBound(ObjCMethodDecl *Method, |
| 3424 | const ObjCObjectType *TypeBound) { |
| 3425 | if (!TypeBound) |
| 3426 | return true; |
| 3427 | |
| 3428 | if (TypeBound->isObjCId()) |
| 3429 | // FIXME: should we handle the case of bounding to id<A, B> differently? |
| 3430 | return true; |
| 3431 | |
| 3432 | auto *BoundInterface = TypeBound->getInterface(); |
| 3433 | assert(BoundInterface && "unexpected object type!"); |
| 3434 | |
| 3435 | // Check if the Method belongs to a protocol. We should allow any method |
| 3436 | // defined in any protocol, because any subclass could adopt the protocol. |
| 3437 | auto *MethodProtocol = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext()); |
| 3438 | if (MethodProtocol) { |
| 3439 | return true; |
| 3440 | } |
| 3441 | |
| 3442 | // If the Method belongs to a class, check if it belongs to the class |
| 3443 | // hierarchy of the class bound. |
| 3444 | if (ObjCInterfaceDecl *MethodInterface = Method->getClassInterface()) { |
| 3445 | // We allow methods declared within classes that are part of the hierarchy |
| 3446 | // of the class bound (superclass of, subclass of, or the same as the class |
| 3447 | // bound). |
| 3448 | return MethodInterface == BoundInterface || |
| 3449 | MethodInterface->isSuperClassOf(BoundInterface) || |
| 3450 | BoundInterface->isSuperClassOf(MethodInterface); |
| 3451 | } |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 3452 | llvm_unreachable("unknown method context"); |
Manman Ren | 7ed4f98 | 2016-04-07 19:32:24 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3455 | /// We first select the type of the method: Instance or Factory, then collect |
| 3456 | /// all methods with that type. |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3457 | bool Sema::CollectMultipleMethodsInGlobalPool( |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3458 | Selector Sel, SmallVectorImpl<ObjCMethodDecl *> &Methods, |
Manman Ren | 7ed4f98 | 2016-04-07 19:32:24 +0000 | [diff] [blame] | 3459 | bool InstanceFirst, bool CheckTheOther, |
| 3460 | const ObjCObjectType *TypeBound) { |
Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 3461 | if (ExternalSource) |
| 3462 | ReadMethodPool(Sel); |
| 3463 | |
| 3464 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
| 3465 | if (Pos == MethodPool.end()) |
| 3466 | return false; |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3467 | |
Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 3468 | // Gather the non-hidden methods. |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3469 | ObjCMethodList &MethList = InstanceFirst ? Pos->second.first : |
| 3470 | Pos->second.second; |
Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 3471 | for (ObjCMethodList *M = &MethList; M; M = M->getNext()) |
Manman Ren | 7ed4f98 | 2016-04-07 19:32:24 +0000 | [diff] [blame] | 3472 | if (M->getMethod() && !M->getMethod()->isHidden()) { |
| 3473 | if (FilterMethodsByTypeBound(M->getMethod(), TypeBound)) |
| 3474 | Methods.push_back(M->getMethod()); |
| 3475 | } |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3476 | |
| 3477 | // Return if we find any method with the desired kind. |
| 3478 | if (!Methods.empty()) |
| 3479 | return Methods.size() > 1; |
| 3480 | |
| 3481 | if (!CheckTheOther) |
| 3482 | return false; |
| 3483 | |
| 3484 | // Gather the other kind. |
| 3485 | ObjCMethodList &MethList2 = InstanceFirst ? Pos->second.second : |
| 3486 | Pos->second.first; |
| 3487 | for (ObjCMethodList *M = &MethList2; M; M = M->getNext()) |
Manman Ren | 7ed4f98 | 2016-04-07 19:32:24 +0000 | [diff] [blame] | 3488 | if (M->getMethod() && !M->getMethod()->isHidden()) { |
| 3489 | if (FilterMethodsByTypeBound(M->getMethod(), TypeBound)) |
| 3490 | Methods.push_back(M->getMethod()); |
| 3491 | } |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3492 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3493 | return Methods.size() > 1; |
Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 3494 | } |
| 3495 | |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3496 | bool Sema::AreMultipleMethodsInGlobalPool( |
| 3497 | Selector Sel, ObjCMethodDecl *BestMethod, SourceRange R, |
| 3498 | bool receiverIdOrClass, SmallVectorImpl<ObjCMethodDecl *> &Methods) { |
| 3499 | // Diagnose finding more than one method in global pool. |
| 3500 | SmallVector<ObjCMethodDecl *, 4> FilteredMethods; |
| 3501 | FilteredMethods.push_back(BestMethod); |
| 3502 | |
| 3503 | for (auto *M : Methods) |
| 3504 | if (M != BestMethod && !M->hasAttr<UnavailableAttr>()) |
| 3505 | FilteredMethods.push_back(M); |
| 3506 | |
| 3507 | if (FilteredMethods.size() > 1) |
| 3508 | DiagnoseMultipleMethodInGlobalPool(FilteredMethods, Sel, R, |
| 3509 | receiverIdOrClass); |
| 3510 | |
Fariborz Jahanian | c62d16f | 2014-11-13 22:27:05 +0000 | [diff] [blame] | 3511 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3512 | // Test for no method in the pool which should not trigger any warning by |
| 3513 | // caller. |
Fariborz Jahanian | c62d16f | 2014-11-13 22:27:05 +0000 | [diff] [blame] | 3514 | if (Pos == MethodPool.end()) |
| 3515 | return true; |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3516 | ObjCMethodList &MethList = |
| 3517 | BestMethod->isInstanceMethod() ? Pos->second.first : Pos->second.second; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3518 | return MethList.hasMoreThanOneDecl(); |
Fariborz Jahanian | c62d16f | 2014-11-13 22:27:05 +0000 | [diff] [blame] | 3519 | } |
| 3520 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3521 | ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R, |
Fariborz Jahanian | 3337b2e | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 3522 | bool receiverIdOrClass, |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3523 | bool instance) { |
Douglas Gregor | 70f449b | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 3524 | if (ExternalSource) |
| 3525 | ReadMethodPool(Sel); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3526 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3527 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
Douglas Gregor | 70f449b | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 3528 | if (Pos == MethodPool.end()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3529 | return nullptr; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3530 | |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3531 | // Gather the non-hidden methods. |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3532 | ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second; |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 3533 | SmallVector<ObjCMethodDecl *, 4> Methods; |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 3534 | for (ObjCMethodList *M = &MethList; M; M = M->getNext()) { |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3535 | if (M->getMethod() && !M->getMethod()->isHidden()) |
| 3536 | return M->getMethod(); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3537 | } |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3538 | return nullptr; |
| 3539 | } |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3540 | |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3541 | void Sema::DiagnoseMultipleMethodInGlobalPool(SmallVectorImpl<ObjCMethodDecl*> &Methods, |
| 3542 | Selector Sel, SourceRange R, |
| 3543 | bool receiverIdOrClass) { |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3544 | // We found multiple methods, so we may have to complain. |
| 3545 | bool issueDiagnostic = false, issueError = false; |
Jonathan Roelofs | 7441136 | 2015-04-28 18:04:44 +0000 | [diff] [blame] | 3546 | |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3547 | // We support a warning which complains about *any* difference in |
| 3548 | // method signature. |
| 3549 | bool strictSelectorMatch = |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3550 | receiverIdOrClass && |
| 3551 | !Diags.isIgnored(diag::warn_strict_multiple_method_decl, R.getBegin()); |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3552 | if (strictSelectorMatch) { |
| 3553 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 3554 | if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) { |
| 3555 | issueDiagnostic = true; |
| 3556 | break; |
| 3557 | } |
| 3558 | } |
| 3559 | } |
Jonathan Roelofs | 7441136 | 2015-04-28 18:04:44 +0000 | [diff] [blame] | 3560 | |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3561 | // If we didn't see any strict differences, we won't see any loose |
| 3562 | // differences. In ARC, however, we also need to check for loose |
| 3563 | // mismatches, because most of them are errors. |
| 3564 | if (!strictSelectorMatch || |
| 3565 | (issueDiagnostic && getLangOpts().ObjCAutoRefCount)) |
| 3566 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 3567 | // This checks if the methods differ in type mismatch. |
| 3568 | if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) && |
| 3569 | !isAcceptableMethodMismatch(Methods[0], Methods[I])) { |
| 3570 | issueDiagnostic = true; |
| 3571 | if (getLangOpts().ObjCAutoRefCount) |
| 3572 | issueError = true; |
| 3573 | break; |
| 3574 | } |
| 3575 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3576 | |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3577 | if (issueDiagnostic) { |
| 3578 | if (issueError) |
| 3579 | Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R; |
| 3580 | else if (strictSelectorMatch) |
| 3581 | Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R; |
| 3582 | else |
| 3583 | Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3584 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3585 | Diag(Methods[0]->getBeginLoc(), |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3586 | issueError ? diag::note_possibility : diag::note_using) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3587 | << Methods[0]->getSourceRange(); |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3588 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3589 | Diag(Methods[I]->getBeginLoc(), diag::note_also_found) |
| 3590 | << Methods[I]->getSourceRange(); |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3591 | } |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3592 | } |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3593 | } |
| 3594 | |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 3595 | ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) { |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3596 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
| 3597 | if (Pos == MethodPool.end()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3598 | return nullptr; |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3599 | |
| 3600 | GlobalMethods &Methods = Pos->second; |
Fariborz Jahanian | ec762bd | 2014-03-26 20:59:26 +0000 | [diff] [blame] | 3601 | for (const ObjCMethodList *Method = &Methods.first; Method; |
| 3602 | Method = Method->getNext()) |
Fariborz Jahanian | 4019c7f | 2015-02-19 21:52:41 +0000 | [diff] [blame] | 3603 | if (Method->getMethod() && |
| 3604 | (Method->getMethod()->isDefined() || |
| 3605 | Method->getMethod()->isPropertyAccessor())) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3606 | return Method->getMethod(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3607 | |
Fariborz Jahanian | ec762bd | 2014-03-26 20:59:26 +0000 | [diff] [blame] | 3608 | for (const ObjCMethodList *Method = &Methods.second; Method; |
| 3609 | Method = Method->getNext()) |
Fariborz Jahanian | 4019c7f | 2015-02-19 21:52:41 +0000 | [diff] [blame] | 3610 | if (Method->getMethod() && |
| 3611 | (Method->getMethod()->isDefined() || |
| 3612 | Method->getMethod()->isPropertyAccessor())) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3613 | return Method->getMethod(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3614 | return nullptr; |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 3615 | } |
| 3616 | |
Fariborz Jahanian | 42f8938 | 2013-05-30 21:48:58 +0000 | [diff] [blame] | 3617 | static void |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3618 | HelperSelectorsForTypoCorrection( |
| 3619 | SmallVectorImpl<const ObjCMethodDecl *> &BestMethod, |
| 3620 | StringRef Typo, const ObjCMethodDecl * Method) { |
| 3621 | const unsigned MaxEditDistance = 1; |
| 3622 | unsigned BestEditDistance = MaxEditDistance + 1; |
Richard Trieu | ea8d370 | 2013-06-06 02:22:29 +0000 | [diff] [blame] | 3623 | std::string MethodName = Method->getSelector().getAsString(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3624 | |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3625 | unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size()); |
| 3626 | if (MinPossibleEditDistance > 0 && |
| 3627 | Typo.size() / MinPossibleEditDistance < 1) |
| 3628 | return; |
| 3629 | unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance); |
| 3630 | if (EditDistance > MaxEditDistance) |
| 3631 | return; |
| 3632 | if (EditDistance == BestEditDistance) |
| 3633 | BestMethod.push_back(Method); |
| 3634 | else if (EditDistance < BestEditDistance) { |
| 3635 | BestMethod.clear(); |
| 3636 | BestMethod.push_back(Method); |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3637 | } |
| 3638 | } |
| 3639 | |
Fariborz Jahanian | 7548167 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 3640 | static bool HelperIsMethodInObjCType(Sema &S, Selector Sel, |
| 3641 | QualType ObjectType) { |
| 3642 | if (ObjectType.isNull()) |
| 3643 | return true; |
| 3644 | if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/)) |
| 3645 | return true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3646 | return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) != |
| 3647 | nullptr; |
Fariborz Jahanian | 7548167 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 3648 | } |
| 3649 | |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3650 | const ObjCMethodDecl * |
Fariborz Jahanian | 7548167 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 3651 | Sema::SelectorsForTypoCorrection(Selector Sel, |
| 3652 | QualType ObjectType) { |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3653 | unsigned NumArgs = Sel.getNumArgs(); |
| 3654 | SmallVector<const ObjCMethodDecl *, 8> Methods; |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3655 | bool ObjectIsId = true, ObjectIsClass = true; |
| 3656 | if (ObjectType.isNull()) |
| 3657 | ObjectIsId = ObjectIsClass = false; |
| 3658 | else if (!ObjectType->isObjCObjectPointerType()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3659 | return nullptr; |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3660 | else if (const ObjCObjectPointerType *ObjCPtr = |
| 3661 | ObjectType->getAsObjCInterfacePointerType()) { |
| 3662 | ObjectType = QualType(ObjCPtr->getInterfaceType(), 0); |
| 3663 | ObjectIsId = ObjectIsClass = false; |
| 3664 | } |
| 3665 | else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType()) |
| 3666 | ObjectIsClass = false; |
| 3667 | else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType()) |
| 3668 | ObjectIsId = false; |
| 3669 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3670 | return nullptr; |
| 3671 | |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3672 | for (GlobalMethodPool::iterator b = MethodPool.begin(), |
| 3673 | e = MethodPool.end(); b != e; b++) { |
| 3674 | // instance methods |
| 3675 | for (ObjCMethodList *M = &b->second.first; M; M=M->getNext()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3676 | if (M->getMethod() && |
| 3677 | (M->getMethod()->getSelector().getNumArgs() == NumArgs) && |
| 3678 | (M->getMethod()->getSelector() != Sel)) { |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3679 | if (ObjectIsId) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3680 | Methods.push_back(M->getMethod()); |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3681 | else if (!ObjectIsClass && |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3682 | HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(), |
| 3683 | ObjectType)) |
| 3684 | Methods.push_back(M->getMethod()); |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3685 | } |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3686 | // class methods |
| 3687 | for (ObjCMethodList *M = &b->second.second; M; M=M->getNext()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3688 | if (M->getMethod() && |
| 3689 | (M->getMethod()->getSelector().getNumArgs() == NumArgs) && |
| 3690 | (M->getMethod()->getSelector() != Sel)) { |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3691 | if (ObjectIsClass) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3692 | Methods.push_back(M->getMethod()); |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3693 | else if (!ObjectIsId && |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3694 | HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(), |
| 3695 | ObjectType)) |
| 3696 | Methods.push_back(M->getMethod()); |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3697 | } |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3698 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3699 | |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3700 | SmallVector<const ObjCMethodDecl *, 8> SelectedMethods; |
| 3701 | for (unsigned i = 0, e = Methods.size(); i < e; i++) { |
| 3702 | HelperSelectorsForTypoCorrection(SelectedMethods, |
| 3703 | Sel.getAsString(), Methods[i]); |
| 3704 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3705 | return (SelectedMethods.size() == 1) ? SelectedMethods[0] : nullptr; |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
Fariborz Jahanian | 42f8938 | 2013-05-30 21:48:58 +0000 | [diff] [blame] | 3708 | /// DiagnoseDuplicateIvars - |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3709 | /// Check for duplicate ivars in the entire class at the start of |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 3710 | /// \@implementation. This becomes necesssary because class extension can |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 3711 | /// 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] | 3712 | /// class's \@implementation is seen. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3713 | void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 3714 | ObjCInterfaceDecl *SID) { |
Aaron Ballman | 59abbd4 | 2014-03-13 21:09:43 +0000 | [diff] [blame] | 3715 | for (auto *Ivar : ID->ivars()) { |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 3716 | if (Ivar->isInvalidDecl()) |
| 3717 | continue; |
| 3718 | if (IdentifierInfo *II = Ivar->getIdentifier()) { |
| 3719 | ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II); |
| 3720 | if (prevIvar) { |
| 3721 | Diag(Ivar->getLocation(), diag::err_duplicate_member) << II; |
| 3722 | Diag(prevIvar->getLocation(), diag::note_previous_declaration); |
| 3723 | Ivar->setInvalidDecl(); |
| 3724 | } |
| 3725 | } |
| 3726 | } |
| 3727 | } |
| 3728 | |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 3729 | /// Diagnose attempts to define ARC-__weak ivars when __weak is disabled. |
| 3730 | static void DiagnoseWeakIvars(Sema &S, ObjCImplementationDecl *ID) { |
| 3731 | if (S.getLangOpts().ObjCWeak) return; |
| 3732 | |
| 3733 | for (auto ivar = ID->getClassInterface()->all_declared_ivar_begin(); |
| 3734 | ivar; ivar = ivar->getNextIvar()) { |
| 3735 | if (ivar->isInvalidDecl()) continue; |
| 3736 | if (ivar->getType().getObjCLifetime() == Qualifiers::OCL_Weak) { |
| 3737 | if (S.getLangOpts().ObjCWeakRuntime) { |
| 3738 | S.Diag(ivar->getLocation(), diag::err_arc_weak_disabled); |
| 3739 | } else { |
| 3740 | S.Diag(ivar->getLocation(), diag::err_arc_weak_no_runtime); |
| 3741 | } |
| 3742 | } |
| 3743 | } |
| 3744 | } |
| 3745 | |
Volodymyr Sapsai | 30680e9 | 2017-10-23 22:01:41 +0000 | [diff] [blame] | 3746 | /// Diagnose attempts to use flexible array member with retainable object type. |
| 3747 | static void DiagnoseRetainableFlexibleArrayMember(Sema &S, |
| 3748 | ObjCInterfaceDecl *ID) { |
| 3749 | if (!S.getLangOpts().ObjCAutoRefCount) |
| 3750 | return; |
| 3751 | |
| 3752 | for (auto ivar = ID->all_declared_ivar_begin(); ivar; |
| 3753 | ivar = ivar->getNextIvar()) { |
| 3754 | if (ivar->isInvalidDecl()) |
| 3755 | continue; |
| 3756 | QualType IvarTy = ivar->getType(); |
| 3757 | if (IvarTy->isIncompleteArrayType() && |
| 3758 | (IvarTy.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) && |
| 3759 | IvarTy->isObjCLifetimeType()) { |
| 3760 | S.Diag(ivar->getLocation(), diag::err_flexible_array_arc_retainable); |
| 3761 | ivar->setInvalidDecl(); |
| 3762 | } |
| 3763 | } |
| 3764 | } |
| 3765 | |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3766 | Sema::ObjCContainerKind Sema::getObjCContainerKind() const { |
| 3767 | switch (CurContext->getDeclKind()) { |
| 3768 | case Decl::ObjCInterface: |
| 3769 | return Sema::OCK_Interface; |
| 3770 | case Decl::ObjCProtocol: |
| 3771 | return Sema::OCK_Protocol; |
| 3772 | case Decl::ObjCCategory: |
Benjamin Kramer | a008d3a | 2015-04-10 11:37:55 +0000 | [diff] [blame] | 3773 | if (cast<ObjCCategoryDecl>(CurContext)->IsClassExtension()) |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3774 | return Sema::OCK_ClassExtension; |
Benjamin Kramer | a008d3a | 2015-04-10 11:37:55 +0000 | [diff] [blame] | 3775 | return Sema::OCK_Category; |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3776 | case Decl::ObjCImplementation: |
| 3777 | return Sema::OCK_Implementation; |
| 3778 | case Decl::ObjCCategoryImpl: |
| 3779 | return Sema::OCK_CategoryImplementation; |
| 3780 | |
| 3781 | default: |
| 3782 | return Sema::OCK_None; |
| 3783 | } |
| 3784 | } |
| 3785 | |
Volodymyr Sapsai | 30680e9 | 2017-10-23 22:01:41 +0000 | [diff] [blame] | 3786 | static bool IsVariableSizedType(QualType T) { |
| 3787 | if (T->isIncompleteArrayType()) |
| 3788 | return true; |
| 3789 | const auto *RecordTy = T->getAs<RecordType>(); |
| 3790 | return (RecordTy && RecordTy->getDecl()->hasFlexibleArrayMember()); |
| 3791 | } |
| 3792 | |
| 3793 | static void DiagnoseVariableSizedIvars(Sema &S, ObjCContainerDecl *OCD) { |
| 3794 | ObjCInterfaceDecl *IntfDecl = nullptr; |
| 3795 | ObjCInterfaceDecl::ivar_range Ivars = llvm::make_range( |
| 3796 | ObjCInterfaceDecl::ivar_iterator(), ObjCInterfaceDecl::ivar_iterator()); |
| 3797 | if ((IntfDecl = dyn_cast<ObjCInterfaceDecl>(OCD))) { |
| 3798 | Ivars = IntfDecl->ivars(); |
| 3799 | } else if (auto *ImplDecl = dyn_cast<ObjCImplementationDecl>(OCD)) { |
| 3800 | IntfDecl = ImplDecl->getClassInterface(); |
| 3801 | Ivars = ImplDecl->ivars(); |
| 3802 | } else if (auto *CategoryDecl = dyn_cast<ObjCCategoryDecl>(OCD)) { |
| 3803 | if (CategoryDecl->IsClassExtension()) { |
| 3804 | IntfDecl = CategoryDecl->getClassInterface(); |
| 3805 | Ivars = CategoryDecl->ivars(); |
| 3806 | } |
| 3807 | } |
| 3808 | |
| 3809 | // Check if variable sized ivar is in interface and visible to subclasses. |
| 3810 | if (!isa<ObjCInterfaceDecl>(OCD)) { |
| 3811 | for (auto ivar : Ivars) { |
| 3812 | if (!ivar->isInvalidDecl() && IsVariableSizedType(ivar->getType())) { |
| 3813 | S.Diag(ivar->getLocation(), diag::warn_variable_sized_ivar_visibility) |
| 3814 | << ivar->getDeclName() << ivar->getType(); |
| 3815 | } |
| 3816 | } |
| 3817 | } |
| 3818 | |
| 3819 | // Subsequent checks require interface decl. |
| 3820 | if (!IntfDecl) |
| 3821 | return; |
| 3822 | |
| 3823 | // Check if variable sized ivar is followed by another ivar. |
| 3824 | for (ObjCIvarDecl *ivar = IntfDecl->all_declared_ivar_begin(); ivar; |
| 3825 | ivar = ivar->getNextIvar()) { |
| 3826 | if (ivar->isInvalidDecl() || !ivar->getNextIvar()) |
| 3827 | continue; |
| 3828 | QualType IvarTy = ivar->getType(); |
| 3829 | bool IsInvalidIvar = false; |
| 3830 | if (IvarTy->isIncompleteArrayType()) { |
| 3831 | S.Diag(ivar->getLocation(), diag::err_flexible_array_not_at_end) |
| 3832 | << ivar->getDeclName() << IvarTy |
| 3833 | << TTK_Class; // Use "class" for Obj-C. |
| 3834 | IsInvalidIvar = true; |
| 3835 | } else if (const RecordType *RecordTy = IvarTy->getAs<RecordType>()) { |
| 3836 | if (RecordTy->getDecl()->hasFlexibleArrayMember()) { |
| 3837 | S.Diag(ivar->getLocation(), |
| 3838 | diag::err_objc_variable_sized_type_not_at_end) |
| 3839 | << ivar->getDeclName() << IvarTy; |
| 3840 | IsInvalidIvar = true; |
| 3841 | } |
| 3842 | } |
| 3843 | if (IsInvalidIvar) { |
| 3844 | S.Diag(ivar->getNextIvar()->getLocation(), |
| 3845 | diag::note_next_ivar_declaration) |
| 3846 | << ivar->getNextIvar()->getSynthesize(); |
| 3847 | ivar->setInvalidDecl(); |
| 3848 | } |
| 3849 | } |
| 3850 | |
| 3851 | // Check if ObjC container adds ivars after variable sized ivar in superclass. |
| 3852 | // Perform the check only if OCD is the first container to declare ivars to |
| 3853 | // avoid multiple warnings for the same ivar. |
| 3854 | ObjCIvarDecl *FirstIvar = |
| 3855 | (Ivars.begin() == Ivars.end()) ? nullptr : *Ivars.begin(); |
| 3856 | if (FirstIvar && (FirstIvar == IntfDecl->all_declared_ivar_begin())) { |
| 3857 | const ObjCInterfaceDecl *SuperClass = IntfDecl->getSuperClass(); |
| 3858 | while (SuperClass && SuperClass->ivar_empty()) |
| 3859 | SuperClass = SuperClass->getSuperClass(); |
| 3860 | if (SuperClass) { |
| 3861 | auto IvarIter = SuperClass->ivar_begin(); |
| 3862 | std::advance(IvarIter, SuperClass->ivar_size() - 1); |
| 3863 | const ObjCIvarDecl *LastIvar = *IvarIter; |
| 3864 | if (IsVariableSizedType(LastIvar->getType())) { |
| 3865 | S.Diag(FirstIvar->getLocation(), |
| 3866 | diag::warn_superclass_variable_sized_type_not_at_end) |
| 3867 | << FirstIvar->getDeclName() << LastIvar->getDeclName() |
| 3868 | << LastIvar->getType() << SuperClass->getDeclName(); |
| 3869 | S.Diag(LastIvar->getLocation(), diag::note_entity_declared_at) |
| 3870 | << LastIvar->getDeclName(); |
| 3871 | } |
| 3872 | } |
| 3873 | } |
| 3874 | } |
| 3875 | |
Fariborz Jahanian | 0080fb5 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 3876 | // Note: For class/category implementations, allMethods is always null. |
Robert Wilhelm | 57c6711 | 2013-07-17 21:14:35 +0000 | [diff] [blame] | 3877 | Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods, |
Fariborz Jahanian | dfb7687 | 2013-07-17 00:05:08 +0000 | [diff] [blame] | 3878 | ArrayRef<DeclGroupPtrTy> allTUVars) { |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3879 | if (getObjCContainerKind() == Sema::OCK_None) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3880 | return nullptr; |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3881 | |
| 3882 | assert(AtEnd.isValid() && "Invalid location for '@end'"); |
| 3883 | |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 3884 | auto *OCD = cast<ObjCContainerDecl>(CurContext); |
| 3885 | Decl *ClassDecl = OCD; |
| 3886 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3887 | bool isInterfaceDeclKind = |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 3888 | isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) |
| 3889 | || isa<ObjCProtocolDecl>(ClassDecl); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3890 | bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 3891 | |
Steve Naroff | 35c62ae | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 3892 | // FIXME: Remove these and use the ObjCContainerDecl/DeclContext. |
| 3893 | llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; |
| 3894 | llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; |
| 3895 | |
Fariborz Jahanian | 0080fb5 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 3896 | for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3897 | ObjCMethodDecl *Method = |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3898 | cast_or_null<ObjCMethodDecl>(allMethods[i]); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3899 | |
| 3900 | if (!Method) continue; // Already issued a diagnostic. |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 3901 | if (Method->isInstanceMethod()) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3902 | /// Check for instance method of the same name with incompatible types |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3903 | const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3904 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3905 | : false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3906 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 42b1e9e | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 3907 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3908 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3909 | << Method->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3910 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | 87e9275 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 3911 | Method->setInvalidDecl(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3912 | } else { |
Fariborz Jahanian | c17c86b | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 3913 | if (PrevMethod) { |
Argyrios Kyrtzidis | dcaaa21 | 2011-10-14 08:02:31 +0000 | [diff] [blame] | 3914 | Method->setAsRedeclaration(PrevMethod); |
Fariborz Jahanian | c17c86b | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 3915 | if (!Context.getSourceManager().isInSystemHeader( |
| 3916 | Method->getLocation())) |
| 3917 | Diag(Method->getLocation(), diag::warn_duplicate_method_decl) |
| 3918 | << Method->getDeclName(); |
| 3919 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 3920 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3921 | InsMap[Method->getSelector()] = Method; |
| 3922 | /// The following allows us to typecheck messages to "id". |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3923 | AddInstanceMethodToGlobalPool(Method); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3924 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3925 | } else { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3926 | /// Check for class method of the same name with incompatible types |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3927 | const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3928 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3929 | : false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3930 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 42b1e9e | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 3931 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3932 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3933 | << Method->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3934 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | 87e9275 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 3935 | Method->setInvalidDecl(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3936 | } else { |
Fariborz Jahanian | c17c86b | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 3937 | if (PrevMethod) { |
Argyrios Kyrtzidis | dcaaa21 | 2011-10-14 08:02:31 +0000 | [diff] [blame] | 3938 | Method->setAsRedeclaration(PrevMethod); |
Fariborz Jahanian | c17c86b | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 3939 | if (!Context.getSourceManager().isInSystemHeader( |
| 3940 | Method->getLocation())) |
| 3941 | Diag(Method->getLocation(), diag::warn_duplicate_method_decl) |
| 3942 | << Method->getDeclName(); |
| 3943 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 3944 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3945 | ClsMap[Method->getSelector()] = Method; |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3946 | AddFactoryMethodToGlobalPool(Method); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3947 | } |
| 3948 | } |
| 3949 | } |
Douglas Gregor | b898209 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 3950 | if (isa<ObjCInterfaceDecl>(ClassDecl)) { |
| 3951 | // Nothing to do here. |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 3952 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Fariborz Jahanian | 62293f4 | 2008-12-06 19:59:02 +0000 | [diff] [blame] | 3953 | // Categories are used to extend the class by declaring new methods. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3954 | // 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] | 3955 | // need to compare the added property to those in the class. |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 3956 | |
Fariborz Jahanian | c21f543 | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 3957 | if (C->IsClassExtension()) { |
| 3958 | ObjCInterfaceDecl *CCPrimary = C->getClassInterface(); |
| 3959 | DiagnoseClassExtensionDupMethods(C, CCPrimary); |
Fariborz Jahanian | c21f543 | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 3960 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3961 | } |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 3962 | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) { |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 3963 | if (CDecl->getIdentifier()) |
| 3964 | // ProcessPropertyDecl is responsible for diagnosing conflicts with any |
| 3965 | // user-defined setter/getter. It also synthesizes setter/getter methods |
| 3966 | // and adds them to the DeclContext and global method pools. |
Manman Ren | efe1bac | 2016-01-27 20:00:32 +0000 | [diff] [blame] | 3967 | for (auto *I : CDecl->properties()) |
Douglas Gregor | e17765e | 2015-11-03 17:02:34 +0000 | [diff] [blame] | 3968 | ProcessPropertyDecl(I); |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 3969 | CDecl->setAtEndRange(AtEnd); |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 3970 | } |
| 3971 | if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 3972 | IC->setAtEndRange(AtEnd); |
Fariborz Jahanian | 13e0c90 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 3973 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) { |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 3974 | // Any property declared in a class extension might have user |
| 3975 | // declared setter or getter in current class extension or one |
| 3976 | // of the other class extensions. Mark them as synthesized as |
| 3977 | // property will be synthesized when property with same name is |
| 3978 | // seen in the @implementation. |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 3979 | for (const auto *Ext : IDecl->visible_extensions()) { |
Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame] | 3980 | for (const auto *Property : Ext->instance_properties()) { |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 3981 | // Skip over properties declared @dynamic |
| 3982 | if (const ObjCPropertyImplDecl *PIDecl |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 3983 | = IC->FindPropertyImplDecl(Property->getIdentifier(), |
| 3984 | Property->getQueryKind())) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3985 | if (PIDecl->getPropertyImplementation() |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 3986 | == ObjCPropertyImplDecl::Dynamic) |
| 3987 | continue; |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 3988 | |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 3989 | for (const auto *Ext : IDecl->visible_extensions()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 3990 | if (ObjCMethodDecl *GetterMethod |
| 3991 | = Ext->getInstanceMethod(Property->getGetterName())) |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 3992 | GetterMethod->setPropertyAccessor(true); |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 3993 | if (!Property->isReadOnly()) |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 3994 | if (ObjCMethodDecl *SetterMethod |
| 3995 | = Ext->getInstanceMethod(Property->getSetterName())) |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 3996 | SetterMethod->setPropertyAccessor(true); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 3997 | } |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 3998 | } |
| 3999 | } |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 4000 | ImplMethodsVsClassMethods(S, IC, IDecl); |
Fariborz Jahanian | 13e0c90 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 4001 | AtomicPropertySetterGetterRules(IC, IDecl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4002 | DiagnoseOwningPropertyGetterSynthesis(IC); |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4003 | DiagnoseUnusedBackingIvarInAccessor(S, IC); |
Fariborz Jahanian | 20cfff3 | 2015-03-11 16:59:48 +0000 | [diff] [blame] | 4004 | if (IDecl->hasDesignatedInitializers()) |
| 4005 | DiagnoseMissingDesignatedInitOverrides(IC, IDecl); |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 4006 | DiagnoseWeakIvars(*this, IC); |
Volodymyr Sapsai | 30680e9 | 2017-10-23 22:01:41 +0000 | [diff] [blame] | 4007 | DiagnoseRetainableFlexibleArrayMember(*this, IDecl); |
Argyrios Kyrtzidis | db5ce0f | 2013-12-03 21:11:54 +0000 | [diff] [blame] | 4008 | |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4009 | bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4010 | if (IDecl->getSuperClass() == nullptr) { |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4011 | // This class has no superclass, so check that it has been marked with |
| 4012 | // __attribute((objc_root_class)). |
| 4013 | if (!HasRootClassAttr) { |
| 4014 | SourceLocation DeclLoc(IDecl->getLocation()); |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 4015 | SourceLocation SuperClassLoc(getLocForEndOfToken(DeclLoc)); |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4016 | Diag(DeclLoc, diag::warn_objc_root_class_missing) |
| 4017 | << IDecl->getIdentifier(); |
| 4018 | // See if NSObject is in the current scope, and if it is, suggest |
| 4019 | // adding " : NSObject " to the class declaration. |
| 4020 | NamedDecl *IF = LookupSingleName(TUScope, |
| 4021 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject), |
| 4022 | DeclLoc, LookupOrdinaryName); |
| 4023 | ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
| 4024 | if (NSObjectDecl && NSObjectDecl->getDefinition()) { |
| 4025 | Diag(SuperClassLoc, diag::note_objc_needs_superclass) |
| 4026 | << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject "); |
| 4027 | } else { |
| 4028 | Diag(SuperClassLoc, diag::note_objc_needs_superclass); |
| 4029 | } |
| 4030 | } |
| 4031 | } else if (HasRootClassAttr) { |
| 4032 | // Complain that only root classes may have this attribute. |
| 4033 | Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass); |
| 4034 | } |
| 4035 | |
Alex Lorenz | a8c44ba | 2016-10-28 10:25:10 +0000 | [diff] [blame] | 4036 | if (const ObjCInterfaceDecl *Super = IDecl->getSuperClass()) { |
| 4037 | // An interface can subclass another interface with a |
| 4038 | // objc_subclassing_restricted attribute when it has that attribute as |
| 4039 | // well (because of interfaces imported from Swift). Therefore we have |
| 4040 | // to check if we can subclass in the implementation as well. |
| 4041 | if (IDecl->hasAttr<ObjCSubclassingRestrictedAttr>() && |
| 4042 | Super->hasAttr<ObjCSubclassingRestrictedAttr>()) { |
| 4043 | Diag(IC->getLocation(), diag::err_restricted_superclass_mismatch); |
| 4044 | Diag(Super->getLocation(), diag::note_class_declared); |
| 4045 | } |
| 4046 | } |
| 4047 | |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 4048 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 4049 | while (IDecl->getSuperClass()) { |
| 4050 | DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass()); |
| 4051 | IDecl = IDecl->getSuperClass(); |
| 4052 | } |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4053 | } |
Fariborz Jahanian | 13e0c90 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 4054 | } |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 4055 | SetIvarInitializers(IC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4056 | } else if (ObjCCategoryImplDecl* CatImplClass = |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 4057 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 4058 | CatImplClass->setAtEndRange(AtEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4059 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4060 | // Find category interface decl and then check that all methods declared |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 4061 | // in this interface are implemented in the category @implementation. |
Chris Lattner | 41fd42e | 2009-02-16 18:32:47 +0000 | [diff] [blame] | 4062 | if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 4063 | if (ObjCCategoryDecl *Cat |
| 4064 | = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) { |
| 4065 | ImplMethodsVsClassMethods(S, CatImplClass, Cat); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4066 | } |
| 4067 | } |
Alex Lorenz | a8c44ba | 2016-10-28 10:25:10 +0000 | [diff] [blame] | 4068 | } else if (const auto *IntfDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
| 4069 | if (const ObjCInterfaceDecl *Super = IntfDecl->getSuperClass()) { |
| 4070 | if (!IntfDecl->hasAttr<ObjCSubclassingRestrictedAttr>() && |
| 4071 | Super->hasAttr<ObjCSubclassingRestrictedAttr>()) { |
| 4072 | Diag(IntfDecl->getLocation(), diag::err_restricted_superclass_mismatch); |
| 4073 | Diag(Super->getLocation(), diag::note_class_declared); |
| 4074 | } |
| 4075 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4076 | } |
Volodymyr Sapsai | 30680e9 | 2017-10-23 22:01:41 +0000 | [diff] [blame] | 4077 | DiagnoseVariableSizedIvars(*this, OCD); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 4078 | if (isInterfaceDeclKind) { |
| 4079 | // Reject invalid vardecls. |
Fariborz Jahanian | 0080fb5 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 4080 | for (unsigned i = 0, e = allTUVars.size(); i != e; i++) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 4081 | DeclGroupRef DG = allTUVars[i].get(); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 4082 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 4083 | if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) { |
Daniel Dunbar | 0ca1660 | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 4084 | if (!VDecl->hasExternalStorage()) |
Steve Naroff | 42959b2 | 2009-04-13 17:58:46 +0000 | [diff] [blame] | 4085 | Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass); |
Fariborz Jahanian | 629aed9 | 2009-03-21 18:06:45 +0000 | [diff] [blame] | 4086 | } |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 4087 | } |
Fariborz Jahanian | 3654e65 | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 4088 | } |
Fariborz Jahanian | 4327b32 | 2011-08-29 17:33:12 +0000 | [diff] [blame] | 4089 | ActOnObjCContainerFinishDefinition(); |
Argyrios Kyrtzidis | bd8b150 | 2011-10-17 19:48:13 +0000 | [diff] [blame] | 4090 | |
Fariborz Jahanian | 0080fb5 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 4091 | for (unsigned i = 0, e = allTUVars.size(); i != e; i++) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 4092 | DeclGroupRef DG = allTUVars[i].get(); |
Argyrios Kyrtzidis | 8ad3bab | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 4093 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 4094 | (*I)->setTopLevelDeclInObjCContainer(); |
Argyrios Kyrtzidis | bd8b150 | 2011-10-17 19:48:13 +0000 | [diff] [blame] | 4095 | Consumer.HandleTopLevelDeclInObjCContainer(DG); |
| 4096 | } |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 4097 | |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 4098 | ActOnDocumentableDecl(ClassDecl); |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 4099 | return ClassDecl; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4100 | } |
| 4101 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4102 | /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for |
| 4103 | /// 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] | 4104 | static Decl::ObjCDeclQualifier |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4105 | CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { |
John McCall | ca87290 | 2011-05-01 03:04:29 +0000 | [diff] [blame] | 4106 | return (Decl::ObjCDeclQualifier) (unsigned) PQTVal; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4109 | /// Check whether the declared result type of the given Objective-C |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4110 | /// method declaration is compatible with the method's class. |
| 4111 | /// |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4112 | static Sema::ResultTypeCompatibilityKind |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4113 | CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method, |
| 4114 | ObjCInterfaceDecl *CurrentClass) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4115 | QualType ResultType = Method->getReturnType(); |
| 4116 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4117 | // If an Objective-C method inherits its related result type, then its |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4118 | // declared result type must be compatible with its own class type. The |
| 4119 | // declared result type is compatible if: |
| 4120 | if (const ObjCObjectPointerType *ResultObjectType |
| 4121 | = ResultType->getAs<ObjCObjectPointerType>()) { |
| 4122 | // - it is id or qualified id, or |
| 4123 | if (ResultObjectType->isObjCIdType() || |
| 4124 | ResultObjectType->isObjCQualifiedIdType()) |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4125 | return Sema::RTC_Compatible; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4126 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4127 | if (CurrentClass) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4128 | if (ObjCInterfaceDecl *ResultClass |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4129 | = ResultObjectType->getInterfaceDecl()) { |
| 4130 | // - it is the same as the method's class type, or |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 4131 | if (declaresSameEntity(CurrentClass, ResultClass)) |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4132 | return Sema::RTC_Compatible; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4133 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4134 | // - it is a superclass of the method's class type |
| 4135 | if (ResultClass->isSuperClassOf(CurrentClass)) |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4136 | return Sema::RTC_Compatible; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4137 | } |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4138 | } else { |
| 4139 | // Any Objective-C pointer type might be acceptable for a protocol |
| 4140 | // method; we just don't know. |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4141 | return Sema::RTC_Unknown; |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4142 | } |
| 4143 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4144 | |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4145 | return Sema::RTC_Incompatible; |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4148 | namespace { |
| 4149 | /// A helper class for searching for methods which a particular method |
| 4150 | /// overrides. |
| 4151 | class OverrideSearch { |
Daniel Dunbar | d6d74c3 | 2012-02-29 03:04:05 +0000 | [diff] [blame] | 4152 | public: |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4153 | Sema &S; |
| 4154 | ObjCMethodDecl *Method; |
Akira Hatanaka | 4c687f3 | 2018-02-06 23:44:40 +0000 | [diff] [blame] | 4155 | llvm::SmallSetVector<ObjCMethodDecl*, 4> Overridden; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4156 | bool Recursive; |
| 4157 | |
| 4158 | public: |
| 4159 | OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) { |
| 4160 | Selector selector = method->getSelector(); |
| 4161 | |
| 4162 | // Bypass this search if we've never seen an instance/class method |
| 4163 | // with this selector before. |
| 4164 | Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector); |
| 4165 | if (it == S.MethodPool.end()) { |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 4166 | if (!S.getExternalSource()) return; |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 4167 | S.ReadMethodPool(selector); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4168 | |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 4169 | it = S.MethodPool.find(selector); |
| 4170 | if (it == S.MethodPool.end()) |
| 4171 | return; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4172 | } |
| 4173 | ObjCMethodList &list = |
| 4174 | method->isInstanceMethod() ? it->second.first : it->second.second; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 4175 | if (!list.getMethod()) return; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4176 | |
| 4177 | ObjCContainerDecl *container |
| 4178 | = cast<ObjCContainerDecl>(method->getDeclContext()); |
| 4179 | |
| 4180 | // Prevent the search from reaching this container again. This is |
| 4181 | // important with categories, which override methods from the |
| 4182 | // interface and each other. |
Douglas Gregor | cf4ac44 | 2012-05-03 21:25:24 +0000 | [diff] [blame] | 4183 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) { |
| 4184 | searchFromContainer(container); |
Douglas Gregor | c5928af | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 4185 | if (ObjCInterfaceDecl *Interface = Category->getClassInterface()) |
| 4186 | searchFromContainer(Interface); |
Douglas Gregor | cf4ac44 | 2012-05-03 21:25:24 +0000 | [diff] [blame] | 4187 | } else { |
| 4188 | searchFromContainer(container); |
| 4189 | } |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4190 | } |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4191 | |
Akira Hatanaka | 4c687f3 | 2018-02-06 23:44:40 +0000 | [diff] [blame] | 4192 | typedef decltype(Overridden)::iterator iterator; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4193 | iterator begin() const { return Overridden.begin(); } |
| 4194 | iterator end() const { return Overridden.end(); } |
| 4195 | |
| 4196 | private: |
| 4197 | void searchFromContainer(ObjCContainerDecl *container) { |
| 4198 | if (container->isInvalidDecl()) return; |
| 4199 | |
| 4200 | switch (container->getDeclKind()) { |
| 4201 | #define OBJCCONTAINER(type, base) \ |
| 4202 | case Decl::type: \ |
| 4203 | searchFrom(cast<type##Decl>(container)); \ |
| 4204 | break; |
| 4205 | #define ABSTRACT_DECL(expansion) |
| 4206 | #define DECL(type, base) \ |
| 4207 | case Decl::type: |
| 4208 | #include "clang/AST/DeclNodes.inc" |
| 4209 | llvm_unreachable("not an ObjC container!"); |
| 4210 | } |
| 4211 | } |
| 4212 | |
| 4213 | void searchFrom(ObjCProtocolDecl *protocol) { |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 4214 | if (!protocol->hasDefinition()) |
| 4215 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4216 | |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4217 | // A method in a protocol declaration overrides declarations from |
| 4218 | // referenced ("parent") protocols. |
| 4219 | search(protocol->getReferencedProtocols()); |
| 4220 | } |
| 4221 | |
| 4222 | void searchFrom(ObjCCategoryDecl *category) { |
| 4223 | // A method in a category declaration overrides declarations from |
| 4224 | // the main class and from protocols the category references. |
Douglas Gregor | cf4ac44 | 2012-05-03 21:25:24 +0000 | [diff] [blame] | 4225 | // The main class is handled in the constructor. |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4226 | search(category->getReferencedProtocols()); |
| 4227 | } |
| 4228 | |
| 4229 | void searchFrom(ObjCCategoryImplDecl *impl) { |
| 4230 | // A method in a category definition that has a category |
| 4231 | // declaration overrides declarations from the category |
| 4232 | // declaration. |
| 4233 | if (ObjCCategoryDecl *category = impl->getCategoryDecl()) { |
| 4234 | search(category); |
Douglas Gregor | c5928af | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 4235 | if (ObjCInterfaceDecl *Interface = category->getClassInterface()) |
| 4236 | search(Interface); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4237 | |
| 4238 | // Otherwise it overrides declarations from the class. |
Douglas Gregor | c5928af | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 4239 | } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) { |
| 4240 | search(Interface); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4241 | } |
| 4242 | } |
| 4243 | |
| 4244 | void searchFrom(ObjCInterfaceDecl *iface) { |
| 4245 | // A method in a class declaration overrides declarations from |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 4246 | if (!iface->hasDefinition()) |
| 4247 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4248 | |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4249 | // - categories, |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 4250 | for (auto *Cat : iface->known_categories()) |
| 4251 | search(Cat); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4252 | |
| 4253 | // - the super class, and |
| 4254 | if (ObjCInterfaceDecl *super = iface->getSuperClass()) |
| 4255 | search(super); |
| 4256 | |
| 4257 | // - any referenced protocols. |
| 4258 | search(iface->getReferencedProtocols()); |
| 4259 | } |
| 4260 | |
| 4261 | void searchFrom(ObjCImplementationDecl *impl) { |
| 4262 | // A method in a class implementation overrides declarations from |
| 4263 | // the class interface. |
Douglas Gregor | c5928af | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 4264 | if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) |
| 4265 | search(Interface); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4266 | } |
| 4267 | |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4268 | void search(const ObjCProtocolList &protocols) { |
| 4269 | for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end(); |
| 4270 | i != e; ++i) |
| 4271 | search(*i); |
| 4272 | } |
| 4273 | |
| 4274 | void search(ObjCContainerDecl *container) { |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4275 | // Check for a method in this container which matches this selector. |
| 4276 | ObjCMethodDecl *meth = container->getMethod(Method->getSelector(), |
Argyrios Kyrtzidis | bd8cd3e | 2013-03-29 21:51:48 +0000 | [diff] [blame] | 4277 | Method->isInstanceMethod(), |
| 4278 | /*AllowHidden=*/true); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4279 | |
| 4280 | // If we find one, record it and bail out. |
| 4281 | if (meth) { |
| 4282 | Overridden.insert(meth); |
| 4283 | return; |
| 4284 | } |
| 4285 | |
| 4286 | // Otherwise, search for methods that a hypothetical method here |
| 4287 | // would have overridden. |
| 4288 | |
| 4289 | // Note that we're now in a recursive case. |
| 4290 | Recursive = true; |
| 4291 | |
| 4292 | searchFromContainer(container); |
| 4293 | } |
| 4294 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 4295 | } // end anonymous namespace |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4296 | |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4297 | void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod, |
| 4298 | ObjCInterfaceDecl *CurrentClass, |
| 4299 | ResultTypeCompatibilityKind RTC) { |
| 4300 | // Search for overridden methods and merge information down from them. |
| 4301 | OverrideSearch overrides(*this, ObjCMethod); |
| 4302 | // Keep track if the method overrides any method in the class's base classes, |
| 4303 | // its protocols, or its categories' protocols; we will keep that info |
| 4304 | // in the ObjCMethodDecl. |
| 4305 | // For this info, a method in an implementation is not considered as |
| 4306 | // overriding the same method in the interface or its categories. |
| 4307 | bool hasOverriddenMethodsInBaseOrProtocol = false; |
| 4308 | for (OverrideSearch::iterator |
| 4309 | i = overrides.begin(), e = overrides.end(); i != e; ++i) { |
| 4310 | ObjCMethodDecl *overridden = *i; |
| 4311 | |
Argyrios Kyrtzidis | c2091d5 | 2013-04-17 00:09:08 +0000 | [diff] [blame] | 4312 | if (!hasOverriddenMethodsInBaseOrProtocol) { |
| 4313 | if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) || |
| 4314 | CurrentClass != overridden->getClassInterface() || |
| 4315 | overridden->isOverriding()) { |
| 4316 | hasOverriddenMethodsInBaseOrProtocol = true; |
| 4317 | |
| 4318 | } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) { |
| 4319 | // OverrideSearch will return as "overridden" the same method in the |
| 4320 | // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to |
| 4321 | // check whether a category of a base class introduced a method with the |
| 4322 | // same selector, after the interface method declaration. |
| 4323 | // To avoid unnecessary lookups in the majority of cases, we use the |
| 4324 | // extra info bits in GlobalMethodPool to check whether there were any |
| 4325 | // category methods with this selector. |
| 4326 | GlobalMethodPool::iterator It = |
| 4327 | MethodPool.find(ObjCMethod->getSelector()); |
| 4328 | if (It != MethodPool.end()) { |
| 4329 | ObjCMethodList &List = |
| 4330 | ObjCMethod->isInstanceMethod()? It->second.first: It->second.second; |
| 4331 | unsigned CategCount = List.getBits(); |
| 4332 | if (CategCount > 0) { |
| 4333 | // If the method is in a category we'll do lookup if there were at |
| 4334 | // least 2 category methods recorded, otherwise only one will do. |
| 4335 | if (CategCount > 1 || |
| 4336 | !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) { |
| 4337 | OverrideSearch overrides(*this, overridden); |
| 4338 | for (OverrideSearch::iterator |
| 4339 | OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) { |
| 4340 | ObjCMethodDecl *SuperOverridden = *OI; |
Argyrios Kyrtzidis | 04703a6 | 2013-04-27 00:10:12 +0000 | [diff] [blame] | 4341 | if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) || |
| 4342 | CurrentClass != SuperOverridden->getClassInterface()) { |
Argyrios Kyrtzidis | c2091d5 | 2013-04-17 00:09:08 +0000 | [diff] [blame] | 4343 | hasOverriddenMethodsInBaseOrProtocol = true; |
| 4344 | overridden->setOverriding(true); |
| 4345 | break; |
| 4346 | } |
| 4347 | } |
| 4348 | } |
| 4349 | } |
| 4350 | } |
| 4351 | } |
| 4352 | } |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4353 | |
| 4354 | // Propagate down the 'related result type' bit from overridden methods. |
| 4355 | if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType()) |
Erich Keane | 9b18eca | 2018-08-01 21:31:08 +0000 | [diff] [blame] | 4356 | ObjCMethod->setRelatedResultType(); |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4357 | |
| 4358 | // Then merge the declarations. |
| 4359 | mergeObjCMethodDecls(ObjCMethod, overridden); |
| 4360 | |
| 4361 | if (ObjCMethod->isImplicit() && overridden->isImplicit()) |
| 4362 | continue; // Conflicting properties are detected elsewhere. |
| 4363 | |
| 4364 | // Check for overriding methods |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4365 | if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) || |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4366 | isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) |
| 4367 | CheckConflictingOverridingMethod(ObjCMethod, overridden, |
| 4368 | isa<ObjCProtocolDecl>(overridden->getDeclContext())); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4369 | |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4370 | if (CurrentClass && overridden->getDeclContext() != CurrentClass && |
Fariborz Jahanian | 31a2568 | 2012-07-05 22:26:07 +0000 | [diff] [blame] | 4371 | isa<ObjCInterfaceDecl>(overridden->getDeclContext()) && |
| 4372 | !overridden->isImplicit() /* not meant for properties */) { |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4373 | ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(), |
| 4374 | E = ObjCMethod->param_end(); |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 4375 | ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(), |
| 4376 | PrevE = overridden->param_end(); |
| 4377 | for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) { |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4378 | assert(PrevI != overridden->param_end() && "Param mismatch"); |
| 4379 | QualType T1 = Context.getCanonicalType((*ParamI)->getType()); |
| 4380 | QualType T2 = Context.getCanonicalType((*PrevI)->getType()); |
| 4381 | // If type of argument of method in this class does not match its |
| 4382 | // respective argument type in the super class method, issue warning; |
| 4383 | if (!Context.typesAreCompatible(T1, T2)) { |
| 4384 | Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super) |
| 4385 | << T1 << T2; |
| 4386 | Diag(overridden->getLocation(), diag::note_previous_declaration); |
| 4387 | break; |
| 4388 | } |
| 4389 | } |
| 4390 | } |
| 4391 | } |
| 4392 | |
| 4393 | ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol); |
| 4394 | } |
| 4395 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4396 | /// Merge type nullability from for a redeclaration of the same entity, |
| 4397 | /// producing the updated type of the redeclared entity. |
| 4398 | static QualType mergeTypeNullabilityForRedecl(Sema &S, SourceLocation loc, |
| 4399 | QualType type, |
| 4400 | bool usesCSKeyword, |
| 4401 | SourceLocation prevLoc, |
| 4402 | QualType prevType, |
| 4403 | bool prevUsesCSKeyword) { |
| 4404 | // Determine the nullability of both types. |
| 4405 | auto nullability = type->getNullability(S.Context); |
| 4406 | auto prevNullability = prevType->getNullability(S.Context); |
| 4407 | |
| 4408 | // Easy case: both have nullability. |
| 4409 | if (nullability.hasValue() == prevNullability.hasValue()) { |
| 4410 | // Neither has nullability; continue. |
| 4411 | if (!nullability) |
| 4412 | return type; |
| 4413 | |
| 4414 | // The nullabilities are equivalent; do nothing. |
| 4415 | if (*nullability == *prevNullability) |
| 4416 | return type; |
| 4417 | |
| 4418 | // Complain about mismatched nullability. |
| 4419 | S.Diag(loc, diag::err_nullability_conflicting) |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 4420 | << DiagNullabilityKind(*nullability, usesCSKeyword) |
| 4421 | << DiagNullabilityKind(*prevNullability, prevUsesCSKeyword); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4422 | return type; |
| 4423 | } |
| 4424 | |
| 4425 | // If it's the redeclaration that has nullability, don't change anything. |
| 4426 | if (nullability) |
| 4427 | return type; |
| 4428 | |
| 4429 | // Otherwise, provide the result with the same nullability. |
| 4430 | return S.Context.getAttributedType( |
| 4431 | AttributedType::getNullabilityAttrKind(*prevNullability), |
| 4432 | type, type); |
| 4433 | } |
| 4434 | |
NAKAMURA Takumi | 2df5c3c | 2015-06-20 03:52:52 +0000 | [diff] [blame] | 4435 | /// Merge information from the declaration of a method in the \@interface |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4436 | /// (or a category/extension) into the corresponding method in the |
| 4437 | /// @implementation (for a class or category). |
| 4438 | static void mergeInterfaceMethodToImpl(Sema &S, |
| 4439 | ObjCMethodDecl *method, |
| 4440 | ObjCMethodDecl *prevMethod) { |
| 4441 | // Merge the objc_requires_super attribute. |
| 4442 | if (prevMethod->hasAttr<ObjCRequiresSuperAttr>() && |
| 4443 | !method->hasAttr<ObjCRequiresSuperAttr>()) { |
| 4444 | // merge the attribute into implementation. |
| 4445 | method->addAttr( |
| 4446 | ObjCRequiresSuperAttr::CreateImplicit(S.Context, |
| 4447 | method->getLocation())); |
| 4448 | } |
| 4449 | |
| 4450 | // Merge nullability of the result type. |
| 4451 | QualType newReturnType |
| 4452 | = mergeTypeNullabilityForRedecl( |
| 4453 | S, method->getReturnTypeSourceRange().getBegin(), |
| 4454 | method->getReturnType(), |
| 4455 | method->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability, |
| 4456 | prevMethod->getReturnTypeSourceRange().getBegin(), |
| 4457 | prevMethod->getReturnType(), |
| 4458 | prevMethod->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability); |
| 4459 | method->setReturnType(newReturnType); |
| 4460 | |
| 4461 | // Handle each of the parameters. |
| 4462 | unsigned numParams = method->param_size(); |
| 4463 | unsigned numPrevParams = prevMethod->param_size(); |
| 4464 | for (unsigned i = 0, n = std::min(numParams, numPrevParams); i != n; ++i) { |
| 4465 | ParmVarDecl *param = method->param_begin()[i]; |
| 4466 | ParmVarDecl *prevParam = prevMethod->param_begin()[i]; |
| 4467 | |
| 4468 | // Merge nullability. |
| 4469 | QualType newParamType |
| 4470 | = mergeTypeNullabilityForRedecl( |
| 4471 | S, param->getLocation(), param->getType(), |
| 4472 | param->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability, |
| 4473 | prevParam->getLocation(), prevParam->getType(), |
| 4474 | prevParam->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability); |
| 4475 | param->setType(newParamType); |
| 4476 | } |
| 4477 | } |
| 4478 | |
Alex Lorenz | a8a372d | 2017-04-27 10:43:48 +0000 | [diff] [blame] | 4479 | /// Verify that the method parameters/return value have types that are supported |
| 4480 | /// by the x86 target. |
| 4481 | static void checkObjCMethodX86VectorTypes(Sema &SemaRef, |
| 4482 | const ObjCMethodDecl *Method) { |
| 4483 | assert(SemaRef.getASTContext().getTargetInfo().getTriple().getArch() == |
| 4484 | llvm::Triple::x86 && |
| 4485 | "x86-specific check invoked for a different target"); |
| 4486 | SourceLocation Loc; |
| 4487 | QualType T; |
| 4488 | for (const ParmVarDecl *P : Method->parameters()) { |
| 4489 | if (P->getType()->isVectorType()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4490 | Loc = P->getBeginLoc(); |
Alex Lorenz | a8a372d | 2017-04-27 10:43:48 +0000 | [diff] [blame] | 4491 | T = P->getType(); |
| 4492 | break; |
| 4493 | } |
| 4494 | } |
| 4495 | if (Loc.isInvalid()) { |
| 4496 | if (Method->getReturnType()->isVectorType()) { |
| 4497 | Loc = Method->getReturnTypeSourceRange().getBegin(); |
| 4498 | T = Method->getReturnType(); |
| 4499 | } else |
| 4500 | return; |
| 4501 | } |
| 4502 | |
| 4503 | // Vector parameters/return values are not supported by objc_msgSend on x86 in |
| 4504 | // iOS < 9 and macOS < 10.11. |
| 4505 | const auto &Triple = SemaRef.getASTContext().getTargetInfo().getTriple(); |
| 4506 | VersionTuple AcceptedInVersion; |
| 4507 | if (Triple.getOS() == llvm::Triple::IOS) |
| 4508 | AcceptedInVersion = VersionTuple(/*Major=*/9); |
| 4509 | else if (Triple.isMacOSX()) |
| 4510 | AcceptedInVersion = VersionTuple(/*Major=*/10, /*Minor=*/11); |
| 4511 | else |
| 4512 | return; |
Alex Lorenz | a8a372d | 2017-04-27 10:43:48 +0000 | [diff] [blame] | 4513 | if (SemaRef.getASTContext().getTargetInfo().getPlatformMinVersion() >= |
Alex Lorenz | 9282483 | 2017-05-05 16:15:17 +0000 | [diff] [blame] | 4514 | AcceptedInVersion) |
Alex Lorenz | a8a372d | 2017-04-27 10:43:48 +0000 | [diff] [blame] | 4515 | return; |
| 4516 | SemaRef.Diag(Loc, diag::err_objc_method_unsupported_param_ret_type) |
| 4517 | << T << (Method->getReturnType()->isVectorType() ? /*return value*/ 1 |
| 4518 | : /*parameter*/ 0) |
| 4519 | << (Triple.isMacOSX() ? "macOS 10.11" : "iOS 9"); |
| 4520 | } |
| 4521 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4522 | Decl *Sema::ActOnMethodDeclaration( |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4523 | Scope *S, SourceLocation MethodLoc, SourceLocation EndLoc, |
| 4524 | tok::TokenKind MethodType, ObjCDeclSpec &ReturnQT, ParsedType ReturnType, |
| 4525 | ArrayRef<SourceLocation> SelectorLocs, Selector Sel, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4526 | // optional arguments. The number of types/arguments is obtained |
| 4527 | // from the Sel.getNumArgs(). |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4528 | ObjCArgInfo *ArgInfo, DeclaratorChunk::ParamInfo *CParamInfo, |
| 4529 | unsigned CNumArgs, // c-style args |
| 4530 | const ParsedAttributesView &AttrList, tok::ObjCKeywordKind MethodDeclKind, |
Fariborz Jahanian | c677f69 | 2011-03-12 18:54:30 +0000 | [diff] [blame] | 4531 | bool isVariadic, bool MethodDefinition) { |
Steve Naroff | 83777fe | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 4532 | // Make sure we can establish a context for the method. |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4533 | if (!CurContext->isObjCContainer()) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 4534 | Diag(MethodLoc, diag::err_missing_method_context); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4535 | return nullptr; |
Steve Naroff | 83777fe | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 4536 | } |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 4537 | Decl *ClassDecl = cast<ObjCContainerDecl>(CurContext); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4538 | QualType resultDeclType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4539 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4540 | bool HasRelatedResultType = false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4541 | TypeSourceInfo *ReturnTInfo = nullptr; |
Steve Naroff | 3260641 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 4542 | if (ReturnType) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4543 | resultDeclType = GetTypeFromParser(ReturnType, &ReturnTInfo); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4544 | |
Eli Friedman | 31a5bcc | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 4545 | if (CheckFunctionReturnType(resultDeclType, MethodLoc)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4546 | return nullptr; |
Eli Friedman | 31a5bcc | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 4547 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4548 | QualType bareResultType = resultDeclType; |
| 4549 | (void)AttributedType::stripOuterNullability(bareResultType); |
| 4550 | HasRelatedResultType = (bareResultType == Context.getObjCInstanceType()); |
Fariborz Jahanian | b5a52ca | 2011-07-21 17:00:47 +0000 | [diff] [blame] | 4551 | } else { // get the type for "id". |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4552 | resultDeclType = Context.getObjCIdType(); |
Fariborz Jahanian | b21138f | 2011-07-21 17:38:14 +0000 | [diff] [blame] | 4553 | Diag(MethodLoc, diag::warn_missing_method_return_type) |
Argyrios Kyrtzidis | dfd6570 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 4554 | << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)"); |
Fariborz Jahanian | b5a52ca | 2011-07-21 17:00:47 +0000 | [diff] [blame] | 4555 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4556 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4557 | ObjCMethodDecl *ObjCMethod = ObjCMethodDecl::Create( |
| 4558 | Context, MethodLoc, EndLoc, Sel, resultDeclType, ReturnTInfo, CurContext, |
| 4559 | MethodType == tok::minus, isVariadic, |
| 4560 | /*isPropertyAccessor=*/false, |
| 4561 | /*isImplicitlyDeclared=*/false, /*isDefined=*/false, |
| 4562 | MethodDeclKind == tok::objc_optional ? ObjCMethodDecl::Optional |
| 4563 | : ObjCMethodDecl::Required, |
| 4564 | HasRelatedResultType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4565 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4566 | SmallVector<ParmVarDecl*, 16> Params; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4567 | |
Chris Lattner | 23b0faf | 2009-04-11 19:42:43 +0000 | [diff] [blame] | 4568 | for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) { |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 4569 | QualType ArgType; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4570 | TypeSourceInfo *DI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4571 | |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 4572 | if (!ArgInfo[i].Type) { |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 4573 | ArgType = Context.getObjCIdType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4574 | DI = nullptr; |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 4575 | } else { |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 4576 | ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI); |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 4577 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4578 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4579 | LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 4580 | LookupOrdinaryName, forRedeclarationInCurContext()); |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4581 | LookupName(R, S); |
| 4582 | if (R.isSingleResult()) { |
| 4583 | NamedDecl *PrevDecl = R.getFoundDecl(); |
| 4584 | if (S->isDeclScope(PrevDecl)) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4585 | Diag(ArgInfo[i].NameLoc, |
| 4586 | (MethodDefinition ? diag::warn_method_param_redefinition |
| 4587 | : diag::warn_method_param_declaration)) |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4588 | << ArgInfo[i].Name; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4589 | Diag(PrevDecl->getLocation(), |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4590 | diag::note_previous_declaration); |
| 4591 | } |
| 4592 | } |
| 4593 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4594 | SourceLocation StartLoc = DI |
| 4595 | ? DI->getTypeLoc().getBeginLoc() |
| 4596 | : ArgInfo[i].NameLoc; |
| 4597 | |
John McCall | d44f4d7 | 2011-04-23 02:46:06 +0000 | [diff] [blame] | 4598 | ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc, |
| 4599 | ArgInfo[i].NameLoc, ArgInfo[i].Name, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4600 | ArgType, DI, SC_None); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4601 | |
John McCall | 8249083 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 4602 | Param->setObjCMethodScopeInfo(i); |
| 4603 | |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 4604 | Param->setObjCDeclQualifier( |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 4605 | CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4606 | |
Chris Lattner | 9713a1c | 2009-04-11 19:34:56 +0000 | [diff] [blame] | 4607 | // Apply the attributes to the parameter. |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4608 | ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 4609 | AddPragmaAttributes(TUScope, Param); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4610 | |
Fariborz Jahanian | 52d02f6 | 2012-01-14 18:44:35 +0000 | [diff] [blame] | 4611 | if (Param->hasAttr<BlocksAttr>()) { |
| 4612 | Diag(Param->getLocation(), diag::err_block_on_nonlocal); |
| 4613 | Param->setInvalidDecl(); |
| 4614 | } |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4615 | S->AddDecl(Param); |
| 4616 | IdResolver.AddDecl(Param); |
| 4617 | |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 4618 | Params.push_back(Param); |
| 4619 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4620 | |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 4621 | for (unsigned i = 0, e = CNumArgs; i != e; ++i) { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4622 | ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param); |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 4623 | QualType ArgType = Param->getType(); |
| 4624 | if (ArgType.isNull()) |
| 4625 | ArgType = Context.getObjCIdType(); |
| 4626 | else |
| 4627 | // 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] | 4628 | ArgType = Context.getAdjustedParameterType(ArgType); |
Eli Friedman | 31a5bcc | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 4629 | |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 4630 | Param->setDeclContext(ObjCMethod); |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 4631 | Params.push_back(Param); |
| 4632 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4633 | |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4634 | ObjCMethod->setMethodParams(Context, Params, SelectorLocs); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4635 | ObjCMethod->setObjCDeclQualifier( |
| 4636 | CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 4637 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4638 | ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 4639 | AddPragmaAttributes(TUScope, ObjCMethod); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4640 | |
Douglas Gregor | 87e9275 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 4641 | // Add the method now. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4642 | const ObjCMethodDecl *PrevMethod = nullptr; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4643 | if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4644 | if (MethodType == tok::minus) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4645 | PrevMethod = ImpDecl->getInstanceMethod(Sel); |
| 4646 | ImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4647 | } else { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4648 | PrevMethod = ImpDecl->getClassMethod(Sel); |
| 4649 | ImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4650 | } |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4651 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4652 | // Merge information from the @interface declaration into the |
| 4653 | // @implementation. |
| 4654 | if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface()) { |
| 4655 | if (auto *IMD = IDecl->lookupMethod(ObjCMethod->getSelector(), |
| 4656 | ObjCMethod->isInstanceMethod())) { |
| 4657 | mergeInterfaceMethodToImpl(*this, ObjCMethod, IMD); |
| 4658 | |
| 4659 | // Warn about defining -dealloc in a category. |
| 4660 | if (isa<ObjCCategoryImplDecl>(ImpDecl) && IMD->isOverriding() && |
| 4661 | ObjCMethod->getSelector().getMethodFamily() == OMF_dealloc) { |
| 4662 | Diag(ObjCMethod->getLocation(), diag::warn_dealloc_in_category) |
| 4663 | << ObjCMethod->getDeclName(); |
| 4664 | } |
| 4665 | } |
Akira Hatanaka | a6b5e00 | 2018-07-28 04:06:13 +0000 | [diff] [blame] | 4666 | |
| 4667 | // Warn if a method declared in a protocol to which a category or |
| 4668 | // extension conforms is non-escaping and the implementation's method is |
| 4669 | // escaping. |
| 4670 | for (auto *C : IDecl->visible_categories()) |
| 4671 | for (auto &P : C->protocols()) |
| 4672 | if (auto *IMD = P->lookupMethod(ObjCMethod->getSelector(), |
| 4673 | ObjCMethod->isInstanceMethod())) { |
| 4674 | assert(ObjCMethod->parameters().size() == |
| 4675 | IMD->parameters().size() && |
| 4676 | "Methods have different number of parameters"); |
| 4677 | auto OI = IMD->param_begin(), OE = IMD->param_end(); |
| 4678 | auto NI = ObjCMethod->param_begin(); |
| 4679 | for (; OI != OE; ++OI, ++NI) |
| 4680 | diagnoseNoescape(*NI, *OI, C, P, *this); |
| 4681 | } |
Fariborz Jahanian | 7e350d2 | 2013-12-17 22:44:28 +0000 | [diff] [blame] | 4682 | } |
Douglas Gregor | 87e9275 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 4683 | } else { |
| 4684 | cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4685 | } |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4686 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4687 | if (PrevMethod) { |
| 4688 | // You can never have two method definitions with the same name. |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 4689 | Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4690 | << ObjCMethod->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 4691 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Fariborz Jahanian | 096f7c1 | 2013-05-13 17:27:00 +0000 | [diff] [blame] | 4692 | ObjCMethod->setInvalidDecl(); |
| 4693 | return ObjCMethod; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4694 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4695 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4696 | // If this Objective-C method does not have a related result type, but we |
| 4697 | // are allowed to infer related result types, try to do so based on the |
| 4698 | // method family. |
| 4699 | ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl); |
| 4700 | if (!CurrentClass) { |
| 4701 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) |
| 4702 | CurrentClass = Cat->getClassInterface(); |
| 4703 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl)) |
| 4704 | CurrentClass = Impl->getClassInterface(); |
| 4705 | else if (ObjCCategoryImplDecl *CatImpl |
| 4706 | = dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) |
| 4707 | CurrentClass = CatImpl->getClassInterface(); |
| 4708 | } |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4709 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4710 | ResultTypeCompatibilityKind RTC |
| 4711 | = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4712 | |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4713 | CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4714 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4715 | bool ARCError = false; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4716 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | e48f389 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 4717 | ARCError = CheckARCMethodDecl(ObjCMethod); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4718 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4719 | // Infer the related result type when possible. |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4720 | if (!ARCError && RTC == Sema::RTC_Compatible && |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4721 | !ObjCMethod->hasRelatedResultType() && |
| 4722 | LangOpts.ObjCInferRelatedResultType) { |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4723 | bool InferRelatedResultType = false; |
| 4724 | switch (ObjCMethod->getMethodFamily()) { |
| 4725 | case OMF_None: |
| 4726 | case OMF_copy: |
| 4727 | case OMF_dealloc: |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 4728 | case OMF_finalize: |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4729 | case OMF_mutableCopy: |
| 4730 | case OMF_release: |
| 4731 | case OMF_retainCount: |
Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 4732 | case OMF_initialize: |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 4733 | case OMF_performSelector: |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4734 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4735 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4736 | case OMF_alloc: |
| 4737 | case OMF_new: |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 4738 | InferRelatedResultType = ObjCMethod->isClassMethod(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4739 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4740 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4741 | case OMF_init: |
| 4742 | case OMF_autorelease: |
| 4743 | case OMF_retain: |
| 4744 | case OMF_self: |
| 4745 | InferRelatedResultType = ObjCMethod->isInstanceMethod(); |
| 4746 | break; |
| 4747 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4748 | |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 4749 | if (InferRelatedResultType && |
| 4750 | !ObjCMethod->getReturnType()->isObjCIndependentClassType()) |
Erich Keane | 9b18eca | 2018-08-01 21:31:08 +0000 | [diff] [blame] | 4751 | ObjCMethod->setRelatedResultType(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4752 | } |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 4753 | |
Alex Lorenz | a8a372d | 2017-04-27 10:43:48 +0000 | [diff] [blame] | 4754 | if (MethodDefinition && |
| 4755 | Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86) |
| 4756 | checkObjCMethodX86VectorTypes(*this, ObjCMethod); |
| 4757 | |
Steven Wu | 3bb4aa5 | 2018-04-16 23:34:18 +0000 | [diff] [blame] | 4758 | // + load method cannot have availability attributes. It get called on |
| 4759 | // startup, so it has to have the availability of the deployment target. |
| 4760 | if (const auto *attr = ObjCMethod->getAttr<AvailabilityAttr>()) { |
| 4761 | if (ObjCMethod->isClassMethod() && |
| 4762 | ObjCMethod->getSelector().getAsString() == "load") { |
| 4763 | Diag(attr->getLocation(), diag::warn_availability_on_static_initializer) |
| 4764 | << 0; |
| 4765 | ObjCMethod->dropAttr<AvailabilityAttr>(); |
| 4766 | } |
| 4767 | } |
| 4768 | |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 4769 | ActOnDocumentableDecl(ObjCMethod); |
| 4770 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4771 | return ObjCMethod; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4774 | bool Sema::CheckObjCDeclScope(Decl *D) { |
Fariborz Jahanian | f36734d | 2011-08-22 18:34:22 +0000 | [diff] [blame] | 4775 | // Following is also an error. But it is caused by a missing @end |
| 4776 | // and diagnostic is issued elsewhere. |
Argyrios Kyrtzidis | 822c433 | 2012-03-23 23:24:23 +0000 | [diff] [blame] | 4777 | if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4778 | return false; |
Argyrios Kyrtzidis | 822c433 | 2012-03-23 23:24:23 +0000 | [diff] [blame] | 4779 | |
| 4780 | // If we switched context to translation unit while we are still lexically in |
| 4781 | // an objc container, it means the parser missed emitting an error. |
| 4782 | if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext())) |
| 4783 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4784 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 4785 | Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); |
| 4786 | D->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4787 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 4788 | return true; |
| 4789 | } |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4790 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4791 | /// Called whenever \@defs(ClassName) is encountered in the source. Inserts the |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4792 | /// instance variables of ClassName into Decls. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4793 | void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart, |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4794 | IdentifierInfo *ClassName, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4795 | SmallVectorImpl<Decl*> &Decls) { |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4796 | // Check that ClassName is a valid class |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4797 | ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4798 | if (!Class) { |
| 4799 | Diag(DeclStart, diag::err_undef_interface) << ClassName; |
| 4800 | return; |
| 4801 | } |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 4802 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | ece1b2b | 2009-04-21 20:28:41 +0000 | [diff] [blame] | 4803 | Diag(DeclStart, diag::err_atdef_nonfragile_interface); |
| 4804 | return; |
| 4805 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4806 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4807 | // Collect the instance variables |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4808 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4809 | Context.DeepCollectObjCIvars(Class, true, Ivars); |
Fariborz Jahanian | 7dae114 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 4810 | // For each ivar, create a fresh ObjCAtDefsFieldDecl. |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4811 | for (unsigned i = 0; i < Ivars.size(); i++) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 4812 | const FieldDecl* ID = Ivars[i]; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4813 | RecordDecl *Record = dyn_cast<RecordDecl>(TagD); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4814 | Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, |
| 4815 | /*FIXME: StartL=*/ID->getLocation(), |
| 4816 | ID->getLocation(), |
Fariborz Jahanian | 7dae114 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 4817 | ID->getIdentifier(), ID->getType(), |
| 4818 | ID->getBitWidth()); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4819 | Decls.push_back(FD); |
Fariborz Jahanian | 7dae114 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 4820 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4821 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4822 | // Introduce all of these fields into the appropriate scope. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4823 | for (SmallVectorImpl<Decl*>::iterator D = Decls.begin(); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4824 | D != Decls.end(); ++D) { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4825 | FieldDecl *FD = cast<FieldDecl>(*D); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4826 | if (getLangOpts().CPlusPlus) |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 4827 | PushOnScopeChains(FD, S); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4828 | else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD)) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4829 | Record->addDecl(FD); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4830 | } |
| 4831 | } |
| 4832 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4833 | /// Build a type-check a new Objective-C exception variable declaration. |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4834 | VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T, |
| 4835 | SourceLocation StartLoc, |
| 4836 | SourceLocation IdLoc, |
| 4837 | IdentifierInfo *Id, |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4838 | bool Invalid) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4839 | // 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] | 4840 | // duration shall not be qualified by an address-space qualifier." |
| 4841 | // Since all parameters have automatic store duration, they can not have |
| 4842 | // an address space. |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 4843 | if (T.getAddressSpace() != LangAS::Default) { |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4844 | Diag(IdLoc, diag::err_arg_with_address_space); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4845 | Invalid = true; |
| 4846 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4847 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4848 | // An @catch parameter must be an unqualified object pointer type; |
| 4849 | // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"? |
| 4850 | if (Invalid) { |
| 4851 | // Don't do any further checking. |
Douglas Gregor | f4e837f | 2010-04-26 17:57:08 +0000 | [diff] [blame] | 4852 | } else if (T->isDependentType()) { |
| 4853 | // Okay: we don't know what this type will instantiate to. |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4854 | } else if (T->isObjCQualifiedIdType()) { |
| 4855 | Invalid = true; |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4856 | Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm); |
Saleem Abdulrasool | 278e1c4 | 2018-05-20 19:26:44 +0000 | [diff] [blame] | 4857 | } else if (T->isObjCIdType()) { |
| 4858 | // Okay: we don't know what this type will instantiate to. |
| 4859 | } else if (!T->isObjCObjectPointerType()) { |
| 4860 | Invalid = true; |
| 4861 | Diag(IdLoc, diag::err_catch_param_not_objc_type); |
| 4862 | } else if (!T->getAs<ObjCObjectPointerType>()->getInterfaceType()) { |
| 4863 | Invalid = true; |
| 4864 | Diag(IdLoc, diag::err_catch_param_not_objc_type); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4865 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4866 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4867 | VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4868 | T, TInfo, SC_None); |
Douglas Gregor | 3f324d56 | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 4869 | New->setExceptionVariable(true); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4870 | |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 4871 | // In ARC, infer 'retaining' for variables of retainable type. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4872 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New)) |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 4873 | Invalid = true; |
| 4874 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4875 | if (Invalid) |
| 4876 | New->setInvalidDecl(); |
| 4877 | return New; |
| 4878 | } |
| 4879 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4880 | Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) { |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4881 | const DeclSpec &DS = D.getDeclSpec(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4882 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4883 | // We allow the "register" storage class on exception variables because |
| 4884 | // GCC did, but we drop it completely. Any other storage class is an error. |
| 4885 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
| 4886 | Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm) |
| 4887 | << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc())); |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4888 | } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) { |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4889 | Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm) |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4890 | << DeclSpec::getSpecifierName(SCS); |
| 4891 | } |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4892 | if (DS.isInlineSpecified()) |
| 4893 | Diag(DS.getInlineSpecLoc(), diag::err_inline_non_function) |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4894 | << getLangOpts().CPlusPlus17; |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4895 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
| 4896 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 4897 | diag::err_invalid_thread) |
| 4898 | << DeclSpec::getSpecifierName(TSCS); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4899 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 4900 | |
Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4901 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4902 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4903 | // Check that there are no default arguments inside the type of this |
| 4904 | // exception object (C++ only). |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4905 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4906 | CheckExtraCXXDefaultArguments(D); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4907 | |
Argyrios Kyrtzidis | ef7022f | 2011-06-28 03:01:15 +0000 | [diff] [blame] | 4908 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 4909 | QualType ExceptionType = TInfo->getType(); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4910 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4911 | VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, |
| 4912 | D.getSourceRange().getBegin(), |
| 4913 | D.getIdentifierLoc(), |
| 4914 | D.getIdentifier(), |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4915 | D.isInvalidType()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4916 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4917 | // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1). |
| 4918 | if (D.getCXXScopeSpec().isSet()) { |
| 4919 | Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm) |
| 4920 | << D.getCXXScopeSpec().getRange(); |
| 4921 | New->setInvalidDecl(); |
| 4922 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4923 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4924 | // Add the parameter declaration into this scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4925 | S->AddDecl(New); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4926 | if (D.getIdentifier()) |
| 4927 | IdResolver.AddDecl(New); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4928 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4929 | ProcessDeclAttributes(S, New, D); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4930 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4931 | if (New->hasAttr<BlocksAttr>()) |
| 4932 | Diag(New->getLocation(), diag::err_block_on_nonlocal); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4933 | return New; |
Douglas Gregor | e11ee11 | 2010-04-23 23:01:43 +0000 | [diff] [blame] | 4934 | } |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 4935 | |
| 4936 | /// CollectIvarsToConstructOrDestruct - Collect those ivars which require |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 4937 | /// initialization. |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4938 | void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4939 | SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4940 | for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4941 | Iv= Iv->getNextIvar()) { |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 4942 | QualType QT = Context.getBaseElementType(Iv->getType()); |
Douglas Gregor | 527786e | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 4943 | if (QT->isRecordType()) |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4944 | Ivars.push_back(Iv); |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 4945 | } |
| 4946 | } |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 4947 | |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 4948 | void Sema::DiagnoseUseOfUnimplementedSelectors() { |
Douglas Gregor | 72e357f | 2011-07-28 14:54:22 +0000 | [diff] [blame] | 4949 | // Load referenced selectors from the external source. |
| 4950 | if (ExternalSource) { |
| 4951 | SmallVector<std::pair<Selector, SourceLocation>, 4> Sels; |
| 4952 | ExternalSource->ReadReferencedSelectors(Sels); |
| 4953 | for (unsigned I = 0, N = Sels.size(); I != N; ++I) |
| 4954 | ReferencedSelectors[Sels[I].first] = Sels[I].second; |
| 4955 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4956 | |
Fariborz Jahanian | c9b7c20 | 2011-02-04 23:19:27 +0000 | [diff] [blame] | 4957 | // Warning will be issued only when selector table is |
| 4958 | // generated (which means there is at lease one implementation |
| 4959 | // in the TU). This is to match gcc's behavior. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4960 | if (ReferencedSelectors.empty() || |
Fariborz Jahanian | c9b7c20 | 2011-02-04 23:19:27 +0000 | [diff] [blame] | 4961 | !Context.AnyObjCImplementation()) |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 4962 | return; |
Chandler Carruth | 12c8f65 | 2015-03-27 00:55:05 +0000 | [diff] [blame] | 4963 | for (auto &SelectorAndLocation : ReferencedSelectors) { |
| 4964 | Selector Sel = SelectorAndLocation.first; |
| 4965 | SourceLocation Loc = SelectorAndLocation.second; |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 4966 | if (!LookupImplementedMethodInGlobalPool(Sel)) |
Chandler Carruth | 12c8f65 | 2015-03-27 00:55:05 +0000 | [diff] [blame] | 4967 | Diag(Loc, diag::warn_unimplemented_selector) << Sel; |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 4968 | } |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 4969 | } |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 4970 | |
| 4971 | ObjCIvarDecl * |
| 4972 | Sema::GetIvarBackingPropertyAccessor(const ObjCMethodDecl *Method, |
| 4973 | const ObjCPropertyDecl *&PDecl) const { |
Fariborz Jahanian | 1cc7ae1 | 2014-01-02 17:24:32 +0000 | [diff] [blame] | 4974 | if (Method->isClassMethod()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4975 | return nullptr; |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 4976 | const ObjCInterfaceDecl *IDecl = Method->getClassInterface(); |
| 4977 | if (!IDecl) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4978 | return nullptr; |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4979 | Method = IDecl->lookupMethod(Method->getSelector(), /*isInstance=*/true, |
| 4980 | /*shallowCategoryLookup=*/false, |
| 4981 | /*followSuper=*/false); |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 4982 | if (!Method || !Method->isPropertyAccessor()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4983 | return nullptr; |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4984 | if ((PDecl = Method->findPropertyDecl())) |
Fariborz Jahanian | 122d94f | 2014-01-27 22:27:43 +0000 | [diff] [blame] | 4985 | if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) { |
| 4986 | // property backing ivar must belong to property's class |
| 4987 | // or be a private ivar in class's implementation. |
| 4988 | // FIXME. fix the const-ness issue. |
| 4989 | IV = const_cast<ObjCInterfaceDecl *>(IDecl)->lookupInstanceVariable( |
| 4990 | IV->getIdentifier()); |
| 4991 | return IV; |
| 4992 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4993 | return nullptr; |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 4994 | } |
| 4995 | |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4996 | namespace { |
| 4997 | /// Used by Sema::DiagnoseUnusedBackingIvarInAccessor to check if a property |
| 4998 | /// accessor references the backing ivar. |
Argyrios Kyrtzidis | 98045c1 | 2014-01-03 19:53:09 +0000 | [diff] [blame] | 4999 | class UnusedBackingIvarChecker : |
Richard Smith | 5066845 | 2015-11-24 03:55:01 +0000 | [diff] [blame] | 5000 | public RecursiveASTVisitor<UnusedBackingIvarChecker> { |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 5001 | public: |
| 5002 | Sema &S; |
| 5003 | const ObjCMethodDecl *Method; |
| 5004 | const ObjCIvarDecl *IvarD; |
| 5005 | bool AccessedIvar; |
| 5006 | bool InvokedSelfMethod; |
| 5007 | |
| 5008 | UnusedBackingIvarChecker(Sema &S, const ObjCMethodDecl *Method, |
| 5009 | const ObjCIvarDecl *IvarD) |
| 5010 | : S(S), Method(Method), IvarD(IvarD), |
| 5011 | AccessedIvar(false), InvokedSelfMethod(false) { |
| 5012 | assert(IvarD); |
| 5013 | } |
| 5014 | |
| 5015 | bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { |
| 5016 | if (E->getDecl() == IvarD) { |
| 5017 | AccessedIvar = true; |
| 5018 | return false; |
| 5019 | } |
| 5020 | return true; |
| 5021 | } |
| 5022 | |
| 5023 | bool VisitObjCMessageExpr(ObjCMessageExpr *E) { |
| 5024 | if (E->getReceiverKind() == ObjCMessageExpr::Instance && |
| 5025 | S.isSelfExpr(E->getInstanceReceiver(), Method)) { |
| 5026 | InvokedSelfMethod = true; |
| 5027 | } |
| 5028 | return true; |
| 5029 | } |
| 5030 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 5031 | } // end anonymous namespace |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 5032 | |
| 5033 | void Sema::DiagnoseUnusedBackingIvarInAccessor(Scope *S, |
| 5034 | const ObjCImplementationDecl *ImplD) { |
| 5035 | if (S->hasUnrecoverableErrorOccurred()) |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 5036 | return; |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 5037 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 5038 | for (const auto *CurMethod : ImplD->instance_methods()) { |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 5039 | unsigned DIAG = diag::warn_unused_property_backing_ivar; |
| 5040 | SourceLocation Loc = CurMethod->getLocation(); |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 5041 | if (Diags.isIgnored(DIAG, Loc)) |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 5042 | continue; |
| 5043 | |
| 5044 | const ObjCPropertyDecl *PDecl; |
| 5045 | const ObjCIvarDecl *IV = GetIvarBackingPropertyAccessor(CurMethod, PDecl); |
| 5046 | if (!IV) |
| 5047 | continue; |
| 5048 | |
| 5049 | UnusedBackingIvarChecker Checker(*this, CurMethod, IV); |
| 5050 | Checker.TraverseStmt(CurMethod->getBody()); |
| 5051 | if (Checker.AccessedIvar) |
| 5052 | continue; |
| 5053 | |
Fariborz Jahanian | 5b3105d | 2014-01-02 22:42:09 +0000 | [diff] [blame] | 5054 | // 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] | 5055 | // implementation makes a self call. This is to prevent false positive in |
| 5056 | // cases where the ivar is accessed by another method that the accessor |
| 5057 | // delegates to. |
| 5058 | if (!IV->isReferenced() || !Checker.InvokedSelfMethod) { |
Argyrios Kyrtzidis | d8a3532 | 2014-01-03 19:39:23 +0000 | [diff] [blame] | 5059 | Diag(Loc, DIAG) << IV; |
Fariborz Jahanian | 5b3105d | 2014-01-02 22:42:09 +0000 | [diff] [blame] | 5060 | Diag(PDecl->getLocation(), diag::note_property_declare); |
| 5061 | } |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 5062 | } |
| 5063 | } |