ASTImporter.cpp revision 0bc15d92bf98cd01e7904d7fca9895dacc237618
11b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===//
21b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//
31b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//                     The LLVM Compiler Infrastructure
41b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//
51b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor// This file is distributed under the University of Illinois Open Source
61b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor// License. See LICENSE.TXT for details.
71b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//
81b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//===----------------------------------------------------------------------===//
91b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//
101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//  This file defines the ASTImporter class which imports AST nodes from one
111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//  context into another context.
121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//
131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//===----------------------------------------------------------------------===//
141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor#include "clang/AST/ASTImporter.h"
151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor#include "clang/AST/ASTContext.h"
17885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor#include "clang/AST/ASTDiagnostic.h"
1896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor#include "clang/AST/DeclCXX.h"
191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor#include "clang/AST/DeclObjC.h"
20089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor#include "clang/AST/DeclVisitor.h"
214800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor#include "clang/AST/StmtVisitor.h"
221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor#include "clang/AST/TypeVisitor.h"
23885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor#include "clang/Basic/FileManager.h"
24885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor#include "clang/Basic/SourceManager.h"
25885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor#include "llvm/Support/MemoryBuffer.h"
2673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor#include <deque>
271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregorusing namespace clang;
291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregornamespace {
31089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
324800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                          public DeclVisitor<ASTNodeImporter, Decl *>,
334800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor                          public StmtVisitor<ASTNodeImporter, Stmt *> {
341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ASTImporter &Importer;
351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  public:
371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    using TypeVisitor<ASTNodeImporter, QualType>::Visit;
409bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
414800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // Importing types
4489cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    QualType VisitType(Type *T);
451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitBuiltinType(BuiltinType *T);
461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitComplexType(ComplexType *T);
471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitPointerType(PointerType *T);
481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitBlockPointerType(BlockPointerType *T);
491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitLValueReferenceType(LValueReferenceType *T);
501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitRValueReferenceType(RValueReferenceType *T);
511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitMemberPointerType(MemberPointerType *T);
521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitConstantArrayType(ConstantArrayType *T);
531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitIncompleteArrayType(IncompleteArrayType *T);
541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitVariableArrayType(VariableArrayType *T);
551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentSizedArrayType
561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentSizedExtVectorType
571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitVectorType(VectorType *T);
581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitExtVectorType(ExtVectorType *T);
591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitFunctionNoProtoType(FunctionNoProtoType *T);
601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitFunctionProtoType(FunctionProtoType *T);
611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: UnresolvedUsingType
621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitTypedefType(TypedefType *T);
631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitTypeOfExprType(TypeOfExprType *T);
641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentTypeOfExprType
651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitTypeOfType(TypeOfType *T);
661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitDecltypeType(DecltypeType *T);
671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentDecltypeType
681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitRecordType(RecordType *T);
691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitEnumType(EnumType *T);
701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: TemplateTypeParmType
711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: SubstTemplateTypeParmType
72d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualType VisitTemplateSpecializationType(TemplateSpecializationType *T);
73465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    QualType VisitElaboratedType(ElaboratedType *T);
744714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    // FIXME: DependentNameType
7533500955d731c73717af52088b7fc0e7a85681e7John McCall    // FIXME: DependentTemplateSpecializationType
761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitObjCInterfaceType(ObjCInterfaceType *T);
77c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    QualType VisitObjCObjectType(ObjCObjectType *T);
781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T);
79089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
80089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    // Importing declarations
81a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
82a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                         DeclContext *&LexicalDC, DeclarationName &Name,
83788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                         SourceLocation &Loc);
842577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
852577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                  DeclarationNameInfo& To);
86083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    void ImportDeclContext(DeclContext *FromDC);
87d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    bool ImportDefinition(RecordDecl *From, RecordDecl *To);
88040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    TemplateParameterList *ImportTemplateParameterList(
89040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                 TemplateParameterList *Params);
90d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
91d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    bool ImportTemplateArguments(const TemplateArgument *FromArgs,
92d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                 unsigned NumFromArgs,
93d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                               llvm::SmallVectorImpl<TemplateArgument> &ToArgs);
9496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
9573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
96040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
9789cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    Decl *VisitDecl(Decl *D);
98788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    Decl *VisitNamespaceDecl(NamespaceDecl *D);
999e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
10036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
10196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *VisitRecordDecl(RecordDecl *D);
10236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
103a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D);
104c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
105c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
106c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
107c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
10896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
10987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
1102e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
111089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
1122cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
113a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Decl *VisitParmVarDecl(ParmVarDecl *D);
114c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
115b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
1162e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
117a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
1183daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
119dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
120e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
121954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
1222b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
123a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Decl *VisitObjCClassDecl(ObjCClassDecl *D);
124040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
125040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
126040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
127040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
128d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    Decl *VisitClassTemplateSpecializationDecl(
129d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                            ClassTemplateSpecializationDecl *D);
130a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
1314800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    // Importing statements
1324800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Stmt *VisitStmt(Stmt *S);
1334800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
1344800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    // Importing expressions
1354800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Expr *VisitExpr(Expr *E);
136440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    Expr *VisitDeclRefExpr(DeclRefExpr *E);
1374800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Expr *VisitIntegerLiteral(IntegerLiteral *E);
138b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor    Expr *VisitCharacterLiteral(CharacterLiteral *E);
139f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitParenExpr(ParenExpr *E);
140f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitUnaryOperator(UnaryOperator *E);
141bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    Expr *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
142f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitBinaryOperator(BinaryOperator *E);
143f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
14436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
145008847a70ab122a99911149199855060fb3753b4Douglas Gregor    Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
1461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  };
1471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
1481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//----------------------------------------------------------------------------
15073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor// Structural Equivalence
15173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor//----------------------------------------------------------------------------
15273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
15373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregornamespace {
15473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  struct StructuralEquivalenceContext {
15573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief AST contexts for which we are checking structural equivalence.
15673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    ASTContext &C1, &C2;
15773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
15873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief The set of "tentative" equivalences between two canonical
15973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// declarations, mapping from a declaration in the first context to the
16073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// declaration in the second context that we believe to be equivalent.
16173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
16273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
16373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Queue of declarations in the first context whose equivalence
16473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// with a declaration in the second context still needs to be verified.
16573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    std::deque<Decl *> DeclsToCheck;
16673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
167ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    /// \brief Declaration (from, to) pairs that are known not to be equivalent
168ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    /// (which we have already complained about).
169ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
170ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
17173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Whether we're being strict about the spelling of types when
17273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// unifying two types.
17373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool StrictTypeSpelling;
17473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
17573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
176ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor               llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
17773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                 bool StrictTypeSpelling = false)
17833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis      : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
179ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        StrictTypeSpelling(StrictTypeSpelling) { }
18073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
18173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Determine whether the two declarations are structurally
18273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// equivalent.
18373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
18473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
18573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Determine whether the two types are structurally equivalent.
18673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructurallyEquivalent(QualType T1, QualType T2);
18773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
18873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  private:
18973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Finish checking all of the structural equivalences.
19073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    ///
19173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \returns true if an error occurred, false otherwise.
19273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool Finish();
19373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
19473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  public:
19573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
19633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis      return C1.getDiagnostics().Report(Loc, DiagID);
19773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
19873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
19973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
20033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis      return C2.getDiagnostics().Report(Loc, DiagID);
20173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
20273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  };
20373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
20473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
20573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
20673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     QualType T1, QualType T2);
20773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
20873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Decl *D1, Decl *D2);
20973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
21073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine if two APInts have the same value, after zero-extending
21173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// one of them (if needed!) to ensure that the bit-widths match.
21273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
21373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() == I2.getBitWidth())
21473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == I2;
21573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
21673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() > I2.getBitWidth())
2179f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    return I1 == I2.zext(I1.getBitWidth());
21873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
2199f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad  return I1.zext(I2.getBitWidth()) == I2;
22073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
22173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
22273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine if two APSInts have the same value, zero- or sign-extending
22373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// as needed.
22473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
22573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
22673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == I2;
22773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
22873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Check for a bit-width mismatch.
22973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() > I2.getBitWidth())
2309f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    return IsSameValue(I1, I2.extend(I1.getBitWidth()));
23173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  else if (I2.getBitWidth() > I1.getBitWidth())
2329f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    return IsSameValue(I1.extend(I2.getBitWidth()), I2);
23373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
23473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // We have a signedness mismatch. Turn the signed value into an unsigned
23573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // value.
23673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.isSigned()) {
23773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (I1.isNegative())
23873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
23973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return llvm::APSInt(I1, true) == I2;
24173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
24273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I2.isNegative())
24473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
24573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return I1 == llvm::APSInt(I2, true);
24773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
24873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two expressions.
25073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
25173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Expr *E1, Expr *E2) {
25273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!E1 || !E2)
25373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return E1 == E2;
25473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
25573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Actually perform a structural comparison!
25673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
25773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
25873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
25973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two identifiers are equivalent.
26073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
26173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const IdentifierInfo *Name2) {
26273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!Name1 || !Name2)
26373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return Name1 == Name2;
26473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
26573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return Name1->getName() == Name2->getName();
26673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
26773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
26873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two nested-name-specifiers are equivalent.
26973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
27073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     NestedNameSpecifier *NNS1,
27173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     NestedNameSpecifier *NNS2) {
27273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Implement!
27373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
27473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
27573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
27673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two template arguments are equivalent.
27773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
27873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const TemplateArgument &Arg1,
27973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const TemplateArgument &Arg2) {
280d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (Arg1.getKind() != Arg2.getKind())
281d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return false;
282d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
283d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  switch (Arg1.getKind()) {
284d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Null:
285d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return true;
286d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
287d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Type:
288d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
289d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
290d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Integral:
291d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
292d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                          Arg2.getIntegralType()))
293d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return false;
294d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
295d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
296d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
297d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Declaration:
298d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
299d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
300d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Template:
301d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return IsStructurallyEquivalent(Context,
302d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg1.getAsTemplate(),
303d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg2.getAsTemplate());
304a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
305a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  case TemplateArgument::TemplateExpansion:
306a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    return IsStructurallyEquivalent(Context,
307a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                                    Arg1.getAsTemplateOrTemplatePattern(),
308a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                                    Arg2.getAsTemplateOrTemplatePattern());
309a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
310d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Expression:
311d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return IsStructurallyEquivalent(Context,
312d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg1.getAsExpr(), Arg2.getAsExpr());
313d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
314d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Pack:
315d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Arg1.pack_size() != Arg2.pack_size())
316d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return false;
317d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
318d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
319d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (!IsStructurallyEquivalent(Context,
320d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg1.pack_begin()[I],
321d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg2.pack_begin()[I]))
322d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return false;
323d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
324d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return true;
325d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
326d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
327d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm_unreachable("Invalid template argument kind");
32873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
32973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
33073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
33173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence for the common part of array
33273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// types.
33373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
33473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                          const ArrayType *Array1,
33573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                          const ArrayType *Array2) {
33673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!IsStructurallyEquivalent(Context,
33773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                Array1->getElementType(),
33873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                Array2->getElementType()))
33973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
34073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Array1->getSizeModifier() != Array2->getSizeModifier())
34173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
34273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
34373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
34473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
34573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
34673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
34773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
34873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two types.
34973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
35073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     QualType T1, QualType T2) {
35173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (T1.isNull() || T2.isNull())
35273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return T1.isNull() && T2.isNull();
35373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
35473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!Context.StrictTypeSpelling) {
35573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // We aren't being strict about token-to-token equivalence of types,
35673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // so map down to the canonical type.
35773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    T1 = Context.C1.getCanonicalType(T1);
35873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    T2 = Context.C2.getCanonicalType(T2);
35973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
36073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
36173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (T1.getQualifiers() != T2.getQualifiers())
36273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
36373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
364ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  Type::TypeClass TC = T1->getTypeClass();
365ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
366ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T1->getTypeClass() != T2->getTypeClass()) {
367ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // Compare function types with prototypes vs. without prototypes as if
368ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // both did not have prototypes.
369ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (T1->getTypeClass() == Type::FunctionProto &&
370ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        T2->getTypeClass() == Type::FunctionNoProto)
371ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      TC = Type::FunctionNoProto;
372ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    else if (T1->getTypeClass() == Type::FunctionNoProto &&
373ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor             T2->getTypeClass() == Type::FunctionProto)
374ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      TC = Type::FunctionNoProto;
375ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    else
376ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      return false;
377ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  }
37873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
379ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  switch (TC) {
380ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  case Type::Builtin:
38173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Deal with Char_S/Char_U.
38273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
38373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
38473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
38573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
38673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Complex:
38773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
38873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<ComplexType>(T1)->getElementType(),
38973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<ComplexType>(T2)->getElementType()))
39073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
39173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
39273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
39373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Pointer:
39473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
39573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<PointerType>(T1)->getPointeeType(),
39673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<PointerType>(T2)->getPointeeType()))
39773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
39873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
39973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
40073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::BlockPointer:
40173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
40273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<BlockPointerType>(T1)->getPointeeType(),
40373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<BlockPointerType>(T2)->getPointeeType()))
40473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
40573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
40673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
40773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::LValueReference:
40873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::RValueReference: {
40973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ReferenceType *Ref1 = cast<ReferenceType>(T1);
41073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ReferenceType *Ref2 = cast<ReferenceType>(T2);
41173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
41273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
41373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Ref1->isInnerRef() != Ref2->isInnerRef())
41473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
41573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
41673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ref1->getPointeeTypeAsWritten(),
41773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ref2->getPointeeTypeAsWritten()))
41873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
41973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
42073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
42173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
42273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::MemberPointer: {
42373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
42473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
42573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
42673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  MemPtr1->getPointeeType(),
42773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  MemPtr2->getPointeeType()))
42873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
42973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
43073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(MemPtr1->getClass(), 0),
43173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(MemPtr2->getClass(), 0)))
43273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
43373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
43473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
43573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
43673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ConstantArray: {
43773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
43873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
43973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsSameValue(Array1->getSize(), Array2->getSize()))
44073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
44173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
44273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
44373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
44473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
44573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
44673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
44773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::IncompleteArray:
44873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context,
44973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                       cast<ArrayType>(T1),
45073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                       cast<ArrayType>(T2)))
45173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
45273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
45373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
45473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::VariableArray: {
45573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
45673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
45773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
45873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Array1->getSizeExpr(), Array2->getSizeExpr()))
45973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
46173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
46273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
46473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
46573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
46673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
46773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::DependentSizedArray: {
46873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
46973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
47073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
47173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Array1->getSizeExpr(), Array2->getSizeExpr()))
47273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
47373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
47473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
47573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
47673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
47773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
47873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
47973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
48073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::DependentSizedExtVector: {
48173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedExtVectorType *Vec1
48273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<DependentSizedExtVectorType>(T1);
48373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedExtVectorType *Vec2
48473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<DependentSizedExtVectorType>(T2);
48573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
48673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getSizeExpr(), Vec2->getSizeExpr()))
48773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
48873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
48973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getElementType(),
49073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec2->getElementType()))
49173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
49273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
49373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
49473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
49573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Vector:
49673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ExtVector: {
49773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VectorType *Vec1 = cast<VectorType>(T1);
49873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VectorType *Vec2 = cast<VectorType>(T2);
49973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
50073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getElementType(),
50173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec2->getElementType()))
50273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
50373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Vec1->getNumElements() != Vec2->getNumElements())
50473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
505e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    if (Vec1->getVectorKind() != Vec2->getVectorKind())
50673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
5070e12b44081c6395a6d60a05a85a6012f7bb23b16Douglas Gregor    break;
50873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
50973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
51073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::FunctionProto: {
51173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
51273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
51373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getNumArgs() != Proto2->getNumArgs())
51473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
51573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
51673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
51773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto1->getArgType(I),
51873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto2->getArgType(I)))
51973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
52073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
52173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->isVariadic() != Proto2->isVariadic())
52273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
52373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
52473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
52573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
52673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
52773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
52873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
52973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
53073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
53173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto1->getExceptionType(I),
53273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto2->getExceptionType(I)))
53373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
53473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
53573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
53673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
53773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
53873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Fall through to check the bits common with FunctionNoProtoType.
53973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
54073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
54173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::FunctionNoProto: {
54273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionType *Function1 = cast<FunctionType>(T1);
54373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionType *Function2 = cast<FunctionType>(T2);
54473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
54573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Function1->getResultType(),
54673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Function2->getResultType()))
54773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
548264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      if (Function1->getExtInfo() != Function2->getExtInfo())
549264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola        return false;
55073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
55173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
55273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
55373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::UnresolvedUsing:
55473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
55573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<UnresolvedUsingType>(T1)->getDecl(),
55673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<UnresolvedUsingType>(T2)->getDecl()))
55773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
55873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
55973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
5609d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
5619d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  case Type::Attributed:
5629d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    if (!IsStructurallyEquivalent(Context,
5639d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                  cast<AttributedType>(T1)->getModifiedType(),
5649d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                  cast<AttributedType>(T2)->getModifiedType()))
5659d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall      return false;
5669d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    if (!IsStructurallyEquivalent(Context,
5679d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                cast<AttributedType>(T1)->getEquivalentType(),
5689d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                cast<AttributedType>(T2)->getEquivalentType()))
5699d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall      return false;
5709d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    break;
57173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
572075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  case Type::Paren:
573075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    if (!IsStructurallyEquivalent(Context,
574075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara                                  cast<ParenType>(T1)->getInnerType(),
575075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara                                  cast<ParenType>(T2)->getInnerType()))
576075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      return false;
577075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    break;
578075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
57973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Typedef:
58073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
58173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypedefType>(T1)->getDecl(),
58273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypedefType>(T2)->getDecl()))
58373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
58473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
58573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
58673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TypeOfExpr:
58773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
58873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
58973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
59073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
59173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
59273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
59373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TypeOf:
59473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
59573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypeOfType>(T1)->getUnderlyingType(),
59673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypeOfType>(T2)->getUnderlyingType()))
59773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
59873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
59973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
60073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Decltype:
60173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
60273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<DecltypeType>(T1)->getUnderlyingExpr(),
60373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<DecltypeType>(T2)->getUnderlyingExpr()))
60473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
60573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
60673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
60773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Record:
60873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Enum:
60973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
61073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TagType>(T1)->getDecl(),
61173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TagType>(T2)->getDecl()))
61273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
61373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
614465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
61573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TemplateTypeParm: {
61673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
61773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
61873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->getDepth() != Parm2->getDepth())
61973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
62073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->getIndex() != Parm2->getIndex())
62173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
62273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->isParameterPack() != Parm2->isParameterPack())
62373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
62473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
62573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Names of template type parameters are never significant.
62673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
62773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
62873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
62973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::SubstTemplateTypeParm: {
63073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const SubstTemplateTypeParmType *Subst1
63173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<SubstTemplateTypeParmType>(T1);
63273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const SubstTemplateTypeParmType *Subst2
63373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<SubstTemplateTypeParmType>(T2);
63473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
63573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Subst1->getReplacedParameter(), 0),
63673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Subst2->getReplacedParameter(), 0)))
63773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
63873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
63973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Subst1->getReplacementType(),
64073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Subst2->getReplacementType()))
64173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
64273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
64373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
64473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
6450bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  case Type::SubstTemplateTypeParmPack: {
6460bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    const SubstTemplateTypeParmPackType *Subst1
6470bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor      = cast<SubstTemplateTypeParmPackType>(T1);
6480bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    const SubstTemplateTypeParmPackType *Subst2
6490bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor      = cast<SubstTemplateTypeParmPackType>(T2);
6500bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    if (!IsStructurallyEquivalent(Context,
6510bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                                  QualType(Subst1->getReplacedParameter(), 0),
6520bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                                  QualType(Subst2->getReplacedParameter(), 0)))
6530bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor      return false;
6540bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    if (!IsStructurallyEquivalent(Context,
6550bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                                  Subst1->getArgumentPack(),
6560bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                                  Subst2->getArgumentPack()))
6570bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor      return false;
6580bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    break;
6590bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  }
66073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TemplateSpecialization: {
66173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateSpecializationType *Spec1
66273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<TemplateSpecializationType>(T1);
66373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateSpecializationType *Spec2
66473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<TemplateSpecializationType>(T2);
66573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
66673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Spec1->getTemplateName(),
66773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Spec2->getTemplateName()))
66873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
66973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Spec1->getNumArgs() != Spec2->getNumArgs())
67073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
67173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
67273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
67373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Spec1->getArg(I), Spec2->getArg(I)))
67473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
67573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
67673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
67773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
67873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
679465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Type::Elaborated: {
680465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
681465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
682465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    // CHECKME: what if a keyword is ETK_None or ETK_typename ?
683465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Elab1->getKeyword() != Elab2->getKeyword())
684465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return false;
68573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
686465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab1->getQualifier(),
687465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab2->getQualifier()))
68873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
68973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
690465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab1->getNamedType(),
691465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab2->getNamedType()))
69273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
69373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
69473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
69573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
6963cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  case Type::InjectedClassName: {
6973cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
6983cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
6993cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (!IsStructurallyEquivalent(Context,
70031f17ecbef57b5679c017c375db330546b7b5145John McCall                                  Inj1->getInjectedSpecializationType(),
70131f17ecbef57b5679c017c375db330546b7b5145John McCall                                  Inj2->getInjectedSpecializationType()))
7023cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      return false;
7033cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    break;
7043cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
7053cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
7064714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  case Type::DependentName: {
7074714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    const DependentNameType *Typename1 = cast<DependentNameType>(T1);
7084714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    const DependentNameType *Typename2 = cast<DependentNameType>(T2);
70973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
71073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename1->getQualifier(),
71173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename2->getQualifier()))
71273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
71373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
71473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename2->getIdentifier()))
71573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
71673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
71773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
71873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
71973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
72033500955d731c73717af52088b7fc0e7a85681e7John McCall  case Type::DependentTemplateSpecialization: {
72133500955d731c73717af52088b7fc0e7a85681e7John McCall    const DependentTemplateSpecializationType *Spec1 =
72233500955d731c73717af52088b7fc0e7a85681e7John McCall      cast<DependentTemplateSpecializationType>(T1);
72333500955d731c73717af52088b7fc0e7a85681e7John McCall    const DependentTemplateSpecializationType *Spec2 =
72433500955d731c73717af52088b7fc0e7a85681e7John McCall      cast<DependentTemplateSpecializationType>(T2);
72533500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!IsStructurallyEquivalent(Context,
72633500955d731c73717af52088b7fc0e7a85681e7John McCall                                  Spec1->getQualifier(),
72733500955d731c73717af52088b7fc0e7a85681e7John McCall                                  Spec2->getQualifier()))
72833500955d731c73717af52088b7fc0e7a85681e7John McCall      return false;
72933500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
73033500955d731c73717af52088b7fc0e7a85681e7John McCall                                  Spec2->getIdentifier()))
73133500955d731c73717af52088b7fc0e7a85681e7John McCall      return false;
73233500955d731c73717af52088b7fc0e7a85681e7John McCall    if (Spec1->getNumArgs() != Spec2->getNumArgs())
73333500955d731c73717af52088b7fc0e7a85681e7John McCall      return false;
73433500955d731c73717af52088b7fc0e7a85681e7John McCall    for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
73533500955d731c73717af52088b7fc0e7a85681e7John McCall      if (!IsStructurallyEquivalent(Context,
73633500955d731c73717af52088b7fc0e7a85681e7John McCall                                    Spec1->getArg(I), Spec2->getArg(I)))
73733500955d731c73717af52088b7fc0e7a85681e7John McCall        return false;
73833500955d731c73717af52088b7fc0e7a85681e7John McCall    }
73933500955d731c73717af52088b7fc0e7a85681e7John McCall    break;
74033500955d731c73717af52088b7fc0e7a85681e7John McCall  }
7417536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
7427536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  case Type::PackExpansion:
7437536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    if (!IsStructurallyEquivalent(Context,
7447536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor                                  cast<PackExpansionType>(T1)->getPattern(),
7457536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor                                  cast<PackExpansionType>(T2)->getPattern()))
7467536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor      return false;
7477536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    break;
7487536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
74973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ObjCInterface: {
75073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
75173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
75273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
75373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Iface1->getDecl(), Iface2->getDecl()))
75473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
755c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    break;
756c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  }
757c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
758c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject: {
759c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
760c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
761c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (!IsStructurallyEquivalent(Context,
762c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                  Obj1->getBaseType(),
763c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                  Obj2->getBaseType()))
764c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return false;
765c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
76673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
767c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
76873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
769c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Obj1->getProtocol(I),
770c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Obj2->getProtocol(I)))
77173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
77273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
77373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
77473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
77573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
77673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ObjCObjectPointer: {
77773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
77873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
77973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
78073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ptr1->getPointeeType(),
78173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ptr2->getPointeeType()))
78273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
78373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
78473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
78573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
78673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  } // end switch
78773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
78873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
78973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
79073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
79173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two records.
79273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
79373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     RecordDecl *D1, RecordDecl *D2) {
79473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (D1->isUnion() != D2->isUnion()) {
79573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
79673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
79773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
79873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << D1->getDeclName() << (unsigned)D1->getTagKind();
79973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
80073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
80173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
802d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // If both declarations are class template specializations, we know
803d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // the ODR applies, so check the template and template arguments.
804d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateSpecializationDecl *Spec1
805d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = dyn_cast<ClassTemplateSpecializationDecl>(D1);
806d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateSpecializationDecl *Spec2
807d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = dyn_cast<ClassTemplateSpecializationDecl>(D2);
808d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (Spec1 && Spec2) {
809d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Check that the specialized templates are the same.
810d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
811d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                  Spec2->getSpecializedTemplate()))
812d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return false;
813d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
814d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Check that the template arguments are the same.
815d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
816d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return false;
817d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
818d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
819d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (!IsStructurallyEquivalent(Context,
820d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Spec1->getTemplateArgs().get(I),
821d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Spec2->getTemplateArgs().get(I)))
822d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return false;
823d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
824d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // If one is a class template specialization and the other is not, these
825d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // structures are diferent.
826d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  else if (Spec1 || Spec2)
827d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return false;
828d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
829ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Compare the definitions of these two records. If either or both are
830ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // incomplete, we assume that they are equivalent.
831ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  D1 = D1->getDefinition();
832ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  D2 = D2->getDefinition();
833ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (!D1 || !D2)
834ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return true;
835ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
83673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
83773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
83873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
83973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
840040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          << Context.C2.getTypeDeclType(D2);
84173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
842040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          << D2CXX->getNumBases();
84373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
844040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          << D1CXX->getNumBases();
84573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
84673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
84773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
84873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      // Check the base classes.
84973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
85073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                           BaseEnd1 = D1CXX->bases_end(),
85173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                Base2 = D2CXX->bases_begin();
85273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor           Base1 != BaseEnd1;
85373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor           ++Base1, ++Base2) {
85473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!IsStructurallyEquivalent(Context,
85573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                      Base1->getType(), Base2->getType())) {
85673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
85773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Context.C2.getTypeDeclType(D2);
85873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
85973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->getType()
86073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->getSourceRange();
86173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
86273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getType()
86373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getSourceRange();
86473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          return false;
86573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        }
86673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
86773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check virtual vs. non-virtual inheritance mismatch.
86873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (Base1->isVirtual() != Base2->isVirtual()) {
86973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
87073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Context.C2.getTypeDeclType(D2);
87173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(Base2->getSourceRange().getBegin(),
87273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                        diag::note_odr_virtual_base)
87373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->isVirtual() << Base2->getSourceRange();
87473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
87573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->isVirtual()
87673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getSourceRange();
87773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          return false;
87873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        }
87973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
88073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    } else if (D1CXX->getNumBases() > 0) {
88173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
88273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
88373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
88473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
88573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Base1->getType()
88673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Base1->getSourceRange();
88773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
88873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
88973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
89073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
89173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
89273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Check the fields for consistency.
89373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
89473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                             Field2End = D2->field_end();
89573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
89673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Field1End = D1->field_end();
89773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       Field1 != Field1End;
89873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       ++Field1, ++Field2) {
89973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field2 == Field2End) {
90073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
90173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
90273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Field1->getLocation(), diag::note_odr_field)
90373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName() << Field1->getType();
90473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
90573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
90673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
90773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
90873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
90973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Field1->getType(), Field2->getType())) {
91073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
91173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
91273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(Field2->getLocation(), diag::note_odr_field)
91373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field2->getDeclName() << Field2->getType();
91473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Field1->getLocation(), diag::note_odr_field)
91573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName() << Field1->getType();
91673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
91773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
91873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
91973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field1->isBitField() != Field2->isBitField()) {
92073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
92173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
92273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (Field1->isBitField()) {
92373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        llvm::APSInt Bits;
92473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
92573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
92673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field1->getDeclName() << Field1->getType()
92773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits.toString(10, false);
92873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
92973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName();
93073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
93173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        llvm::APSInt Bits;
93273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
93373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
93473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName() << Field2->getType()
93573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits.toString(10, false);
93673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(),
93773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                          diag::note_odr_not_bit_field)
93873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName();
93973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
94073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
94173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
94273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
94373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field1->isBitField()) {
94473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      // Make sure that the bit-fields are the same length.
94573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      llvm::APSInt Bits1, Bits2;
94673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
94773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
94873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
94973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
95073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
95173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsSameValue(Bits1, Bits2)) {
95273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
95373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Context.C2.getTypeDeclType(D2);
95473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
95573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName() << Field2->getType()
95673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits2.toString(10, false);
95773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
95873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field1->getDeclName() << Field1->getType()
95973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits1.toString(10, false);
96073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
96173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
96273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
96373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
96473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
96573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Field2 != Field2End) {
96673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
96773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
96873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(Field2->getLocation(), diag::note_odr_field)
96973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Field2->getDeclName() << Field2->getType();
97073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
97173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
97273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
97373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
97473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
97573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
97673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
97773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two enums.
97873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
97973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     EnumDecl *D1, EnumDecl *D2) {
98073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
98173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                             EC2End = D2->enumerator_end();
98273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
98373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  EC1End = D1->enumerator_end();
98473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       EC1 != EC1End; ++EC1, ++EC2) {
98573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (EC2 == EC2End) {
98673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
98773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
98873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
98973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getDeclName()
99073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getInitVal().toString(10);
99173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
99273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
99373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
99473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
99573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::APSInt Val1 = EC1->getInitVal();
99673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::APSInt Val2 = EC2->getInitVal();
99773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsSameValue(Val1, Val2) ||
99873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
99973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
100073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
100173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
100273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC2->getDeclName()
100373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC2->getInitVal().toString(10);
100473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
100573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getDeclName()
100673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getInitVal().toString(10);
100773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
100873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
100973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
101073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
101173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (EC2 != EC2End) {
101273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
101373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
101473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
101573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << EC2->getDeclName()
101673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << EC2->getInitVal().toString(10);
101773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
101873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
101973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
102073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
102173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
102273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
1023040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1024040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1025040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateParameterList *Params1,
1026040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateParameterList *Params2) {
1027040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (Params1->size() != Params2->size()) {
1028040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(Params2->getTemplateLoc(),
1029040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                  diag::err_odr_different_num_template_parameters)
1030040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << Params1->size() << Params2->size();
1031040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(Params1->getTemplateLoc(),
1032040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                  diag::note_odr_template_parameter_list);
1033040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1034040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1035040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1036040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1037040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1038040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Context.Diag2(Params2->getParam(I)->getLocation(),
1039040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                    diag::err_odr_different_template_parameter_kind);
1040040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Context.Diag1(Params1->getParam(I)->getLocation(),
1041040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                    diag::note_odr_template_parameter_here);
1042040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return false;
1043040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
1044040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1045040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1046040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          Params2->getParam(I))) {
1047040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1048040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return false;
1049040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
1050040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1051040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1052040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return true;
1053040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1054040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1055040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1056040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTypeParmDecl *D1,
1057040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTypeParmDecl *D2) {
1058040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D1->isParameterPack() != D2->isParameterPack()) {
1059040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1060040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D2->isParameterPack();
1061040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1062040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D1->isParameterPack();
1063040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1064040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1065040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1066040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return true;
1067040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1068040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1069040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1070040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     NonTypeTemplateParmDecl *D1,
1071040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     NonTypeTemplateParmDecl *D2) {
1072040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Enable once we have variadic templates.
1073040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#if 0
1074040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D1->isParameterPack() != D2->isParameterPack()) {
1075040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1076040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D2->isParameterPack();
1077040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1078040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D1->isParameterPack();
1079040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1080040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1081040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#endif
1082040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1083040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check types.
1084040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1085040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(),
1086040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                  diag::err_odr_non_type_parameter_type_inconsistent)
1087040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D2->getType() << D1->getType();
1088040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1089040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D1->getType();
1090040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1091040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1092040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1093040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return true;
1094040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1095040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1096040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1097040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTemplateParmDecl *D1,
1098040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTemplateParmDecl *D2) {
1099040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Enable once we have variadic templates.
1100040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#if 0
1101040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D1->isParameterPack() != D2->isParameterPack()) {
1102040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1103040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    << D2->isParameterPack();
1104040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1105040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    << D1->isParameterPack();
1106040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1107040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1108040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#endif
1109040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1110040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check template parameter lists.
1111040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1112040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                  D2->getTemplateParameters());
1113040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1114040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1115040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1116040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     ClassTemplateDecl *D1,
1117040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     ClassTemplateDecl *D2) {
1118040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check template parameters.
1119040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!IsStructurallyEquivalent(Context,
1120040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                D1->getTemplateParameters(),
1121040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                D2->getTemplateParameters()))
1122040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
112373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
1124040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check the templated declaration.
1125040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1126040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          D2->getTemplatedDecl());
1127040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1128040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
112973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two declarations.
113073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
113173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Decl *D1, Decl *D2) {
113273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Check for known structural equivalences via a callback of some sort.
113373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
1134ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Check whether we already know that these two declarations are not
1135ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // structurally equivalent.
1136ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1137ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                      D2->getCanonicalDecl())))
1138ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return false;
1139ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
114073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Determine whether we've already produced a tentative equivalence for D1.
114173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
114273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (EquivToD1)
114373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return EquivToD1 == D2->getCanonicalDecl();
114473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
114573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Produce a tentative equivalence D1 <-> D2, which will be checked later.
114673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EquivToD1 = D2->getCanonicalDecl();
114773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
114873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
114973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
115073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
115173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
115273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                            Decl *D2) {
115373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!::IsStructurallyEquivalent(*this, D1, D2))
115473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
115573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
115673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return !Finish();
115773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
115873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
115973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
116073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                            QualType T2) {
116173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!::IsStructurallyEquivalent(*this, T1, T2))
116273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
116373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
116473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return !Finish();
116573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
116673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
116773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::Finish() {
116873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  while (!DeclsToCheck.empty()) {
116973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Check the next declaration.
117073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Decl *D1 = DeclsToCheck.front();
117173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DeclsToCheck.pop_front();
117273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
117373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Decl *D2 = TentativeEquivalences[D1];
117473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    assert(D2 && "Unrecorded tentative equivalence?");
117573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
1176ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    bool Equivalent = true;
1177ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
117873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Switch on all declaration kinds. For now, we're just going to
117973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // check the obvious ones.
118073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
118173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
118273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check for equivalent structure names.
118373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name1 = Record1->getIdentifier();
118473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name1 && Record1->getTypedefForAnonDecl())
118573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
118673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name2 = Record2->getIdentifier();
118773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name2 && Record2->getTypedefForAnonDecl())
118873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
1189ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (!::IsStructurallyEquivalent(Name1, Name2) ||
1190ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this, Record1, Record2))
1191ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
119273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
119373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Record/non-record mismatch.
1194ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
119573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
1196ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
119773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
119873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check for equivalent enum names.
119973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name1 = Enum1->getIdentifier();
120073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name1 && Enum1->getTypedefForAnonDecl())
120173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
120273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name2 = Enum2->getIdentifier();
120373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name2 && Enum2->getTypedefForAnonDecl())
120473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
1205ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (!::IsStructurallyEquivalent(Name1, Name2) ||
1206ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1207ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
120873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
120973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Enum/non-enum mismatch
1210ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
121173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
1212ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
121373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
121473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
1215ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                        Typedef2->getIdentifier()) ||
1216ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this,
121773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        Typedef1->getUnderlyingType(),
121873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        Typedef2->getUnderlyingType()))
1219ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
122073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
122173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Typedef/non-typedef mismatch.
1222ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
122373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
1224040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (ClassTemplateDecl *ClassTemplate1
1225040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                           = dyn_cast<ClassTemplateDecl>(D1)) {
1226040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1227040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1228040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                        ClassTemplate2->getIdentifier()) ||
1229040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor            !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1230040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1231040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1232040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Class template/non-class-template mismatch.
1233040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1234040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1235040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1236040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1237040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1238040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1239040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1240040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Kind mismatch.
1241040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1242040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1243040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (NonTypeTemplateParmDecl *NTTP1
1244040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1245040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (NonTypeTemplateParmDecl *NTTP2
1246040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1247040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1248040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1249040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1250040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Kind mismatch.
1251040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1252040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1253040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (TemplateTemplateParmDecl *TTP1
1254040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                  = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1255040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (TemplateTemplateParmDecl *TTP2
1256040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                    = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1257040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1258040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1259040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1260040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Kind mismatch.
1261040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1262040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1263040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
1264040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1265ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (!Equivalent) {
1266ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      // Note that these two declarations are not equivalent (and we already
1267ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      // know about it).
1268ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1269ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                               D2->getCanonicalDecl()));
1270ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      return true;
1271ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    }
127273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Check other declaration kinds!
127373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
127473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
127573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return false;
127673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
127773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
127873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor//----------------------------------------------------------------------------
12791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor// Import Types
12801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//----------------------------------------------------------------------------
12811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
128289cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas GregorQualType ASTNodeImporter::VisitType(Type *T) {
128389cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor  Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
128489cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    << T->getTypeClassName();
128589cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor  return QualType();
128689cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor}
128789cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor
12881b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
12891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  switch (T->getKind()) {
12901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Void: return Importer.getToContext().VoidTy;
12911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Bool: return Importer.getToContext().BoolTy;
12921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char_U:
12941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // The context we're importing from has an unsigned 'char'. If we're
12951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // importing into a context with a signed 'char', translate to
12961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // 'unsigned char' instead.
12971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (Importer.getToContext().getLangOptions().CharIsSigned)
12981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return Importer.getToContext().UnsignedCharTy;
12991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().CharTy;
13011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
13031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char16:
13051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++!
13061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().Char16Ty;
13071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char32:
13091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++!
13101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().Char32Ty;
13111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
13131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
13141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
13151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ULongLong:
13161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().UnsignedLongLongTy;
13171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
13181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char_S:
13201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // The context we're importing from has an unsigned 'char'. If we're
13211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // importing into a context with a signed 'char', translate to
13221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // 'unsigned char' instead.
13231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Importer.getToContext().getLangOptions().CharIsSigned)
13241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return Importer.getToContext().SignedCharTy;
13251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().CharTy;
13271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
13293f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::WChar_S:
13303f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::WChar_U:
13311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: If not in C++, shall we translate to the C equivalent of
13321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // wchar_t?
13331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().WCharTy;
13341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Short : return Importer.getToContext().ShortTy;
13361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Int : return Importer.getToContext().IntTy;
13371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Long : return Importer.getToContext().LongTy;
13381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
13391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
13401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Float: return Importer.getToContext().FloatTy;
13411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Double: return Importer.getToContext().DoubleTy;
13421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
13431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::NullPtr:
13451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++0x!
13461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().NullPtrTy;
13471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
13491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
13501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UndeducedAuto:
13511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++0x!
13521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().UndeducedAutoTy;
13531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCId:
13551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports Objective-C!
13561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinIdTy;
13571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCClass:
13591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinClassTy;
13601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCSel:
13621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinSelTy;
13631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
13641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return QualType();
13661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13681b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
13691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
13701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
13711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getComplexType(ToElementType);
13741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13761b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitPointerType(PointerType *T) {
13771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
13781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
13791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getPointerType(ToPointeeType);
13821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13841b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
13851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for blocks support in "to" context.
13861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
13871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
13881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getBlockPointerType(ToPointeeType);
13911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13931b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
13941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++ support in "to" context.
13951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
13961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
13971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getLValueReferenceType(ToPointeeType);
14001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14021b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
14031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++0x support in "to" context.
14041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
14051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
14061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getRValueReferenceType(ToPointeeType);
14091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14111b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
14121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++ support in "to" context.
14131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
14141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
14151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
14181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getMemberPointerType(ToPointeeType,
14191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      ClassType.getTypePtr());
14201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14221b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
14231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getConstantArrayType(ToElementType,
14281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSize(),
14291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSizeModifier(),
14301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->getIndexTypeCVRQualifiers());
14311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14331b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
14341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getIncompleteArrayType(ToElementType,
14391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                        T->getSizeModifier(),
14401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                T->getIndexTypeCVRQualifiers());
14411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14431b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
14441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *Size = Importer.Import(T->getSizeExpr());
14491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!Size)
14501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  SourceRange Brackets = Importer.Import(T->getBracketsRange());
14531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getVariableArrayType(ToElementType, Size,
14541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSizeModifier(),
14551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                T->getIndexTypeCVRQualifiers(),
14561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      Brackets);
14571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14591b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitVectorType(VectorType *T) {
14601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getVectorType(ToElementType,
14651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->getNumElements(),
1466e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                               T->getVectorKind());
14671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14691b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
14701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getExtVectorType(ToElementType,
14751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                  T->getNumElements());
14761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14781b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
14791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: What happens if we're importing a function without a prototype
14801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // into C++? Should we make it variadic?
14811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToResultType = Importer.Import(T->getResultType());
14821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToResultType.isNull())
14831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
1484264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola
14851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getFunctionNoProtoType(ToResultType,
1486264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                        T->getExtInfo());
14871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14891b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
14901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToResultType = Importer.Import(T->getResultType());
14911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToResultType.isNull())
14921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Import argument types
14951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<QualType, 4> ArgTypes;
14961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
14971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                         AEnd = T->arg_type_end();
14981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       A != AEnd; ++A) {
14991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType ArgType = Importer.Import(*A);
15001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (ArgType.isNull())
15011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
15021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ArgTypes.push_back(ArgType);
15031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
15041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Import exception types
15061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<QualType, 4> ExceptionTypes;
15071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (FunctionProtoType::exception_iterator E = T->exception_begin(),
15081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                          EEnd = T->exception_end();
15091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       E != EEnd; ++E) {
15101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType ExceptionType = Importer.Import(*E);
15111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (ExceptionType.isNull())
15121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
15131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ExceptionTypes.push_back(ExceptionType);
15141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
1515e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
1516e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1517e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.Exceptions = ExceptionTypes.data();
15181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1520e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                                 ArgTypes.size(), EPI);
15211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15231b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
15241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  TypedefDecl *ToDecl
15251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
15261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
15271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeDeclType(ToDecl);
15301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15321b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
15331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
15341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToExpr)
15351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeOfExprType(ToExpr);
15381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15401b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
15411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
15421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToUnderlyingType.isNull())
15431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeOfType(ToUnderlyingType);
15461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15481b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
15491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
15501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToExpr)
15511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getDecltypeType(ToExpr);
15541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15561b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitRecordType(RecordType *T) {
15571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  RecordDecl *ToDecl
15581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
15591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
15601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTagDeclType(ToDecl);
15631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15651b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitEnumType(EnumType *T) {
15661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  EnumDecl *ToDecl
15671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
15681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
15691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTagDeclType(ToDecl);
15721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1574d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorQualType ASTNodeImporter::VisitTemplateSpecializationType(
1575d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                TemplateSpecializationType *T) {
1576d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1577d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (ToTemplate.isNull())
1578d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return QualType();
1579d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1580d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs;
1581d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1582d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return QualType();
1583d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1584d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  QualType ToCanonType;
1585d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (!QualType(T, 0).isCanonical()) {
1586d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualType FromCanonType
1587d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1588d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ToCanonType =Importer.Import(FromCanonType);
1589d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToCanonType.isNull())
1590d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return QualType();
1591d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1592d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1593d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                         ToTemplateArgs.data(),
1594d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                         ToTemplateArgs.size(),
1595d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                               ToCanonType);
1596d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1597d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
15981b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
1599465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *ToQualifier = 0;
1600465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  // Note: the qualifier in an ElaboratedType is optional.
1601465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T->getQualifier()) {
1602465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ToQualifier = Importer.Import(T->getQualifier());
1603465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (!ToQualifier)
1604465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return QualType();
1605465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
16061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
16071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToNamedType = Importer.Import(T->getNamedType());
16081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToNamedType.isNull())
16091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
16101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1611465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Importer.getToContext().getElaboratedType(T->getKeyword(),
1612465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                                   ToQualifier, ToNamedType);
16131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
16141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
16151b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
16161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  ObjCInterfaceDecl *Class
16171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
16181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!Class)
16191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
16201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1621c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCInterfaceType(Class);
1622c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
1623c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
1624c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) {
1625c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  QualType ToBaseType = Importer.Import(T->getBaseType());
1626c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ToBaseType.isNull())
1627c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType();
1628c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
16291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
1630c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator P = T->qual_begin(),
16311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                     PEnd = T->qual_end();
16321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       P != PEnd; ++P) {
16331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ObjCProtocolDecl *Protocol
16341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
16351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Protocol)
16361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
16371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    Protocols.push_back(Protocol);
16381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
16391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1640c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCObjectType(ToBaseType,
1641c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                                   Protocols.data(),
1642c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                                   Protocols.size());
16431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
16441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
16451b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
16461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
16471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
16481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
16491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1650c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
16511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
16521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1653089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor//----------------------------------------------------------------------------
1654089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor// Import Declarations
1655089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor//----------------------------------------------------------------------------
1656a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregorbool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1657a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      DeclContext *&LexicalDC,
1658a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      DeclarationName &Name,
1659a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      SourceLocation &Loc) {
1660089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Import the context of this declaration.
1661a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DC = Importer.ImportContext(D->getDeclContext());
1662089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  if (!DC)
1663a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return true;
1664a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1665a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  LexicalDC = DC;
16669bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
16679bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
16689bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    if (!LexicalDC)
1669a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      return true;
16709bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  }
1671a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1672089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Import the name of this declaration.
1673a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Name = Importer.Import(D->getDeclName());
1674089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  if (D->getDeclName() && !Name)
1675a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return true;
1676a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1677a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the location of this declaration.
1678a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Loc = Importer.Import(D->getLocation());
1679a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  return false;
1680a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1681a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
16822577743c5650c646fb705df01403707e94f2df04Abramo Bagnaravoid
16832577743c5650c646fb705df01403707e94f2df04Abramo BagnaraASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
16842577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                          DeclarationNameInfo& To) {
16852577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  // NOTE: To.Name and To.Loc are already imported.
16862577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  // We only have to import To.LocInfo.
16872577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  switch (To.getName().getNameKind()) {
16882577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::Identifier:
16892577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::ObjCZeroArgSelector:
16902577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::ObjCOneArgSelector:
16912577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::ObjCMultiArgSelector:
16922577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXUsingDirective:
16932577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
16942577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
16952577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXOperatorName: {
16962577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    SourceRange Range = From.getCXXOperatorNameRange();
16972577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    To.setCXXOperatorNameRange(Importer.Import(Range));
16982577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
16992577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
17002577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXLiteralOperatorName: {
17012577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
17022577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
17032577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
17042577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
17052577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXConstructorName:
17062577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXDestructorName:
17072577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXConversionFunctionName: {
17082577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
17092577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    To.setNamedTypeInfo(Importer.Import(FromTInfo));
17102577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
17112577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
17122577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    assert(0 && "Unknown name kind.");
17132577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
17142577743c5650c646fb705df01403707e94f2df04Abramo Bagnara}
17152577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
1716083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregorvoid ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1717083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1718083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor                               FromEnd = FromDC->decls_end();
1719083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor       From != FromEnd;
1720083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor       ++From)
1721083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    Importer.Import(*From);
1722083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor}
1723083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor
1724d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregorbool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) {
1725d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (To->getDefinition())
1726d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return false;
1727d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1728d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  To->startDefinition();
1729d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1730d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Add base classes.
1731d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1732d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1733d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1734d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1735d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (CXXRecordDecl::base_class_iterator
1736d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                  Base1 = FromCXX->bases_begin(),
1737d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor            FromBaseEnd = FromCXX->bases_end();
1738d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor         Base1 != FromBaseEnd;
1739d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor         ++Base1) {
1740d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      QualType T = Importer.Import(Base1->getType());
1741d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (T.isNull())
1742c04d9d1be026cb201a716df1cd28a88878958beeDouglas Gregor        return true;
1743f90b27ad077c3339b62befc892382845339f9490Douglas Gregor
1744f90b27ad077c3339b62befc892382845339f9490Douglas Gregor      SourceLocation EllipsisLoc;
1745f90b27ad077c3339b62befc892382845339f9490Douglas Gregor      if (Base1->isPackExpansion())
1746f90b27ad077c3339b62befc892382845339f9490Douglas Gregor        EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
1747d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1748d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      Bases.push_back(
1749d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                    new (Importer.getToContext())
1750d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                      CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1751d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                       Base1->isVirtual(),
1752d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                       Base1->isBaseOfClass(),
1753d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                       Base1->getAccessSpecifierAsWritten(),
1754f90b27ad077c3339b62befc892382845339f9490Douglas Gregor                                   Importer.Import(Base1->getTypeSourceInfo()),
1755f90b27ad077c3339b62befc892382845339f9490Douglas Gregor                                       EllipsisLoc));
1756d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
1757d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Bases.empty())
1758d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      ToCXX->setBases(Bases.data(), Bases.size());
1759d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1760d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1761d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ImportDeclContext(From);
1762d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  To->completeDefinition();
1763c04d9d1be026cb201a716df1cd28a88878958beeDouglas Gregor  return false;
1764d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1765d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1766040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorTemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1767040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                TemplateParameterList *Params) {
1768040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  llvm::SmallVector<NamedDecl *, 4> ToParams;
1769040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  ToParams.reserve(Params->size());
1770040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  for (TemplateParameterList::iterator P = Params->begin(),
1771040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                    PEnd = Params->end();
1772040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor       P != PEnd; ++P) {
1773040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *To = Importer.Import(*P);
1774040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!To)
1775040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return 0;
1776040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1777040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    ToParams.push_back(cast<NamedDecl>(To));
1778040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1779040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1780040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return TemplateParameterList::Create(Importer.getToContext(),
1781040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       Importer.Import(Params->getTemplateLoc()),
1782040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       Importer.Import(Params->getLAngleLoc()),
1783040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       ToParams.data(), ToParams.size(),
1784040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       Importer.Import(Params->getRAngleLoc()));
1785040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1786040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1787d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorTemplateArgument
1788d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1789d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  switch (From.getKind()) {
1790d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Null:
1791d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument();
1792d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1793d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Type: {
1794d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualType ToType = Importer.Import(From.getAsType());
1795d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToType.isNull())
1796d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1797d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(ToType);
1798d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1799d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1800d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Integral: {
1801d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualType ToType = Importer.Import(From.getIntegralType());
1802d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToType.isNull())
1803d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1804d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(*From.getAsIntegral(), ToType);
1805d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1806d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1807d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Declaration:
1808d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Decl *To = Importer.Import(From.getAsDecl()))
1809d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument(To);
1810d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument();
1811d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1812d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Template: {
1813d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1814d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToTemplate.isNull())
1815d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1816d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1817d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(ToTemplate);
1818d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1819a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
1820a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  case TemplateArgument::TemplateExpansion: {
1821a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    TemplateName ToTemplate
1822a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      = Importer.Import(From.getAsTemplateOrTemplatePattern());
1823a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    if (ToTemplate.isNull())
1824a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      return TemplateArgument();
1825a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
1826a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    return TemplateArgument(ToTemplate, true);
1827a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  }
1828a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
1829d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Expression:
1830d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1831d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument(ToExpr);
1832d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument();
1833d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1834d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Pack: {
1835d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    llvm::SmallVector<TemplateArgument, 2> ToPack;
1836d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ToPack.reserve(From.pack_size());
1837d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1838d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1839d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1840d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    TemplateArgument *ToArgs
1841d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1842d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1843d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(ToArgs, ToPack.size());
1844d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1845d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1846d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1847d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm_unreachable("Invalid template argument kind");
1848d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return TemplateArgument();
1849d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1850d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1851d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregorbool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1852d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                              unsigned NumFromArgs,
1853d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                              llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1854d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  for (unsigned I = 0; I != NumFromArgs; ++I) {
1855d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1856d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (To.isNull() && !FromArgs[I].isNull())
1857d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return true;
1858d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1859d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ToArgs.push_back(To);
1860d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1861d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1862d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return false;
1863d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1864d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
186596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregorbool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
186673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        RecordDecl *ToRecord) {
1867bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
186873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Importer.getToContext(),
1869ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getNonEquivalentDecls());
1870bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
187196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
187296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
187336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregorbool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
1874bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
187573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Importer.getToContext(),
1876ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getNonEquivalentDecls());
1877bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
187836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
187936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
1880040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorbool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1881040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                        ClassTemplateDecl *To) {
1882040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1883040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                   Importer.getToContext(),
1884040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                   Importer.getNonEquivalentDecls());
1885040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return Ctx.IsStructurallyEquivalent(From, To);
1886040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1887040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1888a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitDecl(Decl *D) {
1889a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1890a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    << D->getDeclKindName();
1891a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  return 0;
1892a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1893a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1894788c62d1e87bfb596078817237f672a5f000999aDouglas GregorDecl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1895788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  // Import the major distinguishing characteristics of this namespace.
1896788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  DeclContext *DC, *LexicalDC;
1897788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  DeclarationName Name;
1898788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  SourceLocation Loc;
1899788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1900788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    return 0;
1901788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1902788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  NamespaceDecl *MergeWithNamespace = 0;
1903788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (!Name) {
1904788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // This is an anonymous namespace. Adopt an existing anonymous
1905788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // namespace if we can.
1906788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // FIXME: Not testable.
1907788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1908788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      MergeWithNamespace = TU->getAnonymousNamespace();
1909788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    else
1910788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1911788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  } else {
1912788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1913788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1914788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor         Lookup.first != Lookup.second;
1915788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor         ++Lookup.first) {
19160d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
1917788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        continue;
1918788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1919788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1920788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        MergeWithNamespace = FoundNS;
1921788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        ConflictingDecls.clear();
1922788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        break;
1923788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      }
1924788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1925788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
1926788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
1927788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1928788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (!ConflictingDecls.empty()) {
19290d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
1930788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                         ConflictingDecls.data(),
1931788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                         ConflictingDecls.size());
1932788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
1933788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  }
1934788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1935788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  // Create the "to" namespace, if needed.
1936788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  NamespaceDecl *ToNamespace = MergeWithNamespace;
1937788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (!ToNamespace) {
1938788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1939788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                        Name.getAsIdentifierInfo());
1940788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    ToNamespace->setLexicalDeclContext(LexicalDC);
1941788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    LexicalDC->addDecl(ToNamespace);
1942788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1943788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // If this is an anonymous namespace, register it as the anonymous
1944788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // namespace within its context.
1945788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (!Name) {
1946788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1947788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        TU->setAnonymousNamespace(ToNamespace);
1948788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      else
1949788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1950788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
1951788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  }
1952788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  Importer.Imported(D, ToNamespace);
1953788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1954788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  ImportDeclContext(D);
1955788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1956788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  return ToNamespace;
1957788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor}
1958788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
19599e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas GregorDecl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
19609e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // Import the major distinguishing characteristics of this typedef.
19619e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  DeclContext *DC, *LexicalDC;
19629e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  DeclarationName Name;
19639e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  SourceLocation Loc;
19649e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
19659e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    return 0;
19669e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
19679e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // If this typedef is not in block scope, determine whether we've
19689e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // seen a typedef with the same name (that we can merge with) or any
19699e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // other entity by that name (which name lookup could conflict with).
19709e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  if (!DC->isFunctionOrMethod()) {
19719e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
19729e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
19739e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
19749e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor         Lookup.first != Lookup.second;
19759e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor         ++Lookup.first) {
19769e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
19779e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor        continue;
19789e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
1979ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1980ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                            FoundTypedef->getUnderlyingType()))
19815ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor          return Importer.Imported(D, FoundTypedef);
19829e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      }
19839e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
19849e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
19859e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    }
19869e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
19879e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    if (!ConflictingDecls.empty()) {
19889e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
19899e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                         ConflictingDecls.data(),
19909e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                         ConflictingDecls.size());
19919e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (!Name)
19929e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor        return 0;
19939e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    }
19949e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  }
19959e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
1996ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the underlying type of this typedef;
1997ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getUnderlyingType());
1998ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
1999ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2000ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
20019e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // Create the new typedef node.
20029e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
20039e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
20049e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                               Loc, Name.getAsIdentifierInfo(),
20059e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                               TInfo);
2006325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToTypedef->setAccess(D->getAccess());
20079e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  ToTypedef->setLexicalDeclContext(LexicalDC);
20085ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToTypedef);
20099e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  LexicalDC->addDecl(ToTypedef);
2010ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
20119e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  return ToTypedef;
20129e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor}
20139e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
201436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorDecl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
201536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the major distinguishing characteristics of this enum.
201636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclContext *DC, *LexicalDC;
201736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName Name;
201836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  SourceLocation Loc;
201936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
202036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
202136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
202236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Figure out what enum name we're looking for.
202336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  unsigned IDNS = Decl::IDNS_Tag;
202436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName SearchName = Name;
202536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!SearchName && D->getTypedefForAnonDecl()) {
202636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
202736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    IDNS = Decl::IDNS_Ordinary;
202836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  } else if (Importer.getToContext().getLangOptions().CPlusPlus)
202936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    IDNS |= Decl::IDNS_Ordinary;
203036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
203136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // We may already have an enum of the same name; try to find and match it.
203236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!DC->isFunctionOrMethod() && SearchName) {
203336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
203436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
203536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         Lookup.first != Lookup.second;
203636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++Lookup.first) {
203736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
203836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        continue;
203936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
204036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Decl *Found = *Lookup.first;
204136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
204236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
204336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor          Found = Tag->getDecl();
204436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      }
204536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
204636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
20475ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor        if (IsStructuralMatch(D, FoundEnum))
20485ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor          return Importer.Imported(D, FoundEnum);
204936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      }
205036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
205136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
205236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
205336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
205436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (!ConflictingDecls.empty()) {
205536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
205636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.data(),
205736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.size());
205836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
205936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
206036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
206136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Create the enum declaration.
206273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
2063a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                  Name.getAsIdentifierInfo(),
2064a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                  Importer.Import(D->getTagKeywordLoc()), 0,
2065a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                  D->isScoped(), D->isScopedUsingClassTag(),
2066a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                  D->isFixed());
2067b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
2068b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (D->getQualifier()) {
2069b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2070b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2071b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    D2->setQualifierInfo(NNS, NNSRange);
2072b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2073325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  D2->setAccess(D->getAccess());
207473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  D2->setLexicalDeclContext(LexicalDC);
207573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Importer.Imported(D, D2);
207673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  LexicalDC->addDecl(D2);
207736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
207836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the integer type.
207936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  QualType ToIntegerType = Importer.Import(D->getIntegerType());
208036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (ToIntegerType.isNull())
208136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
208273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  D2->setIntegerType(ToIntegerType);
208336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
208436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the definition
208536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (D->isDefinition()) {
208636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
208736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (T.isNull())
208836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      return 0;
208936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
209036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    QualType ToPromotionType = Importer.Import(D->getPromotionType());
209136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (ToPromotionType.isNull())
209236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      return 0;
209336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
209473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->startDefinition();
2095083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    ImportDeclContext(D);
20961b5a618c59025898806160ed5e7f0ff5bb79e482John McCall
20971b5a618c59025898806160ed5e7f0ff5bb79e482John McCall    // FIXME: we might need to merge the number of positive or negative bits
20981b5a618c59025898806160ed5e7f0ff5bb79e482John McCall    // if the enumerator lists don't match.
20991b5a618c59025898806160ed5e7f0ff5bb79e482John McCall    D2->completeDefinition(T, ToPromotionType,
21001b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                           D->getNumPositiveBits(),
21011b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                           D->getNumNegativeBits());
210236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
210336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
210473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return D2;
210536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
210636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
210796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas GregorDecl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
210896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // If this record has a definition in the translation unit we're coming from,
210996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // but this particular declaration is not that definition, import the
211096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // definition and map to that.
2111952b017601f9c82b51119c3a1600f1312a833db9Douglas Gregor  TagDecl *Definition = D->getDefinition();
211296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (Definition && Definition != D) {
211396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *ImportedDef = Importer.Import(Definition);
21145ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor    if (!ImportedDef)
21155ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor      return 0;
21165ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
21175ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor    return Importer.Imported(D, ImportedDef);
211896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
211996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
212096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Import the major distinguishing characteristics of this record.
212196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclContext *DC, *LexicalDC;
212296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName Name;
212396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  SourceLocation Loc;
212496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
212596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
212696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
212796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Figure out what structure name we're looking for.
212896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  unsigned IDNS = Decl::IDNS_Tag;
212996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName SearchName = Name;
213096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!SearchName && D->getTypedefForAnonDecl()) {
213196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
213296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    IDNS = Decl::IDNS_Ordinary;
213396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  } else if (Importer.getToContext().getLangOptions().CPlusPlus)
213496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    IDNS |= Decl::IDNS_Ordinary;
213596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
213696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // We may already have a record of the same name; try to find and match it.
2137e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor  RecordDecl *AdoptDecl = 0;
213896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!DC->isFunctionOrMethod() && SearchName) {
213996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
214096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
214196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         Lookup.first != Lookup.second;
214296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         ++Lookup.first) {
214396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
214496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor        continue;
214596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
214696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Decl *Found = *Lookup.first;
214796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
214896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor        if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
214996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor          Found = Tag->getDecl();
215096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
215196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
215296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
2153e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2154e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2155e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // The record types structurally match, or the "from" translation
2156e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // unit only had a forward declaration anyway; call it the same
2157e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // function.
2158e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // FIXME: For C++, we should also merge methods here.
21595ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor            return Importer.Imported(D, FoundDef);
2160e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          }
2161e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        } else {
2162e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          // We have a forward declaration of this type, so adopt that forward
2163e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          // declaration rather than building a new one.
2164e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          AdoptDecl = FoundRecord;
2165e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          continue;
2166e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        }
216796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
216896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
216996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
217096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
217196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
217296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    if (!ConflictingDecls.empty()) {
217396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
217496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         ConflictingDecls.data(),
217596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         ConflictingDecls.size());
217696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
217796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
217896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
217996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Create the record declaration.
218073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  RecordDecl *D2 = AdoptDecl;
218173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!D2) {
21825250f27420386452a21692a6292c99ee7febdac4John McCall    if (isa<CXXRecordDecl>(D)) {
218373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2184e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   D->getTagKind(),
2185e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   DC, Loc,
2186e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   Name.getAsIdentifierInfo(),
218796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                        Importer.Import(D->getTagKeywordLoc()));
218873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      D2 = D2CXX;
2189325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor      D2->setAccess(D->getAccess());
2190e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor    } else {
219173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
2192e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                    DC, Loc,
2193e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                    Name.getAsIdentifierInfo(),
2194e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                    Importer.Import(D->getTagKeywordLoc()));
219596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
2196b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Import the qualifier, if any.
2197b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (D->getQualifier()) {
2198b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2199b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2200b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      D2->setQualifierInfo(NNS, NNSRange);
2201b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
220273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->setLexicalDeclContext(LexicalDC);
220373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    LexicalDC->addDecl(D2);
220496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
22055ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
220673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Importer.Imported(D, D2);
2207e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor
2208d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D->isDefinition() && ImportDefinition(D, D2))
2209d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
221096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
221173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return D2;
221296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
221396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
221436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorDecl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
221536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the major distinguishing characteristics of this enumerator.
221636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclContext *DC, *LexicalDC;
221736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName Name;
221836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  SourceLocation Loc;
2219ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
222036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
2221ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2222ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2223ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2224ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2225ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
222636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Determine whether there are any other declarations with the same name and
222736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // in the same context.
222836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!LexicalDC->isFunctionOrMethod()) {
222936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
223036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
223136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
223236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         Lookup.first != Lookup.second;
223336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++Lookup.first) {
223436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
223536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        continue;
223636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
223736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
223836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
223936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
224036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (!ConflictingDecls.empty()) {
224136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
224236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.data(),
224336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.size());
224436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!Name)
224536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        return 0;
224636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
224736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
224836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
224936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  Expr *Init = Importer.Import(D->getInitExpr());
225036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (D->getInitExpr() && !Init)
225136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
225236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
225336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  EnumConstantDecl *ToEnumerator
225436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
225536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                               Name.getAsIdentifierInfo(), T,
225636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                               Init, D->getInitVal());
2257325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToEnumerator->setAccess(D->getAccess());
225836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  ToEnumerator->setLexicalDeclContext(LexicalDC);
22595ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToEnumerator);
226036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  LexicalDC->addDecl(ToEnumerator);
226136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  return ToEnumerator;
226236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
226396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
2264a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2265a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the major distinguishing characteristics of this function.
2266a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC, *LexicalDC;
2267a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclarationName Name;
2268a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc;
2269ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2270089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    return 0;
22712577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
2272a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Try to find a function in our own ("to") context with the same name, same
2273a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // type, and in the same context as the function we're importing.
2274a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  if (!LexicalDC->isFunctionOrMethod()) {
2275a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2276a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
2277a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2278a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor         Lookup.first != Lookup.second;
2279a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor         ++Lookup.first) {
2280a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2281a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        continue;
2282a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2283a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2284a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        if (isExternalLinkage(FoundFunction->getLinkage()) &&
2285a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            isExternalLinkage(D->getLinkage())) {
2286ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          if (Importer.IsStructurallyEquivalent(D->getType(),
2287ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                FoundFunction->getType())) {
2288a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            // FIXME: Actually try to merge the body and other attributes.
22895ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor            return Importer.Imported(D, FoundFunction);
2290a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          }
2291a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2292a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // FIXME: Check for overloading more carefully, e.g., by boosting
2293a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Sema::IsOverload out to the AST library.
2294a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2295a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Function overloading is okay in C++.
2296a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          if (Importer.getToContext().getLangOptions().CPlusPlus)
2297a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            continue;
2298a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2299a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Complain about inconsistent function types.
2300a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
2301ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            << Name << D->getType() << FoundFunction->getType();
2302a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          Importer.ToDiag(FoundFunction->getLocation(),
2303a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                          diag::note_odr_value_here)
2304a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            << FoundFunction->getType();
2305a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        }
2306a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      }
2307a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2308a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
2309a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    }
2310a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2311a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    if (!ConflictingDecls.empty()) {
2312a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
2313a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                         ConflictingDecls.data(),
2314a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                         ConflictingDecls.size());
2315a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (!Name)
2316a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        return 0;
2317a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    }
2318a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
2319ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
23202577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo(Name, Loc);
23212577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  // Import additional name location/type info.
23222577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
23232577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
2324ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
2325ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2326ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2327ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2328a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2329a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the function parameters.
2330a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2331a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2332a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor       P != PEnd; ++P) {
2333a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2334a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    if (!ToP)
2335a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      return 0;
2336a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2337a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Parameters.push_back(ToP);
2338a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
2339a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2340a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Create the imported function.
2341a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2342c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  FunctionDecl *ToFunction = 0;
2343c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2344c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2345c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            cast<CXXRecordDecl>(DC),
23462577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                            NameInfo, T, TInfo,
2347c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            FromConstructor->isExplicit(),
2348c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            D->isInlineSpecified(),
2349c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            D->isImplicit());
2350c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else if (isa<CXXDestructorDecl>(D)) {
2351c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2352c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           cast<CXXRecordDecl>(DC),
2353b41d899a6023385c00a61eb9dd3e44db9dc7994eCraig Silverstein                                           NameInfo, T, TInfo,
2354c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isInlineSpecified(),
2355c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isImplicit());
2356c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else if (CXXConversionDecl *FromConversion
2357c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           = dyn_cast<CXXConversionDecl>(D)) {
2358c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2359c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           cast<CXXRecordDecl>(DC),
23602577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                           NameInfo, T, TInfo,
2361c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isInlineSpecified(),
2362c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           FromConversion->isExplicit());
23630629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor  } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
23640629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor    ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
23650629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       cast<CXXRecordDecl>(DC),
23660629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       NameInfo, T, TInfo,
23670629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       Method->isStatic(),
23680629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       Method->getStorageClassAsWritten(),
23690629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       Method->isInlineSpecified());
2370c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else {
23712577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
23722577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                      NameInfo, T, TInfo, D->getStorageClass(),
237316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                      D->getStorageClassAsWritten(),
2374c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                      D->isInlineSpecified(),
2375c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                      D->hasWrittenPrototype());
2376c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  }
2377b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2378b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
2379b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (D->getQualifier()) {
2380b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2381b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2382b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    ToFunction->setQualifierInfo(NNS, NNSRange);
2383b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2384325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToFunction->setAccess(D->getAccess());
2385c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToFunction->setLexicalDeclContext(LexicalDC);
2386c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Importer.Imported(D, ToFunction);
2387a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2388a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Set the parameters.
2389a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
2390c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Parameters[I]->setOwningFunction(ToFunction);
2391c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToFunction->addDecl(Parameters[I]);
2392a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
2393c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToFunction->setParams(Parameters.data(), Parameters.size());
2394a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2395a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // FIXME: Other bits to merge?
239681134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor
239781134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor  // Add this function to the lexical context.
239881134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor  LexicalDC->addDecl(ToFunction);
239981134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor
2400c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToFunction;
2401a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
2402a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2403c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2404c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitFunctionDecl(D);
2405c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2406c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
2407c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2408c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
2409c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2410c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
2411c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2412c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
2413c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2414c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
2415c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2416c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
2417c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2418c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
241996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas GregorDecl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
242096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Import the major distinguishing characteristics of a variable.
242196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclContext *DC, *LexicalDC;
242296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName Name;
242396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  SourceLocation Loc;
2424ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2425ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2426ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2427ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
2428ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2429ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
243096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
243196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
243296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
243396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  Expr *BitWidth = Importer.Import(D->getBitWidth());
243496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!BitWidth && D->getBitWidth())
243596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
243696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
243796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
243896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         Loc, Name.getAsIdentifierInfo(),
243996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         T, TInfo, BitWidth, D->isMutable());
2440325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToField->setAccess(D->getAccess());
244196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  ToField->setLexicalDeclContext(LexicalDC);
24425ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToField);
244396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  LexicalDC->addDecl(ToField);
244496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  return ToField;
244596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
244696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
244787c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetDecl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
244887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  // Import the major distinguishing characteristics of a variable.
244987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  DeclContext *DC, *LexicalDC;
245087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  DeclarationName Name;
245187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  SourceLocation Loc;
245287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
245387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    return 0;
245487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
245587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  // Import the type.
245687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  QualType T = Importer.Import(D->getType());
245787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (T.isNull())
245887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    return 0;
245987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
246087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  NamedDecl **NamedChain =
246187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
246287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
246387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  unsigned i = 0;
246487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
246587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       PE = D->chain_end(); PI != PE; ++PI) {
246687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    Decl* D = Importer.Import(*PI);
246787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    if (!D)
246887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet      return 0;
246987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    NamedChain[i++] = cast<NamedDecl>(D);
247087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  }
247187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
247287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
247387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                         Importer.getToContext(), DC,
247487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                         Loc, Name.getAsIdentifierInfo(), T,
247587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                         NamedChain, D->getChainingSize());
247687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  ToIndirectField->setAccess(D->getAccess());
247787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  ToIndirectField->setLexicalDeclContext(LexicalDC);
247887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  Importer.Imported(D, ToIndirectField);
247987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  LexicalDC->addDecl(ToIndirectField);
248087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return ToIndirectField;
248187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
248287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
24832e55e3af2f6d6c0509495357fade95105dd144cdDouglas GregorDecl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
24842e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Import the major distinguishing characteristics of an ivar.
24852e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  DeclContext *DC, *LexicalDC;
24862e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  DeclarationName Name;
24872e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  SourceLocation Loc;
24882e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
24892e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
24902e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
24912e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Determine whether we've already imported this ivar
24922e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
24932e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor       Lookup.first != Lookup.second;
24942e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor       ++Lookup.first) {
24952e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
24962e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (Importer.IsStructurallyEquivalent(D->getType(),
24972e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                            FoundIvar->getType())) {
24982e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.Imported(D, FoundIvar);
24992e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        return FoundIvar;
25002e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      }
25012e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
25022e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
25032e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << Name << D->getType() << FoundIvar->getType();
25042e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
25052e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << FoundIvar->getType();
25062e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      return 0;
25072e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    }
25082e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  }
25092e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
25102e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Import the type.
25112e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  QualType T = Importer.Import(D->getType());
25122e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (T.isNull())
25132e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
25142e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
25152e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
25162e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  Expr *BitWidth = Importer.Import(D->getBitWidth());
25172e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (!BitWidth && D->getBitWidth())
25182e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
25192e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
2520a06549226f45d5b72169a3d054415616dd1014a2Daniel Dunbar  ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2521a06549226f45d5b72169a3d054415616dd1014a2Daniel Dunbar                                              cast<ObjCContainerDecl>(DC),
25222e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              Loc, Name.getAsIdentifierInfo(),
25232e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              T, TInfo, D->getAccessControl(),
2524ac0021ba802e193e0f9f8207768c7862c7603bc0Fariborz Jahanian                                              BitWidth, D->getSynthesize());
25252e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  ToIvar->setLexicalDeclContext(LexicalDC);
25262e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  Importer.Imported(D, ToIvar);
25272e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  LexicalDC->addDecl(ToIvar);
25282e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  return ToIvar;
25292e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
25302e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor}
25312e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
2532a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2533a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the major distinguishing characteristics of a variable.
2534a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC, *LexicalDC;
2535a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclarationName Name;
2536a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc;
2537ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2538a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return 0;
2539089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2540089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Try to find a variable in our own ("to") context with the same name and
2541089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // in the same context as the variable we're importing.
25429bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (D->isFileVarDecl()) {
2543089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    VarDecl *MergeWithVar = 0;
2544089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2545089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
25469bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2547089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor         Lookup.first != Lookup.second;
2548089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor         ++Lookup.first) {
2549089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2550089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        continue;
2551089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2552089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2553089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        // We have found a variable that we may need to merge with. Check it.
2554089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        if (isExternalLinkage(FoundVar->getLinkage()) &&
2555089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            isExternalLinkage(D->getLinkage())) {
2556ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          if (Importer.IsStructurallyEquivalent(D->getType(),
2557ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                FoundVar->getType())) {
2558089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            MergeWithVar = FoundVar;
2559089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            break;
2560089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          }
2561089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2562d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          const ArrayType *FoundArray
2563d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            = Importer.getToContext().getAsArrayType(FoundVar->getType());
2564d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          const ArrayType *TArray
2565ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            = Importer.getToContext().getAsArrayType(D->getType());
2566d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          if (FoundArray && TArray) {
2567d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            if (isa<IncompleteArrayType>(FoundArray) &&
2568d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor                isa<ConstantArrayType>(TArray)) {
2569ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              // Import the type.
2570ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              QualType T = Importer.Import(D->getType());
2571ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              if (T.isNull())
2572ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                return 0;
2573ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2574d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              FoundVar->setType(T);
2575d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              MergeWithVar = FoundVar;
2576d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              break;
2577d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            } else if (isa<IncompleteArrayType>(TArray) &&
2578d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor                       isa<ConstantArrayType>(FoundArray)) {
2579d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              MergeWithVar = FoundVar;
2580d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              break;
25810f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor            }
25820f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor          }
25830f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor
2584089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
2585ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            << Name << D->getType() << FoundVar->getType();
2586089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2587089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            << FoundVar->getType();
2588089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        }
2589089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      }
2590089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2591089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
2592089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2593089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2594089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    if (MergeWithVar) {
2595089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      // An equivalent variable with external linkage has been found. Link
2596089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      // the two declarations, then merge them.
25975ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor      Importer.Imported(D, MergeWithVar);
2598089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2599089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (VarDecl *DDef = D->getDefinition()) {
2600089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2601089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(ExistingDef->getLocation(),
2602089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                          diag::err_odr_variable_multiple_def)
2603089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            << Name;
2604089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2605089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        } else {
2606089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Expr *Init = Importer.Import(DDef->getInit());
2607838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor          MergeWithVar->setInit(Init);
2608089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        }
2609089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      }
2610089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2611089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      return MergeWithVar;
2612089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2613089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2614089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    if (!ConflictingDecls.empty()) {
2615089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
2616089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                         ConflictingDecls.data(),
2617089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                         ConflictingDecls.size());
2618089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (!Name)
2619089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        return 0;
2620089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2621089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  }
262282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2623ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
2624ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2625ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2626ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2627ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2628089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Create the imported variable.
262982fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2630089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2631089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                   Name.getAsIdentifierInfo(), T, TInfo,
263216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClass(),
263316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten());
2634b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
2635b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (D->getQualifier()) {
2636b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2637b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2638b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    ToVar->setQualifierInfo(NNS, NNSRange);
2639b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2640325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToVar->setAccess(D->getAccess());
26419bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ToVar->setLexicalDeclContext(LexicalDC);
26425ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToVar);
26439bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  LexicalDC->addDecl(ToVar);
26449bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
2645089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Merge the initializer.
2646089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // FIXME: Can we really import any initializer? Alternatively, we could force
2647089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // ourselves to import every declaration of a variable and then only use
2648089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // getInit() here.
2649838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
2650089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2651089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // FIXME: Other bits to merge?
2652089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2653089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  return ToVar;
2654089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
2655089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
26562cd00932b9b5403047139ce8cfaa3ae47966f894Douglas GregorDecl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
26572cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Parameters are created in the translation unit's context, then moved
26582cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // into the function declaration's context afterward.
26592cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
26602cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
26612cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the name of this declaration.
26622cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
26632cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  if (D->getDeclName() && !Name)
26642cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    return 0;
26652cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
26662cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the location of this declaration.
26672cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
26682cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
26692cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the parameter's type.
26702cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  QualType T = Importer.Import(D->getType());
26712cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  if (T.isNull())
26722cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    return 0;
26732cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
26742cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Create the imported parameter.
26752cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  ImplicitParamDecl *ToParm
26762cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    = ImplicitParamDecl::Create(Importer.getToContext(), DC,
26772cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor                                Loc, Name.getAsIdentifierInfo(),
26782cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor                                T);
26792cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  return Importer.Imported(D, ToParm);
26802cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor}
26812cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
2682a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2683a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Parameters are created in the translation unit's context, then moved
2684a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // into the function declaration's context afterward.
2685a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2686a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
268782fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // Import the name of this declaration.
268882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
268982fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (D->getDeclName() && !Name)
269082fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
269182fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2692a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the location of this declaration.
2693a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
2694a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2695a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the parameter's type.
2696a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  QualType T = Importer.Import(D->getType());
269782fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (T.isNull())
269882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
269982fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2700a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Create the imported parameter.
2701a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2702a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2703a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            Loc, Name.getAsIdentifierInfo(),
2704a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            T, TInfo, D->getStorageClass(),
270516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                             D->getStorageClassAsWritten(),
2706a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            /*FIXME: Default argument*/ 0);
2707bf73b352acb7a2d041ce8b50171dd7f8e2b2c1bbJohn McCall  ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
27085ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  return Importer.Imported(D, ToParm);
2709a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
271082fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2711c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas GregorDecl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2712c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the major distinguishing characteristics of a method.
2713c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  DeclContext *DC, *LexicalDC;
2714c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  DeclarationName Name;
2715c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  SourceLocation Loc;
2716c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2717c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return 0;
2718c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2719c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2720c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       Lookup.first != Lookup.second;
2721c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       ++Lookup.first) {
2722c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2723c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2724c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        continue;
2725c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2726c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check return types.
2727c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2728c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                             FoundMethod->getResultType())) {
2729c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2730c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name
2731c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->getResultType() << FoundMethod->getResultType();
2732c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2733c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2734c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2735c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2736c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2737c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2738c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check the number of parameters.
2739c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (D->param_size() != FoundMethod->param_size()) {
2740c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2741c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name
2742c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->param_size() << FoundMethod->param_size();
2743c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2744c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2745c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2746c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2747c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2748c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2749c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check parameter types.
2750c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2751c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor             PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2752c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor           P != PEnd; ++P, ++FoundP) {
2753c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2754c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                               (*FoundP)->getType())) {
2755c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          Importer.FromDiag((*P)->getLocation(),
2756c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                            diag::err_odr_objc_method_param_type_inconsistent)
2757c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << D->isInstanceMethod() << Name
2758c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << (*P)->getType() << (*FoundP)->getType();
2759c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2760c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << (*FoundP)->getType();
2761c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          return 0;
2762c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        }
2763c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2764c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2765c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check variadic/non-variadic.
2766c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check the number of parameters.
2767c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (D->isVariadic() != FoundMethod->isVariadic()) {
2768c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2769c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2770c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2771c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2772c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2773c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2774c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2775c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2776c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // FIXME: Any other bits we need to merge?
2777c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      return Importer.Imported(D, FoundMethod);
2778c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    }
2779c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2780c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2781c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the result type.
2782c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  QualType ResultTy = Importer.Import(D->getResultType());
2783c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (ResultTy.isNull())
2784c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return 0;
2785c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
27864bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor  TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
27874bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor
2788c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ObjCMethodDecl *ToMethod
2789c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    = ObjCMethodDecl::Create(Importer.getToContext(),
2790c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Loc,
2791c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Importer.Import(D->getLocEnd()),
2792c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Name.getObjCSelector(),
27934bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor                             ResultTy, ResultTInfo, DC,
2794c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isInstanceMethod(),
2795c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isVariadic(),
2796c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isSynthesized(),
27973fe104154dd2e8ffb351142d74f308938b5c99bfFariborz Jahanian                             D->isDefined(),
2798c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->getImplementationControl());
2799c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2800c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // FIXME: When we decide to merge method definitions, we'll need to
2801c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // deal with implicit parameters.
2802c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2803c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the parameters
2804c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2805c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2806c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                   FromPEnd = D->param_end();
2807c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       FromP != FromPEnd;
2808c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       ++FromP) {
2809c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2810c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    if (!ToP)
2811c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      return 0;
2812c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2813c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToParams.push_back(ToP);
2814c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2815c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2816c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Set the parameters.
2817c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2818c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToParams[I]->setOwningFunction(ToMethod);
2819c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToMethod->addDecl(ToParams[I]);
2820c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2821c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToMethod->setMethodParams(Importer.getToContext(),
28224ecb25fa94897b2c03510292acace710e5262ba5Fariborz Jahanian                            ToParams.data(), ToParams.size(),
28234ecb25fa94897b2c03510292acace710e5262ba5Fariborz Jahanian                            ToParams.size());
2824c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2825c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToMethod->setLexicalDeclContext(LexicalDC);
2826c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Importer.Imported(D, ToMethod);
2827c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  LexicalDC->addDecl(ToMethod);
2828c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToMethod;
2829c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor}
2830c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2831b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas GregorDecl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2832b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import the major distinguishing characteristics of a category.
2833b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  DeclContext *DC, *LexicalDC;
2834b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  DeclarationName Name;
2835b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  SourceLocation Loc;
2836b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2837b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    return 0;
2838b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2839b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCInterfaceDecl *ToInterface
2840b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2841b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (!ToInterface)
2842b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    return 0;
2843b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2844b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Determine if we've already encountered this category.
2845b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCCategoryDecl *MergeWithCategory
2846b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2847b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCCategoryDecl *ToCategory = MergeWithCategory;
2848b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (!ToCategory) {
2849b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2850b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Importer.Import(D->getAtLoc()),
2851b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Loc,
2852b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                       Importer.Import(D->getCategoryNameLoc()),
2853b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Name.getAsIdentifierInfo());
2854b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setLexicalDeclContext(LexicalDC);
2855b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    LexicalDC->addDecl(ToCategory);
2856b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Imported(D, ToCategory);
2857b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2858b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // Link this category into its class's category list.
2859b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setClassInterface(ToInterface);
2860b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->insertNextClassCategory();
2861b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2862b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // Import protocols
2863b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2864b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2865b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2866b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      = D->protocol_loc_begin();
2867b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2868b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          FromProtoEnd = D->protocol_end();
2869b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor         FromProto != FromProtoEnd;
2870b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor         ++FromProto, ++FromProtoLoc) {
2871b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      ObjCProtocolDecl *ToProto
2872b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2873b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      if (!ToProto)
2874b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor        return 0;
2875b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      Protocols.push_back(ToProto);
2876b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2877b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    }
2878b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2879b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
2880b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2881b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                ProtocolLocs.data(), Importer.getToContext());
2882b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2883b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  } else {
2884b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Imported(D, ToCategory);
2885b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  }
2886b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2887b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import all of the members of this category.
2888083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
2889b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2890b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // If we have an implementation, import it as well.
2891b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (D->getImplementation()) {
2892b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ObjCCategoryImplDecl *Impl
2893cad2c59b0c087edea83d0fbf6eabde4a7960c778Douglas Gregor      = cast_or_null<ObjCCategoryImplDecl>(
2894cad2c59b0c087edea83d0fbf6eabde4a7960c778Douglas Gregor                                       Importer.Import(D->getImplementation()));
2895b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    if (!Impl)
2896b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      return 0;
2897b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2898b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setImplementation(Impl);
2899b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  }
2900b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2901b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  return ToCategory;
2902b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor}
2903b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
29042e2a400383c7a64a927f61eeed596b08928e1d4bDouglas GregorDecl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
2905b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import the major distinguishing characteristics of a protocol.
29062e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  DeclContext *DC, *LexicalDC;
29072e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  DeclarationName Name;
29082e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  SourceLocation Loc;
29092e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
29102e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    return 0;
29112e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
29122e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  ObjCProtocolDecl *MergeWithProtocol = 0;
29132e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
29142e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       Lookup.first != Lookup.second;
29152e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++Lookup.first) {
29162e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
29172e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      continue;
29182e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
29192e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
29202e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      break;
29212e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  }
29222e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
29232e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  ObjCProtocolDecl *ToProto = MergeWithProtocol;
29242e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  if (!ToProto || ToProto->isForwardDecl()) {
29252e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if (!ToProto) {
29262e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
29272e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                                         Name.getAsIdentifierInfo());
29282e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto->setForwardDecl(D->isForwardDecl());
29292e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto->setLexicalDeclContext(LexicalDC);
29302e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      LexicalDC->addDecl(ToProto);
29312e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    }
29322e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Imported(D, ToProto);
29332e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
29342e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    // Import protocols
29352e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
29362e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
29372e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    ObjCProtocolDecl::protocol_loc_iterator
29382e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      FromProtoLoc = D->protocol_loc_begin();
29392e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
29402e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                                          FromProtoEnd = D->protocol_end();
29412e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       FromProto != FromProtoEnd;
29422e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++FromProto, ++FromProtoLoc) {
29432e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ObjCProtocolDecl *ToProto
29442e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
29452e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      if (!ToProto)
29462e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor        return 0;
29472e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      Protocols.push_back(ToProto);
29482e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
29492e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    }
29502e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
29512e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
29522e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    ToProto->setProtocolList(Protocols.data(), Protocols.size(),
29532e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                             ProtocolLocs.data(), Importer.getToContext());
29542e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  } else {
29552e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Imported(D, ToProto);
29562e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  }
29572e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
2958b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import all of the members of this protocol.
2959083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
29602e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
29612e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  return ToProto;
29622e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor}
29632e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
2964a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas GregorDecl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2965a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // Import the major distinguishing characteristics of an @interface.
2966a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  DeclContext *DC, *LexicalDC;
2967a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  DeclarationName Name;
2968a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  SourceLocation Loc;
2969a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2970a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    return 0;
2971a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2972a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  ObjCInterfaceDecl *MergeWithIface = 0;
2973a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2974a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       Lookup.first != Lookup.second;
2975a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++Lookup.first) {
2976a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2977a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      continue;
2978a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2979a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2980a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      break;
2981a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
2982a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2983a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  ObjCInterfaceDecl *ToIface = MergeWithIface;
2984a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (!ToIface || ToIface->isForwardDecl()) {
2985a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!ToIface) {
2986a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2987a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          DC, Loc,
2988a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          Name.getAsIdentifierInfo(),
2989deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor                                          Importer.Import(D->getClassLoc()),
2990a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          D->isForwardDecl(),
2991a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          D->isImplicitInterfaceDecl());
29922e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToIface->setForwardDecl(D->isForwardDecl());
2993a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setLexicalDeclContext(LexicalDC);
2994a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      LexicalDC->addDecl(ToIface);
2995a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
2996a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Imported(D, ToIface);
2997a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2998a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (D->getSuperClass()) {
2999a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ObjCInterfaceDecl *Super
3000a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3001a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      if (!Super)
3002a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        return 0;
3003a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3004a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setSuperClass(Super);
3005a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3006a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
3007a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3008a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // Import protocols
3009a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3010a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
3011a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ObjCInterfaceDecl::protocol_loc_iterator
3012a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      FromProtoLoc = D->protocol_loc_begin();
301353b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek
301453b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // FIXME: Should we be usng all_referenced_protocol_begin() here?
3015a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3016a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                           FromProtoEnd = D->protocol_end();
3017a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       FromProto != FromProtoEnd;
3018a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++FromProto, ++FromProtoLoc) {
3019a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ObjCProtocolDecl *ToProto
3020a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3021a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      if (!ToProto)
3022a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        return 0;
3023a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      Protocols.push_back(ToProto);
3024a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3025a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
3026a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3027a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
3028a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3029a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                             ProtocolLocs.data(), Importer.getToContext());
3030a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3031a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // Import @end range
3032a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3033a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  } else {
3034a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Imported(D, ToIface);
30352e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
30362e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    // Check for consistency of superclasses.
30372e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    DeclarationName FromSuperName, ToSuperName;
30382e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (D->getSuperClass())
30392e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
30402e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (ToIface->getSuperClass())
30412e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      ToSuperName = ToIface->getSuperClass()->getDeclName();
30422e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (FromSuperName != ToSuperName) {
30432e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(ToIface->getLocation(),
30442e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                      diag::err_odr_objc_superclass_inconsistent)
30452e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << ToIface->getDeclName();
30462e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (ToIface->getSuperClass())
30472e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.ToDiag(ToIface->getSuperClassLoc(),
30482e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                        diag::note_odr_objc_superclass)
30492e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor          << ToIface->getSuperClass()->getDeclName();
30502e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      else
30512e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.ToDiag(ToIface->getLocation(),
30522e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                        diag::note_odr_objc_missing_superclass);
30532e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (D->getSuperClass())
30542e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.FromDiag(D->getSuperClassLoc(),
30552e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                          diag::note_odr_objc_superclass)
30562e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor          << D->getSuperClass()->getDeclName();
30572e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      else
30582e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.FromDiag(D->getLocation(),
30592e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                          diag::note_odr_objc_missing_superclass);
30602e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      return 0;
30612e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    }
3062a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
3063a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3064b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import categories. When the categories themselves are imported, they'll
3065b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // hook themselves into this interface.
3066b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3067b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor       FromCat = FromCat->getNextClassCategory())
3068b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Import(FromCat);
3069b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
3070a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // Import all of the members of this class.
3071083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
3072a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3073a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // If we have an @implementation, import it as well.
3074a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (D->getImplementation()) {
3075dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3076dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                       Importer.Import(D->getImplementation()));
3077a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!Impl)
3078a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      return 0;
3079a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3080a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setImplementation(Impl);
3081a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
3082a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
30832e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  return ToIface;
3084a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor}
3085a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
30863daef29bf390dbdb3603748280afd5827d1811daDouglas GregorDecl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
30873daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
30883daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor                                        Importer.Import(D->getCategoryDecl()));
30893daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  if (!Category)
30903daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    return 0;
30913daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
30923daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
30933daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  if (!ToImpl) {
30943daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    DeclContext *DC = Importer.ImportContext(D->getDeclContext());
30953daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    if (!DC)
30963daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor      return 0;
30973daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
30983daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
30993daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor                                          Importer.Import(D->getLocation()),
31003daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor                                          Importer.Import(D->getIdentifier()),
31013daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor                                          Category->getClassInterface());
31023daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
31033daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    DeclContext *LexicalDC = DC;
31043daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    if (D->getDeclContext() != D->getLexicalDeclContext()) {
31053daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor      LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
31063daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor      if (!LexicalDC)
31073daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor        return 0;
31083daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
31093daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor      ToImpl->setLexicalDeclContext(LexicalDC);
31103daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    }
31113daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
31123daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    LexicalDC->addDecl(ToImpl);
31133daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    Category->setImplementation(ToImpl);
31143daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  }
31153daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
31163daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  Importer.Imported(D, ToImpl);
3117cad2c59b0c087edea83d0fbf6eabde4a7960c778Douglas Gregor  ImportDeclContext(D);
31183daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  return ToImpl;
31193daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor}
31203daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
3121dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas GregorDecl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3122dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  // Find the corresponding interface.
3123dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3124dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                       Importer.Import(D->getClassInterface()));
3125dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  if (!Iface)
3126dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    return 0;
3127dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3128dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  // Import the superclass, if any.
3129dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ObjCInterfaceDecl *Super = 0;
3130dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  if (D->getSuperClass()) {
3131dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Super = cast_or_null<ObjCInterfaceDecl>(
3132dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                          Importer.Import(D->getSuperClass()));
3133dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    if (!Super)
3134dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      return 0;
3135dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  }
3136dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3137dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ObjCImplementationDecl *Impl = Iface->getImplementation();
3138dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  if (!Impl) {
3139dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // We haven't imported an implementation yet. Create a new @implementation
3140dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // now.
3141dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3142dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                  Importer.ImportContext(D->getDeclContext()),
3143dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                          Importer.Import(D->getLocation()),
3144dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                          Iface, Super);
3145dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3146dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    if (D->getDeclContext() != D->getLexicalDeclContext()) {
3147dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      DeclContext *LexicalDC
3148dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        = Importer.ImportContext(D->getLexicalDeclContext());
3149dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      if (!LexicalDC)
3150dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        return 0;
3151dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      Impl->setLexicalDeclContext(LexicalDC);
3152dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    }
3153dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3154dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // Associate the implementation with the class it implements.
3155dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Iface->setImplementation(Impl);
3156dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Importer.Imported(D, Iface->getImplementation());
3157dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  } else {
3158dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Importer.Imported(D, Iface->getImplementation());
3159dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3160dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // Verify that the existing @implementation has the same superclass.
3161dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    if ((Super && !Impl->getSuperClass()) ||
3162dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        (!Super && Impl->getSuperClass()) ||
3163dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        (Super && Impl->getSuperClass() &&
3164dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor         Super->getCanonicalDecl() != Impl->getSuperClass())) {
3165dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        Importer.ToDiag(Impl->getLocation(),
3166dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                        diag::err_odr_objc_superclass_inconsistent)
3167dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          << Iface->getDeclName();
3168dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        // FIXME: It would be nice to have the location of the superclass
3169dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        // below.
3170dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        if (Impl->getSuperClass())
3171dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.ToDiag(Impl->getLocation(),
3172dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                          diag::note_odr_objc_superclass)
3173dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          << Impl->getSuperClass()->getDeclName();
3174dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        else
3175dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.ToDiag(Impl->getLocation(),
3176dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                          diag::note_odr_objc_missing_superclass);
3177dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        if (D->getSuperClass())
3178dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.FromDiag(D->getLocation(),
3179dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                            diag::note_odr_objc_superclass)
3180dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          << D->getSuperClass()->getDeclName();
3181dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        else
3182dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.FromDiag(D->getLocation(),
3183dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                            diag::note_odr_objc_missing_superclass);
3184dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      return 0;
3185dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    }
3186dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  }
3187dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3188dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  // Import all of the members of this @implementation.
3189dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ImportDeclContext(D);
3190dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3191dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  return Impl;
3192dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor}
3193dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3194e3261624c1870e52d7efc2ac83e647713361ac6cDouglas GregorDecl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3195e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Import the major distinguishing characteristics of an @property.
3196e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  DeclContext *DC, *LexicalDC;
3197e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  DeclarationName Name;
3198e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  SourceLocation Loc;
3199e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3200e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    return 0;
3201e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3202e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Check whether we have already imported this property.
3203e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3204e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       Lookup.first != Lookup.second;
3205e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       ++Lookup.first) {
3206e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    if (ObjCPropertyDecl *FoundProp
3207e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                                = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3208e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // Check property types.
3209e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      if (!Importer.IsStructurallyEquivalent(D->getType(),
3210e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                                             FoundProp->getType())) {
3211e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3212e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor          << Name << D->getType() << FoundProp->getType();
3213e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3214e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor          << FoundProp->getType();
3215e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        return 0;
3216e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      }
3217e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3218e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // FIXME: Check property attributes, getters, setters, etc.?
3219e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3220e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // Consider these properties to be equivalent.
3221e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      Importer.Imported(D, FoundProp);
3222e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      return FoundProp;
3223e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    }
3224e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  }
3225e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3226e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Import the type.
322783a230c83a54190366138c1a4f4310ef838b88fcJohn McCall  TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
322883a230c83a54190366138c1a4f4310ef838b88fcJohn McCall  if (!T)
3229e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    return 0;
3230e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3231e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Create the new property.
3232e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ObjCPropertyDecl *ToProperty
3233e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3234e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               Name.getAsIdentifierInfo(),
3235e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               Importer.Import(D->getAtLoc()),
3236e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               T,
3237e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               D->getPropertyImplementation());
3238e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  Importer.Imported(D, ToProperty);
3239e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setLexicalDeclContext(LexicalDC);
3240e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  LexicalDC->addDecl(ToProperty);
3241e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3242e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setPropertyAttributes(D->getPropertyAttributes());
324380aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian  ToProperty->setPropertyAttributesAsWritten(
324480aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian                                      D->getPropertyAttributesAsWritten());
3245e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3246e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3247e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setGetterMethodDecl(
3248e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3249e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setSetterMethodDecl(
3250e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3251e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setPropertyIvarDecl(
3252e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3253e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  return ToProperty;
3254e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor}
3255e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3256954e0c75c42f321945aff8b9ee96da43cd90c752Douglas GregorDecl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3257954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3258954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                        Importer.Import(D->getPropertyDecl()));
3259954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (!Property)
3260954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    return 0;
3261954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3262954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3263954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (!DC)
3264954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    return 0;
3265954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3266954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  // Import the lexical declaration context.
3267954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  DeclContext *LexicalDC = DC;
3268954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
3269954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3270954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    if (!LexicalDC)
3271954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      return 0;
3272954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  }
3273954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3274954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3275954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (!InImpl)
3276954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    return 0;
3277954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3278954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  // Import the ivar (for an @synthesize).
3279954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  ObjCIvarDecl *Ivar = 0;
3280954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (D->getPropertyIvarDecl()) {
3281954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    Ivar = cast_or_null<ObjCIvarDecl>(
3282954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                    Importer.Import(D->getPropertyIvarDecl()));
3283954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    if (!Ivar)
3284954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      return 0;
3285954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  }
3286954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3287954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  ObjCPropertyImplDecl *ToImpl
3288954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3289954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (!ToImpl) {
3290954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3291954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          Importer.Import(D->getLocStart()),
3292954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          Importer.Import(D->getLocation()),
3293954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          Property,
3294954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          D->getPropertyImplementation(),
3295954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          Ivar,
3296954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                  Importer.Import(D->getPropertyIvarDeclLoc()));
3297954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    ToImpl->setLexicalDeclContext(LexicalDC);
3298954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    Importer.Imported(D, ToImpl);
3299954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    LexicalDC->addDecl(ToImpl);
3300954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  } else {
3301954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    // Check that we have the same kind of property implementation (@synthesize
3302954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    // vs. @dynamic).
3303954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3304954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      Importer.ToDiag(ToImpl->getLocation(),
3305954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                      diag::err_odr_objc_property_impl_kind_inconsistent)
3306954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << Property->getDeclName()
3307954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << (ToImpl->getPropertyImplementation()
3308954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                              == ObjCPropertyImplDecl::Dynamic);
3309954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      Importer.FromDiag(D->getLocation(),
3310954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                        diag::note_odr_objc_property_impl_kind)
3311954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << D->getPropertyDecl()->getDeclName()
3312954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3313954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      return 0;
3314954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    }
3315954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3316954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    // For @synthesize, check that we have the same
3317954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3318954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        Ivar != ToImpl->getPropertyIvarDecl()) {
3319954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3320954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                      diag::err_odr_objc_synthesize_ivar_inconsistent)
3321954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << Property->getDeclName()
3322954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << ToImpl->getPropertyIvarDecl()->getDeclName()
3323954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << Ivar->getDeclName();
3324954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3325954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                        diag::note_odr_objc_synthesize_ivar_here)
3326954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << D->getPropertyIvarDecl()->getDeclName();
3327954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      return 0;
3328954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    }
3329954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3330954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    // Merge the existing implementation with the new implementation.
3331954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    Importer.Imported(D, ToImpl);
3332954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  }
3333954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3334954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  return ToImpl;
3335954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor}
3336954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
33372b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas GregorDecl *
33382b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas GregorASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
33392b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  // Import the context of this declaration.
33402b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
33412b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  if (!DC)
33422b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    return 0;
33432b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
33442b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  DeclContext *LexicalDC = DC;
33452b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
33462b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
33472b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    if (!LexicalDC)
33482b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      return 0;
33492b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  }
33502b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
33512b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  // Import the location of this declaration.
33522b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
33532b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
33542b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
33552b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  llvm::SmallVector<SourceLocation, 4> Locations;
33562b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
33572b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    = D->protocol_loc_begin();
33582b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  for (ObjCForwardProtocolDecl::protocol_iterator FromProto
33592b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor         = D->protocol_begin(), FromProtoEnd = D->protocol_end();
33602b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor       FromProto != FromProtoEnd;
33612b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor       ++FromProto, ++FromProtoLoc) {
33622b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    ObjCProtocolDecl *ToProto
33632b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
33642b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    if (!ToProto)
33652b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      continue;
33662b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
33672b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Protocols.push_back(ToProto);
33682b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Locations.push_back(Importer.Import(*FromProtoLoc));
33692b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  }
33702b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
33712b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ObjCForwardProtocolDecl *ToForward
33722b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
33732b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor                                      Protocols.data(), Protocols.size(),
33742b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor                                      Locations.data());
33752b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ToForward->setLexicalDeclContext(LexicalDC);
33762b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  LexicalDC->addDecl(ToForward);
33772b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  Importer.Imported(D, ToForward);
33782b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  return ToForward;
33792b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor}
33802b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
3381a2bc15b7463a9f85a5bff1531d833c278426a733Douglas GregorDecl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3382a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  // Import the context of this declaration.
3383a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3384a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  if (!DC)
3385a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    return 0;
3386a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3387a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  DeclContext *LexicalDC = DC;
3388a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
3389a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3390a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    if (!LexicalDC)
3391a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      return 0;
3392a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  }
3393a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3394a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  // Import the location of this declaration.
3395a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
3396a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3397a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3398a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  llvm::SmallVector<SourceLocation, 4> Locations;
3399a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3400a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor       From != FromEnd; ++From) {
3401a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    ObjCInterfaceDecl *ToIface
3402a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3403a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    if (!ToIface)
3404a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      continue;
3405a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3406a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Interfaces.push_back(ToIface);
3407a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Locations.push_back(Importer.Import(From->getLocation()));
3408a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  }
3409a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3410a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3411a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Loc,
3412a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Interfaces.data(),
3413a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Locations.data(),
3414a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Interfaces.size());
3415a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  ToClass->setLexicalDeclContext(LexicalDC);
3416a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  LexicalDC->addDecl(ToClass);
3417a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  Importer.Imported(D, ToClass);
3418a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  return ToClass;
3419a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor}
3420a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3421040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3422040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // For template arguments, we adopt the translation unit as our declaration
3423040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // context. This context will be fixed when the actual template declaration
3424040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // is created.
3425040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3426040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Import default argument.
3427040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return TemplateTypeParmDecl::Create(Importer.getToContext(),
3428040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                              Importer.getToContext().getTranslationUnitDecl(),
3429040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      Importer.Import(D->getLocation()),
3430040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->getDepth(),
3431040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->getIndex(),
3432040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      Importer.Import(D->getIdentifier()),
3433040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->wasDeclaredWithTypename(),
3434040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->isParameterPack());
3435040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3436040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3437040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *
3438040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3439040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the name of this declaration.
3440040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
3441040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D->getDeclName() && !Name)
3442040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3443040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3444040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the location of this declaration.
3445040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
3446040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3447040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the type of this declaration.
3448040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  QualType T = Importer.Import(D->getType());
3449040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (T.isNull())
3450040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3451040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3452040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import type-source information.
3453040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3454040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D->getTypeSourceInfo() && !TInfo)
3455040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3456040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3457040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Import default argument.
3458040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3459040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3460040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                               Importer.getToContext().getTranslationUnitDecl(),
3461040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         Loc, D->getDepth(), D->getPosition(),
3462040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         Name.getAsIdentifierInfo(),
346310738d36b150aa65206890c1c845cdba076e4200Douglas Gregor                                         T, D->isParameterPack(), TInfo);
3464040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3465040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3466040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *
3467040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3468040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the name of this declaration.
3469040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
3470040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D->getDeclName() && !Name)
3471040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3472040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3473040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the location of this declaration.
3474040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
3475040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3476040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import template parameters.
3477040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  TemplateParameterList *TemplateParams
3478040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    = ImportTemplateParameterList(D->getTemplateParameters());
3479040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!TemplateParams)
3480040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3481040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3482040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Import default argument.
3483040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3484040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3485040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                              Importer.getToContext().getTranslationUnitDecl(),
3486040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          Loc, D->getDepth(), D->getPosition(),
348761c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                          D->isParameterPack(),
3488040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          Name.getAsIdentifierInfo(),
3489040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          TemplateParams);
3490040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3491040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3492040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3493040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // If this record has a definition in the translation unit we're coming from,
3494040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // but this particular declaration is not that definition, import the
3495040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // definition and map to that.
3496040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  CXXRecordDecl *Definition
3497040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3498040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (Definition && Definition != D->getTemplatedDecl()) {
3499040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *ImportedDef
3500040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      = Importer.Import(Definition->getDescribedClassTemplate());
3501040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!ImportedDef)
3502040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return 0;
3503040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3504040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return Importer.Imported(D, ImportedDef);
3505040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
3506040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3507040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the major distinguishing characteristics of this class template.
3508040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclContext *DC, *LexicalDC;
3509040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclarationName Name;
3510040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  SourceLocation Loc;
3511040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3512040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3513040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3514040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // We may already have a template of the same name; try to find and match it.
3515040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!DC->isFunctionOrMethod()) {
3516040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3517040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3518040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor         Lookup.first != Lookup.second;
3519040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor         ++Lookup.first) {
3520040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3521040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        continue;
3522040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3523040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Decl *Found = *Lookup.first;
3524040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (ClassTemplateDecl *FoundTemplate
3525040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                        = dyn_cast<ClassTemplateDecl>(Found)) {
3526040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (IsStructuralMatch(D, FoundTemplate)) {
3527040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          // The class templates structurally match; call it the same template.
3528040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          // FIXME: We may be filling in a forward declaration here. Handle
3529040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          // this case!
3530040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Importer.Imported(D->getTemplatedDecl(),
3531040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                            FoundTemplate->getTemplatedDecl());
3532040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          return Importer.Imported(D, FoundTemplate);
3533040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        }
3534040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
3535040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3536040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
3537040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
3538040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3539040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!ConflictingDecls.empty()) {
3540040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3541040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         ConflictingDecls.data(),
3542040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         ConflictingDecls.size());
3543040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
3544040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3545040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!Name)
3546040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return 0;
3547040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
3548040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3549040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3550040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3551040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Create the declaration that is being templated.
3552040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3553040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                     DTemplated->getTagKind(),
3554040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                     DC,
3555040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     Importer.Import(DTemplated->getLocation()),
3556040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                     Name.getAsIdentifierInfo(),
3557040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                               Importer.Import(DTemplated->getTagKeywordLoc()));
3558040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2Templated->setAccess(DTemplated->getAccess());
3559040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3560040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3561040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the qualifier, if any.
3562040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (DTemplated->getQualifier()) {
3563040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    NestedNameSpecifier *NNS = Importer.Import(DTemplated->getQualifier());
3564040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    SourceRange NNSRange = Importer.Import(DTemplated->getQualifierRange());
3565040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    D2Templated->setQualifierInfo(NNS, NNSRange);
3566040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
3567040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2Templated->setLexicalDeclContext(LexicalDC);
3568040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3569040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Create the class template declaration itself.
3570040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  TemplateParameterList *TemplateParams
3571040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    = ImportTemplateParameterList(D->getTemplateParameters());
3572040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!TemplateParams)
3573040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3574040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3575040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3576040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                    Loc, Name, TemplateParams,
3577040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                    D2Templated,
3578040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  /*PrevDecl=*/0);
3579040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2Templated->setDescribedClassTemplate(D2);
3580040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3581040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2->setAccess(D->getAccess());
3582040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2->setLexicalDeclContext(LexicalDC);
3583040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  LexicalDC->addDecl(D2);
3584040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3585040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Note the relationship between the class templates.
3586040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  Importer.Imported(D, D2);
3587040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  Importer.Imported(DTemplated, D2Templated);
3588040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3589040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3590040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    // FIXME: Import definition!
3591040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
3592040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3593040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return D2;
3594040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3595040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3596d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorDecl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3597d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                          ClassTemplateSpecializationDecl *D) {
3598d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // If this record has a definition in the translation unit we're coming from,
3599d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // but this particular declaration is not that definition, import the
3600d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // definition and map to that.
3601d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  TagDecl *Definition = D->getDefinition();
3602d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (Definition && Definition != D) {
3603d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    Decl *ImportedDef = Importer.Import(Definition);
3604d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!ImportedDef)
3605d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return 0;
3606d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3607d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return Importer.Imported(D, ImportedDef);
3608d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3609d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3610d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateDecl *ClassTemplate
3611d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = cast_or_null<ClassTemplateDecl>(Importer.Import(
3612d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 D->getSpecializedTemplate()));
3613d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (!ClassTemplate)
3614d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3615d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3616d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Import the context of this declaration.
3617d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  DeclContext *DC = ClassTemplate->getDeclContext();
3618d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (!DC)
3619d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3620d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3621d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  DeclContext *LexicalDC = DC;
3622d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
3623d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3624d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!LexicalDC)
3625d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return 0;
3626d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3627d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3628d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Import the location of this declaration.
3629d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
3630d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3631d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Import template arguments.
3632d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3633d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (ImportTemplateArguments(D->getTemplateArgs().data(),
3634d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                              D->getTemplateArgs().size(),
3635d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                              TemplateArgs))
3636d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3637d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3638d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Try to find an existing specialization with these template arguments.
3639d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  void *InsertPos = 0;
3640d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateSpecializationDecl *D2
3641d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = ClassTemplate->findSpecialization(TemplateArgs.data(),
3642d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                        TemplateArgs.size(), InsertPos);
3643d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D2) {
3644d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // We already have a class template specialization with these template
3645d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // arguments.
3646d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3647d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // FIXME: Check for specialization vs. instantiation errors.
3648d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3649d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (RecordDecl *FoundDef = D2->getDefinition()) {
3650d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3651d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        // The record types structurally match, or the "from" translation
3652d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        // unit only had a forward declaration anyway; call it the same
3653d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        // function.
3654d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return Importer.Imported(D, FoundDef);
3655d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      }
3656d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
3657d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  } else {
3658d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Create a new specialization.
3659d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3660d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 D->getTagKind(), DC,
3661d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 Loc, ClassTemplate,
3662d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 TemplateArgs.data(),
3663d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 TemplateArgs.size(),
3664d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 /*PrevDecl=*/0);
3665d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    D2->setSpecializationKind(D->getSpecializationKind());
3666d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3667d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Add this specialization to the class template.
3668d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ClassTemplate->AddSpecialization(D2, InsertPos);
3669d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3670d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Import the qualifier, if any.
3671d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (D->getQualifier()) {
3672d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
3673d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      SourceRange NNSRange = Importer.Import(D->getQualifierRange());
3674d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      D2->setQualifierInfo(NNS, NNSRange);
3675d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
3676d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3677d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3678d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Add the specialization to this context.
3679d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    D2->setLexicalDeclContext(LexicalDC);
3680d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    LexicalDC->addDecl(D2);
3681d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3682d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  Importer.Imported(D, D2);
3683d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3684d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D->isDefinition() && ImportDefinition(D, D2))
3685d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3686d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3687d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return D2;
3688d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
3689d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
36904800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
36914800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor// Import Statements
36924800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
36934800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
36944800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorStmt *ASTNodeImporter::VisitStmt(Stmt *S) {
36954800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
36964800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    << S->getStmtClassName();
36974800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return 0;
36984800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
36994800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
37004800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
37014800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor// Import Expressions
37024800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
37034800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorExpr *ASTNodeImporter::VisitExpr(Expr *E) {
37044800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
37054800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    << E->getStmtClassName();
37064800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return 0;
37074800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
37084800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
3709440806306674e23ad74726208cbdc6f37849dd9dDouglas GregorExpr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3710440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  NestedNameSpecifier *Qualifier = 0;
3711440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (E->getQualifier()) {
3712440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    Qualifier = Importer.Import(E->getQualifier());
3713440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    if (!E->getQualifier())
3714440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor      return 0;
3715440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  }
3716440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
3717440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3718440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (!ToD)
3719440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    return 0;
3720440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
3721440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  QualType T = Importer.Import(E->getType());
3722440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (T.isNull())
3723440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    return 0;
3724440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
3725440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
3726440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             Importer.Import(E->getQualifierRange()),
3727440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             ToD,
3728440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             Importer.Import(E->getLocation()),
3729f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                             T, E->getValueKind(),
3730440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             /*FIXME:TemplateArgs=*/0);
3731440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor}
3732440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
37334800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorExpr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
37344800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  QualType T = Importer.Import(E->getType());
37354800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (T.isNull())
37364800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return 0;
37374800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
37389996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return IntegerLiteral::Create(Importer.getToContext(),
37399996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                                E->getValue(), T,
37409996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                                Importer.Import(E->getLocation()));
37414800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
37424800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
3743b2e400aae8c62c4e1616016f40618baace0da065Douglas GregorExpr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3744b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  QualType T = Importer.Import(E->getType());
3745b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  if (T.isNull())
3746b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor    return 0;
3747b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor
3748b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3749b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor                                                        E->isWide(), T,
3750b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor                                          Importer.Import(E->getLocation()));
3751b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor}
3752b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor
3753f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3754f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
3755f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!SubExpr)
3756f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3757f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3758f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext())
3759f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                  ParenExpr(Importer.Import(E->getLParen()),
3760f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                            Importer.Import(E->getRParen()),
3761f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                            SubExpr);
3762f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3763f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3764f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3765f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
3766f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
3767f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3768f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3769f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
3770f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!SubExpr)
3771f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3772f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3773f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
3774f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                     T, E->getValueKind(),
3775f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                     E->getObjectKind(),
3776f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                         Importer.Import(E->getOperatorLoc()));
3777f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3778f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3779bd249a542878a626192746c1e0c0b21f164e6df7Douglas GregorExpr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
3780bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  QualType ResultType = Importer.Import(E->getType());
3781bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3782bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  if (E->isArgumentType()) {
3783bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3784bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    if (!TInfo)
3785bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor      return 0;
3786bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3787bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3788bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                                           TInfo, ResultType,
3789bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                           Importer.Import(E->getOperatorLoc()),
3790bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                           Importer.Import(E->getRParenLoc()));
3791bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  }
3792bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3793bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3794bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  if (!SubExpr)
3795bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    return 0;
3796bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3797bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3798bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                                         SubExpr, ResultType,
3799bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                          Importer.Import(E->getOperatorLoc()),
3800bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                          Importer.Import(E->getRParenLoc()));
3801bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor}
3802bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3803f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3804f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
3805f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
3806f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3807f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3808f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *LHS = Importer.Import(E->getLHS());
3809f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!LHS)
3810f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3811f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3812f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *RHS = Importer.Import(E->getRHS());
3813f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!RHS)
3814f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3815f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3816f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
3817f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                      T, E->getValueKind(),
3818f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                      E->getObjectKind(),
3819f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                          Importer.Import(E->getOperatorLoc()));
3820f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3821f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3822f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3823f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
3824f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
3825f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3826f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3827f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3828f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (CompLHSType.isNull())
3829f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3830f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3831f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType CompResultType = Importer.Import(E->getComputationResultType());
3832f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (CompResultType.isNull())
3833f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3834f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3835f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *LHS = Importer.Import(E->getLHS());
3836f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!LHS)
3837f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3838f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3839f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *RHS = Importer.Import(E->getRHS());
3840f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!RHS)
3841f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3842f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3843f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext())
3844f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                        CompoundAssignOperator(LHS, RHS, E->getOpcode(),
3845f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                               T, E->getValueKind(),
3846f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                               E->getObjectKind(),
3847f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                               CompLHSType, CompResultType,
3848f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                          Importer.Import(E->getOperatorLoc()));
3849f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3850f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3851f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallbool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3852f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (E->path_empty()) return false;
3853f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
3854f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  // TODO: import cast paths
3855f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return true;
3856f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
3857f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
385836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorExpr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
385936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  QualType T = Importer.Import(E->getType());
386036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (T.isNull())
386136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
386236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
386336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
386436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!SubExpr)
386536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
3866f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
3867f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  CXXCastPath BasePath;
3868f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (ImportCastPath(E, BasePath))
3869f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return 0;
3870f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
3871f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
38725baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                                  SubExpr, &BasePath, E->getValueKind());
387336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
387436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
3875008847a70ab122a99911149199855060fb3753b4Douglas GregorExpr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3876008847a70ab122a99911149199855060fb3753b4Douglas Gregor  QualType T = Importer.Import(E->getType());
3877008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (T.isNull())
3878008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
3879008847a70ab122a99911149199855060fb3753b4Douglas Gregor
3880008847a70ab122a99911149199855060fb3753b4Douglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
3881008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (!SubExpr)
3882008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
3883008847a70ab122a99911149199855060fb3753b4Douglas Gregor
3884008847a70ab122a99911149199855060fb3753b4Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3885008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (!TInfo && E->getTypeInfoAsWritten())
3886008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
3887008847a70ab122a99911149199855060fb3753b4Douglas Gregor
3888f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  CXXCastPath BasePath;
3889f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (ImportCastPath(E, BasePath))
3890f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return 0;
3891f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
3892f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  return CStyleCastExpr::Create(Importer.getToContext(), T,
3893f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                E->getValueKind(), E->getCastKind(),
3894f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                SubExpr, &BasePath, TInfo,
3895f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                Importer.Import(E->getLParenLoc()),
3896f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                Importer.Import(E->getRParenLoc()));
3897008847a70ab122a99911149199855060fb3753b4Douglas Gregor}
3898008847a70ab122a99911149199855060fb3753b4Douglas Gregor
389933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios KyrtzidisASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
390039b49bcaaddb1049234fca9500c0ac02c088e23dChris Lattner                         ASTContext &FromContext, FileManager &FromFileManager)
39011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  : ToContext(ToContext), FromContext(FromContext),
390239b49bcaaddb1049234fca9500c0ac02c088e23dChris Lattner    ToFileManager(ToFileManager), FromFileManager(FromFileManager) {
39039bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ImportedDecls[FromContext.getTranslationUnitDecl()]
39049bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    = ToContext.getTranslationUnitDecl();
39059bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
39069bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
39079bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorASTImporter::~ASTImporter() { }
39081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
39091b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTImporter::Import(QualType FromT) {
39101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (FromT.isNull())
39111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
39121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
3913169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Check whether we've already imported this type.
3914169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  llvm::DenseMap<Type *, Type *>::iterator Pos
3915169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor    = ImportedTypes.find(FromT.getTypePtr());
3916169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  if (Pos != ImportedTypes.end())
3917169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor    return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
39181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
3919169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Import the type
39201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  ASTNodeImporter Importer(*this);
39211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToT = Importer.Visit(FromT.getTypePtr());
39221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToT.isNull())
39231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToT;
39241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
3925169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Record the imported type.
3926169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
3927169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor
39281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
39291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
39301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
39319bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorTypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
393282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (!FromTSI)
393382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return FromTSI;
393482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
393582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // FIXME: For now we just create a "trivial" type source info based
39365606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky  // on the type and a single location. Implement a real version of this.
393782fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  QualType T = Import(FromTSI->getType());
393882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (T.isNull())
393982fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
394082fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
394182fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  return ToContext.getTrivialTypeSourceInfo(T,
3942bd054dba8a3023821f2a0951b0fae05e3522a7c9Abramo Bagnara                        FromTSI->getTypeLoc().getSourceRange().getBegin());
39439bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
39449bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
39459bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorDecl *ASTImporter::Import(Decl *FromD) {
39469bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromD)
39479bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
39489bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
39499bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Check whether we've already imported this declaration.
39509bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
39519bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (Pos != ImportedDecls.end())
39529bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return Pos->second;
39539bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
39549bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Import the type
39559bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ASTNodeImporter Importer(*this);
39569bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  Decl *ToD = Importer.Visit(FromD);
39579bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!ToD)
39589bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
39599bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
39609bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Record the imported declaration.
39619bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ImportedDecls[FromD] = ToD;
3962ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
3963ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3964ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // Keep track of anonymous tags that have an associated typedef.
3965ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (FromTag->getTypedefForAnonDecl())
3966ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      AnonTagsWithPendingTypedefs.push_back(FromTag);
3967ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3968ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // When we've finished transforming a typedef, see whether it was the
3969ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // typedef for an anonymous tag.
3970ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    for (llvm::SmallVector<TagDecl *, 4>::iterator
3971ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor               FromTag = AnonTagsWithPendingTypedefs.begin(),
3972ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            FromTagEnd = AnonTagsWithPendingTypedefs.end();
3973ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor         FromTag != FromTagEnd; ++FromTag) {
3974ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3975ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3976ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          // We found the typedef for an anonymous tag; link them.
3977ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3978ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          AnonTagsWithPendingTypedefs.erase(FromTag);
3979ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          break;
3980ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        }
3981ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      }
3982ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    }
3983ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  }
3984ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
39859bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return ToD;
39869bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
39879bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
39889bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorDeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
39899bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromDC)
39909bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return FromDC;
39919bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
39929bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
39939bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
39949bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
39959bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorExpr *ASTImporter::Import(Expr *FromE) {
39969bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromE)
39979bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
39989bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
39999bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
40009bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
40019bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40029bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorStmt *ASTImporter::Import(Stmt *FromS) {
40039bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromS)
40049bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
40059bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40064800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Check whether we've already imported this declaration.
40074800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
40084800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (Pos != ImportedStmts.end())
40094800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return Pos->second;
40104800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
40114800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Import the type
40124800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  ASTNodeImporter Importer(*this);
40134800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Stmt *ToS = Importer.Visit(FromS);
40144800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (!ToS)
40154800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return 0;
40164800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
40174800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Record the imported declaration.
40184800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  ImportedStmts[FromS] = ToS;
40194800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return ToS;
40209bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
40219bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40229bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorNestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
40239bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromNNS)
40249bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
40259bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40269bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // FIXME: Implement!
40279bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return 0;
40289bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
40299bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
4030d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorTemplateName ASTImporter::Import(TemplateName From) {
4031d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  switch (From.getKind()) {
4032d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::Template:
4033d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (TemplateDecl *ToTemplate
4034d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4035d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateName(ToTemplate);
4036d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4037d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateName();
4038d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4039d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::OverloadedTemplate: {
4040d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4041d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    UnresolvedSet<2> ToTemplates;
4042d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4043d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                             E = FromStorage->end();
4044d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor         I != E; ++I) {
4045d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4046d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        ToTemplates.addDecl(To);
4047d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      else
4048d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return TemplateName();
4049d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
4050d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4051d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                               ToTemplates.end());
4052d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
4053d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4054d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::QualifiedTemplate: {
4055d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4056d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4057d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Qualifier)
4058d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateName();
4059d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4060d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (TemplateDecl *ToTemplate
4061d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4062d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return ToContext.getQualifiedTemplateName(Qualifier,
4063d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                QTN->hasTemplateKeyword(),
4064d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                ToTemplate);
4065d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4066d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateName();
4067d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
4068d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4069d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::DependentTemplate: {
4070d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    DependentTemplateName *DTN = From.getAsDependentTemplateName();
4071d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4072d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Qualifier)
4073d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateName();
4074d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4075d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (DTN->isIdentifier()) {
4076d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return ToContext.getDependentTemplateName(Qualifier,
4077d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                Import(DTN->getIdentifier()));
4078d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
4079d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4080d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4081d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
4082d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
4083d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4084d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm_unreachable("Invalid template name kind");
4085d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return TemplateName();
4086d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
4087d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
40889bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorSourceLocation ASTImporter::Import(SourceLocation FromLoc) {
40899bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (FromLoc.isInvalid())
40909bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return SourceLocation();
40919bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
4092885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &FromSM = FromContext.getSourceManager();
4093885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4094885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // For now, map everything down to its spelling location, so that we
4095885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // don't have to import macro instantiations.
4096885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // FIXME: Import macro instantiations!
4097885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  FromLoc = FromSM.getSpellingLoc(FromLoc);
4098885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4099885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &ToSM = ToContext.getSourceManager();
4100885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4101885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor             .getFileLocWithOffset(Decomposed.second);
41029bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
41039bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
41049bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorSourceRange ASTImporter::Import(SourceRange FromRange) {
41059bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
41069bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
41079bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
4108885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas GregorFileID ASTImporter::Import(FileID FromID) {
4109535a3e20104461c136654d59fb833ae80644ae79Sebastian Redl  llvm::DenseMap<FileID, FileID>::iterator Pos
4110535a3e20104461c136654d59fb833ae80644ae79Sebastian Redl    = ImportedFileIDs.find(FromID);
4111885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  if (Pos != ImportedFileIDs.end())
4112885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    return Pos->second;
4113885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4114885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &FromSM = FromContext.getSourceManager();
4115885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &ToSM = ToContext.getSourceManager();
4116885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4117885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
4118885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4119885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // Include location of this file.
4120885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4121885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4122885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // Map the FileID for to the "to" source manager.
4123885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  FileID ToID;
4124885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
4125885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  if (Cache->Entry) {
4126885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4127885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // disk again
4128885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4129885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // than mmap the files several times.
413039b49bcaaddb1049234fca9500c0ac02c088e23dChris Lattner    const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
4131885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4132885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                             FromSLoc.getFile().getFileCharacteristic());
4133885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  } else {
4134885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We want to re-use the existing MemoryBuffer!
413533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    const llvm::MemoryBuffer *
413633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis        FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
4137885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    llvm::MemoryBuffer *ToBuf
4138a0a270c0f1c0a4e3482438bdc5f4a7bd3d25f0a6Chris Lattner      = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
4139885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                                             FromBuf->getBufferIdentifier());
4140885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4141885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  }
4142885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4143885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4144535a3e20104461c136654d59fb833ae80644ae79Sebastian Redl  ImportedFileIDs[FromID] = ToID;
4145885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  return ToID;
4146885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor}
4147885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
41481b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorDeclarationName ASTImporter::Import(DeclarationName FromName) {
41491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!FromName)
41501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return DeclarationName();
41511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
41521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  switch (FromName.getNameKind()) {
41531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::Identifier:
41541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Import(FromName.getAsIdentifierInfo());
41551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
41561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCZeroArgSelector:
41571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCOneArgSelector:
41581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCMultiArgSelector:
41591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Import(FromName.getObjCSelector());
41601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
41611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXConstructorName: {
41621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
41631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
41641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
41651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
41661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXConstructorName(
41671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
41681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
41691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
41701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXDestructorName: {
41711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
41721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
41731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
41741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
41751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXDestructorName(
41761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
41771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
41781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
41791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXConversionFunctionName: {
41801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
41811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
41821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
41831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
41841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXConversionFunctionName(
41851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
41861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
41871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
41881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXOperatorName:
41891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXOperatorName(
41901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                          FromName.getCXXOverloadedOperator());
41911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
41921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXLiteralOperatorName:
41931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXLiteralOperatorName(
41941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                   Import(FromName.getCXXLiteralIdentifier()));
41951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
41961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXUsingDirective:
41971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: STATICS!
41981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return DeclarationName::getUsingDirectiveName();
41991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
42001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
42011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Silence bogus GCC warning
42021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return DeclarationName();
42031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
42041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
4205d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorIdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
42061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!FromId)
42071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return 0;
42081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
42091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return &ToContext.Idents.get(FromId->getName());
42101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
4211089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
4212c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas GregorSelector ASTImporter::Import(Selector FromSel) {
4213c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (FromSel.isNull())
4214c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return Selector();
4215c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
4216c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  llvm::SmallVector<IdentifierInfo *, 4> Idents;
4217c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4218c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4219c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4220c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4221c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor}
4222c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
4223089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4224089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                DeclContext *DC,
4225089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                unsigned IDNS,
4226089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                NamedDecl **Decls,
4227089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                unsigned NumDecls) {
4228089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  return Name;
4229089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
4230089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
4231089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
423233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  return ToContext.getDiagnostics().Report(Loc, DiagID);
4233089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
4234089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
4235089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
423633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  return FromContext.getDiagnostics().Report(Loc, DiagID);
4237089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
42385ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
42395ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas GregorDecl *ASTImporter::Imported(Decl *From, Decl *To) {
42405ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  ImportedDecls[From] = To;
42415ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  return To;
4242af667588d53de22795c5304f1496ccaac2a71402Daniel Dunbar}
4243ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
4244ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregorbool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
4245ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  llvm::DenseMap<Type *, Type *>::iterator Pos
4246ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor   = ImportedTypes.find(From.getTypePtr());
4247ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4248ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return true;
4249ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
425033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
4251bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(From, To);
4252ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor}
4253