ASTImporter.cpp revision 2b785022973202ea6bafe304a50eb3cac1a0aeb8
11b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===//
21b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//
31b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//                     The LLVM Compiler Infrastructure
41b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//
51b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor// This file is distributed under the University of Illinois Open Source
61b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor// License. See LICENSE.TXT for details.
71b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//
81b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//===----------------------------------------------------------------------===//
91b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//
101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//  This file defines the ASTImporter class which imports AST nodes from one
111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//  context into another context.
121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//
131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//===----------------------------------------------------------------------===//
141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor#include "clang/AST/ASTImporter.h"
151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor#include "clang/AST/ASTContext.h"
17885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor#include "clang/AST/ASTDiagnostic.h"
1896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor#include "clang/AST/DeclCXX.h"
191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor#include "clang/AST/DeclObjC.h"
20089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor#include "clang/AST/DeclVisitor.h"
214800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor#include "clang/AST/StmtVisitor.h"
2282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor#include "clang/AST/TypeLoc.h"
231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor#include "clang/AST/TypeVisitor.h"
24885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor#include "clang/Basic/FileManager.h"
25885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor#include "clang/Basic/SourceManager.h"
26885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor#include "llvm/Support/MemoryBuffer.h"
2773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor#include <deque>
281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregorusing namespace clang;
301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregornamespace {
32089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
334800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                          public DeclVisitor<ASTNodeImporter, Decl *>,
344800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                          public StmtVisitor<ASTNodeImporter, Stmt *> {
351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ASTImporter &Importer;
361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  public:
381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    using TypeVisitor<ASTNodeImporter, QualType>::Visit;
419bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
424800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // Importing types
4589cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    QualType VisitType(Type *T);
461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitBuiltinType(BuiltinType *T);
471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitComplexType(ComplexType *T);
481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitPointerType(PointerType *T);
491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitBlockPointerType(BlockPointerType *T);
501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitLValueReferenceType(LValueReferenceType *T);
511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitRValueReferenceType(RValueReferenceType *T);
521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitMemberPointerType(MemberPointerType *T);
531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitConstantArrayType(ConstantArrayType *T);
541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitIncompleteArrayType(IncompleteArrayType *T);
551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitVariableArrayType(VariableArrayType *T);
561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentSizedArrayType
571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentSizedExtVectorType
581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitVectorType(VectorType *T);
591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitExtVectorType(ExtVectorType *T);
601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitFunctionNoProtoType(FunctionNoProtoType *T);
611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitFunctionProtoType(FunctionProtoType *T);
621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: UnresolvedUsingType
631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitTypedefType(TypedefType *T);
641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitTypeOfExprType(TypeOfExprType *T);
651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentTypeOfExprType
661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitTypeOfType(TypeOfType *T);
671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitDecltypeType(DecltypeType *T);
681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentDecltypeType
691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitRecordType(RecordType *T);
701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitEnumType(EnumType *T);
711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitElaboratedType(ElaboratedType *T);
721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: TemplateTypeParmType
731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: SubstTemplateTypeParmType
741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: TemplateSpecializationType
751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitQualifiedNameType(QualifiedNameType *T);
761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: TypenameType
771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitObjCInterfaceType(ObjCInterfaceType *T);
781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T);
79089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
80089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    // Importing declarations
81a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
82a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                         DeclContext *&LexicalDC, DeclarationName &Name,
83a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                         SourceLocation &Loc);
8496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
8573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
8689cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    Decl *VisitDecl(Decl *D);
879e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
8836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
8996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *VisitRecordDecl(RecordDecl *D);
9036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
91a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D);
9296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
932e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
94089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
952cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
96a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Decl *VisitParmVarDecl(ParmVarDecl *D);
97c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
98b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
992e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
100a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
101e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
1022b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
103a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Decl *VisitObjCClassDecl(ObjCClassDecl *D);
104a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
1054800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    // Importing statements
1064800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Stmt *VisitStmt(Stmt *S);
1074800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
1084800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    // Importing expressions
1094800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Expr *VisitExpr(Expr *E);
1104800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Expr *VisitIntegerLiteral(IntegerLiteral *E);
11136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
1121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  };
1131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
1141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//----------------------------------------------------------------------------
11673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor// Structural Equivalence
11773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor//----------------------------------------------------------------------------
11873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
11973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregornamespace {
12073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  struct StructuralEquivalenceContext {
12173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief AST contexts for which we are checking structural equivalence.
12273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    ASTContext &C1, &C2;
12373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
12473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Diagnostic object used to emit diagnostics.
12573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Diagnostic &Diags;
12673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
12773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief The set of "tentative" equivalences between two canonical
12873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// declarations, mapping from a declaration in the first context to the
12973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// declaration in the second context that we believe to be equivalent.
13073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
13173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
13273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Queue of declarations in the first context whose equivalence
13373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// with a declaration in the second context still needs to be verified.
13473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    std::deque<Decl *> DeclsToCheck;
13573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
136ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    /// \brief Declaration (from, to) pairs that are known not to be equivalent
137ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    /// (which we have already complained about).
138ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
139ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
14073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Whether we're being strict about the spelling of types when
14173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// unifying two types.
14273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool StrictTypeSpelling;
14373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
14473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
14573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                 Diagnostic &Diags,
146ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor               llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
14773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                 bool StrictTypeSpelling = false)
148ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      : C1(C1), C2(C2), Diags(Diags), NonEquivalentDecls(NonEquivalentDecls),
149ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        StrictTypeSpelling(StrictTypeSpelling) { }
15073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
15173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Determine whether the two declarations are structurally
15273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// equivalent.
15373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
15473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
15573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Determine whether the two types are structurally equivalent.
15673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructurallyEquivalent(QualType T1, QualType T2);
15773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
15873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  private:
15973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Finish checking all of the structural equivalences.
16073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    ///
16173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \returns true if an error occurred, false otherwise.
16273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool Finish();
16373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
16473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  public:
16573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
16673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return Diags.Report(FullSourceLoc(Loc, C1.getSourceManager()), DiagID);
16773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
16873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
16973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
17073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return Diags.Report(FullSourceLoc(Loc, C2.getSourceManager()), DiagID);
17173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
17273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  };
17373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
17473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
17573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
17673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     QualType T1, QualType T2);
17773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
17873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Decl *D1, Decl *D2);
17973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
18073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine if two APInts have the same value, after zero-extending
18173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// one of them (if needed!) to ensure that the bit-widths match.
18273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
18373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() == I2.getBitWidth())
18473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == I2;
18573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
18673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() > I2.getBitWidth())
18773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == llvm::APInt(I2).zext(I1.getBitWidth());
18873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
18973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return llvm::APInt(I1).zext(I2.getBitWidth()) == I2;
19073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
19173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
19273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine if two APSInts have the same value, zero- or sign-extending
19373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// as needed.
19473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
19573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
19673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == I2;
19773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
19873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Check for a bit-width mismatch.
19973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() > I2.getBitWidth())
20073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return IsSameValue(I1, llvm::APSInt(I2).extend(I1.getBitWidth()));
20173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  else if (I2.getBitWidth() > I1.getBitWidth())
20273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return IsSameValue(llvm::APSInt(I1).extend(I2.getBitWidth()), I2);
20373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
20473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // We have a signedness mismatch. Turn the signed value into an unsigned
20573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // value.
20673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.isSigned()) {
20773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (I1.isNegative())
20873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
20973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
21073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return llvm::APSInt(I1, true) == I2;
21173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
21273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
21373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I2.isNegative())
21473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
21573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
21673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return I1 == llvm::APSInt(I2, true);
21773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
21873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
21973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two expressions.
22073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
22173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Expr *E1, Expr *E2) {
22273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!E1 || !E2)
22373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return E1 == E2;
22473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
22573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Actually perform a structural comparison!
22673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
22773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
22873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
22973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two identifiers are equivalent.
23073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
23173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const IdentifierInfo *Name2) {
23273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!Name1 || !Name2)
23373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return Name1 == Name2;
23473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
23573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return Name1->getName() == Name2->getName();
23673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
23773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
23873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two nested-name-specifiers are equivalent.
23973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
24073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     NestedNameSpecifier *NNS1,
24173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     NestedNameSpecifier *NNS2) {
24273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Implement!
24373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
24473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
24573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two template arguments are equivalent.
24773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
24873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const TemplateArgument &Arg1,
24973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const TemplateArgument &Arg2) {
25073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Implement!
25173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
25273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
25373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
25473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence for the common part of array
25573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// types.
25673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
25773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                          const ArrayType *Array1,
25873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                          const ArrayType *Array2) {
25973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!IsStructurallyEquivalent(Context,
26073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                Array1->getElementType(),
26173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                Array2->getElementType()))
26273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
26373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Array1->getSizeModifier() != Array2->getSizeModifier())
26473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
26573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
26673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
26773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
26873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
26973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
27073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
27173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two types.
27273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
27373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     QualType T1, QualType T2) {
27473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (T1.isNull() || T2.isNull())
27573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return T1.isNull() && T2.isNull();
27673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
27773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!Context.StrictTypeSpelling) {
27873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // We aren't being strict about token-to-token equivalence of types,
27973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // so map down to the canonical type.
28073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    T1 = Context.C1.getCanonicalType(T1);
28173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    T2 = Context.C2.getCanonicalType(T2);
28273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
28373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
28473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (T1.getQualifiers() != T2.getQualifiers())
28573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
28673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
287ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  Type::TypeClass TC = T1->getTypeClass();
288ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
289ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T1->getTypeClass() != T2->getTypeClass()) {
290ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // Compare function types with prototypes vs. without prototypes as if
291ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // both did not have prototypes.
292ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (T1->getTypeClass() == Type::FunctionProto &&
293ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        T2->getTypeClass() == Type::FunctionNoProto)
294ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      TC = Type::FunctionNoProto;
295ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    else if (T1->getTypeClass() == Type::FunctionNoProto &&
296ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor             T2->getTypeClass() == Type::FunctionProto)
297ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      TC = Type::FunctionNoProto;
298ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    else
299ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      return false;
300ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  }
30173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
302ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  switch (TC) {
303ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  case Type::Builtin:
30473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Deal with Char_S/Char_U.
30573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
30673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
30773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
30873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
30973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Complex:
31073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
31173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<ComplexType>(T1)->getElementType(),
31273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<ComplexType>(T2)->getElementType()))
31373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
31473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
31573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
31673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Pointer:
31773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
31873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<PointerType>(T1)->getPointeeType(),
31973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<PointerType>(T2)->getPointeeType()))
32073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
32173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
32273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
32373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::BlockPointer:
32473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
32573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<BlockPointerType>(T1)->getPointeeType(),
32673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<BlockPointerType>(T2)->getPointeeType()))
32773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
32873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
32973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
33073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::LValueReference:
33173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::RValueReference: {
33273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ReferenceType *Ref1 = cast<ReferenceType>(T1);
33373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ReferenceType *Ref2 = cast<ReferenceType>(T2);
33473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
33573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
33673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Ref1->isInnerRef() != Ref2->isInnerRef())
33773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
33873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
33973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ref1->getPointeeTypeAsWritten(),
34073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ref2->getPointeeTypeAsWritten()))
34173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
34273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
34373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
34473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
34573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::MemberPointer: {
34673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
34773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
34873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
34973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  MemPtr1->getPointeeType(),
35073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  MemPtr2->getPointeeType()))
35173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
35273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
35373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(MemPtr1->getClass(), 0),
35473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(MemPtr2->getClass(), 0)))
35573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
35673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
35773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
35873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
35973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ConstantArray: {
36073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
36173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
36273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsSameValue(Array1->getSize(), Array2->getSize()))
36373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
36473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
36573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
36673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
36773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
36873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
36973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
37073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::IncompleteArray:
37173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context,
37273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                       cast<ArrayType>(T1),
37373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                       cast<ArrayType>(T2)))
37473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
37573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
37673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
37773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::VariableArray: {
37873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
37973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
38073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
38173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Array1->getSizeExpr(), Array2->getSizeExpr()))
38273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
38373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
38473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
38573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
38673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
38773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
38873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
38973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
39073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::DependentSizedArray: {
39173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
39273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
39373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
39473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Array1->getSizeExpr(), Array2->getSizeExpr()))
39573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
39673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
39773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
39873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
39973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
40073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
40173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
40273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
40373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::DependentSizedExtVector: {
40473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedExtVectorType *Vec1
40573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<DependentSizedExtVectorType>(T1);
40673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedExtVectorType *Vec2
40773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<DependentSizedExtVectorType>(T2);
40873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
40973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getSizeExpr(), Vec2->getSizeExpr()))
41073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
41173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
41273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getElementType(),
41373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec2->getElementType()))
41473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
41573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
41673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
41773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
41873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Vector:
41973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ExtVector: {
42073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VectorType *Vec1 = cast<VectorType>(T1);
42173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VectorType *Vec2 = cast<VectorType>(T2);
42273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
42373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getElementType(),
42473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec2->getElementType()))
42573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
42673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Vec1->getNumElements() != Vec2->getNumElements())
42773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
42873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Vec1->isAltiVec() != Vec2->isAltiVec())
42973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
43073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Vec1->isPixel() != Vec2->isPixel())
43173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
43273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
43373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
43473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::FunctionProto: {
43573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
43673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
43773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getNumArgs() != Proto2->getNumArgs())
43873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
43973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
44073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
44173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto1->getArgType(I),
44273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto2->getArgType(I)))
44373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
44473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
44573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->isVariadic() != Proto2->isVariadic())
44673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
44773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
44873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
44973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
45073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
45173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
45273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
45373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
45473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
45573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto1->getExceptionType(I),
45673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto2->getExceptionType(I)))
45773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
45873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
45973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
46073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
46273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Fall through to check the bits common with FunctionNoProtoType.
46373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
46473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
46573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::FunctionNoProto: {
46673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionType *Function1 = cast<FunctionType>(T1);
46773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionType *Function2 = cast<FunctionType>(T2);
46873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
46973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Function1->getResultType(),
47073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Function2->getResultType()))
47173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
47273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Function1->getNoReturnAttr() != Function2->getNoReturnAttr())
47373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
47473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Function1->getCallConv() != Function2->getCallConv())
47573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
47673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
47773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
47873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
47973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::UnresolvedUsing:
48073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
48173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<UnresolvedUsingType>(T1)->getDecl(),
48273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<UnresolvedUsingType>(T2)->getDecl()))
48373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
48473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
48573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
48673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
48773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Typedef:
48873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
48973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypedefType>(T1)->getDecl(),
49073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypedefType>(T2)->getDecl()))
49173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
49273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
49373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
49473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TypeOfExpr:
49573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
49673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
49773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
49873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
49973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
50073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
50173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TypeOf:
50273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
50373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypeOfType>(T1)->getUnderlyingType(),
50473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypeOfType>(T2)->getUnderlyingType()))
50573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
50673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
50773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
50873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Decltype:
50973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
51073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<DecltypeType>(T1)->getUnderlyingExpr(),
51173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<DecltypeType>(T2)->getUnderlyingExpr()))
51273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
51373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
51473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
51573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Record:
51673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Enum:
51773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
51873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TagType>(T1)->getDecl(),
51973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TagType>(T2)->getDecl()))
52073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
52173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
52273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
52373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Elaborated: {
52473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
52573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
52673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Elab1->getTagKind() != Elab2->getTagKind())
52773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
52873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
52973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Elab1->getUnderlyingType(),
53073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Elab2->getUnderlyingType()))
53173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
53273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
53373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
53473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
53573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TemplateTypeParm: {
53673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
53773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
53873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->getDepth() != Parm2->getDepth())
53973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
54073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->getIndex() != Parm2->getIndex())
54173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
54273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->isParameterPack() != Parm2->isParameterPack())
54373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
54473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
54573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Names of template type parameters are never significant.
54673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
54773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
54873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
54973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::SubstTemplateTypeParm: {
55073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const SubstTemplateTypeParmType *Subst1
55173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<SubstTemplateTypeParmType>(T1);
55273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const SubstTemplateTypeParmType *Subst2
55373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<SubstTemplateTypeParmType>(T2);
55473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
55573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Subst1->getReplacedParameter(), 0),
55673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Subst2->getReplacedParameter(), 0)))
55773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
55873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
55973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Subst1->getReplacementType(),
56073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Subst2->getReplacementType()))
56173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
56273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
56373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
56473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
56573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TemplateSpecialization: {
56673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateSpecializationType *Spec1
56773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<TemplateSpecializationType>(T1);
56873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateSpecializationType *Spec2
56973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<TemplateSpecializationType>(T2);
57073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
57173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Spec1->getTemplateName(),
57273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Spec2->getTemplateName()))
57373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
57473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Spec1->getNumArgs() != Spec2->getNumArgs())
57573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
57673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
57773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
57873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Spec1->getArg(I), Spec2->getArg(I)))
57973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
58073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
58173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
58273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
58373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
58473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::QualifiedName: {
58573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const QualifiedNameType *Qual1 = cast<QualifiedNameType>(T1);
58673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const QualifiedNameType *Qual2 = cast<QualifiedNameType>(T2);
58773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
58873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Qual1->getQualifier(),
58973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Qual2->getQualifier()))
59073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
59173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
59273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Qual1->getNamedType(),
59373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Qual2->getNamedType()))
59473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
59573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
59673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
59773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
59873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Typename: {
59973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TypenameType *Typename1 = cast<TypenameType>(T1);
60073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TypenameType *Typename2 = cast<TypenameType>(T2);
60173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
60273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename1->getQualifier(),
60373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename2->getQualifier()))
60473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
60573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
60673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename2->getIdentifier()))
60773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
60873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
60973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Typename1->getTemplateId(), 0),
61073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Typename2->getTemplateId(), 0)))
61173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
61273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
61373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
61473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
61573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
61673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ObjCInterface: {
61773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
61873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
61973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
62073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Iface1->getDecl(), Iface2->getDecl()))
62173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
62273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Iface1->getNumProtocols() != Iface2->getNumProtocols())
62373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
62473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Iface1->getNumProtocols(); I != N; ++I) {
62573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
62673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Iface1->getProtocol(I),
62773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Iface2->getProtocol(I)))
62873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
62973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
63073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
63173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
63273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
63373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ObjCObjectPointer: {
63473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
63573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
63673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
63773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ptr1->getPointeeType(),
63873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ptr2->getPointeeType()))
63973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
64073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Ptr1->getNumProtocols() != Ptr2->getNumProtocols())
64173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
64273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Ptr1->getNumProtocols(); I != N; ++I) {
64373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
64473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Ptr1->getProtocol(I),
64573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Ptr2->getProtocol(I)))
64673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
64773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
64873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
64973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
65073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
65173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  } // end switch
65273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
65373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
65473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
65573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
65673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two records.
65773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
65873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     RecordDecl *D1, RecordDecl *D2) {
65973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (D1->isUnion() != D2->isUnion()) {
66073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
66173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
66273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
66373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << D1->getDeclName() << (unsigned)D1->getTagKind();
66473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
66573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
66673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
667ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Compare the definitions of these two records. If either or both are
668ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // incomplete, we assume that they are equivalent.
669ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  D1 = D1->getDefinition();
670ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  D2 = D2->getDefinition();
671ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (!D1 || !D2)
672ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return true;
673ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
67473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
67573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
67673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
67773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
67873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
67973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
68073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << D2CXX->getNumBases();
68173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
68273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << D1CXX->getNumBases();
68373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
68473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
68573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
68673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      // Check the base classes.
68773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
68873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                           BaseEnd1 = D1CXX->bases_end(),
68973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                Base2 = D2CXX->bases_begin();
69073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor           Base1 != BaseEnd1;
69173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor           ++Base1, ++Base2) {
69273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!IsStructurallyEquivalent(Context,
69373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                      Base1->getType(), Base2->getType())) {
69473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
69573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Context.C2.getTypeDeclType(D2);
69673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
69773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->getType()
69873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->getSourceRange();
69973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
70073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getType()
70173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getSourceRange();
70273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          return false;
70373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        }
70473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
70573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check virtual vs. non-virtual inheritance mismatch.
70673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (Base1->isVirtual() != Base2->isVirtual()) {
70773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
70873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Context.C2.getTypeDeclType(D2);
70973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(Base2->getSourceRange().getBegin(),
71073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                        diag::note_odr_virtual_base)
71173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->isVirtual() << Base2->getSourceRange();
71273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
71373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->isVirtual()
71473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getSourceRange();
71573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          return false;
71673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        }
71773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
71873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    } else if (D1CXX->getNumBases() > 0) {
71973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
72073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
72173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
72273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
72373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Base1->getType()
72473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Base1->getSourceRange();
72573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
72673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
72773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
72873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
72973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
73073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Check the fields for consistency.
73173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
73273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                             Field2End = D2->field_end();
73373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
73473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Field1End = D1->field_end();
73573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       Field1 != Field1End;
73673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       ++Field1, ++Field2) {
73773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field2 == Field2End) {
73873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
73973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
74073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Field1->getLocation(), diag::note_odr_field)
74173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName() << Field1->getType();
74273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
74373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
74473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
74573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
74673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
74773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Field1->getType(), Field2->getType())) {
74873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
74973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
75073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(Field2->getLocation(), diag::note_odr_field)
75173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field2->getDeclName() << Field2->getType();
75273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Field1->getLocation(), diag::note_odr_field)
75373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName() << Field1->getType();
75473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
75573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
75673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
75773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field1->isBitField() != Field2->isBitField()) {
75873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
75973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
76073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (Field1->isBitField()) {
76173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        llvm::APSInt Bits;
76273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
76373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
76473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field1->getDeclName() << Field1->getType()
76573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits.toString(10, false);
76673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
76773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName();
76873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
76973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        llvm::APSInt Bits;
77073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
77173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
77273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName() << Field2->getType()
77373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits.toString(10, false);
77473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(),
77573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                          diag::note_odr_not_bit_field)
77673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName();
77773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
77873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
77973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
78073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
78173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field1->isBitField()) {
78273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      // Make sure that the bit-fields are the same length.
78373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      llvm::APSInt Bits1, Bits2;
78473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
78573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
78673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
78773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
78873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
78973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsSameValue(Bits1, Bits2)) {
79073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
79173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Context.C2.getTypeDeclType(D2);
79273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
79373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName() << Field2->getType()
79473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits2.toString(10, false);
79573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
79673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field1->getDeclName() << Field1->getType()
79773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits1.toString(10, false);
79873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
79973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
80073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
80173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
80273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
80373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Field2 != Field2End) {
80473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
80573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
80673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(Field2->getLocation(), diag::note_odr_field)
80773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Field2->getDeclName() << Field2->getType();
80873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
80973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
81073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
81173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
81273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
81373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
81473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
81573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two enums.
81673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
81773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     EnumDecl *D1, EnumDecl *D2) {
81873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
81973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                             EC2End = D2->enumerator_end();
82073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
82173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  EC1End = D1->enumerator_end();
82273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       EC1 != EC1End; ++EC1, ++EC2) {
82373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (EC2 == EC2End) {
82473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
82573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
82673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
82773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getDeclName()
82873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getInitVal().toString(10);
82973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
83073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
83173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
83273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
83373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::APSInt Val1 = EC1->getInitVal();
83473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::APSInt Val2 = EC2->getInitVal();
83573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsSameValue(Val1, Val2) ||
83673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
83773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
83873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
83973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
84073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC2->getDeclName()
84173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC2->getInitVal().toString(10);
84273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
84373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getDeclName()
84473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getInitVal().toString(10);
84573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
84673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
84773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
84873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
84973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (EC2 != EC2End) {
85073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
85173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
85273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
85373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << EC2->getDeclName()
85473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << EC2->getInitVal().toString(10);
85573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
85673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
85773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
85873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
85973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
86073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
86173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
86273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two declarations.
86373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
86473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Decl *D1, Decl *D2) {
86573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Check for known structural equivalences via a callback of some sort.
86673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
867ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Check whether we already know that these two declarations are not
868ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // structurally equivalent.
869ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
870ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                      D2->getCanonicalDecl())))
871ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return false;
872ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
87373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Determine whether we've already produced a tentative equivalence for D1.
87473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
87573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (EquivToD1)
87673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return EquivToD1 == D2->getCanonicalDecl();
87773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
87873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Produce a tentative equivalence D1 <-> D2, which will be checked later.
87973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EquivToD1 = D2->getCanonicalDecl();
88073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
88173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
88273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
88373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
88473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
88573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                            Decl *D2) {
88673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!::IsStructurallyEquivalent(*this, D1, D2))
88773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
88873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
88973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return !Finish();
89073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
89173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
89273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
89373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                            QualType T2) {
89473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!::IsStructurallyEquivalent(*this, T1, T2))
89573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
89673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
89773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return !Finish();
89873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
89973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
90073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::Finish() {
90173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  while (!DeclsToCheck.empty()) {
90273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Check the next declaration.
90373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Decl *D1 = DeclsToCheck.front();
90473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DeclsToCheck.pop_front();
90573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
90673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Decl *D2 = TentativeEquivalences[D1];
90773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    assert(D2 && "Unrecorded tentative equivalence?");
90873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
909ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    bool Equivalent = true;
910ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
91173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Switch on all declaration kinds. For now, we're just going to
91273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // check the obvious ones.
91373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
91473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
91573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check for equivalent structure names.
91673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name1 = Record1->getIdentifier();
91773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name1 && Record1->getTypedefForAnonDecl())
91873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
91973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name2 = Record2->getIdentifier();
92073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name2 && Record2->getTypedefForAnonDecl())
92173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
922ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (!::IsStructurallyEquivalent(Name1, Name2) ||
923ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this, Record1, Record2))
924ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
92573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
92673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Record/non-record mismatch.
927ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
92873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
929ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
93073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
93173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check for equivalent enum names.
93273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name1 = Enum1->getIdentifier();
93373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name1 && Enum1->getTypedefForAnonDecl())
93473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
93573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name2 = Enum2->getIdentifier();
93673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name2 && Enum2->getTypedefForAnonDecl())
93773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
938ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (!::IsStructurallyEquivalent(Name1, Name2) ||
939ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this, Enum1, Enum2))
940ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
94173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
94273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Enum/non-enum mismatch
943ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
94473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
945ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
94673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
94773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
948ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                        Typedef2->getIdentifier()) ||
949ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this,
95073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        Typedef1->getUnderlyingType(),
95173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        Typedef2->getUnderlyingType()))
952ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
95373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
95473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Typedef/non-typedef mismatch.
955ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
95673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
95773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
958ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
959ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (!Equivalent) {
960ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      // Note that these two declarations are not equivalent (and we already
961ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      // know about it).
962ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
963ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                               D2->getCanonicalDecl()));
964ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      return true;
965ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    }
96673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Check other declaration kinds!
96773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
96873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
96973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return false;
97073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
97173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
97273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor//----------------------------------------------------------------------------
9731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor// Import Types
9741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//----------------------------------------------------------------------------
9751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
97689cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas GregorQualType ASTNodeImporter::VisitType(Type *T) {
97789cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor  Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
97889cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    << T->getTypeClassName();
97989cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor  return QualType();
98089cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor}
98189cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor
9821b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
9831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  switch (T->getKind()) {
9841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Void: return Importer.getToContext().VoidTy;
9851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Bool: return Importer.getToContext().BoolTy;
9861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
9871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char_U:
9881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // The context we're importing from has an unsigned 'char'. If we're
9891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // importing into a context with a signed 'char', translate to
9901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // 'unsigned char' instead.
9911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (Importer.getToContext().getLangOptions().CharIsSigned)
9921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return Importer.getToContext().UnsignedCharTy;
9931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
9941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().CharTy;
9951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
9961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
9971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
9981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char16:
9991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++!
10001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().Char16Ty;
10011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char32:
10031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++!
10041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().Char32Ty;
10051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
10071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
10081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
10091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ULongLong:
10101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().UnsignedLongLongTy;
10111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
10121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char_S:
10141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // The context we're importing from has an unsigned 'char'. If we're
10151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // importing into a context with a signed 'char', translate to
10161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // 'unsigned char' instead.
10171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Importer.getToContext().getLangOptions().CharIsSigned)
10181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return Importer.getToContext().SignedCharTy;
10191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().CharTy;
10211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
10231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::WChar:
10241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: If not in C++, shall we translate to the C equivalent of
10251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // wchar_t?
10261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().WCharTy;
10271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Short : return Importer.getToContext().ShortTy;
10291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Int : return Importer.getToContext().IntTy;
10301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Long : return Importer.getToContext().LongTy;
10311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
10321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
10331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Float: return Importer.getToContext().FloatTy;
10341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Double: return Importer.getToContext().DoubleTy;
10351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
10361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::NullPtr:
10381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++0x!
10391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().NullPtrTy;
10401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
10421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
10431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UndeducedAuto:
10441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++0x!
10451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().UndeducedAutoTy;
10461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCId:
10481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports Objective-C!
10491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinIdTy;
10501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCClass:
10521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinClassTy;
10531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCSel:
10551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinSelTy;
10561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
10571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return QualType();
10591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
10601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10611b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
10621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
10631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
10641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
10651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getComplexType(ToElementType);
10671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
10681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10691b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitPointerType(PointerType *T) {
10701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
10711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
10721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
10731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getPointerType(ToPointeeType);
10751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
10761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10771b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
10781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for blocks support in "to" context.
10791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
10801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
10811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
10821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getBlockPointerType(ToPointeeType);
10841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
10851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10861b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
10871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++ support in "to" context.
10881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
10891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
10901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
10911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getLValueReferenceType(ToPointeeType);
10931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
10941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10951b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
10961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++0x support in "to" context.
10971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
10981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
10991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getRValueReferenceType(ToPointeeType);
11021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11041b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
11051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++ support in "to" context.
11061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
11071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
11081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
11111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getMemberPointerType(ToPointeeType,
11121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      ClassType.getTypePtr());
11131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11151b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
11161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getConstantArrayType(ToElementType,
11211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSize(),
11221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSizeModifier(),
11231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->getIndexTypeCVRQualifiers());
11241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11261b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
11271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getIncompleteArrayType(ToElementType,
11321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                        T->getSizeModifier(),
11331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                T->getIndexTypeCVRQualifiers());
11341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11361b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
11371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *Size = Importer.Import(T->getSizeExpr());
11421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!Size)
11431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  SourceRange Brackets = Importer.Import(T->getBracketsRange());
11461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getVariableArrayType(ToElementType, Size,
11471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSizeModifier(),
11481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                T->getIndexTypeCVRQualifiers(),
11491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      Brackets);
11501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11521b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitVectorType(VectorType *T) {
11531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getVectorType(ToElementType,
11581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->getNumElements(),
11591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->isAltiVec(),
11601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->isPixel());
11611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11631b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
11641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getExtVectorType(ToElementType,
11691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                  T->getNumElements());
11701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11721b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
11731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: What happens if we're importing a function without a prototype
11741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // into C++? Should we make it variadic?
11751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToResultType = Importer.Import(T->getResultType());
11761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToResultType.isNull())
11771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getFunctionNoProtoType(ToResultType,
11801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                        T->getNoReturnAttr(),
11811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                        T->getCallConv());
11821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11841b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
11851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToResultType = Importer.Import(T->getResultType());
11861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToResultType.isNull())
11871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Import argument types
11901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<QualType, 4> ArgTypes;
11911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
11921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                         AEnd = T->arg_type_end();
11931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       A != AEnd; ++A) {
11941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType ArgType = Importer.Import(*A);
11951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (ArgType.isNull())
11961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
11971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ArgTypes.push_back(ArgType);
11981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
11991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Import exception types
12011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<QualType, 4> ExceptionTypes;
12021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (FunctionProtoType::exception_iterator E = T->exception_begin(),
12031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                          EEnd = T->exception_end();
12041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       E != EEnd; ++E) {
12051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType ExceptionType = Importer.Import(*E);
12061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (ExceptionType.isNull())
12071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
12081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ExceptionTypes.push_back(ExceptionType);
12091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
12101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
12121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 ArgTypes.size(),
12131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->isVariadic(),
12141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->getTypeQuals(),
12151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->hasExceptionSpec(),
12161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->hasAnyExceptionSpec(),
12171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 ExceptionTypes.size(),
12181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 ExceptionTypes.data(),
12191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->getNoReturnAttr(),
12201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->getCallConv());
12211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12231b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
12241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  TypedefDecl *ToDecl
12251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
12261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
12271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeDeclType(ToDecl);
12301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12321b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
12331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
12341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToExpr)
12351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeOfExprType(ToExpr);
12381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12401b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
12411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
12421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToUnderlyingType.isNull())
12431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeOfType(ToUnderlyingType);
12461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12481b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
12491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
12501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToExpr)
12511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getDecltypeType(ToExpr);
12541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12561b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitRecordType(RecordType *T) {
12571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  RecordDecl *ToDecl
12581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
12591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
12601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTagDeclType(ToDecl);
12631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12651b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitEnumType(EnumType *T) {
12661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  EnumDecl *ToDecl
12671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
12681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
12691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTagDeclType(ToDecl);
12721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12741b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
12751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
12761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToUnderlyingType.isNull())
12771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getElaboratedType(ToUnderlyingType,
12801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                   T->getTagKind());
12811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12831b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitQualifiedNameType(QualifiedNameType *T) {
12841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  NestedNameSpecifier *ToQualifier = Importer.Import(T->getQualifier());
12851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToQualifier)
12861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToNamedType = Importer.Import(T->getNamedType());
12891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToNamedType.isNull())
12901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getQualifiedNameType(ToQualifier, ToNamedType);
12931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12951b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
12961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  ObjCInterfaceDecl *Class
12971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
12981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!Class)
12991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
13021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (ObjCInterfaceType::qual_iterator P = T->qual_begin(),
13031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                     PEnd = T->qual_end();
13041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       P != PEnd; ++P) {
13051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ObjCProtocolDecl *Protocol
13061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
13071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Protocol)
13081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
13091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    Protocols.push_back(Protocol);
13101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
13111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getObjCInterfaceType(Class,
13131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      Protocols.data(),
13141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      Protocols.size());
13151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13171b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
13181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
13191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
13201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
13231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (ObjCObjectPointerType::qual_iterator P = T->qual_begin(),
13241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                         PEnd = T->qual_end();
13251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       P != PEnd; ++P) {
13261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ObjCProtocolDecl *Protocol
13271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
13281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Protocol)
13291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
13301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    Protocols.push_back(Protocol);
13311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
13321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getObjCObjectPointerType(ToPointeeType,
13341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                          Protocols.data(),
13351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                          Protocols.size());
13361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1338089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor//----------------------------------------------------------------------------
1339089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor// Import Declarations
1340089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor//----------------------------------------------------------------------------
1341a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregorbool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1342a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      DeclContext *&LexicalDC,
1343a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      DeclarationName &Name,
1344a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      SourceLocation &Loc) {
1345089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Import the context of this declaration.
1346a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DC = Importer.ImportContext(D->getDeclContext());
1347089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  if (!DC)
1348a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return true;
1349a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1350a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  LexicalDC = DC;
13519bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
13529bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
13539bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    if (!LexicalDC)
1354a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      return true;
13559bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  }
1356a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1357089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Import the name of this declaration.
1358a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Name = Importer.Import(D->getDeclName());
1359089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  if (D->getDeclName() && !Name)
1360a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return true;
1361a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1362a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the location of this declaration.
1363a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Loc = Importer.Import(D->getLocation());
1364a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  return false;
1365a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1366a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
136796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregorbool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
136873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        RecordDecl *ToRecord) {
136973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  StructuralEquivalenceContext SEC(Importer.getFromContext(),
137073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Importer.getToContext(),
1371ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getDiags(),
1372ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getNonEquivalentDecls());
137373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return SEC.IsStructurallyEquivalent(FromRecord, ToRecord);
137496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
137596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
137636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregorbool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
137773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  StructuralEquivalenceContext SEC(Importer.getFromContext(),
137873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Importer.getToContext(),
1379ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getDiags(),
1380ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getNonEquivalentDecls());
138173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return SEC.IsStructurallyEquivalent(FromEnum, ToEnum);
138236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
138336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
1384a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitDecl(Decl *D) {
1385a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1386a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    << D->getDeclKindName();
1387a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  return 0;
1388a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1389a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
13909e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas GregorDecl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
13919e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // Import the major distinguishing characteristics of this typedef.
13929e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  DeclContext *DC, *LexicalDC;
13939e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  DeclarationName Name;
13949e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  SourceLocation Loc;
13959e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
13969e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    return 0;
13979e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
13989e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // If this typedef is not in block scope, determine whether we've
13999e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // seen a typedef with the same name (that we can merge with) or any
14009e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // other entity by that name (which name lookup could conflict with).
14019e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  if (!DC->isFunctionOrMethod()) {
14029e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
14039e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
14049e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
14059e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor         Lookup.first != Lookup.second;
14069e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor         ++Lookup.first) {
14079e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
14089e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor        continue;
14099e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
1410ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1411ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                            FoundTypedef->getUnderlyingType()))
14125ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor          return Importer.Imported(D, FoundTypedef);
14139e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      }
14149e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
14159e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
14169e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    }
14179e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
14189e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    if (!ConflictingDecls.empty()) {
14199e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
14209e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                         ConflictingDecls.data(),
14219e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                         ConflictingDecls.size());
14229e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (!Name)
14239e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor        return 0;
14249e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    }
14259e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  }
14269e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
1427ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the underlying type of this typedef;
1428ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getUnderlyingType());
1429ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
1430ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
1431ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
14329e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // Create the new typedef node.
14339e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
14349e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
14359e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                               Loc, Name.getAsIdentifierInfo(),
14369e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                               TInfo);
14379e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  ToTypedef->setLexicalDeclContext(LexicalDC);
14385ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToTypedef);
14399e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  LexicalDC->addDecl(ToTypedef);
1440ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
14419e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  return ToTypedef;
14429e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor}
14439e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
144436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorDecl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
144536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the major distinguishing characteristics of this enum.
144636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclContext *DC, *LexicalDC;
144736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName Name;
144836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  SourceLocation Loc;
144936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
145036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
145136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
145236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Figure out what enum name we're looking for.
145336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  unsigned IDNS = Decl::IDNS_Tag;
145436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName SearchName = Name;
145536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!SearchName && D->getTypedefForAnonDecl()) {
145636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
145736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    IDNS = Decl::IDNS_Ordinary;
145836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  } else if (Importer.getToContext().getLangOptions().CPlusPlus)
145936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    IDNS |= Decl::IDNS_Ordinary;
146036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
146136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // We may already have an enum of the same name; try to find and match it.
146236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!DC->isFunctionOrMethod() && SearchName) {
146336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
146436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
146536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         Lookup.first != Lookup.second;
146636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++Lookup.first) {
146736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
146836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        continue;
146936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
147036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Decl *Found = *Lookup.first;
147136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
147236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
147336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor          Found = Tag->getDecl();
147436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      }
147536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
147636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
14775ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor        if (IsStructuralMatch(D, FoundEnum))
14785ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor          return Importer.Imported(D, FoundEnum);
147936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      }
148036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
148136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
148236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
148336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
148436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (!ConflictingDecls.empty()) {
148536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
148636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.data(),
148736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.size());
148836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
148936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
149036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
149136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Create the enum declaration.
149273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
149336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                      Name.getAsIdentifierInfo(),
149436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                      Importer.Import(D->getTagKeywordLoc()),
149536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                      0);
149673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  D2->setLexicalDeclContext(LexicalDC);
149773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Importer.Imported(D, D2);
149873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  LexicalDC->addDecl(D2);
149936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
150036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the integer type.
150136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  QualType ToIntegerType = Importer.Import(D->getIntegerType());
150236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (ToIntegerType.isNull())
150336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
150473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  D2->setIntegerType(ToIntegerType);
150536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
150636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the definition
150736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (D->isDefinition()) {
150836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
150936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (T.isNull())
151036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      return 0;
151136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
151236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    QualType ToPromotionType = Importer.Import(D->getPromotionType());
151336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (ToPromotionType.isNull())
151436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      return 0;
151536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
151673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->startDefinition();
151736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    for (DeclContext::decl_iterator FromMem = D->decls_begin(),
151836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         FromMemEnd = D->decls_end();
151936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         FromMem != FromMemEnd;
152036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++FromMem)
152136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Importer.Import(*FromMem);
152236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
152373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->completeDefinition(T, ToPromotionType);
152436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
152536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
152673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return D2;
152736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
152836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
152996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas GregorDecl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
153096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // If this record has a definition in the translation unit we're coming from,
153196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // but this particular declaration is not that definition, import the
153296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // definition and map to that.
1533952b017601f9c82b51119c3a1600f1312a833db9Douglas Gregor  TagDecl *Definition = D->getDefinition();
153496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (Definition && Definition != D) {
153596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *ImportedDef = Importer.Import(Definition);
15365ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor    if (!ImportedDef)
15375ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor      return 0;
15385ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
15395ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor    return Importer.Imported(D, ImportedDef);
154096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
154196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
154296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Import the major distinguishing characteristics of this record.
154396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclContext *DC, *LexicalDC;
154496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName Name;
154596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  SourceLocation Loc;
154696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
154796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
154896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
154996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Figure out what structure name we're looking for.
155096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  unsigned IDNS = Decl::IDNS_Tag;
155196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName SearchName = Name;
155296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!SearchName && D->getTypedefForAnonDecl()) {
155396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
155496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    IDNS = Decl::IDNS_Ordinary;
155596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  } else if (Importer.getToContext().getLangOptions().CPlusPlus)
155696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    IDNS |= Decl::IDNS_Ordinary;
155796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
155896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // We may already have a record of the same name; try to find and match it.
1559e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor  RecordDecl *AdoptDecl = 0;
156096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!DC->isFunctionOrMethod() && SearchName) {
156196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
156296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
156396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         Lookup.first != Lookup.second;
156496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         ++Lookup.first) {
156596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
156696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor        continue;
156796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
156896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Decl *Found = *Lookup.first;
156996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
157096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor        if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
157196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor          Found = Tag->getDecl();
157296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
157396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
157496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
1575e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
1576e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
1577e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // The record types structurally match, or the "from" translation
1578e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // unit only had a forward declaration anyway; call it the same
1579e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // function.
1580e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // FIXME: For C++, we should also merge methods here.
15815ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor            return Importer.Imported(D, FoundDef);
1582e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          }
1583e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        } else {
1584e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          // We have a forward declaration of this type, so adopt that forward
1585e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          // declaration rather than building a new one.
1586e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          AdoptDecl = FoundRecord;
1587e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          continue;
1588e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        }
158996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
159096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
159196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
159296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
159396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
159496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    if (!ConflictingDecls.empty()) {
159596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
159696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         ConflictingDecls.data(),
159796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         ConflictingDecls.size());
159896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
159996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
160096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
160196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Create the record declaration.
160273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  RecordDecl *D2 = AdoptDecl;
160373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!D2) {
160473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D)) {
160573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
1606e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   D->getTagKind(),
1607e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   DC, Loc,
1608e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   Name.getAsIdentifierInfo(),
160996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                        Importer.Import(D->getTagKeywordLoc()));
161073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      D2 = D2CXX;
1611e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor
1612e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor      if (D->isDefinition()) {
1613e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        // Add base classes.
1614e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1615e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        for (CXXRecordDecl::base_class_iterator
161673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                  Base1 = D1CXX->bases_begin(),
161773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor               FromBaseEnd = D1CXX->bases_end();
161873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor             Base1 != FromBaseEnd;
161973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor             ++Base1) {
162073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          QualType T = Importer.Import(Base1->getType());
1621e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          if (T.isNull())
1622e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            return 0;
1623e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor
1624e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          Bases.push_back(
1625e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            new (Importer.getToContext())
162673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                  CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
162773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Base1->isVirtual(),
162873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Base1->isBaseOfClass(),
162973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Base1->getAccessSpecifierAsWritten(),
163096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                   T));
1631e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        }
1632e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        if (!Bases.empty())
163373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          D2CXX->setBases(Bases.data(), Bases.size());
163496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
1635e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor    } else {
163673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
1637e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                    DC, Loc,
1638e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                    Name.getAsIdentifierInfo(),
1639e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                    Importer.Import(D->getTagKeywordLoc()));
164096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
164173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->setLexicalDeclContext(LexicalDC);
164273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    LexicalDC->addDecl(D2);
164396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
16445ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
164573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Importer.Imported(D, D2);
1646e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor
164796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (D->isDefinition()) {
164873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->startDefinition();
164996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    for (DeclContext::decl_iterator FromMem = D->decls_begin(),
165096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                 FromMemEnd = D->decls_end();
165196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         FromMem != FromMemEnd;
165296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         ++FromMem)
165396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Importer.Import(*FromMem);
165496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
165573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->completeDefinition();
165696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
165796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
165873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return D2;
165996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
166096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
166136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorDecl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
166236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the major distinguishing characteristics of this enumerator.
166336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclContext *DC, *LexicalDC;
166436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName Name;
166536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  SourceLocation Loc;
1666ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
166736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
1668ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
1669ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
1670ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
1671ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
1672ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
167336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Determine whether there are any other declarations with the same name and
167436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // in the same context.
167536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!LexicalDC->isFunctionOrMethod()) {
167636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
167736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
167836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
167936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         Lookup.first != Lookup.second;
168036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++Lookup.first) {
168136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
168236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        continue;
168336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
168436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
168536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
168636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
168736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (!ConflictingDecls.empty()) {
168836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
168936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.data(),
169036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.size());
169136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!Name)
169236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        return 0;
169336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
169436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
169536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
169636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  Expr *Init = Importer.Import(D->getInitExpr());
169736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (D->getInitExpr() && !Init)
169836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
169936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
170036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  EnumConstantDecl *ToEnumerator
170136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
170236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                               Name.getAsIdentifierInfo(), T,
170336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                               Init, D->getInitVal());
170436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  ToEnumerator->setLexicalDeclContext(LexicalDC);
17055ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToEnumerator);
170636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  LexicalDC->addDecl(ToEnumerator);
170736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  return ToEnumerator;
170836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
170996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
1710a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
1711a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the major distinguishing characteristics of this function.
1712a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC, *LexicalDC;
1713a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclarationName Name;
1714a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc;
1715ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1716089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    return 0;
1717089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
1718a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Try to find a function in our own ("to") context with the same name, same
1719a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // type, and in the same context as the function we're importing.
1720a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  if (!LexicalDC->isFunctionOrMethod()) {
1721a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1722a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
1723a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1724a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor         Lookup.first != Lookup.second;
1725a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor         ++Lookup.first) {
1726a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1727a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        continue;
1728a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1729a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
1730a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        if (isExternalLinkage(FoundFunction->getLinkage()) &&
1731a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            isExternalLinkage(D->getLinkage())) {
1732ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          if (Importer.IsStructurallyEquivalent(D->getType(),
1733ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                FoundFunction->getType())) {
1734a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            // FIXME: Actually try to merge the body and other attributes.
17355ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor            return Importer.Imported(D, FoundFunction);
1736a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          }
1737a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1738a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // FIXME: Check for overloading more carefully, e.g., by boosting
1739a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Sema::IsOverload out to the AST library.
1740a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1741a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Function overloading is okay in C++.
1742a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          if (Importer.getToContext().getLangOptions().CPlusPlus)
1743a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            continue;
1744a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1745a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Complain about inconsistent function types.
1746a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
1747ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            << Name << D->getType() << FoundFunction->getType();
1748a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          Importer.ToDiag(FoundFunction->getLocation(),
1749a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                          diag::note_odr_value_here)
1750a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            << FoundFunction->getType();
1751a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        }
1752a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      }
1753a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1754a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
1755a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    }
1756a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1757a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    if (!ConflictingDecls.empty()) {
1758a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
1759a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                         ConflictingDecls.data(),
1760a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                         ConflictingDecls.size());
1761a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (!Name)
1762a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        return 0;
1763a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    }
1764a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
1765ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
1766ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
1767ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
1768ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
1769ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
1770a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1771a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the function parameters.
1772a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 8> Parameters;
1773a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
1774a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor       P != PEnd; ++P) {
1775a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
1776a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    if (!ToP)
1777a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      return 0;
1778a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1779a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Parameters.push_back(ToP);
1780a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
1781a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1782a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Create the imported function.
1783a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1784c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  FunctionDecl *ToFunction
1785a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    = FunctionDecl::Create(Importer.getToContext(), DC, Loc,
1786a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                           Name, T, TInfo, D->getStorageClass(),
1787a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                           D->isInlineSpecified(),
1788a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                           D->hasWrittenPrototype());
1789c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToFunction->setLexicalDeclContext(LexicalDC);
1790c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Importer.Imported(D, ToFunction);
1791c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  LexicalDC->addDecl(ToFunction);
1792a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1793a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Set the parameters.
1794a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
1795c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Parameters[I]->setOwningFunction(ToFunction);
1796c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToFunction->addDecl(Parameters[I]);
1797a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
1798c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToFunction->setParams(Parameters.data(), Parameters.size());
1799a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1800a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // FIXME: Other bits to merge?
1801a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1802c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToFunction;
1803a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1804a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
180596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas GregorDecl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
180696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Import the major distinguishing characteristics of a variable.
180796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclContext *DC, *LexicalDC;
180896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName Name;
180996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  SourceLocation Loc;
1810ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1811ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
1812ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
1813ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
1814ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
1815ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
181696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
181796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
181896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
181996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  Expr *BitWidth = Importer.Import(D->getBitWidth());
182096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!BitWidth && D->getBitWidth())
182196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
182296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
182396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
182496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         Loc, Name.getAsIdentifierInfo(),
182596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         T, TInfo, BitWidth, D->isMutable());
182696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  ToField->setLexicalDeclContext(LexicalDC);
18275ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToField);
182896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  LexicalDC->addDecl(ToField);
182996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  return ToField;
183096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
183196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
18322e55e3af2f6d6c0509495357fade95105dd144cdDouglas GregorDecl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
18332e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Import the major distinguishing characteristics of an ivar.
18342e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  DeclContext *DC, *LexicalDC;
18352e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  DeclarationName Name;
18362e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  SourceLocation Loc;
18372e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
18382e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
18392e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
18402e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Determine whether we've already imported this ivar
18412e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
18422e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor       Lookup.first != Lookup.second;
18432e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor       ++Lookup.first) {
18442e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
18452e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (Importer.IsStructurallyEquivalent(D->getType(),
18462e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                            FoundIvar->getType())) {
18472e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.Imported(D, FoundIvar);
18482e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        return FoundIvar;
18492e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      }
18502e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
18512e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
18522e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << Name << D->getType() << FoundIvar->getType();
18532e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
18542e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << FoundIvar->getType();
18552e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      return 0;
18562e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    }
18572e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  }
18582e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
18592e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Import the type.
18602e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  QualType T = Importer.Import(D->getType());
18612e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (T.isNull())
18622e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
18632e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
18642e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
18652e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  Expr *BitWidth = Importer.Import(D->getBitWidth());
18662e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (!BitWidth && D->getBitWidth())
18672e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
18682e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
18692e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(), DC,
18702e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              Loc, Name.getAsIdentifierInfo(),
18712e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              T, TInfo, D->getAccessControl(),
18722e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              BitWidth);
18732e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  ToIvar->setLexicalDeclContext(LexicalDC);
18742e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  Importer.Imported(D, ToIvar);
18752e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  LexicalDC->addDecl(ToIvar);
18762e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  return ToIvar;
18772e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
18782e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor}
18792e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
1880a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
1881a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the major distinguishing characteristics of a variable.
1882a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC, *LexicalDC;
1883a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclarationName Name;
1884a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc;
1885ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1886a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return 0;
1887089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
1888089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Try to find a variable in our own ("to") context with the same name and
1889089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // in the same context as the variable we're importing.
18909bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (D->isFileVarDecl()) {
1891089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    VarDecl *MergeWithVar = 0;
1892089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1893089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
18949bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1895089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor         Lookup.first != Lookup.second;
1896089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor         ++Lookup.first) {
1897089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1898089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        continue;
1899089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
1900089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
1901089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        // We have found a variable that we may need to merge with. Check it.
1902089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        if (isExternalLinkage(FoundVar->getLinkage()) &&
1903089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            isExternalLinkage(D->getLinkage())) {
1904ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          if (Importer.IsStructurallyEquivalent(D->getType(),
1905ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                FoundVar->getType())) {
1906089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            MergeWithVar = FoundVar;
1907089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            break;
1908089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          }
1909089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
1910d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          const ArrayType *FoundArray
1911d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            = Importer.getToContext().getAsArrayType(FoundVar->getType());
1912d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          const ArrayType *TArray
1913ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            = Importer.getToContext().getAsArrayType(D->getType());
1914d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          if (FoundArray && TArray) {
1915d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            if (isa<IncompleteArrayType>(FoundArray) &&
1916d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor                isa<ConstantArrayType>(TArray)) {
1917ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              // Import the type.
1918ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              QualType T = Importer.Import(D->getType());
1919ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              if (T.isNull())
1920ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                return 0;
1921ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
1922d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              FoundVar->setType(T);
1923d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              MergeWithVar = FoundVar;
1924d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              break;
1925d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            } else if (isa<IncompleteArrayType>(TArray) &&
1926d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor                       isa<ConstantArrayType>(FoundArray)) {
1927d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              MergeWithVar = FoundVar;
1928d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              break;
19290f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor            }
19300f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor          }
19310f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor
1932089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
1933ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            << Name << D->getType() << FoundVar->getType();
1934089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
1935089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            << FoundVar->getType();
1936089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        }
1937089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      }
1938089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
1939089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
1940089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
1941089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
1942089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    if (MergeWithVar) {
1943089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      // An equivalent variable with external linkage has been found. Link
1944089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      // the two declarations, then merge them.
19455ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor      Importer.Imported(D, MergeWithVar);
1946089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
1947089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (VarDecl *DDef = D->getDefinition()) {
1948089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
1949089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(ExistingDef->getLocation(),
1950089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                          diag::err_odr_variable_multiple_def)
1951089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            << Name;
1952089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
1953089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        } else {
1954089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Expr *Init = Importer.Import(DDef->getInit());
1955838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor          MergeWithVar->setInit(Init);
1956089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        }
1957089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      }
1958089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
1959089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      return MergeWithVar;
1960089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
1961089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
1962089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    if (!ConflictingDecls.empty()) {
1963089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
1964089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                         ConflictingDecls.data(),
1965089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                         ConflictingDecls.size());
1966089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (!Name)
1967089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        return 0;
1968089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
1969089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  }
197082fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
1971ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
1972ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
1973ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
1974ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
1975ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
1976089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Create the imported variable.
197782fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1978089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
1979089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                   Name.getAsIdentifierInfo(), T, TInfo,
1980089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                   D->getStorageClass());
19819bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ToVar->setLexicalDeclContext(LexicalDC);
19825ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToVar);
19839bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  LexicalDC->addDecl(ToVar);
19849bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
1985089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Merge the initializer.
1986089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // FIXME: Can we really import any initializer? Alternatively, we could force
1987089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // ourselves to import every declaration of a variable and then only use
1988089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // getInit() here.
1989838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
1990089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
1991089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // FIXME: Other bits to merge?
1992089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
1993089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  return ToVar;
1994089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
1995089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
19962cd00932b9b5403047139ce8cfaa3ae47966f894Douglas GregorDecl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
19972cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Parameters are created in the translation unit's context, then moved
19982cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // into the function declaration's context afterward.
19992cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
20002cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
20012cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the name of this declaration.
20022cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
20032cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  if (D->getDeclName() && !Name)
20042cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    return 0;
20052cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
20062cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the location of this declaration.
20072cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
20082cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
20092cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the parameter's type.
20102cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  QualType T = Importer.Import(D->getType());
20112cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  if (T.isNull())
20122cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    return 0;
20132cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
20142cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Create the imported parameter.
20152cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  ImplicitParamDecl *ToParm
20162cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    = ImplicitParamDecl::Create(Importer.getToContext(), DC,
20172cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor                                Loc, Name.getAsIdentifierInfo(),
20182cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor                                T);
20192cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  return Importer.Imported(D, ToParm);
20202cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor}
20212cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
2022a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2023a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Parameters are created in the translation unit's context, then moved
2024a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // into the function declaration's context afterward.
2025a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2026a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
202782fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // Import the name of this declaration.
202882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
202982fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (D->getDeclName() && !Name)
203082fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
203182fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2032a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the location of this declaration.
2033a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
2034a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2035a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the parameter's type.
2036a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  QualType T = Importer.Import(D->getType());
203782fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (T.isNull())
203882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
203982fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2040a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Create the imported parameter.
2041a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2042a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2043a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            Loc, Name.getAsIdentifierInfo(),
2044a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            T, TInfo, D->getStorageClass(),
2045a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            /*FIXME: Default argument*/ 0);
20465ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  return Importer.Imported(D, ToParm);
2047a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
204882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2049c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas GregorDecl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2050c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the major distinguishing characteristics of a method.
2051c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  DeclContext *DC, *LexicalDC;
2052c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  DeclarationName Name;
2053c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  SourceLocation Loc;
2054c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2055c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return 0;
2056c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2057c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2058c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       Lookup.first != Lookup.second;
2059c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       ++Lookup.first) {
2060c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2061c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2062c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        continue;
2063c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2064c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check return types.
2065c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2066c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                             FoundMethod->getResultType())) {
2067c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2068c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name
2069c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->getResultType() << FoundMethod->getResultType();
2070c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2071c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2072c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2073c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2074c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2075c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2076c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check the number of parameters.
2077c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (D->param_size() != FoundMethod->param_size()) {
2078c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2079c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name
2080c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->param_size() << FoundMethod->param_size();
2081c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2082c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2083c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2084c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2085c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2086c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2087c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check parameter types.
2088c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2089c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor             PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2090c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor           P != PEnd; ++P, ++FoundP) {
2091c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2092c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                               (*FoundP)->getType())) {
2093c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          Importer.FromDiag((*P)->getLocation(),
2094c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                            diag::err_odr_objc_method_param_type_inconsistent)
2095c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << D->isInstanceMethod() << Name
2096c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << (*P)->getType() << (*FoundP)->getType();
2097c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2098c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << (*FoundP)->getType();
2099c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          return 0;
2100c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        }
2101c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2102c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2103c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check variadic/non-variadic.
2104c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check the number of parameters.
2105c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (D->isVariadic() != FoundMethod->isVariadic()) {
2106c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2107c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2108c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2109c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2110c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2111c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2112c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2113c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2114c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // FIXME: Any other bits we need to merge?
2115c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      return Importer.Imported(D, FoundMethod);
2116c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    }
2117c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2118c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2119c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the result type.
2120c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  QualType ResultTy = Importer.Import(D->getResultType());
2121c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (ResultTy.isNull())
2122c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return 0;
2123c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2124c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ObjCMethodDecl *ToMethod
2125c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    = ObjCMethodDecl::Create(Importer.getToContext(),
2126c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Loc,
2127c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Importer.Import(D->getLocEnd()),
2128c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Name.getObjCSelector(),
2129c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             ResultTy, DC,
2130c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isInstanceMethod(),
2131c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isVariadic(),
2132c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isSynthesized(),
2133c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->getImplementationControl());
2134c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2135c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // FIXME: When we decide to merge method definitions, we'll need to
2136c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // deal with implicit parameters.
2137c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2138c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the parameters
2139c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2140c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2141c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                   FromPEnd = D->param_end();
2142c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       FromP != FromPEnd;
2143c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       ++FromP) {
2144c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2145c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    if (!ToP)
2146c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      return 0;
2147c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2148c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToParams.push_back(ToP);
2149c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2150c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2151c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Set the parameters.
2152c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2153c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToParams[I]->setOwningFunction(ToMethod);
2154c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToMethod->addDecl(ToParams[I]);
2155c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2156c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToMethod->setMethodParams(Importer.getToContext(),
2157c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                            ToParams.data(), ToParams.size());
2158c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2159c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToMethod->setLexicalDeclContext(LexicalDC);
2160c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Importer.Imported(D, ToMethod);
2161c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  LexicalDC->addDecl(ToMethod);
2162c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToMethod;
2163c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor}
2164c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2165b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas GregorDecl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2166b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import the major distinguishing characteristics of a category.
2167b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  DeclContext *DC, *LexicalDC;
2168b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  DeclarationName Name;
2169b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  SourceLocation Loc;
2170b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2171b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    return 0;
2172b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2173b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCInterfaceDecl *ToInterface
2174b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2175b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (!ToInterface)
2176b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    return 0;
2177b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2178b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Determine if we've already encountered this category.
2179b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCCategoryDecl *MergeWithCategory
2180b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2181b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCCategoryDecl *ToCategory = MergeWithCategory;
2182b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (!ToCategory) {
2183b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2184b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Importer.Import(D->getAtLoc()),
2185b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Loc,
2186b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                       Importer.Import(D->getCategoryNameLoc()),
2187b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Name.getAsIdentifierInfo());
2188b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setLexicalDeclContext(LexicalDC);
2189b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    LexicalDC->addDecl(ToCategory);
2190b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Imported(D, ToCategory);
2191b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2192b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // Link this category into its class's category list.
2193b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setClassInterface(ToInterface);
2194b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->insertNextClassCategory();
2195b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2196b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // Import protocols
2197b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2198b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2199b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2200b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      = D->protocol_loc_begin();
2201b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2202b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          FromProtoEnd = D->protocol_end();
2203b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor         FromProto != FromProtoEnd;
2204b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor         ++FromProto, ++FromProtoLoc) {
2205b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      ObjCProtocolDecl *ToProto
2206b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2207b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      if (!ToProto)
2208b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor        return 0;
2209b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      Protocols.push_back(ToProto);
2210b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2211b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    }
2212b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2213b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
2214b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2215b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                ProtocolLocs.data(), Importer.getToContext());
2216b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2217b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  } else {
2218b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Imported(D, ToCategory);
2219b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  }
2220b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2221b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import all of the members of this category.
2222b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  for (DeclContext::decl_iterator FromMem = D->decls_begin(),
2223b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                              FromMemEnd = D->decls_end();
2224b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor       FromMem != FromMemEnd;
2225b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor       ++FromMem)
2226b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Import(*FromMem);
2227b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2228b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // If we have an implementation, import it as well.
2229b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (D->getImplementation()) {
2230b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ObjCCategoryImplDecl *Impl
2231b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation()));
2232b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    if (!Impl)
2233b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      return 0;
2234b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2235b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setImplementation(Impl);
2236b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  }
2237b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2238b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  return ToCategory;
2239b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor}
2240b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
22412e2a400383c7a64a927f61eeed596b08928e1d4bDouglas GregorDecl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
2242b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import the major distinguishing characteristics of a protocol.
22432e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  DeclContext *DC, *LexicalDC;
22442e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  DeclarationName Name;
22452e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  SourceLocation Loc;
22462e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
22472e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    return 0;
22482e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
22492e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  ObjCProtocolDecl *MergeWithProtocol = 0;
22502e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
22512e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       Lookup.first != Lookup.second;
22522e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++Lookup.first) {
22532e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
22542e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      continue;
22552e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
22562e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
22572e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      break;
22582e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  }
22592e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
22602e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  ObjCProtocolDecl *ToProto = MergeWithProtocol;
22612e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  if (!ToProto || ToProto->isForwardDecl()) {
22622e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if (!ToProto) {
22632e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
22642e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                                         Name.getAsIdentifierInfo());
22652e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto->setForwardDecl(D->isForwardDecl());
22662e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto->setLexicalDeclContext(LexicalDC);
22672e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      LexicalDC->addDecl(ToProto);
22682e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    }
22692e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Imported(D, ToProto);
22702e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
22712e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    // Import protocols
22722e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
22732e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
22742e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    ObjCProtocolDecl::protocol_loc_iterator
22752e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      FromProtoLoc = D->protocol_loc_begin();
22762e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
22772e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                                          FromProtoEnd = D->protocol_end();
22782e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       FromProto != FromProtoEnd;
22792e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++FromProto, ++FromProtoLoc) {
22802e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ObjCProtocolDecl *ToProto
22812e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
22822e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      if (!ToProto)
22832e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor        return 0;
22842e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      Protocols.push_back(ToProto);
22852e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
22862e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    }
22872e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
22882e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
22892e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    ToProto->setProtocolList(Protocols.data(), Protocols.size(),
22902e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                             ProtocolLocs.data(), Importer.getToContext());
22912e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  } else {
22922e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Imported(D, ToProto);
22932e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  }
22942e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
2295b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import all of the members of this protocol.
22962e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  for (DeclContext::decl_iterator FromMem = D->decls_begin(),
22972e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                               FromMemEnd = D->decls_end();
22982e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       FromMem != FromMemEnd;
22992e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++FromMem)
23002e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Import(*FromMem);
23012e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
23022e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  return ToProto;
23032e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor}
23042e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
2305a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas GregorDecl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2306a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // Import the major distinguishing characteristics of an @interface.
2307a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  DeclContext *DC, *LexicalDC;
2308a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  DeclarationName Name;
2309a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  SourceLocation Loc;
2310a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2311a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    return 0;
2312a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2313a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  ObjCInterfaceDecl *MergeWithIface = 0;
2314a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2315a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       Lookup.first != Lookup.second;
2316a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++Lookup.first) {
2317a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2318a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      continue;
2319a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2320a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2321a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      break;
2322a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
2323a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2324a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  ObjCInterfaceDecl *ToIface = MergeWithIface;
2325a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (!ToIface || ToIface->isForwardDecl()) {
2326a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!ToIface) {
2327a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2328a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          DC, Loc,
2329a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          Name.getAsIdentifierInfo(),
2330a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          Importer.Import(D->getClassLoc()),
2331a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          D->isForwardDecl(),
2332a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          D->isImplicitInterfaceDecl());
23332e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToIface->setForwardDecl(D->isForwardDecl());
2334a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setLexicalDeclContext(LexicalDC);
2335a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      LexicalDC->addDecl(ToIface);
2336a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
2337a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Imported(D, ToIface);
2338a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2339a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (D->getSuperClass()) {
2340a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ObjCInterfaceDecl *Super
2341a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2342a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      if (!Super)
2343a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        return 0;
2344a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2345a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setSuperClass(Super);
2346a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2347a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
2348a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2349a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // Import protocols
2350a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2351a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2352a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ObjCInterfaceDecl::protocol_loc_iterator
2353a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      FromProtoLoc = D->protocol_loc_begin();
2354a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2355a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                           FromProtoEnd = D->protocol_end();
2356a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       FromProto != FromProtoEnd;
2357a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++FromProto, ++FromProtoLoc) {
2358a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ObjCProtocolDecl *ToProto
2359a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2360a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      if (!ToProto)
2361a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        return 0;
2362a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      Protocols.push_back(ToProto);
2363a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2364a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
2365a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2366a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
2367a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2368a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                             ProtocolLocs.data(), Importer.getToContext());
2369a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2370a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // Import @end range
2371a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2372a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  } else {
2373a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Imported(D, ToIface);
23742e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
23752e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    // Check for consistency of superclasses.
23762e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    DeclarationName FromSuperName, ToSuperName;
23772e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (D->getSuperClass())
23782e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
23792e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (ToIface->getSuperClass())
23802e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      ToSuperName = ToIface->getSuperClass()->getDeclName();
23812e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (FromSuperName != ToSuperName) {
23822e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(ToIface->getLocation(),
23832e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                      diag::err_odr_objc_superclass_inconsistent)
23842e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << ToIface->getDeclName();
23852e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (ToIface->getSuperClass())
23862e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.ToDiag(ToIface->getSuperClassLoc(),
23872e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                        diag::note_odr_objc_superclass)
23882e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor          << ToIface->getSuperClass()->getDeclName();
23892e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      else
23902e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.ToDiag(ToIface->getLocation(),
23912e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                        diag::note_odr_objc_missing_superclass);
23922e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (D->getSuperClass())
23932e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.FromDiag(D->getSuperClassLoc(),
23942e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                          diag::note_odr_objc_superclass)
23952e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor          << D->getSuperClass()->getDeclName();
23962e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      else
23972e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.FromDiag(D->getLocation(),
23982e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                          diag::note_odr_objc_missing_superclass);
23992e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      return 0;
24002e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    }
2401a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
2402a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2403b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import categories. When the categories themselves are imported, they'll
2404b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // hook themselves into this interface.
2405b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
2406b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor       FromCat = FromCat->getNextClassCategory())
2407b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Import(FromCat);
2408b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2409a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // Import all of the members of this class.
2410a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  for (DeclContext::decl_iterator FromMem = D->decls_begin(),
2411a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                               FromMemEnd = D->decls_end();
2412a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       FromMem != FromMemEnd;
2413a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++FromMem)
2414a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Import(*FromMem);
2415a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2416a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // If we have an @implementation, import it as well.
2417a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (D->getImplementation()) {
2418a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ObjCImplementationDecl *Impl
2419a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      = cast<ObjCImplementationDecl>(Importer.Import(D->getImplementation()));
2420a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!Impl)
2421a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      return 0;
2422a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2423a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setImplementation(Impl);
2424a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
2425a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
24262e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  return ToIface;
2427a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor}
2428a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2429e3261624c1870e52d7efc2ac83e647713361ac6cDouglas GregorDecl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
2430e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Import the major distinguishing characteristics of an @property.
2431e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  DeclContext *DC, *LexicalDC;
2432e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  DeclarationName Name;
2433e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  SourceLocation Loc;
2434e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2435e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    return 0;
2436e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2437e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Check whether we have already imported this property.
2438e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2439e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       Lookup.first != Lookup.second;
2440e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       ++Lookup.first) {
2441e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    if (ObjCPropertyDecl *FoundProp
2442e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                                = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
2443e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // Check property types.
2444e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      if (!Importer.IsStructurallyEquivalent(D->getType(),
2445e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                                             FoundProp->getType())) {
2446e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
2447e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor          << Name << D->getType() << FoundProp->getType();
2448e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
2449e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor          << FoundProp->getType();
2450e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        return 0;
2451e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      }
2452e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2453e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // FIXME: Check property attributes, getters, setters, etc.?
2454e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2455e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // Consider these properties to be equivalent.
2456e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      Importer.Imported(D, FoundProp);
2457e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      return FoundProp;
2458e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    }
2459e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  }
2460e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2461e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Import the type.
2462e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  QualType T = Importer.Import(D->getType());
2463e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  if (T.isNull())
2464e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    return 0;
2465e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2466e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Create the new property.
2467e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ObjCPropertyDecl *ToProperty
2468e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
2469e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               Name.getAsIdentifierInfo(),
2470e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               Importer.Import(D->getAtLoc()),
2471e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               T,
2472e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               D->getPropertyImplementation());
2473e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  Importer.Imported(D, ToProperty);
2474e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setLexicalDeclContext(LexicalDC);
2475e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  LexicalDC->addDecl(ToProperty);
2476e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2477e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setPropertyAttributes(D->getPropertyAttributes());
2478e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setGetterName(Importer.Import(D->getGetterName()));
2479e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setSetterName(Importer.Import(D->getSetterName()));
2480e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setGetterMethodDecl(
2481e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
2482e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setSetterMethodDecl(
2483e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
2484e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setPropertyIvarDecl(
2485e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
2486e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  return ToProperty;
2487e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor}
2488e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
24892b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas GregorDecl *
24902b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas GregorASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
24912b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  // Import the context of this declaration.
24922b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
24932b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  if (!DC)
24942b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    return 0;
24952b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
24962b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  DeclContext *LexicalDC = DC;
24972b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
24982b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
24992b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    if (!LexicalDC)
25002b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      return 0;
25012b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  }
25022b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
25032b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  // Import the location of this declaration.
25042b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
25052b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
25062b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
25072b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  llvm::SmallVector<SourceLocation, 4> Locations;
25082b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
25092b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    = D->protocol_loc_begin();
25102b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  for (ObjCForwardProtocolDecl::protocol_iterator FromProto
25112b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor         = D->protocol_begin(), FromProtoEnd = D->protocol_end();
25122b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor       FromProto != FromProtoEnd;
25132b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor       ++FromProto, ++FromProtoLoc) {
25142b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    ObjCProtocolDecl *ToProto
25152b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
25162b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    if (!ToProto)
25172b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      continue;
25182b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
25192b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Protocols.push_back(ToProto);
25202b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Locations.push_back(Importer.Import(*FromProtoLoc));
25212b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  }
25222b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
25232b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ObjCForwardProtocolDecl *ToForward
25242b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
25252b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor                                      Protocols.data(), Protocols.size(),
25262b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor                                      Locations.data());
25272b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ToForward->setLexicalDeclContext(LexicalDC);
25282b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  LexicalDC->addDecl(ToForward);
25292b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  Importer.Imported(D, ToForward);
25302b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  return ToForward;
25312b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor}
25322b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
2533a2bc15b7463a9f85a5bff1531d833c278426a733Douglas GregorDecl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
2534a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  // Import the context of this declaration.
2535a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2536a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  if (!DC)
2537a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    return 0;
2538a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2539a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  DeclContext *LexicalDC = DC;
2540a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
2541a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2542a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    if (!LexicalDC)
2543a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      return 0;
2544a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  }
2545a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2546a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  // Import the location of this declaration.
2547a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
2548a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2549a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
2550a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  llvm::SmallVector<SourceLocation, 4> Locations;
2551a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
2552a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor       From != FromEnd; ++From) {
2553a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    ObjCInterfaceDecl *ToIface
2554a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
2555a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    if (!ToIface)
2556a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      continue;
2557a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2558a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Interfaces.push_back(ToIface);
2559a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Locations.push_back(Importer.Import(From->getLocation()));
2560a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  }
2561a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2562a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
2563a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Loc,
2564a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Interfaces.data(),
2565a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Locations.data(),
2566a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Interfaces.size());
2567a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  ToClass->setLexicalDeclContext(LexicalDC);
2568a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  LexicalDC->addDecl(ToClass);
2569a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  Importer.Imported(D, ToClass);
2570a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  return ToClass;
2571a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor}
2572a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
25734800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
25744800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor// Import Statements
25754800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
25764800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
25774800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorStmt *ASTNodeImporter::VisitStmt(Stmt *S) {
25784800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
25794800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    << S->getStmtClassName();
25804800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return 0;
25814800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
25824800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
25834800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
25844800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor// Import Expressions
25854800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
25864800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorExpr *ASTNodeImporter::VisitExpr(Expr *E) {
25874800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
25884800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    << E->getStmtClassName();
25894800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return 0;
25904800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
25914800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
25924800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorExpr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
25934800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  QualType T = Importer.Import(E->getType());
25944800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (T.isNull())
25954800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return 0;
25964800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
25974800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return new (Importer.getToContext())
25984800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    IntegerLiteral(E->getValue(), T, Importer.Import(E->getLocation()));
25994800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
26004800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
260136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorExpr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
260236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  QualType T = Importer.Import(E->getType());
260336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (T.isNull())
260436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
260536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
260636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
260736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!SubExpr)
260836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
260936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
261036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  return new (Importer.getToContext()) ImplicitCastExpr(T, E->getCastKind(),
261136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                                        SubExpr,
261236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                                        E->isLvalueCast());
261336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
261436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
26154800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorASTImporter::ASTImporter(Diagnostic &Diags,
26164800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                         ASTContext &ToContext, FileManager &ToFileManager,
26174800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                         ASTContext &FromContext, FileManager &FromFileManager)
26181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  : ToContext(ToContext), FromContext(FromContext),
2619885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToFileManager(ToFileManager), FromFileManager(FromFileManager),
26204800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Diags(Diags) {
26219bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ImportedDecls[FromContext.getTranslationUnitDecl()]
26229bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    = ToContext.getTranslationUnitDecl();
26239bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
26249bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
26259bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorASTImporter::~ASTImporter() { }
26261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
26271b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTImporter::Import(QualType FromT) {
26281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (FromT.isNull())
26291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
26301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
2631169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Check whether we've already imported this type.
2632169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  llvm::DenseMap<Type *, Type *>::iterator Pos
2633169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor    = ImportedTypes.find(FromT.getTypePtr());
2634169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  if (Pos != ImportedTypes.end())
2635169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor    return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
26361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
2637169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Import the type
26381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  ASTNodeImporter Importer(*this);
26391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToT = Importer.Visit(FromT.getTypePtr());
26401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToT.isNull())
26411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToT;
26421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
2643169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Record the imported type.
2644169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
2645169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor
26461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
26471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
26481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
26499bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorTypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
265082fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (!FromTSI)
265182fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return FromTSI;
265282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
265382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // FIXME: For now we just create a "trivial" type source info based
265482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // on the type and a seingle location. Implement a real version of
265582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // this.
265682fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  QualType T = Import(FromTSI->getType());
265782fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (T.isNull())
265882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
265982fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
266082fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  return ToContext.getTrivialTypeSourceInfo(T,
266182fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor                        FromTSI->getTypeLoc().getFullSourceRange().getBegin());
26629bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
26639bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
26649bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorDecl *ASTImporter::Import(Decl *FromD) {
26659bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromD)
26669bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
26679bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
26689bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Check whether we've already imported this declaration.
26699bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
26709bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (Pos != ImportedDecls.end())
26719bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return Pos->second;
26729bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
26739bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Import the type
26749bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ASTNodeImporter Importer(*this);
26759bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  Decl *ToD = Importer.Visit(FromD);
26769bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!ToD)
26779bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
26789bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
26799bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Record the imported declaration.
26809bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ImportedDecls[FromD] = ToD;
2681ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2682ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
2683ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // Keep track of anonymous tags that have an associated typedef.
2684ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (FromTag->getTypedefForAnonDecl())
2685ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      AnonTagsWithPendingTypedefs.push_back(FromTag);
2686ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
2687ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // When we've finished transforming a typedef, see whether it was the
2688ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // typedef for an anonymous tag.
2689ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    for (llvm::SmallVector<TagDecl *, 4>::iterator
2690ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor               FromTag = AnonTagsWithPendingTypedefs.begin(),
2691ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            FromTagEnd = AnonTagsWithPendingTypedefs.end();
2692ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor         FromTag != FromTagEnd; ++FromTag) {
2693ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
2694ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
2695ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          // We found the typedef for an anonymous tag; link them.
2696ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
2697ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          AnonTagsWithPendingTypedefs.erase(FromTag);
2698ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          break;
2699ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        }
2700ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      }
2701ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    }
2702ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  }
2703ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
27049bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return ToD;
27059bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
27069bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
27079bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorDeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
27089bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromDC)
27099bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return FromDC;
27109bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
27119bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
27129bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
27139bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
27149bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorExpr *ASTImporter::Import(Expr *FromE) {
27159bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromE)
27169bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
27179bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
27189bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
27199bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
27209bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
27219bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorStmt *ASTImporter::Import(Stmt *FromS) {
27229bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromS)
27239bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
27249bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
27254800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Check whether we've already imported this declaration.
27264800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
27274800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (Pos != ImportedStmts.end())
27284800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return Pos->second;
27294800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
27304800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Import the type
27314800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  ASTNodeImporter Importer(*this);
27324800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Stmt *ToS = Importer.Visit(FromS);
27334800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (!ToS)
27344800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return 0;
27354800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
27364800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Record the imported declaration.
27374800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  ImportedStmts[FromS] = ToS;
27384800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return ToS;
27399bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
27409bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
27419bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorNestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
27429bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromNNS)
27439bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
27449bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
27459bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // FIXME: Implement!
27469bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return 0;
27479bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
27489bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
27499bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorSourceLocation ASTImporter::Import(SourceLocation FromLoc) {
27509bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (FromLoc.isInvalid())
27519bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return SourceLocation();
27529bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
2753885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &FromSM = FromContext.getSourceManager();
2754885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
2755885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // For now, map everything down to its spelling location, so that we
2756885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // don't have to import macro instantiations.
2757885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // FIXME: Import macro instantiations!
2758885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  FromLoc = FromSM.getSpellingLoc(FromLoc);
2759885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
2760885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &ToSM = ToContext.getSourceManager();
2761885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  return ToSM.getLocForStartOfFile(Import(Decomposed.first))
2762885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor             .getFileLocWithOffset(Decomposed.second);
27639bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
27649bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
27659bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorSourceRange ASTImporter::Import(SourceRange FromRange) {
27669bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
27679bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
27689bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
2769885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas GregorFileID ASTImporter::Import(FileID FromID) {
2770885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  llvm::DenseMap<unsigned, FileID>::iterator Pos
2771885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    = ImportedFileIDs.find(FromID.getHashValue());
2772885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  if (Pos != ImportedFileIDs.end())
2773885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    return Pos->second;
2774885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
2775885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &FromSM = FromContext.getSourceManager();
2776885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &ToSM = ToContext.getSourceManager();
2777885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
2778885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
2779885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
2780885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // Include location of this file.
2781885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
2782885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
2783885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // Map the FileID for to the "to" source manager.
2784885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  FileID ToID;
2785885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
2786885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  if (Cache->Entry) {
2787885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We probably want to use getVirtualFile(), so we don't hit the
2788885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // disk again
2789885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
2790885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // than mmap the files several times.
2791885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
2792885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToID = ToSM.createFileID(Entry, ToIncludeLoc,
2793885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                             FromSLoc.getFile().getFileCharacteristic());
2794885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  } else {
2795885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We want to re-use the existing MemoryBuffer!
2796885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    const llvm::MemoryBuffer *FromBuf = Cache->getBuffer();
2797885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    llvm::MemoryBuffer *ToBuf
2798885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor      = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBufferStart(),
2799885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                                             FromBuf->getBufferEnd(),
2800885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                                             FromBuf->getBufferIdentifier());
2801885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToID = ToSM.createFileIDForMemBuffer(ToBuf);
2802885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  }
2803885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
2804885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
2805885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  ImportedFileIDs[FromID.getHashValue()] = ToID;
2806885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  return ToID;
2807885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor}
2808885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
28091b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorDeclarationName ASTImporter::Import(DeclarationName FromName) {
28101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!FromName)
28111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return DeclarationName();
28121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
28131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  switch (FromName.getNameKind()) {
28141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::Identifier:
28151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Import(FromName.getAsIdentifierInfo());
28161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
28171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCZeroArgSelector:
28181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCOneArgSelector:
28191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCMultiArgSelector:
28201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Import(FromName.getObjCSelector());
28211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
28221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXConstructorName: {
28231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
28241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
28251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
28261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
28271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXConstructorName(
28281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
28291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
28301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
28311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXDestructorName: {
28321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
28331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
28341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
28351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
28361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXDestructorName(
28371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
28381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
28391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
28401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXConversionFunctionName: {
28411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
28421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
28431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
28441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
28451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXConversionFunctionName(
28461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
28471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
28481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
28491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXOperatorName:
28501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXOperatorName(
28511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                          FromName.getCXXOverloadedOperator());
28521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
28531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXLiteralOperatorName:
28541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXLiteralOperatorName(
28551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                   Import(FromName.getCXXLiteralIdentifier()));
28561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
28571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXUsingDirective:
28581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: STATICS!
28591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return DeclarationName::getUsingDirectiveName();
28601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
28611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
28621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Silence bogus GCC warning
28631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return DeclarationName();
28641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
28651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
28661b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorIdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) {
28671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!FromId)
28681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return 0;
28691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
28701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return &ToContext.Idents.get(FromId->getName());
28711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
2872089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2873c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas GregorSelector ASTImporter::Import(Selector FromSel) {
2874c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (FromSel.isNull())
2875c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return Selector();
2876c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2877c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  llvm::SmallVector<IdentifierInfo *, 4> Idents;
2878c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
2879c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
2880c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
2881c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
2882c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor}
2883c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2884089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
2885089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                DeclContext *DC,
2886089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                unsigned IDNS,
2887089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                NamedDecl **Decls,
2888089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                unsigned NumDecls) {
2889089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  return Name;
2890089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
2891089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2892089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
28934800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return Diags.Report(FullSourceLoc(Loc, ToContext.getSourceManager()),
28944800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                      DiagID);
2895089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
2896089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2897089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
28984800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return Diags.Report(FullSourceLoc(Loc, FromContext.getSourceManager()),
28994800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                      DiagID);
2900089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
29015ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
29025ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas GregorDecl *ASTImporter::Imported(Decl *From, Decl *To) {
29035ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  ImportedDecls[From] = To;
29045ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  return To;
2905af667588d53de22795c5304f1496ccaac2a71402Daniel Dunbar}
2906ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2907ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregorbool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
2908ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  llvm::DenseMap<Type *, Type *>::iterator Pos
2909ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor   = ImportedTypes.find(From.getTypePtr());
2910ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
2911ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return true;
2912ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2913ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  StructuralEquivalenceContext SEC(FromContext, ToContext, Diags,
2914ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   NonEquivalentDecls);
2915ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  return SEC.IsStructurallyEquivalent(From, To);
2916ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor}
2917