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