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