ASTImporter.cpp revision bd054dba8a3023821f2a0951b0fae05e3522a7c9
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
761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitObjCInterfaceType(ObjCInterfaceType *T);
77c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    QualType VisitObjCObjectType(ObjCObjectType *T);
781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T);
79089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
80089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    // Importing declarations
81a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
82a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                         DeclContext *&LexicalDC, DeclarationName &Name,
83788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                         SourceLocation &Loc);
84083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    void ImportDeclContext(DeclContext *FromDC);
8596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
8673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
8789cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    Decl *VisitDecl(Decl *D);
88788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    Decl *VisitNamespaceDecl(NamespaceDecl *D);
899e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
9036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
9196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *VisitRecordDecl(RecordDecl *D);
9236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
93a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D);
94c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
95c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
96c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
97c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
9896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
992e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
100089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
1012cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
102a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Decl *VisitParmVarDecl(ParmVarDecl *D);
103c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
104b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
1052e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
106a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
107e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
1082b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
109a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Decl *VisitObjCClassDecl(ObjCClassDecl *D);
110a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
1114800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    // Importing statements
1124800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Stmt *VisitStmt(Stmt *S);
1134800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
1144800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    // Importing expressions
1154800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Expr *VisitExpr(Expr *E);
116440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    Expr *VisitDeclRefExpr(DeclRefExpr *E);
1174800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Expr *VisitIntegerLiteral(IntegerLiteral *E);
118b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor    Expr *VisitCharacterLiteral(CharacterLiteral *E);
119f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitParenExpr(ParenExpr *E);
120f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitUnaryOperator(UnaryOperator *E);
121bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    Expr *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
122f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitBinaryOperator(BinaryOperator *E);
123f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
12436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
125008847a70ab122a99911149199855060fb3753b4Douglas Gregor    Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
1261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  };
1271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
1281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//----------------------------------------------------------------------------
13073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor// Structural Equivalence
13173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor//----------------------------------------------------------------------------
13273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
13373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregornamespace {
13473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  struct StructuralEquivalenceContext {
13573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief AST contexts for which we are checking structural equivalence.
13673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    ASTContext &C1, &C2;
13773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
13873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Diagnostic object used to emit diagnostics.
13973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Diagnostic &Diags;
14073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
14173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief The set of "tentative" equivalences between two canonical
14273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// declarations, mapping from a declaration in the first context to the
14373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// declaration in the second context that we believe to be equivalent.
14473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
14573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
14673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Queue of declarations in the first context whose equivalence
14773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// with a declaration in the second context still needs to be verified.
14873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    std::deque<Decl *> DeclsToCheck;
14973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
150ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    /// \brief Declaration (from, to) pairs that are known not to be equivalent
151ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    /// (which we have already complained about).
152ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
153ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
15473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Whether we're being strict about the spelling of types when
15573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// unifying two types.
15673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool StrictTypeSpelling;
15773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
15873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
15973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                 Diagnostic &Diags,
160ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor               llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
16173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                 bool StrictTypeSpelling = false)
162ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      : C1(C1), C2(C2), Diags(Diags), NonEquivalentDecls(NonEquivalentDecls),
163ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        StrictTypeSpelling(StrictTypeSpelling) { }
16473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
16573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Determine whether the two declarations are structurally
16673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// equivalent.
16773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
16873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
16973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Determine whether the two types are structurally equivalent.
17073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructurallyEquivalent(QualType T1, QualType T2);
17173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
17273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  private:
17373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Finish checking all of the structural equivalences.
17473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    ///
17573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \returns true if an error occurred, false otherwise.
17673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool Finish();
17773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
17873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  public:
17973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
18073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return Diags.Report(FullSourceLoc(Loc, C1.getSourceManager()), DiagID);
18173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
18273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
18373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
18473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return Diags.Report(FullSourceLoc(Loc, C2.getSourceManager()), DiagID);
18573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
18673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  };
18773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
18873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
18973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
19073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     QualType T1, QualType T2);
19173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
19273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Decl *D1, Decl *D2);
19373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
19473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine if two APInts have the same value, after zero-extending
19573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// one of them (if needed!) to ensure that the bit-widths match.
19673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
19773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() == I2.getBitWidth())
19873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == I2;
19973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
20073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() > I2.getBitWidth())
20173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == llvm::APInt(I2).zext(I1.getBitWidth());
20273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
20373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return llvm::APInt(I1).zext(I2.getBitWidth()) == I2;
20473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
20573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
20673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine if two APSInts have the same value, zero- or sign-extending
20773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// as needed.
20873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
20973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
21073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == I2;
21173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
21273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Check for a bit-width mismatch.
21373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() > I2.getBitWidth())
21473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return IsSameValue(I1, llvm::APSInt(I2).extend(I1.getBitWidth()));
21573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  else if (I2.getBitWidth() > I1.getBitWidth())
21673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return IsSameValue(llvm::APSInt(I1).extend(I2.getBitWidth()), I2);
21773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
21873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // We have a signedness mismatch. Turn the signed value into an unsigned
21973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // value.
22073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.isSigned()) {
22173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (I1.isNegative())
22273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
22373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
22473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return llvm::APSInt(I1, true) == I2;
22573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
22673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
22773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I2.isNegative())
22873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
22973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
23073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return I1 == llvm::APSInt(I2, true);
23173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
23273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
23373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two expressions.
23473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
23573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Expr *E1, Expr *E2) {
23673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!E1 || !E2)
23773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return E1 == E2;
23873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
23973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Actually perform a structural comparison!
24073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
24173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
24273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two identifiers are equivalent.
24473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
24573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const IdentifierInfo *Name2) {
24673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!Name1 || !Name2)
24773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return Name1 == Name2;
24873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return Name1->getName() == Name2->getName();
25073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
25173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
25273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two nested-name-specifiers are equivalent.
25373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
25473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     NestedNameSpecifier *NNS1,
25573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     NestedNameSpecifier *NNS2) {
25673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Implement!
25773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
25873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
25973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
26073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two template arguments are equivalent.
26173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
26273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const TemplateArgument &Arg1,
26373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const TemplateArgument &Arg2) {
26473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Implement!
26573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
26673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
26773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
26873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence for the common part of array
26973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// types.
27073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
27173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                          const ArrayType *Array1,
27273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                          const ArrayType *Array2) {
27373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!IsStructurallyEquivalent(Context,
27473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                Array1->getElementType(),
27573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                Array2->getElementType()))
27673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
27773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Array1->getSizeModifier() != Array2->getSizeModifier())
27873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
27973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
28073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
28173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
28273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
28373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
28473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
28573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two types.
28673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
28773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     QualType T1, QualType T2) {
28873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (T1.isNull() || T2.isNull())
28973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return T1.isNull() && T2.isNull();
29073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
29173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!Context.StrictTypeSpelling) {
29273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // We aren't being strict about token-to-token equivalence of types,
29373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // so map down to the canonical type.
29473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    T1 = Context.C1.getCanonicalType(T1);
29573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    T2 = Context.C2.getCanonicalType(T2);
29673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
29773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
29873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (T1.getQualifiers() != T2.getQualifiers())
29973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
30073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
301ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  Type::TypeClass TC = T1->getTypeClass();
302ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
303ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T1->getTypeClass() != T2->getTypeClass()) {
304ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // Compare function types with prototypes vs. without prototypes as if
305ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // both did not have prototypes.
306ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (T1->getTypeClass() == Type::FunctionProto &&
307ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        T2->getTypeClass() == Type::FunctionNoProto)
308ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      TC = Type::FunctionNoProto;
309ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    else if (T1->getTypeClass() == Type::FunctionNoProto &&
310ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor             T2->getTypeClass() == Type::FunctionProto)
311ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      TC = Type::FunctionNoProto;
312ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    else
313ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      return false;
314ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  }
31573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
316ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  switch (TC) {
317ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  case Type::Builtin:
31873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Deal with Char_S/Char_U.
31973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
32073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
32173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
32273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
32373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Complex:
32473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
32573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<ComplexType>(T1)->getElementType(),
32673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<ComplexType>(T2)->getElementType()))
32773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
32873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
32973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
33073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Pointer:
33173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
33273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<PointerType>(T1)->getPointeeType(),
33373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<PointerType>(T2)->getPointeeType()))
33473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
33573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
33673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
33773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::BlockPointer:
33873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
33973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<BlockPointerType>(T1)->getPointeeType(),
34073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<BlockPointerType>(T2)->getPointeeType()))
34173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
34273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
34373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
34473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::LValueReference:
34573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::RValueReference: {
34673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ReferenceType *Ref1 = cast<ReferenceType>(T1);
34773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ReferenceType *Ref2 = cast<ReferenceType>(T2);
34873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
34973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
35073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Ref1->isInnerRef() != Ref2->isInnerRef())
35173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
35273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
35373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ref1->getPointeeTypeAsWritten(),
35473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ref2->getPointeeTypeAsWritten()))
35573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
35673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
35773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
35873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
35973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::MemberPointer: {
36073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
36173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
36273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
36373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  MemPtr1->getPointeeType(),
36473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  MemPtr2->getPointeeType()))
36573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
36673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
36773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(MemPtr1->getClass(), 0),
36873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(MemPtr2->getClass(), 0)))
36973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
37073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
37173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
37273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
37373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ConstantArray: {
37473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
37573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
37673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsSameValue(Array1->getSize(), Array2->getSize()))
37773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
37873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
37973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
38073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
38173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
38273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
38373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
38473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::IncompleteArray:
38573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context,
38673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                       cast<ArrayType>(T1),
38773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                       cast<ArrayType>(T2)))
38873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
38973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
39073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
39173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::VariableArray: {
39273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
39373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
39473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
39573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Array1->getSizeExpr(), Array2->getSizeExpr()))
39673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
39773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
39873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
39973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
40073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
40173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
40273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
40373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
40473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::DependentSizedArray: {
40573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
40673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
40773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
40873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Array1->getSizeExpr(), Array2->getSizeExpr()))
40973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
41073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
41173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
41273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
41373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
41473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
41573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
41673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
41773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::DependentSizedExtVector: {
41873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedExtVectorType *Vec1
41973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<DependentSizedExtVectorType>(T1);
42073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedExtVectorType *Vec2
42173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<DependentSizedExtVectorType>(T2);
42273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
42373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getSizeExpr(), Vec2->getSizeExpr()))
42473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
42573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
42673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getElementType(),
42773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec2->getElementType()))
42873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
42973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
43073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
43173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
43273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Vector:
43373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ExtVector: {
43473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VectorType *Vec1 = cast<VectorType>(T1);
43573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VectorType *Vec2 = cast<VectorType>(T2);
43673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
43773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getElementType(),
43873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec2->getElementType()))
43973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
44073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Vec1->getNumElements() != Vec2->getNumElements())
44173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
44273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Vec1->isAltiVec() != Vec2->isAltiVec())
44373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
44473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Vec1->isPixel() != Vec2->isPixel())
44573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
4460e12b44081c6395a6d60a05a85a6012f7bb23b16Douglas Gregor    break;
44773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
44873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
44973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::FunctionProto: {
45073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
45173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
45273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getNumArgs() != Proto2->getNumArgs())
45373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
45473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
45573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
45673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto1->getArgType(I),
45773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto2->getArgType(I)))
45873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
45973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
46073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->isVariadic() != Proto2->isVariadic())
46173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
46373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
46573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
46773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
46973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
47073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto1->getExceptionType(I),
47173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto2->getExceptionType(I)))
47273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
47373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
47473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
47573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
47673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
47773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Fall through to check the bits common with FunctionNoProtoType.
47873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
47973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
48073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::FunctionNoProto: {
48173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionType *Function1 = cast<FunctionType>(T1);
48273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionType *Function2 = cast<FunctionType>(T2);
48373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
48473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Function1->getResultType(),
48573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Function2->getResultType()))
48673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
487264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      if (Function1->getExtInfo() != Function2->getExtInfo())
488264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola        return false;
48973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
49073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
49173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
49273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::UnresolvedUsing:
49373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
49473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<UnresolvedUsingType>(T1)->getDecl(),
49573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<UnresolvedUsingType>(T2)->getDecl()))
49673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
49773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
49873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
49973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
50073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Typedef:
50173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
50273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypedefType>(T1)->getDecl(),
50373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypedefType>(T2)->getDecl()))
50473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
50573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
50673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
50773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TypeOfExpr:
50873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
50973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
51073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
51173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
51273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
51373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
51473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TypeOf:
51573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
51673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypeOfType>(T1)->getUnderlyingType(),
51773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypeOfType>(T2)->getUnderlyingType()))
51873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
51973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
52073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
52173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Decltype:
52273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
52373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<DecltypeType>(T1)->getUnderlyingExpr(),
52473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<DecltypeType>(T2)->getUnderlyingExpr()))
52573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
52673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
52773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
52873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Record:
52973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Enum:
53073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
53173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TagType>(T1)->getDecl(),
53273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TagType>(T2)->getDecl()))
53373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
53473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
535465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
53673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TemplateTypeParm: {
53773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
53873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
53973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->getDepth() != Parm2->getDepth())
54073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
54173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->getIndex() != Parm2->getIndex())
54273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
54373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->isParameterPack() != Parm2->isParameterPack())
54473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
54573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
54673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Names of template type parameters are never significant.
54773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
54873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
54973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
55073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::SubstTemplateTypeParm: {
55173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const SubstTemplateTypeParmType *Subst1
55273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<SubstTemplateTypeParmType>(T1);
55373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const SubstTemplateTypeParmType *Subst2
55473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<SubstTemplateTypeParmType>(T2);
55573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
55673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Subst1->getReplacedParameter(), 0),
55773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Subst2->getReplacedParameter(), 0)))
55873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
55973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
56073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Subst1->getReplacementType(),
56173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Subst2->getReplacementType()))
56273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
56373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
56473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
56573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
56673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TemplateSpecialization: {
56773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateSpecializationType *Spec1
56873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<TemplateSpecializationType>(T1);
56973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateSpecializationType *Spec2
57073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<TemplateSpecializationType>(T2);
57173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
57273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Spec1->getTemplateName(),
57373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Spec2->getTemplateName()))
57473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
57573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Spec1->getNumArgs() != Spec2->getNumArgs())
57673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
57773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
57873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
57973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Spec1->getArg(I), Spec2->getArg(I)))
58073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
58173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
58273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
58373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
58473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
585465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Type::Elaborated: {
586465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
587465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
588465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    // CHECKME: what if a keyword is ETK_None or ETK_typename ?
589465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Elab1->getKeyword() != Elab2->getKeyword())
590465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return false;
59173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
592465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab1->getQualifier(),
593465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab2->getQualifier()))
59473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
59573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
596465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab1->getNamedType(),
597465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab2->getNamedType()))
59873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
59973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
60073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
60173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
6023cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  case Type::InjectedClassName: {
6033cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
6043cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
6053cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (!IsStructurallyEquivalent(Context,
60631f17ecbef57b5679c017c375db330546b7b5145John McCall                                  Inj1->getInjectedSpecializationType(),
60731f17ecbef57b5679c017c375db330546b7b5145John McCall                                  Inj2->getInjectedSpecializationType()))
6083cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      return false;
6093cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    break;
6103cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
6113cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
6124714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  case Type::DependentName: {
6134714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    const DependentNameType *Typename1 = cast<DependentNameType>(T1);
6144714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    const DependentNameType *Typename2 = cast<DependentNameType>(T2);
61573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
61673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename1->getQualifier(),
61773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename2->getQualifier()))
61873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
61973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
62073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename2->getIdentifier()))
62173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
62273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
62373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Typename1->getTemplateId(), 0),
62473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Typename2->getTemplateId(), 0)))
62573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
62673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
62773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
62873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
62973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
63073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ObjCInterface: {
63173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
63273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
63373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
63473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Iface1->getDecl(), Iface2->getDecl()))
63573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
636c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    break;
637c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  }
638c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
639c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject: {
640c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
641c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
642c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (!IsStructurallyEquivalent(Context,
643c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                  Obj1->getBaseType(),
644c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                  Obj2->getBaseType()))
645c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return false;
646c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
64773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
648c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
64973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
650c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Obj1->getProtocol(I),
651c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Obj2->getProtocol(I)))
65273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
65373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
65473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
65573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
65673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
65773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ObjCObjectPointer: {
65873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
65973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
66073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
66173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ptr1->getPointeeType(),
66273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ptr2->getPointeeType()))
66373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
66473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
66573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
66673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
66773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  } // end switch
66873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
66973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
67073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
67173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
67273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two records.
67373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
67473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     RecordDecl *D1, RecordDecl *D2) {
67573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (D1->isUnion() != D2->isUnion()) {
67673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
67773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
67873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
67973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << D1->getDeclName() << (unsigned)D1->getTagKind();
68073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
68173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
68273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
683ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Compare the definitions of these two records. If either or both are
684ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // incomplete, we assume that they are equivalent.
685ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  D1 = D1->getDefinition();
686ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  D2 = D2->getDefinition();
687ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (!D1 || !D2)
688ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return true;
689ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
69073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
69173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
69273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
69373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
69473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
69573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
69673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << D2CXX->getNumBases();
69773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
69873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << D1CXX->getNumBases();
69973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
70073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
70173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
70273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      // Check the base classes.
70373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
70473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                           BaseEnd1 = D1CXX->bases_end(),
70573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                Base2 = D2CXX->bases_begin();
70673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor           Base1 != BaseEnd1;
70773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor           ++Base1, ++Base2) {
70873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!IsStructurallyEquivalent(Context,
70973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                      Base1->getType(), Base2->getType())) {
71073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
71173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Context.C2.getTypeDeclType(D2);
71273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
71373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->getType()
71473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->getSourceRange();
71573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
71673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getType()
71773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getSourceRange();
71873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          return false;
71973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        }
72073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
72173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check virtual vs. non-virtual inheritance mismatch.
72273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (Base1->isVirtual() != Base2->isVirtual()) {
72373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
72473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Context.C2.getTypeDeclType(D2);
72573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(Base2->getSourceRange().getBegin(),
72673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                        diag::note_odr_virtual_base)
72773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->isVirtual() << Base2->getSourceRange();
72873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
72973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->isVirtual()
73073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getSourceRange();
73173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          return false;
73273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        }
73373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
73473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    } else if (D1CXX->getNumBases() > 0) {
73573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
73673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
73773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
73873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
73973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Base1->getType()
74073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Base1->getSourceRange();
74173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
74273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
74373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
74473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
74573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
74673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Check the fields for consistency.
74773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
74873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                             Field2End = D2->field_end();
74973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
75073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Field1End = D1->field_end();
75173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       Field1 != Field1End;
75273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       ++Field1, ++Field2) {
75373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field2 == Field2End) {
75473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
75573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
75673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Field1->getLocation(), diag::note_odr_field)
75773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName() << Field1->getType();
75873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
75973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
76073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
76173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
76273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
76373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Field1->getType(), Field2->getType())) {
76473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
76573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
76673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(Field2->getLocation(), diag::note_odr_field)
76773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field2->getDeclName() << Field2->getType();
76873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Field1->getLocation(), diag::note_odr_field)
76973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName() << Field1->getType();
77073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
77173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
77273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
77373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field1->isBitField() != Field2->isBitField()) {
77473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
77573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
77673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (Field1->isBitField()) {
77773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        llvm::APSInt Bits;
77873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
77973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
78073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field1->getDeclName() << Field1->getType()
78173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits.toString(10, false);
78273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
78373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName();
78473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
78573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        llvm::APSInt Bits;
78673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
78773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
78873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName() << Field2->getType()
78973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits.toString(10, false);
79073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(),
79173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                          diag::note_odr_not_bit_field)
79273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName();
79373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
79473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
79573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
79673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
79773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field1->isBitField()) {
79873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      // Make sure that the bit-fields are the same length.
79973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      llvm::APSInt Bits1, Bits2;
80073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
80173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
80273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
80373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
80473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
80573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsSameValue(Bits1, Bits2)) {
80673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
80773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Context.C2.getTypeDeclType(D2);
80873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
80973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName() << Field2->getType()
81073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits2.toString(10, false);
81173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
81273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field1->getDeclName() << Field1->getType()
81373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits1.toString(10, false);
81473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
81573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
81673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
81773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
81873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
81973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Field2 != Field2End) {
82073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
82173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
82273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(Field2->getLocation(), diag::note_odr_field)
82373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Field2->getDeclName() << Field2->getType();
82473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
82573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
82673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
82773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
82873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
82973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
83073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
83173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two enums.
83273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
83373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     EnumDecl *D1, EnumDecl *D2) {
83473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
83573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                             EC2End = D2->enumerator_end();
83673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
83773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  EC1End = D1->enumerator_end();
83873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       EC1 != EC1End; ++EC1, ++EC2) {
83973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (EC2 == EC2End) {
84073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
84173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
84273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
84373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getDeclName()
84473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getInitVal().toString(10);
84573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
84673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
84773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
84873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
84973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::APSInt Val1 = EC1->getInitVal();
85073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::APSInt Val2 = EC2->getInitVal();
85173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsSameValue(Val1, Val2) ||
85273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
85373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
85473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
85573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
85673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC2->getDeclName()
85773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC2->getInitVal().toString(10);
85873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
85973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getDeclName()
86073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getInitVal().toString(10);
86173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
86273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
86373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
86473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
86573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (EC2 != EC2End) {
86673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
86773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
86873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
86973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << EC2->getDeclName()
87073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << EC2->getInitVal().toString(10);
87173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
87273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
87373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
87473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
87573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
87673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
87773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
87873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two declarations.
87973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
88073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Decl *D1, Decl *D2) {
88173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Check for known structural equivalences via a callback of some sort.
88273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
883ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Check whether we already know that these two declarations are not
884ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // structurally equivalent.
885ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
886ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                      D2->getCanonicalDecl())))
887ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return false;
888ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
88973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Determine whether we've already produced a tentative equivalence for D1.
89073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
89173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (EquivToD1)
89273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return EquivToD1 == D2->getCanonicalDecl();
89373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
89473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Produce a tentative equivalence D1 <-> D2, which will be checked later.
89573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EquivToD1 = D2->getCanonicalDecl();
89673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
89773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
89873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
89973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
90073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
90173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                            Decl *D2) {
90273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!::IsStructurallyEquivalent(*this, D1, D2))
90373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
90473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
90573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return !Finish();
90673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
90773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
90873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
90973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                            QualType T2) {
91073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!::IsStructurallyEquivalent(*this, T1, T2))
91173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
91273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
91373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return !Finish();
91473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
91573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
91673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::Finish() {
91773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  while (!DeclsToCheck.empty()) {
91873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Check the next declaration.
91973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Decl *D1 = DeclsToCheck.front();
92073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DeclsToCheck.pop_front();
92173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
92273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Decl *D2 = TentativeEquivalences[D1];
92373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    assert(D2 && "Unrecorded tentative equivalence?");
92473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
925ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    bool Equivalent = true;
926ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
92773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Switch on all declaration kinds. For now, we're just going to
92873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // check the obvious ones.
92973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
93073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
93173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check for equivalent structure names.
93273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name1 = Record1->getIdentifier();
93373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name1 && Record1->getTypedefForAnonDecl())
93473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
93573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name2 = Record2->getIdentifier();
93673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name2 && Record2->getTypedefForAnonDecl())
93773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
938ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (!::IsStructurallyEquivalent(Name1, Name2) ||
939ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this, Record1, Record2))
940ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
94173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
94273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Record/non-record mismatch.
943ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
94473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
945ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
94673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
94773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check for equivalent enum names.
94873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name1 = Enum1->getIdentifier();
94973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name1 && Enum1->getTypedefForAnonDecl())
95073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
95173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name2 = Enum2->getIdentifier();
95273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name2 && Enum2->getTypedefForAnonDecl())
95373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
954ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (!::IsStructurallyEquivalent(Name1, Name2) ||
955ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this, Enum1, Enum2))
956ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
95773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
95873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Enum/non-enum mismatch
959ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
96073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
961ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
96273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
96373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
964ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                        Typedef2->getIdentifier()) ||
965ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this,
96673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        Typedef1->getUnderlyingType(),
96773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        Typedef2->getUnderlyingType()))
968ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
96973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
97073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Typedef/non-typedef mismatch.
971ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
97273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
97373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
974ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
975ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (!Equivalent) {
976ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      // Note that these two declarations are not equivalent (and we already
977ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      // know about it).
978ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
979ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                               D2->getCanonicalDecl()));
980ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      return true;
981ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    }
98273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Check other declaration kinds!
98373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
98473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
98573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return false;
98673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
98773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
98873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor//----------------------------------------------------------------------------
9891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor// Import Types
9901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//----------------------------------------------------------------------------
9911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
99289cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas GregorQualType ASTNodeImporter::VisitType(Type *T) {
99389cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor  Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
99489cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    << T->getTypeClassName();
99589cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor  return QualType();
99689cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor}
99789cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor
9981b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
9991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  switch (T->getKind()) {
10001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Void: return Importer.getToContext().VoidTy;
10011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Bool: return Importer.getToContext().BoolTy;
10021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char_U:
10041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // The context we're importing from has an unsigned 'char'. If we're
10051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // importing into a context with a signed 'char', translate to
10061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // 'unsigned char' instead.
10071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (Importer.getToContext().getLangOptions().CharIsSigned)
10081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return Importer.getToContext().UnsignedCharTy;
10091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().CharTy;
10111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
10131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char16:
10151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++!
10161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().Char16Ty;
10171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char32:
10191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++!
10201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().Char32Ty;
10211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
10231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
10241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
10251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ULongLong:
10261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().UnsignedLongLongTy;
10271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
10281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char_S:
10301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // The context we're importing from has an unsigned 'char'. If we're
10311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // importing into a context with a signed 'char', translate to
10321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // 'unsigned char' instead.
10331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Importer.getToContext().getLangOptions().CharIsSigned)
10341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return Importer.getToContext().SignedCharTy;
10351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().CharTy;
10371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
10391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::WChar:
10401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: If not in C++, shall we translate to the C equivalent of
10411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // wchar_t?
10421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().WCharTy;
10431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Short : return Importer.getToContext().ShortTy;
10451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Int : return Importer.getToContext().IntTy;
10461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Long : return Importer.getToContext().LongTy;
10471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
10481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
10491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Float: return Importer.getToContext().FloatTy;
10501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Double: return Importer.getToContext().DoubleTy;
10511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
10521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::NullPtr:
10541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++0x!
10551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().NullPtrTy;
10561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
10581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
10591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UndeducedAuto:
10601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++0x!
10611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().UndeducedAutoTy;
10621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCId:
10641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports Objective-C!
10651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinIdTy;
10661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCClass:
10681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinClassTy;
10691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCSel:
10711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinSelTy;
10721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
10731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return QualType();
10751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
10761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10771b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
10781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
10791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
10801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
10811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getComplexType(ToElementType);
10831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
10841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10851b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitPointerType(PointerType *T) {
10861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
10871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
10881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
10891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getPointerType(ToPointeeType);
10911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
10921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10931b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
10941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for blocks support in "to" context.
10951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
10961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
10971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
10981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
10991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getBlockPointerType(ToPointeeType);
11001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11021b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
11031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++ support in "to" context.
11041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
11051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
11061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getLValueReferenceType(ToPointeeType);
11091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11111b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
11121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++0x support in "to" context.
11131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
11141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
11151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getRValueReferenceType(ToPointeeType);
11181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11201b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
11211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++ support in "to" context.
11221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
11231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
11241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
11271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getMemberPointerType(ToPointeeType,
11281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      ClassType.getTypePtr());
11291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11311b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
11321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getConstantArrayType(ToElementType,
11371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSize(),
11381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSizeModifier(),
11391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->getIndexTypeCVRQualifiers());
11401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11421b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
11431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getIncompleteArrayType(ToElementType,
11481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                        T->getSizeModifier(),
11491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                T->getIndexTypeCVRQualifiers());
11501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11521b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
11531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *Size = Importer.Import(T->getSizeExpr());
11581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!Size)
11591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  SourceRange Brackets = Importer.Import(T->getBracketsRange());
11621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getVariableArrayType(ToElementType, Size,
11631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSizeModifier(),
11641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                T->getIndexTypeCVRQualifiers(),
11651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      Brackets);
11661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11681b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitVectorType(VectorType *T) {
11691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getVectorType(ToElementType,
11741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->getNumElements(),
11751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->isAltiVec(),
11761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->isPixel());
11771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11791b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
11801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
11811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
11821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
11831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getExtVectorType(ToElementType,
11851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                  T->getNumElements());
11861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11881b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
11891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: What happens if we're importing a function without a prototype
11901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // into C++? Should we make it variadic?
11911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToResultType = Importer.Import(T->getResultType());
11921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToResultType.isNull())
11931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
1194264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola
11951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getFunctionNoProtoType(ToResultType,
1196264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                        T->getExtInfo());
11971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
11981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
11991b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
12001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToResultType = Importer.Import(T->getResultType());
12011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToResultType.isNull())
12021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Import argument types
12051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<QualType, 4> ArgTypes;
12061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
12071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                         AEnd = T->arg_type_end();
12081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       A != AEnd; ++A) {
12091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType ArgType = Importer.Import(*A);
12101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (ArgType.isNull())
12111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
12121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ArgTypes.push_back(ArgType);
12131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
12141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Import exception types
12161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<QualType, 4> ExceptionTypes;
12171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (FunctionProtoType::exception_iterator E = T->exception_begin(),
12181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                          EEnd = T->exception_end();
12191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       E != EEnd; ++E) {
12201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType ExceptionType = Importer.Import(*E);
12211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (ExceptionType.isNull())
12221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
12231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ExceptionTypes.push_back(ExceptionType);
12241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
12251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
12271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 ArgTypes.size(),
12281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->isVariadic(),
12291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->getTypeQuals(),
12301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->hasExceptionSpec(),
12311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->hasAnyExceptionSpec(),
12321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 ExceptionTypes.size(),
12331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 ExceptionTypes.data(),
1234264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                 T->getExtInfo());
12351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12371b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
12381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  TypedefDecl *ToDecl
12391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
12401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
12411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeDeclType(ToDecl);
12441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12461b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
12471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
12481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToExpr)
12491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeOfExprType(ToExpr);
12521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12541b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
12551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
12561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToUnderlyingType.isNull())
12571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeOfType(ToUnderlyingType);
12601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12621b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
12631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
12641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToExpr)
12651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getDecltypeType(ToExpr);
12681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12701b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitRecordType(RecordType *T) {
12711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  RecordDecl *ToDecl
12721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
12731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
12741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTagDeclType(ToDecl);
12771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12791b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitEnumType(EnumType *T) {
12801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  EnumDecl *ToDecl
12811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
12821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
12831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
12841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTagDeclType(ToDecl);
12861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
12871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12881b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
1289465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *ToQualifier = 0;
1290465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  // Note: the qualifier in an ElaboratedType is optional.
1291465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T->getQualifier()) {
1292465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ToQualifier = Importer.Import(T->getQualifier());
1293465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (!ToQualifier)
1294465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return QualType();
1295465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
12961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToNamedType = Importer.Import(T->getNamedType());
12981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToNamedType.isNull())
12991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1301465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Importer.getToContext().getElaboratedType(T->getKeyword(),
1302465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                                   ToQualifier, ToNamedType);
13031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13051b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
13061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  ObjCInterfaceDecl *Class
13071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
13081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!Class)
13091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1311c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCInterfaceType(Class);
1312c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
1313c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
1314c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) {
1315c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  QualType ToBaseType = Importer.Import(T->getBaseType());
1316c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ToBaseType.isNull())
1317c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType();
1318c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
13191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
1320c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator P = T->qual_begin(),
13211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                     PEnd = T->qual_end();
13221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       P != PEnd; ++P) {
13231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ObjCProtocolDecl *Protocol
13241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
13251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Protocol)
13261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
13271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    Protocols.push_back(Protocol);
13281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
13291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1330c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCObjectType(ToBaseType,
1331c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                                   Protocols.data(),
1332c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                                   Protocols.size());
13331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13351b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
13361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
13371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
13381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1340c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
13411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1343089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor//----------------------------------------------------------------------------
1344089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor// Import Declarations
1345089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor//----------------------------------------------------------------------------
1346a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregorbool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1347a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      DeclContext *&LexicalDC,
1348a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      DeclarationName &Name,
1349a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      SourceLocation &Loc) {
1350089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Import the context of this declaration.
1351a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DC = Importer.ImportContext(D->getDeclContext());
1352089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  if (!DC)
1353a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return true;
1354a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1355a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  LexicalDC = DC;
13569bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
13579bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
13589bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    if (!LexicalDC)
1359a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      return true;
13609bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  }
1361a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1362089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Import the name of this declaration.
1363a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Name = Importer.Import(D->getDeclName());
1364089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  if (D->getDeclName() && !Name)
1365a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return true;
1366a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1367a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the location of this declaration.
1368a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Loc = Importer.Import(D->getLocation());
1369a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  return false;
1370a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1371a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1372083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregorvoid ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1373083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1374083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor                               FromEnd = FromDC->decls_end();
1375083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor       From != FromEnd;
1376083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor       ++From)
1377083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    Importer.Import(*From);
1378083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor}
1379083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor
138096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregorbool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
138173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        RecordDecl *ToRecord) {
1382bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
138373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Importer.getToContext(),
1384ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getDiags(),
1385ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getNonEquivalentDecls());
1386bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
138796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
138896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
138936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregorbool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
1390bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
139173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Importer.getToContext(),
1392ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getDiags(),
1393ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getNonEquivalentDecls());
1394bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
139536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
139636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
1397a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitDecl(Decl *D) {
1398a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1399a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    << D->getDeclKindName();
1400a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  return 0;
1401a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1402a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1403788c62d1e87bfb596078817237f672a5f000999aDouglas GregorDecl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1404788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  // Import the major distinguishing characteristics of this namespace.
1405788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  DeclContext *DC, *LexicalDC;
1406788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  DeclarationName Name;
1407788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  SourceLocation Loc;
1408788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1409788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    return 0;
1410788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1411788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  NamespaceDecl *MergeWithNamespace = 0;
1412788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (!Name) {
1413788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // This is an anonymous namespace. Adopt an existing anonymous
1414788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // namespace if we can.
1415788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // FIXME: Not testable.
1416788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1417788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      MergeWithNamespace = TU->getAnonymousNamespace();
1418788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    else
1419788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1420788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  } else {
1421788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1422788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1423788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor         Lookup.first != Lookup.second;
1424788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor         ++Lookup.first) {
14250d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
1426788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        continue;
1427788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1428788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1429788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        MergeWithNamespace = FoundNS;
1430788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        ConflictingDecls.clear();
1431788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        break;
1432788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      }
1433788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1434788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
1435788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
1436788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1437788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (!ConflictingDecls.empty()) {
14380d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
1439788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                         ConflictingDecls.data(),
1440788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                         ConflictingDecls.size());
1441788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
1442788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  }
1443788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1444788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  // Create the "to" namespace, if needed.
1445788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  NamespaceDecl *ToNamespace = MergeWithNamespace;
1446788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (!ToNamespace) {
1447788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1448788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                        Name.getAsIdentifierInfo());
1449788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    ToNamespace->setLexicalDeclContext(LexicalDC);
1450788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    LexicalDC->addDecl(ToNamespace);
1451788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1452788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // If this is an anonymous namespace, register it as the anonymous
1453788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // namespace within its context.
1454788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (!Name) {
1455788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1456788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        TU->setAnonymousNamespace(ToNamespace);
1457788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      else
1458788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1459788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
1460788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  }
1461788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  Importer.Imported(D, ToNamespace);
1462788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1463788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  ImportDeclContext(D);
1464788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1465788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  return ToNamespace;
1466788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor}
1467788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
14689e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas GregorDecl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
14699e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // Import the major distinguishing characteristics of this typedef.
14709e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  DeclContext *DC, *LexicalDC;
14719e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  DeclarationName Name;
14729e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  SourceLocation Loc;
14739e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
14749e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    return 0;
14759e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
14769e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // If this typedef is not in block scope, determine whether we've
14779e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // seen a typedef with the same name (that we can merge with) or any
14789e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // other entity by that name (which name lookup could conflict with).
14799e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  if (!DC->isFunctionOrMethod()) {
14809e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
14819e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
14829e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
14839e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor         Lookup.first != Lookup.second;
14849e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor         ++Lookup.first) {
14859e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
14869e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor        continue;
14879e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
1488ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1489ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                            FoundTypedef->getUnderlyingType()))
14905ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor          return Importer.Imported(D, FoundTypedef);
14919e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      }
14929e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
14939e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
14949e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    }
14959e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
14969e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    if (!ConflictingDecls.empty()) {
14979e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
14989e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                         ConflictingDecls.data(),
14999e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                         ConflictingDecls.size());
15009e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (!Name)
15019e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor        return 0;
15029e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    }
15039e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  }
15049e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
1505ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the underlying type of this typedef;
1506ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getUnderlyingType());
1507ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
1508ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
1509ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
15109e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // Create the new typedef node.
15119e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
15129e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
15139e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                               Loc, Name.getAsIdentifierInfo(),
15149e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                               TInfo);
1515325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToTypedef->setAccess(D->getAccess());
15169e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  ToTypedef->setLexicalDeclContext(LexicalDC);
15175ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToTypedef);
15189e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  LexicalDC->addDecl(ToTypedef);
1519ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
15209e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  return ToTypedef;
15219e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor}
15229e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
152336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorDecl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
152436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the major distinguishing characteristics of this enum.
152536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclContext *DC, *LexicalDC;
152636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName Name;
152736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  SourceLocation Loc;
152836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
152936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
153036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
153136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Figure out what enum name we're looking for.
153236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  unsigned IDNS = Decl::IDNS_Tag;
153336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName SearchName = Name;
153436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!SearchName && D->getTypedefForAnonDecl()) {
153536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
153636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    IDNS = Decl::IDNS_Ordinary;
153736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  } else if (Importer.getToContext().getLangOptions().CPlusPlus)
153836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    IDNS |= Decl::IDNS_Ordinary;
153936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
154036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // We may already have an enum of the same name; try to find and match it.
154136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!DC->isFunctionOrMethod() && SearchName) {
154236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
154336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
154436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         Lookup.first != Lookup.second;
154536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++Lookup.first) {
154636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
154736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        continue;
154836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
154936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Decl *Found = *Lookup.first;
155036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
155136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
155236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor          Found = Tag->getDecl();
155336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      }
155436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
155536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
15565ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor        if (IsStructuralMatch(D, FoundEnum))
15575ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor          return Importer.Imported(D, FoundEnum);
155836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      }
155936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
156036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
156136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
156236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
156336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (!ConflictingDecls.empty()) {
156436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
156536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.data(),
156636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.size());
156736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
156836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
156936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
157036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Create the enum declaration.
157173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
157236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                      Name.getAsIdentifierInfo(),
157336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                      Importer.Import(D->getTagKeywordLoc()),
157436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                      0);
1575b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
1576b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (D->getQualifier()) {
1577b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1578b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1579b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    D2->setQualifierInfo(NNS, NNSRange);
1580b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1581325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  D2->setAccess(D->getAccess());
158273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  D2->setLexicalDeclContext(LexicalDC);
158373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Importer.Imported(D, D2);
158473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  LexicalDC->addDecl(D2);
158536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
158636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the integer type.
158736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  QualType ToIntegerType = Importer.Import(D->getIntegerType());
158836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (ToIntegerType.isNull())
158936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
159073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  D2->setIntegerType(ToIntegerType);
159136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
159236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the definition
159336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (D->isDefinition()) {
159436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
159536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (T.isNull())
159636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      return 0;
159736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
159836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    QualType ToPromotionType = Importer.Import(D->getPromotionType());
159936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (ToPromotionType.isNull())
160036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      return 0;
160136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
160273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->startDefinition();
1603083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    ImportDeclContext(D);
16041b5a618c59025898806160ed5e7f0ff5bb79e482John McCall
16051b5a618c59025898806160ed5e7f0ff5bb79e482John McCall    // FIXME: we might need to merge the number of positive or negative bits
16061b5a618c59025898806160ed5e7f0ff5bb79e482John McCall    // if the enumerator lists don't match.
16071b5a618c59025898806160ed5e7f0ff5bb79e482John McCall    D2->completeDefinition(T, ToPromotionType,
16081b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                           D->getNumPositiveBits(),
16091b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                           D->getNumNegativeBits());
161036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
161136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
161273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return D2;
161336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
161436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
161596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas GregorDecl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
161696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // If this record has a definition in the translation unit we're coming from,
161796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // but this particular declaration is not that definition, import the
161896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // definition and map to that.
1619952b017601f9c82b51119c3a1600f1312a833db9Douglas Gregor  TagDecl *Definition = D->getDefinition();
162096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (Definition && Definition != D) {
162196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *ImportedDef = Importer.Import(Definition);
16225ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor    if (!ImportedDef)
16235ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor      return 0;
16245ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
16255ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor    return Importer.Imported(D, ImportedDef);
162696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
162796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
162896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Import the major distinguishing characteristics of this record.
162996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclContext *DC, *LexicalDC;
163096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName Name;
163196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  SourceLocation Loc;
163296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
163396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
163496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
163596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Figure out what structure name we're looking for.
163696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  unsigned IDNS = Decl::IDNS_Tag;
163796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName SearchName = Name;
163896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!SearchName && D->getTypedefForAnonDecl()) {
163996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
164096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    IDNS = Decl::IDNS_Ordinary;
164196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  } else if (Importer.getToContext().getLangOptions().CPlusPlus)
164296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    IDNS |= Decl::IDNS_Ordinary;
164396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
164496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // We may already have a record of the same name; try to find and match it.
1645e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor  RecordDecl *AdoptDecl = 0;
164696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!DC->isFunctionOrMethod() && SearchName) {
164796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
164896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
164996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         Lookup.first != Lookup.second;
165096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         ++Lookup.first) {
165196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
165296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor        continue;
165396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
165496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Decl *Found = *Lookup.first;
165596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
165696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor        if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
165796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor          Found = Tag->getDecl();
165896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
165996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
166096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
1661e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
1662e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
1663e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // The record types structurally match, or the "from" translation
1664e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // unit only had a forward declaration anyway; call it the same
1665e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // function.
1666e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // FIXME: For C++, we should also merge methods here.
16675ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor            return Importer.Imported(D, FoundDef);
1668e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          }
1669e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        } else {
1670e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          // We have a forward declaration of this type, so adopt that forward
1671e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          // declaration rather than building a new one.
1672e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          AdoptDecl = FoundRecord;
1673e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          continue;
1674e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        }
167596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
167696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
167796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
167896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
167996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
168096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    if (!ConflictingDecls.empty()) {
168196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
168296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         ConflictingDecls.data(),
168396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         ConflictingDecls.size());
168496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
168596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
168696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
168796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Create the record declaration.
168873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  RecordDecl *D2 = AdoptDecl;
168973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!D2) {
169073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D)) {
169173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
1692e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   D->getTagKind(),
1693e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   DC, Loc,
1694e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   Name.getAsIdentifierInfo(),
169596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                        Importer.Import(D->getTagKeywordLoc()));
169673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      D2 = D2CXX;
1697325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor      D2->setAccess(D->getAccess());
1698e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor
1699e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor      if (D->isDefinition()) {
1700e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        // Add base classes.
1701e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1702e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        for (CXXRecordDecl::base_class_iterator
170373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                  Base1 = D1CXX->bases_begin(),
170473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor               FromBaseEnd = D1CXX->bases_end();
170573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor             Base1 != FromBaseEnd;
170673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor             ++Base1) {
170773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          QualType T = Importer.Import(Base1->getType());
1708e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          if (T.isNull())
1709e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            return 0;
1710e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor
1711e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          Bases.push_back(
1712e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            new (Importer.getToContext())
171373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                  CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
171473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Base1->isVirtual(),
171573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Base1->isBaseOfClass(),
171673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Base1->getAccessSpecifierAsWritten(),
171796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                   T));
1718e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        }
1719e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        if (!Bases.empty())
172073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          D2CXX->setBases(Bases.data(), Bases.size());
172196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
1722e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor    } else {
172373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
1724e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                    DC, Loc,
1725e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                    Name.getAsIdentifierInfo(),
1726e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                    Importer.Import(D->getTagKeywordLoc()));
172796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
1728b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Import the qualifier, if any.
1729b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (D->getQualifier()) {
1730b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1731b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1732b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      D2->setQualifierInfo(NNS, NNSRange);
1733b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
173473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->setLexicalDeclContext(LexicalDC);
173573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    LexicalDC->addDecl(D2);
173696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
17375ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
173873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Importer.Imported(D, D2);
1739e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor
174096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (D->isDefinition()) {
174173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->startDefinition();
1742083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    ImportDeclContext(D);
174373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->completeDefinition();
174496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
174596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
174673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return D2;
174796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
174896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
174936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorDecl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
175036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the major distinguishing characteristics of this enumerator.
175136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclContext *DC, *LexicalDC;
175236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName Name;
175336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  SourceLocation Loc;
1754ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
175536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
1756ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
1757ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
1758ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
1759ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
1760ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
176136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Determine whether there are any other declarations with the same name and
176236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // in the same context.
176336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!LexicalDC->isFunctionOrMethod()) {
176436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
176536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
176636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
176736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         Lookup.first != Lookup.second;
176836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++Lookup.first) {
176936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
177036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        continue;
177136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
177236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
177336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
177436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
177536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (!ConflictingDecls.empty()) {
177636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
177736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.data(),
177836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.size());
177936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!Name)
178036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        return 0;
178136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
178236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
178336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
178436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  Expr *Init = Importer.Import(D->getInitExpr());
178536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (D->getInitExpr() && !Init)
178636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
178736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
178836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  EnumConstantDecl *ToEnumerator
178936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
179036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                               Name.getAsIdentifierInfo(), T,
179136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                               Init, D->getInitVal());
1792325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToEnumerator->setAccess(D->getAccess());
179336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  ToEnumerator->setLexicalDeclContext(LexicalDC);
17945ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToEnumerator);
179536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  LexicalDC->addDecl(ToEnumerator);
179636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  return ToEnumerator;
179736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
179896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
1799a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
1800a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the major distinguishing characteristics of this function.
1801a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC, *LexicalDC;
1802a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclarationName Name;
1803a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc;
1804ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1805089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    return 0;
1806089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
1807a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Try to find a function in our own ("to") context with the same name, same
1808a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // type, and in the same context as the function we're importing.
1809a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  if (!LexicalDC->isFunctionOrMethod()) {
1810a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1811a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
1812a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1813a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor         Lookup.first != Lookup.second;
1814a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor         ++Lookup.first) {
1815a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1816a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        continue;
1817a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1818a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
1819a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        if (isExternalLinkage(FoundFunction->getLinkage()) &&
1820a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            isExternalLinkage(D->getLinkage())) {
1821ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          if (Importer.IsStructurallyEquivalent(D->getType(),
1822ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                FoundFunction->getType())) {
1823a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            // FIXME: Actually try to merge the body and other attributes.
18245ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor            return Importer.Imported(D, FoundFunction);
1825a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          }
1826a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1827a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // FIXME: Check for overloading more carefully, e.g., by boosting
1828a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Sema::IsOverload out to the AST library.
1829a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1830a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Function overloading is okay in C++.
1831a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          if (Importer.getToContext().getLangOptions().CPlusPlus)
1832a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            continue;
1833a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1834a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Complain about inconsistent function types.
1835a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
1836ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            << Name << D->getType() << FoundFunction->getType();
1837a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          Importer.ToDiag(FoundFunction->getLocation(),
1838a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                          diag::note_odr_value_here)
1839a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            << FoundFunction->getType();
1840a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        }
1841a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      }
1842a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1843a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
1844a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    }
1845a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1846a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    if (!ConflictingDecls.empty()) {
1847a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
1848a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                         ConflictingDecls.data(),
1849a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                         ConflictingDecls.size());
1850a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (!Name)
1851a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        return 0;
1852a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    }
1853a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
1854ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
1855ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
1856ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
1857ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
1858ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
1859a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1860a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the function parameters.
1861a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 8> Parameters;
1862a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
1863a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor       P != PEnd; ++P) {
1864a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
1865a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    if (!ToP)
1866a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      return 0;
1867a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1868a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Parameters.push_back(ToP);
1869a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
1870a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1871a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Create the imported function.
1872a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1873c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  FunctionDecl *ToFunction = 0;
1874c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
1875c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
1876c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            cast<CXXRecordDecl>(DC),
1877c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            Loc, Name, T, TInfo,
1878c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            FromConstructor->isExplicit(),
1879c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            D->isInlineSpecified(),
1880c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            D->isImplicit());
1881c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else if (isa<CXXDestructorDecl>(D)) {
1882c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
1883c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           cast<CXXRecordDecl>(DC),
1884c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           Loc, Name, T,
1885c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isInlineSpecified(),
1886c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isImplicit());
1887c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else if (CXXConversionDecl *FromConversion
1888c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           = dyn_cast<CXXConversionDecl>(D)) {
1889c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
1890c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           cast<CXXRecordDecl>(DC),
1891c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           Loc, Name, T, TInfo,
1892c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isInlineSpecified(),
1893c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           FromConversion->isExplicit());
1894c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else {
1895c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, Loc,
1896c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                      Name, T, TInfo, D->getStorageClass(),
189716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                      D->getStorageClassAsWritten(),
1898c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                      D->isInlineSpecified(),
1899c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                      D->hasWrittenPrototype());
1900c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  }
1901b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1902b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
1903b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (D->getQualifier()) {
1904b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1905b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1906b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    ToFunction->setQualifierInfo(NNS, NNSRange);
1907b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1908325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToFunction->setAccess(D->getAccess());
1909c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToFunction->setLexicalDeclContext(LexicalDC);
1910c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Importer.Imported(D, ToFunction);
1911c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  LexicalDC->addDecl(ToFunction);
1912a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1913a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Set the parameters.
1914a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
1915c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Parameters[I]->setOwningFunction(ToFunction);
1916c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToFunction->addDecl(Parameters[I]);
1917a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
1918c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToFunction->setParams(Parameters.data(), Parameters.size());
1919a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1920a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // FIXME: Other bits to merge?
1921a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1922c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToFunction;
1923a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1924a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1925c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
1926c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitFunctionDecl(D);
1927c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
1928c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
1929c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1930c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
1931c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
1932c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
1933c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
1934c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
1935c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
1936c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
1937c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
1938c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
1939c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
1940c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
194196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas GregorDecl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
194296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Import the major distinguishing characteristics of a variable.
194396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclContext *DC, *LexicalDC;
194496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName Name;
194596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  SourceLocation Loc;
1946ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1947ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
1948ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
1949ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
1950ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
1951ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
195296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
195396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
195496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
195596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  Expr *BitWidth = Importer.Import(D->getBitWidth());
195696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!BitWidth && D->getBitWidth())
195796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
195896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
195996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
196096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         Loc, Name.getAsIdentifierInfo(),
196196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         T, TInfo, BitWidth, D->isMutable());
1962325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToField->setAccess(D->getAccess());
196396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  ToField->setLexicalDeclContext(LexicalDC);
19645ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToField);
196596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  LexicalDC->addDecl(ToField);
196696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  return ToField;
196796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
196896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
19692e55e3af2f6d6c0509495357fade95105dd144cdDouglas GregorDecl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
19702e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Import the major distinguishing characteristics of an ivar.
19712e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  DeclContext *DC, *LexicalDC;
19722e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  DeclarationName Name;
19732e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  SourceLocation Loc;
19742e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
19752e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
19762e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
19772e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Determine whether we've already imported this ivar
19782e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
19792e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor       Lookup.first != Lookup.second;
19802e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor       ++Lookup.first) {
19812e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
19822e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (Importer.IsStructurallyEquivalent(D->getType(),
19832e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                            FoundIvar->getType())) {
19842e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.Imported(D, FoundIvar);
19852e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        return FoundIvar;
19862e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      }
19872e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
19882e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
19892e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << Name << D->getType() << FoundIvar->getType();
19902e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
19912e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << FoundIvar->getType();
19922e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      return 0;
19932e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    }
19942e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  }
19952e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
19962e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Import the type.
19972e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  QualType T = Importer.Import(D->getType());
19982e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (T.isNull())
19992e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
20002e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
20012e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
20022e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  Expr *BitWidth = Importer.Import(D->getBitWidth());
20032e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (!BitWidth && D->getBitWidth())
20042e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
20052e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
2006a06549226f45d5b72169a3d054415616dd1014a2Daniel Dunbar  ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2007a06549226f45d5b72169a3d054415616dd1014a2Daniel Dunbar                                              cast<ObjCContainerDecl>(DC),
20082e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              Loc, Name.getAsIdentifierInfo(),
20092e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              T, TInfo, D->getAccessControl(),
20102e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              BitWidth);
20112e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  ToIvar->setLexicalDeclContext(LexicalDC);
20122e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  Importer.Imported(D, ToIvar);
20132e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  LexicalDC->addDecl(ToIvar);
20142e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  return ToIvar;
20152e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
20162e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor}
20172e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
2018a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2019a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the major distinguishing characteristics of a variable.
2020a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC, *LexicalDC;
2021a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclarationName Name;
2022a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc;
2023ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2024a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return 0;
2025089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2026089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Try to find a variable in our own ("to") context with the same name and
2027089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // in the same context as the variable we're importing.
20289bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (D->isFileVarDecl()) {
2029089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    VarDecl *MergeWithVar = 0;
2030089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2031089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
20329bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2033089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor         Lookup.first != Lookup.second;
2034089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor         ++Lookup.first) {
2035089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2036089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        continue;
2037089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2038089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2039089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        // We have found a variable that we may need to merge with. Check it.
2040089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        if (isExternalLinkage(FoundVar->getLinkage()) &&
2041089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            isExternalLinkage(D->getLinkage())) {
2042ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          if (Importer.IsStructurallyEquivalent(D->getType(),
2043ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                FoundVar->getType())) {
2044089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            MergeWithVar = FoundVar;
2045089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            break;
2046089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          }
2047089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2048d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          const ArrayType *FoundArray
2049d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            = Importer.getToContext().getAsArrayType(FoundVar->getType());
2050d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          const ArrayType *TArray
2051ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            = Importer.getToContext().getAsArrayType(D->getType());
2052d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          if (FoundArray && TArray) {
2053d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            if (isa<IncompleteArrayType>(FoundArray) &&
2054d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor                isa<ConstantArrayType>(TArray)) {
2055ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              // Import the type.
2056ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              QualType T = Importer.Import(D->getType());
2057ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              if (T.isNull())
2058ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                return 0;
2059ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2060d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              FoundVar->setType(T);
2061d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              MergeWithVar = FoundVar;
2062d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              break;
2063d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            } else if (isa<IncompleteArrayType>(TArray) &&
2064d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor                       isa<ConstantArrayType>(FoundArray)) {
2065d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              MergeWithVar = FoundVar;
2066d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              break;
20670f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor            }
20680f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor          }
20690f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor
2070089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
2071ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            << Name << D->getType() << FoundVar->getType();
2072089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2073089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            << FoundVar->getType();
2074089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        }
2075089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      }
2076089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2077089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
2078089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2079089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2080089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    if (MergeWithVar) {
2081089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      // An equivalent variable with external linkage has been found. Link
2082089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      // the two declarations, then merge them.
20835ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor      Importer.Imported(D, MergeWithVar);
2084089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2085089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (VarDecl *DDef = D->getDefinition()) {
2086089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2087089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(ExistingDef->getLocation(),
2088089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                          diag::err_odr_variable_multiple_def)
2089089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            << Name;
2090089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2091089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        } else {
2092089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Expr *Init = Importer.Import(DDef->getInit());
2093838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor          MergeWithVar->setInit(Init);
2094089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        }
2095089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      }
2096089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2097089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      return MergeWithVar;
2098089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2099089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2100089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    if (!ConflictingDecls.empty()) {
2101089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
2102089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                         ConflictingDecls.data(),
2103089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                         ConflictingDecls.size());
2104089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (!Name)
2105089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        return 0;
2106089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2107089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  }
210882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2109ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
2110ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2111ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2112ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2113ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2114089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Create the imported variable.
211582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2116089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2117089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                   Name.getAsIdentifierInfo(), T, TInfo,
211816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClass(),
211916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten());
2120b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
2121b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (D->getQualifier()) {
2122b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2123b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2124b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    ToVar->setQualifierInfo(NNS, NNSRange);
2125b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2126325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToVar->setAccess(D->getAccess());
21279bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ToVar->setLexicalDeclContext(LexicalDC);
21285ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToVar);
21299bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  LexicalDC->addDecl(ToVar);
21309bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
2131089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Merge the initializer.
2132089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // FIXME: Can we really import any initializer? Alternatively, we could force
2133089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // ourselves to import every declaration of a variable and then only use
2134089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // getInit() here.
2135838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
2136089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2137089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // FIXME: Other bits to merge?
2138089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2139089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  return ToVar;
2140089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
2141089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
21422cd00932b9b5403047139ce8cfaa3ae47966f894Douglas GregorDecl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
21432cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Parameters are created in the translation unit's context, then moved
21442cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // into the function declaration's context afterward.
21452cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
21462cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
21472cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the name of this declaration.
21482cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
21492cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  if (D->getDeclName() && !Name)
21502cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    return 0;
21512cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
21522cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the location of this declaration.
21532cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
21542cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
21552cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the parameter's type.
21562cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  QualType T = Importer.Import(D->getType());
21572cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  if (T.isNull())
21582cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    return 0;
21592cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
21602cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Create the imported parameter.
21612cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  ImplicitParamDecl *ToParm
21622cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    = ImplicitParamDecl::Create(Importer.getToContext(), DC,
21632cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor                                Loc, Name.getAsIdentifierInfo(),
21642cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor                                T);
21652cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  return Importer.Imported(D, ToParm);
21662cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor}
21672cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
2168a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2169a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Parameters are created in the translation unit's context, then moved
2170a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // into the function declaration's context afterward.
2171a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2172a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
217382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // Import the name of this declaration.
217482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
217582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (D->getDeclName() && !Name)
217682fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
217782fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2178a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the location of this declaration.
2179a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
2180a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2181a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the parameter's type.
2182a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  QualType T = Importer.Import(D->getType());
218382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (T.isNull())
218482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
218582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2186a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Create the imported parameter.
2187a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2188a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2189a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            Loc, Name.getAsIdentifierInfo(),
2190a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            T, TInfo, D->getStorageClass(),
219116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                             D->getStorageClassAsWritten(),
2192a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            /*FIXME: Default argument*/ 0);
2193bf73b352acb7a2d041ce8b50171dd7f8e2b2c1bbJohn McCall  ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
21945ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  return Importer.Imported(D, ToParm);
2195a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
219682fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2197c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas GregorDecl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2198c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the major distinguishing characteristics of a method.
2199c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  DeclContext *DC, *LexicalDC;
2200c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  DeclarationName Name;
2201c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  SourceLocation Loc;
2202c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2203c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return 0;
2204c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2205c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2206c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       Lookup.first != Lookup.second;
2207c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       ++Lookup.first) {
2208c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2209c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2210c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        continue;
2211c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2212c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check return types.
2213c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2214c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                             FoundMethod->getResultType())) {
2215c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2216c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name
2217c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->getResultType() << FoundMethod->getResultType();
2218c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2219c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2220c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2221c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2222c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2223c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2224c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check the number of parameters.
2225c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (D->param_size() != FoundMethod->param_size()) {
2226c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2227c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name
2228c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->param_size() << FoundMethod->param_size();
2229c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2230c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2231c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2232c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2233c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2234c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2235c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check parameter types.
2236c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2237c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor             PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2238c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor           P != PEnd; ++P, ++FoundP) {
2239c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2240c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                               (*FoundP)->getType())) {
2241c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          Importer.FromDiag((*P)->getLocation(),
2242c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                            diag::err_odr_objc_method_param_type_inconsistent)
2243c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << D->isInstanceMethod() << Name
2244c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << (*P)->getType() << (*FoundP)->getType();
2245c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2246c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << (*FoundP)->getType();
2247c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          return 0;
2248c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        }
2249c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2250c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2251c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check variadic/non-variadic.
2252c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check the number of parameters.
2253c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (D->isVariadic() != FoundMethod->isVariadic()) {
2254c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2255c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2256c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2257c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2258c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2259c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2260c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2261c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2262c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // FIXME: Any other bits we need to merge?
2263c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      return Importer.Imported(D, FoundMethod);
2264c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    }
2265c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2266c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2267c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the result type.
2268c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  QualType ResultTy = Importer.Import(D->getResultType());
2269c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (ResultTy.isNull())
2270c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return 0;
2271c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
22724bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor  TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
22734bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor
2274c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ObjCMethodDecl *ToMethod
2275c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    = ObjCMethodDecl::Create(Importer.getToContext(),
2276c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Loc,
2277c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Importer.Import(D->getLocEnd()),
2278c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Name.getObjCSelector(),
22794bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor                             ResultTy, ResultTInfo, DC,
2280c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isInstanceMethod(),
2281c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isVariadic(),
2282c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isSynthesized(),
2283c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->getImplementationControl());
2284c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2285c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // FIXME: When we decide to merge method definitions, we'll need to
2286c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // deal with implicit parameters.
2287c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2288c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the parameters
2289c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2290c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2291c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                   FromPEnd = D->param_end();
2292c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       FromP != FromPEnd;
2293c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       ++FromP) {
2294c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2295c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    if (!ToP)
2296c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      return 0;
2297c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2298c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToParams.push_back(ToP);
2299c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2300c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2301c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Set the parameters.
2302c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2303c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToParams[I]->setOwningFunction(ToMethod);
2304c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToMethod->addDecl(ToParams[I]);
2305c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2306c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToMethod->setMethodParams(Importer.getToContext(),
23074ecb25fa94897b2c03510292acace710e5262ba5Fariborz Jahanian                            ToParams.data(), ToParams.size(),
23084ecb25fa94897b2c03510292acace710e5262ba5Fariborz Jahanian                            ToParams.size());
2309c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2310c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToMethod->setLexicalDeclContext(LexicalDC);
2311c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Importer.Imported(D, ToMethod);
2312c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  LexicalDC->addDecl(ToMethod);
2313c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToMethod;
2314c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor}
2315c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2316b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas GregorDecl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2317b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import the major distinguishing characteristics of a category.
2318b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  DeclContext *DC, *LexicalDC;
2319b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  DeclarationName Name;
2320b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  SourceLocation Loc;
2321b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2322b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    return 0;
2323b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2324b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCInterfaceDecl *ToInterface
2325b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2326b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (!ToInterface)
2327b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    return 0;
2328b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2329b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Determine if we've already encountered this category.
2330b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCCategoryDecl *MergeWithCategory
2331b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2332b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCCategoryDecl *ToCategory = MergeWithCategory;
2333b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (!ToCategory) {
2334b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2335b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Importer.Import(D->getAtLoc()),
2336b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Loc,
2337b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                       Importer.Import(D->getCategoryNameLoc()),
2338b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Name.getAsIdentifierInfo());
2339b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setLexicalDeclContext(LexicalDC);
2340b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    LexicalDC->addDecl(ToCategory);
2341b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Imported(D, ToCategory);
2342b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2343b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // Link this category into its class's category list.
2344b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setClassInterface(ToInterface);
2345b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->insertNextClassCategory();
2346b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2347b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // Import protocols
2348b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2349b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2350b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2351b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      = D->protocol_loc_begin();
2352b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2353b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          FromProtoEnd = D->protocol_end();
2354b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor         FromProto != FromProtoEnd;
2355b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor         ++FromProto, ++FromProtoLoc) {
2356b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      ObjCProtocolDecl *ToProto
2357b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2358b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      if (!ToProto)
2359b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor        return 0;
2360b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      Protocols.push_back(ToProto);
2361b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2362b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    }
2363b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2364b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
2365b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2366b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                ProtocolLocs.data(), Importer.getToContext());
2367b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2368b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  } else {
2369b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Imported(D, ToCategory);
2370b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  }
2371b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2372b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import all of the members of this category.
2373083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
2374b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2375b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // If we have an implementation, import it as well.
2376b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (D->getImplementation()) {
2377b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ObjCCategoryImplDecl *Impl
2378b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation()));
2379b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    if (!Impl)
2380b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      return 0;
2381b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2382b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setImplementation(Impl);
2383b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  }
2384b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2385b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  return ToCategory;
2386b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor}
2387b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
23882e2a400383c7a64a927f61eeed596b08928e1d4bDouglas GregorDecl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
2389b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import the major distinguishing characteristics of a protocol.
23902e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  DeclContext *DC, *LexicalDC;
23912e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  DeclarationName Name;
23922e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  SourceLocation Loc;
23932e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
23942e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    return 0;
23952e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
23962e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  ObjCProtocolDecl *MergeWithProtocol = 0;
23972e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
23982e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       Lookup.first != Lookup.second;
23992e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++Lookup.first) {
24002e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
24012e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      continue;
24022e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
24032e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
24042e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      break;
24052e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  }
24062e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
24072e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  ObjCProtocolDecl *ToProto = MergeWithProtocol;
24082e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  if (!ToProto || ToProto->isForwardDecl()) {
24092e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if (!ToProto) {
24102e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
24112e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                                         Name.getAsIdentifierInfo());
24122e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto->setForwardDecl(D->isForwardDecl());
24132e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto->setLexicalDeclContext(LexicalDC);
24142e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      LexicalDC->addDecl(ToProto);
24152e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    }
24162e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Imported(D, ToProto);
24172e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
24182e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    // Import protocols
24192e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
24202e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
24212e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    ObjCProtocolDecl::protocol_loc_iterator
24222e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      FromProtoLoc = D->protocol_loc_begin();
24232e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
24242e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                                          FromProtoEnd = D->protocol_end();
24252e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       FromProto != FromProtoEnd;
24262e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++FromProto, ++FromProtoLoc) {
24272e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ObjCProtocolDecl *ToProto
24282e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
24292e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      if (!ToProto)
24302e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor        return 0;
24312e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      Protocols.push_back(ToProto);
24322e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
24332e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    }
24342e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
24352e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
24362e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    ToProto->setProtocolList(Protocols.data(), Protocols.size(),
24372e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                             ProtocolLocs.data(), Importer.getToContext());
24382e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  } else {
24392e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Imported(D, ToProto);
24402e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  }
24412e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
2442b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import all of the members of this protocol.
2443083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
24442e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
24452e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  return ToProto;
24462e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor}
24472e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
2448a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas GregorDecl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2449a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // Import the major distinguishing characteristics of an @interface.
2450a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  DeclContext *DC, *LexicalDC;
2451a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  DeclarationName Name;
2452a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  SourceLocation Loc;
2453a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2454a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    return 0;
2455a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2456a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  ObjCInterfaceDecl *MergeWithIface = 0;
2457a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2458a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       Lookup.first != Lookup.second;
2459a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++Lookup.first) {
2460a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2461a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      continue;
2462a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2463a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2464a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      break;
2465a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
2466a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2467a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  ObjCInterfaceDecl *ToIface = MergeWithIface;
2468a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (!ToIface || ToIface->isForwardDecl()) {
2469a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!ToIface) {
2470a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2471a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          DC, Loc,
2472a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          Name.getAsIdentifierInfo(),
2473a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          Importer.Import(D->getClassLoc()),
2474a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          D->isForwardDecl(),
2475a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          D->isImplicitInterfaceDecl());
24762e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToIface->setForwardDecl(D->isForwardDecl());
2477a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setLexicalDeclContext(LexicalDC);
2478a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      LexicalDC->addDecl(ToIface);
2479a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
2480a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Imported(D, ToIface);
2481a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2482a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (D->getSuperClass()) {
2483a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ObjCInterfaceDecl *Super
2484a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2485a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      if (!Super)
2486a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        return 0;
2487a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2488a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setSuperClass(Super);
2489a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2490a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
2491a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2492a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // Import protocols
2493a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2494a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2495a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ObjCInterfaceDecl::protocol_loc_iterator
2496a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      FromProtoLoc = D->protocol_loc_begin();
2497a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2498a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                           FromProtoEnd = D->protocol_end();
2499a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       FromProto != FromProtoEnd;
2500a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++FromProto, ++FromProtoLoc) {
2501a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ObjCProtocolDecl *ToProto
2502a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2503a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      if (!ToProto)
2504a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        return 0;
2505a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      Protocols.push_back(ToProto);
2506a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2507a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
2508a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2509a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
2510a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2511a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                             ProtocolLocs.data(), Importer.getToContext());
2512a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2513a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // Import @end range
2514a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2515a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  } else {
2516a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Imported(D, ToIface);
25172e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
25182e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    // Check for consistency of superclasses.
25192e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    DeclarationName FromSuperName, ToSuperName;
25202e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (D->getSuperClass())
25212e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
25222e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (ToIface->getSuperClass())
25232e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      ToSuperName = ToIface->getSuperClass()->getDeclName();
25242e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (FromSuperName != ToSuperName) {
25252e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(ToIface->getLocation(),
25262e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                      diag::err_odr_objc_superclass_inconsistent)
25272e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << ToIface->getDeclName();
25282e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (ToIface->getSuperClass())
25292e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.ToDiag(ToIface->getSuperClassLoc(),
25302e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                        diag::note_odr_objc_superclass)
25312e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor          << ToIface->getSuperClass()->getDeclName();
25322e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      else
25332e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.ToDiag(ToIface->getLocation(),
25342e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                        diag::note_odr_objc_missing_superclass);
25352e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (D->getSuperClass())
25362e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.FromDiag(D->getSuperClassLoc(),
25372e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                          diag::note_odr_objc_superclass)
25382e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor          << D->getSuperClass()->getDeclName();
25392e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      else
25402e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.FromDiag(D->getLocation(),
25412e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                          diag::note_odr_objc_missing_superclass);
25422e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      return 0;
25432e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    }
2544a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
2545a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2546b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import categories. When the categories themselves are imported, they'll
2547b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // hook themselves into this interface.
2548b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
2549b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor       FromCat = FromCat->getNextClassCategory())
2550b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Import(FromCat);
2551b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2552a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // Import all of the members of this class.
2553083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
2554a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2555a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // If we have an @implementation, import it as well.
2556a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (D->getImplementation()) {
2557a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ObjCImplementationDecl *Impl
2558a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      = cast<ObjCImplementationDecl>(Importer.Import(D->getImplementation()));
2559a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!Impl)
2560a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      return 0;
2561a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2562a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setImplementation(Impl);
2563a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
2564a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
25652e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  return ToIface;
2566a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor}
2567a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2568e3261624c1870e52d7efc2ac83e647713361ac6cDouglas GregorDecl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
2569e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Import the major distinguishing characteristics of an @property.
2570e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  DeclContext *DC, *LexicalDC;
2571e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  DeclarationName Name;
2572e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  SourceLocation Loc;
2573e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2574e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    return 0;
2575e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2576e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Check whether we have already imported this property.
2577e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2578e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       Lookup.first != Lookup.second;
2579e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       ++Lookup.first) {
2580e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    if (ObjCPropertyDecl *FoundProp
2581e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                                = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
2582e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // Check property types.
2583e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      if (!Importer.IsStructurallyEquivalent(D->getType(),
2584e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                                             FoundProp->getType())) {
2585e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
2586e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor          << Name << D->getType() << FoundProp->getType();
2587e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
2588e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor          << FoundProp->getType();
2589e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        return 0;
2590e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      }
2591e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2592e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // FIXME: Check property attributes, getters, setters, etc.?
2593e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2594e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // Consider these properties to be equivalent.
2595e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      Importer.Imported(D, FoundProp);
2596e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      return FoundProp;
2597e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    }
2598e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  }
2599e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2600e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Import the type.
2601e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  QualType T = Importer.Import(D->getType());
2602e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  if (T.isNull())
2603e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    return 0;
2604e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2605e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Create the new property.
2606e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ObjCPropertyDecl *ToProperty
2607e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
2608e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               Name.getAsIdentifierInfo(),
2609e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               Importer.Import(D->getAtLoc()),
2610e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               T,
2611e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               D->getPropertyImplementation());
2612e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  Importer.Imported(D, ToProperty);
2613e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setLexicalDeclContext(LexicalDC);
2614e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  LexicalDC->addDecl(ToProperty);
2615e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
2616e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setPropertyAttributes(D->getPropertyAttributes());
2617e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setGetterName(Importer.Import(D->getGetterName()));
2618e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setSetterName(Importer.Import(D->getSetterName()));
2619e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setGetterMethodDecl(
2620e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
2621e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setSetterMethodDecl(
2622e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
2623e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setPropertyIvarDecl(
2624e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
2625e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  return ToProperty;
2626e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor}
2627e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
26282b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas GregorDecl *
26292b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas GregorASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
26302b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  // Import the context of this declaration.
26312b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
26322b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  if (!DC)
26332b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    return 0;
26342b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
26352b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  DeclContext *LexicalDC = DC;
26362b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
26372b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
26382b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    if (!LexicalDC)
26392b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      return 0;
26402b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  }
26412b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
26422b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  // Import the location of this declaration.
26432b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
26442b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
26452b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
26462b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  llvm::SmallVector<SourceLocation, 4> Locations;
26472b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
26482b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    = D->protocol_loc_begin();
26492b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  for (ObjCForwardProtocolDecl::protocol_iterator FromProto
26502b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor         = D->protocol_begin(), FromProtoEnd = D->protocol_end();
26512b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor       FromProto != FromProtoEnd;
26522b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor       ++FromProto, ++FromProtoLoc) {
26532b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    ObjCProtocolDecl *ToProto
26542b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
26552b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    if (!ToProto)
26562b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      continue;
26572b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
26582b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Protocols.push_back(ToProto);
26592b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Locations.push_back(Importer.Import(*FromProtoLoc));
26602b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  }
26612b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
26622b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ObjCForwardProtocolDecl *ToForward
26632b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
26642b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor                                      Protocols.data(), Protocols.size(),
26652b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor                                      Locations.data());
26662b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ToForward->setLexicalDeclContext(LexicalDC);
26672b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  LexicalDC->addDecl(ToForward);
26682b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  Importer.Imported(D, ToForward);
26692b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  return ToForward;
26702b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor}
26712b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
2672a2bc15b7463a9f85a5bff1531d833c278426a733Douglas GregorDecl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
2673a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  // Import the context of this declaration.
2674a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2675a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  if (!DC)
2676a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    return 0;
2677a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2678a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  DeclContext *LexicalDC = DC;
2679a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
2680a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2681a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    if (!LexicalDC)
2682a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      return 0;
2683a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  }
2684a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2685a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  // Import the location of this declaration.
2686a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
2687a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2688a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
2689a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  llvm::SmallVector<SourceLocation, 4> Locations;
2690a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
2691a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor       From != FromEnd; ++From) {
2692a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    ObjCInterfaceDecl *ToIface
2693a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
2694a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    if (!ToIface)
2695a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      continue;
2696a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2697a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Interfaces.push_back(ToIface);
2698a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Locations.push_back(Importer.Import(From->getLocation()));
2699a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  }
2700a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
2701a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
2702a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Loc,
2703a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Interfaces.data(),
2704a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Locations.data(),
2705a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Interfaces.size());
2706a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  ToClass->setLexicalDeclContext(LexicalDC);
2707a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  LexicalDC->addDecl(ToClass);
2708a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  Importer.Imported(D, ToClass);
2709a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  return ToClass;
2710a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor}
2711a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
27124800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
27134800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor// Import Statements
27144800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
27154800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
27164800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorStmt *ASTNodeImporter::VisitStmt(Stmt *S) {
27174800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
27184800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    << S->getStmtClassName();
27194800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return 0;
27204800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
27214800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
27224800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
27234800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor// Import Expressions
27244800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
27254800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorExpr *ASTNodeImporter::VisitExpr(Expr *E) {
27264800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
27274800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    << E->getStmtClassName();
27284800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return 0;
27294800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
27304800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
2731440806306674e23ad74726208cbdc6f37849dd9dDouglas GregorExpr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
2732440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  NestedNameSpecifier *Qualifier = 0;
2733440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (E->getQualifier()) {
2734440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    Qualifier = Importer.Import(E->getQualifier());
2735440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    if (!E->getQualifier())
2736440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor      return 0;
2737440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  }
2738440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
2739440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
2740440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (!ToD)
2741440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    return 0;
2742440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
2743440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  QualType T = Importer.Import(E->getType());
2744440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (T.isNull())
2745440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    return 0;
2746440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
2747440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
2748440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             Importer.Import(E->getQualifierRange()),
2749440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             ToD,
2750440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             Importer.Import(E->getLocation()),
2751440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             T,
2752440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             /*FIXME:TemplateArgs=*/0);
2753440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor}
2754440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
27554800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorExpr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
27564800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  QualType T = Importer.Import(E->getType());
27574800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (T.isNull())
27584800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return 0;
27594800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
27604800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return new (Importer.getToContext())
27614800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    IntegerLiteral(E->getValue(), T, Importer.Import(E->getLocation()));
27624800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
27634800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
2764b2e400aae8c62c4e1616016f40618baace0da065Douglas GregorExpr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
2765b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  QualType T = Importer.Import(E->getType());
2766b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  if (T.isNull())
2767b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor    return 0;
2768b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor
2769b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
2770b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor                                                        E->isWide(), T,
2771b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor                                          Importer.Import(E->getLocation()));
2772b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor}
2773b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor
2774f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
2775f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
2776f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!SubExpr)
2777f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2778f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2779f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext())
2780f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                  ParenExpr(Importer.Import(E->getLParen()),
2781f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                            Importer.Import(E->getRParen()),
2782f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                            SubExpr);
2783f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
2784f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2785f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
2786f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
2787f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
2788f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2789f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2790f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
2791f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!SubExpr)
2792f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2793f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2794f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
2795f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                                     T,
2796f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                         Importer.Import(E->getOperatorLoc()));
2797f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
2798f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2799bd249a542878a626192746c1e0c0b21f164e6df7Douglas GregorExpr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
2800bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  QualType ResultType = Importer.Import(E->getType());
2801bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
2802bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  if (E->isArgumentType()) {
2803bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
2804bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    if (!TInfo)
2805bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor      return 0;
2806bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
2807bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
2808bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                                           TInfo, ResultType,
2809bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                           Importer.Import(E->getOperatorLoc()),
2810bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                           Importer.Import(E->getRParenLoc()));
2811bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  }
2812bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
2813bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  Expr *SubExpr = Importer.Import(E->getArgumentExpr());
2814bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  if (!SubExpr)
2815bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    return 0;
2816bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
2817bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
2818bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                                         SubExpr, ResultType,
2819bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                          Importer.Import(E->getOperatorLoc()),
2820bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                          Importer.Import(E->getRParenLoc()));
2821bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor}
2822bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
2823f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
2824f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
2825f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
2826f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2827f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2828f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *LHS = Importer.Import(E->getLHS());
2829f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!LHS)
2830f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2831f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2832f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *RHS = Importer.Import(E->getRHS());
2833f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!RHS)
2834f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2835f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2836f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
2837f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                                      T,
2838f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                          Importer.Import(E->getOperatorLoc()));
2839f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
2840f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2841f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
2842f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
2843f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
2844f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2845f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2846f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType CompLHSType = Importer.Import(E->getComputationLHSType());
2847f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (CompLHSType.isNull())
2848f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2849f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2850f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType CompResultType = Importer.Import(E->getComputationResultType());
2851f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (CompResultType.isNull())
2852f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2853f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2854f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *LHS = Importer.Import(E->getLHS());
2855f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!LHS)
2856f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2857f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2858f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *RHS = Importer.Import(E->getRHS());
2859f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!RHS)
2860f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
2861f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
2862f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext())
2863f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                        CompoundAssignOperator(LHS, RHS, E->getOpcode(),
2864f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                               T, CompLHSType, CompResultType,
2865f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                          Importer.Import(E->getOperatorLoc()));
2866f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
2867f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
286836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorExpr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
286936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  QualType T = Importer.Import(E->getType());
287036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (T.isNull())
287136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
287236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
287336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
287436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!SubExpr)
287536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
287636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
2877f1b48b7014992155286d58bb1676f9f51031d18bAnders Carlsson  // FIXME: Initialize the base path.
287841b2dcd465f1e438502c420effc9d0c747f9db8fAnders Carlsson  assert(E->getBasePath().empty() && "FIXME: Must copy base path!");
2879f1b48b7014992155286d58bb1676f9f51031d18bAnders Carlsson  CXXBaseSpecifierArray BasePath;
288036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  return new (Importer.getToContext()) ImplicitCastExpr(T, E->getCastKind(),
2881f1b48b7014992155286d58bb1676f9f51031d18bAnders Carlsson                                                        SubExpr, BasePath,
288236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                                        E->isLvalueCast());
288336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
288436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
2885008847a70ab122a99911149199855060fb3753b4Douglas GregorExpr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
2886008847a70ab122a99911149199855060fb3753b4Douglas Gregor  QualType T = Importer.Import(E->getType());
2887008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (T.isNull())
2888008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
2889008847a70ab122a99911149199855060fb3753b4Douglas Gregor
2890008847a70ab122a99911149199855060fb3753b4Douglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
2891008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (!SubExpr)
2892008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
2893008847a70ab122a99911149199855060fb3753b4Douglas Gregor
2894008847a70ab122a99911149199855060fb3753b4Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
2895008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (!TInfo && E->getTypeInfoAsWritten())
2896008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
2897008847a70ab122a99911149199855060fb3753b4Douglas Gregor
289841b2dcd465f1e438502c420effc9d0c747f9db8fAnders Carlsson  // FIXME: Initialize the base path.
289941b2dcd465f1e438502c420effc9d0c747f9db8fAnders Carlsson  assert(E->getBasePath().empty() && "FIXME: Must copy base path!");
290041b2dcd465f1e438502c420effc9d0c747f9db8fAnders Carlsson  CXXBaseSpecifierArray BasePath;
2901008847a70ab122a99911149199855060fb3753b4Douglas Gregor  return new (Importer.getToContext()) CStyleCastExpr(T, E->getCastKind(),
290241b2dcd465f1e438502c420effc9d0c747f9db8fAnders Carlsson                                                      SubExpr, BasePath, TInfo,
2903008847a70ab122a99911149199855060fb3753b4Douglas Gregor                                            Importer.Import(E->getLParenLoc()),
2904008847a70ab122a99911149199855060fb3753b4Douglas Gregor                                            Importer.Import(E->getRParenLoc()));
2905008847a70ab122a99911149199855060fb3753b4Douglas Gregor}
2906008847a70ab122a99911149199855060fb3753b4Douglas Gregor
29074800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorASTImporter::ASTImporter(Diagnostic &Diags,
29084800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                         ASTContext &ToContext, FileManager &ToFileManager,
29094800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                         ASTContext &FromContext, FileManager &FromFileManager)
29101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  : ToContext(ToContext), FromContext(FromContext),
2911885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToFileManager(ToFileManager), FromFileManager(FromFileManager),
29124800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Diags(Diags) {
29139bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ImportedDecls[FromContext.getTranslationUnitDecl()]
29149bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    = ToContext.getTranslationUnitDecl();
29159bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
29169bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
29179bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorASTImporter::~ASTImporter() { }
29181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
29191b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTImporter::Import(QualType FromT) {
29201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (FromT.isNull())
29211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
29221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
2923169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Check whether we've already imported this type.
2924169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  llvm::DenseMap<Type *, Type *>::iterator Pos
2925169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor    = ImportedTypes.find(FromT.getTypePtr());
2926169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  if (Pos != ImportedTypes.end())
2927169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor    return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
29281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
2929169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Import the type
29301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  ASTNodeImporter Importer(*this);
29311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToT = Importer.Visit(FromT.getTypePtr());
29321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToT.isNull())
29331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToT;
29341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
2935169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Record the imported type.
2936169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
2937169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor
29381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
29391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
29401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
29419bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorTypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
294282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (!FromTSI)
294382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return FromTSI;
294482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
294582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // FIXME: For now we just create a "trivial" type source info based
294682fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // on the type and a seingle location. Implement a real version of
294782fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // this.
294882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  QualType T = Import(FromTSI->getType());
294982fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (T.isNull())
295082fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
295182fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
295282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  return ToContext.getTrivialTypeSourceInfo(T,
2953bd054dba8a3023821f2a0951b0fae05e3522a7c9Abramo Bagnara                        FromTSI->getTypeLoc().getSourceRange().getBegin());
29549bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
29559bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
29569bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorDecl *ASTImporter::Import(Decl *FromD) {
29579bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromD)
29589bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
29599bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
29609bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Check whether we've already imported this declaration.
29619bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
29629bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (Pos != ImportedDecls.end())
29639bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return Pos->second;
29649bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
29659bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Import the type
29669bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ASTNodeImporter Importer(*this);
29679bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  Decl *ToD = Importer.Visit(FromD);
29689bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!ToD)
29699bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
29709bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
29719bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Record the imported declaration.
29729bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ImportedDecls[FromD] = ToD;
2973ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2974ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
2975ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // Keep track of anonymous tags that have an associated typedef.
2976ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (FromTag->getTypedefForAnonDecl())
2977ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      AnonTagsWithPendingTypedefs.push_back(FromTag);
2978ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
2979ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // When we've finished transforming a typedef, see whether it was the
2980ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // typedef for an anonymous tag.
2981ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    for (llvm::SmallVector<TagDecl *, 4>::iterator
2982ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor               FromTag = AnonTagsWithPendingTypedefs.begin(),
2983ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            FromTagEnd = AnonTagsWithPendingTypedefs.end();
2984ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor         FromTag != FromTagEnd; ++FromTag) {
2985ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
2986ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
2987ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          // We found the typedef for an anonymous tag; link them.
2988ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
2989ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          AnonTagsWithPendingTypedefs.erase(FromTag);
2990ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          break;
2991ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        }
2992ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      }
2993ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    }
2994ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  }
2995ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
29969bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return ToD;
29979bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
29989bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
29999bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorDeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
30009bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromDC)
30019bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return FromDC;
30029bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30039bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
30049bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30059bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30069bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorExpr *ASTImporter::Import(Expr *FromE) {
30079bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromE)
30089bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
30099bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30109bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
30119bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30129bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30139bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorStmt *ASTImporter::Import(Stmt *FromS) {
30149bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromS)
30159bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
30169bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30174800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Check whether we've already imported this declaration.
30184800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
30194800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (Pos != ImportedStmts.end())
30204800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return Pos->second;
30214800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
30224800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Import the type
30234800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  ASTNodeImporter Importer(*this);
30244800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Stmt *ToS = Importer.Visit(FromS);
30254800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (!ToS)
30264800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return 0;
30274800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
30284800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Record the imported declaration.
30294800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  ImportedStmts[FromS] = ToS;
30304800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return ToS;
30319bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30329bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30339bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorNestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
30349bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromNNS)
30359bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
30369bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30379bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // FIXME: Implement!
30389bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return 0;
30399bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30409bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30419bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorSourceLocation ASTImporter::Import(SourceLocation FromLoc) {
30429bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (FromLoc.isInvalid())
30439bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return SourceLocation();
30449bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
3045885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &FromSM = FromContext.getSourceManager();
3046885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3047885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // For now, map everything down to its spelling location, so that we
3048885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // don't have to import macro instantiations.
3049885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // FIXME: Import macro instantiations!
3050885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  FromLoc = FromSM.getSpellingLoc(FromLoc);
3051885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
3052885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &ToSM = ToContext.getSourceManager();
3053885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  return ToSM.getLocForStartOfFile(Import(Decomposed.first))
3054885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor             .getFileLocWithOffset(Decomposed.second);
30559bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30569bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
30579bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorSourceRange ASTImporter::Import(SourceRange FromRange) {
30589bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
30599bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
30609bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
3061885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas GregorFileID ASTImporter::Import(FileID FromID) {
3062885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  llvm::DenseMap<unsigned, FileID>::iterator Pos
3063885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    = ImportedFileIDs.find(FromID.getHashValue());
3064885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  if (Pos != ImportedFileIDs.end())
3065885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    return Pos->second;
3066885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3067885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &FromSM = FromContext.getSourceManager();
3068885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &ToSM = ToContext.getSourceManager();
3069885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
3070885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
3071885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3072885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // Include location of this file.
3073885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
3074885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3075885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // Map the FileID for to the "to" source manager.
3076885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  FileID ToID;
3077885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
3078885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  if (Cache->Entry) {
3079885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We probably want to use getVirtualFile(), so we don't hit the
3080885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // disk again
3081885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
3082885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // than mmap the files several times.
3083885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
3084885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToID = ToSM.createFileID(Entry, ToIncludeLoc,
3085885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                             FromSLoc.getFile().getFileCharacteristic());
3086885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  } else {
3087885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We want to re-use the existing MemoryBuffer!
3088e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner    const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(getDiags(), FromSM);
3089885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    llvm::MemoryBuffer *ToBuf
3090a0a270c0f1c0a4e3482438bdc5f4a7bd3d25f0a6Chris Lattner      = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
3091885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                                             FromBuf->getBufferIdentifier());
3092885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToID = ToSM.createFileIDForMemBuffer(ToBuf);
3093885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  }
3094885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3095885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3096885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  ImportedFileIDs[FromID.getHashValue()] = ToID;
3097885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  return ToID;
3098885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor}
3099885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
31001b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorDeclarationName ASTImporter::Import(DeclarationName FromName) {
31011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!FromName)
31021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return DeclarationName();
31031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  switch (FromName.getNameKind()) {
31051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::Identifier:
31061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Import(FromName.getAsIdentifierInfo());
31071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCZeroArgSelector:
31091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCOneArgSelector:
31101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCMultiArgSelector:
31111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Import(FromName.getObjCSelector());
31121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXConstructorName: {
31141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
31151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
31161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
31171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXConstructorName(
31191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
31201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
31211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXDestructorName: {
31231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
31241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
31251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
31261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXDestructorName(
31281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
31291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
31301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXConversionFunctionName: {
31321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
31331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
31341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
31351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXConversionFunctionName(
31371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
31381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
31391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXOperatorName:
31411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXOperatorName(
31421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                          FromName.getCXXOverloadedOperator());
31431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXLiteralOperatorName:
31451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXLiteralOperatorName(
31461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                   Import(FromName.getCXXLiteralIdentifier()));
31471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXUsingDirective:
31491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: STATICS!
31501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return DeclarationName::getUsingDirectiveName();
31511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
31521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Silence bogus GCC warning
31541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return DeclarationName();
31551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
31561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31571b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorIdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) {
31581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!FromId)
31591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return 0;
31601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
31611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return &ToContext.Idents.get(FromId->getName());
31621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
3163089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
3164c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas GregorSelector ASTImporter::Import(Selector FromSel) {
3165c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (FromSel.isNull())
3166c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return Selector();
3167c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
3168c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  llvm::SmallVector<IdentifierInfo *, 4> Idents;
3169c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
3170c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
3171c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
3172c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
3173c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor}
3174c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
3175089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
3176089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                DeclContext *DC,
3177089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                unsigned IDNS,
3178089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                NamedDecl **Decls,
3179089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                unsigned NumDecls) {
3180089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  return Name;
3181089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
3182089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
3183089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
31844800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return Diags.Report(FullSourceLoc(Loc, ToContext.getSourceManager()),
31854800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                      DiagID);
3186089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
3187089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
3188089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
31894800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return Diags.Report(FullSourceLoc(Loc, FromContext.getSourceManager()),
31904800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                      DiagID);
3191089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
31925ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
31935ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas GregorDecl *ASTImporter::Imported(Decl *From, Decl *To) {
31945ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  ImportedDecls[From] = To;
31955ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  return To;
3196af667588d53de22795c5304f1496ccaac2a71402Daniel Dunbar}
3197ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
3198ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregorbool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
3199ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  llvm::DenseMap<Type *, Type *>::iterator Pos
3200ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor   = ImportedTypes.find(From.getTypePtr());
3201ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
3202ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return true;
3203ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
3204bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(FromContext, ToContext, Diags,
3205ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   NonEquivalentDecls);
3206bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(From, To);
3207ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor}
3208