ASTImporter.cpp revision 3340e27fe15c52fe28fc722a98fd55d796c90442
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
620aeb2890389ec1872e49a18fb2022bfb9f96578dSean Callanan    QualType VisitParenType(const ParenType *T);
63f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitTypedefType(const TypedefType *T);
64f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitTypeOfExprType(const TypeOfExprType *T);
651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentTypeOfExprType
66f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitTypeOfType(const TypeOfType *T);
67f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitDecltypeType(const DecltypeType *T);
68ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    QualType VisitUnaryTransformType(const UnaryTransformType *T);
6934b41d939a1328f484511c6002ba2456db879a29Richard Smith    QualType VisitAutoType(const AutoType *T);
701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: DependentDecltypeType
71f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitRecordType(const RecordType *T);
72f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitEnumType(const EnumType *T);
731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: TemplateTypeParmType
741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: SubstTemplateTypeParmType
75f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
76f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitElaboratedType(const ElaboratedType *T);
774714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    // FIXME: DependentNameType
7833500955d731c73717af52088b7fc0e7a85681e7John McCall    // FIXME: DependentTemplateSpecializationType
79f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
80f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitObjCObjectType(const ObjCObjectType *T);
81f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
82089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
83089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    // Importing declarations
84a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
85a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                         DeclContext *&LexicalDC, DeclarationName &Name,
86788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                         SourceLocation &Loc);
871cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = 0);
882577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
892577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                  DeclarationNameInfo& To);
90d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
911cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    bool ImportDefinition(RecordDecl *From, RecordDecl *To,
921cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor                          bool ForceImport = false);
931cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    bool ImportDefinition(EnumDecl *From, EnumDecl *To,
941cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor                          bool ForceImport = false);
95040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    TemplateParameterList *ImportTemplateParameterList(
96040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                 TemplateParameterList *Params);
97d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
98d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    bool ImportTemplateArguments(const TemplateArgument *FromArgs,
99d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                 unsigned NumFromArgs,
1005f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                               SmallVectorImpl<TemplateArgument> &ToArgs);
10196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
10273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
103040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
10489cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    Decl *VisitDecl(Decl *D);
105788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    Decl *VisitNamespaceDecl(NamespaceDecl *D);
106162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
1079e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
108162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
10936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
11096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *VisitRecordDecl(RecordDecl *D);
11136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
112a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D);
113c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
114c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
115c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
116c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
11796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
11887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
1192e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
120089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
1212cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
122a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Decl *VisitParmVarDecl(ParmVarDecl *D);
123c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
124b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
1252e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
126a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
1273daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
128dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
129e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
130954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
1312b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
132a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    Decl *VisitObjCClassDecl(ObjCClassDecl *D);
133040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
134040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
135040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
136040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
137d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    Decl *VisitClassTemplateSpecializationDecl(
138d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                            ClassTemplateSpecializationDecl *D);
139a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
1404800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    // Importing statements
1414800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Stmt *VisitStmt(Stmt *S);
1424800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
1434800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    // Importing expressions
1444800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Expr *VisitExpr(Expr *E);
145440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    Expr *VisitDeclRefExpr(DeclRefExpr *E);
1464800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    Expr *VisitIntegerLiteral(IntegerLiteral *E);
147b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor    Expr *VisitCharacterLiteral(CharacterLiteral *E);
148f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitParenExpr(ParenExpr *E);
149f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitUnaryOperator(UnaryOperator *E);
150f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
151f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitBinaryOperator(BinaryOperator *E);
152f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
15336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
154008847a70ab122a99911149199855060fb3753b4Douglas Gregor    Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
1551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  };
1561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
1571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//----------------------------------------------------------------------------
15973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor// Structural Equivalence
16073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor//----------------------------------------------------------------------------
16173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
16273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregornamespace {
16373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  struct StructuralEquivalenceContext {
16473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief AST contexts for which we are checking structural equivalence.
16573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    ASTContext &C1, &C2;
16673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
16773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief The set of "tentative" equivalences between two canonical
16873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// declarations, mapping from a declaration in the first context to the
16973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// declaration in the second context that we believe to be equivalent.
17073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
17173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
17273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Queue of declarations in the first context whose equivalence
17373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// with a declaration in the second context still needs to be verified.
17473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    std::deque<Decl *> DeclsToCheck;
17573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
176ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    /// \brief Declaration (from, to) pairs that are known not to be equivalent
177ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    /// (which we have already complained about).
178ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
179ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
18073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Whether we're being strict about the spelling of types when
18173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// unifying two types.
18273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool StrictTypeSpelling;
18373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
18473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
185ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor               llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
18673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                 bool StrictTypeSpelling = false)
18733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis      : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
188ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        StrictTypeSpelling(StrictTypeSpelling) { }
18973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
19073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Determine whether the two declarations are structurally
19173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// equivalent.
19273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
19373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
19473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Determine whether the two types are structurally equivalent.
19573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool IsStructurallyEquivalent(QualType T1, QualType T2);
19673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
19773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  private:
19873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \brief Finish checking all of the structural equivalences.
19973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    ///
20073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    /// \returns true if an error occurred, false otherwise.
20173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    bool Finish();
20273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
20373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  public:
20473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
20533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis      return C1.getDiagnostics().Report(Loc, DiagID);
20673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
20773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
20873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
20933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis      return C2.getDiagnostics().Report(Loc, DiagID);
21073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
21173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  };
21273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
21373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
21473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
21573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     QualType T1, QualType T2);
21673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
21773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Decl *D1, Decl *D2);
21873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
21973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine if two APInts have the same value, after zero-extending
22073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// one of them (if needed!) to ensure that the bit-widths match.
22173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
22273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() == I2.getBitWidth())
22373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == I2;
22473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
22573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() > I2.getBitWidth())
2269f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    return I1 == I2.zext(I1.getBitWidth());
22773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
2289f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad  return I1.zext(I2.getBitWidth()) == I2;
22973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
23073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
23173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine if two APSInts have the same value, zero- or sign-extending
23273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// as needed.
23373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
23473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
23573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return I1 == I2;
23673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
23773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Check for a bit-width mismatch.
23873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.getBitWidth() > I2.getBitWidth())
2399f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    return IsSameValue(I1, I2.extend(I1.getBitWidth()));
24073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  else if (I2.getBitWidth() > I1.getBitWidth())
2419f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    return IsSameValue(I1.extend(I2.getBitWidth()), I2);
24273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // We have a signedness mismatch. Turn the signed value into an unsigned
24473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // value.
24573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I1.isSigned()) {
24673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (I1.isNegative())
24773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
24873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
24973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return llvm::APSInt(I1, true) == I2;
25073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
25173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
25273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (I2.isNegative())
25373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
25473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
25573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return I1 == llvm::APSInt(I2, true);
25673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
25773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
25873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two expressions.
25973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
26073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Expr *E1, Expr *E2) {
26173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!E1 || !E2)
26273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return E1 == E2;
26373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
26473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Actually perform a structural comparison!
26573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
26673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
26773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
26873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two identifiers are equivalent.
26973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
27073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const IdentifierInfo *Name2) {
27173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!Name1 || !Name2)
27273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return Name1 == Name2;
27373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
27473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return Name1->getName() == Name2->getName();
27573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
27673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
27773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two nested-name-specifiers are equivalent.
27873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
27973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     NestedNameSpecifier *NNS1,
28073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     NestedNameSpecifier *NNS2) {
28173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Implement!
28273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
28373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
28473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
28573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine whether two template arguments are equivalent.
28673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
28773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const TemplateArgument &Arg1,
28873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     const TemplateArgument &Arg2) {
289d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (Arg1.getKind() != Arg2.getKind())
290d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return false;
291d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
292d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  switch (Arg1.getKind()) {
293d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Null:
294d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return true;
295d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
296d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Type:
297d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
298d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
299d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Integral:
300d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
301d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                          Arg2.getIntegralType()))
302d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return false;
303d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
304d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
305d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
306d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Declaration:
307d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
308d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
309d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Template:
310d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return IsStructurallyEquivalent(Context,
311d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg1.getAsTemplate(),
312d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg2.getAsTemplate());
313a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
314a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  case TemplateArgument::TemplateExpansion:
315a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    return IsStructurallyEquivalent(Context,
316a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                                    Arg1.getAsTemplateOrTemplatePattern(),
317a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                                    Arg2.getAsTemplateOrTemplatePattern());
318a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
319d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Expression:
320d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return IsStructurallyEquivalent(Context,
321d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg1.getAsExpr(), Arg2.getAsExpr());
322d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
323d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Pack:
324d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Arg1.pack_size() != Arg2.pack_size())
325d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return false;
326d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
327d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
328d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (!IsStructurallyEquivalent(Context,
329d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg1.pack_begin()[I],
330d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Arg2.pack_begin()[I]))
331d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return false;
332d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
333d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return true;
334d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
335d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
336d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm_unreachable("Invalid template argument kind");
33773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
33873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
33973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
34073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence for the common part of array
34173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// types.
34273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
34373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                          const ArrayType *Array1,
34473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                          const ArrayType *Array2) {
34573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!IsStructurallyEquivalent(Context,
34673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                Array1->getElementType(),
34773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                Array2->getElementType()))
34873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
34973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Array1->getSizeModifier() != Array2->getSizeModifier())
35073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
35173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
35273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
35373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
35473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
35573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
35673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
35773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two types.
35873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
35973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     QualType T1, QualType T2) {
36073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (T1.isNull() || T2.isNull())
36173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return T1.isNull() && T2.isNull();
36273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
36373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!Context.StrictTypeSpelling) {
36473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // We aren't being strict about token-to-token equivalence of types,
36573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // so map down to the canonical type.
36673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    T1 = Context.C1.getCanonicalType(T1);
36773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    T2 = Context.C2.getCanonicalType(T2);
36873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
36973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
37073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (T1.getQualifiers() != T2.getQualifiers())
37173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
37273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
373ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  Type::TypeClass TC = T1->getTypeClass();
374ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
375ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T1->getTypeClass() != T2->getTypeClass()) {
376ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // Compare function types with prototypes vs. without prototypes as if
377ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // both did not have prototypes.
378ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (T1->getTypeClass() == Type::FunctionProto &&
379ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        T2->getTypeClass() == Type::FunctionNoProto)
380ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      TC = Type::FunctionNoProto;
381ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    else if (T1->getTypeClass() == Type::FunctionNoProto &&
382ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor             T2->getTypeClass() == Type::FunctionProto)
383ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      TC = Type::FunctionNoProto;
384ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    else
385ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      return false;
386ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  }
38773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
388ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  switch (TC) {
389ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  case Type::Builtin:
39073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Deal with Char_S/Char_U.
39173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
39273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
39373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
39473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
39573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Complex:
39673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
39773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<ComplexType>(T1)->getElementType(),
39873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<ComplexType>(T2)->getElementType()))
39973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
40073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
40173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
40273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Pointer:
40373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
40473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<PointerType>(T1)->getPointeeType(),
40573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<PointerType>(T2)->getPointeeType()))
40673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
40773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
40873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
40973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::BlockPointer:
41073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
41173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<BlockPointerType>(T1)->getPointeeType(),
41273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<BlockPointerType>(T2)->getPointeeType()))
41373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
41473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
41573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
41673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::LValueReference:
41773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::RValueReference: {
41873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ReferenceType *Ref1 = cast<ReferenceType>(T1);
41973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ReferenceType *Ref2 = cast<ReferenceType>(T2);
42073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
42173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
42273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Ref1->isInnerRef() != Ref2->isInnerRef())
42373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
42473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
42573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ref1->getPointeeTypeAsWritten(),
42673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ref2->getPointeeTypeAsWritten()))
42773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
42873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
42973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
43073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
43173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::MemberPointer: {
43273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
43373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
43473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
43573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  MemPtr1->getPointeeType(),
43673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  MemPtr2->getPointeeType()))
43773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
43873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
43973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(MemPtr1->getClass(), 0),
44073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(MemPtr2->getClass(), 0)))
44173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
44273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
44373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
44473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
44573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ConstantArray: {
44673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
44773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
44873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsSameValue(Array1->getSize(), Array2->getSize()))
44973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
45073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
45173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
45273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
45373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
45473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
45573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
45673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::IncompleteArray:
45773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context,
45873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                       cast<ArrayType>(T1),
45973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                       cast<ArrayType>(T2)))
46073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
46273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
46373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::VariableArray: {
46473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
46573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
46673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
46773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Array1->getSizeExpr(), Array2->getSizeExpr()))
46873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
46973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
47073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
47173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
47273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
47373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
47473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
47573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
47673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::DependentSizedArray: {
47773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
47873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
47973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
48073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Array1->getSizeExpr(), Array2->getSizeExpr()))
48173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
48273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
48373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
48473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
48573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
48673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
48773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
48873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
48973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::DependentSizedExtVector: {
49073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedExtVectorType *Vec1
49173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<DependentSizedExtVectorType>(T1);
49273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const DependentSizedExtVectorType *Vec2
49373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<DependentSizedExtVectorType>(T2);
49473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
49573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getSizeExpr(), Vec2->getSizeExpr()))
49673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
49773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
49873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getElementType(),
49973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec2->getElementType()))
50073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
50173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
50273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
50373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
50473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Vector:
50573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ExtVector: {
50673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VectorType *Vec1 = cast<VectorType>(T1);
50773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const VectorType *Vec2 = cast<VectorType>(T2);
50873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
50973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec1->getElementType(),
51073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Vec2->getElementType()))
51173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
51273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Vec1->getNumElements() != Vec2->getNumElements())
51373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
514e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    if (Vec1->getVectorKind() != Vec2->getVectorKind())
51573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
5160e12b44081c6395a6d60a05a85a6012f7bb23b16Douglas Gregor    break;
51773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
51873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
51973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::FunctionProto: {
52073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
52173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
52273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getNumArgs() != Proto2->getNumArgs())
52373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
52473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
52573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
52673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto1->getArgType(I),
52773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Proto2->getArgType(I)))
52873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
52973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
53073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->isVariadic() != Proto2->isVariadic())
53173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
53260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
53373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
53460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    if (Proto1->getExceptionSpecType() == EST_Dynamic) {
53560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
53660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl        return false;
53760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
53860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl        if (!IsStructurallyEquivalent(Context,
53960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                                      Proto1->getExceptionType(I),
54060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                                      Proto2->getExceptionType(I)))
54160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl          return false;
54260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      }
54360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
54473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
54560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                                    Proto1->getNoexceptExpr(),
54660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                                    Proto2->getNoexceptExpr()))
54773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
54873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
54973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
55073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
55173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
55273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Fall through to check the bits common with FunctionNoProtoType.
55373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
55473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
55573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::FunctionNoProto: {
55673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionType *Function1 = cast<FunctionType>(T1);
55773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const FunctionType *Function2 = cast<FunctionType>(T2);
55873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
55973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Function1->getResultType(),
56073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Function2->getResultType()))
56173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
562264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      if (Function1->getExtInfo() != Function2->getExtInfo())
563264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola        return false;
56473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
56573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
56673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
56773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::UnresolvedUsing:
56873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
56973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<UnresolvedUsingType>(T1)->getDecl(),
57073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<UnresolvedUsingType>(T2)->getDecl()))
57173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
57273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
57373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
5749d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
5759d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  case Type::Attributed:
5769d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    if (!IsStructurallyEquivalent(Context,
5779d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                  cast<AttributedType>(T1)->getModifiedType(),
5789d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                  cast<AttributedType>(T2)->getModifiedType()))
5799d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall      return false;
5809d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    if (!IsStructurallyEquivalent(Context,
5819d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                cast<AttributedType>(T1)->getEquivalentType(),
5829d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                cast<AttributedType>(T2)->getEquivalentType()))
5839d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall      return false;
5849d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    break;
58573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
586075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  case Type::Paren:
587075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    if (!IsStructurallyEquivalent(Context,
588075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara                                  cast<ParenType>(T1)->getInnerType(),
589075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara                                  cast<ParenType>(T2)->getInnerType()))
590075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara      return false;
591075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    break;
592075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
59373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Typedef:
59473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
59573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypedefType>(T1)->getDecl(),
59673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypedefType>(T2)->getDecl()))
59773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
59873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
59973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
60073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TypeOfExpr:
60173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
60273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
60373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
60473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
60573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
60673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
60773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TypeOf:
60873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
60973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypeOfType>(T1)->getUnderlyingType(),
61073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TypeOfType>(T2)->getUnderlyingType()))
61173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
61273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
613ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
614ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
615ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (!IsStructurallyEquivalent(Context,
616ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                             cast<UnaryTransformType>(T1)->getUnderlyingType(),
617ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                             cast<UnaryTransformType>(T1)->getUnderlyingType()))
618ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      return false;
619ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    break;
620ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
62173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Decltype:
62273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
62373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<DecltypeType>(T1)->getUnderlyingExpr(),
62473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<DecltypeType>(T2)->getUnderlyingExpr()))
62573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
62673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
62773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
62834b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto:
62934b41d939a1328f484511c6002ba2456db879a29Richard Smith    if (!IsStructurallyEquivalent(Context,
63034b41d939a1328f484511c6002ba2456db879a29Richard Smith                                  cast<AutoType>(T1)->getDeducedType(),
63134b41d939a1328f484511c6002ba2456db879a29Richard Smith                                  cast<AutoType>(T2)->getDeducedType()))
63234b41d939a1328f484511c6002ba2456db879a29Richard Smith      return false;
63334b41d939a1328f484511c6002ba2456db879a29Richard Smith    break;
63434b41d939a1328f484511c6002ba2456db879a29Richard Smith
63573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Record:
63673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::Enum:
63773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
63873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TagType>(T1)->getDecl(),
63973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  cast<TagType>(T2)->getDecl()))
64073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
64173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
642465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
64373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TemplateTypeParm: {
64473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
64573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
64673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->getDepth() != Parm2->getDepth())
64773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
64873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->getIndex() != Parm2->getIndex())
64973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
65073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Parm1->isParameterPack() != Parm2->isParameterPack())
65173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
65273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
65373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Names of template type parameters are never significant.
65473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
65573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
65673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
65773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::SubstTemplateTypeParm: {
65873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const SubstTemplateTypeParmType *Subst1
65973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<SubstTemplateTypeParmType>(T1);
66073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const SubstTemplateTypeParmType *Subst2
66173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<SubstTemplateTypeParmType>(T2);
66273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
66373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Subst1->getReplacedParameter(), 0),
66473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  QualType(Subst2->getReplacedParameter(), 0)))
66573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
66673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
66773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Subst1->getReplacementType(),
66873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Subst2->getReplacementType()))
66973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
67073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
67173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
67273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
6730bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  case Type::SubstTemplateTypeParmPack: {
6740bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    const SubstTemplateTypeParmPackType *Subst1
6750bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor      = cast<SubstTemplateTypeParmPackType>(T1);
6760bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    const SubstTemplateTypeParmPackType *Subst2
6770bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor      = cast<SubstTemplateTypeParmPackType>(T2);
6780bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    if (!IsStructurallyEquivalent(Context,
6790bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                                  QualType(Subst1->getReplacedParameter(), 0),
6800bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                                  QualType(Subst2->getReplacedParameter(), 0)))
6810bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor      return false;
6820bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    if (!IsStructurallyEquivalent(Context,
6830bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                                  Subst1->getArgumentPack(),
6840bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                                  Subst2->getArgumentPack()))
6850bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor      return false;
6860bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    break;
6870bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  }
68873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::TemplateSpecialization: {
68973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateSpecializationType *Spec1
69073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<TemplateSpecializationType>(T1);
69173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const TemplateSpecializationType *Spec2
69273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      = cast<TemplateSpecializationType>(T2);
69373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
69473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Spec1->getTemplateName(),
69573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Spec2->getTemplateName()))
69673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
69773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Spec1->getNumArgs() != Spec2->getNumArgs())
69873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
69973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
70073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
70173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                    Spec1->getArg(I), Spec2->getArg(I)))
70273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
70373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
70473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
70573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
70673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
707465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Type::Elaborated: {
708465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
709465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
710465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    // CHECKME: what if a keyword is ETK_None or ETK_typename ?
711465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (Elab1->getKeyword() != Elab2->getKeyword())
712465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return false;
71373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
714465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab1->getQualifier(),
715465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab2->getQualifier()))
71673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
71773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
718465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab1->getNamedType(),
719465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                  Elab2->getNamedType()))
72073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
72173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
72273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
72373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
7243cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  case Type::InjectedClassName: {
7253cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
7263cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
7273cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (!IsStructurallyEquivalent(Context,
72831f17ecbef57b5679c017c375db330546b7b5145John McCall                                  Inj1->getInjectedSpecializationType(),
72931f17ecbef57b5679c017c375db330546b7b5145John McCall                                  Inj2->getInjectedSpecializationType()))
7303cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      return false;
7313cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    break;
7323cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
7333cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
7344714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  case Type::DependentName: {
7354714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    const DependentNameType *Typename1 = cast<DependentNameType>(T1);
7364714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    const DependentNameType *Typename2 = cast<DependentNameType>(T2);
73773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
73873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename1->getQualifier(),
73973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename2->getQualifier()))
74073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
74173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
74273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Typename2->getIdentifier()))
74373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
74473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
74573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
74673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
74773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
74833500955d731c73717af52088b7fc0e7a85681e7John McCall  case Type::DependentTemplateSpecialization: {
74933500955d731c73717af52088b7fc0e7a85681e7John McCall    const DependentTemplateSpecializationType *Spec1 =
75033500955d731c73717af52088b7fc0e7a85681e7John McCall      cast<DependentTemplateSpecializationType>(T1);
75133500955d731c73717af52088b7fc0e7a85681e7John McCall    const DependentTemplateSpecializationType *Spec2 =
75233500955d731c73717af52088b7fc0e7a85681e7John McCall      cast<DependentTemplateSpecializationType>(T2);
75333500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!IsStructurallyEquivalent(Context,
75433500955d731c73717af52088b7fc0e7a85681e7John McCall                                  Spec1->getQualifier(),
75533500955d731c73717af52088b7fc0e7a85681e7John McCall                                  Spec2->getQualifier()))
75633500955d731c73717af52088b7fc0e7a85681e7John McCall      return false;
75733500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
75833500955d731c73717af52088b7fc0e7a85681e7John McCall                                  Spec2->getIdentifier()))
75933500955d731c73717af52088b7fc0e7a85681e7John McCall      return false;
76033500955d731c73717af52088b7fc0e7a85681e7John McCall    if (Spec1->getNumArgs() != Spec2->getNumArgs())
76133500955d731c73717af52088b7fc0e7a85681e7John McCall      return false;
76233500955d731c73717af52088b7fc0e7a85681e7John McCall    for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
76333500955d731c73717af52088b7fc0e7a85681e7John McCall      if (!IsStructurallyEquivalent(Context,
76433500955d731c73717af52088b7fc0e7a85681e7John McCall                                    Spec1->getArg(I), Spec2->getArg(I)))
76533500955d731c73717af52088b7fc0e7a85681e7John McCall        return false;
76633500955d731c73717af52088b7fc0e7a85681e7John McCall    }
76733500955d731c73717af52088b7fc0e7a85681e7John McCall    break;
76833500955d731c73717af52088b7fc0e7a85681e7John McCall  }
7697536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
7707536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  case Type::PackExpansion:
7717536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    if (!IsStructurallyEquivalent(Context,
7727536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor                                  cast<PackExpansionType>(T1)->getPattern(),
7737536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor                                  cast<PackExpansionType>(T2)->getPattern()))
7747536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor      return false;
7757536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    break;
7767536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
77773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ObjCInterface: {
77873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
77973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
78073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
78173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Iface1->getDecl(), Iface2->getDecl()))
78273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
783c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    break;
784c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  }
785c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
786c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject: {
787c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
788c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
789c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (!IsStructurallyEquivalent(Context,
790c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                  Obj1->getBaseType(),
791c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                  Obj2->getBaseType()))
792c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return false;
793c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
79473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
795c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
79673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsStructurallyEquivalent(Context,
797c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Obj1->getProtocol(I),
798c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Obj2->getProtocol(I)))
79973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
80073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
80173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
80273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
80373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
80473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  case Type::ObjCObjectPointer: {
80573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
80673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
80773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
80873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ptr1->getPointeeType(),
80973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Ptr2->getPointeeType()))
81073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
81173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    break;
81273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
81373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
81473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  } // end switch
81573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
81673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
81773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
81873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
81973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two records.
82073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
82173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     RecordDecl *D1, RecordDecl *D2) {
82273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (D1->isUnion() != D2->isUnion()) {
82373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
82473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
82573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
82673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << D1->getDeclName() << (unsigned)D1->getTagKind();
82773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
82873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
82973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
830d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // If both declarations are class template specializations, we know
831d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // the ODR applies, so check the template and template arguments.
832d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateSpecializationDecl *Spec1
833d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = dyn_cast<ClassTemplateSpecializationDecl>(D1);
834d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateSpecializationDecl *Spec2
835d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = dyn_cast<ClassTemplateSpecializationDecl>(D2);
836d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (Spec1 && Spec2) {
837d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Check that the specialized templates are the same.
838d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
839d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                  Spec2->getSpecializedTemplate()))
840d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return false;
841d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
842d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Check that the template arguments are the same.
843d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
844d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return false;
845d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
846d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
847d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (!IsStructurallyEquivalent(Context,
848d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Spec1->getTemplateArgs().get(I),
849d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Spec2->getTemplateArgs().get(I)))
850d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return false;
851d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
852d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // If one is a class template specialization and the other is not, these
853fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner  // structures are different.
854d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  else if (Spec1 || Spec2)
855d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return false;
856d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
857ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Compare the definitions of these two records. If either or both are
858ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // incomplete, we assume that they are equivalent.
859ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  D1 = D1->getDefinition();
860ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  D2 = D2->getDefinition();
861ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (!D1 || !D2)
862ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return true;
863ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
86473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
86573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
86673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
86773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
868040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          << Context.C2.getTypeDeclType(D2);
86973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
870040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          << D2CXX->getNumBases();
87173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
872040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          << D1CXX->getNumBases();
87373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
87473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
87573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
87673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      // Check the base classes.
87773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
87873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                           BaseEnd1 = D1CXX->bases_end(),
87973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                Base2 = D2CXX->bases_begin();
88073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor           Base1 != BaseEnd1;
88173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor           ++Base1, ++Base2) {
88273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!IsStructurallyEquivalent(Context,
88373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                      Base1->getType(), Base2->getType())) {
88473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
88573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Context.C2.getTypeDeclType(D2);
88673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
88773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->getType()
88873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->getSourceRange();
88973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
89073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getType()
89173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getSourceRange();
89273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          return false;
89373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        }
89473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
89573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check virtual vs. non-virtual inheritance mismatch.
89673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (Base1->isVirtual() != Base2->isVirtual()) {
89773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
89873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Context.C2.getTypeDeclType(D2);
89973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(Base2->getSourceRange().getBegin(),
90073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                        diag::note_odr_virtual_base)
90173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->isVirtual() << Base2->getSourceRange();
90273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
90373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->isVirtual()
90473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getSourceRange();
90573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          return false;
90673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        }
90773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
90873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    } else if (D1CXX->getNumBases() > 0) {
90973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
91073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
91173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
91273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
91373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Base1->getType()
91473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Base1->getSourceRange();
91573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
91673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
91773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
91873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
91973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
92073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Check the fields for consistency.
92173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
92273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                             Field2End = D2->field_end();
92373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
92473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Field1End = D1->field_end();
92573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       Field1 != Field1End;
92673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       ++Field1, ++Field2) {
92773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field2 == Field2End) {
92873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
92973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
93073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Field1->getLocation(), diag::note_odr_field)
93173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName() << Field1->getType();
93273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
93373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
93473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
93573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
93673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
93773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Field1->getType(), Field2->getType())) {
93873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
93973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
94073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(Field2->getLocation(), diag::note_odr_field)
94173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field2->getDeclName() << Field2->getType();
94273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Field1->getLocation(), diag::note_odr_field)
94373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName() << Field1->getType();
94473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
94573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
94673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
94773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field1->isBitField() != Field2->isBitField()) {
94873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
94973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
95073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (Field1->isBitField()) {
95173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        llvm::APSInt Bits;
95273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
95373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
95473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field1->getDeclName() << Field1->getType()
95573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits.toString(10, false);
95673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
95773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName();
95873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
95973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        llvm::APSInt Bits;
96073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
96173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
96273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName() << Field2->getType()
96373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits.toString(10, false);
96473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(),
96573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                          diag::note_odr_not_bit_field)
96673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName();
96773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
96873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
96973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
97073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
97173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field1->isBitField()) {
97273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      // Make sure that the bit-fields are the same length.
97373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      llvm::APSInt Bits1, Bits2;
97473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
97573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
97673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
97773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
97873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
97973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsSameValue(Bits1, Bits2)) {
98073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
98173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Context.C2.getTypeDeclType(D2);
98273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
98373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName() << Field2->getType()
98473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits2.toString(10, false);
98573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
98673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field1->getDeclName() << Field1->getType()
98773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits1.toString(10, false);
98873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
98973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
99073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
99173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
99273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
99373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Field2 != Field2End) {
99473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
99573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
99673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(Field2->getLocation(), diag::note_odr_field)
99773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Field2->getDeclName() << Field2->getType();
99873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
99973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
100073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
100173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
100273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
100373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
100473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
100573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two enums.
100673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
100773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     EnumDecl *D1, EnumDecl *D2) {
100873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
100973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                             EC2End = D2->enumerator_end();
101073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
101173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  EC1End = D1->enumerator_end();
101273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       EC1 != EC1End; ++EC1, ++EC2) {
101373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (EC2 == EC2End) {
101473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
101573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
101673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
101773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getDeclName()
101873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getInitVal().toString(10);
101973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
102073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
102173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
102273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
102373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::APSInt Val1 = EC1->getInitVal();
102473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::APSInt Val2 = EC2->getInitVal();
102573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsSameValue(Val1, Val2) ||
102673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
102773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
102873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
102973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
103073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC2->getDeclName()
103173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC2->getInitVal().toString(10);
103273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
103373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getDeclName()
103473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getInitVal().toString(10);
103573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
103673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
103773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
103873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
103973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (EC2 != EC2End) {
104073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
104173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
104273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
104373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << EC2->getDeclName()
104473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << EC2->getInitVal().toString(10);
104573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
104673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
104773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
104873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
104973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
105073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
1051040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1052040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1053040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateParameterList *Params1,
1054040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateParameterList *Params2) {
1055040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (Params1->size() != Params2->size()) {
1056040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(Params2->getTemplateLoc(),
1057040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                  diag::err_odr_different_num_template_parameters)
1058040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << Params1->size() << Params2->size();
1059040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(Params1->getTemplateLoc(),
1060040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                  diag::note_odr_template_parameter_list);
1061040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1062040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1063040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1064040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1065040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1066040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Context.Diag2(Params2->getParam(I)->getLocation(),
1067040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                    diag::err_odr_different_template_parameter_kind);
1068040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Context.Diag1(Params1->getParam(I)->getLocation(),
1069040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                    diag::note_odr_template_parameter_here);
1070040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return false;
1071040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
1072040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1073040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1074040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          Params2->getParam(I))) {
1075040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1076040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return false;
1077040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
1078040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1079040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1080040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return true;
1081040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1082040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1083040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1084040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTypeParmDecl *D1,
1085040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTypeParmDecl *D2) {
1086040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D1->isParameterPack() != D2->isParameterPack()) {
1087040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1088040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D2->isParameterPack();
1089040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1090040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D1->isParameterPack();
1091040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1092040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1093040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1094040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return true;
1095040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1096040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1097040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1098040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     NonTypeTemplateParmDecl *D1,
1099040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     NonTypeTemplateParmDecl *D2) {
1100040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Enable once we have variadic templates.
1101040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#if 0
1102040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D1->isParameterPack() != D2->isParameterPack()) {
1103040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1104040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D2->isParameterPack();
1105040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1106040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D1->isParameterPack();
1107040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1108040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1109040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#endif
1110040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1111040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check types.
1112040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1113040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(),
1114040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                  diag::err_odr_non_type_parameter_type_inconsistent)
1115040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D2->getType() << D1->getType();
1116040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1117040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D1->getType();
1118040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1119040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1120040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1121040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return true;
1122040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1123040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1124040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1125040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTemplateParmDecl *D1,
1126040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTemplateParmDecl *D2) {
1127040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Enable once we have variadic templates.
1128040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#if 0
1129040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D1->isParameterPack() != D2->isParameterPack()) {
1130040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1131040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    << D2->isParameterPack();
1132040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1133040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    << D1->isParameterPack();
1134040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1135040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1136040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#endif
1137040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1138040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check template parameter lists.
1139040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1140040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                  D2->getTemplateParameters());
1141040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1142040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1143040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1144040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     ClassTemplateDecl *D1,
1145040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     ClassTemplateDecl *D2) {
1146040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check template parameters.
1147040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!IsStructurallyEquivalent(Context,
1148040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                D1->getTemplateParameters(),
1149040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                D2->getTemplateParameters()))
1150040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
115173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
1152040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check the templated declaration.
1153040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1154040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          D2->getTemplatedDecl());
1155040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1156040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
115773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two declarations.
115873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
115973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Decl *D1, Decl *D2) {
116073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Check for known structural equivalences via a callback of some sort.
116173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
1162ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Check whether we already know that these two declarations are not
1163ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // structurally equivalent.
1164ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1165ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                      D2->getCanonicalDecl())))
1166ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return false;
1167ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
116873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Determine whether we've already produced a tentative equivalence for D1.
116973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
117073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (EquivToD1)
117173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return EquivToD1 == D2->getCanonicalDecl();
117273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
117373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Produce a tentative equivalence D1 <-> D2, which will be checked later.
117473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EquivToD1 = D2->getCanonicalDecl();
117573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
117673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
117773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
117873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
117973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
118073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                            Decl *D2) {
118173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!::IsStructurallyEquivalent(*this, D1, D2))
118273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
118373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
118473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return !Finish();
118573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
118673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
118773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
118873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                            QualType T2) {
118973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!::IsStructurallyEquivalent(*this, T1, T2))
119073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
119173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
119273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return !Finish();
119373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
119473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
119573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::Finish() {
119673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  while (!DeclsToCheck.empty()) {
119773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Check the next declaration.
119873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Decl *D1 = DeclsToCheck.front();
119973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DeclsToCheck.pop_front();
120073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
120173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Decl *D2 = TentativeEquivalences[D1];
120273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    assert(D2 && "Unrecorded tentative equivalence?");
120373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
1204ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    bool Equivalent = true;
1205ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
120673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Switch on all declaration kinds. For now, we're just going to
120773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // check the obvious ones.
120873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
120973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
121073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check for equivalent structure names.
121173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name1 = Record1->getIdentifier();
1212162e1c1b487352434552147967c3dd296ebee2f7Richard Smith        if (!Name1 && Record1->getTypedefNameForAnonDecl())
1213162e1c1b487352434552147967c3dd296ebee2f7Richard Smith          Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
121473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name2 = Record2->getIdentifier();
1215162e1c1b487352434552147967c3dd296ebee2f7Richard Smith        if (!Name2 && Record2->getTypedefNameForAnonDecl())
1216162e1c1b487352434552147967c3dd296ebee2f7Richard Smith          Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
1217ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (!::IsStructurallyEquivalent(Name1, Name2) ||
1218ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this, Record1, Record2))
1219ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
122073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
122173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Record/non-record mismatch.
1222ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
122373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
1224ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
122573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
122673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check for equivalent enum names.
122773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name1 = Enum1->getIdentifier();
1228162e1c1b487352434552147967c3dd296ebee2f7Richard Smith        if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1229162e1c1b487352434552147967c3dd296ebee2f7Richard Smith          Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
123073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name2 = Enum2->getIdentifier();
1231162e1c1b487352434552147967c3dd296ebee2f7Richard Smith        if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1232162e1c1b487352434552147967c3dd296ebee2f7Richard Smith          Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
1233ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (!::IsStructurallyEquivalent(Name1, Name2) ||
1234ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1235ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
123673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
123773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Enum/non-enum mismatch
1238ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
123973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
1240162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1241162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
124273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
1243ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                        Typedef2->getIdentifier()) ||
1244ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this,
124573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        Typedef1->getUnderlyingType(),
124673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        Typedef2->getUnderlyingType()))
1247ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
124873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
124973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Typedef/non-typedef mismatch.
1250ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
125173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
1252040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (ClassTemplateDecl *ClassTemplate1
1253040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                           = dyn_cast<ClassTemplateDecl>(D1)) {
1254040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1255040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1256040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                        ClassTemplate2->getIdentifier()) ||
1257040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor            !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1258040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1259040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1260040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Class template/non-class-template mismatch.
1261040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1262040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1263040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1264040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1265040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1266040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1267040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1268040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Kind mismatch.
1269040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1270040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1271040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (NonTypeTemplateParmDecl *NTTP1
1272040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1273040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (NonTypeTemplateParmDecl *NTTP2
1274040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1275040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1276040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1277040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1278040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Kind mismatch.
1279040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1280040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1281040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (TemplateTemplateParmDecl *TTP1
1282040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                  = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1283040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (TemplateTemplateParmDecl *TTP2
1284040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                    = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1285040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1286040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1287040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1288040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Kind mismatch.
1289040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1290040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1291040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
1292040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1293ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (!Equivalent) {
1294ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      // Note that these two declarations are not equivalent (and we already
1295ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      // know about it).
1296ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1297ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                               D2->getCanonicalDecl()));
1298ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      return true;
1299ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    }
130073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Check other declaration kinds!
130173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
130273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
130373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return false;
130473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
130573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
130673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor//----------------------------------------------------------------------------
13071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor// Import Types
13081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//----------------------------------------------------------------------------
13091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1310f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitType(const Type *T) {
131189cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor  Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
131289cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    << T->getTypeClassName();
131389cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor  return QualType();
131489cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor}
131589cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor
1316f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
13171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  switch (T->getKind()) {
13181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Void: return Importer.getToContext().VoidTy;
13191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Bool: return Importer.getToContext().BoolTy;
13201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char_U:
13221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // The context we're importing from has an unsigned 'char'. If we're
13231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // importing into a context with a signed 'char', translate to
13241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // 'unsigned char' instead.
13251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (Importer.getToContext().getLangOptions().CharIsSigned)
13261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return Importer.getToContext().UnsignedCharTy;
13271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().CharTy;
13291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
13311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char16:
13331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++!
13341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().Char16Ty;
13351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char32:
13371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++!
13381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().Char32Ty;
13391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
13411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
13421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
13431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ULongLong:
13441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().UnsignedLongLongTy;
13451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
13461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char_S:
13481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // The context we're importing from has an unsigned 'char'. If we're
13491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // importing into a context with a signed 'char', translate to
13501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // 'unsigned char' instead.
13511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Importer.getToContext().getLangOptions().CharIsSigned)
13521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return Importer.getToContext().SignedCharTy;
13531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().CharTy;
13551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
13573f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::WChar_S:
13583f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::WChar_U:
13591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: If not in C++, shall we translate to the C equivalent of
13601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // wchar_t?
13611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().WCharTy;
13621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Short : return Importer.getToContext().ShortTy;
13641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Int : return Importer.getToContext().IntTy;
13651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Long : return Importer.getToContext().LongTy;
13661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
13671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
13681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Float: return Importer.getToContext().FloatTy;
13691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Double: return Importer.getToContext().DoubleTy;
13701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
13711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::NullPtr:
13731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++0x!
13741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().NullPtrTy;
13751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
13771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
13781de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  case BuiltinType::UnknownAny: return Importer.getToContext().UnknownAnyTy;
1379864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  case BuiltinType::BoundMember: return Importer.getToContext().BoundMemberTy;
13801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCId:
13821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports Objective-C!
13831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinIdTy;
13841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCClass:
13861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinClassTy;
13871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCSel:
13891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinSelTy;
13901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
13911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return QualType();
13931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
13941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1395f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
13961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
13971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
13981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
13991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getComplexType(ToElementType);
14011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1403f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
14041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
14051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
14061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getPointerType(ToPointeeType);
14091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1411f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
14121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for blocks support in "to" context.
14131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
14141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
14151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getBlockPointerType(ToPointeeType);
14181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1420f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType
1421f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
14221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++ support in "to" context.
14231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
14241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
14251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getLValueReferenceType(ToPointeeType);
14281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1430f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType
1431f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
14321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++0x support in "to" context.
14331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
14341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
14351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getRValueReferenceType(ToPointeeType);
14381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1440f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
14411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++ support in "to" context.
14421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
14431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
14441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
14471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getMemberPointerType(ToPointeeType,
14481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      ClassType.getTypePtr());
14491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1451f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
14521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getConstantArrayType(ToElementType,
14571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSize(),
14581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSizeModifier(),
14591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->getIndexTypeCVRQualifiers());
14601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1462f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType
1463f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
14641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getIncompleteArrayType(ToElementType,
14691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                        T->getSizeModifier(),
14701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                T->getIndexTypeCVRQualifiers());
14711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1473f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
14741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *Size = Importer.Import(T->getSizeExpr());
14791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!Size)
14801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  SourceRange Brackets = Importer.Import(T->getBracketsRange());
14831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getVariableArrayType(ToElementType, Size,
14841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSizeModifier(),
14851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                T->getIndexTypeCVRQualifiers(),
14861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      Brackets);
14871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1489f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
14901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getVectorType(ToElementType,
14951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->getNumElements(),
1496e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                               T->getVectorKind());
14971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1499f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
15001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
15011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
15021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getExtVectorType(ToElementType,
15051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                  T->getNumElements());
15061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1508f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType
1509f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
15101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: What happens if we're importing a function without a prototype
15111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // into C++? Should we make it variadic?
15121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToResultType = Importer.Import(T->getResultType());
15131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToResultType.isNull())
15141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
1515264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola
15161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getFunctionNoProtoType(ToResultType,
1517264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                        T->getExtInfo());
15181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1520f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
15211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToResultType = Importer.Import(T->getResultType());
15221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToResultType.isNull())
15231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Import argument types
15265f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 4> ArgTypes;
15271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
15281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                         AEnd = T->arg_type_end();
15291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       A != AEnd; ++A) {
15301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType ArgType = Importer.Import(*A);
15311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (ArgType.isNull())
15321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
15331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ArgTypes.push_back(ArgType);
15341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
15351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Import exception types
15375f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 4> ExceptionTypes;
15381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (FunctionProtoType::exception_iterator E = T->exception_begin(),
15391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                          EEnd = T->exception_end();
15401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       E != EEnd; ++E) {
15411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType ExceptionType = Importer.Import(*E);
15421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (ExceptionType.isNull())
15431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
15441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ExceptionTypes.push_back(ExceptionType);
15451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
1546e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
1547e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1548e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.Exceptions = ExceptionTypes.data();
15491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1551e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                                 ArgTypes.size(), EPI);
15521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15540aeb2890389ec1872e49a18fb2022bfb9f96578dSean CallananQualType ASTNodeImporter::VisitParenType(const ParenType *T) {
15550aeb2890389ec1872e49a18fb2022bfb9f96578dSean Callanan  QualType ToInnerType = Importer.Import(T->getInnerType());
15560aeb2890389ec1872e49a18fb2022bfb9f96578dSean Callanan  if (ToInnerType.isNull())
15570aeb2890389ec1872e49a18fb2022bfb9f96578dSean Callanan    return QualType();
15580aeb2890389ec1872e49a18fb2022bfb9f96578dSean Callanan
15590aeb2890389ec1872e49a18fb2022bfb9f96578dSean Callanan  return Importer.getToContext().getParenType(ToInnerType);
15600aeb2890389ec1872e49a18fb2022bfb9f96578dSean Callanan}
15610aeb2890389ec1872e49a18fb2022bfb9f96578dSean Callanan
1562f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
1563162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  TypedefNameDecl *ToDecl
1564162e1c1b487352434552147967c3dd296ebee2f7Richard Smith             = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
15651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
15661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeDeclType(ToDecl);
15691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1571f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
15721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
15731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToExpr)
15741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeOfExprType(ToExpr);
15771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1579f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
15801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
15811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToUnderlyingType.isNull())
15821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeOfType(ToUnderlyingType);
15851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1587f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
158834b41d939a1328f484511c6002ba2456db879a29Richard Smith  // FIXME: Make sure that the "to" context supports C++0x!
15891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
15901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToExpr)
15911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getDecltypeType(ToExpr);
15941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1596ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1597ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  QualType ToBaseType = Importer.Import(T->getBaseType());
1598ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1599ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1600ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    return QualType();
1601ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
1602ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  return Importer.getToContext().getUnaryTransformType(ToBaseType,
1603ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                                       ToUnderlyingType,
1604ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                                       T->getUTTKind());
1605ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
1606ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
160734b41d939a1328f484511c6002ba2456db879a29Richard SmithQualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
160834b41d939a1328f484511c6002ba2456db879a29Richard Smith  // FIXME: Make sure that the "to" context supports C++0x!
160934b41d939a1328f484511c6002ba2456db879a29Richard Smith  QualType FromDeduced = T->getDeducedType();
161034b41d939a1328f484511c6002ba2456db879a29Richard Smith  QualType ToDeduced;
161134b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (!FromDeduced.isNull()) {
161234b41d939a1328f484511c6002ba2456db879a29Richard Smith    ToDeduced = Importer.Import(FromDeduced);
161334b41d939a1328f484511c6002ba2456db879a29Richard Smith    if (ToDeduced.isNull())
161434b41d939a1328f484511c6002ba2456db879a29Richard Smith      return QualType();
161534b41d939a1328f484511c6002ba2456db879a29Richard Smith  }
161634b41d939a1328f484511c6002ba2456db879a29Richard Smith
161734b41d939a1328f484511c6002ba2456db879a29Richard Smith  return Importer.getToContext().getAutoType(ToDeduced);
161834b41d939a1328f484511c6002ba2456db879a29Richard Smith}
161934b41d939a1328f484511c6002ba2456db879a29Richard Smith
1620f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
16211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  RecordDecl *ToDecl
16221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
16231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
16241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
16251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
16261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTagDeclType(ToDecl);
16271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
16281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1629f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
16301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  EnumDecl *ToDecl
16311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
16321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
16331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
16341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
16351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTagDeclType(ToDecl);
16361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
16371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1638d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorQualType ASTNodeImporter::VisitTemplateSpecializationType(
1639f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall                                       const TemplateSpecializationType *T) {
1640d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1641d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (ToTemplate.isNull())
1642d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return QualType();
1643d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
16445f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 2> ToTemplateArgs;
1645d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1646d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return QualType();
1647d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1648d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  QualType ToCanonType;
1649d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (!QualType(T, 0).isCanonical()) {
1650d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualType FromCanonType
1651d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1652d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ToCanonType =Importer.Import(FromCanonType);
1653d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToCanonType.isNull())
1654d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return QualType();
1655d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1656d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1657d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                         ToTemplateArgs.data(),
1658d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                         ToTemplateArgs.size(),
1659d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                               ToCanonType);
1660d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1661d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1662f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
1663465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *ToQualifier = 0;
1664465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  // Note: the qualifier in an ElaboratedType is optional.
1665465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T->getQualifier()) {
1666465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ToQualifier = Importer.Import(T->getQualifier());
1667465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (!ToQualifier)
1668465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return QualType();
1669465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
16701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
16711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToNamedType = Importer.Import(T->getNamedType());
16721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToNamedType.isNull())
16731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
16741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1675465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Importer.getToContext().getElaboratedType(T->getKeyword(),
1676465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                                   ToQualifier, ToNamedType);
16771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
16781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1679f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
16801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  ObjCInterfaceDecl *Class
16811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
16821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!Class)
16831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
16841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1685c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCInterfaceType(Class);
1686c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
1687c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
1688f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1689c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  QualType ToBaseType = Importer.Import(T->getBaseType());
1690c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ToBaseType.isNull())
1691c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType();
1692c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
16935f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ObjCProtocolDecl *, 4> Protocols;
1694c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator P = T->qual_begin(),
16951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                     PEnd = T->qual_end();
16961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       P != PEnd; ++P) {
16971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ObjCProtocolDecl *Protocol
16981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
16991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Protocol)
17001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
17011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    Protocols.push_back(Protocol);
17021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
17031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1704c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCObjectType(ToBaseType,
1705c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                                   Protocols.data(),
1706c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                                   Protocols.size());
17071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
17081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1709f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType
1710f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
17111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
17121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
17131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
17141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1715c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
17161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
17171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1718089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor//----------------------------------------------------------------------------
1719089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor// Import Declarations
1720089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor//----------------------------------------------------------------------------
1721a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregorbool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1722a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      DeclContext *&LexicalDC,
1723a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      DeclarationName &Name,
1724a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      SourceLocation &Loc) {
1725089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Import the context of this declaration.
1726a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DC = Importer.ImportContext(D->getDeclContext());
1727089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  if (!DC)
1728a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return true;
1729a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1730a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  LexicalDC = DC;
17319bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
17329bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
17339bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    if (!LexicalDC)
1734a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      return true;
17359bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  }
1736a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1737089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Import the name of this declaration.
1738a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Name = Importer.Import(D->getDeclName());
1739089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  if (D->getDeclName() && !Name)
1740a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return true;
1741a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1742a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the location of this declaration.
1743a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Loc = Importer.Import(D->getLocation());
1744a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  return false;
1745a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1746a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
17471cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregorvoid ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
17481cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (!FromD)
17491cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    return;
17501cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
17511cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (!ToD) {
17521cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    ToD = Importer.Import(FromD);
17531cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    if (!ToD)
17541cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor      return;
17551cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  }
17561cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
17571cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
17581cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
17591cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor      if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
17601cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor        ImportDefinition(FromRecord, ToRecord);
17611cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor      }
17621cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    }
17631cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    return;
17641cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  }
17651cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
17661cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
17671cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
17681cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor      if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
17691cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor        ImportDefinition(FromEnum, ToEnum);
17701cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor      }
17711cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    }
17721cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    return;
17731cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  }
17741cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor}
17751cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
17762577743c5650c646fb705df01403707e94f2df04Abramo Bagnaravoid
17772577743c5650c646fb705df01403707e94f2df04Abramo BagnaraASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
17782577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                          DeclarationNameInfo& To) {
17792577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  // NOTE: To.Name and To.Loc are already imported.
17802577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  // We only have to import To.LocInfo.
17812577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  switch (To.getName().getNameKind()) {
17822577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::Identifier:
17832577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::ObjCZeroArgSelector:
17842577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::ObjCOneArgSelector:
17852577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::ObjCMultiArgSelector:
17862577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXUsingDirective:
17872577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
17882577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
17892577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXOperatorName: {
17902577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    SourceRange Range = From.getCXXOperatorNameRange();
17912577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    To.setCXXOperatorNameRange(Importer.Import(Range));
17922577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
17932577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
17942577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXLiteralOperatorName: {
17952577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
17962577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
17972577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
17982577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
17992577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXConstructorName:
18002577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXDestructorName:
18012577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXConversionFunctionName: {
18022577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
18032577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    To.setNamedTypeInfo(Importer.Import(FromTInfo));
18042577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
18052577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
18062577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    assert(0 && "Unknown name kind.");
18072577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
18082577743c5650c646fb705df01403707e94f2df04Abramo Bagnara}
18092577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
1810d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregorvoid ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1811d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor  if (Importer.isMinimalImport() && !ForceImport) {
18128cc4fd795e01d50a7a7c96f4c0356d23b00d9349Sean Callanan    Importer.ImportContext(FromDC);
1813d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    return;
1814d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor  }
1815d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor
1816083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1817083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor                               FromEnd = FromDC->decls_end();
1818083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor       From != FromEnd;
1819083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor       ++From)
1820083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    Importer.Import(*From);
1821083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor}
1822083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor
18231cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregorbool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
18241cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor                                       bool ForceImport) {
18251cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (To->getDefinition() || To->isBeingDefined())
1826d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return false;
1827d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1828d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  To->startDefinition();
1829d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1830d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Add base classes.
1831d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1832d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1833d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
18345f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<CXXBaseSpecifier *, 4> Bases;
1835d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (CXXRecordDecl::base_class_iterator
1836d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                  Base1 = FromCXX->bases_begin(),
1837d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor            FromBaseEnd = FromCXX->bases_end();
1838d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor         Base1 != FromBaseEnd;
1839d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor         ++Base1) {
1840d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      QualType T = Importer.Import(Base1->getType());
1841d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (T.isNull())
1842c04d9d1be026cb201a716df1cd28a88878958beeDouglas Gregor        return true;
1843f90b27ad077c3339b62befc892382845339f9490Douglas Gregor
1844f90b27ad077c3339b62befc892382845339f9490Douglas Gregor      SourceLocation EllipsisLoc;
1845f90b27ad077c3339b62befc892382845339f9490Douglas Gregor      if (Base1->isPackExpansion())
1846f90b27ad077c3339b62befc892382845339f9490Douglas Gregor        EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
18471cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
18481cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor      // Ensure that we have a definition for the base.
18491cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor      ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
18501cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
1851d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      Bases.push_back(
1852d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                    new (Importer.getToContext())
1853d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                      CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1854d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                       Base1->isVirtual(),
1855d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                       Base1->isBaseOfClass(),
1856d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                       Base1->getAccessSpecifierAsWritten(),
1857f90b27ad077c3339b62befc892382845339f9490Douglas Gregor                                   Importer.Import(Base1->getTypeSourceInfo()),
1858f90b27ad077c3339b62befc892382845339f9490Douglas Gregor                                       EllipsisLoc));
1859d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
1860d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Bases.empty())
1861d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      ToCXX->setBases(Bases.data(), Bases.size());
1862d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1863d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1864673e775beba71a69dbab4fcd733a84f4cfe2ebfcSean Callanan  ImportDeclContext(From, ForceImport);
1865d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  To->completeDefinition();
1866c04d9d1be026cb201a716df1cd28a88878958beeDouglas Gregor  return false;
1867d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1868d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
18691cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregorbool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
18701cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor                                       bool ForceImport) {
18711cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (To->getDefinition() || To->isBeingDefined())
18721cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    return false;
18731cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
18741cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  To->startDefinition();
18751cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
18761cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
18771cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (T.isNull())
18781cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    return true;
18791cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
18801cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  QualType ToPromotionType = Importer.Import(From->getPromotionType());
18811cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (ToPromotionType.isNull())
18821cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    return true;
18831cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
18841cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  ImportDeclContext(From, ForceImport);
18851cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
18861cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  // FIXME: we might need to merge the number of positive or negative bits
18871cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  // if the enumerator lists don't match.
18881cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  To->completeDefinition(T, ToPromotionType,
18891cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor                         From->getNumPositiveBits(),
18901cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor                         From->getNumNegativeBits());
18911cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  return false;
18921cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor}
18931cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
1894040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorTemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1895040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                TemplateParameterList *Params) {
18965f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<NamedDecl *, 4> ToParams;
1897040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  ToParams.reserve(Params->size());
1898040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  for (TemplateParameterList::iterator P = Params->begin(),
1899040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                    PEnd = Params->end();
1900040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor       P != PEnd; ++P) {
1901040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *To = Importer.Import(*P);
1902040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!To)
1903040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return 0;
1904040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1905040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    ToParams.push_back(cast<NamedDecl>(To));
1906040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1907040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1908040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return TemplateParameterList::Create(Importer.getToContext(),
1909040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       Importer.Import(Params->getTemplateLoc()),
1910040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       Importer.Import(Params->getLAngleLoc()),
1911040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       ToParams.data(), ToParams.size(),
1912040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       Importer.Import(Params->getRAngleLoc()));
1913040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1914040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1915d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorTemplateArgument
1916d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1917d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  switch (From.getKind()) {
1918d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Null:
1919d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument();
1920d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1921d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Type: {
1922d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualType ToType = Importer.Import(From.getAsType());
1923d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToType.isNull())
1924d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1925d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(ToType);
1926d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1927d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1928d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Integral: {
1929d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualType ToType = Importer.Import(From.getIntegralType());
1930d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToType.isNull())
1931d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1932d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(*From.getAsIntegral(), ToType);
1933d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1934d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1935d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Declaration:
1936d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Decl *To = Importer.Import(From.getAsDecl()))
1937d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument(To);
1938d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument();
1939d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1940d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Template: {
1941d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1942d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToTemplate.isNull())
1943d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1944d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1945d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(ToTemplate);
1946d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1947a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
1948a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  case TemplateArgument::TemplateExpansion: {
1949a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    TemplateName ToTemplate
1950a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      = Importer.Import(From.getAsTemplateOrTemplatePattern());
1951a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    if (ToTemplate.isNull())
1952a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      return TemplateArgument();
1953a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
19542be29f423acad3bbe39099a78db2805acb5bdf17Douglas Gregor    return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
1955a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  }
1956a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
1957d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Expression:
1958d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1959d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument(ToExpr);
1960d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument();
1961d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1962d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Pack: {
19635f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<TemplateArgument, 2> ToPack;
1964d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ToPack.reserve(From.pack_size());
1965d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1966d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1967d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1968d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    TemplateArgument *ToArgs
1969d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1970d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1971d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(ToArgs, ToPack.size());
1972d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1973d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1974d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1975d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm_unreachable("Invalid template argument kind");
1976d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return TemplateArgument();
1977d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1978d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1979d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregorbool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1980d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                              unsigned NumFromArgs,
19815f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                              SmallVectorImpl<TemplateArgument> &ToArgs) {
1982d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  for (unsigned I = 0; I != NumFromArgs; ++I) {
1983d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1984d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (To.isNull() && !FromArgs[I].isNull())
1985d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return true;
1986d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1987d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ToArgs.push_back(To);
1988d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1989d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1990d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return false;
1991d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1992d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
199396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregorbool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
199473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        RecordDecl *ToRecord) {
1995bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
199673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Importer.getToContext(),
1997ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getNonEquivalentDecls());
1998bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
199996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
200096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
200136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregorbool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
2002bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
200373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Importer.getToContext(),
2004ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getNonEquivalentDecls());
2005bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
200636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
200736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
2008040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorbool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2009040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                        ClassTemplateDecl *To) {
2010040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2011040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                   Importer.getToContext(),
2012040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                   Importer.getNonEquivalentDecls());
2013040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return Ctx.IsStructurallyEquivalent(From, To);
2014040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
2015040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
2016a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitDecl(Decl *D) {
2017a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2018a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    << D->getDeclKindName();
2019a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  return 0;
2020a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
2021a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2022788c62d1e87bfb596078817237f672a5f000999aDouglas GregorDecl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2023788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  // Import the major distinguishing characteristics of this namespace.
2024788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  DeclContext *DC, *LexicalDC;
2025788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  DeclarationName Name;
2026788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  SourceLocation Loc;
2027788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2028788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    return 0;
2029788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
2030788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  NamespaceDecl *MergeWithNamespace = 0;
2031788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (!Name) {
2032788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // This is an anonymous namespace. Adopt an existing anonymous
2033788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // namespace if we can.
2034788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // FIXME: Not testable.
2035788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2036788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      MergeWithNamespace = TU->getAnonymousNamespace();
2037788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    else
2038788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2039788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  } else {
20405f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<NamedDecl *, 4> ConflictingDecls;
2041788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2042788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor         Lookup.first != Lookup.second;
2043788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor         ++Lookup.first) {
20440d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
2045788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        continue;
2046788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
2047788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
2048788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        MergeWithNamespace = FoundNS;
2049788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        ConflictingDecls.clear();
2050788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        break;
2051788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      }
2052788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
2053788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
2054788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
2055788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
2056788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (!ConflictingDecls.empty()) {
20570d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
2058788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                         ConflictingDecls.data(),
2059788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                         ConflictingDecls.size());
2060788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
2061788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  }
2062788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
2063788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  // Create the "to" namespace, if needed.
2064788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  NamespaceDecl *ToNamespace = MergeWithNamespace;
2065788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (!ToNamespace) {
2066acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara    ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
2067acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara                                        Importer.Import(D->getLocStart()),
2068acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara                                        Loc, Name.getAsIdentifierInfo());
2069788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    ToNamespace->setLexicalDeclContext(LexicalDC);
2070788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    LexicalDC->addDecl(ToNamespace);
2071788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
2072788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // If this is an anonymous namespace, register it as the anonymous
2073788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // namespace within its context.
2074788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (!Name) {
2075788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2076788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        TU->setAnonymousNamespace(ToNamespace);
2077788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      else
2078788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2079788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
2080788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  }
2081788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  Importer.Imported(D, ToNamespace);
2082788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
2083788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  ImportDeclContext(D);
2084788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
2085788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  return ToNamespace;
2086788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor}
2087788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
2088162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
20899e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // Import the major distinguishing characteristics of this typedef.
20909e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  DeclContext *DC, *LexicalDC;
20919e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  DeclarationName Name;
20929e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  SourceLocation Loc;
20939e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
20949e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    return 0;
20959e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
20969e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // If this typedef is not in block scope, determine whether we've
20979e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // seen a typedef with the same name (that we can merge with) or any
20989e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // other entity by that name (which name lookup could conflict with).
20999e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  if (!DC->isFunctionOrMethod()) {
21005f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<NamedDecl *, 4> ConflictingDecls;
21019e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
21029e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
21039e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor         Lookup.first != Lookup.second;
21049e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor         ++Lookup.first) {
21059e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
21069e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor        continue;
2107162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (TypedefNameDecl *FoundTypedef =
2108162e1c1b487352434552147967c3dd296ebee2f7Richard Smith            dyn_cast<TypedefNameDecl>(*Lookup.first)) {
2109ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2110ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                            FoundTypedef->getUnderlyingType()))
21115ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor          return Importer.Imported(D, FoundTypedef);
21129e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      }
21139e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
21149e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
21159e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    }
21169e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
21179e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    if (!ConflictingDecls.empty()) {
21189e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
21199e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                         ConflictingDecls.data(),
21209e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                         ConflictingDecls.size());
21219e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (!Name)
21229e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor        return 0;
21239e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    }
21249e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  }
21259e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
2126ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the underlying type of this typedef;
2127ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getUnderlyingType());
2128ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2129ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2130ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
21319e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // Create the new typedef node.
21329e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2133344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara  SourceLocation StartL = Importer.Import(D->getLocStart());
2134162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  TypedefNameDecl *ToTypedef;
2135162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (IsAlias)
2136162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2137162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                    StartL, Loc,
2138162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                    Name.getAsIdentifierInfo(),
2139162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                    TInfo);
2140162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  else
2141162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2142162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                  StartL, Loc,
2143162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                  Name.getAsIdentifierInfo(),
2144162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                  TInfo);
2145325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToTypedef->setAccess(D->getAccess());
21469e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  ToTypedef->setLexicalDeclContext(LexicalDC);
21475ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToTypedef);
21489e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  LexicalDC->addDecl(ToTypedef);
2149ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
21509e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  return ToTypedef;
21519e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor}
21529e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
2153162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2154162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2155162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
2156162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
2157162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2158162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2159162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
2160162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
216136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorDecl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
216236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the major distinguishing characteristics of this enum.
216336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclContext *DC, *LexicalDC;
216436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName Name;
216536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  SourceLocation Loc;
216636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
216736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
216836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
216936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Figure out what enum name we're looking for.
217036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  unsigned IDNS = Decl::IDNS_Tag;
217136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName SearchName = Name;
2172162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (!SearchName && D->getTypedefNameForAnonDecl()) {
2173162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
217436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    IDNS = Decl::IDNS_Ordinary;
217536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  } else if (Importer.getToContext().getLangOptions().CPlusPlus)
217636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    IDNS |= Decl::IDNS_Ordinary;
217736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
217836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // We may already have an enum of the same name; try to find and match it.
217936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!DC->isFunctionOrMethod() && SearchName) {
21805f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<NamedDecl *, 4> ConflictingDecls;
21813340e27fe15c52fe28fc722a98fd55d796c90442Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(SearchName);
218236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         Lookup.first != Lookup.second;
218336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++Lookup.first) {
218436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
218536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        continue;
218636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
218736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Decl *Found = *Lookup.first;
2188162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
218936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
219036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor          Found = Tag->getDecl();
219136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      }
219236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
219336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
21945ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor        if (IsStructuralMatch(D, FoundEnum))
21955ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor          return Importer.Imported(D, FoundEnum);
219636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      }
219736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
219836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
219936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
220036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
220136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (!ConflictingDecls.empty()) {
220236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
220336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.data(),
220436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.size());
220536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
220636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
220736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
220836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Create the enum declaration.
2209ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2210ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                  Importer.Import(D->getLocStart()),
2211ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                  Loc, Name.getAsIdentifierInfo(), 0,
2212a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                  D->isScoped(), D->isScopedUsingClassTag(),
2213a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                  D->isFixed());
2214b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
2215c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2216325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  D2->setAccess(D->getAccess());
221773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  D2->setLexicalDeclContext(LexicalDC);
221873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Importer.Imported(D, D2);
221973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  LexicalDC->addDecl(D2);
222036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
222136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the integer type.
222236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  QualType ToIntegerType = Importer.Import(D->getIntegerType());
222336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (ToIntegerType.isNull())
222436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
222573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  D2->setIntegerType(ToIntegerType);
222636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
222736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the definition
22281cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (D->isDefinition() && ImportDefinition(D, D2))
22291cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    return 0;
223036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
223173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return D2;
223236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
223336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
223496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas GregorDecl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
223596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // If this record has a definition in the translation unit we're coming from,
223696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // but this particular declaration is not that definition, import the
223796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // definition and map to that.
2238952b017601f9c82b51119c3a1600f1312a833db9Douglas Gregor  TagDecl *Definition = D->getDefinition();
223996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (Definition && Definition != D) {
224096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *ImportedDef = Importer.Import(Definition);
22415ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor    if (!ImportedDef)
22425ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor      return 0;
22435ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
22445ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor    return Importer.Imported(D, ImportedDef);
224596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
224696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
224796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Import the major distinguishing characteristics of this record.
224896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclContext *DC, *LexicalDC;
224996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName Name;
225096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  SourceLocation Loc;
225196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
225296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
225396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
225496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Figure out what structure name we're looking for.
225596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  unsigned IDNS = Decl::IDNS_Tag;
225696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName SearchName = Name;
2257162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (!SearchName && D->getTypedefNameForAnonDecl()) {
2258162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
225996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    IDNS = Decl::IDNS_Ordinary;
226096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  } else if (Importer.getToContext().getLangOptions().CPlusPlus)
226196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    IDNS |= Decl::IDNS_Ordinary;
226296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
226396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // We may already have a record of the same name; try to find and match it.
2264e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor  RecordDecl *AdoptDecl = 0;
226596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!DC->isFunctionOrMethod() && SearchName) {
22665f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<NamedDecl *, 4> ConflictingDecls;
22673340e27fe15c52fe28fc722a98fd55d796c90442Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(SearchName);
226896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         Lookup.first != Lookup.second;
226996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         ++Lookup.first) {
227096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
227196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor        continue;
227296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
227396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Decl *Found = *Lookup.first;
2274162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
227596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor        if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
227696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor          Found = Tag->getDecl();
227796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
227896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
227996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
2280e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2281e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2282e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // The record types structurally match, or the "from" translation
2283e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // unit only had a forward declaration anyway; call it the same
2284e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // function.
2285e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // FIXME: For C++, we should also merge methods here.
22865ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor            return Importer.Imported(D, FoundDef);
2287e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          }
2288e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        } else {
2289e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          // We have a forward declaration of this type, so adopt that forward
2290e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          // declaration rather than building a new one.
2291e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          AdoptDecl = FoundRecord;
2292e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          continue;
2293e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        }
229496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
229596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
229696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
229796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
229896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
229996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    if (!ConflictingDecls.empty()) {
230096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
230196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         ConflictingDecls.data(),
230296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         ConflictingDecls.size());
230396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
230496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
230596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
230696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Create the record declaration.
230773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  RecordDecl *D2 = AdoptDecl;
2308ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation StartLoc = Importer.Import(D->getLocStart());
230973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!D2) {
23105250f27420386452a21692a6292c99ee7febdac4John McCall    if (isa<CXXRecordDecl>(D)) {
231173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2312e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   D->getTagKind(),
2313ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                   DC, StartLoc, Loc,
2314ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                   Name.getAsIdentifierInfo());
231573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      D2 = D2CXX;
2316325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor      D2->setAccess(D->getAccess());
2317e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor    } else {
231873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
2319ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                              DC, StartLoc, Loc, Name.getAsIdentifierInfo());
232096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
2321c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor
2322c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
232373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->setLexicalDeclContext(LexicalDC);
232473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    LexicalDC->addDecl(D2);
232596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
23265ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
232773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Importer.Imported(D, D2);
2328e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor
2329d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D->isDefinition() && ImportDefinition(D, D2))
2330d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
233196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
233273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return D2;
233396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
233496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
233536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorDecl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
233636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the major distinguishing characteristics of this enumerator.
233736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclContext *DC, *LexicalDC;
233836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName Name;
233936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  SourceLocation Loc;
2340ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
234136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
2342ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2343ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2344ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2345ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2346ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
234736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Determine whether there are any other declarations with the same name and
234836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // in the same context.
234936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!LexicalDC->isFunctionOrMethod()) {
23505f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<NamedDecl *, 4> ConflictingDecls;
235136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
235236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
235336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         Lookup.first != Lookup.second;
235436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++Lookup.first) {
235536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
235636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        continue;
235736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
235836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
235936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
236036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
236136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (!ConflictingDecls.empty()) {
236236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
236336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.data(),
236436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.size());
236536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!Name)
236636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        return 0;
236736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
236836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
236936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
237036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  Expr *Init = Importer.Import(D->getInitExpr());
237136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (D->getInitExpr() && !Init)
237236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
237336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
237436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  EnumConstantDecl *ToEnumerator
237536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
237636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                               Name.getAsIdentifierInfo(), T,
237736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                               Init, D->getInitVal());
2378325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToEnumerator->setAccess(D->getAccess());
237936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  ToEnumerator->setLexicalDeclContext(LexicalDC);
23805ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToEnumerator);
238136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  LexicalDC->addDecl(ToEnumerator);
238236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  return ToEnumerator;
238336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
238496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
2385a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2386a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the major distinguishing characteristics of this function.
2387a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC, *LexicalDC;
2388a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclarationName Name;
2389a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc;
2390ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2391089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    return 0;
23922577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
2393a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Try to find a function in our own ("to") context with the same name, same
2394a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // type, and in the same context as the function we're importing.
2395a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  if (!LexicalDC->isFunctionOrMethod()) {
23965f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<NamedDecl *, 4> ConflictingDecls;
2397a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
2398a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2399a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor         Lookup.first != Lookup.second;
2400a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor         ++Lookup.first) {
2401a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2402a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        continue;
2403a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2404a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2405a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        if (isExternalLinkage(FoundFunction->getLinkage()) &&
2406a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            isExternalLinkage(D->getLinkage())) {
2407ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          if (Importer.IsStructurallyEquivalent(D->getType(),
2408ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                FoundFunction->getType())) {
2409a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            // FIXME: Actually try to merge the body and other attributes.
24105ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor            return Importer.Imported(D, FoundFunction);
2411a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          }
2412a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2413a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // FIXME: Check for overloading more carefully, e.g., by boosting
2414a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Sema::IsOverload out to the AST library.
2415a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2416a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Function overloading is okay in C++.
2417a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          if (Importer.getToContext().getLangOptions().CPlusPlus)
2418a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            continue;
2419a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2420a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Complain about inconsistent function types.
2421a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
2422ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            << Name << D->getType() << FoundFunction->getType();
2423a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          Importer.ToDiag(FoundFunction->getLocation(),
2424a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                          diag::note_odr_value_here)
2425a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            << FoundFunction->getType();
2426a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        }
2427a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      }
2428a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2429a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
2430a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    }
2431a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2432a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    if (!ConflictingDecls.empty()) {
2433a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
2434a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                         ConflictingDecls.data(),
2435a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                         ConflictingDecls.size());
2436a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (!Name)
2437a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        return 0;
2438a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    }
2439a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
2440ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
24412577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo(Name, Loc);
24422577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  // Import additional name location/type info.
24432577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
24442577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
2445ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
2446ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2447ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2448ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2449a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2450a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the function parameters.
24515f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 8> Parameters;
2452a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2453a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor       P != PEnd; ++P) {
2454a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2455a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    if (!ToP)
2456a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      return 0;
2457a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2458a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Parameters.push_back(ToP);
2459a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
2460a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2461a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Create the imported function.
2462a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2463c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  FunctionDecl *ToFunction = 0;
2464c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2465c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2466c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            cast<CXXRecordDecl>(DC),
2467ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
24682577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                            NameInfo, T, TInfo,
2469c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            FromConstructor->isExplicit(),
2470c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            D->isInlineSpecified(),
2471af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                            D->isImplicit(),
2472af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                            D->isConstexpr());
2473c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else if (isa<CXXDestructorDecl>(D)) {
2474c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2475c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           cast<CXXRecordDecl>(DC),
2476ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           D->getInnerLocStart(),
2477b41d899a6023385c00a61eb9dd3e44db9dc7994eCraig Silverstein                                           NameInfo, T, TInfo,
2478c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isInlineSpecified(),
2479c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isImplicit());
2480c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else if (CXXConversionDecl *FromConversion
2481c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           = dyn_cast<CXXConversionDecl>(D)) {
2482c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2483c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           cast<CXXRecordDecl>(DC),
2484ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           D->getInnerLocStart(),
24852577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                           NameInfo, T, TInfo,
2486c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isInlineSpecified(),
2487f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                           FromConversion->isExplicit(),
2488af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                           D->isConstexpr(),
2489f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                           Importer.Import(D->getLocEnd()));
24900629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor  } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
24910629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor    ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
24920629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       cast<CXXRecordDecl>(DC),
2493ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       D->getInnerLocStart(),
24940629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       NameInfo, T, TInfo,
24950629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       Method->isStatic(),
24960629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       Method->getStorageClassAsWritten(),
2497f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                       Method->isInlineSpecified(),
2498af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                       D->isConstexpr(),
2499f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                       Importer.Import(D->getLocEnd()));
2500c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else {
25012577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2502ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                      D->getInnerLocStart(),
25032577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                      NameInfo, T, TInfo, D->getStorageClass(),
250416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                      D->getStorageClassAsWritten(),
2505c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                      D->isInlineSpecified(),
2506af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                      D->hasWrittenPrototype(),
2507af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                      D->isConstexpr());
2508c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  }
2509b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2510b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
2511c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2512325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToFunction->setAccess(D->getAccess());
2513c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToFunction->setLexicalDeclContext(LexicalDC);
2514f2eca2cf302c50b79891f24b3861d64ea9263831John McCall  ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2515f2eca2cf302c50b79891f24b3861d64ea9263831John McCall  ToFunction->setTrivial(D->isTrivial());
2516f2eca2cf302c50b79891f24b3861d64ea9263831John McCall  ToFunction->setPure(D->isPure());
2517c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Importer.Imported(D, ToFunction);
2518a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2519a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Set the parameters.
2520a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
2521c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Parameters[I]->setOwningFunction(ToFunction);
2522c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToFunction->addDecl(Parameters[I]);
2523a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
25244278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  ToFunction->setParams(Parameters);
2525a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2526a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // FIXME: Other bits to merge?
252781134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor
252881134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor  // Add this function to the lexical context.
252981134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor  LexicalDC->addDecl(ToFunction);
253081134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor
2531c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToFunction;
2532a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
2533a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2534c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2535c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitFunctionDecl(D);
2536c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2537c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
2538c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2539c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
2540c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2541c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
2542c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2543c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
2544c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2545c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
2546c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2547c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
2548c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2549c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
255096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas GregorDecl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
255196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Import the major distinguishing characteristics of a variable.
255296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclContext *DC, *LexicalDC;
255396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName Name;
255496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  SourceLocation Loc;
2555ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2556ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2557ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2558ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
2559ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2560ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
256196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
256296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
256396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
256496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  Expr *BitWidth = Importer.Import(D->getBitWidth());
256596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!BitWidth && D->getBitWidth())
256696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
256796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
2568ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2569ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                         Importer.Import(D->getInnerLocStart()),
257096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         Loc, Name.getAsIdentifierInfo(),
25717a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         T, TInfo, BitWidth, D->isMutable(),
25727a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         D->hasInClassInitializer());
2573325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToField->setAccess(D->getAccess());
257496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  ToField->setLexicalDeclContext(LexicalDC);
25757a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (ToField->hasInClassInitializer())
25767a614d8380297fcd2bc23986241905d97222948cRichard Smith    ToField->setInClassInitializer(D->getInClassInitializer());
25775ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToField);
257896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  LexicalDC->addDecl(ToField);
257996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  return ToField;
258096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
258196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
258287c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetDecl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
258387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  // Import the major distinguishing characteristics of a variable.
258487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  DeclContext *DC, *LexicalDC;
258587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  DeclarationName Name;
258687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  SourceLocation Loc;
258787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
258887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    return 0;
258987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
259087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  // Import the type.
259187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  QualType T = Importer.Import(D->getType());
259287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (T.isNull())
259387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    return 0;
259487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
259587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  NamedDecl **NamedChain =
259687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
259787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
259887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  unsigned i = 0;
259987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
260087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       PE = D->chain_end(); PI != PE; ++PI) {
260187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    Decl* D = Importer.Import(*PI);
260287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    if (!D)
260387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet      return 0;
260487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    NamedChain[i++] = cast<NamedDecl>(D);
260587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  }
260687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
260787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
260887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                         Importer.getToContext(), DC,
260987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                         Loc, Name.getAsIdentifierInfo(), T,
261087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                         NamedChain, D->getChainingSize());
261187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  ToIndirectField->setAccess(D->getAccess());
261287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  ToIndirectField->setLexicalDeclContext(LexicalDC);
261387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  Importer.Imported(D, ToIndirectField);
261487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  LexicalDC->addDecl(ToIndirectField);
261587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return ToIndirectField;
261687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
261787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
26182e55e3af2f6d6c0509495357fade95105dd144cdDouglas GregorDecl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
26192e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Import the major distinguishing characteristics of an ivar.
26202e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  DeclContext *DC, *LexicalDC;
26212e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  DeclarationName Name;
26222e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  SourceLocation Loc;
26232e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
26242e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
26252e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
26262e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Determine whether we've already imported this ivar
26272e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
26282e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor       Lookup.first != Lookup.second;
26292e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor       ++Lookup.first) {
26302e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
26312e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (Importer.IsStructurallyEquivalent(D->getType(),
26322e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                            FoundIvar->getType())) {
26332e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.Imported(D, FoundIvar);
26342e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        return FoundIvar;
26352e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      }
26362e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
26372e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
26382e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << Name << D->getType() << FoundIvar->getType();
26392e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
26402e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << FoundIvar->getType();
26412e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      return 0;
26422e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    }
26432e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  }
26442e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
26452e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Import the type.
26462e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  QualType T = Importer.Import(D->getType());
26472e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (T.isNull())
26482e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
26492e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
26502e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
26512e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  Expr *BitWidth = Importer.Import(D->getBitWidth());
26522e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (!BitWidth && D->getBitWidth())
26532e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
26542e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
2655a06549226f45d5b72169a3d054415616dd1014a2Daniel Dunbar  ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2656a06549226f45d5b72169a3d054415616dd1014a2Daniel Dunbar                                              cast<ObjCContainerDecl>(DC),
2657ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       Importer.Import(D->getInnerLocStart()),
26582e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              Loc, Name.getAsIdentifierInfo(),
26592e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              T, TInfo, D->getAccessControl(),
2660ac0021ba802e193e0f9f8207768c7862c7603bc0Fariborz Jahanian                                              BitWidth, D->getSynthesize());
26612e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  ToIvar->setLexicalDeclContext(LexicalDC);
26622e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  Importer.Imported(D, ToIvar);
26632e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  LexicalDC->addDecl(ToIvar);
26642e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  return ToIvar;
26652e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
26662e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor}
26672e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
2668a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2669a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the major distinguishing characteristics of a variable.
2670a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC, *LexicalDC;
2671a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclarationName Name;
2672a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc;
2673ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2674a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return 0;
2675089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2676089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Try to find a variable in our own ("to") context with the same name and
2677089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // in the same context as the variable we're importing.
26789bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (D->isFileVarDecl()) {
2679089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    VarDecl *MergeWithVar = 0;
26805f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<NamedDecl *, 4> ConflictingDecls;
2681089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
26829bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2683089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor         Lookup.first != Lookup.second;
2684089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor         ++Lookup.first) {
2685089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2686089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        continue;
2687089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2688089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2689089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        // We have found a variable that we may need to merge with. Check it.
2690089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        if (isExternalLinkage(FoundVar->getLinkage()) &&
2691089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            isExternalLinkage(D->getLinkage())) {
2692ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          if (Importer.IsStructurallyEquivalent(D->getType(),
2693ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                FoundVar->getType())) {
2694089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            MergeWithVar = FoundVar;
2695089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            break;
2696089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          }
2697089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2698d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          const ArrayType *FoundArray
2699d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            = Importer.getToContext().getAsArrayType(FoundVar->getType());
2700d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          const ArrayType *TArray
2701ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            = Importer.getToContext().getAsArrayType(D->getType());
2702d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          if (FoundArray && TArray) {
2703d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            if (isa<IncompleteArrayType>(FoundArray) &&
2704d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor                isa<ConstantArrayType>(TArray)) {
2705ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              // Import the type.
2706ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              QualType T = Importer.Import(D->getType());
2707ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              if (T.isNull())
2708ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                return 0;
2709ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2710d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              FoundVar->setType(T);
2711d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              MergeWithVar = FoundVar;
2712d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              break;
2713d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            } else if (isa<IncompleteArrayType>(TArray) &&
2714d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor                       isa<ConstantArrayType>(FoundArray)) {
2715d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              MergeWithVar = FoundVar;
2716d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              break;
27170f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor            }
27180f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor          }
27190f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor
2720089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
2721ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            << Name << D->getType() << FoundVar->getType();
2722089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2723089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            << FoundVar->getType();
2724089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        }
2725089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      }
2726089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2727089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
2728089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2729089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2730089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    if (MergeWithVar) {
2731089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      // An equivalent variable with external linkage has been found. Link
2732089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      // the two declarations, then merge them.
27335ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor      Importer.Imported(D, MergeWithVar);
2734089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2735089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (VarDecl *DDef = D->getDefinition()) {
2736089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2737089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(ExistingDef->getLocation(),
2738089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                          diag::err_odr_variable_multiple_def)
2739089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            << Name;
2740089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2741089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        } else {
2742089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Expr *Init = Importer.Import(DDef->getInit());
2743838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor          MergeWithVar->setInit(Init);
2744089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        }
2745089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      }
2746089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2747089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      return MergeWithVar;
2748089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2749089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2750089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    if (!ConflictingDecls.empty()) {
2751089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
2752089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                         ConflictingDecls.data(),
2753089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                         ConflictingDecls.size());
2754089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (!Name)
2755089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        return 0;
2756089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2757089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  }
275882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2759ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
2760ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2761ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2762ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2763ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2764089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Create the imported variable.
276582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2766ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2767ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   Importer.Import(D->getInnerLocStart()),
2768ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   Loc, Name.getAsIdentifierInfo(),
2769ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   T, TInfo,
277016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClass(),
277116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten());
2772c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2773325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToVar->setAccess(D->getAccess());
27749bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ToVar->setLexicalDeclContext(LexicalDC);
27755ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToVar);
27769bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  LexicalDC->addDecl(ToVar);
27779bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
2778089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Merge the initializer.
2779089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // FIXME: Can we really import any initializer? Alternatively, we could force
2780089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // ourselves to import every declaration of a variable and then only use
2781089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // getInit() here.
2782838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
2783089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2784089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // FIXME: Other bits to merge?
2785089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2786089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  return ToVar;
2787089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
2788089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
27892cd00932b9b5403047139ce8cfaa3ae47966f894Douglas GregorDecl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
27902cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Parameters are created in the translation unit's context, then moved
27912cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // into the function declaration's context afterward.
27922cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
27932cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
27942cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the name of this declaration.
27952cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
27962cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  if (D->getDeclName() && !Name)
27972cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    return 0;
27982cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
27992cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the location of this declaration.
28002cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
28012cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
28022cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the parameter's type.
28032cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  QualType T = Importer.Import(D->getType());
28042cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  if (T.isNull())
28052cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    return 0;
28062cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
28072cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Create the imported parameter.
28082cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  ImplicitParamDecl *ToParm
28092cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    = ImplicitParamDecl::Create(Importer.getToContext(), DC,
28102cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor                                Loc, Name.getAsIdentifierInfo(),
28112cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor                                T);
28122cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  return Importer.Imported(D, ToParm);
28132cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor}
28142cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
2815a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2816a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Parameters are created in the translation unit's context, then moved
2817a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // into the function declaration's context afterward.
2818a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2819a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
282082fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // Import the name of this declaration.
282182fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
282282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (D->getDeclName() && !Name)
282382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
282482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2825a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the location of this declaration.
2826a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
2827a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2828a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the parameter's type.
2829a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  QualType T = Importer.Import(D->getType());
283082fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (T.isNull())
283182fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
283282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2833a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Create the imported parameter.
2834a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2835a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2836ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                     Importer.Import(D->getInnerLocStart()),
2837a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            Loc, Name.getAsIdentifierInfo(),
2838a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            T, TInfo, D->getStorageClass(),
283916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                             D->getStorageClassAsWritten(),
2840a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            /*FIXME: Default argument*/ 0);
2841bf73b352acb7a2d041ce8b50171dd7f8e2b2c1bbJohn McCall  ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
28425ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  return Importer.Imported(D, ToParm);
2843a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
284482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2845c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas GregorDecl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2846c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the major distinguishing characteristics of a method.
2847c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  DeclContext *DC, *LexicalDC;
2848c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  DeclarationName Name;
2849c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  SourceLocation Loc;
2850c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2851c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return 0;
2852c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2853c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2854c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       Lookup.first != Lookup.second;
2855c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       ++Lookup.first) {
2856c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2857c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2858c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        continue;
2859c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2860c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check return types.
2861c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2862c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                             FoundMethod->getResultType())) {
2863c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2864c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name
2865c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->getResultType() << FoundMethod->getResultType();
2866c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2867c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2868c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2869c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2870c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2871c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2872c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check the number of parameters.
2873c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (D->param_size() != FoundMethod->param_size()) {
2874c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2875c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name
2876c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->param_size() << FoundMethod->param_size();
2877c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2878c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2879c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2880c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2881c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2882c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2883c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check parameter types.
2884c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2885c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor             PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2886c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor           P != PEnd; ++P, ++FoundP) {
2887c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2888c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                               (*FoundP)->getType())) {
2889c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          Importer.FromDiag((*P)->getLocation(),
2890c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                            diag::err_odr_objc_method_param_type_inconsistent)
2891c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << D->isInstanceMethod() << Name
2892c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << (*P)->getType() << (*FoundP)->getType();
2893c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2894c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << (*FoundP)->getType();
2895c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          return 0;
2896c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        }
2897c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2898c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2899c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check variadic/non-variadic.
2900c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check the number of parameters.
2901c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (D->isVariadic() != FoundMethod->isVariadic()) {
2902c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2903c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2904c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2905c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2906c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2907c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2908c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2909c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2910c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // FIXME: Any other bits we need to merge?
2911c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      return Importer.Imported(D, FoundMethod);
2912c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    }
2913c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2914c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2915c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the result type.
2916c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  QualType ResultTy = Importer.Import(D->getResultType());
2917c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (ResultTy.isNull())
2918c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return 0;
2919c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
29204bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor  TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
29214bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor
2922c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ObjCMethodDecl *ToMethod
2923c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    = ObjCMethodDecl::Create(Importer.getToContext(),
2924c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Loc,
2925c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Importer.Import(D->getLocEnd()),
2926c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Name.getObjCSelector(),
29274bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor                             ResultTy, ResultTInfo, DC,
2928c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isInstanceMethod(),
2929c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isVariadic(),
2930c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isSynthesized(),
293175cf3e86d33ce810c12084126385371b335c30baArgyrios Kyrtzidis                             D->isImplicit(),
29323fe104154dd2e8ffb351142d74f308938b5c99bfFariborz Jahanian                             D->isDefined(),
2933926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                             D->getImplementationControl(),
2934926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                             D->hasRelatedResultType());
2935c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2936c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // FIXME: When we decide to merge method definitions, we'll need to
2937c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // deal with implicit parameters.
2938c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2939c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the parameters
29405f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 5> ToParams;
2941c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2942c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                   FromPEnd = D->param_end();
2943c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       FromP != FromPEnd;
2944c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       ++FromP) {
2945c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2946c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    if (!ToP)
2947c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      return 0;
2948c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2949c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToParams.push_back(ToP);
2950c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2951c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2952c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Set the parameters.
2953c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2954c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToParams[I]->setOwningFunction(ToMethod);
2955c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToMethod->addDecl(ToParams[I]);
2956c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2957c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToMethod->setMethodParams(Importer.getToContext(),
29584ecb25fa94897b2c03510292acace710e5262ba5Fariborz Jahanian                            ToParams.data(), ToParams.size(),
29594ecb25fa94897b2c03510292acace710e5262ba5Fariborz Jahanian                            ToParams.size());
2960c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2961c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToMethod->setLexicalDeclContext(LexicalDC);
2962c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Importer.Imported(D, ToMethod);
2963c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  LexicalDC->addDecl(ToMethod);
2964c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToMethod;
2965c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor}
2966c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2967b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas GregorDecl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2968b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import the major distinguishing characteristics of a category.
2969b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  DeclContext *DC, *LexicalDC;
2970b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  DeclarationName Name;
2971b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  SourceLocation Loc;
2972b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2973b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    return 0;
2974b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2975b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCInterfaceDecl *ToInterface
2976b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2977b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (!ToInterface)
2978b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    return 0;
2979b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2980b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Determine if we've already encountered this category.
2981b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCCategoryDecl *MergeWithCategory
2982b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2983b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCCategoryDecl *ToCategory = MergeWithCategory;
2984b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (!ToCategory) {
2985b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2986b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Importer.Import(D->getAtLoc()),
2987b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Loc,
2988b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                       Importer.Import(D->getCategoryNameLoc()),
2989955fadbdfecfa24a590febe66a86519096787f2dArgyrios Kyrtzidis                                          Name.getAsIdentifierInfo(),
2990955fadbdfecfa24a590febe66a86519096787f2dArgyrios Kyrtzidis                                          ToInterface);
2991b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setLexicalDeclContext(LexicalDC);
2992b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    LexicalDC->addDecl(ToCategory);
2993b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Imported(D, ToCategory);
2994b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2995b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // Import protocols
29965f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<ObjCProtocolDecl *, 4> Protocols;
29975f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<SourceLocation, 4> ProtocolLocs;
2998b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2999b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      = D->protocol_loc_begin();
3000b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3001b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          FromProtoEnd = D->protocol_end();
3002b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor         FromProto != FromProtoEnd;
3003b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor         ++FromProto, ++FromProtoLoc) {
3004b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      ObjCProtocolDecl *ToProto
3005b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3006b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      if (!ToProto)
3007b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor        return 0;
3008b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      Protocols.push_back(ToProto);
3009b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3010b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    }
3011b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
3012b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
3013b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3014b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                ProtocolLocs.data(), Importer.getToContext());
3015b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
3016b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  } else {
3017b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Imported(D, ToCategory);
3018b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  }
3019b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
3020b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import all of the members of this category.
3021083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
3022b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
3023b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // If we have an implementation, import it as well.
3024b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (D->getImplementation()) {
3025b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ObjCCategoryImplDecl *Impl
3026cad2c59b0c087edea83d0fbf6eabde4a7960c778Douglas Gregor      = cast_or_null<ObjCCategoryImplDecl>(
3027cad2c59b0c087edea83d0fbf6eabde4a7960c778Douglas Gregor                                       Importer.Import(D->getImplementation()));
3028b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    if (!Impl)
3029b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      return 0;
3030b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
3031b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setImplementation(Impl);
3032b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  }
3033b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
3034b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  return ToCategory;
3035b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor}
3036b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
30372e2a400383c7a64a927f61eeed596b08928e1d4bDouglas GregorDecl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
3038b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import the major distinguishing characteristics of a protocol.
30392e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  DeclContext *DC, *LexicalDC;
30402e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  DeclarationName Name;
30412e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  SourceLocation Loc;
30422e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
30432e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    return 0;
30442e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
30452e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  ObjCProtocolDecl *MergeWithProtocol = 0;
30462e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
30472e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       Lookup.first != Lookup.second;
30482e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++Lookup.first) {
30492e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
30502e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      continue;
30512e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
30522e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
30532e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      break;
30542e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  }
30552e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
30562e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  ObjCProtocolDecl *ToProto = MergeWithProtocol;
30572e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  if (!ToProto || ToProto->isForwardDecl()) {
30582e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if (!ToProto) {
30592e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
30602e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                                         Name.getAsIdentifierInfo());
30612e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto->setForwardDecl(D->isForwardDecl());
30622e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto->setLexicalDeclContext(LexicalDC);
30632e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      LexicalDC->addDecl(ToProto);
30642e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    }
30652e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Imported(D, ToProto);
30662e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
30672e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    // Import protocols
30685f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<ObjCProtocolDecl *, 4> Protocols;
30695f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<SourceLocation, 4> ProtocolLocs;
30702e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    ObjCProtocolDecl::protocol_loc_iterator
30712e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      FromProtoLoc = D->protocol_loc_begin();
30722e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
30732e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                                          FromProtoEnd = D->protocol_end();
30742e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       FromProto != FromProtoEnd;
30752e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++FromProto, ++FromProtoLoc) {
30762e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ObjCProtocolDecl *ToProto
30772e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
30782e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      if (!ToProto)
30792e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor        return 0;
30802e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      Protocols.push_back(ToProto);
30812e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
30822e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    }
30832e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
30842e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
30852e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    ToProto->setProtocolList(Protocols.data(), Protocols.size(),
30862e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                             ProtocolLocs.data(), Importer.getToContext());
30872e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  } else {
30882e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Imported(D, ToProto);
30892e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  }
30902e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
3091b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import all of the members of this protocol.
3092083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
30932e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
30942e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  return ToProto;
30952e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor}
30962e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
3097a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas GregorDecl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3098a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // Import the major distinguishing characteristics of an @interface.
3099a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  DeclContext *DC, *LexicalDC;
3100a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  DeclarationName Name;
3101a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  SourceLocation Loc;
3102a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3103a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    return 0;
3104a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3105a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  ObjCInterfaceDecl *MergeWithIface = 0;
3106a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3107a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       Lookup.first != Lookup.second;
3108a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++Lookup.first) {
3109a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3110a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      continue;
3111a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3112a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
3113a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      break;
3114a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
3115a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3116a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  ObjCInterfaceDecl *ToIface = MergeWithIface;
3117a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (!ToIface || ToIface->isForwardDecl()) {
3118a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!ToIface) {
3119a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
3120a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          DC, Loc,
3121a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          Name.getAsIdentifierInfo(),
3122deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor                                          Importer.Import(D->getClassLoc()),
3123a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          D->isForwardDecl(),
3124a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          D->isImplicitInterfaceDecl());
31252e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToIface->setForwardDecl(D->isForwardDecl());
3126a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setLexicalDeclContext(LexicalDC);
3127a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      LexicalDC->addDecl(ToIface);
3128a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
3129a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Imported(D, ToIface);
3130a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3131a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (D->getSuperClass()) {
3132a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ObjCInterfaceDecl *Super
3133a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3134a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      if (!Super)
3135a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        return 0;
3136a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3137a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setSuperClass(Super);
3138a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3139a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
3140a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3141a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // Import protocols
31425f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<ObjCProtocolDecl *, 4> Protocols;
31435f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<SourceLocation, 4> ProtocolLocs;
3144a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ObjCInterfaceDecl::protocol_loc_iterator
3145a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      FromProtoLoc = D->protocol_loc_begin();
314653b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek
314753b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // FIXME: Should we be usng all_referenced_protocol_begin() here?
3148a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3149a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                           FromProtoEnd = D->protocol_end();
3150a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       FromProto != FromProtoEnd;
3151a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++FromProto, ++FromProtoLoc) {
3152a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ObjCProtocolDecl *ToProto
3153a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3154a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      if (!ToProto)
3155a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        return 0;
3156a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      Protocols.push_back(ToProto);
3157a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3158a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
3159a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3160a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
3161a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3162a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                             ProtocolLocs.data(), Importer.getToContext());
3163a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3164a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // Import @end range
3165a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3166a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  } else {
3167a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Imported(D, ToIface);
31682e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
31692e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    // Check for consistency of superclasses.
31702e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    DeclarationName FromSuperName, ToSuperName;
31712e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (D->getSuperClass())
31722e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
31732e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (ToIface->getSuperClass())
31742e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      ToSuperName = ToIface->getSuperClass()->getDeclName();
31752e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (FromSuperName != ToSuperName) {
31762e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(ToIface->getLocation(),
31772e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                      diag::err_odr_objc_superclass_inconsistent)
31782e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << ToIface->getDeclName();
31792e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (ToIface->getSuperClass())
31802e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.ToDiag(ToIface->getSuperClassLoc(),
31812e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                        diag::note_odr_objc_superclass)
31822e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor          << ToIface->getSuperClass()->getDeclName();
31832e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      else
31842e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.ToDiag(ToIface->getLocation(),
31852e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                        diag::note_odr_objc_missing_superclass);
31862e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (D->getSuperClass())
31872e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.FromDiag(D->getSuperClassLoc(),
31882e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                          diag::note_odr_objc_superclass)
31892e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor          << D->getSuperClass()->getDeclName();
31902e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      else
31912e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.FromDiag(D->getLocation(),
31922e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                          diag::note_odr_objc_missing_superclass);
31932e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      return 0;
31942e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    }
3195a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
3196a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3197b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import categories. When the categories themselves are imported, they'll
3198b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // hook themselves into this interface.
3199b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3200b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor       FromCat = FromCat->getNextClassCategory())
3201b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Import(FromCat);
3202b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
3203a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // Import all of the members of this class.
3204083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
3205a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3206a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // If we have an @implementation, import it as well.
3207a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (D->getImplementation()) {
3208dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3209dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                       Importer.Import(D->getImplementation()));
3210a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!Impl)
3211a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      return 0;
3212a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3213a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setImplementation(Impl);
3214a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
3215a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
32162e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  return ToIface;
3217a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor}
3218a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
32193daef29bf390dbdb3603748280afd5827d1811daDouglas GregorDecl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
32203daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
32213daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor                                        Importer.Import(D->getCategoryDecl()));
32223daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  if (!Category)
32233daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    return 0;
32243daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
32253daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
32263daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  if (!ToImpl) {
32273daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    DeclContext *DC = Importer.ImportContext(D->getDeclContext());
32283daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    if (!DC)
32293daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor      return 0;
32303daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
32313daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
32323daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor                                          Importer.Import(D->getLocation()),
32333daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor                                          Importer.Import(D->getIdentifier()),
32343daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor                                          Category->getClassInterface());
32353daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
32363daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    DeclContext *LexicalDC = DC;
32373daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    if (D->getDeclContext() != D->getLexicalDeclContext()) {
32383daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor      LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
32393daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor      if (!LexicalDC)
32403daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor        return 0;
32413daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
32423daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor      ToImpl->setLexicalDeclContext(LexicalDC);
32433daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    }
32443daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
32453daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    LexicalDC->addDecl(ToImpl);
32463daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    Category->setImplementation(ToImpl);
32473daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  }
32483daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
32493daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  Importer.Imported(D, ToImpl);
3250cad2c59b0c087edea83d0fbf6eabde4a7960c778Douglas Gregor  ImportDeclContext(D);
32513daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  return ToImpl;
32523daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor}
32533daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
3254dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas GregorDecl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3255dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  // Find the corresponding interface.
3256dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3257dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                       Importer.Import(D->getClassInterface()));
3258dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  if (!Iface)
3259dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    return 0;
3260dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3261dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  // Import the superclass, if any.
3262dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ObjCInterfaceDecl *Super = 0;
3263dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  if (D->getSuperClass()) {
3264dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Super = cast_or_null<ObjCInterfaceDecl>(
3265dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                          Importer.Import(D->getSuperClass()));
3266dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    if (!Super)
3267dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      return 0;
3268dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  }
3269dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3270dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ObjCImplementationDecl *Impl = Iface->getImplementation();
3271dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  if (!Impl) {
3272dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // We haven't imported an implementation yet. Create a new @implementation
3273dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // now.
3274dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3275dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                  Importer.ImportContext(D->getDeclContext()),
3276dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                          Importer.Import(D->getLocation()),
3277dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                          Iface, Super);
3278dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3279dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    if (D->getDeclContext() != D->getLexicalDeclContext()) {
3280dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      DeclContext *LexicalDC
3281dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        = Importer.ImportContext(D->getLexicalDeclContext());
3282dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      if (!LexicalDC)
3283dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        return 0;
3284dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      Impl->setLexicalDeclContext(LexicalDC);
3285dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    }
3286dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3287dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // Associate the implementation with the class it implements.
3288dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Iface->setImplementation(Impl);
3289dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Importer.Imported(D, Iface->getImplementation());
3290dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  } else {
3291dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Importer.Imported(D, Iface->getImplementation());
3292dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3293dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // Verify that the existing @implementation has the same superclass.
3294dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    if ((Super && !Impl->getSuperClass()) ||
3295dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        (!Super && Impl->getSuperClass()) ||
3296dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        (Super && Impl->getSuperClass() &&
3297dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor         Super->getCanonicalDecl() != Impl->getSuperClass())) {
3298dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        Importer.ToDiag(Impl->getLocation(),
3299dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                        diag::err_odr_objc_superclass_inconsistent)
3300dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          << Iface->getDeclName();
3301dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        // FIXME: It would be nice to have the location of the superclass
3302dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        // below.
3303dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        if (Impl->getSuperClass())
3304dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.ToDiag(Impl->getLocation(),
3305dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                          diag::note_odr_objc_superclass)
3306dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          << Impl->getSuperClass()->getDeclName();
3307dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        else
3308dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.ToDiag(Impl->getLocation(),
3309dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                          diag::note_odr_objc_missing_superclass);
3310dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        if (D->getSuperClass())
3311dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.FromDiag(D->getLocation(),
3312dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                            diag::note_odr_objc_superclass)
3313dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          << D->getSuperClass()->getDeclName();
3314dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        else
3315dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.FromDiag(D->getLocation(),
3316dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                            diag::note_odr_objc_missing_superclass);
3317dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      return 0;
3318dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    }
3319dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  }
3320dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3321dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  // Import all of the members of this @implementation.
3322dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ImportDeclContext(D);
3323dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3324dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  return Impl;
3325dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor}
3326dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3327e3261624c1870e52d7efc2ac83e647713361ac6cDouglas GregorDecl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3328e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Import the major distinguishing characteristics of an @property.
3329e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  DeclContext *DC, *LexicalDC;
3330e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  DeclarationName Name;
3331e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  SourceLocation Loc;
3332e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3333e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    return 0;
3334e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3335e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Check whether we have already imported this property.
3336e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3337e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       Lookup.first != Lookup.second;
3338e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       ++Lookup.first) {
3339e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    if (ObjCPropertyDecl *FoundProp
3340e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                                = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3341e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // Check property types.
3342e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      if (!Importer.IsStructurallyEquivalent(D->getType(),
3343e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                                             FoundProp->getType())) {
3344e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3345e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor          << Name << D->getType() << FoundProp->getType();
3346e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3347e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor          << FoundProp->getType();
3348e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        return 0;
3349e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      }
3350e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3351e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // FIXME: Check property attributes, getters, setters, etc.?
3352e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3353e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // Consider these properties to be equivalent.
3354e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      Importer.Imported(D, FoundProp);
3355e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      return FoundProp;
3356e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    }
3357e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  }
3358e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3359e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Import the type.
336083a230c83a54190366138c1a4f4310ef838b88fcJohn McCall  TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
336183a230c83a54190366138c1a4f4310ef838b88fcJohn McCall  if (!T)
3362e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    return 0;
3363e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3364e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Create the new property.
3365e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ObjCPropertyDecl *ToProperty
3366e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3367e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               Name.getAsIdentifierInfo(),
3368e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               Importer.Import(D->getAtLoc()),
3369e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               T,
3370e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               D->getPropertyImplementation());
3371e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  Importer.Imported(D, ToProperty);
3372e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setLexicalDeclContext(LexicalDC);
3373e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  LexicalDC->addDecl(ToProperty);
3374e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3375e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setPropertyAttributes(D->getPropertyAttributes());
337680aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian  ToProperty->setPropertyAttributesAsWritten(
337780aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian                                      D->getPropertyAttributesAsWritten());
3378e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3379e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3380e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setGetterMethodDecl(
3381e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3382e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setSetterMethodDecl(
3383e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3384e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setPropertyIvarDecl(
3385e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3386e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  return ToProperty;
3387e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor}
3388e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3389954e0c75c42f321945aff8b9ee96da43cd90c752Douglas GregorDecl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3390954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3391954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                        Importer.Import(D->getPropertyDecl()));
3392954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (!Property)
3393954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    return 0;
3394954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3395954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3396954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (!DC)
3397954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    return 0;
3398954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3399954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  // Import the lexical declaration context.
3400954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  DeclContext *LexicalDC = DC;
3401954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
3402954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3403954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    if (!LexicalDC)
3404954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      return 0;
3405954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  }
3406954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3407954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3408954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (!InImpl)
3409954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    return 0;
3410954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3411954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  // Import the ivar (for an @synthesize).
3412954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  ObjCIvarDecl *Ivar = 0;
3413954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (D->getPropertyIvarDecl()) {
3414954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    Ivar = cast_or_null<ObjCIvarDecl>(
3415954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                    Importer.Import(D->getPropertyIvarDecl()));
3416954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    if (!Ivar)
3417954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      return 0;
3418954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  }
3419954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3420954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  ObjCPropertyImplDecl *ToImpl
3421954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3422954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (!ToImpl) {
3423954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3424954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          Importer.Import(D->getLocStart()),
3425954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          Importer.Import(D->getLocation()),
3426954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          Property,
3427954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          D->getPropertyImplementation(),
3428954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          Ivar,
3429954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                  Importer.Import(D->getPropertyIvarDeclLoc()));
3430954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    ToImpl->setLexicalDeclContext(LexicalDC);
3431954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    Importer.Imported(D, ToImpl);
3432954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    LexicalDC->addDecl(ToImpl);
3433954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  } else {
3434954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    // Check that we have the same kind of property implementation (@synthesize
3435954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    // vs. @dynamic).
3436954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3437954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      Importer.ToDiag(ToImpl->getLocation(),
3438954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                      diag::err_odr_objc_property_impl_kind_inconsistent)
3439954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << Property->getDeclName()
3440954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << (ToImpl->getPropertyImplementation()
3441954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                              == ObjCPropertyImplDecl::Dynamic);
3442954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      Importer.FromDiag(D->getLocation(),
3443954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                        diag::note_odr_objc_property_impl_kind)
3444954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << D->getPropertyDecl()->getDeclName()
3445954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3446954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      return 0;
3447954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    }
3448954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3449954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    // For @synthesize, check that we have the same
3450954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3451954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        Ivar != ToImpl->getPropertyIvarDecl()) {
3452954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3453954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                      diag::err_odr_objc_synthesize_ivar_inconsistent)
3454954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << Property->getDeclName()
3455954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << ToImpl->getPropertyIvarDecl()->getDeclName()
3456954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << Ivar->getDeclName();
3457954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3458954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                        diag::note_odr_objc_synthesize_ivar_here)
3459954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << D->getPropertyIvarDecl()->getDeclName();
3460954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      return 0;
3461954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    }
3462954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3463954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    // Merge the existing implementation with the new implementation.
3464954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    Importer.Imported(D, ToImpl);
3465954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  }
3466954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3467954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  return ToImpl;
3468954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor}
3469954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
34702b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas GregorDecl *
34712b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas GregorASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
34722b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  // Import the context of this declaration.
34732b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
34742b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  if (!DC)
34752b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    return 0;
34762b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
34772b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  DeclContext *LexicalDC = DC;
34782b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
34792b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
34802b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    if (!LexicalDC)
34812b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      return 0;
34822b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  }
34832b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
34842b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  // Import the location of this declaration.
34852b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
34862b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
34875f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ObjCProtocolDecl *, 4> Protocols;
34885f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 4> Locations;
34892b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
34902b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    = D->protocol_loc_begin();
34912b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  for (ObjCForwardProtocolDecl::protocol_iterator FromProto
34922b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor         = D->protocol_begin(), FromProtoEnd = D->protocol_end();
34932b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor       FromProto != FromProtoEnd;
34942b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor       ++FromProto, ++FromProtoLoc) {
34952b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    ObjCProtocolDecl *ToProto
34962b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
34972b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    if (!ToProto)
34982b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      continue;
34992b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
35002b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Protocols.push_back(ToProto);
35012b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Locations.push_back(Importer.Import(*FromProtoLoc));
35022b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  }
35032b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
35042b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ObjCForwardProtocolDecl *ToForward
35052b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
35062b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor                                      Protocols.data(), Protocols.size(),
35072b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor                                      Locations.data());
35082b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ToForward->setLexicalDeclContext(LexicalDC);
35092b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  LexicalDC->addDecl(ToForward);
35102b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  Importer.Imported(D, ToForward);
35112b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  return ToForward;
35122b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor}
35132b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
3514a2bc15b7463a9f85a5bff1531d833c278426a733Douglas GregorDecl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3515a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  // Import the context of this declaration.
3516a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3517a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  if (!DC)
3518a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    return 0;
3519a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3520a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  DeclContext *LexicalDC = DC;
3521a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
3522a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3523a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    if (!LexicalDC)
3524a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      return 0;
3525a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  }
3526a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3527a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  // Import the location of this declaration.
3528a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
352995ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian  ObjCClassDecl::ObjCClassRef *From = D->getForwardDecl();
353095ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian  ObjCInterfaceDecl *ToIface
353195ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3532a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
353395ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian                                        Loc,
353495ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian                                        ToIface,
353595ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian                                        Importer.Import(From->getLocation()));
353695ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian
3537a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  ToClass->setLexicalDeclContext(LexicalDC);
3538a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  LexicalDC->addDecl(ToClass);
3539a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  Importer.Imported(D, ToClass);
3540a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  return ToClass;
3541a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor}
3542a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3543040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3544040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // For template arguments, we adopt the translation unit as our declaration
3545040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // context. This context will be fixed when the actual template declaration
3546040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // is created.
3547040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3548040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Import default argument.
3549040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return TemplateTypeParmDecl::Create(Importer.getToContext(),
3550040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                              Importer.getToContext().getTranslationUnitDecl(),
3551344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                      Importer.Import(D->getLocStart()),
3552040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      Importer.Import(D->getLocation()),
3553040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->getDepth(),
3554040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->getIndex(),
3555040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      Importer.Import(D->getIdentifier()),
3556040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->wasDeclaredWithTypename(),
3557040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->isParameterPack());
3558040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3559040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3560040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *
3561040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3562040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the name of this declaration.
3563040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
3564040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D->getDeclName() && !Name)
3565040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3566040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3567040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the location of this declaration.
3568040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
3569040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3570040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the type of this declaration.
3571040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  QualType T = Importer.Import(D->getType());
3572040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (T.isNull())
3573040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3574040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3575040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import type-source information.
3576040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3577040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D->getTypeSourceInfo() && !TInfo)
3578040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3579040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3580040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Import default argument.
3581040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3582040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3583040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                               Importer.getToContext().getTranslationUnitDecl(),
3584ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                         Importer.Import(D->getInnerLocStart()),
3585040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         Loc, D->getDepth(), D->getPosition(),
3586040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         Name.getAsIdentifierInfo(),
358710738d36b150aa65206890c1c845cdba076e4200Douglas Gregor                                         T, D->isParameterPack(), TInfo);
3588040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3589040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3590040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *
3591040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3592040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the name of this declaration.
3593040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
3594040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D->getDeclName() && !Name)
3595040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3596040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3597040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the location of this declaration.
3598040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
3599040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3600040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import template parameters.
3601040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  TemplateParameterList *TemplateParams
3602040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    = ImportTemplateParameterList(D->getTemplateParameters());
3603040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!TemplateParams)
3604040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3605040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3606040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Import default argument.
3607040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3608040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3609040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                              Importer.getToContext().getTranslationUnitDecl(),
3610040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          Loc, D->getDepth(), D->getPosition(),
361161c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                          D->isParameterPack(),
3612040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          Name.getAsIdentifierInfo(),
3613040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          TemplateParams);
3614040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3615040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3616040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3617040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // If this record has a definition in the translation unit we're coming from,
3618040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // but this particular declaration is not that definition, import the
3619040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // definition and map to that.
3620040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  CXXRecordDecl *Definition
3621040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3622040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (Definition && Definition != D->getTemplatedDecl()) {
3623040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *ImportedDef
3624040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      = Importer.Import(Definition->getDescribedClassTemplate());
3625040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!ImportedDef)
3626040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return 0;
3627040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3628040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return Importer.Imported(D, ImportedDef);
3629040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
3630040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3631040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the major distinguishing characteristics of this class template.
3632040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclContext *DC, *LexicalDC;
3633040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclarationName Name;
3634040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  SourceLocation Loc;
3635040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3636040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3637040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3638040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // We may already have a template of the same name; try to find and match it.
3639040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!DC->isFunctionOrMethod()) {
36405f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<NamedDecl *, 4> ConflictingDecls;
3641040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3642040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor         Lookup.first != Lookup.second;
3643040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor         ++Lookup.first) {
3644040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3645040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        continue;
3646040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3647040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Decl *Found = *Lookup.first;
3648040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (ClassTemplateDecl *FoundTemplate
3649040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                        = dyn_cast<ClassTemplateDecl>(Found)) {
3650040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (IsStructuralMatch(D, FoundTemplate)) {
3651040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          // The class templates structurally match; call it the same template.
3652040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          // FIXME: We may be filling in a forward declaration here. Handle
3653040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          // this case!
3654040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Importer.Imported(D->getTemplatedDecl(),
3655040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                            FoundTemplate->getTemplatedDecl());
3656040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          return Importer.Imported(D, FoundTemplate);
3657040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        }
3658040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
3659040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3660040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
3661040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
3662040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3663040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!ConflictingDecls.empty()) {
3664040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3665040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         ConflictingDecls.data(),
3666040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         ConflictingDecls.size());
3667040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
3668040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3669040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!Name)
3670040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return 0;
3671040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
3672040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3673040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3674040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3675040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Create the declaration that is being templated.
3676ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3677ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
3678040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3679040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                     DTemplated->getTagKind(),
3680ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     DC, StartLoc, IdLoc,
3681ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                   Name.getAsIdentifierInfo());
3682040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2Templated->setAccess(DTemplated->getAccess());
3683c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
3684040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2Templated->setLexicalDeclContext(LexicalDC);
3685040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3686040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Create the class template declaration itself.
3687040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  TemplateParameterList *TemplateParams
3688040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    = ImportTemplateParameterList(D->getTemplateParameters());
3689040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!TemplateParams)
3690040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3691040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3692040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3693040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                    Loc, Name, TemplateParams,
3694040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                    D2Templated,
3695040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  /*PrevDecl=*/0);
3696040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2Templated->setDescribedClassTemplate(D2);
3697040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3698040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2->setAccess(D->getAccess());
3699040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2->setLexicalDeclContext(LexicalDC);
3700040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  LexicalDC->addDecl(D2);
3701040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3702040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Note the relationship between the class templates.
3703040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  Importer.Imported(D, D2);
3704040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  Importer.Imported(DTemplated, D2Templated);
3705040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3706040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3707040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    // FIXME: Import definition!
3708040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
3709040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3710040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return D2;
3711040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3712040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3713d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorDecl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3714d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                          ClassTemplateSpecializationDecl *D) {
3715d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // If this record has a definition in the translation unit we're coming from,
3716d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // but this particular declaration is not that definition, import the
3717d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // definition and map to that.
3718d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  TagDecl *Definition = D->getDefinition();
3719d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (Definition && Definition != D) {
3720d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    Decl *ImportedDef = Importer.Import(Definition);
3721d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!ImportedDef)
3722d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return 0;
3723d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3724d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return Importer.Imported(D, ImportedDef);
3725d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3726d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3727d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateDecl *ClassTemplate
3728d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = cast_or_null<ClassTemplateDecl>(Importer.Import(
3729d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 D->getSpecializedTemplate()));
3730d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (!ClassTemplate)
3731d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3732d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3733d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Import the context of this declaration.
3734d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  DeclContext *DC = ClassTemplate->getDeclContext();
3735d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (!DC)
3736d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3737d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3738d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  DeclContext *LexicalDC = DC;
3739d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
3740d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3741d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!LexicalDC)
3742d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return 0;
3743d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3744d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3745d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Import the location of this declaration.
3746ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation StartLoc = Importer.Import(D->getLocStart());
3747ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation IdLoc = Importer.Import(D->getLocation());
3748d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3749d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Import template arguments.
37505f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 2> TemplateArgs;
3751d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (ImportTemplateArguments(D->getTemplateArgs().data(),
3752d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                              D->getTemplateArgs().size(),
3753d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                              TemplateArgs))
3754d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3755d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3756d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Try to find an existing specialization with these template arguments.
3757d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  void *InsertPos = 0;
3758d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateSpecializationDecl *D2
3759d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = ClassTemplate->findSpecialization(TemplateArgs.data(),
3760d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                        TemplateArgs.size(), InsertPos);
3761d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D2) {
3762d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // We already have a class template specialization with these template
3763d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // arguments.
3764d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3765d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // FIXME: Check for specialization vs. instantiation errors.
3766d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3767d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (RecordDecl *FoundDef = D2->getDefinition()) {
3768d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3769d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        // The record types structurally match, or the "from" translation
3770d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        // unit only had a forward declaration anyway; call it the same
3771d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        // function.
3772d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return Importer.Imported(D, FoundDef);
3773d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      }
3774d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
3775d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  } else {
3776d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Create a new specialization.
3777d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3778d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 D->getTagKind(), DC,
3779ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                 StartLoc, IdLoc,
3780ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                 ClassTemplate,
3781d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 TemplateArgs.data(),
3782d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 TemplateArgs.size(),
3783d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 /*PrevDecl=*/0);
3784d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    D2->setSpecializationKind(D->getSpecializationKind());
3785d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3786d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Add this specialization to the class template.
3787d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ClassTemplate->AddSpecialization(D2, InsertPos);
3788d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3789d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Import the qualifier, if any.
3790c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
3791d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3792d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Add the specialization to this context.
3793d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    D2->setLexicalDeclContext(LexicalDC);
3794d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    LexicalDC->addDecl(D2);
3795d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3796d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  Importer.Imported(D, D2);
3797d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3798d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D->isDefinition() && ImportDefinition(D, D2))
3799d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3800d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3801d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return D2;
3802d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
3803d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
38044800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
38054800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor// Import Statements
38064800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
38074800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
38084800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorStmt *ASTNodeImporter::VisitStmt(Stmt *S) {
38094800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
38104800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    << S->getStmtClassName();
38114800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return 0;
38124800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
38134800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
38144800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
38154800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor// Import Expressions
38164800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
38174800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorExpr *ASTNodeImporter::VisitExpr(Expr *E) {
38184800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
38194800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    << E->getStmtClassName();
38204800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return 0;
38214800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
38224800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
3823440806306674e23ad74726208cbdc6f37849dd9dDouglas GregorExpr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3824440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3825440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (!ToD)
3826440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    return 0;
38273aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth
38283aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  NamedDecl *FoundD = 0;
38293aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  if (E->getDecl() != E->getFoundDecl()) {
38303aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
38313aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    if (!FoundD)
38323aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth      return 0;
38333aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  }
3834440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
3835440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  QualType T = Importer.Import(E->getType());
3836440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (T.isNull())
3837440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    return 0;
3838440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
383940d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor  return DeclRefExpr::Create(Importer.getToContext(),
384040d96a69c0e1e8c10f92d450c305a7aae696ca9cDouglas Gregor                             Importer.Import(E->getQualifierLoc()),
3841440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             ToD,
3842440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             Importer.Import(E->getLocation()),
3843f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                             T, E->getValueKind(),
38443aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth                             FoundD,
3845440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor                             /*FIXME:TemplateArgs=*/0);
3846440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor}
3847440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
38484800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorExpr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
38494800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  QualType T = Importer.Import(E->getType());
38504800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (T.isNull())
38514800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return 0;
38524800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
38539996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return IntegerLiteral::Create(Importer.getToContext(),
38549996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                                E->getValue(), T,
38559996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                                Importer.Import(E->getLocation()));
38564800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
38574800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
3858b2e400aae8c62c4e1616016f40618baace0da065Douglas GregorExpr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3859b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  QualType T = Importer.Import(E->getType());
3860b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  if (T.isNull())
3861b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor    return 0;
3862b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor
38635cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
38645cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                                                        E->getKind(), T,
3865b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor                                          Importer.Import(E->getLocation()));
3866b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor}
3867b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor
3868f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3869f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
3870f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!SubExpr)
3871f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3872f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3873f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext())
3874f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                  ParenExpr(Importer.Import(E->getLParen()),
3875f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                            Importer.Import(E->getRParen()),
3876f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                            SubExpr);
3877f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3878f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3879f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3880f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
3881f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
3882f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3883f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3884f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
3885f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!SubExpr)
3886f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3887f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3888f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
3889f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                     T, E->getValueKind(),
3890f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                     E->getObjectKind(),
3891f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                         Importer.Import(E->getOperatorLoc()));
3892f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3893f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3894f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter CollingbourneExpr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3895f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne                                            UnaryExprOrTypeTraitExpr *E) {
3896bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  QualType ResultType = Importer.Import(E->getType());
3897bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3898bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  if (E->isArgumentType()) {
3899bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3900bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    if (!TInfo)
3901bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor      return 0;
3902bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3903f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3904f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne                                           TInfo, ResultType,
3905bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                           Importer.Import(E->getOperatorLoc()),
3906bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                           Importer.Import(E->getRParenLoc()));
3907bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  }
3908bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3909bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3910bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  if (!SubExpr)
3911bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    return 0;
3912bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3913f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3914f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne                                          SubExpr, ResultType,
3915bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                          Importer.Import(E->getOperatorLoc()),
3916bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                          Importer.Import(E->getRParenLoc()));
3917bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor}
3918bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3919f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3920f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
3921f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
3922f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3923f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3924f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *LHS = Importer.Import(E->getLHS());
3925f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!LHS)
3926f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3927f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3928f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *RHS = Importer.Import(E->getRHS());
3929f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!RHS)
3930f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3931f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3932f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
3933f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                      T, E->getValueKind(),
3934f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                      E->getObjectKind(),
3935f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                          Importer.Import(E->getOperatorLoc()));
3936f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3937f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3938f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3939f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
3940f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
3941f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3942f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3943f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3944f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (CompLHSType.isNull())
3945f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3946f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3947f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType CompResultType = Importer.Import(E->getComputationResultType());
3948f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (CompResultType.isNull())
3949f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3950f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3951f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *LHS = Importer.Import(E->getLHS());
3952f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!LHS)
3953f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3954f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3955f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *RHS = Importer.Import(E->getRHS());
3956f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!RHS)
3957f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3958f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3959f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext())
3960f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                        CompoundAssignOperator(LHS, RHS, E->getOpcode(),
3961f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                               T, E->getValueKind(),
3962f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                               E->getObjectKind(),
3963f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                               CompLHSType, CompResultType,
3964f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                          Importer.Import(E->getOperatorLoc()));
3965f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3966f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3967da57f3eeab7b7f7f6e6788956f0a0d9adf196a7dBenjamin Kramerstatic bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3968f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (E->path_empty()) return false;
3969f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
3970f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  // TODO: import cast paths
3971f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return true;
3972f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
3973f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
397436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorExpr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
397536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  QualType T = Importer.Import(E->getType());
397636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (T.isNull())
397736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
397836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
397936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
398036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!SubExpr)
398136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
3982f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
3983f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  CXXCastPath BasePath;
3984f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (ImportCastPath(E, BasePath))
3985f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return 0;
3986f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
3987f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
39885baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                                  SubExpr, &BasePath, E->getValueKind());
398936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
399036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
3991008847a70ab122a99911149199855060fb3753b4Douglas GregorExpr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3992008847a70ab122a99911149199855060fb3753b4Douglas Gregor  QualType T = Importer.Import(E->getType());
3993008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (T.isNull())
3994008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
3995008847a70ab122a99911149199855060fb3753b4Douglas Gregor
3996008847a70ab122a99911149199855060fb3753b4Douglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
3997008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (!SubExpr)
3998008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
3999008847a70ab122a99911149199855060fb3753b4Douglas Gregor
4000008847a70ab122a99911149199855060fb3753b4Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4001008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (!TInfo && E->getTypeInfoAsWritten())
4002008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
4003008847a70ab122a99911149199855060fb3753b4Douglas Gregor
4004f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  CXXCastPath BasePath;
4005f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (ImportCastPath(E, BasePath))
4006f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return 0;
4007f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
4008f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  return CStyleCastExpr::Create(Importer.getToContext(), T,
4009f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                E->getValueKind(), E->getCastKind(),
4010f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                SubExpr, &BasePath, TInfo,
4011f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                Importer.Import(E->getLParenLoc()),
4012f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                Importer.Import(E->getRParenLoc()));
4013008847a70ab122a99911149199855060fb3753b4Douglas Gregor}
4014008847a70ab122a99911149199855060fb3753b4Douglas Gregor
401533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios KyrtzidisASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
4016d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor                         ASTContext &FromContext, FileManager &FromFileManager,
4017d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor                         bool MinimalImport)
40181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  : ToContext(ToContext), FromContext(FromContext),
4019d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4020d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    Minimal(MinimalImport)
4021d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor{
40229bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ImportedDecls[FromContext.getTranslationUnitDecl()]
40239bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    = ToContext.getTranslationUnitDecl();
40249bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
40259bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40269bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorASTImporter::~ASTImporter() { }
40271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
40281b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTImporter::Import(QualType FromT) {
40291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (FromT.isNull())
40301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
4031f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall
4032f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *fromTy = FromT.getTypePtr();
40331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
4034169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Check whether we've already imported this type.
4035f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  llvm::DenseMap<const Type *, const Type *>::iterator Pos
4036f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    = ImportedTypes.find(fromTy);
4037169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  if (Pos != ImportedTypes.end())
4038f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
40391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
4040169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Import the type
40411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  ASTNodeImporter Importer(*this);
4042f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  QualType ToT = Importer.Visit(fromTy);
40431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToT.isNull())
40441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToT;
40451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
4046169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Record the imported type.
4047f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  ImportedTypes[fromTy] = ToT.getTypePtr();
4048169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor
4049f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
40501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
40511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
40529bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorTypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
405382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (!FromTSI)
405482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return FromTSI;
405582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
405682fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // FIXME: For now we just create a "trivial" type source info based
40575606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky  // on the type and a single location. Implement a real version of this.
405882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  QualType T = Import(FromTSI->getType());
405982fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (T.isNull())
406082fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
406182fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
406282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  return ToContext.getTrivialTypeSourceInfo(T,
4063bd054dba8a3023821f2a0951b0fae05e3522a7c9Abramo Bagnara                        FromTSI->getTypeLoc().getSourceRange().getBegin());
40649bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
40659bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40669bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorDecl *ASTImporter::Import(Decl *FromD) {
40679bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromD)
40689bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
40699bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40701cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  ASTNodeImporter Importer(*this);
40711cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
40729bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Check whether we've already imported this declaration.
40739bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
40741cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (Pos != ImportedDecls.end()) {
40751cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    Decl *ToD = Pos->second;
40761cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    Importer.ImportDefinitionIfNeeded(FromD, ToD);
40771cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    return ToD;
40781cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  }
40799bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40809bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Import the type
40819bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  Decl *ToD = Importer.Visit(FromD);
40829bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!ToD)
40839bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
40849bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40859bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Record the imported declaration.
40869bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ImportedDecls[FromD] = ToD;
4087ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
4088ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4089ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // Keep track of anonymous tags that have an associated typedef.
4090162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    if (FromTag->getTypedefNameForAnonDecl())
4091ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      AnonTagsWithPendingTypedefs.push_back(FromTag);
4092162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
4093ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // When we've finished transforming a typedef, see whether it was the
4094ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // typedef for an anonymous tag.
40955f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    for (SmallVector<TagDecl *, 4>::iterator
4096ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor               FromTag = AnonTagsWithPendingTypedefs.begin(),
4097ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            FromTagEnd = AnonTagsWithPendingTypedefs.end();
4098ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor         FromTag != FromTagEnd; ++FromTag) {
4099162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
4100ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4101ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          // We found the typedef for an anonymous tag; link them.
4102162e1c1b487352434552147967c3dd296ebee2f7Richard Smith          ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
4103ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          AnonTagsWithPendingTypedefs.erase(FromTag);
4104ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          break;
4105ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        }
4106ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      }
4107ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    }
4108ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  }
4109ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
41109bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return ToD;
41119bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
41129bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
41139bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorDeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
41149bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromDC)
41159bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return FromDC;
41169bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
41179bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
41189bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
41199bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
41209bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorExpr *ASTImporter::Import(Expr *FromE) {
41219bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromE)
41229bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
41239bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
41249bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
41259bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
41269bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
41279bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorStmt *ASTImporter::Import(Stmt *FromS) {
41289bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromS)
41299bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
41309bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
41314800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Check whether we've already imported this declaration.
41324800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
41334800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (Pos != ImportedStmts.end())
41344800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return Pos->second;
41354800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
41364800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Import the type
41374800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  ASTNodeImporter Importer(*this);
41384800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Stmt *ToS = Importer.Visit(FromS);
41394800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (!ToS)
41404800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return 0;
41414800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
41424800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Record the imported declaration.
41434800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  ImportedStmts[FromS] = ToS;
41444800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return ToS;
41459bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
41469bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
41479bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorNestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
41489bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromNNS)
41499bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
41509bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
41518703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
41528703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor
41538703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  switch (FromNNS->getKind()) {
41548703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  case NestedNameSpecifier::Identifier:
41558703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
41568703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor      return NestedNameSpecifier::Create(ToContext, prefix, II);
41578703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    }
41588703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    return 0;
41598703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor
41608703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  case NestedNameSpecifier::Namespace:
41618703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    if (NamespaceDecl *NS =
41628703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor          cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
41638703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor      return NestedNameSpecifier::Create(ToContext, prefix, NS);
41648703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    }
41658703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    return 0;
41668703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor
41678703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  case NestedNameSpecifier::NamespaceAlias:
41688703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    if (NamespaceAliasDecl *NSAD =
41698703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor          cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
41708703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor      return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
41718703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    }
41728703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    return 0;
41738703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor
41748703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  case NestedNameSpecifier::Global:
41758703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    return NestedNameSpecifier::GlobalSpecifier(ToContext);
41768703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor
41778703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  case NestedNameSpecifier::TypeSpec:
41788703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
41798703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor      QualType T = Import(QualType(FromNNS->getAsType(), 0u));
41808703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor      if (!T.isNull()) {
41818703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor        bool bTemplate = FromNNS->getKind() ==
41828703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor                         NestedNameSpecifier::TypeSpecWithTemplate;
41838703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor        return NestedNameSpecifier::Create(ToContext, prefix,
41848703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor                                           bTemplate, T.getTypePtr());
41858703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor      }
41868703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    }
41878703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    return 0;
41888703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  }
41898703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor
41908703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  llvm_unreachable("Invalid nested name specifier kind");
41919bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return 0;
41929bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
41939bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
4194c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas GregorNestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4195c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  // FIXME: Implement!
4196c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  return NestedNameSpecifierLoc();
4197c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor}
4198c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor
4199d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorTemplateName ASTImporter::Import(TemplateName From) {
4200d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  switch (From.getKind()) {
4201d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::Template:
4202d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (TemplateDecl *ToTemplate
4203d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4204d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateName(ToTemplate);
4205d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4206d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateName();
4207d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4208d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::OverloadedTemplate: {
4209d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4210d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    UnresolvedSet<2> ToTemplates;
4211d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4212d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                             E = FromStorage->end();
4213d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor         I != E; ++I) {
4214d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4215d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        ToTemplates.addDecl(To);
4216d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      else
4217d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return TemplateName();
4218d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
4219d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4220d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                               ToTemplates.end());
4221d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
4222d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4223d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::QualifiedTemplate: {
4224d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4225d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4226d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Qualifier)
4227d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateName();
4228d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4229d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (TemplateDecl *ToTemplate
4230d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4231d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return ToContext.getQualifiedTemplateName(Qualifier,
4232d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                QTN->hasTemplateKeyword(),
4233d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                ToTemplate);
4234d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4235d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateName();
4236d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
4237d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4238d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::DependentTemplate: {
4239d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    DependentTemplateName *DTN = From.getAsDependentTemplateName();
4240d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4241d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Qualifier)
4242d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateName();
4243d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4244d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (DTN->isIdentifier()) {
4245d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return ToContext.getDependentTemplateName(Qualifier,
4246d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                Import(DTN->getIdentifier()));
4247d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
4248d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4249d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4250d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
4251146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
4252146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParm: {
4253146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmStorage *subst
4254146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = From.getAsSubstTemplateTemplateParm();
4255146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateTemplateParmDecl *param
4256146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4257146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    if (!param)
4258146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      return TemplateName();
4259146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
4260146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateName replacement = Import(subst->getReplacement());
4261146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    if (replacement.isNull()) return TemplateName();
4262146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
4263146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return ToContext.getSubstTemplateTemplateParm(param, replacement);
4264146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
42651aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
42661aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  case TemplateName::SubstTemplateTemplateParmPack: {
42671aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    SubstTemplateTemplateParmPackStorage *SubstPack
42681aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor      = From.getAsSubstTemplateTemplateParmPack();
42691aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    TemplateTemplateParmDecl *Param
42701aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor      = cast_or_null<TemplateTemplateParmDecl>(
42711aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                        Import(SubstPack->getParameterPack()));
42721aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    if (!Param)
42731aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor      return TemplateName();
42741aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
42751aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    ASTNodeImporter Importer(*this);
42761aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    TemplateArgument ArgPack
42771aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor      = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
42781aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    if (ArgPack.isNull())
42791aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor      return TemplateName();
42801aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
42811aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
42821aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
4283d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
4284d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4285d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm_unreachable("Invalid template name kind");
4286d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return TemplateName();
4287d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
4288d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
42899bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorSourceLocation ASTImporter::Import(SourceLocation FromLoc) {
42909bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (FromLoc.isInvalid())
42919bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return SourceLocation();
42929bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
4293885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &FromSM = FromContext.getSourceManager();
4294885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4295885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // For now, map everything down to its spelling location, so that we
4296b10aa3e3a26f84d1a0dda6e4c057f83a11a3076cChandler Carruth  // don't have to import macro expansions.
4297b10aa3e3a26f84d1a0dda6e4c057f83a11a3076cChandler Carruth  // FIXME: Import macro expansions!
4298885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  FromLoc = FromSM.getSpellingLoc(FromLoc);
4299885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4300885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &ToSM = ToContext.getSourceManager();
4301885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4302a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis             .getLocWithOffset(Decomposed.second);
43039bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
43049bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
43059bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorSourceRange ASTImporter::Import(SourceRange FromRange) {
43069bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
43079bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
43089bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
4309885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas GregorFileID ASTImporter::Import(FileID FromID) {
4310535a3e20104461c136654d59fb833ae80644ae79Sebastian Redl  llvm::DenseMap<FileID, FileID>::iterator Pos
4311535a3e20104461c136654d59fb833ae80644ae79Sebastian Redl    = ImportedFileIDs.find(FromID);
4312885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  if (Pos != ImportedFileIDs.end())
4313885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    return Pos->second;
4314885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4315885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &FromSM = FromContext.getSourceManager();
4316885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &ToSM = ToContext.getSourceManager();
4317885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4318b10aa3e3a26f84d1a0dda6e4c057f83a11a3076cChandler Carruth  assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
4319885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4320885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // Include location of this file.
4321885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4322885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4323885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // Map the FileID for to the "to" source manager.
4324885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  FileID ToID;
4325885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
4326b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  if (Cache->OrigEntry) {
4327885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4328885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // disk again
4329885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4330885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // than mmap the files several times.
4331b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
4332885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4333885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                             FromSLoc.getFile().getFileCharacteristic());
4334885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  } else {
4335885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We want to re-use the existing MemoryBuffer!
433633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    const llvm::MemoryBuffer *
433733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis        FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
4338885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    llvm::MemoryBuffer *ToBuf
4339a0a270c0f1c0a4e3482438bdc5f4a7bd3d25f0a6Chris Lattner      = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
4340885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                                             FromBuf->getBufferIdentifier());
4341885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4342885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  }
4343885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4344885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4345535a3e20104461c136654d59fb833ae80644ae79Sebastian Redl  ImportedFileIDs[FromID] = ToID;
4346885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  return ToID;
4347885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor}
4348885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4349d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregorvoid ASTImporter::ImportDefinition(Decl *From) {
4350d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor  Decl *To = Import(From);
4351d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor  if (!To)
4352d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    return;
4353d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor
4354d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor  if (DeclContext *FromDC = cast<DeclContext>(From)) {
4355d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    ASTNodeImporter Importer(*this);
4356673e775beba71a69dbab4fcd733a84f4cfe2ebfcSean Callanan
4357673e775beba71a69dbab4fcd733a84f4cfe2ebfcSean Callanan    if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4358673e775beba71a69dbab4fcd733a84f4cfe2ebfcSean Callanan      if (!ToRecord->getDefinition()) {
4359673e775beba71a69dbab4fcd733a84f4cfe2ebfcSean Callanan        Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
4360673e775beba71a69dbab4fcd733a84f4cfe2ebfcSean Callanan                                  /*ForceImport=*/true);
4361673e775beba71a69dbab4fcd733a84f4cfe2ebfcSean Callanan        return;
4362673e775beba71a69dbab4fcd733a84f4cfe2ebfcSean Callanan      }
4363673e775beba71a69dbab4fcd733a84f4cfe2ebfcSean Callanan    }
43641cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
43651cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
43661cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor      if (!ToEnum->getDefinition()) {
43671cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor        Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
43681cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor                                  /*ForceImport=*/true);
43691cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor        return;
43701cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor      }
43711cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    }
43721cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
4373d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    Importer.ImportDeclContext(FromDC, true);
4374d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor  }
4375d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor}
4376d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor
43771b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorDeclarationName ASTImporter::Import(DeclarationName FromName) {
43781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!FromName)
43791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return DeclarationName();
43801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
43811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  switch (FromName.getNameKind()) {
43821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::Identifier:
43831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Import(FromName.getAsIdentifierInfo());
43841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
43851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCZeroArgSelector:
43861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCOneArgSelector:
43871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCMultiArgSelector:
43881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Import(FromName.getObjCSelector());
43891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
43901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXConstructorName: {
43911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
43921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
43931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
43941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
43951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXConstructorName(
43961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
43971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
43981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
43991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXDestructorName: {
44001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
44011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
44021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
44031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXDestructorName(
44051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
44061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
44071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXConversionFunctionName: {
44091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
44101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
44111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
44121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXConversionFunctionName(
44141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
44151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
44161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXOperatorName:
44181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXOperatorName(
44191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                          FromName.getCXXOverloadedOperator());
44201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXLiteralOperatorName:
44221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXLiteralOperatorName(
44231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                   Import(FromName.getCXXLiteralIdentifier()));
44241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXUsingDirective:
44261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: STATICS!
44271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return DeclarationName::getUsingDirectiveName();
44281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
44291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Silence bogus GCC warning
44311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return DeclarationName();
44321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
44331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
4434d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorIdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
44351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!FromId)
44361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return 0;
44371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return &ToContext.Idents.get(FromId->getName());
44391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
4440089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
4441c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas GregorSelector ASTImporter::Import(Selector FromSel) {
4442c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (FromSel.isNull())
4443c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return Selector();
4444c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
44455f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 4> Idents;
4446c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4447c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4448c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4449c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4450c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor}
4451c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
4452089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4453089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                DeclContext *DC,
4454089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                unsigned IDNS,
4455089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                NamedDecl **Decls,
4456089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                unsigned NumDecls) {
4457089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  return Name;
4458089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
4459089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
4460089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
446133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  return ToContext.getDiagnostics().Report(Loc, DiagID);
4462089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
4463089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
4464089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
446533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  return FromContext.getDiagnostics().Report(Loc, DiagID);
4466089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
44675ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
44685ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas GregorDecl *ASTImporter::Imported(Decl *From, Decl *To) {
44695ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  ImportedDecls[From] = To;
44705ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  return To;
4471af667588d53de22795c5304f1496ccaac2a71402Daniel Dunbar}
4472ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
4473ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregorbool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
4474f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  llvm::DenseMap<const Type *, const Type *>::iterator Pos
4475ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor   = ImportedTypes.find(From.getTypePtr());
4476ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4477ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return true;
4478ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
447933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
4480bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(From, To);
4481ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor}
4482