ASTImporter.cpp revision b001de7458d17c17e6d8b8034c7cfcefd3b70c00
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  }
813b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
814b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic: {
815b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (!IsStructurallyEquivalent(Context,
816b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman                                  cast<AtomicType>(T1)->getValueType(),
817b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman                                  cast<AtomicType>(T2)->getValueType()))
818b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return false;
819b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    break;
820b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
821b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
82273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  } // end switch
82373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
82473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
82573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
82673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
82773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two records.
82873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
82973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     RecordDecl *D1, RecordDecl *D2) {
83073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (D1->isUnion() != D2->isUnion()) {
83173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
83273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
83373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
83473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << D1->getDeclName() << (unsigned)D1->getTagKind();
83573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
83673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
83773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
838d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // If both declarations are class template specializations, we know
839d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // the ODR applies, so check the template and template arguments.
840d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateSpecializationDecl *Spec1
841d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = dyn_cast<ClassTemplateSpecializationDecl>(D1);
842d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateSpecializationDecl *Spec2
843d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = dyn_cast<ClassTemplateSpecializationDecl>(D2);
844d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (Spec1 && Spec2) {
845d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Check that the specialized templates are the same.
846d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
847d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                  Spec2->getSpecializedTemplate()))
848d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return false;
849d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
850d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Check that the template arguments are the same.
851d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
852d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return false;
853d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
854d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
855d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (!IsStructurallyEquivalent(Context,
856d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Spec1->getTemplateArgs().get(I),
857d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                    Spec2->getTemplateArgs().get(I)))
858d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return false;
859d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
860d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // If one is a class template specialization and the other is not, these
861fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner  // structures are different.
862d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  else if (Spec1 || Spec2)
863d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return false;
864d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
865ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Compare the definitions of these two records. If either or both are
866ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // incomplete, we assume that they are equivalent.
867ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  D1 = D1->getDefinition();
868ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  D2 = D2->getDefinition();
869ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (!D1 || !D2)
870ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return true;
871ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
87273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
87373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
87473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
87573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
876040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          << Context.C2.getTypeDeclType(D2);
87773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
878040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          << D2CXX->getNumBases();
87973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
880040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          << D1CXX->getNumBases();
88173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
88273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
88373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
88473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      // Check the base classes.
88573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
88673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                           BaseEnd1 = D1CXX->bases_end(),
88773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                Base2 = D2CXX->bases_begin();
88873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor           Base1 != BaseEnd1;
88973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor           ++Base1, ++Base2) {
89073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!IsStructurallyEquivalent(Context,
89173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                      Base1->getType(), Base2->getType())) {
89273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
89373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Context.C2.getTypeDeclType(D2);
89473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
89573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->getType()
89673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->getSourceRange();
89773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
89873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getType()
89973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getSourceRange();
90073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          return false;
90173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        }
90273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
90373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check virtual vs. non-virtual inheritance mismatch.
90473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (Base1->isVirtual() != Base2->isVirtual()) {
90573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
90673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Context.C2.getTypeDeclType(D2);
90773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag2(Base2->getSourceRange().getBegin(),
90873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                        diag::note_odr_virtual_base)
90973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base2->isVirtual() << Base2->getSourceRange();
91073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
91173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->isVirtual()
91273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor            << Base1->getSourceRange();
91373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          return false;
91473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        }
91573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
91673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    } else if (D1CXX->getNumBases() > 0) {
91773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
91873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
91973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
92073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
92173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Base1->getType()
92273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Base1->getSourceRange();
92373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
92473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
92573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
92673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
92773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
92873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Check the fields for consistency.
92973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
93073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                             Field2End = D2->field_end();
93173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
93273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Field1End = D1->field_end();
93373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       Field1 != Field1End;
93473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       ++Field1, ++Field2) {
93573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field2 == Field2End) {
93673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
93773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
93873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Field1->getLocation(), diag::note_odr_field)
93973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName() << Field1->getType();
94073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
94173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
94273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
94373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
94473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsStructurallyEquivalent(Context,
94573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  Field1->getType(), Field2->getType())) {
94673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
94773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
94873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(Field2->getLocation(), diag::note_odr_field)
94973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field2->getDeclName() << Field2->getType();
95073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(Field1->getLocation(), diag::note_odr_field)
95173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName() << Field1->getType();
95273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
95373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
95473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
95573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field1->isBitField() != Field2->isBitField()) {
95673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
95773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
95873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (Field1->isBitField()) {
95973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        llvm::APSInt Bits;
96073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
96173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
96273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field1->getDeclName() << Field1->getType()
96373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits.toString(10, false);
96473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
96573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName();
96673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
96773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        llvm::APSInt Bits;
96873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
96973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
97073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName() << Field2->getType()
97173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits.toString(10, false);
97273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(),
97373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                          diag::note_odr_not_bit_field)
97473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Field1->getDeclName();
97573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
97673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
97773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
97873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
97973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (Field1->isBitField()) {
98073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      // Make sure that the bit-fields are the same length.
98173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      llvm::APSInt Bits1, Bits2;
98273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
98373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
98473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
98573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
98673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
98773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (!IsSameValue(Bits1, Bits2)) {
98873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
98973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Context.C2.getTypeDeclType(D2);
99073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
99173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field2->getDeclName() << Field2->getType()
99273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits2.toString(10, false);
99373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
99473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Field1->getDeclName() << Field1->getType()
99573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor          << Bits1.toString(10, false);
99673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        return false;
99773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
99873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
99973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
100073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
100173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (Field2 != Field2End) {
100273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
100373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
100473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(Field2->getLocation(), diag::note_odr_field)
100573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Field2->getDeclName() << Field2->getType();
100673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
100773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
100873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
100973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
101073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
101173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
101273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
101373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two enums.
101473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
101573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     EnumDecl *D1, EnumDecl *D2) {
101673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
101773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                             EC2End = D2->enumerator_end();
101873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
101973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                  EC1End = D1->enumerator_end();
102073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor       EC1 != EC1End; ++EC1, ++EC2) {
102173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (EC2 == EC2End) {
102273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
102373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
102473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
102573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getDeclName()
102673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getInitVal().toString(10);
102773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
102873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
102973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
103073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
103173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::APSInt Val1 = EC1->getInitVal();
103273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    llvm::APSInt Val2 = EC2->getInitVal();
103373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (!IsSameValue(Val1, Val2) ||
103473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
103573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
103673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << Context.C2.getTypeDeclType(D2);
103773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
103873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC2->getDeclName()
103973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC2->getInitVal().toString(10);
104073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
104173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getDeclName()
104273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        << EC1->getInitVal().toString(10);
104373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      return false;
104473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    }
104573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
104673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
104773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (EC2 != EC2End) {
104873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
104973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << Context.C2.getTypeDeclType(D2);
105073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
105173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << EC2->getDeclName()
105273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      << EC2->getInitVal().toString(10);
105373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
105473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
105573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
105673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
105773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
105873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
1059040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1060040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1061040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateParameterList *Params1,
1062040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateParameterList *Params2) {
1063040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (Params1->size() != Params2->size()) {
1064040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(Params2->getTemplateLoc(),
1065040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                  diag::err_odr_different_num_template_parameters)
1066040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << Params1->size() << Params2->size();
1067040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(Params1->getTemplateLoc(),
1068040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                  diag::note_odr_template_parameter_list);
1069040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1070040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1071040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1072040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1073040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1074040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Context.Diag2(Params2->getParam(I)->getLocation(),
1075040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                    diag::err_odr_different_template_parameter_kind);
1076040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Context.Diag1(Params1->getParam(I)->getLocation(),
1077040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                    diag::note_odr_template_parameter_here);
1078040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return false;
1079040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
1080040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1081040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1082040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          Params2->getParam(I))) {
1083040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1084040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return false;
1085040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
1086040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1087040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1088040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return true;
1089040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1090040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1091040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1092040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTypeParmDecl *D1,
1093040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTypeParmDecl *D2) {
1094040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D1->isParameterPack() != D2->isParameterPack()) {
1095040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1096040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D2->isParameterPack();
1097040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1098040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D1->isParameterPack();
1099040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1100040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1101040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1102040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return true;
1103040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1104040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1105040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1106040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     NonTypeTemplateParmDecl *D1,
1107040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     NonTypeTemplateParmDecl *D2) {
1108040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Enable once we have variadic templates.
1109040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#if 0
1110040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D1->isParameterPack() != D2->isParameterPack()) {
1111040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1112040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D2->isParameterPack();
1113040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1114040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D1->isParameterPack();
1115040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1116040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1117040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#endif
1118040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1119040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check types.
1120040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1121040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(),
1122040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                  diag::err_odr_non_type_parameter_type_inconsistent)
1123040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D2->getType() << D1->getType();
1124040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1125040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      << D1->getType();
1126040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1127040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1128040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1129040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return true;
1130040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1131040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1132040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1133040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTemplateParmDecl *D1,
1134040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     TemplateTemplateParmDecl *D2) {
1135040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Enable once we have variadic templates.
1136040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#if 0
1137040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D1->isParameterPack() != D2->isParameterPack()) {
1138040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1139040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    << D2->isParameterPack();
1140040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1141040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    << D1->isParameterPack();
1142040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
1143040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1144040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor#endif
1145040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1146040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check template parameter lists.
1147040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1148040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                  D2->getTemplateParameters());
1149040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1150040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1151040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1152040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     ClassTemplateDecl *D1,
1153040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     ClassTemplateDecl *D2) {
1154040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check template parameters.
1155040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!IsStructurallyEquivalent(Context,
1156040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                D1->getTemplateParameters(),
1157040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                D2->getTemplateParameters()))
1158040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return false;
115973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
1160040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Check the templated declaration.
1161040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1162040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          D2->getTemplatedDecl());
1163040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1164040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
116573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor/// \brief Determine structural equivalence of two declarations.
116673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorstatic bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
116773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                     Decl *D1, Decl *D2) {
116873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // FIXME: Check for known structural equivalences via a callback of some sort.
116973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
1170ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Check whether we already know that these two declarations are not
1171ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // structurally equivalent.
1172ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1173ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                      D2->getCanonicalDecl())))
1174ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return false;
1175ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
117673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Determine whether we've already produced a tentative equivalence for D1.
117773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
117873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (EquivToD1)
117973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return EquivToD1 == D2->getCanonicalDecl();
118073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
118173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  // Produce a tentative equivalence D1 <-> D2, which will be checked later.
118273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  EquivToD1 = D2->getCanonicalDecl();
118373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
118473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return true;
118573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
118673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
118773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
118873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                            Decl *D2) {
118973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!::IsStructurallyEquivalent(*this, D1, D2))
119073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
119173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
119273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return !Finish();
119373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
119473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
119573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
119673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                                            QualType T2) {
119773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!::IsStructurallyEquivalent(*this, T1, T2))
119873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    return false;
119973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
120073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return !Finish();
120173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
120273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
120373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregorbool StructuralEquivalenceContext::Finish() {
120473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  while (!DeclsToCheck.empty()) {
120573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // Check the next declaration.
120673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Decl *D1 = DeclsToCheck.front();
120773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    DeclsToCheck.pop_front();
120873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
120973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    Decl *D2 = TentativeEquivalences[D1];
121073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    assert(D2 && "Unrecorded tentative equivalence?");
121173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
1212ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    bool Equivalent = true;
1213ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
121473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Switch on all declaration kinds. For now, we're just going to
121573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // check the obvious ones.
121673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
121773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
121873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check for equivalent structure names.
121973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name1 = Record1->getIdentifier();
1220162e1c1b487352434552147967c3dd296ebee2f7Richard Smith        if (!Name1 && Record1->getTypedefNameForAnonDecl())
1221162e1c1b487352434552147967c3dd296ebee2f7Richard Smith          Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
122273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name2 = Record2->getIdentifier();
1223162e1c1b487352434552147967c3dd296ebee2f7Richard Smith        if (!Name2 && Record2->getTypedefNameForAnonDecl())
1224162e1c1b487352434552147967c3dd296ebee2f7Richard Smith          Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
1225ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (!::IsStructurallyEquivalent(Name1, Name2) ||
1226ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this, Record1, Record2))
1227ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
122873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
122973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Record/non-record mismatch.
1230ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
123173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
1232ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
123373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
123473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Check for equivalent enum names.
123573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name1 = Enum1->getIdentifier();
1236162e1c1b487352434552147967c3dd296ebee2f7Richard Smith        if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1237162e1c1b487352434552147967c3dd296ebee2f7Richard Smith          Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
123873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        IdentifierInfo *Name2 = Enum2->getIdentifier();
1239162e1c1b487352434552147967c3dd296ebee2f7Richard Smith        if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1240162e1c1b487352434552147967c3dd296ebee2f7Richard Smith          Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
1241ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (!::IsStructurallyEquivalent(Name1, Name2) ||
1242ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1243ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
124473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
124573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Enum/non-enum mismatch
1246ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
124773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
1248162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1249162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
125073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
1251ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                        Typedef2->getIdentifier()) ||
1252ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            !::IsStructurallyEquivalent(*this,
125373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        Typedef1->getUnderlyingType(),
125473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        Typedef2->getUnderlyingType()))
1255ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          Equivalent = false;
125673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      } else {
125773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor        // Typedef/non-typedef mismatch.
1258ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        Equivalent = false;
125973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      }
1260040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (ClassTemplateDecl *ClassTemplate1
1261040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                           = dyn_cast<ClassTemplateDecl>(D1)) {
1262040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1263040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1264040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                        ClassTemplate2->getIdentifier()) ||
1265040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor            !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1266040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1267040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1268040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Class template/non-class-template mismatch.
1269040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1270040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1271040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1272040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1273040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1274040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1275040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1276040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Kind mismatch.
1277040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1278040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1279040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (NonTypeTemplateParmDecl *NTTP1
1280040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                     = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1281040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (NonTypeTemplateParmDecl *NTTP2
1282040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1283040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1284040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1285040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1286040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Kind mismatch.
1287040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1288040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1289040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    } else if (TemplateTemplateParmDecl *TTP1
1290040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                  = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1291040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (TemplateTemplateParmDecl *TTP2
1292040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                    = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1293040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1294040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Equivalent = false;
1295040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      } else {
1296040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        // Kind mismatch.
1297040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        Equivalent = false;
1298040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
1299040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
1300040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1301ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    if (!Equivalent) {
1302ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      // Note that these two declarations are not equivalent (and we already
1303ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      // know about it).
1304ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1305ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                               D2->getCanonicalDecl()));
1306ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      return true;
1307ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    }
130873dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    // FIXME: Check other declaration kinds!
130973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  }
131073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
131173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return false;
131273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor}
131373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor
131473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor//----------------------------------------------------------------------------
13151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor// Import Types
13161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor//----------------------------------------------------------------------------
13171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1318f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitType(const Type *T) {
131989cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor  Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
132089cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor    << T->getTypeClassName();
132189cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor  return QualType();
132289cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor}
132389cc9d6e2aaf794fbd2c228a3755c19062ca0ba0Douglas Gregor
1324f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
13251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  switch (T->getKind()) {
13261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Void: return Importer.getToContext().VoidTy;
13271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Bool: return Importer.getToContext().BoolTy;
13281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char_U:
13301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // The context we're importing from has an unsigned 'char'. If we're
13311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // importing into a context with a signed 'char', translate to
13321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // 'unsigned char' instead.
13331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (Importer.getToContext().getLangOptions().CharIsSigned)
13341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return Importer.getToContext().UnsignedCharTy;
13351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().CharTy;
13371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
13391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char16:
13411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++!
13421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().Char16Ty;
13431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char32:
13451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++!
13461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().Char32Ty;
13471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
13491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
13501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
13511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ULongLong:
13521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().UnsignedLongLongTy;
13531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
13541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Char_S:
13561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // The context we're importing from has an unsigned 'char'. If we're
13571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // importing into a context with a signed 'char', translate to
13581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // 'unsigned char' instead.
13591b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Importer.getToContext().getLangOptions().CharIsSigned)
13601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return Importer.getToContext().SignedCharTy;
13611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().CharTy;
13631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
13653f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::WChar_S:
13663f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::WChar_U:
13671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: If not in C++, shall we translate to the C equivalent of
13681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // wchar_t?
13691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().WCharTy;
13701b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13711b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Short : return Importer.getToContext().ShortTy;
13721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Int : return Importer.getToContext().IntTy;
13731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Long : return Importer.getToContext().LongTy;
13741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
13751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
13761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Float: return Importer.getToContext().FloatTy;
13771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Double: return Importer.getToContext().DoubleTy;
13781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
13791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::NullPtr:
13811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports C++0x!
13821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().NullPtrTy;
13831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
13851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
13861de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  case BuiltinType::UnknownAny: return Importer.getToContext().UnknownAnyTy;
1387864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  case BuiltinType::BoundMember: return Importer.getToContext().BoundMemberTy;
13881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCId:
13901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: Make sure that the "to" context supports Objective-C!
13911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinIdTy;
13921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCClass:
13941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinClassTy;
13951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
13961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case BuiltinType::ObjCSel:
13971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Importer.getToContext().ObjCBuiltinSelTy;
13981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
13991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return QualType();
14011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1403f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
14041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getComplexType(ToElementType);
14091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1411f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
14121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
14131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
14141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getPointerType(ToPointeeType);
14171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1419f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
14201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for blocks support in "to" context.
14211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
14221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
14231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getBlockPointerType(ToPointeeType);
14261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1428f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType
1429f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
14301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++ support in "to" context.
14311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
14321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
14331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getLValueReferenceType(ToPointeeType);
14361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1438f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType
1439f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
14401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++0x support in "to" context.
14411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
14421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
14431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getRValueReferenceType(ToPointeeType);
14461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1448f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
14491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: Check for C++ support in "to" context.
14501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
14511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
14521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
14551b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getMemberPointerType(ToPointeeType,
14561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      ClassType.getTypePtr());
14571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1459f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
14601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14621b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getConstantArrayType(ToElementType,
14651b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSize(),
14661b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSizeModifier(),
14671b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->getIndexTypeCVRQualifiers());
14681b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14691b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1470f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType
1471f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
14721b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getIncompleteArrayType(ToElementType,
14771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                        T->getSizeModifier(),
14781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                T->getIndexTypeCVRQualifiers());
14791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1481f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
14821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
14841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *Size = Importer.Import(T->getSizeExpr());
14871b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!Size)
14881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
14891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
14901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  SourceRange Brackets = Importer.Import(T->getBracketsRange());
14911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getVariableArrayType(ToElementType, Size,
14921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      T->getSizeModifier(),
14931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                T->getIndexTypeCVRQualifiers(),
14941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                      Brackets);
14951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
14961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1497f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
14981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
14991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
15001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getVectorType(ToElementType,
15031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               T->getNumElements(),
1504e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                               T->getVectorKind());
15051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1507f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
15081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToElementType = Importer.Import(T->getElementType());
15091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToElementType.isNull())
15101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getExtVectorType(ToElementType,
15131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                                  T->getNumElements());
15141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1516f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType
1517f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
15181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // FIXME: What happens if we're importing a function without a prototype
15191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // into C++? Should we make it variadic?
15201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToResultType = Importer.Import(T->getResultType());
15211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToResultType.isNull())
15221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
1523264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola
15241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getFunctionNoProtoType(ToResultType,
1525264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                        T->getExtInfo());
15261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1528f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
15291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToResultType = Importer.Import(T->getResultType());
15301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToResultType.isNull())
15311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Import argument types
15345f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 4> ArgTypes;
15351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
15361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                         AEnd = T->arg_type_end();
15371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       A != AEnd; ++A) {
15381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType ArgType = Importer.Import(*A);
15391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (ArgType.isNull())
15401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
15411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ArgTypes.push_back(ArgType);
15421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
15431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Import exception types
15455f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 4> ExceptionTypes;
15461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  for (FunctionProtoType::exception_iterator E = T->exception_begin(),
15471b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                          EEnd = T->exception_end();
15481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       E != EEnd; ++E) {
15491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType ExceptionType = Importer.Import(*E);
15501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (ExceptionType.isNull())
15511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
15521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ExceptionTypes.push_back(ExceptionType);
15531b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
1554e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
1555e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1556e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  EPI.Exceptions = ExceptionTypes.data();
15571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1559e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                                 ArgTypes.size(), EPI);
15601b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15611b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15620aeb2890389ec1872e49a18fb2022bfb9f96578dSean CallananQualType ASTNodeImporter::VisitParenType(const ParenType *T) {
15630aeb2890389ec1872e49a18fb2022bfb9f96578dSean Callanan  QualType ToInnerType = Importer.Import(T->getInnerType());
15640aeb2890389ec1872e49a18fb2022bfb9f96578dSean Callanan  if (ToInnerType.isNull())
15650aeb2890389ec1872e49a18fb2022bfb9f96578dSean Callanan    return QualType();
15660aeb2890389ec1872e49a18fb2022bfb9f96578dSean Callanan
15670aeb2890389ec1872e49a18fb2022bfb9f96578dSean Callanan  return Importer.getToContext().getParenType(ToInnerType);
15680aeb2890389ec1872e49a18fb2022bfb9f96578dSean Callanan}
15690aeb2890389ec1872e49a18fb2022bfb9f96578dSean Callanan
1570f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
1571162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  TypedefNameDecl *ToDecl
1572162e1c1b487352434552147967c3dd296ebee2f7Richard Smith             = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
15731b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
15741b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15751b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15761b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeDeclType(ToDecl);
15771b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1579f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
15801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
15811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToExpr)
15821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15831b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15841b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeOfExprType(ToExpr);
15851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1587f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
15881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
15891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToUnderlyingType.isNull())
15901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
15911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
15921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTypeOfType(ToUnderlyingType);
15931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
15941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1595f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
159634b41d939a1328f484511c6002ba2456db879a29Richard Smith  // FIXME: Make sure that the "to" context supports C++0x!
15971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
15981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToExpr)
15991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
16001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
16011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getDecltypeType(ToExpr);
16021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
16031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1604ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1605ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  QualType ToBaseType = Importer.Import(T->getBaseType());
1606ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1607ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1608ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    return QualType();
1609ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
1610ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  return Importer.getToContext().getUnaryTransformType(ToBaseType,
1611ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                                       ToUnderlyingType,
1612ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                                       T->getUTTKind());
1613ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
1614ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
161534b41d939a1328f484511c6002ba2456db879a29Richard SmithQualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
161634b41d939a1328f484511c6002ba2456db879a29Richard Smith  // FIXME: Make sure that the "to" context supports C++0x!
161734b41d939a1328f484511c6002ba2456db879a29Richard Smith  QualType FromDeduced = T->getDeducedType();
161834b41d939a1328f484511c6002ba2456db879a29Richard Smith  QualType ToDeduced;
161934b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (!FromDeduced.isNull()) {
162034b41d939a1328f484511c6002ba2456db879a29Richard Smith    ToDeduced = Importer.Import(FromDeduced);
162134b41d939a1328f484511c6002ba2456db879a29Richard Smith    if (ToDeduced.isNull())
162234b41d939a1328f484511c6002ba2456db879a29Richard Smith      return QualType();
162334b41d939a1328f484511c6002ba2456db879a29Richard Smith  }
162434b41d939a1328f484511c6002ba2456db879a29Richard Smith
162534b41d939a1328f484511c6002ba2456db879a29Richard Smith  return Importer.getToContext().getAutoType(ToDeduced);
162634b41d939a1328f484511c6002ba2456db879a29Richard Smith}
162734b41d939a1328f484511c6002ba2456db879a29Richard Smith
1628f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
16291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  RecordDecl *ToDecl
16301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
16311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
16321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
16331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
16341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTagDeclType(ToDecl);
16351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
16361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1637f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
16381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  EnumDecl *ToDecl
16391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
16401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!ToDecl)
16411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
16421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
16431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return Importer.getToContext().getTagDeclType(ToDecl);
16441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
16451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1646d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorQualType ASTNodeImporter::VisitTemplateSpecializationType(
1647f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall                                       const TemplateSpecializationType *T) {
1648d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1649d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (ToTemplate.isNull())
1650d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return QualType();
1651d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
16525f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 2> ToTemplateArgs;
1653d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1654d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return QualType();
1655d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1656d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  QualType ToCanonType;
1657d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (!QualType(T, 0).isCanonical()) {
1658d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualType FromCanonType
1659d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1660d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ToCanonType =Importer.Import(FromCanonType);
1661d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToCanonType.isNull())
1662d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return QualType();
1663d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1664d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1665d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                         ToTemplateArgs.data(),
1666d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                         ToTemplateArgs.size(),
1667d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                               ToCanonType);
1668d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1669d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1670f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
1671465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  NestedNameSpecifier *ToQualifier = 0;
1672465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  // Note: the qualifier in an ElaboratedType is optional.
1673465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (T->getQualifier()) {
1674465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ToQualifier = Importer.Import(T->getQualifier());
1675465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    if (!ToQualifier)
1676465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      return QualType();
1677465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
16781b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
16791b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToNamedType = Importer.Import(T->getNamedType());
16801b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToNamedType.isNull())
16811b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
16821b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1683465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return Importer.getToContext().getElaboratedType(T->getKeyword(),
1684465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                                                   ToQualifier, ToNamedType);
16851b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
16861b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1687f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
16881b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  ObjCInterfaceDecl *Class
16891b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
16901b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!Class)
16911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
16921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1693c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCInterfaceType(Class);
1694c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
1695c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
1696f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1697c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  QualType ToBaseType = Importer.Import(T->getBaseType());
1698c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ToBaseType.isNull())
1699c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType();
1700c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
17015f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ObjCProtocolDecl *, 4> Protocols;
1702c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator P = T->qual_begin(),
17031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                     PEnd = T->qual_end();
17041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor       P != PEnd; ++P) {
17051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    ObjCProtocolDecl *Protocol
17061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
17071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (!Protocol)
17081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return QualType();
17091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    Protocols.push_back(Protocol);
17101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
17111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1712c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCObjectType(ToBaseType,
1713c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                                   Protocols.data(),
1714c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                                   Protocols.size());
17151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
17161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1717f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallQualType
1718f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
17191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  QualType ToPointeeType = Importer.Import(T->getPointeeType());
17201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToPointeeType.isNull())
17211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
17221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1723c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
17241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
17251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
1726089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor//----------------------------------------------------------------------------
1727089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor// Import Declarations
1728089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor//----------------------------------------------------------------------------
1729a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregorbool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1730a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      DeclContext *&LexicalDC,
1731a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      DeclarationName &Name,
1732a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                      SourceLocation &Loc) {
1733089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Import the context of this declaration.
1734a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DC = Importer.ImportContext(D->getDeclContext());
1735089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  if (!DC)
1736a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return true;
1737a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1738a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  LexicalDC = DC;
17399bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
17409bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
17419bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    if (!LexicalDC)
1742a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      return true;
17439bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  }
1744a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1745089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Import the name of this declaration.
1746a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Name = Importer.Import(D->getDeclName());
1747089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  if (D->getDeclName() && !Name)
1748a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return true;
1749a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
1750a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the location of this declaration.
1751a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Loc = Importer.Import(D->getLocation());
1752a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  return false;
1753a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
1754a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
17551cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregorvoid ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
17561cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (!FromD)
17571cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    return;
17581cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
17591cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (!ToD) {
17601cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    ToD = Importer.Import(FromD);
17611cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    if (!ToD)
17621cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor      return;
17631cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  }
17641cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
17651cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
17661cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
17671cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor      if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
17681cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor        ImportDefinition(FromRecord, ToRecord);
17691cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor      }
17701cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    }
17711cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    return;
17721cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  }
17731cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
17741cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
17751cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
17761cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor      if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
17771cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor        ImportDefinition(FromEnum, ToEnum);
17781cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor      }
17791cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    }
17801cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    return;
17811cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  }
17821cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor}
17831cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
17842577743c5650c646fb705df01403707e94f2df04Abramo Bagnaravoid
17852577743c5650c646fb705df01403707e94f2df04Abramo BagnaraASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
17862577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                          DeclarationNameInfo& To) {
17872577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  // NOTE: To.Name and To.Loc are already imported.
17882577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  // We only have to import To.LocInfo.
17892577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  switch (To.getName().getNameKind()) {
17902577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::Identifier:
17912577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::ObjCZeroArgSelector:
17922577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::ObjCOneArgSelector:
17932577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::ObjCMultiArgSelector:
17942577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXUsingDirective:
17952577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
17962577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
17972577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXOperatorName: {
17982577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    SourceRange Range = From.getCXXOperatorNameRange();
17992577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    To.setCXXOperatorNameRange(Importer.Import(Range));
18002577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
18012577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
18022577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXLiteralOperatorName: {
18032577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
18042577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
18052577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
18062577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
18072577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXConstructorName:
18082577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXDestructorName:
18092577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  case DeclarationName::CXXConversionFunctionName: {
18102577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
18112577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    To.setNamedTypeInfo(Importer.Import(FromTInfo));
18122577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return;
18132577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
1814b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Unknown name kind.");
18152577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  }
18162577743c5650c646fb705df01403707e94f2df04Abramo Bagnara}
18172577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
1818d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregorvoid ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1819d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor  if (Importer.isMinimalImport() && !ForceImport) {
18208cc4fd795e01d50a7a7c96f4c0356d23b00d9349Sean Callanan    Importer.ImportContext(FromDC);
1821d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    return;
1822d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor  }
1823d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor
1824083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1825083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor                               FromEnd = FromDC->decls_end();
1826083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor       From != FromEnd;
1827083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor       ++From)
1828083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor    Importer.Import(*From);
1829083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor}
1830083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor
18311cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregorbool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
18321cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor                                       bool ForceImport) {
18331cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (To->getDefinition() || To->isBeingDefined())
1834d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return false;
1835d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1836d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  To->startDefinition();
1837d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1838d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Add base classes.
1839d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1840d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1841d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
18425f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<CXXBaseSpecifier *, 4> Bases;
1843d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (CXXRecordDecl::base_class_iterator
1844d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                  Base1 = FromCXX->bases_begin(),
1845d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor            FromBaseEnd = FromCXX->bases_end();
1846d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor         Base1 != FromBaseEnd;
1847d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor         ++Base1) {
1848d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      QualType T = Importer.Import(Base1->getType());
1849d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (T.isNull())
1850c04d9d1be026cb201a716df1cd28a88878958beeDouglas Gregor        return true;
1851f90b27ad077c3339b62befc892382845339f9490Douglas Gregor
1852f90b27ad077c3339b62befc892382845339f9490Douglas Gregor      SourceLocation EllipsisLoc;
1853f90b27ad077c3339b62befc892382845339f9490Douglas Gregor      if (Base1->isPackExpansion())
1854f90b27ad077c3339b62befc892382845339f9490Douglas Gregor        EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
18551cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
18561cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor      // Ensure that we have a definition for the base.
18571cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor      ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
18581cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
1859d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      Bases.push_back(
1860d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                    new (Importer.getToContext())
1861d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                      CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1862d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                       Base1->isVirtual(),
1863d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                       Base1->isBaseOfClass(),
1864d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                       Base1->getAccessSpecifierAsWritten(),
1865f90b27ad077c3339b62befc892382845339f9490Douglas Gregor                                   Importer.Import(Base1->getTypeSourceInfo()),
1866f90b27ad077c3339b62befc892382845339f9490Douglas Gregor                                       EllipsisLoc));
1867d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
1868d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Bases.empty())
1869d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      ToCXX->setBases(Bases.data(), Bases.size());
1870d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1871d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1872673e775beba71a69dbab4fcd733a84f4cfe2ebfcSean Callanan  ImportDeclContext(From, ForceImport);
1873d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  To->completeDefinition();
1874c04d9d1be026cb201a716df1cd28a88878958beeDouglas Gregor  return false;
1875d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1876d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
18771cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregorbool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
18781cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor                                       bool ForceImport) {
18791cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (To->getDefinition() || To->isBeingDefined())
18801cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    return false;
18811cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
18821cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  To->startDefinition();
18831cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
18841cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
18851cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (T.isNull())
18861cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    return true;
18871cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
18881cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  QualType ToPromotionType = Importer.Import(From->getPromotionType());
18891cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (ToPromotionType.isNull())
18901cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    return true;
18911cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
18921cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  ImportDeclContext(From, ForceImport);
18931cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
18941cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  // FIXME: we might need to merge the number of positive or negative bits
18951cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  // if the enumerator lists don't match.
18961cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  To->completeDefinition(T, ToPromotionType,
18971cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor                         From->getNumPositiveBits(),
18981cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor                         From->getNumNegativeBits());
18991cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  return false;
19001cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor}
19011cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
1902040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorTemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1903040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                TemplateParameterList *Params) {
19045f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<NamedDecl *, 4> ToParams;
1905040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  ToParams.reserve(Params->size());
1906040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  for (TemplateParameterList::iterator P = Params->begin(),
1907040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                    PEnd = Params->end();
1908040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor       P != PEnd; ++P) {
1909040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *To = Importer.Import(*P);
1910040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!To)
1911040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return 0;
1912040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1913040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    ToParams.push_back(cast<NamedDecl>(To));
1914040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
1915040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1916040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return TemplateParameterList::Create(Importer.getToContext(),
1917040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       Importer.Import(Params->getTemplateLoc()),
1918040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       Importer.Import(Params->getLAngleLoc()),
1919040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       ToParams.data(), ToParams.size(),
1920040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                       Importer.Import(Params->getRAngleLoc()));
1921040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
1922040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
1923d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorTemplateArgument
1924d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1925d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  switch (From.getKind()) {
1926d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Null:
1927d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument();
1928d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1929d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Type: {
1930d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualType ToType = Importer.Import(From.getAsType());
1931d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToType.isNull())
1932d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1933d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(ToType);
1934d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1935d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1936d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Integral: {
1937d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualType ToType = Importer.Import(From.getIntegralType());
1938d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToType.isNull())
1939d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1940d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(*From.getAsIntegral(), ToType);
1941d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1942d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1943d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Declaration:
1944d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Decl *To = Importer.Import(From.getAsDecl()))
1945d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument(To);
1946d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument();
1947d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1948d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Template: {
1949d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1950d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ToTemplate.isNull())
1951d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1952d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1953d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(ToTemplate);
1954d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1955a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
1956a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  case TemplateArgument::TemplateExpansion: {
1957a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    TemplateName ToTemplate
1958a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      = Importer.Import(From.getAsTemplateOrTemplatePattern());
1959a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    if (ToTemplate.isNull())
1960a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      return TemplateArgument();
1961a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
19622be29f423acad3bbe39099a78db2805acb5bdf17Douglas Gregor    return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
1963a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  }
1964a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
1965d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Expression:
1966d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1967d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument(ToExpr);
1968d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument();
1969d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1970d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateArgument::Pack: {
19715f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<TemplateArgument, 2> ToPack;
1972d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ToPack.reserve(From.pack_size());
1973d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1974d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateArgument();
1975d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1976d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    TemplateArgument *ToArgs
1977d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1978d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1979d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateArgument(ToArgs, ToPack.size());
1980d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1981d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1982d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1983d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm_unreachable("Invalid template argument kind");
1984d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return TemplateArgument();
1985d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
1986d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1987d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregorbool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1988d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                              unsigned NumFromArgs,
19895f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                              SmallVectorImpl<TemplateArgument> &ToArgs) {
1990d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  for (unsigned I = 0; I != NumFromArgs; ++I) {
1991d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1992d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (To.isNull() && !FromArgs[I].isNull())
1993d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return true;
1994d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1995d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ToArgs.push_back(To);
1996d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
1997d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
1998d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return false;
1999d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
2000d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
200196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregorbool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
200273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                        RecordDecl *ToRecord) {
2003bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
200473dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Importer.getToContext(),
2005ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getNonEquivalentDecls());
2006bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
200796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
200896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
200936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregorbool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
2010bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
201173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor                                   Importer.getToContext(),
2012ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                   Importer.getNonEquivalentDecls());
2013bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
201436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
201536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
2016040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregorbool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2017040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                        ClassTemplateDecl *To) {
2018040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2019040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                   Importer.getToContext(),
2020040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                   Importer.getNonEquivalentDecls());
2021040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return Ctx.IsStructurallyEquivalent(From, To);
2022040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
2023040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
2024a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitDecl(Decl *D) {
2025a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2026a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    << D->getDeclKindName();
2027a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  return 0;
2028a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
2029a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2030788c62d1e87bfb596078817237f672a5f000999aDouglas GregorDecl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2031788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  // Import the major distinguishing characteristics of this namespace.
2032788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  DeclContext *DC, *LexicalDC;
2033788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  DeclarationName Name;
2034788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  SourceLocation Loc;
2035788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2036788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    return 0;
2037788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
2038788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  NamespaceDecl *MergeWithNamespace = 0;
2039788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (!Name) {
2040788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // This is an anonymous namespace. Adopt an existing anonymous
2041788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // namespace if we can.
2042788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // FIXME: Not testable.
2043788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2044788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      MergeWithNamespace = TU->getAnonymousNamespace();
2045788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    else
2046788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2047788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  } else {
20485f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<NamedDecl *, 4> ConflictingDecls;
2049788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2050788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor         Lookup.first != Lookup.second;
2051788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor         ++Lookup.first) {
20520d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
2053788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        continue;
2054788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
2055788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
2056788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        MergeWithNamespace = FoundNS;
2057788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        ConflictingDecls.clear();
2058788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        break;
2059788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      }
2060788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
2061788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
2062788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
2063788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
2064788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (!ConflictingDecls.empty()) {
20650d6b1640eb4d1a4a0203235cfdfcdaf3335af36dJohn McCall      Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
2066788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                         ConflictingDecls.data(),
2067788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor                                         ConflictingDecls.size());
2068788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
2069788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  }
2070788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
2071788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  // Create the "to" namespace, if needed.
2072788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  NamespaceDecl *ToNamespace = MergeWithNamespace;
2073788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  if (!ToNamespace) {
2074acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara    ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
2075acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara                                        Importer.Import(D->getLocStart()),
2076acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara                                        Loc, Name.getAsIdentifierInfo());
2077788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    ToNamespace->setLexicalDeclContext(LexicalDC);
2078788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    LexicalDC->addDecl(ToNamespace);
2079788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
2080788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // If this is an anonymous namespace, register it as the anonymous
2081788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    // namespace within its context.
2082788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    if (!Name) {
2083788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2084788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        TU->setAnonymousNamespace(ToNamespace);
2085788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor      else
2086788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor        cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2087788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor    }
2088788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  }
2089788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  Importer.Imported(D, ToNamespace);
2090788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
2091788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  ImportDeclContext(D);
2092788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
2093788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor  return ToNamespace;
2094788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor}
2095788c62d1e87bfb596078817237f672a5f000999aDouglas Gregor
2096162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
20979e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // Import the major distinguishing characteristics of this typedef.
20989e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  DeclContext *DC, *LexicalDC;
20999e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  DeclarationName Name;
21009e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  SourceLocation Loc;
21019e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
21029e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    return 0;
21039e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
21049e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // If this typedef is not in block scope, determine whether we've
21059e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // seen a typedef with the same name (that we can merge with) or any
21069e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // other entity by that name (which name lookup could conflict with).
21079e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  if (!DC->isFunctionOrMethod()) {
21085f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<NamedDecl *, 4> ConflictingDecls;
21099e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
21109e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
21119e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor         Lookup.first != Lookup.second;
21129e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor         ++Lookup.first) {
21139e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
21149e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor        continue;
2115162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (TypedefNameDecl *FoundTypedef =
2116162e1c1b487352434552147967c3dd296ebee2f7Richard Smith            dyn_cast<TypedefNameDecl>(*Lookup.first)) {
2117ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2118ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                            FoundTypedef->getUnderlyingType()))
21195ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor          return Importer.Imported(D, FoundTypedef);
21209e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      }
21219e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
21229e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
21239e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    }
21249e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
21259e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    if (!ConflictingDecls.empty()) {
21269e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
21279e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                         ConflictingDecls.data(),
21289e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor                                         ConflictingDecls.size());
21299e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor      if (!Name)
21309e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor        return 0;
21319e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor    }
21329e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  }
21339e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
2134ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the underlying type of this typedef;
2135ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getUnderlyingType());
2136ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2137ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2138ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
21399e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  // Create the new typedef node.
21409e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2141344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara  SourceLocation StartL = Importer.Import(D->getLocStart());
2142162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  TypedefNameDecl *ToTypedef;
2143162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (IsAlias)
2144162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2145162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                    StartL, Loc,
2146162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                    Name.getAsIdentifierInfo(),
2147162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                    TInfo);
2148162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  else
2149162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2150162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                  StartL, Loc,
2151162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                  Name.getAsIdentifierInfo(),
2152162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                  TInfo);
2153325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToTypedef->setAccess(D->getAccess());
21549e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  ToTypedef->setLexicalDeclContext(LexicalDC);
21555ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToTypedef);
21569e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  LexicalDC->addDecl(ToTypedef);
2157ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
21589e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor  return ToTypedef;
21599e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor}
21609e5d996444e40fdf5cce44ee82bec0d4e3df3d56Douglas Gregor
2161162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2162162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2163162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
2164162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
2165162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2166162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2167162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
2168162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
216936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorDecl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
217036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the major distinguishing characteristics of this enum.
217136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclContext *DC, *LexicalDC;
217236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName Name;
217336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  SourceLocation Loc;
217436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
217536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
217636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
217736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Figure out what enum name we're looking for.
217836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  unsigned IDNS = Decl::IDNS_Tag;
217936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName SearchName = Name;
2180162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (!SearchName && D->getTypedefNameForAnonDecl()) {
2181162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
218236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    IDNS = Decl::IDNS_Ordinary;
218336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  } else if (Importer.getToContext().getLangOptions().CPlusPlus)
218436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    IDNS |= Decl::IDNS_Ordinary;
218536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
218636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // We may already have an enum of the same name; try to find and match it.
218736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!DC->isFunctionOrMethod() && SearchName) {
21885f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<NamedDecl *, 4> ConflictingDecls;
21893340e27fe15c52fe28fc722a98fd55d796c90442Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(SearchName);
219036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         Lookup.first != Lookup.second;
219136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++Lookup.first) {
219236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
219336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        continue;
219436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
219536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Decl *Found = *Lookup.first;
2196162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
219736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
219836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor          Found = Tag->getDecl();
219936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      }
220036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
220136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
22025ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor        if (IsStructuralMatch(D, FoundEnum))
22035ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor          return Importer.Imported(D, FoundEnum);
220436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      }
220536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
220636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
220736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
220836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
220936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (!ConflictingDecls.empty()) {
221036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
221136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.data(),
221236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.size());
221336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
221436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
221536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
221636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Create the enum declaration.
2217ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2218ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                  Importer.Import(D->getLocStart()),
2219ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                  Loc, Name.getAsIdentifierInfo(), 0,
2220a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                  D->isScoped(), D->isScopedUsingClassTag(),
2221a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                  D->isFixed());
2222b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
2223c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2224325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  D2->setAccess(D->getAccess());
222573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  D2->setLexicalDeclContext(LexicalDC);
222673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Importer.Imported(D, D2);
222773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  LexicalDC->addDecl(D2);
222836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
222936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the integer type.
223036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  QualType ToIntegerType = Importer.Import(D->getIntegerType());
223136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (ToIntegerType.isNull())
223236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
223373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  D2->setIntegerType(ToIntegerType);
223436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
223536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the definition
22361cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (D->isDefinition() && ImportDefinition(D, D2))
22371cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    return 0;
223836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
223973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return D2;
224036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
224136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
224296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas GregorDecl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
224396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // If this record has a definition in the translation unit we're coming from,
224496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // but this particular declaration is not that definition, import the
224596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // definition and map to that.
2246952b017601f9c82b51119c3a1600f1312a833db9Douglas Gregor  TagDecl *Definition = D->getDefinition();
224796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (Definition && Definition != D) {
224896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    Decl *ImportedDef = Importer.Import(Definition);
22495ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor    if (!ImportedDef)
22505ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor      return 0;
22515ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
22525ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor    return Importer.Imported(D, ImportedDef);
225396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
225496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
225596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Import the major distinguishing characteristics of this record.
225696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclContext *DC, *LexicalDC;
225796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName Name;
225896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  SourceLocation Loc;
225996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
226096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
226196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
226296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Figure out what structure name we're looking for.
226396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  unsigned IDNS = Decl::IDNS_Tag;
226496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName SearchName = Name;
2265162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (!SearchName && D->getTypedefNameForAnonDecl()) {
2266162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
226796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    IDNS = Decl::IDNS_Ordinary;
226896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  } else if (Importer.getToContext().getLangOptions().CPlusPlus)
226996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    IDNS |= Decl::IDNS_Ordinary;
227096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
227196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // We may already have a record of the same name; try to find and match it.
2272e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor  RecordDecl *AdoptDecl = 0;
227396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!DC->isFunctionOrMethod() && SearchName) {
22745f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<NamedDecl *, 4> ConflictingDecls;
22753340e27fe15c52fe28fc722a98fd55d796c90442Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(SearchName);
227696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         Lookup.first != Lookup.second;
227796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor         ++Lookup.first) {
227896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
227996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor        continue;
228096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
228196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Decl *Found = *Lookup.first;
2282162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
228396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor        if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
228496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor          Found = Tag->getDecl();
228596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
228696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
228796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
2288e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2289e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2290e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // The record types structurally match, or the "from" translation
2291e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // unit only had a forward declaration anyway; call it the same
2292e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // function.
2293e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor            // FIXME: For C++, we should also merge methods here.
22945ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor            return Importer.Imported(D, FoundDef);
2295e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          }
2296e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        } else {
2297e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          // We have a forward declaration of this type, so adopt that forward
2298e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          // declaration rather than building a new one.
2299e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          AdoptDecl = FoundRecord;
2300e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor          continue;
2301e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor        }
230296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      }
230396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
230496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
230596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
230696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
230796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    if (!ConflictingDecls.empty()) {
230896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
230996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         ConflictingDecls.data(),
231096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         ConflictingDecls.size());
231196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
231296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
231396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
231496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Create the record declaration.
231573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  RecordDecl *D2 = AdoptDecl;
2316ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation StartLoc = Importer.Import(D->getLocStart());
231773dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  if (!D2) {
23185250f27420386452a21692a6292c99ee7febdac4John McCall    if (isa<CXXRecordDecl>(D)) {
231973dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2320e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor                                                   D->getTagKind(),
2321ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                   DC, StartLoc, Loc,
2322ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                   Name.getAsIdentifierInfo());
232373dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      D2 = D2CXX;
2324325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor      D2->setAccess(D->getAccess());
2325e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor    } else {
232673dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor      D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
2327ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                              DC, StartLoc, Loc, Name.getAsIdentifierInfo());
232896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    }
2329c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor
2330c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
233173dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    D2->setLexicalDeclContext(LexicalDC);
233273dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor    LexicalDC->addDecl(D2);
233396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  }
23345ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
233573dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  Importer.Imported(D, D2);
2336e72b5dc556805ea4c91f0ae6d8f8404fc341b687Douglas Gregor
2337d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D->isDefinition() && ImportDefinition(D, D2))
2338d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
233996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
234073dc30b71e218ba2b776b10d07dc2aff09cb2c47Douglas Gregor  return D2;
234196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
234296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
234336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorDecl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
234436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Import the major distinguishing characteristics of this enumerator.
234536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclContext *DC, *LexicalDC;
234636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  DeclarationName Name;
234736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  SourceLocation Loc;
2348ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
234936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
2350ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2351ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2352ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2353ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2354ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
235536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // Determine whether there are any other declarations with the same name and
235636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  // in the same context.
235736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!LexicalDC->isFunctionOrMethod()) {
23585f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<NamedDecl *, 4> ConflictingDecls;
235936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
236036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
236136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         Lookup.first != Lookup.second;
236236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor         ++Lookup.first) {
236336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
236436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        continue;
236536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
236636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      ConflictingDecls.push_back(*Lookup.first);
236736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
236836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
236936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    if (!ConflictingDecls.empty()) {
237036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
237136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.data(),
237236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                                         ConflictingDecls.size());
237336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor      if (!Name)
237436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor        return 0;
237536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    }
237636ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  }
237736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
237836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  Expr *Init = Importer.Import(D->getInitExpr());
237936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (D->getInitExpr() && !Init)
238036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
238136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
238236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  EnumConstantDecl *ToEnumerator
238336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
238436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                               Name.getAsIdentifierInfo(), T,
238536ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor                               Init, D->getInitVal());
2386325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToEnumerator->setAccess(D->getAccess());
238736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  ToEnumerator->setLexicalDeclContext(LexicalDC);
23885ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToEnumerator);
238936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  LexicalDC->addDecl(ToEnumerator);
239036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  return ToEnumerator;
239136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
239296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
2393a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2394a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the major distinguishing characteristics of this function.
2395a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC, *LexicalDC;
2396a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclarationName Name;
2397a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc;
2398ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2399089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    return 0;
24002577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
2401a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Try to find a function in our own ("to") context with the same name, same
2402a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // type, and in the same context as the function we're importing.
2403a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  if (!LexicalDC->isFunctionOrMethod()) {
24045f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<NamedDecl *, 4> ConflictingDecls;
2405a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
2406a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2407a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor         Lookup.first != Lookup.second;
2408a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor         ++Lookup.first) {
2409a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2410a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        continue;
2411a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2412a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2413a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        if (isExternalLinkage(FoundFunction->getLinkage()) &&
2414a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            isExternalLinkage(D->getLinkage())) {
2415ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          if (Importer.IsStructurallyEquivalent(D->getType(),
2416ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                FoundFunction->getType())) {
2417a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            // FIXME: Actually try to merge the body and other attributes.
24185ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor            return Importer.Imported(D, FoundFunction);
2419a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          }
2420a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2421a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // FIXME: Check for overloading more carefully, e.g., by boosting
2422a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Sema::IsOverload out to the AST library.
2423a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2424a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Function overloading is okay in C++.
2425a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          if (Importer.getToContext().getLangOptions().CPlusPlus)
2426a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            continue;
2427a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2428a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          // Complain about inconsistent function types.
2429a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
2430ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            << Name << D->getType() << FoundFunction->getType();
2431a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor          Importer.ToDiag(FoundFunction->getLocation(),
2432a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                          diag::note_odr_value_here)
2433a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor            << FoundFunction->getType();
2434a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        }
2435a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      }
2436a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2437a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
2438a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    }
2439a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2440a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    if (!ConflictingDecls.empty()) {
2441a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
2442a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                         ConflictingDecls.data(),
2443a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                         ConflictingDecls.size());
2444a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      if (!Name)
2445a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor        return 0;
2446a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    }
2447a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
2448ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
24492577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo(Name, Loc);
24502577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  // Import additional name location/type info.
24512577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
24522577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
2453ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
2454ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2455ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2456ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2457a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2458a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the function parameters.
24595f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 8> Parameters;
2460a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2461a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor       P != PEnd; ++P) {
2462a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2463a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    if (!ToP)
2464a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor      return 0;
2465a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2466a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    Parameters.push_back(ToP);
2467a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
2468a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2469a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Create the imported function.
2470a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2471c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  FunctionDecl *ToFunction = 0;
2472c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2473c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2474c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            cast<CXXRecordDecl>(DC),
2475ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
24762577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                            NameInfo, T, TInfo,
2477c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            FromConstructor->isExplicit(),
2478c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                            D->isInlineSpecified(),
2479af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                            D->isImplicit(),
2480af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                            D->isConstexpr());
2481c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else if (isa<CXXDestructorDecl>(D)) {
2482c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2483c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           cast<CXXRecordDecl>(DC),
2484ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           D->getInnerLocStart(),
2485b41d899a6023385c00a61eb9dd3e44db9dc7994eCraig Silverstein                                           NameInfo, T, TInfo,
2486c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isInlineSpecified(),
2487c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isImplicit());
2488c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else if (CXXConversionDecl *FromConversion
2489c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           = dyn_cast<CXXConversionDecl>(D)) {
2490c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor    ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2491c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           cast<CXXRecordDecl>(DC),
2492ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           D->getInnerLocStart(),
24932577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                           NameInfo, T, TInfo,
2494c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                           D->isInlineSpecified(),
2495f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                           FromConversion->isExplicit(),
2496af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                           D->isConstexpr(),
2497f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                           Importer.Import(D->getLocEnd()));
24980629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor  } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
24990629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor    ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
25000629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       cast<CXXRecordDecl>(DC),
2501ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       D->getInnerLocStart(),
25020629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       NameInfo, T, TInfo,
25030629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       Method->isStatic(),
25040629cbe86b6d890076548778ed8597ee298adcbaDouglas Gregor                                       Method->getStorageClassAsWritten(),
2505f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                       Method->isInlineSpecified(),
2506af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                       D->isConstexpr(),
2507f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                       Importer.Import(D->getLocEnd()));
2508c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  } else {
25092577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2510ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                      D->getInnerLocStart(),
25112577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                      NameInfo, T, TInfo, D->getStorageClass(),
251216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                      D->getStorageClassAsWritten(),
2513c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor                                      D->isInlineSpecified(),
2514af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                      D->hasWrittenPrototype(),
2515af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                      D->isConstexpr());
2516c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  }
2517b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2518b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Import the qualifier, if any.
2519c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2520325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToFunction->setAccess(D->getAccess());
2521c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToFunction->setLexicalDeclContext(LexicalDC);
2522f2eca2cf302c50b79891f24b3861d64ea9263831John McCall  ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2523f2eca2cf302c50b79891f24b3861d64ea9263831John McCall  ToFunction->setTrivial(D->isTrivial());
2524f2eca2cf302c50b79891f24b3861d64ea9263831John McCall  ToFunction->setPure(D->isPure());
2525c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Importer.Imported(D, ToFunction);
2526a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2527a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Set the parameters.
2528a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
2529c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Parameters[I]->setOwningFunction(ToFunction);
2530c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToFunction->addDecl(Parameters[I]);
2531a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  }
25324278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  ToFunction->setParams(Parameters);
2533a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2534a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // FIXME: Other bits to merge?
253581134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor
253681134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor  // Add this function to the lexical context.
253781134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor  LexicalDC->addDecl(ToFunction);
253881134ad7a056e45b7cb4ee1b562ab8c8413d65b7Douglas Gregor
2539c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToFunction;
2540a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
2541a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2542c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2543c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitFunctionDecl(D);
2544c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2545c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
2546c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2547c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
2548c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2549c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
2550c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2551c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
2552c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2553c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
2554c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas GregorDecl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2555c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor  return VisitCXXMethodDecl(D);
2556c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor}
2557c144f3534e2f7a70ea9ebf8dd83090a2883d61fbDouglas Gregor
255896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas GregorDecl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
255996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  // Import the major distinguishing characteristics of a variable.
256096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclContext *DC, *LexicalDC;
256196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  DeclarationName Name;
256296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  SourceLocation Loc;
2563ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2564ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2565ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2566ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
2567ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2568ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
256996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
257096a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
257196a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
257296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  Expr *BitWidth = Importer.Import(D->getBitWidth());
257396a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  if (!BitWidth && D->getBitWidth())
257496a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor    return 0;
257596a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
2576ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2577ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                         Importer.Import(D->getInnerLocStart()),
257896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor                                         Loc, Name.getAsIdentifierInfo(),
25797a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         T, TInfo, BitWidth, D->isMutable(),
25807a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         D->hasInClassInitializer());
2581325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToField->setAccess(D->getAccess());
258296a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  ToField->setLexicalDeclContext(LexicalDC);
25837a614d8380297fcd2bc23986241905d97222948cRichard Smith  if (ToField->hasInClassInitializer())
25847a614d8380297fcd2bc23986241905d97222948cRichard Smith    ToField->setInClassInitializer(D->getInClassInitializer());
25855ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToField);
258696a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  LexicalDC->addDecl(ToField);
258796a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor  return ToField;
258896a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor}
258996a01b4acf76fb8fe1e05341a97a27b39fb0b914Douglas Gregor
259087c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetDecl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
259187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  // Import the major distinguishing characteristics of a variable.
259287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  DeclContext *DC, *LexicalDC;
259387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  DeclarationName Name;
259487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  SourceLocation Loc;
259587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
259687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    return 0;
259787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
259887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  // Import the type.
259987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  QualType T = Importer.Import(D->getType());
260087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (T.isNull())
260187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    return 0;
260287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
260387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  NamedDecl **NamedChain =
260487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
260587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
260687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  unsigned i = 0;
260787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
260887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       PE = D->chain_end(); PI != PE; ++PI) {
260987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    Decl* D = Importer.Import(*PI);
261087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    if (!D)
261187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet      return 0;
261287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    NamedChain[i++] = cast<NamedDecl>(D);
261387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  }
261487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
261587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
261687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                         Importer.getToContext(), DC,
261787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                         Loc, Name.getAsIdentifierInfo(), T,
261887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                         NamedChain, D->getChainingSize());
261987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  ToIndirectField->setAccess(D->getAccess());
262087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  ToIndirectField->setLexicalDeclContext(LexicalDC);
262187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  Importer.Imported(D, ToIndirectField);
262287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  LexicalDC->addDecl(ToIndirectField);
262387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return ToIndirectField;
262487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
262587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
26262e55e3af2f6d6c0509495357fade95105dd144cdDouglas GregorDecl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
26272e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Import the major distinguishing characteristics of an ivar.
26282e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  DeclContext *DC, *LexicalDC;
26292e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  DeclarationName Name;
26302e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  SourceLocation Loc;
26312e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
26322e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
26332e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
26342e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Determine whether we've already imported this ivar
26352e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
26362e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor       Lookup.first != Lookup.second;
26372e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor       ++Lookup.first) {
26382e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
26392e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (Importer.IsStructurallyEquivalent(D->getType(),
26402e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                            FoundIvar->getType())) {
26412e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.Imported(D, FoundIvar);
26422e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        return FoundIvar;
26432e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      }
26442e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
26452e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
26462e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << Name << D->getType() << FoundIvar->getType();
26472e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
26482e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << FoundIvar->getType();
26492e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      return 0;
26502e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    }
26512e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  }
26522e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
26532e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  // Import the type.
26542e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  QualType T = Importer.Import(D->getType());
26552e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (T.isNull())
26562e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
26572e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
26582e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
26592e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  Expr *BitWidth = Importer.Import(D->getBitWidth());
26602e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  if (!BitWidth && D->getBitWidth())
26612e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    return 0;
26622e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
2663a06549226f45d5b72169a3d054415616dd1014a2Daniel Dunbar  ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2664a06549226f45d5b72169a3d054415616dd1014a2Daniel Dunbar                                              cast<ObjCContainerDecl>(DC),
2665ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       Importer.Import(D->getInnerLocStart()),
26662e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              Loc, Name.getAsIdentifierInfo(),
26672e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                                              T, TInfo, D->getAccessControl(),
2668ac0021ba802e193e0f9f8207768c7862c7603bc0Fariborz Jahanian                                              BitWidth, D->getSynthesize());
26692e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  ToIvar->setLexicalDeclContext(LexicalDC);
26702e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  Importer.Imported(D, ToIvar);
26712e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  LexicalDC->addDecl(ToIvar);
26722e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor  return ToIvar;
26732e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
26742e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor}
26752e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
2676a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2677a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the major distinguishing characteristics of a variable.
2678a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC, *LexicalDC;
2679a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclarationName Name;
2680a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc;
2681ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2682a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor    return 0;
2683089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2684089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Try to find a variable in our own ("to") context with the same name and
2685089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // in the same context as the variable we're importing.
26869bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (D->isFileVarDecl()) {
2687089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    VarDecl *MergeWithVar = 0;
26885f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<NamedDecl *, 4> ConflictingDecls;
2689089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    unsigned IDNS = Decl::IDNS_Ordinary;
26909bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2691089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor         Lookup.first != Lookup.second;
2692089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor         ++Lookup.first) {
2693089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2694089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        continue;
2695089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2696089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2697089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        // We have found a variable that we may need to merge with. Check it.
2698089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        if (isExternalLinkage(FoundVar->getLinkage()) &&
2699089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            isExternalLinkage(D->getLinkage())) {
2700ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          if (Importer.IsStructurallyEquivalent(D->getType(),
2701ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                                                FoundVar->getType())) {
2702089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            MergeWithVar = FoundVar;
2703089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            break;
2704089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          }
2705089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2706d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          const ArrayType *FoundArray
2707d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            = Importer.getToContext().getAsArrayType(FoundVar->getType());
2708d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          const ArrayType *TArray
2709ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            = Importer.getToContext().getAsArrayType(D->getType());
2710d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor          if (FoundArray && TArray) {
2711d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            if (isa<IncompleteArrayType>(FoundArray) &&
2712d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor                isa<ConstantArrayType>(TArray)) {
2713ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              // Import the type.
2714ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              QualType T = Importer.Import(D->getType());
2715ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor              if (T.isNull())
2716ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor                return 0;
2717ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2718d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              FoundVar->setType(T);
2719d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              MergeWithVar = FoundVar;
2720d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              break;
2721d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor            } else if (isa<IncompleteArrayType>(TArray) &&
2722d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor                       isa<ConstantArrayType>(FoundArray)) {
2723d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              MergeWithVar = FoundVar;
2724d014542e1c6710d2c230b65b0cfcb8302a596b21Douglas Gregor              break;
27250f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor            }
27260f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor          }
27270f962a8e61e1c094a89df17f9d3ad947d31c4e5cDouglas Gregor
2728089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
2729ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            << Name << D->getType() << FoundVar->getType();
2730089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2731089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            << FoundVar->getType();
2732089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        }
2733089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      }
2734089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2735089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
2736089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2737089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2738089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    if (MergeWithVar) {
2739089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      // An equivalent variable with external linkage has been found. Link
2740089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      // the two declarations, then merge them.
27415ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor      Importer.Imported(D, MergeWithVar);
2742089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2743089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (VarDecl *DDef = D->getDefinition()) {
2744089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2745089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.ToDiag(ExistingDef->getLocation(),
2746089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                          diag::err_odr_variable_multiple_def)
2747089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor            << Name;
2748089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2749089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        } else {
2750089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor          Expr *Init = Importer.Import(DDef->getInit());
2751838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor          MergeWithVar->setInit(Init);
2752089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        }
2753089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      }
2754089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2755089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      return MergeWithVar;
2756089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2757089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2758089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    if (!ConflictingDecls.empty()) {
2759089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, IDNS,
2760089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                         ConflictingDecls.data(),
2761089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                         ConflictingDecls.size());
2762089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor      if (!Name)
2763089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor        return 0;
2764089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor    }
2765089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  }
276682fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2767ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  // Import the type.
2768ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  QualType T = Importer.Import(D->getType());
2769ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (T.isNull())
2770ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return 0;
2771ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
2772089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Create the imported variable.
277382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2774ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2775ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   Importer.Import(D->getInnerLocStart()),
2776ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   Loc, Name.getAsIdentifierInfo(),
2777ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   T, TInfo,
277816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClass(),
277916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten());
2780c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
2781325bf177ac0f1db7888e2d7b194515466ba91762Douglas Gregor  ToVar->setAccess(D->getAccess());
27829bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ToVar->setLexicalDeclContext(LexicalDC);
27835ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  Importer.Imported(D, ToVar);
27849bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  LexicalDC->addDecl(ToVar);
27859bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
2786089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // Merge the initializer.
2787089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // FIXME: Can we really import any initializer? Alternatively, we could force
2788089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // ourselves to import every declaration of a variable and then only use
2789089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // getInit() here.
2790838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
2791089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2792089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  // FIXME: Other bits to merge?
2793089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
2794089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  return ToVar;
2795089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
2796089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
27972cd00932b9b5403047139ce8cfaa3ae47966f894Douglas GregorDecl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
27982cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Parameters are created in the translation unit's context, then moved
27992cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // into the function declaration's context afterward.
28002cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
28012cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
28022cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the name of this declaration.
28032cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
28042cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  if (D->getDeclName() && !Name)
28052cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    return 0;
28062cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
28072cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the location of this declaration.
28082cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
28092cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
28102cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Import the parameter's type.
28112cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  QualType T = Importer.Import(D->getType());
28122cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  if (T.isNull())
28132cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    return 0;
28142cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
28152cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  // Create the imported parameter.
28162cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  ImplicitParamDecl *ToParm
28172cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor    = ImplicitParamDecl::Create(Importer.getToContext(), DC,
28182cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor                                Loc, Name.getAsIdentifierInfo(),
28192cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor                                T);
28202cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor  return Importer.Imported(D, ToParm);
28212cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor}
28222cd00932b9b5403047139ce8cfaa3ae47966f894Douglas Gregor
2823a404ea673cbee5e74af710a5f1ab571e71580b67Douglas GregorDecl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2824a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Parameters are created in the translation unit's context, then moved
2825a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // into the function declaration's context afterward.
2826a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2827a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
282882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // Import the name of this declaration.
282982fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
283082fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (D->getDeclName() && !Name)
283182fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
283282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2833a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the location of this declaration.
2834a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
2835a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor
2836a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Import the parameter's type.
2837a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  QualType T = Importer.Import(D->getType());
283882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (T.isNull())
283982fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
284082fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2841a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  // Create the imported parameter.
2842a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2843a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor  ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2844ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                     Importer.Import(D->getInnerLocStart()),
2845a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            Loc, Name.getAsIdentifierInfo(),
2846a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            T, TInfo, D->getStorageClass(),
284716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                             D->getStorageClassAsWritten(),
2848a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor                                            /*FIXME: Default argument*/ 0);
2849bf73b352acb7a2d041ce8b50171dd7f8e2b2c1bbJohn McCall  ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
28505ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  return Importer.Imported(D, ToParm);
2851a404ea673cbee5e74af710a5f1ab571e71580b67Douglas Gregor}
285282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
2853c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas GregorDecl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2854c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the major distinguishing characteristics of a method.
2855c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  DeclContext *DC, *LexicalDC;
2856c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  DeclarationName Name;
2857c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  SourceLocation Loc;
2858c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2859c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return 0;
2860c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2861c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2862c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       Lookup.first != Lookup.second;
2863c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       ++Lookup.first) {
2864c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2865c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2866c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        continue;
2867c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2868c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check return types.
2869c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2870c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                             FoundMethod->getResultType())) {
2871c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2872c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name
2873c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->getResultType() << FoundMethod->getResultType();
2874c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2875c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2876c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2877c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2878c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2879c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2880c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check the number of parameters.
2881c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (D->param_size() != FoundMethod->param_size()) {
2882c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2883c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name
2884c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->param_size() << FoundMethod->param_size();
2885c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2886c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2887c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2888c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2889c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2890c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2891c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check parameter types.
2892c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2893c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor             PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2894c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor           P != PEnd; ++P, ++FoundP) {
2895c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2896c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                               (*FoundP)->getType())) {
2897c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          Importer.FromDiag((*P)->getLocation(),
2898c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                            diag::err_odr_objc_method_param_type_inconsistent)
2899c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << D->isInstanceMethod() << Name
2900c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << (*P)->getType() << (*FoundP)->getType();
2901c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2902c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor            << (*FoundP)->getType();
2903c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          return 0;
2904c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        }
2905c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2906c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2907c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check variadic/non-variadic.
2908c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // Check the number of parameters.
2909c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      if (D->isVariadic() != FoundMethod->isVariadic()) {
2910c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2911c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2912c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        Importer.ToDiag(FoundMethod->getLocation(),
2913c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                        diag::note_odr_objc_method_here)
2914c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor          << D->isInstanceMethod() << Name;
2915c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor        return 0;
2916c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      }
2917c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2918c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      // FIXME: Any other bits we need to merge?
2919c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      return Importer.Imported(D, FoundMethod);
2920c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    }
2921c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2922c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2923c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the result type.
2924c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  QualType ResultTy = Importer.Import(D->getResultType());
2925c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (ResultTy.isNull())
2926c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return 0;
2927c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
29284bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor  TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
29294bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor
2930c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ObjCMethodDecl *ToMethod
2931c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    = ObjCMethodDecl::Create(Importer.getToContext(),
2932c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Loc,
2933c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Importer.Import(D->getLocEnd()),
2934c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             Name.getObjCSelector(),
29354bc1cb6aa635a5bf8fae99bf69c56c724c1e786cDouglas Gregor                             ResultTy, ResultTInfo, DC,
2936c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isInstanceMethod(),
2937c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isVariadic(),
2938c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                             D->isSynthesized(),
293975cf3e86d33ce810c12084126385371b335c30baArgyrios Kyrtzidis                             D->isImplicit(),
29403fe104154dd2e8ffb351142d74f308938b5c99bfFariborz Jahanian                             D->isDefined(),
2941926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                             D->getImplementationControl(),
2942926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                             D->hasRelatedResultType());
2943c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2944c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // FIXME: When we decide to merge method definitions, we'll need to
2945c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // deal with implicit parameters.
2946c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2947c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Import the parameters
29485f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 5> ToParams;
2949c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2950c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor                                   FromPEnd = D->param_end();
2951c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       FromP != FromPEnd;
2952c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor       ++FromP) {
2953c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2954c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    if (!ToP)
2955c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor      return 0;
2956c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2957c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToParams.push_back(ToP);
2958c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2959c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2960c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  // Set the parameters.
2961c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2962c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToParams[I]->setOwningFunction(ToMethod);
2963c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    ToMethod->addDecl(ToParams[I]);
2964c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  }
2965491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  SmallVector<SourceLocation, 12> SelLocs;
2966491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  D->getSelectorLocs(SelLocs);
2967491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
2968c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2969c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  ToMethod->setLexicalDeclContext(LexicalDC);
2970c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Importer.Imported(D, ToMethod);
2971c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  LexicalDC->addDecl(ToMethod);
2972c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToMethod;
2973c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor}
2974c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
2975b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas GregorDecl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2976b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import the major distinguishing characteristics of a category.
2977b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  DeclContext *DC, *LexicalDC;
2978b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  DeclarationName Name;
2979b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  SourceLocation Loc;
2980b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2981b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    return 0;
2982b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2983b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCInterfaceDecl *ToInterface
2984b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2985b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (!ToInterface)
2986b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    return 0;
2987b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
2988b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Determine if we've already encountered this category.
2989b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCCategoryDecl *MergeWithCategory
2990b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2991b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  ObjCCategoryDecl *ToCategory = MergeWithCategory;
2992b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (!ToCategory) {
2993b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
29941711fc91efb36d131f7ba771f73f0154dc1abd1fArgyrios Kyrtzidis                                          Importer.Import(D->getAtStartLoc()),
2995b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          Loc,
2996b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                       Importer.Import(D->getCategoryNameLoc()),
2997955fadbdfecfa24a590febe66a86519096787f2dArgyrios Kyrtzidis                                          Name.getAsIdentifierInfo(),
2998955fadbdfecfa24a590febe66a86519096787f2dArgyrios Kyrtzidis                                          ToInterface);
2999b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setLexicalDeclContext(LexicalDC);
3000b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    LexicalDC->addDecl(ToCategory);
3001b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Imported(D, ToCategory);
3002b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
3003b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // Import protocols
30045f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<ObjCProtocolDecl *, 4> Protocols;
30055f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<SourceLocation, 4> ProtocolLocs;
3006b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3007b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      = D->protocol_loc_begin();
3008b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3009b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                          FromProtoEnd = D->protocol_end();
3010b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor         FromProto != FromProtoEnd;
3011b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor         ++FromProto, ++FromProtoLoc) {
3012b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      ObjCProtocolDecl *ToProto
3013b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3014b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      if (!ToProto)
3015b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor        return 0;
3016b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      Protocols.push_back(ToProto);
3017b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3018b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    }
3019b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
3020b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
3021b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3022b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor                                ProtocolLocs.data(), Importer.getToContext());
3023b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
3024b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  } else {
3025b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Imported(D, ToCategory);
3026b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  }
3027b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
3028b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import all of the members of this category.
3029083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
3030b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
3031b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // If we have an implementation, import it as well.
3032b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  if (D->getImplementation()) {
3033b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ObjCCategoryImplDecl *Impl
3034cad2c59b0c087edea83d0fbf6eabde4a7960c778Douglas Gregor      = cast_or_null<ObjCCategoryImplDecl>(
3035cad2c59b0c087edea83d0fbf6eabde4a7960c778Douglas Gregor                                       Importer.Import(D->getImplementation()));
3036b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    if (!Impl)
3037b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor      return 0;
3038b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
3039b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    ToCategory->setImplementation(Impl);
3040b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  }
3041b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
3042b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  return ToCategory;
3043b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor}
3044b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
30452e2a400383c7a64a927f61eeed596b08928e1d4bDouglas GregorDecl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
3046b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import the major distinguishing characteristics of a protocol.
30472e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  DeclContext *DC, *LexicalDC;
30482e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  DeclarationName Name;
30492e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  SourceLocation Loc;
30502e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
30512e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    return 0;
30522e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
30532e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  ObjCProtocolDecl *MergeWithProtocol = 0;
30542e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
30552e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       Lookup.first != Lookup.second;
30562e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++Lookup.first) {
30572e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
30582e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      continue;
30592e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
30602e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
30612e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      break;
30622e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  }
30632e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
30642e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  ObjCProtocolDecl *ToProto = MergeWithProtocol;
30652e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  if (!ToProto || ToProto->isForwardDecl()) {
30662e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    if (!ToProto) {
30671711fc91efb36d131f7ba771f73f0154dc1abd1fArgyrios Kyrtzidis      ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
30681711fc91efb36d131f7ba771f73f0154dc1abd1fArgyrios Kyrtzidis                                         Name.getAsIdentifierInfo(), Loc,
30691711fc91efb36d131f7ba771f73f0154dc1abd1fArgyrios Kyrtzidis                                         Importer.Import(D->getAtStartLoc()));
30702e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto->setForwardDecl(D->isForwardDecl());
30712e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToProto->setLexicalDeclContext(LexicalDC);
30722e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      LexicalDC->addDecl(ToProto);
30732e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    }
30742e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Imported(D, ToProto);
30752e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
30762e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    // Import protocols
30775f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<ObjCProtocolDecl *, 4> Protocols;
30785f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<SourceLocation, 4> ProtocolLocs;
30792e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    ObjCProtocolDecl::protocol_loc_iterator
30802e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      FromProtoLoc = D->protocol_loc_begin();
30812e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
30822e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                                          FromProtoEnd = D->protocol_end();
30832e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       FromProto != FromProtoEnd;
30842e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor       ++FromProto, ++FromProtoLoc) {
30852e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ObjCProtocolDecl *ToProto
30862e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
30872e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      if (!ToProto)
30882e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor        return 0;
30892e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      Protocols.push_back(ToProto);
30902e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
30912e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    }
30922e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
30932e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
30942e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    ToProto->setProtocolList(Protocols.data(), Protocols.size(),
30952e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor                             ProtocolLocs.data(), Importer.getToContext());
30962e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  } else {
30972e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor    Importer.Imported(D, ToProto);
30982e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  }
30992e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
3100b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import all of the members of this protocol.
3101083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
31022e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
31032e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  return ToProto;
31042e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor}
31052e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor
3106a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas GregorDecl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3107a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // Import the major distinguishing characteristics of an @interface.
3108a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  DeclContext *DC, *LexicalDC;
3109a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  DeclarationName Name;
3110a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  SourceLocation Loc;
3111a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3112a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    return 0;
3113a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3114a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  ObjCInterfaceDecl *MergeWithIface = 0;
3115a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3116a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       Lookup.first != Lookup.second;
3117a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++Lookup.first) {
3118a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3119a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      continue;
3120a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3121a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
3122a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      break;
3123a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
3124a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3125a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  ObjCInterfaceDecl *ToIface = MergeWithIface;
3126a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (!ToIface || ToIface->isForwardDecl()) {
3127a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!ToIface) {
31281711fc91efb36d131f7ba771f73f0154dc1abd1fArgyrios Kyrtzidis      ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
31291711fc91efb36d131f7ba771f73f0154dc1abd1fArgyrios Kyrtzidis                                          Importer.Import(D->getAtStartLoc()),
31301711fc91efb36d131f7ba771f73f0154dc1abd1fArgyrios Kyrtzidis                                          Name.getAsIdentifierInfo(), Loc,
3131a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          D->isForwardDecl(),
3132a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                          D->isImplicitInterfaceDecl());
31332e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor      ToIface->setForwardDecl(D->isForwardDecl());
3134a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setLexicalDeclContext(LexicalDC);
3135a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      LexicalDC->addDecl(ToIface);
3136a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
3137a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Imported(D, ToIface);
3138a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3139a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (D->getSuperClass()) {
3140a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ObjCInterfaceDecl *Super
3141a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3142a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      if (!Super)
3143a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        return 0;
3144a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3145a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setSuperClass(Super);
3146a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3147a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
3148a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3149a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // Import protocols
31505f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<ObjCProtocolDecl *, 4> Protocols;
31515f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<SourceLocation, 4> ProtocolLocs;
3152a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ObjCInterfaceDecl::protocol_loc_iterator
3153a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      FromProtoLoc = D->protocol_loc_begin();
315453b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek
315553b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // FIXME: Should we be usng all_referenced_protocol_begin() here?
3156a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3157a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                                           FromProtoEnd = D->protocol_end();
3158a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       FromProto != FromProtoEnd;
3159a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor       ++FromProto, ++FromProtoLoc) {
3160a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ObjCProtocolDecl *ToProto
3161a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3162a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      if (!ToProto)
3163a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor        return 0;
3164a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      Protocols.push_back(ToProto);
3165a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3166a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    }
3167a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3168a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // FIXME: If we're merging, make sure that the protocol list is the same.
3169a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3170a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor                             ProtocolLocs.data(), Importer.getToContext());
3171a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3172a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    // Import @end range
3173a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3174a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  } else {
3175a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    Importer.Imported(D, ToIface);
31762e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor
31772e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    // Check for consistency of superclasses.
31782e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    DeclarationName FromSuperName, ToSuperName;
31792e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (D->getSuperClass())
31802e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
31812e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (ToIface->getSuperClass())
31822e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      ToSuperName = ToIface->getSuperClass()->getDeclName();
31832e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    if (FromSuperName != ToSuperName) {
31842e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      Importer.ToDiag(ToIface->getLocation(),
31852e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                      diag::err_odr_objc_superclass_inconsistent)
31862e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        << ToIface->getDeclName();
31872e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (ToIface->getSuperClass())
31882e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.ToDiag(ToIface->getSuperClassLoc(),
31892e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                        diag::note_odr_objc_superclass)
31902e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor          << ToIface->getSuperClass()->getDeclName();
31912e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      else
31922e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.ToDiag(ToIface->getLocation(),
31932e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                        diag::note_odr_objc_missing_superclass);
31942e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      if (D->getSuperClass())
31952e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.FromDiag(D->getSuperClassLoc(),
31962e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                          diag::note_odr_objc_superclass)
31972e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor          << D->getSuperClass()->getDeclName();
31982e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      else
31992e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor        Importer.FromDiag(D->getLocation(),
32002e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor                          diag::note_odr_objc_missing_superclass);
32012e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor      return 0;
32022e55e3af2f6d6c0509495357fade95105dd144cdDouglas Gregor    }
3203a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
3204a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3205b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // Import categories. When the categories themselves are imported, they'll
3206b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  // hook themselves into this interface.
3207b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor  for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3208b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor       FromCat = FromCat->getNextClassCategory())
3209b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor    Importer.Import(FromCat);
3210b4677b62a8e9bba0913dfe1456bd9d689cdbccacDouglas Gregor
3211a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // Import all of the members of this class.
3212083a821bbd02bbc521275a2f144abe39a6c2745cDouglas Gregor  ImportDeclContext(D);
3213a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3214a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  // If we have an @implementation, import it as well.
3215a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  if (D->getImplementation()) {
3216dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3217dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                       Importer.Import(D->getImplementation()));
3218a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    if (!Impl)
3219a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor      return 0;
3220a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
3221a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor    ToIface->setImplementation(Impl);
3222a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor  }
3223a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
32242e2a400383c7a64a927f61eeed596b08928e1d4bDouglas Gregor  return ToIface;
3225a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor}
3226a12d294db5bd59cd8836b9bd0dafadaaa2e3dd8aDouglas Gregor
32273daef29bf390dbdb3603748280afd5827d1811daDouglas GregorDecl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
32283daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
32293daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor                                        Importer.Import(D->getCategoryDecl()));
32303daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  if (!Category)
32313daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    return 0;
32323daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
32333daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
32343daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  if (!ToImpl) {
32353daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    DeclContext *DC = Importer.ImportContext(D->getDeclContext());
32363daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    if (!DC)
32373daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor      return 0;
32383daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
32393daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
32403daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor                                          Importer.Import(D->getIdentifier()),
32411711fc91efb36d131f7ba771f73f0154dc1abd1fArgyrios Kyrtzidis                                          Category->getClassInterface(),
32421711fc91efb36d131f7ba771f73f0154dc1abd1fArgyrios Kyrtzidis                                          Importer.Import(D->getLocation()),
32431711fc91efb36d131f7ba771f73f0154dc1abd1fArgyrios Kyrtzidis                                          Importer.Import(D->getAtStartLoc()));
32443daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
32453daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    DeclContext *LexicalDC = DC;
32463daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    if (D->getDeclContext() != D->getLexicalDeclContext()) {
32473daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor      LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
32483daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor      if (!LexicalDC)
32493daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor        return 0;
32503daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
32513daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor      ToImpl->setLexicalDeclContext(LexicalDC);
32523daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    }
32533daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
32543daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    LexicalDC->addDecl(ToImpl);
32553daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor    Category->setImplementation(ToImpl);
32563daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  }
32573daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
32583daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  Importer.Imported(D, ToImpl);
3259cad2c59b0c087edea83d0fbf6eabde4a7960c778Douglas Gregor  ImportDeclContext(D);
32603daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor  return ToImpl;
32613daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor}
32623daef29bf390dbdb3603748280afd5827d1811daDouglas Gregor
3263dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas GregorDecl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3264dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  // Find the corresponding interface.
3265dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3266dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                       Importer.Import(D->getClassInterface()));
3267dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  if (!Iface)
3268dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    return 0;
3269dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3270dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  // Import the superclass, if any.
3271dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ObjCInterfaceDecl *Super = 0;
3272dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  if (D->getSuperClass()) {
3273dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Super = cast_or_null<ObjCInterfaceDecl>(
3274dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                          Importer.Import(D->getSuperClass()));
3275dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    if (!Super)
3276dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      return 0;
3277dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  }
3278dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3279dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ObjCImplementationDecl *Impl = Iface->getImplementation();
3280dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  if (!Impl) {
3281dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // We haven't imported an implementation yet. Create a new @implementation
3282dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // now.
3283dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3284dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                  Importer.ImportContext(D->getDeclContext()),
32851711fc91efb36d131f7ba771f73f0154dc1abd1fArgyrios Kyrtzidis                                          Iface, Super,
3286dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                                          Importer.Import(D->getLocation()),
32871711fc91efb36d131f7ba771f73f0154dc1abd1fArgyrios Kyrtzidis                                          Importer.Import(D->getAtStartLoc()));
3288dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3289dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    if (D->getDeclContext() != D->getLexicalDeclContext()) {
3290dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      DeclContext *LexicalDC
3291dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        = Importer.ImportContext(D->getLexicalDeclContext());
3292dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      if (!LexicalDC)
3293dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        return 0;
3294dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      Impl->setLexicalDeclContext(LexicalDC);
3295dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    }
3296dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3297dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // Associate the implementation with the class it implements.
3298dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Iface->setImplementation(Impl);
3299dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Importer.Imported(D, Iface->getImplementation());
3300dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  } else {
3301dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    Importer.Imported(D, Iface->getImplementation());
3302dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3303dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    // Verify that the existing @implementation has the same superclass.
3304dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    if ((Super && !Impl->getSuperClass()) ||
3305dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        (!Super && Impl->getSuperClass()) ||
3306dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        (Super && Impl->getSuperClass() &&
3307dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor         Super->getCanonicalDecl() != Impl->getSuperClass())) {
3308dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        Importer.ToDiag(Impl->getLocation(),
3309dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                        diag::err_odr_objc_superclass_inconsistent)
3310dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          << Iface->getDeclName();
3311dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        // FIXME: It would be nice to have the location of the superclass
3312dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        // below.
3313dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        if (Impl->getSuperClass())
3314dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.ToDiag(Impl->getLocation(),
3315dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                          diag::note_odr_objc_superclass)
3316dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          << Impl->getSuperClass()->getDeclName();
3317dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        else
3318dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.ToDiag(Impl->getLocation(),
3319dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                          diag::note_odr_objc_missing_superclass);
3320dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        if (D->getSuperClass())
3321dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.FromDiag(D->getLocation(),
3322dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                            diag::note_odr_objc_superclass)
3323dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          << D->getSuperClass()->getDeclName();
3324dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor        else
3325dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor          Importer.FromDiag(D->getLocation(),
3326dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor                            diag::note_odr_objc_missing_superclass);
3327dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor      return 0;
3328dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor    }
3329dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  }
3330dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3331dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  // Import all of the members of this @implementation.
3332dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  ImportDeclContext(D);
3333dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3334dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor  return Impl;
3335dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor}
3336dd182ff10b9145e432dea1fd2fb67100ccca3b10Douglas Gregor
3337e3261624c1870e52d7efc2ac83e647713361ac6cDouglas GregorDecl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3338e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Import the major distinguishing characteristics of an @property.
3339e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  DeclContext *DC, *LexicalDC;
3340e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  DeclarationName Name;
3341e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  SourceLocation Loc;
3342e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3343e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    return 0;
3344e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3345e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Check whether we have already imported this property.
3346e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3347e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       Lookup.first != Lookup.second;
3348e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       ++Lookup.first) {
3349e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    if (ObjCPropertyDecl *FoundProp
3350e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                                = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3351e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // Check property types.
3352e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      if (!Importer.IsStructurallyEquivalent(D->getType(),
3353e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                                             FoundProp->getType())) {
3354e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3355e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor          << Name << D->getType() << FoundProp->getType();
3356e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3357e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor          << FoundProp->getType();
3358e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor        return 0;
3359e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      }
3360e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3361e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // FIXME: Check property attributes, getters, setters, etc.?
3362e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3363e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      // Consider these properties to be equivalent.
3364e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      Importer.Imported(D, FoundProp);
3365e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor      return FoundProp;
3366e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    }
3367e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  }
3368e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3369e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Import the type.
337083a230c83a54190366138c1a4f4310ef838b88fcJohn McCall  TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
337183a230c83a54190366138c1a4f4310ef838b88fcJohn McCall  if (!T)
3372e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    return 0;
3373e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3374e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  // Create the new property.
3375e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ObjCPropertyDecl *ToProperty
3376e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor    = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3377e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               Name.getAsIdentifierInfo(),
3378e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               Importer.Import(D->getAtLoc()),
3379e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               T,
3380e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor                               D->getPropertyImplementation());
3381e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  Importer.Imported(D, ToProperty);
3382e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setLexicalDeclContext(LexicalDC);
3383e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  LexicalDC->addDecl(ToProperty);
3384e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3385e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setPropertyAttributes(D->getPropertyAttributes());
338680aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian  ToProperty->setPropertyAttributesAsWritten(
338780aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian                                      D->getPropertyAttributesAsWritten());
3388e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3389e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3390e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setGetterMethodDecl(
3391e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3392e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setSetterMethodDecl(
3393e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor     cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3394e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  ToProperty->setPropertyIvarDecl(
3395e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor       cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3396e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor  return ToProperty;
3397e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor}
3398e3261624c1870e52d7efc2ac83e647713361ac6cDouglas Gregor
3399954e0c75c42f321945aff8b9ee96da43cd90c752Douglas GregorDecl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3400954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3401954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                        Importer.Import(D->getPropertyDecl()));
3402954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (!Property)
3403954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    return 0;
3404954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3405954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3406954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (!DC)
3407954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    return 0;
3408954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3409954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  // Import the lexical declaration context.
3410954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  DeclContext *LexicalDC = DC;
3411954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
3412954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3413954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    if (!LexicalDC)
3414954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      return 0;
3415954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  }
3416954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3417954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3418954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (!InImpl)
3419954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    return 0;
3420954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3421954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  // Import the ivar (for an @synthesize).
3422954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  ObjCIvarDecl *Ivar = 0;
3423954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (D->getPropertyIvarDecl()) {
3424954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    Ivar = cast_or_null<ObjCIvarDecl>(
3425954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                    Importer.Import(D->getPropertyIvarDecl()));
3426954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    if (!Ivar)
3427954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      return 0;
3428954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  }
3429954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3430954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  ObjCPropertyImplDecl *ToImpl
3431954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3432954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  if (!ToImpl) {
3433954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3434954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          Importer.Import(D->getLocStart()),
3435954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          Importer.Import(D->getLocation()),
3436954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          Property,
3437954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          D->getPropertyImplementation(),
3438954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                          Ivar,
3439954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                  Importer.Import(D->getPropertyIvarDeclLoc()));
3440954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    ToImpl->setLexicalDeclContext(LexicalDC);
3441954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    Importer.Imported(D, ToImpl);
3442954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    LexicalDC->addDecl(ToImpl);
3443954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  } else {
3444954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    // Check that we have the same kind of property implementation (@synthesize
3445954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    // vs. @dynamic).
3446954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3447954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      Importer.ToDiag(ToImpl->getLocation(),
3448954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                      diag::err_odr_objc_property_impl_kind_inconsistent)
3449954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << Property->getDeclName()
3450954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << (ToImpl->getPropertyImplementation()
3451954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                                              == ObjCPropertyImplDecl::Dynamic);
3452954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      Importer.FromDiag(D->getLocation(),
3453954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                        diag::note_odr_objc_property_impl_kind)
3454954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << D->getPropertyDecl()->getDeclName()
3455954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3456954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      return 0;
3457954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    }
3458954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3459954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    // For @synthesize, check that we have the same
3460954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3461954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        Ivar != ToImpl->getPropertyIvarDecl()) {
3462954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3463954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                      diag::err_odr_objc_synthesize_ivar_inconsistent)
3464954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << Property->getDeclName()
3465954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << ToImpl->getPropertyIvarDecl()->getDeclName()
3466954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << Ivar->getDeclName();
3467954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3468954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor                        diag::note_odr_objc_synthesize_ivar_here)
3469954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor        << D->getPropertyIvarDecl()->getDeclName();
3470954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor      return 0;
3471954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    }
3472954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3473954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    // Merge the existing implementation with the new implementation.
3474954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor    Importer.Imported(D, ToImpl);
3475954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  }
3476954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
3477954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor  return ToImpl;
3478954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor}
3479954e0c75c42f321945aff8b9ee96da43cd90c752Douglas Gregor
34802b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas GregorDecl *
34812b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas GregorASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
34822b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  // Import the context of this declaration.
34832b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
34842b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  if (!DC)
34852b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    return 0;
34862b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
34872b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  DeclContext *LexicalDC = DC;
34882b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
34892b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
34902b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    if (!LexicalDC)
34912b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      return 0;
34922b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  }
34932b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
34942b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  // Import the location of this declaration.
34952b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
34962b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
34975f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ObjCProtocolDecl *, 4> Protocols;
34985f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 4> Locations;
34992b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
35002b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    = D->protocol_loc_begin();
35012b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  for (ObjCForwardProtocolDecl::protocol_iterator FromProto
35022b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor         = D->protocol_begin(), FromProtoEnd = D->protocol_end();
35032b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor       FromProto != FromProtoEnd;
35042b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor       ++FromProto, ++FromProtoLoc) {
35052b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    ObjCProtocolDecl *ToProto
35062b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
35072b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    if (!ToProto)
35082b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor      continue;
35092b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
35102b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Protocols.push_back(ToProto);
35112b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    Locations.push_back(Importer.Import(*FromProtoLoc));
35122b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  }
35132b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
35142b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ObjCForwardProtocolDecl *ToForward
35152b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor    = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
35162b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor                                      Protocols.data(), Protocols.size(),
35172b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor                                      Locations.data());
35182b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  ToForward->setLexicalDeclContext(LexicalDC);
35192b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  LexicalDC->addDecl(ToForward);
35202b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  Importer.Imported(D, ToForward);
35212b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor  return ToForward;
35222b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor}
35232b785022973202ea6bafe304a50eb3cac1a0aeb8Douglas Gregor
3524a2bc15b7463a9f85a5bff1531d833c278426a733Douglas GregorDecl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3525a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  // Import the context of this declaration.
3526a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3527a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  if (!DC)
3528a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    return 0;
3529a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3530a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  DeclContext *LexicalDC = DC;
3531a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
3532a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3533a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor    if (!LexicalDC)
3534a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor      return 0;
3535a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  }
3536a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3537a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  // Import the location of this declaration.
3538a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
353995ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian  ObjCClassDecl::ObjCClassRef *From = D->getForwardDecl();
354095ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian  ObjCInterfaceDecl *ToIface
354195ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3542a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
354395ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian                                        Loc,
354495ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian                                        ToIface,
354595ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian                                        Importer.Import(From->getLocation()));
354695ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian
3547a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  ToClass->setLexicalDeclContext(LexicalDC);
3548a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  LexicalDC->addDecl(ToClass);
3549a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  Importer.Imported(D, ToClass);
3550a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor  return ToClass;
3551a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor}
3552a2bc15b7463a9f85a5bff1531d833c278426a733Douglas Gregor
3553040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3554040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // For template arguments, we adopt the translation unit as our declaration
3555040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // context. This context will be fixed when the actual template declaration
3556040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // is created.
3557040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3558040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Import default argument.
3559040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return TemplateTypeParmDecl::Create(Importer.getToContext(),
3560040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                              Importer.getToContext().getTranslationUnitDecl(),
3561344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                      Importer.Import(D->getLocStart()),
3562040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      Importer.Import(D->getLocation()),
3563040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->getDepth(),
3564040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->getIndex(),
3565040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      Importer.Import(D->getIdentifier()),
3566040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->wasDeclaredWithTypename(),
3567040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                      D->isParameterPack());
3568040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3569040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3570040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *
3571040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3572040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the name of this declaration.
3573040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
3574040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D->getDeclName() && !Name)
3575040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3576040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3577040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the location of this declaration.
3578040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
3579040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3580040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the type of this declaration.
3581040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  QualType T = Importer.Import(D->getType());
3582040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (T.isNull())
3583040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3584040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3585040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import type-source information.
3586040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3587040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D->getTypeSourceInfo() && !TInfo)
3588040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3589040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3590040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Import default argument.
3591040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3592040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3593040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                               Importer.getToContext().getTranslationUnitDecl(),
3594ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                         Importer.Import(D->getInnerLocStart()),
3595040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         Loc, D->getDepth(), D->getPosition(),
3596040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         Name.getAsIdentifierInfo(),
359710738d36b150aa65206890c1c845cdba076e4200Douglas Gregor                                         T, D->isParameterPack(), TInfo);
3598040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3599040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3600040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *
3601040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3602040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the name of this declaration.
3603040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclarationName Name = Importer.Import(D->getDeclName());
3604040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (D->getDeclName() && !Name)
3605040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3606040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3607040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the location of this declaration.
3608040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  SourceLocation Loc = Importer.Import(D->getLocation());
3609040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3610040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import template parameters.
3611040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  TemplateParameterList *TemplateParams
3612040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    = ImportTemplateParameterList(D->getTemplateParameters());
3613040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!TemplateParams)
3614040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3615040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3616040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // FIXME: Import default argument.
3617040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3618040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3619040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                              Importer.getToContext().getTranslationUnitDecl(),
3620040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          Loc, D->getDepth(), D->getPosition(),
362161c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                          D->isParameterPack(),
3622040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          Name.getAsIdentifierInfo(),
3623040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                          TemplateParams);
3624040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3625040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3626040afaeea2313dc69fd532995ac88cccdd62da56Douglas GregorDecl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3627040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // If this record has a definition in the translation unit we're coming from,
3628040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // but this particular declaration is not that definition, import the
3629040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // definition and map to that.
3630040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  CXXRecordDecl *Definition
3631040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3632040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (Definition && Definition != D->getTemplatedDecl()) {
3633040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    Decl *ImportedDef
3634040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      = Importer.Import(Definition->getDescribedClassTemplate());
3635040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!ImportedDef)
3636040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return 0;
3637040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3638040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return Importer.Imported(D, ImportedDef);
3639040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
3640040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3641040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Import the major distinguishing characteristics of this class template.
3642040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclContext *DC, *LexicalDC;
3643040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  DeclarationName Name;
3644040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  SourceLocation Loc;
3645040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3646040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3647040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3648040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // We may already have a template of the same name; try to find and match it.
3649040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!DC->isFunctionOrMethod()) {
36505f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<NamedDecl *, 4> ConflictingDecls;
3651040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3652040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor         Lookup.first != Lookup.second;
3653040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor         ++Lookup.first) {
3654040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3655040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        continue;
3656040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3657040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Decl *Found = *Lookup.first;
3658040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      if (ClassTemplateDecl *FoundTemplate
3659040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                        = dyn_cast<ClassTemplateDecl>(Found)) {
3660040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        if (IsStructuralMatch(D, FoundTemplate)) {
3661040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          // The class templates structurally match; call it the same template.
3662040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          // FIXME: We may be filling in a forward declaration here. Handle
3663040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          // this case!
3664040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          Importer.Imported(D->getTemplatedDecl(),
3665040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                            FoundTemplate->getTemplatedDecl());
3666040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor          return Importer.Imported(D, FoundTemplate);
3667040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor        }
3668040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      }
3669040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3670040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      ConflictingDecls.push_back(*Lookup.first);
3671040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
3672040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3673040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!ConflictingDecls.empty()) {
3674040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3675040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         ConflictingDecls.data(),
3676040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                         ConflictingDecls.size());
3677040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    }
3678040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3679040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    if (!Name)
3680040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor      return 0;
3681040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
3682040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3683040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3684040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3685040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Create the declaration that is being templated.
3686ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3687ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
3688040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3689040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                     DTemplated->getTagKind(),
3690ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     DC, StartLoc, IdLoc,
3691ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                   Name.getAsIdentifierInfo());
3692040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2Templated->setAccess(DTemplated->getAccess());
3693c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
3694040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2Templated->setLexicalDeclContext(LexicalDC);
3695040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3696040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Create the class template declaration itself.
3697040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  TemplateParameterList *TemplateParams
3698040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    = ImportTemplateParameterList(D->getTemplateParameters());
3699040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (!TemplateParams)
3700040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    return 0;
3701040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3702040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3703040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                    Loc, Name, TemplateParams,
3704040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor                                                    D2Templated,
3705040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  /*PrevDecl=*/0);
3706040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2Templated->setDescribedClassTemplate(D2);
3707040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3708040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2->setAccess(D->getAccess());
3709040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  D2->setLexicalDeclContext(LexicalDC);
3710040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  LexicalDC->addDecl(D2);
3711040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3712040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  // Note the relationship between the class templates.
3713040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  Importer.Imported(D, D2);
3714040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  Importer.Imported(DTemplated, D2Templated);
3715040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3716040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3717040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor    // FIXME: Import definition!
3718040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  }
3719040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3720040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor  return D2;
3721040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor}
3722040afaeea2313dc69fd532995ac88cccdd62da56Douglas Gregor
3723d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorDecl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3724d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                          ClassTemplateSpecializationDecl *D) {
3725d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // If this record has a definition in the translation unit we're coming from,
3726d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // but this particular declaration is not that definition, import the
3727d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // definition and map to that.
3728d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  TagDecl *Definition = D->getDefinition();
3729d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (Definition && Definition != D) {
3730d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    Decl *ImportedDef = Importer.Import(Definition);
3731d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!ImportedDef)
3732d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return 0;
3733d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3734d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return Importer.Imported(D, ImportedDef);
3735d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3736d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3737d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateDecl *ClassTemplate
3738d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = cast_or_null<ClassTemplateDecl>(Importer.Import(
3739d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 D->getSpecializedTemplate()));
3740d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (!ClassTemplate)
3741d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3742d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3743d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Import the context of this declaration.
3744d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  DeclContext *DC = ClassTemplate->getDeclContext();
3745d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (!DC)
3746d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3747d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3748d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  DeclContext *LexicalDC = DC;
3749d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D->getDeclContext() != D->getLexicalDeclContext()) {
3750d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3751d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!LexicalDC)
3752d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return 0;
3753d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3754d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3755d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Import the location of this declaration.
3756ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation StartLoc = Importer.Import(D->getLocStart());
3757ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation IdLoc = Importer.Import(D->getLocation());
3758d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3759d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Import template arguments.
37605f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 2> TemplateArgs;
3761d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (ImportTemplateArguments(D->getTemplateArgs().data(),
3762d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                              D->getTemplateArgs().size(),
3763d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                              TemplateArgs))
3764d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3765d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3766d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  // Try to find an existing specialization with these template arguments.
3767d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  void *InsertPos = 0;
3768d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  ClassTemplateSpecializationDecl *D2
3769d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    = ClassTemplate->findSpecialization(TemplateArgs.data(),
3770d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                        TemplateArgs.size(), InsertPos);
3771d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D2) {
3772d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // We already have a class template specialization with these template
3773d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // arguments.
3774d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3775d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // FIXME: Check for specialization vs. instantiation errors.
3776d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3777d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (RecordDecl *FoundDef = D2->getDefinition()) {
3778d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3779d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        // The record types structurally match, or the "from" translation
3780d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        // unit only had a forward declaration anyway; call it the same
3781d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        // function.
3782d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return Importer.Imported(D, FoundDef);
3783d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      }
3784d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
3785d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  } else {
3786d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Create a new specialization.
3787d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3788d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 D->getTagKind(), DC,
3789ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                 StartLoc, IdLoc,
3790ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                 ClassTemplate,
3791d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 TemplateArgs.data(),
3792d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 TemplateArgs.size(),
3793d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                 /*PrevDecl=*/0);
3794d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    D2->setSpecializationKind(D->getSpecializationKind());
3795d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3796d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Add this specialization to the class template.
3797d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    ClassTemplate->AddSpecialization(D2, InsertPos);
3798d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3799d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Import the qualifier, if any.
3800c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
3801d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3802d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    // Add the specialization to this context.
3803d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    D2->setLexicalDeclContext(LexicalDC);
3804d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    LexicalDC->addDecl(D2);
3805d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
3806d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  Importer.Imported(D, D2);
3807d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3808d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  if (D->isDefinition() && ImportDefinition(D, D2))
3809d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return 0;
3810d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
3811d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return D2;
3812d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
3813d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
38144800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
38154800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor// Import Statements
38164800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
38174800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
38184800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorStmt *ASTNodeImporter::VisitStmt(Stmt *S) {
38194800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
38204800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    << S->getStmtClassName();
38214800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return 0;
38224800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
38234800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
38244800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
38254800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor// Import Expressions
38264800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor//----------------------------------------------------------------------------
38274800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorExpr *ASTNodeImporter::VisitExpr(Expr *E) {
38284800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
38294800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    << E->getStmtClassName();
38304800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return 0;
38314800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
38324800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
3833440806306674e23ad74726208cbdc6f37849dd9dDouglas GregorExpr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3834440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3835440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (!ToD)
3836440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    return 0;
38373aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth
38383aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  NamedDecl *FoundD = 0;
38393aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  if (E->getDecl() != E->getFoundDecl()) {
38403aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
38413aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth    if (!FoundD)
38423aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth      return 0;
38433aa8140bde5b9bedf13e46ec0a668daa54814196Chandler Carruth  }
3844440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
3845440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  QualType T = Importer.Import(E->getType());
3846440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor  if (T.isNull())
3847440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor    return 0;
38487cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara
38497cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara  DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
38507cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara                                         Importer.Import(E->getQualifierLoc()),
38517cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara                                         ToD,
38527cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara                                         Importer.Import(E->getLocation()),
38537cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara                                         T, E->getValueKind(),
38547cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara                                         FoundD,
38557cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara                                         /*FIXME:TemplateArgs=*/0);
38567cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara  if (E->hadMultipleCandidates())
38577cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara    DRE->setHadMultipleCandidates(true);
38587cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara  return DRE;
3859440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor}
3860440806306674e23ad74726208cbdc6f37849dd9dDouglas Gregor
38614800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas GregorExpr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
38624800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  QualType T = Importer.Import(E->getType());
38634800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (T.isNull())
38644800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return 0;
38654800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
38669996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis  return IntegerLiteral::Create(Importer.getToContext(),
38679996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                                E->getValue(), T,
38689996a7f06a3c5b4554692e7177930cf4e8ef09afArgyrios Kyrtzidis                                Importer.Import(E->getLocation()));
38694800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor}
38704800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
3871b2e400aae8c62c4e1616016f40618baace0da065Douglas GregorExpr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3872b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  QualType T = Importer.Import(E->getType());
3873b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor  if (T.isNull())
3874b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor    return 0;
3875b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor
38765cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
38775cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                                                        E->getKind(), T,
3878b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor                                          Importer.Import(E->getLocation()));
3879b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor}
3880b2e400aae8c62c4e1616016f40618baace0da065Douglas Gregor
3881f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3882f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
3883f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!SubExpr)
3884f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3885f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3886f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext())
3887f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                  ParenExpr(Importer.Import(E->getLParen()),
3888f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                            Importer.Import(E->getRParen()),
3889f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                            SubExpr);
3890f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3891f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3892f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3893f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
3894f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
3895f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3896f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3897f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
3898f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!SubExpr)
3899f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3900f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3901f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
3902f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                     T, E->getValueKind(),
3903f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                     E->getObjectKind(),
3904f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                         Importer.Import(E->getOperatorLoc()));
3905f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3906f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3907f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter CollingbourneExpr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3908f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne                                            UnaryExprOrTypeTraitExpr *E) {
3909bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  QualType ResultType = Importer.Import(E->getType());
3910bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3911bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  if (E->isArgumentType()) {
3912bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3913bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    if (!TInfo)
3914bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor      return 0;
3915bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3916f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne    return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3917f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne                                           TInfo, ResultType,
3918bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                           Importer.Import(E->getOperatorLoc()),
3919bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                           Importer.Import(E->getRParenLoc()));
3920bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  }
3921bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3922bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3923bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor  if (!SubExpr)
3924bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor    return 0;
3925bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3926f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne  return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3927f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne                                          SubExpr, ResultType,
3928bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                          Importer.Import(E->getOperatorLoc()),
3929bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor                                          Importer.Import(E->getRParenLoc()));
3930bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor}
3931bd249a542878a626192746c1e0c0b21f164e6df7Douglas Gregor
3932f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3933f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
3934f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
3935f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3936f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3937f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *LHS = Importer.Import(E->getLHS());
3938f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!LHS)
3939f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3940f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3941f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *RHS = Importer.Import(E->getRHS());
3942f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!RHS)
3943f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3944f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3945f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
3946f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                      T, E->getValueKind(),
3947f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                                      E->getObjectKind(),
3948f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                          Importer.Import(E->getOperatorLoc()));
3949f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3950f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3951f638f9580ba0da99a66668f00e1a1d4987067bddDouglas GregorExpr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3952f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType T = Importer.Import(E->getType());
3953f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (T.isNull())
3954f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3955f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3956f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3957f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (CompLHSType.isNull())
3958f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3959f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3960f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  QualType CompResultType = Importer.Import(E->getComputationResultType());
3961f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (CompResultType.isNull())
3962f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3963f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3964f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *LHS = Importer.Import(E->getLHS());
3965f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!LHS)
3966f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3967f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3968f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  Expr *RHS = Importer.Import(E->getRHS());
3969f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  if (!RHS)
3970f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor    return 0;
3971f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3972f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor  return new (Importer.getToContext())
3973f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                        CompoundAssignOperator(LHS, RHS, E->getOpcode(),
3974f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                               T, E->getValueKind(),
3975f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                               E->getObjectKind(),
3976f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                               CompLHSType, CompResultType,
3977f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor                                          Importer.Import(E->getOperatorLoc()));
3978f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor}
3979f638f9580ba0da99a66668f00e1a1d4987067bddDouglas Gregor
3980da57f3eeab7b7f7f6e6788956f0a0d9adf196a7dBenjamin Kramerstatic bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3981f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (E->path_empty()) return false;
3982f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
3983f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  // TODO: import cast paths
3984f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return true;
3985f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall}
3986f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
398736ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas GregorExpr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
398836ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  QualType T = Importer.Import(E->getType());
398936ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (T.isNull())
399036ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
399136ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
399236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
399336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor  if (!SubExpr)
399436ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor    return 0;
3995f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
3996f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  CXXCastPath BasePath;
3997f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (ImportCastPath(E, BasePath))
3998f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return 0;
3999f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
4000f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
40015baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                                  SubExpr, &BasePath, E->getValueKind());
400236ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor}
400336ead2e992abb30aa3b4a40b4c8cb22cc9389fefDouglas Gregor
4004008847a70ab122a99911149199855060fb3753b4Douglas GregorExpr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4005008847a70ab122a99911149199855060fb3753b4Douglas Gregor  QualType T = Importer.Import(E->getType());
4006008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (T.isNull())
4007008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
4008008847a70ab122a99911149199855060fb3753b4Douglas Gregor
4009008847a70ab122a99911149199855060fb3753b4Douglas Gregor  Expr *SubExpr = Importer.Import(E->getSubExpr());
4010008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (!SubExpr)
4011008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
4012008847a70ab122a99911149199855060fb3753b4Douglas Gregor
4013008847a70ab122a99911149199855060fb3753b4Douglas Gregor  TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4014008847a70ab122a99911149199855060fb3753b4Douglas Gregor  if (!TInfo && E->getTypeInfoAsWritten())
4015008847a70ab122a99911149199855060fb3753b4Douglas Gregor    return 0;
4016008847a70ab122a99911149199855060fb3753b4Douglas Gregor
4017f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  CXXCastPath BasePath;
4018f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall  if (ImportCastPath(E, BasePath))
4019f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall    return 0;
4020f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall
4021f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall  return CStyleCastExpr::Create(Importer.getToContext(), T,
4022f89e55ab1bfb3ea997f8b02997c611a02254eb2dJohn McCall                                E->getValueKind(), E->getCastKind(),
4023f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                SubExpr, &BasePath, TInfo,
4024f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                Importer.Import(E->getLParenLoc()),
4025f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                Importer.Import(E->getRParenLoc()));
4026008847a70ab122a99911149199855060fb3753b4Douglas Gregor}
4027008847a70ab122a99911149199855060fb3753b4Douglas Gregor
402833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios KyrtzidisASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
4029d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor                         ASTContext &FromContext, FileManager &FromFileManager,
4030d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor                         bool MinimalImport)
40311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  : ToContext(ToContext), FromContext(FromContext),
4032d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4033d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    Minimal(MinimalImport)
4034d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor{
40359bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ImportedDecls[FromContext.getTranslationUnitDecl()]
40369bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    = ToContext.getTranslationUnitDecl();
40379bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
40389bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40399bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorASTImporter::~ASTImporter() { }
40401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
40411b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorQualType ASTImporter::Import(QualType FromT) {
40421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (FromT.isNull())
40431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return QualType();
4044f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall
4045f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *fromTy = FromT.getTypePtr();
40461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
4047169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Check whether we've already imported this type.
4048f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  llvm::DenseMap<const Type *, const Type *>::iterator Pos
4049f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    = ImportedTypes.find(fromTy);
4050169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  if (Pos != ImportedTypes.end())
4051f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
40521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
4053169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Import the type
40541b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  ASTNodeImporter Importer(*this);
4055f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  QualType ToT = Importer.Visit(fromTy);
40561b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (ToT.isNull())
40571b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToT;
40581b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
4059169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor  // Record the imported type.
4060f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  ImportedTypes[fromTy] = ToT.getTypePtr();
4061169fba50da76d71723cd1d91629cabb310f8bf9eDouglas Gregor
4062f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
40631b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
40641b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
40659bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorTypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
406682fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (!FromTSI)
406782fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return FromTSI;
406882fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
406982fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  // FIXME: For now we just create a "trivial" type source info based
40705606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky  // on the type and a single location. Implement a real version of this.
407182fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  QualType T = Import(FromTSI->getType());
407282fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  if (T.isNull())
407382fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor    return 0;
407482fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor
407582fc4bfa6f8ea35488b9038dd83d40766c3645cfDouglas Gregor  return ToContext.getTrivialTypeSourceInfo(T,
4076bd054dba8a3023821f2a0951b0fae05e3522a7c9Abramo Bagnara                        FromTSI->getTypeLoc().getSourceRange().getBegin());
40779bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
40789bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40799bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorDecl *ASTImporter::Import(Decl *FromD) {
40809bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromD)
40819bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
40829bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40831cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  ASTNodeImporter Importer(*this);
40841cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
40859bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Check whether we've already imported this declaration.
40869bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
40871cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  if (Pos != ImportedDecls.end()) {
40881cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    Decl *ToD = Pos->second;
40891cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    Importer.ImportDefinitionIfNeeded(FromD, ToD);
40901cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    return ToD;
40911cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor  }
40929bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40939bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Import the type
40949bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  Decl *ToD = Importer.Visit(FromD);
40959bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!ToD)
40969bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
40979bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
40989bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // Record the imported declaration.
40999bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  ImportedDecls[FromD] = ToD;
4100ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
4101ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4102ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // Keep track of anonymous tags that have an associated typedef.
4103162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    if (FromTag->getTypedefNameForAnonDecl())
4104ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      AnonTagsWithPendingTypedefs.push_back(FromTag);
4105162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
4106ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // When we've finished transforming a typedef, see whether it was the
4107ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    // typedef for an anonymous tag.
41085f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    for (SmallVector<TagDecl *, 4>::iterator
4109ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor               FromTag = AnonTagsWithPendingTypedefs.begin(),
4110ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor            FromTagEnd = AnonTagsWithPendingTypedefs.end();
4111ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor         FromTag != FromTagEnd; ++FromTag) {
4112162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
4113ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4114ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          // We found the typedef for an anonymous tag; link them.
4115162e1c1b487352434552147967c3dd296ebee2f7Richard Smith          ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
4116ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          AnonTagsWithPendingTypedefs.erase(FromTag);
4117ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor          break;
4118ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor        }
4119ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor      }
4120ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    }
4121ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  }
4122ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
41239bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return ToD;
41249bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
41259bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
41269bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorDeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
41279bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromDC)
41289bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return FromDC;
41299bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
41309bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
41319bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
41329bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
41339bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorExpr *ASTImporter::Import(Expr *FromE) {
41349bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromE)
41359bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
41369bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
41379bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
41389bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
41399bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
41409bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorStmt *ASTImporter::Import(Stmt *FromS) {
41419bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromS)
41429bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
41439bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
41444800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Check whether we've already imported this declaration.
41454800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
41464800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (Pos != ImportedStmts.end())
41474800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return Pos->second;
41484800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
41494800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Import the type
41504800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  ASTNodeImporter Importer(*this);
41514800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  Stmt *ToS = Importer.Visit(FromS);
41524800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  if (!ToS)
41534800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor    return 0;
41544800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor
41554800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  // Record the imported declaration.
41564800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  ImportedStmts[FromS] = ToS;
41574800d95d28b20eca5d57c108ae3d2e6e312c1182Douglas Gregor  return ToS;
41589bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
41599bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
41609bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorNestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
41619bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (!FromNNS)
41629bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return 0;
41639bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
41648703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
41658703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor
41668703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  switch (FromNNS->getKind()) {
41678703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  case NestedNameSpecifier::Identifier:
41688703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
41698703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor      return NestedNameSpecifier::Create(ToContext, prefix, II);
41708703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    }
41718703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    return 0;
41728703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor
41738703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  case NestedNameSpecifier::Namespace:
41748703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    if (NamespaceDecl *NS =
41758703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor          cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
41768703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor      return NestedNameSpecifier::Create(ToContext, prefix, NS);
41778703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    }
41788703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    return 0;
41798703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor
41808703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  case NestedNameSpecifier::NamespaceAlias:
41818703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    if (NamespaceAliasDecl *NSAD =
41828703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor          cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
41838703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor      return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
41848703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    }
41858703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    return 0;
41868703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor
41878703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  case NestedNameSpecifier::Global:
41888703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    return NestedNameSpecifier::GlobalSpecifier(ToContext);
41898703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor
41908703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  case NestedNameSpecifier::TypeSpec:
41918703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
41928703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor      QualType T = Import(QualType(FromNNS->getAsType(), 0u));
41938703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor      if (!T.isNull()) {
41948703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor        bool bTemplate = FromNNS->getKind() ==
41958703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor                         NestedNameSpecifier::TypeSpecWithTemplate;
41968703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor        return NestedNameSpecifier::Create(ToContext, prefix,
41978703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor                                           bTemplate, T.getTypePtr());
41988703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor      }
41998703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    }
42008703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor    return 0;
42018703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  }
42028703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor
42038703b1c8f95a43c0208ae8acc6b209d99c11f386Douglas Gregor  llvm_unreachable("Invalid nested name specifier kind");
42049bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return 0;
42059bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
42069bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
4207c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas GregorNestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4208c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  // FIXME: Implement!
4209c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  return NestedNameSpecifierLoc();
4210c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor}
4211c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor
4212d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorTemplateName ASTImporter::Import(TemplateName From) {
4213d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  switch (From.getKind()) {
4214d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::Template:
4215d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (TemplateDecl *ToTemplate
4216d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4217d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateName(ToTemplate);
4218d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4219d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateName();
4220d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4221d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::OverloadedTemplate: {
4222d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4223d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    UnresolvedSet<2> ToTemplates;
4224d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4225d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                             E = FromStorage->end();
4226d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor         I != E; ++I) {
4227d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4228d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        ToTemplates.addDecl(To);
4229d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      else
4230d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        return TemplateName();
4231d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
4232d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4233d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                               ToTemplates.end());
4234d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
4235d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4236d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::QualifiedTemplate: {
4237d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4238d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4239d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Qualifier)
4240d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateName();
4241d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4242d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (TemplateDecl *ToTemplate
4243d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor        = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4244d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return ToContext.getQualifiedTemplateName(Qualifier,
4245d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                QTN->hasTemplateKeyword(),
4246d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                ToTemplate);
4247d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4248d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return TemplateName();
4249d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
4250d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4251d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  case TemplateName::DependentTemplate: {
4252d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    DependentTemplateName *DTN = From.getAsDependentTemplateName();
4253d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4254d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (!Qualifier)
4255d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return TemplateName();
4256d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4257d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    if (DTN->isIdentifier()) {
4258d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor      return ToContext.getDependentTemplateName(Qualifier,
4259d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor                                                Import(DTN->getIdentifier()));
4260d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    }
4261d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4262d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor    return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4263d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
4264146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
4265146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParm: {
4266146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmStorage *subst
4267146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = From.getAsSubstTemplateTemplateParm();
4268146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateTemplateParmDecl *param
4269146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4270146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    if (!param)
4271146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      return TemplateName();
4272146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
4273146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateName replacement = Import(subst->getReplacement());
4274146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    if (replacement.isNull()) return TemplateName();
4275146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
4276146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return ToContext.getSubstTemplateTemplateParm(param, replacement);
4277146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
42781aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
42791aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  case TemplateName::SubstTemplateTemplateParmPack: {
42801aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    SubstTemplateTemplateParmPackStorage *SubstPack
42811aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor      = From.getAsSubstTemplateTemplateParmPack();
42821aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    TemplateTemplateParmDecl *Param
42831aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor      = cast_or_null<TemplateTemplateParmDecl>(
42841aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                        Import(SubstPack->getParameterPack()));
42851aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    if (!Param)
42861aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor      return TemplateName();
42871aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
42881aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    ASTNodeImporter Importer(*this);
42891aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    TemplateArgument ArgPack
42901aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor      = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
42911aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    if (ArgPack.isNull())
42921aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor      return TemplateName();
42931aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
42941aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
42951aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
4296d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  }
4297d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
4298d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  llvm_unreachable("Invalid template name kind");
4299d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor  return TemplateName();
4300d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor}
4301d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas Gregor
43029bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorSourceLocation ASTImporter::Import(SourceLocation FromLoc) {
43039bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  if (FromLoc.isInvalid())
43049bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    return SourceLocation();
43059bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
4306885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &FromSM = FromContext.getSourceManager();
4307885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4308885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // For now, map everything down to its spelling location, so that we
4309b10aa3e3a26f84d1a0dda6e4c057f83a11a3076cChandler Carruth  // don't have to import macro expansions.
4310b10aa3e3a26f84d1a0dda6e4c057f83a11a3076cChandler Carruth  // FIXME: Import macro expansions!
4311885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  FromLoc = FromSM.getSpellingLoc(FromLoc);
4312885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4313885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &ToSM = ToContext.getSourceManager();
4314885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4315a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis             .getLocWithOffset(Decomposed.second);
43169bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
43179bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
43189bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorSourceRange ASTImporter::Import(SourceRange FromRange) {
43199bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
43209bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
43219bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
4322885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas GregorFileID ASTImporter::Import(FileID FromID) {
4323535a3e20104461c136654d59fb833ae80644ae79Sebastian Redl  llvm::DenseMap<FileID, FileID>::iterator Pos
4324535a3e20104461c136654d59fb833ae80644ae79Sebastian Redl    = ImportedFileIDs.find(FromID);
4325885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  if (Pos != ImportedFileIDs.end())
4326885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    return Pos->second;
4327885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4328885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &FromSM = FromContext.getSourceManager();
4329885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceManager &ToSM = ToContext.getSourceManager();
4330885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4331b10aa3e3a26f84d1a0dda6e4c057f83a11a3076cChandler Carruth  assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
4332885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4333885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // Include location of this file.
4334885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4335885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4336885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  // Map the FileID for to the "to" source manager.
4337885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  FileID ToID;
4338885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
4339b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis  if (Cache->OrigEntry) {
4340885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4341885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // disk again
4342885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4343885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // than mmap the files several times.
4344b1c86492f9a9bef01a4567408c22f961bbd604feArgyrios Kyrtzidis    const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
4345885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4346885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                             FromSLoc.getFile().getFileCharacteristic());
4347885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  } else {
4348885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    // FIXME: We want to re-use the existing MemoryBuffer!
434933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    const llvm::MemoryBuffer *
435033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis        FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
4351885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    llvm::MemoryBuffer *ToBuf
4352a0a270c0f1c0a4e3482438bdc5f4a7bd3d25f0a6Chris Lattner      = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
4353885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor                                             FromBuf->getBufferIdentifier());
4354885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor    ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4355885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  }
4356885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4357885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4358535a3e20104461c136654d59fb833ae80644ae79Sebastian Redl  ImportedFileIDs[FromID] = ToID;
4359885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor  return ToID;
4360885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor}
4361885237354fd902998c6ae9d7cc3dc8de96b123dcDouglas Gregor
4362d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregorvoid ASTImporter::ImportDefinition(Decl *From) {
4363d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor  Decl *To = Import(From);
4364d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor  if (!To)
4365d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    return;
4366d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor
4367d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor  if (DeclContext *FromDC = cast<DeclContext>(From)) {
4368d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    ASTNodeImporter Importer(*this);
4369673e775beba71a69dbab4fcd733a84f4cfe2ebfcSean Callanan
4370673e775beba71a69dbab4fcd733a84f4cfe2ebfcSean Callanan    if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4371673e775beba71a69dbab4fcd733a84f4cfe2ebfcSean Callanan      if (!ToRecord->getDefinition()) {
4372673e775beba71a69dbab4fcd733a84f4cfe2ebfcSean Callanan        Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
4373673e775beba71a69dbab4fcd733a84f4cfe2ebfcSean Callanan                                  /*ForceImport=*/true);
4374673e775beba71a69dbab4fcd733a84f4cfe2ebfcSean Callanan        return;
4375673e775beba71a69dbab4fcd733a84f4cfe2ebfcSean Callanan      }
4376673e775beba71a69dbab4fcd733a84f4cfe2ebfcSean Callanan    }
43771cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
43781cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
43791cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor      if (!ToEnum->getDefinition()) {
43801cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor        Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
43811cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor                                  /*ForceImport=*/true);
43821cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor        return;
43831cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor      }
43841cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor    }
43851cf038c516c5e1f56ea9cff61aac7f7841744a91Douglas Gregor
4386d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor    Importer.ImportDeclContext(FromDC, true);
4387d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor  }
4388d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor}
4389d8868a634d4fd362243dc646e58c3cf956c81dcdDouglas Gregor
43901b2949d27ec72894dec017c330c0548af4bb2476Douglas GregorDeclarationName ASTImporter::Import(DeclarationName FromName) {
43911b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!FromName)
43921b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return DeclarationName();
43931b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
43941b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  switch (FromName.getNameKind()) {
43951b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::Identifier:
43961b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Import(FromName.getAsIdentifierInfo());
43971b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
43981b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCZeroArgSelector:
43991b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCOneArgSelector:
44001b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::ObjCMultiArgSelector:
44011b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return Import(FromName.getObjCSelector());
44021b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44031b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXConstructorName: {
44041b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
44051b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
44061b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
44071b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44081b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXConstructorName(
44091b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
44101b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
44111b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44121b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXDestructorName: {
44131b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
44141b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
44151b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
44161b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44171b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXDestructorName(
44181b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
44191b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
44201b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44211b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXConversionFunctionName: {
44221b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    QualType T = Import(FromName.getCXXNameType());
44231b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    if (T.isNull())
44241b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor      return DeclarationName();
44251b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44261b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXConversionFunctionName(
44271b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                               ToContext.getCanonicalType(T));
44281b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
44291b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44301b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXOperatorName:
44311b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXOperatorName(
44321b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                          FromName.getCXXOverloadedOperator());
44331b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44341b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXLiteralOperatorName:
44351b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return ToContext.DeclarationNames.getCXXLiteralOperatorName(
44361b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor                                   Import(FromName.getCXXLiteralIdentifier()));
44371b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44381b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  case DeclarationName::CXXUsingDirective:
44391b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    // FIXME: STATICS!
44401b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return DeclarationName::getUsingDirectiveName();
44411b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  }
44421b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44431b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  // Silence bogus GCC warning
44441b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return DeclarationName();
44451b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
44461b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
4447d5dc83a85c1b9aa32f8262126183df5d71c357aeDouglas GregorIdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
44481b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  if (!FromId)
44491b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor    return 0;
44501b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor
44511b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor  return &ToContext.Idents.get(FromId->getName());
44521b2949d27ec72894dec017c330c0548af4bb2476Douglas Gregor}
4453089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
4454c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas GregorSelector ASTImporter::Import(Selector FromSel) {
4455c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  if (FromSel.isNull())
4456c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    return Selector();
4457c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
44585f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 4> Idents;
4459c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4460c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4461c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor    Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4462c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor  return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4463c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor}
4464c3f2d2bb839593eed8861bce14228df0faf7b6c0Douglas Gregor
4465089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4466089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                DeclContext *DC,
4467089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                unsigned IDNS,
4468089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                NamedDecl **Decls,
4469089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor                                                unsigned NumDecls) {
4470089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor  return Name;
4471089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
4472089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
4473089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
447433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  return ToContext.getDiagnostics().Report(Loc, DiagID);
4475089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
4476089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor
4477089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas GregorDiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
447833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  return FromContext.getDiagnostics().Report(Loc, DiagID);
4479089459a16bf7e9cd10617d1fac5ec0240a0a1ee6Douglas Gregor}
44805ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor
44815ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas GregorDecl *ASTImporter::Imported(Decl *From, Decl *To) {
44825ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  ImportedDecls[From] = To;
44835ce5dab3c30e4255b8f62b148b6a86f09a444aaaDouglas Gregor  return To;
4484af667588d53de22795c5304f1496ccaac2a71402Daniel Dunbar}
4485ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
4486ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregorbool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
4487f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  llvm::DenseMap<const Type *, const Type *>::iterator Pos
4488ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor   = ImportedTypes.find(From.getTypePtr());
4489ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor  if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4490ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor    return true;
4491ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor
449233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
4493bb2d176c4f434e9e73f63f2f9d128f63ce3e10d0Benjamin Kramer  return Ctx.IsStructurallyEquivalent(From, To);
4494ea35d11905f756ad33b87bd89cd3ac1e7ce57994Douglas Gregor}
4495