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