ASTImporter.cpp revision deacbdca554298ccdf636f19c6094a8825ec6b34
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"
221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor#include "clang/AST/TypeVisitor.h"
23885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor#include "clang/Basic/FileManager.h"
24885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor#include "clang/Basic/SourceManager.h"
25885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor#include "llvm/Support/MemoryBuffer.h"
2673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor#include <deque>
271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregorusing namespace clang;
291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregornamespace {
31089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
324800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                          public DeclVisitor<ASTNodeImporter, Decl *>,
334800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                          public StmtVisitor<ASTNodeImporter, Stmt *> {
341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ASTImporter &Importer;
351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  public:
371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    using TypeVisitor<ASTNodeImporter, QualType>::Visit;
409bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
414800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // Importing types
4489cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    QualType VisitType(Type *T);
451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitBuiltinType(BuiltinType *T);
461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitComplexType(ComplexType *T);
471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitPointerType(PointerType *T);
481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitBlockPointerType(BlockPointerType *T);
491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitLValueReferenceType(LValueReferenceType *T);
501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitRValueReferenceType(RValueReferenceType *T);
511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitMemberPointerType(MemberPointerType *T);
521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitConstantArrayType(ConstantArrayType *T);
531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitIncompleteArrayType(IncompleteArrayType *T);
541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitVariableArrayType(VariableArrayType *T);
551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentSizedArrayType
561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentSizedExtVectorType
571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitVectorType(VectorType *T);
581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitExtVectorType(ExtVectorType *T);
591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitFunctionNoProtoType(FunctionNoProtoType *T);
601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitFunctionProtoType(FunctionProtoType *T);
611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: UnresolvedUsingType
621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitTypedefType(TypedefType *T);
631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitTypeOfExprType(TypeOfExprType *T);
641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentTypeOfExprType
651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitTypeOfType(TypeOfType *T);
661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitDecltypeType(DecltypeType *T);
671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentDecltypeType
681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitRecordType(RecordType *T);
691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitEnumType(EnumType *T);
701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: TemplateTypeParmType
711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: SubstTemplateTypeParmType
721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: TemplateSpecializationType
73465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    QualType VisitElaboratedType(ElaboratedType *T);
744714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    // FIXME: DependentNameType
7533500955d731c73717af52088b7fc0e7a85681e7John McCall    // FIXME: DependentTemplateSpecializationType
761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitObjCInterfaceType(ObjCInterfaceType *T);
77c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    QualType VisitObjCObjectType(ObjCObjectType *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,
83788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                         SourceLocation &Loc);
84083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    void ImportDeclContext(DeclContext *FromDC);
8596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
8673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
8789cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    Decl *VisitDecl(Decl *D);
88788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    Decl *VisitNamespaceDecl(NamespaceDecl *D);
899e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
9036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
9196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *VisitRecordDecl(RecordDecl *D);
9236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
93a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D);
94c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
95c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
96c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
97c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
9896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
992e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
100089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
1012cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
102a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Decl *VisitParmVarDecl(ParmVarDecl *D);
103c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
104b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
1052e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
106a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
107e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
1082b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
109a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Decl *VisitObjCClassDecl(ObjCClassDecl *D);
110a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
1114800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    // Importing statements
1124800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Stmt *VisitStmt(Stmt *S);
1134800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
1144800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    // Importing expressions
1154800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Expr *VisitExpr(Expr *E);
116440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    Expr *VisitDeclRefExpr(DeclRefExpr *E);
1174800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Expr *VisitIntegerLiteral(IntegerLiteral *E);
118b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor    Expr *VisitCharacterLiteral(CharacterLiteral *E);
119f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitParenExpr(ParenExpr *E);
120f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitUnaryOperator(UnaryOperator *E);
121bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    Expr *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
122f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitBinaryOperator(BinaryOperator *E);
123f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
12436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
125008847a70ab122a99911149199855060fb3753b4Douglas Gregor    Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
126f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
127f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    bool ImportCasePath(CastExpr *E, CXXCastPath &Path);
1281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  };
1291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
1301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//----------------------------------------------------------------------------
13273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor// Structural Equivalence
13373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor//----------------------------------------------------------------------------
13473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
13573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregornamespace {
13673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  struct StructuralEquivalenceContext {
13773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief AST contexts for which we are checking structural equivalence.
13873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    ASTContext &C1, &C2;
13973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
14073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Diagnostic object used to emit diagnostics.
14173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Diagnostic &Diags;
14273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
14373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief The set of "tentative" equivalences between two canonical
14473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// declarations, mapping from a declaration in the first context to the
14573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// declaration in the second context that we believe to be equivalent.
14673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
14773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
14873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Queue of declarations in the first context whose equivalence
14973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// with a declaration in the second context still needs to be verified.
15073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    std::deque<Decl *> DeclsToCheck;
15173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
152ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    /// \brief Declaration (from, to) pairs that are known not to be equivalent
153ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    /// (which we have already complained about).
154ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
155ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
15673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Whether we're being strict about the spelling of types when
15773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// unifying two types.
15873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool StrictTypeSpelling;
15973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
16073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
16173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                 Diagnostic &Diags,
162ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor               llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
16373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                 bool StrictTypeSpelling = false)
164ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      : C1(C1), C2(C2), Diags(Diags), NonEquivalentDecls(NonEquivalentDecls),
165ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        StrictTypeSpelling(StrictTypeSpelling) { }
16673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
16773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Determine whether the two declarations are structurally
16873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// equivalent.
16973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
17073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
17173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Determine whether the two types are structurally equivalent.
17273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructurallyEquivalent(QualType T1, QualType T2);
17373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
17473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  private:
17573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Finish checking all of the structural equivalences.
17673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    ///
17773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \returns true if an error occurred, false otherwise.
17873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool Finish();
17973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
18073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  public:
18173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
18273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return Diags.Report(FullSourceLoc(Loc, C1.getSourceManager()), DiagID);
18373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
18473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
18573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
18673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return Diags.Report(FullSourceLoc(Loc, C2.getSourceManager()), DiagID);
18773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
18873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  };
18973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
19073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
19173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
19273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     QualType T1, QualType T2);
19373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
19473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Decl *D1, Decl *D2);
19573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
19673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine if two APInts have the same value, after zero-extending
19773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// one of them (if needed!) to ensure that the bit-widths match.
19873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
19973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() == I2.getBitWidth())
20073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == I2;
20173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
20273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() > I2.getBitWidth())
20373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == llvm::APInt(I2).zext(I1.getBitWidth());
20473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
20573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return llvm::APInt(I1).zext(I2.getBitWidth()) == I2;
20673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
20773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
20873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine if two APSInts have the same value, zero- or sign-extending
20973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// as needed.
21073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
21173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
21273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == I2;
21373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
21473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Check for a bit-width mismatch.
21573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() > I2.getBitWidth())
21673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return IsSameValue(I1, llvm::APSInt(I2).extend(I1.getBitWidth()));
21773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  else if (I2.getBitWidth() > I1.getBitWidth())
21873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return IsSameValue(llvm::APSInt(I1).extend(I2.getBitWidth()), I2);
21973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
22073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // We have a signedness mismatch. Turn the signed value into an unsigned
22173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // value.
22273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.isSigned()) {
22373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (I1.isNegative())
22473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
22573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
22673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return llvm::APSInt(I1, true) == I2;
22773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
22873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
22973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I2.isNegative())
23073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
23173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
23273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return I1 == llvm::APSInt(I2, true);
23373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
23473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
23573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two expressions.
23673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
23773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Expr *E1, Expr *E2) {
23873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!E1 || !E2)
23973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return E1 == E2;
24073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Actually perform a structural comparison!
24273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
24373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
24473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two identifiers are equivalent.
24673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
24773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const IdentifierInfo *Name2) {
24873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!Name1 || !Name2)
24973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return Name1 == Name2;
25073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
25173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return Name1->getName() == Name2->getName();
25273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
25373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
25473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two nested-name-specifiers are equivalent.
25573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
25673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     NestedNameSpecifier *NNS1,
25773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     NestedNameSpecifier *NNS2) {
25873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Implement!
25973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
26073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
26173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
26273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two template arguments are equivalent.
26373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
26473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const TemplateArgument &Arg1,
26573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const TemplateArgument &Arg2) {
26673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Implement!
26773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
26873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
26973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
27073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence for the common part of array
27173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// types.
27273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
27373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                          const ArrayType *Array1,
27473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                          const ArrayType *Array2) {
27573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!IsStructurallyEquivalent(Context,
27673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                Array1->getElementType(),
27773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                Array2->getElementType()))
27873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
27973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Array1->getSizeModifier() != Array2->getSizeModifier())
28073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
28173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
28273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
28373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
28473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
28573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
28673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
28773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two types.
28873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
28973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     QualType T1, QualType T2) {
29073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (T1.isNull() || T2.isNull())
29173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return T1.isNull() && T2.isNull();
29273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
29373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!Context.StrictTypeSpelling) {
29473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // We aren't being strict about token-to-token equivalence of types,
29573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // so map down to the canonical type.
29673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    T1 = Context.C1.getCanonicalType(T1);
29773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    T2 = Context.C2.getCanonicalType(T2);
29873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
29973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
30073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (T1.getQualifiers() != T2.getQualifiers())
30173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
30273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
303ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  Type::TypeClass TC = T1->getTypeClass();
304ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
305ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T1->getTypeClass() != T2->getTypeClass()) {
306ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // Compare function types with prototypes vs. without prototypes as if
307ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // both did not have prototypes.
308ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (T1->getTypeClass() == Type::FunctionProto &&
309ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        T2->getTypeClass() == Type::FunctionNoProto)
310ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      TC = Type::FunctionNoProto;
311ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    else if (T1->getTypeClass() == Type::FunctionNoProto &&
312ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor             T2->getTypeClass() == Type::FunctionProto)
313ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      TC = Type::FunctionNoProto;
314ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    else
315ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      return false;
316ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  }
31773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
318ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  switch (TC) {
319ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  case Type::Builtin:
32073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Deal with Char_S/Char_U.
32173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
32273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
32373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
32473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
32573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Complex:
32673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
32773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<ComplexType>(T1)->getElementType(),
32873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<ComplexType>(T2)->getElementType()))
32973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
33073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
33173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
33273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Pointer:
33373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
33473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<PointerType>(T1)->getPointeeType(),
33573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<PointerType>(T2)->getPointeeType()))
33673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
33773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
33873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
33973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::BlockPointer:
34073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
34173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<BlockPointerType>(T1)->getPointeeType(),
34273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<BlockPointerType>(T2)->getPointeeType()))
34373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
34473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
34573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
34673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::LValueReference:
34773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::RValueReference: {
34873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ReferenceType *Ref1 = cast<ReferenceType>(T1);
34973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ReferenceType *Ref2 = cast<ReferenceType>(T2);
35073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
35173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
35273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Ref1->isInnerRef() != Ref2->isInnerRef())
35373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
35473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
35573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ref1->getPointeeTypeAsWritten(),
35673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ref2->getPointeeTypeAsWritten()))
35773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
35873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
35973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
36073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
36173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::MemberPointer: {
36273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
36373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
36473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
36573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  MemPtr1->getPointeeType(),
36673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  MemPtr2->getPointeeType()))
36773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
36873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
36973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(MemPtr1->getClass(), 0),
37073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(MemPtr2->getClass(), 0)))
37173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
37273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
37373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
37473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
37573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ConstantArray: {
37673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
37773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
37873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsSameValue(Array1->getSize(), Array2->getSize()))
37973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
38073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
38173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
38273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
38373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
38473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
38573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
38673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::IncompleteArray:
38773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context,
38873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                       cast<ArrayType>(T1),
38973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                       cast<ArrayType>(T2)))
39073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
39173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
39273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
39373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::VariableArray: {
39473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
39573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
39673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
39773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Array1->getSizeExpr(), Array2->getSizeExpr()))
39873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
39973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
40073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
40173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
40273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
40373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
40473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
40573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
40673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::DependentSizedArray: {
40773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
40873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
40973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
41073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Array1->getSizeExpr(), Array2->getSizeExpr()))
41173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
41273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
41373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
41473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
41573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
41673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
41773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
41873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
41973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::DependentSizedExtVector: {
42073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedExtVectorType *Vec1
42173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<DependentSizedExtVectorType>(T1);
42273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedExtVectorType *Vec2
42373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<DependentSizedExtVectorType>(T2);
42473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
42573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getSizeExpr(), Vec2->getSizeExpr()))
42673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
42773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
42873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getElementType(),
42973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec2->getElementType()))
43073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
43173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
43273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
43373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
43473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Vector:
43573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ExtVector: {
43673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VectorType *Vec1 = cast<VectorType>(T1);
43773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VectorType *Vec2 = cast<VectorType>(T2);
43873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
43973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getElementType(),
44073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec2->getElementType()))
44173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
44273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Vec1->getNumElements() != Vec2->getNumElements())
44373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
444788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    if (Vec1->getAltiVecSpecific() != Vec2->getAltiVecSpecific())
44573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
4460e12b44081c6395a6d60a05a85a6012f7bb23b16Douglas Gregor    break;
44773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
44873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
44973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::FunctionProto: {
45073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
45173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
45273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getNumArgs() != Proto2->getNumArgs())
45373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
45473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
45573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
45673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto1->getArgType(I),
45773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto2->getArgType(I)))
45873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
45973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
46073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->isVariadic() != Proto2->isVariadic())
46173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
46373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
46573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
46773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
46973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
47073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto1->getExceptionType(I),
47173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto2->getExceptionType(I)))
47273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
47373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
47473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
47573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
47673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
47773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Fall through to check the bits common with FunctionNoProtoType.
47873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
47973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
48073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::FunctionNoProto: {
48173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionType *Function1 = cast<FunctionType>(T1);
48273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionType *Function2 = cast<FunctionType>(T2);
48373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
48473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Function1->getResultType(),
48573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Function2->getResultType()))
48673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
487264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      if (Function1->getExtInfo() != Function2->getExtInfo())
488264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola        return false;
48973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
49073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
49173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
49273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::UnresolvedUsing:
49373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
49473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<UnresolvedUsingType>(T1)->getDecl(),
49573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<UnresolvedUsingType>(T2)->getDecl()))
49673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
49773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
49873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
49973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
50073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Typedef:
50173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
50273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypedefType>(T1)->getDecl(),
50373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypedefType>(T2)->getDecl()))
50473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
50573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
50673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
50773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TypeOfExpr:
50873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
50973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
51073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
51173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
51273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
51373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
51473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TypeOf:
51573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
51673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypeOfType>(T1)->getUnderlyingType(),
51773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypeOfType>(T2)->getUnderlyingType()))
51873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
51973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
52073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
52173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Decltype:
52273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
52373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<DecltypeType>(T1)->getUnderlyingExpr(),
52473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<DecltypeType>(T2)->getUnderlyingExpr()))
52573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
52673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
52773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
52873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Record:
52973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Enum:
53073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
53173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TagType>(T1)->getDecl(),
53273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TagType>(T2)->getDecl()))
53373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
53473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
535465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
53673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TemplateTypeParm: {
53773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
53873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
53973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->getDepth() != Parm2->getDepth())
54073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
54173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->getIndex() != Parm2->getIndex())
54273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
54373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->isParameterPack() != Parm2->isParameterPack())
54473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
54573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
54673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Names of template type parameters are never significant.
54773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
54873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
54973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
55073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::SubstTemplateTypeParm: {
55173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const SubstTemplateTypeParmType *Subst1
55273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<SubstTemplateTypeParmType>(T1);
55373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const SubstTemplateTypeParmType *Subst2
55473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<SubstTemplateTypeParmType>(T2);
55573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
55673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Subst1->getReplacedParameter(), 0),
55773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Subst2->getReplacedParameter(), 0)))
55873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
55973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
56073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Subst1->getReplacementType(),
56173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Subst2->getReplacementType()))
56273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
56373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
56473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
56573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
56673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TemplateSpecialization: {
56773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateSpecializationType *Spec1
56873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<TemplateSpecializationType>(T1);
56973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateSpecializationType *Spec2
57073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<TemplateSpecializationType>(T2);
57173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
57273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Spec1->getTemplateName(),
57373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Spec2->getTemplateName()))
57473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
57573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Spec1->getNumArgs() != Spec2->getNumArgs())
57673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
57773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
57873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
57973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Spec1->getArg(I), Spec2->getArg(I)))
58073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
58173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
58273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
58373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
58473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
585465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Type::Elaborated: {
586465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
587465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
588465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    // CHECKME: what if a keyword is ETK_None or ETK_typename ?
589465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Elab1->getKeyword() != Elab2->getKeyword())
590465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return false;
59173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
592465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab1->getQualifier(),
593465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab2->getQualifier()))
59473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
59573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
596465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab1->getNamedType(),
597465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab2->getNamedType()))
59873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
59973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
60073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
60173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
6023cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  case Type::InjectedClassName: {
6033cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
6043cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
6053cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (!IsStructurallyEquivalent(Context,
60631f17ecbef57b5679c017c375db330546b7b5145John McCall                                  Inj1->getInjectedSpecializationType(),
60731f17ecbef57b5679c017c375db330546b7b5145John McCall                                  Inj2->getInjectedSpecializationType()))
6083cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      return false;
6093cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    break;
6103cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
6113cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
6124714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  case Type::DependentName: {
6134714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    const DependentNameType *Typename1 = cast<DependentNameType>(T1);
6144714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    const DependentNameType *Typename2 = cast<DependentNameType>(T2);
61573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
61673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename1->getQualifier(),
61773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename2->getQualifier()))
61873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
61973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
62073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename2->getIdentifier()))
62173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
62273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
62373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
62473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
62573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
62633500955d731c73717af52088b7fc0e7a85681e7John McCall  case Type::DependentTemplateSpecialization: {
62733500955d731c73717af52088b7fc0e7a85681e7John McCall    const DependentTemplateSpecializationType *Spec1 =
62833500955d731c73717af52088b7fc0e7a85681e7John McCall      cast<DependentTemplateSpecializationType>(T1);
62933500955d731c73717af52088b7fc0e7a85681e7John McCall    const DependentTemplateSpecializationType *Spec2 =
63033500955d731c73717af52088b7fc0e7a85681e7John McCall      cast<DependentTemplateSpecializationType>(T2);
63133500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!IsStructurallyEquivalent(Context,
63233500955d731c73717af52088b7fc0e7a85681e7John McCall                                  Spec1->getQualifier(),
63333500955d731c73717af52088b7fc0e7a85681e7John McCall                                  Spec2->getQualifier()))
63433500955d731c73717af52088b7fc0e7a85681e7John McCall      return false;
63533500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
63633500955d731c73717af52088b7fc0e7a85681e7John McCall                                  Spec2->getIdentifier()))
63733500955d731c73717af52088b7fc0e7a85681e7John McCall      return false;
63833500955d731c73717af52088b7fc0e7a85681e7John McCall    if (Spec1->getNumArgs() != Spec2->getNumArgs())
63933500955d731c73717af52088b7fc0e7a85681e7John McCall      return false;
64033500955d731c73717af52088b7fc0e7a85681e7John McCall    for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
64133500955d731c73717af52088b7fc0e7a85681e7John McCall      if (!IsStructurallyEquivalent(Context,
64233500955d731c73717af52088b7fc0e7a85681e7John McCall                                    Spec1->getArg(I), Spec2->getArg(I)))
64333500955d731c73717af52088b7fc0e7a85681e7John McCall        return false;
64433500955d731c73717af52088b7fc0e7a85681e7John McCall    }
64533500955d731c73717af52088b7fc0e7a85681e7John McCall    break;
64633500955d731c73717af52088b7fc0e7a85681e7John McCall  }
64733500955d731c73717af52088b7fc0e7a85681e7John McCall
64873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ObjCInterface: {
64973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
65073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
65173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
65273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Iface1->getDecl(), Iface2->getDecl()))
65373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
654c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    break;
655c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  }
656c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
657c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject: {
658c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
659c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
660c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (!IsStructurallyEquivalent(Context,
661c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                  Obj1->getBaseType(),
662c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                  Obj2->getBaseType()))
663c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return false;
664c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
66573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
666c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
66773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
668c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Obj1->getProtocol(I),
669c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Obj2->getProtocol(I)))
67073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
67173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
67273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
67373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
67473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
67573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ObjCObjectPointer: {
67673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
67773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
67873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
67973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ptr1->getPointeeType(),
68073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ptr2->getPointeeType()))
68173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
68273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
68373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
68473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
68573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  } // end switch
68673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
68773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
68873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
68973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
69073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two records.
69173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
69273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     RecordDecl *D1, RecordDecl *D2) {
69373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (D1->isUnion() != D2->isUnion()) {
69473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
69573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
69673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
69773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << D1->getDeclName() << (unsigned)D1->getTagKind();
69873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
69973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
70073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
701ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Compare the definitions of these two records. If either or both are
702ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // incomplete, we assume that they are equivalent.
703ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  D1 = D1->getDefinition();
704ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  D2 = D2->getDefinition();
705ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (!D1 || !D2)
706ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return true;
707ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
70873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
70973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
71073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
71173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
71273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
71373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
71473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << D2CXX->getNumBases();
71573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
71673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << D1CXX->getNumBases();
71773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
71873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
71973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
72073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      // Check the base classes.
72173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
72273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                           BaseEnd1 = D1CXX->bases_end(),
72373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                Base2 = D2CXX->bases_begin();
72473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor           Base1 != BaseEnd1;
72573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor           ++Base1, ++Base2) {
72673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!IsStructurallyEquivalent(Context,
72773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                      Base1->getType(), Base2->getType())) {
72873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
72973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Context.C2.getTypeDeclType(D2);
73073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
73173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->getType()
73273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->getSourceRange();
73373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
73473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getType()
73573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getSourceRange();
73673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          return false;
73773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        }
73873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
73973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check virtual vs. non-virtual inheritance mismatch.
74073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (Base1->isVirtual() != Base2->isVirtual()) {
74173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
74273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Context.C2.getTypeDeclType(D2);
74373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(Base2->getSourceRange().getBegin(),
74473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                        diag::note_odr_virtual_base)
74573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->isVirtual() << Base2->getSourceRange();
74673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
74773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->isVirtual()
74873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getSourceRange();
74973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          return false;
75073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        }
75173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
75273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    } else if (D1CXX->getNumBases() > 0) {
75373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
75473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
75573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
75673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
75773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Base1->getType()
75873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Base1->getSourceRange();
75973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
76073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
76173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
76273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
76373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
76473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Check the fields for consistency.
76573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
76673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                             Field2End = D2->field_end();
76773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
76873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Field1End = D1->field_end();
76973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       Field1 != Field1End;
77073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       ++Field1, ++Field2) {
77173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field2 == Field2End) {
77273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
77373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
77473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Field1->getLocation(), diag::note_odr_field)
77573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName() << Field1->getType();
77673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
77773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
77873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
77973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
78073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
78173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Field1->getType(), Field2->getType())) {
78273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
78373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
78473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(Field2->getLocation(), diag::note_odr_field)
78573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field2->getDeclName() << Field2->getType();
78673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Field1->getLocation(), diag::note_odr_field)
78773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName() << Field1->getType();
78873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
78973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
79073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
79173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field1->isBitField() != Field2->isBitField()) {
79273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
79373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
79473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (Field1->isBitField()) {
79573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        llvm::APSInt Bits;
79673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
79773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
79873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field1->getDeclName() << Field1->getType()
79973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits.toString(10, false);
80073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
80173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName();
80273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
80373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        llvm::APSInt Bits;
80473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
80573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
80673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName() << Field2->getType()
80773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits.toString(10, false);
80873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(),
80973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                          diag::note_odr_not_bit_field)
81073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName();
81173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
81273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
81373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
81473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
81573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field1->isBitField()) {
81673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      // Make sure that the bit-fields are the same length.
81773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      llvm::APSInt Bits1, Bits2;
81873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
81973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
82073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
82173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
82273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
82373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsSameValue(Bits1, Bits2)) {
82473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
82573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Context.C2.getTypeDeclType(D2);
82673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
82773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName() << Field2->getType()
82873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits2.toString(10, false);
82973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
83073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field1->getDeclName() << Field1->getType()
83173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits1.toString(10, false);
83273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
83373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
83473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
83573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
83673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
83773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Field2 != Field2End) {
83873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
83973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
84073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(Field2->getLocation(), diag::note_odr_field)
84173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Field2->getDeclName() << Field2->getType();
84273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
84373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
84473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
84573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
84673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
84773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
84873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
84973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two enums.
85073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
85173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     EnumDecl *D1, EnumDecl *D2) {
85273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
85373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                             EC2End = D2->enumerator_end();
85473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
85573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  EC1End = D1->enumerator_end();
85673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       EC1 != EC1End; ++EC1, ++EC2) {
85773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (EC2 == EC2End) {
85873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
85973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
86073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
86173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getDeclName()
86273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getInitVal().toString(10);
86373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
86473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
86573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
86673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
86773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::APSInt Val1 = EC1->getInitVal();
86873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::APSInt Val2 = EC2->getInitVal();
86973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsSameValue(Val1, Val2) ||
87073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
87173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
87273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
87373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
87473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC2->getDeclName()
87573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC2->getInitVal().toString(10);
87673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
87773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getDeclName()
87873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getInitVal().toString(10);
87973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
88073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
88173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
88273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
88373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (EC2 != EC2End) {
88473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
88573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
88673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
88773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << EC2->getDeclName()
88873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << EC2->getInitVal().toString(10);
88973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
89073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
89173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
89273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
89373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
89473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
89573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
89673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two declarations.
89773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
89873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Decl *D1, Decl *D2) {
89973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Check for known structural equivalences via a callback of some sort.
90073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
901ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Check whether we already know that these two declarations are not
902ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // structurally equivalent.
903ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
904ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                      D2->getCanonicalDecl())))
905ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return false;
906ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
90773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Determine whether we've already produced a tentative equivalence for D1.
90873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
90973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (EquivToD1)
91073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return EquivToD1 == D2->getCanonicalDecl();
91173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
91273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Produce a tentative equivalence D1 <-> D2, which will be checked later.
91373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EquivToD1 = D2->getCanonicalDecl();
91473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
91573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
91673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
91773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
91873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
91973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                            Decl *D2) {
92073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!::IsStructurallyEquivalent(*this, D1, D2))
92173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
92273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
92373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return !Finish();
92473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
92573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
92673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
92773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                            QualType T2) {
92873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!::IsStructurallyEquivalent(*this, T1, T2))
92973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
93073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
93173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return !Finish();
93273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
93373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
93473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::Finish() {
93573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  while (!DeclsToCheck.empty()) {
93673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Check the next declaration.
93773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Decl *D1 = DeclsToCheck.front();
93873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DeclsToCheck.pop_front();
93973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
94073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Decl *D2 = TentativeEquivalences[D1];
94173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    assert(D2 && "Unrecorded tentative equivalence?");
94273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
943ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    bool Equivalent = true;
944ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
94573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Switch on all declaration kinds. For now, we're just going to
94673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // check the obvious ones.
94773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
94873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
94973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check for equivalent structure names.
95073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name1 = Record1->getIdentifier();
95173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name1 && Record1->getTypedefForAnonDecl())
95273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
95373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name2 = Record2->getIdentifier();
95473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name2 && Record2->getTypedefForAnonDecl())
95573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
956ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (!::IsStructurallyEquivalent(Name1, Name2) ||
957ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this, Record1, Record2))
958ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
95973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
96073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Record/non-record mismatch.
961ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
96273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
963ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
96473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
96573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check for equivalent enum names.
96673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name1 = Enum1->getIdentifier();
96773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name1 && Enum1->getTypedefForAnonDecl())
96873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
96973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name2 = Enum2->getIdentifier();
97073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name2 && Enum2->getTypedefForAnonDecl())
97173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
972ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (!::IsStructurallyEquivalent(Name1, Name2) ||
973ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this, Enum1, Enum2))
974ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
97573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
97673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Enum/non-enum mismatch
977ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
97873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
979ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
98073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
98173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
982ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                        Typedef2->getIdentifier()) ||
983ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this,
98473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        Typedef1->getUnderlyingType(),
98573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        Typedef2->getUnderlyingType()))
986ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
98773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
98873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Typedef/non-typedef mismatch.
989ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
99073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
99173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
992ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
993ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (!Equivalent) {
994ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      // Note that these two declarations are not equivalent (and we already
995ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      // know about it).
996ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
997ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                               D2->getCanonicalDecl()));
998ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      return true;
999ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    }
100073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Check other declaration kinds!
100173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
100273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
100373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return false;
100473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
100573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
100673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor//----------------------------------------------------------------------------
10071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor// Import Types
10081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//----------------------------------------------------------------------------
10091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
101089cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas GregorQualType ASTNodeImporter::VisitType(Type *T) {
101189cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor  Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
101289cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    << T->getTypeClassName();
101389cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor  return QualType();
101489cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor}
101589cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor
10161b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
10171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  switch (T->getKind()) {
10181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Void: return Importer.getToContext().VoidTy;
10191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Bool: return Importer.getToContext().BoolTy;
10201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char_U:
10221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // The context we're importing from has an unsigned 'char'. If we're
10231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // importing into a context with a signed 'char', translate to
10241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // 'unsigned char' instead.
10251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (Importer.getToContext().getLangOptions().CharIsSigned)
10261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return Importer.getToContext().UnsignedCharTy;
10271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().CharTy;
10291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
10311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char16:
10331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++!
10341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().Char16Ty;
10351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char32:
10371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++!
10381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().Char32Ty;
10391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
10411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
10421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
10431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ULongLong:
10441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().UnsignedLongLongTy;
10451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
10461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char_S:
10481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // The context we're importing from has an unsigned 'char'. If we're
10491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // importing into a context with a signed 'char', translate to
10501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // 'unsigned char' instead.
10511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Importer.getToContext().getLangOptions().CharIsSigned)
10521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return Importer.getToContext().SignedCharTy;
10531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().CharTy;
10551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
10571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::WChar:
10581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: If not in C++, shall we translate to the C equivalent of
10591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // wchar_t?
10601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().WCharTy;
10611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Short : return Importer.getToContext().ShortTy;
10631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Int : return Importer.getToContext().IntTy;
10641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Long : return Importer.getToContext().LongTy;
10651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
10661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
10671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Float: return Importer.getToContext().FloatTy;
10681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Double: return Importer.getToContext().DoubleTy;
10691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
10701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::NullPtr:
10721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++0x!
10731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().NullPtrTy;
10741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
10761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
10771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UndeducedAuto:
10781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++0x!
10791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().UndeducedAutoTy;
10801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCId:
10821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports Objective-C!
10831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinIdTy;
10841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCClass:
10861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinClassTy;
10871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCSel:
10891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinSelTy;
10901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
10911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return QualType();
10931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
10941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10951b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
10961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
10971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
10981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
10991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getComplexType(ToElementType);
11011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11031b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitPointerType(PointerType *T) {
11041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
11051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
11061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getPointerType(ToPointeeType);
11091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11111b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
11121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for blocks support in "to" context.
11131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
11141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
11151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getBlockPointerType(ToPointeeType);
11181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11201b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
11211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++ support in "to" context.
11221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
11231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
11241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getLValueReferenceType(ToPointeeType);
11271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11291b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
11301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++0x support in "to" context.
11311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
11321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
11331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getRValueReferenceType(ToPointeeType);
11361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11381b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
11391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++ support in "to" context.
11401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
11411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
11421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
11451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getMemberPointerType(ToPointeeType,
11461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      ClassType.getTypePtr());
11471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11491b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
11501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getConstantArrayType(ToElementType,
11551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSize(),
11561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSizeModifier(),
11571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->getIndexTypeCVRQualifiers());
11581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11601b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
11611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getIncompleteArrayType(ToElementType,
11661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                        T->getSizeModifier(),
11671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                T->getIndexTypeCVRQualifiers());
11681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11701b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
11711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *Size = Importer.Import(T->getSizeExpr());
11761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!Size)
11771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  SourceRange Brackets = Importer.Import(T->getBracketsRange());
11801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getVariableArrayType(ToElementType, Size,
11811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSizeModifier(),
11821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                T->getIndexTypeCVRQualifiers(),
11831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      Brackets);
11841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11861b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitVectorType(VectorType *T) {
11871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getVectorType(ToElementType,
11921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->getNumElements(),
1193788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner                                               T->getAltiVecSpecific());
11941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11961b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
11971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getExtVectorType(ToElementType,
12021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                  T->getNumElements());
12031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12051b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
12061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: What happens if we're importing a function without a prototype
12071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // into C++? Should we make it variadic?
12081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToResultType = Importer.Import(T->getResultType());
12091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToResultType.isNull())
12101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
1211264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola
12121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getFunctionNoProtoType(ToResultType,
1213264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                        T->getExtInfo());
12141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12161b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
12171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToResultType = Importer.Import(T->getResultType());
12181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToResultType.isNull())
12191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Import argument types
12221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<QualType, 4> ArgTypes;
12231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
12241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                         AEnd = T->arg_type_end();
12251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       A != AEnd; ++A) {
12261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType ArgType = Importer.Import(*A);
12271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (ArgType.isNull())
12281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
12291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ArgTypes.push_back(ArgType);
12301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
12311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Import exception types
12331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<QualType, 4> ExceptionTypes;
12341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (FunctionProtoType::exception_iterator E = T->exception_begin(),
12351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                          EEnd = T->exception_end();
12361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       E != EEnd; ++E) {
12371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType ExceptionType = Importer.Import(*E);
12381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (ExceptionType.isNull())
12391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
12401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ExceptionTypes.push_back(ExceptionType);
12411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
12421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
12441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 ArgTypes.size(),
12451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->isVariadic(),
12461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->getTypeQuals(),
12471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->hasExceptionSpec(),
12481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->hasAnyExceptionSpec(),
12491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 ExceptionTypes.size(),
12501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 ExceptionTypes.data(),
1251264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                 T->getExtInfo());
12521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12541b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
12551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  TypedefDecl *ToDecl
12561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
12571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
12581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeDeclType(ToDecl);
12611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12631b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
12641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
12651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToExpr)
12661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeOfExprType(ToExpr);
12691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12711b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
12721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
12731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToUnderlyingType.isNull())
12741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeOfType(ToUnderlyingType);
12771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12791b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
12801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
12811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToExpr)
12821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getDecltypeType(ToExpr);
12851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12871b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitRecordType(RecordType *T) {
12881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  RecordDecl *ToDecl
12891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
12901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
12911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTagDeclType(ToDecl);
12941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12961b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitEnumType(EnumType *T) {
12971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  EnumDecl *ToDecl
12981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
12991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
13001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTagDeclType(ToDecl);
13031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13051b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
1306465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *ToQualifier = 0;
1307465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  // Note: the qualifier in an ElaboratedType is optional.
1308465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T->getQualifier()) {
1309465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ToQualifier = Importer.Import(T->getQualifier());
1310465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (!ToQualifier)
1311465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return QualType();
1312465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
13131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToNamedType = Importer.Import(T->getNamedType());
13151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToNamedType.isNull())
13161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1318465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Importer.getToContext().getElaboratedType(T->getKeyword(),
1319465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                                   ToQualifier, ToNamedType);
13201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13221b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
13231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  ObjCInterfaceDecl *Class
13241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
13251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!Class)
13261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1328c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCInterfaceType(Class);
1329c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
1330c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
1331c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) {
1332c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  QualType ToBaseType = Importer.Import(T->getBaseType());
1333c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ToBaseType.isNull())
1334c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType();
1335c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
13361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
1337c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator P = T->qual_begin(),
13381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                     PEnd = T->qual_end();
13391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       P != PEnd; ++P) {
13401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ObjCProtocolDecl *Protocol
13411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
13421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Protocol)
13431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
13441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    Protocols.push_back(Protocol);
13451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
13461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1347c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCObjectType(ToBaseType,
1348c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                                   Protocols.data(),
1349c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                                   Protocols.size());
13501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13521b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
13531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
13541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
13551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1357c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
13581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1360089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor//----------------------------------------------------------------------------
1361089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor// Import Declarations
1362089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor//----------------------------------------------------------------------------
1363a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregorbool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1364a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      DeclContext *&LexicalDC,
1365a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      DeclarationName &Name,
1366a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      SourceLocation &Loc) {
1367089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Import the context of this declaration.
1368a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DC = Importer.ImportContext(D->getDeclContext());
1369089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  if (!DC)
1370a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return true;
1371a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1372a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  LexicalDC = DC;
13739bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
13749bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
13759bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    if (!LexicalDC)
1376a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      return true;
13779bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  }
1378a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1379089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Import the name of this declaration.
1380a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Name = Importer.Import(D->getDeclName());
1381089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  if (D->getDeclName() && !Name)
1382a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return true;
1383a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1384a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the location of this declaration.
1385a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Loc = Importer.Import(D->getLocation());
1386a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  return false;
1387a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1388a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1389083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregorvoid ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1390083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1391083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor                               FromEnd = FromDC->decls_end();
1392083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor       From != FromEnd;
1393083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor       ++From)
1394083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    Importer.Import(*From);
1395083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor}
1396083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor
139796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregorbool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
139873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        RecordDecl *ToRecord) {
1399bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
140073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Importer.getToContext(),
1401ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getDiags(),
1402ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getNonEquivalentDecls());
1403bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
140496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
140596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
140636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregorbool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
1407bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
140873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Importer.getToContext(),
1409ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getDiags(),
1410ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getNonEquivalentDecls());
1411bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
141236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
141336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
1414a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitDecl(Decl *D) {
1415a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1416a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    << D->getDeclKindName();
1417a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  return 0;
1418a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1419a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1420788c62d1e87bfb596078817237f672a5f000999aDouglas GregorDecl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1421788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  // Import the major distinguishing characteristics of this namespace.
1422788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  DeclContext *DC, *LexicalDC;
1423788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  DeclarationName Name;
1424788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  SourceLocation Loc;
1425788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1426788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    return 0;
1427788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1428788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  NamespaceDecl *MergeWithNamespace = 0;
1429788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (!Name) {
1430788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // This is an anonymous namespace. Adopt an existing anonymous
1431788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // namespace if we can.
1432788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // FIXME: Not testable.
1433788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1434788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      MergeWithNamespace = TU->getAnonymousNamespace();
1435788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    else
1436788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1437788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  } else {
1438788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1439788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1440788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor         Lookup.first != Lookup.second;
1441788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor         ++Lookup.first) {
14420d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
1443788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        continue;
1444788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1445788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1446788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        MergeWithNamespace = FoundNS;
1447788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        ConflictingDecls.clear();
1448788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        break;
1449788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      }
1450788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1451788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
1452788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
1453788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1454788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (!ConflictingDecls.empty()) {
14550d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
1456788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                         ConflictingDecls.data(),
1457788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                         ConflictingDecls.size());
1458788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
1459788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  }
1460788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1461788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  // Create the "to" namespace, if needed.
1462788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  NamespaceDecl *ToNamespace = MergeWithNamespace;
1463788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (!ToNamespace) {
1464788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1465788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                        Name.getAsIdentifierInfo());
1466788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    ToNamespace->setLexicalDeclContext(LexicalDC);
1467788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    LexicalDC->addDecl(ToNamespace);
1468788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1469788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // If this is an anonymous namespace, register it as the anonymous
1470788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // namespace within its context.
1471788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (!Name) {
1472788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1473788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        TU->setAnonymousNamespace(ToNamespace);
1474788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      else
1475788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1476788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
1477788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  }
1478788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  Importer.Imported(D, ToNamespace);
1479788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1480788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  ImportDeclContext(D);
1481788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1482788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  return ToNamespace;
1483788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor}
1484788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
14859e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas GregorDecl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
14869e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // Import the major distinguishing characteristics of this typedef.
14879e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  DeclContext *DC, *LexicalDC;
14889e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  DeclarationName Name;
14899e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  SourceLocation Loc;
14909e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
14919e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    return 0;
14929e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
14939e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // If this typedef is not in block scope, determine whether we've
14949e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // seen a typedef with the same name (that we can merge with) or any
14959e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // other entity by that name (which name lookup could conflict with).
14969e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  if (!DC->isFunctionOrMethod()) {
14979e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
14989e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
14999e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
15009e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor         Lookup.first != Lookup.second;
15019e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor         ++Lookup.first) {
15029e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
15039e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor        continue;
15049e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
1505ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1506ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                            FoundTypedef->getUnderlyingType()))
15075ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor          return Importer.Imported(D, FoundTypedef);
15089e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      }
15099e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
15109e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
15119e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    }
15129e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
15139e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    if (!ConflictingDecls.empty()) {
15149e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
15159e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                         ConflictingDecls.data(),
15169e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                         ConflictingDecls.size());
15179e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (!Name)
15189e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor        return 0;
15199e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    }
15209e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  }
15219e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
1522ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the underlying type of this typedef;
1523ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getUnderlyingType());
1524ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
1525ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
1526ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
15279e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // Create the new typedef node.
15289e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
15299e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
15309e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                               Loc, Name.getAsIdentifierInfo(),
15319e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                               TInfo);
1532325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToTypedef->setAccess(D->getAccess());
15339e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  ToTypedef->setLexicalDeclContext(LexicalDC);
15345ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToTypedef);
15359e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  LexicalDC->addDecl(ToTypedef);
1536ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
15379e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  return ToTypedef;
15389e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor}
15399e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
154036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorDecl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
154136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the major distinguishing characteristics of this enum.
154236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclContext *DC, *LexicalDC;
154336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName Name;
154436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  SourceLocation Loc;
154536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
154636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
154736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
154836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Figure out what enum name we're looking for.
154936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  unsigned IDNS = Decl::IDNS_Tag;
155036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName SearchName = Name;
155136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!SearchName && D->getTypedefForAnonDecl()) {
155236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
155336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    IDNS = Decl::IDNS_Ordinary;
155436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  } else if (Importer.getToContext().getLangOptions().CPlusPlus)
155536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    IDNS |= Decl::IDNS_Ordinary;
155636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
155736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // We may already have an enum of the same name; try to find and match it.
155836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!DC->isFunctionOrMethod() && SearchName) {
155936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
156036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
156136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         Lookup.first != Lookup.second;
156236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++Lookup.first) {
156336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
156436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        continue;
156536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
156636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Decl *Found = *Lookup.first;
156736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
156836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
156936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor          Found = Tag->getDecl();
157036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      }
157136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
157236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
15735ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor        if (IsStructuralMatch(D, FoundEnum))
15745ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor          return Importer.Imported(D, FoundEnum);
157536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      }
157636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
157736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
157836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
157936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
158036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (!ConflictingDecls.empty()) {
158136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
158236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.data(),
158336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.size());
158436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
158536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
158636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
158736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Create the enum declaration.
158873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
158936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                      Name.getAsIdentifierInfo(),
159036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                      Importer.Import(D->getTagKeywordLoc()),
159136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                      0);
1592b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
1593b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (D->getQualifier()) {
1594b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1595b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1596b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    D2->setQualifierInfo(NNS, NNSRange);
1597b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1598325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  D2->setAccess(D->getAccess());
159973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  D2->setLexicalDeclContext(LexicalDC);
160073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Importer.Imported(D, D2);
160173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  LexicalDC->addDecl(D2);
160236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
160336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the integer type.
160436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  QualType ToIntegerType = Importer.Import(D->getIntegerType());
160536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (ToIntegerType.isNull())
160636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
160773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  D2->setIntegerType(ToIntegerType);
160836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
160936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the definition
161036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (D->isDefinition()) {
161136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
161236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (T.isNull())
161336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      return 0;
161436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
161536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    QualType ToPromotionType = Importer.Import(D->getPromotionType());
161636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (ToPromotionType.isNull())
161736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      return 0;
161836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
161973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->startDefinition();
1620083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    ImportDeclContext(D);
16211b5a618c59025898806160ed5e7f0ff5bb79e482John McCall
16221b5a618c59025898806160ed5e7f0ff5bb79e482John McCall    // FIXME: we might need to merge the number of positive or negative bits
16231b5a618c59025898806160ed5e7f0ff5bb79e482John McCall    // if the enumerator lists don't match.
16241b5a618c59025898806160ed5e7f0ff5bb79e482John McCall    D2->completeDefinition(T, ToPromotionType,
16251b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                           D->getNumPositiveBits(),
16261b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                           D->getNumNegativeBits());
162736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
162836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
162973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return D2;
163036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
163136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
163296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas GregorDecl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
163396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // If this record has a definition in the translation unit we're coming from,
163496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // but this particular declaration is not that definition, import the
163596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // definition and map to that.
1636952b017601f9c82b51119c3a1600f1312a833db9Douglas Gregor  TagDecl *Definition = D->getDefinition();
163796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (Definition && Definition != D) {
163896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *ImportedDef = Importer.Import(Definition);
16395ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor    if (!ImportedDef)
16405ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor      return 0;
16415ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
16425ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor    return Importer.Imported(D, ImportedDef);
164396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
164496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
164596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Import the major distinguishing characteristics of this record.
164696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclContext *DC, *LexicalDC;
164796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName Name;
164896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  SourceLocation Loc;
164996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
165096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
165196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
165296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Figure out what structure name we're looking for.
165396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  unsigned IDNS = Decl::IDNS_Tag;
165496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName SearchName = Name;
165596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!SearchName && D->getTypedefForAnonDecl()) {
165696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
165796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    IDNS = Decl::IDNS_Ordinary;
165896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  } else if (Importer.getToContext().getLangOptions().CPlusPlus)
165996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    IDNS |= Decl::IDNS_Ordinary;
166096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
166196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // We may already have a record of the same name; try to find and match it.
1662e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor  RecordDecl *AdoptDecl = 0;
166396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!DC->isFunctionOrMethod() && SearchName) {
166496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
166596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
166696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         Lookup.first != Lookup.second;
166796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         ++Lookup.first) {
166896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
166996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor        continue;
167096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
167196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Decl *Found = *Lookup.first;
167296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
167396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor        if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
167496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor          Found = Tag->getDecl();
167596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
167696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
167796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
1678e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
1679e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
1680e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // The record types structurally match, or the "from" translation
1681e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // unit only had a forward declaration anyway; call it the same
1682e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // function.
1683e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // FIXME: For C++, we should also merge methods here.
16845ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor            return Importer.Imported(D, FoundDef);
1685e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          }
1686e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        } else {
1687e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          // We have a forward declaration of this type, so adopt that forward
1688e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          // declaration rather than building a new one.
1689e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          AdoptDecl = FoundRecord;
1690e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          continue;
1691e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        }
169296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
169396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
169496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
169596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
169696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
169796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    if (!ConflictingDecls.empty()) {
169896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
169996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         ConflictingDecls.data(),
170096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         ConflictingDecls.size());
170196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
170296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
170396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
170496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Create the record declaration.
170573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  RecordDecl *D2 = AdoptDecl;
170673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!D2) {
17075250f27420386452a21692a6292c99ee7febdac4John McCall    if (isa<CXXRecordDecl>(D)) {
170873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
1709e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   D->getTagKind(),
1710e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   DC, Loc,
1711e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   Name.getAsIdentifierInfo(),
171296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                        Importer.Import(D->getTagKeywordLoc()));
171373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      D2 = D2CXX;
1714325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor      D2->setAccess(D->getAccess());
1715e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor    } else {
171673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
1717e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                    DC, Loc,
1718e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                    Name.getAsIdentifierInfo(),
1719e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                    Importer.Import(D->getTagKeywordLoc()));
172096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
1721b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Import the qualifier, if any.
1722b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (D->getQualifier()) {
1723b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1724b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1725b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      D2->setQualifierInfo(NNS, NNSRange);
1726b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
172773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->setLexicalDeclContext(LexicalDC);
172873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    LexicalDC->addDecl(D2);
172996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
17305ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
173173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Importer.Imported(D, D2);
1732e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor
173396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (D->isDefinition()) {
173473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->startDefinition();
17355250f27420386452a21692a6292c99ee7febdac4John McCall
17365250f27420386452a21692a6292c99ee7febdac4John McCall    // Add base classes.
17375250f27420386452a21692a6292c99ee7febdac4John McCall    if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
17385250f27420386452a21692a6292c99ee7febdac4John McCall      CXXRecordDecl *D1CXX = cast<CXXRecordDecl>(D);
17395250f27420386452a21692a6292c99ee7febdac4John McCall
17405250f27420386452a21692a6292c99ee7febdac4John McCall      llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
17415250f27420386452a21692a6292c99ee7febdac4John McCall      for (CXXRecordDecl::base_class_iterator
17425250f27420386452a21692a6292c99ee7febdac4John McCall                Base1 = D1CXX->bases_begin(),
17435250f27420386452a21692a6292c99ee7febdac4John McCall             FromBaseEnd = D1CXX->bases_end();
17445250f27420386452a21692a6292c99ee7febdac4John McCall           Base1 != FromBaseEnd;
17455250f27420386452a21692a6292c99ee7febdac4John McCall           ++Base1) {
17465250f27420386452a21692a6292c99ee7febdac4John McCall        QualType T = Importer.Import(Base1->getType());
17475250f27420386452a21692a6292c99ee7febdac4John McCall        if (T.isNull())
17485250f27420386452a21692a6292c99ee7febdac4John McCall          return 0;
17495250f27420386452a21692a6292c99ee7febdac4John McCall
17505250f27420386452a21692a6292c99ee7febdac4John McCall        Bases.push_back(
17515250f27420386452a21692a6292c99ee7febdac4John McCall          new (Importer.getToContext())
17525250f27420386452a21692a6292c99ee7febdac4John McCall                CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
17535250f27420386452a21692a6292c99ee7febdac4John McCall                                 Base1->isVirtual(),
17545250f27420386452a21692a6292c99ee7febdac4John McCall                                 Base1->isBaseOfClass(),
17555250f27420386452a21692a6292c99ee7febdac4John McCall                                 Base1->getAccessSpecifierAsWritten(),
17565606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky                                 Importer.Import(Base1->getTypeSourceInfo())));
17575250f27420386452a21692a6292c99ee7febdac4John McCall      }
17585250f27420386452a21692a6292c99ee7febdac4John McCall      if (!Bases.empty())
17595250f27420386452a21692a6292c99ee7febdac4John McCall        D2CXX->setBases(Bases.data(), Bases.size());
17605250f27420386452a21692a6292c99ee7febdac4John McCall    }
17615250f27420386452a21692a6292c99ee7febdac4John McCall
1762083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    ImportDeclContext(D);
176373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->completeDefinition();
176496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
176596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
176673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return D2;
176796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
176896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
176936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorDecl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
177036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the major distinguishing characteristics of this enumerator.
177136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclContext *DC, *LexicalDC;
177236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName Name;
177336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  SourceLocation Loc;
1774ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
177536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
1776ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
1777ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
1778ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
1779ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
1780ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
178136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Determine whether there are any other declarations with the same name and
178236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // in the same context.
178336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!LexicalDC->isFunctionOrMethod()) {
178436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
178536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
178636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
178736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         Lookup.first != Lookup.second;
178836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++Lookup.first) {
178936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
179036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        continue;
179136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
179236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
179336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
179436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
179536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (!ConflictingDecls.empty()) {
179636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
179736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.data(),
179836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.size());
179936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!Name)
180036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        return 0;
180136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
180236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
180336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
180436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  Expr *Init = Importer.Import(D->getInitExpr());
180536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (D->getInitExpr() && !Init)
180636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
180736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
180836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  EnumConstantDecl *ToEnumerator
180936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
181036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                               Name.getAsIdentifierInfo(), T,
181136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                               Init, D->getInitVal());
1812325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToEnumerator->setAccess(D->getAccess());
181336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  ToEnumerator->setLexicalDeclContext(LexicalDC);
18145ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToEnumerator);
181536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  LexicalDC->addDecl(ToEnumerator);
181636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  return ToEnumerator;
181736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
181896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
1819a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
1820a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the major distinguishing characteristics of this function.
1821a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC, *LexicalDC;
1822a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclarationName Name;
1823a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc;
1824ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1825089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    return 0;
1826089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
1827a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Try to find a function in our own ("to") context with the same name, same
1828a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // type, and in the same context as the function we're importing.
1829a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  if (!LexicalDC->isFunctionOrMethod()) {
1830a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1831a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
1832a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1833a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor         Lookup.first != Lookup.second;
1834a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor         ++Lookup.first) {
1835a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1836a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        continue;
1837a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1838a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
1839a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        if (isExternalLinkage(FoundFunction->getLinkage()) &&
1840a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            isExternalLinkage(D->getLinkage())) {
1841ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          if (Importer.IsStructurallyEquivalent(D->getType(),
1842ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                FoundFunction->getType())) {
1843a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            // FIXME: Actually try to merge the body and other attributes.
18445ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor            return Importer.Imported(D, FoundFunction);
1845a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          }
1846a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1847a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // FIXME: Check for overloading more carefully, e.g., by boosting
1848a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Sema::IsOverload out to the AST library.
1849a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1850a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Function overloading is okay in C++.
1851a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          if (Importer.getToContext().getLangOptions().CPlusPlus)
1852a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            continue;
1853a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1854a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Complain about inconsistent function types.
1855a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
1856ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            << Name << D->getType() << FoundFunction->getType();
1857a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          Importer.ToDiag(FoundFunction->getLocation(),
1858a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                          diag::note_odr_value_here)
1859a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            << FoundFunction->getType();
1860a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        }
1861a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      }
1862a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1863a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
1864a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    }
1865a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1866a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    if (!ConflictingDecls.empty()) {
1867a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
1868a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                         ConflictingDecls.data(),
1869a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                         ConflictingDecls.size());
1870a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (!Name)
1871a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        return 0;
1872a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    }
1873a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
1874ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
1875ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
1876ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
1877ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
1878ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
1879a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1880a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the function parameters.
1881a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 8> Parameters;
1882a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
1883a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor       P != PEnd; ++P) {
1884a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
1885a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    if (!ToP)
1886a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      return 0;
1887a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1888a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Parameters.push_back(ToP);
1889a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
1890a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1891a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Create the imported function.
1892a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1893c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  FunctionDecl *ToFunction = 0;
1894c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
1895c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
1896c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            cast<CXXRecordDecl>(DC),
1897c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            Loc, Name, T, TInfo,
1898c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            FromConstructor->isExplicit(),
1899c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            D->isInlineSpecified(),
1900c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            D->isImplicit());
1901c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else if (isa<CXXDestructorDecl>(D)) {
1902c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
1903c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           cast<CXXRecordDecl>(DC),
1904c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           Loc, Name, T,
1905c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isInlineSpecified(),
1906c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isImplicit());
1907c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else if (CXXConversionDecl *FromConversion
1908c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           = dyn_cast<CXXConversionDecl>(D)) {
1909c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
1910c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           cast<CXXRecordDecl>(DC),
1911c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           Loc, Name, T, TInfo,
1912c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isInlineSpecified(),
1913c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           FromConversion->isExplicit());
1914c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else {
1915c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, Loc,
1916c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                      Name, T, TInfo, D->getStorageClass(),
191716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                      D->getStorageClassAsWritten(),
1918c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                      D->isInlineSpecified(),
1919c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                      D->hasWrittenPrototype());
1920c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  }
1921b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1922b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
1923b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (D->getQualifier()) {
1924b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1925b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1926b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    ToFunction->setQualifierInfo(NNS, NNSRange);
1927b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1928325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToFunction->setAccess(D->getAccess());
1929c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToFunction->setLexicalDeclContext(LexicalDC);
1930c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Importer.Imported(D, ToFunction);
1931c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  LexicalDC->addDecl(ToFunction);
1932a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1933a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Set the parameters.
1934a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
1935c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Parameters[I]->setOwningFunction(ToFunction);
1936c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToFunction->addDecl(Parameters[I]);
1937a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
1938c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToFunction->setParams(Parameters.data(), Parameters.size());
1939a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1940a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // FIXME: Other bits to merge?
1941a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1942c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToFunction;
1943a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1944a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1945c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
1946c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitFunctionDecl(D);
1947c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
1948c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
1949c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1950c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
1951c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
1952c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
1953c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
1954c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
1955c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
1956c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
1957c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
1958c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
1959c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
1960c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
196196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas GregorDecl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
196296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Import the major distinguishing characteristics of a variable.
196396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclContext *DC, *LexicalDC;
196496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName Name;
196596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  SourceLocation Loc;
1966ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1967ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
1968ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
1969ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
1970ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
1971ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
197296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
197396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
197496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
197596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  Expr *BitWidth = Importer.Import(D->getBitWidth());
197696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!BitWidth && D->getBitWidth())
197796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
197896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
197996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
198096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         Loc, Name.getAsIdentifierInfo(),
198196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         T, TInfo, BitWidth, D->isMutable());
1982325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToField->setAccess(D->getAccess());
198396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  ToField->setLexicalDeclContext(LexicalDC);
19845ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToField);
198596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  LexicalDC->addDecl(ToField);
198696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  return ToField;
198796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
198896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
19892e55e3af2f6d6c0509495357fade95105dd144cdDouglas GregorDecl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
19902e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Import the major distinguishing characteristics of an ivar.
19912e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  DeclContext *DC, *LexicalDC;
19922e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  DeclarationName Name;
19932e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  SourceLocation Loc;
19942e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
19952e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
19962e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
19972e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Determine whether we've already imported this ivar
19982e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
19992e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor       Lookup.first != Lookup.second;
20002e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor       ++Lookup.first) {
20012e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
20022e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (Importer.IsStructurallyEquivalent(D->getType(),
20032e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                            FoundIvar->getType())) {
20042e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.Imported(D, FoundIvar);
20052e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        return FoundIvar;
20062e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      }
20072e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
20082e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
20092e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << Name << D->getType() << FoundIvar->getType();
20102e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
20112e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << FoundIvar->getType();
20122e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      return 0;
20132e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    }
20142e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  }
20152e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
20162e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Import the type.
20172e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  QualType T = Importer.Import(D->getType());
20182e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (T.isNull())
20192e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
20202e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
20212e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
20222e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  Expr *BitWidth = Importer.Import(D->getBitWidth());
20232e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (!BitWidth && D->getBitWidth())
20242e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
20252e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
2026a06549226f45d5b72169a3d054415616dd1014a2Daniel Dunbar  ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2027a06549226f45d5b72169a3d054415616dd1014a2Daniel Dunbar                                              cast<ObjCContainerDecl>(DC),
20282e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              Loc, Name.getAsIdentifierInfo(),
20292e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              T, TInfo, D->getAccessControl(),
2030ac0021ba802e193e0f9f8207768c7862c7603bc0Fariborz Jahanian                                              BitWidth, D->getSynthesize());
20312e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  ToIvar->setLexicalDeclContext(LexicalDC);
20322e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  Importer.Imported(D, ToIvar);
20332e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  LexicalDC->addDecl(ToIvar);
20342e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  return ToIvar;
20352e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
20362e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor}
20372e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
2038a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2039a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the major distinguishing characteristics of a variable.
2040a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC, *LexicalDC;
2041a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclarationName Name;
2042a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc;
2043ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2044a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return 0;
2045089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2046089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Try to find a variable in our own ("to") context with the same name and
2047089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // in the same context as the variable we're importing.
20489bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (D->isFileVarDecl()) {
2049089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    VarDecl *MergeWithVar = 0;
2050089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2051089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
20529bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2053089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor         Lookup.first != Lookup.second;
2054089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor         ++Lookup.first) {
2055089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2056089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        continue;
2057089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2058089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2059089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        // We have found a variable that we may need to merge with. Check it.
2060089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        if (isExternalLinkage(FoundVar->getLinkage()) &&
2061089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            isExternalLinkage(D->getLinkage())) {
2062ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          if (Importer.IsStructurallyEquivalent(D->getType(),
2063ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                FoundVar->getType())) {
2064089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            MergeWithVar = FoundVar;
2065089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            break;
2066089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          }
2067089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2068d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          const ArrayType *FoundArray
2069d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            = Importer.getToContext().getAsArrayType(FoundVar->getType());
2070d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          const ArrayType *TArray
2071ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            = Importer.getToContext().getAsArrayType(D->getType());
2072d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          if (FoundArray && TArray) {
2073d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            if (isa<IncompleteArrayType>(FoundArray) &&
2074d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor                isa<ConstantArrayType>(TArray)) {
2075ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              // Import the type.
2076ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              QualType T = Importer.Import(D->getType());
2077ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              if (T.isNull())
2078ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                return 0;
2079ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2080d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              FoundVar->setType(T);
2081d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              MergeWithVar = FoundVar;
2082d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              break;
2083d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            } else if (isa<IncompleteArrayType>(TArray) &&
2084d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor                       isa<ConstantArrayType>(FoundArray)) {
2085d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              MergeWithVar = FoundVar;
2086d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              break;
20870f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor            }
20880f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor          }
20890f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor
2090089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
2091ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            << Name << D->getType() << FoundVar->getType();
2092089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2093089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            << FoundVar->getType();
2094089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        }
2095089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      }
2096089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2097089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
2098089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2099089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2100089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    if (MergeWithVar) {
2101089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      // An equivalent variable with external linkage has been found. Link
2102089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      // the two declarations, then merge them.
21035ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor      Importer.Imported(D, MergeWithVar);
2104089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2105089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (VarDecl *DDef = D->getDefinition()) {
2106089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2107089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(ExistingDef->getLocation(),
2108089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                          diag::err_odr_variable_multiple_def)
2109089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            << Name;
2110089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2111089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        } else {
2112089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Expr *Init = Importer.Import(DDef->getInit());
2113838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor          MergeWithVar->setInit(Init);
2114089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        }
2115089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      }
2116089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2117089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      return MergeWithVar;
2118089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2119089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2120089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    if (!ConflictingDecls.empty()) {
2121089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
2122089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                         ConflictingDecls.data(),
2123089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                         ConflictingDecls.size());
2124089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (!Name)
2125089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        return 0;
2126089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2127089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  }
212882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2129ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
2130ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2131ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2132ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2133ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2134089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Create the imported variable.
213582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2136089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2137089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                   Name.getAsIdentifierInfo(), T, TInfo,
213816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClass(),
213916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten());
2140b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
2141b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (D->getQualifier()) {
2142b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2143b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2144b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    ToVar->setQualifierInfo(NNS, NNSRange);
2145b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2146325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToVar->setAccess(D->getAccess());
21479bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ToVar->setLexicalDeclContext(LexicalDC);
21485ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToVar);
21499bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  LexicalDC->addDecl(ToVar);
21509bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
2151089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Merge the initializer.
2152089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // FIXME: Can we really import any initializer? Alternatively, we could force
2153089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // ourselves to import every declaration of a variable and then only use
2154089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // getInit() here.
2155838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
2156089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2157089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // FIXME: Other bits to merge?
2158089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2159089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  return ToVar;
2160089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
2161089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
21622cd00932b9b5403047139ce8cfaa3ae47966f894Douglas GregorDecl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
21632cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Parameters are created in the translation unit's context, then moved
21642cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // into the function declaration's context afterward.
21652cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
21662cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
21672cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the name of this declaration.
21682cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
21692cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  if (D->getDeclName() && !Name)
21702cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    return 0;
21712cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
21722cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the location of this declaration.
21732cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
21742cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
21752cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the parameter's type.
21762cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  QualType T = Importer.Import(D->getType());
21772cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  if (T.isNull())
21782cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    return 0;
21792cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
21802cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Create the imported parameter.
21812cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  ImplicitParamDecl *ToParm
21822cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    = ImplicitParamDecl::Create(Importer.getToContext(), DC,
21832cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor                                Loc, Name.getAsIdentifierInfo(),
21842cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor                                T);
21852cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  return Importer.Imported(D, ToParm);
21862cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor}
21872cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
2188a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2189a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Parameters are created in the translation unit's context, then moved
2190a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // into the function declaration's context afterward.
2191a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2192a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
219382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // Import the name of this declaration.
219482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
219582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (D->getDeclName() && !Name)
219682fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
219782fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2198a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the location of this declaration.
2199a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
2200a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2201a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the parameter's type.
2202a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  QualType T = Importer.Import(D->getType());
220382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (T.isNull())
220482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
220582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2206a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Create the imported parameter.
2207a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2208a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2209a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            Loc, Name.getAsIdentifierInfo(),
2210a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            T, TInfo, D->getStorageClass(),
221116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                             D->getStorageClassAsWritten(),
2212a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            /*FIXME: Default argument*/ 0);
2213bf73b352acb7a2d041ce8b50171dd7f8e2b2c1bbJohn McCall  ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
22145ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  return Importer.Imported(D, ToParm);
2215a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
221682fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2217c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas GregorDecl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2218c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the major distinguishing characteristics of a method.
2219c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  DeclContext *DC, *LexicalDC;
2220c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  DeclarationName Name;
2221c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  SourceLocation Loc;
2222c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2223c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return 0;
2224c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2225c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2226c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       Lookup.first != Lookup.second;
2227c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       ++Lookup.first) {
2228c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2229c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2230c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        continue;
2231c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2232c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check return types.
2233c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2234c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                             FoundMethod->getResultType())) {
2235c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2236c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name
2237c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->getResultType() << FoundMethod->getResultType();
2238c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2239c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2240c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2241c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2242c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2243c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2244c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check the number of parameters.
2245c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (D->param_size() != FoundMethod->param_size()) {
2246c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2247c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name
2248c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->param_size() << FoundMethod->param_size();
2249c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2250c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2251c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2252c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2253c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2254c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2255c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check parameter types.
2256c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2257c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor             PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2258c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor           P != PEnd; ++P, ++FoundP) {
2259c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2260c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                               (*FoundP)->getType())) {
2261c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          Importer.FromDiag((*P)->getLocation(),
2262c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                            diag::err_odr_objc_method_param_type_inconsistent)
2263c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << D->isInstanceMethod() << Name
2264c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << (*P)->getType() << (*FoundP)->getType();
2265c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2266c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << (*FoundP)->getType();
2267c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          return 0;
2268c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        }
2269c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2270c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2271c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check variadic/non-variadic.
2272c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check the number of parameters.
2273c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (D->isVariadic() != FoundMethod->isVariadic()) {
2274c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2275c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2276c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2277c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2278c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2279c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2280c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2281c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2282c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // FIXME: Any other bits we need to merge?
2283c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      return Importer.Imported(D, FoundMethod);
2284c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    }
2285c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2286c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2287c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the result type.
2288c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  QualType ResultTy = Importer.Import(D->getResultType());
2289c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (ResultTy.isNull())
2290c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return 0;
2291c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
22924bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor  TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
22934bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor
2294c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ObjCMethodDecl *ToMethod
2295c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    = ObjCMethodDecl::Create(Importer.getToContext(),
2296c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Loc,
2297c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Importer.Import(D->getLocEnd()),
2298c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Name.getObjCSelector(),
22994bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor                             ResultTy, ResultTInfo, DC,
2300c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isInstanceMethod(),
2301c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isVariadic(),
2302c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isSynthesized(),
23033fe104154dd2e8ffb351142d74f308938b5c99bfFariborz Jahanian                             D->isDefined(),
2304c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->getImplementationControl());
2305c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2306c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // FIXME: When we decide to merge method definitions, we'll need to
2307c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // deal with implicit parameters.
2308c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2309c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the parameters
2310c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2311c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2312c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                   FromPEnd = D->param_end();
2313c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       FromP != FromPEnd;
2314c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       ++FromP) {
2315c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2316c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    if (!ToP)
2317c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      return 0;
2318c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2319c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToParams.push_back(ToP);
2320c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2321c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2322c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Set the parameters.
2323c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2324c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToParams[I]->setOwningFunction(ToMethod);
2325c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToMethod->addDecl(ToParams[I]);
2326c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2327c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToMethod->setMethodParams(Importer.getToContext(),
23284ecb25fa94897b2c03510292acace710e5262ba5Fariborz Jahanian                            ToParams.data(), ToParams.size(),
23294ecb25fa94897b2c03510292acace710e5262ba5Fariborz Jahanian                            ToParams.size());
2330c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2331c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToMethod->setLexicalDeclContext(LexicalDC);
2332c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Importer.Imported(D, ToMethod);
2333c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  LexicalDC->addDecl(ToMethod);
2334c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToMethod;
2335c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor}
2336c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2337b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas GregorDecl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2338b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import the major distinguishing characteristics of a category.
2339b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  DeclContext *DC, *LexicalDC;
2340b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  DeclarationName Name;
2341b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  SourceLocation Loc;
2342b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2343b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    return 0;
2344b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2345b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCInterfaceDecl *ToInterface
2346b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2347b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (!ToInterface)
2348b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    return 0;
2349b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2350b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Determine if we've already encountered this category.
2351b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCCategoryDecl *MergeWithCategory
2352b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2353b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCCategoryDecl *ToCategory = MergeWithCategory;
2354b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (!ToCategory) {
2355b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2356b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Importer.Import(D->getAtLoc()),
2357b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Loc,
2358b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                       Importer.Import(D->getCategoryNameLoc()),
2359b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Name.getAsIdentifierInfo());
2360b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setLexicalDeclContext(LexicalDC);
2361b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    LexicalDC->addDecl(ToCategory);
2362b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Imported(D, ToCategory);
2363b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2364b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // Link this category into its class's category list.
2365b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setClassInterface(ToInterface);
2366b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->insertNextClassCategory();
2367b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2368b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // Import protocols
2369b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2370b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2371b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2372b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      = D->protocol_loc_begin();
2373b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2374b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          FromProtoEnd = D->protocol_end();
2375b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor         FromProto != FromProtoEnd;
2376b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor         ++FromProto, ++FromProtoLoc) {
2377b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      ObjCProtocolDecl *ToProto
2378b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2379b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      if (!ToProto)
2380b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor        return 0;
2381b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      Protocols.push_back(ToProto);
2382b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2383b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    }
2384b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2385b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
2386b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2387b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                ProtocolLocs.data(), Importer.getToContext());
2388b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2389b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  } else {
2390b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Imported(D, ToCategory);
2391b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  }
2392b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2393b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import all of the members of this category.
2394083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
2395b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2396b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // If we have an implementation, import it as well.
2397b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (D->getImplementation()) {
2398b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ObjCCategoryImplDecl *Impl
2399b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation()));
2400b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    if (!Impl)
2401b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      return 0;
2402b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2403b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setImplementation(Impl);
2404b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  }
2405b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2406b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  return ToCategory;
2407b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor}
2408b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
24092e2a400383c7a64a927f61eeed596b08928e1d4bDouglas GregorDecl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
2410b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import the major distinguishing characteristics of a protocol.
24112e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  DeclContext *DC, *LexicalDC;
24122e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  DeclarationName Name;
24132e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  SourceLocation Loc;
24142e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
24152e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    return 0;
24162e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
24172e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  ObjCProtocolDecl *MergeWithProtocol = 0;
24182e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
24192e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       Lookup.first != Lookup.second;
24202e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++Lookup.first) {
24212e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
24222e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      continue;
24232e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
24242e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
24252e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      break;
24262e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  }
24272e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
24282e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  ObjCProtocolDecl *ToProto = MergeWithProtocol;
24292e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  if (!ToProto || ToProto->isForwardDecl()) {
24302e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if (!ToProto) {
24312e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
24322e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                                         Name.getAsIdentifierInfo());
24332e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto->setForwardDecl(D->isForwardDecl());
24342e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto->setLexicalDeclContext(LexicalDC);
24352e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      LexicalDC->addDecl(ToProto);
24362e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    }
24372e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Imported(D, ToProto);
24382e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
24392e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    // Import protocols
24402e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
24412e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
24422e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    ObjCProtocolDecl::protocol_loc_iterator
24432e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      FromProtoLoc = D->protocol_loc_begin();
24442e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
24452e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                                          FromProtoEnd = D->protocol_end();
24462e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       FromProto != FromProtoEnd;
24472e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++FromProto, ++FromProtoLoc) {
24482e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ObjCProtocolDecl *ToProto
24492e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
24502e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      if (!ToProto)
24512e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor        return 0;
24522e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      Protocols.push_back(ToProto);
24532e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
24542e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    }
24552e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
24562e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
24572e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    ToProto->setProtocolList(Protocols.data(), Protocols.size(),
24582e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                             ProtocolLocs.data(), Importer.getToContext());
24592e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  } else {
24602e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Imported(D, ToProto);
24612e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  }
24622e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
2463b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import all of the members of this protocol.
2464083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
24652e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
24662e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  return ToProto;
24672e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor}
24682e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
2469a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas GregorDecl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2470a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // Import the major distinguishing characteristics of an @interface.
2471a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  DeclContext *DC, *LexicalDC;
2472a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  DeclarationName Name;
2473a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  SourceLocation Loc;
2474a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2475a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    return 0;
2476a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2477a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  ObjCInterfaceDecl *MergeWithIface = 0;
2478a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2479a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       Lookup.first != Lookup.second;
2480a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++Lookup.first) {
2481a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2482a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      continue;
2483a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2484a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2485a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      break;
2486a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
2487a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2488a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  ObjCInterfaceDecl *ToIface = MergeWithIface;
2489a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (!ToIface || ToIface->isForwardDecl()) {
2490a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!ToIface) {
2491a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2492a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          DC, Loc,
2493a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          Name.getAsIdentifierInfo(),
2494deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor                                          Importer.Import(D->getClassLoc()),
2495a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          D->isForwardDecl(),
2496a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          D->isImplicitInterfaceDecl());
24972e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToIface->setForwardDecl(D->isForwardDecl());
2498a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setLexicalDeclContext(LexicalDC);
2499a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      LexicalDC->addDecl(ToIface);
2500a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
2501a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Imported(D, ToIface);
2502a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2503a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (D->getSuperClass()) {
2504a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ObjCInterfaceDecl *Super
2505a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2506a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      if (!Super)
2507a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        return 0;
2508a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2509a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setSuperClass(Super);
2510a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2511a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
2512a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2513a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // Import protocols
2514a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2515a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2516a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ObjCInterfaceDecl::protocol_loc_iterator
2517a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      FromProtoLoc = D->protocol_loc_begin();
2518a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2519a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                           FromProtoEnd = D->protocol_end();
2520a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       FromProto != FromProtoEnd;
2521a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++FromProto, ++FromProtoLoc) {
2522a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ObjCProtocolDecl *ToProto
2523a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2524a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      if (!ToProto)
2525a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        return 0;
2526a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      Protocols.push_back(ToProto);
2527a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2528a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
2529a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2530a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
2531a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2532a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                             ProtocolLocs.data(), Importer.getToContext());
2533a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2534a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // Import @end range
2535a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2536a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  } else {
2537a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Imported(D, ToIface);
25382e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
25392e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    // Check for consistency of superclasses.
25402e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    DeclarationName FromSuperName, ToSuperName;
25412e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (D->getSuperClass())
25422e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
25432e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (ToIface->getSuperClass())
25442e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      ToSuperName = ToIface->getSuperClass()->getDeclName();
25452e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (FromSuperName != ToSuperName) {
25462e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(ToIface->getLocation(),
25472e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                      diag::err_odr_objc_superclass_inconsistent)
25482e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << ToIface->getDeclName();
25492e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (ToIface->getSuperClass())
25502e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.ToDiag(ToIface->getSuperClassLoc(),
25512e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                        diag::note_odr_objc_superclass)
25522e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor          << ToIface->getSuperClass()->getDeclName();
25532e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      else
25542e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.ToDiag(ToIface->getLocation(),
25552e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                        diag::note_odr_objc_missing_superclass);
25562e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (D->getSuperClass())
25572e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.FromDiag(D->getSuperClassLoc(),
25582e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                          diag::note_odr_objc_superclass)
25592e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor          << D->getSuperClass()->getDeclName();
25602e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      else
25612e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.FromDiag(D->getLocation(),
25622e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                          diag::note_odr_objc_missing_superclass);
25632e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      return 0;
25642e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    }
2565a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
2566a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2567b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import categories. When the categories themselves are imported, they'll
2568b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // hook themselves into this interface.
2569b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
2570b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor       FromCat = FromCat->getNextClassCategory())
2571b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Import(FromCat);
2572b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2573a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // Import all of the members of this class.
2574083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
2575a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2576a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // If we have an @implementation, import it as well.
2577a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (D->getImplementation()) {
2578a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ObjCImplementationDecl *Impl
2579a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      = cast<ObjCImplementationDecl>(Importer.Import(D->getImplementation()));
2580a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!Impl)
2581a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      return 0;
2582a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2583a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setImplementation(Impl);
2584a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
2585a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
25862e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  return ToIface;
2587a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor}
2588a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2589e3261624c1870e52d7efc2ac83e647713361ac6cDouglas GregorDecl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
2590e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Import the major distinguishing characteristics of an @property.
2591e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  DeclContext *DC, *LexicalDC;
2592e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  DeclarationName Name;
2593e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  SourceLocation Loc;
2594e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2595e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    return 0;
2596e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2597e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Check whether we have already imported this property.
2598e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2599e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       Lookup.first != Lookup.second;
2600e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       ++Lookup.first) {
2601e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    if (ObjCPropertyDecl *FoundProp
2602e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                                = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
2603e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // Check property types.
2604e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      if (!Importer.IsStructurallyEquivalent(D->getType(),
2605e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                                             FoundProp->getType())) {
2606e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
2607e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor          << Name << D->getType() << FoundProp->getType();
2608e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
2609e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor          << FoundProp->getType();
2610e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        return 0;
2611e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      }
2612e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2613e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // FIXME: Check property attributes, getters, setters, etc.?
2614e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2615e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // Consider these properties to be equivalent.
2616e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      Importer.Imported(D, FoundProp);
2617e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      return FoundProp;
2618e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    }
2619e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  }
2620e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2621e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Import the type.
262283a230c83a54190366138c1a4f4310ef838b88fcJohn McCall  TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
262383a230c83a54190366138c1a4f4310ef838b88fcJohn McCall  if (!T)
2624e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    return 0;
2625e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2626e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Create the new property.
2627e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ObjCPropertyDecl *ToProperty
2628e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
2629e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               Name.getAsIdentifierInfo(),
2630e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               Importer.Import(D->getAtLoc()),
2631e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               T,
2632e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               D->getPropertyImplementation());
2633e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  Importer.Imported(D, ToProperty);
2634e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setLexicalDeclContext(LexicalDC);
2635e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  LexicalDC->addDecl(ToProperty);
2636e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2637e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setPropertyAttributes(D->getPropertyAttributes());
263880aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian  ToProperty->setPropertyAttributesAsWritten(
263980aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian                                      D->getPropertyAttributesAsWritten());
2640e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setGetterName(Importer.Import(D->getGetterName()));
2641e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setSetterName(Importer.Import(D->getSetterName()));
2642e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setGetterMethodDecl(
2643e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
2644e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setSetterMethodDecl(
2645e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
2646e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setPropertyIvarDecl(
2647e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
2648e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  return ToProperty;
2649e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor}
2650e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
26512b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas GregorDecl *
26522b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas GregorASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
26532b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  // Import the context of this declaration.
26542b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
26552b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  if (!DC)
26562b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    return 0;
26572b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
26582b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  DeclContext *LexicalDC = DC;
26592b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
26602b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
26612b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    if (!LexicalDC)
26622b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      return 0;
26632b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  }
26642b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
26652b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  // Import the location of this declaration.
26662b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
26672b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
26682b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
26692b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  llvm::SmallVector<SourceLocation, 4> Locations;
26702b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
26712b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    = D->protocol_loc_begin();
26722b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  for (ObjCForwardProtocolDecl::protocol_iterator FromProto
26732b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor         = D->protocol_begin(), FromProtoEnd = D->protocol_end();
26742b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor       FromProto != FromProtoEnd;
26752b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor       ++FromProto, ++FromProtoLoc) {
26762b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    ObjCProtocolDecl *ToProto
26772b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
26782b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    if (!ToProto)
26792b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      continue;
26802b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
26812b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Protocols.push_back(ToProto);
26822b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Locations.push_back(Importer.Import(*FromProtoLoc));
26832b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  }
26842b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
26852b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ObjCForwardProtocolDecl *ToForward
26862b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
26872b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor                                      Protocols.data(), Protocols.size(),
26882b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor                                      Locations.data());
26892b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ToForward->setLexicalDeclContext(LexicalDC);
26902b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  LexicalDC->addDecl(ToForward);
26912b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  Importer.Imported(D, ToForward);
26922b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  return ToForward;
26932b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor}
26942b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
2695a2bc15b7463a9f85a5bff1531d833c278426a733Douglas GregorDecl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
2696a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  // Import the context of this declaration.
2697a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2698a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  if (!DC)
2699a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    return 0;
2700a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2701a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  DeclContext *LexicalDC = DC;
2702a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
2703a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2704a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    if (!LexicalDC)
2705a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      return 0;
2706a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  }
2707a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2708a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  // Import the location of this declaration.
2709a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
2710a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2711a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
2712a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  llvm::SmallVector<SourceLocation, 4> Locations;
2713a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
2714a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor       From != FromEnd; ++From) {
2715a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    ObjCInterfaceDecl *ToIface
2716a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
2717a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    if (!ToIface)
2718a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      continue;
2719a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2720a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Interfaces.push_back(ToIface);
2721a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Locations.push_back(Importer.Import(From->getLocation()));
2722a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  }
2723a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2724a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
2725a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Loc,
2726a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Interfaces.data(),
2727a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Locations.data(),
2728a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Interfaces.size());
2729a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  ToClass->setLexicalDeclContext(LexicalDC);
2730a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  LexicalDC->addDecl(ToClass);
2731a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  Importer.Imported(D, ToClass);
2732a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  return ToClass;
2733a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor}
2734a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
27354800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
27364800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor// Import Statements
27374800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
27384800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
27394800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorStmt *ASTNodeImporter::VisitStmt(Stmt *S) {
27404800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
27414800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    << S->getStmtClassName();
27424800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return 0;
27434800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
27444800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
27454800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
27464800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor// Import Expressions
27474800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
27484800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorExpr *ASTNodeImporter::VisitExpr(Expr *E) {
27494800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
27504800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    << E->getStmtClassName();
27514800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return 0;
27524800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
27534800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
2754440806306674e23ad74726208cbdc6f37849dd9dDouglas GregorExpr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
2755440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  NestedNameSpecifier *Qualifier = 0;
2756440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (E->getQualifier()) {
2757440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    Qualifier = Importer.Import(E->getQualifier());
2758440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    if (!E->getQualifier())
2759440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor      return 0;
2760440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  }
2761440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
2762440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
2763440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (!ToD)
2764440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    return 0;
2765440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
2766440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  QualType T = Importer.Import(E->getType());
2767440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (T.isNull())
2768440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    return 0;
2769440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
2770440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
2771440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             Importer.Import(E->getQualifierRange()),
2772440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             ToD,
2773440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             Importer.Import(E->getLocation()),
2774440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             T,
2775440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             /*FIXME:TemplateArgs=*/0);
2776440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor}
2777440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
27784800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorExpr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
27794800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  QualType T = Importer.Import(E->getType());
27804800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (T.isNull())
27814800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return 0;
27824800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
27834800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return new (Importer.getToContext())
27844800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    IntegerLiteral(E->getValue(), T, Importer.Import(E->getLocation()));
27854800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
27864800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
2787b2e400aae8c62c4e1616016f40618baace0da065Douglas GregorExpr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
2788b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  QualType T = Importer.Import(E->getType());
2789b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  if (T.isNull())
2790b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor    return 0;
2791b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor
2792b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
2793b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor                                                        E->isWide(), T,
2794b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor                                          Importer.Import(E->getLocation()));
2795b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor}
2796b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor
2797f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
2798f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
2799f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!SubExpr)
2800f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2801f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2802f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext())
2803f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                  ParenExpr(Importer.Import(E->getLParen()),
2804f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                            Importer.Import(E->getRParen()),
2805f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                            SubExpr);
2806f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
2807f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2808f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
2809f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
2810f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
2811f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2812f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2813f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
2814f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!SubExpr)
2815f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2816f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2817f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
2818f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                                     T,
2819f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                         Importer.Import(E->getOperatorLoc()));
2820f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
2821f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2822bd249a542878a626192746c1e0c0b21f164e6df7Douglas GregorExpr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
2823bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  QualType ResultType = Importer.Import(E->getType());
2824bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
2825bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  if (E->isArgumentType()) {
2826bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
2827bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    if (!TInfo)
2828bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor      return 0;
2829bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
2830bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
2831bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                                           TInfo, ResultType,
2832bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                           Importer.Import(E->getOperatorLoc()),
2833bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                           Importer.Import(E->getRParenLoc()));
2834bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  }
2835bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
2836bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  Expr *SubExpr = Importer.Import(E->getArgumentExpr());
2837bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  if (!SubExpr)
2838bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    return 0;
2839bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
2840bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
2841bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                                         SubExpr, ResultType,
2842bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                          Importer.Import(E->getOperatorLoc()),
2843bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                          Importer.Import(E->getRParenLoc()));
2844bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor}
2845bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
2846f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
2847f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
2848f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
2849f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2850f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2851f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *LHS = Importer.Import(E->getLHS());
2852f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!LHS)
2853f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2854f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2855f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *RHS = Importer.Import(E->getRHS());
2856f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!RHS)
2857f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2858f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2859f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
2860f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                                      T,
2861f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                          Importer.Import(E->getOperatorLoc()));
2862f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
2863f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2864f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
2865f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
2866f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
2867f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2868f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2869f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType CompLHSType = Importer.Import(E->getComputationLHSType());
2870f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (CompLHSType.isNull())
2871f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2872f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2873f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType CompResultType = Importer.Import(E->getComputationResultType());
2874f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (CompResultType.isNull())
2875f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2876f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2877f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *LHS = Importer.Import(E->getLHS());
2878f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!LHS)
2879f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2880f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2881f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *RHS = Importer.Import(E->getRHS());
2882f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!RHS)
2883f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2884f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2885f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext())
2886f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                        CompoundAssignOperator(LHS, RHS, E->getOpcode(),
2887f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                               T, CompLHSType, CompResultType,
2888f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                          Importer.Import(E->getOperatorLoc()));
2889f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
2890f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2891f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallbool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
2892f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (E->path_empty()) return false;
2893f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
2894f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  // TODO: import cast paths
2895f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return true;
2896f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
2897f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
289836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorExpr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
289936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  QualType T = Importer.Import(E->getType());
290036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (T.isNull())
290136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
290236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
290336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
290436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!SubExpr)
290536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
2906f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
2907f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  CXXCastPath BasePath;
2908f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (ImportCastPath(E, BasePath))
2909f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return 0;
2910f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
2911f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
2912f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                  SubExpr, &BasePath, E->getCategory());
291336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
291436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
2915008847a70ab122a99911149199855060fb3753b4Douglas GregorExpr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
2916008847a70ab122a99911149199855060fb3753b4Douglas Gregor  QualType T = Importer.Import(E->getType());
2917008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (T.isNull())
2918008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
2919008847a70ab122a99911149199855060fb3753b4Douglas Gregor
2920008847a70ab122a99911149199855060fb3753b4Douglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
2921008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (!SubExpr)
2922008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
2923008847a70ab122a99911149199855060fb3753b4Douglas Gregor
2924008847a70ab122a99911149199855060fb3753b4Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
2925008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (!TInfo && E->getTypeInfoAsWritten())
2926008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
2927008847a70ab122a99911149199855060fb3753b4Douglas Gregor
2928f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  CXXCastPath BasePath;
2929f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (ImportCastPath(E, BasePath))
2930f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return 0;
2931f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
2932f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return CStyleCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
2933f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                SubExpr, &BasePath, TInfo,
2934f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                Importer.Import(E->getLParenLoc()),
2935f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                Importer.Import(E->getRParenLoc()));
2936008847a70ab122a99911149199855060fb3753b4Douglas Gregor}
2937008847a70ab122a99911149199855060fb3753b4Douglas Gregor
29384800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorASTImporter::ASTImporter(Diagnostic &Diags,
29394800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                         ASTContext &ToContext, FileManager &ToFileManager,
29404800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                         ASTContext &FromContext, FileManager &FromFileManager)
29411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  : ToContext(ToContext), FromContext(FromContext),
2942885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToFileManager(ToFileManager), FromFileManager(FromFileManager),
29434800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Diags(Diags) {
29449bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ImportedDecls[FromContext.getTranslationUnitDecl()]
29459bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    = ToContext.getTranslationUnitDecl();
29469bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
29479bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
29489bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorASTImporter::~ASTImporter() { }
29491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
29501b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTImporter::Import(QualType FromT) {
29511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (FromT.isNull())
29521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
29531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
2954169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Check whether we've already imported this type.
2955169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  llvm::DenseMap<Type *, Type *>::iterator Pos
2956169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor    = ImportedTypes.find(FromT.getTypePtr());
2957169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  if (Pos != ImportedTypes.end())
2958169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor    return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
29591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
2960169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Import the type
29611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  ASTNodeImporter Importer(*this);
29621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToT = Importer.Visit(FromT.getTypePtr());
29631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToT.isNull())
29641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToT;
29651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
2966169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Record the imported type.
2967169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
2968169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor
29691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
29701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
29711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
29729bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorTypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
297382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (!FromTSI)
297482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return FromTSI;
297582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
297682fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // FIXME: For now we just create a "trivial" type source info based
29775606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky  // on the type and a single location. Implement a real version of this.
297882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  QualType T = Import(FromTSI->getType());
297982fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (T.isNull())
298082fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
298182fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
298282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  return ToContext.getTrivialTypeSourceInfo(T,
2983bd054dba8a3023821f2a0951b0fae05e3522a7c9Abramo Bagnara                        FromTSI->getTypeLoc().getSourceRange().getBegin());
29849bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
29859bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
29869bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorDecl *ASTImporter::Import(Decl *FromD) {
29879bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromD)
29889bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
29899bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
29909bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Check whether we've already imported this declaration.
29919bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
29929bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (Pos != ImportedDecls.end())
29939bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return Pos->second;
29949bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
29959bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Import the type
29969bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ASTNodeImporter Importer(*this);
29979bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  Decl *ToD = Importer.Visit(FromD);
29989bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!ToD)
29999bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
30009bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30019bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Record the imported declaration.
30029bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ImportedDecls[FromD] = ToD;
3003ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
3004ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3005ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // Keep track of anonymous tags that have an associated typedef.
3006ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (FromTag->getTypedefForAnonDecl())
3007ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      AnonTagsWithPendingTypedefs.push_back(FromTag);
3008ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3009ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // When we've finished transforming a typedef, see whether it was the
3010ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // typedef for an anonymous tag.
3011ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    for (llvm::SmallVector<TagDecl *, 4>::iterator
3012ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor               FromTag = AnonTagsWithPendingTypedefs.begin(),
3013ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            FromTagEnd = AnonTagsWithPendingTypedefs.end();
3014ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor         FromTag != FromTagEnd; ++FromTag) {
3015ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3016ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3017ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          // We found the typedef for an anonymous tag; link them.
3018ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3019ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          AnonTagsWithPendingTypedefs.erase(FromTag);
3020ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          break;
3021ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        }
3022ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      }
3023ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    }
3024ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  }
3025ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
30269bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return ToD;
30279bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30289bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30299bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorDeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
30309bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromDC)
30319bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return FromDC;
30329bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30339bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
30349bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30359bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30369bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorExpr *ASTImporter::Import(Expr *FromE) {
30379bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromE)
30389bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
30399bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30409bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
30419bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30429bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30439bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorStmt *ASTImporter::Import(Stmt *FromS) {
30449bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromS)
30459bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
30469bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30474800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Check whether we've already imported this declaration.
30484800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
30494800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (Pos != ImportedStmts.end())
30504800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return Pos->second;
30514800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
30524800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Import the type
30534800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  ASTNodeImporter Importer(*this);
30544800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Stmt *ToS = Importer.Visit(FromS);
30554800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (!ToS)
30564800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return 0;
30574800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
30584800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Record the imported declaration.
30594800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  ImportedStmts[FromS] = ToS;
30604800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return ToS;
30619bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30629bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30639bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorNestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
30649bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromNNS)
30659bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
30669bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30679bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // FIXME: Implement!
30689bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return 0;
30699bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30709bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30719bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorSourceLocation ASTImporter::Import(SourceLocation FromLoc) {
30729bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (FromLoc.isInvalid())
30739bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return SourceLocation();
30749bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
3075885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &FromSM = FromContext.getSourceManager();
3076885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3077885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // For now, map everything down to its spelling location, so that we
3078885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // don't have to import macro instantiations.
3079885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // FIXME: Import macro instantiations!
3080885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  FromLoc = FromSM.getSpellingLoc(FromLoc);
3081885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
3082885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &ToSM = ToContext.getSourceManager();
3083885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  return ToSM.getLocForStartOfFile(Import(Decomposed.first))
3084885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor             .getFileLocWithOffset(Decomposed.second);
30859bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30869bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30879bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorSourceRange ASTImporter::Import(SourceRange FromRange) {
30889bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
30899bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30909bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
3091885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas GregorFileID ASTImporter::Import(FileID FromID) {
3092885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  llvm::DenseMap<unsigned, FileID>::iterator Pos
3093885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    = ImportedFileIDs.find(FromID.getHashValue());
3094885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  if (Pos != ImportedFileIDs.end())
3095885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    return Pos->second;
3096885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3097885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &FromSM = FromContext.getSourceManager();
3098885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &ToSM = ToContext.getSourceManager();
3099885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
3100885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
3101885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3102885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // Include location of this file.
3103885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
3104885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3105885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // Map the FileID for to the "to" source manager.
3106885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  FileID ToID;
3107885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
3108885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  if (Cache->Entry) {
3109885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We probably want to use getVirtualFile(), so we don't hit the
3110885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // disk again
3111885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
3112885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // than mmap the files several times.
3113885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
3114885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToID = ToSM.createFileID(Entry, ToIncludeLoc,
3115885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                             FromSLoc.getFile().getFileCharacteristic());
3116885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  } else {
3117885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We want to re-use the existing MemoryBuffer!
3118e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(getDiags(), FromSM);
3119885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    llvm::MemoryBuffer *ToBuf
3120a0a270c0f1c0a4e3482438bdc5f4a7bd3d25f0a6Chris Lattner      = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
3121885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                                             FromBuf->getBufferIdentifier());
3122885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToID = ToSM.createFileIDForMemBuffer(ToBuf);
3123885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  }
3124885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3125885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3126885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  ImportedFileIDs[FromID.getHashValue()] = ToID;
3127885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  return ToID;
3128885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor}
3129885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
31301b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorDeclarationName ASTImporter::Import(DeclarationName FromName) {
31311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!FromName)
31321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return DeclarationName();
31331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  switch (FromName.getNameKind()) {
31351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::Identifier:
31361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Import(FromName.getAsIdentifierInfo());
31371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCZeroArgSelector:
31391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCOneArgSelector:
31401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCMultiArgSelector:
31411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Import(FromName.getObjCSelector());
31421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXConstructorName: {
31441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
31451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
31461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
31471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXConstructorName(
31491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
31501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
31511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXDestructorName: {
31531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
31541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
31551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
31561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXDestructorName(
31581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
31591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
31601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXConversionFunctionName: {
31621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
31631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
31641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
31651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXConversionFunctionName(
31671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
31681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
31691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXOperatorName:
31711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXOperatorName(
31721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                          FromName.getCXXOverloadedOperator());
31731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXLiteralOperatorName:
31751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXLiteralOperatorName(
31761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                   Import(FromName.getCXXLiteralIdentifier()));
31771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXUsingDirective:
31791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: STATICS!
31801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return DeclarationName::getUsingDirectiveName();
31811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
31821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Silence bogus GCC warning
31841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return DeclarationName();
31851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
31861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31871b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorIdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) {
31881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!FromId)
31891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return 0;
31901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return &ToContext.Idents.get(FromId->getName());
31921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
3193089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
3194c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas GregorSelector ASTImporter::Import(Selector FromSel) {
3195c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (FromSel.isNull())
3196c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return Selector();
3197c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
3198c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  llvm::SmallVector<IdentifierInfo *, 4> Idents;
3199c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
3200c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
3201c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
3202c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
3203c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor}
3204c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
3205089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
3206089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                DeclContext *DC,
3207089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                unsigned IDNS,
3208089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                NamedDecl **Decls,
3209089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                unsigned NumDecls) {
3210089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  return Name;
3211089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
3212089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
3213089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
32144800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return Diags.Report(FullSourceLoc(Loc, ToContext.getSourceManager()),
32154800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                      DiagID);
3216089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
3217089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
3218089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
32194800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return Diags.Report(FullSourceLoc(Loc, FromContext.getSourceManager()),
32204800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                      DiagID);
3221089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
32225ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
32235ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas GregorDecl *ASTImporter::Imported(Decl *From, Decl *To) {
32245ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  ImportedDecls[From] = To;
32255ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  return To;
3226af667588d53de22795c5304f1496ccaac2a71402Daniel Dunbar}
3227ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
3228ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregorbool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
3229ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  llvm::DenseMap<Type *, Type *>::iterator Pos
3230ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor   = ImportedTypes.find(From.getTypePtr());
3231ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
3232ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return true;
3233ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
3234bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(FromContext, ToContext, Diags,
3235ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   NonEquivalentDecls);
3236bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(From, To);
3237ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor}
3238