Chris Lattner | 4d39148 | 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 | 0bc735f | 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 | 4d39148 | 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 | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/ASTMutationListener.h" |
| 18 | #include "clang/AST/DeclObjC.h" |
Steve Naroff | ca33129 | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprObjC.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Sema/DeclSpec.h" |
| 24 | #include "clang/Sema/ExternalSemaSource.h" |
| 25 | #include "clang/Sema/Lookup.h" |
| 26 | #include "clang/Sema/Scope.h" |
| 27 | #include "clang/Sema/ScopeInfo.h" |
John McCall | 50df6ae | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseSet.h" |
| 29 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 32 | /// Check whether the given method, which must be in the 'init' |
| 33 | /// family, is a valid member of that family. |
| 34 | /// |
| 35 | /// \param receiverTypeIfCall - if null, check this as if declaring it; |
| 36 | /// if non-null, check this as if making a call to it with the given |
| 37 | /// receiver type |
| 38 | /// |
| 39 | /// \return true to indicate that there was an error and appropriate |
| 40 | /// actions were taken |
| 41 | bool Sema::checkInitMethod(ObjCMethodDecl *method, |
| 42 | QualType receiverTypeIfCall) { |
| 43 | if (method->isInvalidDecl()) return true; |
| 44 | |
| 45 | // This castAs is safe: methods that don't return an object |
| 46 | // pointer won't be inferred as inits and will reject an explicit |
| 47 | // objc_method_family(init). |
| 48 | |
| 49 | // We ignore protocols here. Should we? What about Class? |
| 50 | |
| 51 | const ObjCObjectType *result = method->getResultType() |
| 52 | ->castAs<ObjCObjectPointerType>()->getObjectType(); |
| 53 | |
| 54 | if (result->isObjCId()) { |
| 55 | return false; |
| 56 | } else if (result->isObjCClass()) { |
| 57 | // fall through: always an error |
| 58 | } else { |
| 59 | ObjCInterfaceDecl *resultClass = result->getInterface(); |
| 60 | assert(resultClass && "unexpected object type!"); |
| 61 | |
| 62 | // It's okay for the result type to still be a forward declaration |
| 63 | // if we're checking an interface declaration. |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 64 | if (!resultClass->hasDefinition()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 65 | if (receiverTypeIfCall.isNull() && |
| 66 | !isa<ObjCImplementationDecl>(method->getDeclContext())) |
| 67 | return false; |
| 68 | |
| 69 | // Otherwise, we try to compare class types. |
| 70 | } else { |
| 71 | // If this method was declared in a protocol, we can't check |
| 72 | // anything unless we have a receiver type that's an interface. |
| 73 | const ObjCInterfaceDecl *receiverClass = 0; |
| 74 | if (isa<ObjCProtocolDecl>(method->getDeclContext())) { |
| 75 | if (receiverTypeIfCall.isNull()) |
| 76 | return false; |
| 77 | |
| 78 | receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>() |
| 79 | ->getInterfaceDecl(); |
| 80 | |
| 81 | // This can be null for calls to e.g. id<Foo>. |
| 82 | if (!receiverClass) return false; |
| 83 | } else { |
| 84 | receiverClass = method->getClassInterface(); |
| 85 | assert(receiverClass && "method not associated with a class!"); |
| 86 | } |
| 87 | |
| 88 | // If either class is a subclass of the other, it's fine. |
| 89 | if (receiverClass->isSuperClassOf(resultClass) || |
| 90 | resultClass->isSuperClassOf(receiverClass)) |
| 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | SourceLocation loc = method->getLocation(); |
| 96 | |
| 97 | // If we're in a system header, and this is not a call, just make |
| 98 | // the method unusable. |
| 99 | if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) { |
| 100 | method->addAttr(new (Context) UnavailableAttr(loc, Context, |
| 101 | "init method returns a type unrelated to its receiver type")); |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | // Otherwise, it's an error. |
| 106 | Diag(loc, diag::err_arc_init_method_unrelated_result_type); |
| 107 | method->setInvalidDecl(); |
| 108 | return true; |
| 109 | } |
| 110 | |
Fariborz Jahanian | 3240fe3 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 111 | void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod, |
Douglas Gregor | f4d918f | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 112 | const ObjCMethodDecl *Overridden) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 113 | if (Overridden->hasRelatedResultType() && |
| 114 | !NewMethod->hasRelatedResultType()) { |
| 115 | // This can only happen when the method follows a naming convention that |
| 116 | // implies a related result type, and the original (overridden) method has |
| 117 | // a suitable return type, but the new (overriding) method does not have |
| 118 | // a suitable return type. |
| 119 | QualType ResultType = NewMethod->getResultType(); |
| 120 | SourceRange ResultTypeRange; |
| 121 | if (const TypeSourceInfo *ResultTypeInfo |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 122 | = NewMethod->getResultTypeSourceInfo()) |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 123 | ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange(); |
| 124 | |
| 125 | // Figure out which class this method is part of, if any. |
| 126 | ObjCInterfaceDecl *CurrentClass |
| 127 | = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext()); |
| 128 | if (!CurrentClass) { |
| 129 | DeclContext *DC = NewMethod->getDeclContext(); |
| 130 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC)) |
| 131 | CurrentClass = Cat->getClassInterface(); |
| 132 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC)) |
| 133 | CurrentClass = Impl->getClassInterface(); |
| 134 | else if (ObjCCategoryImplDecl *CatImpl |
| 135 | = dyn_cast<ObjCCategoryImplDecl>(DC)) |
| 136 | CurrentClass = CatImpl->getClassInterface(); |
| 137 | } |
| 138 | |
| 139 | if (CurrentClass) { |
| 140 | Diag(NewMethod->getLocation(), |
| 141 | diag::warn_related_result_type_compatibility_class) |
| 142 | << Context.getObjCInterfaceType(CurrentClass) |
| 143 | << ResultType |
| 144 | << ResultTypeRange; |
| 145 | } else { |
| 146 | Diag(NewMethod->getLocation(), |
| 147 | diag::warn_related_result_type_compatibility_protocol) |
| 148 | << ResultType |
| 149 | << ResultTypeRange; |
| 150 | } |
| 151 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 152 | if (ObjCMethodFamily Family = Overridden->getMethodFamily()) |
| 153 | Diag(Overridden->getLocation(), |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 154 | diag::note_related_result_type_family) |
| 155 | << /*overridden method*/ 0 |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 156 | << Family; |
| 157 | else |
| 158 | Diag(Overridden->getLocation(), |
| 159 | diag::note_related_result_type_overridden); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 160 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 161 | if (getLangOpts().ObjCAutoRefCount) { |
Fariborz Jahanian | 3240fe3 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 162 | if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() != |
| 163 | Overridden->hasAttr<NSReturnsRetainedAttr>())) { |
| 164 | Diag(NewMethod->getLocation(), |
| 165 | diag::err_nsreturns_retained_attribute_mismatch) << 1; |
| 166 | Diag(Overridden->getLocation(), diag::note_previous_decl) |
| 167 | << "method"; |
| 168 | } |
| 169 | if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() != |
| 170 | Overridden->hasAttr<NSReturnsNotRetainedAttr>())) { |
| 171 | Diag(NewMethod->getLocation(), |
| 172 | diag::err_nsreturns_retained_attribute_mismatch) << 0; |
| 173 | Diag(Overridden->getLocation(), diag::note_previous_decl) |
| 174 | << "method"; |
| 175 | } |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 176 | ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(), |
| 177 | oe = Overridden->param_end(); |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 178 | for (ObjCMethodDecl::param_iterator |
| 179 | ni = NewMethod->param_begin(), ne = NewMethod->param_end(); |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 180 | ni != ne && oi != oe; ++ni, ++oi) { |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 181 | const ParmVarDecl *oldDecl = (*oi); |
Fariborz Jahanian | 3240fe3 | 2011-09-27 22:35:36 +0000 | [diff] [blame] | 182 | ParmVarDecl *newDecl = (*ni); |
| 183 | if (newDecl->hasAttr<NSConsumedAttr>() != |
| 184 | oldDecl->hasAttr<NSConsumedAttr>()) { |
| 185 | Diag(newDecl->getLocation(), |
| 186 | diag::err_nsconsumed_attribute_mismatch); |
| 187 | Diag(oldDecl->getLocation(), diag::note_previous_decl) |
| 188 | << "parameter"; |
| 189 | } |
| 190 | } |
| 191 | } |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 192 | } |
| 193 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 194 | /// \brief Check a method declaration for compatibility with the Objective-C |
| 195 | /// ARC conventions. |
John McCall | b846381 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 196 | bool Sema::CheckARCMethodDecl(ObjCMethodDecl *method) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 197 | ObjCMethodFamily family = method->getMethodFamily(); |
| 198 | switch (family) { |
| 199 | case OMF_None: |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 200 | case OMF_finalize: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 201 | case OMF_retain: |
| 202 | case OMF_release: |
| 203 | case OMF_autorelease: |
| 204 | case OMF_retainCount: |
| 205 | case OMF_self: |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 206 | case OMF_performSelector: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 207 | return false; |
| 208 | |
Fariborz Jahanian | 1b0a13e | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 209 | case OMF_dealloc: |
John McCall | b846381 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 210 | if (!Context.hasSameType(method->getResultType(), Context.VoidTy)) { |
Fariborz Jahanian | 1b0a13e | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 211 | SourceRange ResultTypeRange; |
| 212 | if (const TypeSourceInfo *ResultTypeInfo |
| 213 | = method->getResultTypeSourceInfo()) |
| 214 | ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange(); |
| 215 | if (ResultTypeRange.isInvalid()) |
John McCall | b846381 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 216 | Diag(method->getLocation(), diag::error_dealloc_bad_result_type) |
Fariborz Jahanian | 1b0a13e | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 217 | << method->getResultType() |
| 218 | << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)"); |
| 219 | else |
John McCall | b846381 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 220 | Diag(method->getLocation(), diag::error_dealloc_bad_result_type) |
Fariborz Jahanian | 1b0a13e | 2012-07-30 20:52:48 +0000 | [diff] [blame] | 221 | << method->getResultType() |
| 222 | << FixItHint::CreateReplacement(ResultTypeRange, "void"); |
| 223 | return true; |
| 224 | } |
| 225 | return false; |
| 226 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 227 | case OMF_init: |
| 228 | // If the method doesn't obey the init rules, don't bother annotating it. |
John McCall | b846381 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 229 | if (checkInitMethod(method, QualType())) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 230 | return true; |
| 231 | |
John McCall | b846381 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 232 | method->addAttr(new (Context) NSConsumesSelfAttr(SourceLocation(), |
| 233 | Context)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 234 | |
| 235 | // Don't add a second copy of this attribute, but otherwise don't |
| 236 | // let it be suppressed. |
| 237 | if (method->hasAttr<NSReturnsRetainedAttr>()) |
| 238 | return false; |
| 239 | break; |
| 240 | |
| 241 | case OMF_alloc: |
| 242 | case OMF_copy: |
| 243 | case OMF_mutableCopy: |
| 244 | case OMF_new: |
| 245 | if (method->hasAttr<NSReturnsRetainedAttr>() || |
| 246 | method->hasAttr<NSReturnsNotRetainedAttr>() || |
| 247 | method->hasAttr<NSReturnsAutoreleasedAttr>()) |
| 248 | return false; |
| 249 | break; |
| 250 | } |
| 251 | |
John McCall | b846381 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 252 | method->addAttr(new (Context) NSReturnsRetainedAttr(SourceLocation(), |
| 253 | Context)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 254 | return false; |
| 255 | } |
| 256 | |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 257 | static void DiagnoseObjCImplementedDeprecations(Sema &S, |
| 258 | NamedDecl *ND, |
| 259 | SourceLocation ImplLoc, |
| 260 | int select) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 261 | if (ND && ND->isDeprecated()) { |
Fariborz Jahanian | 98d810e | 2011-02-16 00:30:31 +0000 | [diff] [blame] | 262 | S.Diag(ImplLoc, diag::warn_deprecated_def) << select; |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 263 | if (select == 0) |
Ted Kremenek | 3306ec1 | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 264 | S.Diag(ND->getLocation(), diag::note_method_declared_at) |
| 265 | << ND->getDeclName(); |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 266 | else |
| 267 | S.Diag(ND->getLocation(), diag::note_previous_decl) << "class"; |
| 268 | } |
| 269 | } |
| 270 | |
Fariborz Jahanian | 140ab23 | 2011-08-31 17:37:55 +0000 | [diff] [blame] | 271 | /// AddAnyMethodToGlobalPool - Add any method, instance or factory to global |
| 272 | /// pool. |
| 273 | void Sema::AddAnyMethodToGlobalPool(Decl *D) { |
| 274 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); |
| 275 | |
| 276 | // If we don't have a valid method decl, simply return. |
| 277 | if (!MDecl) |
| 278 | return; |
| 279 | if (MDecl->isInstanceMethod()) |
| 280 | AddInstanceMethodToGlobalPool(MDecl, true); |
| 281 | else |
| 282 | AddFactoryMethodToGlobalPool(MDecl, true); |
| 283 | } |
| 284 | |
Fariborz Jahanian | a1a32f7 | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 285 | /// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer |
| 286 | /// has explicit ownership attribute; false otherwise. |
| 287 | static bool |
| 288 | HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) { |
| 289 | QualType T = Param->getType(); |
| 290 | |
Fariborz Jahanian | a1a32f7 | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 291 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 292 | T = PT->getPointeeType(); |
| 293 | } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) { |
| 294 | T = RT->getPointeeType(); |
| 295 | } else { |
| 296 | return true; |
| 297 | } |
| 298 | |
| 299 | // If we have a lifetime qualifier, but it's local, we must have |
| 300 | // inferred it. So, it is implicit. |
| 301 | return !T.getLocalQualifiers().hasObjCLifetime(); |
| 302 | } |
| 303 | |
Fariborz Jahanian | 8c6cb46 | 2012-08-08 23:41:08 +0000 | [diff] [blame] | 304 | /// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible |
| 305 | /// and user declared, in the method definition's AST. |
| 306 | void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) { |
| 307 | assert((getCurMethodDecl() == 0) && "Methodparsing confused"); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 308 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 309 | |
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 310 | // If we don't have a valid method decl, simply return. |
| 311 | if (!MDecl) |
| 312 | return; |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 313 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 314 | // Allow all of Sema to see that we are entering a method definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 315 | PushDeclContext(FnBodyScope, MDecl); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 316 | PushFunctionScope(); |
| 317 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 318 | // Create Decl objects for each parameter, entrring them in the scope for |
| 319 | // binding to their use. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 320 | |
| 321 | // Insert the invisible arguments, self and _cmd! |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 322 | MDecl->createImplicitParams(Context, MDecl->getClassInterface()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Daniel Dunbar | 451318c | 2008-08-26 06:07:48 +0000 | [diff] [blame] | 324 | PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); |
| 325 | PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 326 | |
Reid Kleckner | 8c0501c | 2013-06-24 14:38:26 +0000 | [diff] [blame] | 327 | // The ObjC parser requires parameter names so there's no need to check. |
| 328 | CheckParmsForFunctionDef(MDecl->param_begin(), MDecl->param_end(), |
| 329 | /*CheckParameterNames=*/false); |
| 330 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 331 | // Introduce all of the other parameters into this scope. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 332 | for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(), |
Fariborz Jahanian | 23c0104 | 2010-09-17 22:07:07 +0000 | [diff] [blame] | 333 | E = MDecl->param_end(); PI != E; ++PI) { |
| 334 | ParmVarDecl *Param = (*PI); |
| 335 | if (!Param->isInvalidDecl() && |
Fariborz Jahanian | a1a32f7 | 2012-09-13 18:53:14 +0000 | [diff] [blame] | 336 | getLangOpts().ObjCAutoRefCount && |
| 337 | !HasExplicitOwnershipAttr(*this, Param)) |
| 338 | Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) << |
| 339 | Param->getType(); |
Fariborz Jahanian | 918546c | 2012-08-30 23:56:02 +0000 | [diff] [blame] | 340 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 341 | if ((*PI)->getIdentifier()) |
| 342 | PushOnScopeChains(*PI, FnBodyScope); |
Fariborz Jahanian | 23c0104 | 2010-09-17 22:07:07 +0000 | [diff] [blame] | 343 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 344 | |
| 345 | // In ARC, disallow definition of retain/release/autorelease/retainCount |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 346 | if (getLangOpts().ObjCAutoRefCount) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 347 | switch (MDecl->getMethodFamily()) { |
| 348 | case OMF_retain: |
| 349 | case OMF_retainCount: |
| 350 | case OMF_release: |
| 351 | case OMF_autorelease: |
| 352 | Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def) |
Fariborz Jahanian | b8ed071 | 2013-05-16 19:08:44 +0000 | [diff] [blame] | 353 | << 0 << MDecl->getSelector(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 354 | break; |
| 355 | |
| 356 | case OMF_None: |
| 357 | case OMF_dealloc: |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 358 | case OMF_finalize: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 359 | case OMF_alloc: |
| 360 | case OMF_init: |
| 361 | case OMF_mutableCopy: |
| 362 | case OMF_copy: |
| 363 | case OMF_new: |
| 364 | case OMF_self: |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 365 | case OMF_performSelector: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 366 | break; |
| 367 | } |
| 368 | } |
| 369 | |
Nico Weber | 9a1ecf0 | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 370 | // Warn on deprecated methods under -Wdeprecated-implementations, |
| 371 | // and prepare for warning on missing super calls. |
| 372 | if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) { |
Fariborz Jahanian | 8410113 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 373 | ObjCMethodDecl *IMD = |
| 374 | IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()); |
| 375 | |
Fariborz Jahanian | 5fa6676 | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 376 | if (IMD) { |
| 377 | ObjCImplDecl *ImplDeclOfMethodDef = |
| 378 | dyn_cast<ObjCImplDecl>(MDecl->getDeclContext()); |
| 379 | ObjCContainerDecl *ContDeclOfMethodDecl = |
| 380 | dyn_cast<ObjCContainerDecl>(IMD->getDeclContext()); |
| 381 | ObjCImplDecl *ImplDeclOfMethodDecl = 0; |
| 382 | if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl)) |
| 383 | ImplDeclOfMethodDecl = OID->getImplementation(); |
| 384 | else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl)) |
| 385 | ImplDeclOfMethodDecl = CD->getImplementation(); |
| 386 | // No need to issue deprecated warning if deprecated mehod in class/category |
| 387 | // is being implemented in its own implementation (no overriding is involved). |
| 388 | if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef) |
| 389 | DiagnoseObjCImplementedDeprecations(*this, |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 390 | dyn_cast<NamedDecl>(IMD), |
| 391 | MDecl->getLocation(), 0); |
Fariborz Jahanian | 5fa6676 | 2012-11-17 20:53:53 +0000 | [diff] [blame] | 392 | } |
Nico Weber | 9a1ecf0 | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 393 | |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 394 | // If this is "dealloc" or "finalize", set some bit here. |
Nico Weber | 9a1ecf0 | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 395 | // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false. |
| 396 | // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set. |
| 397 | // Only do this if the current class actually has a superclass. |
Jordan Rose | 41f3f3a | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 398 | if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) { |
Jordan Rose | 535a5d0 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 399 | ObjCMethodFamily Family = MDecl->getMethodFamily(); |
| 400 | if (Family == OMF_dealloc) { |
| 401 | if (!(getLangOpts().ObjCAutoRefCount || |
| 402 | getLangOpts().getGC() == LangOptions::GCOnly)) |
| 403 | getCurFunction()->ObjCShouldCallSuper = true; |
| 404 | |
| 405 | } else if (Family == OMF_finalize) { |
| 406 | if (Context.getLangOpts().getGC() != LangOptions::NonGC) |
| 407 | getCurFunction()->ObjCShouldCallSuper = true; |
| 408 | |
Fariborz Jahanian | 38b3bd8 | 2013-07-09 22:02:20 +0000 | [diff] [blame] | 409 | } else if (MDecl->hasAttr<ObjCRequiresSuperAttr>()) |
| 410 | getCurFunction()->ObjCShouldCallSuper = true; |
| 411 | else { |
Jordan Rose | 535a5d0 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 412 | const ObjCMethodDecl *SuperMethod = |
Jordan Rose | 41f3f3a | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 413 | SuperClass->lookupMethod(MDecl->getSelector(), |
| 414 | MDecl->isInstanceMethod()); |
Jordan Rose | 535a5d0 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 415 | getCurFunction()->ObjCShouldCallSuper = |
| 416 | (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>()); |
Fariborz Jahanian | 6f93860 | 2012-09-10 18:04:25 +0000 | [diff] [blame] | 417 | } |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 418 | } |
Nico Weber | 9a1ecf0 | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 419 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 422 | namespace { |
| 423 | |
| 424 | // Callback to only accept typo corrections that are Objective-C classes. |
| 425 | // If an ObjCInterfaceDecl* is given to the constructor, then the validation |
| 426 | // function will reject corrections to that class. |
| 427 | class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback { |
| 428 | public: |
| 429 | ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {} |
| 430 | explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl) |
| 431 | : CurrentIDecl(IDecl) {} |
| 432 | |
| 433 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 434 | ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
| 435 | return ID && !declaresSameEntity(ID, CurrentIDecl); |
| 436 | } |
| 437 | |
| 438 | private: |
| 439 | ObjCInterfaceDecl *CurrentIDecl; |
| 440 | }; |
| 441 | |
| 442 | } |
| 443 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 444 | Decl *Sema:: |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 445 | ActOnStartClassInterface(SourceLocation AtInterfaceLoc, |
| 446 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 447 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 448 | Decl * const *ProtoRefs, unsigned NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 449 | const SourceLocation *ProtoLocs, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 450 | SourceLocation EndProtoLoc, AttributeList *AttrList) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 451 | assert(ClassName && "Missing class identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 453 | // Check for another declaration kind with the same name. |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 454 | NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 455 | LookupOrdinaryName, ForRedeclaration); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 456 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 457 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 458 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 459 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 460 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 462 | // Create a declaration to describe this @interface. |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 463 | ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Argyrios Kyrtzidis | e7e8fca | 2013-06-18 21:26:33 +0000 | [diff] [blame] | 464 | |
| 465 | if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) { |
| 466 | // A previous decl with a different name is because of |
| 467 | // @compatibility_alias, for example: |
| 468 | // \code |
| 469 | // @class NewImage; |
| 470 | // @compatibility_alias OldImage NewImage; |
| 471 | // \endcode |
| 472 | // A lookup for 'OldImage' will return the 'NewImage' decl. |
| 473 | // |
| 474 | // In such a case use the real declaration name, instead of the alias one, |
| 475 | // otherwise we will break IdentifierResolver and redecls-chain invariants. |
| 476 | // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl |
| 477 | // has been aliased. |
| 478 | ClassName = PrevIDecl->getIdentifier(); |
| 479 | } |
| 480 | |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 481 | ObjCInterfaceDecl *IDecl |
| 482 | = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName, |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 483 | PrevIDecl, ClassLoc); |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 484 | |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 485 | if (PrevIDecl) { |
| 486 | // Class already seen. Was it a definition? |
| 487 | if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) { |
| 488 | Diag(AtInterfaceLoc, diag::err_duplicate_class_def) |
| 489 | << PrevIDecl->getDeclName(); |
Douglas Gregor | 2e5c15b | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 490 | Diag(Def->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 491 | IDecl->setInvalidDecl(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 492 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 493 | } |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 494 | |
| 495 | if (AttrList) |
| 496 | ProcessDeclAttributeList(TUScope, IDecl, AttrList); |
| 497 | PushOnScopeChains(IDecl, TUScope); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 | |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 499 | // Start the definition of this class. If we're in a redefinition case, there |
| 500 | // may already be a definition, so we'll end up adding to it. |
Douglas Gregor | 2e5c15b | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 501 | if (!IDecl->hasDefinition()) |
| 502 | IDecl->startDefinition(); |
| 503 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 504 | if (SuperName) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 505 | // Check if a different kind of symbol declared in this scope. |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 506 | PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
| 507 | LookupOrdinaryName); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 508 | |
| 509 | if (!PrevDecl) { |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 510 | // Try to correct for a typo in the superclass name without correcting |
| 511 | // to the class we're defining. |
| 512 | ObjCInterfaceValidatorCCC Validator(IDecl); |
| 513 | if (TypoCorrection Corrected = CorrectTypo( |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 514 | DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 515 | NULL, Validator)) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame^] | 516 | diagnoseTypo(Corrected, PDiag(diag::err_undef_superclass_suggest) |
| 517 | << SuperName << ClassName); |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 518 | PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 522 | if (declaresSameEntity(PrevDecl, IDecl)) { |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 523 | Diag(SuperLoc, diag::err_recursive_superclass) |
| 524 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 525 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 526 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 527 | ObjCInterfaceDecl *SuperClassDecl = |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 528 | dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 529 | |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 530 | // Diagnose classes that inherit from deprecated classes. |
| 531 | if (SuperClassDecl) |
| 532 | (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 534 | if (PrevDecl && SuperClassDecl == 0) { |
| 535 | // The previous declaration was not a class decl. Check if we have a |
| 536 | // typedef. If we do, get the underlying class type. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 537 | if (const TypedefNameDecl *TDecl = |
| 538 | dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) { |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 539 | QualType T = TDecl->getUnderlyingType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 540 | if (T->isObjCObjectType()) { |
Fariborz Jahanian | 740991b | 2013-04-04 18:45:52 +0000 | [diff] [blame] | 541 | if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) { |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 542 | SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl); |
Fariborz Jahanian | 740991b | 2013-04-04 18:45:52 +0000 | [diff] [blame] | 543 | // This handles the following case: |
| 544 | // @interface NewI @end |
| 545 | // typedef NewI DeprI __attribute__((deprecated("blah"))) |
| 546 | // @interface SI : DeprI /* warn here */ @end |
| 547 | (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc); |
| 548 | } |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 552 | // This handles the following case: |
| 553 | // |
| 554 | // typedef int SuperClass; |
| 555 | // @interface MyClass : SuperClass {} @end |
| 556 | // |
| 557 | if (!SuperClassDecl) { |
| 558 | Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName; |
| 559 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Steve Naroff | 818cb9e | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 560 | } |
| 561 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 563 | if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) { |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 564 | if (!SuperClassDecl) |
| 565 | Diag(SuperLoc, diag::err_undef_superclass) |
| 566 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 567 | else if (RequireCompleteType(SuperLoc, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 568 | Context.getObjCInterfaceType(SuperClassDecl), |
| 569 | diag::err_forward_superclass, |
| 570 | SuperClassDecl->getDeclName(), |
| 571 | ClassName, |
| 572 | SourceRange(AtInterfaceLoc, ClassLoc))) { |
Fariborz Jahanian | a813973 | 2011-06-23 23:16:19 +0000 | [diff] [blame] | 573 | SuperClassDecl = 0; |
| 574 | } |
Steve Naroff | 818cb9e | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 575 | } |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 576 | IDecl->setSuperClass(SuperClassDecl); |
| 577 | IDecl->setSuperClassLoc(SuperLoc); |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 578 | IDecl->setEndOfDefinitionLoc(SuperLoc); |
Steve Naroff | 818cb9e | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 579 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 580 | } else { // we have a root class. |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 581 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 582 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 583 | |
Sebastian Redl | 0b17c61 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 584 | // Check then save referenced protocols. |
Chris Lattner | 06036d3 | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 585 | if (NumProtoRefs) { |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 586 | IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 587 | ProtoLocs, Context); |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 588 | IDecl->setEndOfDefinitionLoc(EndProtoLoc); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 589 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 590 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 591 | CheckObjCDeclScope(IDecl); |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 592 | return ActOnObjCContainerStartDefinition(IDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Richard Smith | de01b7a | 2012-08-08 23:32:13 +0000 | [diff] [blame] | 595 | /// ActOnCompatibilityAlias - this action is called after complete parsing of |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 596 | /// a \@compatibility_alias declaration. It sets up the alias relationships. |
Richard Smith | de01b7a | 2012-08-08 23:32:13 +0000 | [diff] [blame] | 597 | Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc, |
| 598 | IdentifierInfo *AliasName, |
| 599 | SourceLocation AliasLocation, |
| 600 | IdentifierInfo *ClassName, |
| 601 | SourceLocation ClassLocation) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 602 | // Look for previous declaration of alias name |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 603 | NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 604 | LookupOrdinaryName, ForRedeclaration); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 605 | if (ADecl) { |
Eli Friedman | 104f96b | 2013-06-21 01:49:53 +0000 | [diff] [blame] | 606 | Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; |
Chris Lattner | 8b265bd | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 607 | Diag(ADecl->getLocation(), diag::note_previous_declaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 608 | return 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 609 | } |
| 610 | // Check for class declaration |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 611 | NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 612 | LookupOrdinaryName, ForRedeclaration); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 613 | if (const TypedefNameDecl *TDecl = |
| 614 | dyn_cast_or_null<TypedefNameDecl>(CDeclU)) { |
Fariborz Jahanian | 305c658 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 615 | QualType T = TDecl->getUnderlyingType(); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 616 | if (T->isObjCObjectType()) { |
| 617 | if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) { |
Fariborz Jahanian | 305c658 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 618 | ClassName = IDecl->getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 619 | CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 620 | LookupOrdinaryName, ForRedeclaration); |
Fariborz Jahanian | 305c658 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | } |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 624 | ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); |
| 625 | if (CDecl == 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 626 | Diag(ClassLocation, diag::warn_undef_interface) << ClassName; |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 627 | if (CDeclU) |
Chris Lattner | 8b265bd | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 628 | Diag(CDeclU->getLocation(), diag::note_previous_declaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 629 | return 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 630 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 631 | |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 632 | // Everything checked out, instantiate a new alias declaration AST. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | ObjCCompatibleAliasDecl *AliasDecl = |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 634 | ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 636 | if (!CheckObjCDeclScope(AliasDecl)) |
Douglas Gregor | 516ff43 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 637 | PushOnScopeChains(AliasDecl, TUScope); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 638 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 639 | return AliasDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Fariborz Jahanian | 819e9bf | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 642 | bool Sema::CheckForwardProtocolDeclarationForCircularDependency( |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 643 | IdentifierInfo *PName, |
| 644 | SourceLocation &Ploc, SourceLocation PrevLoc, |
Fariborz Jahanian | 819e9bf | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 645 | const ObjCList<ObjCProtocolDecl> &PList) { |
| 646 | |
| 647 | bool res = false; |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 648 | for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(), |
| 649 | E = PList.end(); I != E; ++I) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 650 | if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(), |
| 651 | Ploc)) { |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 652 | if (PDecl->getIdentifier() == PName) { |
| 653 | Diag(Ploc, diag::err_protocol_has_circular_dependency); |
| 654 | Diag(PrevLoc, diag::note_previous_definition); |
Fariborz Jahanian | 819e9bf | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 655 | res = true; |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 656 | } |
Douglas Gregor | 5e2a1ff | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 657 | |
| 658 | if (!PDecl->hasDefinition()) |
| 659 | continue; |
| 660 | |
Fariborz Jahanian | 819e9bf | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 661 | if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc, |
| 662 | PDecl->getLocation(), PDecl->getReferencedProtocols())) |
| 663 | res = true; |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
Fariborz Jahanian | 819e9bf | 2011-05-13 18:02:08 +0000 | [diff] [blame] | 666 | return res; |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 667 | } |
| 668 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 669 | Decl * |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 670 | Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc, |
| 671 | IdentifierInfo *ProtocolName, |
| 672 | SourceLocation ProtocolLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 673 | Decl * const *ProtoRefs, |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 674 | unsigned NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 675 | const SourceLocation *ProtoLocs, |
Daniel Dunbar | 246e70f | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 676 | SourceLocation EndProtoLoc, |
| 677 | AttributeList *AttrList) { |
Fariborz Jahanian | 96b69a7 | 2011-05-12 22:04:39 +0000 | [diff] [blame] | 678 | bool err = false; |
Daniel Dunbar | 246e70f | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 679 | // FIXME: Deal with AttrList. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 680 | assert(ProtocolName && "Missing protocol identifier"); |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 681 | ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc, |
| 682 | ForRedeclaration); |
| 683 | ObjCProtocolDecl *PDecl = 0; |
| 684 | if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) { |
| 685 | // If we already have a definition, complain. |
| 686 | Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName; |
| 687 | Diag(Def->getLocation(), diag::note_previous_definition); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 688 | |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 689 | // Create a new protocol that is completely distinct from previous |
| 690 | // declarations, and do not make this protocol available for name lookup. |
| 691 | // That way, we'll end up completely ignoring the duplicate. |
| 692 | // FIXME: Can we turn this into an error? |
| 693 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName, |
| 694 | ProtocolLoc, AtProtoInterfaceLoc, |
Douglas Gregor | c9d3c7e | 2012-01-01 22:06:18 +0000 | [diff] [blame] | 695 | /*PrevDecl=*/0); |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 696 | PDecl->startDefinition(); |
| 697 | } else { |
| 698 | if (PrevDecl) { |
| 699 | // Check for circular dependencies among protocol declarations. This can |
| 700 | // only happen if this protocol was forward-declared. |
Argyrios Kyrtzidis | 4fc04da | 2011-11-13 22:08:30 +0000 | [diff] [blame] | 701 | ObjCList<ObjCProtocolDecl> PList; |
| 702 | PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context); |
| 703 | err = CheckForwardProtocolDeclarationForCircularDependency( |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 704 | ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList); |
Argyrios Kyrtzidis | 4fc04da | 2011-11-13 22:08:30 +0000 | [diff] [blame] | 705 | } |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 706 | |
| 707 | // Create the new declaration. |
Argyrios Kyrtzidis | 1711fc9 | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 708 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName, |
Argyrios Kyrtzidis | b05d7b2 | 2011-10-17 19:48:06 +0000 | [diff] [blame] | 709 | ProtocolLoc, AtProtoInterfaceLoc, |
Douglas Gregor | c9d3c7e | 2012-01-01 22:06:18 +0000 | [diff] [blame] | 710 | /*PrevDecl=*/PrevDecl); |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 711 | |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 712 | PushOnScopeChains(PDecl, TUScope); |
Douglas Gregor | 5e2a1ff | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 713 | PDecl->startDefinition(); |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 714 | } |
Douglas Gregor | 5e2a1ff | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 715 | |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 716 | if (AttrList) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 717 | ProcessDeclAttributeList(TUScope, PDecl, AttrList); |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 718 | |
| 719 | // Merge attributes from previous declarations. |
| 720 | if (PrevDecl) |
| 721 | mergeDeclAttributes(PDecl, PrevDecl); |
| 722 | |
Fariborz Jahanian | 96b69a7 | 2011-05-12 22:04:39 +0000 | [diff] [blame] | 723 | if (!err && NumProtoRefs ) { |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 724 | /// Check then save referenced protocols. |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 725 | PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 726 | ProtoLocs, Context); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 727 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 728 | |
| 729 | CheckObjCDeclScope(PDecl); |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 730 | return ActOnObjCContainerStartDefinition(PDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | /// FindProtocolDeclaration - This routine looks up protocols and |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 734 | /// issues an error if they are not declared. It returns list of |
| 735 | /// protocol declarations in its 'Protocols' argument. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 736 | void |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 737 | Sema::FindProtocolDeclaration(bool WarnOnDeclarations, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 738 | const IdentifierLocPair *ProtocolId, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 739 | unsigned NumProtocols, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 740 | SmallVectorImpl<Decl *> &Protocols) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 741 | for (unsigned i = 0; i != NumProtocols; ++i) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 742 | ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first, |
| 743 | ProtocolId[i].second); |
Chris Lattner | eacc392 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 744 | if (!PDecl) { |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 745 | DeclFilterCCC<ObjCProtocolDecl> Validator; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 746 | TypoCorrection Corrected = CorrectTypo( |
| 747 | DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second), |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 748 | LookupObjCProtocolName, TUScope, NULL, Validator); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame^] | 749 | if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) |
| 750 | diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest) |
| 751 | << ProtocolId[i].first); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | if (!PDecl) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 755 | Diag(ProtocolId[i].second, diag::err_undeclared_protocol) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 756 | << ProtocolId[i].first; |
Chris Lattner | eacc392 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 757 | continue; |
| 758 | } |
Fariborz Jahanian | 3c9a024 | 2013-04-09 17:52:29 +0000 | [diff] [blame] | 759 | // If this is a forward protocol declaration, get its definition. |
| 760 | if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition()) |
| 761 | PDecl = PDecl->getDefinition(); |
| 762 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 763 | (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second); |
Chris Lattner | eacc392 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 764 | |
| 765 | // If this is a forward declaration and we are supposed to warn in this |
| 766 | // case, do it. |
Douglas Gregor | 0f9b9f3 | 2013-01-17 00:38:46 +0000 | [diff] [blame] | 767 | // FIXME: Recover nicely in the hidden case. |
| 768 | if (WarnOnDeclarations && |
| 769 | (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden())) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 770 | Diag(ProtocolId[i].second, diag::warn_undef_protocolref) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 771 | << ProtocolId[i].first; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 772 | Protocols.push_back(PDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
Fariborz Jahanian | 78c39c7 | 2009-03-02 19:06:08 +0000 | [diff] [blame] | 776 | /// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 777 | /// a class method in its extension. |
| 778 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 779 | void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 780 | ObjCInterfaceDecl *ID) { |
| 781 | if (!ID) |
| 782 | return; // Possibly due to previous error |
| 783 | |
| 784 | llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 785 | for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(), |
| 786 | e = ID->meth_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 787 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 788 | MethodMap[MD->getSelector()] = MD; |
| 789 | } |
| 790 | |
| 791 | if (MethodMap.empty()) |
| 792 | return; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 793 | for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(), |
| 794 | e = CAT->meth_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 795 | ObjCMethodDecl *Method = *i; |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 796 | const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()]; |
| 797 | if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) { |
| 798 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
| 799 | << Method->getDeclName(); |
| 800 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 805 | /// ActOnForwardProtocolDeclaration - Handle \@protocol foo; |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 806 | Sema::DeclGroupPtrTy |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 807 | Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 808 | const IdentifierLocPair *IdentList, |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 809 | unsigned NumElts, |
| 810 | AttributeList *attrList) { |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 811 | SmallVector<Decl *, 8> DeclsInGroup; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 812 | for (unsigned i = 0; i != NumElts; ++i) { |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 813 | IdentifierInfo *Ident = IdentList[i].first; |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 814 | ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second, |
| 815 | ForRedeclaration); |
| 816 | ObjCProtocolDecl *PDecl |
| 817 | = ObjCProtocolDecl::Create(Context, CurContext, Ident, |
| 818 | IdentList[i].second, AtProtocolLoc, |
Douglas Gregor | c9d3c7e | 2012-01-01 22:06:18 +0000 | [diff] [blame] | 819 | PrevDecl); |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 820 | |
| 821 | PushOnScopeChains(PDecl, TUScope); |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 822 | CheckObjCDeclScope(PDecl); |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 823 | |
Douglas Gregor | 3937f87 | 2012-01-01 20:33:24 +0000 | [diff] [blame] | 824 | if (attrList) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 825 | ProcessDeclAttributeList(TUScope, PDecl, attrList); |
Douglas Gregor | 27c6da2 | 2012-01-01 20:30:41 +0000 | [diff] [blame] | 826 | |
| 827 | if (PrevDecl) |
| 828 | mergeDeclAttributes(PDecl, PrevDecl); |
| 829 | |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 830 | DeclsInGroup.push_back(PDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 831 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 832 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 833 | return BuildDeclaratorGroup(DeclsInGroup, false); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 834 | } |
| 835 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 836 | Decl *Sema:: |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 837 | ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc, |
| 838 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 839 | IdentifierInfo *CategoryName, |
| 840 | SourceLocation CategoryLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 841 | Decl * const *ProtoRefs, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 842 | unsigned NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 843 | const SourceLocation *ProtoLocs, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 844 | SourceLocation EndProtoLoc) { |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 845 | ObjCCategoryDecl *CDecl; |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 846 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
Ted Kremenek | 09b6897 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 847 | |
| 848 | /// Check that class of this category is already completely declared. |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 849 | |
| 850 | if (!IDecl |
| 851 | || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 852 | diag::err_category_forward_interface, |
| 853 | CategoryName == 0)) { |
Ted Kremenek | 09b6897 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 854 | // Create an invalid ObjCCategoryDecl to serve as context for |
| 855 | // the enclosing method declarations. We mark the decl invalid |
| 856 | // to make it clear that this isn't a valid AST. |
| 857 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
Argyrios Kyrtzidis | 955fadb | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 858 | ClassLoc, CategoryLoc, CategoryName,IDecl); |
Ted Kremenek | 09b6897 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 859 | CDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 9a0b6b4 | 2012-03-12 18:34:26 +0000 | [diff] [blame] | 860 | CurContext->addDecl(CDecl); |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 861 | |
| 862 | if (!IDecl) |
| 863 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 864 | return ActOnObjCContainerStartDefinition(CDecl); |
Ted Kremenek | 09b6897 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 867 | if (!CategoryName && IDecl->getImplementation()) { |
| 868 | Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName; |
| 869 | Diag(IDecl->getImplementation()->getLocation(), |
| 870 | diag::note_implementation_declared); |
Ted Kremenek | 09b6897 | 2010-02-23 19:39:46 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 873 | if (CategoryName) { |
| 874 | /// Check for duplicate interface declaration for this category |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 875 | if (ObjCCategoryDecl *Previous |
| 876 | = IDecl->FindCategoryDeclaration(CategoryName)) { |
| 877 | // Class extensions can be declared multiple times, categories cannot. |
| 878 | Diag(CategoryLoc, diag::warn_dup_category_def) |
| 879 | << ClassName << CategoryName; |
| 880 | Diag(Previous->getLocation(), diag::note_previous_definition); |
Chris Lattner | 70f1954 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 881 | } |
| 882 | } |
Chris Lattner | 70f1954 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 883 | |
Argyrios Kyrtzidis | 955fadb | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 884 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
| 885 | ClassLoc, CategoryLoc, CategoryName, IDecl); |
| 886 | // FIXME: PushOnScopeChains? |
| 887 | CurContext->addDecl(CDecl); |
| 888 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 889 | if (NumProtoRefs) { |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 890 | CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 891 | ProtoLocs, Context); |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 892 | // Protocols in the class extension belong to the class. |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 893 | if (CDecl->IsClassExtension()) |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 894 | IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs, |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 895 | NumProtoRefs, Context); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 896 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 898 | CheckObjCDeclScope(CDecl); |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 899 | return ActOnObjCContainerStartDefinition(CDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | /// ActOnStartCategoryImplementation - Perform semantic checks on the |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 903 | /// category implementation declaration and build an ObjCCategoryImplDecl |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 904 | /// object. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 905 | Decl *Sema::ActOnStartCategoryImplementation( |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 906 | SourceLocation AtCatImplLoc, |
| 907 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 908 | IdentifierInfo *CatName, SourceLocation CatLoc) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 909 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 910 | ObjCCategoryDecl *CatIDecl = 0; |
Argyrios Kyrtzidis | 5a61e0c | 2012-03-02 19:14:29 +0000 | [diff] [blame] | 911 | if (IDecl && IDecl->hasDefinition()) { |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 912 | CatIDecl = IDecl->FindCategoryDeclaration(CatName); |
| 913 | if (!CatIDecl) { |
| 914 | // Category @implementation with no corresponding @interface. |
| 915 | // Create and install one. |
Argyrios Kyrtzidis | 37f4057 | 2011-11-23 20:27:26 +0000 | [diff] [blame] | 916 | CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc, |
| 917 | ClassLoc, CatLoc, |
Argyrios Kyrtzidis | 955fadb | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 918 | CatName, IDecl); |
Argyrios Kyrtzidis | 37f4057 | 2011-11-23 20:27:26 +0000 | [diff] [blame] | 919 | CatIDecl->setImplicit(); |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 923 | ObjCCategoryImplDecl *CDecl = |
Argyrios Kyrtzidis | 1711fc9 | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 924 | ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl, |
Argyrios Kyrtzidis | c699400 | 2011-12-09 00:31:40 +0000 | [diff] [blame] | 925 | ClassLoc, AtCatImplLoc, CatLoc); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 926 | /// Check that class of this category is already completely declared. |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 927 | if (!IDecl) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 928 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 929 | CDecl->setInvalidDecl(); |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 930 | } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
| 931 | diag::err_undef_interface)) { |
| 932 | CDecl->setInvalidDecl(); |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 933 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 934 | |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 935 | // FIXME: PushOnScopeChains? |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 936 | CurContext->addDecl(CDecl); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 937 | |
Argyrios Kyrtzidis | c076e37 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 938 | // If the interface is deprecated/unavailable, warn/error about it. |
| 939 | if (IDecl) |
| 940 | DiagnoseUseOfDecl(IDecl, ClassLoc); |
| 941 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 942 | /// Check that CatName, category name, is not used in another implementation. |
| 943 | if (CatIDecl) { |
| 944 | if (CatIDecl->getImplementation()) { |
| 945 | Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName |
| 946 | << CatName; |
| 947 | Diag(CatIDecl->getImplementation()->getLocation(), |
| 948 | diag::note_previous_definition); |
Argyrios Kyrtzidis | df08c4b | 2013-05-30 18:53:21 +0000 | [diff] [blame] | 949 | CDecl->setInvalidDecl(); |
Fariborz Jahanian | b1224f6 | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 950 | } else { |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 951 | CatIDecl->setImplementation(CDecl); |
Fariborz Jahanian | b1224f6 | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 952 | // Warn on implementating category of deprecated class under |
| 953 | // -Wdeprecated-implementations flag. |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 954 | DiagnoseObjCImplementedDeprecations(*this, |
| 955 | dyn_cast<NamedDecl>(IDecl), |
| 956 | CDecl->getLocation(), 2); |
Fariborz Jahanian | b1224f6 | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 957 | } |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 958 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 959 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 960 | CheckObjCDeclScope(CDecl); |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 961 | return ActOnObjCContainerStartDefinition(CDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 962 | } |
| 963 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 964 | Decl *Sema::ActOnStartClassImplementation( |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 965 | SourceLocation AtClassImplLoc, |
| 966 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | IdentifierInfo *SuperClassname, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 968 | SourceLocation SuperClassLoc) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame^] | 969 | ObjCInterfaceDecl *IDecl = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 970 | // Check for another declaration kind with the same name. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 971 | NamedDecl *PrevDecl |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 972 | = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName, |
| 973 | ForRedeclaration); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 974 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 975 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 976 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 977 | } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) { |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 978 | RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
| 979 | diag::warn_undef_interface); |
Douglas Gregor | 95ff742 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 980 | } else { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame^] | 981 | // We did not find anything with the name ClassName; try to correct for |
Douglas Gregor | 95ff742 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 982 | // typos in the class name. |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 983 | ObjCInterfaceValidatorCCC Validator; |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame^] | 984 | TypoCorrection Corrected = |
| 985 | CorrectTypo(DeclarationNameInfo(ClassName, ClassLoc), |
| 986 | LookupOrdinaryName, TUScope, NULL, Validator); |
| 987 | if (Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
| 988 | // Suggest the (potentially) correct interface name. Don't provide a |
| 989 | // code-modification hint or use the typo name for recovery, because |
| 990 | // this is just a warning. The program may actually be correct. |
| 991 | diagnoseTypo(Corrected, |
| 992 | PDiag(diag::warn_undef_interface_suggest) << ClassName, |
| 993 | /*ErrorRecovery*/false); |
Douglas Gregor | 95ff742 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 994 | } else { |
| 995 | Diag(ClassLoc, diag::warn_undef_interface) << ClassName; |
| 996 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 997 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 998 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 999 | // Check that super class name is valid class name |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1000 | ObjCInterfaceDecl* SDecl = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1001 | if (SuperClassname) { |
| 1002 | // Check if a different kind of symbol declared in this scope. |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1003 | PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc, |
| 1004 | LookupOrdinaryName); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1005 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1006 | Diag(SuperClassLoc, diag::err_redefinition_different_kind) |
| 1007 | << SuperClassname; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1008 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1009 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1010 | SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Argyrios Kyrtzidis | cd707ab | 2012-03-13 01:09:36 +0000 | [diff] [blame] | 1011 | if (SDecl && !SDecl->hasDefinition()) |
| 1012 | SDecl = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1013 | if (!SDecl) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1014 | Diag(SuperClassLoc, diag::err_undef_superclass) |
| 1015 | << SuperClassname << ClassName; |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 1016 | else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1017 | // This implementation and its interface do not have the same |
| 1018 | // super class. |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1019 | Diag(SuperClassLoc, diag::err_conflicting_super_class) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1020 | << SDecl->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1021 | Diag(SDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1026 | if (!IDecl) { |
| 1027 | // Legacy case of @implementation with no corresponding @interface. |
| 1028 | // Build, chain & install the interface decl into the identifier. |
Daniel Dunbar | f641492 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 1029 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1030 | // FIXME: Do we support attributes on the @implementation? If so we should |
| 1031 | // copy them over. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc, |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 1033 | ClassName, /*PrevDecl=*/0, ClassLoc, |
| 1034 | true); |
Douglas Gregor | 2e5c15b | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 1035 | IDecl->startDefinition(); |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 1036 | if (SDecl) { |
| 1037 | IDecl->setSuperClass(SDecl); |
| 1038 | IDecl->setSuperClassLoc(SuperClassLoc); |
| 1039 | IDecl->setEndOfDefinitionLoc(SuperClassLoc); |
| 1040 | } else { |
| 1041 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
| 1042 | } |
| 1043 | |
Douglas Gregor | 8b9fb30 | 2009-04-24 00:16:12 +0000 | [diff] [blame] | 1044 | PushOnScopeChains(IDecl, TUScope); |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1045 | } else { |
| 1046 | // Mark the interface as being completed, even if it was just as |
| 1047 | // @class ....; |
| 1048 | // declaration; the user cannot reopen it. |
Douglas Gregor | 2e5c15b | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 1049 | if (!IDecl->hasDefinition()) |
| 1050 | IDecl->startDefinition(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1051 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1052 | |
| 1053 | ObjCImplementationDecl* IMPDecl = |
Argyrios Kyrtzidis | 1711fc9 | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 1054 | ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl, |
Argyrios Kyrtzidis | 634c563 | 2013-05-03 18:05:44 +0000 | [diff] [blame] | 1055 | ClassLoc, AtClassImplLoc, SuperClassLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1056 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1057 | if (CheckObjCDeclScope(IMPDecl)) |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1058 | return ActOnObjCContainerStartDefinition(IMPDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1059 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1060 | // Check that there is no duplicate implementation of this class. |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1061 | if (IDecl->getImplementation()) { |
| 1062 | // FIXME: Don't leak everything! |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1063 | Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 1064 | Diag(IDecl->getImplementation()->getLocation(), |
| 1065 | diag::note_previous_definition); |
Argyrios Kyrtzidis | df08c4b | 2013-05-30 18:53:21 +0000 | [diff] [blame] | 1066 | IMPDecl->setInvalidDecl(); |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 1067 | } else { // add it to the list. |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1068 | IDecl->setImplementation(IMPDecl); |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 1069 | PushOnScopeChains(IMPDecl, TUScope); |
Fariborz Jahanian | b1224f6 | 2011-02-15 00:59:30 +0000 | [diff] [blame] | 1070 | // Warn on implementating deprecated class under |
| 1071 | // -Wdeprecated-implementations flag. |
Fariborz Jahanian | 5ac96d5 | 2011-02-15 17:49:58 +0000 | [diff] [blame] | 1072 | DiagnoseObjCImplementedDeprecations(*this, |
| 1073 | dyn_cast<NamedDecl>(IDecl), |
| 1074 | IMPDecl->getLocation(), 1); |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1075 | } |
Argyrios Kyrtzidis | 3a38744 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 1076 | return ActOnObjCContainerStartDefinition(IMPDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Argyrios Kyrtzidis | 644af7b | 2012-02-23 21:11:20 +0000 | [diff] [blame] | 1079 | Sema::DeclGroupPtrTy |
| 1080 | Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) { |
| 1081 | SmallVector<Decl *, 64> DeclsInGroup; |
| 1082 | DeclsInGroup.reserve(Decls.size() + 1); |
| 1083 | |
| 1084 | for (unsigned i = 0, e = Decls.size(); i != e; ++i) { |
| 1085 | Decl *Dcl = Decls[i]; |
| 1086 | if (!Dcl) |
| 1087 | continue; |
| 1088 | if (Dcl->getDeclContext()->isFileContext()) |
| 1089 | Dcl->setTopLevelDeclInObjCContainer(); |
| 1090 | DeclsInGroup.push_back(Dcl); |
| 1091 | } |
| 1092 | |
| 1093 | DeclsInGroup.push_back(ObjCImpDecl); |
| 1094 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1095 | return BuildDeclaratorGroup(DeclsInGroup, false); |
Argyrios Kyrtzidis | 644af7b | 2012-02-23 21:11:20 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1098 | void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, |
| 1099 | ObjCIvarDecl **ivars, unsigned numIvars, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1100 | SourceLocation RBrace) { |
| 1101 | assert(ImpDecl && "missing implementation decl"); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1102 | ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1103 | if (!IDecl) |
| 1104 | return; |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 1105 | /// Check case of non-existing \@interface decl. |
| 1106 | /// (legacy objective-c \@implementation decl without an \@interface decl). |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1107 | /// Add implementations's ivar to the synthesize class's ivar list. |
Steve Naroff | 33feeb0 | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 1108 | if (IDecl->isImplicitInterfaceDecl()) { |
Douglas Gregor | 05c272f | 2011-12-15 22:34:59 +0000 | [diff] [blame] | 1109 | IDecl->setEndOfDefinitionLoc(RBrace); |
Fariborz Jahanian | 3a21cd9 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 1110 | // Add ivar's to class's DeclContext. |
| 1111 | for (unsigned i = 0, e = numIvars; i != e; ++i) { |
Fariborz Jahanian | 2f14c4d | 2010-02-17 18:10:54 +0000 | [diff] [blame] | 1112 | ivars[i]->setLexicalDeclContext(ImpDecl); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1113 | IDecl->makeDeclVisibleInContext(ivars[i]); |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 1114 | ImpDecl->addDecl(ivars[i]); |
Fariborz Jahanian | 3a21cd9 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1117 | return; |
| 1118 | } |
| 1119 | // If implementation has empty ivar list, just return. |
| 1120 | if (numIvars == 0) |
| 1121 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1122 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1123 | assert(ivars && "missing @implementation ivars"); |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1124 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | bd94d44 | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 1125 | if (ImpDecl->getSuperClass()) |
| 1126 | Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use); |
| 1127 | for (unsigned i = 0; i < numIvars; i++) { |
| 1128 | ObjCIvarDecl* ImplIvar = ivars[i]; |
| 1129 | if (const ObjCIvarDecl *ClsIvar = |
| 1130 | IDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
| 1131 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
| 1132 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
| 1133 | continue; |
| 1134 | } |
Fariborz Jahanian | 3b20f58 | 2013-06-26 22:10:27 +0000 | [diff] [blame] | 1135 | // Check class extensions (unnamed categories) for duplicate ivars. |
| 1136 | for (ObjCInterfaceDecl::visible_extensions_iterator |
| 1137 | Ext = IDecl->visible_extensions_begin(), |
| 1138 | ExtEnd = IDecl->visible_extensions_end(); |
| 1139 | Ext != ExtEnd; ++Ext) { |
| 1140 | ObjCCategoryDecl *CDecl = *Ext; |
| 1141 | if (const ObjCIvarDecl *ClsExtIvar = |
| 1142 | CDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
| 1143 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
| 1144 | Diag(ClsExtIvar->getLocation(), diag::note_previous_definition); |
| 1145 | continue; |
| 1146 | } |
| 1147 | } |
Fariborz Jahanian | bd94d44 | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 1148 | // Instance ivar to Implementation's DeclContext. |
| 1149 | ImplIvar->setLexicalDeclContext(ImpDecl); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1150 | IDecl->makeDeclVisibleInContext(ImplIvar); |
Fariborz Jahanian | bd94d44 | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 1151 | ImpDecl->addDecl(ImplIvar); |
| 1152 | } |
| 1153 | return; |
| 1154 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1155 | // Check interface's Ivar list against those in the implementation. |
| 1156 | // names and types must match. |
| 1157 | // |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1158 | unsigned j = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | ObjCInterfaceDecl::ivar_iterator |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 1160 | IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); |
| 1161 | for (; numIvars > 0 && IVI != IVE; ++IVI) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1162 | ObjCIvarDecl* ImplIvar = ivars[j++]; |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1163 | ObjCIvarDecl* ClsIvar = *IVI; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1164 | assert (ImplIvar && "missing implementation ivar"); |
| 1165 | assert (ClsIvar && "missing class ivar"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1166 | |
Steve Naroff | ca33129 | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 1167 | // First, make sure the types match. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1168 | if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1169 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1170 | << ImplIvar->getIdentifier() |
| 1171 | << ImplIvar->getType() << ClsIvar->getType(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1172 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1173 | } else if (ImplIvar->isBitField() && ClsIvar->isBitField() && |
| 1174 | ImplIvar->getBitWidthValue(Context) != |
| 1175 | ClsIvar->getBitWidthValue(Context)) { |
| 1176 | Diag(ImplIvar->getBitWidth()->getLocStart(), |
| 1177 | diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier(); |
| 1178 | Diag(ClsIvar->getBitWidth()->getLocStart(), |
| 1179 | diag::note_previous_definition); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | } |
Steve Naroff | ca33129 | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 1181 | // Make sure the names are identical. |
| 1182 | if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1183 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1184 | << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1185 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1186 | } |
| 1187 | --numIvars; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1188 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1189 | |
Chris Lattner | 609e4c7 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 1190 | if (numIvars > 0) |
Chris Lattner | 0e39105 | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 1191 | Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | 609e4c7 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 1192 | else if (IVI != IVE) |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1193 | Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 1196 | void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method, |
Fariborz Jahanian | 5214683 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 1197 | bool &IncompleteImpl, unsigned DiagID) { |
Fariborz Jahanian | 327126e | 2011-06-24 20:31:37 +0000 | [diff] [blame] | 1198 | // No point warning no definition of method which is 'unavailable'. |
Douglas Gregor | 86f6cf6 | 2012-12-11 18:53:07 +0000 | [diff] [blame] | 1199 | switch (method->getAvailability()) { |
| 1200 | case AR_Available: |
| 1201 | case AR_Deprecated: |
| 1202 | break; |
| 1203 | |
| 1204 | // Don't warn about unavailable or not-yet-introduced methods. |
| 1205 | case AR_NotYetIntroduced: |
| 1206 | case AR_Unavailable: |
Fariborz Jahanian | 327126e | 2011-06-24 20:31:37 +0000 | [diff] [blame] | 1207 | return; |
Douglas Gregor | 86f6cf6 | 2012-12-11 18:53:07 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Ted Kremenek | 8b43d2b | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 1210 | // FIXME: For now ignore 'IncompleteImpl'. |
| 1211 | // Previously we grouped all unimplemented methods under a single |
| 1212 | // warning, but some users strongly voiced that they would prefer |
| 1213 | // separate warnings. We will give that approach a try, as that |
| 1214 | // matches what we do with protocols. |
| 1215 | |
| 1216 | Diag(ImpLoc, DiagID) << method->getDeclName(); |
| 1217 | |
| 1218 | // Issue a note to the original declaration. |
| 1219 | SourceLocation MethodLoc = method->getLocStart(); |
| 1220 | if (MethodLoc.isValid()) |
| 1221 | Diag(MethodLoc, diag::note_method_declared_at) << method; |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
David Chisnall | e8a2d4c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 1224 | /// Determines if type B can be substituted for type A. Returns true if we can |
| 1225 | /// guarantee that anything that the user will do to an object of type A can |
| 1226 | /// also be done to an object of type B. This is trivially true if the two |
| 1227 | /// types are the same, or if B is a subclass of A. It becomes more complex |
| 1228 | /// in cases where protocols are involved. |
| 1229 | /// |
| 1230 | /// Object types in Objective-C describe the minimum requirements for an |
| 1231 | /// object, rather than providing a complete description of a type. For |
| 1232 | /// example, if A is a subclass of B, then B* may refer to an instance of A. |
| 1233 | /// The principle of substitutability means that we may use an instance of A |
| 1234 | /// anywhere that we may use an instance of B - it will implement all of the |
| 1235 | /// ivars of B and all of the methods of B. |
| 1236 | /// |
| 1237 | /// This substitutability is important when type checking methods, because |
| 1238 | /// the implementation may have stricter type definitions than the interface. |
| 1239 | /// The interface specifies minimum requirements, but the implementation may |
| 1240 | /// have more accurate ones. For example, a method may privately accept |
| 1241 | /// instances of B, but only publish that it accepts instances of A. Any |
| 1242 | /// object passed to it will be type checked against B, and so will implicitly |
| 1243 | /// by a valid A*. Similarly, a method may return a subclass of the class that |
| 1244 | /// it is declared as returning. |
| 1245 | /// |
| 1246 | /// This is most important when considering subclassing. A method in a |
| 1247 | /// subclass must accept any object as an argument that its superclass's |
| 1248 | /// implementation accepts. It may, however, accept a more general type |
| 1249 | /// without breaking substitutability (i.e. you can still use the subclass |
| 1250 | /// anywhere that you can use the superclass, but not vice versa). The |
| 1251 | /// converse requirement applies to return types: the return type for a |
| 1252 | /// subclass method must be a valid object of the kind that the superclass |
| 1253 | /// advertises, but it may be specified more accurately. This avoids the need |
| 1254 | /// for explicit down-casting by callers. |
| 1255 | /// |
| 1256 | /// Note: This is a stricter requirement than for assignment. |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1257 | static bool isObjCTypeSubstitutable(ASTContext &Context, |
| 1258 | const ObjCObjectPointerType *A, |
| 1259 | const ObjCObjectPointerType *B, |
| 1260 | bool rejectId) { |
| 1261 | // Reject a protocol-unqualified id. |
| 1262 | if (rejectId && B->isObjCIdType()) return false; |
David Chisnall | e8a2d4c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 1263 | |
| 1264 | // If B is a qualified id, then A must also be a qualified id and it must |
| 1265 | // implement all of the protocols in B. It may not be a qualified class. |
| 1266 | // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a |
| 1267 | // stricter definition so it is not substitutable for id<A>. |
| 1268 | if (B->isObjCQualifiedIdType()) { |
| 1269 | return A->isObjCQualifiedIdType() && |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1270 | Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0), |
| 1271 | QualType(B,0), |
| 1272 | false); |
David Chisnall | e8a2d4c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | /* |
| 1276 | // id is a special type that bypasses type checking completely. We want a |
| 1277 | // warning when it is used in one place but not another. |
| 1278 | if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false; |
| 1279 | |
| 1280 | |
| 1281 | // If B is a qualified id, then A must also be a qualified id (which it isn't |
| 1282 | // if we've got this far) |
| 1283 | if (B->isObjCQualifiedIdType()) return false; |
| 1284 | */ |
| 1285 | |
| 1286 | // Now we know that A and B are (potentially-qualified) class types. The |
| 1287 | // normal rules for assignment apply. |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1288 | return Context.canAssignObjCInterfaces(A, B); |
David Chisnall | e8a2d4c | 2010-10-25 17:23:52 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1291 | static SourceRange getTypeRange(TypeSourceInfo *TSI) { |
| 1292 | return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange()); |
| 1293 | } |
| 1294 | |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1295 | static bool CheckMethodOverrideReturn(Sema &S, |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1296 | ObjCMethodDecl *MethodImpl, |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1297 | ObjCMethodDecl *MethodDecl, |
Fariborz Jahanian | eee3ef1 | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 1298 | bool IsProtocolMethodDecl, |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1299 | bool IsOverridingMode, |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1300 | bool Warn) { |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1301 | if (IsProtocolMethodDecl && |
| 1302 | (MethodDecl->getObjCDeclQualifier() != |
| 1303 | MethodImpl->getObjCDeclQualifier())) { |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1304 | if (Warn) { |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1305 | S.Diag(MethodImpl->getLocation(), |
| 1306 | (IsOverridingMode ? |
| 1307 | diag::warn_conflicting_overriding_ret_type_modifiers |
| 1308 | : diag::warn_conflicting_ret_type_modifiers)) |
| 1309 | << MethodImpl->getDeclName() |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1310 | << getTypeRange(MethodImpl->getResultTypeSourceInfo()); |
| 1311 | S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration) |
| 1312 | << getTypeRange(MethodDecl->getResultTypeSourceInfo()); |
| 1313 | } |
| 1314 | else |
| 1315 | return false; |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1318 | if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(), |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1319 | MethodDecl->getResultType())) |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1320 | return true; |
| 1321 | if (!Warn) |
| 1322 | return false; |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1323 | |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1324 | unsigned DiagID = |
| 1325 | IsOverridingMode ? diag::warn_conflicting_overriding_ret_types |
| 1326 | : diag::warn_conflicting_ret_types; |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1327 | |
| 1328 | // Mismatches between ObjC pointers go into a different warning |
| 1329 | // category, and sometimes they're even completely whitelisted. |
| 1330 | if (const ObjCObjectPointerType *ImplPtrTy = |
| 1331 | MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) { |
| 1332 | if (const ObjCObjectPointerType *IfacePtrTy = |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1333 | MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) { |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1334 | // Allow non-matching return types as long as they don't violate |
| 1335 | // the principle of substitutability. Specifically, we permit |
| 1336 | // return types that are subclasses of the declared return type, |
| 1337 | // or that are more-qualified versions of the declared type. |
| 1338 | if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false)) |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1339 | return false; |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1340 | |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1341 | DiagID = |
| 1342 | IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types |
| 1343 | : diag::warn_non_covariant_ret_types; |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | S.Diag(MethodImpl->getLocation(), DiagID) |
| 1348 | << MethodImpl->getDeclName() |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1349 | << MethodDecl->getResultType() |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1350 | << MethodImpl->getResultType() |
| 1351 | << getTypeRange(MethodImpl->getResultTypeSourceInfo()); |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1352 | S.Diag(MethodDecl->getLocation(), |
| 1353 | IsOverridingMode ? diag::note_previous_declaration |
| 1354 | : diag::note_previous_definition) |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1355 | << getTypeRange(MethodDecl->getResultTypeSourceInfo()); |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1356 | return false; |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1359 | static bool CheckMethodOverrideParam(Sema &S, |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1360 | ObjCMethodDecl *MethodImpl, |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1361 | ObjCMethodDecl *MethodDecl, |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1362 | ParmVarDecl *ImplVar, |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1363 | ParmVarDecl *IfaceVar, |
Fariborz Jahanian | eee3ef1 | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 1364 | bool IsProtocolMethodDecl, |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1365 | bool IsOverridingMode, |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1366 | bool Warn) { |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1367 | if (IsProtocolMethodDecl && |
| 1368 | (ImplVar->getObjCDeclQualifier() != |
| 1369 | IfaceVar->getObjCDeclQualifier())) { |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1370 | if (Warn) { |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1371 | if (IsOverridingMode) |
| 1372 | S.Diag(ImplVar->getLocation(), |
| 1373 | diag::warn_conflicting_overriding_param_modifiers) |
| 1374 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
| 1375 | << MethodImpl->getDeclName(); |
| 1376 | else S.Diag(ImplVar->getLocation(), |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1377 | diag::warn_conflicting_param_modifiers) |
| 1378 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1379 | << MethodImpl->getDeclName(); |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1380 | S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration) |
| 1381 | << getTypeRange(IfaceVar->getTypeSourceInfo()); |
| 1382 | } |
| 1383 | else |
| 1384 | return false; |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1387 | QualType ImplTy = ImplVar->getType(); |
| 1388 | QualType IfaceTy = IfaceVar->getType(); |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1389 | |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1390 | if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy)) |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1391 | return true; |
| 1392 | |
| 1393 | if (!Warn) |
| 1394 | return false; |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1395 | unsigned DiagID = |
| 1396 | IsOverridingMode ? diag::warn_conflicting_overriding_param_types |
| 1397 | : diag::warn_conflicting_param_types; |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1398 | |
| 1399 | // Mismatches between ObjC pointers go into a different warning |
| 1400 | // category, and sometimes they're even completely whitelisted. |
| 1401 | if (const ObjCObjectPointerType *ImplPtrTy = |
| 1402 | ImplTy->getAs<ObjCObjectPointerType>()) { |
| 1403 | if (const ObjCObjectPointerType *IfacePtrTy = |
| 1404 | IfaceTy->getAs<ObjCObjectPointerType>()) { |
| 1405 | // Allow non-matching argument types as long as they don't |
| 1406 | // violate the principle of substitutability. Specifically, the |
| 1407 | // implementation must accept any objects that the superclass |
| 1408 | // accepts, however it may also accept others. |
| 1409 | if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true)) |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1410 | return false; |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1411 | |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1412 | DiagID = |
| 1413 | IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types |
| 1414 | : diag::warn_non_contravariant_param_types; |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | S.Diag(ImplVar->getLocation(), DiagID) |
| 1419 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1420 | << MethodImpl->getDeclName() << IfaceTy << ImplTy; |
| 1421 | S.Diag(IfaceVar->getLocation(), |
| 1422 | (IsOverridingMode ? diag::note_previous_declaration |
| 1423 | : diag::note_previous_definition)) |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1424 | << getTypeRange(IfaceVar->getTypeSourceInfo()); |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1425 | return false; |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1426 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1427 | |
| 1428 | /// In ARC, check whether the conventional meanings of the two methods |
| 1429 | /// match. If they don't, it's a hard error. |
| 1430 | static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl, |
| 1431 | ObjCMethodDecl *decl) { |
| 1432 | ObjCMethodFamily implFamily = impl->getMethodFamily(); |
| 1433 | ObjCMethodFamily declFamily = decl->getMethodFamily(); |
| 1434 | if (implFamily == declFamily) return false; |
| 1435 | |
| 1436 | // Since conventions are sorted by selector, the only possibility is |
| 1437 | // that the types differ enough to cause one selector or the other |
| 1438 | // to fall out of the family. |
| 1439 | assert(implFamily == OMF_None || declFamily == OMF_None); |
| 1440 | |
| 1441 | // No further diagnostics required on invalid declarations. |
| 1442 | if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true; |
| 1443 | |
| 1444 | const ObjCMethodDecl *unmatched = impl; |
| 1445 | ObjCMethodFamily family = declFamily; |
| 1446 | unsigned errorID = diag::err_arc_lost_method_convention; |
| 1447 | unsigned noteID = diag::note_arc_lost_method_convention; |
| 1448 | if (declFamily == OMF_None) { |
| 1449 | unmatched = decl; |
| 1450 | family = implFamily; |
| 1451 | errorID = diag::err_arc_gained_method_convention; |
| 1452 | noteID = diag::note_arc_gained_method_convention; |
| 1453 | } |
| 1454 | |
| 1455 | // Indexes into a %select clause in the diagnostic. |
| 1456 | enum FamilySelector { |
| 1457 | F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new |
| 1458 | }; |
| 1459 | FamilySelector familySelector = FamilySelector(); |
| 1460 | |
| 1461 | switch (family) { |
| 1462 | case OMF_None: llvm_unreachable("logic error, no method convention"); |
| 1463 | case OMF_retain: |
| 1464 | case OMF_release: |
| 1465 | case OMF_autorelease: |
| 1466 | case OMF_dealloc: |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 1467 | case OMF_finalize: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1468 | case OMF_retainCount: |
| 1469 | case OMF_self: |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 1470 | case OMF_performSelector: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1471 | // Mismatches for these methods don't change ownership |
| 1472 | // conventions, so we don't care. |
| 1473 | return false; |
| 1474 | |
| 1475 | case OMF_init: familySelector = F_init; break; |
| 1476 | case OMF_alloc: familySelector = F_alloc; break; |
| 1477 | case OMF_copy: familySelector = F_copy; break; |
| 1478 | case OMF_mutableCopy: familySelector = F_mutableCopy; break; |
| 1479 | case OMF_new: familySelector = F_new; break; |
| 1480 | } |
| 1481 | |
| 1482 | enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn }; |
| 1483 | ReasonSelector reasonSelector; |
| 1484 | |
| 1485 | // The only reason these methods don't fall within their families is |
| 1486 | // due to unusual result types. |
| 1487 | if (unmatched->getResultType()->isObjCObjectPointerType()) { |
| 1488 | reasonSelector = R_UnrelatedReturn; |
| 1489 | } else { |
| 1490 | reasonSelector = R_NonObjectReturn; |
| 1491 | } |
| 1492 | |
Joerg Sonnenberger | 7348454 | 2013-06-26 21:31:47 +0000 | [diff] [blame] | 1493 | S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector); |
| 1494 | S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1495 | |
| 1496 | return true; |
| 1497 | } |
John McCall | 10302c0 | 2010-10-28 02:34:38 +0000 | [diff] [blame] | 1498 | |
Fariborz Jahanian | 8daab97 | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 1499 | void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1500 | ObjCMethodDecl *MethodDecl, |
Fariborz Jahanian | 36bc2c6 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 1501 | bool IsProtocolMethodDecl) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1502 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1503 | checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl)) |
| 1504 | return; |
| 1505 | |
Fariborz Jahanian | 21761c8 | 2011-02-21 23:49:15 +0000 | [diff] [blame] | 1506 | CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
Fariborz Jahanian | 36bc2c6 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 1507 | IsProtocolMethodDecl, false, |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1508 | true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | |
Chris Lattner | 3aff919 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 1510 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 1511 | IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(), |
| 1512 | EF = MethodDecl->param_end(); |
| 1513 | IM != EM && IF != EF; ++IM, ++IF) { |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1514 | CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF, |
Fariborz Jahanian | 36bc2c6 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 1515 | IsProtocolMethodDecl, false, true); |
Fariborz Jahanian | 2112190 | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 1516 | } |
Fariborz Jahanian | 730cfb1 | 2011-08-10 17:16:30 +0000 | [diff] [blame] | 1517 | |
Fariborz Jahanian | 2112190 | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 1518 | if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) { |
Fariborz Jahanian | 36bc2c6 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 1519 | Diag(ImpMethodDecl->getLocation(), |
| 1520 | diag::warn_conflicting_variadic); |
Fariborz Jahanian | 2112190 | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 1521 | Diag(MethodDecl->getLocation(), diag::note_previous_declaration); |
Fariborz Jahanian | 2112190 | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 1522 | } |
Fariborz Jahanian | 2112190 | 2011-08-08 18:03:17 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
Fariborz Jahanian | 36bc2c6 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 1525 | void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method, |
| 1526 | ObjCMethodDecl *Overridden, |
| 1527 | bool IsProtocolMethodDecl) { |
| 1528 | |
| 1529 | CheckMethodOverrideReturn(*this, Method, Overridden, |
| 1530 | IsProtocolMethodDecl, true, |
| 1531 | true); |
| 1532 | |
| 1533 | for (ObjCMethodDecl::param_iterator IM = Method->param_begin(), |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 1534 | IF = Overridden->param_begin(), EM = Method->param_end(), |
| 1535 | EF = Overridden->param_end(); |
| 1536 | IM != EM && IF != EF; ++IM, ++IF) { |
Fariborz Jahanian | 36bc2c6 | 2011-10-10 17:53:29 +0000 | [diff] [blame] | 1537 | CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF, |
| 1538 | IsProtocolMethodDecl, true, true); |
| 1539 | } |
| 1540 | |
| 1541 | if (Method->isVariadic() != Overridden->isVariadic()) { |
| 1542 | Diag(Method->getLocation(), |
| 1543 | diag::warn_conflicting_overriding_variadic); |
| 1544 | Diag(Overridden->getLocation(), diag::note_previous_declaration); |
| 1545 | } |
| 1546 | } |
| 1547 | |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1548 | /// WarnExactTypedMethods - This routine issues a warning if method |
| 1549 | /// implementation declaration matches exactly that of its declaration. |
| 1550 | void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
| 1551 | ObjCMethodDecl *MethodDecl, |
| 1552 | bool IsProtocolMethodDecl) { |
| 1553 | // don't issue warning when protocol method is optional because primary |
| 1554 | // class is not required to implement it and it is safe for protocol |
| 1555 | // to implement it. |
| 1556 | if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional) |
| 1557 | return; |
| 1558 | // don't issue warning when primary class's method is |
| 1559 | // depecated/unavailable. |
| 1560 | if (MethodDecl->hasAttr<UnavailableAttr>() || |
| 1561 | MethodDecl->hasAttr<DeprecatedAttr>()) |
| 1562 | return; |
| 1563 | |
| 1564 | bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
| 1565 | IsProtocolMethodDecl, false, false); |
| 1566 | if (match) |
| 1567 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 1568 | IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(), |
| 1569 | EF = MethodDecl->param_end(); |
| 1570 | IM != EM && IF != EF; ++IM, ++IF) { |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1571 | match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, |
| 1572 | *IM, *IF, |
| 1573 | IsProtocolMethodDecl, false, false); |
| 1574 | if (!match) |
| 1575 | break; |
| 1576 | } |
| 1577 | if (match) |
| 1578 | match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic()); |
David Chisnall | 7ca13ef | 2011-08-08 17:32:19 +0000 | [diff] [blame] | 1579 | if (match) |
| 1580 | match = !(MethodDecl->isClassMethod() && |
| 1581 | MethodDecl->getSelector() == GetNullarySelector("load", Context)); |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1582 | |
| 1583 | if (match) { |
| 1584 | Diag(ImpMethodDecl->getLocation(), |
| 1585 | diag::warn_category_method_impl_match); |
Ted Kremenek | 3306ec1 | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 1586 | Diag(MethodDecl->getLocation(), diag::note_method_declared_at) |
| 1587 | << MethodDecl->getDeclName(); |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1588 | } |
| 1589 | } |
| 1590 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1591 | /// FIXME: Type hierarchies in Objective-C can be deep. We could most likely |
| 1592 | /// improve the efficiency of selector lookups and type checking by associating |
| 1593 | /// with each protocol / interface / category the flattened instance tables. If |
| 1594 | /// we used an immutable set to keep the table then it wouldn't add significant |
| 1595 | /// memory cost and it would be handy for lookups. |
Daniel Dunbar | b20ef3e | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 1596 | |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 1597 | /// CheckProtocolMethodDefs - This routine checks unimplemented methods |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1598 | /// Declared in protocol, and those referenced by it. |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 1599 | void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc, |
| 1600 | ObjCProtocolDecl *PDecl, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1601 | bool& IncompleteImpl, |
Benjamin Kramer | 811bfcd | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 1602 | const SelectorSet &InsMap, |
| 1603 | const SelectorSet &ClsMap, |
Fariborz Jahanian | f283859 | 2010-03-27 21:10:05 +0000 | [diff] [blame] | 1604 | ObjCContainerDecl *CDecl) { |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1605 | ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl); |
| 1606 | ObjCInterfaceDecl *IDecl = C ? C->getClassInterface() |
| 1607 | : dyn_cast<ObjCInterfaceDecl>(CDecl); |
Fariborz Jahanian | f283859 | 2010-03-27 21:10:05 +0000 | [diff] [blame] | 1608 | assert (IDecl && "CheckProtocolMethodDefs - IDecl is null"); |
| 1609 | |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1610 | ObjCInterfaceDecl *Super = IDecl->getSuperClass(); |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1611 | ObjCInterfaceDecl *NSIDecl = 0; |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1612 | if (getLangOpts().ObjCRuntime.isNeXTFamily()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1613 | // check to see if class implements forwardInvocation method and objects |
| 1614 | // of this class are derived from 'NSProxy' so that to forward requests |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1615 | // from one object to another. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1616 | // Under such conditions, which means that every method possible is |
| 1617 | // implemented in the class, we should not issue "Method definition not |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1618 | // found" warnings. |
| 1619 | // FIXME: Use a general GetUnarySelector method for this. |
| 1620 | IdentifierInfo* II = &Context.Idents.get("forwardInvocation"); |
| 1621 | Selector fISelector = Context.Selectors.getSelector(1, &II); |
| 1622 | if (InsMap.count(fISelector)) |
| 1623 | // Is IDecl derived from 'NSProxy'? If so, no instance methods |
| 1624 | // need be implemented in the implementation. |
| 1625 | NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy")); |
| 1626 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1627 | |
Fariborz Jahanian | 32b94be | 2013-01-07 19:21:03 +0000 | [diff] [blame] | 1628 | // If this is a forward protocol declaration, get its definition. |
| 1629 | if (!PDecl->isThisDeclarationADefinition() && |
| 1630 | PDecl->getDefinition()) |
| 1631 | PDecl = PDecl->getDefinition(); |
| 1632 | |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1633 | // If a method lookup fails locally we still need to look and see if |
| 1634 | // the method was implemented by a base class or an inherited |
| 1635 | // protocol. This lookup is slow, but occurs rarely in correct code |
| 1636 | // and otherwise would terminate in a warning. |
| 1637 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1638 | // check unimplemented instance methods. |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1639 | if (!NSIDecl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1640 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1641 | E = PDecl->instmeth_end(); I != E; ++I) { |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1642 | ObjCMethodDecl *method = *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1643 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 1644 | !method->isPropertyAccessor() && |
| 1645 | !InsMap.count(method->getSelector()) && |
| 1646 | (!Super || !Super->lookupInstanceMethod(method->getSelector()))) { |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1647 | // If a method is not implemented in the category implementation but |
| 1648 | // has been declared in its primary class, superclass, |
| 1649 | // or in one of their protocols, no need to issue the warning. |
| 1650 | // This is because method will be implemented in the primary class |
| 1651 | // or one of its super class implementation. |
| 1652 | |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1653 | // Ugly, but necessary. Method declared in protcol might have |
| 1654 | // have been synthesized due to a property declared in the class which |
| 1655 | // uses the protocol. |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1656 | if (ObjCMethodDecl *MethodInClass = |
| 1657 | IDecl->lookupInstanceMethod(method->getSelector(), |
Fariborz Jahanian | bf393be | 2012-04-05 22:14:12 +0000 | [diff] [blame] | 1658 | true /*shallowCategoryLookup*/)) |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 1659 | if (C || MethodInClass->isPropertyAccessor()) |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1660 | continue; |
| 1661 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
| 1662 | if (Diags.getDiagnosticLevel(DIAG, ImpLoc) |
| 1663 | != DiagnosticsEngine::Ignored) { |
| 1664 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG); |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1665 | Diag(CDecl->getLocation(), diag::note_required_for_protocol_at) |
| 1666 | << PDecl->getDeclName(); |
Fariborz Jahanian | 8822f7c | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 1667 | } |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1668 | } |
| 1669 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1670 | // check unimplemented class methods |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1672 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1673 | I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1674 | ObjCMethodDecl *method = *I; |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1675 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
| 1676 | !ClsMap.count(method->getSelector()) && |
Fariborz Jahanian | 8822f7c | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 1677 | (!Super || !Super->lookupClassMethod(method->getSelector()))) { |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1678 | // See above comment for instance method lookups. |
| 1679 | if (C && IDecl->lookupClassMethod(method->getSelector(), |
Fariborz Jahanian | bf393be | 2012-04-05 22:14:12 +0000 | [diff] [blame] | 1680 | true /*shallowCategoryLookup*/)) |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1681 | continue; |
Fariborz Jahanian | 5214683 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 1682 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1683 | if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != |
| 1684 | DiagnosticsEngine::Ignored) { |
Fariborz Jahanian | 5214683 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 1685 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG); |
| 1686 | Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) << |
| 1687 | PDecl->getDeclName(); |
| 1688 | } |
Fariborz Jahanian | 8822f7c | 2010-03-27 19:02:17 +0000 | [diff] [blame] | 1689 | } |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1690 | } |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 1691 | // Check on this protocols's referenced protocols, recursively. |
| 1692 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 1693 | E = PDecl->protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1694 | CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
Fariborz Jahanian | 1e159bc | 2011-07-16 00:08:33 +0000 | [diff] [blame] | 1697 | /// MatchAllMethodDeclarations - Check methods declared in interface |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1698 | /// or protocol against those declared in their implementations. |
| 1699 | /// |
Benjamin Kramer | 811bfcd | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 1700 | void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap, |
| 1701 | const SelectorSet &ClsMap, |
| 1702 | SelectorSet &InsMapSeen, |
| 1703 | SelectorSet &ClsMapSeen, |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1704 | ObjCImplDecl* IMPDecl, |
| 1705 | ObjCContainerDecl* CDecl, |
| 1706 | bool &IncompleteImpl, |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1707 | bool ImmediateClass, |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1708 | bool WarnCategoryMethodImpl) { |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1709 | // Check and see if instance methods in class interface have been |
| 1710 | // implemented in the implementation class. If so, their types match. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1711 | for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(), |
| 1712 | E = CDecl->instmeth_end(); I != E; ++I) { |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1713 | if (InsMapSeen.count((*I)->getSelector())) |
| 1714 | continue; |
| 1715 | InsMapSeen.insert((*I)->getSelector()); |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 1716 | if (!(*I)->isPropertyAccessor() && |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1717 | !InsMap.count((*I)->getSelector())) { |
| 1718 | if (ImmediateClass) |
Fariborz Jahanian | 5214683 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 1719 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl, |
Ted Kremenek | 8b43d2b | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 1720 | diag::warn_undef_method_impl); |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1721 | continue; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1722 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1723 | ObjCMethodDecl *ImpMethodDecl = |
Argyrios Kyrtzidis | 2334f3a | 2011-08-30 19:43:21 +0000 | [diff] [blame] | 1724 | IMPDecl->getInstanceMethod((*I)->getSelector()); |
| 1725 | assert(CDecl->getInstanceMethod((*I)->getSelector()) && |
| 1726 | "Expected to find the method through lookup as well"); |
| 1727 | ObjCMethodDecl *MethodDecl = *I; |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1728 | // ImpMethodDecl may be null as in a @dynamic property. |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1729 | if (ImpMethodDecl) { |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1730 | if (!WarnCategoryMethodImpl) |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1731 | WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl, |
| 1732 | isa<ObjCProtocolDecl>(CDecl)); |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 1733 | else if (!MethodDecl->isPropertyAccessor()) |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1734 | WarnExactTypedMethods(ImpMethodDecl, MethodDecl, |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1735 | isa<ObjCProtocolDecl>(CDecl)); |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1736 | } |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1737 | } |
| 1738 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1739 | |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1740 | // Check and see if class methods in class interface have been |
| 1741 | // implemented in the implementation class. If so, their types match. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1742 | for (ObjCInterfaceDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1743 | I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) { |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1744 | if (ClsMapSeen.count((*I)->getSelector())) |
| 1745 | continue; |
| 1746 | ClsMapSeen.insert((*I)->getSelector()); |
| 1747 | if (!ClsMap.count((*I)->getSelector())) { |
| 1748 | if (ImmediateClass) |
Fariborz Jahanian | 5214683 | 2010-03-31 18:23:33 +0000 | [diff] [blame] | 1749 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl, |
Ted Kremenek | 8b43d2b | 2013-03-27 00:02:21 +0000 | [diff] [blame] | 1750 | diag::warn_undef_method_impl); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1751 | } else { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1752 | ObjCMethodDecl *ImpMethodDecl = |
| 1753 | IMPDecl->getClassMethod((*I)->getSelector()); |
Argyrios Kyrtzidis | 2334f3a | 2011-08-30 19:43:21 +0000 | [diff] [blame] | 1754 | assert(CDecl->getClassMethod((*I)->getSelector()) && |
| 1755 | "Expected to find the method through lookup as well"); |
| 1756 | ObjCMethodDecl *MethodDecl = *I; |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1757 | if (!WarnCategoryMethodImpl) |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1758 | WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl, |
| 1759 | isa<ObjCProtocolDecl>(CDecl)); |
| 1760 | else |
| 1761 | WarnExactTypedMethods(ImpMethodDecl, MethodDecl, |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1762 | isa<ObjCProtocolDecl>(CDecl)); |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1763 | } |
| 1764 | } |
Fariborz Jahanian | f54e3ae | 2010-10-08 22:59:25 +0000 | [diff] [blame] | 1765 | |
Fariborz Jahanian | 41594c5 | 2013-08-14 23:58:55 +0000 | [diff] [blame] | 1766 | if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) { |
| 1767 | // Also, check for methods declared in protocols inherited by |
| 1768 | // this protocol. |
| 1769 | for (ObjCProtocolDecl::protocol_iterator |
| 1770 | PI = PD->protocol_begin(), E = PD->protocol_end(); PI != E; ++PI) |
| 1771 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 1772 | IMPDecl, (*PI), IncompleteImpl, false, |
| 1773 | WarnCategoryMethodImpl); |
| 1774 | } |
| 1775 | |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1776 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
Fariborz Jahanian | 6a6bb28 | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 1777 | // when checking that methods in implementation match their declaration, |
| 1778 | // i.e. when WarnCategoryMethodImpl is false, check declarations in class |
| 1779 | // extension; as well as those in categories. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1780 | if (!WarnCategoryMethodImpl) { |
| 1781 | for (ObjCInterfaceDecl::visible_categories_iterator |
| 1782 | Cat = I->visible_categories_begin(), |
| 1783 | CatEnd = I->visible_categories_end(); |
| 1784 | Cat != CatEnd; ++Cat) { |
Fariborz Jahanian | 6a6bb28 | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 1785 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1786 | IMPDecl, *Cat, IncompleteImpl, false, |
Fariborz Jahanian | 6a6bb28 | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 1787 | WarnCategoryMethodImpl); |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1788 | } |
| 1789 | } else { |
Fariborz Jahanian | 6a6bb28 | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 1790 | // Also methods in class extensions need be looked at next. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1791 | for (ObjCInterfaceDecl::visible_extensions_iterator |
| 1792 | Ext = I->visible_extensions_begin(), |
| 1793 | ExtEnd = I->visible_extensions_end(); |
| 1794 | Ext != ExtEnd; ++Ext) { |
Fariborz Jahanian | 6a6bb28 | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 1795 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1796 | IMPDecl, *Ext, IncompleteImpl, false, |
Fariborz Jahanian | 6a6bb28 | 2012-10-23 23:06:22 +0000 | [diff] [blame] | 1797 | WarnCategoryMethodImpl); |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1798 | } |
| 1799 | } |
| 1800 | |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1801 | // Check for any implementation of a methods declared in protocol. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1802 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 1803 | PI = I->all_referenced_protocol_begin(), |
| 1804 | E = I->all_referenced_protocol_end(); PI != E; ++PI) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1805 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 1806 | IMPDecl, |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1807 | (*PI), IncompleteImpl, false, |
| 1808 | WarnCategoryMethodImpl); |
Fariborz Jahanian | eee3ef1 | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 1809 | |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1810 | // FIXME. For now, we are not checking for extact match of methods |
| 1811 | // in category implementation and its primary class's super class. |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1812 | if (!WarnCategoryMethodImpl && I->getSuperClass()) |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1813 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1814 | IMPDecl, |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1815 | I->getSuperClass(), IncompleteImpl, false); |
| 1816 | } |
| 1817 | } |
| 1818 | |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1819 | /// CheckCategoryVsClassMethodMatches - Checks that methods implemented in |
| 1820 | /// category matches with those implemented in its primary class and |
| 1821 | /// warns each time an exact match is found. |
| 1822 | void Sema::CheckCategoryVsClassMethodMatches( |
| 1823 | ObjCCategoryImplDecl *CatIMPDecl) { |
Benjamin Kramer | 811bfcd | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 1824 | SelectorSet InsMap, ClsMap; |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1825 | |
| 1826 | for (ObjCImplementationDecl::instmeth_iterator |
| 1827 | I = CatIMPDecl->instmeth_begin(), |
| 1828 | E = CatIMPDecl->instmeth_end(); I!=E; ++I) |
| 1829 | InsMap.insert((*I)->getSelector()); |
| 1830 | |
| 1831 | for (ObjCImplementationDecl::classmeth_iterator |
| 1832 | I = CatIMPDecl->classmeth_begin(), |
| 1833 | E = CatIMPDecl->classmeth_end(); I != E; ++I) |
| 1834 | ClsMap.insert((*I)->getSelector()); |
| 1835 | if (InsMap.empty() && ClsMap.empty()) |
| 1836 | return; |
| 1837 | |
| 1838 | // Get category's primary class. |
| 1839 | ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl(); |
| 1840 | if (!CatDecl) |
| 1841 | return; |
| 1842 | ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface(); |
| 1843 | if (!IDecl) |
| 1844 | return; |
Benjamin Kramer | 811bfcd | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 1845 | SelectorSet InsMapSeen, ClsMapSeen; |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1846 | bool IncompleteImpl = false; |
| 1847 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 1848 | CatIMPDecl, IDecl, |
Fariborz Jahanian | bb3d14e | 2012-02-09 21:30:24 +0000 | [diff] [blame] | 1849 | IncompleteImpl, false, |
| 1850 | true /*WarnCategoryMethodImpl*/); |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1851 | } |
Fariborz Jahanian | eee3ef1 | 2011-07-24 20:53:26 +0000 | [diff] [blame] | 1852 | |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1853 | void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | ObjCContainerDecl* CDecl, |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1855 | bool IncompleteImpl) { |
Benjamin Kramer | 811bfcd | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 1856 | SelectorSet InsMap; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1857 | // Check and see if instance methods in class interface have been |
| 1858 | // implemented in the implementation class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1859 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1860 | I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I) |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 1861 | InsMap.insert((*I)->getSelector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1862 | |
Fariborz Jahanian | 12bac25 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 1863 | // Check and see if properties declared in the interface have either 1) |
| 1864 | // an implementation or 2) there is a @synthesize/@dynamic implementation |
| 1865 | // of the property in the @implementation. |
Fariborz Jahanian | eb4f2c5 | 2012-01-03 19:46:00 +0000 | [diff] [blame] | 1866 | if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1867 | if (!(LangOpts.ObjCDefaultSynthProperties && |
| 1868 | LangOpts.ObjCRuntime.isNonFragile()) || |
| 1869 | IDecl->isObjCRequiresPropertyDefs()) |
Fariborz Jahanian | c775b1a | 2013-04-24 17:06:38 +0000 | [diff] [blame] | 1870 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl); |
Fariborz Jahanian | 3ac1eda | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1871 | |
Benjamin Kramer | 811bfcd | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 1872 | SelectorSet ClsMap; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1873 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1874 | I = IMPDecl->classmeth_begin(), |
| 1875 | E = IMPDecl->classmeth_end(); I != E; ++I) |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 1876 | ClsMap.insert((*I)->getSelector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1877 | |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1878 | // Check for type conflict of methods declared in a class/protocol and |
| 1879 | // its implementation; if any. |
Benjamin Kramer | 811bfcd | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 1880 | SelectorSet InsMapSeen, ClsMapSeen; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1881 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 1882 | IMPDecl, CDecl, |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1883 | IncompleteImpl, true); |
Fariborz Jahanian | 7413307 | 2011-08-03 18:21:12 +0000 | [diff] [blame] | 1884 | |
Fariborz Jahanian | fefe91e | 2011-07-28 23:19:50 +0000 | [diff] [blame] | 1885 | // check all methods implemented in category against those declared |
| 1886 | // in its primary class. |
| 1887 | if (ObjCCategoryImplDecl *CatDecl = |
| 1888 | dyn_cast<ObjCCategoryImplDecl>(IMPDecl)) |
| 1889 | CheckCategoryVsClassMethodMatches(CatDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1890 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1891 | // Check the protocol list for unimplemented methods in the @implementation |
| 1892 | // class. |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1893 | // Check and see if class methods in class interface have been |
| 1894 | // implemented in the implementation class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1895 | |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1896 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1897 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 1898 | PI = I->all_referenced_protocol_begin(), |
| 1899 | E = I->all_referenced_protocol_end(); PI != E; ++PI) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1900 | CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl, |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1901 | InsMap, ClsMap, I); |
| 1902 | // Check class extensions (unnamed categories) |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1903 | for (ObjCInterfaceDecl::visible_extensions_iterator |
| 1904 | Ext = I->visible_extensions_begin(), |
| 1905 | ExtEnd = I->visible_extensions_end(); |
| 1906 | Ext != ExtEnd; ++Ext) { |
| 1907 | ImplMethodsVsClassMethods(S, IMPDecl, *Ext, IncompleteImpl); |
| 1908 | } |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1909 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Fariborz Jahanian | b106fc6 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 1910 | // For extended class, unimplemented methods in its protocols will |
| 1911 | // be reported in the primary class. |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1912 | if (!C->IsClassExtension()) { |
Fariborz Jahanian | b106fc6 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 1913 | for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(), |
| 1914 | E = C->protocol_end(); PI != E; ++PI) |
| 1915 | CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl, |
Fariborz Jahanian | f283859 | 2010-03-27 21:10:05 +0000 | [diff] [blame] | 1916 | InsMap, ClsMap, CDecl); |
Fariborz Jahanian | c775b1a | 2013-04-24 17:06:38 +0000 | [diff] [blame] | 1917 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl); |
Fariborz Jahanian | 3ad230e | 2010-01-20 19:36:21 +0000 | [diff] [blame] | 1918 | } |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1919 | } else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1920 | llvm_unreachable("invalid ObjCContainerDecl type."); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | /// ActOnForwardClassDeclaration - |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 1924 | Sema::DeclGroupPtrTy |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1925 | Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
Chris Lattner | bdbde4d | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 1926 | IdentifierInfo **IdentList, |
Ted Kremenek | c09cba6 | 2009-11-17 23:12:20 +0000 | [diff] [blame] | 1927 | SourceLocation *IdentLocs, |
Chris Lattner | bdbde4d | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 1928 | unsigned NumElts) { |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 1929 | SmallVector<Decl *, 8> DeclsInGroup; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1930 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1931 | // Check for another declaration kind with the same name. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1932 | NamedDecl *PrevDecl |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1933 | = LookupSingleName(TUScope, IdentList[i], IdentLocs[i], |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 1934 | LookupOrdinaryName, ForRedeclaration); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1935 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Steve Naroff | c733388 | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 1936 | // GCC apparently allows the following idiom: |
| 1937 | // |
| 1938 | // typedef NSObject < XCElementTogglerP > XCElementToggler; |
| 1939 | // @class XCElementToggler; |
| 1940 | // |
Fariborz Jahanian | e42670b | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 1941 | // Here we have chosen to ignore the forward class declaration |
| 1942 | // with a warning. Since this is the implied behavior. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1943 | TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1944 | if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1945 | Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i]; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1946 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1947 | } else { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1948 | // a forward class declaration matching a typedef name of a class refers |
Fariborz Jahanian | e42670b | 2012-01-24 00:40:15 +0000 | [diff] [blame] | 1949 | // to the underlying class. Just ignore the forward class with a warning |
| 1950 | // as this will force the intended behavior which is to lookup the typedef |
| 1951 | // name. |
| 1952 | if (isa<ObjCObjectType>(TDD->getUnderlyingType())) { |
| 1953 | Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i]; |
| 1954 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 1955 | continue; |
| 1956 | } |
Fariborz Jahanian | cae27c5 | 2009-05-07 21:49:26 +0000 | [diff] [blame] | 1957 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1958 | } |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1959 | |
| 1960 | // Create a declaration to describe this forward declaration. |
Douglas Gregor | 0af5501 | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 1961 | ObjCInterfaceDecl *PrevIDecl |
| 1962 | = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Argyrios Kyrtzidis | e7e8fca | 2013-06-18 21:26:33 +0000 | [diff] [blame] | 1963 | |
| 1964 | IdentifierInfo *ClassName = IdentList[i]; |
| 1965 | if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) { |
| 1966 | // A previous decl with a different name is because of |
| 1967 | // @compatibility_alias, for example: |
| 1968 | // \code |
| 1969 | // @class NewImage; |
| 1970 | // @compatibility_alias OldImage NewImage; |
| 1971 | // \endcode |
| 1972 | // A lookup for 'OldImage' will return the 'NewImage' decl. |
| 1973 | // |
| 1974 | // In such a case use the real declaration name, instead of the alias one, |
| 1975 | // otherwise we will break IdentifierResolver and redecls-chain invariants. |
| 1976 | // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl |
| 1977 | // has been aliased. |
| 1978 | ClassName = PrevIDecl->getIdentifier(); |
| 1979 | } |
| 1980 | |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1981 | ObjCInterfaceDecl *IDecl |
| 1982 | = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc, |
Argyrios Kyrtzidis | e7e8fca | 2013-06-18 21:26:33 +0000 | [diff] [blame] | 1983 | ClassName, PrevIDecl, IdentLocs[i]); |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1984 | IDecl->setAtEndRange(IdentLocs[i]); |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1985 | |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 1986 | PushOnScopeChains(IDecl, TUScope); |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 1987 | CheckObjCDeclScope(IDecl); |
| 1988 | DeclsInGroup.push_back(IDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1989 | } |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1990 | |
| 1991 | return BuildDeclaratorGroup(DeclsInGroup, false); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
John McCall | 0f4c4c4 | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 1994 | static bool tryMatchRecordTypes(ASTContext &Context, |
| 1995 | Sema::MethodMatchStrategy strategy, |
| 1996 | const Type *left, const Type *right); |
| 1997 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1998 | static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy, |
| 1999 | QualType leftQT, QualType rightQT) { |
| 2000 | const Type *left = |
| 2001 | Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr(); |
| 2002 | const Type *right = |
| 2003 | Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr(); |
| 2004 | |
| 2005 | if (left == right) return true; |
| 2006 | |
| 2007 | // If we're doing a strict match, the types have to match exactly. |
| 2008 | if (strategy == Sema::MMS_strict) return false; |
| 2009 | |
| 2010 | if (left->isIncompleteType() || right->isIncompleteType()) return false; |
| 2011 | |
| 2012 | // Otherwise, use this absurdly complicated algorithm to try to |
| 2013 | // validate the basic, low-level compatibility of the two types. |
| 2014 | |
| 2015 | // As a minimum, require the sizes and alignments to match. |
| 2016 | if (Context.getTypeInfo(left) != Context.getTypeInfo(right)) |
| 2017 | return false; |
| 2018 | |
| 2019 | // Consider all the kinds of non-dependent canonical types: |
| 2020 | // - functions and arrays aren't possible as return and parameter types |
| 2021 | |
| 2022 | // - vector types of equal size can be arbitrarily mixed |
| 2023 | if (isa<VectorType>(left)) return isa<VectorType>(right); |
| 2024 | if (isa<VectorType>(right)) return false; |
| 2025 | |
| 2026 | // - references should only match references of identical type |
John McCall | 0f4c4c4 | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 2027 | // - structs, unions, and Objective-C objects must match more-or-less |
| 2028 | // exactly |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2029 | // - everything else should be a scalar |
| 2030 | if (!left->isScalarType() || !right->isScalarType()) |
John McCall | 0f4c4c4 | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 2031 | return tryMatchRecordTypes(Context, strategy, left, right); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2032 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2033 | // Make scalars agree in kind, except count bools as chars, and group |
| 2034 | // all non-member pointers together. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2035 | Type::ScalarTypeKind leftSK = left->getScalarTypeKind(); |
| 2036 | Type::ScalarTypeKind rightSK = right->getScalarTypeKind(); |
| 2037 | if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral; |
| 2038 | if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2039 | if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer) |
| 2040 | leftSK = Type::STK_ObjCObjectPointer; |
| 2041 | if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer) |
| 2042 | rightSK = Type::STK_ObjCObjectPointer; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2043 | |
| 2044 | // Note that data member pointers and function member pointers don't |
| 2045 | // intermix because of the size differences. |
| 2046 | |
| 2047 | return (leftSK == rightSK); |
| 2048 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2049 | |
John McCall | 0f4c4c4 | 2011-06-16 01:15:19 +0000 | [diff] [blame] | 2050 | static bool tryMatchRecordTypes(ASTContext &Context, |
| 2051 | Sema::MethodMatchStrategy strategy, |
| 2052 | const Type *lt, const Type *rt) { |
| 2053 | assert(lt && rt && lt != rt); |
| 2054 | |
| 2055 | if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false; |
| 2056 | RecordDecl *left = cast<RecordType>(lt)->getDecl(); |
| 2057 | RecordDecl *right = cast<RecordType>(rt)->getDecl(); |
| 2058 | |
| 2059 | // Require union-hood to match. |
| 2060 | if (left->isUnion() != right->isUnion()) return false; |
| 2061 | |
| 2062 | // Require an exact match if either is non-POD. |
| 2063 | if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) || |
| 2064 | (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD())) |
| 2065 | return false; |
| 2066 | |
| 2067 | // Require size and alignment to match. |
| 2068 | if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false; |
| 2069 | |
| 2070 | // Require fields to match. |
| 2071 | RecordDecl::field_iterator li = left->field_begin(), le = left->field_end(); |
| 2072 | RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end(); |
| 2073 | for (; li != le && ri != re; ++li, ++ri) { |
| 2074 | if (!matchTypes(Context, strategy, li->getType(), ri->getType())) |
| 2075 | return false; |
| 2076 | } |
| 2077 | return (li == le && ri == re); |
| 2078 | } |
| 2079 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2080 | /// MatchTwoMethodDeclarations - Checks that two methods have matching type and |
| 2081 | /// returns true, or false, accordingly. |
| 2082 | /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2083 | bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left, |
| 2084 | const ObjCMethodDecl *right, |
| 2085 | MethodMatchStrategy strategy) { |
| 2086 | if (!matchTypes(Context, strategy, |
| 2087 | left->getResultType(), right->getResultType())) |
| 2088 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2089 | |
Douglas Gregor | 7666b03 | 2013-02-07 19:13:24 +0000 | [diff] [blame] | 2090 | // If either is hidden, it is not considered to match. |
| 2091 | if (left->isHidden() || right->isHidden()) |
| 2092 | return false; |
| 2093 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2094 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2095 | (left->hasAttr<NSReturnsRetainedAttr>() |
| 2096 | != right->hasAttr<NSReturnsRetainedAttr>() || |
| 2097 | left->hasAttr<NSConsumesSelfAttr>() |
| 2098 | != right->hasAttr<NSConsumesSelfAttr>())) |
| 2099 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2100 | |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 2101 | ObjCMethodDecl::param_const_iterator |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2102 | li = left->param_begin(), le = left->param_end(), ri = right->param_begin(), |
| 2103 | re = right->param_end(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2104 | |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 2105 | for (; li != le && ri != re; ++li, ++ri) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2106 | assert(ri != right->param_end() && "Param mismatch"); |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 2107 | const ParmVarDecl *lparm = *li, *rparm = *ri; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2108 | |
| 2109 | if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType())) |
| 2110 | return false; |
| 2111 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2112 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2113 | lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>()) |
| 2114 | return false; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2115 | } |
| 2116 | return true; |
| 2117 | } |
| 2118 | |
Douglas Gregor | ff310c7 | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 2119 | void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) { |
Argyrios Kyrtzidis | 2e3d8c0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 2120 | // Record at the head of the list whether there were 0, 1, or >= 2 methods |
| 2121 | // inside categories. |
Argyrios Kyrtzidis | ab3d509 | 2013-04-27 00:10:12 +0000 | [diff] [blame] | 2122 | if (ObjCCategoryDecl * |
| 2123 | CD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) |
| 2124 | if (!CD->IsClassExtension() && List->getBits() < 2) |
| 2125 | List->setBits(List->getBits()+1); |
Argyrios Kyrtzidis | 2e3d8c0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 2126 | |
Douglas Gregor | 44fae52 | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 2127 | // If the list is empty, make it a singleton list. |
| 2128 | if (List->Method == 0) { |
| 2129 | List->Method = Method; |
Argyrios Kyrtzidis | 2e3d8c0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 2130 | List->setNext(0); |
Douglas Gregor | ff310c7 | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 2131 | return; |
Douglas Gregor | 44fae52 | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | // We've seen a method with this name, see if we have already seen this type |
| 2135 | // signature. |
| 2136 | ObjCMethodList *Previous = List; |
Argyrios Kyrtzidis | 2e3d8c0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 2137 | for (; List; Previous = List, List = List->getNext()) { |
Douglas Gregor | fc46be9 | 2013-06-21 00:20:25 +0000 | [diff] [blame] | 2138 | // If we are building a module, keep all of the methods. |
| 2139 | if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty()) |
| 2140 | continue; |
| 2141 | |
Douglas Gregor | 5ac4b69 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 2142 | if (!MatchTwoMethodDeclarations(Method, List->Method)) |
Douglas Gregor | 44fae52 | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 2143 | continue; |
| 2144 | |
| 2145 | ObjCMethodDecl *PrevObjCMethod = List->Method; |
| 2146 | |
| 2147 | // Propagate the 'defined' bit. |
| 2148 | if (Method->isDefined()) |
| 2149 | PrevObjCMethod->setDefined(true); |
| 2150 | |
| 2151 | // If a method is deprecated, push it in the global pool. |
| 2152 | // This is used for better diagnostics. |
| 2153 | if (Method->isDeprecated()) { |
| 2154 | if (!PrevObjCMethod->isDeprecated()) |
| 2155 | List->Method = Method; |
| 2156 | } |
| 2157 | // If new method is unavailable, push it into global pool |
| 2158 | // unless previous one is deprecated. |
| 2159 | if (Method->isUnavailable()) { |
| 2160 | if (PrevObjCMethod->getAvailability() < AR_Deprecated) |
| 2161 | List->Method = Method; |
| 2162 | } |
| 2163 | |
Douglas Gregor | ff310c7 | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 2164 | return; |
Douglas Gregor | 44fae52 | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | // We have a new signature for an existing method - add it. |
| 2168 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Douglas Gregor | 5ac4b69 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 2169 | ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>(); |
Argyrios Kyrtzidis | 2e3d8c0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 2170 | Previous->setNext(new (Mem) ObjCMethodList(Method, 0)); |
Douglas Gregor | 44fae52 | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2173 | /// \brief Read the contents of the method pool for a given selector from |
| 2174 | /// external storage. |
Douglas Gregor | 5ac4b69 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 2175 | void Sema::ReadMethodPool(Selector Sel) { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2176 | assert(ExternalSource && "We need an external AST source"); |
Douglas Gregor | 5ac4b69 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 2177 | ExternalSource->ReadMethodPool(Sel); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
Douglas Gregor | ff310c7 | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 2180 | void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl, |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2181 | bool instance) { |
Argyrios Kyrtzidis | 9a0b6b4 | 2012-03-12 18:34:26 +0000 | [diff] [blame] | 2182 | // Ignore methods of invalid containers. |
| 2183 | if (cast<Decl>(Method->getDeclContext())->isInvalidDecl()) |
Douglas Gregor | ff310c7 | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 2184 | return; |
Argyrios Kyrtzidis | 9a0b6b4 | 2012-03-12 18:34:26 +0000 | [diff] [blame] | 2185 | |
Douglas Gregor | 0d266d6 | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 2186 | if (ExternalSource) |
| 2187 | ReadMethodPool(Method->getSelector()); |
| 2188 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2189 | GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector()); |
Douglas Gregor | 0d266d6 | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 2190 | if (Pos == MethodPool.end()) |
| 2191 | Pos = MethodPool.insert(std::make_pair(Method->getSelector(), |
| 2192 | GlobalMethods())).first; |
Douglas Gregor | 44fae52 | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 2193 | |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 2194 | Method->setDefined(impl); |
Douglas Gregor | 44fae52 | 2012-01-25 00:19:56 +0000 | [diff] [blame] | 2195 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2196 | ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second; |
Douglas Gregor | ff310c7 | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 2197 | addMethodToGlobalList(&Entry, Method); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2200 | /// Determines if this is an "acceptable" loose mismatch in the global |
| 2201 | /// method pool. This exists mostly as a hack to get around certain |
| 2202 | /// global mismatches which we can't afford to make warnings / errors. |
| 2203 | /// Really, what we want is a way to take a method out of the global |
| 2204 | /// method pool. |
| 2205 | static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen, |
| 2206 | ObjCMethodDecl *other) { |
| 2207 | if (!chosen->isInstanceMethod()) |
| 2208 | return false; |
| 2209 | |
| 2210 | Selector sel = chosen->getSelector(); |
| 2211 | if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length") |
| 2212 | return false; |
| 2213 | |
| 2214 | // Don't complain about mismatches for -length if the method we |
| 2215 | // chose has an integral result type. |
| 2216 | return (chosen->getResultType()->isIntegerType()); |
| 2217 | } |
| 2218 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2219 | ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 2220 | bool receiverIdOrClass, |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2221 | bool warn, bool instance) { |
Douglas Gregor | 0d266d6 | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 2222 | if (ExternalSource) |
| 2223 | ReadMethodPool(Sel); |
| 2224 | |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2225 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
Douglas Gregor | 0d266d6 | 2012-01-25 00:59:09 +0000 | [diff] [blame] | 2226 | if (Pos == MethodPool.end()) |
| 2227 | return 0; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2228 | |
Douglas Gregor | f0e0004 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 2229 | // Gather the non-hidden methods. |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2230 | ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second; |
Robert Wilhelm | e7205c0 | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 2231 | SmallVector<ObjCMethodDecl *, 4> Methods; |
Argyrios Kyrtzidis | 2e3d8c0 | 2013-04-17 00:08:58 +0000 | [diff] [blame] | 2232 | for (ObjCMethodList *M = &MethList; M; M = M->getNext()) { |
Douglas Gregor | f0e0004 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 2233 | if (M->Method && !M->Method->isHidden()) { |
| 2234 | // If we're not supposed to warn about mismatches, we're done. |
| 2235 | if (!warn) |
| 2236 | return M->Method; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2237 | |
Douglas Gregor | f0e0004 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 2238 | Methods.push_back(M->Method); |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2239 | } |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2240 | } |
Douglas Gregor | f0e0004 | 2013-01-16 18:47:38 +0000 | [diff] [blame] | 2241 | |
| 2242 | // If there aren't any visible methods, we're done. |
| 2243 | // FIXME: Recover if there are any known-but-hidden methods? |
| 2244 | if (Methods.empty()) |
| 2245 | return 0; |
| 2246 | |
| 2247 | if (Methods.size() == 1) |
| 2248 | return Methods[0]; |
| 2249 | |
| 2250 | // We found multiple methods, so we may have to complain. |
| 2251 | bool issueDiagnostic = false, issueError = false; |
| 2252 | |
| 2253 | // We support a warning which complains about *any* difference in |
| 2254 | // method signature. |
| 2255 | bool strictSelectorMatch = |
| 2256 | (receiverIdOrClass && warn && |
| 2257 | (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl, |
| 2258 | R.getBegin()) |
| 2259 | != DiagnosticsEngine::Ignored)); |
| 2260 | if (strictSelectorMatch) { |
| 2261 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 2262 | if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) { |
| 2263 | issueDiagnostic = true; |
| 2264 | break; |
| 2265 | } |
| 2266 | } |
| 2267 | } |
| 2268 | |
| 2269 | // If we didn't see any strict differences, we won't see any loose |
| 2270 | // differences. In ARC, however, we also need to check for loose |
| 2271 | // mismatches, because most of them are errors. |
| 2272 | if (!strictSelectorMatch || |
| 2273 | (issueDiagnostic && getLangOpts().ObjCAutoRefCount)) |
| 2274 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 2275 | // This checks if the methods differ in type mismatch. |
| 2276 | if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) && |
| 2277 | !isAcceptableMethodMismatch(Methods[0], Methods[I])) { |
| 2278 | issueDiagnostic = true; |
| 2279 | if (getLangOpts().ObjCAutoRefCount) |
| 2280 | issueError = true; |
| 2281 | break; |
| 2282 | } |
| 2283 | } |
| 2284 | |
| 2285 | if (issueDiagnostic) { |
| 2286 | if (issueError) |
| 2287 | Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R; |
| 2288 | else if (strictSelectorMatch) |
| 2289 | Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R; |
| 2290 | else |
| 2291 | Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; |
| 2292 | |
| 2293 | Diag(Methods[0]->getLocStart(), |
| 2294 | issueError ? diag::note_possibility : diag::note_using) |
| 2295 | << Methods[0]->getSourceRange(); |
| 2296 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 2297 | Diag(Methods[I]->getLocStart(), diag::note_also_found) |
| 2298 | << Methods[I]->getSourceRange(); |
| 2299 | } |
| 2300 | } |
| 2301 | return Methods[0]; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 2304 | ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) { |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 2305 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
| 2306 | if (Pos == MethodPool.end()) |
| 2307 | return 0; |
| 2308 | |
| 2309 | GlobalMethods &Methods = Pos->second; |
| 2310 | |
| 2311 | if (Methods.first.Method && Methods.first.Method->isDefined()) |
| 2312 | return Methods.first.Method; |
| 2313 | if (Methods.second.Method && Methods.second.Method->isDefined()) |
| 2314 | return Methods.second.Method; |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 2315 | return 0; |
| 2316 | } |
| 2317 | |
Fariborz Jahanian | f98c688 | 2013-05-30 21:48:58 +0000 | [diff] [blame] | 2318 | static void |
Fariborz Jahanian | 9464a08 | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 2319 | HelperSelectorsForTypoCorrection( |
| 2320 | SmallVectorImpl<const ObjCMethodDecl *> &BestMethod, |
| 2321 | StringRef Typo, const ObjCMethodDecl * Method) { |
| 2322 | const unsigned MaxEditDistance = 1; |
| 2323 | unsigned BestEditDistance = MaxEditDistance + 1; |
Richard Trieu | 4fe9644 | 2013-06-06 02:22:29 +0000 | [diff] [blame] | 2324 | std::string MethodName = Method->getSelector().getAsString(); |
Fariborz Jahanian | 9464a08 | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 2325 | |
| 2326 | unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size()); |
| 2327 | if (MinPossibleEditDistance > 0 && |
| 2328 | Typo.size() / MinPossibleEditDistance < 1) |
| 2329 | return; |
| 2330 | unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance); |
| 2331 | if (EditDistance > MaxEditDistance) |
| 2332 | return; |
| 2333 | if (EditDistance == BestEditDistance) |
| 2334 | BestMethod.push_back(Method); |
| 2335 | else if (EditDistance < BestEditDistance) { |
| 2336 | BestMethod.clear(); |
| 2337 | BestMethod.push_back(Method); |
Fariborz Jahanian | 9464a08 | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 2338 | } |
| 2339 | } |
| 2340 | |
Fariborz Jahanian | d395e34 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 2341 | static bool HelperIsMethodInObjCType(Sema &S, Selector Sel, |
| 2342 | QualType ObjectType) { |
| 2343 | if (ObjectType.isNull()) |
| 2344 | return true; |
| 2345 | if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/)) |
| 2346 | return true; |
| 2347 | return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) != 0; |
| 2348 | } |
| 2349 | |
Fariborz Jahanian | 9464a08 | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 2350 | const ObjCMethodDecl * |
Fariborz Jahanian | d395e34 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 2351 | Sema::SelectorsForTypoCorrection(Selector Sel, |
| 2352 | QualType ObjectType) { |
Fariborz Jahanian | 9464a08 | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 2353 | unsigned NumArgs = Sel.getNumArgs(); |
| 2354 | SmallVector<const ObjCMethodDecl *, 8> Methods; |
Fariborz Jahanian | 419245e | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 2355 | bool ObjectIsId = true, ObjectIsClass = true; |
| 2356 | if (ObjectType.isNull()) |
| 2357 | ObjectIsId = ObjectIsClass = false; |
| 2358 | else if (!ObjectType->isObjCObjectPointerType()) |
| 2359 | return 0; |
| 2360 | else if (const ObjCObjectPointerType *ObjCPtr = |
| 2361 | ObjectType->getAsObjCInterfacePointerType()) { |
| 2362 | ObjectType = QualType(ObjCPtr->getInterfaceType(), 0); |
| 2363 | ObjectIsId = ObjectIsClass = false; |
| 2364 | } |
| 2365 | else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType()) |
| 2366 | ObjectIsClass = false; |
| 2367 | else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType()) |
| 2368 | ObjectIsId = false; |
| 2369 | else |
| 2370 | return 0; |
Fariborz Jahanian | 9464a08 | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 2371 | |
| 2372 | for (GlobalMethodPool::iterator b = MethodPool.begin(), |
| 2373 | e = MethodPool.end(); b != e; b++) { |
| 2374 | // instance methods |
| 2375 | for (ObjCMethodList *M = &b->second.first; M; M=M->getNext()) |
| 2376 | if (M->Method && |
Fariborz Jahanian | cd9c9b5 | 2013-06-18 17:10:58 +0000 | [diff] [blame] | 2377 | (M->Method->getSelector().getNumArgs() == NumArgs) && |
| 2378 | (M->Method->getSelector() != Sel)) { |
Fariborz Jahanian | 419245e | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 2379 | if (ObjectIsId) |
| 2380 | Methods.push_back(M->Method); |
| 2381 | else if (!ObjectIsClass && |
| 2382 | HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType)) |
| 2383 | Methods.push_back(M->Method); |
| 2384 | } |
Fariborz Jahanian | 9464a08 | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 2385 | // class methods |
| 2386 | for (ObjCMethodList *M = &b->second.second; M; M=M->getNext()) |
| 2387 | if (M->Method && |
Fariborz Jahanian | cd9c9b5 | 2013-06-18 17:10:58 +0000 | [diff] [blame] | 2388 | (M->Method->getSelector().getNumArgs() == NumArgs) && |
| 2389 | (M->Method->getSelector() != Sel)) { |
Fariborz Jahanian | 419245e | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 2390 | if (ObjectIsClass) |
| 2391 | Methods.push_back(M->Method); |
| 2392 | else if (!ObjectIsId && |
| 2393 | HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType)) |
| 2394 | Methods.push_back(M->Method); |
| 2395 | } |
Fariborz Jahanian | 9464a08 | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | SmallVector<const ObjCMethodDecl *, 8> SelectedMethods; |
| 2399 | for (unsigned i = 0, e = Methods.size(); i < e; i++) { |
| 2400 | HelperSelectorsForTypoCorrection(SelectedMethods, |
| 2401 | Sel.getAsString(), Methods[i]); |
| 2402 | } |
| 2403 | return (SelectedMethods.size() == 1) ? SelectedMethods[0] : NULL; |
| 2404 | } |
| 2405 | |
| 2406 | static void |
Fariborz Jahanian | f98c688 | 2013-05-30 21:48:58 +0000 | [diff] [blame] | 2407 | HelperToDiagnoseMismatchedMethodsInGlobalPool(Sema &S, |
| 2408 | ObjCMethodList &MethList) { |
| 2409 | ObjCMethodList *M = &MethList; |
| 2410 | ObjCMethodDecl *TargetMethod = M->Method; |
| 2411 | while (TargetMethod && |
| 2412 | isa<ObjCImplDecl>(TargetMethod->getDeclContext())) { |
| 2413 | M = M->getNext(); |
| 2414 | TargetMethod = M ? M->Method : 0; |
| 2415 | } |
| 2416 | if (!TargetMethod) |
| 2417 | return; |
| 2418 | bool FirstTime = true; |
| 2419 | for (M = M->getNext(); M; M=M->getNext()) { |
| 2420 | ObjCMethodDecl *MatchingMethodDecl = M->Method; |
| 2421 | if (isa<ObjCImplDecl>(MatchingMethodDecl->getDeclContext())) |
| 2422 | continue; |
| 2423 | if (!S.MatchTwoMethodDeclarations(TargetMethod, |
| 2424 | MatchingMethodDecl, Sema::MMS_loose)) { |
| 2425 | if (FirstTime) { |
| 2426 | FirstTime = false; |
| 2427 | S.Diag(TargetMethod->getLocation(), diag::warning_multiple_selectors) |
| 2428 | << TargetMethod->getSelector(); |
| 2429 | } |
| 2430 | S.Diag(MatchingMethodDecl->getLocation(), diag::note_also_found); |
| 2431 | } |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | void Sema::DiagnoseMismatchedMethodsInGlobalPool() { |
| 2436 | unsigned DIAG = diag::warning_multiple_selectors; |
| 2437 | if (Diags.getDiagnosticLevel(DIAG, SourceLocation()) |
| 2438 | == DiagnosticsEngine::Ignored) |
| 2439 | return; |
| 2440 | for (GlobalMethodPool::iterator b = MethodPool.begin(), |
| 2441 | e = MethodPool.end(); b != e; b++) { |
| 2442 | // first, instance methods |
| 2443 | ObjCMethodList &InstMethList = b->second.first; |
| 2444 | HelperToDiagnoseMismatchedMethodsInGlobalPool(*this, InstMethList); |
Fariborz Jahanian | 639aa52 | 2013-05-30 21:52:50 +0000 | [diff] [blame] | 2445 | // second, class methods |
Fariborz Jahanian | f98c688 | 2013-05-30 21:48:58 +0000 | [diff] [blame] | 2446 | ObjCMethodList &ClsMethList = b->second.second; |
| 2447 | HelperToDiagnoseMismatchedMethodsInGlobalPool(*this, ClsMethList); |
| 2448 | } |
| 2449 | } |
| 2450 | |
| 2451 | /// DiagnoseDuplicateIvars - |
Fariborz Jahanian | f914b97 | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 2452 | /// Check for duplicate ivars in the entire class at the start of |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2453 | /// \@implementation. This becomes necesssary because class extension can |
Fariborz Jahanian | f914b97 | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 2454 | /// add ivars to a class in random order which will not be known until |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2455 | /// class's \@implementation is seen. |
Fariborz Jahanian | f914b97 | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 2456 | void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID, |
| 2457 | ObjCInterfaceDecl *SID) { |
| 2458 | for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(), |
| 2459 | IVE = ID->ivar_end(); IVI != IVE; ++IVI) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2460 | ObjCIvarDecl* Ivar = *IVI; |
Fariborz Jahanian | f914b97 | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 2461 | if (Ivar->isInvalidDecl()) |
| 2462 | continue; |
| 2463 | if (IdentifierInfo *II = Ivar->getIdentifier()) { |
| 2464 | ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II); |
| 2465 | if (prevIvar) { |
| 2466 | Diag(Ivar->getLocation(), diag::err_duplicate_member) << II; |
| 2467 | Diag(prevIvar->getLocation(), diag::note_previous_declaration); |
| 2468 | Ivar->setInvalidDecl(); |
| 2469 | } |
| 2470 | } |
| 2471 | } |
| 2472 | } |
| 2473 | |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 2474 | Sema::ObjCContainerKind Sema::getObjCContainerKind() const { |
| 2475 | switch (CurContext->getDeclKind()) { |
| 2476 | case Decl::ObjCInterface: |
| 2477 | return Sema::OCK_Interface; |
| 2478 | case Decl::ObjCProtocol: |
| 2479 | return Sema::OCK_Protocol; |
| 2480 | case Decl::ObjCCategory: |
| 2481 | if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension()) |
| 2482 | return Sema::OCK_ClassExtension; |
| 2483 | else |
| 2484 | return Sema::OCK_Category; |
| 2485 | case Decl::ObjCImplementation: |
| 2486 | return Sema::OCK_Implementation; |
| 2487 | case Decl::ObjCCategoryImpl: |
| 2488 | return Sema::OCK_CategoryImplementation; |
| 2489 | |
| 2490 | default: |
| 2491 | return Sema::OCK_None; |
| 2492 | } |
| 2493 | } |
| 2494 | |
Fariborz Jahanian | a3c6246 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 2495 | // Note: For class/category implementations, allMethods is always null. |
Robert Wilhelm | 0111e4d | 2013-07-17 21:14:35 +0000 | [diff] [blame] | 2496 | Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods, |
Fariborz Jahanian | 80f8aca | 2013-07-17 00:05:08 +0000 | [diff] [blame] | 2497 | ArrayRef<DeclGroupPtrTy> allTUVars) { |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 2498 | if (getObjCContainerKind() == Sema::OCK_None) |
| 2499 | return 0; |
| 2500 | |
| 2501 | assert(AtEnd.isValid() && "Invalid location for '@end'"); |
| 2502 | |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 2503 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); |
| 2504 | Decl *ClassDecl = cast<Decl>(OCD); |
Fariborz Jahanian | 63e963c | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 2505 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2506 | bool isInterfaceDeclKind = |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 2507 | isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) |
| 2508 | || isa<ObjCProtocolDecl>(ClassDecl); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2509 | bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 2510 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 2511 | // FIXME: Remove these and use the ObjCContainerDecl/DeclContext. |
| 2512 | llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; |
| 2513 | llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; |
| 2514 | |
Fariborz Jahanian | a3c6246 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 2515 | for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2516 | ObjCMethodDecl *Method = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2517 | cast_or_null<ObjCMethodDecl>(allMethods[i]); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2518 | |
| 2519 | if (!Method) continue; // Already issued a diagnostic. |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2520 | if (Method->isInstanceMethod()) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2521 | /// Check for instance method of the same name with incompatible types |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2522 | const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2523 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2524 | : false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2525 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 82b4e76 | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 2526 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2527 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2528 | << Method->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2529 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | bdb2d50 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 2530 | Method->setInvalidDecl(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2531 | } else { |
Fariborz Jahanian | 7209646 | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 2532 | if (PrevMethod) { |
Argyrios Kyrtzidis | 3a919e7 | 2011-10-14 08:02:31 +0000 | [diff] [blame] | 2533 | Method->setAsRedeclaration(PrevMethod); |
Fariborz Jahanian | 7209646 | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 2534 | if (!Context.getSourceManager().isInSystemHeader( |
| 2535 | Method->getLocation())) |
| 2536 | Diag(Method->getLocation(), diag::warn_duplicate_method_decl) |
| 2537 | << Method->getDeclName(); |
| 2538 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 2539 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2540 | InsMap[Method->getSelector()] = Method; |
| 2541 | /// The following allows us to typecheck messages to "id". |
Douglas Gregor | ff310c7 | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 2542 | AddInstanceMethodToGlobalPool(Method); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2543 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2544 | } else { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2545 | /// Check for class method of the same name with incompatible types |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2546 | const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2547 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2548 | : false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2549 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 82b4e76 | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 2550 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2551 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2552 | << Method->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2553 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | bdb2d50 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 2554 | Method->setInvalidDecl(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2555 | } else { |
Fariborz Jahanian | 7209646 | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 2556 | if (PrevMethod) { |
Argyrios Kyrtzidis | 3a919e7 | 2011-10-14 08:02:31 +0000 | [diff] [blame] | 2557 | Method->setAsRedeclaration(PrevMethod); |
Fariborz Jahanian | 7209646 | 2011-12-13 19:40:34 +0000 | [diff] [blame] | 2558 | if (!Context.getSourceManager().isInSystemHeader( |
| 2559 | Method->getLocation())) |
| 2560 | Diag(Method->getLocation(), diag::warn_duplicate_method_decl) |
| 2561 | << Method->getDeclName(); |
| 2562 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 2563 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2564 | ClsMap[Method->getSelector()] = Method; |
Douglas Gregor | ff310c7 | 2012-05-01 23:37:00 +0000 | [diff] [blame] | 2565 | AddFactoryMethodToGlobalPool(Method); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2566 | } |
| 2567 | } |
| 2568 | } |
Douglas Gregor | b892d70 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 2569 | if (isa<ObjCInterfaceDecl>(ClassDecl)) { |
| 2570 | // Nothing to do here. |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 2571 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Fariborz Jahanian | 77e14bd | 2008-12-06 19:59:02 +0000 | [diff] [blame] | 2572 | // Categories are used to extend the class by declaring new methods. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2573 | // By the same token, they are also used to add new properties. No |
Fariborz Jahanian | 77e14bd | 2008-12-06 19:59:02 +0000 | [diff] [blame] | 2574 | // need to compare the added property to those in the class. |
Daniel Dunbar | b20ef3e | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 2575 | |
Fariborz Jahanian | 88f5e9b | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 2576 | if (C->IsClassExtension()) { |
| 2577 | ObjCInterfaceDecl *CCPrimary = C->getClassInterface(); |
| 2578 | DiagnoseClassExtensionDupMethods(C, CCPrimary); |
Fariborz Jahanian | 88f5e9b | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 2579 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2580 | } |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 2581 | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) { |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 2582 | if (CDecl->getIdentifier()) |
| 2583 | // ProcessPropertyDecl is responsible for diagnosing conflicts with any |
| 2584 | // user-defined setter/getter. It also synthesizes setter/getter methods |
| 2585 | // and adds them to the DeclContext and global method pools. |
| 2586 | for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(), |
| 2587 | E = CDecl->prop_end(); |
| 2588 | I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2589 | ProcessPropertyDecl(*I, CDecl); |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 2590 | CDecl->setAtEndRange(AtEnd); |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 2591 | } |
| 2592 | if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 2593 | IC->setAtEndRange(AtEnd); |
Fariborz Jahanian | 7ca8b06 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 2594 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) { |
Fariborz Jahanian | c78f684 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 2595 | // Any property declared in a class extension might have user |
| 2596 | // declared setter or getter in current class extension or one |
| 2597 | // of the other class extensions. Mark them as synthesized as |
| 2598 | // property will be synthesized when property with same name is |
| 2599 | // seen in the @implementation. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2600 | for (ObjCInterfaceDecl::visible_extensions_iterator |
| 2601 | Ext = IDecl->visible_extensions_begin(), |
| 2602 | ExtEnd = IDecl->visible_extensions_end(); |
| 2603 | Ext != ExtEnd; ++Ext) { |
| 2604 | for (ObjCContainerDecl::prop_iterator I = Ext->prop_begin(), |
| 2605 | E = Ext->prop_end(); I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2606 | ObjCPropertyDecl *Property = *I; |
Fariborz Jahanian | c78f684 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 2607 | // Skip over properties declared @dynamic |
| 2608 | if (const ObjCPropertyImplDecl *PIDecl |
| 2609 | = IC->FindPropertyImplDecl(Property->getIdentifier())) |
| 2610 | if (PIDecl->getPropertyImplementation() |
| 2611 | == ObjCPropertyImplDecl::Dynamic) |
| 2612 | continue; |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2613 | |
| 2614 | for (ObjCInterfaceDecl::visible_extensions_iterator |
| 2615 | Ext = IDecl->visible_extensions_begin(), |
| 2616 | ExtEnd = IDecl->visible_extensions_end(); |
| 2617 | Ext != ExtEnd; ++Ext) { |
| 2618 | if (ObjCMethodDecl *GetterMethod |
| 2619 | = Ext->getInstanceMethod(Property->getGetterName())) |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2620 | GetterMethod->setPropertyAccessor(true); |
Fariborz Jahanian | c78f684 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 2621 | if (!Property->isReadOnly()) |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2622 | if (ObjCMethodDecl *SetterMethod |
| 2623 | = Ext->getInstanceMethod(Property->getSetterName())) |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2624 | SetterMethod->setPropertyAccessor(true); |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2625 | } |
Fariborz Jahanian | c78f684 | 2010-12-11 18:39:37 +0000 | [diff] [blame] | 2626 | } |
| 2627 | } |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 2628 | ImplMethodsVsClassMethods(S, IC, IDecl); |
Fariborz Jahanian | 7ca8b06 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 2629 | AtomicPropertySetterGetterRules(IC, IDecl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2630 | DiagnoseOwningPropertyGetterSynthesis(IC); |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 2631 | |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2632 | bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>(); |
| 2633 | if (IDecl->getSuperClass() == NULL) { |
| 2634 | // This class has no superclass, so check that it has been marked with |
| 2635 | // __attribute((objc_root_class)). |
| 2636 | if (!HasRootClassAttr) { |
| 2637 | SourceLocation DeclLoc(IDecl->getLocation()); |
| 2638 | SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc)); |
| 2639 | Diag(DeclLoc, diag::warn_objc_root_class_missing) |
| 2640 | << IDecl->getIdentifier(); |
| 2641 | // See if NSObject is in the current scope, and if it is, suggest |
| 2642 | // adding " : NSObject " to the class declaration. |
| 2643 | NamedDecl *IF = LookupSingleName(TUScope, |
| 2644 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject), |
| 2645 | DeclLoc, LookupOrdinaryName); |
| 2646 | ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
| 2647 | if (NSObjectDecl && NSObjectDecl->getDefinition()) { |
| 2648 | Diag(SuperClassLoc, diag::note_objc_needs_superclass) |
| 2649 | << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject "); |
| 2650 | } else { |
| 2651 | Diag(SuperClassLoc, diag::note_objc_needs_superclass); |
| 2652 | } |
| 2653 | } |
| 2654 | } else if (HasRootClassAttr) { |
| 2655 | // Complain that only root classes may have this attribute. |
| 2656 | Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass); |
| 2657 | } |
| 2658 | |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2659 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | f914b97 | 2010-02-23 23:41:11 +0000 | [diff] [blame] | 2660 | while (IDecl->getSuperClass()) { |
| 2661 | DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass()); |
| 2662 | IDecl = IDecl->getSuperClass(); |
| 2663 | } |
Patrick Beard | b2f6820 | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2664 | } |
Fariborz Jahanian | 7ca8b06 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 2665 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 2666 | SetIvarInitializers(IC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2667 | } else if (ObjCCategoryImplDecl* CatImplClass = |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 2668 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 2669 | CatImplClass->setAtEndRange(AtEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2670 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2671 | // Find category interface decl and then check that all methods declared |
Daniel Dunbar | b20ef3e | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 2672 | // in this interface are implemented in the category @implementation. |
Chris Lattner | 97a5887 | 2009-02-16 18:32:47 +0000 | [diff] [blame] | 2673 | if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) { |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2674 | if (ObjCCategoryDecl *Cat |
| 2675 | = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) { |
| 2676 | ImplMethodsVsClassMethods(S, CatImplClass, Cat); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2677 | } |
| 2678 | } |
| 2679 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2680 | if (isInterfaceDeclKind) { |
| 2681 | // Reject invalid vardecls. |
Fariborz Jahanian | a3c6246 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 2682 | for (unsigned i = 0, e = allTUVars.size(); i != e; i++) { |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2683 | DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>(); |
| 2684 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 2685 | if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) { |
Daniel Dunbar | 5466c7b | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 2686 | if (!VDecl->hasExternalStorage()) |
Steve Naroff | 8745416 | 2009-04-13 17:58:46 +0000 | [diff] [blame] | 2687 | Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass); |
Fariborz Jahanian | b31cb7f | 2009-03-21 18:06:45 +0000 | [diff] [blame] | 2688 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2689 | } |
Fariborz Jahanian | 38e24c7 | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 2690 | } |
Fariborz Jahanian | 10af879 | 2011-08-29 17:33:12 +0000 | [diff] [blame] | 2691 | ActOnObjCContainerFinishDefinition(); |
Argyrios Kyrtzidis | b4a686d | 2011-10-17 19:48:13 +0000 | [diff] [blame] | 2692 | |
Fariborz Jahanian | a3c6246 | 2013-07-16 15:33:19 +0000 | [diff] [blame] | 2693 | for (unsigned i = 0, e = allTUVars.size(); i != e; i++) { |
Argyrios Kyrtzidis | b4a686d | 2011-10-17 19:48:13 +0000 | [diff] [blame] | 2694 | DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>(); |
Argyrios Kyrtzidis | c14a03d | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 2695 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 2696 | (*I)->setTopLevelDeclInObjCContainer(); |
Argyrios Kyrtzidis | b4a686d | 2011-10-17 19:48:13 +0000 | [diff] [blame] | 2697 | Consumer.HandleTopLevelDeclInObjCContainer(DG); |
| 2698 | } |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 2699 | |
Dmitri Gribenko | abd56c8 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 2700 | ActOnDocumentableDecl(ClassDecl); |
Erik Verbruggen | d64251f | 2011-12-06 09:25:23 +0000 | [diff] [blame] | 2701 | return ClassDecl; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | |
| 2705 | /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for |
| 2706 | /// objective-c's type qualifier from the parser version of the same info. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2707 | static Decl::ObjCDeclQualifier |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2708 | CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { |
John McCall | 09e2c52 | 2011-05-01 03:04:29 +0000 | [diff] [blame] | 2709 | return (Decl::ObjCDeclQualifier) (unsigned) PQTVal; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
Ted Kremenek | 422bae7 | 2010-04-18 04:59:38 +0000 | [diff] [blame] | 2712 | static inline |
Fariborz Jahanian | 129a60b | 2012-08-24 23:50:13 +0000 | [diff] [blame] | 2713 | unsigned countAlignAttr(const AttrVec &A) { |
| 2714 | unsigned count=0; |
| 2715 | for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) |
| 2716 | if ((*i)->getKind() == attr::Aligned) |
| 2717 | ++count; |
| 2718 | return count; |
| 2719 | } |
| 2720 | |
| 2721 | static inline |
Fariborz Jahanian | 7fda400 | 2011-10-22 01:21:15 +0000 | [diff] [blame] | 2722 | bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD, |
| 2723 | const AttrVec &A) { |
| 2724 | // If method is only declared in implementation (private method), |
Fariborz Jahanian | 7fda400 | 2011-10-22 01:21:15 +0000 | [diff] [blame] | 2725 | // No need to issue any diagnostics on method definition with attributes. |
Fariborz Jahanian | ee28a4b | 2011-10-22 01:56:45 +0000 | [diff] [blame] | 2726 | if (!IMD) |
Fariborz Jahanian | 7fda400 | 2011-10-22 01:21:15 +0000 | [diff] [blame] | 2727 | return false; |
Fariborz Jahanian | 129a60b | 2012-08-24 23:50:13 +0000 | [diff] [blame] | 2728 | |
Fariborz Jahanian | ee28a4b | 2011-10-22 01:56:45 +0000 | [diff] [blame] | 2729 | // method declared in interface has no attribute. |
Fariborz Jahanian | 129a60b | 2012-08-24 23:50:13 +0000 | [diff] [blame] | 2730 | // But implementation has attributes. This is invalid. |
| 2731 | // Except when implementation has 'Align' attribute which is |
| 2732 | // immaterial to method declared in interface. |
Fariborz Jahanian | ee28a4b | 2011-10-22 01:56:45 +0000 | [diff] [blame] | 2733 | if (!IMD->hasAttrs()) |
Fariborz Jahanian | 129a60b | 2012-08-24 23:50:13 +0000 | [diff] [blame] | 2734 | return (A.size() > countAlignAttr(A)); |
Fariborz Jahanian | ee28a4b | 2011-10-22 01:56:45 +0000 | [diff] [blame] | 2735 | |
Fariborz Jahanian | 7fda400 | 2011-10-22 01:21:15 +0000 | [diff] [blame] | 2736 | const AttrVec &D = IMD->getAttrs(); |
Fariborz Jahanian | 7fda400 | 2011-10-22 01:21:15 +0000 | [diff] [blame] | 2737 | |
Fariborz Jahanian | 129a60b | 2012-08-24 23:50:13 +0000 | [diff] [blame] | 2738 | unsigned countAlignOnImpl = countAlignAttr(A); |
| 2739 | if (!countAlignOnImpl && (A.size() != D.size())) |
| 2740 | return true; |
| 2741 | else if (countAlignOnImpl) { |
| 2742 | unsigned countAlignOnDecl = countAlignAttr(D); |
| 2743 | if (countAlignOnDecl && (A.size() != D.size())) |
| 2744 | return true; |
| 2745 | else if (!countAlignOnDecl && |
| 2746 | ((A.size()-countAlignOnImpl) != D.size())) |
| 2747 | return true; |
| 2748 | } |
| 2749 | |
Fariborz Jahanian | 7fda400 | 2011-10-22 01:21:15 +0000 | [diff] [blame] | 2750 | // attributes on method declaration and definition must match exactly. |
| 2751 | // Note that we have at most a couple of attributes on methods, so this |
| 2752 | // n*n search is good enough. |
| 2753 | for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) { |
Fariborz Jahanian | 129a60b | 2012-08-24 23:50:13 +0000 | [diff] [blame] | 2754 | if ((*i)->getKind() == attr::Aligned) |
| 2755 | continue; |
Fariborz Jahanian | 7fda400 | 2011-10-22 01:21:15 +0000 | [diff] [blame] | 2756 | bool match = false; |
| 2757 | for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) { |
| 2758 | if ((*i)->getKind() == (*i1)->getKind()) { |
| 2759 | match = true; |
| 2760 | break; |
| 2761 | } |
| 2762 | } |
| 2763 | if (!match) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2764 | return true; |
Fariborz Jahanian | 7fda400 | 2011-10-22 01:21:15 +0000 | [diff] [blame] | 2765 | } |
Fariborz Jahanian | 129a60b | 2012-08-24 23:50:13 +0000 | [diff] [blame] | 2766 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2767 | return false; |
Ted Kremenek | 422bae7 | 2010-04-18 04:59:38 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2770 | /// \brief Check whether the declared result type of the given Objective-C |
| 2771 | /// method declaration is compatible with the method's class. |
| 2772 | /// |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 2773 | static Sema::ResultTypeCompatibilityKind |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2774 | CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method, |
| 2775 | ObjCInterfaceDecl *CurrentClass) { |
| 2776 | QualType ResultType = Method->getResultType(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2777 | |
| 2778 | // If an Objective-C method inherits its related result type, then its |
| 2779 | // declared result type must be compatible with its own class type. The |
| 2780 | // declared result type is compatible if: |
| 2781 | if (const ObjCObjectPointerType *ResultObjectType |
| 2782 | = ResultType->getAs<ObjCObjectPointerType>()) { |
| 2783 | // - it is id or qualified id, or |
| 2784 | if (ResultObjectType->isObjCIdType() || |
| 2785 | ResultObjectType->isObjCQualifiedIdType()) |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 2786 | return Sema::RTC_Compatible; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2787 | |
| 2788 | if (CurrentClass) { |
| 2789 | if (ObjCInterfaceDecl *ResultClass |
| 2790 | = ResultObjectType->getInterfaceDecl()) { |
| 2791 | // - it is the same as the method's class type, or |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 2792 | if (declaresSameEntity(CurrentClass, ResultClass)) |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 2793 | return Sema::RTC_Compatible; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2794 | |
| 2795 | // - it is a superclass of the method's class type |
| 2796 | if (ResultClass->isSuperClassOf(CurrentClass)) |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 2797 | return Sema::RTC_Compatible; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2798 | } |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 2799 | } else { |
| 2800 | // Any Objective-C pointer type might be acceptable for a protocol |
| 2801 | // method; we just don't know. |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 2802 | return Sema::RTC_Unknown; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2803 | } |
| 2804 | } |
| 2805 | |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 2806 | return Sema::RTC_Incompatible; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2809 | namespace { |
| 2810 | /// A helper class for searching for methods which a particular method |
| 2811 | /// overrides. |
| 2812 | class OverrideSearch { |
Daniel Dunbar | b732fce | 2012-02-29 03:04:05 +0000 | [diff] [blame] | 2813 | public: |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2814 | Sema &S; |
| 2815 | ObjCMethodDecl *Method; |
Daniel Dunbar | b732fce | 2012-02-29 03:04:05 +0000 | [diff] [blame] | 2816 | llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden; |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2817 | bool Recursive; |
| 2818 | |
| 2819 | public: |
| 2820 | OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) { |
| 2821 | Selector selector = method->getSelector(); |
| 2822 | |
| 2823 | // Bypass this search if we've never seen an instance/class method |
| 2824 | // with this selector before. |
| 2825 | Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector); |
| 2826 | if (it == S.MethodPool.end()) { |
Axel Naumann | 0ec56b7 | 2012-10-18 19:05:02 +0000 | [diff] [blame] | 2827 | if (!S.getExternalSource()) return; |
Douglas Gregor | 5ac4b69 | 2012-01-25 00:49:42 +0000 | [diff] [blame] | 2828 | S.ReadMethodPool(selector); |
| 2829 | |
| 2830 | it = S.MethodPool.find(selector); |
| 2831 | if (it == S.MethodPool.end()) |
| 2832 | return; |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2833 | } |
| 2834 | ObjCMethodList &list = |
| 2835 | method->isInstanceMethod() ? it->second.first : it->second.second; |
| 2836 | if (!list.Method) return; |
| 2837 | |
| 2838 | ObjCContainerDecl *container |
| 2839 | = cast<ObjCContainerDecl>(method->getDeclContext()); |
| 2840 | |
| 2841 | // Prevent the search from reaching this container again. This is |
| 2842 | // important with categories, which override methods from the |
| 2843 | // interface and each other. |
Douglas Gregor | c968334 | 2012-05-03 21:25:24 +0000 | [diff] [blame] | 2844 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) { |
| 2845 | searchFromContainer(container); |
Douglas Gregor | dd87224 | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 2846 | if (ObjCInterfaceDecl *Interface = Category->getClassInterface()) |
| 2847 | searchFromContainer(Interface); |
Douglas Gregor | c968334 | 2012-05-03 21:25:24 +0000 | [diff] [blame] | 2848 | } else { |
| 2849 | searchFromContainer(container); |
| 2850 | } |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2851 | } |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2852 | |
Daniel Dunbar | b732fce | 2012-02-29 03:04:05 +0000 | [diff] [blame] | 2853 | typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator; |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2854 | iterator begin() const { return Overridden.begin(); } |
| 2855 | iterator end() const { return Overridden.end(); } |
| 2856 | |
| 2857 | private: |
| 2858 | void searchFromContainer(ObjCContainerDecl *container) { |
| 2859 | if (container->isInvalidDecl()) return; |
| 2860 | |
| 2861 | switch (container->getDeclKind()) { |
| 2862 | #define OBJCCONTAINER(type, base) \ |
| 2863 | case Decl::type: \ |
| 2864 | searchFrom(cast<type##Decl>(container)); \ |
| 2865 | break; |
| 2866 | #define ABSTRACT_DECL(expansion) |
| 2867 | #define DECL(type, base) \ |
| 2868 | case Decl::type: |
| 2869 | #include "clang/AST/DeclNodes.inc" |
| 2870 | llvm_unreachable("not an ObjC container!"); |
| 2871 | } |
| 2872 | } |
| 2873 | |
| 2874 | void searchFrom(ObjCProtocolDecl *protocol) { |
Douglas Gregor | 5e2a1ff | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 2875 | if (!protocol->hasDefinition()) |
| 2876 | return; |
| 2877 | |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2878 | // A method in a protocol declaration overrides declarations from |
| 2879 | // referenced ("parent") protocols. |
| 2880 | search(protocol->getReferencedProtocols()); |
| 2881 | } |
| 2882 | |
| 2883 | void searchFrom(ObjCCategoryDecl *category) { |
| 2884 | // A method in a category declaration overrides declarations from |
| 2885 | // the main class and from protocols the category references. |
Douglas Gregor | c968334 | 2012-05-03 21:25:24 +0000 | [diff] [blame] | 2886 | // The main class is handled in the constructor. |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2887 | search(category->getReferencedProtocols()); |
| 2888 | } |
| 2889 | |
| 2890 | void searchFrom(ObjCCategoryImplDecl *impl) { |
| 2891 | // A method in a category definition that has a category |
| 2892 | // declaration overrides declarations from the category |
| 2893 | // declaration. |
| 2894 | if (ObjCCategoryDecl *category = impl->getCategoryDecl()) { |
| 2895 | search(category); |
Douglas Gregor | dd87224 | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 2896 | if (ObjCInterfaceDecl *Interface = category->getClassInterface()) |
| 2897 | search(Interface); |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2898 | |
| 2899 | // Otherwise it overrides declarations from the class. |
Douglas Gregor | dd87224 | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 2900 | } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) { |
| 2901 | search(Interface); |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2902 | } |
| 2903 | } |
| 2904 | |
| 2905 | void searchFrom(ObjCInterfaceDecl *iface) { |
| 2906 | // A method in a class declaration overrides declarations from |
Douglas Gregor | 2e5c15b | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 2907 | if (!iface->hasDefinition()) |
| 2908 | return; |
| 2909 | |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2910 | // - categories, |
Argyrios Kyrtzidis | 04593d0 | 2013-03-29 21:51:48 +0000 | [diff] [blame] | 2911 | for (ObjCInterfaceDecl::known_categories_iterator |
| 2912 | cat = iface->known_categories_begin(), |
| 2913 | catEnd = iface->known_categories_end(); |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2914 | cat != catEnd; ++cat) { |
| 2915 | search(*cat); |
| 2916 | } |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2917 | |
| 2918 | // - the super class, and |
| 2919 | if (ObjCInterfaceDecl *super = iface->getSuperClass()) |
| 2920 | search(super); |
| 2921 | |
| 2922 | // - any referenced protocols. |
| 2923 | search(iface->getReferencedProtocols()); |
| 2924 | } |
| 2925 | |
| 2926 | void searchFrom(ObjCImplementationDecl *impl) { |
| 2927 | // A method in a class implementation overrides declarations from |
| 2928 | // the class interface. |
Douglas Gregor | dd87224 | 2012-05-17 22:39:14 +0000 | [diff] [blame] | 2929 | if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) |
| 2930 | search(Interface); |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2931 | } |
| 2932 | |
| 2933 | |
| 2934 | void search(const ObjCProtocolList &protocols) { |
| 2935 | for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end(); |
| 2936 | i != e; ++i) |
| 2937 | search(*i); |
| 2938 | } |
| 2939 | |
| 2940 | void search(ObjCContainerDecl *container) { |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2941 | // Check for a method in this container which matches this selector. |
| 2942 | ObjCMethodDecl *meth = container->getMethod(Method->getSelector(), |
Argyrios Kyrtzidis | 04593d0 | 2013-03-29 21:51:48 +0000 | [diff] [blame] | 2943 | Method->isInstanceMethod(), |
| 2944 | /*AllowHidden=*/true); |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 2945 | |
| 2946 | // If we find one, record it and bail out. |
| 2947 | if (meth) { |
| 2948 | Overridden.insert(meth); |
| 2949 | return; |
| 2950 | } |
| 2951 | |
| 2952 | // Otherwise, search for methods that a hypothetical method here |
| 2953 | // would have overridden. |
| 2954 | |
| 2955 | // Note that we're now in a recursive case. |
| 2956 | Recursive = true; |
| 2957 | |
| 2958 | searchFromContainer(container); |
| 2959 | } |
| 2960 | }; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 2963 | void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod, |
| 2964 | ObjCInterfaceDecl *CurrentClass, |
| 2965 | ResultTypeCompatibilityKind RTC) { |
| 2966 | // Search for overridden methods and merge information down from them. |
| 2967 | OverrideSearch overrides(*this, ObjCMethod); |
| 2968 | // Keep track if the method overrides any method in the class's base classes, |
| 2969 | // its protocols, or its categories' protocols; we will keep that info |
| 2970 | // in the ObjCMethodDecl. |
| 2971 | // For this info, a method in an implementation is not considered as |
| 2972 | // overriding the same method in the interface or its categories. |
| 2973 | bool hasOverriddenMethodsInBaseOrProtocol = false; |
| 2974 | for (OverrideSearch::iterator |
| 2975 | i = overrides.begin(), e = overrides.end(); i != e; ++i) { |
| 2976 | ObjCMethodDecl *overridden = *i; |
| 2977 | |
Argyrios Kyrtzidis | e7a7772 | 2013-04-17 00:09:08 +0000 | [diff] [blame] | 2978 | if (!hasOverriddenMethodsInBaseOrProtocol) { |
| 2979 | if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) || |
| 2980 | CurrentClass != overridden->getClassInterface() || |
| 2981 | overridden->isOverriding()) { |
| 2982 | hasOverriddenMethodsInBaseOrProtocol = true; |
| 2983 | |
| 2984 | } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) { |
| 2985 | // OverrideSearch will return as "overridden" the same method in the |
| 2986 | // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to |
| 2987 | // check whether a category of a base class introduced a method with the |
| 2988 | // same selector, after the interface method declaration. |
| 2989 | // To avoid unnecessary lookups in the majority of cases, we use the |
| 2990 | // extra info bits in GlobalMethodPool to check whether there were any |
| 2991 | // category methods with this selector. |
| 2992 | GlobalMethodPool::iterator It = |
| 2993 | MethodPool.find(ObjCMethod->getSelector()); |
| 2994 | if (It != MethodPool.end()) { |
| 2995 | ObjCMethodList &List = |
| 2996 | ObjCMethod->isInstanceMethod()? It->second.first: It->second.second; |
| 2997 | unsigned CategCount = List.getBits(); |
| 2998 | if (CategCount > 0) { |
| 2999 | // If the method is in a category we'll do lookup if there were at |
| 3000 | // least 2 category methods recorded, otherwise only one will do. |
| 3001 | if (CategCount > 1 || |
| 3002 | !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) { |
| 3003 | OverrideSearch overrides(*this, overridden); |
| 3004 | for (OverrideSearch::iterator |
| 3005 | OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) { |
| 3006 | ObjCMethodDecl *SuperOverridden = *OI; |
Argyrios Kyrtzidis | ab3d509 | 2013-04-27 00:10:12 +0000 | [diff] [blame] | 3007 | if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) || |
| 3008 | CurrentClass != SuperOverridden->getClassInterface()) { |
Argyrios Kyrtzidis | e7a7772 | 2013-04-17 00:09:08 +0000 | [diff] [blame] | 3009 | hasOverriddenMethodsInBaseOrProtocol = true; |
| 3010 | overridden->setOverriding(true); |
| 3011 | break; |
| 3012 | } |
| 3013 | } |
| 3014 | } |
| 3015 | } |
| 3016 | } |
| 3017 | } |
| 3018 | } |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3019 | |
| 3020 | // Propagate down the 'related result type' bit from overridden methods. |
| 3021 | if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType()) |
| 3022 | ObjCMethod->SetRelatedResultType(); |
| 3023 | |
| 3024 | // Then merge the declarations. |
| 3025 | mergeObjCMethodDecls(ObjCMethod, overridden); |
| 3026 | |
| 3027 | if (ObjCMethod->isImplicit() && overridden->isImplicit()) |
| 3028 | continue; // Conflicting properties are detected elsewhere. |
| 3029 | |
| 3030 | // Check for overriding methods |
| 3031 | if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) || |
| 3032 | isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) |
| 3033 | CheckConflictingOverridingMethod(ObjCMethod, overridden, |
| 3034 | isa<ObjCProtocolDecl>(overridden->getDeclContext())); |
| 3035 | |
| 3036 | if (CurrentClass && overridden->getDeclContext() != CurrentClass && |
Fariborz Jahanian | c4133a4 | 2012-07-05 22:26:07 +0000 | [diff] [blame] | 3037 | isa<ObjCInterfaceDecl>(overridden->getDeclContext()) && |
| 3038 | !overridden->isImplicit() /* not meant for properties */) { |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3039 | ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(), |
| 3040 | E = ObjCMethod->param_end(); |
Douglas Gregor | 0a4a23a | 2012-05-17 23:13:29 +0000 | [diff] [blame] | 3041 | ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(), |
| 3042 | PrevE = overridden->param_end(); |
| 3043 | for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) { |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3044 | assert(PrevI != overridden->param_end() && "Param mismatch"); |
| 3045 | QualType T1 = Context.getCanonicalType((*ParamI)->getType()); |
| 3046 | QualType T2 = Context.getCanonicalType((*PrevI)->getType()); |
| 3047 | // If type of argument of method in this class does not match its |
| 3048 | // respective argument type in the super class method, issue warning; |
| 3049 | if (!Context.typesAreCompatible(T1, T2)) { |
| 3050 | Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super) |
| 3051 | << T1 << T2; |
| 3052 | Diag(overridden->getLocation(), diag::note_previous_declaration); |
| 3053 | break; |
| 3054 | } |
| 3055 | } |
| 3056 | } |
| 3057 | } |
| 3058 | |
| 3059 | ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol); |
| 3060 | } |
| 3061 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3062 | Decl *Sema::ActOnMethodDeclaration( |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 3063 | Scope *S, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3064 | SourceLocation MethodLoc, SourceLocation EndLoc, |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 3065 | tok::TokenKind MethodType, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3066 | ObjCDeclSpec &ReturnQT, ParsedType ReturnType, |
Argyrios Kyrtzidis | 11d7716 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 3067 | ArrayRef<SourceLocation> SelectorLocs, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3068 | Selector Sel, |
| 3069 | // optional arguments. The number of types/arguments is obtained |
| 3070 | // from the Sel.getNumArgs(). |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 3071 | ObjCArgInfo *ArgInfo, |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 3072 | DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3073 | AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, |
Fariborz Jahanian | 90ba78c | 2011-03-12 18:54:30 +0000 | [diff] [blame] | 3074 | bool isVariadic, bool MethodDefinition) { |
Steve Naroff | da323ad | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 3075 | // Make sure we can establish a context for the method. |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 3076 | if (!CurContext->isObjCContainer()) { |
Steve Naroff | da323ad | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 3077 | Diag(MethodLoc, diag::error_missing_method_context); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3078 | return 0; |
Steve Naroff | da323ad | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 3079 | } |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 3080 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); |
| 3081 | Decl *ClassDecl = cast<Decl>(OCD); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3082 | QualType resultDeclType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3083 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3084 | bool HasRelatedResultType = false; |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 3085 | TypeSourceInfo *ResultTInfo = 0; |
Steve Naroff | ccef371 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 3086 | if (ReturnType) { |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 3087 | resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3088 | |
Eli Friedman | ddb5a39 | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 3089 | if (CheckFunctionReturnType(resultDeclType, MethodLoc)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3090 | return 0; |
Eli Friedman | ddb5a39 | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 3091 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3092 | HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType()); |
Fariborz Jahanian | aab24a6 | 2011-07-21 17:00:47 +0000 | [diff] [blame] | 3093 | } else { // get the type for "id". |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3094 | resultDeclType = Context.getObjCIdType(); |
Fariborz Jahanian | feb4fa1 | 2011-07-21 17:38:14 +0000 | [diff] [blame] | 3095 | Diag(MethodLoc, diag::warn_missing_method_return_type) |
Argyrios Kyrtzidis | 11d7716 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 3096 | << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)"); |
Fariborz Jahanian | aab24a6 | 2011-07-21 17:00:47 +0000 | [diff] [blame] | 3097 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3098 | |
| 3099 | ObjCMethodDecl* ObjCMethod = |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 3100 | ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, |
Argyrios Kyrtzidis | 11d7716 | 2011-10-03 06:36:36 +0000 | [diff] [blame] | 3101 | resultDeclType, |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 3102 | ResultTInfo, |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 3103 | CurContext, |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 3104 | MethodType == tok::minus, isVariadic, |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 3105 | /*isPropertyAccessor=*/false, |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 3106 | /*isImplicitlyDeclared=*/false, /*isDefined=*/false, |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3107 | MethodDeclKind == tok::objc_optional |
| 3108 | ? ObjCMethodDecl::Optional |
| 3109 | : ObjCMethodDecl::Required, |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3110 | HasRelatedResultType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3111 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3112 | SmallVector<ParmVarDecl*, 16> Params; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3113 | |
Chris Lattner | 7db638d | 2009-04-11 19:42:43 +0000 | [diff] [blame] | 3114 | for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) { |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 3115 | QualType ArgType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3116 | TypeSourceInfo *DI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3117 | |
David Blaikie | 7247c88 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 3118 | if (!ArgInfo[i].Type) { |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 3119 | ArgType = Context.getObjCIdType(); |
| 3120 | DI = 0; |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 3121 | } else { |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 3122 | ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI); |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 3123 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3124 | |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 3125 | LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc, |
| 3126 | LookupOrdinaryName, ForRedeclaration); |
| 3127 | LookupName(R, S); |
| 3128 | if (R.isSingleResult()) { |
| 3129 | NamedDecl *PrevDecl = R.getFoundDecl(); |
| 3130 | if (S->isDeclScope(PrevDecl)) { |
Fariborz Jahanian | 90ba78c | 2011-03-12 18:54:30 +0000 | [diff] [blame] | 3131 | Diag(ArgInfo[i].NameLoc, |
| 3132 | (MethodDefinition ? diag::warn_method_param_redefinition |
| 3133 | : diag::warn_method_param_declaration)) |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 3134 | << ArgInfo[i].Name; |
| 3135 | Diag(PrevDecl->getLocation(), |
| 3136 | diag::note_previous_declaration); |
| 3137 | } |
| 3138 | } |
| 3139 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3140 | SourceLocation StartLoc = DI |
| 3141 | ? DI->getTypeLoc().getBeginLoc() |
| 3142 | : ArgInfo[i].NameLoc; |
| 3143 | |
John McCall | 81ef3e6 | 2011-04-23 02:46:06 +0000 | [diff] [blame] | 3144 | ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc, |
| 3145 | ArgInfo[i].NameLoc, ArgInfo[i].Name, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3146 | ArgType, DI, SC_None); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3147 | |
John McCall | 7079886 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 3148 | Param->setObjCMethodScopeInfo(i); |
| 3149 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 3150 | Param->setObjCDeclQualifier( |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 3151 | CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3152 | |
Chris Lattner | f97e8fa | 2009-04-11 19:34:56 +0000 | [diff] [blame] | 3153 | // Apply the attributes to the parameter. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 3154 | ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3155 | |
Fariborz Jahanian | 47b1d96 | 2012-01-14 18:44:35 +0000 | [diff] [blame] | 3156 | if (Param->hasAttr<BlocksAttr>()) { |
| 3157 | Diag(Param->getLocation(), diag::err_block_on_nonlocal); |
| 3158 | Param->setInvalidDecl(); |
| 3159 | } |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 3160 | S->AddDecl(Param); |
| 3161 | IdResolver.AddDecl(Param); |
| 3162 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 3163 | Params.push_back(Param); |
| 3164 | } |
Fariborz Jahanian | 7f53253 | 2011-02-09 22:20:01 +0000 | [diff] [blame] | 3165 | |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 3166 | for (unsigned i = 0, e = CNumArgs; i != e; ++i) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3167 | ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param); |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 3168 | QualType ArgType = Param->getType(); |
| 3169 | if (ArgType.isNull()) |
| 3170 | ArgType = Context.getObjCIdType(); |
| 3171 | else |
| 3172 | // Perform the default array/function conversions (C99 6.7.5.3p[7,8]). |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 3173 | ArgType = Context.getAdjustedParameterType(ArgType); |
Eli Friedman | ddb5a39 | 2013-06-14 21:14:10 +0000 | [diff] [blame] | 3174 | |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 3175 | Param->setDeclContext(ObjCMethod); |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 3176 | Params.push_back(Param); |
| 3177 | } |
| 3178 | |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 3179 | ObjCMethod->setMethodParams(Context, Params, SelectorLocs); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3180 | ObjCMethod->setObjCDeclQualifier( |
| 3181 | CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 3182 | |
| 3183 | if (AttrList) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 3184 | ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3185 | |
Douglas Gregor | bdb2d50 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 3186 | // Add the method now. |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3187 | const ObjCMethodDecl *PrevMethod = 0; |
| 3188 | if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3189 | if (MethodType == tok::minus) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3190 | PrevMethod = ImpDecl->getInstanceMethod(Sel); |
| 3191 | ImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3192 | } else { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3193 | PrevMethod = ImpDecl->getClassMethod(Sel); |
| 3194 | ImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3195 | } |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3196 | |
Fariborz Jahanian | 7fda400 | 2011-10-22 01:21:15 +0000 | [diff] [blame] | 3197 | ObjCMethodDecl *IMD = 0; |
| 3198 | if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface()) |
| 3199 | IMD = IDecl->lookupMethod(ObjCMethod->getSelector(), |
| 3200 | ObjCMethod->isInstanceMethod()); |
Fariborz Jahanian | 38b3bd8 | 2013-07-09 22:02:20 +0000 | [diff] [blame] | 3201 | if (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>() && |
| 3202 | !ObjCMethod->hasAttr<ObjCRequiresSuperAttr>()) { |
| 3203 | // merge the attribute into implementation. |
| 3204 | ObjCMethod->addAttr( |
| 3205 | new (Context) ObjCRequiresSuperAttr(ObjCMethod->getLocation(), Context)); |
| 3206 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3207 | if (ObjCMethod->hasAttrs() && |
Fariborz Jahanian | ec23678 | 2011-12-06 00:02:41 +0000 | [diff] [blame] | 3208 | containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) { |
Fariborz Jahanian | 28441e6 | 2011-12-21 00:09:11 +0000 | [diff] [blame] | 3209 | SourceLocation MethodLoc = IMD->getLocation(); |
| 3210 | if (!getSourceManager().isInSystemHeader(MethodLoc)) { |
| 3211 | Diag(EndLoc, diag::warn_attribute_method_def); |
Ted Kremenek | 3306ec1 | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 3212 | Diag(MethodLoc, diag::note_method_declared_at) |
| 3213 | << ObjCMethod->getDeclName(); |
Fariborz Jahanian | 28441e6 | 2011-12-21 00:09:11 +0000 | [diff] [blame] | 3214 | } |
Fariborz Jahanian | ec23678 | 2011-12-06 00:02:41 +0000 | [diff] [blame] | 3215 | } |
Douglas Gregor | bdb2d50 | 2010-12-21 17:34:17 +0000 | [diff] [blame] | 3216 | } else { |
| 3217 | cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3218 | } |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3219 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3220 | if (PrevMethod) { |
| 3221 | // You can never have two method definitions with the same name. |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3222 | Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3223 | << ObjCMethod->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 3224 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Fariborz Jahanian | fbff0c4 | 2013-05-13 17:27:00 +0000 | [diff] [blame] | 3225 | ObjCMethod->setInvalidDecl(); |
| 3226 | return ObjCMethod; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3227 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3228 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3229 | // If this Objective-C method does not have a related result type, but we |
| 3230 | // are allowed to infer related result types, try to do so based on the |
| 3231 | // method family. |
| 3232 | ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl); |
| 3233 | if (!CurrentClass) { |
| 3234 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) |
| 3235 | CurrentClass = Cat->getClassInterface(); |
| 3236 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl)) |
| 3237 | CurrentClass = Impl->getClassInterface(); |
| 3238 | else if (ObjCCategoryImplDecl *CatImpl |
| 3239 | = dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) |
| 3240 | CurrentClass = CatImpl->getClassInterface(); |
| 3241 | } |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3242 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3243 | ResultTypeCompatibilityKind RTC |
| 3244 | = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass); |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3245 | |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3246 | CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC); |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3247 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3248 | bool ARCError = false; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3249 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | b846381 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 3250 | ARCError = CheckARCMethodDecl(ObjCMethod); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3251 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3252 | // Infer the related result type when possible. |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 3253 | if (!ARCError && RTC == Sema::RTC_Compatible && |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3254 | !ObjCMethod->hasRelatedResultType() && |
| 3255 | LangOpts.ObjCInferRelatedResultType) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3256 | bool InferRelatedResultType = false; |
| 3257 | switch (ObjCMethod->getMethodFamily()) { |
| 3258 | case OMF_None: |
| 3259 | case OMF_copy: |
| 3260 | case OMF_dealloc: |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 3261 | case OMF_finalize: |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3262 | case OMF_mutableCopy: |
| 3263 | case OMF_release: |
| 3264 | case OMF_retainCount: |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 3265 | case OMF_performSelector: |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3266 | break; |
| 3267 | |
| 3268 | case OMF_alloc: |
| 3269 | case OMF_new: |
| 3270 | InferRelatedResultType = ObjCMethod->isClassMethod(); |
| 3271 | break; |
| 3272 | |
| 3273 | case OMF_init: |
| 3274 | case OMF_autorelease: |
| 3275 | case OMF_retain: |
| 3276 | case OMF_self: |
| 3277 | InferRelatedResultType = ObjCMethod->isInstanceMethod(); |
| 3278 | break; |
| 3279 | } |
| 3280 | |
John McCall | 6c2c250 | 2011-07-22 02:45:48 +0000 | [diff] [blame] | 3281 | if (InferRelatedResultType) |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3282 | ObjCMethod->SetRelatedResultType(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3283 | } |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 3284 | |
| 3285 | ActOnDocumentableDecl(ObjCMethod); |
| 3286 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3287 | return ObjCMethod; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3290 | bool Sema::CheckObjCDeclScope(Decl *D) { |
Fariborz Jahanian | 58a7649 | 2011-08-22 18:34:22 +0000 | [diff] [blame] | 3291 | // Following is also an error. But it is caused by a missing @end |
| 3292 | // and diagnostic is issued elsewhere. |
Argyrios Kyrtzidis | fce79eb | 2012-03-23 23:24:23 +0000 | [diff] [blame] | 3293 | if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 3294 | return false; |
Argyrios Kyrtzidis | fce79eb | 2012-03-23 23:24:23 +0000 | [diff] [blame] | 3295 | |
| 3296 | // If we switched context to translation unit while we are still lexically in |
| 3297 | // an objc container, it means the parser missed emitting an error. |
| 3298 | if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext())) |
| 3299 | return false; |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 3300 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 3301 | Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); |
| 3302 | D->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3303 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 3304 | return true; |
| 3305 | } |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3306 | |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 3307 | /// Called whenever \@defs(ClassName) is encountered in the source. Inserts the |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3308 | /// instance variables of ClassName into Decls. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3309 | void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart, |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3310 | IdentifierInfo *ClassName, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3311 | SmallVectorImpl<Decl*> &Decls) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3312 | // Check that ClassName is a valid class |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 3313 | ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3314 | if (!Class) { |
| 3315 | Diag(DeclStart, diag::err_undef_interface) << ClassName; |
| 3316 | return; |
| 3317 | } |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 3318 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | 0468fb9 | 2009-04-21 20:28:41 +0000 | [diff] [blame] | 3319 | Diag(DeclStart, diag::err_atdef_nonfragile_interface); |
| 3320 | return; |
| 3321 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3322 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3323 | // Collect the instance variables |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3324 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 3325 | Context.DeepCollectObjCIvars(Class, true, Ivars); |
Fariborz Jahanian | 4183335 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 3326 | // For each ivar, create a fresh ObjCAtDefsFieldDecl. |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 3327 | for (unsigned i = 0; i < Ivars.size(); i++) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3328 | const FieldDecl* ID = cast<FieldDecl>(Ivars[i]); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3329 | RecordDecl *Record = dyn_cast<RecordDecl>(TagD); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3330 | Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, |
| 3331 | /*FIXME: StartL=*/ID->getLocation(), |
| 3332 | ID->getLocation(), |
Fariborz Jahanian | 4183335 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 3333 | ID->getIdentifier(), ID->getType(), |
| 3334 | ID->getBitWidth()); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3335 | Decls.push_back(FD); |
Fariborz Jahanian | 4183335 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 3336 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3337 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3338 | // Introduce all of these fields into the appropriate scope. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3339 | for (SmallVectorImpl<Decl*>::iterator D = Decls.begin(); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3340 | D != Decls.end(); ++D) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3341 | FieldDecl *FD = cast<FieldDecl>(*D); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3342 | if (getLangOpts().CPlusPlus) |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3343 | PushOnScopeChains(cast<FieldDecl>(FD), S); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3344 | else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD)) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3345 | Record->addDecl(FD); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 3346 | } |
| 3347 | } |
| 3348 | |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3349 | /// \brief Build a type-check a new Objective-C exception variable declaration. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3350 | VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T, |
| 3351 | SourceLocation StartLoc, |
| 3352 | SourceLocation IdLoc, |
| 3353 | IdentifierInfo *Id, |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3354 | bool Invalid) { |
| 3355 | // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage |
| 3356 | // duration shall not be qualified by an address-space qualifier." |
| 3357 | // Since all parameters have automatic store duration, they can not have |
| 3358 | // an address space. |
| 3359 | if (T.getAddressSpace() != 0) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3360 | Diag(IdLoc, diag::err_arg_with_address_space); |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3361 | Invalid = true; |
| 3362 | } |
| 3363 | |
| 3364 | // An @catch parameter must be an unqualified object pointer type; |
| 3365 | // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"? |
| 3366 | if (Invalid) { |
| 3367 | // Don't do any further checking. |
Douglas Gregor | be270a0 | 2010-04-26 17:57:08 +0000 | [diff] [blame] | 3368 | } else if (T->isDependentType()) { |
| 3369 | // Okay: we don't know what this type will instantiate to. |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3370 | } else if (!T->isObjCObjectPointerType()) { |
| 3371 | Invalid = true; |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3372 | Diag(IdLoc ,diag::err_catch_param_not_objc_type); |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3373 | } else if (T->isObjCQualifiedIdType()) { |
| 3374 | Invalid = true; |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3375 | Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm); |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3376 | } |
| 3377 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3378 | VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3379 | T, TInfo, SC_None); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 3380 | New->setExceptionVariable(true); |
| 3381 | |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 3382 | // In ARC, infer 'retaining' for variables of retainable type. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3383 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New)) |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 3384 | Invalid = true; |
| 3385 | |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3386 | if (Invalid) |
| 3387 | New->setInvalidDecl(); |
| 3388 | return New; |
| 3389 | } |
| 3390 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3391 | Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) { |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3392 | const DeclSpec &DS = D.getDeclSpec(); |
| 3393 | |
| 3394 | // We allow the "register" storage class on exception variables because |
| 3395 | // GCC did, but we drop it completely. Any other storage class is an error. |
| 3396 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
| 3397 | Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm) |
| 3398 | << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc())); |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3399 | } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) { |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3400 | Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm) |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3401 | << DeclSpec::getSpecifierName(SCS); |
| 3402 | } |
| 3403 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
| 3404 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 3405 | diag::err_invalid_thread) |
| 3406 | << DeclSpec::getSpecifierName(TSCS); |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3407 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 3408 | |
Richard Smith | c7f8116 | 2013-03-18 22:52:47 +0000 | [diff] [blame] | 3409 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3410 | |
| 3411 | // Check that there are no default arguments inside the type of this |
| 3412 | // exception object (C++ only). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3413 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3414 | CheckExtraCXXDefaultArguments(D); |
| 3415 | |
Argyrios Kyrtzidis | 3215398 | 2011-06-28 03:01:15 +0000 | [diff] [blame] | 3416 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 3417 | QualType ExceptionType = TInfo->getType(); |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3418 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3419 | VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, |
| 3420 | D.getSourceRange().getBegin(), |
| 3421 | D.getIdentifierLoc(), |
| 3422 | D.getIdentifier(), |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3423 | D.isInvalidType()); |
| 3424 | |
| 3425 | // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1). |
| 3426 | if (D.getCXXScopeSpec().isSet()) { |
| 3427 | Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm) |
| 3428 | << D.getCXXScopeSpec().getRange(); |
| 3429 | New->setInvalidDecl(); |
| 3430 | } |
| 3431 | |
| 3432 | // Add the parameter declaration into this scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3433 | S->AddDecl(New); |
Douglas Gregor | 160b563 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3434 | if (D.getIdentifier()) |
| 3435 | IdResolver.AddDecl(New); |
| 3436 | |
| 3437 | ProcessDeclAttributes(S, New, D); |
| 3438 | |
| 3439 | if (New->hasAttr<BlocksAttr>()) |
| 3440 | Diag(New->getLocation(), diag::err_block_on_nonlocal); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3441 | return New; |
Douglas Gregor | 4e6c0d1 | 2010-04-23 23:01:43 +0000 | [diff] [blame] | 3442 | } |
Fariborz Jahanian | 786cd15 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 3443 | |
| 3444 | /// CollectIvarsToConstructOrDestruct - Collect those ivars which require |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 3445 | /// initialization. |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 3446 | void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3447 | SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 3448 | for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv; |
| 3449 | Iv= Iv->getNextIvar()) { |
Fariborz Jahanian | 786cd15 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 3450 | QualType QT = Context.getBaseElementType(Iv->getType()); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 3451 | if (QT->isRecordType()) |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 3452 | Ivars.push_back(Iv); |
Fariborz Jahanian | 786cd15 | 2010-04-27 17:18:58 +0000 | [diff] [blame] | 3453 | } |
| 3454 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 3455 | |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 3456 | void Sema::DiagnoseUseOfUnimplementedSelectors() { |
Douglas Gregor | 5b9dc7c | 2011-07-28 14:54:22 +0000 | [diff] [blame] | 3457 | // Load referenced selectors from the external source. |
| 3458 | if (ExternalSource) { |
| 3459 | SmallVector<std::pair<Selector, SourceLocation>, 4> Sels; |
| 3460 | ExternalSource->ReadReferencedSelectors(Sels); |
| 3461 | for (unsigned I = 0, N = Sels.size(); I != N; ++I) |
| 3462 | ReferencedSelectors[Sels[I].first] = Sels[I].second; |
| 3463 | } |
| 3464 | |
Fariborz Jahanian | f98c688 | 2013-05-30 21:48:58 +0000 | [diff] [blame] | 3465 | DiagnoseMismatchedMethodsInGlobalPool(); |
| 3466 | |
Fariborz Jahanian | 8b78913 | 2011-02-04 23:19:27 +0000 | [diff] [blame] | 3467 | // Warning will be issued only when selector table is |
| 3468 | // generated (which means there is at lease one implementation |
| 3469 | // in the TU). This is to match gcc's behavior. |
| 3470 | if (ReferencedSelectors.empty() || |
| 3471 | !Context.AnyObjCImplementation()) |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 3472 | return; |
| 3473 | for (llvm::DenseMap<Selector, SourceLocation>::iterator S = |
| 3474 | ReferencedSelectors.begin(), |
| 3475 | E = ReferencedSelectors.end(); S != E; ++S) { |
| 3476 | Selector Sel = (*S).first; |
| 3477 | if (!LookupImplementedMethodInGlobalPool(Sel)) |
| 3478 | Diag((*S).second, diag::warn_unimplemented_selector) << Sel; |
| 3479 | } |
| 3480 | return; |
| 3481 | } |