Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1 | //===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for Objective C declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 14 | #include "TypeLocBuilder.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/ASTMutationListener.h" |
| 18 | #include "clang/AST/DeclObjC.h" |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprObjC.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 21 | #include "clang/AST/RecursiveASTVisitor.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Sema/DeclSpec.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Sema/Lookup.h" |
| 25 | #include "clang/Sema/Scope.h" |
| 26 | #include "clang/Sema/ScopeInfo.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 27 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseMap.h" |
John McCall | a1e130b | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseSet.h" |
| 30 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 33 | /// Check whether the given method, which must be in the 'init' |
| 34 | /// family, is a valid member of that family. |
| 35 | /// |
| 36 | /// \param receiverTypeIfCall - if null, check this as if declaring it; |
| 37 | /// if non-null, check this as if making a call to it with the given |
| 38 | /// receiver type |
| 39 | /// |
| 40 | /// \return true to indicate that there was an error and appropriate |
| 41 | /// actions were taken |
| 42 | bool Sema::checkInitMethod(ObjCMethodDecl *method, |
| 43 | QualType receiverTypeIfCall) { |
| 44 | if (method->isInvalidDecl()) return true; |
| 45 | |
| 46 | // This castAs is safe: methods that don't return an object |
| 47 | // pointer won't be inferred as inits and will reject an explicit |
| 48 | // objc_method_family(init). |
| 49 | |
| 50 | // We ignore protocols here. Should we? What about Class? |
| 51 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 52 | const ObjCObjectType *result = |
| 53 | method->getReturnType()->castAs<ObjCObjectPointerType>()->getObjectType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 54 | |
| 55 | if (result->isObjCId()) { |
| 56 | return false; |
| 57 | } else if (result->isObjCClass()) { |
| 58 | // fall through: always an error |
| 59 | } else { |
| 60 | ObjCInterfaceDecl *resultClass = result->getInterface(); |
| 61 | assert(resultClass && "unexpected object type!"); |
| 62 | |
| 63 | // It's okay for the result type to still be a forward declaration |
| 64 | // if we're checking an interface declaration. |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 65 | if (!resultClass->hasDefinition()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 66 | if (receiverTypeIfCall.isNull() && |
| 67 | !isa<ObjCImplementationDecl>(method->getDeclContext())) |
| 68 | return false; |
| 69 | |
| 70 | // Otherwise, we try to compare class types. |
| 71 | } else { |
| 72 | // If this method was declared in a protocol, we can't check |
| 73 | // anything unless we have a receiver type that's an interface. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 74 | const ObjCInterfaceDecl *receiverClass = nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 75 | if (isa<ObjCProtocolDecl>(method->getDeclContext())) { |
| 76 | if (receiverTypeIfCall.isNull()) |
| 77 | return false; |
| 78 | |
| 79 | receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>() |
| 80 | ->getInterfaceDecl(); |
| 81 | |
| 82 | // This can be null for calls to e.g. id<Foo>. |
| 83 | if (!receiverClass) return false; |
| 84 | } else { |
| 85 | receiverClass = method->getClassInterface(); |
| 86 | assert(receiverClass && "method not associated with a class!"); |
| 87 | } |
| 88 | |
| 89 | // If either class is a subclass of the other, it's fine. |
| 90 | if (receiverClass->isSuperClassOf(resultClass) || |
| 91 | resultClass->isSuperClassOf(receiverClass)) |
| 92 | return false; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | SourceLocation loc = method->getLocation(); |
| 97 | |
| 98 | // If we're in a system header, and this is not a call, just make |
| 99 | // the method unusable. |
| 100 | if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 101 | method->addAttr(UnavailableAttr::CreateImplicit(Context, "", |
| 102 | UnavailableAttr::IR_ARCInitReturnsUnrelated, loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
| 106 | // Otherwise, it's an error. |
| 107 | Diag(loc, diag::err_arc_init_method_unrelated_result_type); |
| 108 | method->setInvalidDecl(); |
| 109 | return true; |
| 110 | } |
| 111 | |
Fariborz Jahanian | ac8dbf0 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 112 | void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 113 | const ObjCMethodDecl *Overridden) { |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 114 | if (Overridden->hasRelatedResultType() && |
| 115 | !NewMethod->hasRelatedResultType()) { |
| 116 | // This can only happen when the method follows a naming convention that |
| 117 | // implies a related result type, and the original (overridden) method has |
| 118 | // a suitable return type, but the new (overriding) method does not have |
| 119 | // a suitable return type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 120 | QualType ResultType = NewMethod->getReturnType(); |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 121 | SourceRange ResultTypeRange = NewMethod->getReturnTypeSourceRange(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 122 | |
| 123 | // Figure out which class this method is part of, if any. |
| 124 | ObjCInterfaceDecl *CurrentClass |
| 125 | = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext()); |
| 126 | if (!CurrentClass) { |
| 127 | DeclContext *DC = NewMethod->getDeclContext(); |
| 128 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC)) |
| 129 | CurrentClass = Cat->getClassInterface(); |
| 130 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC)) |
| 131 | CurrentClass = Impl->getClassInterface(); |
| 132 | else if (ObjCCategoryImplDecl *CatImpl |
| 133 | = dyn_cast<ObjCCategoryImplDecl>(DC)) |
| 134 | CurrentClass = CatImpl->getClassInterface(); |
| 135 | } |
| 136 | |
| 137 | if (CurrentClass) { |
| 138 | Diag(NewMethod->getLocation(), |
| 139 | diag::warn_related_result_type_compatibility_class) |
| 140 | << Context.getObjCInterfaceType(CurrentClass) |
| 141 | << ResultType |
| 142 | << ResultTypeRange; |
| 143 | } else { |
| 144 | Diag(NewMethod->getLocation(), |
| 145 | diag::warn_related_result_type_compatibility_protocol) |
| 146 | << ResultType |
| 147 | << ResultTypeRange; |
| 148 | } |
| 149 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 150 | if (ObjCMethodFamily Family = Overridden->getMethodFamily()) |
| 151 | Diag(Overridden->getLocation(), |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 152 | diag::note_related_result_type_family) |
| 153 | << /*overridden method*/ 0 |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 154 | << Family; |
| 155 | else |
| 156 | Diag(Overridden->getLocation(), |
| 157 | diag::note_related_result_type_overridden); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 158 | } |
Akira Hatanaka | 7d85b8f | 2017-09-20 05:39:18 +0000 | [diff] [blame] | 159 | |
| 160 | if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() != |
| 161 | Overridden->hasAttr<NSReturnsRetainedAttr>())) { |
| 162 | Diag(NewMethod->getLocation(), |
Alex Lorenz | 26d282f | 2018-01-03 23:52:42 +0000 | [diff] [blame^] | 163 | getLangOpts().ObjCAutoRefCount |
| 164 | ? diag::err_nsreturns_retained_attribute_mismatch |
| 165 | : diag::warn_nsreturns_retained_attribute_mismatch) |
| 166 | << 1; |
Akira Hatanaka | 7d85b8f | 2017-09-20 05:39:18 +0000 | [diff] [blame] | 167 | Diag(Overridden->getLocation(), diag::note_previous_decl) << "method"; |
| 168 | } |
| 169 | if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() != |
| 170 | Overridden->hasAttr<NSReturnsNotRetainedAttr>())) { |
| 171 | Diag(NewMethod->getLocation(), |
Alex Lorenz | 26d282f | 2018-01-03 23:52:42 +0000 | [diff] [blame^] | 172 | getLangOpts().ObjCAutoRefCount |
| 173 | ? diag::err_nsreturns_retained_attribute_mismatch |
| 174 | : diag::warn_nsreturns_retained_attribute_mismatch) |
| 175 | << 0; |
Akira Hatanaka | 7d85b8f | 2017-09-20 05:39:18 +0000 | [diff] [blame] | 176 | Diag(Overridden->getLocation(), diag::note_previous_decl) << "method"; |
| 177 | } |
| 178 | |
| 179 | ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(), |
| 180 | oe = Overridden->param_end(); |
| 181 | for (ObjCMethodDecl::param_iterator ni = NewMethod->param_begin(), |
| 182 | ne = NewMethod->param_end(); |
| 183 | ni != ne && oi != oe; ++ni, ++oi) { |
| 184 | const ParmVarDecl *oldDecl = (*oi); |
| 185 | ParmVarDecl *newDecl = (*ni); |
| 186 | if (newDecl->hasAttr<NSConsumedAttr>() != |
| 187 | oldDecl->hasAttr<NSConsumedAttr>()) { |
Alex Lorenz | 26d282f | 2018-01-03 23:52:42 +0000 | [diff] [blame^] | 188 | Diag(newDecl->getLocation(), |
| 189 | getLangOpts().ObjCAutoRefCount |
| 190 | ? diag::err_nsconsumed_attribute_mismatch |
| 191 | : diag::warn_nsconsumed_attribute_mismatch); |
Akira Hatanaka | 7d85b8f | 2017-09-20 05:39:18 +0000 | [diff] [blame] | 192 | Diag(oldDecl->getLocation(), diag::note_previous_decl) << "parameter"; |
Fariborz Jahanian | ac8dbf0 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 193 | } |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 194 | |
| 195 | // A parameter of the overriding method should be annotated with noescape |
| 196 | // if the corresponding parameter of the overridden method is annotated. |
| 197 | if (oldDecl->hasAttr<NoEscapeAttr>() && !newDecl->hasAttr<NoEscapeAttr>()) { |
| 198 | Diag(newDecl->getLocation(), |
| 199 | diag::warn_overriding_method_missing_noescape); |
| 200 | Diag(oldDecl->getLocation(), diag::note_overridden_marked_noescape); |
| 201 | } |
Fariborz Jahanian | ac8dbf0 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 202 | } |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 203 | } |
| 204 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 205 | /// \brief Check a method declaration for compatibility with the Objective-C |
| 206 | /// ARC conventions. |
John McCall | e48f389 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 207 | bool Sema::CheckARCMethodDecl(ObjCMethodDecl *method) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 208 | ObjCMethodFamily family = method->getMethodFamily(); |
| 209 | switch (family) { |
| 210 | case OMF_None: |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 211 | case OMF_finalize: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 212 | case OMF_retain: |
| 213 | case OMF_release: |
| 214 | case OMF_autorelease: |
| 215 | case OMF_retainCount: |
| 216 | case OMF_self: |
Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 217 | case OMF_initialize: |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 218 | case OMF_performSelector: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 219 | return false; |
| 220 | |
Fariborz Jahanian | b7f03c1 | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 221 | case OMF_dealloc: |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 222 | if (!Context.hasSameType(method->getReturnType(), Context.VoidTy)) { |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 223 | SourceRange ResultTypeRange = method->getReturnTypeSourceRange(); |
Fariborz Jahanian | b7f03c1 | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 224 | if (ResultTypeRange.isInvalid()) |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 225 | Diag(method->getLocation(), diag::err_dealloc_bad_result_type) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 226 | << method->getReturnType() |
| 227 | << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)"); |
Fariborz Jahanian | b7f03c1 | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 228 | else |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 229 | Diag(method->getLocation(), diag::err_dealloc_bad_result_type) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 230 | << method->getReturnType() |
| 231 | << FixItHint::CreateReplacement(ResultTypeRange, "void"); |
Fariborz Jahanian | b7f03c1 | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 232 | return true; |
| 233 | } |
| 234 | return false; |
| 235 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 236 | case OMF_init: |
| 237 | // 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] | 238 | if (checkInitMethod(method, QualType())) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 239 | return true; |
| 240 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 241 | method->addAttr(NSConsumesSelfAttr::CreateImplicit(Context)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 242 | |
| 243 | // Don't add a second copy of this attribute, but otherwise don't |
| 244 | // let it be suppressed. |
| 245 | if (method->hasAttr<NSReturnsRetainedAttr>()) |
| 246 | return false; |
| 247 | break; |
| 248 | |
| 249 | case OMF_alloc: |
| 250 | case OMF_copy: |
| 251 | case OMF_mutableCopy: |
| 252 | case OMF_new: |
| 253 | if (method->hasAttr<NSReturnsRetainedAttr>() || |
| 254 | method->hasAttr<NSReturnsNotRetainedAttr>() || |
| 255 | method->hasAttr<NSReturnsAutoreleasedAttr>()) |
| 256 | return false; |
| 257 | break; |
| 258 | } |
| 259 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 260 | method->addAttr(NSReturnsRetainedAttr::CreateImplicit(Context)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | |
Alex Lorenz | f81d97e | 2017-07-13 16:35:59 +0000 | [diff] [blame] | 264 | static void DiagnoseObjCImplementedDeprecations(Sema &S, const NamedDecl *ND, |
| 265 | SourceLocation ImplLoc) { |
| 266 | if (!ND) |
| 267 | return; |
| 268 | bool IsCategory = false; |
Alex Lorenz | e1088dc | 2017-07-13 16:37:11 +0000 | [diff] [blame] | 269 | AvailabilityResult Availability = ND->getAvailability(); |
| 270 | if (Availability != AR_Deprecated) { |
Eric Christopher | 7aba978 | 2017-07-14 01:42:57 +0000 | [diff] [blame] | 271 | if (isa<ObjCMethodDecl>(ND)) { |
Alex Lorenz | e1088dc | 2017-07-13 16:37:11 +0000 | [diff] [blame] | 272 | if (Availability != AR_Unavailable) |
| 273 | return; |
| 274 | // Warn about implementing unavailable methods. |
| 275 | S.Diag(ImplLoc, diag::warn_unavailable_def); |
| 276 | S.Diag(ND->getLocation(), diag::note_method_declared_at) |
| 277 | << ND->getDeclName(); |
| 278 | return; |
| 279 | } |
Alex Lorenz | f81d97e | 2017-07-13 16:35:59 +0000 | [diff] [blame] | 280 | if (const auto *CD = dyn_cast<ObjCCategoryDecl>(ND)) { |
| 281 | if (!CD->getClassInterface()->isDeprecated()) |
| 282 | return; |
| 283 | ND = CD->getClassInterface(); |
| 284 | IsCategory = true; |
| 285 | } else |
| 286 | return; |
Fariborz Jahanian | d724a10 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 287 | } |
Alex Lorenz | f81d97e | 2017-07-13 16:35:59 +0000 | [diff] [blame] | 288 | S.Diag(ImplLoc, diag::warn_deprecated_def) |
| 289 | << (isa<ObjCMethodDecl>(ND) |
| 290 | ? /*Method*/ 0 |
| 291 | : isa<ObjCCategoryDecl>(ND) || IsCategory ? /*Category*/ 2 |
| 292 | : /*Class*/ 1); |
| 293 | if (isa<ObjCMethodDecl>(ND)) |
| 294 | S.Diag(ND->getLocation(), diag::note_method_declared_at) |
| 295 | << ND->getDeclName(); |
| 296 | else |
| 297 | S.Diag(ND->getLocation(), diag::note_previous_decl) |
| 298 | << (isa<ObjCCategoryDecl>(ND) ? "category" : "class"); |
Fariborz Jahanian | d724a10 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Fariborz Jahanian | bd0642f | 2011-08-31 17:37:55 +0000 | [diff] [blame] | 301 | /// AddAnyMethodToGlobalPool - Add any method, instance or factory to global |
| 302 | /// pool. |
| 303 | void Sema::AddAnyMethodToGlobalPool(Decl *D) { |
| 304 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); |
| 305 | |
| 306 | // If we don't have a valid method decl, simply return. |
| 307 | if (!MDecl) |
| 308 | return; |
| 309 | if (MDecl->isInstanceMethod()) |
| 310 | AddInstanceMethodToGlobalPool(MDecl, true); |
| 311 | else |
| 312 | AddFactoryMethodToGlobalPool(MDecl, true); |
| 313 | } |
| 314 | |
Fariborz Jahanian | 1dfeace | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 315 | /// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer |
| 316 | /// has explicit ownership attribute; false otherwise. |
| 317 | static bool |
| 318 | HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) { |
| 319 | QualType T = Param->getType(); |
| 320 | |
Fariborz Jahanian | 1dfeace | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 321 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 322 | T = PT->getPointeeType(); |
| 323 | } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) { |
| 324 | T = RT->getPointeeType(); |
| 325 | } else { |
| 326 | return true; |
| 327 | } |
| 328 | |
| 329 | // If we have a lifetime qualifier, but it's local, we must have |
| 330 | // inferred it. So, it is implicit. |
| 331 | return !T.getLocalQualifiers().hasObjCLifetime(); |
| 332 | } |
| 333 | |
Fariborz Jahanian | 18d0a5d | 2012-08-08 23:41:08 +0000 | [diff] [blame] | 334 | /// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible |
| 335 | /// and user declared, in the method definition's AST. |
| 336 | void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 337 | assert((getCurMethodDecl() == nullptr) && "Methodparsing confused"); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 338 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); |
Fariborz Jahanian | 577574a | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 339 | |
Steve Naroff | 542cd5d | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 340 | // If we don't have a valid method decl, simply return. |
| 341 | if (!MDecl) |
| 342 | return; |
Steve Naroff | 1d2538c | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 343 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 344 | // 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] | 345 | PushDeclContext(FnBodyScope, MDecl); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 346 | PushFunctionScope(); |
| 347 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 348 | // Create Decl objects for each parameter, entrring them in the scope for |
| 349 | // binding to their use. |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 350 | |
| 351 | // Insert the invisible arguments, self and _cmd! |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 352 | MDecl->createImplicitParams(Context, MDecl->getClassInterface()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | |
Daniel Dunbar | 279d1cc | 2008-08-26 06:07:48 +0000 | [diff] [blame] | 354 | PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); |
| 355 | PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 356 | |
Reid Kleckner | 5a11580 | 2013-06-24 14:38:26 +0000 | [diff] [blame] | 357 | // 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] | 358 | CheckParmsForFunctionDef(MDecl->parameters(), |
Reid Kleckner | 5a11580 | 2013-06-24 14:38:26 +0000 | [diff] [blame] | 359 | /*CheckParameterNames=*/false); |
| 360 | |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 361 | // Introduce all of the other parameters into this scope. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 362 | for (auto *Param : MDecl->parameters()) { |
Fariborz Jahanian | b3e8712 | 2010-09-17 22:07:07 +0000 | [diff] [blame] | 363 | if (!Param->isInvalidDecl() && |
Fariborz Jahanian | 1dfeace | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 364 | getLangOpts().ObjCAutoRefCount && |
| 365 | !HasExplicitOwnershipAttr(*this, Param)) |
| 366 | Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) << |
| 367 | Param->getType(); |
Fariborz Jahanian | cd278ff | 2012-08-30 23:56:02 +0000 | [diff] [blame] | 368 | |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 369 | if (Param->getIdentifier()) |
| 370 | PushOnScopeChains(Param, FnBodyScope); |
Fariborz Jahanian | b3e8712 | 2010-09-17 22:07:07 +0000 | [diff] [blame] | 371 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 372 | |
| 373 | // In ARC, disallow definition of retain/release/autorelease/retainCount |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 374 | if (getLangOpts().ObjCAutoRefCount) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 375 | switch (MDecl->getMethodFamily()) { |
| 376 | case OMF_retain: |
| 377 | case OMF_retainCount: |
| 378 | case OMF_release: |
| 379 | case OMF_autorelease: |
| 380 | Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def) |
Fariborz Jahanian | 39d1c42 | 2013-05-16 19:08:44 +0000 | [diff] [blame] | 381 | << 0 << MDecl->getSelector(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 382 | break; |
| 383 | |
| 384 | case OMF_None: |
| 385 | case OMF_dealloc: |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 386 | case OMF_finalize: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 387 | case OMF_alloc: |
| 388 | case OMF_init: |
| 389 | case OMF_mutableCopy: |
| 390 | case OMF_copy: |
| 391 | case OMF_new: |
| 392 | case OMF_self: |
Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 393 | case OMF_initialize: |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 394 | case OMF_performSelector: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 395 | break; |
| 396 | } |
| 397 | } |
| 398 | |
Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 399 | // Warn on deprecated methods under -Wdeprecated-implementations, |
| 400 | // and prepare for warning on missing super calls. |
| 401 | if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) { |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 402 | ObjCMethodDecl *IMD = |
| 403 | IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()); |
| 404 | |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 405 | if (IMD) { |
| 406 | ObjCImplDecl *ImplDeclOfMethodDef = |
| 407 | dyn_cast<ObjCImplDecl>(MDecl->getDeclContext()); |
| 408 | ObjCContainerDecl *ContDeclOfMethodDecl = |
| 409 | dyn_cast<ObjCContainerDecl>(IMD->getDeclContext()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 410 | ObjCImplDecl *ImplDeclOfMethodDecl = nullptr; |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 411 | if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl)) |
| 412 | ImplDeclOfMethodDecl = OID->getImplementation(); |
Fariborz Jahanian | ed39e7c | 2014-03-18 16:25:22 +0000 | [diff] [blame] | 413 | else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl)) { |
| 414 | if (CD->IsClassExtension()) { |
| 415 | if (ObjCInterfaceDecl *OID = CD->getClassInterface()) |
| 416 | ImplDeclOfMethodDecl = OID->getImplementation(); |
| 417 | } else |
| 418 | ImplDeclOfMethodDecl = CD->getImplementation(); |
Fariborz Jahanian | 19a08bb | 2014-03-18 00:10:37 +0000 | [diff] [blame] | 419 | } |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 420 | // No need to issue deprecated warning if deprecated mehod in class/category |
| 421 | // is being implemented in its own implementation (no overriding is involved). |
Fariborz Jahanian | ed39e7c | 2014-03-18 16:25:22 +0000 | [diff] [blame] | 422 | if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef) |
Alex Lorenz | f81d97e | 2017-07-13 16:35:59 +0000 | [diff] [blame] | 423 | DiagnoseObjCImplementedDeprecations(*this, IMD, MDecl->getLocation()); |
Fariborz Jahanian | d91d21c | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 424 | } |
Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 425 | |
Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 426 | if (MDecl->getMethodFamily() == OMF_init) { |
| 427 | if (MDecl->isDesignatedInitializerForTheInterface()) { |
| 428 | getCurFunction()->ObjCIsDesignatedInit = true; |
Fariborz Jahanian | e3b5c99 | 2014-03-14 23:30:18 +0000 | [diff] [blame] | 429 | getCurFunction()->ObjCWarnForNoDesignatedInitChain = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 430 | IC->getSuperClass() != nullptr; |
Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 431 | } else if (IC->hasDesignatedInitializers()) { |
| 432 | getCurFunction()->ObjCIsSecondaryInit = true; |
Fariborz Jahanian | e3b5c99 | 2014-03-14 23:30:18 +0000 | [diff] [blame] | 433 | getCurFunction()->ObjCWarnForNoInitDelegation = true; |
Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
Argyrios Kyrtzidis | 22bfa2c | 2013-12-03 21:11:36 +0000 | [diff] [blame] | 436 | |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 437 | // If this is "dealloc" or "finalize", set some bit here. |
Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 438 | // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false. |
| 439 | // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set. |
| 440 | // Only do this if the current class actually has a superclass. |
Jordan Rose | d03d99d | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 441 | if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) { |
Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 442 | ObjCMethodFamily Family = MDecl->getMethodFamily(); |
| 443 | if (Family == OMF_dealloc) { |
| 444 | if (!(getLangOpts().ObjCAutoRefCount || |
| 445 | getLangOpts().getGC() == LangOptions::GCOnly)) |
| 446 | getCurFunction()->ObjCShouldCallSuper = true; |
| 447 | |
| 448 | } else if (Family == OMF_finalize) { |
| 449 | if (Context.getLangOpts().getGC() != LangOptions::NonGC) |
| 450 | getCurFunction()->ObjCShouldCallSuper = true; |
| 451 | |
Fariborz Jahanian | ce4bbb2 | 2013-11-05 00:28:21 +0000 | [diff] [blame] | 452 | } else { |
Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 453 | const ObjCMethodDecl *SuperMethod = |
Jordan Rose | d03d99d | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 454 | SuperClass->lookupMethod(MDecl->getSelector(), |
| 455 | MDecl->isInstanceMethod()); |
Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 456 | getCurFunction()->ObjCShouldCallSuper = |
| 457 | (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>()); |
Fariborz Jahanian | d6876b2 | 2012-09-10 18:04:25 +0000 | [diff] [blame] | 458 | } |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 459 | } |
Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 460 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 463 | namespace { |
| 464 | |
| 465 | // Callback to only accept typo corrections that are Objective-C classes. |
| 466 | // If an ObjCInterfaceDecl* is given to the constructor, then the validation |
| 467 | // function will reject corrections to that class. |
| 468 | class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback { |
| 469 | public: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 470 | ObjCInterfaceValidatorCCC() : CurrentIDecl(nullptr) {} |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 471 | explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl) |
| 472 | : CurrentIDecl(IDecl) {} |
| 473 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 474 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 475 | ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
| 476 | return ID && !declaresSameEntity(ID, CurrentIDecl); |
| 477 | } |
| 478 | |
| 479 | private: |
| 480 | ObjCInterfaceDecl *CurrentIDecl; |
| 481 | }; |
| 482 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 483 | } // end anonymous namespace |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 484 | |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 485 | static void diagnoseUseOfProtocols(Sema &TheSema, |
| 486 | ObjCContainerDecl *CD, |
| 487 | ObjCProtocolDecl *const *ProtoRefs, |
| 488 | unsigned NumProtoRefs, |
| 489 | const SourceLocation *ProtoLocs) { |
| 490 | assert(ProtoRefs); |
| 491 | // Diagnose availability in the context of the ObjC container. |
| 492 | Sema::ContextRAII SavedContext(TheSema, CD); |
| 493 | for (unsigned i = 0; i < NumProtoRefs; ++i) { |
Alex Lorenz | cdd596f | 2017-07-07 09:15:29 +0000 | [diff] [blame] | 494 | (void)TheSema.DiagnoseUseOfDecl(ProtoRefs[i], ProtoLocs[i], |
| 495 | /*UnknownObjCClass=*/nullptr, |
| 496 | /*ObjCPropertyAccess=*/false, |
| 497 | /*AvoidPartialAvailabilityChecks=*/true); |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 501 | void Sema:: |
| 502 | ActOnSuperClassOfClassInterface(Scope *S, |
| 503 | SourceLocation AtInterfaceLoc, |
| 504 | ObjCInterfaceDecl *IDecl, |
| 505 | IdentifierInfo *ClassName, |
| 506 | SourceLocation ClassLoc, |
| 507 | IdentifierInfo *SuperName, |
| 508 | SourceLocation SuperLoc, |
| 509 | ArrayRef<ParsedType> SuperTypeArgs, |
| 510 | SourceRange SuperTypeArgsRange) { |
| 511 | // Check if a different kind of symbol declared in this scope. |
| 512 | NamedDecl *PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
| 513 | LookupOrdinaryName); |
| 514 | |
| 515 | if (!PrevDecl) { |
| 516 | // Try to correct for a typo in the superclass name without correcting |
| 517 | // to the class we're defining. |
| 518 | if (TypoCorrection Corrected = CorrectTypo( |
| 519 | DeclarationNameInfo(SuperName, SuperLoc), |
| 520 | LookupOrdinaryName, TUScope, |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 521 | nullptr, llvm::make_unique<ObjCInterfaceValidatorCCC>(IDecl), |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 522 | CTK_ErrorRecovery)) { |
| 523 | diagnoseTypo(Corrected, PDiag(diag::err_undef_superclass_suggest) |
| 524 | << SuperName << ClassName); |
| 525 | PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | if (declaresSameEntity(PrevDecl, IDecl)) { |
| 530 | Diag(SuperLoc, diag::err_recursive_superclass) |
| 531 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
| 532 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
| 533 | } else { |
| 534 | ObjCInterfaceDecl *SuperClassDecl = |
| 535 | dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
| 536 | QualType SuperClassType; |
| 537 | |
| 538 | // Diagnose classes that inherit from deprecated classes. |
| 539 | if (SuperClassDecl) { |
| 540 | (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc); |
| 541 | SuperClassType = Context.getObjCInterfaceType(SuperClassDecl); |
| 542 | } |
| 543 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 544 | if (PrevDecl && !SuperClassDecl) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 545 | // The previous declaration was not a class decl. Check if we have a |
| 546 | // typedef. If we do, get the underlying class type. |
| 547 | if (const TypedefNameDecl *TDecl = |
| 548 | dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) { |
| 549 | QualType T = TDecl->getUnderlyingType(); |
| 550 | if (T->isObjCObjectType()) { |
| 551 | if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) { |
| 552 | SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl); |
| 553 | SuperClassType = Context.getTypeDeclType(TDecl); |
| 554 | |
| 555 | // This handles the following case: |
| 556 | // @interface NewI @end |
| 557 | // typedef NewI DeprI __attribute__((deprecated("blah"))) |
| 558 | // @interface SI : DeprI /* warn here */ @end |
| 559 | (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc); |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | // This handles the following case: |
| 565 | // |
| 566 | // typedef int SuperClass; |
| 567 | // @interface MyClass : SuperClass {} @end |
| 568 | // |
| 569 | if (!SuperClassDecl) { |
| 570 | Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName; |
| 571 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) { |
| 576 | if (!SuperClassDecl) |
| 577 | Diag(SuperLoc, diag::err_undef_superclass) |
| 578 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
| 579 | else if (RequireCompleteType(SuperLoc, |
| 580 | SuperClassType, |
| 581 | diag::err_forward_superclass, |
| 582 | SuperClassDecl->getDeclName(), |
| 583 | ClassName, |
| 584 | SourceRange(AtInterfaceLoc, ClassLoc))) { |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 585 | SuperClassDecl = nullptr; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 586 | SuperClassType = QualType(); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | if (SuperClassType.isNull()) { |
| 591 | assert(!SuperClassDecl && "Failed to set SuperClassType?"); |
| 592 | return; |
| 593 | } |
| 594 | |
| 595 | // Handle type arguments on the superclass. |
| 596 | TypeSourceInfo *SuperClassTInfo = nullptr; |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 597 | if (!SuperTypeArgs.empty()) { |
| 598 | TypeResult fullSuperClassType = actOnObjCTypeArgsAndProtocolQualifiers( |
| 599 | S, |
| 600 | SuperLoc, |
| 601 | CreateParsedType(SuperClassType, |
| 602 | nullptr), |
| 603 | SuperTypeArgsRange.getBegin(), |
| 604 | SuperTypeArgs, |
| 605 | SuperTypeArgsRange.getEnd(), |
| 606 | SourceLocation(), |
| 607 | { }, |
| 608 | { }, |
| 609 | SourceLocation()); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 610 | if (!fullSuperClassType.isUsable()) |
| 611 | return; |
| 612 | |
| 613 | SuperClassType = GetTypeFromParser(fullSuperClassType.get(), |
| 614 | &SuperClassTInfo); |
| 615 | } |
| 616 | |
| 617 | if (!SuperClassTInfo) { |
| 618 | SuperClassTInfo = Context.getTrivialTypeSourceInfo(SuperClassType, |
| 619 | SuperLoc); |
| 620 | } |
| 621 | |
| 622 | IDecl->setSuperClass(SuperClassTInfo); |
| 623 | IDecl->setEndOfDefinitionLoc(SuperClassTInfo->getTypeLoc().getLocEnd()); |
| 624 | } |
| 625 | } |
| 626 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 627 | DeclResult Sema::actOnObjCTypeParam(Scope *S, |
| 628 | ObjCTypeParamVariance variance, |
| 629 | SourceLocation varianceLoc, |
| 630 | unsigned index, |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 631 | IdentifierInfo *paramName, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 632 | SourceLocation paramLoc, |
| 633 | SourceLocation colonLoc, |
| 634 | ParsedType parsedTypeBound) { |
| 635 | // If there was an explicitly-provided type bound, check it. |
| 636 | TypeSourceInfo *typeBoundInfo = nullptr; |
| 637 | if (parsedTypeBound) { |
| 638 | // The type bound can be any Objective-C pointer type. |
| 639 | QualType typeBound = GetTypeFromParser(parsedTypeBound, &typeBoundInfo); |
| 640 | if (typeBound->isObjCObjectPointerType()) { |
| 641 | // okay |
| 642 | } else if (typeBound->isObjCObjectType()) { |
| 643 | // The user forgot the * on an Objective-C pointer type, e.g., |
| 644 | // "T : NSView". |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 645 | SourceLocation starLoc = getLocForEndOfToken( |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 646 | typeBoundInfo->getTypeLoc().getEndLoc()); |
| 647 | Diag(typeBoundInfo->getTypeLoc().getBeginLoc(), |
| 648 | diag::err_objc_type_param_bound_missing_pointer) |
| 649 | << typeBound << paramName |
| 650 | << FixItHint::CreateInsertion(starLoc, " *"); |
| 651 | |
| 652 | // Create a new type location builder so we can update the type |
| 653 | // location information we have. |
| 654 | TypeLocBuilder builder; |
| 655 | builder.pushFullCopy(typeBoundInfo->getTypeLoc()); |
| 656 | |
| 657 | // Create the Objective-C pointer type. |
| 658 | typeBound = Context.getObjCObjectPointerType(typeBound); |
| 659 | ObjCObjectPointerTypeLoc newT |
| 660 | = builder.push<ObjCObjectPointerTypeLoc>(typeBound); |
| 661 | newT.setStarLoc(starLoc); |
| 662 | |
| 663 | // Form the new type source information. |
| 664 | typeBoundInfo = builder.getTypeSourceInfo(Context, typeBound); |
| 665 | } else { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 666 | // Not a valid type bound. |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 667 | Diag(typeBoundInfo->getTypeLoc().getBeginLoc(), |
| 668 | diag::err_objc_type_param_bound_nonobject) |
| 669 | << typeBound << paramName; |
| 670 | |
| 671 | // Forget the bound; we'll default to id later. |
| 672 | typeBoundInfo = nullptr; |
| 673 | } |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 674 | |
John McCall | 6997525 | 2015-09-23 22:14:21 +0000 | [diff] [blame] | 675 | // Type bounds cannot have qualifiers (even indirectly) or explicit |
| 676 | // nullability. |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 677 | if (typeBoundInfo) { |
John McCall | 6997525 | 2015-09-23 22:14:21 +0000 | [diff] [blame] | 678 | QualType typeBound = typeBoundInfo->getType(); |
| 679 | TypeLoc qual = typeBoundInfo->getTypeLoc().findExplicitQualifierLoc(); |
| 680 | if (qual || typeBound.hasQualifiers()) { |
| 681 | bool diagnosed = false; |
| 682 | SourceRange rangeToRemove; |
| 683 | if (qual) { |
| 684 | if (auto attr = qual.getAs<AttributedTypeLoc>()) { |
| 685 | rangeToRemove = attr.getLocalSourceRange(); |
| 686 | if (attr.getTypePtr()->getImmediateNullability()) { |
| 687 | Diag(attr.getLocStart(), |
| 688 | diag::err_objc_type_param_bound_explicit_nullability) |
| 689 | << paramName << typeBound |
| 690 | << FixItHint::CreateRemoval(rangeToRemove); |
| 691 | diagnosed = true; |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | if (!diagnosed) { |
| 697 | Diag(qual ? qual.getLocStart() |
| 698 | : typeBoundInfo->getTypeLoc().getLocStart(), |
| 699 | diag::err_objc_type_param_bound_qualified) |
| 700 | << paramName << typeBound << typeBound.getQualifiers().getAsString() |
| 701 | << FixItHint::CreateRemoval(rangeToRemove); |
| 702 | } |
| 703 | |
| 704 | // If the type bound has qualifiers other than CVR, we need to strip |
| 705 | // them or we'll probably assert later when trying to apply new |
| 706 | // qualifiers. |
| 707 | Qualifiers quals = typeBound.getQualifiers(); |
| 708 | quals.removeCVRQualifiers(); |
| 709 | if (!quals.empty()) { |
| 710 | typeBoundInfo = |
| 711 | Context.getTrivialTypeSourceInfo(typeBound.getUnqualifiedType()); |
| 712 | } |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 713 | } |
| 714 | } |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | // If there was no explicit type bound (or we removed it due to an error), |
| 718 | // use 'id' instead. |
| 719 | if (!typeBoundInfo) { |
| 720 | colonLoc = SourceLocation(); |
| 721 | typeBoundInfo = Context.getTrivialTypeSourceInfo(Context.getObjCIdType()); |
| 722 | } |
| 723 | |
| 724 | // Create the type parameter. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 725 | return ObjCTypeParamDecl::Create(Context, CurContext, variance, varianceLoc, |
| 726 | index, paramLoc, paramName, colonLoc, |
| 727 | typeBoundInfo); |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | ObjCTypeParamList *Sema::actOnObjCTypeParamList(Scope *S, |
| 731 | SourceLocation lAngleLoc, |
| 732 | ArrayRef<Decl *> typeParamsIn, |
| 733 | SourceLocation rAngleLoc) { |
| 734 | // We know that the array only contains Objective-C type parameters. |
| 735 | ArrayRef<ObjCTypeParamDecl *> |
| 736 | typeParams( |
| 737 | reinterpret_cast<ObjCTypeParamDecl * const *>(typeParamsIn.data()), |
| 738 | typeParamsIn.size()); |
| 739 | |
| 740 | // Diagnose redeclarations of type parameters. |
| 741 | // We do this now because Objective-C type parameters aren't pushed into |
| 742 | // scope until later (after the instance variable block), but we want the |
| 743 | // diagnostics to occur right after we parse the type parameter list. |
| 744 | llvm::SmallDenseMap<IdentifierInfo *, ObjCTypeParamDecl *> knownParams; |
| 745 | for (auto typeParam : typeParams) { |
| 746 | auto known = knownParams.find(typeParam->getIdentifier()); |
| 747 | if (known != knownParams.end()) { |
| 748 | Diag(typeParam->getLocation(), diag::err_objc_type_param_redecl) |
| 749 | << typeParam->getIdentifier() |
| 750 | << SourceRange(known->second->getLocation()); |
| 751 | |
| 752 | typeParam->setInvalidDecl(); |
| 753 | } else { |
| 754 | knownParams.insert(std::make_pair(typeParam->getIdentifier(), typeParam)); |
| 755 | |
| 756 | // Push the type parameter into scope. |
| 757 | PushOnScopeChains(typeParam, S, /*AddToContext=*/false); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | // Create the parameter list. |
| 762 | return ObjCTypeParamList::create(Context, lAngleLoc, typeParams, rAngleLoc); |
| 763 | } |
| 764 | |
| 765 | void Sema::popObjCTypeParamList(Scope *S, ObjCTypeParamList *typeParamList) { |
| 766 | for (auto typeParam : *typeParamList) { |
| 767 | if (!typeParam->isInvalidDecl()) { |
| 768 | S->RemoveDecl(typeParam); |
| 769 | IdResolver.RemoveDecl(typeParam); |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | namespace { |
| 775 | /// The context in which an Objective-C type parameter list occurs, for use |
| 776 | /// in diagnostics. |
| 777 | enum class TypeParamListContext { |
| 778 | ForwardDeclaration, |
| 779 | Definition, |
| 780 | Category, |
| 781 | Extension |
| 782 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 783 | } // end anonymous namespace |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 784 | |
| 785 | /// Check consistency between two Objective-C type parameter lists, e.g., |
NAKAMURA Takumi | 4c3ab45 | 2015-07-08 02:35:56 +0000 | [diff] [blame] | 786 | /// between a category/extension and an \@interface or between an \@class and an |
| 787 | /// \@interface. |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 788 | static bool checkTypeParamListConsistency(Sema &S, |
| 789 | ObjCTypeParamList *prevTypeParams, |
| 790 | ObjCTypeParamList *newTypeParams, |
| 791 | TypeParamListContext newContext) { |
| 792 | // If the sizes don't match, complain about that. |
| 793 | if (prevTypeParams->size() != newTypeParams->size()) { |
| 794 | SourceLocation diagLoc; |
| 795 | if (newTypeParams->size() > prevTypeParams->size()) { |
| 796 | diagLoc = newTypeParams->begin()[prevTypeParams->size()]->getLocation(); |
| 797 | } else { |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 798 | diagLoc = S.getLocForEndOfToken(newTypeParams->back()->getLocEnd()); |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | S.Diag(diagLoc, diag::err_objc_type_param_arity_mismatch) |
| 802 | << static_cast<unsigned>(newContext) |
| 803 | << (newTypeParams->size() > prevTypeParams->size()) |
| 804 | << prevTypeParams->size() |
| 805 | << newTypeParams->size(); |
| 806 | |
| 807 | return true; |
| 808 | } |
| 809 | |
| 810 | // Match up the type parameters. |
| 811 | for (unsigned i = 0, n = prevTypeParams->size(); i != n; ++i) { |
| 812 | ObjCTypeParamDecl *prevTypeParam = prevTypeParams->begin()[i]; |
| 813 | ObjCTypeParamDecl *newTypeParam = newTypeParams->begin()[i]; |
| 814 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 815 | // Check for consistency of the variance. |
| 816 | if (newTypeParam->getVariance() != prevTypeParam->getVariance()) { |
| 817 | if (newTypeParam->getVariance() == ObjCTypeParamVariance::Invariant && |
| 818 | newContext != TypeParamListContext::Definition) { |
| 819 | // When the new type parameter is invariant and is not part |
| 820 | // of the definition, just propagate the variance. |
| 821 | newTypeParam->setVariance(prevTypeParam->getVariance()); |
| 822 | } else if (prevTypeParam->getVariance() |
| 823 | == ObjCTypeParamVariance::Invariant && |
| 824 | !(isa<ObjCInterfaceDecl>(prevTypeParam->getDeclContext()) && |
| 825 | cast<ObjCInterfaceDecl>(prevTypeParam->getDeclContext()) |
| 826 | ->getDefinition() == prevTypeParam->getDeclContext())) { |
| 827 | // When the old parameter is invariant and was not part of the |
| 828 | // definition, just ignore the difference because it doesn't |
| 829 | // matter. |
| 830 | } else { |
| 831 | { |
| 832 | // Diagnose the conflict and update the second declaration. |
| 833 | SourceLocation diagLoc = newTypeParam->getVarianceLoc(); |
| 834 | if (diagLoc.isInvalid()) |
| 835 | diagLoc = newTypeParam->getLocStart(); |
| 836 | |
| 837 | auto diag = S.Diag(diagLoc, |
| 838 | diag::err_objc_type_param_variance_conflict) |
| 839 | << static_cast<unsigned>(newTypeParam->getVariance()) |
| 840 | << newTypeParam->getDeclName() |
| 841 | << static_cast<unsigned>(prevTypeParam->getVariance()) |
| 842 | << prevTypeParam->getDeclName(); |
| 843 | switch (prevTypeParam->getVariance()) { |
| 844 | case ObjCTypeParamVariance::Invariant: |
| 845 | diag << FixItHint::CreateRemoval(newTypeParam->getVarianceLoc()); |
| 846 | break; |
| 847 | |
| 848 | case ObjCTypeParamVariance::Covariant: |
| 849 | case ObjCTypeParamVariance::Contravariant: { |
| 850 | StringRef newVarianceStr |
| 851 | = prevTypeParam->getVariance() == ObjCTypeParamVariance::Covariant |
| 852 | ? "__covariant" |
| 853 | : "__contravariant"; |
| 854 | if (newTypeParam->getVariance() |
| 855 | == ObjCTypeParamVariance::Invariant) { |
| 856 | diag << FixItHint::CreateInsertion(newTypeParam->getLocStart(), |
| 857 | (newVarianceStr + " ").str()); |
| 858 | } else { |
| 859 | diag << FixItHint::CreateReplacement(newTypeParam->getVarianceLoc(), |
| 860 | newVarianceStr); |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
| 867 | << prevTypeParam->getDeclName(); |
| 868 | |
| 869 | // Override the variance. |
| 870 | newTypeParam->setVariance(prevTypeParam->getVariance()); |
| 871 | } |
| 872 | } |
| 873 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 874 | // If the bound types match, there's nothing to do. |
| 875 | if (S.Context.hasSameType(prevTypeParam->getUnderlyingType(), |
| 876 | newTypeParam->getUnderlyingType())) |
| 877 | continue; |
| 878 | |
| 879 | // If the new type parameter's bound was explicit, complain about it being |
| 880 | // different from the original. |
| 881 | if (newTypeParam->hasExplicitBound()) { |
| 882 | SourceRange newBoundRange = newTypeParam->getTypeSourceInfo() |
| 883 | ->getTypeLoc().getSourceRange(); |
| 884 | S.Diag(newBoundRange.getBegin(), diag::err_objc_type_param_bound_conflict) |
| 885 | << newTypeParam->getUnderlyingType() |
| 886 | << newTypeParam->getDeclName() |
| 887 | << prevTypeParam->hasExplicitBound() |
| 888 | << prevTypeParam->getUnderlyingType() |
| 889 | << (newTypeParam->getDeclName() == prevTypeParam->getDeclName()) |
| 890 | << prevTypeParam->getDeclName() |
| 891 | << FixItHint::CreateReplacement( |
| 892 | newBoundRange, |
| 893 | prevTypeParam->getUnderlyingType().getAsString( |
| 894 | S.Context.getPrintingPolicy())); |
| 895 | |
| 896 | S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
| 897 | << prevTypeParam->getDeclName(); |
| 898 | |
| 899 | // Override the new type parameter's bound type with the previous type, |
| 900 | // so that it's consistent. |
| 901 | newTypeParam->setTypeSourceInfo( |
| 902 | S.Context.getTrivialTypeSourceInfo(prevTypeParam->getUnderlyingType())); |
| 903 | continue; |
| 904 | } |
| 905 | |
| 906 | // The new type parameter got the implicit bound of 'id'. That's okay for |
| 907 | // categories and extensions (overwrite it later), but not for forward |
| 908 | // declarations and @interfaces, because those must be standalone. |
| 909 | if (newContext == TypeParamListContext::ForwardDeclaration || |
| 910 | newContext == TypeParamListContext::Definition) { |
| 911 | // Diagnose this problem for forward declarations and definitions. |
| 912 | SourceLocation insertionLoc |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 913 | = S.getLocForEndOfToken(newTypeParam->getLocation()); |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 914 | std::string newCode |
| 915 | = " : " + prevTypeParam->getUnderlyingType().getAsString( |
| 916 | S.Context.getPrintingPolicy()); |
| 917 | S.Diag(newTypeParam->getLocation(), |
| 918 | diag::err_objc_type_param_bound_missing) |
| 919 | << prevTypeParam->getUnderlyingType() |
| 920 | << newTypeParam->getDeclName() |
| 921 | << (newContext == TypeParamListContext::ForwardDeclaration) |
| 922 | << FixItHint::CreateInsertion(insertionLoc, newCode); |
| 923 | |
| 924 | S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
| 925 | << prevTypeParam->getDeclName(); |
| 926 | } |
| 927 | |
| 928 | // Update the new type parameter's bound to match the previous one. |
| 929 | newTypeParam->setTypeSourceInfo( |
| 930 | S.Context.getTrivialTypeSourceInfo(prevTypeParam->getUnderlyingType())); |
| 931 | } |
| 932 | |
| 933 | return false; |
| 934 | } |
| 935 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 936 | Decl *Sema:: |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 937 | ActOnStartClassInterface(Scope *S, SourceLocation AtInterfaceLoc, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 938 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 939 | ObjCTypeParamList *typeParamList, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 940 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 941 | ArrayRef<ParsedType> SuperTypeArgs, |
| 942 | SourceRange SuperTypeArgsRange, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 943 | Decl * const *ProtoRefs, unsigned NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 944 | const SourceLocation *ProtoLocs, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 945 | SourceLocation EndProtoLoc, AttributeList *AttrList) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 946 | assert(ClassName && "Missing class identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 947 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 948 | // Check for another declaration kind with the same name. |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 949 | NamedDecl *PrevDecl = |
| 950 | LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName, |
| 951 | forRedeclarationInCurContext()); |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 952 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 953 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 954 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 955 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 956 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 957 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 958 | // Create a declaration to describe this @interface. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 959 | ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Argyrios Kyrtzidis | dd71063 | 2013-06-18 21:26:33 +0000 | [diff] [blame] | 960 | |
| 961 | if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) { |
| 962 | // A previous decl with a different name is because of |
| 963 | // @compatibility_alias, for example: |
| 964 | // \code |
| 965 | // @class NewImage; |
| 966 | // @compatibility_alias OldImage NewImage; |
| 967 | // \endcode |
| 968 | // A lookup for 'OldImage' will return the 'NewImage' decl. |
| 969 | // |
| 970 | // In such a case use the real declaration name, instead of the alias one, |
| 971 | // otherwise we will break IdentifierResolver and redecls-chain invariants. |
| 972 | // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl |
| 973 | // has been aliased. |
| 974 | ClassName = PrevIDecl->getIdentifier(); |
| 975 | } |
| 976 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 977 | // If there was a forward declaration with type parameters, check |
| 978 | // for consistency. |
| 979 | if (PrevIDecl) { |
| 980 | if (ObjCTypeParamList *prevTypeParamList = PrevIDecl->getTypeParamList()) { |
| 981 | if (typeParamList) { |
| 982 | // Both have type parameter lists; check for consistency. |
| 983 | if (checkTypeParamListConsistency(*this, prevTypeParamList, |
| 984 | typeParamList, |
| 985 | TypeParamListContext::Definition)) { |
| 986 | typeParamList = nullptr; |
| 987 | } |
| 988 | } else { |
| 989 | Diag(ClassLoc, diag::err_objc_parameterized_forward_class_first) |
| 990 | << ClassName; |
| 991 | Diag(prevTypeParamList->getLAngleLoc(), diag::note_previous_decl) |
| 992 | << ClassName; |
| 993 | |
| 994 | // Clone the type parameter list. |
| 995 | SmallVector<ObjCTypeParamDecl *, 4> clonedTypeParams; |
| 996 | for (auto typeParam : *prevTypeParamList) { |
| 997 | clonedTypeParams.push_back( |
| 998 | ObjCTypeParamDecl::Create( |
| 999 | Context, |
| 1000 | CurContext, |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 1001 | typeParam->getVariance(), |
| 1002 | SourceLocation(), |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1003 | typeParam->getIndex(), |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1004 | SourceLocation(), |
| 1005 | typeParam->getIdentifier(), |
| 1006 | SourceLocation(), |
| 1007 | Context.getTrivialTypeSourceInfo(typeParam->getUnderlyingType()))); |
| 1008 | } |
| 1009 | |
| 1010 | typeParamList = ObjCTypeParamList::create(Context, |
| 1011 | SourceLocation(), |
| 1012 | clonedTypeParams, |
| 1013 | SourceLocation()); |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1018 | ObjCInterfaceDecl *IDecl |
| 1019 | = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1020 | typeParamList, PrevIDecl, ClassLoc); |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1021 | if (PrevIDecl) { |
| 1022 | // Class already seen. Was it a definition? |
| 1023 | if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) { |
| 1024 | Diag(AtInterfaceLoc, diag::err_duplicate_class_def) |
| 1025 | << PrevIDecl->getDeclName(); |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 1026 | Diag(Def->getLocation(), diag::note_previous_definition); |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1027 | IDecl->setInvalidDecl(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1028 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1029 | } |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1030 | |
| 1031 | if (AttrList) |
| 1032 | ProcessDeclAttributeList(TUScope, IDecl, AttrList); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1033 | AddPragmaAttributes(TUScope, IDecl); |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1034 | PushOnScopeChains(IDecl, TUScope); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1035 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1036 | // Start the definition of this class. If we're in a redefinition case, there |
| 1037 | // 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] | 1038 | if (!IDecl->hasDefinition()) |
| 1039 | IDecl->startDefinition(); |
| 1040 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1041 | if (SuperName) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1042 | // Diagnose availability in the context of the @interface. |
| 1043 | ContextRAII SavedContext(*this, IDecl); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1044 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1045 | ActOnSuperClassOfClassInterface(S, AtInterfaceLoc, IDecl, |
| 1046 | ClassName, ClassLoc, |
| 1047 | SuperName, SuperLoc, SuperTypeArgs, |
| 1048 | SuperTypeArgsRange); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1049 | } else { // we have a root class. |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 1050 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1051 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1052 | |
Sebastian Redl | e7c1fe6 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 1053 | // Check then save referenced protocols. |
Chris Lattner | df59f5a | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 1054 | if (NumProtoRefs) { |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1055 | diagnoseUseOfProtocols(*this, IDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
| 1056 | NumProtoRefs, ProtoLocs); |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 1057 | IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 1058 | ProtoLocs, Context); |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 1059 | IDecl->setEndOfDefinitionLoc(EndProtoLoc); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1060 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1061 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1062 | CheckObjCDeclScope(IDecl); |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1063 | return ActOnObjCContainerStartDefinition(IDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Fariborz Jahanian | b7c5f74 | 2013-09-25 19:36:32 +0000 | [diff] [blame] | 1066 | /// ActOnTypedefedProtocols - this action finds protocol list as part of the |
| 1067 | /// typedef'ed use for a qualified super class and adds them to the list |
| 1068 | /// of the protocols. |
| 1069 | void Sema::ActOnTypedefedProtocols(SmallVectorImpl<Decl *> &ProtocolRefs, |
Argyrios Kyrtzidis | f95a000 | 2016-11-09 02:47:07 +0000 | [diff] [blame] | 1070 | SmallVectorImpl<SourceLocation> &ProtocolLocs, |
Fariborz Jahanian | b7c5f74 | 2013-09-25 19:36:32 +0000 | [diff] [blame] | 1071 | IdentifierInfo *SuperName, |
| 1072 | SourceLocation SuperLoc) { |
| 1073 | if (!SuperName) |
| 1074 | return; |
| 1075 | NamedDecl* IDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
| 1076 | LookupOrdinaryName); |
| 1077 | if (!IDecl) |
| 1078 | return; |
| 1079 | |
| 1080 | if (const TypedefNameDecl *TDecl = dyn_cast_or_null<TypedefNameDecl>(IDecl)) { |
| 1081 | QualType T = TDecl->getUnderlyingType(); |
| 1082 | if (T->isObjCObjectType()) |
Argyrios Kyrtzidis | f95a000 | 2016-11-09 02:47:07 +0000 | [diff] [blame] | 1083 | if (const ObjCObjectType *OPT = T->getAs<ObjCObjectType>()) { |
Benjamin Kramer | f989042 | 2015-02-17 16:48:30 +0000 | [diff] [blame] | 1084 | ProtocolRefs.append(OPT->qual_begin(), OPT->qual_end()); |
Argyrios Kyrtzidis | f95a000 | 2016-11-09 02:47:07 +0000 | [diff] [blame] | 1085 | // FIXME: Consider whether this should be an invalid loc since the loc |
| 1086 | // is not actually pointing to a protocol name reference but to the |
| 1087 | // typedef reference. Note that the base class name loc is also pointing |
| 1088 | // at the typedef. |
| 1089 | ProtocolLocs.append(OPT->getNumProtocols(), SuperLoc); |
| 1090 | } |
Fariborz Jahanian | b7c5f74 | 2013-09-25 19:36:32 +0000 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
Richard Smith | ac4e36d | 2012-08-08 23:32:13 +0000 | [diff] [blame] | 1094 | /// ActOnCompatibilityAlias - this action is called after complete parsing of |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 1095 | /// a \@compatibility_alias declaration. It sets up the alias relationships. |
Richard Smith | ac4e36d | 2012-08-08 23:32:13 +0000 | [diff] [blame] | 1096 | Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc, |
| 1097 | IdentifierInfo *AliasName, |
| 1098 | SourceLocation AliasLocation, |
| 1099 | IdentifierInfo *ClassName, |
| 1100 | SourceLocation ClassLocation) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1101 | // Look for previous declaration of alias name |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1102 | NamedDecl *ADecl = |
| 1103 | LookupSingleName(TUScope, AliasName, AliasLocation, LookupOrdinaryName, |
| 1104 | forRedeclarationInCurContext()); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1105 | if (ADecl) { |
Eli Friedman | fd6b3f8 | 2013-06-21 01:49:53 +0000 | [diff] [blame] | 1106 | Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; |
Chris Lattner | d068503 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 1107 | Diag(ADecl->getLocation(), diag::note_previous_declaration); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1108 | return nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1109 | } |
| 1110 | // Check for class declaration |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1111 | NamedDecl *CDeclU = |
| 1112 | LookupSingleName(TUScope, ClassName, ClassLocation, LookupOrdinaryName, |
| 1113 | forRedeclarationInCurContext()); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1114 | if (const TypedefNameDecl *TDecl = |
| 1115 | dyn_cast_or_null<TypedefNameDecl>(CDeclU)) { |
Fariborz Jahanian | 17290c3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 1116 | QualType T = TDecl->getUnderlyingType(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1117 | if (T->isObjCObjectType()) { |
| 1118 | if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) { |
Fariborz Jahanian | 17290c3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 1119 | ClassName = IDecl->getIdentifier(); |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1120 | CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1121 | LookupOrdinaryName, |
| 1122 | forRedeclarationInCurContext()); |
Fariborz Jahanian | 17290c3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 1123 | } |
| 1124 | } |
| 1125 | } |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 1126 | ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1127 | if (!CDecl) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1128 | Diag(ClassLocation, diag::warn_undef_interface) << ClassName; |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 1129 | if (CDeclU) |
Chris Lattner | d068503 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 1130 | Diag(CDeclU->getLocation(), diag::note_previous_declaration); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1131 | return nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1132 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1133 | |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 1134 | // Everything checked out, instantiate a new alias declaration AST. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1135 | ObjCCompatibleAliasDecl *AliasDecl = |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1136 | ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1138 | if (!CheckObjCDeclScope(AliasDecl)) |
Douglas Gregor | 38feed8 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 1139 | PushOnScopeChains(AliasDecl, TUScope); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1140 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1141 | return AliasDecl; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1144 | bool Sema::CheckForwardProtocolDeclarationForCircularDependency( |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1145 | IdentifierInfo *PName, |
| 1146 | SourceLocation &Ploc, SourceLocation PrevLoc, |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1147 | const ObjCList<ObjCProtocolDecl> &PList) { |
| 1148 | |
| 1149 | bool res = false; |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1150 | for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(), |
| 1151 | E = PList.end(); I != E; ++I) { |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1152 | if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(), |
| 1153 | Ploc)) { |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1154 | if (PDecl->getIdentifier() == PName) { |
| 1155 | Diag(Ploc, diag::err_protocol_has_circular_dependency); |
| 1156 | Diag(PrevLoc, diag::note_previous_definition); |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1157 | res = true; |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1158 | } |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 1159 | |
| 1160 | if (!PDecl->hasDefinition()) |
| 1161 | continue; |
| 1162 | |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1163 | if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc, |
| 1164 | PDecl->getLocation(), PDecl->getReferencedProtocols())) |
| 1165 | res = true; |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1166 | } |
| 1167 | } |
Fariborz Jahanian | 7d62273 | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 1168 | return res; |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1171 | Decl * |
Chris Lattner | 3bbae00 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 1172 | Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc, |
| 1173 | IdentifierInfo *ProtocolName, |
| 1174 | SourceLocation ProtocolLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1175 | Decl * const *ProtoRefs, |
Chris Lattner | 3bbae00 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 1176 | unsigned NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 1177 | const SourceLocation *ProtoLocs, |
Daniel Dunbar | 26e2ab4 | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 1178 | SourceLocation EndProtoLoc, |
| 1179 | AttributeList *AttrList) { |
Fariborz Jahanian | cadf7c5 | 2011-05-12 22:04:39 +0000 | [diff] [blame] | 1180 | bool err = false; |
Daniel Dunbar | 26e2ab4 | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 1181 | // FIXME: Deal with AttrList. |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1182 | assert(ProtocolName && "Missing protocol identifier"); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1183 | ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1184 | forRedeclarationInCurContext()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1185 | ObjCProtocolDecl *PDecl = nullptr; |
| 1186 | if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : nullptr) { |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1187 | // If we already have a definition, complain. |
| 1188 | Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName; |
| 1189 | Diag(Def->getLocation(), diag::note_previous_definition); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1190 | |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1191 | // Create a new protocol that is completely distinct from previous |
| 1192 | // declarations, and do not make this protocol available for name lookup. |
| 1193 | // That way, we'll end up completely ignoring the duplicate. |
| 1194 | // FIXME: Can we turn this into an error? |
| 1195 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName, |
| 1196 | ProtocolLoc, AtProtoInterfaceLoc, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1197 | /*PrevDecl=*/nullptr); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1198 | PDecl->startDefinition(); |
| 1199 | } else { |
| 1200 | if (PrevDecl) { |
| 1201 | // Check for circular dependencies among protocol declarations. This can |
| 1202 | // only happen if this protocol was forward-declared. |
Argyrios Kyrtzidis | 95dfc12 | 2011-11-13 22:08:30 +0000 | [diff] [blame] | 1203 | ObjCList<ObjCProtocolDecl> PList; |
| 1204 | PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context); |
| 1205 | err = CheckForwardProtocolDeclarationForCircularDependency( |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1206 | ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList); |
Argyrios Kyrtzidis | 95dfc12 | 2011-11-13 22:08:30 +0000 | [diff] [blame] | 1207 | } |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1208 | |
| 1209 | // Create the new declaration. |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 1210 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName, |
Argyrios Kyrtzidis | 1f4bee5 | 2011-10-17 19:48:06 +0000 | [diff] [blame] | 1211 | ProtocolLoc, AtProtoInterfaceLoc, |
Douglas Gregor | 05a1f4d | 2012-01-01 22:06:18 +0000 | [diff] [blame] | 1212 | /*PrevDecl=*/PrevDecl); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1213 | |
Douglas Gregor | de9f17e | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 1214 | PushOnScopeChains(PDecl, TUScope); |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 1215 | PDecl->startDefinition(); |
Chris Lattner | f87ca0a | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 1216 | } |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 1217 | |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1218 | if (AttrList) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1219 | ProcessDeclAttributeList(TUScope, PDecl, AttrList); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1220 | AddPragmaAttributes(TUScope, PDecl); |
| 1221 | |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1222 | // Merge attributes from previous declarations. |
| 1223 | if (PrevDecl) |
| 1224 | mergeDeclAttributes(PDecl, PrevDecl); |
| 1225 | |
Fariborz Jahanian | cadf7c5 | 2011-05-12 22:04:39 +0000 | [diff] [blame] | 1226 | if (!err && NumProtoRefs ) { |
Chris Lattner | acc04a9 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 1227 | /// Check then save referenced protocols. |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1228 | diagnoseUseOfProtocols(*this, PDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
| 1229 | NumProtoRefs, ProtoLocs); |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 1230 | PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 1231 | ProtoLocs, Context); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1232 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1233 | |
| 1234 | CheckObjCDeclScope(PDecl); |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1235 | return ActOnObjCContainerStartDefinition(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1238 | static bool NestedProtocolHasNoDefinition(ObjCProtocolDecl *PDecl, |
| 1239 | ObjCProtocolDecl *&UndefinedProtocol) { |
| 1240 | if (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()) { |
| 1241 | UndefinedProtocol = PDecl; |
| 1242 | return true; |
| 1243 | } |
| 1244 | |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 1245 | for (auto *PI : PDecl->protocols()) |
| 1246 | if (NestedProtocolHasNoDefinition(PI, UndefinedProtocol)) { |
| 1247 | UndefinedProtocol = PI; |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1248 | return true; |
| 1249 | } |
| 1250 | return false; |
| 1251 | } |
| 1252 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1253 | /// FindProtocolDeclaration - This routine looks up protocols and |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1254 | /// issues an error if they are not declared. It returns list of |
| 1255 | /// protocol declarations in its 'Protocols' argument. |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1256 | void |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1257 | Sema::FindProtocolDeclaration(bool WarnOnDeclarations, bool ForObjCContainer, |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1258 | ArrayRef<IdentifierLocPair> ProtocolId, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1259 | SmallVectorImpl<Decl *> &Protocols) { |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1260 | for (const IdentifierLocPair &Pair : ProtocolId) { |
| 1261 | ObjCProtocolDecl *PDecl = LookupProtocol(Pair.first, Pair.second); |
Chris Lattner | 9c1842b | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 1262 | if (!PDecl) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1263 | TypoCorrection Corrected = CorrectTypo( |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1264 | DeclarationNameInfo(Pair.first, Pair.second), |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 1265 | LookupObjCProtocolName, TUScope, nullptr, |
| 1266 | llvm::make_unique<DeclFilterCCC<ObjCProtocolDecl>>(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1267 | CTK_ErrorRecovery); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1268 | if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) |
| 1269 | diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest) |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1270 | << Pair.first); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | if (!PDecl) { |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1274 | Diag(Pair.second, diag::err_undeclared_protocol) << Pair.first; |
Chris Lattner | 9c1842b | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 1275 | continue; |
| 1276 | } |
Fariborz Jahanian | ada44a2 | 2013-04-09 17:52:29 +0000 | [diff] [blame] | 1277 | // If this is a forward protocol declaration, get its definition. |
| 1278 | if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition()) |
| 1279 | PDecl = PDecl->getDefinition(); |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1280 | |
| 1281 | // For an objc container, delay protocol reference checking until after we |
| 1282 | // can set the objc decl as the availability context, otherwise check now. |
| 1283 | if (!ForObjCContainer) { |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1284 | (void)DiagnoseUseOfDecl(PDecl, Pair.second); |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1285 | } |
Chris Lattner | 9c1842b | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 1286 | |
| 1287 | // If this is a forward declaration and we are supposed to warn in this |
| 1288 | // case, do it. |
Douglas Gregor | eed4979 | 2013-01-17 00:38:46 +0000 | [diff] [blame] | 1289 | // FIXME: Recover nicely in the hidden case. |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1290 | ObjCProtocolDecl *UndefinedProtocol; |
| 1291 | |
Douglas Gregor | eed4979 | 2013-01-17 00:38:46 +0000 | [diff] [blame] | 1292 | if (WarnOnDeclarations && |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1293 | NestedProtocolHasNoDefinition(PDecl, UndefinedProtocol)) { |
Craig Topper | a9247eb | 2015-10-22 04:59:56 +0000 | [diff] [blame] | 1294 | Diag(Pair.second, diag::warn_undef_protocolref) << Pair.first; |
Fariborz Jahanian | bf678e8 | 2014-03-11 17:10:51 +0000 | [diff] [blame] | 1295 | Diag(UndefinedProtocol->getLocation(), diag::note_protocol_decl_undefined) |
| 1296 | << UndefinedProtocol; |
| 1297 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1298 | Protocols.push_back(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1299 | } |
| 1300 | } |
| 1301 | |
Benjamin Kramer | 8b851d0 | 2015-07-13 20:42:13 +0000 | [diff] [blame] | 1302 | namespace { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1303 | // Callback to only accept typo corrections that are either |
| 1304 | // Objective-C protocols or valid Objective-C type arguments. |
| 1305 | class ObjCTypeArgOrProtocolValidatorCCC : public CorrectionCandidateCallback { |
| 1306 | ASTContext &Context; |
| 1307 | Sema::LookupNameKind LookupKind; |
| 1308 | public: |
| 1309 | ObjCTypeArgOrProtocolValidatorCCC(ASTContext &context, |
| 1310 | Sema::LookupNameKind lookupKind) |
| 1311 | : Context(context), LookupKind(lookupKind) { } |
| 1312 | |
| 1313 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
| 1314 | // If we're allowed to find protocols and we have a protocol, accept it. |
| 1315 | if (LookupKind != Sema::LookupOrdinaryName) { |
| 1316 | if (candidate.getCorrectionDeclAs<ObjCProtocolDecl>()) |
| 1317 | return true; |
| 1318 | } |
| 1319 | |
| 1320 | // If we're allowed to find type names and we have one, accept it. |
| 1321 | if (LookupKind != Sema::LookupObjCProtocolName) { |
| 1322 | // If we have a type declaration, we might accept this result. |
| 1323 | if (auto typeDecl = candidate.getCorrectionDeclAs<TypeDecl>()) { |
| 1324 | // If we found a tag declaration outside of C++, skip it. This |
| 1325 | // can happy because we look for any name when there is no |
| 1326 | // bias to protocol or type names. |
| 1327 | if (isa<RecordDecl>(typeDecl) && !Context.getLangOpts().CPlusPlus) |
| 1328 | return false; |
| 1329 | |
| 1330 | // Make sure the type is something we would accept as a type |
| 1331 | // argument. |
| 1332 | auto type = Context.getTypeDeclType(typeDecl); |
| 1333 | if (type->isObjCObjectPointerType() || |
| 1334 | type->isBlockPointerType() || |
| 1335 | type->isDependentType() || |
| 1336 | type->isObjCObjectType()) |
| 1337 | return true; |
| 1338 | |
| 1339 | return false; |
| 1340 | } |
| 1341 | |
| 1342 | // If we have an Objective-C class type, accept it; there will |
| 1343 | // be another fix to add the '*'. |
| 1344 | if (candidate.getCorrectionDeclAs<ObjCInterfaceDecl>()) |
| 1345 | return true; |
| 1346 | |
| 1347 | return false; |
| 1348 | } |
| 1349 | |
| 1350 | return false; |
| 1351 | } |
| 1352 | }; |
Benjamin Kramer | 8b851d0 | 2015-07-13 20:42:13 +0000 | [diff] [blame] | 1353 | } // end anonymous namespace |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1354 | |
Bruno Cardoso Lopes | c54768f | 2016-04-13 20:59:07 +0000 | [diff] [blame] | 1355 | void Sema::DiagnoseTypeArgsAndProtocols(IdentifierInfo *ProtocolId, |
| 1356 | SourceLocation ProtocolLoc, |
| 1357 | IdentifierInfo *TypeArgId, |
| 1358 | SourceLocation TypeArgLoc, |
| 1359 | bool SelectProtocolFirst) { |
| 1360 | Diag(TypeArgLoc, diag::err_objc_type_args_and_protocols) |
| 1361 | << SelectProtocolFirst << TypeArgId << ProtocolId |
| 1362 | << SourceRange(ProtocolLoc); |
| 1363 | } |
| 1364 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1365 | void Sema::actOnObjCTypeArgsOrProtocolQualifiers( |
| 1366 | Scope *S, |
Douglas Gregor | 10dc9d8 | 2015-07-07 03:58:28 +0000 | [diff] [blame] | 1367 | ParsedType baseType, |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1368 | SourceLocation lAngleLoc, |
| 1369 | ArrayRef<IdentifierInfo *> identifiers, |
| 1370 | ArrayRef<SourceLocation> identifierLocs, |
| 1371 | SourceLocation rAngleLoc, |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1372 | SourceLocation &typeArgsLAngleLoc, |
| 1373 | SmallVectorImpl<ParsedType> &typeArgs, |
| 1374 | SourceLocation &typeArgsRAngleLoc, |
| 1375 | SourceLocation &protocolLAngleLoc, |
| 1376 | SmallVectorImpl<Decl *> &protocols, |
| 1377 | SourceLocation &protocolRAngleLoc, |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1378 | bool warnOnIncompleteProtocols) { |
| 1379 | // Local function that updates the declaration specifiers with |
| 1380 | // protocol information. |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1381 | unsigned numProtocolsResolved = 0; |
| 1382 | auto resolvedAsProtocols = [&] { |
| 1383 | assert(numProtocolsResolved == identifiers.size() && "Unresolved protocols"); |
| 1384 | |
Douglas Gregor | 10dc9d8 | 2015-07-07 03:58:28 +0000 | [diff] [blame] | 1385 | // Determine whether the base type is a parameterized class, in |
| 1386 | // which case we want to warn about typos such as |
| 1387 | // "NSArray<NSObject>" (that should be NSArray<NSObject *>). |
| 1388 | ObjCInterfaceDecl *baseClass = nullptr; |
| 1389 | QualType base = GetTypeFromParser(baseType, nullptr); |
| 1390 | bool allAreTypeNames = false; |
| 1391 | SourceLocation firstClassNameLoc; |
| 1392 | if (!base.isNull()) { |
| 1393 | if (const auto *objcObjectType = base->getAs<ObjCObjectType>()) { |
| 1394 | baseClass = objcObjectType->getInterface(); |
| 1395 | if (baseClass) { |
| 1396 | if (auto typeParams = baseClass->getTypeParamList()) { |
| 1397 | if (typeParams->size() == numProtocolsResolved) { |
| 1398 | // Note that we should be looking for type names, too. |
| 1399 | allAreTypeNames = true; |
| 1400 | } |
| 1401 | } |
| 1402 | } |
| 1403 | } |
| 1404 | } |
| 1405 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1406 | for (unsigned i = 0, n = protocols.size(); i != n; ++i) { |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1407 | ObjCProtocolDecl *&proto |
| 1408 | = reinterpret_cast<ObjCProtocolDecl *&>(protocols[i]); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1409 | // For an objc container, delay protocol reference checking until after we |
| 1410 | // can set the objc decl as the availability context, otherwise check now. |
| 1411 | if (!warnOnIncompleteProtocols) { |
| 1412 | (void)DiagnoseUseOfDecl(proto, identifierLocs[i]); |
| 1413 | } |
| 1414 | |
| 1415 | // If this is a forward protocol declaration, get its definition. |
| 1416 | if (!proto->isThisDeclarationADefinition() && proto->getDefinition()) |
| 1417 | proto = proto->getDefinition(); |
| 1418 | |
| 1419 | // If this is a forward declaration and we are supposed to warn in this |
| 1420 | // case, do it. |
| 1421 | // FIXME: Recover nicely in the hidden case. |
| 1422 | ObjCProtocolDecl *forwardDecl = nullptr; |
| 1423 | if (warnOnIncompleteProtocols && |
| 1424 | NestedProtocolHasNoDefinition(proto, forwardDecl)) { |
| 1425 | Diag(identifierLocs[i], diag::warn_undef_protocolref) |
| 1426 | << proto->getDeclName(); |
| 1427 | Diag(forwardDecl->getLocation(), diag::note_protocol_decl_undefined) |
| 1428 | << forwardDecl; |
| 1429 | } |
Douglas Gregor | 10dc9d8 | 2015-07-07 03:58:28 +0000 | [diff] [blame] | 1430 | |
| 1431 | // If everything this far has been a type name (and we care |
| 1432 | // about such things), check whether this name refers to a type |
| 1433 | // as well. |
| 1434 | if (allAreTypeNames) { |
| 1435 | if (auto *decl = LookupSingleName(S, identifiers[i], identifierLocs[i], |
| 1436 | LookupOrdinaryName)) { |
| 1437 | if (isa<ObjCInterfaceDecl>(decl)) { |
| 1438 | if (firstClassNameLoc.isInvalid()) |
| 1439 | firstClassNameLoc = identifierLocs[i]; |
| 1440 | } else if (!isa<TypeDecl>(decl)) { |
| 1441 | // Not a type. |
| 1442 | allAreTypeNames = false; |
| 1443 | } |
| 1444 | } else { |
| 1445 | allAreTypeNames = false; |
| 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | // All of the protocols listed also have type names, and at least |
| 1451 | // one is an Objective-C class name. Check whether all of the |
| 1452 | // protocol conformances are declared by the base class itself, in |
| 1453 | // which case we warn. |
| 1454 | if (allAreTypeNames && firstClassNameLoc.isValid()) { |
| 1455 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> knownProtocols; |
| 1456 | Context.CollectInheritedProtocols(baseClass, knownProtocols); |
| 1457 | bool allProtocolsDeclared = true; |
| 1458 | for (auto proto : protocols) { |
| 1459 | if (knownProtocols.count(static_cast<ObjCProtocolDecl *>(proto)) == 0) { |
| 1460 | allProtocolsDeclared = false; |
| 1461 | break; |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | if (allProtocolsDeclared) { |
| 1466 | Diag(firstClassNameLoc, diag::warn_objc_redundant_qualified_class_type) |
| 1467 | << baseClass->getDeclName() << SourceRange(lAngleLoc, rAngleLoc) |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 1468 | << FixItHint::CreateInsertion(getLocForEndOfToken(firstClassNameLoc), |
| 1469 | " *"); |
Douglas Gregor | 10dc9d8 | 2015-07-07 03:58:28 +0000 | [diff] [blame] | 1470 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1473 | protocolLAngleLoc = lAngleLoc; |
| 1474 | protocolRAngleLoc = rAngleLoc; |
| 1475 | assert(protocols.size() == identifierLocs.size()); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1476 | }; |
| 1477 | |
| 1478 | // Attempt to resolve all of the identifiers as protocols. |
| 1479 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1480 | ObjCProtocolDecl *proto = LookupProtocol(identifiers[i], identifierLocs[i]); |
| 1481 | protocols.push_back(proto); |
| 1482 | if (proto) |
| 1483 | ++numProtocolsResolved; |
| 1484 | } |
| 1485 | |
| 1486 | // If all of the names were protocols, these were protocol qualifiers. |
| 1487 | if (numProtocolsResolved == identifiers.size()) |
| 1488 | return resolvedAsProtocols(); |
| 1489 | |
| 1490 | // Attempt to resolve all of the identifiers as type names or |
| 1491 | // Objective-C class names. The latter is technically ill-formed, |
| 1492 | // but is probably something like \c NSArray<NSView *> missing the |
| 1493 | // \c*. |
| 1494 | typedef llvm::PointerUnion<TypeDecl *, ObjCInterfaceDecl *> TypeOrClassDecl; |
| 1495 | SmallVector<TypeOrClassDecl, 4> typeDecls; |
| 1496 | unsigned numTypeDeclsResolved = 0; |
| 1497 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1498 | NamedDecl *decl = LookupSingleName(S, identifiers[i], identifierLocs[i], |
| 1499 | LookupOrdinaryName); |
| 1500 | if (!decl) { |
| 1501 | typeDecls.push_back(TypeOrClassDecl()); |
| 1502 | continue; |
| 1503 | } |
| 1504 | |
| 1505 | if (auto typeDecl = dyn_cast<TypeDecl>(decl)) { |
| 1506 | typeDecls.push_back(typeDecl); |
| 1507 | ++numTypeDeclsResolved; |
| 1508 | continue; |
| 1509 | } |
| 1510 | |
| 1511 | if (auto objcClass = dyn_cast<ObjCInterfaceDecl>(decl)) { |
| 1512 | typeDecls.push_back(objcClass); |
| 1513 | ++numTypeDeclsResolved; |
| 1514 | continue; |
| 1515 | } |
| 1516 | |
| 1517 | typeDecls.push_back(TypeOrClassDecl()); |
| 1518 | } |
| 1519 | |
| 1520 | AttributeFactory attrFactory; |
| 1521 | |
| 1522 | // Local function that forms a reference to the given type or |
| 1523 | // Objective-C class declaration. |
| 1524 | auto resolveTypeReference = [&](TypeOrClassDecl typeDecl, SourceLocation loc) |
| 1525 | -> TypeResult { |
| 1526 | // Form declaration specifiers. They simply refer to the type. |
| 1527 | DeclSpec DS(attrFactory); |
| 1528 | const char* prevSpec; // unused |
| 1529 | unsigned diagID; // unused |
| 1530 | QualType type; |
| 1531 | if (auto *actualTypeDecl = typeDecl.dyn_cast<TypeDecl *>()) |
| 1532 | type = Context.getTypeDeclType(actualTypeDecl); |
| 1533 | else |
| 1534 | type = Context.getObjCInterfaceType(typeDecl.get<ObjCInterfaceDecl *>()); |
| 1535 | TypeSourceInfo *parsedTSInfo = Context.getTrivialTypeSourceInfo(type, loc); |
| 1536 | ParsedType parsedType = CreateParsedType(type, parsedTSInfo); |
| 1537 | DS.SetTypeSpecType(DeclSpec::TST_typename, loc, prevSpec, diagID, |
| 1538 | parsedType, Context.getPrintingPolicy()); |
| 1539 | // Use the identifier location for the type source range. |
| 1540 | DS.SetRangeStart(loc); |
| 1541 | DS.SetRangeEnd(loc); |
| 1542 | |
| 1543 | // Form the declarator. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1544 | Declarator D(DS, DeclaratorContext::TypeNameContext); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1545 | |
| 1546 | // If we have a typedef of an Objective-C class type that is missing a '*', |
| 1547 | // add the '*'. |
| 1548 | if (type->getAs<ObjCInterfaceType>()) { |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 1549 | SourceLocation starLoc = getLocForEndOfToken(loc); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1550 | ParsedAttributes parsedAttrs(attrFactory); |
| 1551 | D.AddTypeInfo(DeclaratorChunk::getPointer(/*typeQuals=*/0, starLoc, |
| 1552 | SourceLocation(), |
| 1553 | SourceLocation(), |
| 1554 | SourceLocation(), |
Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 1555 | SourceLocation(), |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1556 | SourceLocation()), |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1557 | parsedAttrs, |
| 1558 | starLoc); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1559 | |
| 1560 | // Diagnose the missing '*'. |
| 1561 | Diag(loc, diag::err_objc_type_arg_missing_star) |
| 1562 | << type |
| 1563 | << FixItHint::CreateInsertion(starLoc, " *"); |
| 1564 | } |
| 1565 | |
| 1566 | // Convert this to a type. |
| 1567 | return ActOnTypeName(S, D); |
| 1568 | }; |
| 1569 | |
| 1570 | // Local function that updates the declaration specifiers with |
| 1571 | // type argument information. |
| 1572 | auto resolvedAsTypeDecls = [&] { |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1573 | // We did not resolve these as protocols. |
| 1574 | protocols.clear(); |
| 1575 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1576 | assert(numTypeDeclsResolved == identifiers.size() && "Unresolved type decl"); |
| 1577 | // Map type declarations to type arguments. |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1578 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1579 | // Map type reference to a type. |
| 1580 | TypeResult type = resolveTypeReference(typeDecls[i], identifierLocs[i]); |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1581 | if (!type.isUsable()) { |
| 1582 | typeArgs.clear(); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1583 | return; |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1584 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1585 | |
| 1586 | typeArgs.push_back(type.get()); |
| 1587 | } |
| 1588 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1589 | typeArgsLAngleLoc = lAngleLoc; |
| 1590 | typeArgsRAngleLoc = rAngleLoc; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1591 | }; |
| 1592 | |
| 1593 | // If all of the identifiers can be resolved as type names or |
| 1594 | // Objective-C class names, we have type arguments. |
| 1595 | if (numTypeDeclsResolved == identifiers.size()) |
| 1596 | return resolvedAsTypeDecls(); |
| 1597 | |
| 1598 | // Error recovery: some names weren't found, or we have a mix of |
| 1599 | // type and protocol names. Go resolve all of the unresolved names |
| 1600 | // and complain if we can't find a consistent answer. |
| 1601 | LookupNameKind lookupKind = LookupAnyName; |
| 1602 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1603 | // If we already have a protocol or type. Check whether it is the |
| 1604 | // right thing. |
| 1605 | if (protocols[i] || typeDecls[i]) { |
| 1606 | // If we haven't figured out whether we want types or protocols |
| 1607 | // yet, try to figure it out from this name. |
| 1608 | if (lookupKind == LookupAnyName) { |
| 1609 | // If this name refers to both a protocol and a type (e.g., \c |
| 1610 | // NSObject), don't conclude anything yet. |
| 1611 | if (protocols[i] && typeDecls[i]) |
| 1612 | continue; |
| 1613 | |
| 1614 | // Otherwise, let this name decide whether we'll be correcting |
| 1615 | // toward types or protocols. |
| 1616 | lookupKind = protocols[i] ? LookupObjCProtocolName |
| 1617 | : LookupOrdinaryName; |
| 1618 | continue; |
| 1619 | } |
| 1620 | |
| 1621 | // If we want protocols and we have a protocol, there's nothing |
| 1622 | // more to do. |
| 1623 | if (lookupKind == LookupObjCProtocolName && protocols[i]) |
| 1624 | continue; |
| 1625 | |
| 1626 | // If we want types and we have a type declaration, there's |
| 1627 | // nothing more to do. |
| 1628 | if (lookupKind == LookupOrdinaryName && typeDecls[i]) |
| 1629 | continue; |
| 1630 | |
| 1631 | // We have a conflict: some names refer to protocols and others |
| 1632 | // refer to types. |
Bruno Cardoso Lopes | c54768f | 2016-04-13 20:59:07 +0000 | [diff] [blame] | 1633 | DiagnoseTypeArgsAndProtocols(identifiers[0], identifierLocs[0], |
| 1634 | identifiers[i], identifierLocs[i], |
| 1635 | protocols[i] != nullptr); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1636 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1637 | protocols.clear(); |
| 1638 | typeArgs.clear(); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1639 | return; |
| 1640 | } |
| 1641 | |
| 1642 | // Perform typo correction on the name. |
| 1643 | TypoCorrection corrected = CorrectTypo( |
| 1644 | DeclarationNameInfo(identifiers[i], identifierLocs[i]), lookupKind, S, |
| 1645 | nullptr, |
| 1646 | llvm::make_unique<ObjCTypeArgOrProtocolValidatorCCC>(Context, |
| 1647 | lookupKind), |
| 1648 | CTK_ErrorRecovery); |
| 1649 | if (corrected) { |
| 1650 | // Did we find a protocol? |
| 1651 | if (auto proto = corrected.getCorrectionDeclAs<ObjCProtocolDecl>()) { |
| 1652 | diagnoseTypo(corrected, |
| 1653 | PDiag(diag::err_undeclared_protocol_suggest) |
| 1654 | << identifiers[i]); |
| 1655 | lookupKind = LookupObjCProtocolName; |
| 1656 | protocols[i] = proto; |
| 1657 | ++numProtocolsResolved; |
| 1658 | continue; |
| 1659 | } |
| 1660 | |
| 1661 | // Did we find a type? |
| 1662 | if (auto typeDecl = corrected.getCorrectionDeclAs<TypeDecl>()) { |
| 1663 | diagnoseTypo(corrected, |
| 1664 | PDiag(diag::err_unknown_typename_suggest) |
| 1665 | << identifiers[i]); |
| 1666 | lookupKind = LookupOrdinaryName; |
| 1667 | typeDecls[i] = typeDecl; |
| 1668 | ++numTypeDeclsResolved; |
| 1669 | continue; |
| 1670 | } |
| 1671 | |
| 1672 | // Did we find an Objective-C class? |
| 1673 | if (auto objcClass = corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
| 1674 | diagnoseTypo(corrected, |
| 1675 | PDiag(diag::err_unknown_type_or_class_name_suggest) |
| 1676 | << identifiers[i] << true); |
| 1677 | lookupKind = LookupOrdinaryName; |
| 1678 | typeDecls[i] = objcClass; |
| 1679 | ++numTypeDeclsResolved; |
| 1680 | continue; |
| 1681 | } |
| 1682 | } |
| 1683 | |
| 1684 | // We couldn't find anything. |
| 1685 | Diag(identifierLocs[i], |
| 1686 | (lookupKind == LookupAnyName ? diag::err_objc_type_arg_missing |
| 1687 | : lookupKind == LookupObjCProtocolName ? diag::err_undeclared_protocol |
| 1688 | : diag::err_unknown_typename)) |
| 1689 | << identifiers[i]; |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1690 | protocols.clear(); |
| 1691 | typeArgs.clear(); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1692 | return; |
| 1693 | } |
| 1694 | |
| 1695 | // If all of the names were (corrected to) protocols, these were |
| 1696 | // protocol qualifiers. |
| 1697 | if (numProtocolsResolved == identifiers.size()) |
| 1698 | return resolvedAsProtocols(); |
| 1699 | |
| 1700 | // Otherwise, all of the names were (corrected to) types. |
| 1701 | assert(numTypeDeclsResolved == identifiers.size() && "Not all types?"); |
| 1702 | return resolvedAsTypeDecls(); |
| 1703 | } |
| 1704 | |
Fariborz Jahanian | abf63e7b | 2009-03-02 19:06:08 +0000 | [diff] [blame] | 1705 | /// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1706 | /// a class method in its extension. |
| 1707 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1708 | void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1709 | ObjCInterfaceDecl *ID) { |
| 1710 | if (!ID) |
| 1711 | return; // Possibly due to previous error |
| 1712 | |
| 1713 | llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap; |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 1714 | for (auto *MD : ID->methods()) |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1715 | MethodMap[MD->getSelector()] = MD; |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1716 | |
| 1717 | if (MethodMap.empty()) |
| 1718 | return; |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 1719 | for (const auto *Method : CAT->methods()) { |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1720 | const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()]; |
Fariborz Jahanian | 83d674e | 2014-03-17 17:46:10 +0000 | [diff] [blame] | 1721 | if (PrevMethod && |
| 1722 | (PrevMethod->isInstanceMethod() == Method->isInstanceMethod()) && |
| 1723 | !MatchTwoMethodDeclarations(Method, PrevMethod)) { |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1724 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
| 1725 | << Method->getDeclName(); |
| 1726 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 1727 | } |
| 1728 | } |
| 1729 | } |
| 1730 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 1731 | /// ActOnForwardProtocolDeclaration - Handle \@protocol foo; |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1732 | Sema::DeclGroupPtrTy |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1733 | Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
Craig Topper | 0f723bb | 2015-10-22 05:00:01 +0000 | [diff] [blame] | 1734 | ArrayRef<IdentifierLocPair> IdentList, |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1735 | AttributeList *attrList) { |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1736 | SmallVector<Decl *, 8> DeclsInGroup; |
Craig Topper | 0f723bb | 2015-10-22 05:00:01 +0000 | [diff] [blame] | 1737 | for (const IdentifierLocPair &IdentPair : IdentList) { |
| 1738 | IdentifierInfo *Ident = IdentPair.first; |
| 1739 | ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentPair.second, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1740 | forRedeclarationInCurContext()); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1741 | ObjCProtocolDecl *PDecl |
| 1742 | = ObjCProtocolDecl::Create(Context, CurContext, Ident, |
Craig Topper | 0f723bb | 2015-10-22 05:00:01 +0000 | [diff] [blame] | 1743 | IdentPair.second, AtProtocolLoc, |
Douglas Gregor | 05a1f4d | 2012-01-01 22:06:18 +0000 | [diff] [blame] | 1744 | PrevDecl); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1745 | |
| 1746 | PushOnScopeChains(PDecl, TUScope); |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1747 | CheckObjCDeclScope(PDecl); |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1748 | |
Douglas Gregor | 42ff1bb | 2012-01-01 20:33:24 +0000 | [diff] [blame] | 1749 | if (attrList) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1750 | ProcessDeclAttributeList(TUScope, PDecl, attrList); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1751 | AddPragmaAttributes(TUScope, PDecl); |
| 1752 | |
Douglas Gregor | 32c1757 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 1753 | if (PrevDecl) |
| 1754 | mergeDeclAttributes(PDecl, PrevDecl); |
| 1755 | |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 1756 | DeclsInGroup.push_back(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1757 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1758 | |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1759 | return BuildDeclaratorGroup(DeclsInGroup); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1762 | Decl *Sema:: |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1763 | ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc, |
| 1764 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1765 | ObjCTypeParamList *typeParamList, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1766 | IdentifierInfo *CategoryName, |
| 1767 | SourceLocation CategoryLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1768 | Decl * const *ProtoRefs, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 1769 | unsigned NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 1770 | const SourceLocation *ProtoLocs, |
Alex Lorenz | f937139 | 2017-03-23 11:44:25 +0000 | [diff] [blame] | 1771 | SourceLocation EndProtoLoc, |
| 1772 | AttributeList *AttrList) { |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1773 | ObjCCategoryDecl *CDecl; |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1774 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1775 | |
| 1776 | /// Check that class of this category is already completely declared. |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1777 | |
| 1778 | if (!IDecl |
| 1779 | || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1780 | diag::err_category_forward_interface, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1781 | CategoryName == nullptr)) { |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1782 | // Create an invalid ObjCCategoryDecl to serve as context for |
| 1783 | // the enclosing method declarations. We mark the decl invalid |
| 1784 | // to make it clear that this isn't a valid AST. |
| 1785 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1786 | ClassLoc, CategoryLoc, CategoryName, |
| 1787 | IDecl, typeParamList); |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1788 | CDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | b15def2 | 2012-03-12 18:34:26 +0000 | [diff] [blame] | 1789 | CurContext->addDecl(CDecl); |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1790 | |
| 1791 | if (!IDecl) |
| 1792 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1793 | return ActOnObjCContainerStartDefinition(CDecl); |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1796 | if (!CategoryName && IDecl->getImplementation()) { |
| 1797 | Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName; |
| 1798 | Diag(IDecl->getImplementation()->getLocation(), |
| 1799 | diag::note_implementation_declared); |
Ted Kremenek | 514ff70 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1802 | if (CategoryName) { |
| 1803 | /// Check for duplicate interface declaration for this category |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1804 | if (ObjCCategoryDecl *Previous |
| 1805 | = IDecl->FindCategoryDeclaration(CategoryName)) { |
| 1806 | // Class extensions can be declared multiple times, categories cannot. |
| 1807 | Diag(CategoryLoc, diag::warn_dup_category_def) |
| 1808 | << ClassName << CategoryName; |
| 1809 | Diag(Previous->getLocation(), diag::note_previous_definition); |
Chris Lattner | 9018ca8 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 1810 | } |
| 1811 | } |
Chris Lattner | 9018ca8 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 1812 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1813 | // If we have a type parameter list, check it. |
| 1814 | if (typeParamList) { |
| 1815 | if (auto prevTypeParamList = IDecl->getTypeParamList()) { |
| 1816 | if (checkTypeParamListConsistency(*this, prevTypeParamList, typeParamList, |
| 1817 | CategoryName |
| 1818 | ? TypeParamListContext::Category |
| 1819 | : TypeParamListContext::Extension)) |
| 1820 | typeParamList = nullptr; |
| 1821 | } else { |
| 1822 | Diag(typeParamList->getLAngleLoc(), |
| 1823 | diag::err_objc_parameterized_category_nonclass) |
| 1824 | << (CategoryName != nullptr) |
| 1825 | << ClassName |
| 1826 | << typeParamList->getSourceRange(); |
| 1827 | |
| 1828 | typeParamList = nullptr; |
| 1829 | } |
| 1830 | } |
| 1831 | |
Argyrios Kyrtzidis | 3a5094b | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 1832 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1833 | ClassLoc, CategoryLoc, CategoryName, IDecl, |
| 1834 | typeParamList); |
Argyrios Kyrtzidis | 3a5094b | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 1835 | // FIXME: PushOnScopeChains? |
| 1836 | CurContext->addDecl(CDecl); |
| 1837 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1838 | if (NumProtoRefs) { |
Argyrios Kyrtzidis | 4ecdd2c | 2015-04-19 20:15:55 +0000 | [diff] [blame] | 1839 | diagnoseUseOfProtocols(*this, CDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
| 1840 | NumProtoRefs, ProtoLocs); |
| 1841 | CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 1842 | ProtoLocs, Context); |
Fariborz Jahanian | 092cd6e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 1843 | // Protocols in the class extension belong to the class. |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1844 | if (CDecl->IsClassExtension()) |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 1845 | IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs, |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1846 | NumProtoRefs, Context); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1847 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1848 | |
Alex Lorenz | f937139 | 2017-03-23 11:44:25 +0000 | [diff] [blame] | 1849 | if (AttrList) |
| 1850 | ProcessDeclAttributeList(TUScope, CDecl, AttrList); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1851 | AddPragmaAttributes(TUScope, CDecl); |
Alex Lorenz | f937139 | 2017-03-23 11:44:25 +0000 | [diff] [blame] | 1852 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1853 | CheckObjCDeclScope(CDecl); |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1854 | return ActOnObjCContainerStartDefinition(CDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | /// ActOnStartCategoryImplementation - Perform semantic checks on the |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1858 | /// category implementation declaration and build an ObjCCategoryImplDecl |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1859 | /// object. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1860 | Decl *Sema::ActOnStartCategoryImplementation( |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1861 | SourceLocation AtCatImplLoc, |
| 1862 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 1863 | IdentifierInfo *CatName, SourceLocation CatLoc) { |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1864 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1865 | ObjCCategoryDecl *CatIDecl = nullptr; |
Argyrios Kyrtzidis | 4af2cb3 | 2012-03-02 19:14:29 +0000 | [diff] [blame] | 1866 | if (IDecl && IDecl->hasDefinition()) { |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1867 | CatIDecl = IDecl->FindCategoryDeclaration(CatName); |
| 1868 | if (!CatIDecl) { |
| 1869 | // Category @implementation with no corresponding @interface. |
| 1870 | // Create and install one. |
Argyrios Kyrtzidis | 41fc05c | 2011-11-23 20:27:26 +0000 | [diff] [blame] | 1871 | CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc, |
| 1872 | ClassLoc, CatLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1873 | CatName, IDecl, |
| 1874 | /*typeParamList=*/nullptr); |
Argyrios Kyrtzidis | 41fc05c | 2011-11-23 20:27:26 +0000 | [diff] [blame] | 1875 | CatIDecl->setImplicit(); |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1876 | } |
| 1877 | } |
| 1878 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | ObjCCategoryImplDecl *CDecl = |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 1880 | ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl, |
Argyrios Kyrtzidis | 4996f5f | 2011-12-09 00:31:40 +0000 | [diff] [blame] | 1881 | ClassLoc, AtCatImplLoc, CatLoc); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1882 | /// Check that class of this category is already completely declared. |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1883 | if (!IDecl) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1884 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 1885 | CDecl->setInvalidDecl(); |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1886 | } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
| 1887 | diag::err_undef_interface)) { |
| 1888 | CDecl->setInvalidDecl(); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 1889 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1890 | |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1891 | // FIXME: PushOnScopeChains? |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1892 | CurContext->addDecl(CDecl); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1893 | |
Douglas Gregor | 24ae22c | 2016-04-01 23:23:52 +0000 | [diff] [blame] | 1894 | // If the interface has the objc_runtime_visible attribute, we |
| 1895 | // cannot implement a category for it. |
| 1896 | if (IDecl && IDecl->hasAttr<ObjCRuntimeVisibleAttr>()) { |
| 1897 | Diag(ClassLoc, diag::err_objc_runtime_visible_category) |
| 1898 | << IDecl->getDeclName(); |
| 1899 | } |
| 1900 | |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1901 | /// Check that CatName, category name, is not used in another implementation. |
| 1902 | if (CatIDecl) { |
| 1903 | if (CatIDecl->getImplementation()) { |
| 1904 | Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName |
| 1905 | << CatName; |
| 1906 | Diag(CatIDecl->getImplementation()->getLocation(), |
| 1907 | diag::note_previous_definition); |
Argyrios Kyrtzidis | 0f6d5ca | 2013-05-30 18:53:21 +0000 | [diff] [blame] | 1908 | CDecl->setInvalidDecl(); |
Fariborz Jahanian | d33ab8c | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 1909 | } else { |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1910 | CatIDecl->setImplementation(CDecl); |
Fariborz Jahanian | d33ab8c | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 1911 | // Warn on implementating category of deprecated class under |
| 1912 | // -Wdeprecated-implementations flag. |
Alex Lorenz | f81d97e | 2017-07-13 16:35:59 +0000 | [diff] [blame] | 1913 | DiagnoseObjCImplementedDeprecations(*this, CatIDecl, |
| 1914 | CDecl->getLocation()); |
Fariborz Jahanian | d33ab8c | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 1915 | } |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1916 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1917 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1918 | CheckObjCDeclScope(CDecl); |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1919 | return ActOnObjCContainerStartDefinition(CDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1922 | Decl *Sema::ActOnStartClassImplementation( |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1923 | SourceLocation AtClassImplLoc, |
| 1924 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1925 | IdentifierInfo *SuperClassname, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1926 | SourceLocation SuperClassLoc) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1927 | ObjCInterfaceDecl *IDecl = nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1928 | // Check for another declaration kind with the same name. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1929 | NamedDecl *PrevDecl |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 1930 | = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1931 | forRedeclarationInCurContext()); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1932 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1933 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1934 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1935 | } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) { |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 1936 | // FIXME: This will produce an error if the definition of the interface has |
| 1937 | // been imported from a module but is not visible. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 1938 | RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
| 1939 | diag::warn_undef_interface); |
Douglas Gregor | 40f7a00 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 1940 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1941 | // 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] | 1942 | // typos in the class name. |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 1943 | TypoCorrection Corrected = CorrectTypo( |
| 1944 | DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope, |
| 1945 | nullptr, llvm::make_unique<ObjCInterfaceValidatorCCC>(), CTK_NonError); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1946 | if (Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
| 1947 | // Suggest the (potentially) correct interface name. Don't provide a |
| 1948 | // code-modification hint or use the typo name for recovery, because |
| 1949 | // this is just a warning. The program may actually be correct. |
| 1950 | diagnoseTypo(Corrected, |
| 1951 | PDiag(diag::warn_undef_interface_suggest) << ClassName, |
| 1952 | /*ErrorRecovery*/false); |
Douglas Gregor | 40f7a00 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 1953 | } else { |
| 1954 | Diag(ClassLoc, diag::warn_undef_interface) << ClassName; |
| 1955 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1956 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1957 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1958 | // Check that super class name is valid class name |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1959 | ObjCInterfaceDecl *SDecl = nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1960 | if (SuperClassname) { |
| 1961 | // Check if a different kind of symbol declared in this scope. |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1962 | PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc, |
| 1963 | LookupOrdinaryName); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1964 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1965 | Diag(SuperClassLoc, diag::err_redefinition_different_kind) |
| 1966 | << SuperClassname; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1967 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1968 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1969 | SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Argyrios Kyrtzidis | 3b60cff | 2012-03-13 01:09:36 +0000 | [diff] [blame] | 1970 | if (SDecl && !SDecl->hasDefinition()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1971 | SDecl = nullptr; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1972 | if (!SDecl) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1973 | Diag(SuperClassLoc, diag::err_undef_superclass) |
| 1974 | << SuperClassname << ClassName; |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 1975 | else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1976 | // This implementation and its interface do not have the same |
| 1977 | // super class. |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1978 | Diag(SuperClassLoc, diag::err_conflicting_super_class) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1979 | << SDecl->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1980 | Diag(SDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1981 | } |
| 1982 | } |
| 1983 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1984 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1985 | if (!IDecl) { |
| 1986 | // Legacy case of @implementation with no corresponding @interface. |
| 1987 | // Build, chain & install the interface decl into the identifier. |
Daniel Dunbar | 73a73f5 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 1988 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1989 | // FIXME: Do we support attributes on the @implementation? If so we should |
| 1990 | // copy them over. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1991 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 1992 | ClassName, /*typeParamList=*/nullptr, |
| 1993 | /*PrevDecl=*/nullptr, ClassLoc, |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 1994 | true); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1995 | AddPragmaAttributes(TUScope, IDecl); |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 1996 | IDecl->startDefinition(); |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 1997 | if (SDecl) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1998 | IDecl->setSuperClass(Context.getTrivialTypeSourceInfo( |
| 1999 | Context.getObjCInterfaceType(SDecl), |
| 2000 | SuperClassLoc)); |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 2001 | IDecl->setEndOfDefinitionLoc(SuperClassLoc); |
| 2002 | } else { |
| 2003 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
| 2004 | } |
| 2005 | |
Douglas Gregor | ac345a3 | 2009-04-24 00:16:12 +0000 | [diff] [blame] | 2006 | PushOnScopeChains(IDecl, TUScope); |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2007 | } else { |
| 2008 | // Mark the interface as being completed, even if it was just as |
| 2009 | // @class ....; |
| 2010 | // declaration; the user cannot reopen it. |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 2011 | if (!IDecl->hasDefinition()) |
| 2012 | IDecl->startDefinition(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2013 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2014 | |
| 2015 | ObjCImplementationDecl* IMPDecl = |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 2016 | ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl, |
Argyrios Kyrtzidis | fac3162 | 2013-05-03 18:05:44 +0000 | [diff] [blame] | 2017 | ClassLoc, AtClassImplLoc, SuperClassLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 2019 | if (CheckObjCDeclScope(IMPDecl)) |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 2020 | return ActOnObjCContainerStartDefinition(IMPDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2021 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2022 | // Check that there is no duplicate implementation of this class. |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2023 | if (IDecl->getImplementation()) { |
| 2024 | // FIXME: Don't leak everything! |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2025 | Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; |
Argyrios Kyrtzidis | 43cee935 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 2026 | Diag(IDecl->getImplementation()->getLocation(), |
| 2027 | diag::note_previous_definition); |
Argyrios Kyrtzidis | 0f6d5ca | 2013-05-30 18:53:21 +0000 | [diff] [blame] | 2028 | IMPDecl->setInvalidDecl(); |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 2029 | } else { // add it to the list. |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2030 | IDecl->setImplementation(IMPDecl); |
Douglas Gregor | 79947a2 | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 2031 | PushOnScopeChains(IMPDecl, TUScope); |
Fariborz Jahanian | d33ab8c | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 2032 | // Warn on implementating deprecated class under |
| 2033 | // -Wdeprecated-implementations flag. |
Alex Lorenz | f81d97e | 2017-07-13 16:35:59 +0000 | [diff] [blame] | 2034 | DiagnoseObjCImplementedDeprecations(*this, IDecl, IMPDecl->getLocation()); |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2035 | } |
Douglas Gregor | 24ae22c | 2016-04-01 23:23:52 +0000 | [diff] [blame] | 2036 | |
| 2037 | // If the superclass has the objc_runtime_visible attribute, we |
| 2038 | // cannot implement a subclass of it. |
| 2039 | if (IDecl->getSuperClass() && |
| 2040 | IDecl->getSuperClass()->hasAttr<ObjCRuntimeVisibleAttr>()) { |
| 2041 | Diag(ClassLoc, diag::err_objc_runtime_visible_subclass) |
| 2042 | << IDecl->getDeclName() |
| 2043 | << IDecl->getSuperClass()->getDeclName(); |
| 2044 | } |
| 2045 | |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 2046 | return ActOnObjCContainerStartDefinition(IMPDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
Argyrios Kyrtzidis | 2e85c5f | 2012-02-23 21:11:20 +0000 | [diff] [blame] | 2049 | Sema::DeclGroupPtrTy |
| 2050 | Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) { |
| 2051 | SmallVector<Decl *, 64> DeclsInGroup; |
| 2052 | DeclsInGroup.reserve(Decls.size() + 1); |
| 2053 | |
| 2054 | for (unsigned i = 0, e = Decls.size(); i != e; ++i) { |
| 2055 | Decl *Dcl = Decls[i]; |
| 2056 | if (!Dcl) |
| 2057 | continue; |
| 2058 | if (Dcl->getDeclContext()->isFileContext()) |
| 2059 | Dcl->setTopLevelDeclInObjCContainer(); |
| 2060 | DeclsInGroup.push_back(Dcl); |
| 2061 | } |
| 2062 | |
| 2063 | DeclsInGroup.push_back(ObjCImpDecl); |
| 2064 | |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2065 | return BuildDeclaratorGroup(DeclsInGroup); |
Argyrios Kyrtzidis | 2e85c5f | 2012-02-23 21:11:20 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2068 | void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, |
| 2069 | ObjCIvarDecl **ivars, unsigned numIvars, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2070 | SourceLocation RBrace) { |
| 2071 | assert(ImpDecl && "missing implementation decl"); |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2072 | ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2073 | if (!IDecl) |
| 2074 | return; |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2075 | /// Check case of non-existing \@interface decl. |
| 2076 | /// (legacy objective-c \@implementation decl without an \@interface decl). |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2077 | /// Add implementations's ivar to the synthesize class's ivar list. |
Steve Naroff | aac654a | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 2078 | if (IDecl->isImplicitInterfaceDecl()) { |
Douglas Gregor | 1640832 | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 2079 | IDecl->setEndOfDefinitionLoc(RBrace); |
Fariborz Jahanian | 20912d6 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 2080 | // Add ivar's to class's DeclContext. |
| 2081 | for (unsigned i = 0, e = numIvars; i != e; ++i) { |
Fariborz Jahanian | c0309cd | 2010-02-17 18:10:54 +0000 | [diff] [blame] | 2082 | ivars[i]->setLexicalDeclContext(ImpDecl); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 2083 | IDecl->makeDeclVisibleInContext(ivars[i]); |
Fariborz Jahanian | aef6622 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 2084 | ImpDecl->addDecl(ivars[i]); |
Fariborz Jahanian | 20912d6 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2087 | return; |
| 2088 | } |
| 2089 | // If implementation has empty ivar list, just return. |
| 2090 | if (numIvars == 0) |
| 2091 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2093 | assert(ivars && "missing @implementation ivars"); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2094 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | 34e3cef | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 2095 | if (ImpDecl->getSuperClass()) |
| 2096 | Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use); |
| 2097 | for (unsigned i = 0; i < numIvars; i++) { |
| 2098 | ObjCIvarDecl* ImplIvar = ivars[i]; |
| 2099 | if (const ObjCIvarDecl *ClsIvar = |
| 2100 | IDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
| 2101 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
| 2102 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
| 2103 | continue; |
| 2104 | } |
Fariborz Jahanian | e23f26b | 2013-06-26 22:10:27 +0000 | [diff] [blame] | 2105 | // Check class extensions (unnamed categories) for duplicate ivars. |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 2106 | for (const auto *CDecl : IDecl->visible_extensions()) { |
Fariborz Jahanian | e23f26b | 2013-06-26 22:10:27 +0000 | [diff] [blame] | 2107 | if (const ObjCIvarDecl *ClsExtIvar = |
| 2108 | CDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
| 2109 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
| 2110 | Diag(ClsExtIvar->getLocation(), diag::note_previous_definition); |
| 2111 | continue; |
| 2112 | } |
| 2113 | } |
Fariborz Jahanian | 34e3cef | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 2114 | // Instance ivar to Implementation's DeclContext. |
| 2115 | ImplIvar->setLexicalDeclContext(ImpDecl); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 2116 | IDecl->makeDeclVisibleInContext(ImplIvar); |
Fariborz Jahanian | 34e3cef | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 2117 | ImpDecl->addDecl(ImplIvar); |
| 2118 | } |
| 2119 | return; |
| 2120 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2121 | // Check interface's Ivar list against those in the implementation. |
| 2122 | // names and types must match. |
| 2123 | // |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2124 | unsigned j = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2125 | ObjCInterfaceDecl::ivar_iterator |
Chris Lattner | 061227a | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 2126 | IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); |
| 2127 | for (; numIvars > 0 && IVI != IVE; ++IVI) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2128 | ObjCIvarDecl* ImplIvar = ivars[j++]; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2129 | ObjCIvarDecl* ClsIvar = *IVI; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2130 | assert (ImplIvar && "missing implementation ivar"); |
| 2131 | assert (ClsIvar && "missing class ivar"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2132 | |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 2133 | // First, make sure the types match. |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2134 | if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2135 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2136 | << ImplIvar->getIdentifier() |
| 2137 | << ImplIvar->getType() << ClsIvar->getType(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2138 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2139 | } else if (ImplIvar->isBitField() && ClsIvar->isBitField() && |
| 2140 | ImplIvar->getBitWidthValue(Context) != |
| 2141 | ClsIvar->getBitWidthValue(Context)) { |
| 2142 | Diag(ImplIvar->getBitWidth()->getLocStart(), |
| 2143 | diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier(); |
| 2144 | Diag(ClsIvar->getBitWidth()->getLocStart(), |
| 2145 | diag::note_previous_definition); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2146 | } |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 2147 | // Make sure the names are identical. |
| 2148 | if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2149 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2150 | << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2151 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2152 | } |
| 2153 | --numIvars; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2154 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2155 | |
Chris Lattner | 0f29d98 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 2156 | if (numIvars > 0) |
Alp Toker | fff0674 | 2013-12-02 03:50:21 +0000 | [diff] [blame] | 2157 | Diag(ivars[j]->getLocation(), diag::err_inconsistent_ivar_count); |
Chris Lattner | 0f29d98 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 2158 | else if (IVI != IVE) |
Alp Toker | fff0674 | 2013-12-02 03:50:21 +0000 | [diff] [blame] | 2159 | Diag(IVI->getLocation(), diag::err_inconsistent_ivar_count); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
Ted Kremenek | f87decd | 2013-12-13 05:58:44 +0000 | [diff] [blame] | 2162 | static void WarnUndefinedMethod(Sema &S, SourceLocation ImpLoc, |
| 2163 | ObjCMethodDecl *method, |
| 2164 | bool &IncompleteImpl, |
Ted Kremenek | 2ccf19e | 2013-12-13 05:58:51 +0000 | [diff] [blame] | 2165 | unsigned DiagID, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2166 | NamedDecl *NeededFor = nullptr) { |
Fariborz Jahanian | 9fc39c4 | 2011-06-24 20:31:37 +0000 | [diff] [blame] | 2167 | // No point warning no definition of method which is 'unavailable'. |
Douglas Gregor | c2e3d5c | 2012-12-11 18:53:07 +0000 | [diff] [blame] | 2168 | switch (method->getAvailability()) { |
| 2169 | case AR_Available: |
| 2170 | case AR_Deprecated: |
| 2171 | break; |
| 2172 | |
| 2173 | // Don't warn about unavailable or not-yet-introduced methods. |
| 2174 | case AR_NotYetIntroduced: |
| 2175 | case AR_Unavailable: |
Fariborz Jahanian | 9fc39c4 | 2011-06-24 20:31:37 +0000 | [diff] [blame] | 2176 | return; |
Douglas Gregor | c2e3d5c | 2012-12-11 18:53:07 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
Ted Kremenek | 65d6357 | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 2179 | // FIXME: For now ignore 'IncompleteImpl'. |
| 2180 | // Previously we grouped all unimplemented methods under a single |
| 2181 | // warning, but some users strongly voiced that they would prefer |
| 2182 | // separate warnings. We will give that approach a try, as that |
| 2183 | // matches what we do with protocols. |
Ted Kremenek | 2ccf19e | 2013-12-13 05:58:51 +0000 | [diff] [blame] | 2184 | { |
| 2185 | const Sema::SemaDiagnosticBuilder &B = S.Diag(ImpLoc, DiagID); |
| 2186 | B << method; |
| 2187 | if (NeededFor) |
| 2188 | B << NeededFor; |
| 2189 | } |
Ted Kremenek | 65d6357 | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 2190 | |
| 2191 | // Issue a note to the original declaration. |
| 2192 | SourceLocation MethodLoc = method->getLocStart(); |
| 2193 | if (MethodLoc.isValid()) |
Ted Kremenek | f87decd | 2013-12-13 05:58:44 +0000 | [diff] [blame] | 2194 | S.Diag(MethodLoc, diag::note_method_declared_at) << method; |
Steve Naroff | 15833ed | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 2195 | } |
| 2196 | |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2197 | /// Determines if type B can be substituted for type A. Returns true if we can |
| 2198 | /// guarantee that anything that the user will do to an object of type A can |
| 2199 | /// also be done to an object of type B. This is trivially true if the two |
| 2200 | /// types are the same, or if B is a subclass of A. It becomes more complex |
| 2201 | /// in cases where protocols are involved. |
| 2202 | /// |
| 2203 | /// Object types in Objective-C describe the minimum requirements for an |
| 2204 | /// object, rather than providing a complete description of a type. For |
| 2205 | /// example, if A is a subclass of B, then B* may refer to an instance of A. |
| 2206 | /// The principle of substitutability means that we may use an instance of A |
| 2207 | /// anywhere that we may use an instance of B - it will implement all of the |
| 2208 | /// ivars of B and all of the methods of B. |
| 2209 | /// |
| 2210 | /// This substitutability is important when type checking methods, because |
| 2211 | /// the implementation may have stricter type definitions than the interface. |
| 2212 | /// The interface specifies minimum requirements, but the implementation may |
| 2213 | /// have more accurate ones. For example, a method may privately accept |
| 2214 | /// instances of B, but only publish that it accepts instances of A. Any |
| 2215 | /// object passed to it will be type checked against B, and so will implicitly |
| 2216 | /// by a valid A*. Similarly, a method may return a subclass of the class that |
| 2217 | /// it is declared as returning. |
| 2218 | /// |
| 2219 | /// This is most important when considering subclassing. A method in a |
| 2220 | /// subclass must accept any object as an argument that its superclass's |
| 2221 | /// implementation accepts. It may, however, accept a more general type |
| 2222 | /// without breaking substitutability (i.e. you can still use the subclass |
| 2223 | /// anywhere that you can use the superclass, but not vice versa). The |
| 2224 | /// converse requirement applies to return types: the return type for a |
| 2225 | /// subclass method must be a valid object of the kind that the superclass |
| 2226 | /// advertises, but it may be specified more accurately. This avoids the need |
| 2227 | /// for explicit down-casting by callers. |
| 2228 | /// |
| 2229 | /// Note: This is a stricter requirement than for assignment. |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2230 | static bool isObjCTypeSubstitutable(ASTContext &Context, |
| 2231 | const ObjCObjectPointerType *A, |
| 2232 | const ObjCObjectPointerType *B, |
| 2233 | bool rejectId) { |
| 2234 | // Reject a protocol-unqualified id. |
| 2235 | if (rejectId && B->isObjCIdType()) return false; |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2236 | |
| 2237 | // If B is a qualified id, then A must also be a qualified id and it must |
| 2238 | // implement all of the protocols in B. It may not be a qualified class. |
| 2239 | // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a |
| 2240 | // stricter definition so it is not substitutable for id<A>. |
| 2241 | if (B->isObjCQualifiedIdType()) { |
| 2242 | return A->isObjCQualifiedIdType() && |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2243 | Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0), |
| 2244 | QualType(B,0), |
| 2245 | false); |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | /* |
| 2249 | // id is a special type that bypasses type checking completely. We want a |
| 2250 | // warning when it is used in one place but not another. |
| 2251 | if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false; |
| 2252 | |
| 2253 | |
| 2254 | // If B is a qualified id, then A must also be a qualified id (which it isn't |
| 2255 | // if we've got this far) |
| 2256 | if (B->isObjCQualifiedIdType()) return false; |
| 2257 | */ |
| 2258 | |
| 2259 | // Now we know that A and B are (potentially-qualified) class types. The |
| 2260 | // normal rules for assignment apply. |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2261 | return Context.canAssignObjCInterfaces(A, B); |
David Chisnall | b62d15c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2264 | static SourceRange getTypeRange(TypeSourceInfo *TSI) { |
| 2265 | return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange()); |
| 2266 | } |
| 2267 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2268 | /// Determine whether two set of Objective-C declaration qualifiers conflict. |
| 2269 | static bool objcModifiersConflict(Decl::ObjCDeclQualifier x, |
| 2270 | Decl::ObjCDeclQualifier y) { |
| 2271 | return (x & ~Decl::OBJC_TQ_CSNullability) != |
| 2272 | (y & ~Decl::OBJC_TQ_CSNullability); |
| 2273 | } |
| 2274 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2275 | static bool CheckMethodOverrideReturn(Sema &S, |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2276 | ObjCMethodDecl *MethodImpl, |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2277 | ObjCMethodDecl *MethodDecl, |
Fariborz Jahanian | 4ceec3f | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 2278 | bool IsProtocolMethodDecl, |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2279 | bool IsOverridingMode, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2280 | bool Warn) { |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2281 | if (IsProtocolMethodDecl && |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2282 | objcModifiersConflict(MethodDecl->getObjCDeclQualifier(), |
| 2283 | MethodImpl->getObjCDeclQualifier())) { |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2284 | if (Warn) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2285 | S.Diag(MethodImpl->getLocation(), |
| 2286 | (IsOverridingMode |
| 2287 | ? diag::warn_conflicting_overriding_ret_type_modifiers |
| 2288 | : diag::warn_conflicting_ret_type_modifiers)) |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2289 | << MethodImpl->getDeclName() |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 2290 | << MethodImpl->getReturnTypeSourceRange(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2291 | S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration) |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 2292 | << MethodDecl->getReturnTypeSourceRange(); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2293 | } |
| 2294 | else |
| 2295 | return false; |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2296 | } |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2297 | if (Warn && IsOverridingMode && |
| 2298 | !isa<ObjCImplementationDecl>(MethodImpl->getDeclContext()) && |
| 2299 | !S.Context.hasSameNullabilityTypeQualifier(MethodImpl->getReturnType(), |
| 2300 | MethodDecl->getReturnType(), |
| 2301 | false)) { |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 2302 | auto nullabilityMethodImpl = |
| 2303 | *MethodImpl->getReturnType()->getNullability(S.Context); |
| 2304 | auto nullabilityMethodDecl = |
| 2305 | *MethodDecl->getReturnType()->getNullability(S.Context); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2306 | S.Diag(MethodImpl->getLocation(), |
| 2307 | diag::warn_conflicting_nullability_attr_overriding_ret_types) |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 2308 | << DiagNullabilityKind( |
| 2309 | nullabilityMethodImpl, |
| 2310 | ((MethodImpl->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2311 | != 0)) |
| 2312 | << DiagNullabilityKind( |
| 2313 | nullabilityMethodDecl, |
| 2314 | ((MethodDecl->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2315 | != 0)); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2316 | S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration); |
| 2317 | } |
| 2318 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2319 | if (S.Context.hasSameUnqualifiedType(MethodImpl->getReturnType(), |
| 2320 | MethodDecl->getReturnType())) |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2321 | return true; |
| 2322 | if (!Warn) |
| 2323 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2324 | |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2325 | unsigned DiagID = |
| 2326 | IsOverridingMode ? diag::warn_conflicting_overriding_ret_types |
| 2327 | : diag::warn_conflicting_ret_types; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2328 | |
| 2329 | // Mismatches between ObjC pointers go into a different warning |
| 2330 | // category, and sometimes they're even completely whitelisted. |
| 2331 | if (const ObjCObjectPointerType *ImplPtrTy = |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2332 | MethodImpl->getReturnType()->getAs<ObjCObjectPointerType>()) { |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2333 | if (const ObjCObjectPointerType *IfacePtrTy = |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2334 | MethodDecl->getReturnType()->getAs<ObjCObjectPointerType>()) { |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2335 | // Allow non-matching return types as long as they don't violate |
| 2336 | // the principle of substitutability. Specifically, we permit |
| 2337 | // return types that are subclasses of the declared return type, |
| 2338 | // or that are more-qualified versions of the declared type. |
| 2339 | if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false)) |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2340 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2341 | |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2342 | DiagID = |
| 2343 | IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types |
Craig Topper | 8f7f3ea | 2015-11-17 05:40:05 +0000 | [diff] [blame] | 2344 | : diag::warn_non_covariant_ret_types; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2345 | } |
| 2346 | } |
| 2347 | |
| 2348 | S.Diag(MethodImpl->getLocation(), DiagID) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2349 | << MethodImpl->getDeclName() << MethodDecl->getReturnType() |
| 2350 | << MethodImpl->getReturnType() |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 2351 | << MethodImpl->getReturnTypeSourceRange(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2352 | S.Diag(MethodDecl->getLocation(), IsOverridingMode |
| 2353 | ? diag::note_previous_declaration |
| 2354 | : diag::note_previous_definition) |
Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 2355 | << MethodDecl->getReturnTypeSourceRange(); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2356 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2359 | static bool CheckMethodOverrideParam(Sema &S, |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2360 | ObjCMethodDecl *MethodImpl, |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2361 | ObjCMethodDecl *MethodDecl, |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2362 | ParmVarDecl *ImplVar, |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2363 | ParmVarDecl *IfaceVar, |
Fariborz Jahanian | 4ceec3f | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 2364 | bool IsProtocolMethodDecl, |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2365 | bool IsOverridingMode, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2366 | bool Warn) { |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2367 | if (IsProtocolMethodDecl && |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2368 | objcModifiersConflict(ImplVar->getObjCDeclQualifier(), |
| 2369 | IfaceVar->getObjCDeclQualifier())) { |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2370 | if (Warn) { |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2371 | if (IsOverridingMode) |
| 2372 | S.Diag(ImplVar->getLocation(), |
| 2373 | diag::warn_conflicting_overriding_param_modifiers) |
| 2374 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
| 2375 | << MethodImpl->getDeclName(); |
| 2376 | else S.Diag(ImplVar->getLocation(), |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2377 | diag::warn_conflicting_param_modifiers) |
| 2378 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2379 | << MethodImpl->getDeclName(); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2380 | S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration) |
| 2381 | << getTypeRange(IfaceVar->getTypeSourceInfo()); |
| 2382 | } |
| 2383 | else |
| 2384 | return false; |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2387 | QualType ImplTy = ImplVar->getType(); |
| 2388 | QualType IfaceTy = IfaceVar->getType(); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2389 | if (Warn && IsOverridingMode && |
| 2390 | !isa<ObjCImplementationDecl>(MethodImpl->getDeclContext()) && |
| 2391 | !S.Context.hasSameNullabilityTypeQualifier(ImplTy, IfaceTy, true)) { |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2392 | S.Diag(ImplVar->getLocation(), |
| 2393 | diag::warn_conflicting_nullability_attr_overriding_param_types) |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 2394 | << DiagNullabilityKind( |
| 2395 | *ImplTy->getNullability(S.Context), |
| 2396 | ((ImplVar->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2397 | != 0)) |
| 2398 | << DiagNullabilityKind( |
| 2399 | *IfaceTy->getNullability(S.Context), |
| 2400 | ((IfaceVar->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2401 | != 0)); |
| 2402 | S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2403 | } |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2404 | if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy)) |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2405 | return true; |
Manman Ren | c5705ba | 2016-09-13 17:41:05 +0000 | [diff] [blame] | 2406 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2407 | if (!Warn) |
| 2408 | return false; |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2409 | unsigned DiagID = |
| 2410 | IsOverridingMode ? diag::warn_conflicting_overriding_param_types |
| 2411 | : diag::warn_conflicting_param_types; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2412 | |
| 2413 | // Mismatches between ObjC pointers go into a different warning |
| 2414 | // category, and sometimes they're even completely whitelisted. |
| 2415 | if (const ObjCObjectPointerType *ImplPtrTy = |
| 2416 | ImplTy->getAs<ObjCObjectPointerType>()) { |
| 2417 | if (const ObjCObjectPointerType *IfacePtrTy = |
| 2418 | IfaceTy->getAs<ObjCObjectPointerType>()) { |
| 2419 | // Allow non-matching argument types as long as they don't |
| 2420 | // violate the principle of substitutability. Specifically, the |
| 2421 | // implementation must accept any objects that the superclass |
| 2422 | // accepts, however it may also accept others. |
| 2423 | if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true)) |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2424 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2425 | |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2426 | DiagID = |
| 2427 | IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types |
Craig Topper | 8f7f3ea | 2015-11-17 05:40:05 +0000 | [diff] [blame] | 2428 | : diag::warn_non_contravariant_param_types; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2429 | } |
| 2430 | } |
| 2431 | |
| 2432 | S.Diag(ImplVar->getLocation(), DiagID) |
| 2433 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2434 | << MethodImpl->getDeclName() << IfaceTy << ImplTy; |
| 2435 | S.Diag(IfaceVar->getLocation(), |
| 2436 | (IsOverridingMode ? diag::note_previous_declaration |
Craig Topper | 8f7f3ea | 2015-11-17 05:40:05 +0000 | [diff] [blame] | 2437 | : diag::note_previous_definition)) |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2438 | << getTypeRange(IfaceVar->getTypeSourceInfo()); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2439 | return false; |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2440 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2441 | |
| 2442 | /// In ARC, check whether the conventional meanings of the two methods |
| 2443 | /// match. If they don't, it's a hard error. |
| 2444 | static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl, |
| 2445 | ObjCMethodDecl *decl) { |
| 2446 | ObjCMethodFamily implFamily = impl->getMethodFamily(); |
| 2447 | ObjCMethodFamily declFamily = decl->getMethodFamily(); |
| 2448 | if (implFamily == declFamily) return false; |
| 2449 | |
| 2450 | // Since conventions are sorted by selector, the only possibility is |
| 2451 | // that the types differ enough to cause one selector or the other |
| 2452 | // to fall out of the family. |
| 2453 | assert(implFamily == OMF_None || declFamily == OMF_None); |
| 2454 | |
| 2455 | // No further diagnostics required on invalid declarations. |
| 2456 | if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true; |
| 2457 | |
| 2458 | const ObjCMethodDecl *unmatched = impl; |
| 2459 | ObjCMethodFamily family = declFamily; |
| 2460 | unsigned errorID = diag::err_arc_lost_method_convention; |
| 2461 | unsigned noteID = diag::note_arc_lost_method_convention; |
| 2462 | if (declFamily == OMF_None) { |
| 2463 | unmatched = decl; |
| 2464 | family = implFamily; |
| 2465 | errorID = diag::err_arc_gained_method_convention; |
| 2466 | noteID = diag::note_arc_gained_method_convention; |
| 2467 | } |
| 2468 | |
| 2469 | // Indexes into a %select clause in the diagnostic. |
| 2470 | enum FamilySelector { |
| 2471 | F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new |
| 2472 | }; |
| 2473 | FamilySelector familySelector = FamilySelector(); |
| 2474 | |
| 2475 | switch (family) { |
| 2476 | case OMF_None: llvm_unreachable("logic error, no method convention"); |
| 2477 | case OMF_retain: |
| 2478 | case OMF_release: |
| 2479 | case OMF_autorelease: |
| 2480 | case OMF_dealloc: |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 2481 | case OMF_finalize: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2482 | case OMF_retainCount: |
| 2483 | case OMF_self: |
Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 2484 | case OMF_initialize: |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 2485 | case OMF_performSelector: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2486 | // Mismatches for these methods don't change ownership |
| 2487 | // conventions, so we don't care. |
| 2488 | return false; |
| 2489 | |
| 2490 | case OMF_init: familySelector = F_init; break; |
| 2491 | case OMF_alloc: familySelector = F_alloc; break; |
| 2492 | case OMF_copy: familySelector = F_copy; break; |
| 2493 | case OMF_mutableCopy: familySelector = F_mutableCopy; break; |
| 2494 | case OMF_new: familySelector = F_new; break; |
| 2495 | } |
| 2496 | |
| 2497 | enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn }; |
| 2498 | ReasonSelector reasonSelector; |
| 2499 | |
| 2500 | // The only reason these methods don't fall within their families is |
| 2501 | // due to unusual result types. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2502 | if (unmatched->getReturnType()->isObjCObjectPointerType()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2503 | reasonSelector = R_UnrelatedReturn; |
| 2504 | } else { |
| 2505 | reasonSelector = R_NonObjectReturn; |
| 2506 | } |
| 2507 | |
Joerg Sonnenberger | ffc6d49 | 2013-06-26 21:31:47 +0000 | [diff] [blame] | 2508 | S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector); |
| 2509 | S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2510 | |
| 2511 | return true; |
| 2512 | } |
John McCall | 071df46 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 2513 | |
Fariborz Jahanian | 7988d7d | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 2514 | void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2515 | ObjCMethodDecl *MethodDecl, |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2516 | bool IsProtocolMethodDecl) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2517 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2518 | checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl)) |
| 2519 | return; |
| 2520 | |
Fariborz Jahanian | d7b0cb5 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 2521 | CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2522 | IsProtocolMethodDecl, false, |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2523 | true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2524 | |
Chris Lattner | 67f35b0 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 2525 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2526 | IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(), |
| 2527 | EF = MethodDecl->param_end(); |
| 2528 | IM != EM && IF != EF; ++IM, ++IF) { |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2529 | CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF, |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2530 | IsProtocolMethodDecl, false, true); |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2531 | } |
Fariborz Jahanian | 3c12dd7 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 2532 | |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2533 | if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) { |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2534 | Diag(ImpMethodDecl->getLocation(), |
| 2535 | diag::warn_conflicting_variadic); |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2536 | Diag(MethodDecl->getLocation(), diag::note_previous_declaration); |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2537 | } |
Fariborz Jahanian | 5ac085a | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2540 | void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method, |
| 2541 | ObjCMethodDecl *Overridden, |
| 2542 | bool IsProtocolMethodDecl) { |
| 2543 | |
| 2544 | CheckMethodOverrideReturn(*this, Method, Overridden, |
| 2545 | IsProtocolMethodDecl, true, |
| 2546 | true); |
| 2547 | |
| 2548 | for (ObjCMethodDecl::param_iterator IM = Method->param_begin(), |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2549 | IF = Overridden->param_begin(), EM = Method->param_end(), |
| 2550 | EF = Overridden->param_end(); |
| 2551 | IM != EM && IF != EF; ++IM, ++IF) { |
Fariborz Jahanian | 9a81f84 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 2552 | CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF, |
| 2553 | IsProtocolMethodDecl, true, true); |
| 2554 | } |
| 2555 | |
| 2556 | if (Method->isVariadic() != Overridden->isVariadic()) { |
| 2557 | Diag(Method->getLocation(), |
| 2558 | diag::warn_conflicting_overriding_variadic); |
| 2559 | Diag(Overridden->getLocation(), diag::note_previous_declaration); |
| 2560 | } |
| 2561 | } |
| 2562 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2563 | /// WarnExactTypedMethods - This routine issues a warning if method |
| 2564 | /// implementation declaration matches exactly that of its declaration. |
| 2565 | void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
| 2566 | ObjCMethodDecl *MethodDecl, |
| 2567 | bool IsProtocolMethodDecl) { |
| 2568 | // don't issue warning when protocol method is optional because primary |
| 2569 | // class is not required to implement it and it is safe for protocol |
| 2570 | // to implement it. |
| 2571 | if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional) |
| 2572 | return; |
| 2573 | // don't issue warning when primary class's method is |
| 2574 | // depecated/unavailable. |
| 2575 | if (MethodDecl->hasAttr<UnavailableAttr>() || |
| 2576 | MethodDecl->hasAttr<DeprecatedAttr>()) |
| 2577 | return; |
| 2578 | |
| 2579 | bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
| 2580 | IsProtocolMethodDecl, false, false); |
| 2581 | if (match) |
| 2582 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2583 | IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(), |
| 2584 | EF = MethodDecl->param_end(); |
| 2585 | IM != EM && IF != EF; ++IM, ++IF) { |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2586 | match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, |
| 2587 | *IM, *IF, |
| 2588 | IsProtocolMethodDecl, false, false); |
| 2589 | if (!match) |
| 2590 | break; |
| 2591 | } |
| 2592 | if (match) |
| 2593 | match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic()); |
David Chisnall | 9291851 | 2011-08-08 17:32:19 +0000 | [diff] [blame] | 2594 | if (match) |
| 2595 | match = !(MethodDecl->isClassMethod() && |
| 2596 | MethodDecl->getSelector() == GetNullarySelector("load", Context)); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2597 | |
| 2598 | if (match) { |
| 2599 | Diag(ImpMethodDecl->getLocation(), |
| 2600 | diag::warn_category_method_impl_match); |
Ted Kremenek | 59b10db | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 2601 | Diag(MethodDecl->getLocation(), diag::note_method_declared_at) |
| 2602 | << MethodDecl->getDeclName(); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2603 | } |
| 2604 | } |
| 2605 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2606 | /// FIXME: Type hierarchies in Objective-C can be deep. We could most likely |
| 2607 | /// improve the efficiency of selector lookups and type checking by associating |
| 2608 | /// with each protocol / interface / category the flattened instance tables. If |
| 2609 | /// we used an immutable set to keep the table then it wouldn't add significant |
| 2610 | /// memory cost and it would be handy for lookups. |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 2611 | |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2612 | typedef llvm::DenseSet<IdentifierInfo*> ProtocolNameSet; |
Ahmed Charles | af94d56 | 2014-03-09 11:34:25 +0000 | [diff] [blame] | 2613 | typedef std::unique_ptr<ProtocolNameSet> LazyProtocolNameSet; |
Ted Kremenek | 760a2ac | 2014-03-05 23:18:22 +0000 | [diff] [blame] | 2614 | |
| 2615 | static void findProtocolsWithExplicitImpls(const ObjCProtocolDecl *PDecl, |
| 2616 | ProtocolNameSet &PNS) { |
| 2617 | if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) |
| 2618 | PNS.insert(PDecl->getIdentifier()); |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 2619 | for (const auto *PI : PDecl->protocols()) |
| 2620 | findProtocolsWithExplicitImpls(PI, PNS); |
Ted Kremenek | 760a2ac | 2014-03-05 23:18:22 +0000 | [diff] [blame] | 2621 | } |
| 2622 | |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2623 | /// Recursively populates a set with all conformed protocols in a class |
| 2624 | /// hierarchy that have the 'objc_protocol_requires_explicit_implementation' |
| 2625 | /// attribute. |
| 2626 | static void findProtocolsWithExplicitImpls(const ObjCInterfaceDecl *Super, |
| 2627 | ProtocolNameSet &PNS) { |
| 2628 | if (!Super) |
| 2629 | return; |
| 2630 | |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2631 | for (const auto *I : Super->all_referenced_protocols()) |
| 2632 | findProtocolsWithExplicitImpls(I, PNS); |
Ted Kremenek | 760a2ac | 2014-03-05 23:18:22 +0000 | [diff] [blame] | 2633 | |
| 2634 | findProtocolsWithExplicitImpls(Super->getSuperClass(), PNS); |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2635 | } |
| 2636 | |
Steve Naroff | a3699224 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 2637 | /// CheckProtocolMethodDefs - This routine checks unimplemented methods |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2638 | /// Declared in protocol, and those referenced by it. |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2639 | static void CheckProtocolMethodDefs(Sema &S, |
| 2640 | SourceLocation ImpLoc, |
| 2641 | ObjCProtocolDecl *PDecl, |
| 2642 | bool& IncompleteImpl, |
| 2643 | const Sema::SelectorSet &InsMap, |
| 2644 | const Sema::SelectorSet &ClsMap, |
Ted Kremenek | 33e430f | 2013-12-13 06:26:14 +0000 | [diff] [blame] | 2645 | ObjCContainerDecl *CDecl, |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2646 | LazyProtocolNameSet &ProtocolsExplictImpl) { |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2647 | ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl); |
| 2648 | ObjCInterfaceDecl *IDecl = C ? C->getClassInterface() |
| 2649 | : dyn_cast<ObjCInterfaceDecl>(CDecl); |
Fariborz Jahanian | 2e8074b | 2010-03-27 21:10:05 +0000 | [diff] [blame] | 2650 | assert (IDecl && "CheckProtocolMethodDefs - IDecl is null"); |
| 2651 | |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 2652 | ObjCInterfaceDecl *Super = IDecl->getSuperClass(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2653 | ObjCInterfaceDecl *NSIDecl = nullptr; |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2654 | |
| 2655 | // If this protocol is marked 'objc_protocol_requires_explicit_implementation' |
| 2656 | // then we should check if any class in the super class hierarchy also |
| 2657 | // conforms to this protocol, either directly or via protocol inheritance. |
| 2658 | // If so, we can skip checking this protocol completely because we |
| 2659 | // know that a parent class already satisfies this protocol. |
| 2660 | // |
| 2661 | // Note: we could generalize this logic for all protocols, and merely |
| 2662 | // add the limit on looking at the super class chain for just |
| 2663 | // specially marked protocols. This may be a good optimization. This |
| 2664 | // change is restricted to 'objc_protocol_requires_explicit_implementation' |
| 2665 | // protocols for now for controlled evaluation. |
| 2666 | if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) { |
Ahmed Charles | af94d56 | 2014-03-09 11:34:25 +0000 | [diff] [blame] | 2667 | if (!ProtocolsExplictImpl) { |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2668 | ProtocolsExplictImpl.reset(new ProtocolNameSet); |
| 2669 | findProtocolsWithExplicitImpls(Super, *ProtocolsExplictImpl); |
| 2670 | } |
| 2671 | if (ProtocolsExplictImpl->find(PDecl->getIdentifier()) != |
| 2672 | ProtocolsExplictImpl->end()) |
| 2673 | return; |
| 2674 | |
| 2675 | // If no super class conforms to the protocol, we should not search |
| 2676 | // for methods in the super class to implicitly satisfy the protocol. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2677 | Super = nullptr; |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2680 | if (S.getLangOpts().ObjCRuntime.isNeXTFamily()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2681 | // check to see if class implements forwardInvocation method and objects |
| 2682 | // of this class are derived from 'NSProxy' so that to forward requests |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2683 | // from one object to another. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2684 | // Under such conditions, which means that every method possible is |
| 2685 | // implemented in the class, we should not issue "Method definition not |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2686 | // found" warnings. |
| 2687 | // FIXME: Use a general GetUnarySelector method for this. |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2688 | IdentifierInfo* II = &S.Context.Idents.get("forwardInvocation"); |
| 2689 | Selector fISelector = S.Context.Selectors.getSelector(1, &II); |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2690 | if (InsMap.count(fISelector)) |
| 2691 | // Is IDecl derived from 'NSProxy'? If so, no instance methods |
| 2692 | // need be implemented in the implementation. |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2693 | NSIDecl = IDecl->lookupInheritedClass(&S.Context.Idents.get("NSProxy")); |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2694 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2695 | |
Fariborz Jahanian | c41cf05 | 2013-01-07 19:21:03 +0000 | [diff] [blame] | 2696 | // If this is a forward protocol declaration, get its definition. |
| 2697 | if (!PDecl->isThisDeclarationADefinition() && |
| 2698 | PDecl->getDefinition()) |
| 2699 | PDecl = PDecl->getDefinition(); |
| 2700 | |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 2701 | // If a method lookup fails locally we still need to look and see if |
| 2702 | // the method was implemented by a base class or an inherited |
| 2703 | // protocol. This lookup is slow, but occurs rarely in correct code |
| 2704 | // and otherwise would terminate in a warning. |
| 2705 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2706 | // check unimplemented instance methods. |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2707 | if (!NSIDecl) |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2708 | for (auto *method : PDecl->instance_methods()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2709 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2710 | !method->isPropertyAccessor() && |
| 2711 | !InsMap.count(method->getSelector()) && |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2712 | (!Super || !Super->lookupMethod(method->getSelector(), |
| 2713 | true /* instance */, |
| 2714 | false /* shallowCategory */, |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2715 | true /* followsSuper */, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2716 | nullptr /* category */))) { |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2717 | // If a method is not implemented in the category implementation but |
| 2718 | // has been declared in its primary class, superclass, |
| 2719 | // or in one of their protocols, no need to issue the warning. |
| 2720 | // This is because method will be implemented in the primary class |
| 2721 | // or one of its super class implementation. |
| 2722 | |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2723 | // Ugly, but necessary. Method declared in protcol might have |
| 2724 | // have been synthesized due to a property declared in the class which |
| 2725 | // uses the protocol. |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2726 | if (ObjCMethodDecl *MethodInClass = |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2727 | IDecl->lookupMethod(method->getSelector(), |
| 2728 | true /* instance */, |
| 2729 | true /* shallowCategoryLookup */, |
| 2730 | false /* followSuper */)) |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2731 | if (C || MethodInClass->isPropertyAccessor()) |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2732 | continue; |
| 2733 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 2734 | if (!S.Diags.isIgnored(DIAG, ImpLoc)) { |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2735 | WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, |
Ted Kremenek | 2ccf19e | 2013-12-13 05:58:51 +0000 | [diff] [blame] | 2736 | PDecl); |
Fariborz Jahanian | 97752f7 | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 2737 | } |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 2738 | } |
| 2739 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2740 | // check unimplemented class methods |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2741 | for (auto *method : PDecl->class_methods()) { |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 2742 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
| 2743 | !ClsMap.count(method->getSelector()) && |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2744 | (!Super || !Super->lookupMethod(method->getSelector(), |
| 2745 | false /* class method */, |
| 2746 | false /* shallowCategoryLookup */, |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2747 | true /* followSuper */, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2748 | nullptr /* category */))) { |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2749 | // See above comment for instance method lookups. |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2750 | if (C && IDecl->lookupMethod(method->getSelector(), |
| 2751 | false /* class */, |
| 2752 | true /* shallowCategoryLookup */, |
| 2753 | false /* followSuper */)) |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2754 | continue; |
Ted Kremenek | 0078150 | 2013-11-23 01:01:29 +0000 | [diff] [blame] | 2755 | |
Fariborz Jahanian | c1fb862 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 2756 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 2757 | if (!S.Diags.isIgnored(DIAG, ImpLoc)) { |
Ted Kremenek | 285ee85 | 2013-12-13 06:26:10 +0000 | [diff] [blame] | 2758 | WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, PDecl); |
Fariborz Jahanian | c1fb862 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 2759 | } |
Fariborz Jahanian | 97752f7 | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 2760 | } |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 2761 | } |
Chris Lattner | 390d39a | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 2762 | // Check on this protocols's referenced protocols, recursively. |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 2763 | for (auto *PI : PDecl->protocols()) |
| 2764 | CheckProtocolMethodDefs(S, ImpLoc, PI, IncompleteImpl, InsMap, ClsMap, |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2765 | CDecl, ProtocolsExplictImpl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
Fariborz Jahanian | f9ae68a | 2011-07-16 00:08:33 +0000 | [diff] [blame] | 2768 | /// MatchAllMethodDeclarations - Check methods declared in interface |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2769 | /// or protocol against those declared in their implementations. |
| 2770 | /// |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2771 | void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap, |
| 2772 | const SelectorSet &ClsMap, |
| 2773 | SelectorSet &InsMapSeen, |
| 2774 | SelectorSet &ClsMapSeen, |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2775 | ObjCImplDecl* IMPDecl, |
| 2776 | ObjCContainerDecl* CDecl, |
| 2777 | bool &IncompleteImpl, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2778 | bool ImmediateClass, |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2779 | bool WarnCategoryMethodImpl) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2780 | // Check and see if instance methods in class interface have been |
| 2781 | // implemented in the implementation class. If so, their types match. |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2782 | for (auto *I : CDecl->instance_methods()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 2783 | if (!InsMapSeen.insert(I->getSelector()).second) |
Benjamin Kramer | 9f8e2d7 | 2013-10-14 15:16:10 +0000 | [diff] [blame] | 2784 | continue; |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2785 | if (!I->isPropertyAccessor() && |
| 2786 | !InsMap.count(I->getSelector())) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2787 | if (ImmediateClass) |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2788 | WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl, |
Ted Kremenek | 65d6357 | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 2789 | diag::warn_undef_method_impl); |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2790 | continue; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2791 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2792 | ObjCMethodDecl *ImpMethodDecl = |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2793 | IMPDecl->getInstanceMethod(I->getSelector()); |
Manman Ren | a58f92f | 2016-10-11 21:18:20 +0000 | [diff] [blame] | 2794 | assert(CDecl->getInstanceMethod(I->getSelector(), true/*AllowHidden*/) && |
Argyrios Kyrtzidis | 342e08f | 2011-08-30 19:43:21 +0000 | [diff] [blame] | 2795 | "Expected to find the method through lookup as well"); |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2796 | // ImpMethodDecl may be null as in a @dynamic property. |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2797 | if (ImpMethodDecl) { |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2798 | if (!WarnCategoryMethodImpl) |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2799 | WarnConflictingTypedMethods(ImpMethodDecl, I, |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2800 | isa<ObjCProtocolDecl>(CDecl)); |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2801 | else if (!I->isPropertyAccessor()) |
| 2802 | WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl)); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2803 | } |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2804 | } |
| 2805 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2806 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2807 | // Check and see if class methods in class interface have been |
| 2808 | // implemented in the implementation class. If so, their types match. |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2809 | for (auto *I : CDecl->class_methods()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 2810 | if (!ClsMapSeen.insert(I->getSelector()).second) |
Benjamin Kramer | 9f8e2d7 | 2013-10-14 15:16:10 +0000 | [diff] [blame] | 2811 | continue; |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2812 | if (!I->isPropertyAccessor() && |
| 2813 | !ClsMap.count(I->getSelector())) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2814 | if (ImmediateClass) |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2815 | WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl, |
Ted Kremenek | 65d6357 | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 2816 | diag::warn_undef_method_impl); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2817 | } else { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2818 | ObjCMethodDecl *ImpMethodDecl = |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2819 | IMPDecl->getClassMethod(I->getSelector()); |
Manman Ren | a58f92f | 2016-10-11 21:18:20 +0000 | [diff] [blame] | 2820 | assert(CDecl->getClassMethod(I->getSelector(), true/*AllowHidden*/) && |
Argyrios Kyrtzidis | 342e08f | 2011-08-30 19:43:21 +0000 | [diff] [blame] | 2821 | "Expected to find the method through lookup as well"); |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2822 | // ImpMethodDecl may be null as in a @dynamic property. |
| 2823 | if (ImpMethodDecl) { |
| 2824 | if (!WarnCategoryMethodImpl) |
| 2825 | WarnConflictingTypedMethods(ImpMethodDecl, I, |
| 2826 | isa<ObjCProtocolDecl>(CDecl)); |
| 2827 | else if (!I->isPropertyAccessor()) |
| 2828 | WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl)); |
| 2829 | } |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2830 | } |
| 2831 | } |
Fariborz Jahanian | 73853e5 | 2010-10-08 22:59:25 +0000 | [diff] [blame] | 2832 | |
Fariborz Jahanian | 8181caa | 2013-08-14 23:58:55 +0000 | [diff] [blame] | 2833 | if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) { |
| 2834 | // Also, check for methods declared in protocols inherited by |
| 2835 | // this protocol. |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 2836 | for (auto *PI : PD->protocols()) |
Fariborz Jahanian | 8181caa | 2013-08-14 23:58:55 +0000 | [diff] [blame] | 2837 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 2838 | IMPDecl, PI, IncompleteImpl, false, |
Fariborz Jahanian | 8181caa | 2013-08-14 23:58:55 +0000 | [diff] [blame] | 2839 | WarnCategoryMethodImpl); |
| 2840 | } |
| 2841 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2842 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2843 | // when checking that methods in implementation match their declaration, |
| 2844 | // i.e. when WarnCategoryMethodImpl is false, check declarations in class |
| 2845 | // extension; as well as those in categories. |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2846 | if (!WarnCategoryMethodImpl) { |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 2847 | for (auto *Cat : I->visible_categories()) |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2848 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Argyrios Kyrtzidis | 3a43754 | 2015-10-13 23:27:34 +0000 | [diff] [blame] | 2849 | IMPDecl, Cat, IncompleteImpl, |
| 2850 | ImmediateClass && Cat->IsClassExtension(), |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2851 | WarnCategoryMethodImpl); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2852 | } else { |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2853 | // Also methods in class extensions need be looked at next. |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 2854 | for (auto *Ext : I->visible_extensions()) |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2855 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 2856 | IMPDecl, Ext, IncompleteImpl, false, |
Fariborz Jahanian | 6f5309c | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 2857 | WarnCategoryMethodImpl); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2860 | // Check for any implementation of a methods declared in protocol. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2861 | for (auto *PI : I->all_referenced_protocols()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2862 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2863 | IMPDecl, PI, IncompleteImpl, false, |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2864 | WarnCategoryMethodImpl); |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2865 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2866 | // FIXME. For now, we are not checking for extact match of methods |
| 2867 | // in category implementation and its primary class's super class. |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2868 | if (!WarnCategoryMethodImpl && I->getSuperClass()) |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2869 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2870 | IMPDecl, |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2871 | I->getSuperClass(), IncompleteImpl, false); |
| 2872 | } |
| 2873 | } |
| 2874 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2875 | /// CheckCategoryVsClassMethodMatches - Checks that methods implemented in |
| 2876 | /// category matches with those implemented in its primary class and |
| 2877 | /// warns each time an exact match is found. |
| 2878 | void Sema::CheckCategoryVsClassMethodMatches( |
| 2879 | ObjCCategoryImplDecl *CatIMPDecl) { |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2880 | // Get category's primary class. |
| 2881 | ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl(); |
| 2882 | if (!CatDecl) |
| 2883 | return; |
| 2884 | ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface(); |
| 2885 | if (!IDecl) |
| 2886 | return; |
Fariborz Jahanian | f3077a2 | 2013-12-05 20:52:31 +0000 | [diff] [blame] | 2887 | ObjCInterfaceDecl *SuperIDecl = IDecl->getSuperClass(); |
| 2888 | SelectorSet InsMap, ClsMap; |
| 2889 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2890 | for (const auto *I : CatIMPDecl->instance_methods()) { |
| 2891 | Selector Sel = I->getSelector(); |
Fariborz Jahanian | f3077a2 | 2013-12-05 20:52:31 +0000 | [diff] [blame] | 2892 | // When checking for methods implemented in the category, skip over |
| 2893 | // those declared in category class's super class. This is because |
| 2894 | // the super class must implement the method. |
| 2895 | if (SuperIDecl && SuperIDecl->lookupMethod(Sel, true)) |
| 2896 | continue; |
| 2897 | InsMap.insert(Sel); |
| 2898 | } |
| 2899 | |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2900 | for (const auto *I : CatIMPDecl->class_methods()) { |
| 2901 | Selector Sel = I->getSelector(); |
Fariborz Jahanian | f3077a2 | 2013-12-05 20:52:31 +0000 | [diff] [blame] | 2902 | if (SuperIDecl && SuperIDecl->lookupMethod(Sel, false)) |
| 2903 | continue; |
| 2904 | ClsMap.insert(Sel); |
| 2905 | } |
| 2906 | if (InsMap.empty() && ClsMap.empty()) |
| 2907 | return; |
| 2908 | |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2909 | SelectorSet InsMapSeen, ClsMapSeen; |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2910 | bool IncompleteImpl = false; |
| 2911 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 2912 | CatIMPDecl, IDecl, |
Fariborz Jahanian | 29082a5 | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 2913 | IncompleteImpl, false, |
| 2914 | true /*WarnCategoryMethodImpl*/); |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2915 | } |
Fariborz Jahanian | 4ceec3f | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 2916 | |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 2917 | void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2918 | ObjCContainerDecl* CDecl, |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 2919 | bool IncompleteImpl) { |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2920 | SelectorSet InsMap; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2921 | // Check and see if instance methods in class interface have been |
| 2922 | // implemented in the implementation class. |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2923 | for (const auto *I : IMPDecl->instance_methods()) |
| 2924 | InsMap.insert(I->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2925 | |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 2926 | // Add the selectors for getters/setters of @dynamic properties. |
| 2927 | for (const auto *PImpl : IMPDecl->property_impls()) { |
| 2928 | // We only care about @dynamic implementations. |
| 2929 | if (PImpl->getPropertyImplementation() != ObjCPropertyImplDecl::Dynamic) |
| 2930 | continue; |
| 2931 | |
| 2932 | const auto *P = PImpl->getPropertyDecl(); |
| 2933 | if (!P) continue; |
| 2934 | |
| 2935 | InsMap.insert(P->getGetterName()); |
| 2936 | if (!P->getSetterName().isNull()) |
| 2937 | InsMap.insert(P->getSetterName()); |
| 2938 | } |
| 2939 | |
Fariborz Jahanian | 6c6aea9 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 2940 | // Check and see if properties declared in the interface have either 1) |
| 2941 | // an implementation or 2) there is a @synthesize/@dynamic implementation |
| 2942 | // of the property in the @implementation. |
Ted Kremenek | 348e88c | 2014-02-21 19:41:34 +0000 | [diff] [blame] | 2943 | if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 2944 | bool SynthesizeProperties = LangOpts.ObjCDefaultSynthProperties && |
| 2945 | LangOpts.ObjCRuntime.isNonFragile() && |
| 2946 | !IDecl->isObjCRequiresPropertyDefs(); |
| 2947 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, SynthesizeProperties); |
| 2948 | } |
| 2949 | |
Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 2950 | // Diagnose null-resettable synthesized setters. |
| 2951 | diagnoseNullResettableSynthesizedSetters(IMPDecl); |
| 2952 | |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2953 | SelectorSet ClsMap; |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2954 | for (const auto *I : IMPDecl->class_methods()) |
| 2955 | ClsMap.insert(I->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2956 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2957 | // Check for type conflict of methods declared in a class/protocol and |
| 2958 | // its implementation; if any. |
Benjamin Kramer | b33ffee | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 2959 | SelectorSet InsMapSeen, ClsMapSeen; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2960 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 2961 | IMPDecl, CDecl, |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2962 | IncompleteImpl, true); |
Fariborz Jahanian | 2bda1b6 | 2011-08-03 18:21:12 +0000 | [diff] [blame] | 2963 | |
Fariborz Jahanian | 9f8b19e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 2964 | // check all methods implemented in category against those declared |
| 2965 | // in its primary class. |
| 2966 | if (ObjCCategoryImplDecl *CatDecl = |
| 2967 | dyn_cast<ObjCCategoryImplDecl>(IMPDecl)) |
| 2968 | CheckCategoryVsClassMethodMatches(CatDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2969 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2970 | // Check the protocol list for unimplemented methods in the @implementation |
| 2971 | // class. |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 2972 | // Check and see if class methods in class interface have been |
| 2973 | // implemented in the implementation class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2974 | |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2975 | LazyProtocolNameSet ExplicitImplProtocols; |
| 2976 | |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 2977 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2978 | for (auto *PI : I->all_referenced_protocols()) |
| 2979 | CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), PI, IncompleteImpl, |
| 2980 | InsMap, ClsMap, I, ExplicitImplProtocols); |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 2981 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Fariborz Jahanian | 8764c74 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 2982 | // For extended class, unimplemented methods in its protocols will |
| 2983 | // be reported in the primary class. |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 2984 | if (!C->IsClassExtension()) { |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 2985 | for (auto *P : C->protocols()) |
| 2986 | CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), P, |
Ted Kremenek | 4b3c66e | 2014-03-05 08:13:08 +0000 | [diff] [blame] | 2987 | IncompleteImpl, InsMap, ClsMap, CDecl, |
| 2988 | ExplicitImplProtocols); |
Ted Kremenek | 348e88c | 2014-02-21 19:41:34 +0000 | [diff] [blame] | 2989 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 2990 | /*SynthesizeProperties=*/false); |
Fariborz Jahanian | 4f8a571 | 2010-01-20 19:36:21 +0000 | [diff] [blame] | 2991 | } |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 2992 | } else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2993 | llvm_unreachable("invalid ObjCContainerDecl type."); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2994 | } |
| 2995 | |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 2996 | Sema::DeclGroupPtrTy |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2997 | Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
Chris Lattner | 99a8331 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 2998 | IdentifierInfo **IdentList, |
Ted Kremenek | a26da85 | 2009-11-17 23:12:20 +0000 | [diff] [blame] | 2999 | SourceLocation *IdentLocs, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3000 | ArrayRef<ObjCTypeParamList *> TypeParamLists, |
Chris Lattner | 99a8331 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 3001 | unsigned NumElts) { |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 3002 | SmallVector<Decl *, 8> DeclsInGroup; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3003 | for (unsigned i = 0; i != NumElts; ++i) { |
| 3004 | // Check for another declaration kind with the same name. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3005 | NamedDecl *PrevDecl |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 3006 | = LookupSingleName(TUScope, IdentList[i], IdentLocs[i], |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 3007 | LookupOrdinaryName, forRedeclarationInCurContext()); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3008 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Steve Naroff | 946166f | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 3009 | // GCC apparently allows the following idiom: |
| 3010 | // |
| 3011 | // typedef NSObject < XCElementTogglerP > XCElementToggler; |
| 3012 | // @class XCElementToggler; |
| 3013 | // |
Fariborz Jahanian | 04c4455 | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 3014 | // Here we have chosen to ignore the forward class declaration |
| 3015 | // with a warning. Since this is the implied behavior. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3016 | TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3017 | if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3018 | Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i]; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3019 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3020 | } else { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3021 | // a forward class declaration matching a typedef name of a class refers |
Fariborz Jahanian | 04c4455 | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 3022 | // to the underlying class. Just ignore the forward class with a warning |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3023 | // as this will force the intended behavior which is to lookup the |
| 3024 | // typedef name. |
Fariborz Jahanian | 04c4455 | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 3025 | if (isa<ObjCObjectType>(TDD->getUnderlyingType())) { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3026 | Diag(AtClassLoc, diag::warn_forward_class_redefinition) |
| 3027 | << IdentList[i]; |
Fariborz Jahanian | 04c4455 | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 3028 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3029 | continue; |
| 3030 | } |
Fariborz Jahanian | 0d45181 | 2009-05-07 21:49:26 +0000 | [diff] [blame] | 3031 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3032 | } |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 3033 | |
| 3034 | // Create a declaration to describe this forward declaration. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3035 | ObjCInterfaceDecl *PrevIDecl |
| 3036 | = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Argyrios Kyrtzidis | dd71063 | 2013-06-18 21:26:33 +0000 | [diff] [blame] | 3037 | |
| 3038 | IdentifierInfo *ClassName = IdentList[i]; |
| 3039 | if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) { |
| 3040 | // A previous decl with a different name is because of |
| 3041 | // @compatibility_alias, for example: |
| 3042 | // \code |
| 3043 | // @class NewImage; |
| 3044 | // @compatibility_alias OldImage NewImage; |
| 3045 | // \endcode |
| 3046 | // A lookup for 'OldImage' will return the 'NewImage' decl. |
| 3047 | // |
| 3048 | // In such a case use the real declaration name, instead of the alias one, |
| 3049 | // otherwise we will break IdentifierResolver and redecls-chain invariants. |
| 3050 | // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl |
| 3051 | // has been aliased. |
| 3052 | ClassName = PrevIDecl->getIdentifier(); |
| 3053 | } |
| 3054 | |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3055 | // If this forward declaration has type parameters, compare them with the |
| 3056 | // type parameters of the previous declaration. |
| 3057 | ObjCTypeParamList *TypeParams = TypeParamLists[i]; |
| 3058 | if (PrevIDecl && TypeParams) { |
| 3059 | if (ObjCTypeParamList *PrevTypeParams = PrevIDecl->getTypeParamList()) { |
| 3060 | // Check for consistency with the previous declaration. |
| 3061 | if (checkTypeParamListConsistency( |
| 3062 | *this, PrevTypeParams, TypeParams, |
| 3063 | TypeParamListContext::ForwardDeclaration)) { |
| 3064 | TypeParams = nullptr; |
| 3065 | } |
| 3066 | } else if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) { |
| 3067 | // The @interface does not have type parameters. Complain. |
| 3068 | Diag(IdentLocs[i], diag::err_objc_parameterized_forward_class) |
| 3069 | << ClassName |
| 3070 | << TypeParams->getSourceRange(); |
| 3071 | Diag(Def->getLocation(), diag::note_defined_here) |
| 3072 | << ClassName; |
| 3073 | |
| 3074 | TypeParams = nullptr; |
| 3075 | } |
| 3076 | } |
| 3077 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 3078 | ObjCInterfaceDecl *IDecl |
| 3079 | = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc, |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 3080 | ClassName, TypeParams, PrevIDecl, |
| 3081 | IdentLocs[i]); |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 3082 | IDecl->setAtEndRange(IdentLocs[i]); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3083 | |
Douglas Gregor | dc9166c | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 3084 | PushOnScopeChains(IDecl, TUScope); |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 3085 | CheckObjCDeclScope(IDecl); |
| 3086 | DeclsInGroup.push_back(IDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3087 | } |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 3088 | |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 3089 | return BuildDeclaratorGroup(DeclsInGroup); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3092 | static bool tryMatchRecordTypes(ASTContext &Context, |
| 3093 | Sema::MethodMatchStrategy strategy, |
| 3094 | const Type *left, const Type *right); |
| 3095 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3096 | static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy, |
| 3097 | QualType leftQT, QualType rightQT) { |
| 3098 | const Type *left = |
| 3099 | Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr(); |
| 3100 | const Type *right = |
| 3101 | Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr(); |
| 3102 | |
| 3103 | if (left == right) return true; |
| 3104 | |
| 3105 | // If we're doing a strict match, the types have to match exactly. |
| 3106 | if (strategy == Sema::MMS_strict) return false; |
| 3107 | |
| 3108 | if (left->isIncompleteType() || right->isIncompleteType()) return false; |
| 3109 | |
| 3110 | // Otherwise, use this absurdly complicated algorithm to try to |
| 3111 | // validate the basic, low-level compatibility of the two types. |
| 3112 | |
| 3113 | // As a minimum, require the sizes and alignments to match. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 3114 | TypeInfo LeftTI = Context.getTypeInfo(left); |
| 3115 | TypeInfo RightTI = Context.getTypeInfo(right); |
| 3116 | if (LeftTI.Width != RightTI.Width) |
| 3117 | return false; |
| 3118 | |
| 3119 | if (LeftTI.Align != RightTI.Align) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3120 | return false; |
| 3121 | |
| 3122 | // Consider all the kinds of non-dependent canonical types: |
| 3123 | // - functions and arrays aren't possible as return and parameter types |
| 3124 | |
| 3125 | // - vector types of equal size can be arbitrarily mixed |
| 3126 | if (isa<VectorType>(left)) return isa<VectorType>(right); |
| 3127 | if (isa<VectorType>(right)) return false; |
| 3128 | |
| 3129 | // - references should only match references of identical type |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3130 | // - structs, unions, and Objective-C objects must match more-or-less |
| 3131 | // exactly |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3132 | // - everything else should be a scalar |
| 3133 | if (!left->isScalarType() || !right->isScalarType()) |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3134 | return tryMatchRecordTypes(Context, strategy, left, right); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3135 | |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3136 | // Make scalars agree in kind, except count bools as chars, and group |
| 3137 | // all non-member pointers together. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3138 | Type::ScalarTypeKind leftSK = left->getScalarTypeKind(); |
| 3139 | Type::ScalarTypeKind rightSK = right->getScalarTypeKind(); |
| 3140 | if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral; |
| 3141 | if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral; |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3142 | if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer) |
| 3143 | leftSK = Type::STK_ObjCObjectPointer; |
| 3144 | if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer) |
| 3145 | rightSK = Type::STK_ObjCObjectPointer; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3146 | |
| 3147 | // Note that data member pointers and function member pointers don't |
| 3148 | // intermix because of the size differences. |
| 3149 | |
| 3150 | return (leftSK == rightSK); |
| 3151 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3152 | |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3153 | static bool tryMatchRecordTypes(ASTContext &Context, |
| 3154 | Sema::MethodMatchStrategy strategy, |
| 3155 | const Type *lt, const Type *rt) { |
| 3156 | assert(lt && rt && lt != rt); |
| 3157 | |
| 3158 | if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false; |
| 3159 | RecordDecl *left = cast<RecordType>(lt)->getDecl(); |
| 3160 | RecordDecl *right = cast<RecordType>(rt)->getDecl(); |
| 3161 | |
| 3162 | // Require union-hood to match. |
| 3163 | if (left->isUnion() != right->isUnion()) return false; |
| 3164 | |
| 3165 | // Require an exact match if either is non-POD. |
| 3166 | if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) || |
| 3167 | (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD())) |
| 3168 | return false; |
| 3169 | |
| 3170 | // Require size and alignment to match. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 3171 | TypeInfo LeftTI = Context.getTypeInfo(lt); |
| 3172 | TypeInfo RightTI = Context.getTypeInfo(rt); |
| 3173 | if (LeftTI.Width != RightTI.Width) |
| 3174 | return false; |
| 3175 | |
| 3176 | if (LeftTI.Align != RightTI.Align) |
| 3177 | return false; |
John McCall | 54507ab | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 3178 | |
| 3179 | // Require fields to match. |
| 3180 | RecordDecl::field_iterator li = left->field_begin(), le = left->field_end(); |
| 3181 | RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end(); |
| 3182 | for (; li != le && ri != re; ++li, ++ri) { |
| 3183 | if (!matchTypes(Context, strategy, li->getType(), ri->getType())) |
| 3184 | return false; |
| 3185 | } |
| 3186 | return (li == le && ri == re); |
| 3187 | } |
| 3188 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3189 | /// MatchTwoMethodDeclarations - Checks that two methods have matching type and |
| 3190 | /// returns true, or false, accordingly. |
| 3191 | /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3192 | bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left, |
| 3193 | const ObjCMethodDecl *right, |
| 3194 | MethodMatchStrategy strategy) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3195 | if (!matchTypes(Context, strategy, left->getReturnType(), |
| 3196 | right->getReturnType())) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3197 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3198 | |
Douglas Gregor | 560b7fa | 2013-02-07 19:13:24 +0000 | [diff] [blame] | 3199 | // If either is hidden, it is not considered to match. |
| 3200 | if (left->isHidden() || right->isHidden()) |
| 3201 | return false; |
| 3202 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3203 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3204 | (left->hasAttr<NSReturnsRetainedAttr>() |
| 3205 | != right->hasAttr<NSReturnsRetainedAttr>() || |
| 3206 | left->hasAttr<NSConsumesSelfAttr>() |
| 3207 | != right->hasAttr<NSConsumesSelfAttr>())) |
| 3208 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3209 | |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 3210 | ObjCMethodDecl::param_const_iterator |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 3211 | li = left->param_begin(), le = left->param_end(), ri = right->param_begin(), |
| 3212 | re = right->param_end(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3213 | |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 3214 | for (; li != le && ri != re; ++li, ++ri) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3215 | assert(ri != right->param_end() && "Param mismatch"); |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 3216 | const ParmVarDecl *lparm = *li, *rparm = *ri; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3217 | |
| 3218 | if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType())) |
| 3219 | return false; |
| 3220 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3221 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3222 | lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>()) |
| 3223 | return false; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3224 | } |
| 3225 | return true; |
| 3226 | } |
| 3227 | |
Manman Ren | 7122453 | 2016-04-09 18:59:48 +0000 | [diff] [blame] | 3228 | static bool isMethodContextSameForKindofLookup(ObjCMethodDecl *Method, |
| 3229 | ObjCMethodDecl *MethodInList) { |
| 3230 | auto *MethodProtocol = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext()); |
| 3231 | auto *MethodInListProtocol = |
| 3232 | dyn_cast<ObjCProtocolDecl>(MethodInList->getDeclContext()); |
| 3233 | // If this method belongs to a protocol but the method in list does not, or |
| 3234 | // vice versa, we say the context is not the same. |
| 3235 | if ((MethodProtocol && !MethodInListProtocol) || |
| 3236 | (!MethodProtocol && MethodInListProtocol)) |
| 3237 | return false; |
| 3238 | |
| 3239 | if (MethodProtocol && MethodInListProtocol) |
| 3240 | return true; |
| 3241 | |
| 3242 | ObjCInterfaceDecl *MethodInterface = Method->getClassInterface(); |
| 3243 | ObjCInterfaceDecl *MethodInListInterface = |
| 3244 | MethodInList->getClassInterface(); |
| 3245 | return MethodInterface == MethodInListInterface; |
| 3246 | } |
| 3247 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3248 | void Sema::addMethodToGlobalList(ObjCMethodList *List, |
| 3249 | ObjCMethodDecl *Method) { |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 3250 | // Record at the head of the list whether there were 0, 1, or >= 2 methods |
| 3251 | // inside categories. |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3252 | if (ObjCCategoryDecl *CD = |
| 3253 | dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) |
Argyrios Kyrtzidis | 04703a6 | 2013-04-27 00:10:12 +0000 | [diff] [blame] | 3254 | if (!CD->IsClassExtension() && List->getBits() < 2) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3255 | List->setBits(List->getBits() + 1); |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 3256 | |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3257 | // If the list is empty, make it a singleton list. |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3258 | if (List->getMethod() == nullptr) { |
| 3259 | List->setMethod(Method); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3260 | List->setNext(nullptr); |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3261 | return; |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3262 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3263 | |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3264 | // We've seen a method with this name, see if we have already seen this type |
| 3265 | // signature. |
| 3266 | ObjCMethodList *Previous = List; |
Manman Ren | 051d0b6 | 2016-04-13 23:43:56 +0000 | [diff] [blame] | 3267 | ObjCMethodList *ListWithSameDeclaration = nullptr; |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 3268 | for (; List; Previous = List, List = List->getNext()) { |
Douglas Gregor | 600a2f5 | 2013-06-21 00:20:25 +0000 | [diff] [blame] | 3269 | // If we are building a module, keep all of the methods. |
Richard Smith | bbcc9f0 | 2016-08-26 00:14:38 +0000 | [diff] [blame] | 3270 | if (getLangOpts().isCompilingModule()) |
Douglas Gregor | 600a2f5 | 2013-06-21 00:20:25 +0000 | [diff] [blame] | 3271 | continue; |
| 3272 | |
Manman Ren | 051d0b6 | 2016-04-13 23:43:56 +0000 | [diff] [blame] | 3273 | bool SameDeclaration = MatchTwoMethodDeclarations(Method, |
| 3274 | List->getMethod()); |
Manman Ren | 7122453 | 2016-04-09 18:59:48 +0000 | [diff] [blame] | 3275 | // Looking for method with a type bound requires the correct context exists. |
Manman Ren | 051d0b6 | 2016-04-13 23:43:56 +0000 | [diff] [blame] | 3276 | // We need to insert a method into the list if the context is different. |
| 3277 | // If the method's declaration matches the list |
| 3278 | // a> the method belongs to a different context: we need to insert it, in |
| 3279 | // order to emit the availability message, we need to prioritize over |
| 3280 | // availability among the methods with the same declaration. |
| 3281 | // b> the method belongs to the same context: there is no need to insert a |
| 3282 | // new entry. |
| 3283 | // If the method's declaration does not match the list, we insert it to the |
| 3284 | // end. |
| 3285 | if (!SameDeclaration || |
Manman Ren | 7122453 | 2016-04-09 18:59:48 +0000 | [diff] [blame] | 3286 | !isMethodContextSameForKindofLookup(Method, List->getMethod())) { |
Fariborz Jahanian | d436b2a | 2015-04-07 16:56:27 +0000 | [diff] [blame] | 3287 | // Even if two method types do not match, we would like to say |
| 3288 | // there is more than one declaration so unavailability/deprecated |
| 3289 | // warning is not too noisy. |
| 3290 | if (!Method->isDefined()) |
| 3291 | List->setHasMoreThanOneDecl(true); |
Manman Ren | 051d0b6 | 2016-04-13 23:43:56 +0000 | [diff] [blame] | 3292 | |
| 3293 | // For methods with the same declaration, the one that is deprecated |
| 3294 | // should be put in the front for better diagnostics. |
| 3295 | if (Method->isDeprecated() && SameDeclaration && |
| 3296 | !ListWithSameDeclaration && !List->getMethod()->isDeprecated()) |
| 3297 | ListWithSameDeclaration = List; |
| 3298 | |
| 3299 | if (Method->isUnavailable() && SameDeclaration && |
| 3300 | !ListWithSameDeclaration && |
| 3301 | List->getMethod()->getAvailability() < AR_Deprecated) |
| 3302 | ListWithSameDeclaration = List; |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3303 | continue; |
Fariborz Jahanian | d436b2a | 2015-04-07 16:56:27 +0000 | [diff] [blame] | 3304 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3305 | |
| 3306 | ObjCMethodDecl *PrevObjCMethod = List->getMethod(); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3307 | |
| 3308 | // Propagate the 'defined' bit. |
| 3309 | if (Method->isDefined()) |
| 3310 | PrevObjCMethod->setDefined(true); |
Nico Weber | e3b1104 | 2014-12-27 07:09:37 +0000 | [diff] [blame] | 3311 | else { |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3312 | // Objective-C doesn't allow an @interface for a class after its |
| 3313 | // @implementation. So if Method is not defined and there already is |
| 3314 | // an entry for this type signature, Method has to be for a different |
| 3315 | // class than PrevObjCMethod. |
| 3316 | List->setHasMoreThanOneDecl(true); |
| 3317 | } |
| 3318 | |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3319 | // If a method is deprecated, push it in the global pool. |
| 3320 | // This is used for better diagnostics. |
| 3321 | if (Method->isDeprecated()) { |
| 3322 | if (!PrevObjCMethod->isDeprecated()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3323 | List->setMethod(Method); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3324 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3325 | // If the new method is unavailable, push it into global pool |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3326 | // unless previous one is deprecated. |
| 3327 | if (Method->isUnavailable()) { |
| 3328 | if (PrevObjCMethod->getAvailability() < AR_Deprecated) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3329 | List->setMethod(Method); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3330 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3331 | |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3332 | return; |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3333 | } |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3334 | |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3335 | // We have a new signature for an existing method - add it. |
| 3336 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 3337 | ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>(); |
Manman Ren | 7122453 | 2016-04-09 18:59:48 +0000 | [diff] [blame] | 3338 | |
Manman Ren | 051d0b6 | 2016-04-13 23:43:56 +0000 | [diff] [blame] | 3339 | // We insert it right before ListWithSameDeclaration. |
| 3340 | if (ListWithSameDeclaration) { |
| 3341 | auto *List = new (Mem) ObjCMethodList(*ListWithSameDeclaration); |
| 3342 | // FIXME: should we clear the other bits in ListWithSameDeclaration? |
| 3343 | ListWithSameDeclaration->setMethod(Method); |
| 3344 | ListWithSameDeclaration->setNext(List); |
Manman Ren | 7122453 | 2016-04-09 18:59:48 +0000 | [diff] [blame] | 3345 | return; |
| 3346 | } |
| 3347 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3348 | Previous->setNext(new (Mem) ObjCMethodList(Method)); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3351 | /// \brief Read the contents of the method pool for a given selector from |
| 3352 | /// external storage. |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 3353 | void Sema::ReadMethodPool(Selector Sel) { |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3354 | assert(ExternalSource && "We need an external AST source"); |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 3355 | ExternalSource->ReadMethodPool(Sel); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3356 | } |
| 3357 | |
Manman Ren | a0f31a0 | 2016-04-29 19:04:05 +0000 | [diff] [blame] | 3358 | void Sema::updateOutOfDateSelector(Selector Sel) { |
| 3359 | if (!ExternalSource) |
| 3360 | return; |
| 3361 | ExternalSource->updateOutOfDateSelector(Sel); |
| 3362 | } |
| 3363 | |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3364 | void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl, |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3365 | bool instance) { |
Argyrios Kyrtzidis | b15def2 | 2012-03-12 18:34:26 +0000 | [diff] [blame] | 3366 | // Ignore methods of invalid containers. |
| 3367 | if (cast<Decl>(Method->getDeclContext())->isInvalidDecl()) |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3368 | return; |
Argyrios Kyrtzidis | b15def2 | 2012-03-12 18:34:26 +0000 | [diff] [blame] | 3369 | |
Douglas Gregor | 70f449b | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 3370 | if (ExternalSource) |
| 3371 | ReadMethodPool(Method->getSelector()); |
| 3372 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3373 | GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector()); |
Douglas Gregor | 70f449b | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 3374 | if (Pos == MethodPool.end()) |
| 3375 | Pos = MethodPool.insert(std::make_pair(Method->getSelector(), |
| 3376 | GlobalMethods())).first; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3377 | |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 3378 | Method->setDefined(impl); |
Douglas Gregor | c454afe | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 3379 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3380 | ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second; |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3381 | addMethodToGlobalList(&Entry, Method); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3384 | /// Determines if this is an "acceptable" loose mismatch in the global |
| 3385 | /// method pool. This exists mostly as a hack to get around certain |
| 3386 | /// global mismatches which we can't afford to make warnings / errors. |
| 3387 | /// Really, what we want is a way to take a method out of the global |
| 3388 | /// method pool. |
| 3389 | static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen, |
| 3390 | ObjCMethodDecl *other) { |
| 3391 | if (!chosen->isInstanceMethod()) |
| 3392 | return false; |
| 3393 | |
| 3394 | Selector sel = chosen->getSelector(); |
| 3395 | if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length") |
| 3396 | return false; |
| 3397 | |
| 3398 | // Don't complain about mismatches for -length if the method we |
| 3399 | // chose has an integral result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3400 | return (chosen->getReturnType()->isIntegerType()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3401 | } |
| 3402 | |
Manman Ren | 7ed4f98 | 2016-04-07 19:32:24 +0000 | [diff] [blame] | 3403 | /// Return true if the given method is wthin the type bound. |
| 3404 | static bool FilterMethodsByTypeBound(ObjCMethodDecl *Method, |
| 3405 | const ObjCObjectType *TypeBound) { |
| 3406 | if (!TypeBound) |
| 3407 | return true; |
| 3408 | |
| 3409 | if (TypeBound->isObjCId()) |
| 3410 | // FIXME: should we handle the case of bounding to id<A, B> differently? |
| 3411 | return true; |
| 3412 | |
| 3413 | auto *BoundInterface = TypeBound->getInterface(); |
| 3414 | assert(BoundInterface && "unexpected object type!"); |
| 3415 | |
| 3416 | // Check if the Method belongs to a protocol. We should allow any method |
| 3417 | // defined in any protocol, because any subclass could adopt the protocol. |
| 3418 | auto *MethodProtocol = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext()); |
| 3419 | if (MethodProtocol) { |
| 3420 | return true; |
| 3421 | } |
| 3422 | |
| 3423 | // If the Method belongs to a class, check if it belongs to the class |
| 3424 | // hierarchy of the class bound. |
| 3425 | if (ObjCInterfaceDecl *MethodInterface = Method->getClassInterface()) { |
| 3426 | // We allow methods declared within classes that are part of the hierarchy |
| 3427 | // of the class bound (superclass of, subclass of, or the same as the class |
| 3428 | // bound). |
| 3429 | return MethodInterface == BoundInterface || |
| 3430 | MethodInterface->isSuperClassOf(BoundInterface) || |
| 3431 | BoundInterface->isSuperClassOf(MethodInterface); |
| 3432 | } |
| 3433 | llvm_unreachable("unknow method context"); |
| 3434 | } |
| 3435 | |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3436 | /// We first select the type of the method: Instance or Factory, then collect |
| 3437 | /// all methods with that type. |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3438 | bool Sema::CollectMultipleMethodsInGlobalPool( |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3439 | Selector Sel, SmallVectorImpl<ObjCMethodDecl *> &Methods, |
Manman Ren | 7ed4f98 | 2016-04-07 19:32:24 +0000 | [diff] [blame] | 3440 | bool InstanceFirst, bool CheckTheOther, |
| 3441 | const ObjCObjectType *TypeBound) { |
Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 3442 | if (ExternalSource) |
| 3443 | ReadMethodPool(Sel); |
| 3444 | |
| 3445 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
| 3446 | if (Pos == MethodPool.end()) |
| 3447 | return false; |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3448 | |
Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 3449 | // Gather the non-hidden methods. |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3450 | ObjCMethodList &MethList = InstanceFirst ? Pos->second.first : |
| 3451 | Pos->second.second; |
Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 3452 | for (ObjCMethodList *M = &MethList; M; M = M->getNext()) |
Manman Ren | 7ed4f98 | 2016-04-07 19:32:24 +0000 | [diff] [blame] | 3453 | if (M->getMethod() && !M->getMethod()->isHidden()) { |
| 3454 | if (FilterMethodsByTypeBound(M->getMethod(), TypeBound)) |
| 3455 | Methods.push_back(M->getMethod()); |
| 3456 | } |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3457 | |
| 3458 | // Return if we find any method with the desired kind. |
| 3459 | if (!Methods.empty()) |
| 3460 | return Methods.size() > 1; |
| 3461 | |
| 3462 | if (!CheckTheOther) |
| 3463 | return false; |
| 3464 | |
| 3465 | // Gather the other kind. |
| 3466 | ObjCMethodList &MethList2 = InstanceFirst ? Pos->second.second : |
| 3467 | Pos->second.first; |
| 3468 | for (ObjCMethodList *M = &MethList2; M; M = M->getNext()) |
Manman Ren | 7ed4f98 | 2016-04-07 19:32:24 +0000 | [diff] [blame] | 3469 | if (M->getMethod() && !M->getMethod()->isHidden()) { |
| 3470 | if (FilterMethodsByTypeBound(M->getMethod(), TypeBound)) |
| 3471 | Methods.push_back(M->getMethod()); |
| 3472 | } |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3473 | |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3474 | return Methods.size() > 1; |
Fariborz Jahanian | 30ae8d4 | 2014-08-13 21:07:35 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 3477 | bool Sema::AreMultipleMethodsInGlobalPool( |
| 3478 | Selector Sel, ObjCMethodDecl *BestMethod, SourceRange R, |
| 3479 | bool receiverIdOrClass, SmallVectorImpl<ObjCMethodDecl *> &Methods) { |
| 3480 | // Diagnose finding more than one method in global pool. |
| 3481 | SmallVector<ObjCMethodDecl *, 4> FilteredMethods; |
| 3482 | FilteredMethods.push_back(BestMethod); |
| 3483 | |
| 3484 | for (auto *M : Methods) |
| 3485 | if (M != BestMethod && !M->hasAttr<UnavailableAttr>()) |
| 3486 | FilteredMethods.push_back(M); |
| 3487 | |
| 3488 | if (FilteredMethods.size() > 1) |
| 3489 | DiagnoseMultipleMethodInGlobalPool(FilteredMethods, Sel, R, |
| 3490 | receiverIdOrClass); |
| 3491 | |
Fariborz Jahanian | c62d16f | 2014-11-13 22:27:05 +0000 | [diff] [blame] | 3492 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3493 | // Test for no method in the pool which should not trigger any warning by |
| 3494 | // caller. |
Fariborz Jahanian | c62d16f | 2014-11-13 22:27:05 +0000 | [diff] [blame] | 3495 | if (Pos == MethodPool.end()) |
| 3496 | return true; |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3497 | ObjCMethodList &MethList = |
| 3498 | BestMethod->isInstanceMethod() ? Pos->second.first : Pos->second.second; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3499 | return MethList.hasMoreThanOneDecl(); |
Fariborz Jahanian | c62d16f | 2014-11-13 22:27:05 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3502 | ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R, |
Fariborz Jahanian | 3337b2e | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 3503 | bool receiverIdOrClass, |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3504 | bool instance) { |
Douglas Gregor | 70f449b | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 3505 | if (ExternalSource) |
| 3506 | ReadMethodPool(Sel); |
| 3507 | |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3508 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
Douglas Gregor | 70f449b | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 3509 | if (Pos == MethodPool.end()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3510 | return nullptr; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3511 | |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3512 | // Gather the non-hidden methods. |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3513 | ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second; |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 3514 | SmallVector<ObjCMethodDecl *, 4> Methods; |
Argyrios Kyrtzidis | d3da6e0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 3515 | for (ObjCMethodList *M = &MethList; M; M = M->getNext()) { |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3516 | if (M->getMethod() && !M->getMethod()->isHidden()) |
| 3517 | return M->getMethod(); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3518 | } |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3519 | return nullptr; |
| 3520 | } |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3521 | |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3522 | void Sema::DiagnoseMultipleMethodInGlobalPool(SmallVectorImpl<ObjCMethodDecl*> &Methods, |
| 3523 | Selector Sel, SourceRange R, |
| 3524 | bool receiverIdOrClass) { |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3525 | // We found multiple methods, so we may have to complain. |
| 3526 | bool issueDiagnostic = false, issueError = false; |
Jonathan Roelofs | 7441136 | 2015-04-28 18:04:44 +0000 | [diff] [blame] | 3527 | |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3528 | // We support a warning which complains about *any* difference in |
| 3529 | // method signature. |
| 3530 | bool strictSelectorMatch = |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3531 | receiverIdOrClass && |
| 3532 | !Diags.isIgnored(diag::warn_strict_multiple_method_decl, R.getBegin()); |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3533 | if (strictSelectorMatch) { |
| 3534 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 3535 | if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) { |
| 3536 | issueDiagnostic = true; |
| 3537 | break; |
| 3538 | } |
| 3539 | } |
| 3540 | } |
Jonathan Roelofs | 7441136 | 2015-04-28 18:04:44 +0000 | [diff] [blame] | 3541 | |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3542 | // If we didn't see any strict differences, we won't see any loose |
| 3543 | // differences. In ARC, however, we also need to check for loose |
| 3544 | // mismatches, because most of them are errors. |
| 3545 | if (!strictSelectorMatch || |
| 3546 | (issueDiagnostic && getLangOpts().ObjCAutoRefCount)) |
| 3547 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 3548 | // This checks if the methods differ in type mismatch. |
| 3549 | if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) && |
| 3550 | !isAcceptableMethodMismatch(Methods[0], Methods[I])) { |
| 3551 | issueDiagnostic = true; |
| 3552 | if (getLangOpts().ObjCAutoRefCount) |
| 3553 | issueError = true; |
| 3554 | break; |
| 3555 | } |
| 3556 | } |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3557 | |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3558 | if (issueDiagnostic) { |
| 3559 | if (issueError) |
| 3560 | Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R; |
| 3561 | else if (strictSelectorMatch) |
| 3562 | Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R; |
| 3563 | else |
| 3564 | Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3565 | |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3566 | Diag(Methods[0]->getLocStart(), |
| 3567 | issueError ? diag::note_possibility : diag::note_using) |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3568 | << Methods[0]->getSourceRange(); |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3569 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 3570 | Diag(Methods[I]->getLocStart(), diag::note_also_found) |
Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 3571 | << Methods[I]->getSourceRange(); |
| 3572 | } |
Douglas Gregor | 77f49a4 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 3573 | } |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 3576 | ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) { |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3577 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
| 3578 | if (Pos == MethodPool.end()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3579 | return nullptr; |
Sebastian Redl | 75d8a32 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 3580 | |
| 3581 | GlobalMethods &Methods = Pos->second; |
Fariborz Jahanian | ec762bd | 2014-03-26 20:59:26 +0000 | [diff] [blame] | 3582 | for (const ObjCMethodList *Method = &Methods.first; Method; |
| 3583 | Method = Method->getNext()) |
Fariborz Jahanian | 4019c7f | 2015-02-19 21:52:41 +0000 | [diff] [blame] | 3584 | if (Method->getMethod() && |
| 3585 | (Method->getMethod()->isDefined() || |
| 3586 | Method->getMethod()->isPropertyAccessor())) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3587 | return Method->getMethod(); |
Fariborz Jahanian | ec762bd | 2014-03-26 20:59:26 +0000 | [diff] [blame] | 3588 | |
| 3589 | for (const ObjCMethodList *Method = &Methods.second; Method; |
| 3590 | Method = Method->getNext()) |
Fariborz Jahanian | 4019c7f | 2015-02-19 21:52:41 +0000 | [diff] [blame] | 3591 | if (Method->getMethod() && |
| 3592 | (Method->getMethod()->isDefined() || |
| 3593 | Method->getMethod()->isPropertyAccessor())) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3594 | return Method->getMethod(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3595 | return nullptr; |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 3596 | } |
| 3597 | |
Fariborz Jahanian | 42f8938 | 2013-05-30 21:48:58 +0000 | [diff] [blame] | 3598 | static void |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3599 | HelperSelectorsForTypoCorrection( |
| 3600 | SmallVectorImpl<const ObjCMethodDecl *> &BestMethod, |
| 3601 | StringRef Typo, const ObjCMethodDecl * Method) { |
| 3602 | const unsigned MaxEditDistance = 1; |
| 3603 | unsigned BestEditDistance = MaxEditDistance + 1; |
Richard Trieu | ea8d370 | 2013-06-06 02:22:29 +0000 | [diff] [blame] | 3604 | std::string MethodName = Method->getSelector().getAsString(); |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3605 | |
| 3606 | unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size()); |
| 3607 | if (MinPossibleEditDistance > 0 && |
| 3608 | Typo.size() / MinPossibleEditDistance < 1) |
| 3609 | return; |
| 3610 | unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance); |
| 3611 | if (EditDistance > MaxEditDistance) |
| 3612 | return; |
| 3613 | if (EditDistance == BestEditDistance) |
| 3614 | BestMethod.push_back(Method); |
| 3615 | else if (EditDistance < BestEditDistance) { |
| 3616 | BestMethod.clear(); |
| 3617 | BestMethod.push_back(Method); |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3618 | } |
| 3619 | } |
| 3620 | |
Fariborz Jahanian | 7548167 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 3621 | static bool HelperIsMethodInObjCType(Sema &S, Selector Sel, |
| 3622 | QualType ObjectType) { |
| 3623 | if (ObjectType.isNull()) |
| 3624 | return true; |
| 3625 | if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/)) |
| 3626 | return true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3627 | return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) != |
| 3628 | nullptr; |
Fariborz Jahanian | 7548167 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3631 | const ObjCMethodDecl * |
Fariborz Jahanian | 7548167 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 3632 | Sema::SelectorsForTypoCorrection(Selector Sel, |
| 3633 | QualType ObjectType) { |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3634 | unsigned NumArgs = Sel.getNumArgs(); |
| 3635 | SmallVector<const ObjCMethodDecl *, 8> Methods; |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3636 | bool ObjectIsId = true, ObjectIsClass = true; |
| 3637 | if (ObjectType.isNull()) |
| 3638 | ObjectIsId = ObjectIsClass = false; |
| 3639 | else if (!ObjectType->isObjCObjectPointerType()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3640 | return nullptr; |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3641 | else if (const ObjCObjectPointerType *ObjCPtr = |
| 3642 | ObjectType->getAsObjCInterfacePointerType()) { |
| 3643 | ObjectType = QualType(ObjCPtr->getInterfaceType(), 0); |
| 3644 | ObjectIsId = ObjectIsClass = false; |
| 3645 | } |
| 3646 | else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType()) |
| 3647 | ObjectIsClass = false; |
| 3648 | else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType()) |
| 3649 | ObjectIsId = false; |
| 3650 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3651 | return nullptr; |
| 3652 | |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3653 | for (GlobalMethodPool::iterator b = MethodPool.begin(), |
| 3654 | e = MethodPool.end(); b != e; b++) { |
| 3655 | // instance methods |
| 3656 | for (ObjCMethodList *M = &b->second.first; M; M=M->getNext()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3657 | if (M->getMethod() && |
| 3658 | (M->getMethod()->getSelector().getNumArgs() == NumArgs) && |
| 3659 | (M->getMethod()->getSelector() != Sel)) { |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3660 | if (ObjectIsId) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3661 | Methods.push_back(M->getMethod()); |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3662 | else if (!ObjectIsClass && |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3663 | HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(), |
| 3664 | ObjectType)) |
| 3665 | Methods.push_back(M->getMethod()); |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3666 | } |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3667 | // class methods |
| 3668 | for (ObjCMethodList *M = &b->second.second; M; M=M->getNext()) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3669 | if (M->getMethod() && |
| 3670 | (M->getMethod()->getSelector().getNumArgs() == NumArgs) && |
| 3671 | (M->getMethod()->getSelector() != Sel)) { |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3672 | if (ObjectIsClass) |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3673 | Methods.push_back(M->getMethod()); |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3674 | else if (!ObjectIsId && |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 3675 | HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(), |
| 3676 | ObjectType)) |
| 3677 | Methods.push_back(M->getMethod()); |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 3678 | } |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3679 | } |
| 3680 | |
| 3681 | SmallVector<const ObjCMethodDecl *, 8> SelectedMethods; |
| 3682 | for (unsigned i = 0, e = Methods.size(); i < e; i++) { |
| 3683 | HelperSelectorsForTypoCorrection(SelectedMethods, |
| 3684 | Sel.getAsString(), Methods[i]); |
| 3685 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3686 | return (SelectedMethods.size() == 1) ? SelectedMethods[0] : nullptr; |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 3687 | } |
| 3688 | |
Fariborz Jahanian | 42f8938 | 2013-05-30 21:48:58 +0000 | [diff] [blame] | 3689 | /// DiagnoseDuplicateIvars - |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 3690 | /// Check for duplicate ivars in the entire class at the start of |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 3691 | /// \@implementation. This becomes necesssary because class extension can |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 3692 | /// 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] | 3693 | /// class's \@implementation is seen. |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 3694 | void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID, |
| 3695 | ObjCInterfaceDecl *SID) { |
Aaron Ballman | 59abbd4 | 2014-03-13 21:09:43 +0000 | [diff] [blame] | 3696 | for (auto *Ivar : ID->ivars()) { |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 3697 | if (Ivar->isInvalidDecl()) |
| 3698 | continue; |
| 3699 | if (IdentifierInfo *II = Ivar->getIdentifier()) { |
| 3700 | ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II); |
| 3701 | if (prevIvar) { |
| 3702 | Diag(Ivar->getLocation(), diag::err_duplicate_member) << II; |
| 3703 | Diag(prevIvar->getLocation(), diag::note_previous_declaration); |
| 3704 | Ivar->setInvalidDecl(); |
| 3705 | } |
| 3706 | } |
| 3707 | } |
| 3708 | } |
| 3709 | |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 3710 | /// Diagnose attempts to define ARC-__weak ivars when __weak is disabled. |
| 3711 | static void DiagnoseWeakIvars(Sema &S, ObjCImplementationDecl *ID) { |
| 3712 | if (S.getLangOpts().ObjCWeak) return; |
| 3713 | |
| 3714 | for (auto ivar = ID->getClassInterface()->all_declared_ivar_begin(); |
| 3715 | ivar; ivar = ivar->getNextIvar()) { |
| 3716 | if (ivar->isInvalidDecl()) continue; |
| 3717 | if (ivar->getType().getObjCLifetime() == Qualifiers::OCL_Weak) { |
| 3718 | if (S.getLangOpts().ObjCWeakRuntime) { |
| 3719 | S.Diag(ivar->getLocation(), diag::err_arc_weak_disabled); |
| 3720 | } else { |
| 3721 | S.Diag(ivar->getLocation(), diag::err_arc_weak_no_runtime); |
| 3722 | } |
| 3723 | } |
| 3724 | } |
| 3725 | } |
| 3726 | |
Volodymyr Sapsai | 30680e9 | 2017-10-23 22:01:41 +0000 | [diff] [blame] | 3727 | /// Diagnose attempts to use flexible array member with retainable object type. |
| 3728 | static void DiagnoseRetainableFlexibleArrayMember(Sema &S, |
| 3729 | ObjCInterfaceDecl *ID) { |
| 3730 | if (!S.getLangOpts().ObjCAutoRefCount) |
| 3731 | return; |
| 3732 | |
| 3733 | for (auto ivar = ID->all_declared_ivar_begin(); ivar; |
| 3734 | ivar = ivar->getNextIvar()) { |
| 3735 | if (ivar->isInvalidDecl()) |
| 3736 | continue; |
| 3737 | QualType IvarTy = ivar->getType(); |
| 3738 | if (IvarTy->isIncompleteArrayType() && |
| 3739 | (IvarTy.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) && |
| 3740 | IvarTy->isObjCLifetimeType()) { |
| 3741 | S.Diag(ivar->getLocation(), diag::err_flexible_array_arc_retainable); |
| 3742 | ivar->setInvalidDecl(); |
| 3743 | } |
| 3744 | } |
| 3745 | } |
| 3746 | |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3747 | Sema::ObjCContainerKind Sema::getObjCContainerKind() const { |
| 3748 | switch (CurContext->getDeclKind()) { |
| 3749 | case Decl::ObjCInterface: |
| 3750 | return Sema::OCK_Interface; |
| 3751 | case Decl::ObjCProtocol: |
| 3752 | return Sema::OCK_Protocol; |
| 3753 | case Decl::ObjCCategory: |
Benjamin Kramer | a008d3a | 2015-04-10 11:37:55 +0000 | [diff] [blame] | 3754 | if (cast<ObjCCategoryDecl>(CurContext)->IsClassExtension()) |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3755 | return Sema::OCK_ClassExtension; |
Benjamin Kramer | a008d3a | 2015-04-10 11:37:55 +0000 | [diff] [blame] | 3756 | return Sema::OCK_Category; |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3757 | case Decl::ObjCImplementation: |
| 3758 | return Sema::OCK_Implementation; |
| 3759 | case Decl::ObjCCategoryImpl: |
| 3760 | return Sema::OCK_CategoryImplementation; |
| 3761 | |
| 3762 | default: |
| 3763 | return Sema::OCK_None; |
| 3764 | } |
| 3765 | } |
| 3766 | |
Volodymyr Sapsai | 30680e9 | 2017-10-23 22:01:41 +0000 | [diff] [blame] | 3767 | static bool IsVariableSizedType(QualType T) { |
| 3768 | if (T->isIncompleteArrayType()) |
| 3769 | return true; |
| 3770 | const auto *RecordTy = T->getAs<RecordType>(); |
| 3771 | return (RecordTy && RecordTy->getDecl()->hasFlexibleArrayMember()); |
| 3772 | } |
| 3773 | |
| 3774 | static void DiagnoseVariableSizedIvars(Sema &S, ObjCContainerDecl *OCD) { |
| 3775 | ObjCInterfaceDecl *IntfDecl = nullptr; |
| 3776 | ObjCInterfaceDecl::ivar_range Ivars = llvm::make_range( |
| 3777 | ObjCInterfaceDecl::ivar_iterator(), ObjCInterfaceDecl::ivar_iterator()); |
| 3778 | if ((IntfDecl = dyn_cast<ObjCInterfaceDecl>(OCD))) { |
| 3779 | Ivars = IntfDecl->ivars(); |
| 3780 | } else if (auto *ImplDecl = dyn_cast<ObjCImplementationDecl>(OCD)) { |
| 3781 | IntfDecl = ImplDecl->getClassInterface(); |
| 3782 | Ivars = ImplDecl->ivars(); |
| 3783 | } else if (auto *CategoryDecl = dyn_cast<ObjCCategoryDecl>(OCD)) { |
| 3784 | if (CategoryDecl->IsClassExtension()) { |
| 3785 | IntfDecl = CategoryDecl->getClassInterface(); |
| 3786 | Ivars = CategoryDecl->ivars(); |
| 3787 | } |
| 3788 | } |
| 3789 | |
| 3790 | // Check if variable sized ivar is in interface and visible to subclasses. |
| 3791 | if (!isa<ObjCInterfaceDecl>(OCD)) { |
| 3792 | for (auto ivar : Ivars) { |
| 3793 | if (!ivar->isInvalidDecl() && IsVariableSizedType(ivar->getType())) { |
| 3794 | S.Diag(ivar->getLocation(), diag::warn_variable_sized_ivar_visibility) |
| 3795 | << ivar->getDeclName() << ivar->getType(); |
| 3796 | } |
| 3797 | } |
| 3798 | } |
| 3799 | |
| 3800 | // Subsequent checks require interface decl. |
| 3801 | if (!IntfDecl) |
| 3802 | return; |
| 3803 | |
| 3804 | // Check if variable sized ivar is followed by another ivar. |
| 3805 | for (ObjCIvarDecl *ivar = IntfDecl->all_declared_ivar_begin(); ivar; |
| 3806 | ivar = ivar->getNextIvar()) { |
| 3807 | if (ivar->isInvalidDecl() || !ivar->getNextIvar()) |
| 3808 | continue; |
| 3809 | QualType IvarTy = ivar->getType(); |
| 3810 | bool IsInvalidIvar = false; |
| 3811 | if (IvarTy->isIncompleteArrayType()) { |
| 3812 | S.Diag(ivar->getLocation(), diag::err_flexible_array_not_at_end) |
| 3813 | << ivar->getDeclName() << IvarTy |
| 3814 | << TTK_Class; // Use "class" for Obj-C. |
| 3815 | IsInvalidIvar = true; |
| 3816 | } else if (const RecordType *RecordTy = IvarTy->getAs<RecordType>()) { |
| 3817 | if (RecordTy->getDecl()->hasFlexibleArrayMember()) { |
| 3818 | S.Diag(ivar->getLocation(), |
| 3819 | diag::err_objc_variable_sized_type_not_at_end) |
| 3820 | << ivar->getDeclName() << IvarTy; |
| 3821 | IsInvalidIvar = true; |
| 3822 | } |
| 3823 | } |
| 3824 | if (IsInvalidIvar) { |
| 3825 | S.Diag(ivar->getNextIvar()->getLocation(), |
| 3826 | diag::note_next_ivar_declaration) |
| 3827 | << ivar->getNextIvar()->getSynthesize(); |
| 3828 | ivar->setInvalidDecl(); |
| 3829 | } |
| 3830 | } |
| 3831 | |
| 3832 | // Check if ObjC container adds ivars after variable sized ivar in superclass. |
| 3833 | // Perform the check only if OCD is the first container to declare ivars to |
| 3834 | // avoid multiple warnings for the same ivar. |
| 3835 | ObjCIvarDecl *FirstIvar = |
| 3836 | (Ivars.begin() == Ivars.end()) ? nullptr : *Ivars.begin(); |
| 3837 | if (FirstIvar && (FirstIvar == IntfDecl->all_declared_ivar_begin())) { |
| 3838 | const ObjCInterfaceDecl *SuperClass = IntfDecl->getSuperClass(); |
| 3839 | while (SuperClass && SuperClass->ivar_empty()) |
| 3840 | SuperClass = SuperClass->getSuperClass(); |
| 3841 | if (SuperClass) { |
| 3842 | auto IvarIter = SuperClass->ivar_begin(); |
| 3843 | std::advance(IvarIter, SuperClass->ivar_size() - 1); |
| 3844 | const ObjCIvarDecl *LastIvar = *IvarIter; |
| 3845 | if (IsVariableSizedType(LastIvar->getType())) { |
| 3846 | S.Diag(FirstIvar->getLocation(), |
| 3847 | diag::warn_superclass_variable_sized_type_not_at_end) |
| 3848 | << FirstIvar->getDeclName() << LastIvar->getDeclName() |
| 3849 | << LastIvar->getType() << SuperClass->getDeclName(); |
| 3850 | S.Diag(LastIvar->getLocation(), diag::note_entity_declared_at) |
| 3851 | << LastIvar->getDeclName(); |
| 3852 | } |
| 3853 | } |
| 3854 | } |
| 3855 | } |
| 3856 | |
Fariborz Jahanian | 0080fb5 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 3857 | // Note: For class/category implementations, allMethods is always null. |
Robert Wilhelm | 57c6711 | 2013-07-17 21:14:35 +0000 | [diff] [blame] | 3858 | Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods, |
Fariborz Jahanian | dfb7687 | 2013-07-17 00:05:08 +0000 | [diff] [blame] | 3859 | ArrayRef<DeclGroupPtrTy> allTUVars) { |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3860 | if (getObjCContainerKind() == Sema::OCK_None) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3861 | return nullptr; |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 3862 | |
| 3863 | assert(AtEnd.isValid() && "Invalid location for '@end'"); |
| 3864 | |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 3865 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); |
| 3866 | Decl *ClassDecl = cast<Decl>(OCD); |
Fariborz Jahanian | 9290ede | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 3867 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3868 | bool isInterfaceDeclKind = |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 3869 | isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) |
| 3870 | || isa<ObjCProtocolDecl>(ClassDecl); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3871 | bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 3872 | |
Steve Naroff | 35c62ae | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 3873 | // FIXME: Remove these and use the ObjCContainerDecl/DeclContext. |
| 3874 | llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; |
| 3875 | llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; |
| 3876 | |
Fariborz Jahanian | 0080fb5 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 3877 | for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3878 | ObjCMethodDecl *Method = |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3879 | cast_or_null<ObjCMethodDecl>(allMethods[i]); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3880 | |
| 3881 | if (!Method) continue; // Already issued a diagnostic. |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 3882 | if (Method->isInstanceMethod()) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3883 | /// Check for instance method of the same name with incompatible types |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3884 | const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3885 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3886 | : false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3887 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 42b1e9e | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 3888 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3889 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3890 | << Method->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3891 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | 87e9275 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 3892 | Method->setInvalidDecl(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3893 | } else { |
Fariborz Jahanian | c17c86b | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 3894 | if (PrevMethod) { |
Argyrios Kyrtzidis | dcaaa21 | 2011-10-14 08:02:31 +0000 | [diff] [blame] | 3895 | Method->setAsRedeclaration(PrevMethod); |
Fariborz Jahanian | c17c86b | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 3896 | if (!Context.getSourceManager().isInSystemHeader( |
| 3897 | Method->getLocation())) |
| 3898 | Diag(Method->getLocation(), diag::warn_duplicate_method_decl) |
| 3899 | << Method->getDeclName(); |
| 3900 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 3901 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3902 | InsMap[Method->getSelector()] = Method; |
| 3903 | /// The following allows us to typecheck messages to "id". |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3904 | AddInstanceMethodToGlobalPool(Method); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3905 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3906 | } else { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3907 | /// Check for class method of the same name with incompatible types |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3908 | const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3909 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3910 | : false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3911 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 42b1e9e | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 3912 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3913 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3914 | << Method->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3915 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | 87e9275 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 3916 | Method->setInvalidDecl(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3917 | } else { |
Fariborz Jahanian | c17c86b | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 3918 | if (PrevMethod) { |
Argyrios Kyrtzidis | dcaaa21 | 2011-10-14 08:02:31 +0000 | [diff] [blame] | 3919 | Method->setAsRedeclaration(PrevMethod); |
Fariborz Jahanian | c17c86b | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 3920 | if (!Context.getSourceManager().isInSystemHeader( |
| 3921 | Method->getLocation())) |
| 3922 | Diag(Method->getLocation(), diag::warn_duplicate_method_decl) |
| 3923 | << Method->getDeclName(); |
| 3924 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 3925 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3926 | ClsMap[Method->getSelector()] = Method; |
Douglas Gregor | 0e6fc1a | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 3927 | AddFactoryMethodToGlobalPool(Method); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3928 | } |
| 3929 | } |
| 3930 | } |
Douglas Gregor | b898209 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 3931 | if (isa<ObjCInterfaceDecl>(ClassDecl)) { |
| 3932 | // Nothing to do here. |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 3933 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Fariborz Jahanian | 62293f4 | 2008-12-06 19:59:02 +0000 | [diff] [blame] | 3934 | // Categories are used to extend the class by declaring new methods. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3935 | // 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] | 3936 | // need to compare the added property to those in the class. |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 3937 | |
Fariborz Jahanian | c21f543 | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 3938 | if (C->IsClassExtension()) { |
| 3939 | ObjCInterfaceDecl *CCPrimary = C->getClassInterface(); |
| 3940 | DiagnoseClassExtensionDupMethods(C, CCPrimary); |
Fariborz Jahanian | c21f543 | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 3941 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3942 | } |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 3943 | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) { |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 3944 | if (CDecl->getIdentifier()) |
| 3945 | // ProcessPropertyDecl is responsible for diagnosing conflicts with any |
| 3946 | // user-defined setter/getter. It also synthesizes setter/getter methods |
| 3947 | // and adds them to the DeclContext and global method pools. |
Manman Ren | efe1bac | 2016-01-27 20:00:32 +0000 | [diff] [blame] | 3948 | for (auto *I : CDecl->properties()) |
Douglas Gregor | e17765e | 2015-11-03 17:02:34 +0000 | [diff] [blame] | 3949 | ProcessPropertyDecl(I); |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 3950 | CDecl->setAtEndRange(AtEnd); |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 3951 | } |
| 3952 | if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 3953 | IC->setAtEndRange(AtEnd); |
Fariborz Jahanian | 13e0c90 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 3954 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) { |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 3955 | // Any property declared in a class extension might have user |
| 3956 | // declared setter or getter in current class extension or one |
| 3957 | // of the other class extensions. Mark them as synthesized as |
| 3958 | // property will be synthesized when property with same name is |
| 3959 | // seen in the @implementation. |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 3960 | for (const auto *Ext : IDecl->visible_extensions()) { |
Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame] | 3961 | for (const auto *Property : Ext->instance_properties()) { |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 3962 | // Skip over properties declared @dynamic |
| 3963 | if (const ObjCPropertyImplDecl *PIDecl |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 3964 | = IC->FindPropertyImplDecl(Property->getIdentifier(), |
| 3965 | Property->getQueryKind())) |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 3966 | if (PIDecl->getPropertyImplementation() |
| 3967 | == ObjCPropertyImplDecl::Dynamic) |
| 3968 | continue; |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 3969 | |
Aaron Ballman | f53d8dd | 2014-03-13 21:47:07 +0000 | [diff] [blame] | 3970 | for (const auto *Ext : IDecl->visible_extensions()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 3971 | if (ObjCMethodDecl *GetterMethod |
| 3972 | = Ext->getInstanceMethod(Property->getGetterName())) |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 3973 | GetterMethod->setPropertyAccessor(true); |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 3974 | if (!Property->isReadOnly()) |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 3975 | if (ObjCMethodDecl *SetterMethod |
| 3976 | = Ext->getInstanceMethod(Property->getSetterName())) |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 3977 | SetterMethod->setPropertyAccessor(true); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 3978 | } |
Fariborz Jahanian | 5d7e916 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 3979 | } |
| 3980 | } |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 3981 | ImplMethodsVsClassMethods(S, IC, IDecl); |
Fariborz Jahanian | 13e0c90 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 3982 | AtomicPropertySetterGetterRules(IC, IDecl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3983 | DiagnoseOwningPropertyGetterSynthesis(IC); |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 3984 | DiagnoseUnusedBackingIvarInAccessor(S, IC); |
Fariborz Jahanian | 20cfff3 | 2015-03-11 16:59:48 +0000 | [diff] [blame] | 3985 | if (IDecl->hasDesignatedInitializers()) |
| 3986 | DiagnoseMissingDesignatedInitOverrides(IC, IDecl); |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 3987 | DiagnoseWeakIvars(*this, IC); |
Volodymyr Sapsai | 30680e9 | 2017-10-23 22:01:41 +0000 | [diff] [blame] | 3988 | DiagnoseRetainableFlexibleArrayMember(*this, IDecl); |
Argyrios Kyrtzidis | db5ce0f | 2013-12-03 21:11:54 +0000 | [diff] [blame] | 3989 | |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 3990 | bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3991 | if (IDecl->getSuperClass() == nullptr) { |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 3992 | // This class has no superclass, so check that it has been marked with |
| 3993 | // __attribute((objc_root_class)). |
| 3994 | if (!HasRootClassAttr) { |
| 3995 | SourceLocation DeclLoc(IDecl->getLocation()); |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 3996 | SourceLocation SuperClassLoc(getLocForEndOfToken(DeclLoc)); |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 3997 | Diag(DeclLoc, diag::warn_objc_root_class_missing) |
| 3998 | << IDecl->getIdentifier(); |
| 3999 | // See if NSObject is in the current scope, and if it is, suggest |
| 4000 | // adding " : NSObject " to the class declaration. |
| 4001 | NamedDecl *IF = LookupSingleName(TUScope, |
| 4002 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject), |
| 4003 | DeclLoc, LookupOrdinaryName); |
| 4004 | ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
| 4005 | if (NSObjectDecl && NSObjectDecl->getDefinition()) { |
| 4006 | Diag(SuperClassLoc, diag::note_objc_needs_superclass) |
| 4007 | << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject "); |
| 4008 | } else { |
| 4009 | Diag(SuperClassLoc, diag::note_objc_needs_superclass); |
| 4010 | } |
| 4011 | } |
| 4012 | } else if (HasRootClassAttr) { |
| 4013 | // Complain that only root classes may have this attribute. |
| 4014 | Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass); |
| 4015 | } |
| 4016 | |
Alex Lorenz | a8c44ba | 2016-10-28 10:25:10 +0000 | [diff] [blame] | 4017 | if (const ObjCInterfaceDecl *Super = IDecl->getSuperClass()) { |
| 4018 | // An interface can subclass another interface with a |
| 4019 | // objc_subclassing_restricted attribute when it has that attribute as |
| 4020 | // well (because of interfaces imported from Swift). Therefore we have |
| 4021 | // to check if we can subclass in the implementation as well. |
| 4022 | if (IDecl->hasAttr<ObjCSubclassingRestrictedAttr>() && |
| 4023 | Super->hasAttr<ObjCSubclassingRestrictedAttr>()) { |
| 4024 | Diag(IC->getLocation(), diag::err_restricted_superclass_mismatch); |
| 4025 | Diag(Super->getLocation(), diag::note_class_declared); |
| 4026 | } |
| 4027 | } |
| 4028 | |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 4029 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | 545643c | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 4030 | while (IDecl->getSuperClass()) { |
| 4031 | DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass()); |
| 4032 | IDecl = IDecl->getSuperClass(); |
| 4033 | } |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4034 | } |
Fariborz Jahanian | 13e0c90 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 4035 | } |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 4036 | SetIvarInitializers(IC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4037 | } else if (ObjCCategoryImplDecl* CatImplClass = |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 4038 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 4039 | CatImplClass->setAtEndRange(AtEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4040 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4041 | // Find category interface decl and then check that all methods declared |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 4042 | // in this interface are implemented in the category @implementation. |
Chris Lattner | 41fd42e | 2009-02-16 18:32:47 +0000 | [diff] [blame] | 4043 | if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 4044 | if (ObjCCategoryDecl *Cat |
| 4045 | = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) { |
| 4046 | ImplMethodsVsClassMethods(S, CatImplClass, Cat); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4047 | } |
| 4048 | } |
Alex Lorenz | a8c44ba | 2016-10-28 10:25:10 +0000 | [diff] [blame] | 4049 | } else if (const auto *IntfDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
| 4050 | if (const ObjCInterfaceDecl *Super = IntfDecl->getSuperClass()) { |
| 4051 | if (!IntfDecl->hasAttr<ObjCSubclassingRestrictedAttr>() && |
| 4052 | Super->hasAttr<ObjCSubclassingRestrictedAttr>()) { |
| 4053 | Diag(IntfDecl->getLocation(), diag::err_restricted_superclass_mismatch); |
| 4054 | Diag(Super->getLocation(), diag::note_class_declared); |
| 4055 | } |
| 4056 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4057 | } |
Volodymyr Sapsai | 30680e9 | 2017-10-23 22:01:41 +0000 | [diff] [blame] | 4058 | DiagnoseVariableSizedIvars(*this, OCD); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 4059 | if (isInterfaceDeclKind) { |
| 4060 | // Reject invalid vardecls. |
Fariborz Jahanian | 0080fb5 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 4061 | for (unsigned i = 0, e = allTUVars.size(); i != e; i++) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 4062 | DeclGroupRef DG = allTUVars[i].get(); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 4063 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 4064 | if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) { |
Daniel Dunbar | 0ca1660 | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 4065 | if (!VDecl->hasExternalStorage()) |
Steve Naroff | 42959b2 | 2009-04-13 17:58:46 +0000 | [diff] [blame] | 4066 | Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass); |
Fariborz Jahanian | 629aed9 | 2009-03-21 18:06:45 +0000 | [diff] [blame] | 4067 | } |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 4068 | } |
Fariborz Jahanian | 3654e65 | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 4069 | } |
Fariborz Jahanian | 4327b32 | 2011-08-29 17:33:12 +0000 | [diff] [blame] | 4070 | ActOnObjCContainerFinishDefinition(); |
Argyrios Kyrtzidis | bd8b150 | 2011-10-17 19:48:13 +0000 | [diff] [blame] | 4071 | |
Fariborz Jahanian | 0080fb5 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 4072 | for (unsigned i = 0, e = allTUVars.size(); i != e; i++) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 4073 | DeclGroupRef DG = allTUVars[i].get(); |
Argyrios Kyrtzidis | 8ad3bab | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 4074 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 4075 | (*I)->setTopLevelDeclInObjCContainer(); |
Argyrios Kyrtzidis | bd8b150 | 2011-10-17 19:48:13 +0000 | [diff] [blame] | 4076 | Consumer.HandleTopLevelDeclInObjCContainer(DG); |
| 4077 | } |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 4078 | |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 4079 | ActOnDocumentableDecl(ClassDecl); |
Erik Verbruggen | c6c8d93 | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 4080 | return ClassDecl; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4083 | /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for |
| 4084 | /// 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] | 4085 | static Decl::ObjCDeclQualifier |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4086 | CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { |
John McCall | ca87290 | 2011-05-01 03:04:29 +0000 | [diff] [blame] | 4087 | return (Decl::ObjCDeclQualifier) (unsigned) PQTVal; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4088 | } |
| 4089 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4090 | /// \brief Check whether the declared result type of the given Objective-C |
| 4091 | /// method declaration is compatible with the method's class. |
| 4092 | /// |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4093 | static Sema::ResultTypeCompatibilityKind |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4094 | CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method, |
| 4095 | ObjCInterfaceDecl *CurrentClass) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4096 | QualType ResultType = Method->getReturnType(); |
| 4097 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4098 | // If an Objective-C method inherits its related result type, then its |
| 4099 | // declared result type must be compatible with its own class type. The |
| 4100 | // declared result type is compatible if: |
| 4101 | if (const ObjCObjectPointerType *ResultObjectType |
| 4102 | = ResultType->getAs<ObjCObjectPointerType>()) { |
| 4103 | // - it is id or qualified id, or |
| 4104 | if (ResultObjectType->isObjCIdType() || |
| 4105 | ResultObjectType->isObjCQualifiedIdType()) |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4106 | return Sema::RTC_Compatible; |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4107 | |
| 4108 | if (CurrentClass) { |
| 4109 | if (ObjCInterfaceDecl *ResultClass |
| 4110 | = ResultObjectType->getInterfaceDecl()) { |
| 4111 | // - it is the same as the method's class type, or |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 4112 | if (declaresSameEntity(CurrentClass, ResultClass)) |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4113 | return Sema::RTC_Compatible; |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4114 | |
| 4115 | // - it is a superclass of the method's class type |
| 4116 | if (ResultClass->isSuperClassOf(CurrentClass)) |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4117 | return Sema::RTC_Compatible; |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4118 | } |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4119 | } else { |
| 4120 | // Any Objective-C pointer type might be acceptable for a protocol |
| 4121 | // method; we just don't know. |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4122 | return Sema::RTC_Unknown; |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4123 | } |
| 4124 | } |
| 4125 | |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4126 | return Sema::RTC_Incompatible; |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4127 | } |
| 4128 | |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4129 | namespace { |
| 4130 | /// A helper class for searching for methods which a particular method |
| 4131 | /// overrides. |
| 4132 | class OverrideSearch { |
Daniel Dunbar | d6d74c3 | 2012-02-29 03:04:05 +0000 | [diff] [blame] | 4133 | public: |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4134 | Sema &S; |
| 4135 | ObjCMethodDecl *Method; |
Daniel Dunbar | d6d74c3 | 2012-02-29 03:04:05 +0000 | [diff] [blame] | 4136 | llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4137 | bool Recursive; |
| 4138 | |
| 4139 | public: |
| 4140 | OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) { |
| 4141 | Selector selector = method->getSelector(); |
| 4142 | |
| 4143 | // Bypass this search if we've never seen an instance/class method |
| 4144 | // with this selector before. |
| 4145 | Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector); |
| 4146 | if (it == S.MethodPool.end()) { |
Axel Naumann | dd433f0 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 4147 | if (!S.getExternalSource()) return; |
Douglas Gregor | e171601 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 4148 | S.ReadMethodPool(selector); |
| 4149 | |
| 4150 | it = S.MethodPool.find(selector); |
| 4151 | if (it == S.MethodPool.end()) |
| 4152 | return; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4153 | } |
| 4154 | ObjCMethodList &list = |
| 4155 | method->isInstanceMethod() ? it->second.first : it->second.second; |
Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 4156 | if (!list.getMethod()) return; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4157 | |
| 4158 | ObjCContainerDecl *container |
| 4159 | = cast<ObjCContainerDecl>(method->getDeclContext()); |
| 4160 | |
| 4161 | // Prevent the search from reaching this container again. This is |
| 4162 | // important with categories, which override methods from the |
| 4163 | // interface and each other. |
Douglas Gregor | cf4ac44 | 2012-05-03 21:25:24 +0000 | [diff] [blame] | 4164 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) { |
| 4165 | searchFromContainer(container); |
Douglas Gregor | c5928af | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 4166 | if (ObjCInterfaceDecl *Interface = Category->getClassInterface()) |
| 4167 | searchFromContainer(Interface); |
Douglas Gregor | cf4ac44 | 2012-05-03 21:25:24 +0000 | [diff] [blame] | 4168 | } else { |
| 4169 | searchFromContainer(container); |
| 4170 | } |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4171 | } |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4172 | |
Matthias Braun | 1d03007 | 2016-01-30 01:27:06 +0000 | [diff] [blame] | 4173 | typedef llvm::SmallPtrSetImpl<ObjCMethodDecl*>::iterator iterator; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4174 | iterator begin() const { return Overridden.begin(); } |
| 4175 | iterator end() const { return Overridden.end(); } |
| 4176 | |
| 4177 | private: |
| 4178 | void searchFromContainer(ObjCContainerDecl *container) { |
| 4179 | if (container->isInvalidDecl()) return; |
| 4180 | |
| 4181 | switch (container->getDeclKind()) { |
| 4182 | #define OBJCCONTAINER(type, base) \ |
| 4183 | case Decl::type: \ |
| 4184 | searchFrom(cast<type##Decl>(container)); \ |
| 4185 | break; |
| 4186 | #define ABSTRACT_DECL(expansion) |
| 4187 | #define DECL(type, base) \ |
| 4188 | case Decl::type: |
| 4189 | #include "clang/AST/DeclNodes.inc" |
| 4190 | llvm_unreachable("not an ObjC container!"); |
| 4191 | } |
| 4192 | } |
| 4193 | |
| 4194 | void searchFrom(ObjCProtocolDecl *protocol) { |
Douglas Gregor | e6e48b1 | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 4195 | if (!protocol->hasDefinition()) |
| 4196 | return; |
| 4197 | |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4198 | // A method in a protocol declaration overrides declarations from |
| 4199 | // referenced ("parent") protocols. |
| 4200 | search(protocol->getReferencedProtocols()); |
| 4201 | } |
| 4202 | |
| 4203 | void searchFrom(ObjCCategoryDecl *category) { |
| 4204 | // A method in a category declaration overrides declarations from |
| 4205 | // the main class and from protocols the category references. |
Douglas Gregor | cf4ac44 | 2012-05-03 21:25:24 +0000 | [diff] [blame] | 4206 | // The main class is handled in the constructor. |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4207 | search(category->getReferencedProtocols()); |
| 4208 | } |
| 4209 | |
| 4210 | void searchFrom(ObjCCategoryImplDecl *impl) { |
| 4211 | // A method in a category definition that has a category |
| 4212 | // declaration overrides declarations from the category |
| 4213 | // declaration. |
| 4214 | if (ObjCCategoryDecl *category = impl->getCategoryDecl()) { |
| 4215 | search(category); |
Douglas Gregor | c5928af | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 4216 | if (ObjCInterfaceDecl *Interface = category->getClassInterface()) |
| 4217 | search(Interface); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4218 | |
| 4219 | // Otherwise it overrides declarations from the class. |
Douglas Gregor | c5928af | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 4220 | } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) { |
| 4221 | search(Interface); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4222 | } |
| 4223 | } |
| 4224 | |
| 4225 | void searchFrom(ObjCInterfaceDecl *iface) { |
| 4226 | // A method in a class declaration overrides declarations from |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 4227 | if (!iface->hasDefinition()) |
| 4228 | return; |
| 4229 | |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4230 | // - categories, |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 4231 | for (auto *Cat : iface->known_categories()) |
| 4232 | search(Cat); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4233 | |
| 4234 | // - the super class, and |
| 4235 | if (ObjCInterfaceDecl *super = iface->getSuperClass()) |
| 4236 | search(super); |
| 4237 | |
| 4238 | // - any referenced protocols. |
| 4239 | search(iface->getReferencedProtocols()); |
| 4240 | } |
| 4241 | |
| 4242 | void searchFrom(ObjCImplementationDecl *impl) { |
| 4243 | // A method in a class implementation overrides declarations from |
| 4244 | // the class interface. |
Douglas Gregor | c5928af | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 4245 | if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) |
| 4246 | search(Interface); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4247 | } |
| 4248 | |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4249 | void search(const ObjCProtocolList &protocols) { |
| 4250 | for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end(); |
| 4251 | i != e; ++i) |
| 4252 | search(*i); |
| 4253 | } |
| 4254 | |
| 4255 | void search(ObjCContainerDecl *container) { |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4256 | // Check for a method in this container which matches this selector. |
| 4257 | ObjCMethodDecl *meth = container->getMethod(Method->getSelector(), |
Argyrios Kyrtzidis | bd8cd3e | 2013-03-29 21:51:48 +0000 | [diff] [blame] | 4258 | Method->isInstanceMethod(), |
| 4259 | /*AllowHidden=*/true); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4260 | |
| 4261 | // If we find one, record it and bail out. |
| 4262 | if (meth) { |
| 4263 | Overridden.insert(meth); |
| 4264 | return; |
| 4265 | } |
| 4266 | |
| 4267 | // Otherwise, search for methods that a hypothetical method here |
| 4268 | // would have overridden. |
| 4269 | |
| 4270 | // Note that we're now in a recursive case. |
| 4271 | Recursive = true; |
| 4272 | |
| 4273 | searchFromContainer(container); |
| 4274 | } |
| 4275 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 4276 | } // end anonymous namespace |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4277 | |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4278 | void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod, |
| 4279 | ObjCInterfaceDecl *CurrentClass, |
| 4280 | ResultTypeCompatibilityKind RTC) { |
| 4281 | // Search for overridden methods and merge information down from them. |
| 4282 | OverrideSearch overrides(*this, ObjCMethod); |
| 4283 | // Keep track if the method overrides any method in the class's base classes, |
| 4284 | // its protocols, or its categories' protocols; we will keep that info |
| 4285 | // in the ObjCMethodDecl. |
| 4286 | // For this info, a method in an implementation is not considered as |
| 4287 | // overriding the same method in the interface or its categories. |
| 4288 | bool hasOverriddenMethodsInBaseOrProtocol = false; |
| 4289 | for (OverrideSearch::iterator |
| 4290 | i = overrides.begin(), e = overrides.end(); i != e; ++i) { |
| 4291 | ObjCMethodDecl *overridden = *i; |
| 4292 | |
Argyrios Kyrtzidis | c2091d5 | 2013-04-17 00:09:08 +0000 | [diff] [blame] | 4293 | if (!hasOverriddenMethodsInBaseOrProtocol) { |
| 4294 | if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) || |
| 4295 | CurrentClass != overridden->getClassInterface() || |
| 4296 | overridden->isOverriding()) { |
| 4297 | hasOverriddenMethodsInBaseOrProtocol = true; |
| 4298 | |
| 4299 | } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) { |
| 4300 | // OverrideSearch will return as "overridden" the same method in the |
| 4301 | // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to |
| 4302 | // check whether a category of a base class introduced a method with the |
| 4303 | // same selector, after the interface method declaration. |
| 4304 | // To avoid unnecessary lookups in the majority of cases, we use the |
| 4305 | // extra info bits in GlobalMethodPool to check whether there were any |
| 4306 | // category methods with this selector. |
| 4307 | GlobalMethodPool::iterator It = |
| 4308 | MethodPool.find(ObjCMethod->getSelector()); |
| 4309 | if (It != MethodPool.end()) { |
| 4310 | ObjCMethodList &List = |
| 4311 | ObjCMethod->isInstanceMethod()? It->second.first: It->second.second; |
| 4312 | unsigned CategCount = List.getBits(); |
| 4313 | if (CategCount > 0) { |
| 4314 | // If the method is in a category we'll do lookup if there were at |
| 4315 | // least 2 category methods recorded, otherwise only one will do. |
| 4316 | if (CategCount > 1 || |
| 4317 | !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) { |
| 4318 | OverrideSearch overrides(*this, overridden); |
| 4319 | for (OverrideSearch::iterator |
| 4320 | OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) { |
| 4321 | ObjCMethodDecl *SuperOverridden = *OI; |
Argyrios Kyrtzidis | 04703a6 | 2013-04-27 00:10:12 +0000 | [diff] [blame] | 4322 | if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) || |
| 4323 | CurrentClass != SuperOverridden->getClassInterface()) { |
Argyrios Kyrtzidis | c2091d5 | 2013-04-17 00:09:08 +0000 | [diff] [blame] | 4324 | hasOverriddenMethodsInBaseOrProtocol = true; |
| 4325 | overridden->setOverriding(true); |
| 4326 | break; |
| 4327 | } |
| 4328 | } |
| 4329 | } |
| 4330 | } |
| 4331 | } |
| 4332 | } |
| 4333 | } |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4334 | |
| 4335 | // Propagate down the 'related result type' bit from overridden methods. |
| 4336 | if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType()) |
| 4337 | ObjCMethod->SetRelatedResultType(); |
| 4338 | |
| 4339 | // Then merge the declarations. |
| 4340 | mergeObjCMethodDecls(ObjCMethod, overridden); |
Akira Hatanaka | 73cab88 | 2017-10-12 23:24:38 +0000 | [diff] [blame] | 4341 | } |
| 4342 | |
| 4343 | for (ObjCMethodDecl *overridden : overrides) { |
| 4344 | CheckObjCMethodOverride(ObjCMethod, overridden); |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4345 | |
| 4346 | if (ObjCMethod->isImplicit() && overridden->isImplicit()) |
| 4347 | continue; // Conflicting properties are detected elsewhere. |
| 4348 | |
| 4349 | // Check for overriding methods |
| 4350 | if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) || |
| 4351 | isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) |
| 4352 | CheckConflictingOverridingMethod(ObjCMethod, overridden, |
| 4353 | isa<ObjCProtocolDecl>(overridden->getDeclContext())); |
| 4354 | |
| 4355 | if (CurrentClass && overridden->getDeclContext() != CurrentClass && |
Fariborz Jahanian | 31a2568 | 2012-07-05 22:26:07 +0000 | [diff] [blame] | 4356 | isa<ObjCInterfaceDecl>(overridden->getDeclContext()) && |
| 4357 | !overridden->isImplicit() /* not meant for properties */) { |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4358 | ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(), |
| 4359 | E = ObjCMethod->param_end(); |
Douglas Gregor | 0bf70f4 | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 4360 | ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(), |
| 4361 | PrevE = overridden->param_end(); |
| 4362 | for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) { |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4363 | assert(PrevI != overridden->param_end() && "Param mismatch"); |
| 4364 | QualType T1 = Context.getCanonicalType((*ParamI)->getType()); |
| 4365 | QualType T2 = Context.getCanonicalType((*PrevI)->getType()); |
| 4366 | // If type of argument of method in this class does not match its |
| 4367 | // respective argument type in the super class method, issue warning; |
| 4368 | if (!Context.typesAreCompatible(T1, T2)) { |
| 4369 | Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super) |
| 4370 | << T1 << T2; |
| 4371 | Diag(overridden->getLocation(), diag::note_previous_declaration); |
| 4372 | break; |
| 4373 | } |
| 4374 | } |
| 4375 | } |
| 4376 | } |
| 4377 | |
| 4378 | ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol); |
| 4379 | } |
| 4380 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4381 | /// Merge type nullability from for a redeclaration of the same entity, |
| 4382 | /// producing the updated type of the redeclared entity. |
| 4383 | static QualType mergeTypeNullabilityForRedecl(Sema &S, SourceLocation loc, |
| 4384 | QualType type, |
| 4385 | bool usesCSKeyword, |
| 4386 | SourceLocation prevLoc, |
| 4387 | QualType prevType, |
| 4388 | bool prevUsesCSKeyword) { |
| 4389 | // Determine the nullability of both types. |
| 4390 | auto nullability = type->getNullability(S.Context); |
| 4391 | auto prevNullability = prevType->getNullability(S.Context); |
| 4392 | |
| 4393 | // Easy case: both have nullability. |
| 4394 | if (nullability.hasValue() == prevNullability.hasValue()) { |
| 4395 | // Neither has nullability; continue. |
| 4396 | if (!nullability) |
| 4397 | return type; |
| 4398 | |
| 4399 | // The nullabilities are equivalent; do nothing. |
| 4400 | if (*nullability == *prevNullability) |
| 4401 | return type; |
| 4402 | |
| 4403 | // Complain about mismatched nullability. |
| 4404 | S.Diag(loc, diag::err_nullability_conflicting) |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 4405 | << DiagNullabilityKind(*nullability, usesCSKeyword) |
| 4406 | << DiagNullabilityKind(*prevNullability, prevUsesCSKeyword); |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4407 | return type; |
| 4408 | } |
| 4409 | |
| 4410 | // If it's the redeclaration that has nullability, don't change anything. |
| 4411 | if (nullability) |
| 4412 | return type; |
| 4413 | |
| 4414 | // Otherwise, provide the result with the same nullability. |
| 4415 | return S.Context.getAttributedType( |
| 4416 | AttributedType::getNullabilityAttrKind(*prevNullability), |
| 4417 | type, type); |
| 4418 | } |
| 4419 | |
NAKAMURA Takumi | 2df5c3c | 2015-06-20 03:52:52 +0000 | [diff] [blame] | 4420 | /// Merge information from the declaration of a method in the \@interface |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4421 | /// (or a category/extension) into the corresponding method in the |
| 4422 | /// @implementation (for a class or category). |
| 4423 | static void mergeInterfaceMethodToImpl(Sema &S, |
| 4424 | ObjCMethodDecl *method, |
| 4425 | ObjCMethodDecl *prevMethod) { |
| 4426 | // Merge the objc_requires_super attribute. |
| 4427 | if (prevMethod->hasAttr<ObjCRequiresSuperAttr>() && |
| 4428 | !method->hasAttr<ObjCRequiresSuperAttr>()) { |
| 4429 | // merge the attribute into implementation. |
| 4430 | method->addAttr( |
| 4431 | ObjCRequiresSuperAttr::CreateImplicit(S.Context, |
| 4432 | method->getLocation())); |
| 4433 | } |
| 4434 | |
| 4435 | // Merge nullability of the result type. |
| 4436 | QualType newReturnType |
| 4437 | = mergeTypeNullabilityForRedecl( |
| 4438 | S, method->getReturnTypeSourceRange().getBegin(), |
| 4439 | method->getReturnType(), |
| 4440 | method->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability, |
| 4441 | prevMethod->getReturnTypeSourceRange().getBegin(), |
| 4442 | prevMethod->getReturnType(), |
| 4443 | prevMethod->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability); |
| 4444 | method->setReturnType(newReturnType); |
| 4445 | |
| 4446 | // Handle each of the parameters. |
| 4447 | unsigned numParams = method->param_size(); |
| 4448 | unsigned numPrevParams = prevMethod->param_size(); |
| 4449 | for (unsigned i = 0, n = std::min(numParams, numPrevParams); i != n; ++i) { |
| 4450 | ParmVarDecl *param = method->param_begin()[i]; |
| 4451 | ParmVarDecl *prevParam = prevMethod->param_begin()[i]; |
| 4452 | |
| 4453 | // Merge nullability. |
| 4454 | QualType newParamType |
| 4455 | = mergeTypeNullabilityForRedecl( |
| 4456 | S, param->getLocation(), param->getType(), |
| 4457 | param->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability, |
| 4458 | prevParam->getLocation(), prevParam->getType(), |
| 4459 | prevParam->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability); |
| 4460 | param->setType(newParamType); |
| 4461 | } |
| 4462 | } |
| 4463 | |
Alex Lorenz | a8a372d | 2017-04-27 10:43:48 +0000 | [diff] [blame] | 4464 | /// Verify that the method parameters/return value have types that are supported |
| 4465 | /// by the x86 target. |
| 4466 | static void checkObjCMethodX86VectorTypes(Sema &SemaRef, |
| 4467 | const ObjCMethodDecl *Method) { |
| 4468 | assert(SemaRef.getASTContext().getTargetInfo().getTriple().getArch() == |
| 4469 | llvm::Triple::x86 && |
| 4470 | "x86-specific check invoked for a different target"); |
| 4471 | SourceLocation Loc; |
| 4472 | QualType T; |
| 4473 | for (const ParmVarDecl *P : Method->parameters()) { |
| 4474 | if (P->getType()->isVectorType()) { |
| 4475 | Loc = P->getLocStart(); |
| 4476 | T = P->getType(); |
| 4477 | break; |
| 4478 | } |
| 4479 | } |
| 4480 | if (Loc.isInvalid()) { |
| 4481 | if (Method->getReturnType()->isVectorType()) { |
| 4482 | Loc = Method->getReturnTypeSourceRange().getBegin(); |
| 4483 | T = Method->getReturnType(); |
| 4484 | } else |
| 4485 | return; |
| 4486 | } |
| 4487 | |
| 4488 | // Vector parameters/return values are not supported by objc_msgSend on x86 in |
| 4489 | // iOS < 9 and macOS < 10.11. |
| 4490 | const auto &Triple = SemaRef.getASTContext().getTargetInfo().getTriple(); |
| 4491 | VersionTuple AcceptedInVersion; |
| 4492 | if (Triple.getOS() == llvm::Triple::IOS) |
| 4493 | AcceptedInVersion = VersionTuple(/*Major=*/9); |
| 4494 | else if (Triple.isMacOSX()) |
| 4495 | AcceptedInVersion = VersionTuple(/*Major=*/10, /*Minor=*/11); |
| 4496 | else |
| 4497 | return; |
Alex Lorenz | a8a372d | 2017-04-27 10:43:48 +0000 | [diff] [blame] | 4498 | if (SemaRef.getASTContext().getTargetInfo().getPlatformMinVersion() >= |
Alex Lorenz | 9282483 | 2017-05-05 16:15:17 +0000 | [diff] [blame] | 4499 | AcceptedInVersion) |
Alex Lorenz | a8a372d | 2017-04-27 10:43:48 +0000 | [diff] [blame] | 4500 | return; |
| 4501 | SemaRef.Diag(Loc, diag::err_objc_method_unsupported_param_ret_type) |
| 4502 | << T << (Method->getReturnType()->isVectorType() ? /*return value*/ 1 |
| 4503 | : /*parameter*/ 0) |
| 4504 | << (Triple.isMacOSX() ? "macOS 10.11" : "iOS 9"); |
| 4505 | } |
| 4506 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4507 | Decl *Sema::ActOnMethodDeclaration( |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4508 | Scope *S, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4509 | SourceLocation MethodLoc, SourceLocation EndLoc, |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4510 | tok::TokenKind MethodType, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4511 | ObjCDeclSpec &ReturnQT, ParsedType ReturnType, |
Argyrios Kyrtzidis | dfd6570 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 4512 | ArrayRef<SourceLocation> SelectorLocs, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4513 | Selector Sel, |
| 4514 | // optional arguments. The number of types/arguments is obtained |
| 4515 | // from the Sel.getNumArgs(). |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 4516 | ObjCArgInfo *ArgInfo, |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 4517 | DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4518 | AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, |
Fariborz Jahanian | c677f69 | 2011-03-12 18:54:30 +0000 | [diff] [blame] | 4519 | bool isVariadic, bool MethodDefinition) { |
Steve Naroff | 83777fe | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 4520 | // Make sure we can establish a context for the method. |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4521 | if (!CurContext->isObjCContainer()) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 4522 | Diag(MethodLoc, diag::err_missing_method_context); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4523 | return nullptr; |
Steve Naroff | 83777fe | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 4524 | } |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4525 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); |
| 4526 | Decl *ClassDecl = cast<Decl>(OCD); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4527 | QualType resultDeclType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4528 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4529 | bool HasRelatedResultType = false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4530 | TypeSourceInfo *ReturnTInfo = nullptr; |
Steve Naroff | 3260641 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 4531 | if (ReturnType) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4532 | resultDeclType = GetTypeFromParser(ReturnType, &ReturnTInfo); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4533 | |
Eli Friedman | 31a5bcc | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 4534 | if (CheckFunctionReturnType(resultDeclType, MethodLoc)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4535 | return nullptr; |
Eli Friedman | 31a5bcc | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 4536 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4537 | QualType bareResultType = resultDeclType; |
| 4538 | (void)AttributedType::stripOuterNullability(bareResultType); |
| 4539 | HasRelatedResultType = (bareResultType == Context.getObjCInstanceType()); |
Fariborz Jahanian | b5a52ca | 2011-07-21 17:00:47 +0000 | [diff] [blame] | 4540 | } else { // get the type for "id". |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4541 | resultDeclType = Context.getObjCIdType(); |
Fariborz Jahanian | b21138f | 2011-07-21 17:38:14 +0000 | [diff] [blame] | 4542 | Diag(MethodLoc, diag::warn_missing_method_return_type) |
Argyrios Kyrtzidis | dfd6570 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 4543 | << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)"); |
Fariborz Jahanian | b5a52ca | 2011-07-21 17:00:47 +0000 | [diff] [blame] | 4544 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4545 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4546 | ObjCMethodDecl *ObjCMethod = ObjCMethodDecl::Create( |
| 4547 | Context, MethodLoc, EndLoc, Sel, resultDeclType, ReturnTInfo, CurContext, |
| 4548 | MethodType == tok::minus, isVariadic, |
| 4549 | /*isPropertyAccessor=*/false, |
| 4550 | /*isImplicitlyDeclared=*/false, /*isDefined=*/false, |
| 4551 | MethodDeclKind == tok::objc_optional ? ObjCMethodDecl::Optional |
| 4552 | : ObjCMethodDecl::Required, |
| 4553 | HasRelatedResultType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4554 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4555 | SmallVector<ParmVarDecl*, 16> Params; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4556 | |
Chris Lattner | 23b0faf | 2009-04-11 19:42:43 +0000 | [diff] [blame] | 4557 | for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) { |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 4558 | QualType ArgType; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4559 | TypeSourceInfo *DI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4560 | |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 4561 | if (!ArgInfo[i].Type) { |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 4562 | ArgType = Context.getObjCIdType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4563 | DI = nullptr; |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 4564 | } else { |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 4565 | ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI); |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 4566 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4567 | |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4568 | LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 4569 | LookupOrdinaryName, forRedeclarationInCurContext()); |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4570 | LookupName(R, S); |
| 4571 | if (R.isSingleResult()) { |
| 4572 | NamedDecl *PrevDecl = R.getFoundDecl(); |
| 4573 | if (S->isDeclScope(PrevDecl)) { |
Fariborz Jahanian | c677f69 | 2011-03-12 18:54:30 +0000 | [diff] [blame] | 4574 | Diag(ArgInfo[i].NameLoc, |
| 4575 | (MethodDefinition ? diag::warn_method_param_redefinition |
| 4576 | : diag::warn_method_param_declaration)) |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4577 | << ArgInfo[i].Name; |
| 4578 | Diag(PrevDecl->getLocation(), |
| 4579 | diag::note_previous_declaration); |
| 4580 | } |
| 4581 | } |
| 4582 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4583 | SourceLocation StartLoc = DI |
| 4584 | ? DI->getTypeLoc().getBeginLoc() |
| 4585 | : ArgInfo[i].NameLoc; |
| 4586 | |
John McCall | d44f4d7 | 2011-04-23 02:46:06 +0000 | [diff] [blame] | 4587 | ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc, |
| 4588 | ArgInfo[i].NameLoc, ArgInfo[i].Name, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4589 | ArgType, DI, SC_None); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4590 | |
John McCall | 8249083 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 4591 | Param->setObjCMethodScopeInfo(i); |
| 4592 | |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 4593 | Param->setObjCDeclQualifier( |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 4594 | CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4595 | |
Chris Lattner | 9713a1c | 2009-04-11 19:34:56 +0000 | [diff] [blame] | 4596 | // Apply the attributes to the parameter. |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4597 | ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 4598 | AddPragmaAttributes(TUScope, Param); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4599 | |
Fariborz Jahanian | 52d02f6 | 2012-01-14 18:44:35 +0000 | [diff] [blame] | 4600 | if (Param->hasAttr<BlocksAttr>()) { |
| 4601 | Diag(Param->getLocation(), diag::err_block_on_nonlocal); |
| 4602 | Param->setInvalidDecl(); |
| 4603 | } |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4604 | S->AddDecl(Param); |
| 4605 | IdResolver.AddDecl(Param); |
| 4606 | |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 4607 | Params.push_back(Param); |
| 4608 | } |
Fariborz Jahanian | ca3566f | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 4609 | |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 4610 | for (unsigned i = 0, e = CNumArgs; i != e; ++i) { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4611 | ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param); |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 4612 | QualType ArgType = Param->getType(); |
| 4613 | if (ArgType.isNull()) |
| 4614 | ArgType = Context.getObjCIdType(); |
| 4615 | else |
| 4616 | // 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] | 4617 | ArgType = Context.getAdjustedParameterType(ArgType); |
Eli Friedman | 31a5bcc | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 4618 | |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 4619 | Param->setDeclContext(ObjCMethod); |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 4620 | Params.push_back(Param); |
| 4621 | } |
| 4622 | |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4623 | ObjCMethod->setMethodParams(Context, Params, SelectorLocs); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4624 | ObjCMethod->setObjCDeclQualifier( |
| 4625 | CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 4626 | |
| 4627 | if (AttrList) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4628 | ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 4629 | AddPragmaAttributes(TUScope, ObjCMethod); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4630 | |
Douglas Gregor | 87e9275 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 4631 | // Add the method now. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4632 | const ObjCMethodDecl *PrevMethod = nullptr; |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4633 | if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4634 | if (MethodType == tok::minus) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4635 | PrevMethod = ImpDecl->getInstanceMethod(Sel); |
| 4636 | ImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4637 | } else { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4638 | PrevMethod = ImpDecl->getClassMethod(Sel); |
| 4639 | ImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4640 | } |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4641 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 4642 | // Merge information from the @interface declaration into the |
| 4643 | // @implementation. |
| 4644 | if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface()) { |
| 4645 | if (auto *IMD = IDecl->lookupMethod(ObjCMethod->getSelector(), |
| 4646 | ObjCMethod->isInstanceMethod())) { |
| 4647 | mergeInterfaceMethodToImpl(*this, ObjCMethod, IMD); |
| 4648 | |
| 4649 | // Warn about defining -dealloc in a category. |
| 4650 | if (isa<ObjCCategoryImplDecl>(ImpDecl) && IMD->isOverriding() && |
| 4651 | ObjCMethod->getSelector().getMethodFamily() == OMF_dealloc) { |
| 4652 | Diag(ObjCMethod->getLocation(), diag::warn_dealloc_in_category) |
| 4653 | << ObjCMethod->getDeclName(); |
| 4654 | } |
| 4655 | } |
Fariborz Jahanian | 7e350d2 | 2013-12-17 22:44:28 +0000 | [diff] [blame] | 4656 | } |
Douglas Gregor | 87e9275 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 4657 | } else { |
| 4658 | cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4659 | } |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4660 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4661 | if (PrevMethod) { |
| 4662 | // You can never have two method definitions with the same name. |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 4663 | Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4664 | << ObjCMethod->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 4665 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Fariborz Jahanian | 096f7c1 | 2013-05-13 17:27:00 +0000 | [diff] [blame] | 4666 | ObjCMethod->setInvalidDecl(); |
| 4667 | return ObjCMethod; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4668 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4669 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4670 | // If this Objective-C method does not have a related result type, but we |
| 4671 | // are allowed to infer related result types, try to do so based on the |
| 4672 | // method family. |
| 4673 | ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl); |
| 4674 | if (!CurrentClass) { |
| 4675 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) |
| 4676 | CurrentClass = Cat->getClassInterface(); |
| 4677 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl)) |
| 4678 | CurrentClass = Impl->getClassInterface(); |
| 4679 | else if (ObjCCategoryImplDecl *CatImpl |
| 4680 | = dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) |
| 4681 | CurrentClass = CatImpl->getClassInterface(); |
| 4682 | } |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4683 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4684 | ResultTypeCompatibilityKind RTC |
| 4685 | = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4686 | |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4687 | CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC); |
John McCall | d2930c2 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 4688 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4689 | bool ARCError = false; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4690 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | e48f389 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 4691 | ARCError = CheckARCMethodDecl(ObjCMethod); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4692 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4693 | // Infer the related result type when possible. |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 4694 | if (!ARCError && RTC == Sema::RTC_Compatible && |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4695 | !ObjCMethod->hasRelatedResultType() && |
| 4696 | LangOpts.ObjCInferRelatedResultType) { |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4697 | bool InferRelatedResultType = false; |
| 4698 | switch (ObjCMethod->getMethodFamily()) { |
| 4699 | case OMF_None: |
| 4700 | case OMF_copy: |
| 4701 | case OMF_dealloc: |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 4702 | case OMF_finalize: |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4703 | case OMF_mutableCopy: |
| 4704 | case OMF_release: |
| 4705 | case OMF_retainCount: |
Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 4706 | case OMF_initialize: |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 4707 | case OMF_performSelector: |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4708 | break; |
| 4709 | |
| 4710 | case OMF_alloc: |
| 4711 | case OMF_new: |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 4712 | InferRelatedResultType = ObjCMethod->isClassMethod(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4713 | break; |
| 4714 | |
| 4715 | case OMF_init: |
| 4716 | case OMF_autorelease: |
| 4717 | case OMF_retain: |
| 4718 | case OMF_self: |
| 4719 | InferRelatedResultType = ObjCMethod->isInstanceMethod(); |
| 4720 | break; |
| 4721 | } |
| 4722 | |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 4723 | if (InferRelatedResultType && |
| 4724 | !ObjCMethod->getReturnType()->isObjCIndependentClassType()) |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4725 | ObjCMethod->SetRelatedResultType(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4726 | } |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 4727 | |
Alex Lorenz | a8a372d | 2017-04-27 10:43:48 +0000 | [diff] [blame] | 4728 | if (MethodDefinition && |
| 4729 | Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86) |
| 4730 | checkObjCMethodX86VectorTypes(*this, ObjCMethod); |
| 4731 | |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 4732 | ActOnDocumentableDecl(ObjCMethod); |
| 4733 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4734 | return ObjCMethod; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 4735 | } |
| 4736 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4737 | bool Sema::CheckObjCDeclScope(Decl *D) { |
Fariborz Jahanian | f36734d | 2011-08-22 18:34:22 +0000 | [diff] [blame] | 4738 | // Following is also an error. But it is caused by a missing @end |
| 4739 | // and diagnostic is issued elsewhere. |
Argyrios Kyrtzidis | 822c433 | 2012-03-23 23:24:23 +0000 | [diff] [blame] | 4740 | if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4741 | return false; |
Argyrios Kyrtzidis | 822c433 | 2012-03-23 23:24:23 +0000 | [diff] [blame] | 4742 | |
| 4743 | // If we switched context to translation unit while we are still lexically in |
| 4744 | // an objc container, it means the parser missed emitting an error. |
| 4745 | if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext())) |
| 4746 | return false; |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 4747 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 4748 | Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); |
| 4749 | D->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4750 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 4751 | return true; |
| 4752 | } |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4753 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4754 | /// Called whenever \@defs(ClassName) is encountered in the source. Inserts the |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4755 | /// instance variables of ClassName into Decls. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4756 | void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart, |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4757 | IdentifierInfo *ClassName, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4758 | SmallVectorImpl<Decl*> &Decls) { |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4759 | // Check that ClassName is a valid class |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4760 | ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4761 | if (!Class) { |
| 4762 | Diag(DeclStart, diag::err_undef_interface) << ClassName; |
| 4763 | return; |
| 4764 | } |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 4765 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | ece1b2b | 2009-04-21 20:28:41 +0000 | [diff] [blame] | 4766 | Diag(DeclStart, diag::err_atdef_nonfragile_interface); |
| 4767 | return; |
| 4768 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4769 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4770 | // Collect the instance variables |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4771 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4772 | Context.DeepCollectObjCIvars(Class, true, Ivars); |
Fariborz Jahanian | 7dae114 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 4773 | // For each ivar, create a fresh ObjCAtDefsFieldDecl. |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4774 | for (unsigned i = 0; i < Ivars.size(); i++) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4775 | const FieldDecl* ID = cast<FieldDecl>(Ivars[i]); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4776 | RecordDecl *Record = dyn_cast<RecordDecl>(TagD); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4777 | Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, |
| 4778 | /*FIXME: StartL=*/ID->getLocation(), |
| 4779 | ID->getLocation(), |
Fariborz Jahanian | 7dae114 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 4780 | ID->getIdentifier(), ID->getType(), |
| 4781 | ID->getBitWidth()); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4782 | Decls.push_back(FD); |
Fariborz Jahanian | 7dae114 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 4783 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4784 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4785 | // Introduce all of these fields into the appropriate scope. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4786 | for (SmallVectorImpl<Decl*>::iterator D = Decls.begin(); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4787 | D != Decls.end(); ++D) { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4788 | FieldDecl *FD = cast<FieldDecl>(*D); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4789 | if (getLangOpts().CPlusPlus) |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4790 | PushOnScopeChains(cast<FieldDecl>(FD), S); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4791 | else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD)) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4792 | Record->addDecl(FD); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4793 | } |
| 4794 | } |
| 4795 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4796 | /// \brief Build a type-check a new Objective-C exception variable declaration. |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4797 | VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T, |
| 4798 | SourceLocation StartLoc, |
| 4799 | SourceLocation IdLoc, |
| 4800 | IdentifierInfo *Id, |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4801 | bool Invalid) { |
| 4802 | // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage |
| 4803 | // duration shall not be qualified by an address-space qualifier." |
| 4804 | // Since all parameters have automatic store duration, they can not have |
| 4805 | // an address space. |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 4806 | if (T.getAddressSpace() != LangAS::Default) { |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4807 | Diag(IdLoc, diag::err_arg_with_address_space); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4808 | Invalid = true; |
| 4809 | } |
| 4810 | |
| 4811 | // An @catch parameter must be an unqualified object pointer type; |
| 4812 | // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"? |
| 4813 | if (Invalid) { |
| 4814 | // Don't do any further checking. |
Douglas Gregor | f4e837f | 2010-04-26 17:57:08 +0000 | [diff] [blame] | 4815 | } else if (T->isDependentType()) { |
| 4816 | // Okay: we don't know what this type will instantiate to. |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4817 | } else if (!T->isObjCObjectPointerType()) { |
| 4818 | Invalid = true; |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4819 | Diag(IdLoc ,diag::err_catch_param_not_objc_type); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4820 | } else if (T->isObjCQualifiedIdType()) { |
| 4821 | Invalid = true; |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4822 | Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4823 | } |
| 4824 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4825 | VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4826 | T, TInfo, SC_None); |
Douglas Gregor | 3f324d56 | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 4827 | New->setExceptionVariable(true); |
| 4828 | |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 4829 | // In ARC, infer 'retaining' for variables of retainable type. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4830 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New)) |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 4831 | Invalid = true; |
| 4832 | |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4833 | if (Invalid) |
| 4834 | New->setInvalidDecl(); |
| 4835 | return New; |
| 4836 | } |
| 4837 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4838 | Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) { |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4839 | const DeclSpec &DS = D.getDeclSpec(); |
| 4840 | |
| 4841 | // We allow the "register" storage class on exception variables because |
| 4842 | // GCC did, but we drop it completely. Any other storage class is an error. |
| 4843 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
| 4844 | Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm) |
| 4845 | << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc())); |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4846 | } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) { |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4847 | Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm) |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4848 | << DeclSpec::getSpecifierName(SCS); |
| 4849 | } |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4850 | if (DS.isInlineSpecified()) |
| 4851 | Diag(DS.getInlineSpecLoc(), diag::err_inline_non_function) |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4852 | << getLangOpts().CPlusPlus17; |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4853 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
| 4854 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 4855 | diag::err_invalid_thread) |
| 4856 | << DeclSpec::getSpecifierName(TSCS); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4857 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 4858 | |
Richard Smith | b1402ae | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 4859 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4860 | |
| 4861 | // Check that there are no default arguments inside the type of this |
| 4862 | // exception object (C++ only). |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4863 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4864 | CheckExtraCXXDefaultArguments(D); |
| 4865 | |
Argyrios Kyrtzidis | ef7022f | 2011-06-28 03:01:15 +0000 | [diff] [blame] | 4866 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 4867 | QualType ExceptionType = TInfo->getType(); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4868 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4869 | VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, |
| 4870 | D.getSourceRange().getBegin(), |
| 4871 | D.getIdentifierLoc(), |
| 4872 | D.getIdentifier(), |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4873 | D.isInvalidType()); |
| 4874 | |
| 4875 | // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1). |
| 4876 | if (D.getCXXScopeSpec().isSet()) { |
| 4877 | Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm) |
| 4878 | << D.getCXXScopeSpec().getRange(); |
| 4879 | New->setInvalidDecl(); |
| 4880 | } |
| 4881 | |
| 4882 | // Add the parameter declaration into this scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4883 | S->AddDecl(New); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 4884 | if (D.getIdentifier()) |
| 4885 | IdResolver.AddDecl(New); |
| 4886 | |
| 4887 | ProcessDeclAttributes(S, New, D); |
| 4888 | |
| 4889 | if (New->hasAttr<BlocksAttr>()) |
| 4890 | Diag(New->getLocation(), diag::err_block_on_nonlocal); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4891 | return New; |
Douglas Gregor | e11ee11 | 2010-04-23 23:01:43 +0000 | [diff] [blame] | 4892 | } |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 4893 | |
| 4894 | /// CollectIvarsToConstructOrDestruct - Collect those ivars which require |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 4895 | /// initialization. |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4896 | void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4897 | SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4898 | for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv; |
| 4899 | Iv= Iv->getNextIvar()) { |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 4900 | QualType QT = Context.getBaseElementType(Iv->getType()); |
Douglas Gregor | 527786e | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 4901 | if (QT->isRecordType()) |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4902 | Ivars.push_back(Iv); |
Fariborz Jahanian | 38b77a9 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 4903 | } |
| 4904 | } |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 4905 | |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 4906 | void Sema::DiagnoseUseOfUnimplementedSelectors() { |
Douglas Gregor | 72e357f | 2011-07-28 14:54:22 +0000 | [diff] [blame] | 4907 | // Load referenced selectors from the external source. |
| 4908 | if (ExternalSource) { |
| 4909 | SmallVector<std::pair<Selector, SourceLocation>, 4> Sels; |
| 4910 | ExternalSource->ReadReferencedSelectors(Sels); |
| 4911 | for (unsigned I = 0, N = Sels.size(); I != N; ++I) |
| 4912 | ReferencedSelectors[Sels[I].first] = Sels[I].second; |
| 4913 | } |
| 4914 | |
Fariborz Jahanian | c9b7c20 | 2011-02-04 23:19:27 +0000 | [diff] [blame] | 4915 | // Warning will be issued only when selector table is |
| 4916 | // generated (which means there is at lease one implementation |
| 4917 | // in the TU). This is to match gcc's behavior. |
| 4918 | if (ReferencedSelectors.empty() || |
| 4919 | !Context.AnyObjCImplementation()) |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 4920 | return; |
Chandler Carruth | 12c8f65 | 2015-03-27 00:55:05 +0000 | [diff] [blame] | 4921 | for (auto &SelectorAndLocation : ReferencedSelectors) { |
| 4922 | Selector Sel = SelectorAndLocation.first; |
| 4923 | SourceLocation Loc = SelectorAndLocation.second; |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 4924 | if (!LookupImplementedMethodInGlobalPool(Sel)) |
Chandler Carruth | 12c8f65 | 2015-03-27 00:55:05 +0000 | [diff] [blame] | 4925 | Diag(Loc, diag::warn_unimplemented_selector) << Sel; |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 4926 | } |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 4927 | } |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 4928 | |
| 4929 | ObjCIvarDecl * |
| 4930 | Sema::GetIvarBackingPropertyAccessor(const ObjCMethodDecl *Method, |
| 4931 | const ObjCPropertyDecl *&PDecl) const { |
Fariborz Jahanian | 1cc7ae1 | 2014-01-02 17:24:32 +0000 | [diff] [blame] | 4932 | if (Method->isClassMethod()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4933 | return nullptr; |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 4934 | const ObjCInterfaceDecl *IDecl = Method->getClassInterface(); |
| 4935 | if (!IDecl) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4936 | return nullptr; |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4937 | Method = IDecl->lookupMethod(Method->getSelector(), /*isInstance=*/true, |
| 4938 | /*shallowCategoryLookup=*/false, |
| 4939 | /*followSuper=*/false); |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 4940 | if (!Method || !Method->isPropertyAccessor()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4941 | return nullptr; |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4942 | if ((PDecl = Method->findPropertyDecl())) |
Fariborz Jahanian | 122d94f | 2014-01-27 22:27:43 +0000 | [diff] [blame] | 4943 | if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) { |
| 4944 | // property backing ivar must belong to property's class |
| 4945 | // or be a private ivar in class's implementation. |
| 4946 | // FIXME. fix the const-ness issue. |
| 4947 | IV = const_cast<ObjCInterfaceDecl *>(IDecl)->lookupInstanceVariable( |
| 4948 | IV->getIdentifier()); |
| 4949 | return IV; |
| 4950 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4951 | return nullptr; |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4954 | namespace { |
| 4955 | /// Used by Sema::DiagnoseUnusedBackingIvarInAccessor to check if a property |
| 4956 | /// accessor references the backing ivar. |
Argyrios Kyrtzidis | 98045c1 | 2014-01-03 19:53:09 +0000 | [diff] [blame] | 4957 | class UnusedBackingIvarChecker : |
Richard Smith | 5066845 | 2015-11-24 03:55:01 +0000 | [diff] [blame] | 4958 | public RecursiveASTVisitor<UnusedBackingIvarChecker> { |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4959 | public: |
| 4960 | Sema &S; |
| 4961 | const ObjCMethodDecl *Method; |
| 4962 | const ObjCIvarDecl *IvarD; |
| 4963 | bool AccessedIvar; |
| 4964 | bool InvokedSelfMethod; |
| 4965 | |
| 4966 | UnusedBackingIvarChecker(Sema &S, const ObjCMethodDecl *Method, |
| 4967 | const ObjCIvarDecl *IvarD) |
| 4968 | : S(S), Method(Method), IvarD(IvarD), |
| 4969 | AccessedIvar(false), InvokedSelfMethod(false) { |
| 4970 | assert(IvarD); |
| 4971 | } |
| 4972 | |
| 4973 | bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { |
| 4974 | if (E->getDecl() == IvarD) { |
| 4975 | AccessedIvar = true; |
| 4976 | return false; |
| 4977 | } |
| 4978 | return true; |
| 4979 | } |
| 4980 | |
| 4981 | bool VisitObjCMessageExpr(ObjCMessageExpr *E) { |
| 4982 | if (E->getReceiverKind() == ObjCMessageExpr::Instance && |
| 4983 | S.isSelfExpr(E->getInstanceReceiver(), Method)) { |
| 4984 | InvokedSelfMethod = true; |
| 4985 | } |
| 4986 | return true; |
| 4987 | } |
| 4988 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 4989 | } // end anonymous namespace |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4990 | |
| 4991 | void Sema::DiagnoseUnusedBackingIvarInAccessor(Scope *S, |
| 4992 | const ObjCImplementationDecl *ImplD) { |
| 4993 | if (S->hasUnrecoverableErrorOccurred()) |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 4994 | return; |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4995 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 4996 | for (const auto *CurMethod : ImplD->instance_methods()) { |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 4997 | unsigned DIAG = diag::warn_unused_property_backing_ivar; |
| 4998 | SourceLocation Loc = CurMethod->getLocation(); |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 4999 | if (Diags.isIgnored(DIAG, Loc)) |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 5000 | continue; |
| 5001 | |
| 5002 | const ObjCPropertyDecl *PDecl; |
| 5003 | const ObjCIvarDecl *IV = GetIvarBackingPropertyAccessor(CurMethod, PDecl); |
| 5004 | if (!IV) |
| 5005 | continue; |
| 5006 | |
| 5007 | UnusedBackingIvarChecker Checker(*this, CurMethod, IV); |
| 5008 | Checker.TraverseStmt(CurMethod->getBody()); |
| 5009 | if (Checker.AccessedIvar) |
| 5010 | continue; |
| 5011 | |
Fariborz Jahanian | 5b3105d | 2014-01-02 22:42:09 +0000 | [diff] [blame] | 5012 | // 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] | 5013 | // implementation makes a self call. This is to prevent false positive in |
| 5014 | // cases where the ivar is accessed by another method that the accessor |
| 5015 | // delegates to. |
| 5016 | if (!IV->isReferenced() || !Checker.InvokedSelfMethod) { |
Argyrios Kyrtzidis | d8a3532 | 2014-01-03 19:39:23 +0000 | [diff] [blame] | 5017 | Diag(Loc, DIAG) << IV; |
Fariborz Jahanian | 5b3105d | 2014-01-02 22:42:09 +0000 | [diff] [blame] | 5018 | Diag(PDecl->getLocation(), diag::note_property_declare); |
| 5019 | } |
Fariborz Jahanian | 5e3429c | 2013-10-25 21:44:50 +0000 | [diff] [blame] | 5020 | } |
| 5021 | } |