ASTImporter.cpp revision da57f3eeab7b7f7f6e6788956f0a0d9adf196a7d
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
44f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitType(const Type *T);
45f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitBuiltinType(const BuiltinType *T);
46f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitComplexType(const ComplexType *T);
47f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitPointerType(const PointerType *T);
48f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitBlockPointerType(const BlockPointerType *T);
49f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitLValueReferenceType(const LValueReferenceType *T);
50f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitRValueReferenceType(const RValueReferenceType *T);
51f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitMemberPointerType(const MemberPointerType *T);
52f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitConstantArrayType(const ConstantArrayType *T);
53f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
54f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitVariableArrayType(const VariableArrayType *T);
551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentSizedArrayType
561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentSizedExtVectorType
57f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitVectorType(const VectorType *T);
58f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitExtVectorType(const ExtVectorType *T);
59f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
60f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitFunctionProtoType(const FunctionProtoType *T);
611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: UnresolvedUsingType
62f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitTypedefType(const TypedefType *T);
63f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitTypeOfExprType(const TypeOfExprType *T);
641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentTypeOfExprType
65f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitTypeOfType(const TypeOfType *T);
66f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitDecltypeType(const DecltypeType *T);
6734b41d939a1328f484511c6002ba2456db879a29Richard Smith    QualType VisitAutoType(const AutoType *T);
681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentDecltypeType
69f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitRecordType(const RecordType *T);
70f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitEnumType(const EnumType *T);
711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: TemplateTypeParmType
721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: SubstTemplateTypeParmType
73f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
74f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitElaboratedType(const ElaboratedType *T);
754714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    // FIXME: DependentNameType
7633500955d731c73717af52088b7fc0e7a85681e7John McCall    // FIXME: DependentTemplateSpecializationType
77f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
78f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitObjCObjectType(const ObjCObjectType *T);
79f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
80089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
81089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    // Importing declarations
82a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
83a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                         DeclContext *&LexicalDC, DeclarationName &Name,
84788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                         SourceLocation &Loc);
852577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
862577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                  DeclarationNameInfo& To);
87d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
88d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    bool ImportDefinition(RecordDecl *From, RecordDecl *To);
89040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    TemplateParameterList *ImportTemplateParameterList(
90040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                 TemplateParameterList *Params);
91d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
92d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    bool ImportTemplateArguments(const TemplateArgument *FromArgs,
93d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                 unsigned NumFromArgs,
94d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                               llvm::SmallVectorImpl<TemplateArgument> &ToArgs);
9596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
9673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
97040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
9889cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    Decl *VisitDecl(Decl *D);
99788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    Decl *VisitNamespaceDecl(NamespaceDecl *D);
1009e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
10136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
10296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *VisitRecordDecl(RecordDecl *D);
10336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
104a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D);
105c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
106c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
107c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
108c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
10996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
11087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
1112e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
112089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
1132cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
114a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Decl *VisitParmVarDecl(ParmVarDecl *D);
115c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
116b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
1172e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
118a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
1193daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
120dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
121e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
122954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
1232b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
124a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Decl *VisitObjCClassDecl(ObjCClassDecl *D);
125040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
126040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
127040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
128040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
129d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    Decl *VisitClassTemplateSpecializationDecl(
130d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                            ClassTemplateSpecializationDecl *D);
131a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
1324800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    // Importing statements
1334800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Stmt *VisitStmt(Stmt *S);
1344800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
1354800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    // Importing expressions
1364800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Expr *VisitExpr(Expr *E);
137440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    Expr *VisitDeclRefExpr(DeclRefExpr *E);
1384800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Expr *VisitIntegerLiteral(IntegerLiteral *E);
139b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor    Expr *VisitCharacterLiteral(CharacterLiteral *E);
140f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitParenExpr(ParenExpr *E);
141f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitUnaryOperator(UnaryOperator *E);
142f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
143f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitBinaryOperator(BinaryOperator *E);
144f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
14536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
146008847a70ab122a99911149199855060fb3753b4Douglas Gregor    Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
1471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  };
1481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
1491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//----------------------------------------------------------------------------
15173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor// Structural Equivalence
15273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor//----------------------------------------------------------------------------
15373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
15473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregornamespace {
15573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  struct StructuralEquivalenceContext {
15673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief AST contexts for which we are checking structural equivalence.
15773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    ASTContext &C1, &C2;
15873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
15973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief The set of "tentative" equivalences between two canonical
16073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// declarations, mapping from a declaration in the first context to the
16173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// declaration in the second context that we believe to be equivalent.
16273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
16373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
16473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Queue of declarations in the first context whose equivalence
16573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// with a declaration in the second context still needs to be verified.
16673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    std::deque<Decl *> DeclsToCheck;
16773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
168ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    /// \brief Declaration (from, to) pairs that are known not to be equivalent
169ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    /// (which we have already complained about).
170ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
171ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
17273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Whether we're being strict about the spelling of types when
17373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// unifying two types.
17473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool StrictTypeSpelling;
17573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
17673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
177ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor               llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
17873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                 bool StrictTypeSpelling = false)
17933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis      : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
180ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        StrictTypeSpelling(StrictTypeSpelling) { }
18173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
18273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Determine whether the two declarations are structurally
18373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// equivalent.
18473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
18573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
18673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Determine whether the two types are structurally equivalent.
18773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructurallyEquivalent(QualType T1, QualType T2);
18873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
18973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  private:
19073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Finish checking all of the structural equivalences.
19173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    ///
19273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \returns true if an error occurred, false otherwise.
19373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool Finish();
19473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
19573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  public:
19673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
19733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis      return C1.getDiagnostics().Report(Loc, DiagID);
19873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
19973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
20073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
20133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis      return C2.getDiagnostics().Report(Loc, DiagID);
20273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
20373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  };
20473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
20573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
20673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
20773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     QualType T1, QualType T2);
20873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
20973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Decl *D1, Decl *D2);
21073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
21173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine if two APInts have the same value, after zero-extending
21273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// one of them (if needed!) to ensure that the bit-widths match.
21373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
21473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() == I2.getBitWidth())
21573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == I2;
21673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
21773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() > I2.getBitWidth())
2189f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    return I1 == I2.zext(I1.getBitWidth());
21973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
2209f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad  return I1.zext(I2.getBitWidth()) == I2;
22173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
22273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
22373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine if two APSInts have the same value, zero- or sign-extending
22473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// as needed.
22573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
22673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
22773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == I2;
22873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
22973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Check for a bit-width mismatch.
23073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() > I2.getBitWidth())
2319f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    return IsSameValue(I1, I2.extend(I1.getBitWidth()));
23273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  else if (I2.getBitWidth() > I1.getBitWidth())
2339f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    return IsSameValue(I1.extend(I2.getBitWidth()), I2);
23473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
23573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // We have a signedness mismatch. Turn the signed value into an unsigned
23673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // value.
23773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.isSigned()) {
23873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (I1.isNegative())
23973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
24073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return llvm::APSInt(I1, true) == I2;
24273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
24373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I2.isNegative())
24573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
24673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return I1 == llvm::APSInt(I2, true);
24873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
24973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
25073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two expressions.
25173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
25273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Expr *E1, Expr *E2) {
25373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!E1 || !E2)
25473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return E1 == E2;
25573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
25673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Actually perform a structural comparison!
25773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
25873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
25973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
26073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two identifiers are equivalent.
26173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
26273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const IdentifierInfo *Name2) {
26373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!Name1 || !Name2)
26473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return Name1 == Name2;
26573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
26673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return Name1->getName() == Name2->getName();
26773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
26873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
26973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two nested-name-specifiers are equivalent.
27073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
27173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     NestedNameSpecifier *NNS1,
27273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     NestedNameSpecifier *NNS2) {
27373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Implement!
27473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
27573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
27673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
27773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two template arguments are equivalent.
27873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
27973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const TemplateArgument &Arg1,
28073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const TemplateArgument &Arg2) {
281d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (Arg1.getKind() != Arg2.getKind())
282d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return false;
283d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
284d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  switch (Arg1.getKind()) {
285d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Null:
286d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return true;
287d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
288d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Type:
289d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
290d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
291d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Integral:
292d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
293d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                          Arg2.getIntegralType()))
294d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return false;
295d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
296d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
297d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
298d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Declaration:
299d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
300d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
301d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Template:
302d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return IsStructurallyEquivalent(Context,
303d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg1.getAsTemplate(),
304d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg2.getAsTemplate());
305a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
306a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  case TemplateArgument::TemplateExpansion:
307a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    return IsStructurallyEquivalent(Context,
308a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                                    Arg1.getAsTemplateOrTemplatePattern(),
309a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                                    Arg2.getAsTemplateOrTemplatePattern());
310a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
311d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Expression:
312d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return IsStructurallyEquivalent(Context,
313d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg1.getAsExpr(), Arg2.getAsExpr());
314d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
315d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Pack:
316d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Arg1.pack_size() != Arg2.pack_size())
317d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return false;
318d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
319d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
320d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (!IsStructurallyEquivalent(Context,
321d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg1.pack_begin()[I],
322d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg2.pack_begin()[I]))
323d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return false;
324d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
325d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return true;
326d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
327d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
328d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm_unreachable("Invalid template argument kind");
32973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
33073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
33173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
33273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence for the common part of array
33373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// types.
33473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
33573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                          const ArrayType *Array1,
33673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                          const ArrayType *Array2) {
33773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!IsStructurallyEquivalent(Context,
33873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                Array1->getElementType(),
33973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                Array2->getElementType()))
34073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
34173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Array1->getSizeModifier() != Array2->getSizeModifier())
34273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
34373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
34473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
34573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
34673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
34773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
34873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
34973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two types.
35073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
35173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     QualType T1, QualType T2) {
35273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (T1.isNull() || T2.isNull())
35373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return T1.isNull() && T2.isNull();
35473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
35573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!Context.StrictTypeSpelling) {
35673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // We aren't being strict about token-to-token equivalence of types,
35773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // so map down to the canonical type.
35873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    T1 = Context.C1.getCanonicalType(T1);
35973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    T2 = Context.C2.getCanonicalType(T2);
36073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
36173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
36273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (T1.getQualifiers() != T2.getQualifiers())
36373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
36473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
365ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  Type::TypeClass TC = T1->getTypeClass();
366ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
367ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T1->getTypeClass() != T2->getTypeClass()) {
368ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // Compare function types with prototypes vs. without prototypes as if
369ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // both did not have prototypes.
370ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (T1->getTypeClass() == Type::FunctionProto &&
371ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        T2->getTypeClass() == Type::FunctionNoProto)
372ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      TC = Type::FunctionNoProto;
373ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    else if (T1->getTypeClass() == Type::FunctionNoProto &&
374ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor             T2->getTypeClass() == Type::FunctionProto)
375ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      TC = Type::FunctionNoProto;
376ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    else
377ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      return false;
378ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  }
37973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
380ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  switch (TC) {
381ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  case Type::Builtin:
38273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Deal with Char_S/Char_U.
38373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
38473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
38573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
38673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
38773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Complex:
38873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
38973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<ComplexType>(T1)->getElementType(),
39073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<ComplexType>(T2)->getElementType()))
39173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
39273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
39373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
39473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Pointer:
39573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
39673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<PointerType>(T1)->getPointeeType(),
39773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<PointerType>(T2)->getPointeeType()))
39873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
39973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
40073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
40173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::BlockPointer:
40273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
40373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<BlockPointerType>(T1)->getPointeeType(),
40473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<BlockPointerType>(T2)->getPointeeType()))
40573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
40673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
40773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
40873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::LValueReference:
40973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::RValueReference: {
41073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ReferenceType *Ref1 = cast<ReferenceType>(T1);
41173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ReferenceType *Ref2 = cast<ReferenceType>(T2);
41273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
41373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
41473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Ref1->isInnerRef() != Ref2->isInnerRef())
41573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
41673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
41773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ref1->getPointeeTypeAsWritten(),
41873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ref2->getPointeeTypeAsWritten()))
41973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
42073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
42173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
42273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
42373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::MemberPointer: {
42473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
42573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
42673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
42773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  MemPtr1->getPointeeType(),
42873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  MemPtr2->getPointeeType()))
42973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
43073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
43173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(MemPtr1->getClass(), 0),
43273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(MemPtr2->getClass(), 0)))
43373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
43473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
43573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
43673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
43773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ConstantArray: {
43873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
43973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
44073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsSameValue(Array1->getSize(), Array2->getSize()))
44173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
44273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
44373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
44473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
44573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
44673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
44773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
44873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::IncompleteArray:
44973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context,
45073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                       cast<ArrayType>(T1),
45173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                       cast<ArrayType>(T2)))
45273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
45373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
45473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
45573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::VariableArray: {
45673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
45773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
45873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
45973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Array1->getSizeExpr(), Array2->getSizeExpr()))
46073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
46273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
46373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
46573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
46673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
46773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
46873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::DependentSizedArray: {
46973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
47073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
47173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
47273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Array1->getSizeExpr(), Array2->getSizeExpr()))
47373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
47473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
47573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
47673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
47773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
47873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
47973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
48073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
48173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::DependentSizedExtVector: {
48273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedExtVectorType *Vec1
48373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<DependentSizedExtVectorType>(T1);
48473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedExtVectorType *Vec2
48573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<DependentSizedExtVectorType>(T2);
48673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
48773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getSizeExpr(), Vec2->getSizeExpr()))
48873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
48973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
49073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getElementType(),
49173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec2->getElementType()))
49273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
49373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
49473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
49573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
49673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Vector:
49773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ExtVector: {
49873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VectorType *Vec1 = cast<VectorType>(T1);
49973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VectorType *Vec2 = cast<VectorType>(T2);
50073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
50173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getElementType(),
50273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec2->getElementType()))
50373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
50473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Vec1->getNumElements() != Vec2->getNumElements())
50573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
506e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    if (Vec1->getVectorKind() != Vec2->getVectorKind())
50773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
5080e12b44081c6395a6d60a05a85a6012f7bb23b16Douglas Gregor    break;
50973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
51073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
51173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::FunctionProto: {
51273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
51373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
51473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getNumArgs() != Proto2->getNumArgs())
51573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
51673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
51773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
51873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto1->getArgType(I),
51973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto2->getArgType(I)))
52073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
52173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
52273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->isVariadic() != Proto2->isVariadic())
52373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
52460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
52573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
52660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    if (Proto1->getExceptionSpecType() == EST_Dynamic) {
52760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
52860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl        return false;
52960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
53060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl        if (!IsStructurallyEquivalent(Context,
53160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                                      Proto1->getExceptionType(I),
53260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                                      Proto2->getExceptionType(I)))
53360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl          return false;
53460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      }
53560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
53673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
53760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                                    Proto1->getNoexceptExpr(),
53860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                                    Proto2->getNoexceptExpr()))
53973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
54073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
54173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
54273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
54373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
54473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Fall through to check the bits common with FunctionNoProtoType.
54573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
54673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
54773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::FunctionNoProto: {
54873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionType *Function1 = cast<FunctionType>(T1);
54973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionType *Function2 = cast<FunctionType>(T2);
55073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
55173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Function1->getResultType(),
55273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Function2->getResultType()))
55373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
554264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      if (Function1->getExtInfo() != Function2->getExtInfo())
555264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola        return false;
55673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
55773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
55873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
55973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::UnresolvedUsing:
56073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
56173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<UnresolvedUsingType>(T1)->getDecl(),
56273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<UnresolvedUsingType>(T2)->getDecl()))
56373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
56473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
56573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
5669d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
5679d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  case Type::Attributed:
5689d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    if (!IsStructurallyEquivalent(Context,
5699d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                  cast<AttributedType>(T1)->getModifiedType(),
5709d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                  cast<AttributedType>(T2)->getModifiedType()))
5719d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall      return false;
5729d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    if (!IsStructurallyEquivalent(Context,
5739d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                cast<AttributedType>(T1)->getEquivalentType(),
5749d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                cast<AttributedType>(T2)->getEquivalentType()))
5759d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall      return false;
5769d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    break;
57773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
578075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  case Type::Paren:
579075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    if (!IsStructurallyEquivalent(Context,
580075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara                                  cast<ParenType>(T1)->getInnerType(),
581075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara                                  cast<ParenType>(T2)->getInnerType()))
582075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      return false;
583075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    break;
584075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
58573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Typedef:
58673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
58773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypedefType>(T1)->getDecl(),
58873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypedefType>(T2)->getDecl()))
58973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
59073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
59173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
59273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TypeOfExpr:
59373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
59473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
59573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
59673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
59773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
59873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
59973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TypeOf:
60073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
60173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypeOfType>(T1)->getUnderlyingType(),
60273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypeOfType>(T2)->getUnderlyingType()))
60373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
60473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
60573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
60673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Decltype:
60773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
60873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<DecltypeType>(T1)->getUnderlyingExpr(),
60973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<DecltypeType>(T2)->getUnderlyingExpr()))
61073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
61173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
61273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
61334b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto:
61434b41d939a1328f484511c6002ba2456db879a29Richard Smith    if (!IsStructurallyEquivalent(Context,
61534b41d939a1328f484511c6002ba2456db879a29Richard Smith                                  cast<AutoType>(T1)->getDeducedType(),
61634b41d939a1328f484511c6002ba2456db879a29Richard Smith                                  cast<AutoType>(T2)->getDeducedType()))
61734b41d939a1328f484511c6002ba2456db879a29Richard Smith      return false;
61834b41d939a1328f484511c6002ba2456db879a29Richard Smith    break;
61934b41d939a1328f484511c6002ba2456db879a29Richard Smith
62073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Record:
62173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Enum:
62273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
62373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TagType>(T1)->getDecl(),
62473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TagType>(T2)->getDecl()))
62573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
62673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
627465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
62873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TemplateTypeParm: {
62973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
63073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
63173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->getDepth() != Parm2->getDepth())
63273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
63373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->getIndex() != Parm2->getIndex())
63473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
63573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->isParameterPack() != Parm2->isParameterPack())
63673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
63773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
63873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Names of template type parameters are never significant.
63973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
64073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
64173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
64273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::SubstTemplateTypeParm: {
64373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const SubstTemplateTypeParmType *Subst1
64473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<SubstTemplateTypeParmType>(T1);
64573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const SubstTemplateTypeParmType *Subst2
64673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<SubstTemplateTypeParmType>(T2);
64773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
64873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Subst1->getReplacedParameter(), 0),
64973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Subst2->getReplacedParameter(), 0)))
65073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
65173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
65273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Subst1->getReplacementType(),
65373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Subst2->getReplacementType()))
65473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
65573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
65673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
65773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
6580bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  case Type::SubstTemplateTypeParmPack: {
6590bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    const SubstTemplateTypeParmPackType *Subst1
6600bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor      = cast<SubstTemplateTypeParmPackType>(T1);
6610bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    const SubstTemplateTypeParmPackType *Subst2
6620bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor      = cast<SubstTemplateTypeParmPackType>(T2);
6630bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    if (!IsStructurallyEquivalent(Context,
6640bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                                  QualType(Subst1->getReplacedParameter(), 0),
6650bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                                  QualType(Subst2->getReplacedParameter(), 0)))
6660bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor      return false;
6670bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    if (!IsStructurallyEquivalent(Context,
6680bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                                  Subst1->getArgumentPack(),
6690bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                                  Subst2->getArgumentPack()))
6700bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor      return false;
6710bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    break;
6720bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  }
67373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TemplateSpecialization: {
67473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateSpecializationType *Spec1
67573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<TemplateSpecializationType>(T1);
67673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateSpecializationType *Spec2
67773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<TemplateSpecializationType>(T2);
67873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
67973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Spec1->getTemplateName(),
68073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Spec2->getTemplateName()))
68173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
68273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Spec1->getNumArgs() != Spec2->getNumArgs())
68373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
68473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
68573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
68673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Spec1->getArg(I), Spec2->getArg(I)))
68773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
68873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
68973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
69073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
69173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
692465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Type::Elaborated: {
693465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
694465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
695465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    // CHECKME: what if a keyword is ETK_None or ETK_typename ?
696465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Elab1->getKeyword() != Elab2->getKeyword())
697465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return false;
69873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
699465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab1->getQualifier(),
700465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab2->getQualifier()))
70173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
70273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
703465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab1->getNamedType(),
704465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab2->getNamedType()))
70573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
70673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
70773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
70873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
7093cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  case Type::InjectedClassName: {
7103cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
7113cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
7123cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (!IsStructurallyEquivalent(Context,
71331f17ecbef57b5679c017c375db330546b7b5145John McCall                                  Inj1->getInjectedSpecializationType(),
71431f17ecbef57b5679c017c375db330546b7b5145John McCall                                  Inj2->getInjectedSpecializationType()))
7153cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      return false;
7163cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    break;
7173cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
7183cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
7194714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  case Type::DependentName: {
7204714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    const DependentNameType *Typename1 = cast<DependentNameType>(T1);
7214714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    const DependentNameType *Typename2 = cast<DependentNameType>(T2);
72273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
72373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename1->getQualifier(),
72473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename2->getQualifier()))
72573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
72673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
72773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename2->getIdentifier()))
72873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
72973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
73073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
73173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
73273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
73333500955d731c73717af52088b7fc0e7a85681e7John McCall  case Type::DependentTemplateSpecialization: {
73433500955d731c73717af52088b7fc0e7a85681e7John McCall    const DependentTemplateSpecializationType *Spec1 =
73533500955d731c73717af52088b7fc0e7a85681e7John McCall      cast<DependentTemplateSpecializationType>(T1);
73633500955d731c73717af52088b7fc0e7a85681e7John McCall    const DependentTemplateSpecializationType *Spec2 =
73733500955d731c73717af52088b7fc0e7a85681e7John McCall      cast<DependentTemplateSpecializationType>(T2);
73833500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!IsStructurallyEquivalent(Context,
73933500955d731c73717af52088b7fc0e7a85681e7John McCall                                  Spec1->getQualifier(),
74033500955d731c73717af52088b7fc0e7a85681e7John McCall                                  Spec2->getQualifier()))
74133500955d731c73717af52088b7fc0e7a85681e7John McCall      return false;
74233500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
74333500955d731c73717af52088b7fc0e7a85681e7John McCall                                  Spec2->getIdentifier()))
74433500955d731c73717af52088b7fc0e7a85681e7John McCall      return false;
74533500955d731c73717af52088b7fc0e7a85681e7John McCall    if (Spec1->getNumArgs() != Spec2->getNumArgs())
74633500955d731c73717af52088b7fc0e7a85681e7John McCall      return false;
74733500955d731c73717af52088b7fc0e7a85681e7John McCall    for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
74833500955d731c73717af52088b7fc0e7a85681e7John McCall      if (!IsStructurallyEquivalent(Context,
74933500955d731c73717af52088b7fc0e7a85681e7John McCall                                    Spec1->getArg(I), Spec2->getArg(I)))
75033500955d731c73717af52088b7fc0e7a85681e7John McCall        return false;
75133500955d731c73717af52088b7fc0e7a85681e7John McCall    }
75233500955d731c73717af52088b7fc0e7a85681e7John McCall    break;
75333500955d731c73717af52088b7fc0e7a85681e7John McCall  }
7547536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
7557536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  case Type::PackExpansion:
7567536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    if (!IsStructurallyEquivalent(Context,
7577536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor                                  cast<PackExpansionType>(T1)->getPattern(),
7587536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor                                  cast<PackExpansionType>(T2)->getPattern()))
7597536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor      return false;
7607536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    break;
7617536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
76273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ObjCInterface: {
76373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
76473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
76573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
76673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Iface1->getDecl(), Iface2->getDecl()))
76773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
768c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    break;
769c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  }
770c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
771c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject: {
772c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
773c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
774c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (!IsStructurallyEquivalent(Context,
775c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                  Obj1->getBaseType(),
776c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                  Obj2->getBaseType()))
777c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return false;
778c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
77973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
780c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
78173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
782c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Obj1->getProtocol(I),
783c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Obj2->getProtocol(I)))
78473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
78573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
78673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
78773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
78873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
78973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ObjCObjectPointer: {
79073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
79173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
79273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
79373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ptr1->getPointeeType(),
79473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ptr2->getPointeeType()))
79573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
79673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
79773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
79873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
79973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  } // end switch
80073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
80173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
80273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
80373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
80473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two records.
80573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
80673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     RecordDecl *D1, RecordDecl *D2) {
80773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (D1->isUnion() != D2->isUnion()) {
80873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
80973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
81073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
81173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << D1->getDeclName() << (unsigned)D1->getTagKind();
81273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
81373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
81473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
815d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // If both declarations are class template specializations, we know
816d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // the ODR applies, so check the template and template arguments.
817d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateSpecializationDecl *Spec1
818d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = dyn_cast<ClassTemplateSpecializationDecl>(D1);
819d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateSpecializationDecl *Spec2
820d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = dyn_cast<ClassTemplateSpecializationDecl>(D2);
821d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (Spec1 && Spec2) {
822d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Check that the specialized templates are the same.
823d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
824d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                  Spec2->getSpecializedTemplate()))
825d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return false;
826d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
827d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Check that the template arguments are the same.
828d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
829d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return false;
830d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
831d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
832d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (!IsStructurallyEquivalent(Context,
833d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Spec1->getTemplateArgs().get(I),
834d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Spec2->getTemplateArgs().get(I)))
835d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return false;
836d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
837d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // If one is a class template specialization and the other is not, these
838d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // structures are diferent.
839d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  else if (Spec1 || Spec2)
840d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return false;
841d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
842ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Compare the definitions of these two records. If either or both are
843ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // incomplete, we assume that they are equivalent.
844ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  D1 = D1->getDefinition();
845ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  D2 = D2->getDefinition();
846ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (!D1 || !D2)
847ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return true;
848ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
84973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
85073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
85173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
85273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
853040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          << Context.C2.getTypeDeclType(D2);
85473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
855040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          << D2CXX->getNumBases();
85673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
857040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          << D1CXX->getNumBases();
85873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
85973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
86073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
86173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      // Check the base classes.
86273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
86373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                           BaseEnd1 = D1CXX->bases_end(),
86473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                Base2 = D2CXX->bases_begin();
86573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor           Base1 != BaseEnd1;
86673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor           ++Base1, ++Base2) {
86773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!IsStructurallyEquivalent(Context,
86873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                      Base1->getType(), Base2->getType())) {
86973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
87073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Context.C2.getTypeDeclType(D2);
87173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
87273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->getType()
87373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->getSourceRange();
87473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
87573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getType()
87673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getSourceRange();
87773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          return false;
87873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        }
87973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
88073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check virtual vs. non-virtual inheritance mismatch.
88173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (Base1->isVirtual() != Base2->isVirtual()) {
88273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
88373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Context.C2.getTypeDeclType(D2);
88473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(Base2->getSourceRange().getBegin(),
88573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                        diag::note_odr_virtual_base)
88673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->isVirtual() << Base2->getSourceRange();
88773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
88873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->isVirtual()
88973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getSourceRange();
89073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          return false;
89173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        }
89273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
89373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    } else if (D1CXX->getNumBases() > 0) {
89473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
89573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
89673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
89773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
89873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Base1->getType()
89973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Base1->getSourceRange();
90073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
90173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
90273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
90373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
90473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
90573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Check the fields for consistency.
90673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
90773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                             Field2End = D2->field_end();
90873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
90973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Field1End = D1->field_end();
91073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       Field1 != Field1End;
91173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       ++Field1, ++Field2) {
91273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field2 == Field2End) {
91373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
91473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
91573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Field1->getLocation(), diag::note_odr_field)
91673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName() << Field1->getType();
91773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
91873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
91973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
92073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
92173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
92273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Field1->getType(), Field2->getType())) {
92373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
92473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
92573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(Field2->getLocation(), diag::note_odr_field)
92673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field2->getDeclName() << Field2->getType();
92773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Field1->getLocation(), diag::note_odr_field)
92873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName() << Field1->getType();
92973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
93073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
93173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
93273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field1->isBitField() != Field2->isBitField()) {
93373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
93473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
93573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (Field1->isBitField()) {
93673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        llvm::APSInt Bits;
93773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
93873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
93973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field1->getDeclName() << Field1->getType()
94073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits.toString(10, false);
94173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
94273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName();
94373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
94473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        llvm::APSInt Bits;
94573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
94673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
94773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName() << Field2->getType()
94873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits.toString(10, false);
94973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(),
95073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                          diag::note_odr_not_bit_field)
95173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName();
95273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
95373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
95473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
95573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
95673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field1->isBitField()) {
95773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      // Make sure that the bit-fields are the same length.
95873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      llvm::APSInt Bits1, Bits2;
95973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
96073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
96173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
96273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
96373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
96473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsSameValue(Bits1, Bits2)) {
96573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
96673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Context.C2.getTypeDeclType(D2);
96773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
96873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName() << Field2->getType()
96973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits2.toString(10, false);
97073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
97173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field1->getDeclName() << Field1->getType()
97273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits1.toString(10, false);
97373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
97473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
97573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
97673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
97773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
97873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Field2 != Field2End) {
97973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
98073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
98173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(Field2->getLocation(), diag::note_odr_field)
98273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Field2->getDeclName() << Field2->getType();
98373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
98473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
98573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
98673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
98773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
98873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
98973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
99073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two enums.
99173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
99273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     EnumDecl *D1, EnumDecl *D2) {
99373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
99473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                             EC2End = D2->enumerator_end();
99573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
99673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  EC1End = D1->enumerator_end();
99773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       EC1 != EC1End; ++EC1, ++EC2) {
99873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (EC2 == EC2End) {
99973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
100073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
100173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
100273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getDeclName()
100373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getInitVal().toString(10);
100473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
100573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
100673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
100773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
100873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::APSInt Val1 = EC1->getInitVal();
100973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::APSInt Val2 = EC2->getInitVal();
101073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsSameValue(Val1, Val2) ||
101173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
101273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
101373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
101473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
101573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC2->getDeclName()
101673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC2->getInitVal().toString(10);
101773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
101873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getDeclName()
101973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getInitVal().toString(10);
102073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
102173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
102273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
102373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
102473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (EC2 != EC2End) {
102573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
102673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
102773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
102873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << EC2->getDeclName()
102973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << EC2->getInitVal().toString(10);
103073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
103173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
103273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
103373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
103473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
103573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
1036040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1037040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1038040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateParameterList *Params1,
1039040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateParameterList *Params2) {
1040040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (Params1->size() != Params2->size()) {
1041040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(Params2->getTemplateLoc(),
1042040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                  diag::err_odr_different_num_template_parameters)
1043040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << Params1->size() << Params2->size();
1044040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(Params1->getTemplateLoc(),
1045040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                  diag::note_odr_template_parameter_list);
1046040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1047040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1048040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1049040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1050040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1051040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Context.Diag2(Params2->getParam(I)->getLocation(),
1052040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                    diag::err_odr_different_template_parameter_kind);
1053040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Context.Diag1(Params1->getParam(I)->getLocation(),
1054040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                    diag::note_odr_template_parameter_here);
1055040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return false;
1056040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
1057040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1058040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1059040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          Params2->getParam(I))) {
1060040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1061040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return false;
1062040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
1063040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1064040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1065040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return true;
1066040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1067040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1068040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1069040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTypeParmDecl *D1,
1070040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTypeParmDecl *D2) {
1071040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D1->isParameterPack() != D2->isParameterPack()) {
1072040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1073040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D2->isParameterPack();
1074040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1075040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D1->isParameterPack();
1076040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1077040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1078040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1079040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return true;
1080040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1081040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1082040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1083040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     NonTypeTemplateParmDecl *D1,
1084040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     NonTypeTemplateParmDecl *D2) {
1085040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Enable once we have variadic templates.
1086040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#if 0
1087040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D1->isParameterPack() != D2->isParameterPack()) {
1088040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1089040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D2->isParameterPack();
1090040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1091040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D1->isParameterPack();
1092040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1093040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1094040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#endif
1095040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1096040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check types.
1097040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1098040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(),
1099040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                  diag::err_odr_non_type_parameter_type_inconsistent)
1100040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D2->getType() << D1->getType();
1101040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1102040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D1->getType();
1103040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1104040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1105040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1106040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return true;
1107040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1108040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1109040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1110040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTemplateParmDecl *D1,
1111040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTemplateParmDecl *D2) {
1112040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Enable once we have variadic templates.
1113040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#if 0
1114040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D1->isParameterPack() != D2->isParameterPack()) {
1115040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1116040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    << D2->isParameterPack();
1117040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1118040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    << D1->isParameterPack();
1119040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1120040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1121040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#endif
1122040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1123040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check template parameter lists.
1124040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1125040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                  D2->getTemplateParameters());
1126040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1127040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1128040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1129040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     ClassTemplateDecl *D1,
1130040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     ClassTemplateDecl *D2) {
1131040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check template parameters.
1132040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!IsStructurallyEquivalent(Context,
1133040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                D1->getTemplateParameters(),
1134040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                D2->getTemplateParameters()))
1135040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
113673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
1137040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check the templated declaration.
1138040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1139040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          D2->getTemplatedDecl());
1140040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1141040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
114273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two declarations.
114373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
114473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Decl *D1, Decl *D2) {
114573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Check for known structural equivalences via a callback of some sort.
114673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
1147ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Check whether we already know that these two declarations are not
1148ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // structurally equivalent.
1149ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1150ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                      D2->getCanonicalDecl())))
1151ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return false;
1152ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
115373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Determine whether we've already produced a tentative equivalence for D1.
115473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
115573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (EquivToD1)
115673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return EquivToD1 == D2->getCanonicalDecl();
115773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
115873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Produce a tentative equivalence D1 <-> D2, which will be checked later.
115973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EquivToD1 = D2->getCanonicalDecl();
116073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
116173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
116273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
116373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
116473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
116573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                            Decl *D2) {
116673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!::IsStructurallyEquivalent(*this, D1, D2))
116773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
116873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
116973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return !Finish();
117073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
117173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
117273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
117373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                            QualType T2) {
117473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!::IsStructurallyEquivalent(*this, T1, T2))
117573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
117673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
117773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return !Finish();
117873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
117973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
118073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::Finish() {
118173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  while (!DeclsToCheck.empty()) {
118273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Check the next declaration.
118373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Decl *D1 = DeclsToCheck.front();
118473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DeclsToCheck.pop_front();
118573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
118673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Decl *D2 = TentativeEquivalences[D1];
118773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    assert(D2 && "Unrecorded tentative equivalence?");
118873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
1189ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    bool Equivalent = true;
1190ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
119173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Switch on all declaration kinds. For now, we're just going to
119273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // check the obvious ones.
119373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
119473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
119573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check for equivalent structure names.
119673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name1 = Record1->getIdentifier();
119773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name1 && Record1->getTypedefForAnonDecl())
119873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
119973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name2 = Record2->getIdentifier();
120073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name2 && Record2->getTypedefForAnonDecl())
120173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
1202ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (!::IsStructurallyEquivalent(Name1, Name2) ||
1203ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this, Record1, Record2))
1204ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
120573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
120673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Record/non-record mismatch.
1207ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
120873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
1209ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
121073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
121173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check for equivalent enum names.
121273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name1 = Enum1->getIdentifier();
121373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name1 && Enum1->getTypedefForAnonDecl())
121473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
121573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name2 = Enum2->getIdentifier();
121673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!Name2 && Enum2->getTypedefForAnonDecl())
121773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
1218ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (!::IsStructurallyEquivalent(Name1, Name2) ||
1219ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1220ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
122173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
122273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Enum/non-enum mismatch
1223ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
122473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
1225ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
122673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
122773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
1228ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                        Typedef2->getIdentifier()) ||
1229ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this,
123073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        Typedef1->getUnderlyingType(),
123173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        Typedef2->getUnderlyingType()))
1232ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
123373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
123473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Typedef/non-typedef mismatch.
1235ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
123673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
1237040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (ClassTemplateDecl *ClassTemplate1
1238040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                           = dyn_cast<ClassTemplateDecl>(D1)) {
1239040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1240040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1241040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                        ClassTemplate2->getIdentifier()) ||
1242040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor            !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1243040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1244040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1245040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Class template/non-class-template mismatch.
1246040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1247040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1248040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1249040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1250040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1251040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1252040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1253040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Kind mismatch.
1254040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1255040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1256040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (NonTypeTemplateParmDecl *NTTP1
1257040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1258040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (NonTypeTemplateParmDecl *NTTP2
1259040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1260040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1261040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1262040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1263040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Kind mismatch.
1264040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1265040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1266040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (TemplateTemplateParmDecl *TTP1
1267040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                  = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1268040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (TemplateTemplateParmDecl *TTP2
1269040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                    = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1270040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1271040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1272040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1273040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Kind mismatch.
1274040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1275040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1276040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
1277040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1278ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (!Equivalent) {
1279ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      // Note that these two declarations are not equivalent (and we already
1280ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      // know about it).
1281ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1282ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                               D2->getCanonicalDecl()));
1283ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      return true;
1284ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    }
128573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Check other declaration kinds!
128673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
128773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
128873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return false;
128973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
129073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
129173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor//----------------------------------------------------------------------------
12921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor// Import Types
12931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//----------------------------------------------------------------------------
12941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1295f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitType(const Type *T) {
129689cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor  Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
129789cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    << T->getTypeClassName();
129889cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor  return QualType();
129989cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor}
130089cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor
1301f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
13021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  switch (T->getKind()) {
13031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Void: return Importer.getToContext().VoidTy;
13041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Bool: return Importer.getToContext().BoolTy;
13051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char_U:
13071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // The context we're importing from has an unsigned 'char'. If we're
13081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // importing into a context with a signed 'char', translate to
13091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // 'unsigned char' instead.
13101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (Importer.getToContext().getLangOptions().CharIsSigned)
13111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return Importer.getToContext().UnsignedCharTy;
13121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().CharTy;
13141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
13161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char16:
13181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++!
13191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().Char16Ty;
13201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char32:
13221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++!
13231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().Char32Ty;
13241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
13261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
13271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
13281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ULongLong:
13291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().UnsignedLongLongTy;
13301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
13311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char_S:
13331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // The context we're importing from has an unsigned 'char'. If we're
13341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // importing into a context with a signed 'char', translate to
13351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // 'unsigned char' instead.
13361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Importer.getToContext().getLangOptions().CharIsSigned)
13371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return Importer.getToContext().SignedCharTy;
13381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().CharTy;
13401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
13423f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::WChar_S:
13433f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::WChar_U:
13441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: If not in C++, shall we translate to the C equivalent of
13451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // wchar_t?
13461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().WCharTy;
13471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Short : return Importer.getToContext().ShortTy;
13491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Int : return Importer.getToContext().IntTy;
13501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Long : return Importer.getToContext().LongTy;
13511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
13521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
13531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Float: return Importer.getToContext().FloatTy;
13541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Double: return Importer.getToContext().DoubleTy;
13551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
13561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::NullPtr:
13581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++0x!
13591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().NullPtrTy;
13601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
13621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
13631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCId:
13651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports Objective-C!
13661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinIdTy;
13671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCClass:
13691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinClassTy;
13701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCSel:
13721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinSelTy;
13731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
13741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return QualType();
13761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1378f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
13791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
13801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
13811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getComplexType(ToElementType);
13841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1386f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
13871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
13881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
13891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getPointerType(ToPointeeType);
13921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1394f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
13951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for blocks support in "to" context.
13961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
13971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
13981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getBlockPointerType(ToPointeeType);
14011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1403f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType
1404f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
14051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++ support in "to" context.
14061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
14071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
14081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getLValueReferenceType(ToPointeeType);
14111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1413f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType
1414f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
14151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++0x support in "to" context.
14161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
14171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
14181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getRValueReferenceType(ToPointeeType);
14211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1423f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
14241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++ support in "to" context.
14251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
14261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
14271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
14301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getMemberPointerType(ToPointeeType,
14311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      ClassType.getTypePtr());
14321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1434f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
14351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getConstantArrayType(ToElementType,
14401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSize(),
14411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSizeModifier(),
14421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->getIndexTypeCVRQualifiers());
14431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1445f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType
1446f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
14471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getIncompleteArrayType(ToElementType,
14521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                        T->getSizeModifier(),
14531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                T->getIndexTypeCVRQualifiers());
14541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1456f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
14571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *Size = Importer.Import(T->getSizeExpr());
14621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!Size)
14631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  SourceRange Brackets = Importer.Import(T->getBracketsRange());
14661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getVariableArrayType(ToElementType, Size,
14671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSizeModifier(),
14681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                T->getIndexTypeCVRQualifiers(),
14691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      Brackets);
14701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1472f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
14731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getVectorType(ToElementType,
14781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->getNumElements(),
1479e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                               T->getVectorKind());
14801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1482f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
14831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getExtVectorType(ToElementType,
14881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                  T->getNumElements());
14891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1491f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType
1492f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
14931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: What happens if we're importing a function without a prototype
14941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // into C++? Should we make it variadic?
14951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToResultType = Importer.Import(T->getResultType());
14961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToResultType.isNull())
14971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
1498264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola
14991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getFunctionNoProtoType(ToResultType,
1500264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                        T->getExtInfo());
15011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1503f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
15041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToResultType = Importer.Import(T->getResultType());
15051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToResultType.isNull())
15061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Import argument types
15091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<QualType, 4> ArgTypes;
15101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
15111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                         AEnd = T->arg_type_end();
15121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       A != AEnd; ++A) {
15131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType ArgType = Importer.Import(*A);
15141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (ArgType.isNull())
15151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
15161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ArgTypes.push_back(ArgType);
15171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
15181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Import exception types
15201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<QualType, 4> ExceptionTypes;
15211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (FunctionProtoType::exception_iterator E = T->exception_begin(),
15221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                          EEnd = T->exception_end();
15231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       E != EEnd; ++E) {
15241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType ExceptionType = Importer.Import(*E);
15251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (ExceptionType.isNull())
15261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
15271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ExceptionTypes.push_back(ExceptionType);
15281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
1529e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
1530e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1531e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.Exceptions = ExceptionTypes.data();
15321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1534e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                                 ArgTypes.size(), EPI);
15351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1537f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
15381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  TypedefDecl *ToDecl
15391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
15401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
15411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeDeclType(ToDecl);
15441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1546f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
15471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
15481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToExpr)
15491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeOfExprType(ToExpr);
15521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1554f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
15551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
15561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToUnderlyingType.isNull())
15571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeOfType(ToUnderlyingType);
15601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1562f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
156334b41d939a1328f484511c6002ba2456db879a29Richard Smith  // FIXME: Make sure that the "to" context supports C++0x!
15641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
15651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToExpr)
15661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getDecltypeType(ToExpr);
15691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
157134b41d939a1328f484511c6002ba2456db879a29Richard SmithQualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
157234b41d939a1328f484511c6002ba2456db879a29Richard Smith  // FIXME: Make sure that the "to" context supports C++0x!
157334b41d939a1328f484511c6002ba2456db879a29Richard Smith  QualType FromDeduced = T->getDeducedType();
157434b41d939a1328f484511c6002ba2456db879a29Richard Smith  QualType ToDeduced;
157534b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (!FromDeduced.isNull()) {
157634b41d939a1328f484511c6002ba2456db879a29Richard Smith    ToDeduced = Importer.Import(FromDeduced);
157734b41d939a1328f484511c6002ba2456db879a29Richard Smith    if (ToDeduced.isNull())
157834b41d939a1328f484511c6002ba2456db879a29Richard Smith      return QualType();
157934b41d939a1328f484511c6002ba2456db879a29Richard Smith  }
158034b41d939a1328f484511c6002ba2456db879a29Richard Smith
158134b41d939a1328f484511c6002ba2456db879a29Richard Smith  return Importer.getToContext().getAutoType(ToDeduced);
158234b41d939a1328f484511c6002ba2456db879a29Richard Smith}
158334b41d939a1328f484511c6002ba2456db879a29Richard Smith
1584f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
15851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  RecordDecl *ToDecl
15861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
15871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
15881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTagDeclType(ToDecl);
15911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1593f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
15941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  EnumDecl *ToDecl
15951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
15961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
15971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTagDeclType(ToDecl);
16001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
16011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1602d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorQualType ASTNodeImporter::VisitTemplateSpecializationType(
1603f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall                                       const TemplateSpecializationType *T) {
1604d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1605d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (ToTemplate.isNull())
1606d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return QualType();
1607d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1608d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs;
1609d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1610d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return QualType();
1611d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1612d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  QualType ToCanonType;
1613d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (!QualType(T, 0).isCanonical()) {
1614d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualType FromCanonType
1615d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1616d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ToCanonType =Importer.Import(FromCanonType);
1617d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToCanonType.isNull())
1618d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return QualType();
1619d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1620d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1621d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                         ToTemplateArgs.data(),
1622d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                         ToTemplateArgs.size(),
1623d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                               ToCanonType);
1624d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1625d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1626f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
1627465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *ToQualifier = 0;
1628465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  // Note: the qualifier in an ElaboratedType is optional.
1629465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T->getQualifier()) {
1630465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ToQualifier = Importer.Import(T->getQualifier());
1631465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (!ToQualifier)
1632465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return QualType();
1633465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
16341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
16351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToNamedType = Importer.Import(T->getNamedType());
16361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToNamedType.isNull())
16371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
16381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1639465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Importer.getToContext().getElaboratedType(T->getKeyword(),
1640465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                                   ToQualifier, ToNamedType);
16411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
16421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1643f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
16441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  ObjCInterfaceDecl *Class
16451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
16461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!Class)
16471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
16481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1649c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCInterfaceType(Class);
1650c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
1651c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
1652f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1653c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  QualType ToBaseType = Importer.Import(T->getBaseType());
1654c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ToBaseType.isNull())
1655c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType();
1656c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
16571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
1658c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator P = T->qual_begin(),
16591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                     PEnd = T->qual_end();
16601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       P != PEnd; ++P) {
16611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ObjCProtocolDecl *Protocol
16621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
16631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Protocol)
16641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
16651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    Protocols.push_back(Protocol);
16661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
16671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1668c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCObjectType(ToBaseType,
1669c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                                   Protocols.data(),
1670c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                                   Protocols.size());
16711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
16721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1673f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType
1674f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
16751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
16761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
16771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
16781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1679c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
16801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
16811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1682089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor//----------------------------------------------------------------------------
1683089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor// Import Declarations
1684089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor//----------------------------------------------------------------------------
1685a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregorbool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1686a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      DeclContext *&LexicalDC,
1687a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      DeclarationName &Name,
1688a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      SourceLocation &Loc) {
1689089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Import the context of this declaration.
1690a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DC = Importer.ImportContext(D->getDeclContext());
1691089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  if (!DC)
1692a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return true;
1693a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1694a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  LexicalDC = DC;
16959bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
16969bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
16979bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    if (!LexicalDC)
1698a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      return true;
16999bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  }
1700a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1701089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Import the name of this declaration.
1702a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Name = Importer.Import(D->getDeclName());
1703089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  if (D->getDeclName() && !Name)
1704a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return true;
1705a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1706a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the location of this declaration.
1707a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Loc = Importer.Import(D->getLocation());
1708a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  return false;
1709a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1710a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
17112577743c5650c646fb705df01403707e94f2df04Abramo Bagnaravoid
17122577743c5650c646fb705df01403707e94f2df04Abramo BagnaraASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
17132577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                          DeclarationNameInfo& To) {
17142577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  // NOTE: To.Name and To.Loc are already imported.
17152577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  // We only have to import To.LocInfo.
17162577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  switch (To.getName().getNameKind()) {
17172577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::Identifier:
17182577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::ObjCZeroArgSelector:
17192577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::ObjCOneArgSelector:
17202577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::ObjCMultiArgSelector:
17212577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXUsingDirective:
17222577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
17232577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
17242577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXOperatorName: {
17252577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    SourceRange Range = From.getCXXOperatorNameRange();
17262577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    To.setCXXOperatorNameRange(Importer.Import(Range));
17272577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
17282577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
17292577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXLiteralOperatorName: {
17302577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
17312577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
17322577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
17332577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
17342577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXConstructorName:
17352577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXDestructorName:
17362577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXConversionFunctionName: {
17372577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
17382577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    To.setNamedTypeInfo(Importer.Import(FromTInfo));
17392577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
17402577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
17412577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    assert(0 && "Unknown name kind.");
17422577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
17432577743c5650c646fb705df01403707e94f2df04Abramo Bagnara}
17442577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
1745d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregorvoid ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1746d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor  if (Importer.isMinimalImport() && !ForceImport) {
1747d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    if (DeclContext *ToDC = Importer.ImportContext(FromDC)) {
1748d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor      ToDC->setHasExternalLexicalStorage();
1749d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor      ToDC->setHasExternalVisibleStorage();
1750d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    }
1751d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    return;
1752d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor  }
1753d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor
1754083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1755083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor                               FromEnd = FromDC->decls_end();
1756083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor       From != FromEnd;
1757083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor       ++From)
1758083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    Importer.Import(*From);
1759083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor}
1760083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor
1761d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregorbool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) {
1762d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (To->getDefinition())
1763d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return false;
1764d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1765d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  To->startDefinition();
1766d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1767d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Add base classes.
1768d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1769d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1770d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1771d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1772d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (CXXRecordDecl::base_class_iterator
1773d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                  Base1 = FromCXX->bases_begin(),
1774d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor            FromBaseEnd = FromCXX->bases_end();
1775d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor         Base1 != FromBaseEnd;
1776d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor         ++Base1) {
1777d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      QualType T = Importer.Import(Base1->getType());
1778d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (T.isNull())
1779c04d9d1be026cb201a716df1cd28a88878958beeDouglas Gregor        return true;
1780f90b27ad077c3339b62befc892382845339f9490Douglas Gregor
1781f90b27ad077c3339b62befc892382845339f9490Douglas Gregor      SourceLocation EllipsisLoc;
1782f90b27ad077c3339b62befc892382845339f9490Douglas Gregor      if (Base1->isPackExpansion())
1783f90b27ad077c3339b62befc892382845339f9490Douglas Gregor        EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
1784d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1785d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      Bases.push_back(
1786d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                    new (Importer.getToContext())
1787d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                      CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1788d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                       Base1->isVirtual(),
1789d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                       Base1->isBaseOfClass(),
1790d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                       Base1->getAccessSpecifierAsWritten(),
1791f90b27ad077c3339b62befc892382845339f9490Douglas Gregor                                   Importer.Import(Base1->getTypeSourceInfo()),
1792f90b27ad077c3339b62befc892382845339f9490Douglas Gregor                                       EllipsisLoc));
1793d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
1794d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Bases.empty())
1795d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      ToCXX->setBases(Bases.data(), Bases.size());
1796d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1797d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1798d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ImportDeclContext(From);
1799d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  To->completeDefinition();
1800c04d9d1be026cb201a716df1cd28a88878958beeDouglas Gregor  return false;
1801d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1802d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1803040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorTemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1804040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                TemplateParameterList *Params) {
1805040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  llvm::SmallVector<NamedDecl *, 4> ToParams;
1806040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  ToParams.reserve(Params->size());
1807040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  for (TemplateParameterList::iterator P = Params->begin(),
1808040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                    PEnd = Params->end();
1809040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor       P != PEnd; ++P) {
1810040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *To = Importer.Import(*P);
1811040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!To)
1812040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return 0;
1813040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1814040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    ToParams.push_back(cast<NamedDecl>(To));
1815040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1816040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1817040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return TemplateParameterList::Create(Importer.getToContext(),
1818040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       Importer.Import(Params->getTemplateLoc()),
1819040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       Importer.Import(Params->getLAngleLoc()),
1820040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       ToParams.data(), ToParams.size(),
1821040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       Importer.Import(Params->getRAngleLoc()));
1822040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1823040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1824d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorTemplateArgument
1825d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1826d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  switch (From.getKind()) {
1827d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Null:
1828d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument();
1829d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1830d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Type: {
1831d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualType ToType = Importer.Import(From.getAsType());
1832d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToType.isNull())
1833d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1834d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(ToType);
1835d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1836d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1837d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Integral: {
1838d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualType ToType = Importer.Import(From.getIntegralType());
1839d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToType.isNull())
1840d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1841d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(*From.getAsIntegral(), ToType);
1842d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1843d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1844d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Declaration:
1845d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Decl *To = Importer.Import(From.getAsDecl()))
1846d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument(To);
1847d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument();
1848d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1849d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Template: {
1850d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1851d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToTemplate.isNull())
1852d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1853d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1854d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(ToTemplate);
1855d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1856a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
1857a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  case TemplateArgument::TemplateExpansion: {
1858a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    TemplateName ToTemplate
1859a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      = Importer.Import(From.getAsTemplateOrTemplatePattern());
1860a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    if (ToTemplate.isNull())
1861a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      return TemplateArgument();
1862a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
18632be29f423acad3bbe39099a78db2805acb5bdf17Douglas Gregor    return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
1864a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  }
1865a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
1866d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Expression:
1867d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1868d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument(ToExpr);
1869d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument();
1870d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1871d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Pack: {
1872d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    llvm::SmallVector<TemplateArgument, 2> ToPack;
1873d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ToPack.reserve(From.pack_size());
1874d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1875d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1876d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1877d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    TemplateArgument *ToArgs
1878d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1879d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1880d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(ToArgs, ToPack.size());
1881d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1882d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1883d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1884d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm_unreachable("Invalid template argument kind");
1885d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return TemplateArgument();
1886d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1887d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1888d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregorbool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1889d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                              unsigned NumFromArgs,
1890d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                              llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1891d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  for (unsigned I = 0; I != NumFromArgs; ++I) {
1892d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1893d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (To.isNull() && !FromArgs[I].isNull())
1894d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return true;
1895d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1896d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ToArgs.push_back(To);
1897d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1898d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1899d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return false;
1900d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1901d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
190296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregorbool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
190373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        RecordDecl *ToRecord) {
1904bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
190573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Importer.getToContext(),
1906ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getNonEquivalentDecls());
1907bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
190896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
190996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
191036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregorbool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
1911bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
191273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Importer.getToContext(),
1913ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getNonEquivalentDecls());
1914bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
191536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
191636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
1917040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorbool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1918040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                        ClassTemplateDecl *To) {
1919040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1920040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                   Importer.getToContext(),
1921040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                   Importer.getNonEquivalentDecls());
1922040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return Ctx.IsStructurallyEquivalent(From, To);
1923040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1924040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1925a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitDecl(Decl *D) {
1926a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1927a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    << D->getDeclKindName();
1928a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  return 0;
1929a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1930a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1931788c62d1e87bfb596078817237f672a5f000999aDouglas GregorDecl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1932788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  // Import the major distinguishing characteristics of this namespace.
1933788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  DeclContext *DC, *LexicalDC;
1934788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  DeclarationName Name;
1935788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  SourceLocation Loc;
1936788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1937788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    return 0;
1938788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1939788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  NamespaceDecl *MergeWithNamespace = 0;
1940788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (!Name) {
1941788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // This is an anonymous namespace. Adopt an existing anonymous
1942788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // namespace if we can.
1943788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // FIXME: Not testable.
1944788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1945788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      MergeWithNamespace = TU->getAnonymousNamespace();
1946788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    else
1947788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1948788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  } else {
1949788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1950788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1951788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor         Lookup.first != Lookup.second;
1952788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor         ++Lookup.first) {
19530d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
1954788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        continue;
1955788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1956788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1957788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        MergeWithNamespace = FoundNS;
1958788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        ConflictingDecls.clear();
1959788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        break;
1960788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      }
1961788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1962788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
1963788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
1964788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1965788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (!ConflictingDecls.empty()) {
19660d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
1967788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                         ConflictingDecls.data(),
1968788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                         ConflictingDecls.size());
1969788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
1970788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  }
1971788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1972788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  // Create the "to" namespace, if needed.
1973788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  NamespaceDecl *ToNamespace = MergeWithNamespace;
1974788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (!ToNamespace) {
1975acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara    ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
1976acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara                                        Importer.Import(D->getLocStart()),
1977acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara                                        Loc, Name.getAsIdentifierInfo());
1978788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    ToNamespace->setLexicalDeclContext(LexicalDC);
1979788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    LexicalDC->addDecl(ToNamespace);
1980788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1981788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // If this is an anonymous namespace, register it as the anonymous
1982788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // namespace within its context.
1983788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (!Name) {
1984788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1985788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        TU->setAnonymousNamespace(ToNamespace);
1986788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      else
1987788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1988788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
1989788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  }
1990788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  Importer.Imported(D, ToNamespace);
1991788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1992788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  ImportDeclContext(D);
1993788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
1994788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  return ToNamespace;
1995788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor}
1996788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
19979e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas GregorDecl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
19989e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // Import the major distinguishing characteristics of this typedef.
19999e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  DeclContext *DC, *LexicalDC;
20009e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  DeclarationName Name;
20019e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  SourceLocation Loc;
20029e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
20039e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    return 0;
20049e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
20059e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // If this typedef is not in block scope, determine whether we've
20069e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // seen a typedef with the same name (that we can merge with) or any
20079e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // other entity by that name (which name lookup could conflict with).
20089e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  if (!DC->isFunctionOrMethod()) {
20099e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
20109e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
20119e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
20129e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor         Lookup.first != Lookup.second;
20139e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor         ++Lookup.first) {
20149e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
20159e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor        continue;
20169e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
2017ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2018ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                            FoundTypedef->getUnderlyingType()))
20195ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor          return Importer.Imported(D, FoundTypedef);
20209e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      }
20219e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
20229e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
20239e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    }
20249e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
20259e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    if (!ConflictingDecls.empty()) {
20269e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
20279e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                         ConflictingDecls.data(),
20289e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                         ConflictingDecls.size());
20299e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (!Name)
20309e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor        return 0;
20319e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    }
20329e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  }
20339e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
2034ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the underlying type of this typedef;
2035ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getUnderlyingType());
2036ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2037ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2038ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
20399e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // Create the new typedef node.
20409e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2041344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara  SourceLocation StartL = Importer.Import(D->getLocStart());
20429e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2043344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               StartL, Loc,
2044344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               Name.getAsIdentifierInfo(),
20459e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                               TInfo);
2046325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToTypedef->setAccess(D->getAccess());
20479e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  ToTypedef->setLexicalDeclContext(LexicalDC);
20485ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToTypedef);
20499e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  LexicalDC->addDecl(ToTypedef);
2050ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
20519e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  return ToTypedef;
20529e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor}
20539e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
205436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorDecl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
205536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the major distinguishing characteristics of this enum.
205636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclContext *DC, *LexicalDC;
205736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName Name;
205836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  SourceLocation Loc;
205936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
206036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
206136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
206236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Figure out what enum name we're looking for.
206336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  unsigned IDNS = Decl::IDNS_Tag;
206436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName SearchName = Name;
206536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!SearchName && D->getTypedefForAnonDecl()) {
206636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
206736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    IDNS = Decl::IDNS_Ordinary;
206836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  } else if (Importer.getToContext().getLangOptions().CPlusPlus)
206936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    IDNS |= Decl::IDNS_Ordinary;
207036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
207136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // We may already have an enum of the same name; try to find and match it.
207236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!DC->isFunctionOrMethod() && SearchName) {
207336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
207436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
207536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         Lookup.first != Lookup.second;
207636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++Lookup.first) {
207736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
207836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        continue;
207936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
208036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Decl *Found = *Lookup.first;
208136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
208236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
208336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor          Found = Tag->getDecl();
208436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      }
208536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
208636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
20875ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor        if (IsStructuralMatch(D, FoundEnum))
20885ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor          return Importer.Imported(D, FoundEnum);
208936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      }
209036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
209136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
209236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
209336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
209436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (!ConflictingDecls.empty()) {
209536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
209636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.data(),
209736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.size());
209836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
209936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
210036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
210136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Create the enum declaration.
2102ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2103ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                  Importer.Import(D->getLocStart()),
2104ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                  Loc, Name.getAsIdentifierInfo(), 0,
2105a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                  D->isScoped(), D->isScopedUsingClassTag(),
2106a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                  D->isFixed());
2107b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
2108c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2109325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  D2->setAccess(D->getAccess());
211073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  D2->setLexicalDeclContext(LexicalDC);
211173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Importer.Imported(D, D2);
211273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  LexicalDC->addDecl(D2);
211336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
211436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the integer type.
211536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  QualType ToIntegerType = Importer.Import(D->getIntegerType());
211636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (ToIntegerType.isNull())
211736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
211873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  D2->setIntegerType(ToIntegerType);
211936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
212036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the definition
212136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (D->isDefinition()) {
212236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
212336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (T.isNull())
212436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      return 0;
212536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
212636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    QualType ToPromotionType = Importer.Import(D->getPromotionType());
212736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (ToPromotionType.isNull())
212836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      return 0;
212936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
213073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->startDefinition();
2131083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    ImportDeclContext(D);
21321b5a618c59025898806160ed5e7f0ff5bb79e482John McCall
21331b5a618c59025898806160ed5e7f0ff5bb79e482John McCall    // FIXME: we might need to merge the number of positive or negative bits
21341b5a618c59025898806160ed5e7f0ff5bb79e482John McCall    // if the enumerator lists don't match.
21351b5a618c59025898806160ed5e7f0ff5bb79e482John McCall    D2->completeDefinition(T, ToPromotionType,
21361b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                           D->getNumPositiveBits(),
21371b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                           D->getNumNegativeBits());
213836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
213936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
214073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return D2;
214136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
214236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
214396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas GregorDecl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
214496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // If this record has a definition in the translation unit we're coming from,
214596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // but this particular declaration is not that definition, import the
214696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // definition and map to that.
2147952b017601f9c82b51119c3a1600f1312a833db9Douglas Gregor  TagDecl *Definition = D->getDefinition();
214896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (Definition && Definition != D) {
214996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *ImportedDef = Importer.Import(Definition);
21505ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor    if (!ImportedDef)
21515ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor      return 0;
21525ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
21535ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor    return Importer.Imported(D, ImportedDef);
215496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
215596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
215696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Import the major distinguishing characteristics of this record.
215796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclContext *DC, *LexicalDC;
215896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName Name;
215996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  SourceLocation Loc;
216096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
216196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
216296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
216396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Figure out what structure name we're looking for.
216496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  unsigned IDNS = Decl::IDNS_Tag;
216596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName SearchName = Name;
216696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!SearchName && D->getTypedefForAnonDecl()) {
216796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
216896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    IDNS = Decl::IDNS_Ordinary;
216996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  } else if (Importer.getToContext().getLangOptions().CPlusPlus)
217096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    IDNS |= Decl::IDNS_Ordinary;
217196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
217296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // We may already have a record of the same name; try to find and match it.
2173e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor  RecordDecl *AdoptDecl = 0;
217496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!DC->isFunctionOrMethod() && SearchName) {
217596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
217696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
217796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         Lookup.first != Lookup.second;
217896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         ++Lookup.first) {
217996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
218096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor        continue;
218196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
218296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Decl *Found = *Lookup.first;
218396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
218496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor        if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
218596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor          Found = Tag->getDecl();
218696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
218796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
218896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
2189e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2190e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2191e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // The record types structurally match, or the "from" translation
2192e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // unit only had a forward declaration anyway; call it the same
2193e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // function.
2194e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // FIXME: For C++, we should also merge methods here.
21955ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor            return Importer.Imported(D, FoundDef);
2196e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          }
2197e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        } else {
2198e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          // We have a forward declaration of this type, so adopt that forward
2199e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          // declaration rather than building a new one.
2200e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          AdoptDecl = FoundRecord;
2201e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          continue;
2202e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        }
220396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
220496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
220596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
220696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
220796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
220896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    if (!ConflictingDecls.empty()) {
220996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
221096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         ConflictingDecls.data(),
221196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         ConflictingDecls.size());
221296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
221396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
221496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
221596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Create the record declaration.
221673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  RecordDecl *D2 = AdoptDecl;
2217ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation StartLoc = Importer.Import(D->getLocStart());
221873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!D2) {
22195250f27420386452a21692a6292c99ee7febdac4John McCall    if (isa<CXXRecordDecl>(D)) {
222073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2221e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   D->getTagKind(),
2222ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                   DC, StartLoc, Loc,
2223ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                   Name.getAsIdentifierInfo());
222473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      D2 = D2CXX;
2225325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor      D2->setAccess(D->getAccess());
2226e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor    } else {
222773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
2228ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                              DC, StartLoc, Loc, Name.getAsIdentifierInfo());
222996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
2230c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor
2231c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
223273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->setLexicalDeclContext(LexicalDC);
223373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    LexicalDC->addDecl(D2);
223496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
22355ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
223673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Importer.Imported(D, D2);
2237e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor
2238d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D->isDefinition() && ImportDefinition(D, D2))
2239d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
224096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
224173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return D2;
224296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
224396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
224436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorDecl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
224536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the major distinguishing characteristics of this enumerator.
224636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclContext *DC, *LexicalDC;
224736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName Name;
224836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  SourceLocation Loc;
2249ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
225036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
2251ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2252ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2253ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2254ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2255ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
225636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Determine whether there are any other declarations with the same name and
225736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // in the same context.
225836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!LexicalDC->isFunctionOrMethod()) {
225936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
226036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
226136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
226236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         Lookup.first != Lookup.second;
226336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++Lookup.first) {
226436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
226536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        continue;
226636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
226736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
226836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
226936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
227036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (!ConflictingDecls.empty()) {
227136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
227236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.data(),
227336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.size());
227436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!Name)
227536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        return 0;
227636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
227736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
227836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
227936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  Expr *Init = Importer.Import(D->getInitExpr());
228036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (D->getInitExpr() && !Init)
228136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
228236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
228336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  EnumConstantDecl *ToEnumerator
228436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
228536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                               Name.getAsIdentifierInfo(), T,
228636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                               Init, D->getInitVal());
2287325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToEnumerator->setAccess(D->getAccess());
228836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  ToEnumerator->setLexicalDeclContext(LexicalDC);
22895ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToEnumerator);
229036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  LexicalDC->addDecl(ToEnumerator);
229136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  return ToEnumerator;
229236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
229396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
2294a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2295a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the major distinguishing characteristics of this function.
2296a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC, *LexicalDC;
2297a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclarationName Name;
2298a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc;
2299ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2300089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    return 0;
23012577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
2302a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Try to find a function in our own ("to") context with the same name, same
2303a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // type, and in the same context as the function we're importing.
2304a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  if (!LexicalDC->isFunctionOrMethod()) {
2305a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2306a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
2307a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2308a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor         Lookup.first != Lookup.second;
2309a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor         ++Lookup.first) {
2310a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2311a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        continue;
2312a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2313a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2314a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        if (isExternalLinkage(FoundFunction->getLinkage()) &&
2315a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            isExternalLinkage(D->getLinkage())) {
2316ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          if (Importer.IsStructurallyEquivalent(D->getType(),
2317ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                FoundFunction->getType())) {
2318a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            // FIXME: Actually try to merge the body and other attributes.
23195ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor            return Importer.Imported(D, FoundFunction);
2320a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          }
2321a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2322a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // FIXME: Check for overloading more carefully, e.g., by boosting
2323a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Sema::IsOverload out to the AST library.
2324a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2325a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Function overloading is okay in C++.
2326a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          if (Importer.getToContext().getLangOptions().CPlusPlus)
2327a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            continue;
2328a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2329a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Complain about inconsistent function types.
2330a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
2331ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            << Name << D->getType() << FoundFunction->getType();
2332a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          Importer.ToDiag(FoundFunction->getLocation(),
2333a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                          diag::note_odr_value_here)
2334a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            << FoundFunction->getType();
2335a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        }
2336a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      }
2337a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2338a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
2339a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    }
2340a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2341a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    if (!ConflictingDecls.empty()) {
2342a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
2343a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                         ConflictingDecls.data(),
2344a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                         ConflictingDecls.size());
2345a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (!Name)
2346a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        return 0;
2347a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    }
2348a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
2349ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
23502577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo(Name, Loc);
23512577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  // Import additional name location/type info.
23522577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
23532577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
2354ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
2355ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2356ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2357ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2358a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2359a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the function parameters.
2360a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2361a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2362a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor       P != PEnd; ++P) {
2363a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2364a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    if (!ToP)
2365a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      return 0;
2366a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2367a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Parameters.push_back(ToP);
2368a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
2369a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2370a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Create the imported function.
2371a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2372c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  FunctionDecl *ToFunction = 0;
2373c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2374c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2375c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            cast<CXXRecordDecl>(DC),
2376ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
23772577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                            NameInfo, T, TInfo,
2378c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            FromConstructor->isExplicit(),
2379c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            D->isInlineSpecified(),
2380c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            D->isImplicit());
2381c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else if (isa<CXXDestructorDecl>(D)) {
2382c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2383c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           cast<CXXRecordDecl>(DC),
2384ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           D->getInnerLocStart(),
2385b41d899a6023385c00a61eb9dd3e44db9dc7994eCraig Silverstein                                           NameInfo, T, TInfo,
2386c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isInlineSpecified(),
2387c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isImplicit());
2388c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else if (CXXConversionDecl *FromConversion
2389c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           = dyn_cast<CXXConversionDecl>(D)) {
2390c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2391c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           cast<CXXRecordDecl>(DC),
2392ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           D->getInnerLocStart(),
23932577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                           NameInfo, T, TInfo,
2394c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isInlineSpecified(),
2395f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                           FromConversion->isExplicit(),
2396f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                           Importer.Import(D->getLocEnd()));
23970629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor  } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
23980629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor    ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
23990629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       cast<CXXRecordDecl>(DC),
2400ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       D->getInnerLocStart(),
24010629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       NameInfo, T, TInfo,
24020629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       Method->isStatic(),
24030629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       Method->getStorageClassAsWritten(),
2404f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                       Method->isInlineSpecified(),
2405f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                       Importer.Import(D->getLocEnd()));
2406c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else {
24072577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2408ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                      D->getInnerLocStart(),
24092577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                      NameInfo, T, TInfo, D->getStorageClass(),
241016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                      D->getStorageClassAsWritten(),
2411c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                      D->isInlineSpecified(),
2412c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                      D->hasWrittenPrototype());
2413c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  }
2414b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2415b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
2416c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2417325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToFunction->setAccess(D->getAccess());
2418c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToFunction->setLexicalDeclContext(LexicalDC);
2419f2eca2cf302c50b79891f24b3861d64ea9263831John McCall  ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2420f2eca2cf302c50b79891f24b3861d64ea9263831John McCall  ToFunction->setTrivial(D->isTrivial());
2421f2eca2cf302c50b79891f24b3861d64ea9263831John McCall  ToFunction->setPure(D->isPure());
2422c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Importer.Imported(D, ToFunction);
2423a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2424a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Set the parameters.
2425a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
2426c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Parameters[I]->setOwningFunction(ToFunction);
2427c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToFunction->addDecl(Parameters[I]);
2428a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
2429c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToFunction->setParams(Parameters.data(), Parameters.size());
2430a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2431a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // FIXME: Other bits to merge?
243281134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor
243381134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor  // Add this function to the lexical context.
243481134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor  LexicalDC->addDecl(ToFunction);
243581134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor
2436c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToFunction;
2437a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
2438a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2439c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2440c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitFunctionDecl(D);
2441c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2442c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
2443c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2444c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
2445c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2446c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
2447c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2448c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
2449c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2450c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
2451c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2452c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
2453c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2454c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
245596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas GregorDecl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
245696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Import the major distinguishing characteristics of a variable.
245796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclContext *DC, *LexicalDC;
245896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName Name;
245996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  SourceLocation Loc;
2460ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2461ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2462ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2463ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
2464ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2465ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
246696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
246796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
246896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
246996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  Expr *BitWidth = Importer.Import(D->getBitWidth());
247096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!BitWidth && D->getBitWidth())
247196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
247296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
2473ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2474ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                         Importer.Import(D->getInnerLocStart()),
247596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         Loc, Name.getAsIdentifierInfo(),
247696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         T, TInfo, BitWidth, D->isMutable());
2477325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToField->setAccess(D->getAccess());
247896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  ToField->setLexicalDeclContext(LexicalDC);
24795ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToField);
248096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  LexicalDC->addDecl(ToField);
248196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  return ToField;
248296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
248396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
248487c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetDecl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
248587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  // Import the major distinguishing characteristics of a variable.
248687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  DeclContext *DC, *LexicalDC;
248787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  DeclarationName Name;
248887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  SourceLocation Loc;
248987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
249087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    return 0;
249187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
249287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  // Import the type.
249387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  QualType T = Importer.Import(D->getType());
249487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (T.isNull())
249587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    return 0;
249687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
249787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  NamedDecl **NamedChain =
249887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
249987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
250087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  unsigned i = 0;
250187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
250287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       PE = D->chain_end(); PI != PE; ++PI) {
250387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    Decl* D = Importer.Import(*PI);
250487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    if (!D)
250587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet      return 0;
250687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    NamedChain[i++] = cast<NamedDecl>(D);
250787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  }
250887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
250987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
251087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                         Importer.getToContext(), DC,
251187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                         Loc, Name.getAsIdentifierInfo(), T,
251287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                         NamedChain, D->getChainingSize());
251387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  ToIndirectField->setAccess(D->getAccess());
251487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  ToIndirectField->setLexicalDeclContext(LexicalDC);
251587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  Importer.Imported(D, ToIndirectField);
251687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  LexicalDC->addDecl(ToIndirectField);
251787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return ToIndirectField;
251887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
251987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
25202e55e3af2f6d6c0509495357fade95105dd144cdDouglas GregorDecl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
25212e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Import the major distinguishing characteristics of an ivar.
25222e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  DeclContext *DC, *LexicalDC;
25232e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  DeclarationName Name;
25242e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  SourceLocation Loc;
25252e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
25262e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
25272e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
25282e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Determine whether we've already imported this ivar
25292e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
25302e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor       Lookup.first != Lookup.second;
25312e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor       ++Lookup.first) {
25322e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
25332e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (Importer.IsStructurallyEquivalent(D->getType(),
25342e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                            FoundIvar->getType())) {
25352e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.Imported(D, FoundIvar);
25362e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        return FoundIvar;
25372e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      }
25382e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
25392e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
25402e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << Name << D->getType() << FoundIvar->getType();
25412e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
25422e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << FoundIvar->getType();
25432e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      return 0;
25442e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    }
25452e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  }
25462e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
25472e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Import the type.
25482e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  QualType T = Importer.Import(D->getType());
25492e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (T.isNull())
25502e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
25512e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
25522e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
25532e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  Expr *BitWidth = Importer.Import(D->getBitWidth());
25542e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (!BitWidth && D->getBitWidth())
25552e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
25562e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
2557a06549226f45d5b72169a3d054415616dd1014a2Daniel Dunbar  ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2558a06549226f45d5b72169a3d054415616dd1014a2Daniel Dunbar                                              cast<ObjCContainerDecl>(DC),
2559ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       Importer.Import(D->getInnerLocStart()),
25602e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              Loc, Name.getAsIdentifierInfo(),
25612e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              T, TInfo, D->getAccessControl(),
2562ac0021ba802e193e0f9f8207768c7862c7603bc0Fariborz Jahanian                                              BitWidth, D->getSynthesize());
25632e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  ToIvar->setLexicalDeclContext(LexicalDC);
25642e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  Importer.Imported(D, ToIvar);
25652e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  LexicalDC->addDecl(ToIvar);
25662e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  return ToIvar;
25672e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
25682e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor}
25692e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
2570a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2571a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the major distinguishing characteristics of a variable.
2572a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC, *LexicalDC;
2573a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclarationName Name;
2574a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc;
2575ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2576a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return 0;
2577089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2578089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Try to find a variable in our own ("to") context with the same name and
2579089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // in the same context as the variable we're importing.
25809bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (D->isFileVarDecl()) {
2581089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    VarDecl *MergeWithVar = 0;
2582089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2583089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
25849bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2585089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor         Lookup.first != Lookup.second;
2586089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor         ++Lookup.first) {
2587089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2588089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        continue;
2589089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2590089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2591089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        // We have found a variable that we may need to merge with. Check it.
2592089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        if (isExternalLinkage(FoundVar->getLinkage()) &&
2593089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            isExternalLinkage(D->getLinkage())) {
2594ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          if (Importer.IsStructurallyEquivalent(D->getType(),
2595ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                FoundVar->getType())) {
2596089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            MergeWithVar = FoundVar;
2597089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            break;
2598089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          }
2599089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2600d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          const ArrayType *FoundArray
2601d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            = Importer.getToContext().getAsArrayType(FoundVar->getType());
2602d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          const ArrayType *TArray
2603ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            = Importer.getToContext().getAsArrayType(D->getType());
2604d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          if (FoundArray && TArray) {
2605d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            if (isa<IncompleteArrayType>(FoundArray) &&
2606d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor                isa<ConstantArrayType>(TArray)) {
2607ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              // Import the type.
2608ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              QualType T = Importer.Import(D->getType());
2609ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              if (T.isNull())
2610ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                return 0;
2611ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2612d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              FoundVar->setType(T);
2613d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              MergeWithVar = FoundVar;
2614d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              break;
2615d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            } else if (isa<IncompleteArrayType>(TArray) &&
2616d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor                       isa<ConstantArrayType>(FoundArray)) {
2617d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              MergeWithVar = FoundVar;
2618d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              break;
26190f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor            }
26200f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor          }
26210f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor
2622089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
2623ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            << Name << D->getType() << FoundVar->getType();
2624089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2625089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            << FoundVar->getType();
2626089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        }
2627089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      }
2628089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2629089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
2630089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2631089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2632089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    if (MergeWithVar) {
2633089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      // An equivalent variable with external linkage has been found. Link
2634089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      // the two declarations, then merge them.
26355ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor      Importer.Imported(D, MergeWithVar);
2636089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2637089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (VarDecl *DDef = D->getDefinition()) {
2638089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2639089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(ExistingDef->getLocation(),
2640089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                          diag::err_odr_variable_multiple_def)
2641089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            << Name;
2642089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2643089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        } else {
2644089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Expr *Init = Importer.Import(DDef->getInit());
2645838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor          MergeWithVar->setInit(Init);
2646089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        }
2647089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      }
2648089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2649089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      return MergeWithVar;
2650089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2651089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2652089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    if (!ConflictingDecls.empty()) {
2653089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
2654089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                         ConflictingDecls.data(),
2655089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                         ConflictingDecls.size());
2656089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (!Name)
2657089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        return 0;
2658089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2659089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  }
266082fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2661ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
2662ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2663ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2664ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2665ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2666089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Create the imported variable.
266782fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2668ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2669ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   Importer.Import(D->getInnerLocStart()),
2670ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   Loc, Name.getAsIdentifierInfo(),
2671ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   T, TInfo,
267216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClass(),
267316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten());
2674c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2675325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToVar->setAccess(D->getAccess());
26769bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ToVar->setLexicalDeclContext(LexicalDC);
26775ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToVar);
26789bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  LexicalDC->addDecl(ToVar);
26799bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
2680089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Merge the initializer.
2681089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // FIXME: Can we really import any initializer? Alternatively, we could force
2682089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // ourselves to import every declaration of a variable and then only use
2683089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // getInit() here.
2684838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
2685089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2686089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // FIXME: Other bits to merge?
2687089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2688089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  return ToVar;
2689089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
2690089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
26912cd00932b9b5403047139ce8cfaa3ae47966f894Douglas GregorDecl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
26922cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Parameters are created in the translation unit's context, then moved
26932cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // into the function declaration's context afterward.
26942cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
26952cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
26962cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the name of this declaration.
26972cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
26982cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  if (D->getDeclName() && !Name)
26992cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    return 0;
27002cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
27012cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the location of this declaration.
27022cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
27032cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
27042cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the parameter's type.
27052cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  QualType T = Importer.Import(D->getType());
27062cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  if (T.isNull())
27072cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    return 0;
27082cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
27092cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Create the imported parameter.
27102cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  ImplicitParamDecl *ToParm
27112cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    = ImplicitParamDecl::Create(Importer.getToContext(), DC,
27122cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor                                Loc, Name.getAsIdentifierInfo(),
27132cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor                                T);
27142cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  return Importer.Imported(D, ToParm);
27152cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor}
27162cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
2717a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2718a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Parameters are created in the translation unit's context, then moved
2719a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // into the function declaration's context afterward.
2720a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2721a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
272282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // Import the name of this declaration.
272382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
272482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (D->getDeclName() && !Name)
272582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
272682fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2727a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the location of this declaration.
2728a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
2729a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2730a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the parameter's type.
2731a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  QualType T = Importer.Import(D->getType());
273282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (T.isNull())
273382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
273482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2735a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Create the imported parameter.
2736a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2737a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2738ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                     Importer.Import(D->getInnerLocStart()),
2739a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            Loc, Name.getAsIdentifierInfo(),
2740a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            T, TInfo, D->getStorageClass(),
274116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                             D->getStorageClassAsWritten(),
2742a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            /*FIXME: Default argument*/ 0);
2743bf73b352acb7a2d041ce8b50171dd7f8e2b2c1bbJohn McCall  ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
27445ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  return Importer.Imported(D, ToParm);
2745a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
274682fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2747c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas GregorDecl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2748c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the major distinguishing characteristics of a method.
2749c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  DeclContext *DC, *LexicalDC;
2750c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  DeclarationName Name;
2751c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  SourceLocation Loc;
2752c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2753c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return 0;
2754c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2755c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2756c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       Lookup.first != Lookup.second;
2757c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       ++Lookup.first) {
2758c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2759c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2760c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        continue;
2761c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2762c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check return types.
2763c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2764c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                             FoundMethod->getResultType())) {
2765c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2766c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name
2767c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->getResultType() << FoundMethod->getResultType();
2768c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2769c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2770c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2771c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2772c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2773c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2774c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check the number of parameters.
2775c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (D->param_size() != FoundMethod->param_size()) {
2776c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2777c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name
2778c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->param_size() << FoundMethod->param_size();
2779c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2780c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2781c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2782c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2783c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2784c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2785c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check parameter types.
2786c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2787c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor             PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2788c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor           P != PEnd; ++P, ++FoundP) {
2789c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2790c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                               (*FoundP)->getType())) {
2791c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          Importer.FromDiag((*P)->getLocation(),
2792c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                            diag::err_odr_objc_method_param_type_inconsistent)
2793c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << D->isInstanceMethod() << Name
2794c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << (*P)->getType() << (*FoundP)->getType();
2795c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2796c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << (*FoundP)->getType();
2797c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          return 0;
2798c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        }
2799c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2800c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2801c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check variadic/non-variadic.
2802c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check the number of parameters.
2803c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (D->isVariadic() != FoundMethod->isVariadic()) {
2804c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2805c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2806c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2807c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2808c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2809c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2810c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2811c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2812c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // FIXME: Any other bits we need to merge?
2813c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      return Importer.Imported(D, FoundMethod);
2814c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    }
2815c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2816c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2817c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the result type.
2818c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  QualType ResultTy = Importer.Import(D->getResultType());
2819c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (ResultTy.isNull())
2820c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return 0;
2821c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
28224bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor  TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
28234bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor
2824c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ObjCMethodDecl *ToMethod
2825c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    = ObjCMethodDecl::Create(Importer.getToContext(),
2826c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Loc,
2827c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Importer.Import(D->getLocEnd()),
2828c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Name.getObjCSelector(),
28294bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor                             ResultTy, ResultTInfo, DC,
2830c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isInstanceMethod(),
2831c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isVariadic(),
2832c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isSynthesized(),
28333fe104154dd2e8ffb351142d74f308938b5c99bfFariborz Jahanian                             D->isDefined(),
2834c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->getImplementationControl());
2835c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2836c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // FIXME: When we decide to merge method definitions, we'll need to
2837c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // deal with implicit parameters.
2838c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2839c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the parameters
2840c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2841c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2842c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                   FromPEnd = D->param_end();
2843c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       FromP != FromPEnd;
2844c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       ++FromP) {
2845c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2846c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    if (!ToP)
2847c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      return 0;
2848c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2849c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToParams.push_back(ToP);
2850c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2851c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2852c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Set the parameters.
2853c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2854c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToParams[I]->setOwningFunction(ToMethod);
2855c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToMethod->addDecl(ToParams[I]);
2856c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2857c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToMethod->setMethodParams(Importer.getToContext(),
28584ecb25fa94897b2c03510292acace710e5262ba5Fariborz Jahanian                            ToParams.data(), ToParams.size(),
28594ecb25fa94897b2c03510292acace710e5262ba5Fariborz Jahanian                            ToParams.size());
2860c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2861c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToMethod->setLexicalDeclContext(LexicalDC);
2862c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Importer.Imported(D, ToMethod);
2863c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  LexicalDC->addDecl(ToMethod);
2864c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToMethod;
2865c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor}
2866c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2867b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas GregorDecl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2868b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import the major distinguishing characteristics of a category.
2869b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  DeclContext *DC, *LexicalDC;
2870b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  DeclarationName Name;
2871b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  SourceLocation Loc;
2872b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2873b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    return 0;
2874b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2875b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCInterfaceDecl *ToInterface
2876b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2877b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (!ToInterface)
2878b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    return 0;
2879b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2880b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Determine if we've already encountered this category.
2881b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCCategoryDecl *MergeWithCategory
2882b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2883b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCCategoryDecl *ToCategory = MergeWithCategory;
2884b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (!ToCategory) {
2885b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2886b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Importer.Import(D->getAtLoc()),
2887b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Loc,
2888b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                       Importer.Import(D->getCategoryNameLoc()),
2889b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Name.getAsIdentifierInfo());
2890b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setLexicalDeclContext(LexicalDC);
2891b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    LexicalDC->addDecl(ToCategory);
2892b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Imported(D, ToCategory);
2893b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2894b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // Link this category into its class's category list.
2895b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setClassInterface(ToInterface);
2896b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->insertNextClassCategory();
2897b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2898b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // Import protocols
2899b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2900b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2901b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2902b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      = D->protocol_loc_begin();
2903b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2904b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          FromProtoEnd = D->protocol_end();
2905b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor         FromProto != FromProtoEnd;
2906b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor         ++FromProto, ++FromProtoLoc) {
2907b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      ObjCProtocolDecl *ToProto
2908b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2909b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      if (!ToProto)
2910b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor        return 0;
2911b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      Protocols.push_back(ToProto);
2912b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2913b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    }
2914b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2915b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
2916b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2917b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                ProtocolLocs.data(), Importer.getToContext());
2918b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2919b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  } else {
2920b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Imported(D, ToCategory);
2921b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  }
2922b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2923b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import all of the members of this category.
2924083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
2925b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2926b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // If we have an implementation, import it as well.
2927b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (D->getImplementation()) {
2928b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ObjCCategoryImplDecl *Impl
2929cad2c59b0c087edea83d0fbf6eabde4a7960c778Douglas Gregor      = cast_or_null<ObjCCategoryImplDecl>(
2930cad2c59b0c087edea83d0fbf6eabde4a7960c778Douglas Gregor                                       Importer.Import(D->getImplementation()));
2931b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    if (!Impl)
2932b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      return 0;
2933b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2934b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setImplementation(Impl);
2935b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  }
2936b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2937b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  return ToCategory;
2938b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor}
2939b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
29402e2a400383c7a64a927f61eeed596b08928e1d4bDouglas GregorDecl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
2941b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import the major distinguishing characteristics of a protocol.
29422e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  DeclContext *DC, *LexicalDC;
29432e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  DeclarationName Name;
29442e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  SourceLocation Loc;
29452e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
29462e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    return 0;
29472e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
29482e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  ObjCProtocolDecl *MergeWithProtocol = 0;
29492e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
29502e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       Lookup.first != Lookup.second;
29512e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++Lookup.first) {
29522e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
29532e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      continue;
29542e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
29552e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
29562e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      break;
29572e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  }
29582e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
29592e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  ObjCProtocolDecl *ToProto = MergeWithProtocol;
29602e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  if (!ToProto || ToProto->isForwardDecl()) {
29612e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if (!ToProto) {
29622e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
29632e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                                         Name.getAsIdentifierInfo());
29642e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto->setForwardDecl(D->isForwardDecl());
29652e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto->setLexicalDeclContext(LexicalDC);
29662e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      LexicalDC->addDecl(ToProto);
29672e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    }
29682e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Imported(D, ToProto);
29692e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
29702e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    // Import protocols
29712e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
29722e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
29732e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    ObjCProtocolDecl::protocol_loc_iterator
29742e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      FromProtoLoc = D->protocol_loc_begin();
29752e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
29762e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                                          FromProtoEnd = D->protocol_end();
29772e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       FromProto != FromProtoEnd;
29782e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++FromProto, ++FromProtoLoc) {
29792e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ObjCProtocolDecl *ToProto
29802e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
29812e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      if (!ToProto)
29822e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor        return 0;
29832e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      Protocols.push_back(ToProto);
29842e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
29852e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    }
29862e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
29872e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
29882e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    ToProto->setProtocolList(Protocols.data(), Protocols.size(),
29892e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                             ProtocolLocs.data(), Importer.getToContext());
29902e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  } else {
29912e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Imported(D, ToProto);
29922e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  }
29932e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
2994b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import all of the members of this protocol.
2995083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
29962e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
29972e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  return ToProto;
29982e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor}
29992e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
3000a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas GregorDecl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3001a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // Import the major distinguishing characteristics of an @interface.
3002a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  DeclContext *DC, *LexicalDC;
3003a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  DeclarationName Name;
3004a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  SourceLocation Loc;
3005a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3006a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    return 0;
3007a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3008a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  ObjCInterfaceDecl *MergeWithIface = 0;
3009a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3010a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       Lookup.first != Lookup.second;
3011a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++Lookup.first) {
3012a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3013a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      continue;
3014a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3015a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
3016a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      break;
3017a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
3018a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3019a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  ObjCInterfaceDecl *ToIface = MergeWithIface;
3020a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (!ToIface || ToIface->isForwardDecl()) {
3021a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!ToIface) {
3022a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
3023a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          DC, Loc,
3024a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          Name.getAsIdentifierInfo(),
3025deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor                                          Importer.Import(D->getClassLoc()),
3026a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          D->isForwardDecl(),
3027a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          D->isImplicitInterfaceDecl());
30282e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToIface->setForwardDecl(D->isForwardDecl());
3029a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setLexicalDeclContext(LexicalDC);
3030a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      LexicalDC->addDecl(ToIface);
3031a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
3032a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Imported(D, ToIface);
3033a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3034a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (D->getSuperClass()) {
3035a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ObjCInterfaceDecl *Super
3036a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3037a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      if (!Super)
3038a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        return 0;
3039a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3040a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setSuperClass(Super);
3041a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3042a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
3043a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3044a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // Import protocols
3045a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3046a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
3047a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ObjCInterfaceDecl::protocol_loc_iterator
3048a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      FromProtoLoc = D->protocol_loc_begin();
304953b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek
305053b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // FIXME: Should we be usng all_referenced_protocol_begin() here?
3051a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3052a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                           FromProtoEnd = D->protocol_end();
3053a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       FromProto != FromProtoEnd;
3054a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++FromProto, ++FromProtoLoc) {
3055a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ObjCProtocolDecl *ToProto
3056a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3057a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      if (!ToProto)
3058a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        return 0;
3059a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      Protocols.push_back(ToProto);
3060a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3061a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
3062a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3063a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
3064a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3065a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                             ProtocolLocs.data(), Importer.getToContext());
3066a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3067a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // Import @end range
3068a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3069a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  } else {
3070a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Imported(D, ToIface);
30712e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
30722e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    // Check for consistency of superclasses.
30732e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    DeclarationName FromSuperName, ToSuperName;
30742e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (D->getSuperClass())
30752e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
30762e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (ToIface->getSuperClass())
30772e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      ToSuperName = ToIface->getSuperClass()->getDeclName();
30782e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (FromSuperName != ToSuperName) {
30792e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(ToIface->getLocation(),
30802e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                      diag::err_odr_objc_superclass_inconsistent)
30812e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << ToIface->getDeclName();
30822e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (ToIface->getSuperClass())
30832e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.ToDiag(ToIface->getSuperClassLoc(),
30842e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                        diag::note_odr_objc_superclass)
30852e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor          << ToIface->getSuperClass()->getDeclName();
30862e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      else
30872e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.ToDiag(ToIface->getLocation(),
30882e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                        diag::note_odr_objc_missing_superclass);
30892e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (D->getSuperClass())
30902e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.FromDiag(D->getSuperClassLoc(),
30912e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                          diag::note_odr_objc_superclass)
30922e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor          << D->getSuperClass()->getDeclName();
30932e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      else
30942e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.FromDiag(D->getLocation(),
30952e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                          diag::note_odr_objc_missing_superclass);
30962e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      return 0;
30972e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    }
3098a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
3099a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3100b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import categories. When the categories themselves are imported, they'll
3101b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // hook themselves into this interface.
3102b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3103b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor       FromCat = FromCat->getNextClassCategory())
3104b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Import(FromCat);
3105b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
3106a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // Import all of the members of this class.
3107083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
3108a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3109a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // If we have an @implementation, import it as well.
3110a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (D->getImplementation()) {
3111dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3112dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                       Importer.Import(D->getImplementation()));
3113a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!Impl)
3114a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      return 0;
3115a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3116a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setImplementation(Impl);
3117a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
3118a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
31192e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  return ToIface;
3120a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor}
3121a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
31223daef29bf390dbdb3603748280afd5827d1811daDouglas GregorDecl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
31233daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
31243daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor                                        Importer.Import(D->getCategoryDecl()));
31253daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  if (!Category)
31263daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    return 0;
31273daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
31283daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
31293daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  if (!ToImpl) {
31303daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    DeclContext *DC = Importer.ImportContext(D->getDeclContext());
31313daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    if (!DC)
31323daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor      return 0;
31333daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
31343daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
31353daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor                                          Importer.Import(D->getLocation()),
31363daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor                                          Importer.Import(D->getIdentifier()),
31373daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor                                          Category->getClassInterface());
31383daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
31393daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    DeclContext *LexicalDC = DC;
31403daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    if (D->getDeclContext() != D->getLexicalDeclContext()) {
31413daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor      LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
31423daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor      if (!LexicalDC)
31433daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor        return 0;
31443daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
31453daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor      ToImpl->setLexicalDeclContext(LexicalDC);
31463daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    }
31473daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
31483daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    LexicalDC->addDecl(ToImpl);
31493daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    Category->setImplementation(ToImpl);
31503daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  }
31513daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
31523daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  Importer.Imported(D, ToImpl);
3153cad2c59b0c087edea83d0fbf6eabde4a7960c778Douglas Gregor  ImportDeclContext(D);
31543daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  return ToImpl;
31553daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor}
31563daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
3157dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas GregorDecl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3158dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  // Find the corresponding interface.
3159dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3160dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                       Importer.Import(D->getClassInterface()));
3161dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  if (!Iface)
3162dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    return 0;
3163dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3164dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  // Import the superclass, if any.
3165dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ObjCInterfaceDecl *Super = 0;
3166dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  if (D->getSuperClass()) {
3167dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Super = cast_or_null<ObjCInterfaceDecl>(
3168dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                          Importer.Import(D->getSuperClass()));
3169dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    if (!Super)
3170dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      return 0;
3171dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  }
3172dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3173dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ObjCImplementationDecl *Impl = Iface->getImplementation();
3174dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  if (!Impl) {
3175dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // We haven't imported an implementation yet. Create a new @implementation
3176dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // now.
3177dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3178dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                  Importer.ImportContext(D->getDeclContext()),
3179dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                          Importer.Import(D->getLocation()),
3180dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                          Iface, Super);
3181dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3182dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    if (D->getDeclContext() != D->getLexicalDeclContext()) {
3183dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      DeclContext *LexicalDC
3184dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        = Importer.ImportContext(D->getLexicalDeclContext());
3185dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      if (!LexicalDC)
3186dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        return 0;
3187dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      Impl->setLexicalDeclContext(LexicalDC);
3188dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    }
3189dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3190dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // Associate the implementation with the class it implements.
3191dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Iface->setImplementation(Impl);
3192dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Importer.Imported(D, Iface->getImplementation());
3193dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  } else {
3194dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Importer.Imported(D, Iface->getImplementation());
3195dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3196dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // Verify that the existing @implementation has the same superclass.
3197dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    if ((Super && !Impl->getSuperClass()) ||
3198dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        (!Super && Impl->getSuperClass()) ||
3199dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        (Super && Impl->getSuperClass() &&
3200dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor         Super->getCanonicalDecl() != Impl->getSuperClass())) {
3201dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        Importer.ToDiag(Impl->getLocation(),
3202dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                        diag::err_odr_objc_superclass_inconsistent)
3203dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          << Iface->getDeclName();
3204dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        // FIXME: It would be nice to have the location of the superclass
3205dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        // below.
3206dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        if (Impl->getSuperClass())
3207dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.ToDiag(Impl->getLocation(),
3208dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                          diag::note_odr_objc_superclass)
3209dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          << Impl->getSuperClass()->getDeclName();
3210dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        else
3211dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.ToDiag(Impl->getLocation(),
3212dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                          diag::note_odr_objc_missing_superclass);
3213dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        if (D->getSuperClass())
3214dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.FromDiag(D->getLocation(),
3215dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                            diag::note_odr_objc_superclass)
3216dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          << D->getSuperClass()->getDeclName();
3217dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        else
3218dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.FromDiag(D->getLocation(),
3219dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                            diag::note_odr_objc_missing_superclass);
3220dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      return 0;
3221dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    }
3222dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  }
3223dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3224dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  // Import all of the members of this @implementation.
3225dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ImportDeclContext(D);
3226dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3227dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  return Impl;
3228dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor}
3229dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3230e3261624c1870e52d7efc2ac83e647713361ac6cDouglas GregorDecl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3231e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Import the major distinguishing characteristics of an @property.
3232e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  DeclContext *DC, *LexicalDC;
3233e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  DeclarationName Name;
3234e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  SourceLocation Loc;
3235e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3236e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    return 0;
3237e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3238e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Check whether we have already imported this property.
3239e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3240e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       Lookup.first != Lookup.second;
3241e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       ++Lookup.first) {
3242e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    if (ObjCPropertyDecl *FoundProp
3243e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                                = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3244e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // Check property types.
3245e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      if (!Importer.IsStructurallyEquivalent(D->getType(),
3246e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                                             FoundProp->getType())) {
3247e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3248e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor          << Name << D->getType() << FoundProp->getType();
3249e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3250e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor          << FoundProp->getType();
3251e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        return 0;
3252e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      }
3253e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3254e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // FIXME: Check property attributes, getters, setters, etc.?
3255e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3256e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // Consider these properties to be equivalent.
3257e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      Importer.Imported(D, FoundProp);
3258e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      return FoundProp;
3259e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    }
3260e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  }
3261e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3262e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Import the type.
326383a230c83a54190366138c1a4f4310ef838b88fcJohn McCall  TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
326483a230c83a54190366138c1a4f4310ef838b88fcJohn McCall  if (!T)
3265e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    return 0;
3266e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3267e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Create the new property.
3268e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ObjCPropertyDecl *ToProperty
3269e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3270e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               Name.getAsIdentifierInfo(),
3271e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               Importer.Import(D->getAtLoc()),
3272e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               T,
3273e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               D->getPropertyImplementation());
3274e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  Importer.Imported(D, ToProperty);
3275e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setLexicalDeclContext(LexicalDC);
3276e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  LexicalDC->addDecl(ToProperty);
3277e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3278e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setPropertyAttributes(D->getPropertyAttributes());
327980aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian  ToProperty->setPropertyAttributesAsWritten(
328080aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian                                      D->getPropertyAttributesAsWritten());
3281e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3282e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3283e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setGetterMethodDecl(
3284e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3285e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setSetterMethodDecl(
3286e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3287e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setPropertyIvarDecl(
3288e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3289e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  return ToProperty;
3290e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor}
3291e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3292954e0c75c42f321945aff8b9ee96da43cd90c752Douglas GregorDecl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3293954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3294954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                        Importer.Import(D->getPropertyDecl()));
3295954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (!Property)
3296954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    return 0;
3297954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3298954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3299954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (!DC)
3300954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    return 0;
3301954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3302954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  // Import the lexical declaration context.
3303954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  DeclContext *LexicalDC = DC;
3304954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
3305954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3306954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    if (!LexicalDC)
3307954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      return 0;
3308954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  }
3309954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3310954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3311954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (!InImpl)
3312954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    return 0;
3313954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3314954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  // Import the ivar (for an @synthesize).
3315954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  ObjCIvarDecl *Ivar = 0;
3316954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (D->getPropertyIvarDecl()) {
3317954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    Ivar = cast_or_null<ObjCIvarDecl>(
3318954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                    Importer.Import(D->getPropertyIvarDecl()));
3319954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    if (!Ivar)
3320954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      return 0;
3321954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  }
3322954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3323954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  ObjCPropertyImplDecl *ToImpl
3324954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3325954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (!ToImpl) {
3326954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3327954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          Importer.Import(D->getLocStart()),
3328954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          Importer.Import(D->getLocation()),
3329954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          Property,
3330954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          D->getPropertyImplementation(),
3331954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          Ivar,
3332954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                  Importer.Import(D->getPropertyIvarDeclLoc()));
3333954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    ToImpl->setLexicalDeclContext(LexicalDC);
3334954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    Importer.Imported(D, ToImpl);
3335954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    LexicalDC->addDecl(ToImpl);
3336954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  } else {
3337954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    // Check that we have the same kind of property implementation (@synthesize
3338954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    // vs. @dynamic).
3339954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3340954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      Importer.ToDiag(ToImpl->getLocation(),
3341954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                      diag::err_odr_objc_property_impl_kind_inconsistent)
3342954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << Property->getDeclName()
3343954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << (ToImpl->getPropertyImplementation()
3344954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                              == ObjCPropertyImplDecl::Dynamic);
3345954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      Importer.FromDiag(D->getLocation(),
3346954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                        diag::note_odr_objc_property_impl_kind)
3347954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << D->getPropertyDecl()->getDeclName()
3348954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3349954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      return 0;
3350954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    }
3351954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3352954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    // For @synthesize, check that we have the same
3353954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3354954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        Ivar != ToImpl->getPropertyIvarDecl()) {
3355954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3356954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                      diag::err_odr_objc_synthesize_ivar_inconsistent)
3357954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << Property->getDeclName()
3358954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << ToImpl->getPropertyIvarDecl()->getDeclName()
3359954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << Ivar->getDeclName();
3360954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3361954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                        diag::note_odr_objc_synthesize_ivar_here)
3362954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << D->getPropertyIvarDecl()->getDeclName();
3363954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      return 0;
3364954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    }
3365954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3366954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    // Merge the existing implementation with the new implementation.
3367954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    Importer.Imported(D, ToImpl);
3368954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  }
3369954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3370954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  return ToImpl;
3371954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor}
3372954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
33732b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas GregorDecl *
33742b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas GregorASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
33752b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  // Import the context of this declaration.
33762b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
33772b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  if (!DC)
33782b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    return 0;
33792b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
33802b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  DeclContext *LexicalDC = DC;
33812b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
33822b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
33832b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    if (!LexicalDC)
33842b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      return 0;
33852b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  }
33862b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
33872b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  // Import the location of this declaration.
33882b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
33892b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
33902b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
33912b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  llvm::SmallVector<SourceLocation, 4> Locations;
33922b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
33932b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    = D->protocol_loc_begin();
33942b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  for (ObjCForwardProtocolDecl::protocol_iterator FromProto
33952b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor         = D->protocol_begin(), FromProtoEnd = D->protocol_end();
33962b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor       FromProto != FromProtoEnd;
33972b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor       ++FromProto, ++FromProtoLoc) {
33982b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    ObjCProtocolDecl *ToProto
33992b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
34002b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    if (!ToProto)
34012b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      continue;
34022b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
34032b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Protocols.push_back(ToProto);
34042b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Locations.push_back(Importer.Import(*FromProtoLoc));
34052b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  }
34062b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
34072b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ObjCForwardProtocolDecl *ToForward
34082b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
34092b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor                                      Protocols.data(), Protocols.size(),
34102b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor                                      Locations.data());
34112b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ToForward->setLexicalDeclContext(LexicalDC);
34122b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  LexicalDC->addDecl(ToForward);
34132b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  Importer.Imported(D, ToForward);
34142b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  return ToForward;
34152b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor}
34162b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
3417a2bc15b7463a9f85a5bff1531d833c278426a733Douglas GregorDecl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3418a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  // Import the context of this declaration.
3419a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3420a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  if (!DC)
3421a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    return 0;
3422a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3423a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  DeclContext *LexicalDC = DC;
3424a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
3425a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3426a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    if (!LexicalDC)
3427a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      return 0;
3428a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  }
3429a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3430a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  // Import the location of this declaration.
3431a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
3432a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3433a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3434a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  llvm::SmallVector<SourceLocation, 4> Locations;
3435a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3436a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor       From != FromEnd; ++From) {
3437a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    ObjCInterfaceDecl *ToIface
3438a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3439a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    if (!ToIface)
3440a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      continue;
3441a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3442a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Interfaces.push_back(ToIface);
3443a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Locations.push_back(Importer.Import(From->getLocation()));
3444a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  }
3445a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3446a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3447a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Loc,
3448a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Interfaces.data(),
3449a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Locations.data(),
3450a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor                                                 Interfaces.size());
3451a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  ToClass->setLexicalDeclContext(LexicalDC);
3452a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  LexicalDC->addDecl(ToClass);
3453a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  Importer.Imported(D, ToClass);
3454a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  return ToClass;
3455a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor}
3456a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3457040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3458040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // For template arguments, we adopt the translation unit as our declaration
3459040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // context. This context will be fixed when the actual template declaration
3460040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // is created.
3461040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3462040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Import default argument.
3463040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return TemplateTypeParmDecl::Create(Importer.getToContext(),
3464040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                              Importer.getToContext().getTranslationUnitDecl(),
3465344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                      Importer.Import(D->getLocStart()),
3466040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      Importer.Import(D->getLocation()),
3467040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->getDepth(),
3468040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->getIndex(),
3469040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      Importer.Import(D->getIdentifier()),
3470040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->wasDeclaredWithTypename(),
3471040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->isParameterPack());
3472040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3473040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3474040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *
3475040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3476040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the name of this declaration.
3477040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
3478040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D->getDeclName() && !Name)
3479040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3480040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3481040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the location of this declaration.
3482040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
3483040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3484040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the type of this declaration.
3485040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  QualType T = Importer.Import(D->getType());
3486040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (T.isNull())
3487040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3488040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3489040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import type-source information.
3490040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3491040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D->getTypeSourceInfo() && !TInfo)
3492040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3493040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3494040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Import default argument.
3495040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3496040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3497040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                               Importer.getToContext().getTranslationUnitDecl(),
3498ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                         Importer.Import(D->getInnerLocStart()),
3499040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         Loc, D->getDepth(), D->getPosition(),
3500040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         Name.getAsIdentifierInfo(),
350110738d36b150aa65206890c1c845cdba076e4200Douglas Gregor                                         T, D->isParameterPack(), TInfo);
3502040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3503040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3504040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *
3505040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3506040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the name of this declaration.
3507040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
3508040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D->getDeclName() && !Name)
3509040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3510040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3511040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the location of this declaration.
3512040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
3513040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3514040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import template parameters.
3515040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  TemplateParameterList *TemplateParams
3516040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    = ImportTemplateParameterList(D->getTemplateParameters());
3517040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!TemplateParams)
3518040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3519040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3520040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Import default argument.
3521040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3522040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3523040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                              Importer.getToContext().getTranslationUnitDecl(),
3524040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          Loc, D->getDepth(), D->getPosition(),
352561c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                          D->isParameterPack(),
3526040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          Name.getAsIdentifierInfo(),
3527040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          TemplateParams);
3528040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3529040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3530040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3531040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // If this record has a definition in the translation unit we're coming from,
3532040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // but this particular declaration is not that definition, import the
3533040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // definition and map to that.
3534040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  CXXRecordDecl *Definition
3535040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3536040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (Definition && Definition != D->getTemplatedDecl()) {
3537040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *ImportedDef
3538040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      = Importer.Import(Definition->getDescribedClassTemplate());
3539040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!ImportedDef)
3540040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return 0;
3541040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3542040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return Importer.Imported(D, ImportedDef);
3543040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
3544040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3545040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the major distinguishing characteristics of this class template.
3546040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclContext *DC, *LexicalDC;
3547040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclarationName Name;
3548040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  SourceLocation Loc;
3549040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3550040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3551040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3552040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // We may already have a template of the same name; try to find and match it.
3553040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!DC->isFunctionOrMethod()) {
3554040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3555040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3556040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor         Lookup.first != Lookup.second;
3557040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor         ++Lookup.first) {
3558040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3559040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        continue;
3560040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3561040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Decl *Found = *Lookup.first;
3562040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (ClassTemplateDecl *FoundTemplate
3563040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                        = dyn_cast<ClassTemplateDecl>(Found)) {
3564040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (IsStructuralMatch(D, FoundTemplate)) {
3565040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          // The class templates structurally match; call it the same template.
3566040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          // FIXME: We may be filling in a forward declaration here. Handle
3567040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          // this case!
3568040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Importer.Imported(D->getTemplatedDecl(),
3569040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                            FoundTemplate->getTemplatedDecl());
3570040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          return Importer.Imported(D, FoundTemplate);
3571040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        }
3572040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
3573040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3574040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
3575040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
3576040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3577040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!ConflictingDecls.empty()) {
3578040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3579040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         ConflictingDecls.data(),
3580040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         ConflictingDecls.size());
3581040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
3582040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3583040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!Name)
3584040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return 0;
3585040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
3586040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3587040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3588040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3589040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Create the declaration that is being templated.
3590ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3591ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
3592040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3593040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                     DTemplated->getTagKind(),
3594ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     DC, StartLoc, IdLoc,
3595ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                   Name.getAsIdentifierInfo());
3596040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2Templated->setAccess(DTemplated->getAccess());
3597c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
3598040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2Templated->setLexicalDeclContext(LexicalDC);
3599040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3600040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Create the class template declaration itself.
3601040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  TemplateParameterList *TemplateParams
3602040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    = ImportTemplateParameterList(D->getTemplateParameters());
3603040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!TemplateParams)
3604040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3605040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3606040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3607040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                    Loc, Name, TemplateParams,
3608040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                    D2Templated,
3609040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  /*PrevDecl=*/0);
3610040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2Templated->setDescribedClassTemplate(D2);
3611040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3612040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2->setAccess(D->getAccess());
3613040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2->setLexicalDeclContext(LexicalDC);
3614040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  LexicalDC->addDecl(D2);
3615040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3616040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Note the relationship between the class templates.
3617040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  Importer.Imported(D, D2);
3618040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  Importer.Imported(DTemplated, D2Templated);
3619040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3620040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3621040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    // FIXME: Import definition!
3622040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
3623040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3624040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return D2;
3625040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3626040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3627d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorDecl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3628d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                          ClassTemplateSpecializationDecl *D) {
3629d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // If this record has a definition in the translation unit we're coming from,
3630d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // but this particular declaration is not that definition, import the
3631d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // definition and map to that.
3632d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  TagDecl *Definition = D->getDefinition();
3633d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (Definition && Definition != D) {
3634d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    Decl *ImportedDef = Importer.Import(Definition);
3635d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!ImportedDef)
3636d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return 0;
3637d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3638d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return Importer.Imported(D, ImportedDef);
3639d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3640d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3641d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateDecl *ClassTemplate
3642d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = cast_or_null<ClassTemplateDecl>(Importer.Import(
3643d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 D->getSpecializedTemplate()));
3644d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (!ClassTemplate)
3645d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3646d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3647d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Import the context of this declaration.
3648d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  DeclContext *DC = ClassTemplate->getDeclContext();
3649d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (!DC)
3650d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3651d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3652d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  DeclContext *LexicalDC = DC;
3653d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
3654d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3655d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!LexicalDC)
3656d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return 0;
3657d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3658d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3659d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Import the location of this declaration.
3660ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation StartLoc = Importer.Import(D->getLocStart());
3661ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation IdLoc = Importer.Import(D->getLocation());
3662d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3663d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Import template arguments.
3664d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3665d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (ImportTemplateArguments(D->getTemplateArgs().data(),
3666d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                              D->getTemplateArgs().size(),
3667d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                              TemplateArgs))
3668d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3669d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3670d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Try to find an existing specialization with these template arguments.
3671d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  void *InsertPos = 0;
3672d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateSpecializationDecl *D2
3673d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = ClassTemplate->findSpecialization(TemplateArgs.data(),
3674d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                        TemplateArgs.size(), InsertPos);
3675d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D2) {
3676d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // We already have a class template specialization with these template
3677d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // arguments.
3678d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3679d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // FIXME: Check for specialization vs. instantiation errors.
3680d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3681d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (RecordDecl *FoundDef = D2->getDefinition()) {
3682d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3683d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        // The record types structurally match, or the "from" translation
3684d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        // unit only had a forward declaration anyway; call it the same
3685d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        // function.
3686d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return Importer.Imported(D, FoundDef);
3687d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      }
3688d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
3689d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  } else {
3690d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Create a new specialization.
3691d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3692d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 D->getTagKind(), DC,
3693ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                 StartLoc, IdLoc,
3694ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                 ClassTemplate,
3695d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 TemplateArgs.data(),
3696d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 TemplateArgs.size(),
3697d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 /*PrevDecl=*/0);
3698d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    D2->setSpecializationKind(D->getSpecializationKind());
3699d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3700d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Add this specialization to the class template.
3701d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ClassTemplate->AddSpecialization(D2, InsertPos);
3702d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3703d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Import the qualifier, if any.
3704c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
3705d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3706d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Add the specialization to this context.
3707d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    D2->setLexicalDeclContext(LexicalDC);
3708d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    LexicalDC->addDecl(D2);
3709d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3710d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  Importer.Imported(D, D2);
3711d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3712d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D->isDefinition() && ImportDefinition(D, D2))
3713d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3714d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3715d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return D2;
3716d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
3717d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
37184800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
37194800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor// Import Statements
37204800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
37214800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
37224800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorStmt *ASTNodeImporter::VisitStmt(Stmt *S) {
37234800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
37244800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    << S->getStmtClassName();
37254800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return 0;
37264800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
37274800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
37284800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
37294800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor// Import Expressions
37304800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
37314800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorExpr *ASTNodeImporter::VisitExpr(Expr *E) {
37324800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
37334800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    << E->getStmtClassName();
37344800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return 0;
37354800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
37364800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
3737440806306674e23ad74726208cbdc6f37849dd9dDouglas GregorExpr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3738440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  NestedNameSpecifier *Qualifier = 0;
3739440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (E->getQualifier()) {
3740440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    Qualifier = Importer.Import(E->getQualifier());
3741440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    if (!E->getQualifier())
3742440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor      return 0;
3743440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  }
3744440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
3745440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3746440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (!ToD)
3747440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    return 0;
3748440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
3749440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  QualType T = Importer.Import(E->getType());
3750440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (T.isNull())
3751440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    return 0;
3752440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
375340d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor  return DeclRefExpr::Create(Importer.getToContext(),
375440d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor                             Importer.Import(E->getQualifierLoc()),
3755440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             ToD,
3756440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             Importer.Import(E->getLocation()),
3757f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                             T, E->getValueKind(),
3758440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             /*FIXME:TemplateArgs=*/0);
3759440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor}
3760440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
37614800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorExpr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
37624800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  QualType T = Importer.Import(E->getType());
37634800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (T.isNull())
37644800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return 0;
37654800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
37669996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return IntegerLiteral::Create(Importer.getToContext(),
37679996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                                E->getValue(), T,
37689996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                                Importer.Import(E->getLocation()));
37694800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
37704800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
3771b2e400aae8c62c4e1616016f40618baace0da065Douglas GregorExpr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3772b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  QualType T = Importer.Import(E->getType());
3773b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  if (T.isNull())
3774b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor    return 0;
3775b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor
3776b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3777b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor                                                        E->isWide(), T,
3778b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor                                          Importer.Import(E->getLocation()));
3779b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor}
3780b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor
3781f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3782f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
3783f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!SubExpr)
3784f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3785f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3786f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext())
3787f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                  ParenExpr(Importer.Import(E->getLParen()),
3788f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                            Importer.Import(E->getRParen()),
3789f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                            SubExpr);
3790f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3791f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3792f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3793f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
3794f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
3795f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3796f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3797f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
3798f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!SubExpr)
3799f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3800f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3801f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
3802f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                     T, E->getValueKind(),
3803f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                     E->getObjectKind(),
3804f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                         Importer.Import(E->getOperatorLoc()));
3805f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3806f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3807f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter CollingbourneExpr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3808f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne                                            UnaryExprOrTypeTraitExpr *E) {
3809bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  QualType ResultType = Importer.Import(E->getType());
3810bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3811bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  if (E->isArgumentType()) {
3812bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3813bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    if (!TInfo)
3814bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor      return 0;
3815bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3816f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3817f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne                                           TInfo, ResultType,
3818bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                           Importer.Import(E->getOperatorLoc()),
3819bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                           Importer.Import(E->getRParenLoc()));
3820bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  }
3821bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3822bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3823bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  if (!SubExpr)
3824bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    return 0;
3825bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3826f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3827f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne                                          SubExpr, ResultType,
3828bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                          Importer.Import(E->getOperatorLoc()),
3829bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                          Importer.Import(E->getRParenLoc()));
3830bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor}
3831bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3832f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3833f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
3834f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
3835f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3836f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3837f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *LHS = Importer.Import(E->getLHS());
3838f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!LHS)
3839f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3840f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3841f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *RHS = Importer.Import(E->getRHS());
3842f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!RHS)
3843f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3844f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3845f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
3846f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                      T, E->getValueKind(),
3847f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                      E->getObjectKind(),
3848f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                          Importer.Import(E->getOperatorLoc()));
3849f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3850f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3851f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3852f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
3853f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
3854f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3855f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3856f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3857f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (CompLHSType.isNull())
3858f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3859f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3860f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType CompResultType = Importer.Import(E->getComputationResultType());
3861f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (CompResultType.isNull())
3862f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3863f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3864f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *LHS = Importer.Import(E->getLHS());
3865f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!LHS)
3866f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3867f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3868f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *RHS = Importer.Import(E->getRHS());
3869f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!RHS)
3870f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3871f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3872f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext())
3873f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                        CompoundAssignOperator(LHS, RHS, E->getOpcode(),
3874f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                               T, E->getValueKind(),
3875f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                               E->getObjectKind(),
3876f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                               CompLHSType, CompResultType,
3877f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                          Importer.Import(E->getOperatorLoc()));
3878f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3879f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3880da57f3eeab7b7f7f6e6788956f0a0d9adf196a7dBenjamin Kramerstatic bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3881f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (E->path_empty()) return false;
3882f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
3883f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  // TODO: import cast paths
3884f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return true;
3885f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
3886f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
388736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorExpr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
388836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  QualType T = Importer.Import(E->getType());
388936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (T.isNull())
389036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
389136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
389236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
389336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!SubExpr)
389436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
3895f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
3896f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  CXXCastPath BasePath;
3897f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (ImportCastPath(E, BasePath))
3898f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return 0;
3899f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
3900f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
39015baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                                  SubExpr, &BasePath, E->getValueKind());
390236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
390336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
3904008847a70ab122a99911149199855060fb3753b4Douglas GregorExpr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3905008847a70ab122a99911149199855060fb3753b4Douglas Gregor  QualType T = Importer.Import(E->getType());
3906008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (T.isNull())
3907008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
3908008847a70ab122a99911149199855060fb3753b4Douglas Gregor
3909008847a70ab122a99911149199855060fb3753b4Douglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
3910008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (!SubExpr)
3911008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
3912008847a70ab122a99911149199855060fb3753b4Douglas Gregor
3913008847a70ab122a99911149199855060fb3753b4Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3914008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (!TInfo && E->getTypeInfoAsWritten())
3915008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
3916008847a70ab122a99911149199855060fb3753b4Douglas Gregor
3917f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  CXXCastPath BasePath;
3918f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (ImportCastPath(E, BasePath))
3919f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return 0;
3920f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
3921f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  return CStyleCastExpr::Create(Importer.getToContext(), T,
3922f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                E->getValueKind(), E->getCastKind(),
3923f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                SubExpr, &BasePath, TInfo,
3924f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                Importer.Import(E->getLParenLoc()),
3925f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                Importer.Import(E->getRParenLoc()));
3926008847a70ab122a99911149199855060fb3753b4Douglas Gregor}
3927008847a70ab122a99911149199855060fb3753b4Douglas Gregor
392833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios KyrtzidisASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
3929d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor                         ASTContext &FromContext, FileManager &FromFileManager,
3930d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor                         bool MinimalImport)
39311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  : ToContext(ToContext), FromContext(FromContext),
3932d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    ToFileManager(ToFileManager), FromFileManager(FromFileManager),
3933d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    Minimal(MinimalImport)
3934d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor{
39359bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ImportedDecls[FromContext.getTranslationUnitDecl()]
39369bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    = ToContext.getTranslationUnitDecl();
39379bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
39389bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
39399bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorASTImporter::~ASTImporter() { }
39401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
39411b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTImporter::Import(QualType FromT) {
39421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (FromT.isNull())
39431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
3944f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall
3945f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *fromTy = FromT.getTypePtr();
39461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
3947169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Check whether we've already imported this type.
3948f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  llvm::DenseMap<const Type *, const Type *>::iterator Pos
3949f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    = ImportedTypes.find(fromTy);
3950169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  if (Pos != ImportedTypes.end())
3951f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
39521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
3953169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Import the type
39541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  ASTNodeImporter Importer(*this);
3955f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  QualType ToT = Importer.Visit(fromTy);
39561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToT.isNull())
39571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToT;
39581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
3959169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Record the imported type.
3960f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  ImportedTypes[fromTy] = ToT.getTypePtr();
3961169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor
3962f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
39631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
39641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
39659bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorTypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
396682fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (!FromTSI)
396782fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return FromTSI;
396882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
396982fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // FIXME: For now we just create a "trivial" type source info based
39705606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky  // on the type and a single location. Implement a real version of this.
397182fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  QualType T = Import(FromTSI->getType());
397282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (T.isNull())
397382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
397482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
397582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  return ToContext.getTrivialTypeSourceInfo(T,
3976bd054dba8a3023821f2a0951b0fae05e3522a7c9Abramo Bagnara                        FromTSI->getTypeLoc().getSourceRange().getBegin());
39779bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
39789bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
39799bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorDecl *ASTImporter::Import(Decl *FromD) {
39809bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromD)
39819bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
39829bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
39839bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Check whether we've already imported this declaration.
39849bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
39859bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (Pos != ImportedDecls.end())
39869bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return Pos->second;
39879bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
39889bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Import the type
39899bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ASTNodeImporter Importer(*this);
39909bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  Decl *ToD = Importer.Visit(FromD);
39919bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!ToD)
39929bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
39939bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
39949bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Record the imported declaration.
39959bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ImportedDecls[FromD] = ToD;
3996ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
3997ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3998ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // Keep track of anonymous tags that have an associated typedef.
3999ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (FromTag->getTypedefForAnonDecl())
4000ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      AnonTagsWithPendingTypedefs.push_back(FromTag);
4001ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
4002ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // When we've finished transforming a typedef, see whether it was the
4003ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // typedef for an anonymous tag.
4004ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    for (llvm::SmallVector<TagDecl *, 4>::iterator
4005ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor               FromTag = AnonTagsWithPendingTypedefs.begin(),
4006ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            FromTagEnd = AnonTagsWithPendingTypedefs.end();
4007ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor         FromTag != FromTagEnd; ++FromTag) {
4008ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
4009ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4010ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          // We found the typedef for an anonymous tag; link them.
4011ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
4012ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          AnonTagsWithPendingTypedefs.erase(FromTag);
4013ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          break;
4014ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        }
4015ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      }
4016ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    }
4017ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  }
4018ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
40199bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return ToD;
40209bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
40219bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40229bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorDeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
40239bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromDC)
40249bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return FromDC;
40259bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40269bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
40279bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
40289bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40299bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorExpr *ASTImporter::Import(Expr *FromE) {
40309bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromE)
40319bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
40329bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40339bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
40349bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
40359bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40369bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorStmt *ASTImporter::Import(Stmt *FromS) {
40379bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromS)
40389bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
40399bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40404800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Check whether we've already imported this declaration.
40414800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
40424800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (Pos != ImportedStmts.end())
40434800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return Pos->second;
40444800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
40454800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Import the type
40464800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  ASTNodeImporter Importer(*this);
40474800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Stmt *ToS = Importer.Visit(FromS);
40484800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (!ToS)
40494800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return 0;
40504800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
40514800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Record the imported declaration.
40524800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  ImportedStmts[FromS] = ToS;
40534800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return ToS;
40549bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
40559bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40569bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorNestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
40579bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromNNS)
40589bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
40599bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40609bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // FIXME: Implement!
40619bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return 0;
40629bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
40639bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
4064c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas GregorNestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4065c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  // FIXME: Implement!
4066c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  return NestedNameSpecifierLoc();
4067c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor}
4068c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor
4069d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorTemplateName ASTImporter::Import(TemplateName From) {
4070d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  switch (From.getKind()) {
4071d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::Template:
4072d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (TemplateDecl *ToTemplate
4073d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4074d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateName(ToTemplate);
4075d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4076d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateName();
4077d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4078d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::OverloadedTemplate: {
4079d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4080d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    UnresolvedSet<2> ToTemplates;
4081d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4082d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                             E = FromStorage->end();
4083d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor         I != E; ++I) {
4084d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4085d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        ToTemplates.addDecl(To);
4086d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      else
4087d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return TemplateName();
4088d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
4089d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4090d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                               ToTemplates.end());
4091d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
4092d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4093d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::QualifiedTemplate: {
4094d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4095d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4096d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Qualifier)
4097d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateName();
4098d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4099d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (TemplateDecl *ToTemplate
4100d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4101d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return ToContext.getQualifiedTemplateName(Qualifier,
4102d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                QTN->hasTemplateKeyword(),
4103d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                ToTemplate);
4104d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4105d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateName();
4106d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
4107d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4108d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::DependentTemplate: {
4109d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    DependentTemplateName *DTN = From.getAsDependentTemplateName();
4110d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4111d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Qualifier)
4112d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateName();
4113d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4114d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (DTN->isIdentifier()) {
4115d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return ToContext.getDependentTemplateName(Qualifier,
4116d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                Import(DTN->getIdentifier()));
4117d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
4118d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4119d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4120d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
41211aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
41221aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  case TemplateName::SubstTemplateTemplateParmPack: {
41231aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    SubstTemplateTemplateParmPackStorage *SubstPack
41241aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor      = From.getAsSubstTemplateTemplateParmPack();
41251aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    TemplateTemplateParmDecl *Param
41261aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor      = cast_or_null<TemplateTemplateParmDecl>(
41271aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                        Import(SubstPack->getParameterPack()));
41281aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    if (!Param)
41291aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor      return TemplateName();
41301aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
41311aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    ASTNodeImporter Importer(*this);
41321aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    TemplateArgument ArgPack
41331aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor      = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
41341aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    if (ArgPack.isNull())
41351aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor      return TemplateName();
41361aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
41371aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
41381aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
4139d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
4140d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4141d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm_unreachable("Invalid template name kind");
4142d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return TemplateName();
4143d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
4144d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
41459bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorSourceLocation ASTImporter::Import(SourceLocation FromLoc) {
41469bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (FromLoc.isInvalid())
41479bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return SourceLocation();
41489bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
4149885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &FromSM = FromContext.getSourceManager();
4150885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4151885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // For now, map everything down to its spelling location, so that we
4152885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // don't have to import macro instantiations.
4153885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // FIXME: Import macro instantiations!
4154885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  FromLoc = FromSM.getSpellingLoc(FromLoc);
4155885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4156885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &ToSM = ToContext.getSourceManager();
4157885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4158885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor             .getFileLocWithOffset(Decomposed.second);
41599bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
41609bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
41619bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorSourceRange ASTImporter::Import(SourceRange FromRange) {
41629bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
41639bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
41649bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
4165885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas GregorFileID ASTImporter::Import(FileID FromID) {
4166535a3e20104461c136654d59fb833ae80644ae79Sebastian Redl  llvm::DenseMap<FileID, FileID>::iterator Pos
4167535a3e20104461c136654d59fb833ae80644ae79Sebastian Redl    = ImportedFileIDs.find(FromID);
4168885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  if (Pos != ImportedFileIDs.end())
4169885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    return Pos->second;
4170885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4171885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &FromSM = FromContext.getSourceManager();
4172885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &ToSM = ToContext.getSourceManager();
4173885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4174885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
4175885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4176885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // Include location of this file.
4177885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4178885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4179885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // Map the FileID for to the "to" source manager.
4180885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  FileID ToID;
4181885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
4182b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  if (Cache->OrigEntry) {
4183885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4184885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // disk again
4185885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4186885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // than mmap the files several times.
4187b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
4188885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4189885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                             FromSLoc.getFile().getFileCharacteristic());
4190885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  } else {
4191885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We want to re-use the existing MemoryBuffer!
419233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    const llvm::MemoryBuffer *
419333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis        FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
4194885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    llvm::MemoryBuffer *ToBuf
4195a0a270c0f1c0a4e3482438bdc5f4a7bd3d25f0a6Chris Lattner      = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
4196885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                                             FromBuf->getBufferIdentifier());
4197885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4198885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  }
4199885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4200885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4201535a3e20104461c136654d59fb833ae80644ae79Sebastian Redl  ImportedFileIDs[FromID] = ToID;
4202885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  return ToID;
4203885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor}
4204885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4205d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregorvoid ASTImporter::ImportDefinition(Decl *From) {
4206d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor  Decl *To = Import(From);
4207d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor  if (!To)
4208d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    return;
4209d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor
4210d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor  if (DeclContext *FromDC = cast<DeclContext>(From)) {
4211d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    ASTNodeImporter Importer(*this);
4212d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    Importer.ImportDeclContext(FromDC, true);
4213d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor  }
4214d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor}
4215d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor
42161b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorDeclarationName ASTImporter::Import(DeclarationName FromName) {
42171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!FromName)
42181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return DeclarationName();
42191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
42201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  switch (FromName.getNameKind()) {
42211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::Identifier:
42221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Import(FromName.getAsIdentifierInfo());
42231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
42241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCZeroArgSelector:
42251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCOneArgSelector:
42261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCMultiArgSelector:
42271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Import(FromName.getObjCSelector());
42281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
42291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXConstructorName: {
42301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
42311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
42321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
42331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
42341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXConstructorName(
42351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
42361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
42371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
42381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXDestructorName: {
42391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
42401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
42411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
42421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
42431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXDestructorName(
42441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
42451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
42461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
42471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXConversionFunctionName: {
42481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
42491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
42501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
42511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
42521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXConversionFunctionName(
42531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
42541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
42551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
42561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXOperatorName:
42571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXOperatorName(
42581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                          FromName.getCXXOverloadedOperator());
42591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
42601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXLiteralOperatorName:
42611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXLiteralOperatorName(
42621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                   Import(FromName.getCXXLiteralIdentifier()));
42631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
42641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXUsingDirective:
42651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: STATICS!
42661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return DeclarationName::getUsingDirectiveName();
42671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
42681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
42691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Silence bogus GCC warning
42701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return DeclarationName();
42711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
42721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
4273d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorIdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
42741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!FromId)
42751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return 0;
42761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
42771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return &ToContext.Idents.get(FromId->getName());
42781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
4279089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
4280c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas GregorSelector ASTImporter::Import(Selector FromSel) {
4281c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (FromSel.isNull())
4282c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return Selector();
4283c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
4284c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  llvm::SmallVector<IdentifierInfo *, 4> Idents;
4285c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4286c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4287c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4288c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4289c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor}
4290c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
4291089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4292089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                DeclContext *DC,
4293089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                unsigned IDNS,
4294089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                NamedDecl **Decls,
4295089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                unsigned NumDecls) {
4296089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  return Name;
4297089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
4298089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
4299089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
430033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  return ToContext.getDiagnostics().Report(Loc, DiagID);
4301089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
4302089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
4303089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
430433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  return FromContext.getDiagnostics().Report(Loc, DiagID);
4305089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
43065ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
43075ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas GregorDecl *ASTImporter::Imported(Decl *From, Decl *To) {
43085ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  ImportedDecls[From] = To;
43095ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  return To;
4310af667588d53de22795c5304f1496ccaac2a71402Daniel Dunbar}
4311ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
4312ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregorbool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
4313f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  llvm::DenseMap<const Type *, const Type *>::iterator Pos
4314ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor   = ImportedTypes.find(From.getTypePtr());
4315ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4316ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return true;
4317ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
431833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
4319bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(From, To);
4320ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor}
4321