SemaDeclObjC.cpp revision fefe91e6017321aa7db431b94cb3615f6c97da7a
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements semantic analysis for Objective C declarations.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Sema/SemaInternal.h"
15bd3606426d389370616126af969904ec493cb105Chris Lattner#include "clang/Sema/Lookup.h"
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Sema/ExternalSemaSource.h"
170dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "clang/Sema/Scope.h"
18af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar#include "clang/Sema/ScopeInfo.h"
195f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor#include "clang/AST/ASTConsumer.h"
20bd3606426d389370616126af969904ec493cb105Chris Lattner#include "clang/AST/Expr.h"
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ExprObjC.h"
22c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/ASTContext.h"
2321ef7ae45c8b91f23cf5eab2263421bb398a644bChris Lattner#include "clang/AST/DeclObjC.h"
241b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/SourceManager.h"
252c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner#include "clang/Sema/DeclSpec.h"
268bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman#include "llvm/ADT/DenseSet.h"
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
28e9b7d8ace8674585818990cff585daae7745bd88Steve Naroffusing namespace clang;
29ec9426ca6039279bcc99bc2c625bb2abe4f0353dNate Begeman
30bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner/// Check whether the given method, which must be in the 'init'
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// family, is a valid member of that family.
3220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov///
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// \param receiverTypeIfCall - if null, check this as if declaring it;
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///   if non-null, check this as if making a call to it with the given
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///   receiver type
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
37bd3606426d389370616126af969904ec493cb105Chris Lattner/// \return true to indicate that there was an error and appropriate
38fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner///   actions were taken
39bd3606426d389370616126af969904ec493cb105Chris Lattnerbool Sema::checkInitMethod(ObjCMethodDecl *method,
40bd3606426d389370616126af969904ec493cb105Chris Lattner                           QualType receiverTypeIfCall) {
41bd3606426d389370616126af969904ec493cb105Chris Lattner  if (method->isInvalidDecl()) return true;
422a998148a6823c44d67da347c95eb2ea21f6b986Mike Stump
43a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson  // This castAs is safe: methods that don't return an object
44a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson  // pointer won't be inferred as inits and will reject an explicit
45208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar  // objc_method_family(init).
463c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner
473c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner  // We ignore protocols here.  Should we?  What about Class?
483c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner
493c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner  const ObjCObjectType *result = method->getResultType()
503c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    ->castAs<ObjCObjectPointerType>()->getObjectType();
513c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner
523c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner  if (result->isObjCId()) {
533c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    return false;
54e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta  } else if (result->isObjCClass()) {
55e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta    // fall through: always an error
566dba432c7b862c2219e5d6e52b0cd188fbf84b01Devang Patel  } else {
572b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner    ObjCInterfaceDecl *resultClass = result->getInterface();
582b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner    assert(resultClass && "unexpected object type!");
592b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner
60815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek    // It's okay for the result type to still be a forward declaration
61815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek    // if we're checking an interface declaration.
62815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek    if (resultClass->isForwardDecl()) {
63815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek      if (receiverTypeIfCall.isNull() &&
64815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek          !isa<ObjCImplementationDecl>(method->getDeclContext()))
6589ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson        return false;
6689ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson
6789ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson    // Otherwise, we try to compare class types.
6882227ff4eb665bbf41720ebdc0dc9215a86ba838Chris Lattner    } else {
69208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar      // If this method was declared in a protocol, we can't check
70208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar      // anything unless we have a receiver type that's an interface.
71208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar      const ObjCInterfaceDecl *receiverClass = 0;
726bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar      if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
736bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar        if (receiverTypeIfCall.isNull())
74532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman          return false;
750269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
76f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar        receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
77f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar          ->getInterfaceDecl();
78488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar
792c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner        // This can be null for calls to e.g. id<Foo>.
8090df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar        if (!receiverClass) return false;
8190df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar      } else {
8290df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar        receiverClass = method->getClassInterface();
8390df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar        assert(receiverClass && "method not associated with a class!");
84488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar      }
8556b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar
862c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner      // If either class is a subclass of the other, it's fine.
870a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner      if (receiverClass->isSuperClassOf(resultClass) ||
880a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner          resultClass->isSuperClassOf(receiverClass))
892c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner        return false;
9058c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner    }
91488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar  }
92c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner
9390df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  SourceLocation loc = method->getLocation();
9490df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar
9590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  // If we're in a system header, and this is not a call, just make
9690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  // the method unusable.
97488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar  if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
9856b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar    method->addAttr(new (Context) UnavailableAttr(loc, Context,
99c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner                "init method returns a type unrelated to its receiver type"));
1000a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner    return true;
101c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner  }
102c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner
10304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  // Otherwise, it's an error.
10404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  Diag(loc, diag::err_arc_init_method_unrelated_result_type);
10504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  method->setInvalidDecl();
10604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  return true;
10704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar}
10804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar
10940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidisbool Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
11004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar                                   const ObjCMethodDecl *Overridden,
11104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar                                   bool IsImplementation) {
11204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  if (Overridden->hasRelatedResultType() &&
11304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar      !NewMethod->hasRelatedResultType()) {
11404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    // This can only happen when the method follows a naming convention that
11504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    // implies a related result type, and the original (overridden) method has
11604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    // a suitable return type, but the new (overriding) method does not have
11704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    // a suitable return type.
11804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    QualType ResultType = NewMethod->getResultType();
1197e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar    SourceRange ResultTypeRange;
1206ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar    if (const TypeSourceInfo *ResultTypeInfo
12104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar                                        = NewMethod->getResultTypeSourceInfo())
1224f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman      ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
1234f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman
12404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    // Figure out which class this method is part of, if any.
12504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    ObjCInterfaceDecl *CurrentClass
12604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar      = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
127df102fcb978588d5edbc661fb5da0b6922f9ab1cChris Lattner    if (!CurrentClass) {
1287e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar      DeclContext *DC = NewMethod->getDeclContext();
1296ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar      if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
1307e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar        CurrentClass = Cat->getClassInterface();
1316ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar      else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
13204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar        CurrentClass = Impl->getClassInterface();
1337cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian      else if (ObjCCategoryImplDecl *CatImpl
13404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar               = dyn_cast<ObjCCategoryImplDecl>(DC))
13504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar        CurrentClass = CatImpl->getClassInterface();
13604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    }
13704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar
13804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar    if (CurrentClass) {
13904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar      Diag(NewMethod->getLocation(),
1407cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian           diag::warn_related_result_type_compatibility_class)
1417cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian        << Context.getObjCInterfaceType(CurrentClass)
1427cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian        << ResultType
1432a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson        << ResultTypeRange;
1442a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson    } else {
1452a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson      Diag(NewMethod->getLocation(),
1462a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson           diag::warn_related_result_type_compatibility_protocol)
1472a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson        << ResultType
1482a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson        << ResultTypeRange;
1492a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson    }
1502a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson
1512a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson    Diag(Overridden->getLocation(), diag::note_related_result_type_overridden)
1522a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson      << Overridden->getMethodFamily();
1532a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson  }
1545f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor
1555f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor  return false;
1565f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor}
157c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner
158c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner/// \brief Check a method declaration for compatibility with the Objective-C
1595f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// ARC conventions.
1606ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregorstatic bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
161c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner  ObjCMethodFamily family = method->getMethodFamily();
162c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner  switch (family) {
163c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner  case OMF_None:
1643c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner  case OMF_dealloc:
165c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner  case OMF_retain:
166c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner  case OMF_release:
1676ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  case OMF_autorelease:
1686ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor  case OMF_retainCount:
169fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar  case OMF_self:
170fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar  case OMF_performSelector:
1713c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner    return false;
172fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar
1735f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor  case OMF_init:
1746ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor    // If the method doesn't obey the init rules, don't bother annotating it.
17595d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson    if (S.checkInitMethod(method, QualType()))
17695d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson      return true;
17795d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson
17895d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson    method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
17995d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson                                                       S.Context));
18095d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson
18195d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson    // Don't add a second copy of this attribute, but otherwise don't
18295d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson    // let it be suppressed.
1835f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor    if (method->hasAttr<NSReturnsRetainedAttr>())
1845f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor      return false;
1856d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner    break;
1866d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner
1876bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  case OMF_alloc:
18849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar  case OMF_copy:
1896bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  case OMF_mutableCopy:
1906d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  case OMF_new:
1916d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner    if (method->hasAttr<NSReturnsRetainedAttr>() ||
1926bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar        method->hasAttr<NSReturnsNotRetainedAttr>() ||
1936bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar        method->hasAttr<NSReturnsAutoreleasedAttr>())
1946bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar      return false;
19549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar    break;
1966bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  }
1976bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
1986bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
1996bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                                                        S.Context));
2006bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  return false;
2016bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar}
2020032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson
2036bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarstatic void DiagnoseObjCImplementedDeprecations(Sema &S,
2046bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                                                NamedDecl *ND,
20596e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson                                                SourceLocation ImplLoc,
2066bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar                                                int select) {
2076bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  if (ND && ND->isDeprecated()) {
208572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner    S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
2090032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    if (select == 0)
21096e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson      S.Diag(ND->getLocation(), diag::note_method_declared_at);
2116bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar    else
2126bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar      S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
2136bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  }
2146bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar}
2156bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
2160032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
2170032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson/// and user declared, in the method definition's AST.
2183c4972def972f8ca44dcd0561779a12aaa6fec97Owen Andersonvoid Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
21908e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson  assert(getCurMethodDecl() == 0 && "Method parsing confused");
2206bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
2216bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar
2226bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar  // If we don't have a valid method decl, simply return.
22396e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  if (!MDecl)
2241c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson    return;
225572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner
2267db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson  // Allow the rest of sema to find private method decl implementations.
2271c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson  if (MDecl->isInstanceMethod())
2286d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner    AddInstanceMethodToGlobalPool(MDecl, true);
2296d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner  else
2306d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner    AddFactoryMethodToGlobalPool(MDecl, true);
231532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman
232532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  // Allow all of Sema to see that we are entering a method definition.
233532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  PushDeclContext(FnBodyScope, MDecl);
234532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  PushFunctionScope();
235532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman
236532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  // Create Decl objects for each parameter, entrring them in the scope for
23796e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  // binding to their use.
238532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman
239532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  // Insert the invisible arguments, self and _cmd!
240532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  MDecl->createImplicitParams(Context, MDecl->getClassInterface());
2411c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson
242532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
2431c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson  PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
244532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman
245532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  // Introduce all of the other parameters into this scope.
246532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman  for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
24786daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner       E = MDecl->param_end(); PI != E; ++PI) {
24868584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor    ParmVarDecl *Param = (*PI);
24968584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor    if (!Param->isInvalidDecl() &&
2501fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor        RequireCompleteType(Param->getLocation(), Param->getType(),
2511fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor                            diag::err_typecheck_decl_incomplete_type))
2521fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor          Param->setInvalidDecl();
2531fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    if ((*PI)->getIdentifier())
2541fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor      PushOnScopeChains(*PI, FnBodyScope);
2551fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor  }
2561fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
2571fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor  // In ARC, disallow definition of retain/release/autorelease/retainCount
258167b824ce1947bf71ec8a71296daa2c54ebe58dfAnders Carlsson  if (getLangOptions().ObjCAutoRefCount) {
259167b824ce1947bf71ec8a71296daa2c54ebe58dfAnders Carlsson    switch (MDecl->getMethodFamily()) {
260f5cecfbdcd20be224861f9e67c5973a9a2b61512Argyrios Kyrtzidis    case OMF_retain:
261167b824ce1947bf71ec8a71296daa2c54ebe58dfAnders Carlsson    case OMF_retainCount:
262167b824ce1947bf71ec8a71296daa2c54ebe58dfAnders Carlsson    case OMF_release:
2631fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    case OMF_autorelease:
264167b824ce1947bf71ec8a71296daa2c54ebe58dfAnders Carlsson      Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
265167b824ce1947bf71ec8a71296daa2c54ebe58dfAnders Carlsson        << MDecl->getSelector();
26643907e87c776391d7d96da04b48e7bf06d8baafcEli Friedman      break;
2675e222139610a9f8b2b5f4ddd112f10dec9ec1e97Eli Friedman
2687c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    case OMF_None:
2697c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    case OMF_dealloc:
270005eedce54da6580de37bb115bf581008637e4b1Chris Lattner    case OMF_alloc:
2711fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    case OMF_init:
27286daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner    case OMF_mutableCopy:
273d55a71d852d4d8b785122b8d033a0c06b187067bChris Lattner    case OMF_copy:
274d55a71d852d4d8b785122b8d033a0c06b187067bChris Lattner    case OMF_new:
2759f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    case OMF_self:
276d55a71d852d4d8b785122b8d033a0c06b187067bChris Lattner    case OMF_performSelector:
2779f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor      break;
2789f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    }
2799f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  }
2809f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor
28168584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor  // Warn on implementating deprecated methods under
2829f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  // -Wdeprecated-implementations flag.
2839f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  if (ObjCInterfaceDecl *IC = MDecl->getClassInterface())
2849f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    if (ObjCMethodDecl *IMD =
28568584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor          IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
286d55a71d852d4d8b785122b8d033a0c06b187067bChris Lattner      DiagnoseObjCImplementedDeprecations(*this,
287d55a71d852d4d8b785122b8d033a0c06b187067bChris Lattner                                          dyn_cast<NamedDecl>(IMD),
2881fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor                                          MDecl->getLocation(), 0);
289d55a71d852d4d8b785122b8d033a0c06b187067bChris Lattner}
290cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner
29186daeee2d4aa6523679f07f27a826bf4c42ca95dChris LattnerDecl *Sema::
29286daeee2d4aa6523679f07f27a826bf4c42ca95dChris LattnerActOnStartClassInterface(SourceLocation AtInterfaceLoc,
293cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner                         IdentifierInfo *ClassName, SourceLocation ClassLoc,
29486daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner                         IdentifierInfo *SuperName, SourceLocation SuperLoc,
29586daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner                         Decl * const *ProtoRefs, unsigned NumProtoRefs,
296d55a71d852d4d8b785122b8d033a0c06b187067bChris Lattner                         const SourceLocation *ProtoLocs,
297b3efa98e320590e8be9d62818e89e599303e65b4Douglas Gregor                         SourceLocation EndProtoLoc, AttributeList *AttrList) {
298b3efa98e320590e8be9d62818e89e599303e65b4Douglas Gregor  assert(ClassName && "Missing class identifier");
299b3efa98e320590e8be9d62818e89e599303e65b4Douglas Gregor
300b3efa98e320590e8be9d62818e89e599303e65b4Douglas Gregor  // Check for another declaration kind with the same name.
3017c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
3027c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar                                         LookupOrdinaryName, ForRedeclaration);
3037c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
304b97b69244a4e7916c7b8fca28fa21b36ce5b30b2Daniel Dunbar  if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
305f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
306f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    Diag(PrevDecl->getLocation(), diag::note_previous_definition);
3077c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  }
3087c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
30968584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor  ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
3107c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  if (IDecl) {
31155d6f50b23a1fd0a04b568787a25beb7537e6c9bDaniel Dunbar    // Class already seen. Is it a forward declaration?
3129f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner    if (!IDecl->isForwardDecl()) {
31340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      IDecl->setInvalidDecl();
3147c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar      Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
31540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      Diag(IDecl->getLocation(), diag::note_previous_definition);
31644b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner
317cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner      // Return the previous class interface.
318cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner      // FIXME: don't leak the objects passed in!
319cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner      return IDecl;
320d9d049a93c55624908e81cf3927b7905efeba05fChris Lattner    } else {
3211fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor      IDecl->setLocation(AtInterfaceLoc);
32286daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner      IDecl->setForwardDecl(false);
32386daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner      IDecl->setClassLoc(ClassLoc);
32486daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner      // If the forward decl was in a PCH, we need to write it again in a
32586daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner      // dependent AST file.
32686daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner      IDecl->setChangedSinceDeserialization(true);
32786daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner
32886daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner      // Since this ObjCInterfaceDecl was created by a forward declaration,
3297c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar      // we now add it to the DeclContext since it wasn't added before
330cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner      // (see ActOnForwardClassDeclaration).
331cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner      IDecl->setLexicalDeclContext(CurContext);
3327c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar      CurContext->addDecl(IDecl);
3330dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
334d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes      if (AttrList)
3357c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar        ProcessDeclAttributeList(TUScope, IDecl, AttrList);
336d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes    }
337d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes  } else {
3387dbd8197040313d796282d4af06eccdf8a17319cDaniel Dunbar    IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
3397dbd8197040313d796282d4af06eccdf8a17319cDaniel Dunbar                                      ClassName, ClassLoc);
3407dbd8197040313d796282d4af06eccdf8a17319cDaniel Dunbar    if (AttrList)
341761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel      ProcessDeclAttributeList(TUScope, IDecl, AttrList);
34288b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar
343c134fcb0d7989fe6937e47e6216637647e074aefEli Friedman    PushOnScopeChains(IDecl, TUScope);
344761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  }
345761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel
346ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman  if (SuperName) {
347ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman    // Check if a different kind of symbol declared in this scope.
34840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
349f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov                                LookupOrdinaryName);
350f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov
35140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (!PrevDecl) {
352f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov      // Try to correct for a typo in the superclass name.
353f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar      TypoCorrection Corrected = CorrectTypo(
354f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar          DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
3557c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar          NULL, NULL, false, CTC_NoKeywords);
3567c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar      if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
35774ac74ae244c501027924c99f2a33559a1e23b53Daniel Dunbar        Diag(SuperLoc, diag::err_undef_superclass_suggest)
358f93349f3ec4d69eafba42436c33aaa91bfca7e70Daniel Dunbar          << SuperName << ClassName << PrevDecl->getDeclName();
359af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar        Diag(PrevDecl->getLocation(), diag::note_previous_decl)
36040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis          << PrevDecl->getDeclName();
361af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar      }
36281ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson    }
36340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis
36481ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson    if (PrevDecl == IDecl) {
365f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar      Diag(SuperLoc, diag::err_recursive_superclass)
366f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar        << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
3677c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar      IDecl->setLocEnd(ClassLoc);
3687c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    } else {
3697c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar      ObjCInterfaceDecl *SuperClassDecl =
3707c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar                                dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
37140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis
3727c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar      // Diagnose classes that inherit from deprecated classes.
3737c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar      if (SuperClassDecl)
37440b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis        (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
3757c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
3767c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar      if (PrevDecl && SuperClassDecl == 0) {
3777c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar        // The previous declaration was not a class decl. Check if we have a
3780e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar        // typedef. If we do, get the underlying class type.
3790e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar        if (const TypedefNameDecl *TDecl =
3800e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar              dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
3810e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar          QualType T = TDecl->getUnderlyingType();
3820e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar          if (T->isObjCObjectType()) {
3837c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar            if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
3847c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar              SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
3857c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar          }
3860e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar        }
387f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar
388f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar        // This handles the following case:
389f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar        //
390c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman        // typedef int SuperClass;
391c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman        // @interface MyClass : SuperClass {} @end
392c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman        //
393c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman        if (!SuperClassDecl) {
39445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar          Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
3957c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar          Diag(PrevDecl->getLocation(), diag::note_previous_definition);
3967c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar        }
3977c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar      }
39840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis
3997c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar      if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
40040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis        if (!SuperClassDecl)
40140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis          Diag(SuperLoc, diag::err_undef_superclass)
4027c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar            << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
4037c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar        else if (SuperClassDecl->isForwardDecl()) {
4047c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar          Diag(SuperLoc, diag::err_forward_superclass)
4057c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar            << SuperClassDecl->getDeclName() << ClassName
4067c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar            << SourceRange(AtInterfaceLoc, ClassLoc);
4077c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar          Diag(SuperClassDecl->getLocation(), diag::note_forward_class);
4087c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar          SuperClassDecl = 0;
40940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis        }
4107c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar      }
411219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      IDecl->setSuperClass(SuperClassDecl);
412219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar      IDecl->setSuperClassLoc(SuperLoc);
4130269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar      IDecl->setLocEnd(SuperLoc);
4140269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    }
4150269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  } else { // we have a root class.
41635f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner    IDecl->setLocEnd(ClassLoc);
4170269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  }
4180269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar
4190269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  // Check then save referenced protocols.
4200269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  if (NumProtoRefs) {
421ad64e024bd18cf25dcfa44e049004371838decd8Chris Lattner    IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
4220269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar                           ProtoLocs, Context);
4230269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    IDecl->setLocEnd(EndProtoLoc);
4240032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  }
4250032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson
42635f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner  CheckObjCDeclScope(IDecl);
42735f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner  return IDecl;
42835f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner}
42935f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner
43035f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner/// ActOnCompatiblityAlias - this action is called after complete parsing of
43135f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner/// @compatibility_alias declaration. It sets up the alias relationships.
4323c4972def972f8ca44dcd0561779a12aaa6fec97Owen AndersonDecl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
433a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson                                        IdentifierInfo *AliasName,
43435f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner                                        SourceLocation AliasLocation,
43535f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner                                        IdentifierInfo *ClassName,
436c38e9affd4519ea199af22419c8c794973cc4b23Fariborz Jahanian                                        SourceLocation ClassLocation) {
437c38e9affd4519ea199af22419c8c794973cc4b23Fariborz Jahanian  // Look for previous declaration of alias name
43896e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
439c38e9affd4519ea199af22419c8c794973cc4b23Fariborz Jahanian                                      LookupOrdinaryName, ForRedeclaration);
4400269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  if (ADecl) {
4411c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson    if (isa<ObjCCompatibleAliasDecl>(ADecl))
4420269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar      Diag(AliasLocation, diag::warn_previous_alias_decl);
4437db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson    else
4441c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson      Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
4450269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    Diag(ADecl->getLocation(), diag::note_previous_declaration);
4460269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar    return 0;
4470269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  }
4480269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  // Check for class declaration
4490269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar  NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
45067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner                                       LookupOrdinaryName, ForRedeclaration);
45167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  if (const TypedefNameDecl *TDecl =
45267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner        dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
45367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    QualType T = TDecl->getUnderlyingType();
4542a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson    if (T->isObjCObjectType()) {
45567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner      if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
45667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner        ClassName = IDecl->getIdentifier();
45767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner        CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
45867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner                                  LookupOrdinaryName, ForRedeclaration);
45967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner      }
46067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    }
46167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  }
46267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
463b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson  if (CDecl == 0) {
46467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
46567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    if (CDeclU)
46667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner      Diag(CDeclU->getLocation(), diag::note_previous_declaration);
46767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    return 0;
46867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  }
46967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner
4705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Everything checked out, instantiate a new alias declaration AST.
4715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ObjCCompatibleAliasDecl *AliasDecl =
4728bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
4738bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
4748bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  if (!CheckObjCDeclScope(AliasDecl))
4753c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar    PushOnScopeChains(AliasDecl, TUScope);
4768bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
4778bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  return AliasDecl;
4788bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman}
4798bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
4808bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begemanbool Sema::CheckForwardProtocolDeclarationForCircularDependency(
4818bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  IdentifierInfo *PName,
4828bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  SourceLocation &Ploc, SourceLocation PrevLoc,
4838bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  const ObjCList<ObjCProtocolDecl> &PList) {
4848bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman
4858bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  bool res = false;
4868bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
4878bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman       E = PList.end(); I != E; ++I) {
4888bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
4898bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                                 Ploc)) {
4900032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson      if (PDecl->getIdentifier() == PName) {
4910032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson        Diag(Ploc, diag::err_protocol_has_circular_dependency);
4920032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson        Diag(PrevLoc, diag::note_previous_definition);
4930032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson        res = true;
4940032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson      }
4950032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson      if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
4968bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman            PDecl->getLocation(), PDecl->getReferencedProtocols()))
4978bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman        res = true;
4988bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    }
4998bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  }
5008bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  return res;
50195b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner}
50295b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner
50395b851e55c328af4b69da7bfc1124bf258c0ffe5Chris LattnerDecl *
5048bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate BegemanSema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
5058bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                  IdentifierInfo *ProtocolName,
50695b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner                                  SourceLocation ProtocolLoc,
50795b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner                                  Decl * const *ProtoRefs,
50895b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner                                  unsigned NumProtoRefs,
5098bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                  const SourceLocation *ProtoLocs,
51057d5cee133495bc21d1abdbce45ab05a79274a23Daniel Dunbar                                  SourceLocation EndProtoLoc,
5118bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman                                  AttributeList *AttrList) {
5123c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  bool err = false;
5133c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  // FIXME: Deal with AttrList.
5143c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  assert(ProtocolName && "Missing protocol identifier");
5150032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
5168bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman  if (PDecl) {
51747a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson    // Protocol already seen. Better be a forward protocol declaration
5188bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman    if (!PDecl->isForwardDecl()) {
5198bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman      Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
52073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar      Diag(PDecl->getLocation(), diag::note_previous_definition);
5215c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar      // Just return the protocol we already had.
5225c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar      // FIXME: don't leak the objects passed in!
52340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      return PDecl;
52473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    }
525bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    ObjCList<ObjCProtocolDecl> PList;
526bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
52773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    err = CheckForwardProtocolDeclarationForCircularDependency(
52840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis            ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
52940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis
53073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    // Make sure the cached decl gets a valid start location.
53173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    PDecl->setLocation(AtProtoInterfaceLoc);
53268584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor    PDecl->setForwardDecl(false);
533dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner    CurContext->addDecl(PDecl);
534dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner    // Repeat in dependent AST files.
53586daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner    PDecl->setChangedSinceDeserialization(true);
536cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner  } else {
537cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner    PDecl = ObjCProtocolDecl::Create(Context, CurContext,
538dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner                                     AtProtoInterfaceLoc,ProtocolName);
539dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner    PushOnScopeChains(PDecl, TUScope);
54073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    PDecl->setForwardDecl(false);
541dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner  }
542dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner  if (AttrList)
543dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner    ProcessDeclAttributeList(TUScope, PDecl, AttrList);
544b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor  if (!err && NumProtoRefs ) {
545dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner    /// Check then save referenced protocols.
54673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
54773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar                           ProtoLocs, Context);
548b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner    PDecl->setLocEnd(EndProtoLoc);
5492a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson  }
5502a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson
551bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  CheckObjCDeclScope(PDecl);
552bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  return PDecl;
55340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis}
554bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
555219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar/// FindProtocolDeclaration - This routine looks up protocols and
55667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner/// issues an error if they are not declared. It returns list of
5575e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar/// protocol declarations in its 'Protocols' argument.
55873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarvoid
55973241dfeb5c498255b662984cca369fd28ec3147Daniel DunbarSema::FindProtocolDeclaration(bool WarnOnDeclarations,
56073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar                              const IdentifierLocPair *ProtocolId,
5610269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar                              unsigned NumProtocols,
5620269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar                              SmallVectorImpl<Decl *> &Protocols) {
563bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  for (unsigned i = 0; i != NumProtocols; ++i) {
564bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
565b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor                                             ProtocolId[i].second);
5662928c2107f2e0007f35fe1c224aab63535f1403dAnders Carlsson    if (!PDecl) {
5672928c2107f2e0007f35fe1c224aab63535f1403dAnders Carlsson      TypoCorrection Corrected = CorrectTypo(
5682928c2107f2e0007f35fe1c224aab63535f1403dAnders Carlsson          DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
569b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor          LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
570b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor      if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
571b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor        Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
572b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor          << ProtocolId[i].first << Corrected.getCorrection();
57373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar        Diag(PDecl->getLocation(), diag::note_previous_decl)
5744c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman          << PDecl->getDeclName();
5754c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman      }
57667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    }
57767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner
57873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar    if (!PDecl) {
57967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner      Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
58067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner        << ProtocolId[i].first;
5812a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson      continue;
58267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    }
5832a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson
58467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
58567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner
58667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // If this is a forward declaration and we are supposed to warn in this
58767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // case, do it.
5882a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson    if (WarnOnDeclarations && PDecl->isForwardDecl())
58967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner      Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
590bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar        << ProtocolId[i].first;
591bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    Protocols.push_back(PDecl);
592bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  }
593bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar}
5942a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson
5954c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
5964c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman/// a class method in its extension.
597b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner///
5982a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlssonvoid Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
5992a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson                                            ObjCInterfaceDecl *ID) {
6002a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson  if (!ID)
6012a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson    return;  // Possibly due to previous error
6027267c1693abe7875b0c57268be05005ae013c6c9Anders Carlsson
6037267c1693abe7875b0c57268be05005ae013c6c9Anders Carlsson  llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
604b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner  for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
605b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner       e =  ID->meth_end(); i != e; ++i) {
6062a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson    ObjCMethodDecl *MD = *i;
607bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    MethodMap[MD->getSelector()] = MD;
6082a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson  }
609bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
610bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  if (MethodMap.empty())
611bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    return;
612bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
61374391b48b4791cded373683a3baf67314f358d50Chris Lattner       e =  CAT->meth_end(); i != e; ++i) {
61474391b48b4791cded373683a3baf67314f358d50Chris Lattner    ObjCMethodDecl *Method = *i;
61574391b48b4791cded373683a3baf67314f358d50Chris Lattner    const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
61674391b48b4791cded373683a3baf67314f358d50Chris Lattner    if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
61774391b48b4791cded373683a3baf67314f358d50Chris Lattner      Diag(Method->getLocation(), diag::err_duplicate_method_decl)
61874391b48b4791cded373683a3baf67314f358d50Chris Lattner            << Method->getDeclName();
61974391b48b4791cded373683a3baf67314f358d50Chris Lattner      Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
62074391b48b4791cded373683a3baf67314f358d50Chris Lattner    }
62174391b48b4791cded373683a3baf67314f358d50Chris Lattner  }
622b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner}
6230558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner
6240558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
6250558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris LattnerDecl *
6260558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris LattnerSema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
6270558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner                                      const IdentifierLocPair *IdentList,
6280558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner                                      unsigned NumElts,
6290558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner                                      AttributeList *attrList) {
63096e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  SmallVector<ObjCProtocolDecl*, 32> Protocols;
6313c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  SmallVector<SourceLocation, 8> ProtoLocs;
6320558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner
6330558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  for (unsigned i = 0; i != NumElts; ++i) {
63467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    IdentifierInfo *Ident = IdentList[i].first;
63567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
63667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    bool isNew = false;
6372a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson    if (PDecl == 0) { // Not already seen?
6389fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner      PDecl = ObjCProtocolDecl::Create(Context, CurContext,
63967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner                                       IdentList[i].second, Ident);
64067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner      PushOnScopeChains(PDecl, TUScope, false);
64167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner      isNew = true;
64267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    }
64367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    if (attrList) {
644b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner      ProcessDeclAttributeList(TUScope, PDecl, attrList);
6450c337ed63ff0f04fd8315afabb2d7a51969fdc97Chris Lattner      if (!isNew)
6460c337ed63ff0f04fd8315afabb2d7a51969fdc97Chris Lattner        PDecl->setChangedSinceDeserialization(true);
6470c337ed63ff0f04fd8315afabb2d7a51969fdc97Chris Lattner    }
648b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner    Protocols.push_back(PDecl);
649b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner    ProtoLocs.push_back(IdentList[i].second);
650c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian  }
651c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian
652c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian  ObjCForwardProtocolDecl *PDecl =
653c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian    ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
654c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian                                    Protocols.data(), Protocols.size(),
65597a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian                                    ProtoLocs.data());
65697a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian  CurContext->addDecl(PDecl);
6579889652dbc10060cd604861ed2e5bc6719f845b0Fariborz Jahanian  CheckObjCDeclScope(PDecl);
658c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian  return PDecl;
659c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian}
660b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian
661b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz JahanianDecl *Sema::
662ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz JahanianActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
663ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian                            IdentifierInfo *ClassName, SourceLocation ClassLoc,
664ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian                            IdentifierInfo *CategoryName,
66567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner                            SourceLocation CategoryLoc,
66667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner                            Decl * const *ProtoRefs,
6670558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner                            unsigned NumProtoRefs,
6680558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner                            const SourceLocation *ProtoLocs,
6690558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner                            SourceLocation EndProtoLoc) {
670c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman  ObjCCategoryDecl *CDecl;
6710558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
6720032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson
6730558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  /// Check that class of this category is already completely declared.
674c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman  if (!IDecl || IDecl->isForwardDecl()) {
6750558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    // Create an invalid ObjCCategoryDecl to serve as context for
6760558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    // the enclosing method declarations.  We mark the decl invalid
6770558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    // to make it clear that this isn't a valid AST.
678d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner    CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
679d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner                                     ClassLoc, CategoryLoc, CategoryName);
680c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman    CDecl->setInvalidDecl();
681c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman    Diag(ClassLoc, diag::err_undef_interface) << ClassName;
682c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman    return CDecl;
6830558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  }
6840558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner
6850558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  if (!CategoryName && IDecl->getImplementation()) {
6860558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
68797a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian    Diag(IDecl->getImplementation()->getLocation(),
68897a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian          diag::note_implementation_declared);
68997a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian  }
69097a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian
69197a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian  CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
69297a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian                                   ClassLoc, CategoryLoc, CategoryName);
69397a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian  // FIXME: PushOnScopeChains?
69497a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian  CurContext->addDecl(CDecl);
69597a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian
69697a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian  CDecl->setClassInterface(IDecl);
69797a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian  // Insert class extension to the list of class's categories.
69897a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian  if (!CategoryName)
69997a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian    CDecl->insertNextClassCategory();
70097a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian
70197a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian  // If the interface is deprecated, warn about it.
70297a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian  (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
70397a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian
70480e4b9e0e87064a824d72b6ff89074206ecced58Fariborz Jahanian  if (CategoryName) {
70597a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian    /// Check for duplicate interface declaration for this category
70697a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian    ObjCCategoryDecl *CDeclChain;
70797a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian    for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
70897a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian         CDeclChain = CDeclChain->getNextClassCategory()) {
70997a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian      if (CDeclChain->getIdentifier() == CategoryName) {
71097a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian        // Class extensions can be declared multiple times.
71197a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian        Diag(CategoryLoc, diag::warn_dup_category_def)
71297a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian          << ClassName << CategoryName;
71397a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian        Diag(CDeclChain->getLocation(), diag::note_previous_definition);
71497a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian        break;
715e64941280877d065a27e8cefd2a9038256d0e3acFariborz Jahanian      }
716e64941280877d065a27e8cefd2a9038256d0e3acFariborz Jahanian    }
71797a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian    if (!CDeclChain)
718e64941280877d065a27e8cefd2a9038256d0e3acFariborz Jahanian      CDecl->insertNextClassCategory();
71997a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian  }
72080e4b9e0e87064a824d72b6ff89074206ecced58Fariborz Jahanian
72197a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian  if (NumProtoRefs) {
72297a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian    CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
72397a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian                           ProtoLocs, Context);
72497a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian    // Protocols in the class extension belong to the class.
725ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian    if (CDecl->IsClassExtension())
726ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian     IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
727ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian                                            NumProtoRefs, Context);
728ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian  }
729ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian
730ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian  CheckObjCDeclScope(CDecl);
73197a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian  return CDecl;
732ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian}
733ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian
734ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian/// ActOnStartCategoryImplementation - Perform semantic checks on the
735ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian/// category implementation declaration and build an ObjCCategoryImplDecl
736ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian/// object.
737ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz JahanianDecl *Sema::ActOnStartCategoryImplementation(
738ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian                      SourceLocation AtCatImplLoc,
739ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian                      IdentifierInfo *ClassName, SourceLocation ClassLoc,
740ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian                      IdentifierInfo *CatName, SourceLocation CatLoc) {
741ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian  ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
742ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian  ObjCCategoryDecl *CatIDecl = 0;
743ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian  if (IDecl) {
744183d7181fd59842ac969cbc6fe0376f85dc63ae4Fariborz Jahanian    CatIDecl = IDecl->FindCategoryDeclaration(CatName);
745183d7181fd59842ac969cbc6fe0376f85dc63ae4Fariborz Jahanian    if (!CatIDecl) {
746183d7181fd59842ac969cbc6fe0376f85dc63ae4Fariborz Jahanian      // Category @implementation with no corresponding @interface.
747ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian      // Create and install one.
748ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian      CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
749ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian                                          SourceLocation(), SourceLocation(),
750ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian                                          CatName);
751ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian      CatIDecl->setClassInterface(IDecl);
752ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian      CatIDecl->insertNextClassCategory();
753ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian    }
754ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian  }
755ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian
756ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian  ObjCCategoryImplDecl *CDecl =
757ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian    ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
758ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian                                 IDecl);
759ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian  /// Check that class of this category is already completely declared.
760ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian  if (!IDecl || IDecl->isForwardDecl()) {
761ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian    Diag(ClassLoc, diag::err_undef_interface) << ClassName;
762183d7181fd59842ac969cbc6fe0376f85dc63ae4Fariborz Jahanian    CDecl->setInvalidDecl();
763183d7181fd59842ac969cbc6fe0376f85dc63ae4Fariborz Jahanian  }
764183d7181fd59842ac969cbc6fe0376f85dc63ae4Fariborz Jahanian
765ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian  // FIXME: PushOnScopeChains?
766ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian  CurContext->addDecl(CDecl);
767ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian
768ad25883a644dd6b52c7923dd128a7d05fb26213cFariborz Jahanian  /// Check that CatName, category name, is not used in another implementation.
76997a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian  if (CatIDecl) {
77097a937532c24a8ea44317d4fdee26d9701a1e83cFariborz Jahanian    if (CatIDecl->getImplementation()) {
771b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian      Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
772b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian        << CatName;
773b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian      Diag(CatIDecl->getImplementation()->getLocation(),
774b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian           diag::note_previous_definition);
775b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian    } else {
776b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian      CatIDecl->setImplementation(CDecl);
777b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian      // Warn on implementating category of deprecated class under
778b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian      // -Wdeprecated-implementations flag.
779b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian      DiagnoseObjCImplementedDeprecations(*this,
780b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian                                          dyn_cast<NamedDecl>(IDecl),
781b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian                                          CDecl->getLocation(), 2);
782b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian    }
783b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian  }
784b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian
785b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian  CheckObjCDeclScope(CDecl);
786b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian  return CDecl;
787b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian}
788b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian
789b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz JahanianDecl *Sema::ActOnStartClassImplementation(
790b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian                      SourceLocation AtClassImplLoc,
791b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian                      IdentifierInfo *ClassName, SourceLocation ClassLoc,
792b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian                      IdentifierInfo *SuperClassname,
793b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian                      SourceLocation SuperClassLoc) {
794b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian  ObjCInterfaceDecl* IDecl = 0;
795b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian  // Check for another declaration kind with the same name.
796b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian  NamedDecl *PrevDecl
797b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian    = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
798b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian                       ForRedeclaration);
799b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian  if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
800b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian    Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
801b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian    Diag(PrevDecl->getLocation(), diag::note_previous_definition);
802b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian  } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
803b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian    // If this is a forward declaration of an interface, warn.
804b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian    if (IDecl->isForwardDecl()) {
805b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian      Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
806b193a414f13d7cfa2524a8149eff8d4871f8cbf6Fariborz Jahanian      IDecl = 0;
80774391b48b4791cded373683a3baf67314f358d50Chris Lattner    }
80874391b48b4791cded373683a3baf67314f358d50Chris Lattner  } else {
80974391b48b4791cded373683a3baf67314f358d50Chris Lattner    // We did not find anything with the name ClassName; try to correct for
810b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner    // typos in the class name.
81174391b48b4791cded373683a3baf67314f358d50Chris Lattner    TypoCorrection Corrected = CorrectTypo(
81274391b48b4791cded373683a3baf67314f358d50Chris Lattner        DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
81374391b48b4791cded373683a3baf67314f358d50Chris Lattner        NULL, NULL, false, CTC_NoKeywords);
814b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner    if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
815b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner      // Suggest the (potentially) correct interface name. However, put the
81674391b48b4791cded373683a3baf67314f358d50Chris Lattner      // fix-it hint itself in a separate note, since changing the name in
81777ba708819285931932ecd33691a672bb59d221aEli Friedman      // the warning would make the fix-it change semantics.However, don't
81874391b48b4791cded373683a3baf67314f358d50Chris Lattner      // provide a code-modification hint or use the typo name for recovery,
81974391b48b4791cded373683a3baf67314f358d50Chris Lattner      // because this is just a warning. The program may actually be correct.
82074391b48b4791cded373683a3baf67314f358d50Chris Lattner      DeclarationName CorrectedName = Corrected.getCorrection();
82174391b48b4791cded373683a3baf67314f358d50Chris Lattner      Diag(ClassLoc, diag::warn_undef_interface_suggest)
82274391b48b4791cded373683a3baf67314f358d50Chris Lattner        << ClassName << CorrectedName;
82374391b48b4791cded373683a3baf67314f358d50Chris Lattner      Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
82474391b48b4791cded373683a3baf67314f358d50Chris Lattner        << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
825b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner      IDecl = 0;
82674391b48b4791cded373683a3baf67314f358d50Chris Lattner    } else {
827bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar      Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
82874391b48b4791cded373683a3baf67314f358d50Chris Lattner    }
82974391b48b4791cded373683a3baf67314f358d50Chris Lattner  }
83074391b48b4791cded373683a3baf67314f358d50Chris Lattner
83174391b48b4791cded373683a3baf67314f358d50Chris Lattner  // Check that super class name is valid class name
83274391b48b4791cded373683a3baf67314f358d50Chris Lattner  ObjCInterfaceDecl* SDecl = 0;
83374391b48b4791cded373683a3baf67314f358d50Chris Lattner  if (SuperClassname) {
83474391b48b4791cded373683a3baf67314f358d50Chris Lattner    // Check if a different kind of symbol declared in this scope.
83574391b48b4791cded373683a3baf67314f358d50Chris Lattner    PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
83674391b48b4791cded373683a3baf67314f358d50Chris Lattner                                LookupOrdinaryName);
83774391b48b4791cded373683a3baf67314f358d50Chris Lattner    if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
8383c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar      Diag(SuperClassLoc, diag::err_redefinition_different_kind)
8395d4f5c724533b994de05df49ae259120482ec366Chris Lattner        << SuperClassname;
84099b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner      Diag(PrevDecl->getLocation(), diag::note_previous_definition);
84174391b48b4791cded373683a3baf67314f358d50Chris Lattner    } else {
842570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner      SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
843570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner      if (!SDecl)
84499b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner        Diag(SuperClassLoc, diag::err_undef_superclass)
8453c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson          << SuperClassname << ClassName;
84699b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner      else if (IDecl && IDecl->getSuperClass() != SDecl) {
84767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner        // This implementation and its interface do not have the same
84867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner        // super class.
84967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner        Diag(SuperClassLoc, diag::err_conflicting_super_class)
85067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner          << SDecl->getDeclName();
8512a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson        Diag(SDecl->getLocation(), diag::note_previous_definition);
85267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner      }
85367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    }
85467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  }
85567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner
85667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner  if (!IDecl) {
85767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // Legacy case of @implementation with no corresponding @interface.
85867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner    // Build, chain & install the interface decl into the identifier.
85967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner
86099b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner    // FIXME: Do we support attributes on the @implementation? If so we should
8611c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson    // copy them over.
86299b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner    IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
8631c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson                                      ClassName, ClassLoc, false, true);
86456ebe5082da7411fb37479e230b52735f77cff35Eli Friedman    IDecl->setSuperClass(SDecl);
865d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner    IDecl->setLocEnd(ClassLoc);
86649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar
86799b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner    PushOnScopeChains(IDecl, TUScope);
86874391b48b4791cded373683a3baf67314f358d50Chris Lattner  } else {
869f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // Mark the interface as being completed, even if it was just as
870f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    //   @class ....;
87174391b48b4791cded373683a3baf67314f358d50Chris Lattner    // declaration; the user cannot reopen it.
87249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar    IDecl->setForwardDecl(false);
87374391b48b4791cded373683a3baf67314f358d50Chris Lattner  }
87474391b48b4791cded373683a3baf67314f358d50Chris Lattner
87504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  ObjCImplementationDecl* IMPDecl =
87649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar    ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
87740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis                                   IDecl, SDecl);
87840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis
87974391b48b4791cded373683a3baf67314f358d50Chris Lattner  if (CheckObjCDeclScope(IMPDecl))
88056ebe5082da7411fb37479e230b52735f77cff35Eli Friedman    return IMPDecl;
88156ebe5082da7411fb37479e230b52735f77cff35Eli Friedman
88274391b48b4791cded373683a3baf67314f358d50Chris Lattner  // Check that there is no duplicate implementation of this class.
88374391b48b4791cded373683a3baf67314f358d50Chris Lattner  if (IDecl->getImplementation()) {
88474391b48b4791cded373683a3baf67314f358d50Chris Lattner    // FIXME: Don't leak everything!
88574391b48b4791cded373683a3baf67314f358d50Chris Lattner    Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
886eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar    Diag(IDecl->getImplementation()->getLocation(),
887eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar         diag::note_previous_definition);
88874391b48b4791cded373683a3baf67314f358d50Chris Lattner  } else { // add it to the list.
88974391b48b4791cded373683a3baf67314f358d50Chris Lattner    IDecl->setImplementation(IMPDecl);
89074391b48b4791cded373683a3baf67314f358d50Chris Lattner    PushOnScopeChains(IMPDecl, TUScope);
89174391b48b4791cded373683a3baf67314f358d50Chris Lattner    // Warn on implementating deprecated class under
89274391b48b4791cded373683a3baf67314f358d50Chris Lattner    // -Wdeprecated-implementations flag.
89374391b48b4791cded373683a3baf67314f358d50Chris Lattner    DiagnoseObjCImplementedDeprecations(*this,
89474391b48b4791cded373683a3baf67314f358d50Chris Lattner                                        dyn_cast<NamedDecl>(IDecl),
89574391b48b4791cded373683a3baf67314f358d50Chris Lattner                                        IMPDecl->getLocation(), 1);
89674391b48b4791cded373683a3baf67314f358d50Chris Lattner  }
89774391b48b4791cded373683a3baf67314f358d50Chris Lattner  return IMPDecl;
89874391b48b4791cded373683a3baf67314f358d50Chris Lattner}
89974391b48b4791cded373683a3baf67314f358d50Chris Lattner
90096e0fc726c6fe7538522c60743705d5e696b40afOwen Andersonvoid Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
90174391b48b4791cded373683a3baf67314f358d50Chris Lattner                                    ObjCIvarDecl **ivars, unsigned numIvars,
90274391b48b4791cded373683a3baf67314f358d50Chris Lattner                                    SourceLocation RBrace) {
9033f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar  assert(ImpDecl && "missing implementation decl");
90474391b48b4791cded373683a3baf67314f358d50Chris Lattner  ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
90574391b48b4791cded373683a3baf67314f358d50Chris Lattner  if (!IDecl)
90674391b48b4791cded373683a3baf67314f358d50Chris Lattner    return;
90774391b48b4791cded373683a3baf67314f358d50Chris Lattner  /// Check case of non-existing @interface decl.
90874391b48b4791cded373683a3baf67314f358d50Chris Lattner  /// (legacy objective-c @implementation decl without an @interface decl).
90974391b48b4791cded373683a3baf67314f358d50Chris Lattner  /// Add implementations's ivar to the synthesize class's ivar list.
91074391b48b4791cded373683a3baf67314f358d50Chris Lattner  if (IDecl->isImplicitInterfaceDecl()) {
91196e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    IDecl->setLocEnd(RBrace);
912bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    // Add ivar's to class's DeclContext.
913bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar    for (unsigned i = 0, e = numIvars; i != e; ++i) {
91403f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar      ivars[i]->setLexicalDeclContext(ImpDecl);
91503f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar      IDecl->makeDeclVisibleInContext(ivars[i], false);
91603f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar      ImpDecl->addDecl(ivars[i]);
9177520bd1de12af10ea08c662440565adbdf589317Douglas Gregor    }
9187520bd1de12af10ea08c662440565adbdf589317Douglas Gregor
9197520bd1de12af10ea08c662440565adbdf589317Douglas Gregor    return;
9207520bd1de12af10ea08c662440565adbdf589317Douglas Gregor  }
9217520bd1de12af10ea08c662440565adbdf589317Douglas Gregor  // If implementation has empty ivar list, just return.
9227520bd1de12af10ea08c662440565adbdf589317Douglas Gregor  if (numIvars == 0)
9232a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson    return;
92403f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar
9257520bd1de12af10ea08c662440565adbdf589317Douglas Gregor  assert(ivars && "missing @implementation ivars");
9267520bd1de12af10ea08c662440565adbdf589317Douglas Gregor  if (LangOpts.ObjCNonFragileABI2) {
9277520bd1de12af10ea08c662440565adbdf589317Douglas Gregor    if (ImpDecl->getSuperClass())
9287520bd1de12af10ea08c662440565adbdf589317Douglas Gregor      Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
92903f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    for (unsigned i = 0; i < numIvars; i++) {
93003f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar      ObjCIvarDecl* ImplIvar = ivars[i];
93103f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar      if (const ObjCIvarDecl *ClsIvar =
932bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar            IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
9338f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner        Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
93477ba708819285931932ecd33691a672bb59d221aEli Friedman        Diag(ClsIvar->getLocation(), diag::note_previous_definition);
935b75863d53b8a2bbf0ece8e6df2b6e5be7f3896c4Chris Lattner        continue;
9368f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner      }
937cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman      // Instance ivar to Implementation's DeclContext.
93803f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar      ImplIvar->setLexicalDeclContext(ImpDecl);
93903f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar      IDecl->makeDeclVisibleInContext(ImplIvar, false);
94003f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar      ImpDecl->addDecl(ImplIvar);
94103f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    }
94203f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar    return;
94303f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar  }
94403f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar  // Check interface's Ivar list against those in the implementation.
94503f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar  // names and types must match.
94603f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar  //
947b0d0ea042116c1f451d3db8ceff9f1dd92bc36d2Anders Carlsson  unsigned j = 0;
94877ba708819285931932ecd33691a672bb59d221aEli Friedman  ObjCInterfaceDecl::ivar_iterator
949e9352cc9818ba59e7cf88500ef048991c90f3821Anders Carlsson    IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
95089ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson  for (; numIvars > 0 && IVI != IVE; ++IVI) {
9516e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman    ObjCIvarDecl* ImplIvar = ivars[j++];
9526e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman    ObjCIvarDecl* ClsIvar = *IVI;
95389ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson    assert (ImplIvar && "missing implementation ivar");
95489ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson    assert (ClsIvar && "missing class ivar");
95589ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson
95689ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson    // First, make sure the types match.
95789ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson    if (Context.getCanonicalType(ImplIvar->getType()) !=
95889ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson        Context.getCanonicalType(ClsIvar->getType())) {
95989ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson      Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
9606e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman        << ImplIvar->getIdentifier()
9618f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner        << ImplIvar->getType() << ClsIvar->getType();
9628e53e720b3d7c962e91138a130dbd5d6c2def0e5Devang Patel      Diag(ClsIvar->getLocation(), diag::note_previous_definition);
9632d58406872e5af0c924623d9f7c194c4f09936d3Chris Lattner    } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
964570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner      Expr *ImplBitWidth = ImplIvar->getBitWidth();
965570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner      Expr *ClsBitWidth = ClsIvar->getBitWidth();
966570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner      if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
967570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner          ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
9689d4a15fd3b85434c43ea27562793de63a793321aChris Lattner        Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
9699d4a15fd3b85434c43ea27562793de63a793321aChris Lattner          << ImplIvar->getIdentifier();
9709d4a15fd3b85434c43ea27562793de63a793321aChris Lattner        Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
971570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner      }
972570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    }
9733c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar    // Make sure the names are identical.
974570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
975570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner      Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
976570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner        << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
977570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner      Diag(ClsIvar->getLocation(), diag::note_previous_definition);
978570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    }
979570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    --numIvars;
980570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  }
981570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner
982570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  if (numIvars > 0)
983570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
984570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  else if (IVI != IVE)
985570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
986570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner}
987570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner
988570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattnervoid Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
989570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner                               bool &IncompleteImpl, unsigned DiagID) {
990570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  // No point warning no definition of method which is 'unavailable'.
991570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  if (method->hasAttr<UnavailableAttr>())
992232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar    return;
993570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner  if (!IncompleteImpl) {
994570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    Diag(ImpLoc, diag::warn_incomplete_impl);
9950558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    IncompleteImpl = true;
9960558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  }
99777ba708819285931932ecd33691a672bb59d221aEli Friedman  if (DiagID == diag::warn_unimplemented_protocol_method)
99877ba708819285931932ecd33691a672bb59d221aEli Friedman    Diag(ImpLoc, DiagID) << method->getDeclName();
9993c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson  else
1000570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner    Diag(method->getLocation(), DiagID) << method->getDeclName();
100177ba708819285931932ecd33691a672bb59d221aEli Friedman}
100277ba708819285931932ecd33691a672bb59d221aEli Friedman
1003570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner/// Determines if type B can be substituted for type A.  Returns true if we can
100477ba708819285931932ecd33691a672bb59d221aEli Friedman/// guarantee that anything that the user will do to an object of type A can
100577ba708819285931932ecd33691a672bb59d221aEli Friedman/// also be done to an object of type B.  This is trivially true if the two
100640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis/// types are the same, or if B is a subclass of A.  It becomes more complex
10078bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// in cases where protocols are involved.
10088bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman///
1009f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner/// Object types in Objective-C describe the minimum requirements for an
10108bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// object, rather than providing a complete description of a type.  For
10118bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// example, if A is a subclass of B, then B* may refer to an instance of A.
101288a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner/// The principle of substitutability means that we may use an instance of A
1013e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner/// anywhere that we may use an instance of B - it will implement all of the
1014e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner/// ivars of B and all of the methods of B.
1015e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner///
1016e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner/// This substitutability is important when type checking methods, because
1017e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner/// the implementation may have stricter type definitions than the interface.
1018e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner/// The interface specifies minimum requirements, but the implementation may
1019e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner/// have more accurate ones.  For example, a method may privately accept
1020e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner/// instances of B, but only publish that it accepts instances of A.  Any
1021e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner/// object passed to it will be type checked against B, and so will implicitly
10220de40af3a3aa14e3854c0eafeabd08f6762801f9Eli Friedman/// by a valid A*.  Similarly, a method may return a subclass of the class that
102308d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman/// it is declared as returning.
102488a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner///
10258fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner/// This is most important when considering subclassing.  A method in a
10268fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner/// subclass must accept any object as an argument that its superclass's
102740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis/// implementation accepts.  It may, however, accept a more general type
1028ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner/// without breaking substitutability (i.e. you can still use the subclass
102940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis/// anywhere that you can use the superclass, but not vice versa).  The
1030ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner/// converse requirement applies to return types: the return type for a
1031e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner/// subclass method must be a valid object of the kind that the superclass
1032e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner/// advertises, but it may be specified more accurately.  This avoids the need
1033e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner/// for explicit down-casting by callers.
1034e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner///
1035e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner/// Note: This is a stricter requirement than for assignment.
1036e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattnerstatic bool isObjCTypeSubstitutable(ASTContext &Context,
1037309457d0f1b416c1b379c9f3e172848adffedb23Chris Lattner                                    const ObjCObjectPointerType *A,
1038e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner                                    const ObjCObjectPointerType *B,
103904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar                                    bool rejectId) {
1040e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner  // Reject a protocol-unqualified id.
1041e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner  if (rejectId && B->isObjCIdType()) return false;
1042e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner
104304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  // If B is a qualified id, then A must also be a qualified id and it must
10447e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar  // implement all of the protocols in B.  It may not be a qualified class.
10457c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
104604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar  // stricter definition so it is not substitutable for id<A>.
1047686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta  if (B->isObjCQualifiedIdType()) {
10482d58406872e5af0c924623d9f7c194c4f09936d3Chris Lattner    return A->isObjCQualifiedIdType() &&
104966031a5594bc9a7dc0dc5137c3e7955f835e4639Daniel Dunbar           Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1050686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta                                                     QualType(B,0),
1051686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta                                                     false);
105288a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner  }
10535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1054bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  /*
1055bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  // id is a special type that bypasses type checking completely.  We want a
1056bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  // warning when it is used in one place but not another.
1057bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1058bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner
1059bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner
1060bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  // If B is a qualified id, then A must also be a qualified id (which it isn't
1061bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  // if we've got this far)
1062bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  if (B->isObjCQualifiedIdType()) return false;
1063bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  */
1064bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner
1065bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  // Now we know that A and B are (potentially-qualified) class types.  The
1066bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  // normal rules for assignment apply.
1067bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  return Context.canAssignObjCInterfaces(A, B);
1068bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner}
1069bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner
1070bdb0132722082886558f31eccdba06ae1852c0eeChris Lattnerstatic SourceRange getTypeRange(TypeSourceInfo *TSI) {
1071bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1072bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner}
1073bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner
1074bdb0132722082886558f31eccdba06ae1852c0eeChris Lattnerstatic bool CheckMethodOverrideReturn(Sema &S,
107508c93a7f2210b464e5abe298a5474b99414615f6Chris Lattner                                      ObjCMethodDecl *MethodImpl,
1076bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner                                      ObjCMethodDecl *MethodDecl,
107708c93a7f2210b464e5abe298a5474b99414615f6Chris Lattner                                      bool IsProtocolMethodDecl,
1078bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner                                      bool IsDeclaration,
1079bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner                                      bool Warn) {
1080bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  if (IsProtocolMethodDecl &&
1081bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      (MethodDecl->getObjCDeclQualifier() !=
1082bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner       MethodImpl->getObjCDeclQualifier())) {
1083bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    if (Warn) {
1084bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      S.Diag(MethodImpl->getLocation(),
1085bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner             diag::warn_conflicting_ret_type_modifiers)
1086bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner          << MethodImpl->getDeclName() << IsDeclaration
1087bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner          << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1088bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner        S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1089bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner          << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1090bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    }
1091bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    else
1092bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      return false;
1093bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  }
1094bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner
1095bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
1096bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner                                       MethodDecl->getResultType()))
1097bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    return true;
1098bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  if (!Warn)
1099bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    return false;
1100bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner
1101bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  unsigned DiagID = diag::warn_conflicting_ret_types;
1102bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner
1103bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  // Mismatches between ObjC pointers go into a different warning
1104bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  // category, and sometimes they're even completely whitelisted.
1105bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  if (const ObjCObjectPointerType *ImplPtrTy =
11060032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson        MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1107bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    if (const ObjCObjectPointerType *IfacePtrTy =
1108bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner          MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
1109bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      // Allow non-matching return types as long as they don't violate
1110bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      // the principle of substitutability.  Specifically, we permit
1111bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      // return types that are subclasses of the declared return type,
1112bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      // or that are more-qualified versions of the declared type.
1113bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
1114bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner        return false;
1115bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner
1116bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      DiagID = diag::warn_non_covariant_ret_types;
1117bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    }
1118bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  }
1119b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner
11202b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  S.Diag(MethodImpl->getLocation(), DiagID)
1121b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner    << MethodImpl->getDeclName()
1122b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner    << MethodDecl->getResultType()
11232b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    << MethodImpl->getResultType()
11242b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    << IsDeclaration
11252b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    << getTypeRange(MethodImpl->getResultTypeSourceInfo());
11262b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  S.Diag(MethodDecl->getLocation(), diag::note_previous_definition)
11272b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson    << getTypeRange(MethodDecl->getResultTypeSourceInfo());
11282b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  return false;
11292b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson}
11302b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson
11312b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlssonstatic bool CheckMethodOverrideParam(Sema &S,
11322b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson                                     ObjCMethodDecl *MethodImpl,
11332b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson                                     ObjCMethodDecl *MethodDecl,
11342b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson                                     ParmVarDecl *ImplVar,
11352b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson                                     ParmVarDecl *IfaceVar,
11362b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson                                     bool IsProtocolMethodDecl,
11372b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson                                     bool IsDeclaration,
11382b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson                                     bool Warn) {
113996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  if (IsProtocolMethodDecl &&
11402b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson      (ImplVar->getObjCDeclQualifier() !=
1141ff75e1db95a53c7606e0bb114cf9adc59ab3d7f6Chris Lattner       IfaceVar->getObjCDeclQualifier())) {
1142d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar    if (Warn) {
11439fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner      S.Diag(ImplVar->getLocation(),
1144b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner             diag::warn_conflicting_param_modifiers)
114534809507232bc4c3c4840c7d092c7440219fddafChris Lattner          << getTypeRange(ImplVar->getTypeSourceInfo())
11460558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner          << MethodImpl->getDeclName() << IsDeclaration;
11470558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner      S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
11480558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner          << getTypeRange(IfaceVar->getTypeSourceInfo());
11490558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    }
11500558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    else
11510558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner      return false;
11520558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner  }
11530558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner
1154bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  QualType ImplTy = ImplVar->getType();
1155bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  QualType IfaceTy = IfaceVar->getType();
115642745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar
1157bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
11580558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner    return true;
115934809507232bc4c3c4840c7d092c7440219fddafChris Lattner
116062b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner  if (!Warn)
116162b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    return false;
116262b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner  unsigned DiagID = diag::warn_conflicting_param_types;
116362b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner
116462b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner  // Mismatches between ObjC pointers go into a different warning
116562b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner  // category, and sometimes they're even completely whitelisted.
116662b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner  if (const ObjCObjectPointerType *ImplPtrTy =
116762b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner        ImplTy->getAs<ObjCObjectPointerType>()) {
116834809507232bc4c3c4840c7d092c7440219fddafChris Lattner    if (const ObjCObjectPointerType *IfacePtrTy =
1169b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner          IfaceTy->getAs<ObjCObjectPointerType>()) {
1170bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      // Allow non-matching argument types as long as they don't
1171bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      // violate the principle of substitutability.  Specifically, the
1172bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      // implementation must accept any objects that the superclass
1173bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      // accepts, however it may also accept others.
1174bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner      if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
11759fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner        return false;
1176bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner
11779fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner      DiagID = diag::warn_non_contravariant_param_types;
11789fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner    }
117962b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner  }
118062b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner
1181bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  S.Diag(ImplVar->getLocation(), DiagID)
1182bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    << getTypeRange(ImplVar->getTypeSourceInfo())
11833c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson    << MethodImpl->getDeclName() << IfaceTy << ImplTy
1184bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner    << IsDeclaration;
1185bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner  S.Diag(IfaceVar->getLocation(), diag::note_previous_definition)
118662b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner    << getTypeRange(IfaceVar->getTypeSourceInfo());
118762b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner  return false;
1188bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner}
118962b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner
11900558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner/// In ARC, check whether the conventional meanings of the two methods
1191bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar/// match.  If they don't, it's a hard error.
11920558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattnerstatic bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
11930558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner                                      ObjCMethodDecl *decl) {
1194bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  ObjCMethodFamily implFamily = impl->getMethodFamily();
1195219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  ObjCMethodFamily declFamily = decl->getMethodFamily();
11966379a7a15335e0af543a942efe9cfd514a83dab8Daniel Dunbar  if (implFamily == declFamily) return false;
11977c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
11987c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  // Since conventions are sorted by selector, the only possibility is
1199219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  // that the types differ enough to cause one selector or the other
120040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  // to fall out of the family.
1201219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  assert(implFamily == OMF_None || declFamily == OMF_None);
120240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis
1203219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar  // No further diagnostics required on invalid declarations.
1204bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar  if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1205bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar
1206bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  const ObjCMethodDecl *unmatched = impl;
120740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  ObjCMethodFamily family = declFamily;
1208bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  unsigned errorID = diag::err_arc_lost_method_convention;
1209bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  unsigned noteID = diag::note_arc_lost_method_convention;
1210bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  if (declFamily == OMF_None) {
1211bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    unmatched = decl;
1212bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    family = implFamily;
1213bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    errorID = diag::err_arc_gained_method_convention;
1214bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    noteID = diag::note_arc_gained_method_convention;
1215bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  }
1216bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
1217bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  // Indexes into a %select clause in the diagnostic.
1218bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  enum FamilySelector {
1219bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1220b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner  };
1221bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  FamilySelector familySelector = FamilySelector();
1222bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
122396e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  switch (family) {
1224bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  case OMF_None: llvm_unreachable("logic error, no method convention");
1225bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  case OMF_retain:
1226bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  case OMF_release:
1227bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  case OMF_autorelease:
1228bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  case OMF_dealloc:
1229bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  case OMF_retainCount:
1230bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  case OMF_self:
1231bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  case OMF_performSelector:
1232bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    // Mismatches for these methods don't change ownership
1233bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    // conventions, so we don't care.
1234bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    return false;
1235bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
1236bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  case OMF_init: familySelector = F_init; break;
1237bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  case OMF_alloc: familySelector = F_alloc; break;
1238bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  case OMF_copy: familySelector = F_copy; break;
1239bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1240bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  case OMF_new: familySelector = F_new; break;
1241bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  }
1242bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
1243bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1244bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  ReasonSelector reasonSelector;
1245bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
1246bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  // The only reason these methods don't fall within their families is
1247bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  // due to unusual result types.
1248bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  if (unmatched->getResultType()->isObjCObjectPointerType()) {
1249bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    reasonSelector = R_UnrelatedReturn;
1250bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  } else {
12513c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson    reasonSelector = R_NonObjectReturn;
1252bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  }
1253bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
1254bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1255bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1256bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
1257bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  return true;
1258bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner}
1259bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner
12607c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbarvoid Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
12617c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar                                       ObjCMethodDecl *MethodDecl,
12627c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar                                       bool IsProtocolMethodDecl,
126340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis                                       bool IsDeclaration) {
12647c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  if (getLangOptions().ObjCAutoRefCount &&
12657c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar      checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
12666fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    return;
12677c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
12687c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar  CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
12697c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar                            IsProtocolMethodDecl, IsDeclaration, true);
12707c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
127140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
127240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis       IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
12737c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar       IM != EM; ++IM, ++IF)
12747c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar    CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
12757c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar                             IsProtocolMethodDecl, IsDeclaration, true);
12767c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar
1277bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner  if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
1278bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner    Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic)
1279b808c952bbff821dce727dd801a1098d64394f98Chris Lattner      << IsDeclaration;
1280b808c952bbff821dce727dd801a1098d64394f98Chris Lattner    Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
1281c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stump  }
12823e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor}
12833e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
12843e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// WarnExactTypedMethods - This routine issues a warning if method
1285bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner/// implementation declaration matches exactly that of its declaration.
12863e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregorvoid Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
12873e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor                                 ObjCMethodDecl *MethodDecl,
12883e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor                                 bool IsProtocolMethodDecl) {
12893e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  // don't issue warning when protocol method is optional because primary
1290bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // class is not required to implement it and it is safe for protocol
1291bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  // to implement it.
129286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
129386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return;
129486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // don't issue warning when primary class's method is
1295370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor  // depecated/unavailable.
1296bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner  if (MethodDecl->hasAttr<UnavailableAttr>() ||
1297bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner      MethodDecl->hasAttr<DeprecatedAttr>())
1298bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner    return;
1299b808c952bbff821dce727dd801a1098d64394f98Chris Lattner
1300b808c952bbff821dce727dd801a1098d64394f98Chris Lattner  bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1301bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner                                      IsProtocolMethodDecl, false, false);
1302b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner  if (match)
1303bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner    for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1304bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner         IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
13057acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner         IM != EM; ++IM, ++IF) {
13067acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner      match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
13077acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner                                       *IM, *IF,
13087acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner                                       IsProtocolMethodDecl, false, false);
13097acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner      if (!match)
1310bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner        break;
13115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
13125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (match)
13130032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
13144e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner
13155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (match) {
1316c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    Diag(ImpMethodDecl->getLocation(),
13170c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman         diag::warn_category_method_impl_match);
13180c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman    Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
13190032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  }
13204e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner}
13210c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman
13220c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
132341ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio/// improve the efficiency of selector lookups and type checking by associating
132441ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio/// with each protocol / interface / category the flattened instance tables. If
13250032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson/// we used an immutable set to keep the table then it wouldn't add significant
13264e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner/// memory cost and it would be handy for lookups.
132741ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio
13287acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner/// CheckProtocolMethodDefs - This routine checks unimplemented methods
13291d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar/// Declared in protocol, and those referenced by it.
13301d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbarvoid Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
13311d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar                                   ObjCProtocolDecl *PDecl,
133270ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar                                   bool& IncompleteImpl,
13331d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar                                   const llvm::DenseSet<Selector> &InsMap,
13341d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar                                   const llvm::DenseSet<Selector> &ClsMap,
13351d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar                                   ObjCContainerDecl *CDecl) {
13361d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  ObjCInterfaceDecl *IDecl;
13371d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
13381d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar    IDecl = C->getClassInterface();
13391d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  else
13401d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar    IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
13411d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
13421d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar
13431d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  ObjCInterfaceDecl *Super = IDecl->getSuperClass();
13441d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  ObjCInterfaceDecl *NSIDecl = 0;
13451d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  if (getLangOptions().NeXTRuntime) {
13461d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar    // check to see if class implements forwardInvocation method and objects
13471d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar    // of this class are derived from 'NSProxy' so that to forward requests
1348e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff    // from one object to another.
13491d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar    // Under such conditions, which means that every method possible is
13501d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar    // implemented in the class, we should not issue "Method definition not
13511d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar    // found" warnings.
13521d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar    // FIXME: Use a general GetUnarySelector method for this.
13531d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar    IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
13541d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar    Selector fISelector = Context.Selectors.getSelector(1, &II);
13551d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar    if (InsMap.count(fISelector))
13561d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar      // Is IDecl derived from 'NSProxy'? If so, no instance methods
13571d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar      // need be implemented in the implementation.
13581d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar      NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
13591d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  }
13601d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar
1361e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff  // If a method lookup fails locally we still need to look and see if
13621d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  // the method was implemented by a base class or an inherited
136370ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar  // protocol. This lookup is slow, but occurs rarely in correct code
13641d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  // and otherwise would terminate in a warning.
136570ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar
136670ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar  // check unimplemented instance methods.
136770ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar  if (!NSIDecl)
136870ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar    for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
136970ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar         E = PDecl->instmeth_end(); I != E; ++I) {
137070ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar      ObjCMethodDecl *method = *I;
137170ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar      if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
137270ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar          !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
137370ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar          (!Super ||
137470ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar           !Super->lookupInstanceMethod(method->getSelector()))) {
137570ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar            // Ugly, but necessary. Method declared in protcol might have
137670ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar            // have been synthesized due to a property declared in the class which
137770ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar            // uses the protocol.
137870ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar            ObjCMethodDecl *MethodInClass =
137970ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar            IDecl->lookupInstanceMethod(method->getSelector());
138070ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar            if (!MethodInClass || !MethodInClass->isSynthesized()) {
138170ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar              unsigned DIAG = diag::warn_unimplemented_protocol_method;
1382434da48d0e35764f18b3fc96c75504746050b046Daniel Dunbar              if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1383434da48d0e35764f18b3fc96c75504746050b046Daniel Dunbar                      != Diagnostic::Ignored) {
1384434da48d0e35764f18b3fc96c75504746050b046Daniel Dunbar                WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
138570ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar                Diag(method->getLocation(), diag::note_method_declared_at);
13861d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar                Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
138770ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar                  << PDecl->getDeclName();
13881d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar              }
13891d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar            }
13901d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar          }
13911d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar    }
13921d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  // check unimplemented class methods
13931d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar  for (ObjCProtocolDecl::classmeth_iterator
13941d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar         I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
139570ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar       I != E; ++I) {
139670ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar    ObjCMethodDecl *method = *I;
139770ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar    if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1398c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson        !ClsMap.count(method->getSelector()) &&
13991d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar        (!Super || !Super->lookupClassMethod(method->getSelector()))) {
14001d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar      unsigned DIAG = diag::warn_unimplemented_protocol_method;
1401c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson      if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
14020032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson        WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
14030032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson        Diag(method->getLocation(), diag::note_method_declared_at);
14043e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar        Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1405c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson          PDecl->getDeclName();
14069d4a15fd3b85434c43ea27562793de63a793321aChris Lattner      }
1407c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson    }
1408c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  }
140996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson  // Check on this protocols's referenced protocols, recursively.
14109d4a15fd3b85434c43ea27562793de63a793321aChris Lattner  for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
14119d4a15fd3b85434c43ea27562793de63a793321aChris Lattner       E = PDecl->protocol_end(); PI != E; ++PI)
14123e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar    CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
14133e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar}
14143c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson
1415c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson/// MatchAllMethodDeclarations - Check methods declared in interface
1416c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson/// or protocol against those declared in their implementations.
1417e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson///
14183e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbarvoid Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1419e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson                                      const llvm::DenseSet<Selector> &ClsMap,
1420e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson                                      llvm::DenseSet<Selector> &InsMapSeen,
1421e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson                                      llvm::DenseSet<Selector> &ClsMapSeen,
14225add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson                                      ObjCImplDecl* IMPDecl,
142344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                      ObjCContainerDecl* CDecl,
1424c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson                                      bool &IncompleteImpl,
14255add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson                                      bool ImmediateClass,
1426c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson                                      bool WarnExactMatch) {
1427c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson  // Check and see if instance methods in class interface have been
14283e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar  // implemented in the implementation class. If so, their types match.
14295add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson  for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
14305add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson       E = CDecl->instmeth_end(); I != E; ++I) {
14315add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson    if (InsMapSeen.count((*I)->getSelector()))
1432c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson        continue;
14330032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    InsMapSeen.insert((*I)->getSelector());
1434a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar    if (!(*I)->isSynthesized() &&
1435a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar        !InsMap.count((*I)->getSelector())) {
1436a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar      if (ImmediateClass)
143795b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner        WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1438a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar                            diag::note_undef_method_impl);
1439a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar      continue;
1440a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar    } else {
144195b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner      ObjCMethodDecl *ImpMethodDecl =
144295b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner      IMPDecl->getInstanceMethod((*I)->getSelector());
1443a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar      ObjCMethodDecl *MethodDecl =
1444a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar      CDecl->getInstanceMethod((*I)->getSelector());
1445a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar      assert(MethodDecl &&
144695b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner             "MethodDecl is null in ImplMethodsVsClassMethods");
1447a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar      // ImpMethodDecl may be null as in a @dynamic property.
144895b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner      if (ImpMethodDecl) {
1449a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar        if (!WarnExactMatch)
1450a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar          WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1451a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar                                      isa<ObjCProtocolDecl>(CDecl));
1452a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar        else
14538e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar          WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
14541c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson                               isa<ObjCProtocolDecl>(CDecl));
145595b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner      }
1456a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar    }
14578e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar  }
1458a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar
1459a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar  // Check and see if class methods in class interface have been
1460a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar  // implemented in the implementation class. If so, their types match.
1461a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar   for (ObjCInterfaceDecl::classmeth_iterator
14625add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson       I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
14635add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson     if (ClsMapSeen.count((*I)->getSelector()))
1464c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson       continue;
1465c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson     ClsMapSeen.insert((*I)->getSelector());
14665add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson    if (!ClsMap.count((*I)->getSelector())) {
1467c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson      if (ImmediateClass)
1468c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson        WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
146908e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson                            diag::note_undef_method_impl);
14701c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson    } else {
147195b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner      ObjCMethodDecl *ImpMethodDecl =
147295b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner        IMPDecl->getClassMethod((*I)->getSelector());
14738e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar      ObjCMethodDecl *MethodDecl =
14748e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar        CDecl->getClassMethod((*I)->getSelector());
14751d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar      if (!WarnExactMatch)
14763e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar        WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
14770c67829763b98bc670062b553897a851fab17401Anders Carlsson                                    isa<ObjCProtocolDecl>(CDecl));
1478c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson      else
147945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner        WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
14806143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar                             isa<ObjCProtocolDecl>(CDecl));
14811e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar    }
14826143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  }
14831e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar
14841e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar  if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
14851e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar    // Also methods in class extensions need be looked at next.
14861e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar    for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
14871e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar         ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
14881e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar      MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
14891e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar                                 IMPDecl,
1490dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner                                 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
14911e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar                                 IncompleteImpl, false, WarnExactMatch);
14921e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar
1493dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner    // Check for any implementation of a methods declared in protocol.
1494dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner    for (ObjCInterfaceDecl::all_protocol_iterator
1495dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner          PI = I->all_referenced_protocol_begin(),
1496dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner          E = I->all_referenced_protocol_end(); PI != E; ++PI)
14971e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar      MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
14981e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar                                 IMPDecl,
14991e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar                                 (*PI), IncompleteImpl, false, WarnExactMatch);
15001e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar
15011e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar    // Check for any type mismtch of methods declared in class
15026143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar    // and methods declared in protocol. Do this only when the class
15036143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar    // is being implementaed.
15046143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar    if (isa<ObjCImplementationDecl>(IMPDecl))
15056143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar      MatchMethodsInClassAndItsProtocol(I);
15066143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar
15076143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar    // FIXME. For now, we are not checking for extact match of methods
15086143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar    // in category implementation and its primary class's super class.
15096143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar    if (!WarnExactMatch && I->getSuperClass())
1510eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner      MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1511eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner                                 IMPDecl,
1512eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner                                 I->getSuperClass(), IncompleteImpl, false);
1513eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  }
1514eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner}
1515eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner
1516a210f350fa78c263caa26e0f999cce85bb235309Eli Friedmanstatic void MatchMethodsInClassAndOneProtocol(Sema &S,
1517a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman                                              Sema::SelectorSet &InsMap,
1518eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner                                              Sema::SelectorSet &ClsMap,
1519eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner                                              const ObjCContainerDecl *IDecl,
1520eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner                                              const ObjCProtocolDecl *PDecl) {
1521a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner  if (!InsMap.empty())
152245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner    for (ObjCInterfaceDecl::instmeth_iterator IM = PDecl->instmeth_begin(),
152345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner         E = PDecl->instmeth_end(); IM != E; ++IM) {
15245fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar      Selector Sel = (*IM)->getSelector();
15255fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar      if (InsMap.count(Sel)) {
15266143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar        ObjCMethodDecl *ProtoMethodDecl = PDecl->getInstanceMethod(Sel);
15270032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson        ObjCMethodDecl *ClsMethodDecl = IDecl->getInstanceMethod(Sel);
15280032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson        if (ProtoMethodDecl && ClsMethodDecl)
152945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner          S.WarnConflictingTypedMethods(
153045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner                                      ClsMethodDecl,
15311c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson                                      ProtoMethodDecl, true, true);
153295b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner        InsMap.erase(Sel);
15331c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson      }
153445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner      if (InsMap.empty())
153545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner        break;
15366143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar    }
15376143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  if (!ClsMap.empty())
15386143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar    for (ObjCInterfaceDecl::classmeth_iterator IM = PDecl->classmeth_begin(),
15396143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar         E = PDecl->classmeth_end(); IM != E; ++IM) {
15406143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar      Selector Sel = (*IM)->getSelector();
15416143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar      if (ClsMap.count(Sel)) {
15426143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar        ObjCMethodDecl *ProtoMethodDecl = PDecl->getClassMethod(Sel);
15436143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar        ObjCMethodDecl *ClsMethodDecl = IDecl->getClassMethod(Sel);
15445fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar        if (ProtoMethodDecl && ClsMethodDecl)
15455fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar          S.WarnConflictingTypedMethods(
15468e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar                                        ClsMethodDecl,
15478e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar                                        ProtoMethodDecl, true, true);
15488e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar        ClsMap.erase(Sel);
15498e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar      }
155095b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner      if (ClsMap.empty())
15518e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar        break;
15528e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar    }
15538e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar  if (InsMap.empty() && ClsMap.empty())
15545fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar    return;
155545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner
155645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
155795b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner       PE = PDecl->protocol_end(); PI != PE; ++PI)
155845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner    MatchMethodsInClassAndOneProtocol(S, InsMap, ClsMap, IDecl, (*PI));
155945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner}
1560eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner
156145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner/// MatchMethodsInClassAndItsProtocol - Check that any redeclaration of
156245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner/// method in protocol in its qualified class match in their type and
15635fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar/// issue warnings otherwise.
156445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattnervoid Sema::MatchMethodsInClassAndItsProtocol(const ObjCInterfaceDecl *CDecl) {
156545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner  if (CDecl->all_referenced_protocol_begin() ==
156645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner      CDecl->all_referenced_protocol_end())
15676143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar    return;
15686143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar
15696143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  SelectorSet InsMap, ClsMap;
15706143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar  for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
15715fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar       E = CDecl->instmeth_end(); I != E; ++I)
15725fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar    if (!InsMap.count((*I)->getSelector()))
1573c9f29c61856ffb5f643cedbe87ac076f21a1381aChris Lattner      InsMap.insert((*I)->getSelector());
15746143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar
157541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  for (ObjCInterfaceDecl::classmeth_iterator
1576af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar       I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I)
1577af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    if (!ClsMap.count((*I)->getSelector()))
1578af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      ClsMap.insert((*I)->getSelector());
1579af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
1580653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor  if (!InsMap.empty() || !ClsMap.empty())
158117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    for (ObjCInterfaceDecl::all_protocol_iterator
1582af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar         PI = CDecl->all_referenced_protocol_begin(),
1583af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar         E = CDecl->all_referenced_protocol_end(); PI != E; ++PI)
1584af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      MatchMethodsInClassAndOneProtocol(*this, InsMap, ClsMap, CDecl, (*PI));
1585af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
1586af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  // Also for class extensions
1587af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  if (!CDecl->getFirstClassExtension())
1588af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    return;
1589af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
1590af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  for (const ObjCCategoryDecl *ClsExtDecl = CDecl->getFirstClassExtension();
1591af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar       ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
1592af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    InsMap.clear();
159317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    ClsMap.clear();
1594fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian    for (ObjCCategoryDecl::instmeth_iterator I = ClsExtDecl->instmeth_begin(),
1595fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian         E = ClsExtDecl->instmeth_end(); I != E; ++I)
1596af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      if (!InsMap.count((*I)->getSelector()))
159717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis        InsMap.insert((*I)->getSelector());
1598fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian    for (ObjCCategoryDecl::classmeth_iterator I = ClsExtDecl->classmeth_begin(),
1599fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian         E = ClsExtDecl->classmeth_end(); I != E; ++I)
1600af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      if (!ClsMap.count((*I)->getSelector()))
1601af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar        ClsMap.insert((*I)->getSelector());
1602af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    if (InsMap.empty() && ClsMap.empty())
1603af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar      continue;
160491e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson    for (ObjCInterfaceDecl::all_protocol_iterator
1605984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson         PI = CDecl->all_referenced_protocol_begin(),
160617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         E = CDecl->all_referenced_protocol_end(); PI != E; ++PI)
1607984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson      MatchMethodsInClassAndOneProtocol(*this, InsMap, ClsMap, ClsExtDecl, (*PI));
1608984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson  }
1609984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson}
1610984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson
161191e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
161291e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson/// category matches with those implemented in its primary class and
1613f976be8a63f9e0d3bd50e33dadef02c3c9921747Eli Friedman/// warns each time an exact match is found.
1614f976be8a63f9e0d3bd50e33dadef02c3c9921747Eli Friedmanvoid Sema::CheckCategoryVsClassMethodMatches(
161591e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson                                  ObjCCategoryImplDecl *CatIMPDecl) {
161691e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson  llvm::DenseSet<Selector> InsMap, ClsMap;
161791e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson
161891e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson  for (ObjCImplementationDecl::instmeth_iterator
161917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       I = CatIMPDecl->instmeth_begin(),
162091e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson       E = CatIMPDecl->instmeth_end(); I!=E; ++I)
162191e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson    InsMap.insert((*I)->getSelector());
162291e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson
162391e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson  for (ObjCImplementationDecl::classmeth_iterator
162441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar       I = CatIMPDecl->classmeth_begin(),
162541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar       E = CatIMPDecl->classmeth_end(); I != E; ++I)
162641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    ClsMap.insert((*I)->getSelector());
162741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  if (InsMap.empty() && ClsMap.empty())
162841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    return;
162941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
163041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // Get category's primary class.
163141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
163216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  if (!CatDecl)
163316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return;
163416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
163516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  if (!IDecl)
163641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    return;
1637293361afd4199c92aabff9267fddea890943c586Anders Carlsson  llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
16382b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  bool IncompleteImpl = false;
163941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
164016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor                             CatIMPDecl, IDecl,
164116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor                             IncompleteImpl, false, true /*WarnExactMatch*/);
164216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
164316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
164416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorvoid Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
164516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor                                     ObjCContainerDecl* CDecl,
164641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar                                     bool IncompleteImpl) {
16472a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson  llvm::DenseSet<Selector> InsMap;
164841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // Check and see if instance methods in class interface have been
164941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // implemented in the implementation class.
165095d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson  for (ObjCImplementationDecl::instmeth_iterator
165141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar         I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
1652984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson    InsMap.insert((*I)->getSelector());
165341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
16549cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor  // Check and see if properties declared in the interface have either 1)
16559cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor  // an implementation or 2) there is a @synthesize/@dynamic implementation
1656127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // of the property in the @implementation.
1657127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  if (isa<ObjCInterfaceDecl>(CDecl) &&
16589cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor        !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
165995d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson    DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
166095d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson
166195d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson  llvm::DenseSet<Selector> ClsMap;
166227ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson  for (ObjCImplementationDecl::classmeth_iterator
166327ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson       I = IMPDecl->classmeth_begin(),
166427ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson       E = IMPDecl->classmeth_end(); I != E; ++I)
166536674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson    ClsMap.insert((*I)->getSelector());
166636674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson
166736674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson  // Check for type conflict of methods declared in a class/protocol and
166836674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson  // its implementation; if any.
166936674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson  llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
167095d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson  MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
167141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar                             IMPDecl, CDecl,
167238e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian                             IncompleteImpl, true);
167341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // check all methods implemented in category against those declared
167441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // in its primary class.
1675b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian  if (ObjCCategoryImplDecl *CatDecl =
1676b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian        dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
167741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    CheckCategoryVsClassMethodMatches(CatDecl);
1678285d0dba947b7c9960eaa88e8c4fced0398d4319Chris Lattner
167941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // Check the protocol list for unimplemented methods in the @implementation
1680b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian  // class.
168141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // Check and see if class methods in class interface have been
168241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  // implemented in the implementation class.
168341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
1684af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
1685af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    for (ObjCInterfaceDecl::all_protocol_iterator
168641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar          PI = I->all_referenced_protocol_begin(),
168741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar          E = I->all_referenced_protocol_end(); PI != E; ++PI)
168841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar      CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1689af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar                              InsMap, ClsMap, I);
1690af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    // Check class extensions (unnamed categories)
1691af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar    for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1692af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar         Categories; Categories = Categories->getNextClassExtension())
169341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar      ImplMethodsVsClassMethods(S, IMPDecl,
1694af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar                                const_cast<ObjCCategoryDecl*>(Categories),
169541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar                                IncompleteImpl);
169641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
169741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // For extended class, unimplemented methods in its protocols will
16986fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    // be reported in the primary class.
169941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    if (!C->IsClassExtension()) {
170041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar      for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
170141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar           E = C->protocol_end(); PI != E; ++PI)
170241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar        CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
1703305c658ebce84bb9833fc0e7675554656453b8e8Fariborz Jahanian                                InsMap, ClsMap, CDecl);
170441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar      // Report unimplemented properties in the category as well.
170541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar      // When reporting on missing setter/getters, do not report when
170691e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson      // setter/getter is implemented in category's primary class
170791e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson      // implementation.
170841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar      if (ObjCInterfaceDecl *ID = C->getClassInterface())
170941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar        if (ObjCImplDecl *IMP = ID->getImplementation()) {
171041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar          for (ObjCImplementationDecl::instmeth_iterator
171141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar               I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
171241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar            InsMap.insert((*I)->getSelector());
171341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar        }
171441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar      DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
171541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    }
171641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  } else
171741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    assert(false && "invalid ObjCContainerDecl type.");
171841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar}
171941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
172041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// ActOnForwardClassDeclaration -
172141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel DunbarDecl *
172241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel DunbarSema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
172341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar                                   IdentifierInfo **IdentList,
1724f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump                                   SourceLocation *IdentLocs,
1725f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump                                   unsigned NumElts) {
1726f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
172741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar
172841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar  for (unsigned i = 0; i != NumElts; ++i) {
172941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar    // Check for another declaration kind with the same name.
1730    NamedDecl *PrevDecl
1731      = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
1732                         LookupOrdinaryName, ForRedeclaration);
1733    if (PrevDecl && PrevDecl->isTemplateParameter()) {
1734      // Maybe we will complain about the shadowed template parameter.
1735      DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1736      // Just pretend that we didn't see the previous declaration.
1737      PrevDecl = 0;
1738    }
1739
1740    if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
1741      // GCC apparently allows the following idiom:
1742      //
1743      // typedef NSObject < XCElementTogglerP > XCElementToggler;
1744      // @class XCElementToggler;
1745      //
1746      // FIXME: Make an extension?
1747      TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
1748      if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
1749        Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
1750        Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1751      } else {
1752        // a forward class declaration matching a typedef name of a class refers
1753        // to the underlying class.
1754        if (const ObjCObjectType *OI =
1755              TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1756          PrevDecl = OI->getInterface();
1757      }
1758    }
1759    ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1760    if (!IDecl) {  // Not already seen?  Make a forward decl.
1761      IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1762                                        IdentList[i], IdentLocs[i], true);
1763
1764      // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1765      // the current DeclContext.  This prevents clients that walk DeclContext
1766      // from seeing the imaginary ObjCInterfaceDecl until it is actually
1767      // declared later (if at all).  We also take care to explicitly make
1768      // sure this declaration is visible for name lookup.
1769      PushOnScopeChains(IDecl, TUScope, false);
1770      CurContext->makeDeclVisibleInContext(IDecl, true);
1771    }
1772
1773    Interfaces.push_back(IDecl);
1774  }
1775
1776  assert(Interfaces.size() == NumElts);
1777  ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
1778                                               Interfaces.data(), IdentLocs,
1779                                               Interfaces.size());
1780  CurContext->addDecl(CDecl);
1781  CheckObjCDeclScope(CDecl);
1782  return CDecl;
1783}
1784
1785static bool tryMatchRecordTypes(ASTContext &Context,
1786                                Sema::MethodMatchStrategy strategy,
1787                                const Type *left, const Type *right);
1788
1789static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1790                       QualType leftQT, QualType rightQT) {
1791  const Type *left =
1792    Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1793  const Type *right =
1794    Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1795
1796  if (left == right) return true;
1797
1798  // If we're doing a strict match, the types have to match exactly.
1799  if (strategy == Sema::MMS_strict) return false;
1800
1801  if (left->isIncompleteType() || right->isIncompleteType()) return false;
1802
1803  // Otherwise, use this absurdly complicated algorithm to try to
1804  // validate the basic, low-level compatibility of the two types.
1805
1806  // As a minimum, require the sizes and alignments to match.
1807  if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1808    return false;
1809
1810  // Consider all the kinds of non-dependent canonical types:
1811  // - functions and arrays aren't possible as return and parameter types
1812
1813  // - vector types of equal size can be arbitrarily mixed
1814  if (isa<VectorType>(left)) return isa<VectorType>(right);
1815  if (isa<VectorType>(right)) return false;
1816
1817  // - references should only match references of identical type
1818  // - structs, unions, and Objective-C objects must match more-or-less
1819  //   exactly
1820  // - everything else should be a scalar
1821  if (!left->isScalarType() || !right->isScalarType())
1822    return tryMatchRecordTypes(Context, strategy, left, right);
1823
1824  // Make scalars agree in kind, except count bools as chars.
1825  Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1826  Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1827  if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1828  if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
1829
1830  // Note that data member pointers and function member pointers don't
1831  // intermix because of the size differences.
1832
1833  return (leftSK == rightSK);
1834}
1835
1836static bool tryMatchRecordTypes(ASTContext &Context,
1837                                Sema::MethodMatchStrategy strategy,
1838                                const Type *lt, const Type *rt) {
1839  assert(lt && rt && lt != rt);
1840
1841  if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1842  RecordDecl *left = cast<RecordType>(lt)->getDecl();
1843  RecordDecl *right = cast<RecordType>(rt)->getDecl();
1844
1845  // Require union-hood to match.
1846  if (left->isUnion() != right->isUnion()) return false;
1847
1848  // Require an exact match if either is non-POD.
1849  if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1850      (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1851    return false;
1852
1853  // Require size and alignment to match.
1854  if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1855
1856  // Require fields to match.
1857  RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1858  RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1859  for (; li != le && ri != re; ++li, ++ri) {
1860    if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1861      return false;
1862  }
1863  return (li == le && ri == re);
1864}
1865
1866/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1867/// returns true, or false, accordingly.
1868/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
1869bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1870                                      const ObjCMethodDecl *right,
1871                                      MethodMatchStrategy strategy) {
1872  if (!matchTypes(Context, strategy,
1873                  left->getResultType(), right->getResultType()))
1874    return false;
1875
1876  if (getLangOptions().ObjCAutoRefCount &&
1877      (left->hasAttr<NSReturnsRetainedAttr>()
1878         != right->hasAttr<NSReturnsRetainedAttr>() ||
1879       left->hasAttr<NSConsumesSelfAttr>()
1880         != right->hasAttr<NSConsumesSelfAttr>()))
1881    return false;
1882
1883  ObjCMethodDecl::param_iterator
1884    li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
1885
1886  for (; li != le; ++li, ++ri) {
1887    assert(ri != right->param_end() && "Param mismatch");
1888    ParmVarDecl *lparm = *li, *rparm = *ri;
1889
1890    if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1891      return false;
1892
1893    if (getLangOptions().ObjCAutoRefCount &&
1894        lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1895      return false;
1896  }
1897  return true;
1898}
1899
1900/// \brief Read the contents of the method pool for a given selector from
1901/// external storage.
1902///
1903/// This routine should only be called once, when the method pool has no entry
1904/// for this selector.
1905Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
1906  assert(ExternalSource && "We need an external AST source");
1907  assert(MethodPool.find(Sel) == MethodPool.end() &&
1908         "Selector data already loaded into the method pool");
1909
1910  // Read the method list from the external source.
1911  GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
1912
1913  return MethodPool.insert(std::make_pair(Sel, Methods)).first;
1914}
1915
1916void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1917                                 bool instance) {
1918  GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1919  if (Pos == MethodPool.end()) {
1920    if (ExternalSource)
1921      Pos = ReadMethodPool(Method->getSelector());
1922    else
1923      Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1924                                             GlobalMethods())).first;
1925  }
1926  Method->setDefined(impl);
1927  ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
1928  if (Entry.Method == 0) {
1929    // Haven't seen a method with this selector name yet - add it.
1930    Entry.Method = Method;
1931    Entry.Next = 0;
1932    return;
1933  }
1934
1935  // We've seen a method with this name, see if we have already seen this type
1936  // signature.
1937  for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1938    bool match = MatchTwoMethodDeclarations(Method, List->Method);
1939
1940    if (match) {
1941      ObjCMethodDecl *PrevObjCMethod = List->Method;
1942      PrevObjCMethod->setDefined(impl);
1943      // If a method is deprecated, push it in the global pool.
1944      // This is used for better diagnostics.
1945      if (Method->isDeprecated()) {
1946        if (!PrevObjCMethod->isDeprecated())
1947          List->Method = Method;
1948      }
1949      // If new method is unavailable, push it into global pool
1950      // unless previous one is deprecated.
1951      if (Method->isUnavailable()) {
1952        if (PrevObjCMethod->getAvailability() < AR_Deprecated)
1953          List->Method = Method;
1954      }
1955      return;
1956    }
1957  }
1958
1959  // We have a new signature for an existing method - add it.
1960  // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
1961  ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1962  Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
1963}
1964
1965/// Determines if this is an "acceptable" loose mismatch in the global
1966/// method pool.  This exists mostly as a hack to get around certain
1967/// global mismatches which we can't afford to make warnings / errors.
1968/// Really, what we want is a way to take a method out of the global
1969/// method pool.
1970static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1971                                       ObjCMethodDecl *other) {
1972  if (!chosen->isInstanceMethod())
1973    return false;
1974
1975  Selector sel = chosen->getSelector();
1976  if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1977    return false;
1978
1979  // Don't complain about mismatches for -length if the method we
1980  // chose has an integral result type.
1981  return (chosen->getResultType()->isIntegerType());
1982}
1983
1984ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
1985                                               bool receiverIdOrClass,
1986                                               bool warn, bool instance) {
1987  GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1988  if (Pos == MethodPool.end()) {
1989    if (ExternalSource)
1990      Pos = ReadMethodPool(Sel);
1991    else
1992      return 0;
1993  }
1994
1995  ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
1996
1997  if (warn && MethList.Method && MethList.Next) {
1998    bool issueDiagnostic = false, issueError = false;
1999
2000    // We support a warning which complains about *any* difference in
2001    // method signature.
2002    bool strictSelectorMatch =
2003      (receiverIdOrClass && warn &&
2004       (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2005                                 R.getBegin()) !=
2006      Diagnostic::Ignored));
2007    if (strictSelectorMatch)
2008      for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
2009        if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2010                                        MMS_strict)) {
2011          issueDiagnostic = true;
2012          break;
2013        }
2014      }
2015
2016    // If we didn't see any strict differences, we won't see any loose
2017    // differences.  In ARC, however, we also need to check for loose
2018    // mismatches, because most of them are errors.
2019    if (!strictSelectorMatch ||
2020        (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
2021      for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
2022        // This checks if the methods differ in type mismatch.
2023        if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2024                                        MMS_loose) &&
2025            !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2026          issueDiagnostic = true;
2027          if (getLangOptions().ObjCAutoRefCount)
2028            issueError = true;
2029          break;
2030        }
2031      }
2032
2033    if (issueDiagnostic) {
2034      if (issueError)
2035        Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2036      else if (strictSelectorMatch)
2037        Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2038      else
2039        Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
2040
2041      Diag(MethList.Method->getLocStart(),
2042           issueError ? diag::note_possibility : diag::note_using)
2043        << MethList.Method->getSourceRange();
2044      for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2045        Diag(Next->Method->getLocStart(), diag::note_also_found)
2046          << Next->Method->getSourceRange();
2047    }
2048  }
2049  return MethList.Method;
2050}
2051
2052ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
2053  GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2054  if (Pos == MethodPool.end())
2055    return 0;
2056
2057  GlobalMethods &Methods = Pos->second;
2058
2059  if (Methods.first.Method && Methods.first.Method->isDefined())
2060    return Methods.first.Method;
2061  if (Methods.second.Method && Methods.second.Method->isDefined())
2062    return Methods.second.Method;
2063  return 0;
2064}
2065
2066/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2067/// identical selector names in current and its super classes and issues
2068/// a warning if any of their argument types are incompatible.
2069void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2070                                             ObjCMethodDecl *Method,
2071                                             bool IsInstance)  {
2072  ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2073  if (ID == 0) return;
2074
2075  while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
2076    ObjCMethodDecl *SuperMethodDecl =
2077        SD->lookupMethod(Method->getSelector(), IsInstance);
2078    if (SuperMethodDecl == 0) {
2079      ID = SD;
2080      continue;
2081    }
2082    ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2083      E = Method->param_end();
2084    ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2085    for (; ParamI != E; ++ParamI, ++PrevI) {
2086      // Number of parameters are the same and is guaranteed by selector match.
2087      assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2088      QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2089      QualType T2 = Context.getCanonicalType((*PrevI)->getType());
2090      // If type of argument of method in this class does not match its
2091      // respective argument type in the super class method, issue warning;
2092      if (!Context.typesAreCompatible(T1, T2)) {
2093        Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
2094          << T1 << T2;
2095        Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2096        return;
2097      }
2098    }
2099    ID = SD;
2100  }
2101}
2102
2103/// DiagnoseDuplicateIvars -
2104/// Check for duplicate ivars in the entire class at the start of
2105/// @implementation. This becomes necesssary because class extension can
2106/// add ivars to a class in random order which will not be known until
2107/// class's @implementation is seen.
2108void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2109                                  ObjCInterfaceDecl *SID) {
2110  for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2111       IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2112    ObjCIvarDecl* Ivar = (*IVI);
2113    if (Ivar->isInvalidDecl())
2114      continue;
2115    if (IdentifierInfo *II = Ivar->getIdentifier()) {
2116      ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2117      if (prevIvar) {
2118        Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2119        Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2120        Ivar->setInvalidDecl();
2121      }
2122    }
2123  }
2124}
2125
2126// Note: For class/category implemenations, allMethods/allProperties is
2127// always null.
2128void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2129                      Decl *ClassDecl,
2130                      Decl **allMethods, unsigned allNum,
2131                      Decl **allProperties, unsigned pNum,
2132                      DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
2133  // FIXME: If we don't have a ClassDecl, we have an error. We should consider
2134  // always passing in a decl. If the decl has an error, isInvalidDecl()
2135  // should be true.
2136  if (!ClassDecl)
2137    return;
2138
2139  bool isInterfaceDeclKind =
2140        isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2141         || isa<ObjCProtocolDecl>(ClassDecl);
2142  bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
2143
2144  if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2145    // FIXME: This is wrong.  We shouldn't be pretending that there is
2146    //  an '@end' in the declaration.
2147    SourceLocation L = ClassDecl->getLocation();
2148    AtEnd.setBegin(L);
2149    AtEnd.setEnd(L);
2150    Diag(L, diag::err_missing_atend);
2151  }
2152
2153  // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2154  llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2155  llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2156
2157  for (unsigned i = 0; i < allNum; i++ ) {
2158    ObjCMethodDecl *Method =
2159      cast_or_null<ObjCMethodDecl>(allMethods[i]);
2160
2161    if (!Method) continue;  // Already issued a diagnostic.
2162    if (Method->isInstanceMethod()) {
2163      /// Check for instance method of the same name with incompatible types
2164      const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
2165      bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
2166                              : false;
2167      if ((isInterfaceDeclKind && PrevMethod && !match)
2168          || (checkIdenticalMethods && match)) {
2169          Diag(Method->getLocation(), diag::err_duplicate_method_decl)
2170            << Method->getDeclName();
2171          Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2172        Method->setInvalidDecl();
2173      } else {
2174        InsMap[Method->getSelector()] = Method;
2175        /// The following allows us to typecheck messages to "id".
2176        AddInstanceMethodToGlobalPool(Method);
2177        // verify that the instance method conforms to the same definition of
2178        // parent methods if it shadows one.
2179        CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
2180      }
2181    } else {
2182      /// Check for class method of the same name with incompatible types
2183      const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
2184      bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
2185                              : false;
2186      if ((isInterfaceDeclKind && PrevMethod && !match)
2187          || (checkIdenticalMethods && match)) {
2188        Diag(Method->getLocation(), diag::err_duplicate_method_decl)
2189          << Method->getDeclName();
2190        Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2191        Method->setInvalidDecl();
2192      } else {
2193        ClsMap[Method->getSelector()] = Method;
2194        /// The following allows us to typecheck messages to "Class".
2195        AddFactoryMethodToGlobalPool(Method);
2196        // verify that the class method conforms to the same definition of
2197        // parent methods if it shadows one.
2198        CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
2199      }
2200    }
2201  }
2202  if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
2203    // Compares properties declared in this class to those of its
2204    // super class.
2205    ComparePropertiesInBaseAndSuper(I);
2206    CompareProperties(I, I);
2207  } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
2208    // Categories are used to extend the class by declaring new methods.
2209    // By the same token, they are also used to add new properties. No
2210    // need to compare the added property to those in the class.
2211
2212    // Compare protocol properties with those in category
2213    CompareProperties(C, C);
2214    if (C->IsClassExtension()) {
2215      ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2216      DiagnoseClassExtensionDupMethods(C, CCPrimary);
2217    }
2218  }
2219  if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
2220    if (CDecl->getIdentifier())
2221      // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2222      // user-defined setter/getter. It also synthesizes setter/getter methods
2223      // and adds them to the DeclContext and global method pools.
2224      for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2225                                            E = CDecl->prop_end();
2226           I != E; ++I)
2227        ProcessPropertyDecl(*I, CDecl);
2228    CDecl->setAtEndRange(AtEnd);
2229  }
2230  if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
2231    IC->setAtEndRange(AtEnd);
2232    if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
2233      // Any property declared in a class extension might have user
2234      // declared setter or getter in current class extension or one
2235      // of the other class extensions. Mark them as synthesized as
2236      // property will be synthesized when property with same name is
2237      // seen in the @implementation.
2238      for (const ObjCCategoryDecl *ClsExtDecl =
2239           IDecl->getFirstClassExtension();
2240           ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2241        for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2242             E = ClsExtDecl->prop_end(); I != E; ++I) {
2243          ObjCPropertyDecl *Property = (*I);
2244          // Skip over properties declared @dynamic
2245          if (const ObjCPropertyImplDecl *PIDecl
2246              = IC->FindPropertyImplDecl(Property->getIdentifier()))
2247            if (PIDecl->getPropertyImplementation()
2248                  == ObjCPropertyImplDecl::Dynamic)
2249              continue;
2250
2251          for (const ObjCCategoryDecl *CExtDecl =
2252               IDecl->getFirstClassExtension();
2253               CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2254            if (ObjCMethodDecl *GetterMethod =
2255                CExtDecl->getInstanceMethod(Property->getGetterName()))
2256              GetterMethod->setSynthesized(true);
2257            if (!Property->isReadOnly())
2258              if (ObjCMethodDecl *SetterMethod =
2259                  CExtDecl->getInstanceMethod(Property->getSetterName()))
2260                SetterMethod->setSynthesized(true);
2261          }
2262        }
2263      }
2264
2265      if (LangOpts.ObjCDefaultSynthProperties &&
2266          LangOpts.ObjCNonFragileABI2)
2267        DefaultSynthesizeProperties(S, IC, IDecl);
2268      ImplMethodsVsClassMethods(S, IC, IDecl);
2269      AtomicPropertySetterGetterRules(IC, IDecl);
2270      DiagnoseOwningPropertyGetterSynthesis(IC);
2271
2272      if (LangOpts.ObjCNonFragileABI2)
2273        while (IDecl->getSuperClass()) {
2274          DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2275          IDecl = IDecl->getSuperClass();
2276        }
2277    }
2278    SetIvarInitializers(IC);
2279  } else if (ObjCCategoryImplDecl* CatImplClass =
2280                                   dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
2281    CatImplClass->setAtEndRange(AtEnd);
2282
2283    // Find category interface decl and then check that all methods declared
2284    // in this interface are implemented in the category @implementation.
2285    if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
2286      for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
2287           Categories; Categories = Categories->getNextClassCategory()) {
2288        if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
2289          ImplMethodsVsClassMethods(S, CatImplClass, Categories);
2290          break;
2291        }
2292      }
2293    }
2294  }
2295  if (isInterfaceDeclKind) {
2296    // Reject invalid vardecls.
2297    for (unsigned i = 0; i != tuvNum; i++) {
2298      DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2299      for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2300        if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
2301          if (!VDecl->hasExternalStorage())
2302            Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
2303        }
2304    }
2305  }
2306}
2307
2308
2309/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2310/// objective-c's type qualifier from the parser version of the same info.
2311static Decl::ObjCDeclQualifier
2312CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
2313  return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
2314}
2315
2316static inline
2317bool containsInvalidMethodImplAttribute(const AttrVec &A) {
2318  // The 'ibaction' attribute is allowed on method definitions because of
2319  // how the IBAction macro is used on both method declarations and definitions.
2320  // If the method definitions contains any other attributes, return true.
2321  for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2322    if ((*i)->getKind() != attr::IBAction)
2323      return true;
2324  return false;
2325}
2326
2327/// \brief Check whether the declared result type of the given Objective-C
2328/// method declaration is compatible with the method's class.
2329///
2330static bool
2331CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2332                                    ObjCInterfaceDecl *CurrentClass) {
2333  QualType ResultType = Method->getResultType();
2334  SourceRange ResultTypeRange;
2335  if (const TypeSourceInfo *ResultTypeInfo = Method->getResultTypeSourceInfo())
2336    ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
2337
2338  // If an Objective-C method inherits its related result type, then its
2339  // declared result type must be compatible with its own class type. The
2340  // declared result type is compatible if:
2341  if (const ObjCObjectPointerType *ResultObjectType
2342                                = ResultType->getAs<ObjCObjectPointerType>()) {
2343    //   - it is id or qualified id, or
2344    if (ResultObjectType->isObjCIdType() ||
2345        ResultObjectType->isObjCQualifiedIdType())
2346      return false;
2347
2348    if (CurrentClass) {
2349      if (ObjCInterfaceDecl *ResultClass
2350                                      = ResultObjectType->getInterfaceDecl()) {
2351        //   - it is the same as the method's class type, or
2352        if (CurrentClass == ResultClass)
2353          return false;
2354
2355        //   - it is a superclass of the method's class type
2356        if (ResultClass->isSuperClassOf(CurrentClass))
2357          return false;
2358      }
2359    }
2360  }
2361
2362  return true;
2363}
2364
2365namespace {
2366/// A helper class for searching for methods which a particular method
2367/// overrides.
2368class OverrideSearch {
2369  Sema &S;
2370  ObjCMethodDecl *Method;
2371  llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2372  llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2373  bool Recursive;
2374
2375public:
2376  OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2377    Selector selector = method->getSelector();
2378
2379    // Bypass this search if we've never seen an instance/class method
2380    // with this selector before.
2381    Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2382    if (it == S.MethodPool.end()) {
2383      if (!S.ExternalSource) return;
2384      it = S.ReadMethodPool(selector);
2385    }
2386    ObjCMethodList &list =
2387      method->isInstanceMethod() ? it->second.first : it->second.second;
2388    if (!list.Method) return;
2389
2390    ObjCContainerDecl *container
2391      = cast<ObjCContainerDecl>(method->getDeclContext());
2392
2393    // Prevent the search from reaching this container again.  This is
2394    // important with categories, which override methods from the
2395    // interface and each other.
2396    Searched.insert(container);
2397    searchFromContainer(container);
2398  }
2399
2400  typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2401  iterator begin() const { return Overridden.begin(); }
2402  iterator end() const { return Overridden.end(); }
2403
2404private:
2405  void searchFromContainer(ObjCContainerDecl *container) {
2406    if (container->isInvalidDecl()) return;
2407
2408    switch (container->getDeclKind()) {
2409#define OBJCCONTAINER(type, base) \
2410    case Decl::type: \
2411      searchFrom(cast<type##Decl>(container)); \
2412      break;
2413#define ABSTRACT_DECL(expansion)
2414#define DECL(type, base) \
2415    case Decl::type:
2416#include "clang/AST/DeclNodes.inc"
2417      llvm_unreachable("not an ObjC container!");
2418    }
2419  }
2420
2421  void searchFrom(ObjCProtocolDecl *protocol) {
2422    // A method in a protocol declaration overrides declarations from
2423    // referenced ("parent") protocols.
2424    search(protocol->getReferencedProtocols());
2425  }
2426
2427  void searchFrom(ObjCCategoryDecl *category) {
2428    // A method in a category declaration overrides declarations from
2429    // the main class and from protocols the category references.
2430    search(category->getClassInterface());
2431    search(category->getReferencedProtocols());
2432  }
2433
2434  void searchFrom(ObjCCategoryImplDecl *impl) {
2435    // A method in a category definition that has a category
2436    // declaration overrides declarations from the category
2437    // declaration.
2438    if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2439      search(category);
2440
2441    // Otherwise it overrides declarations from the class.
2442    } else {
2443      search(impl->getClassInterface());
2444    }
2445  }
2446
2447  void searchFrom(ObjCInterfaceDecl *iface) {
2448    // A method in a class declaration overrides declarations from
2449
2450    //   - categories,
2451    for (ObjCCategoryDecl *category = iface->getCategoryList();
2452           category; category = category->getNextClassCategory())
2453      search(category);
2454
2455    //   - the super class, and
2456    if (ObjCInterfaceDecl *super = iface->getSuperClass())
2457      search(super);
2458
2459    //   - any referenced protocols.
2460    search(iface->getReferencedProtocols());
2461  }
2462
2463  void searchFrom(ObjCImplementationDecl *impl) {
2464    // A method in a class implementation overrides declarations from
2465    // the class interface.
2466    search(impl->getClassInterface());
2467  }
2468
2469
2470  void search(const ObjCProtocolList &protocols) {
2471    for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2472         i != e; ++i)
2473      search(*i);
2474  }
2475
2476  void search(ObjCContainerDecl *container) {
2477    // Abort if we've already searched this container.
2478    if (!Searched.insert(container)) return;
2479
2480    // Check for a method in this container which matches this selector.
2481    ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2482                                                Method->isInstanceMethod());
2483
2484    // If we find one, record it and bail out.
2485    if (meth) {
2486      Overridden.insert(meth);
2487      return;
2488    }
2489
2490    // Otherwise, search for methods that a hypothetical method here
2491    // would have overridden.
2492
2493    // Note that we're now in a recursive case.
2494    Recursive = true;
2495
2496    searchFromContainer(container);
2497  }
2498};
2499}
2500
2501Decl *Sema::ActOnMethodDeclaration(
2502    Scope *S,
2503    SourceLocation MethodLoc, SourceLocation EndLoc,
2504    tok::TokenKind MethodType, Decl *ClassDecl,
2505    ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
2506    SourceLocation SelectorStartLoc,
2507    Selector Sel,
2508    // optional arguments. The number of types/arguments is obtained
2509    // from the Sel.getNumArgs().
2510    ObjCArgInfo *ArgInfo,
2511    DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
2512    AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
2513    bool isVariadic, bool MethodDefinition) {
2514  // Make sure we can establish a context for the method.
2515  if (!ClassDecl) {
2516    Diag(MethodLoc, diag::error_missing_method_context);
2517    return 0;
2518  }
2519  QualType resultDeclType;
2520
2521  TypeSourceInfo *ResultTInfo = 0;
2522  if (ReturnType) {
2523    resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
2524
2525    // Methods cannot return interface types. All ObjC objects are
2526    // passed by reference.
2527    if (resultDeclType->isObjCObjectType()) {
2528      Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2529        << 0 << resultDeclType;
2530      return 0;
2531    }
2532  } else { // get the type for "id".
2533    resultDeclType = Context.getObjCIdType();
2534    Diag(MethodLoc, diag::warn_missing_method_return_type)
2535      << FixItHint::CreateInsertion(SelectorStartLoc, "(id)");
2536  }
2537
2538  ObjCMethodDecl* ObjCMethod =
2539    ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
2540                           ResultTInfo,
2541                           cast<DeclContext>(ClassDecl),
2542                           MethodType == tok::minus, isVariadic,
2543                           false, false,
2544                           MethodDeclKind == tok::objc_optional
2545                             ? ObjCMethodDecl::Optional
2546                             : ObjCMethodDecl::Required,
2547                           false);
2548
2549  SmallVector<ParmVarDecl*, 16> Params;
2550
2551  for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
2552    QualType ArgType;
2553    TypeSourceInfo *DI;
2554
2555    if (ArgInfo[i].Type == 0) {
2556      ArgType = Context.getObjCIdType();
2557      DI = 0;
2558    } else {
2559      ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
2560      // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
2561      ArgType = Context.getAdjustedParameterType(ArgType);
2562    }
2563
2564    LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2565                   LookupOrdinaryName, ForRedeclaration);
2566    LookupName(R, S);
2567    if (R.isSingleResult()) {
2568      NamedDecl *PrevDecl = R.getFoundDecl();
2569      if (S->isDeclScope(PrevDecl)) {
2570        Diag(ArgInfo[i].NameLoc,
2571             (MethodDefinition ? diag::warn_method_param_redefinition
2572                               : diag::warn_method_param_declaration))
2573          << ArgInfo[i].Name;
2574        Diag(PrevDecl->getLocation(),
2575             diag::note_previous_declaration);
2576      }
2577    }
2578
2579    SourceLocation StartLoc = DI
2580      ? DI->getTypeLoc().getBeginLoc()
2581      : ArgInfo[i].NameLoc;
2582
2583    ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2584                                        ArgInfo[i].NameLoc, ArgInfo[i].Name,
2585                                        ArgType, DI, SC_None, SC_None);
2586
2587    Param->setObjCMethodScopeInfo(i);
2588
2589    Param->setObjCDeclQualifier(
2590      CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
2591
2592    // Apply the attributes to the parameter.
2593    ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
2594
2595    S->AddDecl(Param);
2596    IdResolver.AddDecl(Param);
2597
2598    Params.push_back(Param);
2599  }
2600
2601  for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
2602    ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
2603    QualType ArgType = Param->getType();
2604    if (ArgType.isNull())
2605      ArgType = Context.getObjCIdType();
2606    else
2607      // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
2608      ArgType = Context.getAdjustedParameterType(ArgType);
2609    if (ArgType->isObjCObjectType()) {
2610      Diag(Param->getLocation(),
2611           diag::err_object_cannot_be_passed_returned_by_value)
2612      << 1 << ArgType;
2613      Param->setInvalidDecl();
2614    }
2615    Param->setDeclContext(ObjCMethod);
2616
2617    Params.push_back(Param);
2618  }
2619
2620  ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
2621                              Sel.getNumArgs());
2622  ObjCMethod->setObjCDeclQualifier(
2623    CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
2624
2625  if (AttrList)
2626    ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
2627
2628  // Add the method now.
2629  const ObjCMethodDecl *PrevMethod = 0;
2630  if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
2631    if (MethodType == tok::minus) {
2632      PrevMethod = ImpDecl->getInstanceMethod(Sel);
2633      ImpDecl->addInstanceMethod(ObjCMethod);
2634    } else {
2635      PrevMethod = ImpDecl->getClassMethod(Sel);
2636      ImpDecl->addClassMethod(ObjCMethod);
2637    }
2638
2639    if (ObjCMethod->hasAttrs() &&
2640        containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
2641      Diag(EndLoc, diag::warn_attribute_method_def);
2642  } else {
2643    cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
2644  }
2645
2646  if (PrevMethod) {
2647    // You can never have two method definitions with the same name.
2648    Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
2649      << ObjCMethod->getDeclName();
2650    Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2651  }
2652
2653  // If this Objective-C method does not have a related result type, but we
2654  // are allowed to infer related result types, try to do so based on the
2655  // method family.
2656  ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2657  if (!CurrentClass) {
2658    if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2659      CurrentClass = Cat->getClassInterface();
2660    else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2661      CurrentClass = Impl->getClassInterface();
2662    else if (ObjCCategoryImplDecl *CatImpl
2663                                   = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2664      CurrentClass = CatImpl->getClassInterface();
2665  }
2666
2667  bool isRelatedResultTypeCompatible =
2668    (getLangOptions().ObjCInferRelatedResultType &&
2669     !CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass));
2670
2671  // Search for overridden methods and merge information down from them.
2672  OverrideSearch overrides(*this, ObjCMethod);
2673  for (OverrideSearch::iterator
2674         i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2675    ObjCMethodDecl *overridden = *i;
2676
2677    // Propagate down the 'related result type' bit from overridden methods.
2678    if (isRelatedResultTypeCompatible && overridden->hasRelatedResultType())
2679      ObjCMethod->SetRelatedResultType();
2680
2681    // Then merge the declarations.
2682    mergeObjCMethodDecls(ObjCMethod, overridden);
2683  }
2684
2685  bool ARCError = false;
2686  if (getLangOptions().ObjCAutoRefCount)
2687    ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2688
2689  if (!ARCError && isRelatedResultTypeCompatible &&
2690      !ObjCMethod->hasRelatedResultType()) {
2691    bool InferRelatedResultType = false;
2692    switch (ObjCMethod->getMethodFamily()) {
2693    case OMF_None:
2694    case OMF_copy:
2695    case OMF_dealloc:
2696    case OMF_mutableCopy:
2697    case OMF_release:
2698    case OMF_retainCount:
2699    case OMF_performSelector:
2700      break;
2701
2702    case OMF_alloc:
2703    case OMF_new:
2704      InferRelatedResultType = ObjCMethod->isClassMethod();
2705      break;
2706
2707    case OMF_init:
2708    case OMF_autorelease:
2709    case OMF_retain:
2710    case OMF_self:
2711      InferRelatedResultType = ObjCMethod->isInstanceMethod();
2712      break;
2713    }
2714
2715    if (InferRelatedResultType)
2716      ObjCMethod->SetRelatedResultType();
2717  }
2718
2719  return ObjCMethod;
2720}
2721
2722bool Sema::CheckObjCDeclScope(Decl *D) {
2723  if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
2724    return false;
2725
2726  Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2727  D->setInvalidDecl();
2728
2729  return true;
2730}
2731
2732/// Called whenever @defs(ClassName) is encountered in the source.  Inserts the
2733/// instance variables of ClassName into Decls.
2734void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
2735                     IdentifierInfo *ClassName,
2736                     SmallVectorImpl<Decl*> &Decls) {
2737  // Check that ClassName is a valid class
2738  ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
2739  if (!Class) {
2740    Diag(DeclStart, diag::err_undef_interface) << ClassName;
2741    return;
2742  }
2743  if (LangOpts.ObjCNonFragileABI) {
2744    Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2745    return;
2746  }
2747
2748  // Collect the instance variables
2749  SmallVector<const ObjCIvarDecl*, 32> Ivars;
2750  Context.DeepCollectObjCIvars(Class, true, Ivars);
2751  // For each ivar, create a fresh ObjCAtDefsFieldDecl.
2752  for (unsigned i = 0; i < Ivars.size(); i++) {
2753    const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
2754    RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
2755    Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2756                                           /*FIXME: StartL=*/ID->getLocation(),
2757                                           ID->getLocation(),
2758                                           ID->getIdentifier(), ID->getType(),
2759                                           ID->getBitWidth());
2760    Decls.push_back(FD);
2761  }
2762
2763  // Introduce all of these fields into the appropriate scope.
2764  for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
2765       D != Decls.end(); ++D) {
2766    FieldDecl *FD = cast<FieldDecl>(*D);
2767    if (getLangOptions().CPlusPlus)
2768      PushOnScopeChains(cast<FieldDecl>(FD), S);
2769    else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
2770      Record->addDecl(FD);
2771  }
2772}
2773
2774/// \brief Build a type-check a new Objective-C exception variable declaration.
2775VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2776                                      SourceLocation StartLoc,
2777                                      SourceLocation IdLoc,
2778                                      IdentifierInfo *Id,
2779                                      bool Invalid) {
2780  // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2781  // duration shall not be qualified by an address-space qualifier."
2782  // Since all parameters have automatic store duration, they can not have
2783  // an address space.
2784  if (T.getAddressSpace() != 0) {
2785    Diag(IdLoc, diag::err_arg_with_address_space);
2786    Invalid = true;
2787  }
2788
2789  // An @catch parameter must be an unqualified object pointer type;
2790  // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2791  if (Invalid) {
2792    // Don't do any further checking.
2793  } else if (T->isDependentType()) {
2794    // Okay: we don't know what this type will instantiate to.
2795  } else if (!T->isObjCObjectPointerType()) {
2796    Invalid = true;
2797    Diag(IdLoc ,diag::err_catch_param_not_objc_type);
2798  } else if (T->isObjCQualifiedIdType()) {
2799    Invalid = true;
2800    Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
2801  }
2802
2803  VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2804                                 T, TInfo, SC_None, SC_None);
2805  New->setExceptionVariable(true);
2806
2807  if (Invalid)
2808    New->setInvalidDecl();
2809  return New;
2810}
2811
2812Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
2813  const DeclSpec &DS = D.getDeclSpec();
2814
2815  // We allow the "register" storage class on exception variables because
2816  // GCC did, but we drop it completely. Any other storage class is an error.
2817  if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2818    Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2819      << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2820  } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2821    Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2822      << DS.getStorageClassSpec();
2823  }
2824  if (D.getDeclSpec().isThreadSpecified())
2825    Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2826  D.getMutableDeclSpec().ClearStorageClassSpecs();
2827
2828  DiagnoseFunctionSpecifiers(D);
2829
2830  // Check that there are no default arguments inside the type of this
2831  // exception object (C++ only).
2832  if (getLangOptions().CPlusPlus)
2833    CheckExtraCXXDefaultArguments(D);
2834
2835  TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
2836  QualType ExceptionType = TInfo->getType();
2837
2838  VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2839                                        D.getSourceRange().getBegin(),
2840                                        D.getIdentifierLoc(),
2841                                        D.getIdentifier(),
2842                                        D.isInvalidType());
2843
2844  // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2845  if (D.getCXXScopeSpec().isSet()) {
2846    Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2847      << D.getCXXScopeSpec().getRange();
2848    New->setInvalidDecl();
2849  }
2850
2851  // Add the parameter declaration into this scope.
2852  S->AddDecl(New);
2853  if (D.getIdentifier())
2854    IdResolver.AddDecl(New);
2855
2856  ProcessDeclAttributes(S, New, D);
2857
2858  if (New->hasAttr<BlocksAttr>())
2859    Diag(New->getLocation(), diag::err_block_on_nonlocal);
2860  return New;
2861}
2862
2863/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
2864/// initialization.
2865void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
2866                                SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
2867  for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2868       Iv= Iv->getNextIvar()) {
2869    QualType QT = Context.getBaseElementType(Iv->getType());
2870    if (QT->isRecordType())
2871      Ivars.push_back(Iv);
2872  }
2873}
2874
2875void Sema::DiagnoseUseOfUnimplementedSelectors() {
2876  // Load referenced selectors from the external source.
2877  if (ExternalSource) {
2878    SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
2879    ExternalSource->ReadReferencedSelectors(Sels);
2880    for (unsigned I = 0, N = Sels.size(); I != N; ++I)
2881      ReferencedSelectors[Sels[I].first] = Sels[I].second;
2882  }
2883
2884  // Warning will be issued only when selector table is
2885  // generated (which means there is at lease one implementation
2886  // in the TU). This is to match gcc's behavior.
2887  if (ReferencedSelectors.empty() ||
2888      !Context.AnyObjCImplementation())
2889    return;
2890  for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2891        ReferencedSelectors.begin(),
2892       E = ReferencedSelectors.end(); S != E; ++S) {
2893    Selector Sel = (*S).first;
2894    if (!LookupImplementedMethodInGlobalPool(Sel))
2895      Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2896  }
2897  return;
2898}
2899