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