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