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