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