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