ASTImporter.cpp revision 3fe104154dd2e8ffb351142d74f308938b5c99bf
11b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===//
21b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//
31b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//                     The LLVM Compiler Infrastructure
41b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//
51b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor// This file is distributed under the University of Illinois Open Source
61b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor// License. See LICENSE.TXT for details.
71b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//
81b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//===----------------------------------------------------------------------===//
91b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//
101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//  This file defines the ASTImporter class which imports AST nodes from one
111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//  context into another context.
121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//
131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//===----------------------------------------------------------------------===//
141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor#include "clang/AST/ASTImporter.h"
151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor#include "clang/AST/ASTContext.h"
17885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor#include "clang/AST/ASTDiagnostic.h"
1896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor#include "clang/AST/DeclCXX.h"
191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor#include "clang/AST/DeclObjC.h"
20089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor#include "clang/AST/DeclVisitor.h"
214800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor#include "clang/AST/StmtVisitor.h"
2282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor#include "clang/AST/TypeLoc.h"
231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor#include "clang/AST/TypeVisitor.h"
24885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor#include "clang/Basic/FileManager.h"
25885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor#include "clang/Basic/SourceManager.h"
26885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor#include "llvm/Support/MemoryBuffer.h"
2773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor#include <deque>
281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregorusing namespace clang;
301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregornamespace {
32089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
334800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                          public DeclVisitor<ASTNodeImporter, Decl *>,
344800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                          public StmtVisitor<ASTNodeImporter, Stmt *> {
351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ASTImporter &Importer;
361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  public:
381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    using TypeVisitor<ASTNodeImporter, QualType>::Visit;
419bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
424800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // Importing types
4589cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    QualType VisitType(Type *T);
461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitBuiltinType(BuiltinType *T);
471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitComplexType(ComplexType *T);
481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitPointerType(PointerType *T);
491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitBlockPointerType(BlockPointerType *T);
501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitLValueReferenceType(LValueReferenceType *T);
511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitRValueReferenceType(RValueReferenceType *T);
521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitMemberPointerType(MemberPointerType *T);
531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitConstantArrayType(ConstantArrayType *T);
541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitIncompleteArrayType(IncompleteArrayType *T);
551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitVariableArrayType(VariableArrayType *T);
561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentSizedArrayType
571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentSizedExtVectorType
581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitVectorType(VectorType *T);
591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitExtVectorType(ExtVectorType *T);
601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitFunctionNoProtoType(FunctionNoProtoType *T);
611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitFunctionProtoType(FunctionProtoType *T);
621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: UnresolvedUsingType
631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitTypedefType(TypedefType *T);
641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitTypeOfExprType(TypeOfExprType *T);
651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentTypeOfExprType
661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitTypeOfType(TypeOfType *T);
671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitDecltypeType(DecltypeType *T);
681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentDecltypeType
691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitRecordType(RecordType *T);
701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitEnumType(EnumType *T);
711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: TemplateTypeParmType
721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: SubstTemplateTypeParmType
731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: TemplateSpecializationType
74465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    QualType VisitElaboratedType(ElaboratedType *T);
754714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    // FIXME: DependentNameType
7633500955d731c73717af52088b7fc0e7a85681e7John McCall    // FIXME: DependentTemplateSpecializationType
771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitObjCInterfaceType(ObjCInterfaceType *T);
78c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    QualType VisitObjCObjectType(ObjCObjectType *T);
791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T);
80089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
81089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    // Importing declarations
82a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
83a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                         DeclContext *&LexicalDC, DeclarationName &Name,
84788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                         SourceLocation &Loc);
85083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    void ImportDeclContext(DeclContext *FromDC);
8696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
8773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
8889cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    Decl *VisitDecl(Decl *D);
89788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    Decl *VisitNamespaceDecl(NamespaceDecl *D);
909e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
9136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
9296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *VisitRecordDecl(RecordDecl *D);
9336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
94a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D);
95c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
96c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
97c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
98c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
9996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
1002e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
101089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
1022cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
103a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Decl *VisitParmVarDecl(ParmVarDecl *D);
104c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
105b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
1062e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
107a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
108e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
1092b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
110a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Decl *VisitObjCClassDecl(ObjCClassDecl *D);
111a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
1124800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    // Importing statements
1134800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Stmt *VisitStmt(Stmt *S);
1144800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
1154800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    // Importing expressions
1164800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Expr *VisitExpr(Expr *E);
117440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    Expr *VisitDeclRefExpr(DeclRefExpr *E);
1184800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Expr *VisitIntegerLiteral(IntegerLiteral *E);
119b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor    Expr *VisitCharacterLiteral(CharacterLiteral *E);
120f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitParenExpr(ParenExpr *E);
121f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitUnaryOperator(UnaryOperator *E);
122bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    Expr *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
123f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitBinaryOperator(BinaryOperator *E);
124f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
12536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
126008847a70ab122a99911149199855060fb3753b4Douglas Gregor    Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
1271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  };
1281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
1291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//----------------------------------------------------------------------------
13173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor// Structural Equivalence
13273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor//----------------------------------------------------------------------------
13373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
13473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregornamespace {
13573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  struct StructuralEquivalenceContext {
13673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief AST contexts for which we are checking structural equivalence.
13773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    ASTContext &C1, &C2;
13873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
13973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Diagnostic object used to emit diagnostics.
14073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Diagnostic &Diags;
14173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
14273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief The set of "tentative" equivalences between two canonical
14373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// declarations, mapping from a declaration in the first context to the
14473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// declaration in the second context that we believe to be equivalent.
14573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
14673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
14773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Queue of declarations in the first context whose equivalence
14873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// with a declaration in the second context still needs to be verified.
14973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    std::deque<Decl *> DeclsToCheck;
15073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
151ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    /// \brief Declaration (from, to) pairs that are known not to be equivalent
152ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    /// (which we have already complained about).
153ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
154ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
15573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Whether we're being strict about the spelling of types when
15673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// unifying two types.
15773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool StrictTypeSpelling;
15873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
15973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
16073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                 Diagnostic &Diags,
161ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor               llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
16273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                 bool StrictTypeSpelling = false)
163ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      : C1(C1), C2(C2), Diags(Diags), NonEquivalentDecls(NonEquivalentDecls),
164ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        StrictTypeSpelling(StrictTypeSpelling) { }
16573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
16673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Determine whether the two declarations are structurally
16773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// equivalent.
16873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
16973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
17073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Determine whether the two types are structurally equivalent.
17173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructurallyEquivalent(QualType T1, QualType T2);
17273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
17373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  private:
17473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Finish checking all of the structural equivalences.
17573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    ///
17673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \returns true if an error occurred, false otherwise.
17773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool Finish();
17873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
17973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  public:
18073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
18173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return Diags.Report(FullSourceLoc(Loc, C1.getSourceManager()), DiagID);
18273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
18373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
18473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
18573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return Diags.Report(FullSourceLoc(Loc, C2.getSourceManager()), DiagID);
18673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
18773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  };
18873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
18973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
19073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
19173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     QualType T1, QualType T2);
19273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
19373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Decl *D1, Decl *D2);
19473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
19573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine if two APInts have the same value, after zero-extending
19673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// one of them (if needed!) to ensure that the bit-widths match.
19773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
19873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() == I2.getBitWidth())
19973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == I2;
20073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
20173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() > I2.getBitWidth())
20273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == llvm::APInt(I2).zext(I1.getBitWidth());
20373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
20473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return llvm::APInt(I1).zext(I2.getBitWidth()) == I2;
20573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
20673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
20773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine if two APSInts have the same value, zero- or sign-extending
20873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// as needed.
20973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
21073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
21173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == I2;
21273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
21373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Check for a bit-width mismatch.
21473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() > I2.getBitWidth())
21573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return IsSameValue(I1, llvm::APSInt(I2).extend(I1.getBitWidth()));
21673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  else if (I2.getBitWidth() > I1.getBitWidth())
21773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return IsSameValue(llvm::APSInt(I1).extend(I2.getBitWidth()), I2);
21873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
21973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // We have a signedness mismatch. Turn the signed value into an unsigned
22073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // value.
22173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.isSigned()) {
22273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (I1.isNegative())
22373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
22473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
22573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return llvm::APSInt(I1, true) == I2;
22673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
22773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
22873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I2.isNegative())
22973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
23073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
23173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return I1 == llvm::APSInt(I2, true);
23273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
23373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
23473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two expressions.
23573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
23673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Expr *E1, Expr *E2) {
23773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!E1 || !E2)
23873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return E1 == E2;
23973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Actually perform a structural comparison!
24173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
24273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
24373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two identifiers are equivalent.
24573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
24673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const IdentifierInfo *Name2) {
24773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!Name1 || !Name2)
24873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return Name1 == Name2;
24973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
25073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return Name1->getName() == Name2->getName();
25173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
25273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
25373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two nested-name-specifiers are equivalent.
25473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
25573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     NestedNameSpecifier *NNS1,
25673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     NestedNameSpecifier *NNS2) {
25773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Implement!
25873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
25973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
26073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
26173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two template arguments are equivalent.
26273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
26373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const TemplateArgument &Arg1,
26473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const TemplateArgument &Arg2) {
26573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Implement!
26673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
26773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
26873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
26973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence for the common part of array
27073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// types.
27173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
27273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                          const ArrayType *Array1,
27373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                          const ArrayType *Array2) {
27473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!IsStructurallyEquivalent(Context,
27573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                Array1->getElementType(),
27673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                Array2->getElementType()))
27773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
27873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Array1->getSizeModifier() != Array2->getSizeModifier())
27973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
28073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
28173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
28273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
28373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
28473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
28573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
28673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two types.
28773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
28873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     QualType T1, QualType T2) {
28973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (T1.isNull() || T2.isNull())
29073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return T1.isNull() && T2.isNull();
29173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
29273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!Context.StrictTypeSpelling) {
29373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // We aren't being strict about token-to-token equivalence of types,
29473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // so map down to the canonical type.
29573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    T1 = Context.C1.getCanonicalType(T1);
29673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    T2 = Context.C2.getCanonicalType(T2);
29773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
29873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
29973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (T1.getQualifiers() != T2.getQualifiers())
30073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
30173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
302ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  Type::TypeClass TC = T1->getTypeClass();
303ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
304ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T1->getTypeClass() != T2->getTypeClass()) {
305ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // Compare function types with prototypes vs. without prototypes as if
306ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // both did not have prototypes.
307ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (T1->getTypeClass() == Type::FunctionProto &&
308ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        T2->getTypeClass() == Type::FunctionNoProto)
309ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      TC = Type::FunctionNoProto;
310ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    else if (T1->getTypeClass() == Type::FunctionNoProto &&
311ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor             T2->getTypeClass() == Type::FunctionProto)
312ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      TC = Type::FunctionNoProto;
313ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    else
314ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      return false;
315ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  }
31673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
317ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  switch (TC) {
318ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  case Type::Builtin:
31973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Deal with Char_S/Char_U.
32073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
32173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
32273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
32373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
32473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Complex:
32573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
32673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<ComplexType>(T1)->getElementType(),
32773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<ComplexType>(T2)->getElementType()))
32873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
32973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
33073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
33173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Pointer:
33273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
33373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<PointerType>(T1)->getPointeeType(),
33473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<PointerType>(T2)->getPointeeType()))
33573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
33673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
33773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
33873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::BlockPointer:
33973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
34073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<BlockPointerType>(T1)->getPointeeType(),
34173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<BlockPointerType>(T2)->getPointeeType()))
34273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
34373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
34473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
34573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::LValueReference:
34673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::RValueReference: {
34773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ReferenceType *Ref1 = cast<ReferenceType>(T1);
34873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ReferenceType *Ref2 = cast<ReferenceType>(T2);
34973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
35073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
35173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Ref1->isInnerRef() != Ref2->isInnerRef())
35273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
35373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
35473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ref1->getPointeeTypeAsWritten(),
35573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ref2->getPointeeTypeAsWritten()))
35673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
35773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
35873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
35973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
36073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::MemberPointer: {
36173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
36273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
36373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
36473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  MemPtr1->getPointeeType(),
36573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  MemPtr2->getPointeeType()))
36673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
36773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
36873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(MemPtr1->getClass(), 0),
36973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(MemPtr2->getClass(), 0)))
37073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
37173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
37273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
37373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
37473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ConstantArray: {
37573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
37673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
37773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsSameValue(Array1->getSize(), Array2->getSize()))
37873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
37973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
38073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
38173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
38273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
38373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
38473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
38573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::IncompleteArray:
38673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context,
38773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                       cast<ArrayType>(T1),
38873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                       cast<ArrayType>(T2)))
38973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
39073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
39173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
39273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::VariableArray: {
39373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
39473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
39573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
39673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Array1->getSizeExpr(), Array2->getSizeExpr()))
39773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
39873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
39973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
40073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
40173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
40273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
40373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
40473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
40573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::DependentSizedArray: {
40673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
40773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
40873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
40973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Array1->getSizeExpr(), Array2->getSizeExpr()))
41073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
41173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
41273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
41373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
41473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
41573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
41673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
41773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
41873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::DependentSizedExtVector: {
41973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedExtVectorType *Vec1
42073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<DependentSizedExtVectorType>(T1);
42173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedExtVectorType *Vec2
42273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<DependentSizedExtVectorType>(T2);
42373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
42473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getSizeExpr(), Vec2->getSizeExpr()))
42573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
42673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
42773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getElementType(),
42873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec2->getElementType()))
42973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
43073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
43173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
43273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
43373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Vector:
43473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ExtVector: {
43573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VectorType *Vec1 = cast<VectorType>(T1);
43673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VectorType *Vec2 = cast<VectorType>(T2);
43773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
43873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getElementType(),
43973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec2->getElementType()))
44073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
44173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Vec1->getNumElements() != Vec2->getNumElements())
44273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
443788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    if (Vec1->getAltiVecSpecific() != Vec2->getAltiVecSpecific())
44473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
4450e12b44081c6395a6d60a05a85a6012f7bb23b16Douglas Gregor    break;
44673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
44773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
44873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::FunctionProto: {
44973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
45073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
45173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getNumArgs() != Proto2->getNumArgs())
45273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
45373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
45473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
45573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto1->getArgType(I),
45673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto2->getArgType(I)))
45773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
45873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
45973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->isVariadic() != Proto2->isVariadic())
46073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
46273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
46473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
46673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
46873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
46973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto1->getExceptionType(I),
47073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto2->getExceptionType(I)))
47173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
47273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
47373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
47473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
47573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
47673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Fall through to check the bits common with FunctionNoProtoType.
47773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
47873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
47973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::FunctionNoProto: {
48073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionType *Function1 = cast<FunctionType>(T1);
48173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionType *Function2 = cast<FunctionType>(T2);
48273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
48373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Function1->getResultType(),
48473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Function2->getResultType()))
48573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
486264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      if (Function1->getExtInfo() != Function2->getExtInfo())
487264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola        return false;
48873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
48973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
49073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
49173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::UnresolvedUsing:
49273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
49373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<UnresolvedUsingType>(T1)->getDecl(),
49473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<UnresolvedUsingType>(T2)->getDecl()))
49573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
49673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
49773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
49873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
49973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Typedef:
50073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
50173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypedefType>(T1)->getDecl(),
50273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypedefType>(T2)->getDecl()))
50373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
50473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
50573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
50673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TypeOfExpr:
50773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
50873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
50973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
51073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
51173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
51273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
51373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TypeOf:
51473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
51573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypeOfType>(T1)->getUnderlyingType(),
51673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypeOfType>(T2)->getUnderlyingType()))
51773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
51873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
51973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
52073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Decltype:
52173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
52273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<DecltypeType>(T1)->getUnderlyingExpr(),
52373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<DecltypeType>(T2)->getUnderlyingExpr()))
52473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
52573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
52673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
52773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Record:
52873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Enum:
52973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
53073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TagType>(T1)->getDecl(),
53173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TagType>(T2)->getDecl()))
53273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
53373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
534465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
53573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TemplateTypeParm: {
53673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
53773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
53873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->getDepth() != Parm2->getDepth())
53973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
54073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->getIndex() != Parm2->getIndex())
54173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
54273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->isParameterPack() != Parm2->isParameterPack())
54373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
54473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
54573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Names of template type parameters are never significant.
54673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
54773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
54873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
54973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::SubstTemplateTypeParm: {
55073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const SubstTemplateTypeParmType *Subst1
55173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<SubstTemplateTypeParmType>(T1);
55273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const SubstTemplateTypeParmType *Subst2
55373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<SubstTemplateTypeParmType>(T2);
55473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
55573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Subst1->getReplacedParameter(), 0),
55673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Subst2->getReplacedParameter(), 0)))
55773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
55873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
55973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Subst1->getReplacementType(),
56073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Subst2->getReplacementType()))
56173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
56273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
56373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
56473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
56573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TemplateSpecialization: {
56673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateSpecializationType *Spec1
56773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<TemplateSpecializationType>(T1);
56873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateSpecializationType *Spec2
56973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<TemplateSpecializationType>(T2);
57073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
57173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Spec1->getTemplateName(),
57273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Spec2->getTemplateName()))
57373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
57473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Spec1->getNumArgs() != Spec2->getNumArgs())
57573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
57673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
57773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
57873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Spec1->getArg(I), Spec2->getArg(I)))
57973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
58073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
58173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
58273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
58373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
584465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Type::Elaborated: {
585465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
586465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
587465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    // CHECKME: what if a keyword is ETK_None or ETK_typename ?
588465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Elab1->getKeyword() != Elab2->getKeyword())
589465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return false;
59073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
591465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab1->getQualifier(),
592465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab2->getQualifier()))
59373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
59473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
595465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab1->getNamedType(),
596465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab2->getNamedType()))
59773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
59873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
59973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
60073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
6013cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  case Type::InjectedClassName: {
6023cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
6033cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
6043cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (!IsStructurallyEquivalent(Context,
60531f17ecbef57b5679c017c375db330546b7b5145John McCall                                  Inj1->getInjectedSpecializationType(),
60631f17ecbef57b5679c017c375db330546b7b5145John McCall                                  Inj2->getInjectedSpecializationType()))
6073cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      return false;
6083cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    break;
6093cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
6103cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
6114714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  case Type::DependentName: {
6124714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    const DependentNameType *Typename1 = cast<DependentNameType>(T1);
6134714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    const DependentNameType *Typename2 = cast<DependentNameType>(T2);
61473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
61573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename1->getQualifier(),
61673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename2->getQualifier()))
61773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
61873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
61973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename2->getIdentifier()))
62073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
62173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
62273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
62373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
62473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
62533500955d731c73717af52088b7fc0e7a85681e7John McCall  case Type::DependentTemplateSpecialization: {
62633500955d731c73717af52088b7fc0e7a85681e7John McCall    const DependentTemplateSpecializationType *Spec1 =
62733500955d731c73717af52088b7fc0e7a85681e7John McCall      cast<DependentTemplateSpecializationType>(T1);
62833500955d731c73717af52088b7fc0e7a85681e7John McCall    const DependentTemplateSpecializationType *Spec2 =
62933500955d731c73717af52088b7fc0e7a85681e7John McCall      cast<DependentTemplateSpecializationType>(T2);
63033500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!IsStructurallyEquivalent(Context,
63133500955d731c73717af52088b7fc0e7a85681e7John McCall                                  Spec1->getQualifier(),
63233500955d731c73717af52088b7fc0e7a85681e7John McCall                                  Spec2->getQualifier()))
63333500955d731c73717af52088b7fc0e7a85681e7John McCall      return false;
63433500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
63533500955d731c73717af52088b7fc0e7a85681e7John McCall                                  Spec2->getIdentifier()))
63633500955d731c73717af52088b7fc0e7a85681e7John McCall      return false;
63733500955d731c73717af52088b7fc0e7a85681e7John McCall    if (Spec1->getNumArgs() != Spec2->getNumArgs())
63833500955d731c73717af52088b7fc0e7a85681e7John McCall      return false;
63933500955d731c73717af52088b7fc0e7a85681e7John McCall    for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
64033500955d731c73717af52088b7fc0e7a85681e7John McCall      if (!IsStructurallyEquivalent(Context,
64133500955d731c73717af52088b7fc0e7a85681e7John McCall                                    Spec1->getArg(I), Spec2->getArg(I)))
64233500955d731c73717af52088b7fc0e7a85681e7John McCall        return false;
64333500955d731c73717af52088b7fc0e7a85681e7John McCall    }
64433500955d731c73717af52088b7fc0e7a85681e7John McCall    break;
64533500955d731c73717af52088b7fc0e7a85681e7John McCall  }
64633500955d731c73717af52088b7fc0e7a85681e7John McCall
64773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ObjCInterface: {
64873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
64973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
65073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
65173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Iface1->getDecl(), Iface2->getDecl()))
65273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
653c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    break;
654c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  }
655c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
656c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject: {
657c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
658c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
659c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (!IsStructurallyEquivalent(Context,
660c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                  Obj1->getBaseType(),
661c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                  Obj2->getBaseType()))
662c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return false;
663c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
66473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
665c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
66673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
667c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Obj1->getProtocol(I),
668c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Obj2->getProtocol(I)))
66973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
67073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
67173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
67273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
67373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
67473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ObjCObjectPointer: {
67573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
67673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
67773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
67873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ptr1->getPointeeType(),
67973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ptr2->getPointeeType()))
68073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
68173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
68273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
68373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
68473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  } // end switch
68573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
68673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
68773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
68873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
68973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two records.
69073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
69173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     RecordDecl *D1, RecordDecl *D2) {
69273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (D1->isUnion() != D2->isUnion()) {
69373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
69473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
69573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
69673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << D1->getDeclName() << (unsigned)D1->getTagKind();
69773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
69873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
69973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
700ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Compare the definitions of these two records. If either or both are
701ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // incomplete, we assume that they are equivalent.
702ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  D1 = D1->getDefinition();
703ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  D2 = D2->getDefinition();
704ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (!D1 || !D2)
705ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return true;
706ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
70773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
70873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
70973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
71073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
71173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
71273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
71373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << D2CXX->getNumBases();
71473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
71573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << D1CXX->getNumBases();
71673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
71773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
71873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
71973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      // Check the base classes.
72073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
72173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                           BaseEnd1 = D1CXX->bases_end(),
72273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                Base2 = D2CXX->bases_begin();
72373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor           Base1 != BaseEnd1;
72473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor           ++Base1, ++Base2) {
72573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!IsStructurallyEquivalent(Context,
72673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                      Base1->getType(), Base2->getType())) {
72773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
72873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Context.C2.getTypeDeclType(D2);
72973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
73073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->getType()
73173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->getSourceRange();
73273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
73373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getType()
73473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getSourceRange();
73573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          return false;
73673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        }
73773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
73873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check virtual vs. non-virtual inheritance mismatch.
73973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (Base1->isVirtual() != Base2->isVirtual()) {
74073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
74173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Context.C2.getTypeDeclType(D2);
74273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(Base2->getSourceRange().getBegin(),
74373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                        diag::note_odr_virtual_base)
74473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->isVirtual() << Base2->getSourceRange();
74573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
74673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->isVirtual()
74773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getSourceRange();
74873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          return false;
74973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        }
75073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
75173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    } else if (D1CXX->getNumBases() > 0) {
75273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
75373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
75473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
75573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
75673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Base1->getType()
75773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Base1->getSourceRange();
75873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
75973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
76073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
76173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
76273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
76373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Check the fields for consistency.
76473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
76573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                             Field2End = D2->field_end();
76673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
76773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Field1End = D1->field_end();
76873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       Field1 != Field1End;
76973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       ++Field1, ++Field2) {
77073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field2 == Field2End) {
77173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
77273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
77373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Field1->getLocation(), diag::note_odr_field)
77473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName() << Field1->getType();
77573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
77673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
77773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
77873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
77973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
78073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Field1->getType(), Field2->getType())) {
78173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
78273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
78373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(Field2->getLocation(), diag::note_odr_field)
78473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field2->getDeclName() << Field2->getType();
78573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Field1->getLocation(), diag::note_odr_field)
78673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName() << Field1->getType();
78773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
78873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
78973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
79073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field1->isBitField() != Field2->isBitField()) {
79173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
79273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
79373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (Field1->isBitField()) {
79473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        llvm::APSInt Bits;
79573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
79673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
79773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field1->getDeclName() << Field1->getType()
79873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits.toString(10, false);
79973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
80073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName();
80173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
80273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        llvm::APSInt Bits;
80373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
80473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
80573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName() << Field2->getType()
80673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits.toString(10, false);
80773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(),
80873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                          diag::note_odr_not_bit_field)
80973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName();
81073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
81173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
81273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
81373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
81473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field1->isBitField()) {
81573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      // Make sure that the bit-fields are the same length.
81673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      llvm::APSInt Bits1, Bits2;
81773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
81873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
81973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
82073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
82173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
82273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsSameValue(Bits1, Bits2)) {
82373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
82473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Context.C2.getTypeDeclType(D2);
82573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
82673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName() << Field2->getType()
82773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits2.toString(10, false);
82873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
82973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field1->getDeclName() << Field1->getType()
83073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits1.toString(10, false);
83173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
83273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
83373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
83473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
83573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
83673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Field2 != Field2End) {
83773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
83873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
83973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(Field2->getLocation(), diag::note_odr_field)
84073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Field2->getDeclName() << Field2->getType();
84173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
84273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
84373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
84473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
84573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
84673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
84773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
84873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two enums.
84973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
85073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     EnumDecl *D1, EnumDecl *D2) {
85173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
85273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                             EC2End = D2->enumerator_end();
85373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
85473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  EC1End = D1->enumerator_end();
85573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       EC1 != EC1End; ++EC1, ++EC2) {
85673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (EC2 == EC2End) {
85773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
85873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
85973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
86073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getDeclName()
86173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getInitVal().toString(10);
86273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
86373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
86473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
86573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
86673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::APSInt Val1 = EC1->getInitVal();
86773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::APSInt Val2 = EC2->getInitVal();
86873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsSameValue(Val1, Val2) ||
86973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
87073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
87173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
87273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
87373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC2->getDeclName()
87473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC2->getInitVal().toString(10);
87573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
87673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getDeclName()
87773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getInitVal().toString(10);
87873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
87973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
88073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
88173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
88273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (EC2 != EC2End) {
88373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
88473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
88573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
88673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << EC2->getDeclName()
88773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << EC2->getInitVal().toString(10);
88873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
88973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
89073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
89173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
89273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
89373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
89473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
89573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two declarations.
89673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
89773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Decl *D1, Decl *D2) {
89873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Check for known structural equivalences via a callback of some sort.
89973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
900ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Check whether we already know that these two declarations are not
901ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // structurally equivalent.
902ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
903ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                      D2->getCanonicalDecl())))
904ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return false;
905ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
90673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Determine whether we've already produced a tentative equivalence for D1.
90773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
90873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (EquivToD1)
90973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return EquivToD1 == D2->getCanonicalDecl();
91073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
91173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Produce a tentative equivalence D1 <-> D2, which will be checked later.
91273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EquivToD1 = D2->getCanonicalDecl();
91373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
91473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
91573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
91673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
91773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
91873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                            Decl *D2) {
91973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!::IsStructurallyEquivalent(*this, D1, D2))
92073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
92173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
92273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return !Finish();
92373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
92473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
92573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
92673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                            QualType T2) {
92773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!::IsStructurallyEquivalent(*this, T1, T2))
92873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
92973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
93073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return !Finish();
93173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
93273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
93373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::Finish() {
93473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  while (!DeclsToCheck.empty()) {
93573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Check the next declaration.
93673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Decl *D1 = DeclsToCheck.front();
93773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DeclsToCheck.pop_front();
93873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
93973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Decl *D2 = TentativeEquivalences[D1];
94073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    assert(D2 && "Unrecorded tentative equivalence?");
94173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
942ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    bool Equivalent = true;
943ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
94473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Switch on all declaration kinds. For now, we're just going to
94573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // check the obvious ones.
94673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
94773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
94873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check for equivalent structure names.
94973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name1 = Record1->getIdentifier();
95073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name1 && Record1->getTypedefForAnonDecl())
95173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
95273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name2 = Record2->getIdentifier();
95373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name2 && Record2->getTypedefForAnonDecl())
95473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
955ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (!::IsStructurallyEquivalent(Name1, Name2) ||
956ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this, Record1, Record2))
957ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
95873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
95973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Record/non-record mismatch.
960ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
96173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
962ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
96373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
96473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check for equivalent enum names.
96573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name1 = Enum1->getIdentifier();
96673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name1 && Enum1->getTypedefForAnonDecl())
96773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
96873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name2 = Enum2->getIdentifier();
96973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name2 && Enum2->getTypedefForAnonDecl())
97073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
971ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (!::IsStructurallyEquivalent(Name1, Name2) ||
972ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this, Enum1, Enum2))
973ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
97473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
97573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Enum/non-enum mismatch
976ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
97773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
978ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
97973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
98073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
981ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                        Typedef2->getIdentifier()) ||
982ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this,
98373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        Typedef1->getUnderlyingType(),
98473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        Typedef2->getUnderlyingType()))
985ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
98673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
98773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Typedef/non-typedef mismatch.
988ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
98973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
99073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
991ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
992ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (!Equivalent) {
993ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      // Note that these two declarations are not equivalent (and we already
994ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      // know about it).
995ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
996ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                               D2->getCanonicalDecl()));
997ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      return true;
998ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    }
99973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Check other declaration kinds!
100073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
100173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
100273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return false;
100373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
100473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
100573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor//----------------------------------------------------------------------------
10061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor// Import Types
10071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//----------------------------------------------------------------------------
10081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
100989cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas GregorQualType ASTNodeImporter::VisitType(Type *T) {
101089cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor  Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
101189cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    << T->getTypeClassName();
101289cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor  return QualType();
101389cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor}
101489cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor
10151b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
10161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  switch (T->getKind()) {
10171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Void: return Importer.getToContext().VoidTy;
10181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Bool: return Importer.getToContext().BoolTy;
10191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char_U:
10211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // The context we're importing from has an unsigned 'char'. If we're
10221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // importing into a context with a signed 'char', translate to
10231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // 'unsigned char' instead.
10241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (Importer.getToContext().getLangOptions().CharIsSigned)
10251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return Importer.getToContext().UnsignedCharTy;
10261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().CharTy;
10281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
10301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char16:
10321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++!
10331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().Char16Ty;
10341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char32:
10361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++!
10371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().Char32Ty;
10381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
10401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
10411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
10421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ULongLong:
10431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().UnsignedLongLongTy;
10441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
10451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char_S:
10471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // The context we're importing from has an unsigned 'char'. If we're
10481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // importing into a context with a signed 'char', translate to
10491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // 'unsigned char' instead.
10501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Importer.getToContext().getLangOptions().CharIsSigned)
10511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return Importer.getToContext().SignedCharTy;
10521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().CharTy;
10541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
10561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::WChar:
10571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: If not in C++, shall we translate to the C equivalent of
10581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // wchar_t?
10591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().WCharTy;
10601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Short : return Importer.getToContext().ShortTy;
10621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Int : return Importer.getToContext().IntTy;
10631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Long : return Importer.getToContext().LongTy;
10641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
10651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
10661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Float: return Importer.getToContext().FloatTy;
10671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Double: return Importer.getToContext().DoubleTy;
10681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
10691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::NullPtr:
10711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++0x!
10721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().NullPtrTy;
10731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
10751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
10761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UndeducedAuto:
10771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++0x!
10781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().UndeducedAutoTy;
10791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCId:
10811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports Objective-C!
10821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinIdTy;
10831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCClass:
10851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinClassTy;
10861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCSel:
10881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinSelTy;
10891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
10901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return QualType();
10921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
10931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10941b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
10951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
10961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
10971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
10981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getComplexType(ToElementType);
11001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11021b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitPointerType(PointerType *T) {
11031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
11041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
11051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getPointerType(ToPointeeType);
11081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11101b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
11111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for blocks support in "to" context.
11121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
11131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
11141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getBlockPointerType(ToPointeeType);
11171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11191b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
11201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++ support in "to" context.
11211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
11221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
11231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getLValueReferenceType(ToPointeeType);
11261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11281b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
11291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++0x support in "to" context.
11301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
11311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
11321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getRValueReferenceType(ToPointeeType);
11351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11371b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
11381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++ support in "to" context.
11391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
11401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
11411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
11441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getMemberPointerType(ToPointeeType,
11451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      ClassType.getTypePtr());
11461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11481b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
11491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getConstantArrayType(ToElementType,
11541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSize(),
11551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSizeModifier(),
11561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->getIndexTypeCVRQualifiers());
11571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11591b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
11601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getIncompleteArrayType(ToElementType,
11651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                        T->getSizeModifier(),
11661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                T->getIndexTypeCVRQualifiers());
11671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11691b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
11701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *Size = Importer.Import(T->getSizeExpr());
11751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!Size)
11761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  SourceRange Brackets = Importer.Import(T->getBracketsRange());
11791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getVariableArrayType(ToElementType, Size,
11801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSizeModifier(),
11811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                T->getIndexTypeCVRQualifiers(),
11821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      Brackets);
11831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11851b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitVectorType(VectorType *T) {
11861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getVectorType(ToElementType,
11911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->getNumElements(),
1192788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner                                               T->getAltiVecSpecific());
11931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11951b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
11961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getExtVectorType(ToElementType,
12011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                  T->getNumElements());
12021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12041b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
12051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: What happens if we're importing a function without a prototype
12061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // into C++? Should we make it variadic?
12071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToResultType = Importer.Import(T->getResultType());
12081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToResultType.isNull())
12091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
1210264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola
12111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getFunctionNoProtoType(ToResultType,
1212264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                        T->getExtInfo());
12131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12151b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
12161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToResultType = Importer.Import(T->getResultType());
12171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToResultType.isNull())
12181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Import argument types
12211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<QualType, 4> ArgTypes;
12221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
12231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                         AEnd = T->arg_type_end();
12241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       A != AEnd; ++A) {
12251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType ArgType = Importer.Import(*A);
12261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (ArgType.isNull())
12271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
12281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ArgTypes.push_back(ArgType);
12291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
12301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Import exception types
12321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<QualType, 4> ExceptionTypes;
12331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (FunctionProtoType::exception_iterator E = T->exception_begin(),
12341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                          EEnd = T->exception_end();
12351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       E != EEnd; ++E) {
12361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType ExceptionType = Importer.Import(*E);
12371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (ExceptionType.isNull())
12381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
12391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ExceptionTypes.push_back(ExceptionType);
12401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
12411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
12431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 ArgTypes.size(),
12441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->isVariadic(),
12451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->getTypeQuals(),
12461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->hasExceptionSpec(),
12471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->hasAnyExceptionSpec(),
12481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 ExceptionTypes.size(),
12491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 ExceptionTypes.data(),
1250264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                 T->getExtInfo());
12511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12531b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
12541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  TypedefDecl *ToDecl
12551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
12561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
12571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeDeclType(ToDecl);
12601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12621b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
12631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
12641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToExpr)
12651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeOfExprType(ToExpr);
12681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12701b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
12711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
12721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToUnderlyingType.isNull())
12731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeOfType(ToUnderlyingType);
12761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12781b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
12791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
12801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToExpr)
12811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getDecltypeType(ToExpr);
12841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12861b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitRecordType(RecordType *T) {
12871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  RecordDecl *ToDecl
12881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
12891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
12901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTagDeclType(ToDecl);
12931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12951b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitEnumType(EnumType *T) {
12961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  EnumDecl *ToDecl
12971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
12981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
12991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTagDeclType(ToDecl);
13021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13041b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
1305465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *ToQualifier = 0;
1306465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  // Note: the qualifier in an ElaboratedType is optional.
1307465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T->getQualifier()) {
1308465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ToQualifier = Importer.Import(T->getQualifier());
1309465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (!ToQualifier)
1310465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return QualType();
1311465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
13121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToNamedType = Importer.Import(T->getNamedType());
13141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToNamedType.isNull())
13151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1317465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Importer.getToContext().getElaboratedType(T->getKeyword(),
1318465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                                   ToQualifier, ToNamedType);
13191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13211b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
13221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  ObjCInterfaceDecl *Class
13231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
13241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!Class)
13251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1327c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCInterfaceType(Class);
1328c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
1329c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
1330c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) {
1331c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  QualType ToBaseType = Importer.Import(T->getBaseType());
1332c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ToBaseType.isNull())
1333c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType();
1334c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
13351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
1336c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator P = T->qual_begin(),
13371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                     PEnd = T->qual_end();
13381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       P != PEnd; ++P) {
13391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ObjCProtocolDecl *Protocol
13401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
13411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Protocol)
13421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
13431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    Protocols.push_back(Protocol);
13441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
13451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1346c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCObjectType(ToBaseType,
1347c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                                   Protocols.data(),
1348c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                                   Protocols.size());
13491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13511b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
13521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
13531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
13541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1356c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
13571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1359089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor//----------------------------------------------------------------------------
1360089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor// Import Declarations
1361089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor//----------------------------------------------------------------------------
1362a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregorbool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1363a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      DeclContext *&LexicalDC,
1364a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      DeclarationName &Name,
1365a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      SourceLocation &Loc) {
1366089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Import the context of this declaration.
1367a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DC = Importer.ImportContext(D->getDeclContext());
1368089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  if (!DC)
1369a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return true;
1370a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1371a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  LexicalDC = DC;
13729bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
13739bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
13749bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    if (!LexicalDC)
1375a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      return true;
13769bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  }
1377a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1378089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Import the name of this declaration.
1379a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Name = Importer.Import(D->getDeclName());
1380089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  if (D->getDeclName() && !Name)
1381a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return true;
1382a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1383a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the location of this declaration.
1384a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Loc = Importer.Import(D->getLocation());
1385a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  return false;
1386a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1387a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1388083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregorvoid ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1389083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1390083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor                               FromEnd = FromDC->decls_end();
1391083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor       From != FromEnd;
1392083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor       ++From)
1393083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    Importer.Import(*From);
1394083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor}
1395083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor
139696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregorbool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
139773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        RecordDecl *ToRecord) {
1398bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
139973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Importer.getToContext(),
1400ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getDiags(),
1401ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getNonEquivalentDecls());
1402bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
140396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
140496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
140536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregorbool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
1406bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
140773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Importer.getToContext(),
1408ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getDiags(),
1409ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getNonEquivalentDecls());
1410bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
141136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
141236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
1413a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitDecl(Decl *D) {
1414a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1415a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    << D->getDeclKindName();
1416a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  return 0;
1417a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1418a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1419788c62d1e87bfb596078817237f672a5f000999aDouglas GregorDecl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1420788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  // Import the major distinguishing characteristics of this namespace.
1421788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  DeclContext *DC, *LexicalDC;
1422788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  DeclarationName Name;
1423788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  SourceLocation Loc;
1424788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1425788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    return 0;
1426788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1427788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  NamespaceDecl *MergeWithNamespace = 0;
1428788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (!Name) {
1429788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // This is an anonymous namespace. Adopt an existing anonymous
1430788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // namespace if we can.
1431788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // FIXME: Not testable.
1432788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1433788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      MergeWithNamespace = TU->getAnonymousNamespace();
1434788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    else
1435788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1436788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  } else {
1437788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1438788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1439788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor         Lookup.first != Lookup.second;
1440788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor         ++Lookup.first) {
14410d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
1442788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        continue;
1443788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1444788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1445788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        MergeWithNamespace = FoundNS;
1446788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        ConflictingDecls.clear();
1447788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        break;
1448788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      }
1449788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1450788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
1451788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
1452788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1453788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (!ConflictingDecls.empty()) {
14540d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
1455788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                         ConflictingDecls.data(),
1456788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                         ConflictingDecls.size());
1457788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
1458788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  }
1459788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1460788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  // Create the "to" namespace, if needed.
1461788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  NamespaceDecl *ToNamespace = MergeWithNamespace;
1462788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (!ToNamespace) {
1463788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1464788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                        Name.getAsIdentifierInfo());
1465788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    ToNamespace->setLexicalDeclContext(LexicalDC);
1466788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    LexicalDC->addDecl(ToNamespace);
1467788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1468788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // If this is an anonymous namespace, register it as the anonymous
1469788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // namespace within its context.
1470788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (!Name) {
1471788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1472788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        TU->setAnonymousNamespace(ToNamespace);
1473788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      else
1474788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1475788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
1476788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  }
1477788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  Importer.Imported(D, ToNamespace);
1478788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1479788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  ImportDeclContext(D);
1480788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1481788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  return ToNamespace;
1482788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor}
1483788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
14849e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas GregorDecl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
14859e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // Import the major distinguishing characteristics of this typedef.
14869e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  DeclContext *DC, *LexicalDC;
14879e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  DeclarationName Name;
14889e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  SourceLocation Loc;
14899e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
14909e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    return 0;
14919e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
14929e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // If this typedef is not in block scope, determine whether we've
14939e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // seen a typedef with the same name (that we can merge with) or any
14949e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // other entity by that name (which name lookup could conflict with).
14959e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  if (!DC->isFunctionOrMethod()) {
14969e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
14979e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
14989e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
14999e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor         Lookup.first != Lookup.second;
15009e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor         ++Lookup.first) {
15019e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
15029e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor        continue;
15039e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
1504ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1505ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                            FoundTypedef->getUnderlyingType()))
15065ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor          return Importer.Imported(D, FoundTypedef);
15079e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      }
15089e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
15099e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
15109e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    }
15119e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
15129e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    if (!ConflictingDecls.empty()) {
15139e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
15149e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                         ConflictingDecls.data(),
15159e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                         ConflictingDecls.size());
15169e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (!Name)
15179e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor        return 0;
15189e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    }
15199e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  }
15209e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
1521ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the underlying type of this typedef;
1522ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getUnderlyingType());
1523ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
1524ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
1525ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
15269e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // Create the new typedef node.
15279e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
15289e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
15299e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                               Loc, Name.getAsIdentifierInfo(),
15309e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                               TInfo);
1531325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToTypedef->setAccess(D->getAccess());
15329e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  ToTypedef->setLexicalDeclContext(LexicalDC);
15335ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToTypedef);
15349e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  LexicalDC->addDecl(ToTypedef);
1535ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
15369e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  return ToTypedef;
15379e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor}
15389e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
153936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorDecl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
154036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the major distinguishing characteristics of this enum.
154136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclContext *DC, *LexicalDC;
154236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName Name;
154336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  SourceLocation Loc;
154436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
154536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
154636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
154736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Figure out what enum name we're looking for.
154836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  unsigned IDNS = Decl::IDNS_Tag;
154936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName SearchName = Name;
155036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!SearchName && D->getTypedefForAnonDecl()) {
155136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
155236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    IDNS = Decl::IDNS_Ordinary;
155336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  } else if (Importer.getToContext().getLangOptions().CPlusPlus)
155436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    IDNS |= Decl::IDNS_Ordinary;
155536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
155636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // We may already have an enum of the same name; try to find and match it.
155736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!DC->isFunctionOrMethod() && SearchName) {
155836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
155936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
156036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         Lookup.first != Lookup.second;
156136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++Lookup.first) {
156236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
156336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        continue;
156436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
156536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Decl *Found = *Lookup.first;
156636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
156736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
156836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor          Found = Tag->getDecl();
156936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      }
157036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
157136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
15725ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor        if (IsStructuralMatch(D, FoundEnum))
15735ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor          return Importer.Imported(D, FoundEnum);
157436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      }
157536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
157636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
157736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
157836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
157936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (!ConflictingDecls.empty()) {
158036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
158136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.data(),
158236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.size());
158336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
158436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
158536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
158636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Create the enum declaration.
158773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
158836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                      Name.getAsIdentifierInfo(),
158936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                      Importer.Import(D->getTagKeywordLoc()),
159036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                      0);
1591b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
1592b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (D->getQualifier()) {
1593b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1594b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1595b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    D2->setQualifierInfo(NNS, NNSRange);
1596b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1597325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  D2->setAccess(D->getAccess());
159873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  D2->setLexicalDeclContext(LexicalDC);
159973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Importer.Imported(D, D2);
160073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  LexicalDC->addDecl(D2);
160136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
160236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the integer type.
160336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  QualType ToIntegerType = Importer.Import(D->getIntegerType());
160436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (ToIntegerType.isNull())
160536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
160673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  D2->setIntegerType(ToIntegerType);
160736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
160836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the definition
160936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (D->isDefinition()) {
161036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
161136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (T.isNull())
161236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      return 0;
161336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
161436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    QualType ToPromotionType = Importer.Import(D->getPromotionType());
161536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (ToPromotionType.isNull())
161636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      return 0;
161736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
161873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->startDefinition();
1619083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    ImportDeclContext(D);
16201b5a618c59025898806160ed5e7f0ff5bb79e482John McCall
16211b5a618c59025898806160ed5e7f0ff5bb79e482John McCall    // FIXME: we might need to merge the number of positive or negative bits
16221b5a618c59025898806160ed5e7f0ff5bb79e482John McCall    // if the enumerator lists don't match.
16231b5a618c59025898806160ed5e7f0ff5bb79e482John McCall    D2->completeDefinition(T, ToPromotionType,
16241b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                           D->getNumPositiveBits(),
16251b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                           D->getNumNegativeBits());
162636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
162736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
162873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return D2;
162936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
163036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
163196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas GregorDecl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
163296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // If this record has a definition in the translation unit we're coming from,
163396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // but this particular declaration is not that definition, import the
163496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // definition and map to that.
1635952b017601f9c82b51119c3a1600f1312a833db9Douglas Gregor  TagDecl *Definition = D->getDefinition();
163696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (Definition && Definition != D) {
163796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *ImportedDef = Importer.Import(Definition);
16385ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor    if (!ImportedDef)
16395ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor      return 0;
16405ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
16415ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor    return Importer.Imported(D, ImportedDef);
164296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
164396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
164496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Import the major distinguishing characteristics of this record.
164596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclContext *DC, *LexicalDC;
164696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName Name;
164796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  SourceLocation Loc;
164896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
164996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
165096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
165196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Figure out what structure name we're looking for.
165296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  unsigned IDNS = Decl::IDNS_Tag;
165396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName SearchName = Name;
165496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!SearchName && D->getTypedefForAnonDecl()) {
165596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
165696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    IDNS = Decl::IDNS_Ordinary;
165796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  } else if (Importer.getToContext().getLangOptions().CPlusPlus)
165896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    IDNS |= Decl::IDNS_Ordinary;
165996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
166096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // We may already have a record of the same name; try to find and match it.
1661e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor  RecordDecl *AdoptDecl = 0;
166296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!DC->isFunctionOrMethod() && SearchName) {
166396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
166496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
166596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         Lookup.first != Lookup.second;
166696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         ++Lookup.first) {
166796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
166896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor        continue;
166996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
167096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Decl *Found = *Lookup.first;
167196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
167296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor        if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
167396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor          Found = Tag->getDecl();
167496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
167596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
167696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
1677e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
1678e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
1679e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // The record types structurally match, or the "from" translation
1680e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // unit only had a forward declaration anyway; call it the same
1681e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // function.
1682e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // FIXME: For C++, we should also merge methods here.
16835ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor            return Importer.Imported(D, FoundDef);
1684e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          }
1685e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        } else {
1686e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          // We have a forward declaration of this type, so adopt that forward
1687e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          // declaration rather than building a new one.
1688e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          AdoptDecl = FoundRecord;
1689e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          continue;
1690e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        }
169196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
169296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
169396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
169496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
169596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
169696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    if (!ConflictingDecls.empty()) {
169796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
169896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         ConflictingDecls.data(),
169996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         ConflictingDecls.size());
170096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
170196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
170296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
170396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Create the record declaration.
170473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  RecordDecl *D2 = AdoptDecl;
170573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!D2) {
17065250f27420386452a21692a6292c99ee7febdac4John McCall    if (isa<CXXRecordDecl>(D)) {
170773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
1708e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   D->getTagKind(),
1709e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   DC, Loc,
1710e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   Name.getAsIdentifierInfo(),
171196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                        Importer.Import(D->getTagKeywordLoc()));
171273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      D2 = D2CXX;
1713325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor      D2->setAccess(D->getAccess());
1714e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor    } else {
171573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
1716e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                    DC, Loc,
1717e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                    Name.getAsIdentifierInfo(),
1718e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                    Importer.Import(D->getTagKeywordLoc()));
171996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
1720b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Import the qualifier, if any.
1721b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (D->getQualifier()) {
1722b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1723b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1724b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      D2->setQualifierInfo(NNS, NNSRange);
1725b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
172673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->setLexicalDeclContext(LexicalDC);
172773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    LexicalDC->addDecl(D2);
172896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
17295ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
173073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Importer.Imported(D, D2);
1731e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor
173296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (D->isDefinition()) {
173373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->startDefinition();
17345250f27420386452a21692a6292c99ee7febdac4John McCall
17355250f27420386452a21692a6292c99ee7febdac4John McCall    // Add base classes.
17365250f27420386452a21692a6292c99ee7febdac4John McCall    if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
17375250f27420386452a21692a6292c99ee7febdac4John McCall      CXXRecordDecl *D1CXX = cast<CXXRecordDecl>(D);
17385250f27420386452a21692a6292c99ee7febdac4John McCall
17395250f27420386452a21692a6292c99ee7febdac4John McCall      llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
17405250f27420386452a21692a6292c99ee7febdac4John McCall      for (CXXRecordDecl::base_class_iterator
17415250f27420386452a21692a6292c99ee7febdac4John McCall                Base1 = D1CXX->bases_begin(),
17425250f27420386452a21692a6292c99ee7febdac4John McCall             FromBaseEnd = D1CXX->bases_end();
17435250f27420386452a21692a6292c99ee7febdac4John McCall           Base1 != FromBaseEnd;
17445250f27420386452a21692a6292c99ee7febdac4John McCall           ++Base1) {
17455250f27420386452a21692a6292c99ee7febdac4John McCall        QualType T = Importer.Import(Base1->getType());
17465250f27420386452a21692a6292c99ee7febdac4John McCall        if (T.isNull())
17475250f27420386452a21692a6292c99ee7febdac4John McCall          return 0;
17485250f27420386452a21692a6292c99ee7febdac4John McCall
17495250f27420386452a21692a6292c99ee7febdac4John McCall        Bases.push_back(
17505250f27420386452a21692a6292c99ee7febdac4John McCall          new (Importer.getToContext())
17515250f27420386452a21692a6292c99ee7febdac4John McCall                CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
17525250f27420386452a21692a6292c99ee7febdac4John McCall                                 Base1->isVirtual(),
17535250f27420386452a21692a6292c99ee7febdac4John McCall                                 Base1->isBaseOfClass(),
17545250f27420386452a21692a6292c99ee7febdac4John McCall                                 Base1->getAccessSpecifierAsWritten(),
17555250f27420386452a21692a6292c99ee7febdac4John McCall                                 T));
17565250f27420386452a21692a6292c99ee7febdac4John McCall      }
17575250f27420386452a21692a6292c99ee7febdac4John McCall      if (!Bases.empty())
17585250f27420386452a21692a6292c99ee7febdac4John McCall        D2CXX->setBases(Bases.data(), Bases.size());
17595250f27420386452a21692a6292c99ee7febdac4John McCall    }
17605250f27420386452a21692a6292c99ee7febdac4John McCall
1761083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    ImportDeclContext(D);
176273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->completeDefinition();
176396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
176496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
176573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return D2;
176696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
176796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
176836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorDecl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
176936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the major distinguishing characteristics of this enumerator.
177036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclContext *DC, *LexicalDC;
177136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName Name;
177236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  SourceLocation Loc;
1773ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
177436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
1775ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
1776ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
1777ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
1778ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
1779ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
178036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Determine whether there are any other declarations with the same name and
178136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // in the same context.
178236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!LexicalDC->isFunctionOrMethod()) {
178336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
178436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
178536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
178636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         Lookup.first != Lookup.second;
178736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++Lookup.first) {
178836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
178936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        continue;
179036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
179136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
179236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
179336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
179436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (!ConflictingDecls.empty()) {
179536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
179636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.data(),
179736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.size());
179836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!Name)
179936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        return 0;
180036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
180136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
180236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
180336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  Expr *Init = Importer.Import(D->getInitExpr());
180436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (D->getInitExpr() && !Init)
180536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
180636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
180736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  EnumConstantDecl *ToEnumerator
180836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
180936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                               Name.getAsIdentifierInfo(), T,
181036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                               Init, D->getInitVal());
1811325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToEnumerator->setAccess(D->getAccess());
181236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  ToEnumerator->setLexicalDeclContext(LexicalDC);
18135ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToEnumerator);
181436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  LexicalDC->addDecl(ToEnumerator);
181536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  return ToEnumerator;
181636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
181796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
1818a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
1819a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the major distinguishing characteristics of this function.
1820a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC, *LexicalDC;
1821a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclarationName Name;
1822a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc;
1823ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1824089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    return 0;
1825089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
1826a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Try to find a function in our own ("to") context with the same name, same
1827a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // type, and in the same context as the function we're importing.
1828a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  if (!LexicalDC->isFunctionOrMethod()) {
1829a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1830a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
1831a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1832a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor         Lookup.first != Lookup.second;
1833a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor         ++Lookup.first) {
1834a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1835a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        continue;
1836a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1837a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
1838a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        if (isExternalLinkage(FoundFunction->getLinkage()) &&
1839a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            isExternalLinkage(D->getLinkage())) {
1840ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          if (Importer.IsStructurallyEquivalent(D->getType(),
1841ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                FoundFunction->getType())) {
1842a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            // FIXME: Actually try to merge the body and other attributes.
18435ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor            return Importer.Imported(D, FoundFunction);
1844a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          }
1845a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1846a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // FIXME: Check for overloading more carefully, e.g., by boosting
1847a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Sema::IsOverload out to the AST library.
1848a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1849a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Function overloading is okay in C++.
1850a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          if (Importer.getToContext().getLangOptions().CPlusPlus)
1851a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            continue;
1852a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1853a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Complain about inconsistent function types.
1854a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
1855ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            << Name << D->getType() << FoundFunction->getType();
1856a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          Importer.ToDiag(FoundFunction->getLocation(),
1857a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                          diag::note_odr_value_here)
1858a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            << FoundFunction->getType();
1859a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        }
1860a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      }
1861a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1862a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
1863a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    }
1864a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1865a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    if (!ConflictingDecls.empty()) {
1866a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
1867a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                         ConflictingDecls.data(),
1868a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                         ConflictingDecls.size());
1869a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (!Name)
1870a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        return 0;
1871a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    }
1872a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
1873ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
1874ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
1875ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
1876ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
1877ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
1878a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1879a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the function parameters.
1880a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 8> Parameters;
1881a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
1882a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor       P != PEnd; ++P) {
1883a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
1884a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    if (!ToP)
1885a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      return 0;
1886a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1887a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Parameters.push_back(ToP);
1888a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
1889a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1890a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Create the imported function.
1891a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1892c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  FunctionDecl *ToFunction = 0;
1893c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
1894c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
1895c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            cast<CXXRecordDecl>(DC),
1896c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            Loc, Name, T, TInfo,
1897c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            FromConstructor->isExplicit(),
1898c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            D->isInlineSpecified(),
1899c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            D->isImplicit());
1900c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else if (isa<CXXDestructorDecl>(D)) {
1901c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
1902c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           cast<CXXRecordDecl>(DC),
1903c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           Loc, Name, T,
1904c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isInlineSpecified(),
1905c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isImplicit());
1906c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else if (CXXConversionDecl *FromConversion
1907c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           = dyn_cast<CXXConversionDecl>(D)) {
1908c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
1909c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           cast<CXXRecordDecl>(DC),
1910c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           Loc, Name, T, TInfo,
1911c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isInlineSpecified(),
1912c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           FromConversion->isExplicit());
1913c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else {
1914c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, Loc,
1915c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                      Name, T, TInfo, D->getStorageClass(),
191616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                      D->getStorageClassAsWritten(),
1917c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                      D->isInlineSpecified(),
1918c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                      D->hasWrittenPrototype());
1919c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  }
1920b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1921b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
1922b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (D->getQualifier()) {
1923b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1924b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1925b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    ToFunction->setQualifierInfo(NNS, NNSRange);
1926b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1927325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToFunction->setAccess(D->getAccess());
1928c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToFunction->setLexicalDeclContext(LexicalDC);
1929c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Importer.Imported(D, ToFunction);
1930c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  LexicalDC->addDecl(ToFunction);
1931a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1932a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Set the parameters.
1933a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
1934c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Parameters[I]->setOwningFunction(ToFunction);
1935c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToFunction->addDecl(Parameters[I]);
1936a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
1937c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToFunction->setParams(Parameters.data(), Parameters.size());
1938a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1939a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // FIXME: Other bits to merge?
1940a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1941c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToFunction;
1942a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1943a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1944c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
1945c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitFunctionDecl(D);
1946c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
1947c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
1948c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1949c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
1950c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
1951c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
1952c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
1953c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
1954c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
1955c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
1956c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
1957c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
1958c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
1959c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
196096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas GregorDecl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
196196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Import the major distinguishing characteristics of a variable.
196296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclContext *DC, *LexicalDC;
196396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName Name;
196496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  SourceLocation Loc;
1965ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1966ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
1967ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
1968ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
1969ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
1970ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
197196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
197296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
197396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
197496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  Expr *BitWidth = Importer.Import(D->getBitWidth());
197596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!BitWidth && D->getBitWidth())
197696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
197796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
197896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
197996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         Loc, Name.getAsIdentifierInfo(),
198096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         T, TInfo, BitWidth, D->isMutable());
1981325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToField->setAccess(D->getAccess());
198296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  ToField->setLexicalDeclContext(LexicalDC);
19835ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToField);
198496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  LexicalDC->addDecl(ToField);
198596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  return ToField;
198696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
198796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
19882e55e3af2f6d6c0509495357fade95105dd144cdDouglas GregorDecl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
19892e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Import the major distinguishing characteristics of an ivar.
19902e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  DeclContext *DC, *LexicalDC;
19912e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  DeclarationName Name;
19922e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  SourceLocation Loc;
19932e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
19942e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
19952e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
19962e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Determine whether we've already imported this ivar
19972e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
19982e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor       Lookup.first != Lookup.second;
19992e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor       ++Lookup.first) {
20002e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
20012e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (Importer.IsStructurallyEquivalent(D->getType(),
20022e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                            FoundIvar->getType())) {
20032e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.Imported(D, FoundIvar);
20042e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        return FoundIvar;
20052e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      }
20062e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
20072e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
20082e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << Name << D->getType() << FoundIvar->getType();
20092e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
20102e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << FoundIvar->getType();
20112e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      return 0;
20122e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    }
20132e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  }
20142e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
20152e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Import the type.
20162e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  QualType T = Importer.Import(D->getType());
20172e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (T.isNull())
20182e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
20192e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
20202e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
20212e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  Expr *BitWidth = Importer.Import(D->getBitWidth());
20222e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (!BitWidth && D->getBitWidth())
20232e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
20242e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
2025a06549226f45d5b72169a3d054415616dd1014a2Daniel Dunbar  ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2026a06549226f45d5b72169a3d054415616dd1014a2Daniel Dunbar                                              cast<ObjCContainerDecl>(DC),
20272e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              Loc, Name.getAsIdentifierInfo(),
20282e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              T, TInfo, D->getAccessControl(),
2029ac0021ba802e193e0f9f8207768c7862c7603bc0Fariborz Jahanian                                              BitWidth, D->getSynthesize());
20302e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  ToIvar->setLexicalDeclContext(LexicalDC);
20312e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  Importer.Imported(D, ToIvar);
20322e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  LexicalDC->addDecl(ToIvar);
20332e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  return ToIvar;
20342e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
20352e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor}
20362e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
2037a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2038a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the major distinguishing characteristics of a variable.
2039a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC, *LexicalDC;
2040a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclarationName Name;
2041a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc;
2042ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2043a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return 0;
2044089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2045089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Try to find a variable in our own ("to") context with the same name and
2046089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // in the same context as the variable we're importing.
20479bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (D->isFileVarDecl()) {
2048089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    VarDecl *MergeWithVar = 0;
2049089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2050089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
20519bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2052089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor         Lookup.first != Lookup.second;
2053089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor         ++Lookup.first) {
2054089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2055089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        continue;
2056089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2057089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2058089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        // We have found a variable that we may need to merge with. Check it.
2059089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        if (isExternalLinkage(FoundVar->getLinkage()) &&
2060089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            isExternalLinkage(D->getLinkage())) {
2061ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          if (Importer.IsStructurallyEquivalent(D->getType(),
2062ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                FoundVar->getType())) {
2063089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            MergeWithVar = FoundVar;
2064089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            break;
2065089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          }
2066089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2067d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          const ArrayType *FoundArray
2068d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            = Importer.getToContext().getAsArrayType(FoundVar->getType());
2069d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          const ArrayType *TArray
2070ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            = Importer.getToContext().getAsArrayType(D->getType());
2071d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          if (FoundArray && TArray) {
2072d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            if (isa<IncompleteArrayType>(FoundArray) &&
2073d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor                isa<ConstantArrayType>(TArray)) {
2074ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              // Import the type.
2075ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              QualType T = Importer.Import(D->getType());
2076ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              if (T.isNull())
2077ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                return 0;
2078ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2079d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              FoundVar->setType(T);
2080d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              MergeWithVar = FoundVar;
2081d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              break;
2082d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            } else if (isa<IncompleteArrayType>(TArray) &&
2083d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor                       isa<ConstantArrayType>(FoundArray)) {
2084d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              MergeWithVar = FoundVar;
2085d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              break;
20860f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor            }
20870f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor          }
20880f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor
2089089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
2090ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            << Name << D->getType() << FoundVar->getType();
2091089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2092089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            << FoundVar->getType();
2093089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        }
2094089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      }
2095089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2096089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
2097089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2098089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2099089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    if (MergeWithVar) {
2100089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      // An equivalent variable with external linkage has been found. Link
2101089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      // the two declarations, then merge them.
21025ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor      Importer.Imported(D, MergeWithVar);
2103089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2104089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (VarDecl *DDef = D->getDefinition()) {
2105089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2106089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(ExistingDef->getLocation(),
2107089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                          diag::err_odr_variable_multiple_def)
2108089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            << Name;
2109089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2110089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        } else {
2111089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Expr *Init = Importer.Import(DDef->getInit());
2112838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor          MergeWithVar->setInit(Init);
2113089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        }
2114089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      }
2115089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2116089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      return MergeWithVar;
2117089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2118089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2119089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    if (!ConflictingDecls.empty()) {
2120089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
2121089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                         ConflictingDecls.data(),
2122089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                         ConflictingDecls.size());
2123089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (!Name)
2124089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        return 0;
2125089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2126089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  }
212782fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2128ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
2129ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2130ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2131ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2132ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2133089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Create the imported variable.
213482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2135089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2136089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                   Name.getAsIdentifierInfo(), T, TInfo,
213716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClass(),
213816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten());
2139b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
2140b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (D->getQualifier()) {
2141b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2142b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2143b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    ToVar->setQualifierInfo(NNS, NNSRange);
2144b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2145325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToVar->setAccess(D->getAccess());
21469bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ToVar->setLexicalDeclContext(LexicalDC);
21475ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToVar);
21489bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  LexicalDC->addDecl(ToVar);
21499bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
2150089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Merge the initializer.
2151089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // FIXME: Can we really import any initializer? Alternatively, we could force
2152089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // ourselves to import every declaration of a variable and then only use
2153089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // getInit() here.
2154838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
2155089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2156089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // FIXME: Other bits to merge?
2157089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2158089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  return ToVar;
2159089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
2160089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
21612cd00932b9b5403047139ce8cfaa3ae47966f894Douglas GregorDecl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
21622cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Parameters are created in the translation unit's context, then moved
21632cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // into the function declaration's context afterward.
21642cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
21652cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
21662cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the name of this declaration.
21672cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
21682cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  if (D->getDeclName() && !Name)
21692cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    return 0;
21702cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
21712cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the location of this declaration.
21722cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
21732cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
21742cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the parameter's type.
21752cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  QualType T = Importer.Import(D->getType());
21762cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  if (T.isNull())
21772cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    return 0;
21782cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
21792cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Create the imported parameter.
21802cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  ImplicitParamDecl *ToParm
21812cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    = ImplicitParamDecl::Create(Importer.getToContext(), DC,
21822cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor                                Loc, Name.getAsIdentifierInfo(),
21832cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor                                T);
21842cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  return Importer.Imported(D, ToParm);
21852cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor}
21862cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
2187a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2188a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Parameters are created in the translation unit's context, then moved
2189a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // into the function declaration's context afterward.
2190a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2191a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
219282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // Import the name of this declaration.
219382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
219482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (D->getDeclName() && !Name)
219582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
219682fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2197a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the location of this declaration.
2198a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
2199a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2200a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the parameter's type.
2201a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  QualType T = Importer.Import(D->getType());
220282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (T.isNull())
220382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
220482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2205a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Create the imported parameter.
2206a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2207a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2208a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            Loc, Name.getAsIdentifierInfo(),
2209a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            T, TInfo, D->getStorageClass(),
221016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                             D->getStorageClassAsWritten(),
2211a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            /*FIXME: Default argument*/ 0);
2212bf73b352acb7a2d041ce8b50171dd7f8e2b2c1bbJohn McCall  ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
22135ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  return Importer.Imported(D, ToParm);
2214a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
221582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2216c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas GregorDecl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2217c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the major distinguishing characteristics of a method.
2218c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  DeclContext *DC, *LexicalDC;
2219c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  DeclarationName Name;
2220c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  SourceLocation Loc;
2221c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2222c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return 0;
2223c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2224c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2225c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       Lookup.first != Lookup.second;
2226c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       ++Lookup.first) {
2227c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2228c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2229c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        continue;
2230c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2231c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check return types.
2232c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2233c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                             FoundMethod->getResultType())) {
2234c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2235c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name
2236c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->getResultType() << FoundMethod->getResultType();
2237c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2238c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2239c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2240c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2241c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2242c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2243c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check the number of parameters.
2244c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (D->param_size() != FoundMethod->param_size()) {
2245c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2246c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name
2247c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->param_size() << FoundMethod->param_size();
2248c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2249c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2250c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2251c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2252c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2253c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2254c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check parameter types.
2255c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2256c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor             PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2257c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor           P != PEnd; ++P, ++FoundP) {
2258c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2259c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                               (*FoundP)->getType())) {
2260c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          Importer.FromDiag((*P)->getLocation(),
2261c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                            diag::err_odr_objc_method_param_type_inconsistent)
2262c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << D->isInstanceMethod() << Name
2263c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << (*P)->getType() << (*FoundP)->getType();
2264c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2265c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << (*FoundP)->getType();
2266c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          return 0;
2267c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        }
2268c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2269c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2270c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check variadic/non-variadic.
2271c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check the number of parameters.
2272c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (D->isVariadic() != FoundMethod->isVariadic()) {
2273c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2274c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2275c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2276c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2277c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2278c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2279c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2280c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2281c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // FIXME: Any other bits we need to merge?
2282c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      return Importer.Imported(D, FoundMethod);
2283c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    }
2284c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2285c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2286c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the result type.
2287c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  QualType ResultTy = Importer.Import(D->getResultType());
2288c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (ResultTy.isNull())
2289c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return 0;
2290c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
22914bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor  TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
22924bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor
2293c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ObjCMethodDecl *ToMethod
2294c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    = ObjCMethodDecl::Create(Importer.getToContext(),
2295c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Loc,
2296c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Importer.Import(D->getLocEnd()),
2297c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Name.getObjCSelector(),
22984bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor                             ResultTy, ResultTInfo, DC,
2299c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isInstanceMethod(),
2300c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isVariadic(),
2301c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isSynthesized(),
23023fe104154dd2e8ffb351142d74f308938b5c99bfFariborz Jahanian                             D->isDefined(),
2303c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->getImplementationControl());
2304c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2305c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // FIXME: When we decide to merge method definitions, we'll need to
2306c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // deal with implicit parameters.
2307c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2308c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the parameters
2309c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2310c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2311c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                   FromPEnd = D->param_end();
2312c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       FromP != FromPEnd;
2313c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       ++FromP) {
2314c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2315c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    if (!ToP)
2316c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      return 0;
2317c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2318c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToParams.push_back(ToP);
2319c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2320c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2321c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Set the parameters.
2322c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2323c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToParams[I]->setOwningFunction(ToMethod);
2324c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToMethod->addDecl(ToParams[I]);
2325c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2326c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToMethod->setMethodParams(Importer.getToContext(),
23274ecb25fa94897b2c03510292acace710e5262ba5Fariborz Jahanian                            ToParams.data(), ToParams.size(),
23284ecb25fa94897b2c03510292acace710e5262ba5Fariborz Jahanian                            ToParams.size());
2329c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2330c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToMethod->setLexicalDeclContext(LexicalDC);
2331c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Importer.Imported(D, ToMethod);
2332c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  LexicalDC->addDecl(ToMethod);
2333c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToMethod;
2334c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor}
2335c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2336b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas GregorDecl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2337b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import the major distinguishing characteristics of a category.
2338b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  DeclContext *DC, *LexicalDC;
2339b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  DeclarationName Name;
2340b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  SourceLocation Loc;
2341b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2342b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    return 0;
2343b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2344b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCInterfaceDecl *ToInterface
2345b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2346b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (!ToInterface)
2347b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    return 0;
2348b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2349b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Determine if we've already encountered this category.
2350b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCCategoryDecl *MergeWithCategory
2351b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2352b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCCategoryDecl *ToCategory = MergeWithCategory;
2353b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (!ToCategory) {
2354b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2355b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Importer.Import(D->getAtLoc()),
2356b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Loc,
2357b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                       Importer.Import(D->getCategoryNameLoc()),
2358b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Name.getAsIdentifierInfo());
2359b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setLexicalDeclContext(LexicalDC);
2360b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    LexicalDC->addDecl(ToCategory);
2361b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Imported(D, ToCategory);
2362b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2363b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // Link this category into its class's category list.
2364b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setClassInterface(ToInterface);
2365b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->insertNextClassCategory();
2366b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2367b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // Import protocols
2368b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2369b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2370b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2371b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      = D->protocol_loc_begin();
2372b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2373b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          FromProtoEnd = D->protocol_end();
2374b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor         FromProto != FromProtoEnd;
2375b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor         ++FromProto, ++FromProtoLoc) {
2376b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      ObjCProtocolDecl *ToProto
2377b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2378b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      if (!ToProto)
2379b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor        return 0;
2380b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      Protocols.push_back(ToProto);
2381b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2382b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    }
2383b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2384b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
2385b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2386b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                ProtocolLocs.data(), Importer.getToContext());
2387b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2388b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  } else {
2389b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Imported(D, ToCategory);
2390b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  }
2391b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2392b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import all of the members of this category.
2393083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
2394b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2395b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // If we have an implementation, import it as well.
2396b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (D->getImplementation()) {
2397b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ObjCCategoryImplDecl *Impl
2398b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation()));
2399b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    if (!Impl)
2400b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      return 0;
2401b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2402b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setImplementation(Impl);
2403b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  }
2404b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2405b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  return ToCategory;
2406b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor}
2407b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
24082e2a400383c7a64a927f61eeed596b08928e1d4bDouglas GregorDecl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
2409b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import the major distinguishing characteristics of a protocol.
24102e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  DeclContext *DC, *LexicalDC;
24112e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  DeclarationName Name;
24122e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  SourceLocation Loc;
24132e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
24142e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    return 0;
24152e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
24162e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  ObjCProtocolDecl *MergeWithProtocol = 0;
24172e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
24182e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       Lookup.first != Lookup.second;
24192e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++Lookup.first) {
24202e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
24212e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      continue;
24222e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
24232e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
24242e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      break;
24252e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  }
24262e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
24272e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  ObjCProtocolDecl *ToProto = MergeWithProtocol;
24282e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  if (!ToProto || ToProto->isForwardDecl()) {
24292e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if (!ToProto) {
24302e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
24312e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                                         Name.getAsIdentifierInfo());
24322e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto->setForwardDecl(D->isForwardDecl());
24332e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto->setLexicalDeclContext(LexicalDC);
24342e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      LexicalDC->addDecl(ToProto);
24352e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    }
24362e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Imported(D, ToProto);
24372e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
24382e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    // Import protocols
24392e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
24402e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
24412e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    ObjCProtocolDecl::protocol_loc_iterator
24422e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      FromProtoLoc = D->protocol_loc_begin();
24432e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
24442e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                                          FromProtoEnd = D->protocol_end();
24452e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       FromProto != FromProtoEnd;
24462e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++FromProto, ++FromProtoLoc) {
24472e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ObjCProtocolDecl *ToProto
24482e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
24492e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      if (!ToProto)
24502e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor        return 0;
24512e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      Protocols.push_back(ToProto);
24522e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
24532e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    }
24542e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
24552e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
24562e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    ToProto->setProtocolList(Protocols.data(), Protocols.size(),
24572e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                             ProtocolLocs.data(), Importer.getToContext());
24582e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  } else {
24592e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Imported(D, ToProto);
24602e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  }
24612e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
2462b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import all of the members of this protocol.
2463083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
24642e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
24652e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  return ToProto;
24662e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor}
24672e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
2468a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas GregorDecl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2469a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // Import the major distinguishing characteristics of an @interface.
2470a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  DeclContext *DC, *LexicalDC;
2471a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  DeclarationName Name;
2472a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  SourceLocation Loc;
2473a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2474a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    return 0;
2475a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2476a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  ObjCInterfaceDecl *MergeWithIface = 0;
2477a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2478a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       Lookup.first != Lookup.second;
2479a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++Lookup.first) {
2480a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2481a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      continue;
2482a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2483a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2484a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      break;
2485a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
2486a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2487a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  ObjCInterfaceDecl *ToIface = MergeWithIface;
2488a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (!ToIface || ToIface->isForwardDecl()) {
2489a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!ToIface) {
2490a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2491a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          DC, Loc,
2492a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          Name.getAsIdentifierInfo(),
2493a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          Importer.Import(D->getClassLoc()),
2494a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          D->isForwardDecl(),
2495a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          D->isImplicitInterfaceDecl());
24962e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToIface->setForwardDecl(D->isForwardDecl());
2497a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setLexicalDeclContext(LexicalDC);
2498a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      LexicalDC->addDecl(ToIface);
2499a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
2500a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Imported(D, ToIface);
2501a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2502a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (D->getSuperClass()) {
2503a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ObjCInterfaceDecl *Super
2504a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2505a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      if (!Super)
2506a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        return 0;
2507a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2508a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setSuperClass(Super);
2509a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2510a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
2511a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2512a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // Import protocols
2513a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2514a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2515a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ObjCInterfaceDecl::protocol_loc_iterator
2516a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      FromProtoLoc = D->protocol_loc_begin();
2517a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2518a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                           FromProtoEnd = D->protocol_end();
2519a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       FromProto != FromProtoEnd;
2520a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++FromProto, ++FromProtoLoc) {
2521a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ObjCProtocolDecl *ToProto
2522a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2523a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      if (!ToProto)
2524a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        return 0;
2525a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      Protocols.push_back(ToProto);
2526a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2527a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
2528a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2529a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
2530a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2531a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                             ProtocolLocs.data(), Importer.getToContext());
2532a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2533a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // Import @end range
2534a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2535a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  } else {
2536a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Imported(D, ToIface);
25372e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
25382e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    // Check for consistency of superclasses.
25392e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    DeclarationName FromSuperName, ToSuperName;
25402e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (D->getSuperClass())
25412e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
25422e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (ToIface->getSuperClass())
25432e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      ToSuperName = ToIface->getSuperClass()->getDeclName();
25442e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (FromSuperName != ToSuperName) {
25452e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(ToIface->getLocation(),
25462e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                      diag::err_odr_objc_superclass_inconsistent)
25472e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << ToIface->getDeclName();
25482e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (ToIface->getSuperClass())
25492e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.ToDiag(ToIface->getSuperClassLoc(),
25502e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                        diag::note_odr_objc_superclass)
25512e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor          << ToIface->getSuperClass()->getDeclName();
25522e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      else
25532e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.ToDiag(ToIface->getLocation(),
25542e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                        diag::note_odr_objc_missing_superclass);
25552e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (D->getSuperClass())
25562e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.FromDiag(D->getSuperClassLoc(),
25572e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                          diag::note_odr_objc_superclass)
25582e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor          << D->getSuperClass()->getDeclName();
25592e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      else
25602e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.FromDiag(D->getLocation(),
25612e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                          diag::note_odr_objc_missing_superclass);
25622e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      return 0;
25632e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    }
2564a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
2565a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2566b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import categories. When the categories themselves are imported, they'll
2567b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // hook themselves into this interface.
2568b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
2569b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor       FromCat = FromCat->getNextClassCategory())
2570b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Import(FromCat);
2571b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2572a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // Import all of the members of this class.
2573083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
2574a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2575a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // If we have an @implementation, import it as well.
2576a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (D->getImplementation()) {
2577a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ObjCImplementationDecl *Impl
2578a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      = cast<ObjCImplementationDecl>(Importer.Import(D->getImplementation()));
2579a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!Impl)
2580a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      return 0;
2581a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2582a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setImplementation(Impl);
2583a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
2584a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
25852e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  return ToIface;
2586a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor}
2587a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2588e3261624c1870e52d7efc2ac83e647713361ac6cDouglas GregorDecl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
2589e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Import the major distinguishing characteristics of an @property.
2590e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  DeclContext *DC, *LexicalDC;
2591e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  DeclarationName Name;
2592e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  SourceLocation Loc;
2593e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2594e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    return 0;
2595e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2596e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Check whether we have already imported this property.
2597e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2598e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       Lookup.first != Lookup.second;
2599e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       ++Lookup.first) {
2600e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    if (ObjCPropertyDecl *FoundProp
2601e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                                = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
2602e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // Check property types.
2603e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      if (!Importer.IsStructurallyEquivalent(D->getType(),
2604e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                                             FoundProp->getType())) {
2605e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
2606e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor          << Name << D->getType() << FoundProp->getType();
2607e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
2608e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor          << FoundProp->getType();
2609e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        return 0;
2610e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      }
2611e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2612e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // FIXME: Check property attributes, getters, setters, etc.?
2613e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2614e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // Consider these properties to be equivalent.
2615e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      Importer.Imported(D, FoundProp);
2616e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      return FoundProp;
2617e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    }
2618e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  }
2619e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2620e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Import the type.
262183a230c83a54190366138c1a4f4310ef838b88fcJohn McCall  TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
262283a230c83a54190366138c1a4f4310ef838b88fcJohn McCall  if (!T)
2623e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    return 0;
2624e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2625e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Create the new property.
2626e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ObjCPropertyDecl *ToProperty
2627e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
2628e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               Name.getAsIdentifierInfo(),
2629e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               Importer.Import(D->getAtLoc()),
2630e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               T,
2631e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               D->getPropertyImplementation());
2632e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  Importer.Imported(D, ToProperty);
2633e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setLexicalDeclContext(LexicalDC);
2634e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  LexicalDC->addDecl(ToProperty);
2635e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2636e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setPropertyAttributes(D->getPropertyAttributes());
263780aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian  ToProperty->setPropertyAttributesAsWritten(
263880aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian                                      D->getPropertyAttributesAsWritten());
2639e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setGetterName(Importer.Import(D->getGetterName()));
2640e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setSetterName(Importer.Import(D->getSetterName()));
2641e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setGetterMethodDecl(
2642e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
2643e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setSetterMethodDecl(
2644e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
2645e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setPropertyIvarDecl(
2646e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
2647e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  return ToProperty;
2648e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor}
2649e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
26502b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas GregorDecl *
26512b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas GregorASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
26522b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  // Import the context of this declaration.
26532b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
26542b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  if (!DC)
26552b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    return 0;
26562b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
26572b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  DeclContext *LexicalDC = DC;
26582b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
26592b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
26602b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    if (!LexicalDC)
26612b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      return 0;
26622b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  }
26632b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
26642b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  // Import the location of this declaration.
26652b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
26662b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
26672b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
26682b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  llvm::SmallVector<SourceLocation, 4> Locations;
26692b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
26702b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    = D->protocol_loc_begin();
26712b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  for (ObjCForwardProtocolDecl::protocol_iterator FromProto
26722b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor         = D->protocol_begin(), FromProtoEnd = D->protocol_end();
26732b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor       FromProto != FromProtoEnd;
26742b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor       ++FromProto, ++FromProtoLoc) {
26752b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    ObjCProtocolDecl *ToProto
26762b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
26772b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    if (!ToProto)
26782b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      continue;
26792b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
26802b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Protocols.push_back(ToProto);
26812b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Locations.push_back(Importer.Import(*FromProtoLoc));
26822b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  }
26832b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
26842b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ObjCForwardProtocolDecl *ToForward
26852b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
26862b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor                                      Protocols.data(), Protocols.size(),
26872b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor                                      Locations.data());
26882b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ToForward->setLexicalDeclContext(LexicalDC);
26892b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  LexicalDC->addDecl(ToForward);
26902b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  Importer.Imported(D, ToForward);
26912b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  return ToForward;
26922b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor}
26932b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
2694a2bc15b7463a9f85a5bff1531d833c278426a733Douglas GregorDecl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
2695a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  // Import the context of this declaration.
2696a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2697a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  if (!DC)
2698a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    return 0;
2699a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2700a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  DeclContext *LexicalDC = DC;
2701a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
2702a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2703a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    if (!LexicalDC)
2704a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      return 0;
2705a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  }
2706a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2707a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  // Import the location of this declaration.
2708a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
2709a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2710a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
2711a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  llvm::SmallVector<SourceLocation, 4> Locations;
2712a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
2713a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor       From != FromEnd; ++From) {
2714a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    ObjCInterfaceDecl *ToIface
2715a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
2716a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    if (!ToIface)
2717a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      continue;
2718a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2719a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Interfaces.push_back(ToIface);
2720a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Locations.push_back(Importer.Import(From->getLocation()));
2721a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  }
2722a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2723a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
2724a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Loc,
2725a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Interfaces.data(),
2726a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Locations.data(),
2727a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Interfaces.size());
2728a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  ToClass->setLexicalDeclContext(LexicalDC);
2729a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  LexicalDC->addDecl(ToClass);
2730a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  Importer.Imported(D, ToClass);
2731a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  return ToClass;
2732a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor}
2733a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
27344800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
27354800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor// Import Statements
27364800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
27374800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
27384800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorStmt *ASTNodeImporter::VisitStmt(Stmt *S) {
27394800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
27404800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    << S->getStmtClassName();
27414800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return 0;
27424800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
27434800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
27444800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
27454800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor// Import Expressions
27464800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
27474800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorExpr *ASTNodeImporter::VisitExpr(Expr *E) {
27484800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
27494800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    << E->getStmtClassName();
27504800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return 0;
27514800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
27524800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
2753440806306674e23ad74726208cbdc6f37849dd9dDouglas GregorExpr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
2754440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  NestedNameSpecifier *Qualifier = 0;
2755440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (E->getQualifier()) {
2756440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    Qualifier = Importer.Import(E->getQualifier());
2757440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    if (!E->getQualifier())
2758440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor      return 0;
2759440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  }
2760440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
2761440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
2762440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (!ToD)
2763440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    return 0;
2764440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
2765440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  QualType T = Importer.Import(E->getType());
2766440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (T.isNull())
2767440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    return 0;
2768440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
2769440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
2770440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             Importer.Import(E->getQualifierRange()),
2771440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             ToD,
2772440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             Importer.Import(E->getLocation()),
2773440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             T,
2774440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             /*FIXME:TemplateArgs=*/0);
2775440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor}
2776440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
27774800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorExpr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
27784800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  QualType T = Importer.Import(E->getType());
27794800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (T.isNull())
27804800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return 0;
27814800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
27824800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return new (Importer.getToContext())
27834800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    IntegerLiteral(E->getValue(), T, Importer.Import(E->getLocation()));
27844800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
27854800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
2786b2e400aae8c62c4e1616016f40618baace0da065Douglas GregorExpr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
2787b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  QualType T = Importer.Import(E->getType());
2788b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  if (T.isNull())
2789b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor    return 0;
2790b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor
2791b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
2792b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor                                                        E->isWide(), T,
2793b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor                                          Importer.Import(E->getLocation()));
2794b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor}
2795b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor
2796f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
2797f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
2798f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!SubExpr)
2799f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2800f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2801f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext())
2802f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                  ParenExpr(Importer.Import(E->getLParen()),
2803f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                            Importer.Import(E->getRParen()),
2804f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                            SubExpr);
2805f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
2806f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2807f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
2808f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
2809f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
2810f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2811f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2812f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
2813f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!SubExpr)
2814f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2815f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2816f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
2817f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                                     T,
2818f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                         Importer.Import(E->getOperatorLoc()));
2819f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
2820f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2821bd249a542878a626192746c1e0c0b21f164e6df7Douglas GregorExpr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
2822bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  QualType ResultType = Importer.Import(E->getType());
2823bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
2824bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  if (E->isArgumentType()) {
2825bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
2826bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    if (!TInfo)
2827bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor      return 0;
2828bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
2829bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
2830bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                                           TInfo, ResultType,
2831bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                           Importer.Import(E->getOperatorLoc()),
2832bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                           Importer.Import(E->getRParenLoc()));
2833bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  }
2834bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
2835bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  Expr *SubExpr = Importer.Import(E->getArgumentExpr());
2836bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  if (!SubExpr)
2837bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    return 0;
2838bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
2839bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
2840bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                                         SubExpr, ResultType,
2841bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                          Importer.Import(E->getOperatorLoc()),
2842bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                          Importer.Import(E->getRParenLoc()));
2843bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor}
2844bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
2845f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
2846f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
2847f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
2848f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2849f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2850f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *LHS = Importer.Import(E->getLHS());
2851f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!LHS)
2852f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2853f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2854f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *RHS = Importer.Import(E->getRHS());
2855f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!RHS)
2856f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2857f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2858f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
2859f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                                      T,
2860f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                          Importer.Import(E->getOperatorLoc()));
2861f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
2862f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2863f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
2864f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
2865f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
2866f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2867f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2868f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType CompLHSType = Importer.Import(E->getComputationLHSType());
2869f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (CompLHSType.isNull())
2870f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2871f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2872f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType CompResultType = Importer.Import(E->getComputationResultType());
2873f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (CompResultType.isNull())
2874f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2875f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2876f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *LHS = Importer.Import(E->getLHS());
2877f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!LHS)
2878f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2879f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2880f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *RHS = Importer.Import(E->getRHS());
2881f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!RHS)
2882f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2883f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2884f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext())
2885f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                        CompoundAssignOperator(LHS, RHS, E->getOpcode(),
2886f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                               T, CompLHSType, CompResultType,
2887f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                          Importer.Import(E->getOperatorLoc()));
2888f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
2889f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
289036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorExpr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
289136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  QualType T = Importer.Import(E->getType());
289236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (T.isNull())
289336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
289436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
289536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
289636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!SubExpr)
289736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
289836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
2899f1b48b7014992155286d58bb1676f9f51031d18bAnders Carlsson  // FIXME: Initialize the base path.
290041b2dcd465f1e438502c420effc9d0c747f9db8fAnders Carlsson  assert(E->getBasePath().empty() && "FIXME: Must copy base path!");
2901f1b48b7014992155286d58bb1676f9f51031d18bAnders Carlsson  CXXBaseSpecifierArray BasePath;
290236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  return new (Importer.getToContext()) ImplicitCastExpr(T, E->getCastKind(),
2903f1b48b7014992155286d58bb1676f9f51031d18bAnders Carlsson                                                        SubExpr, BasePath,
2904906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl                                                        E->getCategory());
290536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
290636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
2907008847a70ab122a99911149199855060fb3753b4Douglas GregorExpr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
2908008847a70ab122a99911149199855060fb3753b4Douglas Gregor  QualType T = Importer.Import(E->getType());
2909008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (T.isNull())
2910008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
2911008847a70ab122a99911149199855060fb3753b4Douglas Gregor
2912008847a70ab122a99911149199855060fb3753b4Douglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
2913008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (!SubExpr)
2914008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
2915008847a70ab122a99911149199855060fb3753b4Douglas Gregor
2916008847a70ab122a99911149199855060fb3753b4Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
2917008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (!TInfo && E->getTypeInfoAsWritten())
2918008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
2919008847a70ab122a99911149199855060fb3753b4Douglas Gregor
292041b2dcd465f1e438502c420effc9d0c747f9db8fAnders Carlsson  // FIXME: Initialize the base path.
292141b2dcd465f1e438502c420effc9d0c747f9db8fAnders Carlsson  assert(E->getBasePath().empty() && "FIXME: Must copy base path!");
292241b2dcd465f1e438502c420effc9d0c747f9db8fAnders Carlsson  CXXBaseSpecifierArray BasePath;
2923008847a70ab122a99911149199855060fb3753b4Douglas Gregor  return new (Importer.getToContext()) CStyleCastExpr(T, E->getCastKind(),
292441b2dcd465f1e438502c420effc9d0c747f9db8fAnders Carlsson                                                      SubExpr, BasePath, TInfo,
2925008847a70ab122a99911149199855060fb3753b4Douglas Gregor                                            Importer.Import(E->getLParenLoc()),
2926008847a70ab122a99911149199855060fb3753b4Douglas Gregor                                            Importer.Import(E->getRParenLoc()));
2927008847a70ab122a99911149199855060fb3753b4Douglas Gregor}
2928008847a70ab122a99911149199855060fb3753b4Douglas Gregor
29294800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorASTImporter::ASTImporter(Diagnostic &Diags,
29304800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                         ASTContext &ToContext, FileManager &ToFileManager,
29314800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                         ASTContext &FromContext, FileManager &FromFileManager)
29321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  : ToContext(ToContext), FromContext(FromContext),
2933885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToFileManager(ToFileManager), FromFileManager(FromFileManager),
29344800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Diags(Diags) {
29359bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ImportedDecls[FromContext.getTranslationUnitDecl()]
29369bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    = ToContext.getTranslationUnitDecl();
29379bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
29389bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
29399bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorASTImporter::~ASTImporter() { }
29401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
29411b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTImporter::Import(QualType FromT) {
29421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (FromT.isNull())
29431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
29441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
2945169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Check whether we've already imported this type.
2946169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  llvm::DenseMap<Type *, Type *>::iterator Pos
2947169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor    = ImportedTypes.find(FromT.getTypePtr());
2948169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  if (Pos != ImportedTypes.end())
2949169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor    return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
29501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
2951169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Import the type
29521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  ASTNodeImporter Importer(*this);
29531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToT = Importer.Visit(FromT.getTypePtr());
29541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToT.isNull())
29551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToT;
29561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
2957169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Record the imported type.
2958169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
2959169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor
29601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
29611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
29621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
29639bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorTypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
296482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (!FromTSI)
296582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return FromTSI;
296682fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
296782fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // FIXME: For now we just create a "trivial" type source info based
296882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // on the type and a seingle location. Implement a real version of
296982fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // this.
297082fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  QualType T = Import(FromTSI->getType());
297182fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (T.isNull())
297282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
297382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
297482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  return ToContext.getTrivialTypeSourceInfo(T,
2975bd054dba8a3023821f2a0951b0fae05e3522a7c9Abramo Bagnara                        FromTSI->getTypeLoc().getSourceRange().getBegin());
29769bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
29779bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
29789bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorDecl *ASTImporter::Import(Decl *FromD) {
29799bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromD)
29809bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
29819bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
29829bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Check whether we've already imported this declaration.
29839bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
29849bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (Pos != ImportedDecls.end())
29859bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return Pos->second;
29869bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
29879bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Import the type
29889bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ASTNodeImporter Importer(*this);
29899bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  Decl *ToD = Importer.Visit(FromD);
29909bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!ToD)
29919bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
29929bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
29939bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Record the imported declaration.
29949bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ImportedDecls[FromD] = ToD;
2995ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2996ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
2997ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // Keep track of anonymous tags that have an associated typedef.
2998ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (FromTag->getTypedefForAnonDecl())
2999ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      AnonTagsWithPendingTypedefs.push_back(FromTag);
3000ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3001ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // When we've finished transforming a typedef, see whether it was the
3002ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // typedef for an anonymous tag.
3003ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    for (llvm::SmallVector<TagDecl *, 4>::iterator
3004ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor               FromTag = AnonTagsWithPendingTypedefs.begin(),
3005ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            FromTagEnd = AnonTagsWithPendingTypedefs.end();
3006ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor         FromTag != FromTagEnd; ++FromTag) {
3007ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3008ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3009ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          // We found the typedef for an anonymous tag; link them.
3010ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3011ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          AnonTagsWithPendingTypedefs.erase(FromTag);
3012ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          break;
3013ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        }
3014ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      }
3015ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    }
3016ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  }
3017ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
30189bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return ToD;
30199bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30209bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30219bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorDeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
30229bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromDC)
30239bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return FromDC;
30249bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30259bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
30269bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30279bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30289bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorExpr *ASTImporter::Import(Expr *FromE) {
30299bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromE)
30309bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
30319bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30329bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
30339bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30349bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30359bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorStmt *ASTImporter::Import(Stmt *FromS) {
30369bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromS)
30379bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
30389bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30394800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Check whether we've already imported this declaration.
30404800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
30414800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (Pos != ImportedStmts.end())
30424800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return Pos->second;
30434800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
30444800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Import the type
30454800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  ASTNodeImporter Importer(*this);
30464800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Stmt *ToS = Importer.Visit(FromS);
30474800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (!ToS)
30484800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return 0;
30494800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
30504800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Record the imported declaration.
30514800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  ImportedStmts[FromS] = ToS;
30524800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return ToS;
30539bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30549bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30559bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorNestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
30569bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromNNS)
30579bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
30589bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30599bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // FIXME: Implement!
30609bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return 0;
30619bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30629bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30639bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorSourceLocation ASTImporter::Import(SourceLocation FromLoc) {
30649bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (FromLoc.isInvalid())
30659bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return SourceLocation();
30669bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
3067885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &FromSM = FromContext.getSourceManager();
3068885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3069885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // For now, map everything down to its spelling location, so that we
3070885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // don't have to import macro instantiations.
3071885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // FIXME: Import macro instantiations!
3072885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  FromLoc = FromSM.getSpellingLoc(FromLoc);
3073885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
3074885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &ToSM = ToContext.getSourceManager();
3075885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  return ToSM.getLocForStartOfFile(Import(Decomposed.first))
3076885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor             .getFileLocWithOffset(Decomposed.second);
30779bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30789bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30799bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorSourceRange ASTImporter::Import(SourceRange FromRange) {
30809bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
30819bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30829bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
3083885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas GregorFileID ASTImporter::Import(FileID FromID) {
3084885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  llvm::DenseMap<unsigned, FileID>::iterator Pos
3085885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    = ImportedFileIDs.find(FromID.getHashValue());
3086885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  if (Pos != ImportedFileIDs.end())
3087885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    return Pos->second;
3088885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3089885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &FromSM = FromContext.getSourceManager();
3090885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &ToSM = ToContext.getSourceManager();
3091885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
3092885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
3093885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3094885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // Include location of this file.
3095885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
3096885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3097885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // Map the FileID for to the "to" source manager.
3098885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  FileID ToID;
3099885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
3100885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  if (Cache->Entry) {
3101885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We probably want to use getVirtualFile(), so we don't hit the
3102885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // disk again
3103885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
3104885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // than mmap the files several times.
3105885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
3106885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToID = ToSM.createFileID(Entry, ToIncludeLoc,
3107885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                             FromSLoc.getFile().getFileCharacteristic());
3108885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  } else {
3109885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We want to re-use the existing MemoryBuffer!
3110e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(getDiags(), FromSM);
3111885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    llvm::MemoryBuffer *ToBuf
3112a0a270c0f1c0a4e3482438bdc5f4a7bd3d25f0a6Chris Lattner      = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
3113885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                                             FromBuf->getBufferIdentifier());
3114885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToID = ToSM.createFileIDForMemBuffer(ToBuf);
3115885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  }
3116885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3117885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3118885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  ImportedFileIDs[FromID.getHashValue()] = ToID;
3119885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  return ToID;
3120885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor}
3121885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
31221b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorDeclarationName ASTImporter::Import(DeclarationName FromName) {
31231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!FromName)
31241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return DeclarationName();
31251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  switch (FromName.getNameKind()) {
31271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::Identifier:
31281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Import(FromName.getAsIdentifierInfo());
31291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCZeroArgSelector:
31311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCOneArgSelector:
31321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCMultiArgSelector:
31331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Import(FromName.getObjCSelector());
31341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXConstructorName: {
31361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
31371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
31381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
31391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXConstructorName(
31411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
31421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
31431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXDestructorName: {
31451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
31461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
31471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
31481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXDestructorName(
31501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
31511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
31521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXConversionFunctionName: {
31541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
31551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
31561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
31571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXConversionFunctionName(
31591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
31601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
31611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXOperatorName:
31631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXOperatorName(
31641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                          FromName.getCXXOverloadedOperator());
31651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXLiteralOperatorName:
31671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXLiteralOperatorName(
31681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                   Import(FromName.getCXXLiteralIdentifier()));
31691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXUsingDirective:
31711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: STATICS!
31721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return DeclarationName::getUsingDirectiveName();
31731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
31741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Silence bogus GCC warning
31761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return DeclarationName();
31771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
31781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31791b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorIdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) {
31801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!FromId)
31811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return 0;
31821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return &ToContext.Idents.get(FromId->getName());
31841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
3185089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
3186c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas GregorSelector ASTImporter::Import(Selector FromSel) {
3187c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (FromSel.isNull())
3188c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return Selector();
3189c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
3190c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  llvm::SmallVector<IdentifierInfo *, 4> Idents;
3191c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
3192c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
3193c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
3194c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
3195c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor}
3196c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
3197089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
3198089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                DeclContext *DC,
3199089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                unsigned IDNS,
3200089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                NamedDecl **Decls,
3201089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                unsigned NumDecls) {
3202089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  return Name;
3203089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
3204089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
3205089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
32064800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return Diags.Report(FullSourceLoc(Loc, ToContext.getSourceManager()),
32074800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                      DiagID);
3208089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
3209089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
3210089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
32114800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return Diags.Report(FullSourceLoc(Loc, FromContext.getSourceManager()),
32124800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                      DiagID);
3213089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
32145ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
32155ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas GregorDecl *ASTImporter::Imported(Decl *From, Decl *To) {
32165ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  ImportedDecls[From] = To;
32175ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  return To;
3218af667588d53de22795c5304f1496ccaac2a71402Daniel Dunbar}
3219ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
3220ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregorbool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
3221ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  llvm::DenseMap<Type *, Type *>::iterator Pos
3222ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor   = ImportedTypes.find(From.getTypePtr());
3223ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
3224ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return true;
3225ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
3226bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(FromContext, ToContext, Diags,
3227ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   NonEquivalentDecls);
3228bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(From, To);
3229ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor}
3230