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