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