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