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