ASTImporter.cpp revision 9f71a8f4c7a182a5236da9e747d57cc1d1bd24c2
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);
118dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
119e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
1202b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
121a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Decl *VisitObjCClassDecl(ObjCClassDecl *D);
122040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
123040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
124040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
125040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
126d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    Decl *VisitClassTemplateSpecializationDecl(
127d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                            ClassTemplateSpecializationDecl *D);
128a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
1294800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    // Importing statements
1304800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Stmt *VisitStmt(Stmt *S);
1314800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
1324800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    // Importing expressions
1334800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Expr *VisitExpr(Expr *E);
134440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    Expr *VisitDeclRefExpr(DeclRefExpr *E);
1354800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Expr *VisitIntegerLiteral(IntegerLiteral *E);
136b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor    Expr *VisitCharacterLiteral(CharacterLiteral *E);
137f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitParenExpr(ParenExpr *E);
138f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitUnaryOperator(UnaryOperator *E);
139bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    Expr *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
140f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitBinaryOperator(BinaryOperator *E);
141f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
14236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
143008847a70ab122a99911149199855060fb3753b4Douglas Gregor    Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
1441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  };
1451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
1461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//----------------------------------------------------------------------------
14873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor// Structural Equivalence
14973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor//----------------------------------------------------------------------------
15073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
15173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregornamespace {
15273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  struct StructuralEquivalenceContext {
15373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief AST contexts for which we are checking structural equivalence.
15473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    ASTContext &C1, &C2;
15573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
15673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief The set of "tentative" equivalences between two canonical
15773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// declarations, mapping from a declaration in the first context to the
15873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// declaration in the second context that we believe to be equivalent.
15973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
16073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
16173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Queue of declarations in the first context whose equivalence
16273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// with a declaration in the second context still needs to be verified.
16373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    std::deque<Decl *> DeclsToCheck;
16473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
165ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    /// \brief Declaration (from, to) pairs that are known not to be equivalent
166ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    /// (which we have already complained about).
167ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
168ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
16973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Whether we're being strict about the spelling of types when
17073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// unifying two types.
17173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool StrictTypeSpelling;
17273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
17373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
174ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor               llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
17573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                 bool StrictTypeSpelling = false)
17633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis      : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
177ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        StrictTypeSpelling(StrictTypeSpelling) { }
17873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
17973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Determine whether the two declarations are structurally
18073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// equivalent.
18173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
18273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
18373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Determine whether the two types are structurally equivalent.
18473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructurallyEquivalent(QualType T1, QualType T2);
18573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
18673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  private:
18773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Finish checking all of the structural equivalences.
18873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    ///
18973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \returns true if an error occurred, false otherwise.
19073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool Finish();
19173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
19273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  public:
19373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
19433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis      return C1.getDiagnostics().Report(Loc, DiagID);
19573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
19673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
19773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
19833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis      return C2.getDiagnostics().Report(Loc, DiagID);
19973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
20073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  };
20173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
20273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
20373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
20473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     QualType T1, QualType T2);
20573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
20673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Decl *D1, Decl *D2);
20773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
20873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine if two APInts have the same value, after zero-extending
20973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// one of them (if needed!) to ensure that the bit-widths match.
21073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
21173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() == I2.getBitWidth())
21273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == I2;
21373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
21473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() > I2.getBitWidth())
2159f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    return I1 == I2.zext(I1.getBitWidth());
21673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
2179f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad  return I1.zext(I2.getBitWidth()) == I2;
21873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
21973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
22073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine if two APSInts have the same value, zero- or sign-extending
22173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// as needed.
22273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
22373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
22473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == I2;
22573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
22673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Check for a bit-width mismatch.
22773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() > I2.getBitWidth())
2289f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    return IsSameValue(I1, I2.extend(I1.getBitWidth()));
22973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  else if (I2.getBitWidth() > I1.getBitWidth())
2309f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    return IsSameValue(I1.extend(I2.getBitWidth()), I2);
23173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
23273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // We have a signedness mismatch. Turn the signed value into an unsigned
23373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // value.
23473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.isSigned()) {
23573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (I1.isNegative())
23673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
23773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
23873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return llvm::APSInt(I1, true) == I2;
23973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
24073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I2.isNegative())
24273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
24373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return I1 == llvm::APSInt(I2, true);
24573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
24673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two expressions.
24873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
24973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Expr *E1, Expr *E2) {
25073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!E1 || !E2)
25173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return E1 == E2;
25273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
25373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Actually perform a structural comparison!
25473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
25573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
25673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
25773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two identifiers are equivalent.
25873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
25973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const IdentifierInfo *Name2) {
26073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!Name1 || !Name2)
26173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return Name1 == Name2;
26273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
26373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return Name1->getName() == Name2->getName();
26473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
26573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
26673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two nested-name-specifiers are equivalent.
26773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
26873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     NestedNameSpecifier *NNS1,
26973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     NestedNameSpecifier *NNS2) {
27073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Implement!
27173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
27273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
27373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
27473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two template arguments are equivalent.
27573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
27673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const TemplateArgument &Arg1,
27773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const TemplateArgument &Arg2) {
278d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (Arg1.getKind() != Arg2.getKind())
279d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return false;
280d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
281d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  switch (Arg1.getKind()) {
282d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Null:
283d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return true;
284d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
285d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Type:
286d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
287d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
288d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Integral:
289d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
290d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                          Arg2.getIntegralType()))
291d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return false;
292d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
293d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
294d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
295d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Declaration:
296d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
297d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
298d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Template:
299d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return IsStructurallyEquivalent(Context,
300d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg1.getAsTemplate(),
301d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg2.getAsTemplate());
302d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
303d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Expression:
304d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return IsStructurallyEquivalent(Context,
305d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg1.getAsExpr(), Arg2.getAsExpr());
306d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
307d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Pack:
308d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Arg1.pack_size() != Arg2.pack_size())
309d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return false;
310d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
311d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
312d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (!IsStructurallyEquivalent(Context,
313d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg1.pack_begin()[I],
314d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg2.pack_begin()[I]))
315d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return false;
316d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
317d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return true;
318d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
319d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
320d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm_unreachable("Invalid template argument kind");
32173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
32273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
32373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
32473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence for the common part of array
32573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// types.
32673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
32773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                          const ArrayType *Array1,
32873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                          const ArrayType *Array2) {
32973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!IsStructurallyEquivalent(Context,
33073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                Array1->getElementType(),
33173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                Array2->getElementType()))
33273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
33373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Array1->getSizeModifier() != Array2->getSizeModifier())
33473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
33573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
33673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
33773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
33873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
33973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
34073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
34173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two types.
34273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
34373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     QualType T1, QualType T2) {
34473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (T1.isNull() || T2.isNull())
34573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return T1.isNull() && T2.isNull();
34673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
34773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!Context.StrictTypeSpelling) {
34873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // We aren't being strict about token-to-token equivalence of types,
34973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // so map down to the canonical type.
35073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    T1 = Context.C1.getCanonicalType(T1);
35173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    T2 = Context.C2.getCanonicalType(T2);
35273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
35373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
35473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (T1.getQualifiers() != T2.getQualifiers())
35573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
35673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
357ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  Type::TypeClass TC = T1->getTypeClass();
358ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
359ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T1->getTypeClass() != T2->getTypeClass()) {
360ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // Compare function types with prototypes vs. without prototypes as if
361ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // both did not have prototypes.
362ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (T1->getTypeClass() == Type::FunctionProto &&
363ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        T2->getTypeClass() == Type::FunctionNoProto)
364ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      TC = Type::FunctionNoProto;
365ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    else if (T1->getTypeClass() == Type::FunctionNoProto &&
366ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor             T2->getTypeClass() == Type::FunctionProto)
367ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      TC = Type::FunctionNoProto;
368ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    else
369ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      return false;
370ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  }
37173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
372ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  switch (TC) {
373ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  case Type::Builtin:
37473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Deal with Char_S/Char_U.
37573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
37673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
37773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
37873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
37973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Complex:
38073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
38173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<ComplexType>(T1)->getElementType(),
38273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<ComplexType>(T2)->getElementType()))
38373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
38473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
38573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
38673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Pointer:
38773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
38873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<PointerType>(T1)->getPointeeType(),
38973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<PointerType>(T2)->getPointeeType()))
39073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
39173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
39273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
39373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::BlockPointer:
39473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
39573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<BlockPointerType>(T1)->getPointeeType(),
39673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<BlockPointerType>(T2)->getPointeeType()))
39773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
39873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
39973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
40073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::LValueReference:
40173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::RValueReference: {
40273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ReferenceType *Ref1 = cast<ReferenceType>(T1);
40373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ReferenceType *Ref2 = cast<ReferenceType>(T2);
40473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
40573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
40673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Ref1->isInnerRef() != Ref2->isInnerRef())
40773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
40873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
40973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ref1->getPointeeTypeAsWritten(),
41073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ref2->getPointeeTypeAsWritten()))
41173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
41273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
41373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
41473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
41573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::MemberPointer: {
41673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
41773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
41873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
41973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  MemPtr1->getPointeeType(),
42073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  MemPtr2->getPointeeType()))
42173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
42273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
42373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(MemPtr1->getClass(), 0),
42473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(MemPtr2->getClass(), 0)))
42573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
42673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
42773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
42873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
42973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ConstantArray: {
43073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
43173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
43273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsSameValue(Array1->getSize(), Array2->getSize()))
43373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
43473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
43573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
43673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
43773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
43873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
43973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
44073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::IncompleteArray:
44173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context,
44273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                       cast<ArrayType>(T1),
44373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                       cast<ArrayType>(T2)))
44473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
44573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
44673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
44773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::VariableArray: {
44873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
44973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
45073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
45173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Array1->getSizeExpr(), Array2->getSizeExpr()))
45273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
45373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
45473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
45573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
45673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
45773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
45873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
45973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
46073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::DependentSizedArray: {
46173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
46273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
46373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
46473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Array1->getSizeExpr(), Array2->getSizeExpr()))
46573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
46773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
46873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
47073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
47173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
47273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
47373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::DependentSizedExtVector: {
47473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedExtVectorType *Vec1
47573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<DependentSizedExtVectorType>(T1);
47673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedExtVectorType *Vec2
47773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<DependentSizedExtVectorType>(T2);
47873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
47973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getSizeExpr(), Vec2->getSizeExpr()))
48073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
48173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
48273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getElementType(),
48373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec2->getElementType()))
48473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
48573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
48673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
48773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
48873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Vector:
48973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ExtVector: {
49073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VectorType *Vec1 = cast<VectorType>(T1);
49173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VectorType *Vec2 = cast<VectorType>(T2);
49273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
49373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getElementType(),
49473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec2->getElementType()))
49573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
49673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Vec1->getNumElements() != Vec2->getNumElements())
49773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
498e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    if (Vec1->getVectorKind() != Vec2->getVectorKind())
49973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
5000e12b44081c6395a6d60a05a85a6012f7bb23b16Douglas Gregor    break;
50173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
50273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
50373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::FunctionProto: {
50473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
50573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
50673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getNumArgs() != Proto2->getNumArgs())
50773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
50873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
50973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
51073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto1->getArgType(I),
51173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto2->getArgType(I)))
51273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
51373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
51473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->isVariadic() != Proto2->isVariadic())
51573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
51673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
51773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
51873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
51973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
52073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
52173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
52273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
52373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
52473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto1->getExceptionType(I),
52573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto2->getExceptionType(I)))
52673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
52773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
52873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
52973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
53073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
53173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Fall through to check the bits common with FunctionNoProtoType.
53273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
53373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
53473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::FunctionNoProto: {
53573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionType *Function1 = cast<FunctionType>(T1);
53673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionType *Function2 = cast<FunctionType>(T2);
53773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
53873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Function1->getResultType(),
53973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Function2->getResultType()))
54073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
541264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      if (Function1->getExtInfo() != Function2->getExtInfo())
542264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola        return false;
54373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
54473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
54573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
54673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::UnresolvedUsing:
54773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
54873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<UnresolvedUsingType>(T1)->getDecl(),
54973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<UnresolvedUsingType>(T2)->getDecl()))
55073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
55173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
55273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
55373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
55473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Typedef:
55573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
55673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypedefType>(T1)->getDecl(),
55773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypedefType>(T2)->getDecl()))
55873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
55973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
56073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
56173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TypeOfExpr:
56273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
56373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
56473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
56573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
56673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
56773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
56873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TypeOf:
56973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
57073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypeOfType>(T1)->getUnderlyingType(),
57173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypeOfType>(T2)->getUnderlyingType()))
57273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
57373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
57473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
57573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Decltype:
57673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
57773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<DecltypeType>(T1)->getUnderlyingExpr(),
57873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<DecltypeType>(T2)->getUnderlyingExpr()))
57973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
58073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
58173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
58273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Record:
58373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Enum:
58473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
58573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TagType>(T1)->getDecl(),
58673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TagType>(T2)->getDecl()))
58773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
58873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
589465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
59073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TemplateTypeParm: {
59173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
59273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
59373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->getDepth() != Parm2->getDepth())
59473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
59573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->getIndex() != Parm2->getIndex())
59673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
59773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->isParameterPack() != Parm2->isParameterPack())
59873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
59973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
60073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Names of template type parameters are never significant.
60173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
60273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
60373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
60473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::SubstTemplateTypeParm: {
60573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const SubstTemplateTypeParmType *Subst1
60673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<SubstTemplateTypeParmType>(T1);
60773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const SubstTemplateTypeParmType *Subst2
60873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<SubstTemplateTypeParmType>(T2);
60973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
61073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Subst1->getReplacedParameter(), 0),
61173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Subst2->getReplacedParameter(), 0)))
61273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
61373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
61473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Subst1->getReplacementType(),
61573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Subst2->getReplacementType()))
61673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
61773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
61873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
61973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
62073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TemplateSpecialization: {
62173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateSpecializationType *Spec1
62273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<TemplateSpecializationType>(T1);
62373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateSpecializationType *Spec2
62473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<TemplateSpecializationType>(T2);
62573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
62673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Spec1->getTemplateName(),
62773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Spec2->getTemplateName()))
62873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
62973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Spec1->getNumArgs() != Spec2->getNumArgs())
63073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
63173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
63273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
63373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Spec1->getArg(I), Spec2->getArg(I)))
63473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
63573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
63673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
63773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
63873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
639465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Type::Elaborated: {
640465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
641465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
642465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    // CHECKME: what if a keyword is ETK_None or ETK_typename ?
643465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Elab1->getKeyword() != Elab2->getKeyword())
644465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return false;
64573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
646465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab1->getQualifier(),
647465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab2->getQualifier()))
64873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
64973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
650465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab1->getNamedType(),
651465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab2->getNamedType()))
65273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
65373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
65473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
65573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
6563cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  case Type::InjectedClassName: {
6573cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
6583cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
6593cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (!IsStructurallyEquivalent(Context,
66031f17ecbef57b5679c017c375db330546b7b5145John McCall                                  Inj1->getInjectedSpecializationType(),
66131f17ecbef57b5679c017c375db330546b7b5145John McCall                                  Inj2->getInjectedSpecializationType()))
6623cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      return false;
6633cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    break;
6643cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
6653cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
6664714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  case Type::DependentName: {
6674714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    const DependentNameType *Typename1 = cast<DependentNameType>(T1);
6684714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    const DependentNameType *Typename2 = cast<DependentNameType>(T2);
66973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
67073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename1->getQualifier(),
67173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename2->getQualifier()))
67273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
67373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
67473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename2->getIdentifier()))
67573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
67673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
67773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
67873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
67973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
68033500955d731c73717af52088b7fc0e7a85681e7John McCall  case Type::DependentTemplateSpecialization: {
68133500955d731c73717af52088b7fc0e7a85681e7John McCall    const DependentTemplateSpecializationType *Spec1 =
68233500955d731c73717af52088b7fc0e7a85681e7John McCall      cast<DependentTemplateSpecializationType>(T1);
68333500955d731c73717af52088b7fc0e7a85681e7John McCall    const DependentTemplateSpecializationType *Spec2 =
68433500955d731c73717af52088b7fc0e7a85681e7John McCall      cast<DependentTemplateSpecializationType>(T2);
68533500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!IsStructurallyEquivalent(Context,
68633500955d731c73717af52088b7fc0e7a85681e7John McCall                                  Spec1->getQualifier(),
68733500955d731c73717af52088b7fc0e7a85681e7John McCall                                  Spec2->getQualifier()))
68833500955d731c73717af52088b7fc0e7a85681e7John McCall      return false;
68933500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
69033500955d731c73717af52088b7fc0e7a85681e7John McCall                                  Spec2->getIdentifier()))
69133500955d731c73717af52088b7fc0e7a85681e7John McCall      return false;
69233500955d731c73717af52088b7fc0e7a85681e7John McCall    if (Spec1->getNumArgs() != Spec2->getNumArgs())
69333500955d731c73717af52088b7fc0e7a85681e7John McCall      return false;
69433500955d731c73717af52088b7fc0e7a85681e7John McCall    for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
69533500955d731c73717af52088b7fc0e7a85681e7John McCall      if (!IsStructurallyEquivalent(Context,
69633500955d731c73717af52088b7fc0e7a85681e7John McCall                                    Spec1->getArg(I), Spec2->getArg(I)))
69733500955d731c73717af52088b7fc0e7a85681e7John McCall        return false;
69833500955d731c73717af52088b7fc0e7a85681e7John McCall    }
69933500955d731c73717af52088b7fc0e7a85681e7John McCall    break;
70033500955d731c73717af52088b7fc0e7a85681e7John McCall  }
70133500955d731c73717af52088b7fc0e7a85681e7John McCall
70273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ObjCInterface: {
70373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
70473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
70573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
70673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Iface1->getDecl(), Iface2->getDecl()))
70773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
708c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    break;
709c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  }
710c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
711c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject: {
712c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
713c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
714c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (!IsStructurallyEquivalent(Context,
715c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                  Obj1->getBaseType(),
716c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                  Obj2->getBaseType()))
717c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return false;
718c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
71973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
720c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
72173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
722c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Obj1->getProtocol(I),
723c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Obj2->getProtocol(I)))
72473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
72573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
72673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
72773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
72873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
72973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ObjCObjectPointer: {
73073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
73173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
73273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
73373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ptr1->getPointeeType(),
73473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ptr2->getPointeeType()))
73573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
73673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
73773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
73873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
73973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  } // end switch
74073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
74173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
74273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
74373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
74473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two records.
74573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
74673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     RecordDecl *D1, RecordDecl *D2) {
74773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (D1->isUnion() != D2->isUnion()) {
74873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
74973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
75073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
75173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << D1->getDeclName() << (unsigned)D1->getTagKind();
75273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
75373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
75473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
755d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // If both declarations are class template specializations, we know
756d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // the ODR applies, so check the template and template arguments.
757d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateSpecializationDecl *Spec1
758d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = dyn_cast<ClassTemplateSpecializationDecl>(D1);
759d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateSpecializationDecl *Spec2
760d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = dyn_cast<ClassTemplateSpecializationDecl>(D2);
761d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (Spec1 && Spec2) {
762d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Check that the specialized templates are the same.
763d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
764d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                  Spec2->getSpecializedTemplate()))
765d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return false;
766d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
767d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Check that the template arguments are the same.
768d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
769d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return false;
770d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
771d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
772d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (!IsStructurallyEquivalent(Context,
773d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Spec1->getTemplateArgs().get(I),
774d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Spec2->getTemplateArgs().get(I)))
775d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return false;
776d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
777d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // If one is a class template specialization and the other is not, these
778d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // structures are diferent.
779d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  else if (Spec1 || Spec2)
780d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return false;
781d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
782ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Compare the definitions of these two records. If either or both are
783ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // incomplete, we assume that they are equivalent.
784ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  D1 = D1->getDefinition();
785ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  D2 = D2->getDefinition();
786ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (!D1 || !D2)
787ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return true;
788ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
78973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
79073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
79173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
79273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
793040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          << Context.C2.getTypeDeclType(D2);
79473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
795040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          << D2CXX->getNumBases();
79673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
797040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          << D1CXX->getNumBases();
79873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
79973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
80073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
80173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      // Check the base classes.
80273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
80373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                           BaseEnd1 = D1CXX->bases_end(),
80473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                Base2 = D2CXX->bases_begin();
80573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor           Base1 != BaseEnd1;
80673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor           ++Base1, ++Base2) {
80773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!IsStructurallyEquivalent(Context,
80873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                      Base1->getType(), Base2->getType())) {
80973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
81073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Context.C2.getTypeDeclType(D2);
81173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
81273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->getType()
81373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->getSourceRange();
81473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
81573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getType()
81673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getSourceRange();
81773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          return false;
81873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        }
81973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
82073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check virtual vs. non-virtual inheritance mismatch.
82173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (Base1->isVirtual() != Base2->isVirtual()) {
82273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
82373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Context.C2.getTypeDeclType(D2);
82473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(Base2->getSourceRange().getBegin(),
82573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                        diag::note_odr_virtual_base)
82673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->isVirtual() << Base2->getSourceRange();
82773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
82873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->isVirtual()
82973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getSourceRange();
83073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          return false;
83173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        }
83273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
83373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    } else if (D1CXX->getNumBases() > 0) {
83473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
83573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
83673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
83773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
83873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Base1->getType()
83973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Base1->getSourceRange();
84073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
84173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
84273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
84373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
84473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
84573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Check the fields for consistency.
84673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
84773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                             Field2End = D2->field_end();
84873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
84973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Field1End = D1->field_end();
85073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       Field1 != Field1End;
85173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       ++Field1, ++Field2) {
85273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field2 == Field2End) {
85373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
85473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
85573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Field1->getLocation(), diag::note_odr_field)
85673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName() << Field1->getType();
85773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
85873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
85973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
86073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
86173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
86273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Field1->getType(), Field2->getType())) {
86373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
86473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
86573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(Field2->getLocation(), diag::note_odr_field)
86673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field2->getDeclName() << Field2->getType();
86773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Field1->getLocation(), diag::note_odr_field)
86873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName() << Field1->getType();
86973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
87073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
87173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
87273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field1->isBitField() != Field2->isBitField()) {
87373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
87473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
87573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (Field1->isBitField()) {
87673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        llvm::APSInt Bits;
87773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
87873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
87973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field1->getDeclName() << Field1->getType()
88073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits.toString(10, false);
88173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
88273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName();
88373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
88473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        llvm::APSInt Bits;
88573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
88673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
88773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName() << Field2->getType()
88873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits.toString(10, false);
88973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(),
89073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                          diag::note_odr_not_bit_field)
89173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName();
89273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
89373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
89473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
89573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
89673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field1->isBitField()) {
89773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      // Make sure that the bit-fields are the same length.
89873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      llvm::APSInt Bits1, Bits2;
89973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
90073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
90173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
90273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
90373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
90473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsSameValue(Bits1, Bits2)) {
90573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
90673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Context.C2.getTypeDeclType(D2);
90773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
90873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName() << Field2->getType()
90973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits2.toString(10, false);
91073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
91173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field1->getDeclName() << Field1->getType()
91273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits1.toString(10, false);
91373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
91473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
91573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
91673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
91773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
91873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Field2 != Field2End) {
91973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
92073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
92173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(Field2->getLocation(), diag::note_odr_field)
92273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Field2->getDeclName() << Field2->getType();
92373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
92473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
92573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
92673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
92773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
92873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
92973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
93073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two enums.
93173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
93273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     EnumDecl *D1, EnumDecl *D2) {
93373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
93473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                             EC2End = D2->enumerator_end();
93573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
93673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  EC1End = D1->enumerator_end();
93773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       EC1 != EC1End; ++EC1, ++EC2) {
93873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (EC2 == EC2End) {
93973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
94073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
94173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
94273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getDeclName()
94373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getInitVal().toString(10);
94473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
94573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
94673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
94773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
94873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::APSInt Val1 = EC1->getInitVal();
94973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::APSInt Val2 = EC2->getInitVal();
95073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsSameValue(Val1, Val2) ||
95173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
95273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
95373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
95473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
95573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC2->getDeclName()
95673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC2->getInitVal().toString(10);
95773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
95873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getDeclName()
95973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getInitVal().toString(10);
96073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
96173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
96273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
96373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
96473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (EC2 != EC2End) {
96573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
96673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
96773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
96873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << EC2->getDeclName()
96973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << EC2->getInitVal().toString(10);
97073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
97173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
97273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
97373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
97473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
97573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
976040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
977040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
978040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateParameterList *Params1,
979040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateParameterList *Params2) {
980040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (Params1->size() != Params2->size()) {
981040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(Params2->getTemplateLoc(),
982040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                  diag::err_odr_different_num_template_parameters)
983040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << Params1->size() << Params2->size();
984040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(Params1->getTemplateLoc(),
985040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                  diag::note_odr_template_parameter_list);
986040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
987040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
988040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
989040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
990040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
991040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Context.Diag2(Params2->getParam(I)->getLocation(),
992040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                    diag::err_odr_different_template_parameter_kind);
993040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Context.Diag1(Params1->getParam(I)->getLocation(),
994040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                    diag::note_odr_template_parameter_here);
995040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return false;
996040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
997040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
998040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
999040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          Params2->getParam(I))) {
1000040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1001040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return false;
1002040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
1003040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1004040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1005040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return true;
1006040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1007040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1008040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1009040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTypeParmDecl *D1,
1010040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTypeParmDecl *D2) {
1011040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D1->isParameterPack() != D2->isParameterPack()) {
1012040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1013040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D2->isParameterPack();
1014040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1015040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D1->isParameterPack();
1016040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1017040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1018040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1019040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return true;
1020040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1021040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1022040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1023040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     NonTypeTemplateParmDecl *D1,
1024040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     NonTypeTemplateParmDecl *D2) {
1025040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Enable once we have variadic templates.
1026040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#if 0
1027040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D1->isParameterPack() != D2->isParameterPack()) {
1028040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1029040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D2->isParameterPack();
1030040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1031040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D1->isParameterPack();
1032040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1033040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1034040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#endif
1035040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1036040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check types.
1037040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1038040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(),
1039040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                  diag::err_odr_non_type_parameter_type_inconsistent)
1040040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D2->getType() << D1->getType();
1041040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1042040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D1->getType();
1043040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1044040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1045040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1046040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return true;
1047040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1048040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1049040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1050040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTemplateParmDecl *D1,
1051040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTemplateParmDecl *D2) {
1052040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Enable once we have variadic templates.
1053040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#if 0
1054040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D1->isParameterPack() != D2->isParameterPack()) {
1055040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1056040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    << D2->isParameterPack();
1057040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1058040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    << D1->isParameterPack();
1059040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1060040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1061040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#endif
1062040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1063040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check template parameter lists.
1064040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1065040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                  D2->getTemplateParameters());
1066040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1067040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1068040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1069040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     ClassTemplateDecl *D1,
1070040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     ClassTemplateDecl *D2) {
1071040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check template parameters.
1072040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!IsStructurallyEquivalent(Context,
1073040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                D1->getTemplateParameters(),
1074040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                D2->getTemplateParameters()))
1075040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
107673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
1077040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check the templated declaration.
1078040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1079040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          D2->getTemplatedDecl());
1080040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1081040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
108273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two declarations.
108373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
108473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Decl *D1, Decl *D2) {
108573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Check for known structural equivalences via a callback of some sort.
108673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
1087ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Check whether we already know that these two declarations are not
1088ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // structurally equivalent.
1089ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1090ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                      D2->getCanonicalDecl())))
1091ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return false;
1092ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
109373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Determine whether we've already produced a tentative equivalence for D1.
109473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
109573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (EquivToD1)
109673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return EquivToD1 == D2->getCanonicalDecl();
109773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
109873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Produce a tentative equivalence D1 <-> D2, which will be checked later.
109973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EquivToD1 = D2->getCanonicalDecl();
110073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
110173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
110273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
110373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
110473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
110573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                            Decl *D2) {
110673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!::IsStructurallyEquivalent(*this, D1, D2))
110773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
110873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
110973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return !Finish();
111073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
111173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
111273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
111373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                            QualType T2) {
111473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!::IsStructurallyEquivalent(*this, T1, T2))
111573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
111673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
111773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return !Finish();
111873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
111973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
112073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::Finish() {
112173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  while (!DeclsToCheck.empty()) {
112273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Check the next declaration.
112373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Decl *D1 = DeclsToCheck.front();
112473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DeclsToCheck.pop_front();
112573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
112673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Decl *D2 = TentativeEquivalences[D1];
112773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    assert(D2 && "Unrecorded tentative equivalence?");
112873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
1129ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    bool Equivalent = true;
1130ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
113173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Switch on all declaration kinds. For now, we're just going to
113273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // check the obvious ones.
113373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
113473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
113573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check for equivalent structure names.
113673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name1 = Record1->getIdentifier();
113773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name1 && Record1->getTypedefForAnonDecl())
113873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
113973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name2 = Record2->getIdentifier();
114073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name2 && Record2->getTypedefForAnonDecl())
114173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
1142ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (!::IsStructurallyEquivalent(Name1, Name2) ||
1143ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this, Record1, Record2))
1144ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
114573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
114673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Record/non-record mismatch.
1147ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
114873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
1149ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
115073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
115173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check for equivalent enum names.
115273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name1 = Enum1->getIdentifier();
115373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name1 && Enum1->getTypedefForAnonDecl())
115473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
115573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name2 = Enum2->getIdentifier();
115673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name2 && Enum2->getTypedefForAnonDecl())
115773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
1158ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (!::IsStructurallyEquivalent(Name1, Name2) ||
1159ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1160ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
116173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
116273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Enum/non-enum mismatch
1163ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
116473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
1165ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
116673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
116773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
1168ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                        Typedef2->getIdentifier()) ||
1169ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this,
117073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        Typedef1->getUnderlyingType(),
117173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        Typedef2->getUnderlyingType()))
1172ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
117373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
117473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Typedef/non-typedef mismatch.
1175ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
117673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
1177040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (ClassTemplateDecl *ClassTemplate1
1178040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                           = dyn_cast<ClassTemplateDecl>(D1)) {
1179040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1180040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1181040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                        ClassTemplate2->getIdentifier()) ||
1182040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor            !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1183040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1184040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1185040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Class template/non-class-template mismatch.
1186040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1187040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1188040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1189040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1190040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1191040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1192040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1193040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Kind mismatch.
1194040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1195040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1196040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (NonTypeTemplateParmDecl *NTTP1
1197040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1198040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (NonTypeTemplateParmDecl *NTTP2
1199040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1200040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1201040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1202040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1203040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Kind mismatch.
1204040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1205040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1206040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (TemplateTemplateParmDecl *TTP1
1207040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                  = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1208040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (TemplateTemplateParmDecl *TTP2
1209040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                    = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1210040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1211040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1212040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1213040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Kind mismatch.
1214040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1215040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1216040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
1217040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1218ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (!Equivalent) {
1219ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      // Note that these two declarations are not equivalent (and we already
1220ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      // know about it).
1221ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1222ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                               D2->getCanonicalDecl()));
1223ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      return true;
1224ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    }
122573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Check other declaration kinds!
122673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
122773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
122873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return false;
122973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
123073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
123173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor//----------------------------------------------------------------------------
12321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor// Import Types
12331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//----------------------------------------------------------------------------
12341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
123589cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas GregorQualType ASTNodeImporter::VisitType(Type *T) {
123689cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor  Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
123789cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    << T->getTypeClassName();
123889cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor  return QualType();
123989cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor}
124089cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor
12411b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
12421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  switch (T->getKind()) {
12431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Void: return Importer.getToContext().VoidTy;
12441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Bool: return Importer.getToContext().BoolTy;
12451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char_U:
12471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // The context we're importing from has an unsigned 'char'. If we're
12481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // importing into a context with a signed 'char', translate to
12491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // 'unsigned char' instead.
12501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (Importer.getToContext().getLangOptions().CharIsSigned)
12511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return Importer.getToContext().UnsignedCharTy;
12521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().CharTy;
12541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
12561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char16:
12581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++!
12591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().Char16Ty;
12601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char32:
12621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++!
12631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().Char32Ty;
12641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
12661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
12671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
12681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ULongLong:
12691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().UnsignedLongLongTy;
12701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
12711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char_S:
12731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // The context we're importing from has an unsigned 'char'. If we're
12741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // importing into a context with a signed 'char', translate to
12751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // 'unsigned char' instead.
12761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Importer.getToContext().getLangOptions().CharIsSigned)
12771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return Importer.getToContext().SignedCharTy;
12781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().CharTy;
12801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
12821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::WChar:
12831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: If not in C++, shall we translate to the C equivalent of
12841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // wchar_t?
12851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().WCharTy;
12861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Short : return Importer.getToContext().ShortTy;
12881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Int : return Importer.getToContext().IntTy;
12891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Long : return Importer.getToContext().LongTy;
12901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
12911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
12921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Float: return Importer.getToContext().FloatTy;
12931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Double: return Importer.getToContext().DoubleTy;
12941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
12951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
12961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::NullPtr:
12971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++0x!
12981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().NullPtrTy;
12991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
13011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
13021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UndeducedAuto:
13031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++0x!
13041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().UndeducedAutoTy;
13051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCId:
13071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports Objective-C!
13081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinIdTy;
13091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCClass:
13111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinClassTy;
13121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCSel:
13141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinSelTy;
13151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
13161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return QualType();
13181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13201b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
13211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
13221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
13231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getComplexType(ToElementType);
13261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13281b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitPointerType(PointerType *T) {
13291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
13301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
13311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getPointerType(ToPointeeType);
13341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13361b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
13371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for blocks support in "to" context.
13381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
13391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
13401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getBlockPointerType(ToPointeeType);
13431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13451b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
13461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++ support in "to" context.
13471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
13481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
13491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getLValueReferenceType(ToPointeeType);
13521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13541b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
13551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++0x support in "to" context.
13561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
13571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
13581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getRValueReferenceType(ToPointeeType);
13611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13631b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
13641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++ support in "to" context.
13651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
13661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
13671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
13701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getMemberPointerType(ToPointeeType,
13711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      ClassType.getTypePtr());
13721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13741b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
13751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
13761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
13771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getConstantArrayType(ToElementType,
13801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSize(),
13811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSizeModifier(),
13821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->getIndexTypeCVRQualifiers());
13831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13851b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
13861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
13871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
13881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getIncompleteArrayType(ToElementType,
13911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                        T->getSizeModifier(),
13921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                T->getIndexTypeCVRQualifiers());
13931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13951b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
13961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
13971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
13981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *Size = Importer.Import(T->getSizeExpr());
14011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!Size)
14021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  SourceRange Brackets = Importer.Import(T->getBracketsRange());
14051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getVariableArrayType(ToElementType, Size,
14061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSizeModifier(),
14071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                T->getIndexTypeCVRQualifiers(),
14081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      Brackets);
14091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14111b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitVectorType(VectorType *T) {
14121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getVectorType(ToElementType,
14171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->getNumElements(),
1418e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                               T->getVectorKind());
14191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14211b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
14221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getExtVectorType(ToElementType,
14271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                  T->getNumElements());
14281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14301b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
14311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: What happens if we're importing a function without a prototype
14321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // into C++? Should we make it variadic?
14331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToResultType = Importer.Import(T->getResultType());
14341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToResultType.isNull())
14351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
1436264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola
14371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getFunctionNoProtoType(ToResultType,
1438264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                        T->getExtInfo());
14391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14411b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
14421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToResultType = Importer.Import(T->getResultType());
14431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToResultType.isNull())
14441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Import argument types
14471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<QualType, 4> ArgTypes;
14481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
14491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                         AEnd = T->arg_type_end();
14501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       A != AEnd; ++A) {
14511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType ArgType = Importer.Import(*A);
14521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (ArgType.isNull())
14531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
14541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ArgTypes.push_back(ArgType);
14551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
14561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Import exception types
14581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<QualType, 4> ExceptionTypes;
14591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (FunctionProtoType::exception_iterator E = T->exception_begin(),
14601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                          EEnd = T->exception_end();
14611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       E != EEnd; ++E) {
14621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType ExceptionType = Importer.Import(*E);
14631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (ExceptionType.isNull())
14641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
14651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ExceptionTypes.push_back(ExceptionType);
14661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
14671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
14691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 ArgTypes.size(),
14701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->isVariadic(),
14711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->getTypeQuals(),
14721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->hasExceptionSpec(),
14731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 T->hasAnyExceptionSpec(),
14741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 ExceptionTypes.size(),
14751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                 ExceptionTypes.data(),
1476264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                 T->getExtInfo());
14771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14791b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
14801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  TypedefDecl *ToDecl
14811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
14821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
14831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeDeclType(ToDecl);
14861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14881b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
14891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
14901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToExpr)
14911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeOfExprType(ToExpr);
14941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14961b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
14971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
14981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToUnderlyingType.isNull())
14991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeOfType(ToUnderlyingType);
15021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15041b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
15051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
15061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToExpr)
15071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getDecltypeType(ToExpr);
15101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15121b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitRecordType(RecordType *T) {
15131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  RecordDecl *ToDecl
15141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
15151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
15161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTagDeclType(ToDecl);
15191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15211b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitEnumType(EnumType *T) {
15221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  EnumDecl *ToDecl
15231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
15241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
15251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTagDeclType(ToDecl);
15281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1530d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorQualType ASTNodeImporter::VisitTemplateSpecializationType(
1531d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                TemplateSpecializationType *T) {
1532d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1533d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (ToTemplate.isNull())
1534d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return QualType();
1535d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1536d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs;
1537d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1538d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return QualType();
1539d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1540d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  QualType ToCanonType;
1541d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (!QualType(T, 0).isCanonical()) {
1542d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualType FromCanonType
1543d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1544d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ToCanonType =Importer.Import(FromCanonType);
1545d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToCanonType.isNull())
1546d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return QualType();
1547d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1548d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1549d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                         ToTemplateArgs.data(),
1550d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                         ToTemplateArgs.size(),
1551d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                               ToCanonType);
1552d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1553d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
15541b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
1555465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *ToQualifier = 0;
1556465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  // Note: the qualifier in an ElaboratedType is optional.
1557465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T->getQualifier()) {
1558465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ToQualifier = Importer.Import(T->getQualifier());
1559465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (!ToQualifier)
1560465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return QualType();
1561465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
15621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToNamedType = Importer.Import(T->getNamedType());
15641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToNamedType.isNull())
15651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1567465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Importer.getToContext().getElaboratedType(T->getKeyword(),
1568465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                                   ToQualifier, ToNamedType);
15691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15711b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
15721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  ObjCInterfaceDecl *Class
15731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
15741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!Class)
15751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1577c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCInterfaceType(Class);
1578c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
1579c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
1580c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) {
1581c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  QualType ToBaseType = Importer.Import(T->getBaseType());
1582c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ToBaseType.isNull())
1583c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType();
1584c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
15851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
1586c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator P = T->qual_begin(),
15871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                     PEnd = T->qual_end();
15881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       P != PEnd; ++P) {
15891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ObjCProtocolDecl *Protocol
15901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
15911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Protocol)
15921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
15931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    Protocols.push_back(Protocol);
15941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
15951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1596c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCObjectType(ToBaseType,
1597c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                                   Protocols.data(),
1598c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                                   Protocols.size());
15991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
16001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
16011b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
16021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
16031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
16041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
16051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1606c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
16071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
16081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1609089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor//----------------------------------------------------------------------------
1610089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor// Import Declarations
1611089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor//----------------------------------------------------------------------------
1612a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregorbool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1613a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      DeclContext *&LexicalDC,
1614a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      DeclarationName &Name,
1615a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      SourceLocation &Loc) {
1616089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Import the context of this declaration.
1617a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DC = Importer.ImportContext(D->getDeclContext());
1618089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  if (!DC)
1619a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return true;
1620a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1621a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  LexicalDC = DC;
16229bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
16239bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
16249bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    if (!LexicalDC)
1625a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      return true;
16269bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  }
1627a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1628089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Import the name of this declaration.
1629a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Name = Importer.Import(D->getDeclName());
1630089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  if (D->getDeclName() && !Name)
1631a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return true;
1632a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1633a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the location of this declaration.
1634a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Loc = Importer.Import(D->getLocation());
1635a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  return false;
1636a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1637a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
16382577743c5650c646fb705df01403707e94f2df04Abramo Bagnaravoid
16392577743c5650c646fb705df01403707e94f2df04Abramo BagnaraASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
16402577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                          DeclarationNameInfo& To) {
16412577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  // NOTE: To.Name and To.Loc are already imported.
16422577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  // We only have to import To.LocInfo.
16432577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  switch (To.getName().getNameKind()) {
16442577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::Identifier:
16452577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::ObjCZeroArgSelector:
16462577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::ObjCOneArgSelector:
16472577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::ObjCMultiArgSelector:
16482577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXUsingDirective:
16492577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
16502577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
16512577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXOperatorName: {
16522577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    SourceRange Range = From.getCXXOperatorNameRange();
16532577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    To.setCXXOperatorNameRange(Importer.Import(Range));
16542577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
16552577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
16562577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXLiteralOperatorName: {
16572577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
16582577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
16592577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
16602577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
16612577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXConstructorName:
16622577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXDestructorName:
16632577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXConversionFunctionName: {
16642577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
16652577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    To.setNamedTypeInfo(Importer.Import(FromTInfo));
16662577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
16672577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
16682577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    assert(0 && "Unknown name kind.");
16692577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
16702577743c5650c646fb705df01403707e94f2df04Abramo Bagnara}
16712577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
1672083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregorvoid ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1673083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1674083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor                               FromEnd = FromDC->decls_end();
1675083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor       From != FromEnd;
1676083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor       ++From)
1677083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    Importer.Import(*From);
1678083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor}
1679083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor
1680d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregorbool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) {
1681d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (To->getDefinition())
1682d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return false;
1683d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1684d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  To->startDefinition();
1685d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1686d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Add base classes.
1687d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1688d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1689d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1690d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1691d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (CXXRecordDecl::base_class_iterator
1692d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                  Base1 = FromCXX->bases_begin(),
1693d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor            FromBaseEnd = FromCXX->bases_end();
1694d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor         Base1 != FromBaseEnd;
1695d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor         ++Base1) {
1696d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      QualType T = Importer.Import(Base1->getType());
1697d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (T.isNull())
1698c04d9d1be026cb201a716df1cd28a88878958beeDouglas Gregor        return true;
1699d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1700d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      Bases.push_back(
1701d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                    new (Importer.getToContext())
1702d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                      CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1703d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                       Base1->isVirtual(),
1704d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                       Base1->isBaseOfClass(),
1705d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                       Base1->getAccessSpecifierAsWritten(),
1706d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                       Importer.Import(Base1->getTypeSourceInfo())));
1707d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
1708d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Bases.empty())
1709d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      ToCXX->setBases(Bases.data(), Bases.size());
1710d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1711d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1712d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ImportDeclContext(From);
1713d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  To->completeDefinition();
1714c04d9d1be026cb201a716df1cd28a88878958beeDouglas Gregor  return false;
1715d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1716d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1717040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorTemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1718040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                TemplateParameterList *Params) {
1719040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  llvm::SmallVector<NamedDecl *, 4> ToParams;
1720040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  ToParams.reserve(Params->size());
1721040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  for (TemplateParameterList::iterator P = Params->begin(),
1722040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                    PEnd = Params->end();
1723040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor       P != PEnd; ++P) {
1724040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *To = Importer.Import(*P);
1725040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!To)
1726040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return 0;
1727040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1728040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    ToParams.push_back(cast<NamedDecl>(To));
1729040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1730040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1731040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return TemplateParameterList::Create(Importer.getToContext(),
1732040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       Importer.Import(Params->getTemplateLoc()),
1733040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       Importer.Import(Params->getLAngleLoc()),
1734040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       ToParams.data(), ToParams.size(),
1735040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       Importer.Import(Params->getRAngleLoc()));
1736040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1737040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1738d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorTemplateArgument
1739d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1740d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  switch (From.getKind()) {
1741d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Null:
1742d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument();
1743d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1744d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Type: {
1745d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualType ToType = Importer.Import(From.getAsType());
1746d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToType.isNull())
1747d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1748d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(ToType);
1749d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1750d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1751d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Integral: {
1752d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualType ToType = Importer.Import(From.getIntegralType());
1753d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToType.isNull())
1754d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1755d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(*From.getAsIntegral(), ToType);
1756d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1757d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1758d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Declaration:
1759d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Decl *To = Importer.Import(From.getAsDecl()))
1760d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument(To);
1761d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument();
1762d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1763d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Template: {
1764d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1765d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToTemplate.isNull())
1766d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1767d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1768d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(ToTemplate);
1769d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1770d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1771d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Expression:
1772d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1773d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument(ToExpr);
1774d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument();
1775d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1776d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Pack: {
1777d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    llvm::SmallVector<TemplateArgument, 2> ToPack;
1778d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ToPack.reserve(From.pack_size());
1779d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1780d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1781d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1782d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    TemplateArgument *ToArgs
1783d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1784d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1785d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(ToArgs, ToPack.size());
1786d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1787d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1788d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1789d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm_unreachable("Invalid template argument kind");
1790d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return TemplateArgument();
1791d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1792d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1793d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregorbool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1794d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                              unsigned NumFromArgs,
1795d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                              llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1796d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  for (unsigned I = 0; I != NumFromArgs; ++I) {
1797d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1798d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (To.isNull() && !FromArgs[I].isNull())
1799d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return true;
1800d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1801d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ToArgs.push_back(To);
1802d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1803d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1804d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return false;
1805d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1806d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
180796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregorbool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
180873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        RecordDecl *ToRecord) {
1809bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
181073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Importer.getToContext(),
1811ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getNonEquivalentDecls());
1812bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
181396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
181496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
181536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregorbool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
1816bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
181773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Importer.getToContext(),
1818ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getNonEquivalentDecls());
1819bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
182036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
182136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
1822040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorbool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1823040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                        ClassTemplateDecl *To) {
1824040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1825040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                   Importer.getToContext(),
1826040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                   Importer.getNonEquivalentDecls());
1827040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return Ctx.IsStructurallyEquivalent(From, To);
1828040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1829040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1830a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitDecl(Decl *D) {
1831a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1832a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    << D->getDeclKindName();
1833a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  return 0;
1834a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1835a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1836788c62d1e87bfb596078817237f672a5f000999aDouglas GregorDecl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1837788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  // Import the major distinguishing characteristics of this namespace.
1838788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  DeclContext *DC, *LexicalDC;
1839788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  DeclarationName Name;
1840788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  SourceLocation Loc;
1841788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1842788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    return 0;
1843788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1844788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  NamespaceDecl *MergeWithNamespace = 0;
1845788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (!Name) {
1846788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // This is an anonymous namespace. Adopt an existing anonymous
1847788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // namespace if we can.
1848788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // FIXME: Not testable.
1849788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1850788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      MergeWithNamespace = TU->getAnonymousNamespace();
1851788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    else
1852788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1853788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  } else {
1854788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1855788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1856788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor         Lookup.first != Lookup.second;
1857788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor         ++Lookup.first) {
18580d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
1859788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        continue;
1860788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1861788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1862788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        MergeWithNamespace = FoundNS;
1863788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        ConflictingDecls.clear();
1864788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        break;
1865788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      }
1866788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1867788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
1868788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
1869788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1870788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (!ConflictingDecls.empty()) {
18710d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
1872788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                         ConflictingDecls.data(),
1873788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                         ConflictingDecls.size());
1874788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
1875788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  }
1876788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1877788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  // Create the "to" namespace, if needed.
1878788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  NamespaceDecl *ToNamespace = MergeWithNamespace;
1879788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (!ToNamespace) {
1880788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1881788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                        Name.getAsIdentifierInfo());
1882788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    ToNamespace->setLexicalDeclContext(LexicalDC);
1883788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    LexicalDC->addDecl(ToNamespace);
1884788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1885788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // If this is an anonymous namespace, register it as the anonymous
1886788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // namespace within its context.
1887788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (!Name) {
1888788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1889788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        TU->setAnonymousNamespace(ToNamespace);
1890788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      else
1891788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1892788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
1893788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  }
1894788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  Importer.Imported(D, ToNamespace);
1895788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1896788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  ImportDeclContext(D);
1897788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1898788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  return ToNamespace;
1899788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor}
1900788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
19019e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas GregorDecl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
19029e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // Import the major distinguishing characteristics of this typedef.
19039e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  DeclContext *DC, *LexicalDC;
19049e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  DeclarationName Name;
19059e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  SourceLocation Loc;
19069e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
19079e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    return 0;
19089e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
19099e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // If this typedef is not in block scope, determine whether we've
19109e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // seen a typedef with the same name (that we can merge with) or any
19119e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // other entity by that name (which name lookup could conflict with).
19129e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  if (!DC->isFunctionOrMethod()) {
19139e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
19149e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
19159e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
19169e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor         Lookup.first != Lookup.second;
19179e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor         ++Lookup.first) {
19189e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
19199e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor        continue;
19209e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
1921ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1922ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                            FoundTypedef->getUnderlyingType()))
19235ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor          return Importer.Imported(D, FoundTypedef);
19249e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      }
19259e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
19269e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
19279e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    }
19289e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
19299e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    if (!ConflictingDecls.empty()) {
19309e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
19319e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                         ConflictingDecls.data(),
19329e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                         ConflictingDecls.size());
19339e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (!Name)
19349e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor        return 0;
19359e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    }
19369e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  }
19379e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
1938ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the underlying type of this typedef;
1939ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getUnderlyingType());
1940ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
1941ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
1942ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
19439e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // Create the new typedef node.
19449e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
19459e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
19469e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                               Loc, Name.getAsIdentifierInfo(),
19479e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                               TInfo);
1948325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToTypedef->setAccess(D->getAccess());
19499e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  ToTypedef->setLexicalDeclContext(LexicalDC);
19505ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToTypedef);
19519e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  LexicalDC->addDecl(ToTypedef);
1952ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
19539e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  return ToTypedef;
19549e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor}
19559e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
195636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorDecl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
195736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the major distinguishing characteristics of this enum.
195836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclContext *DC, *LexicalDC;
195936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName Name;
196036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  SourceLocation Loc;
196136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
196236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
196336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
196436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Figure out what enum name we're looking for.
196536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  unsigned IDNS = Decl::IDNS_Tag;
196636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName SearchName = Name;
196736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!SearchName && D->getTypedefForAnonDecl()) {
196836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
196936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    IDNS = Decl::IDNS_Ordinary;
197036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  } else if (Importer.getToContext().getLangOptions().CPlusPlus)
197136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    IDNS |= Decl::IDNS_Ordinary;
197236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
197336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // We may already have an enum of the same name; try to find and match it.
197436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!DC->isFunctionOrMethod() && SearchName) {
197536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
197636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
197736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         Lookup.first != Lookup.second;
197836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++Lookup.first) {
197936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
198036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        continue;
198136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
198236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Decl *Found = *Lookup.first;
198336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
198436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
198536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor          Found = Tag->getDecl();
198636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      }
198736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
198836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
19895ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor        if (IsStructuralMatch(D, FoundEnum))
19905ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor          return Importer.Imported(D, FoundEnum);
199136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      }
199236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
199336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
199436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
199536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
199636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (!ConflictingDecls.empty()) {
199736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
199836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.data(),
199936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.size());
200036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
200136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
200236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
200336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Create the enum declaration.
200473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
2005a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                  Name.getAsIdentifierInfo(),
2006a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                  Importer.Import(D->getTagKeywordLoc()), 0,
2007a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                  D->isScoped(), D->isScopedUsingClassTag(),
2008a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                  D->isFixed());
2009b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
2010b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (D->getQualifier()) {
2011b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2012b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2013b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    D2->setQualifierInfo(NNS, NNSRange);
2014b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2015325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  D2->setAccess(D->getAccess());
201673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  D2->setLexicalDeclContext(LexicalDC);
201773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Importer.Imported(D, D2);
201873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  LexicalDC->addDecl(D2);
201936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
202036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the integer type.
202136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  QualType ToIntegerType = Importer.Import(D->getIntegerType());
202236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (ToIntegerType.isNull())
202336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
202473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  D2->setIntegerType(ToIntegerType);
202536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
202636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the definition
202736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (D->isDefinition()) {
202836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
202936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (T.isNull())
203036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      return 0;
203136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
203236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    QualType ToPromotionType = Importer.Import(D->getPromotionType());
203336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (ToPromotionType.isNull())
203436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      return 0;
203536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
203673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->startDefinition();
2037083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    ImportDeclContext(D);
20381b5a618c59025898806160ed5e7f0ff5bb79e482John McCall
20391b5a618c59025898806160ed5e7f0ff5bb79e482John McCall    // FIXME: we might need to merge the number of positive or negative bits
20401b5a618c59025898806160ed5e7f0ff5bb79e482John McCall    // if the enumerator lists don't match.
20411b5a618c59025898806160ed5e7f0ff5bb79e482John McCall    D2->completeDefinition(T, ToPromotionType,
20421b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                           D->getNumPositiveBits(),
20431b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                           D->getNumNegativeBits());
204436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
204536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
204673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return D2;
204736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
204836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
204996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas GregorDecl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
205096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // If this record has a definition in the translation unit we're coming from,
205196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // but this particular declaration is not that definition, import the
205296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // definition and map to that.
2053952b017601f9c82b51119c3a1600f1312a833db9Douglas Gregor  TagDecl *Definition = D->getDefinition();
205496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (Definition && Definition != D) {
205596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *ImportedDef = Importer.Import(Definition);
20565ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor    if (!ImportedDef)
20575ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor      return 0;
20585ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
20595ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor    return Importer.Imported(D, ImportedDef);
206096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
206196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
206296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Import the major distinguishing characteristics of this record.
206396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclContext *DC, *LexicalDC;
206496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName Name;
206596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  SourceLocation Loc;
206696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
206796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
206896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
206996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Figure out what structure name we're looking for.
207096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  unsigned IDNS = Decl::IDNS_Tag;
207196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName SearchName = Name;
207296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!SearchName && D->getTypedefForAnonDecl()) {
207396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
207496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    IDNS = Decl::IDNS_Ordinary;
207596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  } else if (Importer.getToContext().getLangOptions().CPlusPlus)
207696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    IDNS |= Decl::IDNS_Ordinary;
207796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
207896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // We may already have a record of the same name; try to find and match it.
2079e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor  RecordDecl *AdoptDecl = 0;
208096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!DC->isFunctionOrMethod() && SearchName) {
208196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
208296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
208396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         Lookup.first != Lookup.second;
208496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         ++Lookup.first) {
208596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
208696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor        continue;
208796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
208896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Decl *Found = *Lookup.first;
208996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
209096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor        if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
209196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor          Found = Tag->getDecl();
209296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
209396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
209496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
2095e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2096e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2097e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // The record types structurally match, or the "from" translation
2098e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // unit only had a forward declaration anyway; call it the same
2099e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // function.
2100e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // FIXME: For C++, we should also merge methods here.
21015ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor            return Importer.Imported(D, FoundDef);
2102e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          }
2103e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        } else {
2104e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          // We have a forward declaration of this type, so adopt that forward
2105e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          // declaration rather than building a new one.
2106e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          AdoptDecl = FoundRecord;
2107e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          continue;
2108e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        }
210996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
211096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
211196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
211296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
211396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
211496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    if (!ConflictingDecls.empty()) {
211596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
211696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         ConflictingDecls.data(),
211796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         ConflictingDecls.size());
211896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
211996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
212096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
212196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Create the record declaration.
212273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  RecordDecl *D2 = AdoptDecl;
212373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!D2) {
21245250f27420386452a21692a6292c99ee7febdac4John McCall    if (isa<CXXRecordDecl>(D)) {
212573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2126e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   D->getTagKind(),
2127e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   DC, Loc,
2128e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   Name.getAsIdentifierInfo(),
212996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                        Importer.Import(D->getTagKeywordLoc()));
213073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      D2 = D2CXX;
2131325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor      D2->setAccess(D->getAccess());
2132e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor    } else {
213373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
2134e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                    DC, Loc,
2135e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                    Name.getAsIdentifierInfo(),
2136e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                    Importer.Import(D->getTagKeywordLoc()));
213796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
2138b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Import the qualifier, if any.
2139b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (D->getQualifier()) {
2140b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2141b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2142b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      D2->setQualifierInfo(NNS, NNSRange);
2143b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
214473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->setLexicalDeclContext(LexicalDC);
214573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    LexicalDC->addDecl(D2);
214696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
21475ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
214873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Importer.Imported(D, D2);
2149e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor
2150d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D->isDefinition() && ImportDefinition(D, D2))
2151d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
215296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
215373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return D2;
215496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
215596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
215636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorDecl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
215736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the major distinguishing characteristics of this enumerator.
215836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclContext *DC, *LexicalDC;
215936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName Name;
216036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  SourceLocation Loc;
2161ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
216236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
2163ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2164ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2165ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2166ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2167ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
216836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Determine whether there are any other declarations with the same name and
216936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // in the same context.
217036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!LexicalDC->isFunctionOrMethod()) {
217136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
217236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
217336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
217436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         Lookup.first != Lookup.second;
217536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++Lookup.first) {
217636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
217736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        continue;
217836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
217936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
218036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
218136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
218236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (!ConflictingDecls.empty()) {
218336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
218436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.data(),
218536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.size());
218636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!Name)
218736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        return 0;
218836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
218936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
219036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
219136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  Expr *Init = Importer.Import(D->getInitExpr());
219236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (D->getInitExpr() && !Init)
219336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
219436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
219536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  EnumConstantDecl *ToEnumerator
219636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
219736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                               Name.getAsIdentifierInfo(), T,
219836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                               Init, D->getInitVal());
2199325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToEnumerator->setAccess(D->getAccess());
220036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  ToEnumerator->setLexicalDeclContext(LexicalDC);
22015ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToEnumerator);
220236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  LexicalDC->addDecl(ToEnumerator);
220336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  return ToEnumerator;
220436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
220596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
2206a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2207a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the major distinguishing characteristics of this function.
2208a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC, *LexicalDC;
2209a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclarationName Name;
2210a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc;
2211ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2212089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    return 0;
22132577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
2214a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Try to find a function in our own ("to") context with the same name, same
2215a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // type, and in the same context as the function we're importing.
2216a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  if (!LexicalDC->isFunctionOrMethod()) {
2217a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2218a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
2219a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2220a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor         Lookup.first != Lookup.second;
2221a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor         ++Lookup.first) {
2222a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2223a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        continue;
2224a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2225a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2226a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        if (isExternalLinkage(FoundFunction->getLinkage()) &&
2227a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            isExternalLinkage(D->getLinkage())) {
2228ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          if (Importer.IsStructurallyEquivalent(D->getType(),
2229ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                FoundFunction->getType())) {
2230a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            // FIXME: Actually try to merge the body and other attributes.
22315ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor            return Importer.Imported(D, FoundFunction);
2232a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          }
2233a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2234a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // FIXME: Check for overloading more carefully, e.g., by boosting
2235a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Sema::IsOverload out to the AST library.
2236a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2237a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Function overloading is okay in C++.
2238a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          if (Importer.getToContext().getLangOptions().CPlusPlus)
2239a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            continue;
2240a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2241a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Complain about inconsistent function types.
2242a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
2243ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            << Name << D->getType() << FoundFunction->getType();
2244a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          Importer.ToDiag(FoundFunction->getLocation(),
2245a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                          diag::note_odr_value_here)
2246a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            << FoundFunction->getType();
2247a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        }
2248a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      }
2249a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2250a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
2251a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    }
2252a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2253a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    if (!ConflictingDecls.empty()) {
2254a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
2255a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                         ConflictingDecls.data(),
2256a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                         ConflictingDecls.size());
2257a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (!Name)
2258a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        return 0;
2259a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    }
2260a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
2261ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
22622577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo(Name, Loc);
22632577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  // Import additional name location/type info.
22642577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
22652577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
2266ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
2267ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2268ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2269ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2270a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2271a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the function parameters.
2272a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2273a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2274a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor       P != PEnd; ++P) {
2275a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2276a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    if (!ToP)
2277a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      return 0;
2278a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2279a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Parameters.push_back(ToP);
2280a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
2281a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2282a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Create the imported function.
2283a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2284c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  FunctionDecl *ToFunction = 0;
2285c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2286c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2287c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            cast<CXXRecordDecl>(DC),
22882577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                            NameInfo, T, TInfo,
2289c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            FromConstructor->isExplicit(),
2290c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            D->isInlineSpecified(),
2291c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            D->isImplicit());
2292c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else if (isa<CXXDestructorDecl>(D)) {
2293c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2294c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           cast<CXXRecordDecl>(DC),
2295b41d899a6023385c00a61eb9dd3e44db9dc7994eCraig Silverstein                                           NameInfo, T, TInfo,
2296c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isInlineSpecified(),
2297c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isImplicit());
2298c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else if (CXXConversionDecl *FromConversion
2299c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           = dyn_cast<CXXConversionDecl>(D)) {
2300c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2301c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           cast<CXXRecordDecl>(DC),
23022577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                           NameInfo, T, TInfo,
2303c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isInlineSpecified(),
2304c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           FromConversion->isExplicit());
23050629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor  } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
23060629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor    ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
23070629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       cast<CXXRecordDecl>(DC),
23080629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       NameInfo, T, TInfo,
23090629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       Method->isStatic(),
23100629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       Method->getStorageClassAsWritten(),
23110629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       Method->isInlineSpecified());
2312c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else {
23132577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
23142577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                      NameInfo, T, TInfo, D->getStorageClass(),
231516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                      D->getStorageClassAsWritten(),
2316c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                      D->isInlineSpecified(),
2317c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                      D->hasWrittenPrototype());
2318c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  }
2319b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2320b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
2321b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (D->getQualifier()) {
2322b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2323b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2324b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    ToFunction->setQualifierInfo(NNS, NNSRange);
2325b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2326325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToFunction->setAccess(D->getAccess());
2327c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToFunction->setLexicalDeclContext(LexicalDC);
2328c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Importer.Imported(D, ToFunction);
2329a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2330a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Set the parameters.
2331a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
2332c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Parameters[I]->setOwningFunction(ToFunction);
2333c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToFunction->addDecl(Parameters[I]);
2334a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
2335c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToFunction->setParams(Parameters.data(), Parameters.size());
2336a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2337a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // FIXME: Other bits to merge?
233881134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor
233981134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor  // Add this function to the lexical context.
234081134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor  LexicalDC->addDecl(ToFunction);
234181134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor
2342c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToFunction;
2343a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
2344a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2345c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2346c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitFunctionDecl(D);
2347c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2348c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
2349c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2350c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
2351c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2352c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
2353c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2354c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
2355c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2356c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
2357c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2358c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
2359c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2360c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
236196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas GregorDecl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
236296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Import the major distinguishing characteristics of a variable.
236396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclContext *DC, *LexicalDC;
236496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName Name;
236596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  SourceLocation Loc;
2366ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2367ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2368ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2369ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
2370ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2371ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
237296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
237396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
237496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
237596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  Expr *BitWidth = Importer.Import(D->getBitWidth());
237696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!BitWidth && D->getBitWidth())
237796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
237896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
237996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
238096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         Loc, Name.getAsIdentifierInfo(),
238196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         T, TInfo, BitWidth, D->isMutable());
2382325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToField->setAccess(D->getAccess());
238396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  ToField->setLexicalDeclContext(LexicalDC);
23845ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToField);
238596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  LexicalDC->addDecl(ToField);
238696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  return ToField;
238796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
238896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
238987c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetDecl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
239087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  // Import the major distinguishing characteristics of a variable.
239187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  DeclContext *DC, *LexicalDC;
239287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  DeclarationName Name;
239387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  SourceLocation Loc;
239487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
239587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    return 0;
239687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
239787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  // Import the type.
239887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  QualType T = Importer.Import(D->getType());
239987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (T.isNull())
240087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    return 0;
240187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
240287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  NamedDecl **NamedChain =
240387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
240487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
240587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  unsigned i = 0;
240687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
240787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       PE = D->chain_end(); PI != PE; ++PI) {
240887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    Decl* D = Importer.Import(*PI);
240987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    if (!D)
241087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet      return 0;
241187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    NamedChain[i++] = cast<NamedDecl>(D);
241287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  }
241387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
241487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
241587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                         Importer.getToContext(), DC,
241687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                         Loc, Name.getAsIdentifierInfo(), T,
241787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                         NamedChain, D->getChainingSize());
241887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  ToIndirectField->setAccess(D->getAccess());
241987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  ToIndirectField->setLexicalDeclContext(LexicalDC);
242087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  Importer.Imported(D, ToIndirectField);
242187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  LexicalDC->addDecl(ToIndirectField);
242287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return ToIndirectField;
242387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
242487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
24252e55e3af2f6d6c0509495357fade95105dd144cdDouglas GregorDecl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
24262e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Import the major distinguishing characteristics of an ivar.
24272e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  DeclContext *DC, *LexicalDC;
24282e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  DeclarationName Name;
24292e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  SourceLocation Loc;
24302e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
24312e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
24322e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
24332e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Determine whether we've already imported this ivar
24342e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
24352e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor       Lookup.first != Lookup.second;
24362e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor       ++Lookup.first) {
24372e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
24382e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (Importer.IsStructurallyEquivalent(D->getType(),
24392e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                            FoundIvar->getType())) {
24402e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.Imported(D, FoundIvar);
24412e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        return FoundIvar;
24422e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      }
24432e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
24442e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
24452e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << Name << D->getType() << FoundIvar->getType();
24462e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
24472e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << FoundIvar->getType();
24482e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      return 0;
24492e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    }
24502e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  }
24512e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
24522e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Import the type.
24532e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  QualType T = Importer.Import(D->getType());
24542e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (T.isNull())
24552e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
24562e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
24572e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
24582e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  Expr *BitWidth = Importer.Import(D->getBitWidth());
24592e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (!BitWidth && D->getBitWidth())
24602e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
24612e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
2462a06549226f45d5b72169a3d054415616dd1014a2Daniel Dunbar  ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2463a06549226f45d5b72169a3d054415616dd1014a2Daniel Dunbar                                              cast<ObjCContainerDecl>(DC),
24642e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              Loc, Name.getAsIdentifierInfo(),
24652e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              T, TInfo, D->getAccessControl(),
2466ac0021ba802e193e0f9f8207768c7862c7603bc0Fariborz Jahanian                                              BitWidth, D->getSynthesize());
24672e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  ToIvar->setLexicalDeclContext(LexicalDC);
24682e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  Importer.Imported(D, ToIvar);
24692e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  LexicalDC->addDecl(ToIvar);
24702e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  return ToIvar;
24712e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
24722e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor}
24732e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
2474a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2475a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the major distinguishing characteristics of a variable.
2476a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC, *LexicalDC;
2477a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclarationName Name;
2478a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc;
2479ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2480a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return 0;
2481089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2482089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Try to find a variable in our own ("to") context with the same name and
2483089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // in the same context as the variable we're importing.
24849bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (D->isFileVarDecl()) {
2485089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    VarDecl *MergeWithVar = 0;
2486089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2487089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
24889bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2489089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor         Lookup.first != Lookup.second;
2490089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor         ++Lookup.first) {
2491089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2492089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        continue;
2493089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2494089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2495089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        // We have found a variable that we may need to merge with. Check it.
2496089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        if (isExternalLinkage(FoundVar->getLinkage()) &&
2497089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            isExternalLinkage(D->getLinkage())) {
2498ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          if (Importer.IsStructurallyEquivalent(D->getType(),
2499ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                FoundVar->getType())) {
2500089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            MergeWithVar = FoundVar;
2501089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            break;
2502089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          }
2503089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2504d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          const ArrayType *FoundArray
2505d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            = Importer.getToContext().getAsArrayType(FoundVar->getType());
2506d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          const ArrayType *TArray
2507ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            = Importer.getToContext().getAsArrayType(D->getType());
2508d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          if (FoundArray && TArray) {
2509d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            if (isa<IncompleteArrayType>(FoundArray) &&
2510d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor                isa<ConstantArrayType>(TArray)) {
2511ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              // Import the type.
2512ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              QualType T = Importer.Import(D->getType());
2513ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              if (T.isNull())
2514ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                return 0;
2515ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2516d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              FoundVar->setType(T);
2517d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              MergeWithVar = FoundVar;
2518d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              break;
2519d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            } else if (isa<IncompleteArrayType>(TArray) &&
2520d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor                       isa<ConstantArrayType>(FoundArray)) {
2521d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              MergeWithVar = FoundVar;
2522d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              break;
25230f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor            }
25240f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor          }
25250f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor
2526089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
2527ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            << Name << D->getType() << FoundVar->getType();
2528089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2529089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            << FoundVar->getType();
2530089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        }
2531089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      }
2532089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2533089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
2534089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2535089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2536089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    if (MergeWithVar) {
2537089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      // An equivalent variable with external linkage has been found. Link
2538089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      // the two declarations, then merge them.
25395ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor      Importer.Imported(D, MergeWithVar);
2540089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2541089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (VarDecl *DDef = D->getDefinition()) {
2542089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2543089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(ExistingDef->getLocation(),
2544089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                          diag::err_odr_variable_multiple_def)
2545089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            << Name;
2546089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2547089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        } else {
2548089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Expr *Init = Importer.Import(DDef->getInit());
2549838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor          MergeWithVar->setInit(Init);
2550089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        }
2551089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      }
2552089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2553089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      return MergeWithVar;
2554089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2555089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2556089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    if (!ConflictingDecls.empty()) {
2557089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
2558089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                         ConflictingDecls.data(),
2559089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                         ConflictingDecls.size());
2560089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (!Name)
2561089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        return 0;
2562089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2563089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  }
256482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2565ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
2566ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2567ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2568ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2569ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2570089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Create the imported variable.
257182fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2572089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2573089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                   Name.getAsIdentifierInfo(), T, TInfo,
257416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClass(),
257516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten());
2576b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
2577b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (D->getQualifier()) {
2578b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2579b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2580b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    ToVar->setQualifierInfo(NNS, NNSRange);
2581b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2582325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToVar->setAccess(D->getAccess());
25839bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ToVar->setLexicalDeclContext(LexicalDC);
25845ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToVar);
25859bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  LexicalDC->addDecl(ToVar);
25869bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
2587089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Merge the initializer.
2588089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // FIXME: Can we really import any initializer? Alternatively, we could force
2589089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // ourselves to import every declaration of a variable and then only use
2590089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // getInit() here.
2591838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
2592089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2593089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // FIXME: Other bits to merge?
2594089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2595089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  return ToVar;
2596089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
2597089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
25982cd00932b9b5403047139ce8cfaa3ae47966f894Douglas GregorDecl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
25992cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Parameters are created in the translation unit's context, then moved
26002cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // into the function declaration's context afterward.
26012cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
26022cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
26032cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the name of this declaration.
26042cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
26052cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  if (D->getDeclName() && !Name)
26062cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    return 0;
26072cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
26082cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the location of this declaration.
26092cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
26102cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
26112cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the parameter's type.
26122cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  QualType T = Importer.Import(D->getType());
26132cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  if (T.isNull())
26142cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    return 0;
26152cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
26162cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Create the imported parameter.
26172cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  ImplicitParamDecl *ToParm
26182cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    = ImplicitParamDecl::Create(Importer.getToContext(), DC,
26192cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor                                Loc, Name.getAsIdentifierInfo(),
26202cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor                                T);
26212cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  return Importer.Imported(D, ToParm);
26222cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor}
26232cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
2624a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2625a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Parameters are created in the translation unit's context, then moved
2626a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // into the function declaration's context afterward.
2627a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2628a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
262982fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // Import the name of this declaration.
263082fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
263182fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (D->getDeclName() && !Name)
263282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
263382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2634a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the location of this declaration.
2635a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
2636a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2637a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the parameter's type.
2638a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  QualType T = Importer.Import(D->getType());
263982fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (T.isNull())
264082fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
264182fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2642a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Create the imported parameter.
2643a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2644a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2645a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            Loc, Name.getAsIdentifierInfo(),
2646a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            T, TInfo, D->getStorageClass(),
264716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                             D->getStorageClassAsWritten(),
2648a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            /*FIXME: Default argument*/ 0);
2649bf73b352acb7a2d041ce8b50171dd7f8e2b2c1bbJohn McCall  ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
26505ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  return Importer.Imported(D, ToParm);
2651a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
265282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2653c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas GregorDecl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2654c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the major distinguishing characteristics of a method.
2655c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  DeclContext *DC, *LexicalDC;
2656c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  DeclarationName Name;
2657c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  SourceLocation Loc;
2658c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2659c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return 0;
2660c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2661c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2662c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       Lookup.first != Lookup.second;
2663c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       ++Lookup.first) {
2664c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2665c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2666c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        continue;
2667c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2668c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check return types.
2669c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2670c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                             FoundMethod->getResultType())) {
2671c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2672c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name
2673c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->getResultType() << FoundMethod->getResultType();
2674c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2675c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2676c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2677c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2678c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2679c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2680c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check the number of parameters.
2681c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (D->param_size() != FoundMethod->param_size()) {
2682c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2683c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name
2684c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->param_size() << FoundMethod->param_size();
2685c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2686c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2687c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2688c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2689c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2690c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2691c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check parameter types.
2692c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2693c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor             PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2694c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor           P != PEnd; ++P, ++FoundP) {
2695c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2696c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                               (*FoundP)->getType())) {
2697c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          Importer.FromDiag((*P)->getLocation(),
2698c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                            diag::err_odr_objc_method_param_type_inconsistent)
2699c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << D->isInstanceMethod() << Name
2700c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << (*P)->getType() << (*FoundP)->getType();
2701c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2702c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << (*FoundP)->getType();
2703c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          return 0;
2704c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        }
2705c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2706c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2707c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check variadic/non-variadic.
2708c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check the number of parameters.
2709c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (D->isVariadic() != FoundMethod->isVariadic()) {
2710c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2711c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2712c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2713c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2714c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2715c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2716c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2717c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2718c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // FIXME: Any other bits we need to merge?
2719c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      return Importer.Imported(D, FoundMethod);
2720c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    }
2721c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2722c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2723c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the result type.
2724c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  QualType ResultTy = Importer.Import(D->getResultType());
2725c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (ResultTy.isNull())
2726c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return 0;
2727c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
27284bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor  TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
27294bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor
2730c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ObjCMethodDecl *ToMethod
2731c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    = ObjCMethodDecl::Create(Importer.getToContext(),
2732c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Loc,
2733c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Importer.Import(D->getLocEnd()),
2734c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Name.getObjCSelector(),
27354bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor                             ResultTy, ResultTInfo, DC,
2736c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isInstanceMethod(),
2737c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isVariadic(),
2738c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isSynthesized(),
27393fe104154dd2e8ffb351142d74f308938b5c99bfFariborz Jahanian                             D->isDefined(),
2740c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->getImplementationControl());
2741c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2742c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // FIXME: When we decide to merge method definitions, we'll need to
2743c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // deal with implicit parameters.
2744c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2745c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the parameters
2746c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2747c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2748c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                   FromPEnd = D->param_end();
2749c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       FromP != FromPEnd;
2750c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       ++FromP) {
2751c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2752c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    if (!ToP)
2753c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      return 0;
2754c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2755c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToParams.push_back(ToP);
2756c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2757c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2758c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Set the parameters.
2759c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2760c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToParams[I]->setOwningFunction(ToMethod);
2761c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToMethod->addDecl(ToParams[I]);
2762c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2763c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToMethod->setMethodParams(Importer.getToContext(),
27644ecb25fa94897b2c03510292acace710e5262ba5Fariborz Jahanian                            ToParams.data(), ToParams.size(),
27654ecb25fa94897b2c03510292acace710e5262ba5Fariborz Jahanian                            ToParams.size());
2766c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2767c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToMethod->setLexicalDeclContext(LexicalDC);
2768c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Importer.Imported(D, ToMethod);
2769c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  LexicalDC->addDecl(ToMethod);
2770c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToMethod;
2771c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor}
2772c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2773b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas GregorDecl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2774b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import the major distinguishing characteristics of a category.
2775b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  DeclContext *DC, *LexicalDC;
2776b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  DeclarationName Name;
2777b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  SourceLocation Loc;
2778b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2779b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    return 0;
2780b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2781b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCInterfaceDecl *ToInterface
2782b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2783b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (!ToInterface)
2784b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    return 0;
2785b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2786b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Determine if we've already encountered this category.
2787b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCCategoryDecl *MergeWithCategory
2788b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2789b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCCategoryDecl *ToCategory = MergeWithCategory;
2790b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (!ToCategory) {
2791b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2792b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Importer.Import(D->getAtLoc()),
2793b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Loc,
2794b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                       Importer.Import(D->getCategoryNameLoc()),
2795b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Name.getAsIdentifierInfo());
2796b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setLexicalDeclContext(LexicalDC);
2797b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    LexicalDC->addDecl(ToCategory);
2798b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Imported(D, ToCategory);
2799b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2800b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // Link this category into its class's category list.
2801b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setClassInterface(ToInterface);
2802b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->insertNextClassCategory();
2803b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2804b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // Import protocols
2805b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2806b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2807b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2808b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      = D->protocol_loc_begin();
2809b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2810b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          FromProtoEnd = D->protocol_end();
2811b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor         FromProto != FromProtoEnd;
2812b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor         ++FromProto, ++FromProtoLoc) {
2813b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      ObjCProtocolDecl *ToProto
2814b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2815b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      if (!ToProto)
2816b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor        return 0;
2817b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      Protocols.push_back(ToProto);
2818b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2819b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    }
2820b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2821b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
2822b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2823b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                ProtocolLocs.data(), Importer.getToContext());
2824b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2825b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  } else {
2826b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Imported(D, ToCategory);
2827b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  }
2828b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2829b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import all of the members of this category.
2830083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
2831b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2832b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // If we have an implementation, import it as well.
2833b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (D->getImplementation()) {
2834b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ObjCCategoryImplDecl *Impl
2835b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation()));
2836b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    if (!Impl)
2837b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      return 0;
2838b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2839b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setImplementation(Impl);
2840b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  }
2841b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2842b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  return ToCategory;
2843b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor}
2844b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
28452e2a400383c7a64a927f61eeed596b08928e1d4bDouglas GregorDecl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
2846b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import the major distinguishing characteristics of a protocol.
28472e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  DeclContext *DC, *LexicalDC;
28482e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  DeclarationName Name;
28492e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  SourceLocation Loc;
28502e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
28512e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    return 0;
28522e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
28532e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  ObjCProtocolDecl *MergeWithProtocol = 0;
28542e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
28552e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       Lookup.first != Lookup.second;
28562e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++Lookup.first) {
28572e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
28582e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      continue;
28592e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
28602e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
28612e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      break;
28622e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  }
28632e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
28642e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  ObjCProtocolDecl *ToProto = MergeWithProtocol;
28652e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  if (!ToProto || ToProto->isForwardDecl()) {
28662e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if (!ToProto) {
28672e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
28682e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                                         Name.getAsIdentifierInfo());
28692e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto->setForwardDecl(D->isForwardDecl());
28702e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto->setLexicalDeclContext(LexicalDC);
28712e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      LexicalDC->addDecl(ToProto);
28722e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    }
28732e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Imported(D, ToProto);
28742e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
28752e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    // Import protocols
28762e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
28772e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
28782e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    ObjCProtocolDecl::protocol_loc_iterator
28792e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      FromProtoLoc = D->protocol_loc_begin();
28802e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
28812e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                                          FromProtoEnd = D->protocol_end();
28822e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       FromProto != FromProtoEnd;
28832e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++FromProto, ++FromProtoLoc) {
28842e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ObjCProtocolDecl *ToProto
28852e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
28862e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      if (!ToProto)
28872e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor        return 0;
28882e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      Protocols.push_back(ToProto);
28892e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
28902e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    }
28912e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
28922e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
28932e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    ToProto->setProtocolList(Protocols.data(), Protocols.size(),
28942e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                             ProtocolLocs.data(), Importer.getToContext());
28952e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  } else {
28962e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Imported(D, ToProto);
28972e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  }
28982e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
2899b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import all of the members of this protocol.
2900083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
29012e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
29022e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  return ToProto;
29032e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor}
29042e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
2905a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas GregorDecl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2906a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // Import the major distinguishing characteristics of an @interface.
2907a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  DeclContext *DC, *LexicalDC;
2908a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  DeclarationName Name;
2909a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  SourceLocation Loc;
2910a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2911a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    return 0;
2912a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2913a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  ObjCInterfaceDecl *MergeWithIface = 0;
2914a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2915a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       Lookup.first != Lookup.second;
2916a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++Lookup.first) {
2917a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2918a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      continue;
2919a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2920a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2921a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      break;
2922a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
2923a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2924a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  ObjCInterfaceDecl *ToIface = MergeWithIface;
2925a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (!ToIface || ToIface->isForwardDecl()) {
2926a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!ToIface) {
2927a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2928a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          DC, Loc,
2929a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          Name.getAsIdentifierInfo(),
2930deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor                                          Importer.Import(D->getClassLoc()),
2931a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          D->isForwardDecl(),
2932a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          D->isImplicitInterfaceDecl());
29332e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToIface->setForwardDecl(D->isForwardDecl());
2934a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setLexicalDeclContext(LexicalDC);
2935a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      LexicalDC->addDecl(ToIface);
2936a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
2937a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Imported(D, ToIface);
2938a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2939a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (D->getSuperClass()) {
2940a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ObjCInterfaceDecl *Super
2941a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2942a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      if (!Super)
2943a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        return 0;
2944a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2945a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setSuperClass(Super);
2946a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2947a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
2948a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2949a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // Import protocols
2950a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2951a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2952a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ObjCInterfaceDecl::protocol_loc_iterator
2953a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      FromProtoLoc = D->protocol_loc_begin();
295453b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek
295553b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // FIXME: Should we be usng all_referenced_protocol_begin() here?
2956a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2957a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                           FromProtoEnd = D->protocol_end();
2958a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       FromProto != FromProtoEnd;
2959a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++FromProto, ++FromProtoLoc) {
2960a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ObjCProtocolDecl *ToProto
2961a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2962a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      if (!ToProto)
2963a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        return 0;
2964a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      Protocols.push_back(ToProto);
2965a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2966a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
2967a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2968a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
2969a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2970a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                             ProtocolLocs.data(), Importer.getToContext());
2971a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
2972a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // Import @end range
2973a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2974a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  } else {
2975a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Imported(D, ToIface);
29762e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
29772e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    // Check for consistency of superclasses.
29782e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    DeclarationName FromSuperName, ToSuperName;
29792e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (D->getSuperClass())
29802e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
29812e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (ToIface->getSuperClass())
29822e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      ToSuperName = ToIface->getSuperClass()->getDeclName();
29832e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (FromSuperName != ToSuperName) {
29842e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(ToIface->getLocation(),
29852e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                      diag::err_odr_objc_superclass_inconsistent)
29862e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << ToIface->getDeclName();
29872e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (ToIface->getSuperClass())
29882e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.ToDiag(ToIface->getSuperClassLoc(),
29892e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                        diag::note_odr_objc_superclass)
29902e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor          << ToIface->getSuperClass()->getDeclName();
29912e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      else
29922e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.ToDiag(ToIface->getLocation(),
29932e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                        diag::note_odr_objc_missing_superclass);
29942e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (D->getSuperClass())
29952e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.FromDiag(D->getSuperClassLoc(),
29962e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                          diag::note_odr_objc_superclass)
29972e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor          << D->getSuperClass()->getDeclName();
29982e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      else
29992e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.FromDiag(D->getLocation(),
30002e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                          diag::note_odr_objc_missing_superclass);
30012e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      return 0;
30022e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    }
3003a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
3004a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3005b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import categories. When the categories themselves are imported, they'll
3006b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // hook themselves into this interface.
3007b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3008b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor       FromCat = FromCat->getNextClassCategory())
3009b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Import(FromCat);
3010b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
3011a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // Import all of the members of this class.
3012083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
3013a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3014a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // If we have an @implementation, import it as well.
3015a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (D->getImplementation()) {
3016dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3017dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                       Importer.Import(D->getImplementation()));
3018a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!Impl)
3019a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      return 0;
3020a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3021a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setImplementation(Impl);
3022a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
3023a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
30242e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  return ToIface;
3025a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor}
3026a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3027dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas GregorDecl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3028dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  // Find the corresponding interface.
3029dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3030dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                       Importer.Import(D->getClassInterface()));
3031dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  if (!Iface)
3032dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    return 0;
3033dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3034dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  // Import the superclass, if any.
3035dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ObjCInterfaceDecl *Super = 0;
3036dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  if (D->getSuperClass()) {
3037dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Super = cast_or_null<ObjCInterfaceDecl>(
3038dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                          Importer.Import(D->getSuperClass()));
3039dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    if (!Super)
3040dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      return 0;
3041dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  }
3042dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3043dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ObjCImplementationDecl *Impl = Iface->getImplementation();
3044dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  if (!Impl) {
3045dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // We haven't imported an implementation yet. Create a new @implementation
3046dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // now.
3047dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3048dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                  Importer.ImportContext(D->getDeclContext()),
3049dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                          Importer.Import(D->getLocation()),
3050dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                          Iface, Super);
3051dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3052dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    if (D->getDeclContext() != D->getLexicalDeclContext()) {
3053dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      DeclContext *LexicalDC
3054dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        = Importer.ImportContext(D->getLexicalDeclContext());
3055dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      if (!LexicalDC)
3056dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        return 0;
3057dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      Impl->setLexicalDeclContext(LexicalDC);
3058dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    }
3059dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3060dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // Associate the implementation with the class it implements.
3061dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Iface->setImplementation(Impl);
3062dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Importer.Imported(D, Iface->getImplementation());
3063dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  } else {
3064dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Importer.Imported(D, Iface->getImplementation());
3065dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3066dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // Verify that the existing @implementation has the same superclass.
3067dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    if ((Super && !Impl->getSuperClass()) ||
3068dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        (!Super && Impl->getSuperClass()) ||
3069dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        (Super && Impl->getSuperClass() &&
3070dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor         Super->getCanonicalDecl() != Impl->getSuperClass())) {
3071dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        Importer.ToDiag(Impl->getLocation(),
3072dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                        diag::err_odr_objc_superclass_inconsistent)
3073dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          << Iface->getDeclName();
3074dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        // FIXME: It would be nice to have the location of the superclass
3075dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        // below.
3076dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        if (Impl->getSuperClass())
3077dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.ToDiag(Impl->getLocation(),
3078dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                          diag::note_odr_objc_superclass)
3079dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          << Impl->getSuperClass()->getDeclName();
3080dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        else
3081dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.ToDiag(Impl->getLocation(),
3082dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                          diag::note_odr_objc_missing_superclass);
3083dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        if (D->getSuperClass())
3084dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.FromDiag(D->getLocation(),
3085dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                            diag::note_odr_objc_superclass)
3086dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          << D->getSuperClass()->getDeclName();
3087dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        else
3088dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.FromDiag(D->getLocation(),
3089dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                            diag::note_odr_objc_missing_superclass);
3090dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      return 0;
3091dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    }
3092dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  }
3093dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3094dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  // Import all of the members of this @implementation.
3095dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ImportDeclContext(D);
3096dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3097dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  return Impl;
3098dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor}
3099dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3100e3261624c1870e52d7efc2ac83e647713361ac6cDouglas GregorDecl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3101e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Import the major distinguishing characteristics of an @property.
3102e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  DeclContext *DC, *LexicalDC;
3103e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  DeclarationName Name;
3104e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  SourceLocation Loc;
3105e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3106e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    return 0;
3107e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3108e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Check whether we have already imported this property.
3109e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3110e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       Lookup.first != Lookup.second;
3111e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       ++Lookup.first) {
3112e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    if (ObjCPropertyDecl *FoundProp
3113e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                                = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3114e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // Check property types.
3115e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      if (!Importer.IsStructurallyEquivalent(D->getType(),
3116e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                                             FoundProp->getType())) {
3117e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3118e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor          << Name << D->getType() << FoundProp->getType();
3119e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3120e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor          << FoundProp->getType();
3121e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        return 0;
3122e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      }
3123e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3124e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // FIXME: Check property attributes, getters, setters, etc.?
3125e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3126e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // Consider these properties to be equivalent.
3127e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      Importer.Imported(D, FoundProp);
3128e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      return FoundProp;
3129e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    }
3130e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  }
3131e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3132e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Import the type.
313383a230c83a54190366138c1a4f4310ef838b88fcJohn McCall  TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
313483a230c83a54190366138c1a4f4310ef838b88fcJohn McCall  if (!T)
3135e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    return 0;
3136e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3137e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Create the new property.
3138e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ObjCPropertyDecl *ToProperty
3139e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3140e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               Name.getAsIdentifierInfo(),
3141e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               Importer.Import(D->getAtLoc()),
3142e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               T,
3143e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               D->getPropertyImplementation());
3144e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  Importer.Imported(D, ToProperty);
3145e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setLexicalDeclContext(LexicalDC);
3146e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  LexicalDC->addDecl(ToProperty);
3147e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3148e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setPropertyAttributes(D->getPropertyAttributes());
314980aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian  ToProperty->setPropertyAttributesAsWritten(
315080aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian                                      D->getPropertyAttributesAsWritten());
3151e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3152e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3153e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setGetterMethodDecl(
3154e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3155e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setSetterMethodDecl(
3156e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3157e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setPropertyIvarDecl(
3158e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3159e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  return ToProperty;
3160e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor}
3161e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
31622b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas GregorDecl *
31632b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas GregorASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
31642b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  // Import the context of this declaration.
31652b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
31662b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  if (!DC)
31672b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    return 0;
31682b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
31692b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  DeclContext *LexicalDC = DC;
31702b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
31712b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
31722b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    if (!LexicalDC)
31732b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      return 0;
31742b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  }
31752b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
31762b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  // Import the location of this declaration.
31772b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
31782b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
31792b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
31802b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  llvm::SmallVector<SourceLocation, 4> Locations;
31812b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
31822b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    = D->protocol_loc_begin();
31832b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  for (ObjCForwardProtocolDecl::protocol_iterator FromProto
31842b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor         = D->protocol_begin(), FromProtoEnd = D->protocol_end();
31852b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor       FromProto != FromProtoEnd;
31862b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor       ++FromProto, ++FromProtoLoc) {
31872b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    ObjCProtocolDecl *ToProto
31882b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
31892b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    if (!ToProto)
31902b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      continue;
31912b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
31922b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Protocols.push_back(ToProto);
31932b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Locations.push_back(Importer.Import(*FromProtoLoc));
31942b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  }
31952b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
31962b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ObjCForwardProtocolDecl *ToForward
31972b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
31982b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor                                      Protocols.data(), Protocols.size(),
31992b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor                                      Locations.data());
32002b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ToForward->setLexicalDeclContext(LexicalDC);
32012b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  LexicalDC->addDecl(ToForward);
32022b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  Importer.Imported(D, ToForward);
32032b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  return ToForward;
32042b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor}
32052b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
3206a2bc15b7463a9f85a5bff1531d833c278426a733Douglas GregorDecl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3207a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  // Import the context of this declaration.
3208a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3209a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  if (!DC)
3210a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    return 0;
3211a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3212a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  DeclContext *LexicalDC = DC;
3213a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
3214a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3215a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    if (!LexicalDC)
3216a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      return 0;
3217a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  }
3218a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3219a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  // Import the location of this declaration.
3220a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
3221a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3222a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3223a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  llvm::SmallVector<SourceLocation, 4> Locations;
3224a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3225a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor       From != FromEnd; ++From) {
3226a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    ObjCInterfaceDecl *ToIface
3227a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3228a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    if (!ToIface)
3229a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      continue;
3230a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3231a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Interfaces.push_back(ToIface);
3232a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Locations.push_back(Importer.Import(From->getLocation()));
3233a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  }
3234a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3235a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3236a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Loc,
3237a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Interfaces.data(),
3238a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Locations.data(),
3239a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Interfaces.size());
3240a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  ToClass->setLexicalDeclContext(LexicalDC);
3241a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  LexicalDC->addDecl(ToClass);
3242a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  Importer.Imported(D, ToClass);
3243a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  return ToClass;
3244a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor}
3245a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3246040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3247040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // For template arguments, we adopt the translation unit as our declaration
3248040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // context. This context will be fixed when the actual template declaration
3249040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // is created.
3250040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3251040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Import default argument.
3252040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return TemplateTypeParmDecl::Create(Importer.getToContext(),
3253040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                              Importer.getToContext().getTranslationUnitDecl(),
3254040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      Importer.Import(D->getLocation()),
3255040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->getDepth(),
3256040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->getIndex(),
3257040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      Importer.Import(D->getIdentifier()),
3258040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->wasDeclaredWithTypename(),
3259040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->isParameterPack());
3260040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3261040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3262040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *
3263040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3264040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the name of this declaration.
3265040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
3266040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D->getDeclName() && !Name)
3267040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3268040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3269040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the location of this declaration.
3270040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
3271040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3272040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the type of this declaration.
3273040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  QualType T = Importer.Import(D->getType());
3274040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (T.isNull())
3275040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3276040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3277040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import type-source information.
3278040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3279040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D->getTypeSourceInfo() && !TInfo)
3280040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3281040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3282040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Import default argument.
3283040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3284040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3285040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                               Importer.getToContext().getTranslationUnitDecl(),
3286040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         Loc, D->getDepth(), D->getPosition(),
3287040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         Name.getAsIdentifierInfo(),
3288040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         T, TInfo);
3289040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3290040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3291040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *
3292040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3293040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the name of this declaration.
3294040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
3295040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D->getDeclName() && !Name)
3296040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3297040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3298040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the location of this declaration.
3299040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
3300040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3301040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import template parameters.
3302040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  TemplateParameterList *TemplateParams
3303040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    = ImportTemplateParameterList(D->getTemplateParameters());
3304040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!TemplateParams)
3305040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3306040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3307040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Import default argument.
3308040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3309040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3310040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                              Importer.getToContext().getTranslationUnitDecl(),
3311040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          Loc, D->getDepth(), D->getPosition(),
3312040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          Name.getAsIdentifierInfo(),
3313040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          TemplateParams);
3314040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3315040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3316040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3317040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // If this record has a definition in the translation unit we're coming from,
3318040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // but this particular declaration is not that definition, import the
3319040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // definition and map to that.
3320040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  CXXRecordDecl *Definition
3321040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3322040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (Definition && Definition != D->getTemplatedDecl()) {
3323040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *ImportedDef
3324040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      = Importer.Import(Definition->getDescribedClassTemplate());
3325040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!ImportedDef)
3326040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return 0;
3327040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3328040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return Importer.Imported(D, ImportedDef);
3329040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
3330040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3331040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the major distinguishing characteristics of this class template.
3332040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclContext *DC, *LexicalDC;
3333040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclarationName Name;
3334040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  SourceLocation Loc;
3335040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3336040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3337040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3338040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // We may already have a template of the same name; try to find and match it.
3339040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!DC->isFunctionOrMethod()) {
3340040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3341040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3342040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor         Lookup.first != Lookup.second;
3343040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor         ++Lookup.first) {
3344040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3345040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        continue;
3346040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3347040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Decl *Found = *Lookup.first;
3348040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (ClassTemplateDecl *FoundTemplate
3349040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                        = dyn_cast<ClassTemplateDecl>(Found)) {
3350040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (IsStructuralMatch(D, FoundTemplate)) {
3351040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          // The class templates structurally match; call it the same template.
3352040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          // FIXME: We may be filling in a forward declaration here. Handle
3353040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          // this case!
3354040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Importer.Imported(D->getTemplatedDecl(),
3355040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                            FoundTemplate->getTemplatedDecl());
3356040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          return Importer.Imported(D, FoundTemplate);
3357040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        }
3358040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
3359040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3360040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
3361040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
3362040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3363040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!ConflictingDecls.empty()) {
3364040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3365040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         ConflictingDecls.data(),
3366040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         ConflictingDecls.size());
3367040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
3368040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3369040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!Name)
3370040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return 0;
3371040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
3372040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3373040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3374040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3375040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Create the declaration that is being templated.
3376040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3377040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                     DTemplated->getTagKind(),
3378040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                     DC,
3379040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     Importer.Import(DTemplated->getLocation()),
3380040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                     Name.getAsIdentifierInfo(),
3381040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                               Importer.Import(DTemplated->getTagKeywordLoc()));
3382040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2Templated->setAccess(DTemplated->getAccess());
3383040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3384040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3385040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the qualifier, if any.
3386040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (DTemplated->getQualifier()) {
3387040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    NestedNameSpecifier *NNS = Importer.Import(DTemplated->getQualifier());
3388040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    SourceRange NNSRange = Importer.Import(DTemplated->getQualifierRange());
3389040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    D2Templated->setQualifierInfo(NNS, NNSRange);
3390040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
3391040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2Templated->setLexicalDeclContext(LexicalDC);
3392040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3393040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Create the class template declaration itself.
3394040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  TemplateParameterList *TemplateParams
3395040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    = ImportTemplateParameterList(D->getTemplateParameters());
3396040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!TemplateParams)
3397040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3398040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3399040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3400040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                    Loc, Name, TemplateParams,
3401040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                    D2Templated,
3402040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  /*PrevDecl=*/0);
3403040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2Templated->setDescribedClassTemplate(D2);
3404040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3405040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2->setAccess(D->getAccess());
3406040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2->setLexicalDeclContext(LexicalDC);
3407040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  LexicalDC->addDecl(D2);
3408040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3409040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Note the relationship between the class templates.
3410040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  Importer.Imported(D, D2);
3411040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  Importer.Imported(DTemplated, D2Templated);
3412040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3413040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3414040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    // FIXME: Import definition!
3415040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
3416040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3417040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return D2;
3418040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3419040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3420d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorDecl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3421d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                          ClassTemplateSpecializationDecl *D) {
3422d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // If this record has a definition in the translation unit we're coming from,
3423d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // but this particular declaration is not that definition, import the
3424d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // definition and map to that.
3425d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  TagDecl *Definition = D->getDefinition();
3426d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (Definition && Definition != D) {
3427d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    Decl *ImportedDef = Importer.Import(Definition);
3428d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!ImportedDef)
3429d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return 0;
3430d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3431d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return Importer.Imported(D, ImportedDef);
3432d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3433d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3434d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateDecl *ClassTemplate
3435d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = cast_or_null<ClassTemplateDecl>(Importer.Import(
3436d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 D->getSpecializedTemplate()));
3437d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (!ClassTemplate)
3438d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3439d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3440d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Import the context of this declaration.
3441d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  DeclContext *DC = ClassTemplate->getDeclContext();
3442d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (!DC)
3443d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3444d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3445d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  DeclContext *LexicalDC = DC;
3446d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
3447d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3448d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!LexicalDC)
3449d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return 0;
3450d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3451d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3452d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Import the location of this declaration.
3453d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
3454d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3455d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Import template arguments.
3456d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3457d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (ImportTemplateArguments(D->getTemplateArgs().data(),
3458d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                              D->getTemplateArgs().size(),
3459d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                              TemplateArgs))
3460d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3461d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3462d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Try to find an existing specialization with these template arguments.
3463d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  void *InsertPos = 0;
3464d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateSpecializationDecl *D2
3465d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = ClassTemplate->findSpecialization(TemplateArgs.data(),
3466d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                        TemplateArgs.size(), InsertPos);
3467d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D2) {
3468d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // We already have a class template specialization with these template
3469d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // arguments.
3470d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3471d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // FIXME: Check for specialization vs. instantiation errors.
3472d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3473d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (RecordDecl *FoundDef = D2->getDefinition()) {
3474d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3475d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        // The record types structurally match, or the "from" translation
3476d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        // unit only had a forward declaration anyway; call it the same
3477d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        // function.
3478d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return Importer.Imported(D, FoundDef);
3479d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      }
3480d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
3481d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  } else {
3482d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Create a new specialization.
3483d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3484d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 D->getTagKind(), DC,
3485d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 Loc, ClassTemplate,
3486d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 TemplateArgs.data(),
3487d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 TemplateArgs.size(),
3488d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 /*PrevDecl=*/0);
3489d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    D2->setSpecializationKind(D->getSpecializationKind());
3490d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3491d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Add this specialization to the class template.
3492d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ClassTemplate->AddSpecialization(D2, InsertPos);
3493d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3494d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Import the qualifier, if any.
3495d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (D->getQualifier()) {
3496d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
3497d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      SourceRange NNSRange = Importer.Import(D->getQualifierRange());
3498d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      D2->setQualifierInfo(NNS, NNSRange);
3499d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
3500d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3501d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3502d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Add the specialization to this context.
3503d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    D2->setLexicalDeclContext(LexicalDC);
3504d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    LexicalDC->addDecl(D2);
3505d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3506d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  Importer.Imported(D, D2);
3507d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3508d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D->isDefinition() && ImportDefinition(D, D2))
3509d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3510d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3511d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return D2;
3512d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
3513d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
35144800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
35154800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor// Import Statements
35164800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
35174800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
35184800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorStmt *ASTNodeImporter::VisitStmt(Stmt *S) {
35194800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
35204800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    << S->getStmtClassName();
35214800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return 0;
35224800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
35234800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
35244800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
35254800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor// Import Expressions
35264800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
35274800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorExpr *ASTNodeImporter::VisitExpr(Expr *E) {
35284800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
35294800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    << E->getStmtClassName();
35304800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return 0;
35314800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
35324800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
3533440806306674e23ad74726208cbdc6f37849dd9dDouglas GregorExpr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3534440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  NestedNameSpecifier *Qualifier = 0;
3535440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (E->getQualifier()) {
3536440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    Qualifier = Importer.Import(E->getQualifier());
3537440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    if (!E->getQualifier())
3538440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor      return 0;
3539440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  }
3540440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
3541440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3542440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (!ToD)
3543440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    return 0;
3544440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
3545440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  QualType T = Importer.Import(E->getType());
3546440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (T.isNull())
3547440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    return 0;
3548440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
3549440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
3550440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             Importer.Import(E->getQualifierRange()),
3551440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             ToD,
3552440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             Importer.Import(E->getLocation()),
3553f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                             T, E->getValueKind(),
3554440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             /*FIXME:TemplateArgs=*/0);
3555440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor}
3556440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
35574800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorExpr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
35584800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  QualType T = Importer.Import(E->getType());
35594800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (T.isNull())
35604800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return 0;
35614800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
35629996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return IntegerLiteral::Create(Importer.getToContext(),
35639996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                                E->getValue(), T,
35649996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                                Importer.Import(E->getLocation()));
35654800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
35664800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
3567b2e400aae8c62c4e1616016f40618baace0da065Douglas GregorExpr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3568b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  QualType T = Importer.Import(E->getType());
3569b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  if (T.isNull())
3570b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor    return 0;
3571b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor
3572b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3573b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor                                                        E->isWide(), T,
3574b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor                                          Importer.Import(E->getLocation()));
3575b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor}
3576b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor
3577f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3578f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
3579f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!SubExpr)
3580f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3581f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3582f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext())
3583f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                  ParenExpr(Importer.Import(E->getLParen()),
3584f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                            Importer.Import(E->getRParen()),
3585f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                            SubExpr);
3586f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3587f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3588f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3589f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
3590f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
3591f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3592f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3593f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
3594f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!SubExpr)
3595f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3596f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3597f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
3598f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                     T, E->getValueKind(),
3599f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                     E->getObjectKind(),
3600f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                         Importer.Import(E->getOperatorLoc()));
3601f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3602f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3603bd249a542878a626192746c1e0c0b21f164e6df7Douglas GregorExpr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
3604bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  QualType ResultType = Importer.Import(E->getType());
3605bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3606bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  if (E->isArgumentType()) {
3607bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3608bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    if (!TInfo)
3609bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor      return 0;
3610bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3611bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3612bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                                           TInfo, ResultType,
3613bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                           Importer.Import(E->getOperatorLoc()),
3614bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                           Importer.Import(E->getRParenLoc()));
3615bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  }
3616bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3617bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3618bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  if (!SubExpr)
3619bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    return 0;
3620bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3621bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3622bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                                         SubExpr, ResultType,
3623bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                          Importer.Import(E->getOperatorLoc()),
3624bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                          Importer.Import(E->getRParenLoc()));
3625bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor}
3626bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3627f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3628f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
3629f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
3630f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3631f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3632f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *LHS = Importer.Import(E->getLHS());
3633f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!LHS)
3634f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3635f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3636f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *RHS = Importer.Import(E->getRHS());
3637f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!RHS)
3638f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3639f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3640f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
3641f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                      T, E->getValueKind(),
3642f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                      E->getObjectKind(),
3643f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                          Importer.Import(E->getOperatorLoc()));
3644f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3645f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3646f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3647f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
3648f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
3649f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3650f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3651f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3652f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (CompLHSType.isNull())
3653f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3654f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3655f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType CompResultType = Importer.Import(E->getComputationResultType());
3656f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (CompResultType.isNull())
3657f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3658f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3659f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *LHS = Importer.Import(E->getLHS());
3660f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!LHS)
3661f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3662f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3663f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *RHS = Importer.Import(E->getRHS());
3664f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!RHS)
3665f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3666f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3667f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext())
3668f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                        CompoundAssignOperator(LHS, RHS, E->getOpcode(),
3669f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                               T, E->getValueKind(),
3670f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                               E->getObjectKind(),
3671f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                               CompLHSType, CompResultType,
3672f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                          Importer.Import(E->getOperatorLoc()));
3673f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3674f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3675f871d0cc377a1367b519a6cce26be74607566ebaJohn McCallbool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3676f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (E->path_empty()) return false;
3677f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
3678f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  // TODO: import cast paths
3679f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return true;
3680f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
3681f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
368236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorExpr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
368336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  QualType T = Importer.Import(E->getType());
368436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (T.isNull())
368536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
368636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
368736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
368836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!SubExpr)
368936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
3690f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
3691f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  CXXCastPath BasePath;
3692f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (ImportCastPath(E, BasePath))
3693f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return 0;
3694f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
3695f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
36965baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                                  SubExpr, &BasePath, E->getValueKind());
369736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
369836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
3699008847a70ab122a99911149199855060fb3753b4Douglas GregorExpr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3700008847a70ab122a99911149199855060fb3753b4Douglas Gregor  QualType T = Importer.Import(E->getType());
3701008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (T.isNull())
3702008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
3703008847a70ab122a99911149199855060fb3753b4Douglas Gregor
3704008847a70ab122a99911149199855060fb3753b4Douglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
3705008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (!SubExpr)
3706008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
3707008847a70ab122a99911149199855060fb3753b4Douglas Gregor
3708008847a70ab122a99911149199855060fb3753b4Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3709008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (!TInfo && E->getTypeInfoAsWritten())
3710008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
3711008847a70ab122a99911149199855060fb3753b4Douglas Gregor
3712f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  CXXCastPath BasePath;
3713f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (ImportCastPath(E, BasePath))
3714f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return 0;
3715f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
3716f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  return CStyleCastExpr::Create(Importer.getToContext(), T,
3717f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                E->getValueKind(), E->getCastKind(),
3718f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                SubExpr, &BasePath, TInfo,
3719f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                Importer.Import(E->getLParenLoc()),
3720f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                Importer.Import(E->getRParenLoc()));
3721008847a70ab122a99911149199855060fb3753b4Douglas Gregor}
3722008847a70ab122a99911149199855060fb3753b4Douglas Gregor
372333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios KyrtzidisASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
372439b49bcaaddb1049234fca9500c0ac02c088e23dChris Lattner                         ASTContext &FromContext, FileManager &FromFileManager)
37251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  : ToContext(ToContext), FromContext(FromContext),
372639b49bcaaddb1049234fca9500c0ac02c088e23dChris Lattner    ToFileManager(ToFileManager), FromFileManager(FromFileManager) {
37279bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ImportedDecls[FromContext.getTranslationUnitDecl()]
37289bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    = ToContext.getTranslationUnitDecl();
37299bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
37309bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
37319bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorASTImporter::~ASTImporter() { }
37321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
37331b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTImporter::Import(QualType FromT) {
37341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (FromT.isNull())
37351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
37361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
3737169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Check whether we've already imported this type.
3738169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  llvm::DenseMap<Type *, Type *>::iterator Pos
3739169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor    = ImportedTypes.find(FromT.getTypePtr());
3740169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  if (Pos != ImportedTypes.end())
3741169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor    return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
37421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
3743169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Import the type
37441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  ASTNodeImporter Importer(*this);
37451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToT = Importer.Visit(FromT.getTypePtr());
37461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToT.isNull())
37471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToT;
37481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
3749169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Record the imported type.
3750169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
3751169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor
37521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
37531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
37541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
37559bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorTypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
375682fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (!FromTSI)
375782fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return FromTSI;
375882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
375982fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // FIXME: For now we just create a "trivial" type source info based
37605606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky  // on the type and a single location. Implement a real version of this.
376182fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  QualType T = Import(FromTSI->getType());
376282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (T.isNull())
376382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
376482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
376582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  return ToContext.getTrivialTypeSourceInfo(T,
3766bd054dba8a3023821f2a0951b0fae05e3522a7c9Abramo Bagnara                        FromTSI->getTypeLoc().getSourceRange().getBegin());
37679bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
37689bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
37699bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorDecl *ASTImporter::Import(Decl *FromD) {
37709bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromD)
37719bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
37729bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
37739bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Check whether we've already imported this declaration.
37749bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
37759bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (Pos != ImportedDecls.end())
37769bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return Pos->second;
37779bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
37789bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Import the type
37799bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ASTNodeImporter Importer(*this);
37809bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  Decl *ToD = Importer.Visit(FromD);
37819bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!ToD)
37829bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
37839bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
37849bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Record the imported declaration.
37859bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ImportedDecls[FromD] = ToD;
3786ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
3787ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3788ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // Keep track of anonymous tags that have an associated typedef.
3789ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (FromTag->getTypedefForAnonDecl())
3790ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      AnonTagsWithPendingTypedefs.push_back(FromTag);
3791ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3792ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // When we've finished transforming a typedef, see whether it was the
3793ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // typedef for an anonymous tag.
3794ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    for (llvm::SmallVector<TagDecl *, 4>::iterator
3795ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor               FromTag = AnonTagsWithPendingTypedefs.begin(),
3796ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            FromTagEnd = AnonTagsWithPendingTypedefs.end();
3797ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor         FromTag != FromTagEnd; ++FromTag) {
3798ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3799ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3800ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          // We found the typedef for an anonymous tag; link them.
3801ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3802ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          AnonTagsWithPendingTypedefs.erase(FromTag);
3803ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          break;
3804ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        }
3805ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      }
3806ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    }
3807ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  }
3808ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
38099bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return ToD;
38109bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
38119bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
38129bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorDeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
38139bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromDC)
38149bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return FromDC;
38159bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
38169bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
38179bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
38189bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
38199bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorExpr *ASTImporter::Import(Expr *FromE) {
38209bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromE)
38219bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
38229bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
38239bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
38249bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
38259bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
38269bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorStmt *ASTImporter::Import(Stmt *FromS) {
38279bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromS)
38289bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
38299bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
38304800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Check whether we've already imported this declaration.
38314800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
38324800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (Pos != ImportedStmts.end())
38334800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return Pos->second;
38344800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
38354800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Import the type
38364800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  ASTNodeImporter Importer(*this);
38374800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Stmt *ToS = Importer.Visit(FromS);
38384800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (!ToS)
38394800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return 0;
38404800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
38414800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Record the imported declaration.
38424800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  ImportedStmts[FromS] = ToS;
38434800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return ToS;
38449bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
38459bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
38469bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorNestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
38479bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromNNS)
38489bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
38499bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
38509bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // FIXME: Implement!
38519bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return 0;
38529bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
38539bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
3854d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorTemplateName ASTImporter::Import(TemplateName From) {
3855d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  switch (From.getKind()) {
3856d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::Template:
3857d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (TemplateDecl *ToTemplate
3858d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
3859d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateName(ToTemplate);
3860d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3861d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateName();
3862d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3863d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::OverloadedTemplate: {
3864d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
3865d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    UnresolvedSet<2> ToTemplates;
3866d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
3867d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                             E = FromStorage->end();
3868d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor         I != E; ++I) {
3869d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
3870d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        ToTemplates.addDecl(To);
3871d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      else
3872d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return TemplateName();
3873d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
3874d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
3875d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                               ToTemplates.end());
3876d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3877d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3878d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::QualifiedTemplate: {
3879d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
3880d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
3881d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Qualifier)
3882d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateName();
3883d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3884d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (TemplateDecl *ToTemplate
3885d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
3886d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return ToContext.getQualifiedTemplateName(Qualifier,
3887d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                QTN->hasTemplateKeyword(),
3888d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                ToTemplate);
3889d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3890d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateName();
3891d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3892d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3893d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::DependentTemplate: {
3894d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    DependentTemplateName *DTN = From.getAsDependentTemplateName();
3895d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
3896d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Qualifier)
3897d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateName();
3898d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3899d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (DTN->isIdentifier()) {
3900d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return ToContext.getDependentTemplateName(Qualifier,
3901d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                Import(DTN->getIdentifier()));
3902d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
3903d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3904d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
3905d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3906d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3907d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3908d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm_unreachable("Invalid template name kind");
3909d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return TemplateName();
3910d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
3911d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
39129bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorSourceLocation ASTImporter::Import(SourceLocation FromLoc) {
39139bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (FromLoc.isInvalid())
39149bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return SourceLocation();
39159bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
3916885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &FromSM = FromContext.getSourceManager();
3917885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3918885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // For now, map everything down to its spelling location, so that we
3919885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // don't have to import macro instantiations.
3920885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // FIXME: Import macro instantiations!
3921885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  FromLoc = FromSM.getSpellingLoc(FromLoc);
3922885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
3923885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &ToSM = ToContext.getSourceManager();
3924885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  return ToSM.getLocForStartOfFile(Import(Decomposed.first))
3925885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor             .getFileLocWithOffset(Decomposed.second);
39269bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
39279bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
39289bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorSourceRange ASTImporter::Import(SourceRange FromRange) {
39299bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
39309bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
39319bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
3932885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas GregorFileID ASTImporter::Import(FileID FromID) {
3933535a3e20104461c136654d59fb833ae80644ae79Sebastian Redl  llvm::DenseMap<FileID, FileID>::iterator Pos
3934535a3e20104461c136654d59fb833ae80644ae79Sebastian Redl    = ImportedFileIDs.find(FromID);
3935885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  if (Pos != ImportedFileIDs.end())
3936885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    return Pos->second;
3937885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3938885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &FromSM = FromContext.getSourceManager();
3939885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &ToSM = ToContext.getSourceManager();
3940885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
3941885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
3942885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3943885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // Include location of this file.
3944885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
3945885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3946885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // Map the FileID for to the "to" source manager.
3947885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  FileID ToID;
3948885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
3949885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  if (Cache->Entry) {
3950885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We probably want to use getVirtualFile(), so we don't hit the
3951885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // disk again
3952885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
3953885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // than mmap the files several times.
395439b49bcaaddb1049234fca9500c0ac02c088e23dChris Lattner    const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
3955885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToID = ToSM.createFileID(Entry, ToIncludeLoc,
3956885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                             FromSLoc.getFile().getFileCharacteristic());
3957885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  } else {
3958885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We want to re-use the existing MemoryBuffer!
395933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    const llvm::MemoryBuffer *
396033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis        FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
3961885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    llvm::MemoryBuffer *ToBuf
3962a0a270c0f1c0a4e3482438bdc5f4a7bd3d25f0a6Chris Lattner      = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
3963885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                                             FromBuf->getBufferIdentifier());
3964885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToID = ToSM.createFileIDForMemBuffer(ToBuf);
3965885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  }
3966885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3967885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
3968535a3e20104461c136654d59fb833ae80644ae79Sebastian Redl  ImportedFileIDs[FromID] = ToID;
3969885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  return ToID;
3970885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor}
3971885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
39721b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorDeclarationName ASTImporter::Import(DeclarationName FromName) {
39731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!FromName)
39741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return DeclarationName();
39751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
39761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  switch (FromName.getNameKind()) {
39771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::Identifier:
39781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Import(FromName.getAsIdentifierInfo());
39791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
39801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCZeroArgSelector:
39811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCOneArgSelector:
39821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCMultiArgSelector:
39831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Import(FromName.getObjCSelector());
39841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
39851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXConstructorName: {
39861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
39871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
39881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
39891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
39901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXConstructorName(
39911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
39921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
39931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
39941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXDestructorName: {
39951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
39961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
39971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
39981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
39991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXDestructorName(
40001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
40011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
40021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
40031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXConversionFunctionName: {
40041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
40051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
40061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
40071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
40081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXConversionFunctionName(
40091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
40101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
40111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
40121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXOperatorName:
40131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXOperatorName(
40141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                          FromName.getCXXOverloadedOperator());
40151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
40161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXLiteralOperatorName:
40171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXLiteralOperatorName(
40181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                   Import(FromName.getCXXLiteralIdentifier()));
40191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
40201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXUsingDirective:
40211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: STATICS!
40221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return DeclarationName::getUsingDirectiveName();
40231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
40241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
40251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Silence bogus GCC warning
40261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return DeclarationName();
40271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
40281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
4029d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorIdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
40301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!FromId)
40311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return 0;
40321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
40331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return &ToContext.Idents.get(FromId->getName());
40341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
4035089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
4036c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas GregorSelector ASTImporter::Import(Selector FromSel) {
4037c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (FromSel.isNull())
4038c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return Selector();
4039c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
4040c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  llvm::SmallVector<IdentifierInfo *, 4> Idents;
4041c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4042c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4043c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4044c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4045c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor}
4046c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
4047089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4048089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                DeclContext *DC,
4049089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                unsigned IDNS,
4050089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                NamedDecl **Decls,
4051089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                unsigned NumDecls) {
4052089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  return Name;
4053089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
4054089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
4055089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
405633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  return ToContext.getDiagnostics().Report(Loc, DiagID);
4057089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
4058089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
4059089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
406033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  return FromContext.getDiagnostics().Report(Loc, DiagID);
4061089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
40625ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
40635ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas GregorDecl *ASTImporter::Imported(Decl *From, Decl *To) {
40645ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  ImportedDecls[From] = To;
40655ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  return To;
4066af667588d53de22795c5304f1496ccaac2a71402Daniel Dunbar}
4067ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
4068ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregorbool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
4069ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  llvm::DenseMap<Type *, Type *>::iterator Pos
4070ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor   = ImportedTypes.find(From.getTypePtr());
4071ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4072ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return true;
4073ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
407433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
4075bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(From, To);
4076ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor}
4077