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