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