xref: /minix3/external/bsd/llvm/dist/clang/lib/Sema/SemaDeclObjC.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc //  This file implements semantic analysis for Objective C declarations.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "clang/Sema/SemaInternal.h"
15f4a2713aSLionel Sambuc #include "clang/AST/ASTConsumer.h"
16f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
17f4a2713aSLionel Sambuc #include "clang/AST/ASTMutationListener.h"
18*0a6a1f1dSLionel Sambuc #include "clang/AST/DataRecursiveASTVisitor.h"
19f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
20f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
21f4a2713aSLionel Sambuc #include "clang/AST/ExprObjC.h"
22f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
23f4a2713aSLionel Sambuc #include "clang/Sema/DeclSpec.h"
24f4a2713aSLionel Sambuc #include "clang/Sema/ExternalSemaSource.h"
25f4a2713aSLionel Sambuc #include "clang/Sema/Lookup.h"
26f4a2713aSLionel Sambuc #include "clang/Sema/Scope.h"
27f4a2713aSLionel Sambuc #include "clang/Sema/ScopeInfo.h"
28f4a2713aSLionel Sambuc #include "llvm/ADT/DenseSet.h"
29f4a2713aSLionel Sambuc 
30f4a2713aSLionel Sambuc using namespace clang;
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc /// Check whether the given method, which must be in the 'init'
33f4a2713aSLionel Sambuc /// family, is a valid member of that family.
34f4a2713aSLionel Sambuc ///
35f4a2713aSLionel Sambuc /// \param receiverTypeIfCall - if null, check this as if declaring it;
36f4a2713aSLionel Sambuc ///   if non-null, check this as if making a call to it with the given
37f4a2713aSLionel Sambuc ///   receiver type
38f4a2713aSLionel Sambuc ///
39f4a2713aSLionel Sambuc /// \return true to indicate that there was an error and appropriate
40f4a2713aSLionel Sambuc ///   actions were taken
checkInitMethod(ObjCMethodDecl * method,QualType receiverTypeIfCall)41f4a2713aSLionel Sambuc bool Sema::checkInitMethod(ObjCMethodDecl *method,
42f4a2713aSLionel Sambuc                            QualType receiverTypeIfCall) {
43f4a2713aSLionel Sambuc   if (method->isInvalidDecl()) return true;
44f4a2713aSLionel Sambuc 
45f4a2713aSLionel Sambuc   // This castAs is safe: methods that don't return an object
46f4a2713aSLionel Sambuc   // pointer won't be inferred as inits and will reject an explicit
47f4a2713aSLionel Sambuc   // objc_method_family(init).
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc   // We ignore protocols here.  Should we?  What about Class?
50f4a2713aSLionel Sambuc 
51*0a6a1f1dSLionel Sambuc   const ObjCObjectType *result =
52*0a6a1f1dSLionel Sambuc       method->getReturnType()->castAs<ObjCObjectPointerType>()->getObjectType();
53f4a2713aSLionel Sambuc 
54f4a2713aSLionel Sambuc   if (result->isObjCId()) {
55f4a2713aSLionel Sambuc     return false;
56f4a2713aSLionel Sambuc   } else if (result->isObjCClass()) {
57f4a2713aSLionel Sambuc     // fall through: always an error
58f4a2713aSLionel Sambuc   } else {
59f4a2713aSLionel Sambuc     ObjCInterfaceDecl *resultClass = result->getInterface();
60f4a2713aSLionel Sambuc     assert(resultClass && "unexpected object type!");
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc     // It's okay for the result type to still be a forward declaration
63f4a2713aSLionel Sambuc     // if we're checking an interface declaration.
64f4a2713aSLionel Sambuc     if (!resultClass->hasDefinition()) {
65f4a2713aSLionel Sambuc       if (receiverTypeIfCall.isNull() &&
66f4a2713aSLionel Sambuc           !isa<ObjCImplementationDecl>(method->getDeclContext()))
67f4a2713aSLionel Sambuc         return false;
68f4a2713aSLionel Sambuc 
69f4a2713aSLionel Sambuc     // Otherwise, we try to compare class types.
70f4a2713aSLionel Sambuc     } else {
71f4a2713aSLionel Sambuc       // If this method was declared in a protocol, we can't check
72f4a2713aSLionel Sambuc       // anything unless we have a receiver type that's an interface.
73*0a6a1f1dSLionel Sambuc       const ObjCInterfaceDecl *receiverClass = nullptr;
74f4a2713aSLionel Sambuc       if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
75f4a2713aSLionel Sambuc         if (receiverTypeIfCall.isNull())
76f4a2713aSLionel Sambuc           return false;
77f4a2713aSLionel Sambuc 
78f4a2713aSLionel Sambuc         receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
79f4a2713aSLionel Sambuc           ->getInterfaceDecl();
80f4a2713aSLionel Sambuc 
81f4a2713aSLionel Sambuc         // This can be null for calls to e.g. id<Foo>.
82f4a2713aSLionel Sambuc         if (!receiverClass) return false;
83f4a2713aSLionel Sambuc       } else {
84f4a2713aSLionel Sambuc         receiverClass = method->getClassInterface();
85f4a2713aSLionel Sambuc         assert(receiverClass && "method not associated with a class!");
86f4a2713aSLionel Sambuc       }
87f4a2713aSLionel Sambuc 
88f4a2713aSLionel Sambuc       // If either class is a subclass of the other, it's fine.
89f4a2713aSLionel Sambuc       if (receiverClass->isSuperClassOf(resultClass) ||
90f4a2713aSLionel Sambuc           resultClass->isSuperClassOf(receiverClass))
91f4a2713aSLionel Sambuc         return false;
92f4a2713aSLionel Sambuc     }
93f4a2713aSLionel Sambuc   }
94f4a2713aSLionel Sambuc 
95f4a2713aSLionel Sambuc   SourceLocation loc = method->getLocation();
96f4a2713aSLionel Sambuc 
97f4a2713aSLionel Sambuc   // If we're in a system header, and this is not a call, just make
98f4a2713aSLionel Sambuc   // the method unusable.
99f4a2713aSLionel Sambuc   if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
100*0a6a1f1dSLionel Sambuc     method->addAttr(UnavailableAttr::CreateImplicit(Context,
101*0a6a1f1dSLionel Sambuc                 "init method returns a type unrelated to its receiver type",
102*0a6a1f1dSLionel Sambuc                 loc));
103f4a2713aSLionel Sambuc     return true;
104f4a2713aSLionel Sambuc   }
105f4a2713aSLionel Sambuc 
106f4a2713aSLionel Sambuc   // Otherwise, it's an error.
107f4a2713aSLionel Sambuc   Diag(loc, diag::err_arc_init_method_unrelated_result_type);
108f4a2713aSLionel Sambuc   method->setInvalidDecl();
109f4a2713aSLionel Sambuc   return true;
110f4a2713aSLionel Sambuc }
111f4a2713aSLionel Sambuc 
CheckObjCMethodOverride(ObjCMethodDecl * NewMethod,const ObjCMethodDecl * Overridden)112f4a2713aSLionel Sambuc void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
113f4a2713aSLionel Sambuc                                    const ObjCMethodDecl *Overridden) {
114f4a2713aSLionel Sambuc   if (Overridden->hasRelatedResultType() &&
115f4a2713aSLionel Sambuc       !NewMethod->hasRelatedResultType()) {
116f4a2713aSLionel Sambuc     // This can only happen when the method follows a naming convention that
117f4a2713aSLionel Sambuc     // implies a related result type, and the original (overridden) method has
118f4a2713aSLionel Sambuc     // a suitable return type, but the new (overriding) method does not have
119f4a2713aSLionel Sambuc     // a suitable return type.
120*0a6a1f1dSLionel Sambuc     QualType ResultType = NewMethod->getReturnType();
121*0a6a1f1dSLionel Sambuc     SourceRange ResultTypeRange = NewMethod->getReturnTypeSourceRange();
122f4a2713aSLionel Sambuc 
123f4a2713aSLionel Sambuc     // Figure out which class this method is part of, if any.
124f4a2713aSLionel Sambuc     ObjCInterfaceDecl *CurrentClass
125f4a2713aSLionel Sambuc       = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
126f4a2713aSLionel Sambuc     if (!CurrentClass) {
127f4a2713aSLionel Sambuc       DeclContext *DC = NewMethod->getDeclContext();
128f4a2713aSLionel Sambuc       if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
129f4a2713aSLionel Sambuc         CurrentClass = Cat->getClassInterface();
130f4a2713aSLionel Sambuc       else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
131f4a2713aSLionel Sambuc         CurrentClass = Impl->getClassInterface();
132f4a2713aSLionel Sambuc       else if (ObjCCategoryImplDecl *CatImpl
133f4a2713aSLionel Sambuc                = dyn_cast<ObjCCategoryImplDecl>(DC))
134f4a2713aSLionel Sambuc         CurrentClass = CatImpl->getClassInterface();
135f4a2713aSLionel Sambuc     }
136f4a2713aSLionel Sambuc 
137f4a2713aSLionel Sambuc     if (CurrentClass) {
138f4a2713aSLionel Sambuc       Diag(NewMethod->getLocation(),
139f4a2713aSLionel Sambuc            diag::warn_related_result_type_compatibility_class)
140f4a2713aSLionel Sambuc         << Context.getObjCInterfaceType(CurrentClass)
141f4a2713aSLionel Sambuc         << ResultType
142f4a2713aSLionel Sambuc         << ResultTypeRange;
143f4a2713aSLionel Sambuc     } else {
144f4a2713aSLionel Sambuc       Diag(NewMethod->getLocation(),
145f4a2713aSLionel Sambuc            diag::warn_related_result_type_compatibility_protocol)
146f4a2713aSLionel Sambuc         << ResultType
147f4a2713aSLionel Sambuc         << ResultTypeRange;
148f4a2713aSLionel Sambuc     }
149f4a2713aSLionel Sambuc 
150f4a2713aSLionel Sambuc     if (ObjCMethodFamily Family = Overridden->getMethodFamily())
151f4a2713aSLionel Sambuc       Diag(Overridden->getLocation(),
152f4a2713aSLionel Sambuc            diag::note_related_result_type_family)
153f4a2713aSLionel Sambuc         << /*overridden method*/ 0
154f4a2713aSLionel Sambuc         << Family;
155f4a2713aSLionel Sambuc     else
156f4a2713aSLionel Sambuc       Diag(Overridden->getLocation(),
157f4a2713aSLionel Sambuc            diag::note_related_result_type_overridden);
158f4a2713aSLionel Sambuc   }
159f4a2713aSLionel Sambuc   if (getLangOpts().ObjCAutoRefCount) {
160f4a2713aSLionel Sambuc     if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
161f4a2713aSLionel Sambuc          Overridden->hasAttr<NSReturnsRetainedAttr>())) {
162f4a2713aSLionel Sambuc         Diag(NewMethod->getLocation(),
163f4a2713aSLionel Sambuc              diag::err_nsreturns_retained_attribute_mismatch) << 1;
164f4a2713aSLionel Sambuc         Diag(Overridden->getLocation(), diag::note_previous_decl)
165f4a2713aSLionel Sambuc         << "method";
166f4a2713aSLionel Sambuc     }
167f4a2713aSLionel Sambuc     if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
168f4a2713aSLionel Sambuc               Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
169f4a2713aSLionel Sambuc         Diag(NewMethod->getLocation(),
170f4a2713aSLionel Sambuc              diag::err_nsreturns_retained_attribute_mismatch) << 0;
171f4a2713aSLionel Sambuc         Diag(Overridden->getLocation(), diag::note_previous_decl)
172f4a2713aSLionel Sambuc         << "method";
173f4a2713aSLionel Sambuc     }
174f4a2713aSLionel Sambuc     ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(),
175f4a2713aSLionel Sambuc                                          oe = Overridden->param_end();
176f4a2713aSLionel Sambuc     for (ObjCMethodDecl::param_iterator
177f4a2713aSLionel Sambuc            ni = NewMethod->param_begin(), ne = NewMethod->param_end();
178f4a2713aSLionel Sambuc          ni != ne && oi != oe; ++ni, ++oi) {
179f4a2713aSLionel Sambuc       const ParmVarDecl *oldDecl = (*oi);
180f4a2713aSLionel Sambuc       ParmVarDecl *newDecl = (*ni);
181f4a2713aSLionel Sambuc       if (newDecl->hasAttr<NSConsumedAttr>() !=
182f4a2713aSLionel Sambuc           oldDecl->hasAttr<NSConsumedAttr>()) {
183f4a2713aSLionel Sambuc         Diag(newDecl->getLocation(),
184f4a2713aSLionel Sambuc              diag::err_nsconsumed_attribute_mismatch);
185f4a2713aSLionel Sambuc         Diag(oldDecl->getLocation(), diag::note_previous_decl)
186f4a2713aSLionel Sambuc           << "parameter";
187f4a2713aSLionel Sambuc       }
188f4a2713aSLionel Sambuc     }
189f4a2713aSLionel Sambuc   }
190f4a2713aSLionel Sambuc }
191f4a2713aSLionel Sambuc 
192f4a2713aSLionel Sambuc /// \brief Check a method declaration for compatibility with the Objective-C
193f4a2713aSLionel Sambuc /// ARC conventions.
CheckARCMethodDecl(ObjCMethodDecl * method)194f4a2713aSLionel Sambuc bool Sema::CheckARCMethodDecl(ObjCMethodDecl *method) {
195f4a2713aSLionel Sambuc   ObjCMethodFamily family = method->getMethodFamily();
196f4a2713aSLionel Sambuc   switch (family) {
197f4a2713aSLionel Sambuc   case OMF_None:
198f4a2713aSLionel Sambuc   case OMF_finalize:
199f4a2713aSLionel Sambuc   case OMF_retain:
200f4a2713aSLionel Sambuc   case OMF_release:
201f4a2713aSLionel Sambuc   case OMF_autorelease:
202f4a2713aSLionel Sambuc   case OMF_retainCount:
203f4a2713aSLionel Sambuc   case OMF_self:
204*0a6a1f1dSLionel Sambuc   case OMF_initialize:
205f4a2713aSLionel Sambuc   case OMF_performSelector:
206f4a2713aSLionel Sambuc     return false;
207f4a2713aSLionel Sambuc 
208f4a2713aSLionel Sambuc   case OMF_dealloc:
209*0a6a1f1dSLionel Sambuc     if (!Context.hasSameType(method->getReturnType(), Context.VoidTy)) {
210*0a6a1f1dSLionel Sambuc       SourceRange ResultTypeRange = method->getReturnTypeSourceRange();
211f4a2713aSLionel Sambuc       if (ResultTypeRange.isInvalid())
212f4a2713aSLionel Sambuc         Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
213*0a6a1f1dSLionel Sambuc             << method->getReturnType()
214f4a2713aSLionel Sambuc             << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)");
215f4a2713aSLionel Sambuc       else
216f4a2713aSLionel Sambuc         Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
217*0a6a1f1dSLionel Sambuc             << method->getReturnType()
218f4a2713aSLionel Sambuc             << FixItHint::CreateReplacement(ResultTypeRange, "void");
219f4a2713aSLionel Sambuc       return true;
220f4a2713aSLionel Sambuc     }
221f4a2713aSLionel Sambuc     return false;
222f4a2713aSLionel Sambuc 
223f4a2713aSLionel Sambuc   case OMF_init:
224f4a2713aSLionel Sambuc     // If the method doesn't obey the init rules, don't bother annotating it.
225f4a2713aSLionel Sambuc     if (checkInitMethod(method, QualType()))
226f4a2713aSLionel Sambuc       return true;
227f4a2713aSLionel Sambuc 
228*0a6a1f1dSLionel Sambuc     method->addAttr(NSConsumesSelfAttr::CreateImplicit(Context));
229f4a2713aSLionel Sambuc 
230f4a2713aSLionel Sambuc     // Don't add a second copy of this attribute, but otherwise don't
231f4a2713aSLionel Sambuc     // let it be suppressed.
232f4a2713aSLionel Sambuc     if (method->hasAttr<NSReturnsRetainedAttr>())
233f4a2713aSLionel Sambuc       return false;
234f4a2713aSLionel Sambuc     break;
235f4a2713aSLionel Sambuc 
236f4a2713aSLionel Sambuc   case OMF_alloc:
237f4a2713aSLionel Sambuc   case OMF_copy:
238f4a2713aSLionel Sambuc   case OMF_mutableCopy:
239f4a2713aSLionel Sambuc   case OMF_new:
240f4a2713aSLionel Sambuc     if (method->hasAttr<NSReturnsRetainedAttr>() ||
241f4a2713aSLionel Sambuc         method->hasAttr<NSReturnsNotRetainedAttr>() ||
242f4a2713aSLionel Sambuc         method->hasAttr<NSReturnsAutoreleasedAttr>())
243f4a2713aSLionel Sambuc       return false;
244f4a2713aSLionel Sambuc     break;
245f4a2713aSLionel Sambuc   }
246f4a2713aSLionel Sambuc 
247*0a6a1f1dSLionel Sambuc   method->addAttr(NSReturnsRetainedAttr::CreateImplicit(Context));
248f4a2713aSLionel Sambuc   return false;
249f4a2713aSLionel Sambuc }
250f4a2713aSLionel Sambuc 
DiagnoseObjCImplementedDeprecations(Sema & S,NamedDecl * ND,SourceLocation ImplLoc,int select)251f4a2713aSLionel Sambuc static void DiagnoseObjCImplementedDeprecations(Sema &S,
252f4a2713aSLionel Sambuc                                                 NamedDecl *ND,
253f4a2713aSLionel Sambuc                                                 SourceLocation ImplLoc,
254f4a2713aSLionel Sambuc                                                 int select) {
255f4a2713aSLionel Sambuc   if (ND && ND->isDeprecated()) {
256f4a2713aSLionel Sambuc     S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
257f4a2713aSLionel Sambuc     if (select == 0)
258f4a2713aSLionel Sambuc       S.Diag(ND->getLocation(), diag::note_method_declared_at)
259f4a2713aSLionel Sambuc         << ND->getDeclName();
260f4a2713aSLionel Sambuc     else
261f4a2713aSLionel Sambuc       S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
262f4a2713aSLionel Sambuc   }
263f4a2713aSLionel Sambuc }
264f4a2713aSLionel Sambuc 
265f4a2713aSLionel Sambuc /// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
266f4a2713aSLionel Sambuc /// pool.
AddAnyMethodToGlobalPool(Decl * D)267f4a2713aSLionel Sambuc void Sema::AddAnyMethodToGlobalPool(Decl *D) {
268f4a2713aSLionel Sambuc   ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
269f4a2713aSLionel Sambuc 
270f4a2713aSLionel Sambuc   // If we don't have a valid method decl, simply return.
271f4a2713aSLionel Sambuc   if (!MDecl)
272f4a2713aSLionel Sambuc     return;
273f4a2713aSLionel Sambuc   if (MDecl->isInstanceMethod())
274f4a2713aSLionel Sambuc     AddInstanceMethodToGlobalPool(MDecl, true);
275f4a2713aSLionel Sambuc   else
276f4a2713aSLionel Sambuc     AddFactoryMethodToGlobalPool(MDecl, true);
277f4a2713aSLionel Sambuc }
278f4a2713aSLionel Sambuc 
279f4a2713aSLionel Sambuc /// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer
280f4a2713aSLionel Sambuc /// has explicit ownership attribute; false otherwise.
281f4a2713aSLionel Sambuc static bool
HasExplicitOwnershipAttr(Sema & S,ParmVarDecl * Param)282f4a2713aSLionel Sambuc HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) {
283f4a2713aSLionel Sambuc   QualType T = Param->getType();
284f4a2713aSLionel Sambuc 
285f4a2713aSLionel Sambuc   if (const PointerType *PT = T->getAs<PointerType>()) {
286f4a2713aSLionel Sambuc     T = PT->getPointeeType();
287f4a2713aSLionel Sambuc   } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
288f4a2713aSLionel Sambuc     T = RT->getPointeeType();
289f4a2713aSLionel Sambuc   } else {
290f4a2713aSLionel Sambuc     return true;
291f4a2713aSLionel Sambuc   }
292f4a2713aSLionel Sambuc 
293f4a2713aSLionel Sambuc   // If we have a lifetime qualifier, but it's local, we must have
294f4a2713aSLionel Sambuc   // inferred it. So, it is implicit.
295f4a2713aSLionel Sambuc   return !T.getLocalQualifiers().hasObjCLifetime();
296f4a2713aSLionel Sambuc }
297f4a2713aSLionel Sambuc 
298f4a2713aSLionel Sambuc /// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
299f4a2713aSLionel Sambuc /// and user declared, in the method definition's AST.
ActOnStartOfObjCMethodDef(Scope * FnBodyScope,Decl * D)300f4a2713aSLionel Sambuc void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
301*0a6a1f1dSLionel Sambuc   assert((getCurMethodDecl() == nullptr) && "Methodparsing confused");
302f4a2713aSLionel Sambuc   ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
303f4a2713aSLionel Sambuc 
304f4a2713aSLionel Sambuc   // If we don't have a valid method decl, simply return.
305f4a2713aSLionel Sambuc   if (!MDecl)
306f4a2713aSLionel Sambuc     return;
307f4a2713aSLionel Sambuc 
308f4a2713aSLionel Sambuc   // Allow all of Sema to see that we are entering a method definition.
309f4a2713aSLionel Sambuc   PushDeclContext(FnBodyScope, MDecl);
310f4a2713aSLionel Sambuc   PushFunctionScope();
311f4a2713aSLionel Sambuc 
312f4a2713aSLionel Sambuc   // Create Decl objects for each parameter, entrring them in the scope for
313f4a2713aSLionel Sambuc   // binding to their use.
314f4a2713aSLionel Sambuc 
315f4a2713aSLionel Sambuc   // Insert the invisible arguments, self and _cmd!
316f4a2713aSLionel Sambuc   MDecl->createImplicitParams(Context, MDecl->getClassInterface());
317f4a2713aSLionel Sambuc 
318f4a2713aSLionel Sambuc   PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
319f4a2713aSLionel Sambuc   PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
320f4a2713aSLionel Sambuc 
321f4a2713aSLionel Sambuc   // The ObjC parser requires parameter names so there's no need to check.
322f4a2713aSLionel Sambuc   CheckParmsForFunctionDef(MDecl->param_begin(), MDecl->param_end(),
323f4a2713aSLionel Sambuc                            /*CheckParameterNames=*/false);
324f4a2713aSLionel Sambuc 
325f4a2713aSLionel Sambuc   // Introduce all of the other parameters into this scope.
326*0a6a1f1dSLionel Sambuc   for (auto *Param : MDecl->params()) {
327f4a2713aSLionel Sambuc     if (!Param->isInvalidDecl() &&
328f4a2713aSLionel Sambuc         getLangOpts().ObjCAutoRefCount &&
329f4a2713aSLionel Sambuc         !HasExplicitOwnershipAttr(*this, Param))
330f4a2713aSLionel Sambuc       Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) <<
331f4a2713aSLionel Sambuc             Param->getType();
332f4a2713aSLionel Sambuc 
333*0a6a1f1dSLionel Sambuc     if (Param->getIdentifier())
334*0a6a1f1dSLionel Sambuc       PushOnScopeChains(Param, FnBodyScope);
335f4a2713aSLionel Sambuc   }
336f4a2713aSLionel Sambuc 
337f4a2713aSLionel Sambuc   // In ARC, disallow definition of retain/release/autorelease/retainCount
338f4a2713aSLionel Sambuc   if (getLangOpts().ObjCAutoRefCount) {
339f4a2713aSLionel Sambuc     switch (MDecl->getMethodFamily()) {
340f4a2713aSLionel Sambuc     case OMF_retain:
341f4a2713aSLionel Sambuc     case OMF_retainCount:
342f4a2713aSLionel Sambuc     case OMF_release:
343f4a2713aSLionel Sambuc     case OMF_autorelease:
344f4a2713aSLionel Sambuc       Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
345f4a2713aSLionel Sambuc         << 0 << MDecl->getSelector();
346f4a2713aSLionel Sambuc       break;
347f4a2713aSLionel Sambuc 
348f4a2713aSLionel Sambuc     case OMF_None:
349f4a2713aSLionel Sambuc     case OMF_dealloc:
350f4a2713aSLionel Sambuc     case OMF_finalize:
351f4a2713aSLionel Sambuc     case OMF_alloc:
352f4a2713aSLionel Sambuc     case OMF_init:
353f4a2713aSLionel Sambuc     case OMF_mutableCopy:
354f4a2713aSLionel Sambuc     case OMF_copy:
355f4a2713aSLionel Sambuc     case OMF_new:
356f4a2713aSLionel Sambuc     case OMF_self:
357*0a6a1f1dSLionel Sambuc     case OMF_initialize:
358f4a2713aSLionel Sambuc     case OMF_performSelector:
359f4a2713aSLionel Sambuc       break;
360f4a2713aSLionel Sambuc     }
361f4a2713aSLionel Sambuc   }
362f4a2713aSLionel Sambuc 
363f4a2713aSLionel Sambuc   // Warn on deprecated methods under -Wdeprecated-implementations,
364f4a2713aSLionel Sambuc   // and prepare for warning on missing super calls.
365f4a2713aSLionel Sambuc   if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
366f4a2713aSLionel Sambuc     ObjCMethodDecl *IMD =
367f4a2713aSLionel Sambuc       IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod());
368f4a2713aSLionel Sambuc 
369f4a2713aSLionel Sambuc     if (IMD) {
370f4a2713aSLionel Sambuc       ObjCImplDecl *ImplDeclOfMethodDef =
371f4a2713aSLionel Sambuc         dyn_cast<ObjCImplDecl>(MDecl->getDeclContext());
372f4a2713aSLionel Sambuc       ObjCContainerDecl *ContDeclOfMethodDecl =
373f4a2713aSLionel Sambuc         dyn_cast<ObjCContainerDecl>(IMD->getDeclContext());
374*0a6a1f1dSLionel Sambuc       ObjCImplDecl *ImplDeclOfMethodDecl = nullptr;
375f4a2713aSLionel Sambuc       if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl))
376f4a2713aSLionel Sambuc         ImplDeclOfMethodDecl = OID->getImplementation();
377*0a6a1f1dSLionel Sambuc       else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl)) {
378*0a6a1f1dSLionel Sambuc         if (CD->IsClassExtension()) {
379*0a6a1f1dSLionel Sambuc           if (ObjCInterfaceDecl *OID = CD->getClassInterface())
380*0a6a1f1dSLionel Sambuc             ImplDeclOfMethodDecl = OID->getImplementation();
381*0a6a1f1dSLionel Sambuc         } else
382f4a2713aSLionel Sambuc             ImplDeclOfMethodDecl = CD->getImplementation();
383*0a6a1f1dSLionel Sambuc       }
384f4a2713aSLionel Sambuc       // No need to issue deprecated warning if deprecated mehod in class/category
385f4a2713aSLionel Sambuc       // is being implemented in its own implementation (no overriding is involved).
386f4a2713aSLionel Sambuc       if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef)
387f4a2713aSLionel Sambuc         DiagnoseObjCImplementedDeprecations(*this,
388f4a2713aSLionel Sambuc                                           dyn_cast<NamedDecl>(IMD),
389f4a2713aSLionel Sambuc                                           MDecl->getLocation(), 0);
390f4a2713aSLionel Sambuc     }
391f4a2713aSLionel Sambuc 
392*0a6a1f1dSLionel Sambuc     if (MDecl->getMethodFamily() == OMF_init) {
393*0a6a1f1dSLionel Sambuc       if (MDecl->isDesignatedInitializerForTheInterface()) {
394*0a6a1f1dSLionel Sambuc         getCurFunction()->ObjCIsDesignatedInit = true;
395*0a6a1f1dSLionel Sambuc         getCurFunction()->ObjCWarnForNoDesignatedInitChain =
396*0a6a1f1dSLionel Sambuc             IC->getSuperClass() != nullptr;
397*0a6a1f1dSLionel Sambuc       } else if (IC->hasDesignatedInitializers()) {
398*0a6a1f1dSLionel Sambuc         getCurFunction()->ObjCIsSecondaryInit = true;
399*0a6a1f1dSLionel Sambuc         getCurFunction()->ObjCWarnForNoInitDelegation = true;
400*0a6a1f1dSLionel Sambuc       }
401*0a6a1f1dSLionel Sambuc     }
402*0a6a1f1dSLionel Sambuc 
403f4a2713aSLionel Sambuc     // If this is "dealloc" or "finalize", set some bit here.
404f4a2713aSLionel Sambuc     // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
405f4a2713aSLionel Sambuc     // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
406f4a2713aSLionel Sambuc     // Only do this if the current class actually has a superclass.
407f4a2713aSLionel Sambuc     if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) {
408f4a2713aSLionel Sambuc       ObjCMethodFamily Family = MDecl->getMethodFamily();
409f4a2713aSLionel Sambuc       if (Family == OMF_dealloc) {
410f4a2713aSLionel Sambuc         if (!(getLangOpts().ObjCAutoRefCount ||
411f4a2713aSLionel Sambuc               getLangOpts().getGC() == LangOptions::GCOnly))
412f4a2713aSLionel Sambuc           getCurFunction()->ObjCShouldCallSuper = true;
413f4a2713aSLionel Sambuc 
414f4a2713aSLionel Sambuc       } else if (Family == OMF_finalize) {
415f4a2713aSLionel Sambuc         if (Context.getLangOpts().getGC() != LangOptions::NonGC)
416f4a2713aSLionel Sambuc           getCurFunction()->ObjCShouldCallSuper = true;
417f4a2713aSLionel Sambuc 
418f4a2713aSLionel Sambuc       } else {
419f4a2713aSLionel Sambuc         const ObjCMethodDecl *SuperMethod =
420f4a2713aSLionel Sambuc           SuperClass->lookupMethod(MDecl->getSelector(),
421f4a2713aSLionel Sambuc                                    MDecl->isInstanceMethod());
422f4a2713aSLionel Sambuc         getCurFunction()->ObjCShouldCallSuper =
423f4a2713aSLionel Sambuc           (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>());
424f4a2713aSLionel Sambuc       }
425f4a2713aSLionel Sambuc     }
426f4a2713aSLionel Sambuc   }
427f4a2713aSLionel Sambuc }
428f4a2713aSLionel Sambuc 
429f4a2713aSLionel Sambuc namespace {
430f4a2713aSLionel Sambuc 
431f4a2713aSLionel Sambuc // Callback to only accept typo corrections that are Objective-C classes.
432f4a2713aSLionel Sambuc // If an ObjCInterfaceDecl* is given to the constructor, then the validation
433f4a2713aSLionel Sambuc // function will reject corrections to that class.
434f4a2713aSLionel Sambuc class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
435f4a2713aSLionel Sambuc  public:
ObjCInterfaceValidatorCCC()436*0a6a1f1dSLionel Sambuc   ObjCInterfaceValidatorCCC() : CurrentIDecl(nullptr) {}
ObjCInterfaceValidatorCCC(ObjCInterfaceDecl * IDecl)437f4a2713aSLionel Sambuc   explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
438f4a2713aSLionel Sambuc       : CurrentIDecl(IDecl) {}
439f4a2713aSLionel Sambuc 
ValidateCandidate(const TypoCorrection & candidate)440*0a6a1f1dSLionel Sambuc   bool ValidateCandidate(const TypoCorrection &candidate) override {
441f4a2713aSLionel Sambuc     ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
442f4a2713aSLionel Sambuc     return ID && !declaresSameEntity(ID, CurrentIDecl);
443f4a2713aSLionel Sambuc   }
444f4a2713aSLionel Sambuc 
445f4a2713aSLionel Sambuc  private:
446f4a2713aSLionel Sambuc   ObjCInterfaceDecl *CurrentIDecl;
447f4a2713aSLionel Sambuc };
448f4a2713aSLionel Sambuc 
449f4a2713aSLionel Sambuc }
450f4a2713aSLionel Sambuc 
451f4a2713aSLionel Sambuc Decl *Sema::
ActOnStartClassInterface(SourceLocation AtInterfaceLoc,IdentifierInfo * ClassName,SourceLocation ClassLoc,IdentifierInfo * SuperName,SourceLocation SuperLoc,Decl * const * ProtoRefs,unsigned NumProtoRefs,const SourceLocation * ProtoLocs,SourceLocation EndProtoLoc,AttributeList * AttrList)452f4a2713aSLionel Sambuc ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
453f4a2713aSLionel Sambuc                          IdentifierInfo *ClassName, SourceLocation ClassLoc,
454f4a2713aSLionel Sambuc                          IdentifierInfo *SuperName, SourceLocation SuperLoc,
455f4a2713aSLionel Sambuc                          Decl * const *ProtoRefs, unsigned NumProtoRefs,
456f4a2713aSLionel Sambuc                          const SourceLocation *ProtoLocs,
457f4a2713aSLionel Sambuc                          SourceLocation EndProtoLoc, AttributeList *AttrList) {
458f4a2713aSLionel Sambuc   assert(ClassName && "Missing class identifier");
459f4a2713aSLionel Sambuc 
460f4a2713aSLionel Sambuc   // Check for another declaration kind with the same name.
461f4a2713aSLionel Sambuc   NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
462f4a2713aSLionel Sambuc                                          LookupOrdinaryName, ForRedeclaration);
463f4a2713aSLionel Sambuc 
464f4a2713aSLionel Sambuc   if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
465f4a2713aSLionel Sambuc     Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
466f4a2713aSLionel Sambuc     Diag(PrevDecl->getLocation(), diag::note_previous_definition);
467f4a2713aSLionel Sambuc   }
468f4a2713aSLionel Sambuc 
469f4a2713aSLionel Sambuc   // Create a declaration to describe this @interface.
470f4a2713aSLionel Sambuc   ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
471f4a2713aSLionel Sambuc 
472f4a2713aSLionel Sambuc   if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
473f4a2713aSLionel Sambuc     // A previous decl with a different name is because of
474f4a2713aSLionel Sambuc     // @compatibility_alias, for example:
475f4a2713aSLionel Sambuc     // \code
476f4a2713aSLionel Sambuc     //   @class NewImage;
477f4a2713aSLionel Sambuc     //   @compatibility_alias OldImage NewImage;
478f4a2713aSLionel Sambuc     // \endcode
479f4a2713aSLionel Sambuc     // A lookup for 'OldImage' will return the 'NewImage' decl.
480f4a2713aSLionel Sambuc     //
481f4a2713aSLionel Sambuc     // In such a case use the real declaration name, instead of the alias one,
482f4a2713aSLionel Sambuc     // otherwise we will break IdentifierResolver and redecls-chain invariants.
483f4a2713aSLionel Sambuc     // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
484f4a2713aSLionel Sambuc     // has been aliased.
485f4a2713aSLionel Sambuc     ClassName = PrevIDecl->getIdentifier();
486f4a2713aSLionel Sambuc   }
487f4a2713aSLionel Sambuc 
488f4a2713aSLionel Sambuc   ObjCInterfaceDecl *IDecl
489f4a2713aSLionel Sambuc     = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
490f4a2713aSLionel Sambuc                                 PrevIDecl, ClassLoc);
491f4a2713aSLionel Sambuc 
492f4a2713aSLionel Sambuc   if (PrevIDecl) {
493f4a2713aSLionel Sambuc     // Class already seen. Was it a definition?
494f4a2713aSLionel Sambuc     if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
495f4a2713aSLionel Sambuc       Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
496f4a2713aSLionel Sambuc         << PrevIDecl->getDeclName();
497f4a2713aSLionel Sambuc       Diag(Def->getLocation(), diag::note_previous_definition);
498f4a2713aSLionel Sambuc       IDecl->setInvalidDecl();
499f4a2713aSLionel Sambuc     }
500f4a2713aSLionel Sambuc   }
501f4a2713aSLionel Sambuc 
502f4a2713aSLionel Sambuc   if (AttrList)
503f4a2713aSLionel Sambuc     ProcessDeclAttributeList(TUScope, IDecl, AttrList);
504f4a2713aSLionel Sambuc   PushOnScopeChains(IDecl, TUScope);
505f4a2713aSLionel Sambuc 
506f4a2713aSLionel Sambuc   // Start the definition of this class. If we're in a redefinition case, there
507f4a2713aSLionel Sambuc   // may already be a definition, so we'll end up adding to it.
508f4a2713aSLionel Sambuc   if (!IDecl->hasDefinition())
509f4a2713aSLionel Sambuc     IDecl->startDefinition();
510f4a2713aSLionel Sambuc 
511f4a2713aSLionel Sambuc   if (SuperName) {
512f4a2713aSLionel Sambuc     // Check if a different kind of symbol declared in this scope.
513f4a2713aSLionel Sambuc     PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
514f4a2713aSLionel Sambuc                                 LookupOrdinaryName);
515f4a2713aSLionel Sambuc 
516f4a2713aSLionel Sambuc     if (!PrevDecl) {
517f4a2713aSLionel Sambuc       // Try to correct for a typo in the superclass name without correcting
518f4a2713aSLionel Sambuc       // to the class we're defining.
519*0a6a1f1dSLionel Sambuc       if (TypoCorrection Corrected =
520*0a6a1f1dSLionel Sambuc               CorrectTypo(DeclarationNameInfo(SuperName, SuperLoc),
521*0a6a1f1dSLionel Sambuc                           LookupOrdinaryName, TUScope, nullptr,
522*0a6a1f1dSLionel Sambuc                           llvm::make_unique<ObjCInterfaceValidatorCCC>(IDecl),
523*0a6a1f1dSLionel Sambuc                           CTK_ErrorRecovery)) {
524f4a2713aSLionel Sambuc         diagnoseTypo(Corrected, PDiag(diag::err_undef_superclass_suggest)
525f4a2713aSLionel Sambuc                                     << SuperName << ClassName);
526f4a2713aSLionel Sambuc         PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
527f4a2713aSLionel Sambuc       }
528f4a2713aSLionel Sambuc     }
529f4a2713aSLionel Sambuc 
530f4a2713aSLionel Sambuc     if (declaresSameEntity(PrevDecl, IDecl)) {
531f4a2713aSLionel Sambuc       Diag(SuperLoc, diag::err_recursive_superclass)
532f4a2713aSLionel Sambuc         << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
533f4a2713aSLionel Sambuc       IDecl->setEndOfDefinitionLoc(ClassLoc);
534f4a2713aSLionel Sambuc     } else {
535f4a2713aSLionel Sambuc       ObjCInterfaceDecl *SuperClassDecl =
536f4a2713aSLionel Sambuc                                 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
537f4a2713aSLionel Sambuc 
538f4a2713aSLionel Sambuc       // Diagnose classes that inherit from deprecated classes.
539f4a2713aSLionel Sambuc       if (SuperClassDecl)
540f4a2713aSLionel Sambuc         (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
541f4a2713aSLionel Sambuc 
542*0a6a1f1dSLionel Sambuc       if (PrevDecl && !SuperClassDecl) {
543f4a2713aSLionel Sambuc         // The previous declaration was not a class decl. Check if we have a
544f4a2713aSLionel Sambuc         // typedef. If we do, get the underlying class type.
545f4a2713aSLionel Sambuc         if (const TypedefNameDecl *TDecl =
546f4a2713aSLionel Sambuc               dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
547f4a2713aSLionel Sambuc           QualType T = TDecl->getUnderlyingType();
548f4a2713aSLionel Sambuc           if (T->isObjCObjectType()) {
549f4a2713aSLionel Sambuc             if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
550f4a2713aSLionel Sambuc               SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
551f4a2713aSLionel Sambuc               // This handles the following case:
552f4a2713aSLionel Sambuc               // @interface NewI @end
553f4a2713aSLionel Sambuc               // typedef NewI DeprI __attribute__((deprecated("blah")))
554f4a2713aSLionel Sambuc               // @interface SI : DeprI /* warn here */ @end
555f4a2713aSLionel Sambuc               (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc);
556f4a2713aSLionel Sambuc             }
557f4a2713aSLionel Sambuc           }
558f4a2713aSLionel Sambuc         }
559f4a2713aSLionel Sambuc 
560f4a2713aSLionel Sambuc         // This handles the following case:
561f4a2713aSLionel Sambuc         //
562f4a2713aSLionel Sambuc         // typedef int SuperClass;
563f4a2713aSLionel Sambuc         // @interface MyClass : SuperClass {} @end
564f4a2713aSLionel Sambuc         //
565f4a2713aSLionel Sambuc         if (!SuperClassDecl) {
566f4a2713aSLionel Sambuc           Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
567f4a2713aSLionel Sambuc           Diag(PrevDecl->getLocation(), diag::note_previous_definition);
568f4a2713aSLionel Sambuc         }
569f4a2713aSLionel Sambuc       }
570f4a2713aSLionel Sambuc 
571f4a2713aSLionel Sambuc       if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
572f4a2713aSLionel Sambuc         if (!SuperClassDecl)
573f4a2713aSLionel Sambuc           Diag(SuperLoc, diag::err_undef_superclass)
574f4a2713aSLionel Sambuc             << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
575f4a2713aSLionel Sambuc         else if (RequireCompleteType(SuperLoc,
576f4a2713aSLionel Sambuc                                   Context.getObjCInterfaceType(SuperClassDecl),
577f4a2713aSLionel Sambuc                                      diag::err_forward_superclass,
578f4a2713aSLionel Sambuc                                      SuperClassDecl->getDeclName(),
579f4a2713aSLionel Sambuc                                      ClassName,
580f4a2713aSLionel Sambuc                                      SourceRange(AtInterfaceLoc, ClassLoc))) {
581*0a6a1f1dSLionel Sambuc           SuperClassDecl = nullptr;
582f4a2713aSLionel Sambuc         }
583f4a2713aSLionel Sambuc       }
584f4a2713aSLionel Sambuc       IDecl->setSuperClass(SuperClassDecl);
585f4a2713aSLionel Sambuc       IDecl->setSuperClassLoc(SuperLoc);
586f4a2713aSLionel Sambuc       IDecl->setEndOfDefinitionLoc(SuperLoc);
587f4a2713aSLionel Sambuc     }
588f4a2713aSLionel Sambuc   } else { // we have a root class.
589f4a2713aSLionel Sambuc     IDecl->setEndOfDefinitionLoc(ClassLoc);
590f4a2713aSLionel Sambuc   }
591f4a2713aSLionel Sambuc 
592f4a2713aSLionel Sambuc   // Check then save referenced protocols.
593f4a2713aSLionel Sambuc   if (NumProtoRefs) {
594f4a2713aSLionel Sambuc     IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
595f4a2713aSLionel Sambuc                            ProtoLocs, Context);
596f4a2713aSLionel Sambuc     IDecl->setEndOfDefinitionLoc(EndProtoLoc);
597f4a2713aSLionel Sambuc   }
598f4a2713aSLionel Sambuc 
599f4a2713aSLionel Sambuc   CheckObjCDeclScope(IDecl);
600f4a2713aSLionel Sambuc   return ActOnObjCContainerStartDefinition(IDecl);
601f4a2713aSLionel Sambuc }
602f4a2713aSLionel Sambuc 
603f4a2713aSLionel Sambuc /// ActOnTypedefedProtocols - this action finds protocol list as part of the
604f4a2713aSLionel Sambuc /// typedef'ed use for a qualified super class and adds them to the list
605f4a2713aSLionel Sambuc /// of the protocols.
ActOnTypedefedProtocols(SmallVectorImpl<Decl * > & ProtocolRefs,IdentifierInfo * SuperName,SourceLocation SuperLoc)606f4a2713aSLionel Sambuc void Sema::ActOnTypedefedProtocols(SmallVectorImpl<Decl *> &ProtocolRefs,
607f4a2713aSLionel Sambuc                                    IdentifierInfo *SuperName,
608f4a2713aSLionel Sambuc                                    SourceLocation SuperLoc) {
609f4a2713aSLionel Sambuc   if (!SuperName)
610f4a2713aSLionel Sambuc     return;
611f4a2713aSLionel Sambuc   NamedDecl* IDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
612f4a2713aSLionel Sambuc                                       LookupOrdinaryName);
613f4a2713aSLionel Sambuc   if (!IDecl)
614f4a2713aSLionel Sambuc     return;
615f4a2713aSLionel Sambuc 
616f4a2713aSLionel Sambuc   if (const TypedefNameDecl *TDecl = dyn_cast_or_null<TypedefNameDecl>(IDecl)) {
617f4a2713aSLionel Sambuc     QualType T = TDecl->getUnderlyingType();
618f4a2713aSLionel Sambuc     if (T->isObjCObjectType())
619f4a2713aSLionel Sambuc       if (const ObjCObjectType *OPT = T->getAs<ObjCObjectType>())
620*0a6a1f1dSLionel Sambuc         for (auto *I : OPT->quals())
621*0a6a1f1dSLionel Sambuc           ProtocolRefs.push_back(I);
622f4a2713aSLionel Sambuc   }
623f4a2713aSLionel Sambuc }
624f4a2713aSLionel Sambuc 
625f4a2713aSLionel Sambuc /// ActOnCompatibilityAlias - this action is called after complete parsing of
626f4a2713aSLionel Sambuc /// a \@compatibility_alias declaration. It sets up the alias relationships.
ActOnCompatibilityAlias(SourceLocation AtLoc,IdentifierInfo * AliasName,SourceLocation AliasLocation,IdentifierInfo * ClassName,SourceLocation ClassLocation)627f4a2713aSLionel Sambuc Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
628f4a2713aSLionel Sambuc                                     IdentifierInfo *AliasName,
629f4a2713aSLionel Sambuc                                     SourceLocation AliasLocation,
630f4a2713aSLionel Sambuc                                     IdentifierInfo *ClassName,
631f4a2713aSLionel Sambuc                                     SourceLocation ClassLocation) {
632f4a2713aSLionel Sambuc   // Look for previous declaration of alias name
633f4a2713aSLionel Sambuc   NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
634f4a2713aSLionel Sambuc                                       LookupOrdinaryName, ForRedeclaration);
635f4a2713aSLionel Sambuc   if (ADecl) {
636f4a2713aSLionel Sambuc     Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
637f4a2713aSLionel Sambuc     Diag(ADecl->getLocation(), diag::note_previous_declaration);
638*0a6a1f1dSLionel Sambuc     return nullptr;
639f4a2713aSLionel Sambuc   }
640f4a2713aSLionel Sambuc   // Check for class declaration
641f4a2713aSLionel Sambuc   NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
642f4a2713aSLionel Sambuc                                        LookupOrdinaryName, ForRedeclaration);
643f4a2713aSLionel Sambuc   if (const TypedefNameDecl *TDecl =
644f4a2713aSLionel Sambuc         dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
645f4a2713aSLionel Sambuc     QualType T = TDecl->getUnderlyingType();
646f4a2713aSLionel Sambuc     if (T->isObjCObjectType()) {
647f4a2713aSLionel Sambuc       if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
648f4a2713aSLionel Sambuc         ClassName = IDecl->getIdentifier();
649f4a2713aSLionel Sambuc         CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
650f4a2713aSLionel Sambuc                                   LookupOrdinaryName, ForRedeclaration);
651f4a2713aSLionel Sambuc       }
652f4a2713aSLionel Sambuc     }
653f4a2713aSLionel Sambuc   }
654f4a2713aSLionel Sambuc   ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
655*0a6a1f1dSLionel Sambuc   if (!CDecl) {
656f4a2713aSLionel Sambuc     Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
657f4a2713aSLionel Sambuc     if (CDeclU)
658f4a2713aSLionel Sambuc       Diag(CDeclU->getLocation(), diag::note_previous_declaration);
659*0a6a1f1dSLionel Sambuc     return nullptr;
660f4a2713aSLionel Sambuc   }
661f4a2713aSLionel Sambuc 
662f4a2713aSLionel Sambuc   // Everything checked out, instantiate a new alias declaration AST.
663f4a2713aSLionel Sambuc   ObjCCompatibleAliasDecl *AliasDecl =
664f4a2713aSLionel Sambuc     ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
665f4a2713aSLionel Sambuc 
666f4a2713aSLionel Sambuc   if (!CheckObjCDeclScope(AliasDecl))
667f4a2713aSLionel Sambuc     PushOnScopeChains(AliasDecl, TUScope);
668f4a2713aSLionel Sambuc 
669f4a2713aSLionel Sambuc   return AliasDecl;
670f4a2713aSLionel Sambuc }
671f4a2713aSLionel Sambuc 
CheckForwardProtocolDeclarationForCircularDependency(IdentifierInfo * PName,SourceLocation & Ploc,SourceLocation PrevLoc,const ObjCList<ObjCProtocolDecl> & PList)672f4a2713aSLionel Sambuc bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
673f4a2713aSLionel Sambuc   IdentifierInfo *PName,
674f4a2713aSLionel Sambuc   SourceLocation &Ploc, SourceLocation PrevLoc,
675f4a2713aSLionel Sambuc   const ObjCList<ObjCProtocolDecl> &PList) {
676f4a2713aSLionel Sambuc 
677f4a2713aSLionel Sambuc   bool res = false;
678f4a2713aSLionel Sambuc   for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
679f4a2713aSLionel Sambuc        E = PList.end(); I != E; ++I) {
680f4a2713aSLionel Sambuc     if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
681f4a2713aSLionel Sambuc                                                  Ploc)) {
682f4a2713aSLionel Sambuc       if (PDecl->getIdentifier() == PName) {
683f4a2713aSLionel Sambuc         Diag(Ploc, diag::err_protocol_has_circular_dependency);
684f4a2713aSLionel Sambuc         Diag(PrevLoc, diag::note_previous_definition);
685f4a2713aSLionel Sambuc         res = true;
686f4a2713aSLionel Sambuc       }
687f4a2713aSLionel Sambuc 
688f4a2713aSLionel Sambuc       if (!PDecl->hasDefinition())
689f4a2713aSLionel Sambuc         continue;
690f4a2713aSLionel Sambuc 
691f4a2713aSLionel Sambuc       if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
692f4a2713aSLionel Sambuc             PDecl->getLocation(), PDecl->getReferencedProtocols()))
693f4a2713aSLionel Sambuc         res = true;
694f4a2713aSLionel Sambuc     }
695f4a2713aSLionel Sambuc   }
696f4a2713aSLionel Sambuc   return res;
697f4a2713aSLionel Sambuc }
698f4a2713aSLionel Sambuc 
699f4a2713aSLionel Sambuc Decl *
ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,IdentifierInfo * ProtocolName,SourceLocation ProtocolLoc,Decl * const * ProtoRefs,unsigned NumProtoRefs,const SourceLocation * ProtoLocs,SourceLocation EndProtoLoc,AttributeList * AttrList)700f4a2713aSLionel Sambuc Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
701f4a2713aSLionel Sambuc                                   IdentifierInfo *ProtocolName,
702f4a2713aSLionel Sambuc                                   SourceLocation ProtocolLoc,
703f4a2713aSLionel Sambuc                                   Decl * const *ProtoRefs,
704f4a2713aSLionel Sambuc                                   unsigned NumProtoRefs,
705f4a2713aSLionel Sambuc                                   const SourceLocation *ProtoLocs,
706f4a2713aSLionel Sambuc                                   SourceLocation EndProtoLoc,
707f4a2713aSLionel Sambuc                                   AttributeList *AttrList) {
708f4a2713aSLionel Sambuc   bool err = false;
709f4a2713aSLionel Sambuc   // FIXME: Deal with AttrList.
710f4a2713aSLionel Sambuc   assert(ProtocolName && "Missing protocol identifier");
711f4a2713aSLionel Sambuc   ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
712f4a2713aSLionel Sambuc                                               ForRedeclaration);
713*0a6a1f1dSLionel Sambuc   ObjCProtocolDecl *PDecl = nullptr;
714*0a6a1f1dSLionel Sambuc   if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : nullptr) {
715f4a2713aSLionel Sambuc     // If we already have a definition, complain.
716f4a2713aSLionel Sambuc     Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
717f4a2713aSLionel Sambuc     Diag(Def->getLocation(), diag::note_previous_definition);
718f4a2713aSLionel Sambuc 
719f4a2713aSLionel Sambuc     // Create a new protocol that is completely distinct from previous
720f4a2713aSLionel Sambuc     // declarations, and do not make this protocol available for name lookup.
721f4a2713aSLionel Sambuc     // That way, we'll end up completely ignoring the duplicate.
722f4a2713aSLionel Sambuc     // FIXME: Can we turn this into an error?
723f4a2713aSLionel Sambuc     PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
724f4a2713aSLionel Sambuc                                      ProtocolLoc, AtProtoInterfaceLoc,
725*0a6a1f1dSLionel Sambuc                                      /*PrevDecl=*/nullptr);
726f4a2713aSLionel Sambuc     PDecl->startDefinition();
727f4a2713aSLionel Sambuc   } else {
728f4a2713aSLionel Sambuc     if (PrevDecl) {
729f4a2713aSLionel Sambuc       // Check for circular dependencies among protocol declarations. This can
730f4a2713aSLionel Sambuc       // only happen if this protocol was forward-declared.
731f4a2713aSLionel Sambuc       ObjCList<ObjCProtocolDecl> PList;
732f4a2713aSLionel Sambuc       PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
733f4a2713aSLionel Sambuc       err = CheckForwardProtocolDeclarationForCircularDependency(
734f4a2713aSLionel Sambuc               ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
735f4a2713aSLionel Sambuc     }
736f4a2713aSLionel Sambuc 
737f4a2713aSLionel Sambuc     // Create the new declaration.
738f4a2713aSLionel Sambuc     PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
739f4a2713aSLionel Sambuc                                      ProtocolLoc, AtProtoInterfaceLoc,
740f4a2713aSLionel Sambuc                                      /*PrevDecl=*/PrevDecl);
741f4a2713aSLionel Sambuc 
742f4a2713aSLionel Sambuc     PushOnScopeChains(PDecl, TUScope);
743f4a2713aSLionel Sambuc     PDecl->startDefinition();
744f4a2713aSLionel Sambuc   }
745f4a2713aSLionel Sambuc 
746f4a2713aSLionel Sambuc   if (AttrList)
747f4a2713aSLionel Sambuc     ProcessDeclAttributeList(TUScope, PDecl, AttrList);
748f4a2713aSLionel Sambuc 
749f4a2713aSLionel Sambuc   // Merge attributes from previous declarations.
750f4a2713aSLionel Sambuc   if (PrevDecl)
751f4a2713aSLionel Sambuc     mergeDeclAttributes(PDecl, PrevDecl);
752f4a2713aSLionel Sambuc 
753f4a2713aSLionel Sambuc   if (!err && NumProtoRefs ) {
754f4a2713aSLionel Sambuc     /// Check then save referenced protocols.
755f4a2713aSLionel Sambuc     PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
756f4a2713aSLionel Sambuc                            ProtoLocs, Context);
757f4a2713aSLionel Sambuc   }
758f4a2713aSLionel Sambuc 
759f4a2713aSLionel Sambuc   CheckObjCDeclScope(PDecl);
760f4a2713aSLionel Sambuc   return ActOnObjCContainerStartDefinition(PDecl);
761f4a2713aSLionel Sambuc }
762f4a2713aSLionel Sambuc 
NestedProtocolHasNoDefinition(ObjCProtocolDecl * PDecl,ObjCProtocolDecl * & UndefinedProtocol)763*0a6a1f1dSLionel Sambuc static bool NestedProtocolHasNoDefinition(ObjCProtocolDecl *PDecl,
764*0a6a1f1dSLionel Sambuc                                           ObjCProtocolDecl *&UndefinedProtocol) {
765*0a6a1f1dSLionel Sambuc   if (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()) {
766*0a6a1f1dSLionel Sambuc     UndefinedProtocol = PDecl;
767*0a6a1f1dSLionel Sambuc     return true;
768*0a6a1f1dSLionel Sambuc   }
769*0a6a1f1dSLionel Sambuc 
770*0a6a1f1dSLionel Sambuc   for (auto *PI : PDecl->protocols())
771*0a6a1f1dSLionel Sambuc     if (NestedProtocolHasNoDefinition(PI, UndefinedProtocol)) {
772*0a6a1f1dSLionel Sambuc       UndefinedProtocol = PI;
773*0a6a1f1dSLionel Sambuc       return true;
774*0a6a1f1dSLionel Sambuc     }
775*0a6a1f1dSLionel Sambuc   return false;
776*0a6a1f1dSLionel Sambuc }
777*0a6a1f1dSLionel Sambuc 
778f4a2713aSLionel Sambuc /// FindProtocolDeclaration - This routine looks up protocols and
779f4a2713aSLionel Sambuc /// issues an error if they are not declared. It returns list of
780f4a2713aSLionel Sambuc /// protocol declarations in its 'Protocols' argument.
781f4a2713aSLionel Sambuc void
FindProtocolDeclaration(bool WarnOnDeclarations,const IdentifierLocPair * ProtocolId,unsigned NumProtocols,SmallVectorImpl<Decl * > & Protocols)782f4a2713aSLionel Sambuc Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
783f4a2713aSLionel Sambuc                               const IdentifierLocPair *ProtocolId,
784f4a2713aSLionel Sambuc                               unsigned NumProtocols,
785f4a2713aSLionel Sambuc                               SmallVectorImpl<Decl *> &Protocols) {
786f4a2713aSLionel Sambuc   for (unsigned i = 0; i != NumProtocols; ++i) {
787f4a2713aSLionel Sambuc     ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
788f4a2713aSLionel Sambuc                                              ProtocolId[i].second);
789f4a2713aSLionel Sambuc     if (!PDecl) {
790f4a2713aSLionel Sambuc       TypoCorrection Corrected = CorrectTypo(
791f4a2713aSLionel Sambuc           DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
792*0a6a1f1dSLionel Sambuc           LookupObjCProtocolName, TUScope, nullptr,
793*0a6a1f1dSLionel Sambuc           llvm::make_unique<DeclFilterCCC<ObjCProtocolDecl>>(),
794*0a6a1f1dSLionel Sambuc           CTK_ErrorRecovery);
795f4a2713aSLionel Sambuc       if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>()))
796f4a2713aSLionel Sambuc         diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest)
797f4a2713aSLionel Sambuc                                     << ProtocolId[i].first);
798f4a2713aSLionel Sambuc     }
799f4a2713aSLionel Sambuc 
800f4a2713aSLionel Sambuc     if (!PDecl) {
801f4a2713aSLionel Sambuc       Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
802f4a2713aSLionel Sambuc         << ProtocolId[i].first;
803f4a2713aSLionel Sambuc       continue;
804f4a2713aSLionel Sambuc     }
805f4a2713aSLionel Sambuc     // If this is a forward protocol declaration, get its definition.
806f4a2713aSLionel Sambuc     if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition())
807f4a2713aSLionel Sambuc       PDecl = PDecl->getDefinition();
808f4a2713aSLionel Sambuc 
809f4a2713aSLionel Sambuc     (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
810f4a2713aSLionel Sambuc 
811f4a2713aSLionel Sambuc     // If this is a forward declaration and we are supposed to warn in this
812f4a2713aSLionel Sambuc     // case, do it.
813f4a2713aSLionel Sambuc     // FIXME: Recover nicely in the hidden case.
814*0a6a1f1dSLionel Sambuc     ObjCProtocolDecl *UndefinedProtocol;
815*0a6a1f1dSLionel Sambuc 
816f4a2713aSLionel Sambuc     if (WarnOnDeclarations &&
817*0a6a1f1dSLionel Sambuc         NestedProtocolHasNoDefinition(PDecl, UndefinedProtocol)) {
818f4a2713aSLionel Sambuc       Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
819f4a2713aSLionel Sambuc         << ProtocolId[i].first;
820*0a6a1f1dSLionel Sambuc       Diag(UndefinedProtocol->getLocation(), diag::note_protocol_decl_undefined)
821*0a6a1f1dSLionel Sambuc         << UndefinedProtocol;
822*0a6a1f1dSLionel Sambuc     }
823f4a2713aSLionel Sambuc     Protocols.push_back(PDecl);
824f4a2713aSLionel Sambuc   }
825f4a2713aSLionel Sambuc }
826f4a2713aSLionel Sambuc 
827f4a2713aSLionel Sambuc /// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
828f4a2713aSLionel Sambuc /// a class method in its extension.
829f4a2713aSLionel Sambuc ///
DiagnoseClassExtensionDupMethods(ObjCCategoryDecl * CAT,ObjCInterfaceDecl * ID)830f4a2713aSLionel Sambuc void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
831f4a2713aSLionel Sambuc                                             ObjCInterfaceDecl *ID) {
832f4a2713aSLionel Sambuc   if (!ID)
833f4a2713aSLionel Sambuc     return;  // Possibly due to previous error
834f4a2713aSLionel Sambuc 
835f4a2713aSLionel Sambuc   llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
836*0a6a1f1dSLionel Sambuc   for (auto *MD : ID->methods())
837f4a2713aSLionel Sambuc     MethodMap[MD->getSelector()] = MD;
838f4a2713aSLionel Sambuc 
839f4a2713aSLionel Sambuc   if (MethodMap.empty())
840f4a2713aSLionel Sambuc     return;
841*0a6a1f1dSLionel Sambuc   for (const auto *Method : CAT->methods()) {
842f4a2713aSLionel Sambuc     const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
843*0a6a1f1dSLionel Sambuc     if (PrevMethod &&
844*0a6a1f1dSLionel Sambuc         (PrevMethod->isInstanceMethod() == Method->isInstanceMethod()) &&
845*0a6a1f1dSLionel Sambuc         !MatchTwoMethodDeclarations(Method, PrevMethod)) {
846f4a2713aSLionel Sambuc       Diag(Method->getLocation(), diag::err_duplicate_method_decl)
847f4a2713aSLionel Sambuc             << Method->getDeclName();
848f4a2713aSLionel Sambuc       Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
849f4a2713aSLionel Sambuc     }
850f4a2713aSLionel Sambuc   }
851f4a2713aSLionel Sambuc }
852f4a2713aSLionel Sambuc 
853f4a2713aSLionel Sambuc /// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
854f4a2713aSLionel Sambuc Sema::DeclGroupPtrTy
ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,const IdentifierLocPair * IdentList,unsigned NumElts,AttributeList * attrList)855f4a2713aSLionel Sambuc Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
856f4a2713aSLionel Sambuc                                       const IdentifierLocPair *IdentList,
857f4a2713aSLionel Sambuc                                       unsigned NumElts,
858f4a2713aSLionel Sambuc                                       AttributeList *attrList) {
859f4a2713aSLionel Sambuc   SmallVector<Decl *, 8> DeclsInGroup;
860f4a2713aSLionel Sambuc   for (unsigned i = 0; i != NumElts; ++i) {
861f4a2713aSLionel Sambuc     IdentifierInfo *Ident = IdentList[i].first;
862f4a2713aSLionel Sambuc     ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
863f4a2713aSLionel Sambuc                                                 ForRedeclaration);
864f4a2713aSLionel Sambuc     ObjCProtocolDecl *PDecl
865f4a2713aSLionel Sambuc       = ObjCProtocolDecl::Create(Context, CurContext, Ident,
866f4a2713aSLionel Sambuc                                  IdentList[i].second, AtProtocolLoc,
867f4a2713aSLionel Sambuc                                  PrevDecl);
868f4a2713aSLionel Sambuc 
869f4a2713aSLionel Sambuc     PushOnScopeChains(PDecl, TUScope);
870f4a2713aSLionel Sambuc     CheckObjCDeclScope(PDecl);
871f4a2713aSLionel Sambuc 
872f4a2713aSLionel Sambuc     if (attrList)
873f4a2713aSLionel Sambuc       ProcessDeclAttributeList(TUScope, PDecl, attrList);
874f4a2713aSLionel Sambuc 
875f4a2713aSLionel Sambuc     if (PrevDecl)
876f4a2713aSLionel Sambuc       mergeDeclAttributes(PDecl, PrevDecl);
877f4a2713aSLionel Sambuc 
878f4a2713aSLionel Sambuc     DeclsInGroup.push_back(PDecl);
879f4a2713aSLionel Sambuc   }
880f4a2713aSLionel Sambuc 
881f4a2713aSLionel Sambuc   return BuildDeclaratorGroup(DeclsInGroup, false);
882f4a2713aSLionel Sambuc }
883f4a2713aSLionel Sambuc 
884f4a2713aSLionel Sambuc Decl *Sema::
ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,IdentifierInfo * ClassName,SourceLocation ClassLoc,IdentifierInfo * CategoryName,SourceLocation CategoryLoc,Decl * const * ProtoRefs,unsigned NumProtoRefs,const SourceLocation * ProtoLocs,SourceLocation EndProtoLoc)885f4a2713aSLionel Sambuc ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
886f4a2713aSLionel Sambuc                             IdentifierInfo *ClassName, SourceLocation ClassLoc,
887f4a2713aSLionel Sambuc                             IdentifierInfo *CategoryName,
888f4a2713aSLionel Sambuc                             SourceLocation CategoryLoc,
889f4a2713aSLionel Sambuc                             Decl * const *ProtoRefs,
890f4a2713aSLionel Sambuc                             unsigned NumProtoRefs,
891f4a2713aSLionel Sambuc                             const SourceLocation *ProtoLocs,
892f4a2713aSLionel Sambuc                             SourceLocation EndProtoLoc) {
893f4a2713aSLionel Sambuc   ObjCCategoryDecl *CDecl;
894f4a2713aSLionel Sambuc   ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
895f4a2713aSLionel Sambuc 
896f4a2713aSLionel Sambuc   /// Check that class of this category is already completely declared.
897f4a2713aSLionel Sambuc 
898f4a2713aSLionel Sambuc   if (!IDecl
899f4a2713aSLionel Sambuc       || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
900f4a2713aSLionel Sambuc                              diag::err_category_forward_interface,
901*0a6a1f1dSLionel Sambuc                              CategoryName == nullptr)) {
902f4a2713aSLionel Sambuc     // Create an invalid ObjCCategoryDecl to serve as context for
903f4a2713aSLionel Sambuc     // the enclosing method declarations.  We mark the decl invalid
904f4a2713aSLionel Sambuc     // to make it clear that this isn't a valid AST.
905f4a2713aSLionel Sambuc     CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
906f4a2713aSLionel Sambuc                                      ClassLoc, CategoryLoc, CategoryName,IDecl);
907f4a2713aSLionel Sambuc     CDecl->setInvalidDecl();
908f4a2713aSLionel Sambuc     CurContext->addDecl(CDecl);
909f4a2713aSLionel Sambuc 
910f4a2713aSLionel Sambuc     if (!IDecl)
911f4a2713aSLionel Sambuc       Diag(ClassLoc, diag::err_undef_interface) << ClassName;
912f4a2713aSLionel Sambuc     return ActOnObjCContainerStartDefinition(CDecl);
913f4a2713aSLionel Sambuc   }
914f4a2713aSLionel Sambuc 
915f4a2713aSLionel Sambuc   if (!CategoryName && IDecl->getImplementation()) {
916f4a2713aSLionel Sambuc     Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
917f4a2713aSLionel Sambuc     Diag(IDecl->getImplementation()->getLocation(),
918f4a2713aSLionel Sambuc           diag::note_implementation_declared);
919f4a2713aSLionel Sambuc   }
920f4a2713aSLionel Sambuc 
921f4a2713aSLionel Sambuc   if (CategoryName) {
922f4a2713aSLionel Sambuc     /// Check for duplicate interface declaration for this category
923f4a2713aSLionel Sambuc     if (ObjCCategoryDecl *Previous
924f4a2713aSLionel Sambuc           = IDecl->FindCategoryDeclaration(CategoryName)) {
925f4a2713aSLionel Sambuc       // Class extensions can be declared multiple times, categories cannot.
926f4a2713aSLionel Sambuc       Diag(CategoryLoc, diag::warn_dup_category_def)
927f4a2713aSLionel Sambuc         << ClassName << CategoryName;
928f4a2713aSLionel Sambuc       Diag(Previous->getLocation(), diag::note_previous_definition);
929f4a2713aSLionel Sambuc     }
930f4a2713aSLionel Sambuc   }
931f4a2713aSLionel Sambuc 
932f4a2713aSLionel Sambuc   CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
933f4a2713aSLionel Sambuc                                    ClassLoc, CategoryLoc, CategoryName, IDecl);
934f4a2713aSLionel Sambuc   // FIXME: PushOnScopeChains?
935f4a2713aSLionel Sambuc   CurContext->addDecl(CDecl);
936f4a2713aSLionel Sambuc 
937f4a2713aSLionel Sambuc   if (NumProtoRefs) {
938f4a2713aSLionel Sambuc     CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
939f4a2713aSLionel Sambuc                            ProtoLocs, Context);
940f4a2713aSLionel Sambuc     // Protocols in the class extension belong to the class.
941f4a2713aSLionel Sambuc     if (CDecl->IsClassExtension())
942f4a2713aSLionel Sambuc      IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
943f4a2713aSLionel Sambuc                                             NumProtoRefs, Context);
944f4a2713aSLionel Sambuc   }
945f4a2713aSLionel Sambuc 
946f4a2713aSLionel Sambuc   CheckObjCDeclScope(CDecl);
947f4a2713aSLionel Sambuc   return ActOnObjCContainerStartDefinition(CDecl);
948f4a2713aSLionel Sambuc }
949f4a2713aSLionel Sambuc 
950f4a2713aSLionel Sambuc /// ActOnStartCategoryImplementation - Perform semantic checks on the
951f4a2713aSLionel Sambuc /// category implementation declaration and build an ObjCCategoryImplDecl
952f4a2713aSLionel Sambuc /// object.
ActOnStartCategoryImplementation(SourceLocation AtCatImplLoc,IdentifierInfo * ClassName,SourceLocation ClassLoc,IdentifierInfo * CatName,SourceLocation CatLoc)953f4a2713aSLionel Sambuc Decl *Sema::ActOnStartCategoryImplementation(
954f4a2713aSLionel Sambuc                       SourceLocation AtCatImplLoc,
955f4a2713aSLionel Sambuc                       IdentifierInfo *ClassName, SourceLocation ClassLoc,
956f4a2713aSLionel Sambuc                       IdentifierInfo *CatName, SourceLocation CatLoc) {
957f4a2713aSLionel Sambuc   ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
958*0a6a1f1dSLionel Sambuc   ObjCCategoryDecl *CatIDecl = nullptr;
959f4a2713aSLionel Sambuc   if (IDecl && IDecl->hasDefinition()) {
960f4a2713aSLionel Sambuc     CatIDecl = IDecl->FindCategoryDeclaration(CatName);
961f4a2713aSLionel Sambuc     if (!CatIDecl) {
962f4a2713aSLionel Sambuc       // Category @implementation with no corresponding @interface.
963f4a2713aSLionel Sambuc       // Create and install one.
964f4a2713aSLionel Sambuc       CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
965f4a2713aSLionel Sambuc                                           ClassLoc, CatLoc,
966f4a2713aSLionel Sambuc                                           CatName, IDecl);
967f4a2713aSLionel Sambuc       CatIDecl->setImplicit();
968f4a2713aSLionel Sambuc     }
969f4a2713aSLionel Sambuc   }
970f4a2713aSLionel Sambuc 
971f4a2713aSLionel Sambuc   ObjCCategoryImplDecl *CDecl =
972f4a2713aSLionel Sambuc     ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
973f4a2713aSLionel Sambuc                                  ClassLoc, AtCatImplLoc, CatLoc);
974f4a2713aSLionel Sambuc   /// Check that class of this category is already completely declared.
975f4a2713aSLionel Sambuc   if (!IDecl) {
976f4a2713aSLionel Sambuc     Diag(ClassLoc, diag::err_undef_interface) << ClassName;
977f4a2713aSLionel Sambuc     CDecl->setInvalidDecl();
978f4a2713aSLionel Sambuc   } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
979f4a2713aSLionel Sambuc                                  diag::err_undef_interface)) {
980f4a2713aSLionel Sambuc     CDecl->setInvalidDecl();
981f4a2713aSLionel Sambuc   }
982f4a2713aSLionel Sambuc 
983f4a2713aSLionel Sambuc   // FIXME: PushOnScopeChains?
984f4a2713aSLionel Sambuc   CurContext->addDecl(CDecl);
985f4a2713aSLionel Sambuc 
986f4a2713aSLionel Sambuc   // If the interface is deprecated/unavailable, warn/error about it.
987f4a2713aSLionel Sambuc   if (IDecl)
988f4a2713aSLionel Sambuc     DiagnoseUseOfDecl(IDecl, ClassLoc);
989f4a2713aSLionel Sambuc 
990f4a2713aSLionel Sambuc   /// Check that CatName, category name, is not used in another implementation.
991f4a2713aSLionel Sambuc   if (CatIDecl) {
992f4a2713aSLionel Sambuc     if (CatIDecl->getImplementation()) {
993f4a2713aSLionel Sambuc       Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
994f4a2713aSLionel Sambuc         << CatName;
995f4a2713aSLionel Sambuc       Diag(CatIDecl->getImplementation()->getLocation(),
996f4a2713aSLionel Sambuc            diag::note_previous_definition);
997f4a2713aSLionel Sambuc       CDecl->setInvalidDecl();
998f4a2713aSLionel Sambuc     } else {
999f4a2713aSLionel Sambuc       CatIDecl->setImplementation(CDecl);
1000f4a2713aSLionel Sambuc       // Warn on implementating category of deprecated class under
1001f4a2713aSLionel Sambuc       // -Wdeprecated-implementations flag.
1002f4a2713aSLionel Sambuc       DiagnoseObjCImplementedDeprecations(*this,
1003f4a2713aSLionel Sambuc                                           dyn_cast<NamedDecl>(IDecl),
1004f4a2713aSLionel Sambuc                                           CDecl->getLocation(), 2);
1005f4a2713aSLionel Sambuc     }
1006f4a2713aSLionel Sambuc   }
1007f4a2713aSLionel Sambuc 
1008f4a2713aSLionel Sambuc   CheckObjCDeclScope(CDecl);
1009f4a2713aSLionel Sambuc   return ActOnObjCContainerStartDefinition(CDecl);
1010f4a2713aSLionel Sambuc }
1011f4a2713aSLionel Sambuc 
ActOnStartClassImplementation(SourceLocation AtClassImplLoc,IdentifierInfo * ClassName,SourceLocation ClassLoc,IdentifierInfo * SuperClassname,SourceLocation SuperClassLoc)1012f4a2713aSLionel Sambuc Decl *Sema::ActOnStartClassImplementation(
1013f4a2713aSLionel Sambuc                       SourceLocation AtClassImplLoc,
1014f4a2713aSLionel Sambuc                       IdentifierInfo *ClassName, SourceLocation ClassLoc,
1015f4a2713aSLionel Sambuc                       IdentifierInfo *SuperClassname,
1016f4a2713aSLionel Sambuc                       SourceLocation SuperClassLoc) {
1017*0a6a1f1dSLionel Sambuc   ObjCInterfaceDecl *IDecl = nullptr;
1018f4a2713aSLionel Sambuc   // Check for another declaration kind with the same name.
1019f4a2713aSLionel Sambuc   NamedDecl *PrevDecl
1020f4a2713aSLionel Sambuc     = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
1021f4a2713aSLionel Sambuc                        ForRedeclaration);
1022f4a2713aSLionel Sambuc   if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
1023f4a2713aSLionel Sambuc     Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
1024f4a2713aSLionel Sambuc     Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1025f4a2713aSLionel Sambuc   } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
1026f4a2713aSLionel Sambuc     RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
1027f4a2713aSLionel Sambuc                         diag::warn_undef_interface);
1028f4a2713aSLionel Sambuc   } else {
1029f4a2713aSLionel Sambuc     // We did not find anything with the name ClassName; try to correct for
1030f4a2713aSLionel Sambuc     // typos in the class name.
1031*0a6a1f1dSLionel Sambuc     TypoCorrection Corrected = CorrectTypo(
1032*0a6a1f1dSLionel Sambuc         DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
1033*0a6a1f1dSLionel Sambuc         nullptr, llvm::make_unique<ObjCInterfaceValidatorCCC>(), CTK_NonError);
1034f4a2713aSLionel Sambuc     if (Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) {
1035f4a2713aSLionel Sambuc       // Suggest the (potentially) correct interface name. Don't provide a
1036f4a2713aSLionel Sambuc       // code-modification hint or use the typo name for recovery, because
1037f4a2713aSLionel Sambuc       // this is just a warning. The program may actually be correct.
1038f4a2713aSLionel Sambuc       diagnoseTypo(Corrected,
1039f4a2713aSLionel Sambuc                    PDiag(diag::warn_undef_interface_suggest) << ClassName,
1040f4a2713aSLionel Sambuc                    /*ErrorRecovery*/false);
1041f4a2713aSLionel Sambuc     } else {
1042f4a2713aSLionel Sambuc       Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
1043f4a2713aSLionel Sambuc     }
1044f4a2713aSLionel Sambuc   }
1045f4a2713aSLionel Sambuc 
1046f4a2713aSLionel Sambuc   // Check that super class name is valid class name
1047*0a6a1f1dSLionel Sambuc   ObjCInterfaceDecl *SDecl = nullptr;
1048f4a2713aSLionel Sambuc   if (SuperClassname) {
1049f4a2713aSLionel Sambuc     // Check if a different kind of symbol declared in this scope.
1050f4a2713aSLionel Sambuc     PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
1051f4a2713aSLionel Sambuc                                 LookupOrdinaryName);
1052f4a2713aSLionel Sambuc     if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
1053f4a2713aSLionel Sambuc       Diag(SuperClassLoc, diag::err_redefinition_different_kind)
1054f4a2713aSLionel Sambuc         << SuperClassname;
1055f4a2713aSLionel Sambuc       Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1056f4a2713aSLionel Sambuc     } else {
1057f4a2713aSLionel Sambuc       SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1058f4a2713aSLionel Sambuc       if (SDecl && !SDecl->hasDefinition())
1059*0a6a1f1dSLionel Sambuc         SDecl = nullptr;
1060f4a2713aSLionel Sambuc       if (!SDecl)
1061f4a2713aSLionel Sambuc         Diag(SuperClassLoc, diag::err_undef_superclass)
1062f4a2713aSLionel Sambuc           << SuperClassname << ClassName;
1063f4a2713aSLionel Sambuc       else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
1064f4a2713aSLionel Sambuc         // This implementation and its interface do not have the same
1065f4a2713aSLionel Sambuc         // super class.
1066f4a2713aSLionel Sambuc         Diag(SuperClassLoc, diag::err_conflicting_super_class)
1067f4a2713aSLionel Sambuc           << SDecl->getDeclName();
1068f4a2713aSLionel Sambuc         Diag(SDecl->getLocation(), diag::note_previous_definition);
1069f4a2713aSLionel Sambuc       }
1070f4a2713aSLionel Sambuc     }
1071f4a2713aSLionel Sambuc   }
1072f4a2713aSLionel Sambuc 
1073f4a2713aSLionel Sambuc   if (!IDecl) {
1074f4a2713aSLionel Sambuc     // Legacy case of @implementation with no corresponding @interface.
1075f4a2713aSLionel Sambuc     // Build, chain & install the interface decl into the identifier.
1076f4a2713aSLionel Sambuc 
1077f4a2713aSLionel Sambuc     // FIXME: Do we support attributes on the @implementation? If so we should
1078f4a2713aSLionel Sambuc     // copy them over.
1079f4a2713aSLionel Sambuc     IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
1080*0a6a1f1dSLionel Sambuc                                       ClassName, /*PrevDecl=*/nullptr, ClassLoc,
1081f4a2713aSLionel Sambuc                                       true);
1082f4a2713aSLionel Sambuc     IDecl->startDefinition();
1083f4a2713aSLionel Sambuc     if (SDecl) {
1084f4a2713aSLionel Sambuc       IDecl->setSuperClass(SDecl);
1085f4a2713aSLionel Sambuc       IDecl->setSuperClassLoc(SuperClassLoc);
1086f4a2713aSLionel Sambuc       IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1087f4a2713aSLionel Sambuc     } else {
1088f4a2713aSLionel Sambuc       IDecl->setEndOfDefinitionLoc(ClassLoc);
1089f4a2713aSLionel Sambuc     }
1090f4a2713aSLionel Sambuc 
1091f4a2713aSLionel Sambuc     PushOnScopeChains(IDecl, TUScope);
1092f4a2713aSLionel Sambuc   } else {
1093f4a2713aSLionel Sambuc     // Mark the interface as being completed, even if it was just as
1094f4a2713aSLionel Sambuc     //   @class ....;
1095f4a2713aSLionel Sambuc     // declaration; the user cannot reopen it.
1096f4a2713aSLionel Sambuc     if (!IDecl->hasDefinition())
1097f4a2713aSLionel Sambuc       IDecl->startDefinition();
1098f4a2713aSLionel Sambuc   }
1099f4a2713aSLionel Sambuc 
1100f4a2713aSLionel Sambuc   ObjCImplementationDecl* IMPDecl =
1101f4a2713aSLionel Sambuc     ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
1102f4a2713aSLionel Sambuc                                    ClassLoc, AtClassImplLoc, SuperClassLoc);
1103f4a2713aSLionel Sambuc 
1104f4a2713aSLionel Sambuc   if (CheckObjCDeclScope(IMPDecl))
1105f4a2713aSLionel Sambuc     return ActOnObjCContainerStartDefinition(IMPDecl);
1106f4a2713aSLionel Sambuc 
1107f4a2713aSLionel Sambuc   // Check that there is no duplicate implementation of this class.
1108f4a2713aSLionel Sambuc   if (IDecl->getImplementation()) {
1109f4a2713aSLionel Sambuc     // FIXME: Don't leak everything!
1110f4a2713aSLionel Sambuc     Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
1111f4a2713aSLionel Sambuc     Diag(IDecl->getImplementation()->getLocation(),
1112f4a2713aSLionel Sambuc          diag::note_previous_definition);
1113f4a2713aSLionel Sambuc     IMPDecl->setInvalidDecl();
1114f4a2713aSLionel Sambuc   } else { // add it to the list.
1115f4a2713aSLionel Sambuc     IDecl->setImplementation(IMPDecl);
1116f4a2713aSLionel Sambuc     PushOnScopeChains(IMPDecl, TUScope);
1117f4a2713aSLionel Sambuc     // Warn on implementating deprecated class under
1118f4a2713aSLionel Sambuc     // -Wdeprecated-implementations flag.
1119f4a2713aSLionel Sambuc     DiagnoseObjCImplementedDeprecations(*this,
1120f4a2713aSLionel Sambuc                                         dyn_cast<NamedDecl>(IDecl),
1121f4a2713aSLionel Sambuc                                         IMPDecl->getLocation(), 1);
1122f4a2713aSLionel Sambuc   }
1123f4a2713aSLionel Sambuc   return ActOnObjCContainerStartDefinition(IMPDecl);
1124f4a2713aSLionel Sambuc }
1125f4a2713aSLionel Sambuc 
1126f4a2713aSLionel Sambuc Sema::DeclGroupPtrTy
ActOnFinishObjCImplementation(Decl * ObjCImpDecl,ArrayRef<Decl * > Decls)1127f4a2713aSLionel Sambuc Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1128f4a2713aSLionel Sambuc   SmallVector<Decl *, 64> DeclsInGroup;
1129f4a2713aSLionel Sambuc   DeclsInGroup.reserve(Decls.size() + 1);
1130f4a2713aSLionel Sambuc 
1131f4a2713aSLionel Sambuc   for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1132f4a2713aSLionel Sambuc     Decl *Dcl = Decls[i];
1133f4a2713aSLionel Sambuc     if (!Dcl)
1134f4a2713aSLionel Sambuc       continue;
1135f4a2713aSLionel Sambuc     if (Dcl->getDeclContext()->isFileContext())
1136f4a2713aSLionel Sambuc       Dcl->setTopLevelDeclInObjCContainer();
1137f4a2713aSLionel Sambuc     DeclsInGroup.push_back(Dcl);
1138f4a2713aSLionel Sambuc   }
1139f4a2713aSLionel Sambuc 
1140f4a2713aSLionel Sambuc   DeclsInGroup.push_back(ObjCImpDecl);
1141f4a2713aSLionel Sambuc 
1142f4a2713aSLionel Sambuc   return BuildDeclaratorGroup(DeclsInGroup, false);
1143f4a2713aSLionel Sambuc }
1144f4a2713aSLionel Sambuc 
CheckImplementationIvars(ObjCImplementationDecl * ImpDecl,ObjCIvarDecl ** ivars,unsigned numIvars,SourceLocation RBrace)1145f4a2713aSLionel Sambuc void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1146f4a2713aSLionel Sambuc                                     ObjCIvarDecl **ivars, unsigned numIvars,
1147f4a2713aSLionel Sambuc                                     SourceLocation RBrace) {
1148f4a2713aSLionel Sambuc   assert(ImpDecl && "missing implementation decl");
1149f4a2713aSLionel Sambuc   ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
1150f4a2713aSLionel Sambuc   if (!IDecl)
1151f4a2713aSLionel Sambuc     return;
1152f4a2713aSLionel Sambuc   /// Check case of non-existing \@interface decl.
1153f4a2713aSLionel Sambuc   /// (legacy objective-c \@implementation decl without an \@interface decl).
1154f4a2713aSLionel Sambuc   /// Add implementations's ivar to the synthesize class's ivar list.
1155f4a2713aSLionel Sambuc   if (IDecl->isImplicitInterfaceDecl()) {
1156f4a2713aSLionel Sambuc     IDecl->setEndOfDefinitionLoc(RBrace);
1157f4a2713aSLionel Sambuc     // Add ivar's to class's DeclContext.
1158f4a2713aSLionel Sambuc     for (unsigned i = 0, e = numIvars; i != e; ++i) {
1159f4a2713aSLionel Sambuc       ivars[i]->setLexicalDeclContext(ImpDecl);
1160f4a2713aSLionel Sambuc       IDecl->makeDeclVisibleInContext(ivars[i]);
1161f4a2713aSLionel Sambuc       ImpDecl->addDecl(ivars[i]);
1162f4a2713aSLionel Sambuc     }
1163f4a2713aSLionel Sambuc 
1164f4a2713aSLionel Sambuc     return;
1165f4a2713aSLionel Sambuc   }
1166f4a2713aSLionel Sambuc   // If implementation has empty ivar list, just return.
1167f4a2713aSLionel Sambuc   if (numIvars == 0)
1168f4a2713aSLionel Sambuc     return;
1169f4a2713aSLionel Sambuc 
1170f4a2713aSLionel Sambuc   assert(ivars && "missing @implementation ivars");
1171f4a2713aSLionel Sambuc   if (LangOpts.ObjCRuntime.isNonFragile()) {
1172f4a2713aSLionel Sambuc     if (ImpDecl->getSuperClass())
1173f4a2713aSLionel Sambuc       Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1174f4a2713aSLionel Sambuc     for (unsigned i = 0; i < numIvars; i++) {
1175f4a2713aSLionel Sambuc       ObjCIvarDecl* ImplIvar = ivars[i];
1176f4a2713aSLionel Sambuc       if (const ObjCIvarDecl *ClsIvar =
1177f4a2713aSLionel Sambuc             IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1178f4a2713aSLionel Sambuc         Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1179f4a2713aSLionel Sambuc         Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1180f4a2713aSLionel Sambuc         continue;
1181f4a2713aSLionel Sambuc       }
1182f4a2713aSLionel Sambuc       // Check class extensions (unnamed categories) for duplicate ivars.
1183*0a6a1f1dSLionel Sambuc       for (const auto *CDecl : IDecl->visible_extensions()) {
1184f4a2713aSLionel Sambuc         if (const ObjCIvarDecl *ClsExtIvar =
1185f4a2713aSLionel Sambuc             CDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1186f4a2713aSLionel Sambuc           Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1187f4a2713aSLionel Sambuc           Diag(ClsExtIvar->getLocation(), diag::note_previous_definition);
1188f4a2713aSLionel Sambuc           continue;
1189f4a2713aSLionel Sambuc         }
1190f4a2713aSLionel Sambuc       }
1191f4a2713aSLionel Sambuc       // Instance ivar to Implementation's DeclContext.
1192f4a2713aSLionel Sambuc       ImplIvar->setLexicalDeclContext(ImpDecl);
1193f4a2713aSLionel Sambuc       IDecl->makeDeclVisibleInContext(ImplIvar);
1194f4a2713aSLionel Sambuc       ImpDecl->addDecl(ImplIvar);
1195f4a2713aSLionel Sambuc     }
1196f4a2713aSLionel Sambuc     return;
1197f4a2713aSLionel Sambuc   }
1198f4a2713aSLionel Sambuc   // Check interface's Ivar list against those in the implementation.
1199f4a2713aSLionel Sambuc   // names and types must match.
1200f4a2713aSLionel Sambuc   //
1201f4a2713aSLionel Sambuc   unsigned j = 0;
1202f4a2713aSLionel Sambuc   ObjCInterfaceDecl::ivar_iterator
1203f4a2713aSLionel Sambuc     IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1204f4a2713aSLionel Sambuc   for (; numIvars > 0 && IVI != IVE; ++IVI) {
1205f4a2713aSLionel Sambuc     ObjCIvarDecl* ImplIvar = ivars[j++];
1206f4a2713aSLionel Sambuc     ObjCIvarDecl* ClsIvar = *IVI;
1207f4a2713aSLionel Sambuc     assert (ImplIvar && "missing implementation ivar");
1208f4a2713aSLionel Sambuc     assert (ClsIvar && "missing class ivar");
1209f4a2713aSLionel Sambuc 
1210f4a2713aSLionel Sambuc     // First, make sure the types match.
1211f4a2713aSLionel Sambuc     if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
1212f4a2713aSLionel Sambuc       Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
1213f4a2713aSLionel Sambuc         << ImplIvar->getIdentifier()
1214f4a2713aSLionel Sambuc         << ImplIvar->getType() << ClsIvar->getType();
1215f4a2713aSLionel Sambuc       Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1216f4a2713aSLionel Sambuc     } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1217f4a2713aSLionel Sambuc                ImplIvar->getBitWidthValue(Context) !=
1218f4a2713aSLionel Sambuc                ClsIvar->getBitWidthValue(Context)) {
1219f4a2713aSLionel Sambuc       Diag(ImplIvar->getBitWidth()->getLocStart(),
1220f4a2713aSLionel Sambuc            diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1221f4a2713aSLionel Sambuc       Diag(ClsIvar->getBitWidth()->getLocStart(),
1222f4a2713aSLionel Sambuc            diag::note_previous_definition);
1223f4a2713aSLionel Sambuc     }
1224f4a2713aSLionel Sambuc     // Make sure the names are identical.
1225f4a2713aSLionel Sambuc     if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
1226f4a2713aSLionel Sambuc       Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
1227f4a2713aSLionel Sambuc         << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
1228f4a2713aSLionel Sambuc       Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1229f4a2713aSLionel Sambuc     }
1230f4a2713aSLionel Sambuc     --numIvars;
1231f4a2713aSLionel Sambuc   }
1232f4a2713aSLionel Sambuc 
1233f4a2713aSLionel Sambuc   if (numIvars > 0)
1234*0a6a1f1dSLionel Sambuc     Diag(ivars[j]->getLocation(), diag::err_inconsistent_ivar_count);
1235f4a2713aSLionel Sambuc   else if (IVI != IVE)
1236*0a6a1f1dSLionel Sambuc     Diag(IVI->getLocation(), diag::err_inconsistent_ivar_count);
1237f4a2713aSLionel Sambuc }
1238f4a2713aSLionel Sambuc 
WarnUndefinedMethod(Sema & S,SourceLocation ImpLoc,ObjCMethodDecl * method,bool & IncompleteImpl,unsigned DiagID,NamedDecl * NeededFor=nullptr)1239*0a6a1f1dSLionel Sambuc static void WarnUndefinedMethod(Sema &S, SourceLocation ImpLoc,
1240*0a6a1f1dSLionel Sambuc                                 ObjCMethodDecl *method,
1241*0a6a1f1dSLionel Sambuc                                 bool &IncompleteImpl,
1242*0a6a1f1dSLionel Sambuc                                 unsigned DiagID,
1243*0a6a1f1dSLionel Sambuc                                 NamedDecl *NeededFor = nullptr) {
1244f4a2713aSLionel Sambuc   // No point warning no definition of method which is 'unavailable'.
1245f4a2713aSLionel Sambuc   switch (method->getAvailability()) {
1246f4a2713aSLionel Sambuc   case AR_Available:
1247f4a2713aSLionel Sambuc   case AR_Deprecated:
1248f4a2713aSLionel Sambuc     break;
1249f4a2713aSLionel Sambuc 
1250f4a2713aSLionel Sambuc       // Don't warn about unavailable or not-yet-introduced methods.
1251f4a2713aSLionel Sambuc   case AR_NotYetIntroduced:
1252f4a2713aSLionel Sambuc   case AR_Unavailable:
1253f4a2713aSLionel Sambuc     return;
1254f4a2713aSLionel Sambuc   }
1255f4a2713aSLionel Sambuc 
1256f4a2713aSLionel Sambuc   // FIXME: For now ignore 'IncompleteImpl'.
1257f4a2713aSLionel Sambuc   // Previously we grouped all unimplemented methods under a single
1258f4a2713aSLionel Sambuc   // warning, but some users strongly voiced that they would prefer
1259f4a2713aSLionel Sambuc   // separate warnings.  We will give that approach a try, as that
1260f4a2713aSLionel Sambuc   // matches what we do with protocols.
1261*0a6a1f1dSLionel Sambuc   {
1262*0a6a1f1dSLionel Sambuc     const Sema::SemaDiagnosticBuilder &B = S.Diag(ImpLoc, DiagID);
1263*0a6a1f1dSLionel Sambuc     B << method;
1264*0a6a1f1dSLionel Sambuc     if (NeededFor)
1265*0a6a1f1dSLionel Sambuc       B << NeededFor;
1266*0a6a1f1dSLionel Sambuc   }
1267f4a2713aSLionel Sambuc 
1268f4a2713aSLionel Sambuc   // Issue a note to the original declaration.
1269f4a2713aSLionel Sambuc   SourceLocation MethodLoc = method->getLocStart();
1270f4a2713aSLionel Sambuc   if (MethodLoc.isValid())
1271*0a6a1f1dSLionel Sambuc     S.Diag(MethodLoc, diag::note_method_declared_at) << method;
1272f4a2713aSLionel Sambuc }
1273f4a2713aSLionel Sambuc 
1274f4a2713aSLionel Sambuc /// Determines if type B can be substituted for type A.  Returns true if we can
1275f4a2713aSLionel Sambuc /// guarantee that anything that the user will do to an object of type A can
1276f4a2713aSLionel Sambuc /// also be done to an object of type B.  This is trivially true if the two
1277f4a2713aSLionel Sambuc /// types are the same, or if B is a subclass of A.  It becomes more complex
1278f4a2713aSLionel Sambuc /// in cases where protocols are involved.
1279f4a2713aSLionel Sambuc ///
1280f4a2713aSLionel Sambuc /// Object types in Objective-C describe the minimum requirements for an
1281f4a2713aSLionel Sambuc /// object, rather than providing a complete description of a type.  For
1282f4a2713aSLionel Sambuc /// example, if A is a subclass of B, then B* may refer to an instance of A.
1283f4a2713aSLionel Sambuc /// The principle of substitutability means that we may use an instance of A
1284f4a2713aSLionel Sambuc /// anywhere that we may use an instance of B - it will implement all of the
1285f4a2713aSLionel Sambuc /// ivars of B and all of the methods of B.
1286f4a2713aSLionel Sambuc ///
1287f4a2713aSLionel Sambuc /// This substitutability is important when type checking methods, because
1288f4a2713aSLionel Sambuc /// the implementation may have stricter type definitions than the interface.
1289f4a2713aSLionel Sambuc /// The interface specifies minimum requirements, but the implementation may
1290f4a2713aSLionel Sambuc /// have more accurate ones.  For example, a method may privately accept
1291f4a2713aSLionel Sambuc /// instances of B, but only publish that it accepts instances of A.  Any
1292f4a2713aSLionel Sambuc /// object passed to it will be type checked against B, and so will implicitly
1293f4a2713aSLionel Sambuc /// by a valid A*.  Similarly, a method may return a subclass of the class that
1294f4a2713aSLionel Sambuc /// it is declared as returning.
1295f4a2713aSLionel Sambuc ///
1296f4a2713aSLionel Sambuc /// This is most important when considering subclassing.  A method in a
1297f4a2713aSLionel Sambuc /// subclass must accept any object as an argument that its superclass's
1298f4a2713aSLionel Sambuc /// implementation accepts.  It may, however, accept a more general type
1299f4a2713aSLionel Sambuc /// without breaking substitutability (i.e. you can still use the subclass
1300f4a2713aSLionel Sambuc /// anywhere that you can use the superclass, but not vice versa).  The
1301f4a2713aSLionel Sambuc /// converse requirement applies to return types: the return type for a
1302f4a2713aSLionel Sambuc /// subclass method must be a valid object of the kind that the superclass
1303f4a2713aSLionel Sambuc /// advertises, but it may be specified more accurately.  This avoids the need
1304f4a2713aSLionel Sambuc /// for explicit down-casting by callers.
1305f4a2713aSLionel Sambuc ///
1306f4a2713aSLionel Sambuc /// Note: This is a stricter requirement than for assignment.
isObjCTypeSubstitutable(ASTContext & Context,const ObjCObjectPointerType * A,const ObjCObjectPointerType * B,bool rejectId)1307f4a2713aSLionel Sambuc static bool isObjCTypeSubstitutable(ASTContext &Context,
1308f4a2713aSLionel Sambuc                                     const ObjCObjectPointerType *A,
1309f4a2713aSLionel Sambuc                                     const ObjCObjectPointerType *B,
1310f4a2713aSLionel Sambuc                                     bool rejectId) {
1311f4a2713aSLionel Sambuc   // Reject a protocol-unqualified id.
1312f4a2713aSLionel Sambuc   if (rejectId && B->isObjCIdType()) return false;
1313f4a2713aSLionel Sambuc 
1314f4a2713aSLionel Sambuc   // If B is a qualified id, then A must also be a qualified id and it must
1315f4a2713aSLionel Sambuc   // implement all of the protocols in B.  It may not be a qualified class.
1316f4a2713aSLionel Sambuc   // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1317f4a2713aSLionel Sambuc   // stricter definition so it is not substitutable for id<A>.
1318f4a2713aSLionel Sambuc   if (B->isObjCQualifiedIdType()) {
1319f4a2713aSLionel Sambuc     return A->isObjCQualifiedIdType() &&
1320f4a2713aSLionel Sambuc            Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1321f4a2713aSLionel Sambuc                                                      QualType(B,0),
1322f4a2713aSLionel Sambuc                                                      false);
1323f4a2713aSLionel Sambuc   }
1324f4a2713aSLionel Sambuc 
1325f4a2713aSLionel Sambuc   /*
1326f4a2713aSLionel Sambuc   // id is a special type that bypasses type checking completely.  We want a
1327f4a2713aSLionel Sambuc   // warning when it is used in one place but not another.
1328f4a2713aSLionel Sambuc   if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1329f4a2713aSLionel Sambuc 
1330f4a2713aSLionel Sambuc 
1331f4a2713aSLionel Sambuc   // If B is a qualified id, then A must also be a qualified id (which it isn't
1332f4a2713aSLionel Sambuc   // if we've got this far)
1333f4a2713aSLionel Sambuc   if (B->isObjCQualifiedIdType()) return false;
1334f4a2713aSLionel Sambuc   */
1335f4a2713aSLionel Sambuc 
1336f4a2713aSLionel Sambuc   // Now we know that A and B are (potentially-qualified) class types.  The
1337f4a2713aSLionel Sambuc   // normal rules for assignment apply.
1338f4a2713aSLionel Sambuc   return Context.canAssignObjCInterfaces(A, B);
1339f4a2713aSLionel Sambuc }
1340f4a2713aSLionel Sambuc 
getTypeRange(TypeSourceInfo * TSI)1341f4a2713aSLionel Sambuc static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1342f4a2713aSLionel Sambuc   return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1343f4a2713aSLionel Sambuc }
1344f4a2713aSLionel Sambuc 
CheckMethodOverrideReturn(Sema & S,ObjCMethodDecl * MethodImpl,ObjCMethodDecl * MethodDecl,bool IsProtocolMethodDecl,bool IsOverridingMode,bool Warn)1345f4a2713aSLionel Sambuc static bool CheckMethodOverrideReturn(Sema &S,
1346f4a2713aSLionel Sambuc                                       ObjCMethodDecl *MethodImpl,
1347f4a2713aSLionel Sambuc                                       ObjCMethodDecl *MethodDecl,
1348f4a2713aSLionel Sambuc                                       bool IsProtocolMethodDecl,
1349f4a2713aSLionel Sambuc                                       bool IsOverridingMode,
1350f4a2713aSLionel Sambuc                                       bool Warn) {
1351f4a2713aSLionel Sambuc   if (IsProtocolMethodDecl &&
1352f4a2713aSLionel Sambuc       (MethodDecl->getObjCDeclQualifier() !=
1353f4a2713aSLionel Sambuc        MethodImpl->getObjCDeclQualifier())) {
1354f4a2713aSLionel Sambuc     if (Warn) {
1355f4a2713aSLionel Sambuc       S.Diag(MethodImpl->getLocation(),
1356*0a6a1f1dSLionel Sambuc              (IsOverridingMode
1357*0a6a1f1dSLionel Sambuc                   ? diag::warn_conflicting_overriding_ret_type_modifiers
1358f4a2713aSLionel Sambuc                   : diag::warn_conflicting_ret_type_modifiers))
1359f4a2713aSLionel Sambuc           << MethodImpl->getDeclName()
1360*0a6a1f1dSLionel Sambuc           << MethodImpl->getReturnTypeSourceRange();
1361f4a2713aSLionel Sambuc       S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1362*0a6a1f1dSLionel Sambuc           << MethodDecl->getReturnTypeSourceRange();
1363f4a2713aSLionel Sambuc     }
1364f4a2713aSLionel Sambuc     else
1365f4a2713aSLionel Sambuc       return false;
1366f4a2713aSLionel Sambuc   }
1367f4a2713aSLionel Sambuc 
1368*0a6a1f1dSLionel Sambuc   if (S.Context.hasSameUnqualifiedType(MethodImpl->getReturnType(),
1369*0a6a1f1dSLionel Sambuc                                        MethodDecl->getReturnType()))
1370f4a2713aSLionel Sambuc     return true;
1371f4a2713aSLionel Sambuc   if (!Warn)
1372f4a2713aSLionel Sambuc     return false;
1373f4a2713aSLionel Sambuc 
1374f4a2713aSLionel Sambuc   unsigned DiagID =
1375f4a2713aSLionel Sambuc     IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1376f4a2713aSLionel Sambuc                      : diag::warn_conflicting_ret_types;
1377f4a2713aSLionel Sambuc 
1378f4a2713aSLionel Sambuc   // Mismatches between ObjC pointers go into a different warning
1379f4a2713aSLionel Sambuc   // category, and sometimes they're even completely whitelisted.
1380f4a2713aSLionel Sambuc   if (const ObjCObjectPointerType *ImplPtrTy =
1381*0a6a1f1dSLionel Sambuc           MethodImpl->getReturnType()->getAs<ObjCObjectPointerType>()) {
1382f4a2713aSLionel Sambuc     if (const ObjCObjectPointerType *IfacePtrTy =
1383*0a6a1f1dSLionel Sambuc             MethodDecl->getReturnType()->getAs<ObjCObjectPointerType>()) {
1384f4a2713aSLionel Sambuc       // Allow non-matching return types as long as they don't violate
1385f4a2713aSLionel Sambuc       // the principle of substitutability.  Specifically, we permit
1386f4a2713aSLionel Sambuc       // return types that are subclasses of the declared return type,
1387f4a2713aSLionel Sambuc       // or that are more-qualified versions of the declared type.
1388f4a2713aSLionel Sambuc       if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
1389f4a2713aSLionel Sambuc         return false;
1390f4a2713aSLionel Sambuc 
1391f4a2713aSLionel Sambuc       DiagID =
1392f4a2713aSLionel Sambuc         IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1393f4a2713aSLionel Sambuc                           : diag::warn_non_covariant_ret_types;
1394f4a2713aSLionel Sambuc     }
1395f4a2713aSLionel Sambuc   }
1396f4a2713aSLionel Sambuc 
1397f4a2713aSLionel Sambuc   S.Diag(MethodImpl->getLocation(), DiagID)
1398*0a6a1f1dSLionel Sambuc       << MethodImpl->getDeclName() << MethodDecl->getReturnType()
1399*0a6a1f1dSLionel Sambuc       << MethodImpl->getReturnType()
1400*0a6a1f1dSLionel Sambuc       << MethodImpl->getReturnTypeSourceRange();
1401*0a6a1f1dSLionel Sambuc   S.Diag(MethodDecl->getLocation(), IsOverridingMode
1402*0a6a1f1dSLionel Sambuc                                         ? diag::note_previous_declaration
1403f4a2713aSLionel Sambuc                                         : diag::note_previous_definition)
1404*0a6a1f1dSLionel Sambuc       << MethodDecl->getReturnTypeSourceRange();
1405f4a2713aSLionel Sambuc   return false;
1406f4a2713aSLionel Sambuc }
1407f4a2713aSLionel Sambuc 
CheckMethodOverrideParam(Sema & S,ObjCMethodDecl * MethodImpl,ObjCMethodDecl * MethodDecl,ParmVarDecl * ImplVar,ParmVarDecl * IfaceVar,bool IsProtocolMethodDecl,bool IsOverridingMode,bool Warn)1408f4a2713aSLionel Sambuc static bool CheckMethodOverrideParam(Sema &S,
1409f4a2713aSLionel Sambuc                                      ObjCMethodDecl *MethodImpl,
1410f4a2713aSLionel Sambuc                                      ObjCMethodDecl *MethodDecl,
1411f4a2713aSLionel Sambuc                                      ParmVarDecl *ImplVar,
1412f4a2713aSLionel Sambuc                                      ParmVarDecl *IfaceVar,
1413f4a2713aSLionel Sambuc                                      bool IsProtocolMethodDecl,
1414f4a2713aSLionel Sambuc                                      bool IsOverridingMode,
1415f4a2713aSLionel Sambuc                                      bool Warn) {
1416f4a2713aSLionel Sambuc   if (IsProtocolMethodDecl &&
1417f4a2713aSLionel Sambuc       (ImplVar->getObjCDeclQualifier() !=
1418f4a2713aSLionel Sambuc        IfaceVar->getObjCDeclQualifier())) {
1419f4a2713aSLionel Sambuc     if (Warn) {
1420f4a2713aSLionel Sambuc       if (IsOverridingMode)
1421f4a2713aSLionel Sambuc         S.Diag(ImplVar->getLocation(),
1422f4a2713aSLionel Sambuc                diag::warn_conflicting_overriding_param_modifiers)
1423f4a2713aSLionel Sambuc             << getTypeRange(ImplVar->getTypeSourceInfo())
1424f4a2713aSLionel Sambuc             << MethodImpl->getDeclName();
1425f4a2713aSLionel Sambuc       else S.Diag(ImplVar->getLocation(),
1426f4a2713aSLionel Sambuc              diag::warn_conflicting_param_modifiers)
1427f4a2713aSLionel Sambuc           << getTypeRange(ImplVar->getTypeSourceInfo())
1428f4a2713aSLionel Sambuc           << MethodImpl->getDeclName();
1429f4a2713aSLionel Sambuc       S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1430f4a2713aSLionel Sambuc           << getTypeRange(IfaceVar->getTypeSourceInfo());
1431f4a2713aSLionel Sambuc     }
1432f4a2713aSLionel Sambuc     else
1433f4a2713aSLionel Sambuc       return false;
1434f4a2713aSLionel Sambuc   }
1435f4a2713aSLionel Sambuc 
1436f4a2713aSLionel Sambuc   QualType ImplTy = ImplVar->getType();
1437f4a2713aSLionel Sambuc   QualType IfaceTy = IfaceVar->getType();
1438f4a2713aSLionel Sambuc 
1439f4a2713aSLionel Sambuc   if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
1440f4a2713aSLionel Sambuc     return true;
1441f4a2713aSLionel Sambuc 
1442f4a2713aSLionel Sambuc   if (!Warn)
1443f4a2713aSLionel Sambuc     return false;
1444f4a2713aSLionel Sambuc   unsigned DiagID =
1445f4a2713aSLionel Sambuc     IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1446f4a2713aSLionel Sambuc                      : diag::warn_conflicting_param_types;
1447f4a2713aSLionel Sambuc 
1448f4a2713aSLionel Sambuc   // Mismatches between ObjC pointers go into a different warning
1449f4a2713aSLionel Sambuc   // category, and sometimes they're even completely whitelisted.
1450f4a2713aSLionel Sambuc   if (const ObjCObjectPointerType *ImplPtrTy =
1451f4a2713aSLionel Sambuc         ImplTy->getAs<ObjCObjectPointerType>()) {
1452f4a2713aSLionel Sambuc     if (const ObjCObjectPointerType *IfacePtrTy =
1453f4a2713aSLionel Sambuc           IfaceTy->getAs<ObjCObjectPointerType>()) {
1454f4a2713aSLionel Sambuc       // Allow non-matching argument types as long as they don't
1455f4a2713aSLionel Sambuc       // violate the principle of substitutability.  Specifically, the
1456f4a2713aSLionel Sambuc       // implementation must accept any objects that the superclass
1457f4a2713aSLionel Sambuc       // accepts, however it may also accept others.
1458f4a2713aSLionel Sambuc       if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
1459f4a2713aSLionel Sambuc         return false;
1460f4a2713aSLionel Sambuc 
1461f4a2713aSLionel Sambuc       DiagID =
1462f4a2713aSLionel Sambuc       IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1463f4a2713aSLionel Sambuc                        :  diag::warn_non_contravariant_param_types;
1464f4a2713aSLionel Sambuc     }
1465f4a2713aSLionel Sambuc   }
1466f4a2713aSLionel Sambuc 
1467f4a2713aSLionel Sambuc   S.Diag(ImplVar->getLocation(), DiagID)
1468f4a2713aSLionel Sambuc     << getTypeRange(ImplVar->getTypeSourceInfo())
1469f4a2713aSLionel Sambuc     << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1470f4a2713aSLionel Sambuc   S.Diag(IfaceVar->getLocation(),
1471f4a2713aSLionel Sambuc          (IsOverridingMode ? diag::note_previous_declaration
1472f4a2713aSLionel Sambuc                         : diag::note_previous_definition))
1473f4a2713aSLionel Sambuc     << getTypeRange(IfaceVar->getTypeSourceInfo());
1474f4a2713aSLionel Sambuc   return false;
1475f4a2713aSLionel Sambuc }
1476f4a2713aSLionel Sambuc 
1477f4a2713aSLionel Sambuc /// In ARC, check whether the conventional meanings of the two methods
1478f4a2713aSLionel Sambuc /// match.  If they don't, it's a hard error.
checkMethodFamilyMismatch(Sema & S,ObjCMethodDecl * impl,ObjCMethodDecl * decl)1479f4a2713aSLionel Sambuc static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1480f4a2713aSLionel Sambuc                                       ObjCMethodDecl *decl) {
1481f4a2713aSLionel Sambuc   ObjCMethodFamily implFamily = impl->getMethodFamily();
1482f4a2713aSLionel Sambuc   ObjCMethodFamily declFamily = decl->getMethodFamily();
1483f4a2713aSLionel Sambuc   if (implFamily == declFamily) return false;
1484f4a2713aSLionel Sambuc 
1485f4a2713aSLionel Sambuc   // Since conventions are sorted by selector, the only possibility is
1486f4a2713aSLionel Sambuc   // that the types differ enough to cause one selector or the other
1487f4a2713aSLionel Sambuc   // to fall out of the family.
1488f4a2713aSLionel Sambuc   assert(implFamily == OMF_None || declFamily == OMF_None);
1489f4a2713aSLionel Sambuc 
1490f4a2713aSLionel Sambuc   // No further diagnostics required on invalid declarations.
1491f4a2713aSLionel Sambuc   if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1492f4a2713aSLionel Sambuc 
1493f4a2713aSLionel Sambuc   const ObjCMethodDecl *unmatched = impl;
1494f4a2713aSLionel Sambuc   ObjCMethodFamily family = declFamily;
1495f4a2713aSLionel Sambuc   unsigned errorID = diag::err_arc_lost_method_convention;
1496f4a2713aSLionel Sambuc   unsigned noteID = diag::note_arc_lost_method_convention;
1497f4a2713aSLionel Sambuc   if (declFamily == OMF_None) {
1498f4a2713aSLionel Sambuc     unmatched = decl;
1499f4a2713aSLionel Sambuc     family = implFamily;
1500f4a2713aSLionel Sambuc     errorID = diag::err_arc_gained_method_convention;
1501f4a2713aSLionel Sambuc     noteID = diag::note_arc_gained_method_convention;
1502f4a2713aSLionel Sambuc   }
1503f4a2713aSLionel Sambuc 
1504f4a2713aSLionel Sambuc   // Indexes into a %select clause in the diagnostic.
1505f4a2713aSLionel Sambuc   enum FamilySelector {
1506f4a2713aSLionel Sambuc     F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1507f4a2713aSLionel Sambuc   };
1508f4a2713aSLionel Sambuc   FamilySelector familySelector = FamilySelector();
1509f4a2713aSLionel Sambuc 
1510f4a2713aSLionel Sambuc   switch (family) {
1511f4a2713aSLionel Sambuc   case OMF_None: llvm_unreachable("logic error, no method convention");
1512f4a2713aSLionel Sambuc   case OMF_retain:
1513f4a2713aSLionel Sambuc   case OMF_release:
1514f4a2713aSLionel Sambuc   case OMF_autorelease:
1515f4a2713aSLionel Sambuc   case OMF_dealloc:
1516f4a2713aSLionel Sambuc   case OMF_finalize:
1517f4a2713aSLionel Sambuc   case OMF_retainCount:
1518f4a2713aSLionel Sambuc   case OMF_self:
1519*0a6a1f1dSLionel Sambuc   case OMF_initialize:
1520f4a2713aSLionel Sambuc   case OMF_performSelector:
1521f4a2713aSLionel Sambuc     // Mismatches for these methods don't change ownership
1522f4a2713aSLionel Sambuc     // conventions, so we don't care.
1523f4a2713aSLionel Sambuc     return false;
1524f4a2713aSLionel Sambuc 
1525f4a2713aSLionel Sambuc   case OMF_init: familySelector = F_init; break;
1526f4a2713aSLionel Sambuc   case OMF_alloc: familySelector = F_alloc; break;
1527f4a2713aSLionel Sambuc   case OMF_copy: familySelector = F_copy; break;
1528f4a2713aSLionel Sambuc   case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1529f4a2713aSLionel Sambuc   case OMF_new: familySelector = F_new; break;
1530f4a2713aSLionel Sambuc   }
1531f4a2713aSLionel Sambuc 
1532f4a2713aSLionel Sambuc   enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1533f4a2713aSLionel Sambuc   ReasonSelector reasonSelector;
1534f4a2713aSLionel Sambuc 
1535f4a2713aSLionel Sambuc   // The only reason these methods don't fall within their families is
1536f4a2713aSLionel Sambuc   // due to unusual result types.
1537*0a6a1f1dSLionel Sambuc   if (unmatched->getReturnType()->isObjCObjectPointerType()) {
1538f4a2713aSLionel Sambuc     reasonSelector = R_UnrelatedReturn;
1539f4a2713aSLionel Sambuc   } else {
1540f4a2713aSLionel Sambuc     reasonSelector = R_NonObjectReturn;
1541f4a2713aSLionel Sambuc   }
1542f4a2713aSLionel Sambuc 
1543f4a2713aSLionel Sambuc   S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector);
1544f4a2713aSLionel Sambuc   S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector);
1545f4a2713aSLionel Sambuc 
1546f4a2713aSLionel Sambuc   return true;
1547f4a2713aSLionel Sambuc }
1548f4a2713aSLionel Sambuc 
WarnConflictingTypedMethods(ObjCMethodDecl * ImpMethodDecl,ObjCMethodDecl * MethodDecl,bool IsProtocolMethodDecl)1549f4a2713aSLionel Sambuc void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1550f4a2713aSLionel Sambuc                                        ObjCMethodDecl *MethodDecl,
1551f4a2713aSLionel Sambuc                                        bool IsProtocolMethodDecl) {
1552f4a2713aSLionel Sambuc   if (getLangOpts().ObjCAutoRefCount &&
1553f4a2713aSLionel Sambuc       checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1554f4a2713aSLionel Sambuc     return;
1555f4a2713aSLionel Sambuc 
1556f4a2713aSLionel Sambuc   CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1557f4a2713aSLionel Sambuc                             IsProtocolMethodDecl, false,
1558f4a2713aSLionel Sambuc                             true);
1559f4a2713aSLionel Sambuc 
1560f4a2713aSLionel Sambuc   for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1561f4a2713aSLionel Sambuc        IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1562f4a2713aSLionel Sambuc        EF = MethodDecl->param_end();
1563f4a2713aSLionel Sambuc        IM != EM && IF != EF; ++IM, ++IF) {
1564f4a2713aSLionel Sambuc     CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1565f4a2713aSLionel Sambuc                              IsProtocolMethodDecl, false, true);
1566f4a2713aSLionel Sambuc   }
1567f4a2713aSLionel Sambuc 
1568f4a2713aSLionel Sambuc   if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
1569f4a2713aSLionel Sambuc     Diag(ImpMethodDecl->getLocation(),
1570f4a2713aSLionel Sambuc          diag::warn_conflicting_variadic);
1571f4a2713aSLionel Sambuc     Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
1572f4a2713aSLionel Sambuc   }
1573f4a2713aSLionel Sambuc }
1574f4a2713aSLionel Sambuc 
CheckConflictingOverridingMethod(ObjCMethodDecl * Method,ObjCMethodDecl * Overridden,bool IsProtocolMethodDecl)1575f4a2713aSLionel Sambuc void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1576f4a2713aSLionel Sambuc                                        ObjCMethodDecl *Overridden,
1577f4a2713aSLionel Sambuc                                        bool IsProtocolMethodDecl) {
1578f4a2713aSLionel Sambuc 
1579f4a2713aSLionel Sambuc   CheckMethodOverrideReturn(*this, Method, Overridden,
1580f4a2713aSLionel Sambuc                             IsProtocolMethodDecl, true,
1581f4a2713aSLionel Sambuc                             true);
1582f4a2713aSLionel Sambuc 
1583f4a2713aSLionel Sambuc   for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
1584f4a2713aSLionel Sambuc        IF = Overridden->param_begin(), EM = Method->param_end(),
1585f4a2713aSLionel Sambuc        EF = Overridden->param_end();
1586f4a2713aSLionel Sambuc        IM != EM && IF != EF; ++IM, ++IF) {
1587f4a2713aSLionel Sambuc     CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1588f4a2713aSLionel Sambuc                              IsProtocolMethodDecl, true, true);
1589f4a2713aSLionel Sambuc   }
1590f4a2713aSLionel Sambuc 
1591f4a2713aSLionel Sambuc   if (Method->isVariadic() != Overridden->isVariadic()) {
1592f4a2713aSLionel Sambuc     Diag(Method->getLocation(),
1593f4a2713aSLionel Sambuc          diag::warn_conflicting_overriding_variadic);
1594f4a2713aSLionel Sambuc     Diag(Overridden->getLocation(), diag::note_previous_declaration);
1595f4a2713aSLionel Sambuc   }
1596f4a2713aSLionel Sambuc }
1597f4a2713aSLionel Sambuc 
1598f4a2713aSLionel Sambuc /// WarnExactTypedMethods - This routine issues a warning if method
1599f4a2713aSLionel Sambuc /// implementation declaration matches exactly that of its declaration.
WarnExactTypedMethods(ObjCMethodDecl * ImpMethodDecl,ObjCMethodDecl * MethodDecl,bool IsProtocolMethodDecl)1600f4a2713aSLionel Sambuc void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1601f4a2713aSLionel Sambuc                                  ObjCMethodDecl *MethodDecl,
1602f4a2713aSLionel Sambuc                                  bool IsProtocolMethodDecl) {
1603f4a2713aSLionel Sambuc   // don't issue warning when protocol method is optional because primary
1604f4a2713aSLionel Sambuc   // class is not required to implement it and it is safe for protocol
1605f4a2713aSLionel Sambuc   // to implement it.
1606f4a2713aSLionel Sambuc   if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1607f4a2713aSLionel Sambuc     return;
1608f4a2713aSLionel Sambuc   // don't issue warning when primary class's method is
1609f4a2713aSLionel Sambuc   // depecated/unavailable.
1610f4a2713aSLionel Sambuc   if (MethodDecl->hasAttr<UnavailableAttr>() ||
1611f4a2713aSLionel Sambuc       MethodDecl->hasAttr<DeprecatedAttr>())
1612f4a2713aSLionel Sambuc     return;
1613f4a2713aSLionel Sambuc 
1614f4a2713aSLionel Sambuc   bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1615f4a2713aSLionel Sambuc                                       IsProtocolMethodDecl, false, false);
1616f4a2713aSLionel Sambuc   if (match)
1617f4a2713aSLionel Sambuc     for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1618f4a2713aSLionel Sambuc          IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1619f4a2713aSLionel Sambuc          EF = MethodDecl->param_end();
1620f4a2713aSLionel Sambuc          IM != EM && IF != EF; ++IM, ++IF) {
1621f4a2713aSLionel Sambuc       match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1622f4a2713aSLionel Sambuc                                        *IM, *IF,
1623f4a2713aSLionel Sambuc                                        IsProtocolMethodDecl, false, false);
1624f4a2713aSLionel Sambuc       if (!match)
1625f4a2713aSLionel Sambuc         break;
1626f4a2713aSLionel Sambuc     }
1627f4a2713aSLionel Sambuc   if (match)
1628f4a2713aSLionel Sambuc     match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
1629f4a2713aSLionel Sambuc   if (match)
1630f4a2713aSLionel Sambuc     match = !(MethodDecl->isClassMethod() &&
1631f4a2713aSLionel Sambuc               MethodDecl->getSelector() == GetNullarySelector("load", Context));
1632f4a2713aSLionel Sambuc 
1633f4a2713aSLionel Sambuc   if (match) {
1634f4a2713aSLionel Sambuc     Diag(ImpMethodDecl->getLocation(),
1635f4a2713aSLionel Sambuc          diag::warn_category_method_impl_match);
1636f4a2713aSLionel Sambuc     Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1637f4a2713aSLionel Sambuc       << MethodDecl->getDeclName();
1638f4a2713aSLionel Sambuc   }
1639f4a2713aSLionel Sambuc }
1640f4a2713aSLionel Sambuc 
1641f4a2713aSLionel Sambuc /// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1642f4a2713aSLionel Sambuc /// improve the efficiency of selector lookups and type checking by associating
1643f4a2713aSLionel Sambuc /// with each protocol / interface / category the flattened instance tables. If
1644f4a2713aSLionel Sambuc /// we used an immutable set to keep the table then it wouldn't add significant
1645f4a2713aSLionel Sambuc /// memory cost and it would be handy for lookups.
1646f4a2713aSLionel Sambuc 
1647*0a6a1f1dSLionel Sambuc typedef llvm::DenseSet<IdentifierInfo*> ProtocolNameSet;
1648*0a6a1f1dSLionel Sambuc typedef std::unique_ptr<ProtocolNameSet> LazyProtocolNameSet;
1649*0a6a1f1dSLionel Sambuc 
findProtocolsWithExplicitImpls(const ObjCProtocolDecl * PDecl,ProtocolNameSet & PNS)1650*0a6a1f1dSLionel Sambuc static void findProtocolsWithExplicitImpls(const ObjCProtocolDecl *PDecl,
1651*0a6a1f1dSLionel Sambuc                                            ProtocolNameSet &PNS) {
1652*0a6a1f1dSLionel Sambuc   if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1653*0a6a1f1dSLionel Sambuc     PNS.insert(PDecl->getIdentifier());
1654*0a6a1f1dSLionel Sambuc   for (const auto *PI : PDecl->protocols())
1655*0a6a1f1dSLionel Sambuc     findProtocolsWithExplicitImpls(PI, PNS);
1656*0a6a1f1dSLionel Sambuc }
1657*0a6a1f1dSLionel Sambuc 
1658*0a6a1f1dSLionel Sambuc /// Recursively populates a set with all conformed protocols in a class
1659*0a6a1f1dSLionel Sambuc /// hierarchy that have the 'objc_protocol_requires_explicit_implementation'
1660*0a6a1f1dSLionel Sambuc /// attribute.
findProtocolsWithExplicitImpls(const ObjCInterfaceDecl * Super,ProtocolNameSet & PNS)1661*0a6a1f1dSLionel Sambuc static void findProtocolsWithExplicitImpls(const ObjCInterfaceDecl *Super,
1662*0a6a1f1dSLionel Sambuc                                            ProtocolNameSet &PNS) {
1663*0a6a1f1dSLionel Sambuc   if (!Super)
1664*0a6a1f1dSLionel Sambuc     return;
1665*0a6a1f1dSLionel Sambuc 
1666*0a6a1f1dSLionel Sambuc   for (const auto *I : Super->all_referenced_protocols())
1667*0a6a1f1dSLionel Sambuc     findProtocolsWithExplicitImpls(I, PNS);
1668*0a6a1f1dSLionel Sambuc 
1669*0a6a1f1dSLionel Sambuc   findProtocolsWithExplicitImpls(Super->getSuperClass(), PNS);
1670*0a6a1f1dSLionel Sambuc }
1671*0a6a1f1dSLionel Sambuc 
1672f4a2713aSLionel Sambuc /// CheckProtocolMethodDefs - This routine checks unimplemented methods
1673f4a2713aSLionel Sambuc /// Declared in protocol, and those referenced by it.
CheckProtocolMethodDefs(Sema & S,SourceLocation ImpLoc,ObjCProtocolDecl * PDecl,bool & IncompleteImpl,const Sema::SelectorSet & InsMap,const Sema::SelectorSet & ClsMap,ObjCContainerDecl * CDecl,LazyProtocolNameSet & ProtocolsExplictImpl)1674*0a6a1f1dSLionel Sambuc static void CheckProtocolMethodDefs(Sema &S,
1675*0a6a1f1dSLionel Sambuc                                     SourceLocation ImpLoc,
1676f4a2713aSLionel Sambuc                                     ObjCProtocolDecl *PDecl,
1677f4a2713aSLionel Sambuc                                     bool& IncompleteImpl,
1678*0a6a1f1dSLionel Sambuc                                     const Sema::SelectorSet &InsMap,
1679*0a6a1f1dSLionel Sambuc                                     const Sema::SelectorSet &ClsMap,
1680*0a6a1f1dSLionel Sambuc                                     ObjCContainerDecl *CDecl,
1681*0a6a1f1dSLionel Sambuc                                     LazyProtocolNameSet &ProtocolsExplictImpl) {
1682f4a2713aSLionel Sambuc   ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1683f4a2713aSLionel Sambuc   ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1684f4a2713aSLionel Sambuc                                : dyn_cast<ObjCInterfaceDecl>(CDecl);
1685f4a2713aSLionel Sambuc   assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1686f4a2713aSLionel Sambuc 
1687f4a2713aSLionel Sambuc   ObjCInterfaceDecl *Super = IDecl->getSuperClass();
1688*0a6a1f1dSLionel Sambuc   ObjCInterfaceDecl *NSIDecl = nullptr;
1689*0a6a1f1dSLionel Sambuc 
1690*0a6a1f1dSLionel Sambuc   // If this protocol is marked 'objc_protocol_requires_explicit_implementation'
1691*0a6a1f1dSLionel Sambuc   // then we should check if any class in the super class hierarchy also
1692*0a6a1f1dSLionel Sambuc   // conforms to this protocol, either directly or via protocol inheritance.
1693*0a6a1f1dSLionel Sambuc   // If so, we can skip checking this protocol completely because we
1694*0a6a1f1dSLionel Sambuc   // know that a parent class already satisfies this protocol.
1695*0a6a1f1dSLionel Sambuc   //
1696*0a6a1f1dSLionel Sambuc   // Note: we could generalize this logic for all protocols, and merely
1697*0a6a1f1dSLionel Sambuc   // add the limit on looking at the super class chain for just
1698*0a6a1f1dSLionel Sambuc   // specially marked protocols.  This may be a good optimization.  This
1699*0a6a1f1dSLionel Sambuc   // change is restricted to 'objc_protocol_requires_explicit_implementation'
1700*0a6a1f1dSLionel Sambuc   // protocols for now for controlled evaluation.
1701*0a6a1f1dSLionel Sambuc   if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) {
1702*0a6a1f1dSLionel Sambuc     if (!ProtocolsExplictImpl) {
1703*0a6a1f1dSLionel Sambuc       ProtocolsExplictImpl.reset(new ProtocolNameSet);
1704*0a6a1f1dSLionel Sambuc       findProtocolsWithExplicitImpls(Super, *ProtocolsExplictImpl);
1705*0a6a1f1dSLionel Sambuc     }
1706*0a6a1f1dSLionel Sambuc     if (ProtocolsExplictImpl->find(PDecl->getIdentifier()) !=
1707*0a6a1f1dSLionel Sambuc         ProtocolsExplictImpl->end())
1708*0a6a1f1dSLionel Sambuc       return;
1709*0a6a1f1dSLionel Sambuc 
1710*0a6a1f1dSLionel Sambuc     // If no super class conforms to the protocol, we should not search
1711*0a6a1f1dSLionel Sambuc     // for methods in the super class to implicitly satisfy the protocol.
1712*0a6a1f1dSLionel Sambuc     Super = nullptr;
1713*0a6a1f1dSLionel Sambuc   }
1714*0a6a1f1dSLionel Sambuc 
1715*0a6a1f1dSLionel Sambuc   if (S.getLangOpts().ObjCRuntime.isNeXTFamily()) {
1716f4a2713aSLionel Sambuc     // check to see if class implements forwardInvocation method and objects
1717f4a2713aSLionel Sambuc     // of this class are derived from 'NSProxy' so that to forward requests
1718f4a2713aSLionel Sambuc     // from one object to another.
1719f4a2713aSLionel Sambuc     // Under such conditions, which means that every method possible is
1720f4a2713aSLionel Sambuc     // implemented in the class, we should not issue "Method definition not
1721f4a2713aSLionel Sambuc     // found" warnings.
1722f4a2713aSLionel Sambuc     // FIXME: Use a general GetUnarySelector method for this.
1723*0a6a1f1dSLionel Sambuc     IdentifierInfo* II = &S.Context.Idents.get("forwardInvocation");
1724*0a6a1f1dSLionel Sambuc     Selector fISelector = S.Context.Selectors.getSelector(1, &II);
1725f4a2713aSLionel Sambuc     if (InsMap.count(fISelector))
1726f4a2713aSLionel Sambuc       // Is IDecl derived from 'NSProxy'? If so, no instance methods
1727f4a2713aSLionel Sambuc       // need be implemented in the implementation.
1728*0a6a1f1dSLionel Sambuc       NSIDecl = IDecl->lookupInheritedClass(&S.Context.Idents.get("NSProxy"));
1729f4a2713aSLionel Sambuc   }
1730f4a2713aSLionel Sambuc 
1731f4a2713aSLionel Sambuc   // If this is a forward protocol declaration, get its definition.
1732f4a2713aSLionel Sambuc   if (!PDecl->isThisDeclarationADefinition() &&
1733f4a2713aSLionel Sambuc       PDecl->getDefinition())
1734f4a2713aSLionel Sambuc     PDecl = PDecl->getDefinition();
1735f4a2713aSLionel Sambuc 
1736f4a2713aSLionel Sambuc   // If a method lookup fails locally we still need to look and see if
1737f4a2713aSLionel Sambuc   // the method was implemented by a base class or an inherited
1738f4a2713aSLionel Sambuc   // protocol. This lookup is slow, but occurs rarely in correct code
1739f4a2713aSLionel Sambuc   // and otherwise would terminate in a warning.
1740f4a2713aSLionel Sambuc 
1741f4a2713aSLionel Sambuc   // check unimplemented instance methods.
1742f4a2713aSLionel Sambuc   if (!NSIDecl)
1743*0a6a1f1dSLionel Sambuc     for (auto *method : PDecl->instance_methods()) {
1744f4a2713aSLionel Sambuc       if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1745f4a2713aSLionel Sambuc           !method->isPropertyAccessor() &&
1746f4a2713aSLionel Sambuc           !InsMap.count(method->getSelector()) &&
1747*0a6a1f1dSLionel Sambuc           (!Super || !Super->lookupMethod(method->getSelector(),
1748*0a6a1f1dSLionel Sambuc                                           true /* instance */,
1749*0a6a1f1dSLionel Sambuc                                           false /* shallowCategory */,
1750*0a6a1f1dSLionel Sambuc                                           true /* followsSuper */,
1751*0a6a1f1dSLionel Sambuc                                           nullptr /* category */))) {
1752f4a2713aSLionel Sambuc             // If a method is not implemented in the category implementation but
1753f4a2713aSLionel Sambuc             // has been declared in its primary class, superclass,
1754f4a2713aSLionel Sambuc             // or in one of their protocols, no need to issue the warning.
1755f4a2713aSLionel Sambuc             // This is because method will be implemented in the primary class
1756f4a2713aSLionel Sambuc             // or one of its super class implementation.
1757f4a2713aSLionel Sambuc 
1758f4a2713aSLionel Sambuc             // Ugly, but necessary. Method declared in protcol might have
1759f4a2713aSLionel Sambuc             // have been synthesized due to a property declared in the class which
1760f4a2713aSLionel Sambuc             // uses the protocol.
1761f4a2713aSLionel Sambuc             if (ObjCMethodDecl *MethodInClass =
1762*0a6a1f1dSLionel Sambuc                   IDecl->lookupMethod(method->getSelector(),
1763*0a6a1f1dSLionel Sambuc                                       true /* instance */,
1764*0a6a1f1dSLionel Sambuc                                       true /* shallowCategoryLookup */,
1765*0a6a1f1dSLionel Sambuc                                       false /* followSuper */))
1766f4a2713aSLionel Sambuc               if (C || MethodInClass->isPropertyAccessor())
1767f4a2713aSLionel Sambuc                 continue;
1768f4a2713aSLionel Sambuc             unsigned DIAG = diag::warn_unimplemented_protocol_method;
1769*0a6a1f1dSLionel Sambuc             if (!S.Diags.isIgnored(DIAG, ImpLoc)) {
1770*0a6a1f1dSLionel Sambuc               WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG,
1771*0a6a1f1dSLionel Sambuc                                   PDecl);
1772f4a2713aSLionel Sambuc             }
1773f4a2713aSLionel Sambuc           }
1774f4a2713aSLionel Sambuc     }
1775f4a2713aSLionel Sambuc   // check unimplemented class methods
1776*0a6a1f1dSLionel Sambuc   for (auto *method : PDecl->class_methods()) {
1777f4a2713aSLionel Sambuc     if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1778f4a2713aSLionel Sambuc         !ClsMap.count(method->getSelector()) &&
1779*0a6a1f1dSLionel Sambuc         (!Super || !Super->lookupMethod(method->getSelector(),
1780*0a6a1f1dSLionel Sambuc                                         false /* class method */,
1781*0a6a1f1dSLionel Sambuc                                         false /* shallowCategoryLookup */,
1782*0a6a1f1dSLionel Sambuc                                         true  /* followSuper */,
1783*0a6a1f1dSLionel Sambuc                                         nullptr /* category */))) {
1784f4a2713aSLionel Sambuc       // See above comment for instance method lookups.
1785*0a6a1f1dSLionel Sambuc       if (C && IDecl->lookupMethod(method->getSelector(),
1786*0a6a1f1dSLionel Sambuc                                    false /* class */,
1787*0a6a1f1dSLionel Sambuc                                    true /* shallowCategoryLookup */,
1788*0a6a1f1dSLionel Sambuc                                    false /* followSuper */))
1789f4a2713aSLionel Sambuc         continue;
1790*0a6a1f1dSLionel Sambuc 
1791f4a2713aSLionel Sambuc       unsigned DIAG = diag::warn_unimplemented_protocol_method;
1792*0a6a1f1dSLionel Sambuc       if (!S.Diags.isIgnored(DIAG, ImpLoc)) {
1793*0a6a1f1dSLionel Sambuc         WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, PDecl);
1794f4a2713aSLionel Sambuc       }
1795f4a2713aSLionel Sambuc     }
1796f4a2713aSLionel Sambuc   }
1797f4a2713aSLionel Sambuc   // Check on this protocols's referenced protocols, recursively.
1798*0a6a1f1dSLionel Sambuc   for (auto *PI : PDecl->protocols())
1799*0a6a1f1dSLionel Sambuc     CheckProtocolMethodDefs(S, ImpLoc, PI, IncompleteImpl, InsMap, ClsMap,
1800*0a6a1f1dSLionel Sambuc                             CDecl, ProtocolsExplictImpl);
1801f4a2713aSLionel Sambuc }
1802f4a2713aSLionel Sambuc 
1803f4a2713aSLionel Sambuc /// MatchAllMethodDeclarations - Check methods declared in interface
1804f4a2713aSLionel Sambuc /// or protocol against those declared in their implementations.
1805f4a2713aSLionel Sambuc ///
MatchAllMethodDeclarations(const SelectorSet & InsMap,const SelectorSet & ClsMap,SelectorSet & InsMapSeen,SelectorSet & ClsMapSeen,ObjCImplDecl * IMPDecl,ObjCContainerDecl * CDecl,bool & IncompleteImpl,bool ImmediateClass,bool WarnCategoryMethodImpl)1806f4a2713aSLionel Sambuc void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1807f4a2713aSLionel Sambuc                                       const SelectorSet &ClsMap,
1808f4a2713aSLionel Sambuc                                       SelectorSet &InsMapSeen,
1809f4a2713aSLionel Sambuc                                       SelectorSet &ClsMapSeen,
1810f4a2713aSLionel Sambuc                                       ObjCImplDecl* IMPDecl,
1811f4a2713aSLionel Sambuc                                       ObjCContainerDecl* CDecl,
1812f4a2713aSLionel Sambuc                                       bool &IncompleteImpl,
1813f4a2713aSLionel Sambuc                                       bool ImmediateClass,
1814f4a2713aSLionel Sambuc                                       bool WarnCategoryMethodImpl) {
1815f4a2713aSLionel Sambuc   // Check and see if instance methods in class interface have been
1816f4a2713aSLionel Sambuc   // implemented in the implementation class. If so, their types match.
1817*0a6a1f1dSLionel Sambuc   for (auto *I : CDecl->instance_methods()) {
1818*0a6a1f1dSLionel Sambuc     if (!InsMapSeen.insert(I->getSelector()).second)
1819f4a2713aSLionel Sambuc       continue;
1820*0a6a1f1dSLionel Sambuc     if (!I->isPropertyAccessor() &&
1821*0a6a1f1dSLionel Sambuc         !InsMap.count(I->getSelector())) {
1822f4a2713aSLionel Sambuc       if (ImmediateClass)
1823*0a6a1f1dSLionel Sambuc         WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl,
1824f4a2713aSLionel Sambuc                             diag::warn_undef_method_impl);
1825f4a2713aSLionel Sambuc       continue;
1826f4a2713aSLionel Sambuc     } else {
1827f4a2713aSLionel Sambuc       ObjCMethodDecl *ImpMethodDecl =
1828*0a6a1f1dSLionel Sambuc         IMPDecl->getInstanceMethod(I->getSelector());
1829*0a6a1f1dSLionel Sambuc       assert(CDecl->getInstanceMethod(I->getSelector()) &&
1830f4a2713aSLionel Sambuc              "Expected to find the method through lookup as well");
1831f4a2713aSLionel Sambuc       // ImpMethodDecl may be null as in a @dynamic property.
1832f4a2713aSLionel Sambuc       if (ImpMethodDecl) {
1833f4a2713aSLionel Sambuc         if (!WarnCategoryMethodImpl)
1834*0a6a1f1dSLionel Sambuc           WarnConflictingTypedMethods(ImpMethodDecl, I,
1835f4a2713aSLionel Sambuc                                       isa<ObjCProtocolDecl>(CDecl));
1836*0a6a1f1dSLionel Sambuc         else if (!I->isPropertyAccessor())
1837*0a6a1f1dSLionel Sambuc           WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl));
1838f4a2713aSLionel Sambuc       }
1839f4a2713aSLionel Sambuc     }
1840f4a2713aSLionel Sambuc   }
1841f4a2713aSLionel Sambuc 
1842f4a2713aSLionel Sambuc   // Check and see if class methods in class interface have been
1843f4a2713aSLionel Sambuc   // implemented in the implementation class. If so, their types match.
1844*0a6a1f1dSLionel Sambuc   for (auto *I : CDecl->class_methods()) {
1845*0a6a1f1dSLionel Sambuc     if (!ClsMapSeen.insert(I->getSelector()).second)
1846f4a2713aSLionel Sambuc       continue;
1847*0a6a1f1dSLionel Sambuc     if (!ClsMap.count(I->getSelector())) {
1848f4a2713aSLionel Sambuc       if (ImmediateClass)
1849*0a6a1f1dSLionel Sambuc         WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl,
1850f4a2713aSLionel Sambuc                             diag::warn_undef_method_impl);
1851f4a2713aSLionel Sambuc     } else {
1852f4a2713aSLionel Sambuc       ObjCMethodDecl *ImpMethodDecl =
1853*0a6a1f1dSLionel Sambuc         IMPDecl->getClassMethod(I->getSelector());
1854*0a6a1f1dSLionel Sambuc       assert(CDecl->getClassMethod(I->getSelector()) &&
1855f4a2713aSLionel Sambuc              "Expected to find the method through lookup as well");
1856f4a2713aSLionel Sambuc       if (!WarnCategoryMethodImpl)
1857*0a6a1f1dSLionel Sambuc         WarnConflictingTypedMethods(ImpMethodDecl, I,
1858f4a2713aSLionel Sambuc                                     isa<ObjCProtocolDecl>(CDecl));
1859f4a2713aSLionel Sambuc       else
1860*0a6a1f1dSLionel Sambuc         WarnExactTypedMethods(ImpMethodDecl, I,
1861f4a2713aSLionel Sambuc                               isa<ObjCProtocolDecl>(CDecl));
1862f4a2713aSLionel Sambuc     }
1863f4a2713aSLionel Sambuc   }
1864f4a2713aSLionel Sambuc 
1865f4a2713aSLionel Sambuc   if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) {
1866f4a2713aSLionel Sambuc     // Also, check for methods declared in protocols inherited by
1867f4a2713aSLionel Sambuc     // this protocol.
1868*0a6a1f1dSLionel Sambuc     for (auto *PI : PD->protocols())
1869f4a2713aSLionel Sambuc       MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1870*0a6a1f1dSLionel Sambuc                                  IMPDecl, PI, IncompleteImpl, false,
1871f4a2713aSLionel Sambuc                                  WarnCategoryMethodImpl);
1872f4a2713aSLionel Sambuc   }
1873f4a2713aSLionel Sambuc 
1874f4a2713aSLionel Sambuc   if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1875f4a2713aSLionel Sambuc     // when checking that methods in implementation match their declaration,
1876f4a2713aSLionel Sambuc     // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1877f4a2713aSLionel Sambuc     // extension; as well as those in categories.
1878f4a2713aSLionel Sambuc     if (!WarnCategoryMethodImpl) {
1879*0a6a1f1dSLionel Sambuc       for (auto *Cat : I->visible_categories())
1880f4a2713aSLionel Sambuc         MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1881*0a6a1f1dSLionel Sambuc                                    IMPDecl, Cat, IncompleteImpl, false,
1882f4a2713aSLionel Sambuc                                    WarnCategoryMethodImpl);
1883f4a2713aSLionel Sambuc     } else {
1884f4a2713aSLionel Sambuc       // Also methods in class extensions need be looked at next.
1885*0a6a1f1dSLionel Sambuc       for (auto *Ext : I->visible_extensions())
1886f4a2713aSLionel Sambuc         MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1887*0a6a1f1dSLionel Sambuc                                    IMPDecl, Ext, IncompleteImpl, false,
1888f4a2713aSLionel Sambuc                                    WarnCategoryMethodImpl);
1889f4a2713aSLionel Sambuc     }
1890f4a2713aSLionel Sambuc 
1891f4a2713aSLionel Sambuc     // Check for any implementation of a methods declared in protocol.
1892*0a6a1f1dSLionel Sambuc     for (auto *PI : I->all_referenced_protocols())
1893f4a2713aSLionel Sambuc       MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1894*0a6a1f1dSLionel Sambuc                                  IMPDecl, PI, IncompleteImpl, false,
1895f4a2713aSLionel Sambuc                                  WarnCategoryMethodImpl);
1896f4a2713aSLionel Sambuc 
1897f4a2713aSLionel Sambuc     // FIXME. For now, we are not checking for extact match of methods
1898f4a2713aSLionel Sambuc     // in category implementation and its primary class's super class.
1899f4a2713aSLionel Sambuc     if (!WarnCategoryMethodImpl && I->getSuperClass())
1900f4a2713aSLionel Sambuc       MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1901f4a2713aSLionel Sambuc                                  IMPDecl,
1902f4a2713aSLionel Sambuc                                  I->getSuperClass(), IncompleteImpl, false);
1903f4a2713aSLionel Sambuc   }
1904f4a2713aSLionel Sambuc }
1905f4a2713aSLionel Sambuc 
1906f4a2713aSLionel Sambuc /// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1907f4a2713aSLionel Sambuc /// category matches with those implemented in its primary class and
1908f4a2713aSLionel Sambuc /// warns each time an exact match is found.
CheckCategoryVsClassMethodMatches(ObjCCategoryImplDecl * CatIMPDecl)1909f4a2713aSLionel Sambuc void Sema::CheckCategoryVsClassMethodMatches(
1910f4a2713aSLionel Sambuc                                   ObjCCategoryImplDecl *CatIMPDecl) {
1911f4a2713aSLionel Sambuc   // Get category's primary class.
1912f4a2713aSLionel Sambuc   ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1913f4a2713aSLionel Sambuc   if (!CatDecl)
1914f4a2713aSLionel Sambuc     return;
1915f4a2713aSLionel Sambuc   ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1916f4a2713aSLionel Sambuc   if (!IDecl)
1917f4a2713aSLionel Sambuc     return;
1918*0a6a1f1dSLionel Sambuc   ObjCInterfaceDecl *SuperIDecl = IDecl->getSuperClass();
1919*0a6a1f1dSLionel Sambuc   SelectorSet InsMap, ClsMap;
1920*0a6a1f1dSLionel Sambuc 
1921*0a6a1f1dSLionel Sambuc   for (const auto *I : CatIMPDecl->instance_methods()) {
1922*0a6a1f1dSLionel Sambuc     Selector Sel = I->getSelector();
1923*0a6a1f1dSLionel Sambuc     // When checking for methods implemented in the category, skip over
1924*0a6a1f1dSLionel Sambuc     // those declared in category class's super class. This is because
1925*0a6a1f1dSLionel Sambuc     // the super class must implement the method.
1926*0a6a1f1dSLionel Sambuc     if (SuperIDecl && SuperIDecl->lookupMethod(Sel, true))
1927*0a6a1f1dSLionel Sambuc       continue;
1928*0a6a1f1dSLionel Sambuc     InsMap.insert(Sel);
1929*0a6a1f1dSLionel Sambuc   }
1930*0a6a1f1dSLionel Sambuc 
1931*0a6a1f1dSLionel Sambuc   for (const auto *I : CatIMPDecl->class_methods()) {
1932*0a6a1f1dSLionel Sambuc     Selector Sel = I->getSelector();
1933*0a6a1f1dSLionel Sambuc     if (SuperIDecl && SuperIDecl->lookupMethod(Sel, false))
1934*0a6a1f1dSLionel Sambuc       continue;
1935*0a6a1f1dSLionel Sambuc     ClsMap.insert(Sel);
1936*0a6a1f1dSLionel Sambuc   }
1937*0a6a1f1dSLionel Sambuc   if (InsMap.empty() && ClsMap.empty())
1938*0a6a1f1dSLionel Sambuc     return;
1939*0a6a1f1dSLionel Sambuc 
1940f4a2713aSLionel Sambuc   SelectorSet InsMapSeen, ClsMapSeen;
1941f4a2713aSLionel Sambuc   bool IncompleteImpl = false;
1942f4a2713aSLionel Sambuc   MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1943f4a2713aSLionel Sambuc                              CatIMPDecl, IDecl,
1944f4a2713aSLionel Sambuc                              IncompleteImpl, false,
1945f4a2713aSLionel Sambuc                              true /*WarnCategoryMethodImpl*/);
1946f4a2713aSLionel Sambuc }
1947f4a2713aSLionel Sambuc 
ImplMethodsVsClassMethods(Scope * S,ObjCImplDecl * IMPDecl,ObjCContainerDecl * CDecl,bool IncompleteImpl)1948f4a2713aSLionel Sambuc void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
1949f4a2713aSLionel Sambuc                                      ObjCContainerDecl* CDecl,
1950f4a2713aSLionel Sambuc                                      bool IncompleteImpl) {
1951f4a2713aSLionel Sambuc   SelectorSet InsMap;
1952f4a2713aSLionel Sambuc   // Check and see if instance methods in class interface have been
1953f4a2713aSLionel Sambuc   // implemented in the implementation class.
1954*0a6a1f1dSLionel Sambuc   for (const auto *I : IMPDecl->instance_methods())
1955*0a6a1f1dSLionel Sambuc     InsMap.insert(I->getSelector());
1956f4a2713aSLionel Sambuc 
1957f4a2713aSLionel Sambuc   // Check and see if properties declared in the interface have either 1)
1958f4a2713aSLionel Sambuc   // an implementation or 2) there is a @synthesize/@dynamic implementation
1959f4a2713aSLionel Sambuc   // of the property in the @implementation.
1960*0a6a1f1dSLionel Sambuc   if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1961*0a6a1f1dSLionel Sambuc     bool SynthesizeProperties = LangOpts.ObjCDefaultSynthProperties &&
1962*0a6a1f1dSLionel Sambuc                                 LangOpts.ObjCRuntime.isNonFragile() &&
1963*0a6a1f1dSLionel Sambuc                                 !IDecl->isObjCRequiresPropertyDefs();
1964*0a6a1f1dSLionel Sambuc     DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, SynthesizeProperties);
1965*0a6a1f1dSLionel Sambuc   }
1966f4a2713aSLionel Sambuc 
1967f4a2713aSLionel Sambuc   SelectorSet ClsMap;
1968*0a6a1f1dSLionel Sambuc   for (const auto *I : IMPDecl->class_methods())
1969*0a6a1f1dSLionel Sambuc     ClsMap.insert(I->getSelector());
1970f4a2713aSLionel Sambuc 
1971f4a2713aSLionel Sambuc   // Check for type conflict of methods declared in a class/protocol and
1972f4a2713aSLionel Sambuc   // its implementation; if any.
1973f4a2713aSLionel Sambuc   SelectorSet InsMapSeen, ClsMapSeen;
1974f4a2713aSLionel Sambuc   MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1975f4a2713aSLionel Sambuc                              IMPDecl, CDecl,
1976f4a2713aSLionel Sambuc                              IncompleteImpl, true);
1977f4a2713aSLionel Sambuc 
1978f4a2713aSLionel Sambuc   // check all methods implemented in category against those declared
1979f4a2713aSLionel Sambuc   // in its primary class.
1980f4a2713aSLionel Sambuc   if (ObjCCategoryImplDecl *CatDecl =
1981f4a2713aSLionel Sambuc         dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1982f4a2713aSLionel Sambuc     CheckCategoryVsClassMethodMatches(CatDecl);
1983f4a2713aSLionel Sambuc 
1984f4a2713aSLionel Sambuc   // Check the protocol list for unimplemented methods in the @implementation
1985f4a2713aSLionel Sambuc   // class.
1986f4a2713aSLionel Sambuc   // Check and see if class methods in class interface have been
1987f4a2713aSLionel Sambuc   // implemented in the implementation class.
1988f4a2713aSLionel Sambuc 
1989*0a6a1f1dSLionel Sambuc   LazyProtocolNameSet ExplicitImplProtocols;
1990*0a6a1f1dSLionel Sambuc 
1991f4a2713aSLionel Sambuc   if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1992*0a6a1f1dSLionel Sambuc     for (auto *PI : I->all_referenced_protocols())
1993*0a6a1f1dSLionel Sambuc       CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), PI, IncompleteImpl,
1994*0a6a1f1dSLionel Sambuc                               InsMap, ClsMap, I, ExplicitImplProtocols);
1995f4a2713aSLionel Sambuc     // Check class extensions (unnamed categories)
1996*0a6a1f1dSLionel Sambuc     for (auto *Ext : I->visible_extensions())
1997*0a6a1f1dSLionel Sambuc       ImplMethodsVsClassMethods(S, IMPDecl, Ext, IncompleteImpl);
1998f4a2713aSLionel Sambuc   } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1999f4a2713aSLionel Sambuc     // For extended class, unimplemented methods in its protocols will
2000f4a2713aSLionel Sambuc     // be reported in the primary class.
2001f4a2713aSLionel Sambuc     if (!C->IsClassExtension()) {
2002*0a6a1f1dSLionel Sambuc       for (auto *P : C->protocols())
2003*0a6a1f1dSLionel Sambuc         CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), P,
2004*0a6a1f1dSLionel Sambuc                                 IncompleteImpl, InsMap, ClsMap, CDecl,
2005*0a6a1f1dSLionel Sambuc                                 ExplicitImplProtocols);
2006*0a6a1f1dSLionel Sambuc       DiagnoseUnimplementedProperties(S, IMPDecl, CDecl,
2007*0a6a1f1dSLionel Sambuc                                       /*SynthesizeProperties=*/false);
2008f4a2713aSLionel Sambuc     }
2009f4a2713aSLionel Sambuc   } else
2010f4a2713aSLionel Sambuc     llvm_unreachable("invalid ObjCContainerDecl type.");
2011f4a2713aSLionel Sambuc }
2012f4a2713aSLionel Sambuc 
2013f4a2713aSLionel Sambuc Sema::DeclGroupPtrTy
ActOnForwardClassDeclaration(SourceLocation AtClassLoc,IdentifierInfo ** IdentList,SourceLocation * IdentLocs,unsigned NumElts)2014f4a2713aSLionel Sambuc Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
2015f4a2713aSLionel Sambuc                                    IdentifierInfo **IdentList,
2016f4a2713aSLionel Sambuc                                    SourceLocation *IdentLocs,
2017f4a2713aSLionel Sambuc                                    unsigned NumElts) {
2018f4a2713aSLionel Sambuc   SmallVector<Decl *, 8> DeclsInGroup;
2019f4a2713aSLionel Sambuc   for (unsigned i = 0; i != NumElts; ++i) {
2020f4a2713aSLionel Sambuc     // Check for another declaration kind with the same name.
2021f4a2713aSLionel Sambuc     NamedDecl *PrevDecl
2022f4a2713aSLionel Sambuc       = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
2023f4a2713aSLionel Sambuc                          LookupOrdinaryName, ForRedeclaration);
2024f4a2713aSLionel Sambuc     if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
2025f4a2713aSLionel Sambuc       // GCC apparently allows the following idiom:
2026f4a2713aSLionel Sambuc       //
2027f4a2713aSLionel Sambuc       // typedef NSObject < XCElementTogglerP > XCElementToggler;
2028f4a2713aSLionel Sambuc       // @class XCElementToggler;
2029f4a2713aSLionel Sambuc       //
2030f4a2713aSLionel Sambuc       // Here we have chosen to ignore the forward class declaration
2031f4a2713aSLionel Sambuc       // with a warning. Since this is the implied behavior.
2032f4a2713aSLionel Sambuc       TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
2033f4a2713aSLionel Sambuc       if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
2034f4a2713aSLionel Sambuc         Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
2035f4a2713aSLionel Sambuc         Diag(PrevDecl->getLocation(), diag::note_previous_definition);
2036f4a2713aSLionel Sambuc       } else {
2037f4a2713aSLionel Sambuc         // a forward class declaration matching a typedef name of a class refers
2038f4a2713aSLionel Sambuc         // to the underlying class. Just ignore the forward class with a warning
2039*0a6a1f1dSLionel Sambuc         // as this will force the intended behavior which is to lookup the
2040*0a6a1f1dSLionel Sambuc         // typedef name.
2041f4a2713aSLionel Sambuc         if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
2042*0a6a1f1dSLionel Sambuc           Diag(AtClassLoc, diag::warn_forward_class_redefinition)
2043*0a6a1f1dSLionel Sambuc               << IdentList[i];
2044f4a2713aSLionel Sambuc           Diag(PrevDecl->getLocation(), diag::note_previous_definition);
2045f4a2713aSLionel Sambuc           continue;
2046f4a2713aSLionel Sambuc         }
2047f4a2713aSLionel Sambuc       }
2048f4a2713aSLionel Sambuc     }
2049f4a2713aSLionel Sambuc 
2050f4a2713aSLionel Sambuc     // Create a declaration to describe this forward declaration.
2051f4a2713aSLionel Sambuc     ObjCInterfaceDecl *PrevIDecl
2052f4a2713aSLionel Sambuc       = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
2053f4a2713aSLionel Sambuc 
2054f4a2713aSLionel Sambuc     IdentifierInfo *ClassName = IdentList[i];
2055f4a2713aSLionel Sambuc     if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
2056f4a2713aSLionel Sambuc       // A previous decl with a different name is because of
2057f4a2713aSLionel Sambuc       // @compatibility_alias, for example:
2058f4a2713aSLionel Sambuc       // \code
2059f4a2713aSLionel Sambuc       //   @class NewImage;
2060f4a2713aSLionel Sambuc       //   @compatibility_alias OldImage NewImage;
2061f4a2713aSLionel Sambuc       // \endcode
2062f4a2713aSLionel Sambuc       // A lookup for 'OldImage' will return the 'NewImage' decl.
2063f4a2713aSLionel Sambuc       //
2064f4a2713aSLionel Sambuc       // In such a case use the real declaration name, instead of the alias one,
2065f4a2713aSLionel Sambuc       // otherwise we will break IdentifierResolver and redecls-chain invariants.
2066f4a2713aSLionel Sambuc       // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
2067f4a2713aSLionel Sambuc       // has been aliased.
2068f4a2713aSLionel Sambuc       ClassName = PrevIDecl->getIdentifier();
2069f4a2713aSLionel Sambuc     }
2070f4a2713aSLionel Sambuc 
2071f4a2713aSLionel Sambuc     ObjCInterfaceDecl *IDecl
2072f4a2713aSLionel Sambuc       = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
2073f4a2713aSLionel Sambuc                                   ClassName, PrevIDecl, IdentLocs[i]);
2074f4a2713aSLionel Sambuc     IDecl->setAtEndRange(IdentLocs[i]);
2075f4a2713aSLionel Sambuc 
2076f4a2713aSLionel Sambuc     PushOnScopeChains(IDecl, TUScope);
2077f4a2713aSLionel Sambuc     CheckObjCDeclScope(IDecl);
2078f4a2713aSLionel Sambuc     DeclsInGroup.push_back(IDecl);
2079f4a2713aSLionel Sambuc   }
2080f4a2713aSLionel Sambuc 
2081f4a2713aSLionel Sambuc   return BuildDeclaratorGroup(DeclsInGroup, false);
2082f4a2713aSLionel Sambuc }
2083f4a2713aSLionel Sambuc 
2084f4a2713aSLionel Sambuc static bool tryMatchRecordTypes(ASTContext &Context,
2085f4a2713aSLionel Sambuc                                 Sema::MethodMatchStrategy strategy,
2086f4a2713aSLionel Sambuc                                 const Type *left, const Type *right);
2087f4a2713aSLionel Sambuc 
matchTypes(ASTContext & Context,Sema::MethodMatchStrategy strategy,QualType leftQT,QualType rightQT)2088f4a2713aSLionel Sambuc static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
2089f4a2713aSLionel Sambuc                        QualType leftQT, QualType rightQT) {
2090f4a2713aSLionel Sambuc   const Type *left =
2091f4a2713aSLionel Sambuc     Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
2092f4a2713aSLionel Sambuc   const Type *right =
2093f4a2713aSLionel Sambuc     Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
2094f4a2713aSLionel Sambuc 
2095f4a2713aSLionel Sambuc   if (left == right) return true;
2096f4a2713aSLionel Sambuc 
2097f4a2713aSLionel Sambuc   // If we're doing a strict match, the types have to match exactly.
2098f4a2713aSLionel Sambuc   if (strategy == Sema::MMS_strict) return false;
2099f4a2713aSLionel Sambuc 
2100f4a2713aSLionel Sambuc   if (left->isIncompleteType() || right->isIncompleteType()) return false;
2101f4a2713aSLionel Sambuc 
2102f4a2713aSLionel Sambuc   // Otherwise, use this absurdly complicated algorithm to try to
2103f4a2713aSLionel Sambuc   // validate the basic, low-level compatibility of the two types.
2104f4a2713aSLionel Sambuc 
2105f4a2713aSLionel Sambuc   // As a minimum, require the sizes and alignments to match.
2106*0a6a1f1dSLionel Sambuc   TypeInfo LeftTI = Context.getTypeInfo(left);
2107*0a6a1f1dSLionel Sambuc   TypeInfo RightTI = Context.getTypeInfo(right);
2108*0a6a1f1dSLionel Sambuc   if (LeftTI.Width != RightTI.Width)
2109*0a6a1f1dSLionel Sambuc     return false;
2110*0a6a1f1dSLionel Sambuc 
2111*0a6a1f1dSLionel Sambuc   if (LeftTI.Align != RightTI.Align)
2112f4a2713aSLionel Sambuc     return false;
2113f4a2713aSLionel Sambuc 
2114f4a2713aSLionel Sambuc   // Consider all the kinds of non-dependent canonical types:
2115f4a2713aSLionel Sambuc   // - functions and arrays aren't possible as return and parameter types
2116f4a2713aSLionel Sambuc 
2117f4a2713aSLionel Sambuc   // - vector types of equal size can be arbitrarily mixed
2118f4a2713aSLionel Sambuc   if (isa<VectorType>(left)) return isa<VectorType>(right);
2119f4a2713aSLionel Sambuc   if (isa<VectorType>(right)) return false;
2120f4a2713aSLionel Sambuc 
2121f4a2713aSLionel Sambuc   // - references should only match references of identical type
2122f4a2713aSLionel Sambuc   // - structs, unions, and Objective-C objects must match more-or-less
2123f4a2713aSLionel Sambuc   //   exactly
2124f4a2713aSLionel Sambuc   // - everything else should be a scalar
2125f4a2713aSLionel Sambuc   if (!left->isScalarType() || !right->isScalarType())
2126f4a2713aSLionel Sambuc     return tryMatchRecordTypes(Context, strategy, left, right);
2127f4a2713aSLionel Sambuc 
2128f4a2713aSLionel Sambuc   // Make scalars agree in kind, except count bools as chars, and group
2129f4a2713aSLionel Sambuc   // all non-member pointers together.
2130f4a2713aSLionel Sambuc   Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
2131f4a2713aSLionel Sambuc   Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
2132f4a2713aSLionel Sambuc   if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
2133f4a2713aSLionel Sambuc   if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
2134f4a2713aSLionel Sambuc   if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
2135f4a2713aSLionel Sambuc     leftSK = Type::STK_ObjCObjectPointer;
2136f4a2713aSLionel Sambuc   if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
2137f4a2713aSLionel Sambuc     rightSK = Type::STK_ObjCObjectPointer;
2138f4a2713aSLionel Sambuc 
2139f4a2713aSLionel Sambuc   // Note that data member pointers and function member pointers don't
2140f4a2713aSLionel Sambuc   // intermix because of the size differences.
2141f4a2713aSLionel Sambuc 
2142f4a2713aSLionel Sambuc   return (leftSK == rightSK);
2143f4a2713aSLionel Sambuc }
2144f4a2713aSLionel Sambuc 
tryMatchRecordTypes(ASTContext & Context,Sema::MethodMatchStrategy strategy,const Type * lt,const Type * rt)2145f4a2713aSLionel Sambuc static bool tryMatchRecordTypes(ASTContext &Context,
2146f4a2713aSLionel Sambuc                                 Sema::MethodMatchStrategy strategy,
2147f4a2713aSLionel Sambuc                                 const Type *lt, const Type *rt) {
2148f4a2713aSLionel Sambuc   assert(lt && rt && lt != rt);
2149f4a2713aSLionel Sambuc 
2150f4a2713aSLionel Sambuc   if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2151f4a2713aSLionel Sambuc   RecordDecl *left = cast<RecordType>(lt)->getDecl();
2152f4a2713aSLionel Sambuc   RecordDecl *right = cast<RecordType>(rt)->getDecl();
2153f4a2713aSLionel Sambuc 
2154f4a2713aSLionel Sambuc   // Require union-hood to match.
2155f4a2713aSLionel Sambuc   if (left->isUnion() != right->isUnion()) return false;
2156f4a2713aSLionel Sambuc 
2157f4a2713aSLionel Sambuc   // Require an exact match if either is non-POD.
2158f4a2713aSLionel Sambuc   if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2159f4a2713aSLionel Sambuc       (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2160f4a2713aSLionel Sambuc     return false;
2161f4a2713aSLionel Sambuc 
2162f4a2713aSLionel Sambuc   // Require size and alignment to match.
2163*0a6a1f1dSLionel Sambuc   TypeInfo LeftTI = Context.getTypeInfo(lt);
2164*0a6a1f1dSLionel Sambuc   TypeInfo RightTI = Context.getTypeInfo(rt);
2165*0a6a1f1dSLionel Sambuc   if (LeftTI.Width != RightTI.Width)
2166*0a6a1f1dSLionel Sambuc     return false;
2167*0a6a1f1dSLionel Sambuc 
2168*0a6a1f1dSLionel Sambuc   if (LeftTI.Align != RightTI.Align)
2169*0a6a1f1dSLionel Sambuc     return false;
2170f4a2713aSLionel Sambuc 
2171f4a2713aSLionel Sambuc   // Require fields to match.
2172f4a2713aSLionel Sambuc   RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2173f4a2713aSLionel Sambuc   RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2174f4a2713aSLionel Sambuc   for (; li != le && ri != re; ++li, ++ri) {
2175f4a2713aSLionel Sambuc     if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2176f4a2713aSLionel Sambuc       return false;
2177f4a2713aSLionel Sambuc   }
2178f4a2713aSLionel Sambuc   return (li == le && ri == re);
2179f4a2713aSLionel Sambuc }
2180f4a2713aSLionel Sambuc 
2181f4a2713aSLionel Sambuc /// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2182f4a2713aSLionel Sambuc /// returns true, or false, accordingly.
2183f4a2713aSLionel Sambuc /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
MatchTwoMethodDeclarations(const ObjCMethodDecl * left,const ObjCMethodDecl * right,MethodMatchStrategy strategy)2184f4a2713aSLionel Sambuc bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2185f4a2713aSLionel Sambuc                                       const ObjCMethodDecl *right,
2186f4a2713aSLionel Sambuc                                       MethodMatchStrategy strategy) {
2187*0a6a1f1dSLionel Sambuc   if (!matchTypes(Context, strategy, left->getReturnType(),
2188*0a6a1f1dSLionel Sambuc                   right->getReturnType()))
2189f4a2713aSLionel Sambuc     return false;
2190f4a2713aSLionel Sambuc 
2191f4a2713aSLionel Sambuc   // If either is hidden, it is not considered to match.
2192f4a2713aSLionel Sambuc   if (left->isHidden() || right->isHidden())
2193f4a2713aSLionel Sambuc     return false;
2194f4a2713aSLionel Sambuc 
2195f4a2713aSLionel Sambuc   if (getLangOpts().ObjCAutoRefCount &&
2196f4a2713aSLionel Sambuc       (left->hasAttr<NSReturnsRetainedAttr>()
2197f4a2713aSLionel Sambuc          != right->hasAttr<NSReturnsRetainedAttr>() ||
2198f4a2713aSLionel Sambuc        left->hasAttr<NSConsumesSelfAttr>()
2199f4a2713aSLionel Sambuc          != right->hasAttr<NSConsumesSelfAttr>()))
2200f4a2713aSLionel Sambuc     return false;
2201f4a2713aSLionel Sambuc 
2202f4a2713aSLionel Sambuc   ObjCMethodDecl::param_const_iterator
2203f4a2713aSLionel Sambuc     li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2204f4a2713aSLionel Sambuc     re = right->param_end();
2205f4a2713aSLionel Sambuc 
2206f4a2713aSLionel Sambuc   for (; li != le && ri != re; ++li, ++ri) {
2207f4a2713aSLionel Sambuc     assert(ri != right->param_end() && "Param mismatch");
2208f4a2713aSLionel Sambuc     const ParmVarDecl *lparm = *li, *rparm = *ri;
2209f4a2713aSLionel Sambuc 
2210f4a2713aSLionel Sambuc     if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2211f4a2713aSLionel Sambuc       return false;
2212f4a2713aSLionel Sambuc 
2213f4a2713aSLionel Sambuc     if (getLangOpts().ObjCAutoRefCount &&
2214f4a2713aSLionel Sambuc         lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2215f4a2713aSLionel Sambuc       return false;
2216f4a2713aSLionel Sambuc   }
2217f4a2713aSLionel Sambuc   return true;
2218f4a2713aSLionel Sambuc }
2219f4a2713aSLionel Sambuc 
addMethodToGlobalList(ObjCMethodList * List,ObjCMethodDecl * Method)2220*0a6a1f1dSLionel Sambuc void Sema::addMethodToGlobalList(ObjCMethodList *List,
2221*0a6a1f1dSLionel Sambuc                                  ObjCMethodDecl *Method) {
2222f4a2713aSLionel Sambuc   // Record at the head of the list whether there were 0, 1, or >= 2 methods
2223f4a2713aSLionel Sambuc   // inside categories.
2224*0a6a1f1dSLionel Sambuc   if (ObjCCategoryDecl *CD =
2225*0a6a1f1dSLionel Sambuc           dyn_cast<ObjCCategoryDecl>(Method->getDeclContext()))
2226f4a2713aSLionel Sambuc     if (!CD->IsClassExtension() && List->getBits() < 2)
2227f4a2713aSLionel Sambuc       List->setBits(List->getBits() + 1);
2228f4a2713aSLionel Sambuc 
2229f4a2713aSLionel Sambuc   // If the list is empty, make it a singleton list.
2230*0a6a1f1dSLionel Sambuc   if (List->getMethod() == nullptr) {
2231*0a6a1f1dSLionel Sambuc     List->setMethod(Method);
2232*0a6a1f1dSLionel Sambuc     List->setNext(nullptr);
2233f4a2713aSLionel Sambuc     return;
2234f4a2713aSLionel Sambuc   }
2235f4a2713aSLionel Sambuc 
2236f4a2713aSLionel Sambuc   // We've seen a method with this name, see if we have already seen this type
2237f4a2713aSLionel Sambuc   // signature.
2238f4a2713aSLionel Sambuc   ObjCMethodList *Previous = List;
2239f4a2713aSLionel Sambuc   for (; List; Previous = List, List = List->getNext()) {
2240f4a2713aSLionel Sambuc     // If we are building a module, keep all of the methods.
2241f4a2713aSLionel Sambuc     if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty())
2242f4a2713aSLionel Sambuc       continue;
2243f4a2713aSLionel Sambuc 
2244*0a6a1f1dSLionel Sambuc     if (!MatchTwoMethodDeclarations(Method, List->getMethod()))
2245f4a2713aSLionel Sambuc       continue;
2246f4a2713aSLionel Sambuc 
2247*0a6a1f1dSLionel Sambuc     ObjCMethodDecl *PrevObjCMethod = List->getMethod();
2248f4a2713aSLionel Sambuc 
2249f4a2713aSLionel Sambuc     // Propagate the 'defined' bit.
2250f4a2713aSLionel Sambuc     if (Method->isDefined())
2251f4a2713aSLionel Sambuc       PrevObjCMethod->setDefined(true);
2252*0a6a1f1dSLionel Sambuc     else {
2253*0a6a1f1dSLionel Sambuc       // Objective-C doesn't allow an @interface for a class after its
2254*0a6a1f1dSLionel Sambuc       // @implementation. So if Method is not defined and there already is
2255*0a6a1f1dSLionel Sambuc       // an entry for this type signature, Method has to be for a different
2256*0a6a1f1dSLionel Sambuc       // class than PrevObjCMethod.
2257*0a6a1f1dSLionel Sambuc       List->setHasMoreThanOneDecl(true);
2258*0a6a1f1dSLionel Sambuc     }
2259f4a2713aSLionel Sambuc 
2260f4a2713aSLionel Sambuc     // If a method is deprecated, push it in the global pool.
2261f4a2713aSLionel Sambuc     // This is used for better diagnostics.
2262f4a2713aSLionel Sambuc     if (Method->isDeprecated()) {
2263f4a2713aSLionel Sambuc       if (!PrevObjCMethod->isDeprecated())
2264*0a6a1f1dSLionel Sambuc         List->setMethod(Method);
2265f4a2713aSLionel Sambuc     }
2266*0a6a1f1dSLionel Sambuc     // If the new method is unavailable, push it into global pool
2267f4a2713aSLionel Sambuc     // unless previous one is deprecated.
2268f4a2713aSLionel Sambuc     if (Method->isUnavailable()) {
2269f4a2713aSLionel Sambuc       if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2270*0a6a1f1dSLionel Sambuc         List->setMethod(Method);
2271f4a2713aSLionel Sambuc     }
2272f4a2713aSLionel Sambuc 
2273f4a2713aSLionel Sambuc     return;
2274f4a2713aSLionel Sambuc   }
2275f4a2713aSLionel Sambuc 
2276f4a2713aSLionel Sambuc   // We have a new signature for an existing method - add it.
2277f4a2713aSLionel Sambuc   // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
2278f4a2713aSLionel Sambuc   ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
2279*0a6a1f1dSLionel Sambuc   Previous->setNext(new (Mem) ObjCMethodList(Method));
2280f4a2713aSLionel Sambuc }
2281f4a2713aSLionel Sambuc 
2282f4a2713aSLionel Sambuc /// \brief Read the contents of the method pool for a given selector from
2283f4a2713aSLionel Sambuc /// external storage.
ReadMethodPool(Selector Sel)2284f4a2713aSLionel Sambuc void Sema::ReadMethodPool(Selector Sel) {
2285f4a2713aSLionel Sambuc   assert(ExternalSource && "We need an external AST source");
2286f4a2713aSLionel Sambuc   ExternalSource->ReadMethodPool(Sel);
2287f4a2713aSLionel Sambuc }
2288f4a2713aSLionel Sambuc 
AddMethodToGlobalPool(ObjCMethodDecl * Method,bool impl,bool instance)2289f4a2713aSLionel Sambuc void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
2290f4a2713aSLionel Sambuc                                  bool instance) {
2291f4a2713aSLionel Sambuc   // Ignore methods of invalid containers.
2292f4a2713aSLionel Sambuc   if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
2293f4a2713aSLionel Sambuc     return;
2294f4a2713aSLionel Sambuc 
2295f4a2713aSLionel Sambuc   if (ExternalSource)
2296f4a2713aSLionel Sambuc     ReadMethodPool(Method->getSelector());
2297f4a2713aSLionel Sambuc 
2298f4a2713aSLionel Sambuc   GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
2299f4a2713aSLionel Sambuc   if (Pos == MethodPool.end())
2300f4a2713aSLionel Sambuc     Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2301f4a2713aSLionel Sambuc                                            GlobalMethods())).first;
2302f4a2713aSLionel Sambuc 
2303f4a2713aSLionel Sambuc   Method->setDefined(impl);
2304f4a2713aSLionel Sambuc 
2305f4a2713aSLionel Sambuc   ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
2306f4a2713aSLionel Sambuc   addMethodToGlobalList(&Entry, Method);
2307f4a2713aSLionel Sambuc }
2308f4a2713aSLionel Sambuc 
2309f4a2713aSLionel Sambuc /// Determines if this is an "acceptable" loose mismatch in the global
2310f4a2713aSLionel Sambuc /// method pool.  This exists mostly as a hack to get around certain
2311f4a2713aSLionel Sambuc /// global mismatches which we can't afford to make warnings / errors.
2312f4a2713aSLionel Sambuc /// Really, what we want is a way to take a method out of the global
2313f4a2713aSLionel Sambuc /// method pool.
isAcceptableMethodMismatch(ObjCMethodDecl * chosen,ObjCMethodDecl * other)2314f4a2713aSLionel Sambuc static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2315f4a2713aSLionel Sambuc                                        ObjCMethodDecl *other) {
2316f4a2713aSLionel Sambuc   if (!chosen->isInstanceMethod())
2317f4a2713aSLionel Sambuc     return false;
2318f4a2713aSLionel Sambuc 
2319f4a2713aSLionel Sambuc   Selector sel = chosen->getSelector();
2320f4a2713aSLionel Sambuc   if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2321f4a2713aSLionel Sambuc     return false;
2322f4a2713aSLionel Sambuc 
2323f4a2713aSLionel Sambuc   // Don't complain about mismatches for -length if the method we
2324f4a2713aSLionel Sambuc   // chose has an integral result type.
2325*0a6a1f1dSLionel Sambuc   return (chosen->getReturnType()->isIntegerType());
2326*0a6a1f1dSLionel Sambuc }
2327*0a6a1f1dSLionel Sambuc 
CollectMultipleMethodsInGlobalPool(Selector Sel,SmallVectorImpl<ObjCMethodDecl * > & Methods,bool instance)2328*0a6a1f1dSLionel Sambuc bool Sema::CollectMultipleMethodsInGlobalPool(
2329*0a6a1f1dSLionel Sambuc     Selector Sel, SmallVectorImpl<ObjCMethodDecl *> &Methods, bool instance) {
2330*0a6a1f1dSLionel Sambuc   if (ExternalSource)
2331*0a6a1f1dSLionel Sambuc     ReadMethodPool(Sel);
2332*0a6a1f1dSLionel Sambuc 
2333*0a6a1f1dSLionel Sambuc   GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2334*0a6a1f1dSLionel Sambuc   if (Pos == MethodPool.end())
2335*0a6a1f1dSLionel Sambuc     return false;
2336*0a6a1f1dSLionel Sambuc   // Gather the non-hidden methods.
2337*0a6a1f1dSLionel Sambuc   ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
2338*0a6a1f1dSLionel Sambuc   for (ObjCMethodList *M = &MethList; M; M = M->getNext())
2339*0a6a1f1dSLionel Sambuc     if (M->getMethod() && !M->getMethod()->isHidden())
2340*0a6a1f1dSLionel Sambuc       Methods.push_back(M->getMethod());
2341*0a6a1f1dSLionel Sambuc   return Methods.size() > 1;
2342*0a6a1f1dSLionel Sambuc }
2343*0a6a1f1dSLionel Sambuc 
AreMultipleMethodsInGlobalPool(Selector Sel,bool instance)2344*0a6a1f1dSLionel Sambuc bool Sema::AreMultipleMethodsInGlobalPool(Selector Sel, bool instance) {
2345*0a6a1f1dSLionel Sambuc   GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2346*0a6a1f1dSLionel Sambuc   // Test for no method in the pool which should not trigger any warning by
2347*0a6a1f1dSLionel Sambuc   // caller.
2348*0a6a1f1dSLionel Sambuc   if (Pos == MethodPool.end())
2349*0a6a1f1dSLionel Sambuc     return true;
2350*0a6a1f1dSLionel Sambuc   ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
2351*0a6a1f1dSLionel Sambuc   return MethList.hasMoreThanOneDecl();
2352f4a2713aSLionel Sambuc }
2353f4a2713aSLionel Sambuc 
LookupMethodInGlobalPool(Selector Sel,SourceRange R,bool receiverIdOrClass,bool warn,bool instance)2354f4a2713aSLionel Sambuc ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
2355f4a2713aSLionel Sambuc                                                bool receiverIdOrClass,
2356f4a2713aSLionel Sambuc                                                bool warn, bool instance) {
2357f4a2713aSLionel Sambuc   if (ExternalSource)
2358f4a2713aSLionel Sambuc     ReadMethodPool(Sel);
2359f4a2713aSLionel Sambuc 
2360f4a2713aSLionel Sambuc   GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2361f4a2713aSLionel Sambuc   if (Pos == MethodPool.end())
2362*0a6a1f1dSLionel Sambuc     return nullptr;
2363f4a2713aSLionel Sambuc 
2364f4a2713aSLionel Sambuc   // Gather the non-hidden methods.
2365f4a2713aSLionel Sambuc   ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
2366f4a2713aSLionel Sambuc   SmallVector<ObjCMethodDecl *, 4> Methods;
2367f4a2713aSLionel Sambuc   for (ObjCMethodList *M = &MethList; M; M = M->getNext()) {
2368*0a6a1f1dSLionel Sambuc     if (M->getMethod() && !M->getMethod()->isHidden()) {
2369f4a2713aSLionel Sambuc       // If we're not supposed to warn about mismatches, we're done.
2370f4a2713aSLionel Sambuc       if (!warn)
2371*0a6a1f1dSLionel Sambuc         return M->getMethod();
2372f4a2713aSLionel Sambuc 
2373*0a6a1f1dSLionel Sambuc       Methods.push_back(M->getMethod());
2374f4a2713aSLionel Sambuc     }
2375f4a2713aSLionel Sambuc   }
2376f4a2713aSLionel Sambuc 
2377f4a2713aSLionel Sambuc   // If there aren't any visible methods, we're done.
2378f4a2713aSLionel Sambuc   // FIXME: Recover if there are any known-but-hidden methods?
2379f4a2713aSLionel Sambuc   if (Methods.empty())
2380*0a6a1f1dSLionel Sambuc     return nullptr;
2381f4a2713aSLionel Sambuc 
2382f4a2713aSLionel Sambuc   if (Methods.size() == 1)
2383f4a2713aSLionel Sambuc     return Methods[0];
2384f4a2713aSLionel Sambuc 
2385f4a2713aSLionel Sambuc   // We found multiple methods, so we may have to complain.
2386f4a2713aSLionel Sambuc   bool issueDiagnostic = false, issueError = false;
2387f4a2713aSLionel Sambuc 
2388f4a2713aSLionel Sambuc   // We support a warning which complains about *any* difference in
2389f4a2713aSLionel Sambuc   // method signature.
2390f4a2713aSLionel Sambuc   bool strictSelectorMatch =
2391*0a6a1f1dSLionel Sambuc       receiverIdOrClass && warn &&
2392*0a6a1f1dSLionel Sambuc       !Diags.isIgnored(diag::warn_strict_multiple_method_decl, R.getBegin());
2393f4a2713aSLionel Sambuc   if (strictSelectorMatch) {
2394f4a2713aSLionel Sambuc     for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2395f4a2713aSLionel Sambuc       if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
2396f4a2713aSLionel Sambuc         issueDiagnostic = true;
2397f4a2713aSLionel Sambuc         break;
2398f4a2713aSLionel Sambuc       }
2399f4a2713aSLionel Sambuc     }
2400f4a2713aSLionel Sambuc   }
2401f4a2713aSLionel Sambuc 
2402f4a2713aSLionel Sambuc   // If we didn't see any strict differences, we won't see any loose
2403f4a2713aSLionel Sambuc   // differences.  In ARC, however, we also need to check for loose
2404f4a2713aSLionel Sambuc   // mismatches, because most of them are errors.
2405f4a2713aSLionel Sambuc   if (!strictSelectorMatch ||
2406f4a2713aSLionel Sambuc       (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
2407f4a2713aSLionel Sambuc     for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2408f4a2713aSLionel Sambuc       // This checks if the methods differ in type mismatch.
2409f4a2713aSLionel Sambuc       if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
2410f4a2713aSLionel Sambuc           !isAcceptableMethodMismatch(Methods[0], Methods[I])) {
2411f4a2713aSLionel Sambuc         issueDiagnostic = true;
2412f4a2713aSLionel Sambuc         if (getLangOpts().ObjCAutoRefCount)
2413f4a2713aSLionel Sambuc           issueError = true;
2414f4a2713aSLionel Sambuc         break;
2415f4a2713aSLionel Sambuc       }
2416f4a2713aSLionel Sambuc     }
2417f4a2713aSLionel Sambuc 
2418f4a2713aSLionel Sambuc   if (issueDiagnostic) {
2419f4a2713aSLionel Sambuc     if (issueError)
2420f4a2713aSLionel Sambuc       Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2421f4a2713aSLionel Sambuc     else if (strictSelectorMatch)
2422f4a2713aSLionel Sambuc       Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2423f4a2713aSLionel Sambuc     else
2424f4a2713aSLionel Sambuc       Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
2425f4a2713aSLionel Sambuc 
2426f4a2713aSLionel Sambuc     Diag(Methods[0]->getLocStart(),
2427f4a2713aSLionel Sambuc          issueError ? diag::note_possibility : diag::note_using)
2428f4a2713aSLionel Sambuc       << Methods[0]->getSourceRange();
2429f4a2713aSLionel Sambuc     for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2430f4a2713aSLionel Sambuc       Diag(Methods[I]->getLocStart(), diag::note_also_found)
2431f4a2713aSLionel Sambuc         << Methods[I]->getSourceRange();
2432f4a2713aSLionel Sambuc   }
2433f4a2713aSLionel Sambuc   }
2434f4a2713aSLionel Sambuc   return Methods[0];
2435f4a2713aSLionel Sambuc }
2436f4a2713aSLionel Sambuc 
LookupImplementedMethodInGlobalPool(Selector Sel)2437f4a2713aSLionel Sambuc ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
2438f4a2713aSLionel Sambuc   GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2439f4a2713aSLionel Sambuc   if (Pos == MethodPool.end())
2440*0a6a1f1dSLionel Sambuc     return nullptr;
2441f4a2713aSLionel Sambuc 
2442f4a2713aSLionel Sambuc   GlobalMethods &Methods = Pos->second;
2443*0a6a1f1dSLionel Sambuc   for (const ObjCMethodList *Method = &Methods.first; Method;
2444*0a6a1f1dSLionel Sambuc        Method = Method->getNext())
2445*0a6a1f1dSLionel Sambuc     if (Method->getMethod() && Method->getMethod()->isDefined())
2446*0a6a1f1dSLionel Sambuc       return Method->getMethod();
2447f4a2713aSLionel Sambuc 
2448*0a6a1f1dSLionel Sambuc   for (const ObjCMethodList *Method = &Methods.second; Method;
2449*0a6a1f1dSLionel Sambuc        Method = Method->getNext())
2450*0a6a1f1dSLionel Sambuc     if (Method->getMethod() && Method->getMethod()->isDefined())
2451*0a6a1f1dSLionel Sambuc       return Method->getMethod();
2452*0a6a1f1dSLionel Sambuc   return nullptr;
2453f4a2713aSLionel Sambuc }
2454f4a2713aSLionel Sambuc 
2455f4a2713aSLionel Sambuc static void
HelperSelectorsForTypoCorrection(SmallVectorImpl<const ObjCMethodDecl * > & BestMethod,StringRef Typo,const ObjCMethodDecl * Method)2456f4a2713aSLionel Sambuc HelperSelectorsForTypoCorrection(
2457f4a2713aSLionel Sambuc                       SmallVectorImpl<const ObjCMethodDecl *> &BestMethod,
2458f4a2713aSLionel Sambuc                       StringRef Typo, const ObjCMethodDecl * Method) {
2459f4a2713aSLionel Sambuc   const unsigned MaxEditDistance = 1;
2460f4a2713aSLionel Sambuc   unsigned BestEditDistance = MaxEditDistance + 1;
2461f4a2713aSLionel Sambuc   std::string MethodName = Method->getSelector().getAsString();
2462f4a2713aSLionel Sambuc 
2463f4a2713aSLionel Sambuc   unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size());
2464f4a2713aSLionel Sambuc   if (MinPossibleEditDistance > 0 &&
2465f4a2713aSLionel Sambuc       Typo.size() / MinPossibleEditDistance < 1)
2466f4a2713aSLionel Sambuc     return;
2467f4a2713aSLionel Sambuc   unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance);
2468f4a2713aSLionel Sambuc   if (EditDistance > MaxEditDistance)
2469f4a2713aSLionel Sambuc     return;
2470f4a2713aSLionel Sambuc   if (EditDistance == BestEditDistance)
2471f4a2713aSLionel Sambuc     BestMethod.push_back(Method);
2472f4a2713aSLionel Sambuc   else if (EditDistance < BestEditDistance) {
2473f4a2713aSLionel Sambuc     BestMethod.clear();
2474f4a2713aSLionel Sambuc     BestMethod.push_back(Method);
2475f4a2713aSLionel Sambuc   }
2476f4a2713aSLionel Sambuc }
2477f4a2713aSLionel Sambuc 
HelperIsMethodInObjCType(Sema & S,Selector Sel,QualType ObjectType)2478f4a2713aSLionel Sambuc static bool HelperIsMethodInObjCType(Sema &S, Selector Sel,
2479f4a2713aSLionel Sambuc                                      QualType ObjectType) {
2480f4a2713aSLionel Sambuc   if (ObjectType.isNull())
2481f4a2713aSLionel Sambuc     return true;
2482f4a2713aSLionel Sambuc   if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/))
2483f4a2713aSLionel Sambuc     return true;
2484*0a6a1f1dSLionel Sambuc   return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) !=
2485*0a6a1f1dSLionel Sambuc          nullptr;
2486f4a2713aSLionel Sambuc }
2487f4a2713aSLionel Sambuc 
2488f4a2713aSLionel Sambuc const ObjCMethodDecl *
SelectorsForTypoCorrection(Selector Sel,QualType ObjectType)2489f4a2713aSLionel Sambuc Sema::SelectorsForTypoCorrection(Selector Sel,
2490f4a2713aSLionel Sambuc                                  QualType ObjectType) {
2491f4a2713aSLionel Sambuc   unsigned NumArgs = Sel.getNumArgs();
2492f4a2713aSLionel Sambuc   SmallVector<const ObjCMethodDecl *, 8> Methods;
2493f4a2713aSLionel Sambuc   bool ObjectIsId = true, ObjectIsClass = true;
2494f4a2713aSLionel Sambuc   if (ObjectType.isNull())
2495f4a2713aSLionel Sambuc     ObjectIsId = ObjectIsClass = false;
2496f4a2713aSLionel Sambuc   else if (!ObjectType->isObjCObjectPointerType())
2497*0a6a1f1dSLionel Sambuc     return nullptr;
2498f4a2713aSLionel Sambuc   else if (const ObjCObjectPointerType *ObjCPtr =
2499f4a2713aSLionel Sambuc            ObjectType->getAsObjCInterfacePointerType()) {
2500f4a2713aSLionel Sambuc     ObjectType = QualType(ObjCPtr->getInterfaceType(), 0);
2501f4a2713aSLionel Sambuc     ObjectIsId = ObjectIsClass = false;
2502f4a2713aSLionel Sambuc   }
2503f4a2713aSLionel Sambuc   else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType())
2504f4a2713aSLionel Sambuc     ObjectIsClass = false;
2505f4a2713aSLionel Sambuc   else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType())
2506f4a2713aSLionel Sambuc     ObjectIsId = false;
2507f4a2713aSLionel Sambuc   else
2508*0a6a1f1dSLionel Sambuc     return nullptr;
2509f4a2713aSLionel Sambuc 
2510f4a2713aSLionel Sambuc   for (GlobalMethodPool::iterator b = MethodPool.begin(),
2511f4a2713aSLionel Sambuc        e = MethodPool.end(); b != e; b++) {
2512f4a2713aSLionel Sambuc     // instance methods
2513f4a2713aSLionel Sambuc     for (ObjCMethodList *M = &b->second.first; M; M=M->getNext())
2514*0a6a1f1dSLionel Sambuc       if (M->getMethod() &&
2515*0a6a1f1dSLionel Sambuc           (M->getMethod()->getSelector().getNumArgs() == NumArgs) &&
2516*0a6a1f1dSLionel Sambuc           (M->getMethod()->getSelector() != Sel)) {
2517f4a2713aSLionel Sambuc         if (ObjectIsId)
2518*0a6a1f1dSLionel Sambuc           Methods.push_back(M->getMethod());
2519f4a2713aSLionel Sambuc         else if (!ObjectIsClass &&
2520*0a6a1f1dSLionel Sambuc                  HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(),
2521*0a6a1f1dSLionel Sambuc                                           ObjectType))
2522*0a6a1f1dSLionel Sambuc           Methods.push_back(M->getMethod());
2523f4a2713aSLionel Sambuc       }
2524f4a2713aSLionel Sambuc     // class methods
2525f4a2713aSLionel Sambuc     for (ObjCMethodList *M = &b->second.second; M; M=M->getNext())
2526*0a6a1f1dSLionel Sambuc       if (M->getMethod() &&
2527*0a6a1f1dSLionel Sambuc           (M->getMethod()->getSelector().getNumArgs() == NumArgs) &&
2528*0a6a1f1dSLionel Sambuc           (M->getMethod()->getSelector() != Sel)) {
2529f4a2713aSLionel Sambuc         if (ObjectIsClass)
2530*0a6a1f1dSLionel Sambuc           Methods.push_back(M->getMethod());
2531f4a2713aSLionel Sambuc         else if (!ObjectIsId &&
2532*0a6a1f1dSLionel Sambuc                  HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(),
2533*0a6a1f1dSLionel Sambuc                                           ObjectType))
2534*0a6a1f1dSLionel Sambuc           Methods.push_back(M->getMethod());
2535f4a2713aSLionel Sambuc       }
2536f4a2713aSLionel Sambuc   }
2537f4a2713aSLionel Sambuc 
2538f4a2713aSLionel Sambuc   SmallVector<const ObjCMethodDecl *, 8> SelectedMethods;
2539f4a2713aSLionel Sambuc   for (unsigned i = 0, e = Methods.size(); i < e; i++) {
2540f4a2713aSLionel Sambuc     HelperSelectorsForTypoCorrection(SelectedMethods,
2541f4a2713aSLionel Sambuc                                      Sel.getAsString(), Methods[i]);
2542f4a2713aSLionel Sambuc   }
2543*0a6a1f1dSLionel Sambuc   return (SelectedMethods.size() == 1) ? SelectedMethods[0] : nullptr;
2544f4a2713aSLionel Sambuc }
2545f4a2713aSLionel Sambuc 
2546f4a2713aSLionel Sambuc /// DiagnoseDuplicateIvars -
2547f4a2713aSLionel Sambuc /// Check for duplicate ivars in the entire class at the start of
2548f4a2713aSLionel Sambuc /// \@implementation. This becomes necesssary because class extension can
2549f4a2713aSLionel Sambuc /// add ivars to a class in random order which will not be known until
2550f4a2713aSLionel Sambuc /// class's \@implementation is seen.
DiagnoseDuplicateIvars(ObjCInterfaceDecl * ID,ObjCInterfaceDecl * SID)2551f4a2713aSLionel Sambuc void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2552f4a2713aSLionel Sambuc                                   ObjCInterfaceDecl *SID) {
2553*0a6a1f1dSLionel Sambuc   for (auto *Ivar : ID->ivars()) {
2554f4a2713aSLionel Sambuc     if (Ivar->isInvalidDecl())
2555f4a2713aSLionel Sambuc       continue;
2556f4a2713aSLionel Sambuc     if (IdentifierInfo *II = Ivar->getIdentifier()) {
2557f4a2713aSLionel Sambuc       ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2558f4a2713aSLionel Sambuc       if (prevIvar) {
2559f4a2713aSLionel Sambuc         Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2560f4a2713aSLionel Sambuc         Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2561f4a2713aSLionel Sambuc         Ivar->setInvalidDecl();
2562f4a2713aSLionel Sambuc       }
2563f4a2713aSLionel Sambuc     }
2564f4a2713aSLionel Sambuc   }
2565f4a2713aSLionel Sambuc }
2566f4a2713aSLionel Sambuc 
getObjCContainerKind() const2567f4a2713aSLionel Sambuc Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2568f4a2713aSLionel Sambuc   switch (CurContext->getDeclKind()) {
2569f4a2713aSLionel Sambuc     case Decl::ObjCInterface:
2570f4a2713aSLionel Sambuc       return Sema::OCK_Interface;
2571f4a2713aSLionel Sambuc     case Decl::ObjCProtocol:
2572f4a2713aSLionel Sambuc       return Sema::OCK_Protocol;
2573f4a2713aSLionel Sambuc     case Decl::ObjCCategory:
2574f4a2713aSLionel Sambuc       if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2575f4a2713aSLionel Sambuc         return Sema::OCK_ClassExtension;
2576f4a2713aSLionel Sambuc       else
2577f4a2713aSLionel Sambuc         return Sema::OCK_Category;
2578f4a2713aSLionel Sambuc     case Decl::ObjCImplementation:
2579f4a2713aSLionel Sambuc       return Sema::OCK_Implementation;
2580f4a2713aSLionel Sambuc     case Decl::ObjCCategoryImpl:
2581f4a2713aSLionel Sambuc       return Sema::OCK_CategoryImplementation;
2582f4a2713aSLionel Sambuc 
2583f4a2713aSLionel Sambuc     default:
2584f4a2713aSLionel Sambuc       return Sema::OCK_None;
2585f4a2713aSLionel Sambuc   }
2586f4a2713aSLionel Sambuc }
2587f4a2713aSLionel Sambuc 
2588f4a2713aSLionel Sambuc // Note: For class/category implementations, allMethods is always null.
ActOnAtEnd(Scope * S,SourceRange AtEnd,ArrayRef<Decl * > allMethods,ArrayRef<DeclGroupPtrTy> allTUVars)2589f4a2713aSLionel Sambuc Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods,
2590f4a2713aSLionel Sambuc                        ArrayRef<DeclGroupPtrTy> allTUVars) {
2591f4a2713aSLionel Sambuc   if (getObjCContainerKind() == Sema::OCK_None)
2592*0a6a1f1dSLionel Sambuc     return nullptr;
2593f4a2713aSLionel Sambuc 
2594f4a2713aSLionel Sambuc   assert(AtEnd.isValid() && "Invalid location for '@end'");
2595f4a2713aSLionel Sambuc 
2596f4a2713aSLionel Sambuc   ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2597f4a2713aSLionel Sambuc   Decl *ClassDecl = cast<Decl>(OCD);
2598f4a2713aSLionel Sambuc 
2599f4a2713aSLionel Sambuc   bool isInterfaceDeclKind =
2600f4a2713aSLionel Sambuc         isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2601f4a2713aSLionel Sambuc          || isa<ObjCProtocolDecl>(ClassDecl);
2602f4a2713aSLionel Sambuc   bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
2603f4a2713aSLionel Sambuc 
2604f4a2713aSLionel Sambuc   // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2605f4a2713aSLionel Sambuc   llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2606f4a2713aSLionel Sambuc   llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2607f4a2713aSLionel Sambuc 
2608f4a2713aSLionel Sambuc   for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) {
2609f4a2713aSLionel Sambuc     ObjCMethodDecl *Method =
2610f4a2713aSLionel Sambuc       cast_or_null<ObjCMethodDecl>(allMethods[i]);
2611f4a2713aSLionel Sambuc 
2612f4a2713aSLionel Sambuc     if (!Method) continue;  // Already issued a diagnostic.
2613f4a2713aSLionel Sambuc     if (Method->isInstanceMethod()) {
2614f4a2713aSLionel Sambuc       /// Check for instance method of the same name with incompatible types
2615f4a2713aSLionel Sambuc       const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
2616f4a2713aSLionel Sambuc       bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
2617f4a2713aSLionel Sambuc                               : false;
2618f4a2713aSLionel Sambuc       if ((isInterfaceDeclKind && PrevMethod && !match)
2619f4a2713aSLionel Sambuc           || (checkIdenticalMethods && match)) {
2620f4a2713aSLionel Sambuc           Diag(Method->getLocation(), diag::err_duplicate_method_decl)
2621f4a2713aSLionel Sambuc             << Method->getDeclName();
2622f4a2713aSLionel Sambuc           Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2623f4a2713aSLionel Sambuc         Method->setInvalidDecl();
2624f4a2713aSLionel Sambuc       } else {
2625f4a2713aSLionel Sambuc         if (PrevMethod) {
2626f4a2713aSLionel Sambuc           Method->setAsRedeclaration(PrevMethod);
2627f4a2713aSLionel Sambuc           if (!Context.getSourceManager().isInSystemHeader(
2628f4a2713aSLionel Sambuc                  Method->getLocation()))
2629f4a2713aSLionel Sambuc             Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2630f4a2713aSLionel Sambuc               << Method->getDeclName();
2631f4a2713aSLionel Sambuc           Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2632f4a2713aSLionel Sambuc         }
2633f4a2713aSLionel Sambuc         InsMap[Method->getSelector()] = Method;
2634f4a2713aSLionel Sambuc         /// The following allows us to typecheck messages to "id".
2635f4a2713aSLionel Sambuc         AddInstanceMethodToGlobalPool(Method);
2636f4a2713aSLionel Sambuc       }
2637f4a2713aSLionel Sambuc     } else {
2638f4a2713aSLionel Sambuc       /// Check for class method of the same name with incompatible types
2639f4a2713aSLionel Sambuc       const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
2640f4a2713aSLionel Sambuc       bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
2641f4a2713aSLionel Sambuc                               : false;
2642f4a2713aSLionel Sambuc       if ((isInterfaceDeclKind && PrevMethod && !match)
2643f4a2713aSLionel Sambuc           || (checkIdenticalMethods && match)) {
2644f4a2713aSLionel Sambuc         Diag(Method->getLocation(), diag::err_duplicate_method_decl)
2645f4a2713aSLionel Sambuc           << Method->getDeclName();
2646f4a2713aSLionel Sambuc         Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2647f4a2713aSLionel Sambuc         Method->setInvalidDecl();
2648f4a2713aSLionel Sambuc       } else {
2649f4a2713aSLionel Sambuc         if (PrevMethod) {
2650f4a2713aSLionel Sambuc           Method->setAsRedeclaration(PrevMethod);
2651f4a2713aSLionel Sambuc           if (!Context.getSourceManager().isInSystemHeader(
2652f4a2713aSLionel Sambuc                  Method->getLocation()))
2653f4a2713aSLionel Sambuc             Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2654f4a2713aSLionel Sambuc               << Method->getDeclName();
2655f4a2713aSLionel Sambuc           Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2656f4a2713aSLionel Sambuc         }
2657f4a2713aSLionel Sambuc         ClsMap[Method->getSelector()] = Method;
2658f4a2713aSLionel Sambuc         AddFactoryMethodToGlobalPool(Method);
2659f4a2713aSLionel Sambuc       }
2660f4a2713aSLionel Sambuc     }
2661f4a2713aSLionel Sambuc   }
2662f4a2713aSLionel Sambuc   if (isa<ObjCInterfaceDecl>(ClassDecl)) {
2663f4a2713aSLionel Sambuc     // Nothing to do here.
2664f4a2713aSLionel Sambuc   } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
2665f4a2713aSLionel Sambuc     // Categories are used to extend the class by declaring new methods.
2666f4a2713aSLionel Sambuc     // By the same token, they are also used to add new properties. No
2667f4a2713aSLionel Sambuc     // need to compare the added property to those in the class.
2668f4a2713aSLionel Sambuc 
2669f4a2713aSLionel Sambuc     if (C->IsClassExtension()) {
2670f4a2713aSLionel Sambuc       ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2671f4a2713aSLionel Sambuc       DiagnoseClassExtensionDupMethods(C, CCPrimary);
2672f4a2713aSLionel Sambuc     }
2673f4a2713aSLionel Sambuc   }
2674f4a2713aSLionel Sambuc   if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
2675f4a2713aSLionel Sambuc     if (CDecl->getIdentifier())
2676f4a2713aSLionel Sambuc       // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2677f4a2713aSLionel Sambuc       // user-defined setter/getter. It also synthesizes setter/getter methods
2678f4a2713aSLionel Sambuc       // and adds them to the DeclContext and global method pools.
2679*0a6a1f1dSLionel Sambuc       for (auto *I : CDecl->properties())
2680*0a6a1f1dSLionel Sambuc         ProcessPropertyDecl(I, CDecl);
2681f4a2713aSLionel Sambuc     CDecl->setAtEndRange(AtEnd);
2682f4a2713aSLionel Sambuc   }
2683f4a2713aSLionel Sambuc   if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
2684f4a2713aSLionel Sambuc     IC->setAtEndRange(AtEnd);
2685f4a2713aSLionel Sambuc     if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
2686f4a2713aSLionel Sambuc       // Any property declared in a class extension might have user
2687f4a2713aSLionel Sambuc       // declared setter or getter in current class extension or one
2688f4a2713aSLionel Sambuc       // of the other class extensions. Mark them as synthesized as
2689f4a2713aSLionel Sambuc       // property will be synthesized when property with same name is
2690f4a2713aSLionel Sambuc       // seen in the @implementation.
2691*0a6a1f1dSLionel Sambuc       for (const auto *Ext : IDecl->visible_extensions()) {
2692*0a6a1f1dSLionel Sambuc         for (const auto *Property : Ext->properties()) {
2693f4a2713aSLionel Sambuc           // Skip over properties declared @dynamic
2694f4a2713aSLionel Sambuc           if (const ObjCPropertyImplDecl *PIDecl
2695f4a2713aSLionel Sambuc               = IC->FindPropertyImplDecl(Property->getIdentifier()))
2696f4a2713aSLionel Sambuc             if (PIDecl->getPropertyImplementation()
2697f4a2713aSLionel Sambuc                   == ObjCPropertyImplDecl::Dynamic)
2698f4a2713aSLionel Sambuc               continue;
2699f4a2713aSLionel Sambuc 
2700*0a6a1f1dSLionel Sambuc           for (const auto *Ext : IDecl->visible_extensions()) {
2701f4a2713aSLionel Sambuc             if (ObjCMethodDecl *GetterMethod
2702f4a2713aSLionel Sambuc                   = Ext->getInstanceMethod(Property->getGetterName()))
2703f4a2713aSLionel Sambuc               GetterMethod->setPropertyAccessor(true);
2704f4a2713aSLionel Sambuc             if (!Property->isReadOnly())
2705f4a2713aSLionel Sambuc               if (ObjCMethodDecl *SetterMethod
2706f4a2713aSLionel Sambuc                     = Ext->getInstanceMethod(Property->getSetterName()))
2707f4a2713aSLionel Sambuc                 SetterMethod->setPropertyAccessor(true);
2708f4a2713aSLionel Sambuc           }
2709f4a2713aSLionel Sambuc         }
2710f4a2713aSLionel Sambuc       }
2711f4a2713aSLionel Sambuc       ImplMethodsVsClassMethods(S, IC, IDecl);
2712f4a2713aSLionel Sambuc       AtomicPropertySetterGetterRules(IC, IDecl);
2713f4a2713aSLionel Sambuc       DiagnoseOwningPropertyGetterSynthesis(IC);
2714*0a6a1f1dSLionel Sambuc       DiagnoseUnusedBackingIvarInAccessor(S, IC);
2715*0a6a1f1dSLionel Sambuc       if (IDecl->hasDesignatedInitializers())
2716*0a6a1f1dSLionel Sambuc         DiagnoseMissingDesignatedInitOverrides(IC, IDecl);
2717f4a2713aSLionel Sambuc 
2718f4a2713aSLionel Sambuc       bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2719*0a6a1f1dSLionel Sambuc       if (IDecl->getSuperClass() == nullptr) {
2720f4a2713aSLionel Sambuc         // This class has no superclass, so check that it has been marked with
2721f4a2713aSLionel Sambuc         // __attribute((objc_root_class)).
2722f4a2713aSLionel Sambuc         if (!HasRootClassAttr) {
2723f4a2713aSLionel Sambuc           SourceLocation DeclLoc(IDecl->getLocation());
2724*0a6a1f1dSLionel Sambuc           SourceLocation SuperClassLoc(getLocForEndOfToken(DeclLoc));
2725f4a2713aSLionel Sambuc           Diag(DeclLoc, diag::warn_objc_root_class_missing)
2726f4a2713aSLionel Sambuc             << IDecl->getIdentifier();
2727f4a2713aSLionel Sambuc           // See if NSObject is in the current scope, and if it is, suggest
2728f4a2713aSLionel Sambuc           // adding " : NSObject " to the class declaration.
2729f4a2713aSLionel Sambuc           NamedDecl *IF = LookupSingleName(TUScope,
2730f4a2713aSLionel Sambuc                                            NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2731f4a2713aSLionel Sambuc                                            DeclLoc, LookupOrdinaryName);
2732f4a2713aSLionel Sambuc           ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2733f4a2713aSLionel Sambuc           if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2734f4a2713aSLionel Sambuc             Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2735f4a2713aSLionel Sambuc               << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2736f4a2713aSLionel Sambuc           } else {
2737f4a2713aSLionel Sambuc             Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2738f4a2713aSLionel Sambuc           }
2739f4a2713aSLionel Sambuc         }
2740f4a2713aSLionel Sambuc       } else if (HasRootClassAttr) {
2741f4a2713aSLionel Sambuc         // Complain that only root classes may have this attribute.
2742f4a2713aSLionel Sambuc         Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2743f4a2713aSLionel Sambuc       }
2744f4a2713aSLionel Sambuc 
2745f4a2713aSLionel Sambuc       if (LangOpts.ObjCRuntime.isNonFragile()) {
2746f4a2713aSLionel Sambuc         while (IDecl->getSuperClass()) {
2747f4a2713aSLionel Sambuc           DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2748f4a2713aSLionel Sambuc           IDecl = IDecl->getSuperClass();
2749f4a2713aSLionel Sambuc         }
2750f4a2713aSLionel Sambuc       }
2751f4a2713aSLionel Sambuc     }
2752f4a2713aSLionel Sambuc     SetIvarInitializers(IC);
2753f4a2713aSLionel Sambuc   } else if (ObjCCategoryImplDecl* CatImplClass =
2754f4a2713aSLionel Sambuc                                    dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
2755f4a2713aSLionel Sambuc     CatImplClass->setAtEndRange(AtEnd);
2756f4a2713aSLionel Sambuc 
2757f4a2713aSLionel Sambuc     // Find category interface decl and then check that all methods declared
2758f4a2713aSLionel Sambuc     // in this interface are implemented in the category @implementation.
2759f4a2713aSLionel Sambuc     if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
2760f4a2713aSLionel Sambuc       if (ObjCCategoryDecl *Cat
2761f4a2713aSLionel Sambuc             = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
2762f4a2713aSLionel Sambuc         ImplMethodsVsClassMethods(S, CatImplClass, Cat);
2763f4a2713aSLionel Sambuc       }
2764f4a2713aSLionel Sambuc     }
2765f4a2713aSLionel Sambuc   }
2766f4a2713aSLionel Sambuc   if (isInterfaceDeclKind) {
2767f4a2713aSLionel Sambuc     // Reject invalid vardecls.
2768f4a2713aSLionel Sambuc     for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
2769f4a2713aSLionel Sambuc       DeclGroupRef DG = allTUVars[i].get();
2770f4a2713aSLionel Sambuc       for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2771f4a2713aSLionel Sambuc         if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
2772f4a2713aSLionel Sambuc           if (!VDecl->hasExternalStorage())
2773f4a2713aSLionel Sambuc             Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
2774f4a2713aSLionel Sambuc         }
2775f4a2713aSLionel Sambuc     }
2776f4a2713aSLionel Sambuc   }
2777f4a2713aSLionel Sambuc   ActOnObjCContainerFinishDefinition();
2778f4a2713aSLionel Sambuc 
2779f4a2713aSLionel Sambuc   for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
2780f4a2713aSLionel Sambuc     DeclGroupRef DG = allTUVars[i].get();
2781f4a2713aSLionel Sambuc     for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2782f4a2713aSLionel Sambuc       (*I)->setTopLevelDeclInObjCContainer();
2783f4a2713aSLionel Sambuc     Consumer.HandleTopLevelDeclInObjCContainer(DG);
2784f4a2713aSLionel Sambuc   }
2785f4a2713aSLionel Sambuc 
2786f4a2713aSLionel Sambuc   ActOnDocumentableDecl(ClassDecl);
2787f4a2713aSLionel Sambuc   return ClassDecl;
2788f4a2713aSLionel Sambuc }
2789f4a2713aSLionel Sambuc 
2790f4a2713aSLionel Sambuc 
2791f4a2713aSLionel Sambuc /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2792f4a2713aSLionel Sambuc /// objective-c's type qualifier from the parser version of the same info.
2793f4a2713aSLionel Sambuc static Decl::ObjCDeclQualifier
CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal)2794f4a2713aSLionel Sambuc CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
2795f4a2713aSLionel Sambuc   return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
2796f4a2713aSLionel Sambuc }
2797f4a2713aSLionel Sambuc 
2798f4a2713aSLionel Sambuc /// \brief Check whether the declared result type of the given Objective-C
2799f4a2713aSLionel Sambuc /// method declaration is compatible with the method's class.
2800f4a2713aSLionel Sambuc ///
2801f4a2713aSLionel Sambuc static Sema::ResultTypeCompatibilityKind
CheckRelatedResultTypeCompatibility(Sema & S,ObjCMethodDecl * Method,ObjCInterfaceDecl * CurrentClass)2802f4a2713aSLionel Sambuc CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2803f4a2713aSLionel Sambuc                                     ObjCInterfaceDecl *CurrentClass) {
2804*0a6a1f1dSLionel Sambuc   QualType ResultType = Method->getReturnType();
2805f4a2713aSLionel Sambuc 
2806f4a2713aSLionel Sambuc   // If an Objective-C method inherits its related result type, then its
2807f4a2713aSLionel Sambuc   // declared result type must be compatible with its own class type. The
2808f4a2713aSLionel Sambuc   // declared result type is compatible if:
2809f4a2713aSLionel Sambuc   if (const ObjCObjectPointerType *ResultObjectType
2810f4a2713aSLionel Sambuc                                 = ResultType->getAs<ObjCObjectPointerType>()) {
2811f4a2713aSLionel Sambuc     //   - it is id or qualified id, or
2812f4a2713aSLionel Sambuc     if (ResultObjectType->isObjCIdType() ||
2813f4a2713aSLionel Sambuc         ResultObjectType->isObjCQualifiedIdType())
2814f4a2713aSLionel Sambuc       return Sema::RTC_Compatible;
2815f4a2713aSLionel Sambuc 
2816f4a2713aSLionel Sambuc     if (CurrentClass) {
2817f4a2713aSLionel Sambuc       if (ObjCInterfaceDecl *ResultClass
2818f4a2713aSLionel Sambuc                                       = ResultObjectType->getInterfaceDecl()) {
2819f4a2713aSLionel Sambuc         //   - it is the same as the method's class type, or
2820f4a2713aSLionel Sambuc         if (declaresSameEntity(CurrentClass, ResultClass))
2821f4a2713aSLionel Sambuc           return Sema::RTC_Compatible;
2822f4a2713aSLionel Sambuc 
2823f4a2713aSLionel Sambuc         //   - it is a superclass of the method's class type
2824f4a2713aSLionel Sambuc         if (ResultClass->isSuperClassOf(CurrentClass))
2825f4a2713aSLionel Sambuc           return Sema::RTC_Compatible;
2826f4a2713aSLionel Sambuc       }
2827f4a2713aSLionel Sambuc     } else {
2828f4a2713aSLionel Sambuc       // Any Objective-C pointer type might be acceptable for a protocol
2829f4a2713aSLionel Sambuc       // method; we just don't know.
2830f4a2713aSLionel Sambuc       return Sema::RTC_Unknown;
2831f4a2713aSLionel Sambuc     }
2832f4a2713aSLionel Sambuc   }
2833f4a2713aSLionel Sambuc 
2834f4a2713aSLionel Sambuc   return Sema::RTC_Incompatible;
2835f4a2713aSLionel Sambuc }
2836f4a2713aSLionel Sambuc 
2837f4a2713aSLionel Sambuc namespace {
2838f4a2713aSLionel Sambuc /// A helper class for searching for methods which a particular method
2839f4a2713aSLionel Sambuc /// overrides.
2840f4a2713aSLionel Sambuc class OverrideSearch {
2841f4a2713aSLionel Sambuc public:
2842f4a2713aSLionel Sambuc   Sema &S;
2843f4a2713aSLionel Sambuc   ObjCMethodDecl *Method;
2844f4a2713aSLionel Sambuc   llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
2845f4a2713aSLionel Sambuc   bool Recursive;
2846f4a2713aSLionel Sambuc 
2847f4a2713aSLionel Sambuc public:
OverrideSearch(Sema & S,ObjCMethodDecl * method)2848f4a2713aSLionel Sambuc   OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2849f4a2713aSLionel Sambuc     Selector selector = method->getSelector();
2850f4a2713aSLionel Sambuc 
2851f4a2713aSLionel Sambuc     // Bypass this search if we've never seen an instance/class method
2852f4a2713aSLionel Sambuc     // with this selector before.
2853f4a2713aSLionel Sambuc     Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2854f4a2713aSLionel Sambuc     if (it == S.MethodPool.end()) {
2855f4a2713aSLionel Sambuc       if (!S.getExternalSource()) return;
2856f4a2713aSLionel Sambuc       S.ReadMethodPool(selector);
2857f4a2713aSLionel Sambuc 
2858f4a2713aSLionel Sambuc       it = S.MethodPool.find(selector);
2859f4a2713aSLionel Sambuc       if (it == S.MethodPool.end())
2860f4a2713aSLionel Sambuc         return;
2861f4a2713aSLionel Sambuc     }
2862f4a2713aSLionel Sambuc     ObjCMethodList &list =
2863f4a2713aSLionel Sambuc       method->isInstanceMethod() ? it->second.first : it->second.second;
2864*0a6a1f1dSLionel Sambuc     if (!list.getMethod()) return;
2865f4a2713aSLionel Sambuc 
2866f4a2713aSLionel Sambuc     ObjCContainerDecl *container
2867f4a2713aSLionel Sambuc       = cast<ObjCContainerDecl>(method->getDeclContext());
2868f4a2713aSLionel Sambuc 
2869f4a2713aSLionel Sambuc     // Prevent the search from reaching this container again.  This is
2870f4a2713aSLionel Sambuc     // important with categories, which override methods from the
2871f4a2713aSLionel Sambuc     // interface and each other.
2872f4a2713aSLionel Sambuc     if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2873f4a2713aSLionel Sambuc       searchFromContainer(container);
2874f4a2713aSLionel Sambuc       if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2875f4a2713aSLionel Sambuc         searchFromContainer(Interface);
2876f4a2713aSLionel Sambuc     } else {
2877f4a2713aSLionel Sambuc       searchFromContainer(container);
2878f4a2713aSLionel Sambuc     }
2879f4a2713aSLionel Sambuc   }
2880f4a2713aSLionel Sambuc 
2881f4a2713aSLionel Sambuc   typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
begin() const2882f4a2713aSLionel Sambuc   iterator begin() const { return Overridden.begin(); }
end() const2883f4a2713aSLionel Sambuc   iterator end() const { return Overridden.end(); }
2884f4a2713aSLionel Sambuc 
2885f4a2713aSLionel Sambuc private:
searchFromContainer(ObjCContainerDecl * container)2886f4a2713aSLionel Sambuc   void searchFromContainer(ObjCContainerDecl *container) {
2887f4a2713aSLionel Sambuc     if (container->isInvalidDecl()) return;
2888f4a2713aSLionel Sambuc 
2889f4a2713aSLionel Sambuc     switch (container->getDeclKind()) {
2890f4a2713aSLionel Sambuc #define OBJCCONTAINER(type, base) \
2891f4a2713aSLionel Sambuc     case Decl::type: \
2892f4a2713aSLionel Sambuc       searchFrom(cast<type##Decl>(container)); \
2893f4a2713aSLionel Sambuc       break;
2894f4a2713aSLionel Sambuc #define ABSTRACT_DECL(expansion)
2895f4a2713aSLionel Sambuc #define DECL(type, base) \
2896f4a2713aSLionel Sambuc     case Decl::type:
2897f4a2713aSLionel Sambuc #include "clang/AST/DeclNodes.inc"
2898f4a2713aSLionel Sambuc       llvm_unreachable("not an ObjC container!");
2899f4a2713aSLionel Sambuc     }
2900f4a2713aSLionel Sambuc   }
2901f4a2713aSLionel Sambuc 
searchFrom(ObjCProtocolDecl * protocol)2902f4a2713aSLionel Sambuc   void searchFrom(ObjCProtocolDecl *protocol) {
2903f4a2713aSLionel Sambuc     if (!protocol->hasDefinition())
2904f4a2713aSLionel Sambuc       return;
2905f4a2713aSLionel Sambuc 
2906f4a2713aSLionel Sambuc     // A method in a protocol declaration overrides declarations from
2907f4a2713aSLionel Sambuc     // referenced ("parent") protocols.
2908f4a2713aSLionel Sambuc     search(protocol->getReferencedProtocols());
2909f4a2713aSLionel Sambuc   }
2910f4a2713aSLionel Sambuc 
searchFrom(ObjCCategoryDecl * category)2911f4a2713aSLionel Sambuc   void searchFrom(ObjCCategoryDecl *category) {
2912f4a2713aSLionel Sambuc     // A method in a category declaration overrides declarations from
2913f4a2713aSLionel Sambuc     // the main class and from protocols the category references.
2914f4a2713aSLionel Sambuc     // The main class is handled in the constructor.
2915f4a2713aSLionel Sambuc     search(category->getReferencedProtocols());
2916f4a2713aSLionel Sambuc   }
2917f4a2713aSLionel Sambuc 
searchFrom(ObjCCategoryImplDecl * impl)2918f4a2713aSLionel Sambuc   void searchFrom(ObjCCategoryImplDecl *impl) {
2919f4a2713aSLionel Sambuc     // A method in a category definition that has a category
2920f4a2713aSLionel Sambuc     // declaration overrides declarations from the category
2921f4a2713aSLionel Sambuc     // declaration.
2922f4a2713aSLionel Sambuc     if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2923f4a2713aSLionel Sambuc       search(category);
2924f4a2713aSLionel Sambuc       if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2925f4a2713aSLionel Sambuc         search(Interface);
2926f4a2713aSLionel Sambuc 
2927f4a2713aSLionel Sambuc     // Otherwise it overrides declarations from the class.
2928f4a2713aSLionel Sambuc     } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2929f4a2713aSLionel Sambuc       search(Interface);
2930f4a2713aSLionel Sambuc     }
2931f4a2713aSLionel Sambuc   }
2932f4a2713aSLionel Sambuc 
searchFrom(ObjCInterfaceDecl * iface)2933f4a2713aSLionel Sambuc   void searchFrom(ObjCInterfaceDecl *iface) {
2934f4a2713aSLionel Sambuc     // A method in a class declaration overrides declarations from
2935f4a2713aSLionel Sambuc     if (!iface->hasDefinition())
2936f4a2713aSLionel Sambuc       return;
2937f4a2713aSLionel Sambuc 
2938f4a2713aSLionel Sambuc     //   - categories,
2939*0a6a1f1dSLionel Sambuc     for (auto *Cat : iface->known_categories())
2940*0a6a1f1dSLionel Sambuc       search(Cat);
2941f4a2713aSLionel Sambuc 
2942f4a2713aSLionel Sambuc     //   - the super class, and
2943f4a2713aSLionel Sambuc     if (ObjCInterfaceDecl *super = iface->getSuperClass())
2944f4a2713aSLionel Sambuc       search(super);
2945f4a2713aSLionel Sambuc 
2946f4a2713aSLionel Sambuc     //   - any referenced protocols.
2947f4a2713aSLionel Sambuc     search(iface->getReferencedProtocols());
2948f4a2713aSLionel Sambuc   }
2949f4a2713aSLionel Sambuc 
searchFrom(ObjCImplementationDecl * impl)2950f4a2713aSLionel Sambuc   void searchFrom(ObjCImplementationDecl *impl) {
2951f4a2713aSLionel Sambuc     // A method in a class implementation overrides declarations from
2952f4a2713aSLionel Sambuc     // the class interface.
2953f4a2713aSLionel Sambuc     if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2954f4a2713aSLionel Sambuc       search(Interface);
2955f4a2713aSLionel Sambuc   }
2956f4a2713aSLionel Sambuc 
2957f4a2713aSLionel Sambuc 
search(const ObjCProtocolList & protocols)2958f4a2713aSLionel Sambuc   void search(const ObjCProtocolList &protocols) {
2959f4a2713aSLionel Sambuc     for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2960f4a2713aSLionel Sambuc          i != e; ++i)
2961f4a2713aSLionel Sambuc       search(*i);
2962f4a2713aSLionel Sambuc   }
2963f4a2713aSLionel Sambuc 
search(ObjCContainerDecl * container)2964f4a2713aSLionel Sambuc   void search(ObjCContainerDecl *container) {
2965f4a2713aSLionel Sambuc     // Check for a method in this container which matches this selector.
2966f4a2713aSLionel Sambuc     ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2967f4a2713aSLionel Sambuc                                                 Method->isInstanceMethod(),
2968f4a2713aSLionel Sambuc                                                 /*AllowHidden=*/true);
2969f4a2713aSLionel Sambuc 
2970f4a2713aSLionel Sambuc     // If we find one, record it and bail out.
2971f4a2713aSLionel Sambuc     if (meth) {
2972f4a2713aSLionel Sambuc       Overridden.insert(meth);
2973f4a2713aSLionel Sambuc       return;
2974f4a2713aSLionel Sambuc     }
2975f4a2713aSLionel Sambuc 
2976f4a2713aSLionel Sambuc     // Otherwise, search for methods that a hypothetical method here
2977f4a2713aSLionel Sambuc     // would have overridden.
2978f4a2713aSLionel Sambuc 
2979f4a2713aSLionel Sambuc     // Note that we're now in a recursive case.
2980f4a2713aSLionel Sambuc     Recursive = true;
2981f4a2713aSLionel Sambuc 
2982f4a2713aSLionel Sambuc     searchFromContainer(container);
2983f4a2713aSLionel Sambuc   }
2984f4a2713aSLionel Sambuc };
2985f4a2713aSLionel Sambuc }
2986f4a2713aSLionel Sambuc 
CheckObjCMethodOverrides(ObjCMethodDecl * ObjCMethod,ObjCInterfaceDecl * CurrentClass,ResultTypeCompatibilityKind RTC)2987f4a2713aSLionel Sambuc void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2988f4a2713aSLionel Sambuc                                     ObjCInterfaceDecl *CurrentClass,
2989f4a2713aSLionel Sambuc                                     ResultTypeCompatibilityKind RTC) {
2990f4a2713aSLionel Sambuc   // Search for overridden methods and merge information down from them.
2991f4a2713aSLionel Sambuc   OverrideSearch overrides(*this, ObjCMethod);
2992f4a2713aSLionel Sambuc   // Keep track if the method overrides any method in the class's base classes,
2993f4a2713aSLionel Sambuc   // its protocols, or its categories' protocols; we will keep that info
2994f4a2713aSLionel Sambuc   // in the ObjCMethodDecl.
2995f4a2713aSLionel Sambuc   // For this info, a method in an implementation is not considered as
2996f4a2713aSLionel Sambuc   // overriding the same method in the interface or its categories.
2997f4a2713aSLionel Sambuc   bool hasOverriddenMethodsInBaseOrProtocol = false;
2998f4a2713aSLionel Sambuc   for (OverrideSearch::iterator
2999f4a2713aSLionel Sambuc          i = overrides.begin(), e = overrides.end(); i != e; ++i) {
3000f4a2713aSLionel Sambuc     ObjCMethodDecl *overridden = *i;
3001f4a2713aSLionel Sambuc 
3002f4a2713aSLionel Sambuc     if (!hasOverriddenMethodsInBaseOrProtocol) {
3003f4a2713aSLionel Sambuc       if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
3004f4a2713aSLionel Sambuc           CurrentClass != overridden->getClassInterface() ||
3005f4a2713aSLionel Sambuc           overridden->isOverriding()) {
3006f4a2713aSLionel Sambuc         hasOverriddenMethodsInBaseOrProtocol = true;
3007f4a2713aSLionel Sambuc 
3008f4a2713aSLionel Sambuc       } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) {
3009f4a2713aSLionel Sambuc         // OverrideSearch will return as "overridden" the same method in the
3010f4a2713aSLionel Sambuc         // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to
3011f4a2713aSLionel Sambuc         // check whether a category of a base class introduced a method with the
3012f4a2713aSLionel Sambuc         // same selector, after the interface method declaration.
3013f4a2713aSLionel Sambuc         // To avoid unnecessary lookups in the majority of cases, we use the
3014f4a2713aSLionel Sambuc         // extra info bits in GlobalMethodPool to check whether there were any
3015f4a2713aSLionel Sambuc         // category methods with this selector.
3016f4a2713aSLionel Sambuc         GlobalMethodPool::iterator It =
3017f4a2713aSLionel Sambuc             MethodPool.find(ObjCMethod->getSelector());
3018f4a2713aSLionel Sambuc         if (It != MethodPool.end()) {
3019f4a2713aSLionel Sambuc           ObjCMethodList &List =
3020f4a2713aSLionel Sambuc             ObjCMethod->isInstanceMethod()? It->second.first: It->second.second;
3021f4a2713aSLionel Sambuc           unsigned CategCount = List.getBits();
3022f4a2713aSLionel Sambuc           if (CategCount > 0) {
3023f4a2713aSLionel Sambuc             // If the method is in a category we'll do lookup if there were at
3024f4a2713aSLionel Sambuc             // least 2 category methods recorded, otherwise only one will do.
3025f4a2713aSLionel Sambuc             if (CategCount > 1 ||
3026f4a2713aSLionel Sambuc                 !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) {
3027f4a2713aSLionel Sambuc               OverrideSearch overrides(*this, overridden);
3028f4a2713aSLionel Sambuc               for (OverrideSearch::iterator
3029f4a2713aSLionel Sambuc                      OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) {
3030f4a2713aSLionel Sambuc                 ObjCMethodDecl *SuperOverridden = *OI;
3031f4a2713aSLionel Sambuc                 if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) ||
3032f4a2713aSLionel Sambuc                     CurrentClass != SuperOverridden->getClassInterface()) {
3033f4a2713aSLionel Sambuc                   hasOverriddenMethodsInBaseOrProtocol = true;
3034f4a2713aSLionel Sambuc                   overridden->setOverriding(true);
3035f4a2713aSLionel Sambuc                   break;
3036f4a2713aSLionel Sambuc                 }
3037f4a2713aSLionel Sambuc               }
3038f4a2713aSLionel Sambuc             }
3039f4a2713aSLionel Sambuc           }
3040f4a2713aSLionel Sambuc         }
3041f4a2713aSLionel Sambuc       }
3042f4a2713aSLionel Sambuc     }
3043f4a2713aSLionel Sambuc 
3044f4a2713aSLionel Sambuc     // Propagate down the 'related result type' bit from overridden methods.
3045f4a2713aSLionel Sambuc     if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
3046f4a2713aSLionel Sambuc       ObjCMethod->SetRelatedResultType();
3047f4a2713aSLionel Sambuc 
3048f4a2713aSLionel Sambuc     // Then merge the declarations.
3049f4a2713aSLionel Sambuc     mergeObjCMethodDecls(ObjCMethod, overridden);
3050f4a2713aSLionel Sambuc 
3051f4a2713aSLionel Sambuc     if (ObjCMethod->isImplicit() && overridden->isImplicit())
3052f4a2713aSLionel Sambuc       continue; // Conflicting properties are detected elsewhere.
3053f4a2713aSLionel Sambuc 
3054f4a2713aSLionel Sambuc     // Check for overriding methods
3055f4a2713aSLionel Sambuc     if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
3056f4a2713aSLionel Sambuc         isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
3057f4a2713aSLionel Sambuc       CheckConflictingOverridingMethod(ObjCMethod, overridden,
3058f4a2713aSLionel Sambuc               isa<ObjCProtocolDecl>(overridden->getDeclContext()));
3059f4a2713aSLionel Sambuc 
3060f4a2713aSLionel Sambuc     if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
3061f4a2713aSLionel Sambuc         isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
3062f4a2713aSLionel Sambuc         !overridden->isImplicit() /* not meant for properties */) {
3063f4a2713aSLionel Sambuc       ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
3064f4a2713aSLionel Sambuc                                           E = ObjCMethod->param_end();
3065f4a2713aSLionel Sambuc       ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
3066f4a2713aSLionel Sambuc                                      PrevE = overridden->param_end();
3067f4a2713aSLionel Sambuc       for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
3068f4a2713aSLionel Sambuc         assert(PrevI != overridden->param_end() && "Param mismatch");
3069f4a2713aSLionel Sambuc         QualType T1 = Context.getCanonicalType((*ParamI)->getType());
3070f4a2713aSLionel Sambuc         QualType T2 = Context.getCanonicalType((*PrevI)->getType());
3071f4a2713aSLionel Sambuc         // If type of argument of method in this class does not match its
3072f4a2713aSLionel Sambuc         // respective argument type in the super class method, issue warning;
3073f4a2713aSLionel Sambuc         if (!Context.typesAreCompatible(T1, T2)) {
3074f4a2713aSLionel Sambuc           Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
3075f4a2713aSLionel Sambuc             << T1 << T2;
3076f4a2713aSLionel Sambuc           Diag(overridden->getLocation(), diag::note_previous_declaration);
3077f4a2713aSLionel Sambuc           break;
3078f4a2713aSLionel Sambuc         }
3079f4a2713aSLionel Sambuc       }
3080f4a2713aSLionel Sambuc     }
3081f4a2713aSLionel Sambuc   }
3082f4a2713aSLionel Sambuc 
3083f4a2713aSLionel Sambuc   ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
3084f4a2713aSLionel Sambuc }
3085f4a2713aSLionel Sambuc 
ActOnMethodDeclaration(Scope * S,SourceLocation MethodLoc,SourceLocation EndLoc,tok::TokenKind MethodType,ObjCDeclSpec & ReturnQT,ParsedType ReturnType,ArrayRef<SourceLocation> SelectorLocs,Selector Sel,ObjCArgInfo * ArgInfo,DeclaratorChunk::ParamInfo * CParamInfo,unsigned CNumArgs,AttributeList * AttrList,tok::ObjCKeywordKind MethodDeclKind,bool isVariadic,bool MethodDefinition)3086f4a2713aSLionel Sambuc Decl *Sema::ActOnMethodDeclaration(
3087f4a2713aSLionel Sambuc     Scope *S,
3088f4a2713aSLionel Sambuc     SourceLocation MethodLoc, SourceLocation EndLoc,
3089f4a2713aSLionel Sambuc     tok::TokenKind MethodType,
3090f4a2713aSLionel Sambuc     ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
3091f4a2713aSLionel Sambuc     ArrayRef<SourceLocation> SelectorLocs,
3092f4a2713aSLionel Sambuc     Selector Sel,
3093f4a2713aSLionel Sambuc     // optional arguments. The number of types/arguments is obtained
3094f4a2713aSLionel Sambuc     // from the Sel.getNumArgs().
3095f4a2713aSLionel Sambuc     ObjCArgInfo *ArgInfo,
3096f4a2713aSLionel Sambuc     DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
3097f4a2713aSLionel Sambuc     AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
3098f4a2713aSLionel Sambuc     bool isVariadic, bool MethodDefinition) {
3099f4a2713aSLionel Sambuc   // Make sure we can establish a context for the method.
3100f4a2713aSLionel Sambuc   if (!CurContext->isObjCContainer()) {
3101f4a2713aSLionel Sambuc     Diag(MethodLoc, diag::error_missing_method_context);
3102*0a6a1f1dSLionel Sambuc     return nullptr;
3103f4a2713aSLionel Sambuc   }
3104f4a2713aSLionel Sambuc   ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
3105f4a2713aSLionel Sambuc   Decl *ClassDecl = cast<Decl>(OCD);
3106f4a2713aSLionel Sambuc   QualType resultDeclType;
3107f4a2713aSLionel Sambuc 
3108f4a2713aSLionel Sambuc   bool HasRelatedResultType = false;
3109*0a6a1f1dSLionel Sambuc   TypeSourceInfo *ReturnTInfo = nullptr;
3110f4a2713aSLionel Sambuc   if (ReturnType) {
3111*0a6a1f1dSLionel Sambuc     resultDeclType = GetTypeFromParser(ReturnType, &ReturnTInfo);
3112f4a2713aSLionel Sambuc 
3113f4a2713aSLionel Sambuc     if (CheckFunctionReturnType(resultDeclType, MethodLoc))
3114*0a6a1f1dSLionel Sambuc       return nullptr;
3115f4a2713aSLionel Sambuc 
3116f4a2713aSLionel Sambuc     HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
3117f4a2713aSLionel Sambuc   } else { // get the type for "id".
3118f4a2713aSLionel Sambuc     resultDeclType = Context.getObjCIdType();
3119f4a2713aSLionel Sambuc     Diag(MethodLoc, diag::warn_missing_method_return_type)
3120f4a2713aSLionel Sambuc       << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
3121f4a2713aSLionel Sambuc   }
3122f4a2713aSLionel Sambuc 
3123*0a6a1f1dSLionel Sambuc   ObjCMethodDecl *ObjCMethod = ObjCMethodDecl::Create(
3124*0a6a1f1dSLionel Sambuc       Context, MethodLoc, EndLoc, Sel, resultDeclType, ReturnTInfo, CurContext,
3125f4a2713aSLionel Sambuc       MethodType == tok::minus, isVariadic,
3126f4a2713aSLionel Sambuc       /*isPropertyAccessor=*/false,
3127f4a2713aSLionel Sambuc       /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
3128*0a6a1f1dSLionel Sambuc       MethodDeclKind == tok::objc_optional ? ObjCMethodDecl::Optional
3129f4a2713aSLionel Sambuc                                            : ObjCMethodDecl::Required,
3130f4a2713aSLionel Sambuc       HasRelatedResultType);
3131f4a2713aSLionel Sambuc 
3132f4a2713aSLionel Sambuc   SmallVector<ParmVarDecl*, 16> Params;
3133f4a2713aSLionel Sambuc 
3134f4a2713aSLionel Sambuc   for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
3135f4a2713aSLionel Sambuc     QualType ArgType;
3136f4a2713aSLionel Sambuc     TypeSourceInfo *DI;
3137f4a2713aSLionel Sambuc 
3138f4a2713aSLionel Sambuc     if (!ArgInfo[i].Type) {
3139f4a2713aSLionel Sambuc       ArgType = Context.getObjCIdType();
3140*0a6a1f1dSLionel Sambuc       DI = nullptr;
3141f4a2713aSLionel Sambuc     } else {
3142f4a2713aSLionel Sambuc       ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
3143f4a2713aSLionel Sambuc     }
3144f4a2713aSLionel Sambuc 
3145f4a2713aSLionel Sambuc     LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
3146f4a2713aSLionel Sambuc                    LookupOrdinaryName, ForRedeclaration);
3147f4a2713aSLionel Sambuc     LookupName(R, S);
3148f4a2713aSLionel Sambuc     if (R.isSingleResult()) {
3149f4a2713aSLionel Sambuc       NamedDecl *PrevDecl = R.getFoundDecl();
3150f4a2713aSLionel Sambuc       if (S->isDeclScope(PrevDecl)) {
3151f4a2713aSLionel Sambuc         Diag(ArgInfo[i].NameLoc,
3152f4a2713aSLionel Sambuc              (MethodDefinition ? diag::warn_method_param_redefinition
3153f4a2713aSLionel Sambuc                                : diag::warn_method_param_declaration))
3154f4a2713aSLionel Sambuc           << ArgInfo[i].Name;
3155f4a2713aSLionel Sambuc         Diag(PrevDecl->getLocation(),
3156f4a2713aSLionel Sambuc              diag::note_previous_declaration);
3157f4a2713aSLionel Sambuc       }
3158f4a2713aSLionel Sambuc     }
3159f4a2713aSLionel Sambuc 
3160f4a2713aSLionel Sambuc     SourceLocation StartLoc = DI
3161f4a2713aSLionel Sambuc       ? DI->getTypeLoc().getBeginLoc()
3162f4a2713aSLionel Sambuc       : ArgInfo[i].NameLoc;
3163f4a2713aSLionel Sambuc 
3164f4a2713aSLionel Sambuc     ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
3165f4a2713aSLionel Sambuc                                         ArgInfo[i].NameLoc, ArgInfo[i].Name,
3166f4a2713aSLionel Sambuc                                         ArgType, DI, SC_None);
3167f4a2713aSLionel Sambuc 
3168f4a2713aSLionel Sambuc     Param->setObjCMethodScopeInfo(i);
3169f4a2713aSLionel Sambuc 
3170f4a2713aSLionel Sambuc     Param->setObjCDeclQualifier(
3171f4a2713aSLionel Sambuc       CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
3172f4a2713aSLionel Sambuc 
3173f4a2713aSLionel Sambuc     // Apply the attributes to the parameter.
3174f4a2713aSLionel Sambuc     ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
3175f4a2713aSLionel Sambuc 
3176f4a2713aSLionel Sambuc     if (Param->hasAttr<BlocksAttr>()) {
3177f4a2713aSLionel Sambuc       Diag(Param->getLocation(), diag::err_block_on_nonlocal);
3178f4a2713aSLionel Sambuc       Param->setInvalidDecl();
3179f4a2713aSLionel Sambuc     }
3180f4a2713aSLionel Sambuc     S->AddDecl(Param);
3181f4a2713aSLionel Sambuc     IdResolver.AddDecl(Param);
3182f4a2713aSLionel Sambuc 
3183f4a2713aSLionel Sambuc     Params.push_back(Param);
3184f4a2713aSLionel Sambuc   }
3185f4a2713aSLionel Sambuc 
3186f4a2713aSLionel Sambuc   for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
3187f4a2713aSLionel Sambuc     ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
3188f4a2713aSLionel Sambuc     QualType ArgType = Param->getType();
3189f4a2713aSLionel Sambuc     if (ArgType.isNull())
3190f4a2713aSLionel Sambuc       ArgType = Context.getObjCIdType();
3191f4a2713aSLionel Sambuc     else
3192f4a2713aSLionel Sambuc       // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
3193f4a2713aSLionel Sambuc       ArgType = Context.getAdjustedParameterType(ArgType);
3194f4a2713aSLionel Sambuc 
3195f4a2713aSLionel Sambuc     Param->setDeclContext(ObjCMethod);
3196f4a2713aSLionel Sambuc     Params.push_back(Param);
3197f4a2713aSLionel Sambuc   }
3198f4a2713aSLionel Sambuc 
3199f4a2713aSLionel Sambuc   ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
3200f4a2713aSLionel Sambuc   ObjCMethod->setObjCDeclQualifier(
3201f4a2713aSLionel Sambuc     CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
3202f4a2713aSLionel Sambuc 
3203f4a2713aSLionel Sambuc   if (AttrList)
3204f4a2713aSLionel Sambuc     ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
3205f4a2713aSLionel Sambuc 
3206f4a2713aSLionel Sambuc   // Add the method now.
3207*0a6a1f1dSLionel Sambuc   const ObjCMethodDecl *PrevMethod = nullptr;
3208f4a2713aSLionel Sambuc   if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
3209f4a2713aSLionel Sambuc     if (MethodType == tok::minus) {
3210f4a2713aSLionel Sambuc       PrevMethod = ImpDecl->getInstanceMethod(Sel);
3211f4a2713aSLionel Sambuc       ImpDecl->addInstanceMethod(ObjCMethod);
3212f4a2713aSLionel Sambuc     } else {
3213f4a2713aSLionel Sambuc       PrevMethod = ImpDecl->getClassMethod(Sel);
3214f4a2713aSLionel Sambuc       ImpDecl->addClassMethod(ObjCMethod);
3215f4a2713aSLionel Sambuc     }
3216f4a2713aSLionel Sambuc 
3217*0a6a1f1dSLionel Sambuc     ObjCMethodDecl *IMD = nullptr;
3218f4a2713aSLionel Sambuc     if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
3219f4a2713aSLionel Sambuc       IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
3220f4a2713aSLionel Sambuc                                 ObjCMethod->isInstanceMethod());
3221f4a2713aSLionel Sambuc     if (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>() &&
3222f4a2713aSLionel Sambuc         !ObjCMethod->hasAttr<ObjCRequiresSuperAttr>()) {
3223f4a2713aSLionel Sambuc       // merge the attribute into implementation.
3224*0a6a1f1dSLionel Sambuc       ObjCMethod->addAttr(ObjCRequiresSuperAttr::CreateImplicit(Context,
3225*0a6a1f1dSLionel Sambuc                                                    ObjCMethod->getLocation()));
3226f4a2713aSLionel Sambuc     }
3227*0a6a1f1dSLionel Sambuc     if (isa<ObjCCategoryImplDecl>(ImpDecl)) {
3228*0a6a1f1dSLionel Sambuc       ObjCMethodFamily family =
3229*0a6a1f1dSLionel Sambuc         ObjCMethod->getSelector().getMethodFamily();
3230*0a6a1f1dSLionel Sambuc       if (family == OMF_dealloc && IMD && IMD->isOverriding())
3231*0a6a1f1dSLionel Sambuc         Diag(ObjCMethod->getLocation(), diag::warn_dealloc_in_category)
3232f4a2713aSLionel Sambuc           << ObjCMethod->getDeclName();
3233f4a2713aSLionel Sambuc     }
3234f4a2713aSLionel Sambuc   } else {
3235f4a2713aSLionel Sambuc     cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
3236f4a2713aSLionel Sambuc   }
3237f4a2713aSLionel Sambuc 
3238f4a2713aSLionel Sambuc   if (PrevMethod) {
3239f4a2713aSLionel Sambuc     // You can never have two method definitions with the same name.
3240f4a2713aSLionel Sambuc     Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
3241f4a2713aSLionel Sambuc       << ObjCMethod->getDeclName();
3242f4a2713aSLionel Sambuc     Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
3243f4a2713aSLionel Sambuc     ObjCMethod->setInvalidDecl();
3244f4a2713aSLionel Sambuc     return ObjCMethod;
3245f4a2713aSLionel Sambuc   }
3246f4a2713aSLionel Sambuc 
3247f4a2713aSLionel Sambuc   // If this Objective-C method does not have a related result type, but we
3248f4a2713aSLionel Sambuc   // are allowed to infer related result types, try to do so based on the
3249f4a2713aSLionel Sambuc   // method family.
3250f4a2713aSLionel Sambuc   ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
3251f4a2713aSLionel Sambuc   if (!CurrentClass) {
3252f4a2713aSLionel Sambuc     if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3253f4a2713aSLionel Sambuc       CurrentClass = Cat->getClassInterface();
3254f4a2713aSLionel Sambuc     else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3255f4a2713aSLionel Sambuc       CurrentClass = Impl->getClassInterface();
3256f4a2713aSLionel Sambuc     else if (ObjCCategoryImplDecl *CatImpl
3257f4a2713aSLionel Sambuc                                    = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3258f4a2713aSLionel Sambuc       CurrentClass = CatImpl->getClassInterface();
3259f4a2713aSLionel Sambuc   }
3260f4a2713aSLionel Sambuc 
3261f4a2713aSLionel Sambuc   ResultTypeCompatibilityKind RTC
3262f4a2713aSLionel Sambuc     = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
3263f4a2713aSLionel Sambuc 
3264f4a2713aSLionel Sambuc   CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
3265f4a2713aSLionel Sambuc 
3266f4a2713aSLionel Sambuc   bool ARCError = false;
3267f4a2713aSLionel Sambuc   if (getLangOpts().ObjCAutoRefCount)
3268f4a2713aSLionel Sambuc     ARCError = CheckARCMethodDecl(ObjCMethod);
3269f4a2713aSLionel Sambuc 
3270f4a2713aSLionel Sambuc   // Infer the related result type when possible.
3271f4a2713aSLionel Sambuc   if (!ARCError && RTC == Sema::RTC_Compatible &&
3272f4a2713aSLionel Sambuc       !ObjCMethod->hasRelatedResultType() &&
3273f4a2713aSLionel Sambuc       LangOpts.ObjCInferRelatedResultType) {
3274f4a2713aSLionel Sambuc     bool InferRelatedResultType = false;
3275f4a2713aSLionel Sambuc     switch (ObjCMethod->getMethodFamily()) {
3276f4a2713aSLionel Sambuc     case OMF_None:
3277f4a2713aSLionel Sambuc     case OMF_copy:
3278f4a2713aSLionel Sambuc     case OMF_dealloc:
3279f4a2713aSLionel Sambuc     case OMF_finalize:
3280f4a2713aSLionel Sambuc     case OMF_mutableCopy:
3281f4a2713aSLionel Sambuc     case OMF_release:
3282f4a2713aSLionel Sambuc     case OMF_retainCount:
3283*0a6a1f1dSLionel Sambuc     case OMF_initialize:
3284f4a2713aSLionel Sambuc     case OMF_performSelector:
3285f4a2713aSLionel Sambuc       break;
3286f4a2713aSLionel Sambuc 
3287f4a2713aSLionel Sambuc     case OMF_alloc:
3288f4a2713aSLionel Sambuc     case OMF_new:
3289f4a2713aSLionel Sambuc       InferRelatedResultType = ObjCMethod->isClassMethod();
3290f4a2713aSLionel Sambuc       break;
3291f4a2713aSLionel Sambuc 
3292f4a2713aSLionel Sambuc     case OMF_init:
3293f4a2713aSLionel Sambuc     case OMF_autorelease:
3294f4a2713aSLionel Sambuc     case OMF_retain:
3295f4a2713aSLionel Sambuc     case OMF_self:
3296f4a2713aSLionel Sambuc       InferRelatedResultType = ObjCMethod->isInstanceMethod();
3297f4a2713aSLionel Sambuc       break;
3298f4a2713aSLionel Sambuc     }
3299f4a2713aSLionel Sambuc 
3300f4a2713aSLionel Sambuc     if (InferRelatedResultType)
3301f4a2713aSLionel Sambuc       ObjCMethod->SetRelatedResultType();
3302f4a2713aSLionel Sambuc   }
3303f4a2713aSLionel Sambuc 
3304f4a2713aSLionel Sambuc   ActOnDocumentableDecl(ObjCMethod);
3305f4a2713aSLionel Sambuc 
3306f4a2713aSLionel Sambuc   return ObjCMethod;
3307f4a2713aSLionel Sambuc }
3308f4a2713aSLionel Sambuc 
CheckObjCDeclScope(Decl * D)3309f4a2713aSLionel Sambuc bool Sema::CheckObjCDeclScope(Decl *D) {
3310f4a2713aSLionel Sambuc   // Following is also an error. But it is caused by a missing @end
3311f4a2713aSLionel Sambuc   // and diagnostic is issued elsewhere.
3312f4a2713aSLionel Sambuc   if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
3313f4a2713aSLionel Sambuc     return false;
3314f4a2713aSLionel Sambuc 
3315f4a2713aSLionel Sambuc   // If we switched context to translation unit while we are still lexically in
3316f4a2713aSLionel Sambuc   // an objc container, it means the parser missed emitting an error.
3317f4a2713aSLionel Sambuc   if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3318f4a2713aSLionel Sambuc     return false;
3319f4a2713aSLionel Sambuc 
3320f4a2713aSLionel Sambuc   Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3321f4a2713aSLionel Sambuc   D->setInvalidDecl();
3322f4a2713aSLionel Sambuc 
3323f4a2713aSLionel Sambuc   return true;
3324f4a2713aSLionel Sambuc }
3325f4a2713aSLionel Sambuc 
3326f4a2713aSLionel Sambuc /// Called whenever \@defs(ClassName) is encountered in the source.  Inserts the
3327f4a2713aSLionel Sambuc /// instance variables of ClassName into Decls.
ActOnDefs(Scope * S,Decl * TagD,SourceLocation DeclStart,IdentifierInfo * ClassName,SmallVectorImpl<Decl * > & Decls)3328f4a2713aSLionel Sambuc void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
3329f4a2713aSLionel Sambuc                      IdentifierInfo *ClassName,
3330f4a2713aSLionel Sambuc                      SmallVectorImpl<Decl*> &Decls) {
3331f4a2713aSLionel Sambuc   // Check that ClassName is a valid class
3332f4a2713aSLionel Sambuc   ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
3333f4a2713aSLionel Sambuc   if (!Class) {
3334f4a2713aSLionel Sambuc     Diag(DeclStart, diag::err_undef_interface) << ClassName;
3335f4a2713aSLionel Sambuc     return;
3336f4a2713aSLionel Sambuc   }
3337f4a2713aSLionel Sambuc   if (LangOpts.ObjCRuntime.isNonFragile()) {
3338f4a2713aSLionel Sambuc     Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3339f4a2713aSLionel Sambuc     return;
3340f4a2713aSLionel Sambuc   }
3341f4a2713aSLionel Sambuc 
3342f4a2713aSLionel Sambuc   // Collect the instance variables
3343f4a2713aSLionel Sambuc   SmallVector<const ObjCIvarDecl*, 32> Ivars;
3344f4a2713aSLionel Sambuc   Context.DeepCollectObjCIvars(Class, true, Ivars);
3345f4a2713aSLionel Sambuc   // For each ivar, create a fresh ObjCAtDefsFieldDecl.
3346f4a2713aSLionel Sambuc   for (unsigned i = 0; i < Ivars.size(); i++) {
3347f4a2713aSLionel Sambuc     const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
3348f4a2713aSLionel Sambuc     RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
3349f4a2713aSLionel Sambuc     Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3350f4a2713aSLionel Sambuc                                            /*FIXME: StartL=*/ID->getLocation(),
3351f4a2713aSLionel Sambuc                                            ID->getLocation(),
3352f4a2713aSLionel Sambuc                                            ID->getIdentifier(), ID->getType(),
3353f4a2713aSLionel Sambuc                                            ID->getBitWidth());
3354f4a2713aSLionel Sambuc     Decls.push_back(FD);
3355f4a2713aSLionel Sambuc   }
3356f4a2713aSLionel Sambuc 
3357f4a2713aSLionel Sambuc   // Introduce all of these fields into the appropriate scope.
3358f4a2713aSLionel Sambuc   for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
3359f4a2713aSLionel Sambuc        D != Decls.end(); ++D) {
3360f4a2713aSLionel Sambuc     FieldDecl *FD = cast<FieldDecl>(*D);
3361f4a2713aSLionel Sambuc     if (getLangOpts().CPlusPlus)
3362f4a2713aSLionel Sambuc       PushOnScopeChains(cast<FieldDecl>(FD), S);
3363f4a2713aSLionel Sambuc     else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
3364f4a2713aSLionel Sambuc       Record->addDecl(FD);
3365f4a2713aSLionel Sambuc   }
3366f4a2713aSLionel Sambuc }
3367f4a2713aSLionel Sambuc 
3368f4a2713aSLionel Sambuc /// \brief Build a type-check a new Objective-C exception variable declaration.
BuildObjCExceptionDecl(TypeSourceInfo * TInfo,QualType T,SourceLocation StartLoc,SourceLocation IdLoc,IdentifierInfo * Id,bool Invalid)3369f4a2713aSLionel Sambuc VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3370f4a2713aSLionel Sambuc                                       SourceLocation StartLoc,
3371f4a2713aSLionel Sambuc                                       SourceLocation IdLoc,
3372f4a2713aSLionel Sambuc                                       IdentifierInfo *Id,
3373f4a2713aSLionel Sambuc                                       bool Invalid) {
3374f4a2713aSLionel Sambuc   // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3375f4a2713aSLionel Sambuc   // duration shall not be qualified by an address-space qualifier."
3376f4a2713aSLionel Sambuc   // Since all parameters have automatic store duration, they can not have
3377f4a2713aSLionel Sambuc   // an address space.
3378f4a2713aSLionel Sambuc   if (T.getAddressSpace() != 0) {
3379f4a2713aSLionel Sambuc     Diag(IdLoc, diag::err_arg_with_address_space);
3380f4a2713aSLionel Sambuc     Invalid = true;
3381f4a2713aSLionel Sambuc   }
3382f4a2713aSLionel Sambuc 
3383f4a2713aSLionel Sambuc   // An @catch parameter must be an unqualified object pointer type;
3384f4a2713aSLionel Sambuc   // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3385f4a2713aSLionel Sambuc   if (Invalid) {
3386f4a2713aSLionel Sambuc     // Don't do any further checking.
3387f4a2713aSLionel Sambuc   } else if (T->isDependentType()) {
3388f4a2713aSLionel Sambuc     // Okay: we don't know what this type will instantiate to.
3389f4a2713aSLionel Sambuc   } else if (!T->isObjCObjectPointerType()) {
3390f4a2713aSLionel Sambuc     Invalid = true;
3391f4a2713aSLionel Sambuc     Diag(IdLoc ,diag::err_catch_param_not_objc_type);
3392f4a2713aSLionel Sambuc   } else if (T->isObjCQualifiedIdType()) {
3393f4a2713aSLionel Sambuc     Invalid = true;
3394f4a2713aSLionel Sambuc     Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
3395f4a2713aSLionel Sambuc   }
3396f4a2713aSLionel Sambuc 
3397f4a2713aSLionel Sambuc   VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
3398f4a2713aSLionel Sambuc                                  T, TInfo, SC_None);
3399f4a2713aSLionel Sambuc   New->setExceptionVariable(true);
3400f4a2713aSLionel Sambuc 
3401f4a2713aSLionel Sambuc   // In ARC, infer 'retaining' for variables of retainable type.
3402f4a2713aSLionel Sambuc   if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
3403f4a2713aSLionel Sambuc     Invalid = true;
3404f4a2713aSLionel Sambuc 
3405f4a2713aSLionel Sambuc   if (Invalid)
3406f4a2713aSLionel Sambuc     New->setInvalidDecl();
3407f4a2713aSLionel Sambuc   return New;
3408f4a2713aSLionel Sambuc }
3409f4a2713aSLionel Sambuc 
ActOnObjCExceptionDecl(Scope * S,Declarator & D)3410f4a2713aSLionel Sambuc Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
3411f4a2713aSLionel Sambuc   const DeclSpec &DS = D.getDeclSpec();
3412f4a2713aSLionel Sambuc 
3413f4a2713aSLionel Sambuc   // We allow the "register" storage class on exception variables because
3414f4a2713aSLionel Sambuc   // GCC did, but we drop it completely. Any other storage class is an error.
3415f4a2713aSLionel Sambuc   if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3416f4a2713aSLionel Sambuc     Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3417f4a2713aSLionel Sambuc       << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
3418f4a2713aSLionel Sambuc   } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) {
3419f4a2713aSLionel Sambuc     Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
3420f4a2713aSLionel Sambuc       << DeclSpec::getSpecifierName(SCS);
3421f4a2713aSLionel Sambuc   }
3422f4a2713aSLionel Sambuc   if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec())
3423f4a2713aSLionel Sambuc     Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(),
3424f4a2713aSLionel Sambuc          diag::err_invalid_thread)
3425f4a2713aSLionel Sambuc      << DeclSpec::getSpecifierName(TSCS);
3426f4a2713aSLionel Sambuc   D.getMutableDeclSpec().ClearStorageClassSpecs();
3427f4a2713aSLionel Sambuc 
3428f4a2713aSLionel Sambuc   DiagnoseFunctionSpecifiers(D.getDeclSpec());
3429f4a2713aSLionel Sambuc 
3430f4a2713aSLionel Sambuc   // Check that there are no default arguments inside the type of this
3431f4a2713aSLionel Sambuc   // exception object (C++ only).
3432f4a2713aSLionel Sambuc   if (getLangOpts().CPlusPlus)
3433f4a2713aSLionel Sambuc     CheckExtraCXXDefaultArguments(D);
3434f4a2713aSLionel Sambuc 
3435f4a2713aSLionel Sambuc   TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
3436f4a2713aSLionel Sambuc   QualType ExceptionType = TInfo->getType();
3437f4a2713aSLionel Sambuc 
3438f4a2713aSLionel Sambuc   VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3439f4a2713aSLionel Sambuc                                         D.getSourceRange().getBegin(),
3440f4a2713aSLionel Sambuc                                         D.getIdentifierLoc(),
3441f4a2713aSLionel Sambuc                                         D.getIdentifier(),
3442f4a2713aSLionel Sambuc                                         D.isInvalidType());
3443f4a2713aSLionel Sambuc 
3444f4a2713aSLionel Sambuc   // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3445f4a2713aSLionel Sambuc   if (D.getCXXScopeSpec().isSet()) {
3446f4a2713aSLionel Sambuc     Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3447f4a2713aSLionel Sambuc       << D.getCXXScopeSpec().getRange();
3448f4a2713aSLionel Sambuc     New->setInvalidDecl();
3449f4a2713aSLionel Sambuc   }
3450f4a2713aSLionel Sambuc 
3451f4a2713aSLionel Sambuc   // Add the parameter declaration into this scope.
3452f4a2713aSLionel Sambuc   S->AddDecl(New);
3453f4a2713aSLionel Sambuc   if (D.getIdentifier())
3454f4a2713aSLionel Sambuc     IdResolver.AddDecl(New);
3455f4a2713aSLionel Sambuc 
3456f4a2713aSLionel Sambuc   ProcessDeclAttributes(S, New, D);
3457f4a2713aSLionel Sambuc 
3458f4a2713aSLionel Sambuc   if (New->hasAttr<BlocksAttr>())
3459f4a2713aSLionel Sambuc     Diag(New->getLocation(), diag::err_block_on_nonlocal);
3460f4a2713aSLionel Sambuc   return New;
3461f4a2713aSLionel Sambuc }
3462f4a2713aSLionel Sambuc 
3463f4a2713aSLionel Sambuc /// CollectIvarsToConstructOrDestruct - Collect those ivars which require
3464f4a2713aSLionel Sambuc /// initialization.
CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl * OI,SmallVectorImpl<ObjCIvarDecl * > & Ivars)3465f4a2713aSLionel Sambuc void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
3466f4a2713aSLionel Sambuc                                 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
3467f4a2713aSLionel Sambuc   for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3468f4a2713aSLionel Sambuc        Iv= Iv->getNextIvar()) {
3469f4a2713aSLionel Sambuc     QualType QT = Context.getBaseElementType(Iv->getType());
3470f4a2713aSLionel Sambuc     if (QT->isRecordType())
3471f4a2713aSLionel Sambuc       Ivars.push_back(Iv);
3472f4a2713aSLionel Sambuc   }
3473f4a2713aSLionel Sambuc }
3474f4a2713aSLionel Sambuc 
DiagnoseUseOfUnimplementedSelectors()3475f4a2713aSLionel Sambuc void Sema::DiagnoseUseOfUnimplementedSelectors() {
3476f4a2713aSLionel Sambuc   // Load referenced selectors from the external source.
3477f4a2713aSLionel Sambuc   if (ExternalSource) {
3478f4a2713aSLionel Sambuc     SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3479f4a2713aSLionel Sambuc     ExternalSource->ReadReferencedSelectors(Sels);
3480f4a2713aSLionel Sambuc     for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3481f4a2713aSLionel Sambuc       ReferencedSelectors[Sels[I].first] = Sels[I].second;
3482f4a2713aSLionel Sambuc   }
3483f4a2713aSLionel Sambuc 
3484f4a2713aSLionel Sambuc   // Warning will be issued only when selector table is
3485f4a2713aSLionel Sambuc   // generated (which means there is at lease one implementation
3486f4a2713aSLionel Sambuc   // in the TU). This is to match gcc's behavior.
3487f4a2713aSLionel Sambuc   if (ReferencedSelectors.empty() ||
3488f4a2713aSLionel Sambuc       !Context.AnyObjCImplementation())
3489f4a2713aSLionel Sambuc     return;
3490f4a2713aSLionel Sambuc   for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3491f4a2713aSLionel Sambuc         ReferencedSelectors.begin(),
3492f4a2713aSLionel Sambuc        E = ReferencedSelectors.end(); S != E; ++S) {
3493f4a2713aSLionel Sambuc     Selector Sel = (*S).first;
3494f4a2713aSLionel Sambuc     if (!LookupImplementedMethodInGlobalPool(Sel))
3495f4a2713aSLionel Sambuc       Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3496f4a2713aSLionel Sambuc   }
3497f4a2713aSLionel Sambuc   return;
3498f4a2713aSLionel Sambuc }
3499f4a2713aSLionel Sambuc 
3500f4a2713aSLionel Sambuc ObjCIvarDecl *
GetIvarBackingPropertyAccessor(const ObjCMethodDecl * Method,const ObjCPropertyDecl * & PDecl) const3501f4a2713aSLionel Sambuc Sema::GetIvarBackingPropertyAccessor(const ObjCMethodDecl *Method,
3502f4a2713aSLionel Sambuc                                      const ObjCPropertyDecl *&PDecl) const {
3503*0a6a1f1dSLionel Sambuc   if (Method->isClassMethod())
3504*0a6a1f1dSLionel Sambuc     return nullptr;
3505f4a2713aSLionel Sambuc   const ObjCInterfaceDecl *IDecl = Method->getClassInterface();
3506f4a2713aSLionel Sambuc   if (!IDecl)
3507*0a6a1f1dSLionel Sambuc     return nullptr;
3508*0a6a1f1dSLionel Sambuc   Method = IDecl->lookupMethod(Method->getSelector(), /*isInstance=*/true,
3509*0a6a1f1dSLionel Sambuc                                /*shallowCategoryLookup=*/false,
3510*0a6a1f1dSLionel Sambuc                                /*followSuper=*/false);
3511f4a2713aSLionel Sambuc   if (!Method || !Method->isPropertyAccessor())
3512*0a6a1f1dSLionel Sambuc     return nullptr;
3513*0a6a1f1dSLionel Sambuc   if ((PDecl = Method->findPropertyDecl()))
3514*0a6a1f1dSLionel Sambuc     if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) {
3515*0a6a1f1dSLionel Sambuc       // property backing ivar must belong to property's class
3516*0a6a1f1dSLionel Sambuc       // or be a private ivar in class's implementation.
3517*0a6a1f1dSLionel Sambuc       // FIXME. fix the const-ness issue.
3518*0a6a1f1dSLionel Sambuc       IV = const_cast<ObjCInterfaceDecl *>(IDecl)->lookupInstanceVariable(
3519*0a6a1f1dSLionel Sambuc                                                         IV->getIdentifier());
3520*0a6a1f1dSLionel Sambuc       return IV;
3521f4a2713aSLionel Sambuc     }
3522*0a6a1f1dSLionel Sambuc   return nullptr;
3523f4a2713aSLionel Sambuc }
3524f4a2713aSLionel Sambuc 
3525*0a6a1f1dSLionel Sambuc namespace {
3526*0a6a1f1dSLionel Sambuc   /// Used by Sema::DiagnoseUnusedBackingIvarInAccessor to check if a property
3527*0a6a1f1dSLionel Sambuc   /// accessor references the backing ivar.
3528*0a6a1f1dSLionel Sambuc   class UnusedBackingIvarChecker :
3529*0a6a1f1dSLionel Sambuc       public DataRecursiveASTVisitor<UnusedBackingIvarChecker> {
3530*0a6a1f1dSLionel Sambuc   public:
3531*0a6a1f1dSLionel Sambuc     Sema &S;
3532*0a6a1f1dSLionel Sambuc     const ObjCMethodDecl *Method;
3533*0a6a1f1dSLionel Sambuc     const ObjCIvarDecl *IvarD;
3534*0a6a1f1dSLionel Sambuc     bool AccessedIvar;
3535*0a6a1f1dSLionel Sambuc     bool InvokedSelfMethod;
3536*0a6a1f1dSLionel Sambuc 
UnusedBackingIvarChecker(Sema & S,const ObjCMethodDecl * Method,const ObjCIvarDecl * IvarD)3537*0a6a1f1dSLionel Sambuc     UnusedBackingIvarChecker(Sema &S, const ObjCMethodDecl *Method,
3538*0a6a1f1dSLionel Sambuc                              const ObjCIvarDecl *IvarD)
3539*0a6a1f1dSLionel Sambuc       : S(S), Method(Method), IvarD(IvarD),
3540*0a6a1f1dSLionel Sambuc         AccessedIvar(false), InvokedSelfMethod(false) {
3541*0a6a1f1dSLionel Sambuc       assert(IvarD);
3542*0a6a1f1dSLionel Sambuc     }
3543*0a6a1f1dSLionel Sambuc 
VisitObjCIvarRefExpr(ObjCIvarRefExpr * E)3544*0a6a1f1dSLionel Sambuc     bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
3545*0a6a1f1dSLionel Sambuc       if (E->getDecl() == IvarD) {
3546*0a6a1f1dSLionel Sambuc         AccessedIvar = true;
3547*0a6a1f1dSLionel Sambuc         return false;
3548*0a6a1f1dSLionel Sambuc       }
3549*0a6a1f1dSLionel Sambuc       return true;
3550*0a6a1f1dSLionel Sambuc     }
3551*0a6a1f1dSLionel Sambuc 
VisitObjCMessageExpr(ObjCMessageExpr * E)3552*0a6a1f1dSLionel Sambuc     bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
3553*0a6a1f1dSLionel Sambuc       if (E->getReceiverKind() == ObjCMessageExpr::Instance &&
3554*0a6a1f1dSLionel Sambuc           S.isSelfExpr(E->getInstanceReceiver(), Method)) {
3555*0a6a1f1dSLionel Sambuc         InvokedSelfMethod = true;
3556*0a6a1f1dSLionel Sambuc       }
3557*0a6a1f1dSLionel Sambuc       return true;
3558*0a6a1f1dSLionel Sambuc     }
3559*0a6a1f1dSLionel Sambuc   };
3560*0a6a1f1dSLionel Sambuc }
3561*0a6a1f1dSLionel Sambuc 
DiagnoseUnusedBackingIvarInAccessor(Scope * S,const ObjCImplementationDecl * ImplD)3562*0a6a1f1dSLionel Sambuc void Sema::DiagnoseUnusedBackingIvarInAccessor(Scope *S,
3563*0a6a1f1dSLionel Sambuc                                           const ObjCImplementationDecl *ImplD) {
3564*0a6a1f1dSLionel Sambuc   if (S->hasUnrecoverableErrorOccurred())
3565f4a2713aSLionel Sambuc     return;
3566f4a2713aSLionel Sambuc 
3567*0a6a1f1dSLionel Sambuc   for (const auto *CurMethod : ImplD->instance_methods()) {
3568*0a6a1f1dSLionel Sambuc     unsigned DIAG = diag::warn_unused_property_backing_ivar;
3569*0a6a1f1dSLionel Sambuc     SourceLocation Loc = CurMethod->getLocation();
3570*0a6a1f1dSLionel Sambuc     if (Diags.isIgnored(DIAG, Loc))
3571*0a6a1f1dSLionel Sambuc       continue;
3572*0a6a1f1dSLionel Sambuc 
3573f4a2713aSLionel Sambuc     const ObjCPropertyDecl *PDecl;
3574f4a2713aSLionel Sambuc     const ObjCIvarDecl *IV = GetIvarBackingPropertyAccessor(CurMethod, PDecl);
3575*0a6a1f1dSLionel Sambuc     if (!IV)
3576*0a6a1f1dSLionel Sambuc       continue;
3577*0a6a1f1dSLionel Sambuc 
3578*0a6a1f1dSLionel Sambuc     UnusedBackingIvarChecker Checker(*this, CurMethod, IV);
3579*0a6a1f1dSLionel Sambuc     Checker.TraverseStmt(CurMethod->getBody());
3580*0a6a1f1dSLionel Sambuc     if (Checker.AccessedIvar)
3581*0a6a1f1dSLionel Sambuc       continue;
3582*0a6a1f1dSLionel Sambuc 
3583*0a6a1f1dSLionel Sambuc     // Do not issue this warning if backing ivar is used somewhere and accessor
3584*0a6a1f1dSLionel Sambuc     // implementation makes a self call. This is to prevent false positive in
3585*0a6a1f1dSLionel Sambuc     // cases where the ivar is accessed by another method that the accessor
3586*0a6a1f1dSLionel Sambuc     // delegates to.
3587*0a6a1f1dSLionel Sambuc     if (!IV->isReferenced() || !Checker.InvokedSelfMethod) {
3588*0a6a1f1dSLionel Sambuc       Diag(Loc, DIAG) << IV;
3589f4a2713aSLionel Sambuc       Diag(PDecl->getLocation(), diag::note_property_declare);
3590f4a2713aSLionel Sambuc     }
3591f4a2713aSLionel Sambuc   }
3592*0a6a1f1dSLionel Sambuc }
3593