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