blob: cca43eed3e62bc11db042d68780fcf9d0416acd4 [file] [log] [blame]
Douglas Gregor96e578d2010-02-05 17:54:41 +00001//===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTImporter class which imports AST nodes from one
11// context into another context.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTImporter.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000015#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000016#include "clang/AST/ASTDiagnostic.h"
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +000017#include "clang/AST/ASTStructuralEquivalence.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000022#include "clang/AST/TypeVisitor.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000023#include "clang/Basic/FileManager.h"
24#include "clang/Basic/SourceManager.h"
25#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor3996e242010-02-15 22:01:00 +000026#include <deque>
Douglas Gregor96e578d2010-02-05 17:54:41 +000027
Douglas Gregor3c2404b2011-11-03 18:07:07 +000028namespace clang {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000029 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000030 public DeclVisitor<ASTNodeImporter, Decl *>,
31 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000032 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000033
Douglas Gregor96e578d2010-02-05 17:54:41 +000034 public:
35 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
36
37 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000038 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000039 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000040
41 // Importing types
John McCall424cec92011-01-19 06:33:43 +000042 QualType VisitType(const Type *T);
Gabor Horvath0866c2f2016-11-23 15:24:23 +000043 QualType VisitAtomicType(const AtomicType *T);
John McCall424cec92011-01-19 06:33:43 +000044 QualType VisitBuiltinType(const BuiltinType *T);
Aleksei Sidorina693b372016-09-28 10:16:56 +000045 QualType VisitDecayedType(const DecayedType *T);
John McCall424cec92011-01-19 06:33:43 +000046 QualType VisitComplexType(const ComplexType *T);
47 QualType VisitPointerType(const PointerType *T);
48 QualType VisitBlockPointerType(const BlockPointerType *T);
49 QualType VisitLValueReferenceType(const LValueReferenceType *T);
50 QualType VisitRValueReferenceType(const RValueReferenceType *T);
51 QualType VisitMemberPointerType(const MemberPointerType *T);
52 QualType VisitConstantArrayType(const ConstantArrayType *T);
53 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
54 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000055 // FIXME: DependentSizedArrayType
56 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +000057 QualType VisitVectorType(const VectorType *T);
58 QualType VisitExtVectorType(const ExtVectorType *T);
59 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
60 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +000061 QualType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
Sean Callananda6df8a2011-08-11 16:56:07 +000062 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +000063 QualType VisitTypedefType(const TypedefType *T);
64 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000065 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +000066 QualType VisitTypeOfType(const TypeOfType *T);
67 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +000068 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +000069 QualType VisitAutoType(const AutoType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000070 QualType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000071 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +000072 QualType VisitRecordType(const RecordType *T);
73 QualType VisitEnumType(const EnumType *T);
Sean Callanan72fe0852015-04-02 23:50:08 +000074 QualType VisitAttributedType(const AttributedType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000075 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
Aleksei Sidorin855086d2017-01-23 09:30:36 +000076 QualType VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T);
John McCall424cec92011-01-19 06:33:43 +000077 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
78 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000079 // FIXME: DependentNameType
Gabor Horvath7a91c082017-11-14 11:30:38 +000080 QualType VisitPackExpansionType(const PackExpansionType *T);
John McCallc392f372010-06-11 00:33:02 +000081 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000082 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
83 QualType VisitObjCObjectType(const ObjCObjectType *T);
84 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000085
Douglas Gregor95d82832012-01-24 18:36:04 +000086 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000087 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
88 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +000089 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +000090 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000091 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
92 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000093 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000094
Aleksei Sidorina693b372016-09-28 10:16:56 +000095 bool ImportCastPath(CastExpr *E, CXXCastPath &Path);
96
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000097 typedef DesignatedInitExpr::Designator Designator;
98 Designator ImportDesignator(const Designator &D);
99
Douglas Gregor2e15c842012-02-01 21:00:38 +0000100
Douglas Gregor95d82832012-01-24 18:36:04 +0000101 /// \brief What we should import from the definition.
102 enum ImportDefinitionKind {
103 /// \brief Import the default subset of the definition, which might be
104 /// nothing (if minimal import is set) or might be everything (if minimal
105 /// import is not set).
106 IDK_Default,
107 /// \brief Import everything.
108 IDK_Everything,
109 /// \brief Import only the bare bones needed to establish a valid
110 /// DeclContext.
111 IDK_Basic
112 };
113
Douglas Gregor2e15c842012-02-01 21:00:38 +0000114 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
115 return IDK == IDK_Everything ||
116 (IDK == IDK_Default && !Importer.isMinimalImport());
117 }
118
Douglas Gregord451ea92011-07-29 23:31:30 +0000119 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000120 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000121 bool ImportDefinition(VarDecl *From, VarDecl *To,
122 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000123 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000124 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000125 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000126 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000127 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000128 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000129 TemplateParameterList *ImportTemplateParameterList(
130 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000131 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000132 Optional<TemplateArgumentLoc> ImportTemplateArgumentLoc(
133 const TemplateArgumentLoc &TALoc);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000134 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
135 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000136 SmallVectorImpl<TemplateArgument> &ToArgs);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000137 template <typename InContainerTy>
138 bool ImportTemplateArgumentListInfo(const InContainerTy &Container,
139 TemplateArgumentListInfo &ToTAInfo);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000140 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
141 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000142 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
143 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000144 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000145 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000146 bool IsStructuralMatch(FunctionTemplateDecl *From,
147 FunctionTemplateDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000148 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000149 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000150 Decl *VisitDecl(Decl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000151 Decl *VisitEmptyDecl(EmptyDecl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000152 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000153 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000154 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000155 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000156 Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000157 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000158 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000159 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000160 Decl *VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000161 Decl *VisitLabelDecl(LabelDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000162 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000163 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000164 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000165 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000166 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
167 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
168 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
169 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000170 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000171 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000172 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000173 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000174 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000175 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000176 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000177 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000178 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000179 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000180 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000181 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000182 Decl *VisitUsingDecl(UsingDecl *D);
183 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
184 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
185 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
186 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
187
Douglas Gregor85f3f952015-07-07 03:57:15 +0000188
189 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000190 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000191 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000192 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000193 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000194 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000195 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
196 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
197 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
198 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000199 Decl *VisitClassTemplateSpecializationDecl(
200 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000201 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
202 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000203 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000204
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000205 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000206 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
207
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000208 Stmt *VisitStmt(Stmt *S);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000209 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000210 Stmt *VisitDeclStmt(DeclStmt *S);
211 Stmt *VisitNullStmt(NullStmt *S);
212 Stmt *VisitCompoundStmt(CompoundStmt *S);
213 Stmt *VisitCaseStmt(CaseStmt *S);
214 Stmt *VisitDefaultStmt(DefaultStmt *S);
215 Stmt *VisitLabelStmt(LabelStmt *S);
216 Stmt *VisitAttributedStmt(AttributedStmt *S);
217 Stmt *VisitIfStmt(IfStmt *S);
218 Stmt *VisitSwitchStmt(SwitchStmt *S);
219 Stmt *VisitWhileStmt(WhileStmt *S);
220 Stmt *VisitDoStmt(DoStmt *S);
221 Stmt *VisitForStmt(ForStmt *S);
222 Stmt *VisitGotoStmt(GotoStmt *S);
223 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
224 Stmt *VisitContinueStmt(ContinueStmt *S);
225 Stmt *VisitBreakStmt(BreakStmt *S);
226 Stmt *VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000227 // FIXME: MSAsmStmt
228 // FIXME: SEHExceptStmt
229 // FIXME: SEHFinallyStmt
230 // FIXME: SEHTryStmt
231 // FIXME: SEHLeaveStmt
232 // FIXME: CapturedStmt
233 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
234 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
235 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
236 // FIXME: MSDependentExistsStmt
237 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
238 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
239 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
240 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
241 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
242 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
243 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000244
245 // Importing expressions
246 Expr *VisitExpr(Expr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000247 Expr *VisitVAArgExpr(VAArgExpr *E);
248 Expr *VisitGNUNullExpr(GNUNullExpr *E);
249 Expr *VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000250 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000251 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
252 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
253 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000254 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000255 Expr *VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000256 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000257 Expr *VisitStringLiteral(StringLiteral *E);
258 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
259 Expr *VisitAtomicExpr(AtomicExpr *E);
260 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000261 Expr *VisitParenExpr(ParenExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000262 Expr *VisitParenListExpr(ParenListExpr *E);
263 Expr *VisitStmtExpr(StmtExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000264 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000265 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000266 Expr *VisitBinaryOperator(BinaryOperator *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000267 Expr *VisitConditionalOperator(ConditionalOperator *E);
268 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
269 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000270 Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
271 Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E);
272 Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000273 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000274 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000275 Expr *VisitExplicitCastExpr(ExplicitCastExpr *E);
276 Expr *VisitOffsetOfExpr(OffsetOfExpr *OE);
277 Expr *VisitCXXThrowExpr(CXXThrowExpr *E);
278 Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
279 Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
280 Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
281 Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
282 Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE);
283 Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000284 Expr *VisitPackExpansionExpr(PackExpansionExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000285 Expr *VisitCXXNewExpr(CXXNewExpr *CE);
286 Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000287 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000288 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000289 Expr *VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000290 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC);
Sean Callanan8bca9962016-03-28 21:43:01 +0000291 Expr *VisitCXXThisExpr(CXXThisExpr *E);
292 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +0000293 Expr *VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000294 Expr *VisitMemberExpr(MemberExpr *E);
295 Expr *VisitCallExpr(CallExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000296 Expr *VisitInitListExpr(InitListExpr *E);
Richard Smith30e304e2016-12-14 00:03:17 +0000297 Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
298 Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
Sean Callanandd2c1742016-05-16 20:48:03 +0000299 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
300 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000301 Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +0000302 Expr *VisitTypeTraitExpr(TypeTraitExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000303
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000304
305 template<typename IIter, typename OIter>
306 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
307 typedef typename std::remove_reference<decltype(*Obegin)>::type ItemT;
308 ASTImporter &ImporterRef = Importer;
309 std::transform(Ibegin, Iend, Obegin,
310 [&ImporterRef](ItemT From) -> ItemT {
311 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000312 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000313 }
314
315 template<typename IIter, typename OIter>
316 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
317 typedef typename std::remove_reference<decltype(**Obegin)>::type ItemT;
318 ASTImporter &ImporterRef = Importer;
319 bool Failed = false;
320 std::transform(Ibegin, Iend, Obegin,
321 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000322 ItemT *To = cast_or_null<ItemT>(
323 ImporterRef.Import(From));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000324 if (!To && From)
325 Failed = true;
326 return To;
327 });
328 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000329 }
Aleksei Sidorina693b372016-09-28 10:16:56 +0000330
331 template<typename InContainerTy, typename OutContainerTy>
332 bool ImportContainerChecked(const InContainerTy &InContainer,
333 OutContainerTy &OutContainer) {
334 return ImportArrayChecked(InContainer.begin(), InContainer.end(),
335 OutContainer.begin());
336 }
337
338 template<typename InContainerTy, typename OIter>
339 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
340 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
341 }
Lang Hames19e07e12017-06-20 21:06:00 +0000342
343 // Importing overrides.
344 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000345 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000346}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000347
Douglas Gregor3996e242010-02-15 22:01:00 +0000348//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000349// Import Types
350//----------------------------------------------------------------------------
351
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000352using namespace clang;
353
John McCall424cec92011-01-19 06:33:43 +0000354QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000355 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
356 << T->getTypeClassName();
357 return QualType();
358}
359
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000360QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
361 QualType UnderlyingType = Importer.Import(T->getValueType());
362 if(UnderlyingType.isNull())
363 return QualType();
364
365 return Importer.getToContext().getAtomicType(UnderlyingType);
366}
367
John McCall424cec92011-01-19 06:33:43 +0000368QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000369 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000370#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
371 case BuiltinType::Id: \
372 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000373#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000374#define SHARED_SINGLETON_TYPE(Expansion)
375#define BUILTIN_TYPE(Id, SingletonId) \
376 case BuiltinType::Id: return Importer.getToContext().SingletonId;
377#include "clang/AST/BuiltinTypes.def"
378
379 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
380 // context supports C++.
381
382 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
383 // context supports ObjC.
384
Douglas Gregor96e578d2010-02-05 17:54:41 +0000385 case BuiltinType::Char_U:
386 // The context we're importing from has an unsigned 'char'. If we're
387 // importing into a context with a signed 'char', translate to
388 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000389 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000390 return Importer.getToContext().UnsignedCharTy;
391
392 return Importer.getToContext().CharTy;
393
Douglas Gregor96e578d2010-02-05 17:54:41 +0000394 case BuiltinType::Char_S:
395 // The context we're importing from has an unsigned 'char'. If we're
396 // importing into a context with a signed 'char', translate to
397 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000398 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000399 return Importer.getToContext().SignedCharTy;
400
401 return Importer.getToContext().CharTy;
402
Chris Lattnerad3467e2010-12-25 23:25:43 +0000403 case BuiltinType::WChar_S:
404 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000405 // FIXME: If not in C++, shall we translate to the C equivalent of
406 // wchar_t?
407 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000408 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000409
410 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000411}
412
Aleksei Sidorina693b372016-09-28 10:16:56 +0000413QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
414 QualType OrigT = Importer.Import(T->getOriginalType());
415 if (OrigT.isNull())
416 return QualType();
417
418 return Importer.getToContext().getDecayedType(OrigT);
419}
420
John McCall424cec92011-01-19 06:33:43 +0000421QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000422 QualType ToElementType = Importer.Import(T->getElementType());
423 if (ToElementType.isNull())
424 return QualType();
425
426 return Importer.getToContext().getComplexType(ToElementType);
427}
428
John McCall424cec92011-01-19 06:33:43 +0000429QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000430 QualType ToPointeeType = Importer.Import(T->getPointeeType());
431 if (ToPointeeType.isNull())
432 return QualType();
433
434 return Importer.getToContext().getPointerType(ToPointeeType);
435}
436
John McCall424cec92011-01-19 06:33:43 +0000437QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000438 // FIXME: Check for blocks support in "to" context.
439 QualType ToPointeeType = Importer.Import(T->getPointeeType());
440 if (ToPointeeType.isNull())
441 return QualType();
442
443 return Importer.getToContext().getBlockPointerType(ToPointeeType);
444}
445
John McCall424cec92011-01-19 06:33:43 +0000446QualType
447ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000448 // FIXME: Check for C++ support in "to" context.
449 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
450 if (ToPointeeType.isNull())
451 return QualType();
452
453 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
454}
455
John McCall424cec92011-01-19 06:33:43 +0000456QualType
457ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000458 // FIXME: Check for C++0x support in "to" context.
459 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
460 if (ToPointeeType.isNull())
461 return QualType();
462
463 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
464}
465
John McCall424cec92011-01-19 06:33:43 +0000466QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000467 // FIXME: Check for C++ support in "to" context.
468 QualType ToPointeeType = Importer.Import(T->getPointeeType());
469 if (ToPointeeType.isNull())
470 return QualType();
471
472 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
473 return Importer.getToContext().getMemberPointerType(ToPointeeType,
474 ClassType.getTypePtr());
475}
476
John McCall424cec92011-01-19 06:33:43 +0000477QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000478 QualType ToElementType = Importer.Import(T->getElementType());
479 if (ToElementType.isNull())
480 return QualType();
481
482 return Importer.getToContext().getConstantArrayType(ToElementType,
483 T->getSize(),
484 T->getSizeModifier(),
485 T->getIndexTypeCVRQualifiers());
486}
487
John McCall424cec92011-01-19 06:33:43 +0000488QualType
489ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000490 QualType ToElementType = Importer.Import(T->getElementType());
491 if (ToElementType.isNull())
492 return QualType();
493
494 return Importer.getToContext().getIncompleteArrayType(ToElementType,
495 T->getSizeModifier(),
496 T->getIndexTypeCVRQualifiers());
497}
498
John McCall424cec92011-01-19 06:33:43 +0000499QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000500 QualType ToElementType = Importer.Import(T->getElementType());
501 if (ToElementType.isNull())
502 return QualType();
503
504 Expr *Size = Importer.Import(T->getSizeExpr());
505 if (!Size)
506 return QualType();
507
508 SourceRange Brackets = Importer.Import(T->getBracketsRange());
509 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
510 T->getSizeModifier(),
511 T->getIndexTypeCVRQualifiers(),
512 Brackets);
513}
514
John McCall424cec92011-01-19 06:33:43 +0000515QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000516 QualType ToElementType = Importer.Import(T->getElementType());
517 if (ToElementType.isNull())
518 return QualType();
519
520 return Importer.getToContext().getVectorType(ToElementType,
521 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +0000522 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000523}
524
John McCall424cec92011-01-19 06:33:43 +0000525QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000526 QualType ToElementType = Importer.Import(T->getElementType());
527 if (ToElementType.isNull())
528 return QualType();
529
530 return Importer.getToContext().getExtVectorType(ToElementType,
531 T->getNumElements());
532}
533
John McCall424cec92011-01-19 06:33:43 +0000534QualType
535ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000536 // FIXME: What happens if we're importing a function without a prototype
537 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +0000538 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000539 if (ToResultType.isNull())
540 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000541
Douglas Gregor96e578d2010-02-05 17:54:41 +0000542 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000543 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000544}
545
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +0000546QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000547 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000548 if (ToResultType.isNull())
549 return QualType();
550
551 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000552 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000553 for (const auto &A : T->param_types()) {
554 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000555 if (ArgType.isNull())
556 return QualType();
557 ArgTypes.push_back(ArgType);
558 }
559
560 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000561 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000562 for (const auto &E : T->exceptions()) {
563 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000564 if (ExceptionType.isNull())
565 return QualType();
566 ExceptionTypes.push_back(ExceptionType);
567 }
John McCalldb40c7f2010-12-14 08:05:40 +0000568
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000569 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
570 FunctionProtoType::ExtProtoInfo ToEPI;
571
572 ToEPI.ExtInfo = FromEPI.ExtInfo;
573 ToEPI.Variadic = FromEPI.Variadic;
574 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
575 ToEPI.TypeQuals = FromEPI.TypeQuals;
576 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +0000577 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
578 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
579 ToEPI.ExceptionSpec.NoexceptExpr =
580 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
581 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
582 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
583 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
584 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000585
Jordan Rose5c382722013-03-08 21:51:21 +0000586 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000587}
588
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000589QualType ASTNodeImporter::VisitUnresolvedUsingType(
590 const UnresolvedUsingType *T) {
591 UnresolvedUsingTypenameDecl *ToD = cast_or_null<UnresolvedUsingTypenameDecl>(
592 Importer.Import(T->getDecl()));
593 if (!ToD)
594 return QualType();
595
596 UnresolvedUsingTypenameDecl *ToPrevD =
597 cast_or_null<UnresolvedUsingTypenameDecl>(
598 Importer.Import(T->getDecl()->getPreviousDecl()));
599 if (!ToPrevD && T->getDecl()->getPreviousDecl())
600 return QualType();
601
602 return Importer.getToContext().getTypeDeclType(ToD, ToPrevD);
603}
604
Sean Callananda6df8a2011-08-11 16:56:07 +0000605QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
606 QualType ToInnerType = Importer.Import(T->getInnerType());
607 if (ToInnerType.isNull())
608 return QualType();
609
610 return Importer.getToContext().getParenType(ToInnerType);
611}
612
John McCall424cec92011-01-19 06:33:43 +0000613QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +0000614 TypedefNameDecl *ToDecl
615 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000616 if (!ToDecl)
617 return QualType();
618
619 return Importer.getToContext().getTypeDeclType(ToDecl);
620}
621
John McCall424cec92011-01-19 06:33:43 +0000622QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000623 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
624 if (!ToExpr)
625 return QualType();
626
627 return Importer.getToContext().getTypeOfExprType(ToExpr);
628}
629
John McCall424cec92011-01-19 06:33:43 +0000630QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000631 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
632 if (ToUnderlyingType.isNull())
633 return QualType();
634
635 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
636}
637
John McCall424cec92011-01-19 06:33:43 +0000638QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +0000639 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +0000640 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
641 if (!ToExpr)
642 return QualType();
643
Douglas Gregor81495f32012-02-12 18:42:33 +0000644 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
645 if (UnderlyingType.isNull())
646 return QualType();
647
648 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000649}
650
Alexis Hunte852b102011-05-24 22:41:36 +0000651QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
652 QualType ToBaseType = Importer.Import(T->getBaseType());
653 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
654 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
655 return QualType();
656
657 return Importer.getToContext().getUnaryTransformType(ToBaseType,
658 ToUnderlyingType,
659 T->getUTTKind());
660}
661
Richard Smith30482bc2011-02-20 03:19:35 +0000662QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +0000663 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +0000664 QualType FromDeduced = T->getDeducedType();
665 QualType ToDeduced;
666 if (!FromDeduced.isNull()) {
667 ToDeduced = Importer.Import(FromDeduced);
668 if (ToDeduced.isNull())
669 return QualType();
670 }
671
Richard Smithe301ba22015-11-11 02:02:15 +0000672 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +0000673 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +0000674}
675
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000676QualType ASTNodeImporter::VisitInjectedClassNameType(
677 const InjectedClassNameType *T) {
678 CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
679 if (!D)
680 return QualType();
681
682 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
683 if (InjType.isNull())
684 return QualType();
685
686 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
687 // See comments in InjectedClassNameType definition for details
688 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
689 enum {
690 TypeAlignmentInBits = 4,
691 TypeAlignment = 1 << TypeAlignmentInBits
692 };
693
694 return QualType(new (Importer.getToContext(), TypeAlignment)
695 InjectedClassNameType(D, InjType), 0);
696}
697
John McCall424cec92011-01-19 06:33:43 +0000698QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000699 RecordDecl *ToDecl
700 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
701 if (!ToDecl)
702 return QualType();
703
704 return Importer.getToContext().getTagDeclType(ToDecl);
705}
706
John McCall424cec92011-01-19 06:33:43 +0000707QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000708 EnumDecl *ToDecl
709 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
710 if (!ToDecl)
711 return QualType();
712
713 return Importer.getToContext().getTagDeclType(ToDecl);
714}
715
Sean Callanan72fe0852015-04-02 23:50:08 +0000716QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
717 QualType FromModifiedType = T->getModifiedType();
718 QualType FromEquivalentType = T->getEquivalentType();
719 QualType ToModifiedType;
720 QualType ToEquivalentType;
721
722 if (!FromModifiedType.isNull()) {
723 ToModifiedType = Importer.Import(FromModifiedType);
724 if (ToModifiedType.isNull())
725 return QualType();
726 }
727 if (!FromEquivalentType.isNull()) {
728 ToEquivalentType = Importer.Import(FromEquivalentType);
729 if (ToEquivalentType.isNull())
730 return QualType();
731 }
732
733 return Importer.getToContext().getAttributedType(T->getAttrKind(),
734 ToModifiedType, ToEquivalentType);
735}
736
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000737
738QualType ASTNodeImporter::VisitTemplateTypeParmType(
739 const TemplateTypeParmType *T) {
740 TemplateTypeParmDecl *ParmDecl =
741 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
742 if (!ParmDecl && T->getDecl())
743 return QualType();
744
745 return Importer.getToContext().getTemplateTypeParmType(
746 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
747}
748
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000749QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
750 const SubstTemplateTypeParmType *T) {
751 const TemplateTypeParmType *Replaced =
752 cast_or_null<TemplateTypeParmType>(Importer.Import(
753 QualType(T->getReplacedParameter(), 0)).getTypePtr());
754 if (!Replaced)
755 return QualType();
756
757 QualType Replacement = Importer.Import(T->getReplacementType());
758 if (Replacement.isNull())
759 return QualType();
760 Replacement = Replacement.getCanonicalType();
761
762 return Importer.getToContext().getSubstTemplateTypeParmType(
763 Replaced, Replacement);
764}
765
Douglas Gregore2e50d332010-12-01 01:36:18 +0000766QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +0000767 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000768 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
769 if (ToTemplate.isNull())
770 return QualType();
771
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000772 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +0000773 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
774 return QualType();
775
776 QualType ToCanonType;
777 if (!QualType(T, 0).isCanonical()) {
778 QualType FromCanonType
779 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
780 ToCanonType =Importer.Import(FromCanonType);
781 if (ToCanonType.isNull())
782 return QualType();
783 }
784 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +0000785 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +0000786 ToCanonType);
787}
788
John McCall424cec92011-01-19 06:33:43 +0000789QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +0000790 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000791 // Note: the qualifier in an ElaboratedType is optional.
792 if (T->getQualifier()) {
793 ToQualifier = Importer.Import(T->getQualifier());
794 if (!ToQualifier)
795 return QualType();
796 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000797
798 QualType ToNamedType = Importer.Import(T->getNamedType());
799 if (ToNamedType.isNull())
800 return QualType();
801
Abramo Bagnara6150c882010-05-11 21:36:43 +0000802 return Importer.getToContext().getElaboratedType(T->getKeyword(),
803 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000804}
805
Gabor Horvath7a91c082017-11-14 11:30:38 +0000806QualType ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
807 QualType Pattern = Importer.Import(T->getPattern());
808 if (Pattern.isNull())
809 return QualType();
810
811 return Importer.getToContext().getPackExpansionType(Pattern,
812 T->getNumExpansions());
813}
814
John McCall424cec92011-01-19 06:33:43 +0000815QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000816 ObjCInterfaceDecl *Class
817 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
818 if (!Class)
819 return QualType();
820
John McCall8b07ec22010-05-15 11:32:37 +0000821 return Importer.getToContext().getObjCInterfaceType(Class);
822}
823
John McCall424cec92011-01-19 06:33:43 +0000824QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000825 QualType ToBaseType = Importer.Import(T->getBaseType());
826 if (ToBaseType.isNull())
827 return QualType();
828
Douglas Gregore9d95f12015-07-07 03:57:35 +0000829 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +0000830 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +0000831 QualType ImportedTypeArg = Importer.Import(TypeArg);
832 if (ImportedTypeArg.isNull())
833 return QualType();
834
835 TypeArgs.push_back(ImportedTypeArg);
836 }
837
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000838 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000839 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000840 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000841 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000842 if (!Protocol)
843 return QualType();
844 Protocols.push_back(Protocol);
845 }
846
Douglas Gregore9d95f12015-07-07 03:57:35 +0000847 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +0000848 Protocols,
849 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000850}
851
John McCall424cec92011-01-19 06:33:43 +0000852QualType
853ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000854 QualType ToPointeeType = Importer.Import(T->getPointeeType());
855 if (ToPointeeType.isNull())
856 return QualType();
857
John McCall8b07ec22010-05-15 11:32:37 +0000858 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000859}
860
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000861//----------------------------------------------------------------------------
862// Import Declarations
863//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000864bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
865 DeclContext *&LexicalDC,
866 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +0000867 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000868 SourceLocation &Loc) {
869 // Import the context of this declaration.
870 DC = Importer.ImportContext(D->getDeclContext());
871 if (!DC)
872 return true;
873
874 LexicalDC = DC;
875 if (D->getDeclContext() != D->getLexicalDeclContext()) {
876 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
877 if (!LexicalDC)
878 return true;
879 }
880
881 // Import the name of this declaration.
882 Name = Importer.Import(D->getDeclName());
883 if (D->getDeclName() && !Name)
884 return true;
885
886 // Import the location of this declaration.
887 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +0000888 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000889 return false;
890}
891
Douglas Gregord451ea92011-07-29 23:31:30 +0000892void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
893 if (!FromD)
894 return;
895
896 if (!ToD) {
897 ToD = Importer.Import(FromD);
898 if (!ToD)
899 return;
900 }
901
902 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
903 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +0000904 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +0000905 ImportDefinition(FromRecord, ToRecord);
906 }
907 }
908 return;
909 }
910
911 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
912 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
913 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
914 ImportDefinition(FromEnum, ToEnum);
915 }
916 }
917 return;
918 }
919}
920
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000921void
922ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
923 DeclarationNameInfo& To) {
924 // NOTE: To.Name and To.Loc are already imported.
925 // We only have to import To.LocInfo.
926 switch (To.getName().getNameKind()) {
927 case DeclarationName::Identifier:
928 case DeclarationName::ObjCZeroArgSelector:
929 case DeclarationName::ObjCOneArgSelector:
930 case DeclarationName::ObjCMultiArgSelector:
931 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +0000932 case DeclarationName::CXXDeductionGuideName:
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000933 return;
934
935 case DeclarationName::CXXOperatorName: {
936 SourceRange Range = From.getCXXOperatorNameRange();
937 To.setCXXOperatorNameRange(Importer.Import(Range));
938 return;
939 }
940 case DeclarationName::CXXLiteralOperatorName: {
941 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
942 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
943 return;
944 }
945 case DeclarationName::CXXConstructorName:
946 case DeclarationName::CXXDestructorName:
947 case DeclarationName::CXXConversionFunctionName: {
948 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
949 To.setNamedTypeInfo(Importer.Import(FromTInfo));
950 return;
951 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000952 }
Douglas Gregor07216d12011-11-02 20:52:01 +0000953 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000954}
955
Douglas Gregor2e15c842012-02-01 21:00:38 +0000956void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +0000957 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +0000958 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +0000959 return;
960 }
961
Aaron Ballman629afae2014-03-07 19:56:05 +0000962 for (auto *From : FromDC->decls())
963 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +0000964}
965
Douglas Gregord451ea92011-07-29 23:31:30 +0000966bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000967 ImportDefinitionKind Kind) {
968 if (To->getDefinition() || To->isBeingDefined()) {
969 if (Kind == IDK_Everything)
970 ImportDeclContext(From, /*ForceImport=*/true);
971
Douglas Gregore2e50d332010-12-01 01:36:18 +0000972 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +0000973 }
Douglas Gregore2e50d332010-12-01 01:36:18 +0000974
975 To->startDefinition();
976
977 // Add base classes.
978 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
979 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000980
981 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
982 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
983 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +0000984 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000985 ToData.Aggregate = FromData.Aggregate;
986 ToData.PlainOldData = FromData.PlainOldData;
987 ToData.Empty = FromData.Empty;
988 ToData.Polymorphic = FromData.Polymorphic;
989 ToData.Abstract = FromData.Abstract;
990 ToData.IsStandardLayout = FromData.IsStandardLayout;
991 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
992 ToData.HasPrivateFields = FromData.HasPrivateFields;
993 ToData.HasProtectedFields = FromData.HasProtectedFields;
994 ToData.HasPublicFields = FromData.HasPublicFields;
995 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +0000996 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +0000997 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +0000998 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +0000999 ToData.HasUninitializedReferenceMember
1000 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001001 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001002 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1003 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001004 ToData.NeedOverloadResolutionForCopyConstructor
1005 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001006 ToData.NeedOverloadResolutionForMoveConstructor
1007 = FromData.NeedOverloadResolutionForMoveConstructor;
1008 ToData.NeedOverloadResolutionForMoveAssignment
1009 = FromData.NeedOverloadResolutionForMoveAssignment;
1010 ToData.NeedOverloadResolutionForDestructor
1011 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001012 ToData.DefaultedCopyConstructorIsDeleted
1013 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001014 ToData.DefaultedMoveConstructorIsDeleted
1015 = FromData.DefaultedMoveConstructorIsDeleted;
1016 ToData.DefaultedMoveAssignmentIsDeleted
1017 = FromData.DefaultedMoveAssignmentIsDeleted;
1018 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001019 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1020 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001021 ToData.HasConstexprNonCopyMoveConstructor
1022 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001023 ToData.HasDefaultedDefaultConstructor
1024 = FromData.HasDefaultedDefaultConstructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001025 ToData.CanPassInRegisters = FromData.CanPassInRegisters;
Richard Smith561fb152012-02-25 07:33:38 +00001026 ToData.DefaultedDefaultConstructorIsConstexpr
1027 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001028 ToData.HasConstexprDefaultConstructor
1029 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001030 ToData.HasNonLiteralTypeFieldsOrBases
1031 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001032 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001033 ToData.UserProvidedDefaultConstructor
1034 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001035 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001036 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1037 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1038 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1039 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001040 ToData.ImplicitCopyAssignmentHasConstParam
1041 = FromData.ImplicitCopyAssignmentHasConstParam;
1042 ToData.HasDeclaredCopyConstructorWithConstParam
1043 = FromData.HasDeclaredCopyConstructorWithConstParam;
1044 ToData.HasDeclaredCopyAssignmentWithConstParam
1045 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001046 ToData.IsLambda = FromData.IsLambda;
1047
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001048 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001049 for (const auto &Base1 : FromCXX->bases()) {
1050 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001051 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001052 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001053
1054 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001055 if (Base1.isPackExpansion())
1056 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001057
1058 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00001059 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00001060
Douglas Gregore2e50d332010-12-01 01:36:18 +00001061 Bases.push_back(
1062 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00001063 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1064 Base1.isVirtual(),
1065 Base1.isBaseOfClass(),
1066 Base1.getAccessSpecifierAsWritten(),
1067 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00001068 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001069 }
1070 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001071 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001072 }
1073
Douglas Gregor2e15c842012-02-01 21:00:38 +00001074 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001075 ImportDeclContext(From, /*ForceImport=*/true);
1076
Douglas Gregore2e50d332010-12-01 01:36:18 +00001077 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001078 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001079}
1080
Larisse Voufo39a1e502013-08-06 01:03:05 +00001081bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1082 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00001083 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00001084 return false;
1085
1086 // FIXME: Can we really import any initializer? Alternatively, we could force
1087 // ourselves to import every declaration of a variable and then only use
1088 // getInit() here.
1089 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1090
1091 // FIXME: Other bits to merge?
1092
1093 return false;
1094}
1095
Douglas Gregord451ea92011-07-29 23:31:30 +00001096bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001097 ImportDefinitionKind Kind) {
1098 if (To->getDefinition() || To->isBeingDefined()) {
1099 if (Kind == IDK_Everything)
1100 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001101 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001102 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001103
1104 To->startDefinition();
1105
1106 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1107 if (T.isNull())
1108 return true;
1109
1110 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1111 if (ToPromotionType.isNull())
1112 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001113
1114 if (shouldForceImportDeclContext(Kind))
1115 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001116
1117 // FIXME: we might need to merge the number of positive or negative bits
1118 // if the enumerator lists don't match.
1119 To->completeDefinition(T, ToPromotionType,
1120 From->getNumPositiveBits(),
1121 From->getNumNegativeBits());
1122 return false;
1123}
1124
Douglas Gregora082a492010-11-30 19:14:50 +00001125TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1126 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001127 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1128 if (ImportContainerChecked(*Params, ToParams))
1129 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00001130
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001131 Expr *ToRequiresClause;
1132 if (Expr *const R = Params->getRequiresClause()) {
1133 ToRequiresClause = Importer.Import(R);
1134 if (!ToRequiresClause)
1135 return nullptr;
1136 } else {
1137 ToRequiresClause = nullptr;
1138 }
1139
Douglas Gregora082a492010-11-30 19:14:50 +00001140 return TemplateParameterList::Create(Importer.getToContext(),
1141 Importer.Import(Params->getTemplateLoc()),
1142 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00001143 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001144 Importer.Import(Params->getRAngleLoc()),
1145 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001146}
1147
Douglas Gregore2e50d332010-12-01 01:36:18 +00001148TemplateArgument
1149ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1150 switch (From.getKind()) {
1151 case TemplateArgument::Null:
1152 return TemplateArgument();
1153
1154 case TemplateArgument::Type: {
1155 QualType ToType = Importer.Import(From.getAsType());
1156 if (ToType.isNull())
1157 return TemplateArgument();
1158 return TemplateArgument(ToType);
1159 }
1160
1161 case TemplateArgument::Integral: {
1162 QualType ToType = Importer.Import(From.getIntegralType());
1163 if (ToType.isNull())
1164 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001165 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001166 }
1167
Eli Friedmanb826a002012-09-26 02:36:12 +00001168 case TemplateArgument::Declaration: {
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001169 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
1170 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1171 if (!To || ToType.isNull())
1172 return TemplateArgument();
1173 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00001174 }
1175
1176 case TemplateArgument::NullPtr: {
1177 QualType ToType = Importer.Import(From.getNullPtrType());
1178 if (ToType.isNull())
1179 return TemplateArgument();
1180 return TemplateArgument(ToType, /*isNullPtr*/true);
1181 }
1182
Douglas Gregore2e50d332010-12-01 01:36:18 +00001183 case TemplateArgument::Template: {
1184 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1185 if (ToTemplate.isNull())
1186 return TemplateArgument();
1187
1188 return TemplateArgument(ToTemplate);
1189 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001190
1191 case TemplateArgument::TemplateExpansion: {
1192 TemplateName ToTemplate
1193 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1194 if (ToTemplate.isNull())
1195 return TemplateArgument();
1196
Douglas Gregore1d60df2011-01-14 23:41:42 +00001197 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001198 }
1199
Douglas Gregore2e50d332010-12-01 01:36:18 +00001200 case TemplateArgument::Expression:
1201 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1202 return TemplateArgument(ToExpr);
1203 return TemplateArgument();
1204
1205 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001206 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001207 ToPack.reserve(From.pack_size());
1208 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1209 return TemplateArgument();
Benjamin Kramercce63472015-08-05 09:40:22 +00001210
1211 return TemplateArgument(
1212 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001213 }
1214 }
1215
1216 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00001217}
1218
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001219Optional<TemplateArgumentLoc>
1220ASTNodeImporter::ImportTemplateArgumentLoc(const TemplateArgumentLoc &TALoc) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001221 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1222 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1223 TemplateArgumentLocInfo ToInfo;
1224 if (Arg.getKind() == TemplateArgument::Expression) {
1225 Expr *E = Importer.Import(FromInfo.getAsExpr());
1226 ToInfo = TemplateArgumentLocInfo(E);
1227 if (!E)
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001228 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001229 } else if (Arg.getKind() == TemplateArgument::Type) {
1230 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1231 ToInfo = TemplateArgumentLocInfo(TSI);
1232 else
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001233 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001234 } else {
1235 ToInfo = TemplateArgumentLocInfo(
1236 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1237 Importer.Import(FromInfo.getTemplateNameLoc()),
1238 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1239 }
1240 return TemplateArgumentLoc(Arg, ToInfo);
1241}
1242
Douglas Gregore2e50d332010-12-01 01:36:18 +00001243bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1244 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001245 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001246 for (unsigned I = 0; I != NumFromArgs; ++I) {
1247 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1248 if (To.isNull() && !FromArgs[I].isNull())
1249 return true;
1250
1251 ToArgs.push_back(To);
1252 }
1253
1254 return false;
1255}
1256
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001257template <typename InContainerTy>
1258bool ASTNodeImporter::ImportTemplateArgumentListInfo(
1259 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1260 for (const auto &FromLoc : Container) {
1261 if (auto ToLoc = ImportTemplateArgumentLoc(FromLoc))
1262 ToTAInfo.addArgument(*ToLoc);
1263 else
1264 return true;
1265 }
1266 return false;
1267}
1268
Douglas Gregor5c73e912010-02-11 00:48:18 +00001269bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001270 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001271 // Eliminate a potential failure point where we attempt to re-import
1272 // something we're trying to import while completing ToRecord.
1273 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1274 if (ToOrigin) {
1275 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
1276 if (ToOriginRecord)
1277 ToRecord = ToOriginRecord;
1278 }
1279
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001280 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001281 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001282 Importer.getNonEquivalentDecls(),
1283 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001284 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001285}
1286
Larisse Voufo39a1e502013-08-06 01:03:05 +00001287bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1288 bool Complain) {
1289 StructuralEquivalenceContext Ctx(
1290 Importer.getFromContext(), Importer.getToContext(),
1291 Importer.getNonEquivalentDecls(), false, Complain);
1292 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
1293}
1294
Douglas Gregor98c10182010-02-12 22:17:39 +00001295bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001296 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001297 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001298 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001299 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001300}
1301
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001302bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1303 FunctionTemplateDecl *To) {
1304 StructuralEquivalenceContext Ctx(
1305 Importer.getFromContext(), Importer.getToContext(),
1306 Importer.getNonEquivalentDecls(), false, false);
1307 return Ctx.IsStructurallyEquivalent(From, To);
1308}
1309
Douglas Gregor91155082012-11-14 22:29:20 +00001310bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
1311 EnumConstantDecl *ToEC)
1312{
1313 const llvm::APSInt &FromVal = FromEC->getInitVal();
1314 const llvm::APSInt &ToVal = ToEC->getInitVal();
1315
1316 return FromVal.isSigned() == ToVal.isSigned() &&
1317 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1318 FromVal == ToVal;
1319}
1320
1321bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001322 ClassTemplateDecl *To) {
1323 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1324 Importer.getToContext(),
1325 Importer.getNonEquivalentDecls());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001326 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00001327}
1328
Larisse Voufo39a1e502013-08-06 01:03:05 +00001329bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1330 VarTemplateDecl *To) {
1331 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1332 Importer.getToContext(),
1333 Importer.getNonEquivalentDecls());
1334 return Ctx.IsStructurallyEquivalent(From, To);
1335}
1336
Douglas Gregore4c83e42010-02-09 22:48:33 +00001337Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001338 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001339 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00001340 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00001341}
1342
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001343Decl *ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
1344 // Import the context of this declaration.
1345 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1346 if (!DC)
1347 return nullptr;
1348
1349 DeclContext *LexicalDC = DC;
1350 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1351 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1352 if (!LexicalDC)
1353 return nullptr;
1354 }
1355
1356 // Import the location of this declaration.
1357 SourceLocation Loc = Importer.Import(D->getLocation());
1358
1359 EmptyDecl *ToD = EmptyDecl::Create(Importer.getToContext(), DC, Loc);
1360 ToD->setLexicalDeclContext(LexicalDC);
1361 Importer.Imported(D, ToD);
1362 LexicalDC->addDeclInternal(ToD);
1363 return ToD;
1364}
1365
Sean Callanan65198272011-11-17 23:20:56 +00001366Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1367 TranslationUnitDecl *ToD =
1368 Importer.getToContext().getTranslationUnitDecl();
1369
1370 Importer.Imported(D, ToD);
1371
1372 return ToD;
1373}
1374
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001375Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
1376
1377 SourceLocation Loc = Importer.Import(D->getLocation());
1378 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1379
1380 // Import the context of this declaration.
1381 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1382 if (!DC)
1383 return nullptr;
1384
1385 AccessSpecDecl *accessSpecDecl
1386 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
1387 DC, Loc, ColonLoc);
1388
1389 if (!accessSpecDecl)
1390 return nullptr;
1391
1392 // Lexical DeclContext and Semantic DeclContext
1393 // is always the same for the accessSpec.
1394 accessSpecDecl->setLexicalDeclContext(DC);
1395 DC->addDeclInternal(accessSpecDecl);
1396
1397 return accessSpecDecl;
1398}
1399
Aleksei Sidorina693b372016-09-28 10:16:56 +00001400Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1401 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1402 if (!DC)
1403 return nullptr;
1404
1405 DeclContext *LexicalDC = DC;
1406
1407 // Import the location of this declaration.
1408 SourceLocation Loc = Importer.Import(D->getLocation());
1409
1410 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1411 if (!AssertExpr)
1412 return nullptr;
1413
1414 StringLiteral *FromMsg = D->getMessage();
1415 StringLiteral *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
1416 if (!ToMsg && FromMsg)
1417 return nullptr;
1418
1419 StaticAssertDecl *ToD = StaticAssertDecl::Create(
1420 Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1421 Importer.Import(D->getRParenLoc()), D->isFailed());
1422
1423 ToD->setLexicalDeclContext(LexicalDC);
1424 LexicalDC->addDeclInternal(ToD);
1425 Importer.Imported(D, ToD);
1426 return ToD;
1427}
1428
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001429Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1430 // Import the major distinguishing characteristics of this namespace.
1431 DeclContext *DC, *LexicalDC;
1432 DeclarationName Name;
1433 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001434 NamedDecl *ToD;
1435 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001436 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001437 if (ToD)
1438 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001439
1440 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001441 if (!Name) {
1442 // This is an anonymous namespace. Adopt an existing anonymous
1443 // namespace if we can.
1444 // FIXME: Not testable.
1445 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1446 MergeWithNamespace = TU->getAnonymousNamespace();
1447 else
1448 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1449 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001450 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001451 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001452 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001453 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1454 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001455 continue;
1456
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001457 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001458 MergeWithNamespace = FoundNS;
1459 ConflictingDecls.clear();
1460 break;
1461 }
1462
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001463 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001464 }
1465
1466 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001467 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001468 ConflictingDecls.data(),
1469 ConflictingDecls.size());
1470 }
1471 }
1472
1473 // Create the "to" namespace, if needed.
1474 NamespaceDecl *ToNamespace = MergeWithNamespace;
1475 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001476 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00001477 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001478 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00001479 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00001480 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001481 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001482 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001483
1484 // If this is an anonymous namespace, register it as the anonymous
1485 // namespace within its context.
1486 if (!Name) {
1487 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1488 TU->setAnonymousNamespace(ToNamespace);
1489 else
1490 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1491 }
1492 }
1493 Importer.Imported(D, ToNamespace);
1494
1495 ImportDeclContext(D);
1496
1497 return ToNamespace;
1498}
1499
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001500Decl *ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1501 // Import the major distinguishing characteristics of this namespace.
1502 DeclContext *DC, *LexicalDC;
1503 DeclarationName Name;
1504 SourceLocation Loc;
1505 NamedDecl *LookupD;
1506 if (ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
1507 return nullptr;
1508 if (LookupD)
1509 return LookupD;
1510
1511 // NOTE: No conflict resolution is done for namespace aliases now.
1512
1513 NamespaceDecl *TargetDecl = cast_or_null<NamespaceDecl>(
1514 Importer.Import(D->getNamespace()));
1515 if (!TargetDecl)
1516 return nullptr;
1517
1518 IdentifierInfo *ToII = Importer.Import(D->getIdentifier());
1519 if (!ToII)
1520 return nullptr;
1521
1522 NestedNameSpecifierLoc ToQLoc = Importer.Import(D->getQualifierLoc());
1523 if (D->getQualifierLoc() && !ToQLoc)
1524 return nullptr;
1525
1526 NamespaceAliasDecl *ToD = NamespaceAliasDecl::Create(
1527 Importer.getToContext(), DC, Importer.Import(D->getNamespaceLoc()),
1528 Importer.Import(D->getAliasLoc()), ToII, ToQLoc,
1529 Importer.Import(D->getTargetNameLoc()), TargetDecl);
1530
1531 ToD->setLexicalDeclContext(LexicalDC);
1532 Importer.Imported(D, ToD);
1533 LexicalDC->addDeclInternal(ToD);
1534
1535 return ToD;
1536}
1537
Richard Smithdda56e42011-04-15 14:24:37 +00001538Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001539 // Import the major distinguishing characteristics of this typedef.
1540 DeclContext *DC, *LexicalDC;
1541 DeclarationName Name;
1542 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001543 NamedDecl *ToD;
1544 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001545 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001546 if (ToD)
1547 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001548
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001549 // If this typedef is not in block scope, determine whether we've
1550 // seen a typedef with the same name (that we can merge with) or any
1551 // other entity by that name (which name lookup could conflict with).
1552 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001553 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001554 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001555 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001556 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001557 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1558 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001559 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00001560 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001561 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001562 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1563 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001564 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001565 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001566
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001567 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001568 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001569
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001570 if (!ConflictingDecls.empty()) {
1571 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1572 ConflictingDecls.data(),
1573 ConflictingDecls.size());
1574 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001575 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001576 }
1577 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001578
Douglas Gregorb4964f72010-02-15 23:54:17 +00001579 // Import the underlying type of this typedef;
1580 QualType T = Importer.Import(D->getUnderlyingType());
1581 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001582 return nullptr;
1583
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001584 // Create the new typedef node.
1585 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00001586 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00001587 TypedefNameDecl *ToTypedef;
1588 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001589 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
1590 StartL, Loc,
1591 Name.getAsIdentifierInfo(),
1592 TInfo);
1593 else
Richard Smithdda56e42011-04-15 14:24:37 +00001594 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1595 StartL, Loc,
1596 Name.getAsIdentifierInfo(),
1597 TInfo);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001598
Douglas Gregordd483172010-02-22 17:42:47 +00001599 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001600 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001601 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00001602 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001603
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001604 return ToTypedef;
1605}
1606
Richard Smithdda56e42011-04-15 14:24:37 +00001607Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1608 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1609}
1610
1611Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1612 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1613}
1614
Gabor Horvath7a91c082017-11-14 11:30:38 +00001615Decl *ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1616 // Import the major distinguishing characteristics of this typedef.
1617 DeclContext *DC, *LexicalDC;
1618 DeclarationName Name;
1619 SourceLocation Loc;
1620 NamedDecl *ToD;
1621 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1622 return nullptr;
1623 if (ToD)
1624 return ToD;
1625
1626 // If this typedef is not in block scope, determine whether we've
1627 // seen a typedef with the same name (that we can merge with) or any
1628 // other entity by that name (which name lookup could conflict with).
1629 if (!DC->isFunctionOrMethod()) {
1630 SmallVector<NamedDecl *, 4> ConflictingDecls;
1631 unsigned IDNS = Decl::IDNS_Ordinary;
1632 SmallVector<NamedDecl *, 2> FoundDecls;
1633 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1634 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1635 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1636 continue;
1637 if (auto *FoundAlias =
1638 dyn_cast<TypeAliasTemplateDecl>(FoundDecls[I]))
1639 return Importer.Imported(D, FoundAlias);
1640 ConflictingDecls.push_back(FoundDecls[I]);
1641 }
1642
1643 if (!ConflictingDecls.empty()) {
1644 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1645 ConflictingDecls.data(),
1646 ConflictingDecls.size());
1647 if (!Name)
1648 return nullptr;
1649 }
1650 }
1651
1652 TemplateParameterList *Params = ImportTemplateParameterList(
1653 D->getTemplateParameters());
1654 if (!Params)
1655 return nullptr;
1656
1657 NamedDecl *TemplDecl = cast_or_null<NamedDecl>(
1658 Importer.Import(D->getTemplatedDecl()));
1659 if (!TemplDecl)
1660 return nullptr;
1661
1662 TypeAliasTemplateDecl *ToAlias = TypeAliasTemplateDecl::Create(
1663 Importer.getToContext(), DC, Loc, Name, Params, TemplDecl);
1664
1665 ToAlias->setAccess(D->getAccess());
1666 ToAlias->setLexicalDeclContext(LexicalDC);
1667 Importer.Imported(D, ToAlias);
1668 LexicalDC->addDeclInternal(ToAlias);
1669 return ToD;
1670}
1671
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001672Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
1673 // Import the major distinguishing characteristics of this label.
1674 DeclContext *DC, *LexicalDC;
1675 DeclarationName Name;
1676 SourceLocation Loc;
1677 NamedDecl *ToD;
1678 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1679 return nullptr;
1680 if (ToD)
1681 return ToD;
1682
1683 assert(LexicalDC->isFunctionOrMethod());
1684
1685 LabelDecl *ToLabel = D->isGnuLocal()
1686 ? LabelDecl::Create(Importer.getToContext(),
1687 DC, Importer.Import(D->getLocation()),
1688 Name.getAsIdentifierInfo(),
1689 Importer.Import(D->getLocStart()))
1690 : LabelDecl::Create(Importer.getToContext(),
1691 DC, Importer.Import(D->getLocation()),
1692 Name.getAsIdentifierInfo());
1693 Importer.Imported(D, ToLabel);
1694
1695 LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
1696 if (!Label)
1697 return nullptr;
1698
1699 ToLabel->setStmt(Label);
1700 ToLabel->setLexicalDeclContext(LexicalDC);
1701 LexicalDC->addDeclInternal(ToLabel);
1702 return ToLabel;
1703}
1704
Douglas Gregor98c10182010-02-12 22:17:39 +00001705Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1706 // Import the major distinguishing characteristics of this enum.
1707 DeclContext *DC, *LexicalDC;
1708 DeclarationName Name;
1709 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001710 NamedDecl *ToD;
1711 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001712 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001713 if (ToD)
1714 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001715
Douglas Gregor98c10182010-02-12 22:17:39 +00001716 // Figure out what enum name we're looking for.
1717 unsigned IDNS = Decl::IDNS_Tag;
1718 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001719 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1720 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00001721 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001722 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00001723 IDNS |= Decl::IDNS_Ordinary;
1724
1725 // We may already have an enum of the same name; try to find and match it.
1726 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001727 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001728 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001729 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001730 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1731 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00001732 continue;
1733
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001734 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00001735 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00001736 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1737 Found = Tag->getDecl();
1738 }
1739
1740 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001741 if (IsStructuralMatch(D, FoundEnum))
1742 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001743 }
1744
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001745 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00001746 }
1747
1748 if (!ConflictingDecls.empty()) {
1749 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1750 ConflictingDecls.data(),
1751 ConflictingDecls.size());
1752 }
1753 }
1754
1755 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001756 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
1757 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00001758 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001759 D->isScoped(), D->isScopedUsingClassTag(),
1760 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00001761 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00001762 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00001763 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00001764 D2->setLexicalDeclContext(LexicalDC);
1765 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00001766 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00001767
1768 // Import the integer type.
1769 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1770 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001771 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00001772 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001773
1774 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00001775 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00001776 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00001777
Douglas Gregor3996e242010-02-15 22:01:00 +00001778 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00001779}
1780
Douglas Gregor5c73e912010-02-11 00:48:18 +00001781Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1782 // If this record has a definition in the translation unit we're coming from,
1783 // but this particular declaration is not that definition, import the
1784 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001785 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001786 if (Definition && Definition != D) {
1787 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001788 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00001789 return nullptr;
1790
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001791 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001792 }
1793
1794 // Import the major distinguishing characteristics of this record.
1795 DeclContext *DC, *LexicalDC;
1796 DeclarationName Name;
1797 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001798 NamedDecl *ToD;
1799 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001800 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001801 if (ToD)
1802 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001803
Douglas Gregor5c73e912010-02-11 00:48:18 +00001804 // Figure out what structure name we're looking for.
1805 unsigned IDNS = Decl::IDNS_Tag;
1806 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001807 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1808 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001809 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001810 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00001811 IDNS |= Decl::IDNS_Ordinary;
1812
1813 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00001814 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00001815 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00001816 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001817 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001818 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001819 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00001820
1821 if (!FoundDecls.empty()) {
1822 // We're going to have to compare D against potentially conflicting Decls, so complete it.
1823 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
1824 D->getASTContext().getExternalSource()->CompleteType(D);
1825 }
1826
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001827 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1828 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00001829 continue;
1830
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001831 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00001832 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00001833 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1834 Found = Tag->getDecl();
1835 }
1836
1837 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Gabor Horvath3b392bb2017-04-03 21:06:45 +00001838 if (D->isAnonymousStructOrUnion() &&
1839 FoundRecord->isAnonymousStructOrUnion()) {
1840 // If both anonymous structs/unions are in a record context, make sure
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001841 // they occur in the same location in the context records.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001842 if (Optional<unsigned> Index1 =
1843 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
1844 D)) {
1845 if (Optional<unsigned> Index2 = StructuralEquivalenceContext::
Sean Callanan488f8612016-07-14 19:53:44 +00001846 findUntaggedStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001847 if (*Index1 != *Index2)
1848 continue;
1849 }
1850 }
1851 }
1852
Sean Callanan9092d472017-05-13 00:46:33 +00001853 PrevDecl = FoundRecord;
1854
Douglas Gregor25791052010-02-12 00:09:27 +00001855 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00001856 if ((SearchName && !D->isCompleteDefinition())
1857 || (D->isCompleteDefinition() &&
1858 D->isAnonymousStructOrUnion()
1859 == FoundDef->isAnonymousStructOrUnion() &&
1860 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00001861 // The record types structurally match, or the "from" translation
1862 // unit only had a forward declaration anyway; call it the same
1863 // function.
1864 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001865 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00001866 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00001867 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00001868 // We have a forward declaration of this type, so adopt that forward
1869 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00001870
1871 // If one or both can be completed from external storage then try one
1872 // last time to complete and compare them before doing this.
1873
1874 if (FoundRecord->hasExternalLexicalStorage() &&
1875 !FoundRecord->isCompleteDefinition())
1876 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
1877 if (D->hasExternalLexicalStorage())
1878 D->getASTContext().getExternalSource()->CompleteType(D);
1879
1880 if (FoundRecord->isCompleteDefinition() &&
1881 D->isCompleteDefinition() &&
1882 !IsStructuralMatch(D, FoundRecord))
1883 continue;
1884
Douglas Gregor25791052010-02-12 00:09:27 +00001885 AdoptDecl = FoundRecord;
1886 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00001887 } else if (!SearchName) {
1888 continue;
1889 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00001890 }
1891
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001892 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001893 }
1894
Douglas Gregordd6006f2012-07-17 21:16:27 +00001895 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00001896 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1897 ConflictingDecls.data(),
1898 ConflictingDecls.size());
1899 }
1900 }
1901
1902 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001903 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001904 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00001905 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00001906 CXXRecordDecl *D2CXX = nullptr;
1907 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) {
1908 if (DCXX->isLambda()) {
1909 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
1910 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
1911 DC, TInfo, Loc,
1912 DCXX->isDependentLambda(),
1913 DCXX->isGenericLambda(),
1914 DCXX->getLambdaCaptureDefault());
1915 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
1916 if (DCXX->getLambdaContextDecl() && !CDecl)
1917 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00001918 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
1919 } else if (DCXX->isInjectedClassName()) {
1920 // We have to be careful to do a similar dance to the one in
1921 // Sema::ActOnStartCXXMemberDeclarations
1922 CXXRecordDecl *const PrevDecl = nullptr;
1923 const bool DelayTypeCreation = true;
1924 D2CXX = CXXRecordDecl::Create(
1925 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
1926 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
1927 Importer.getToContext().getTypeDeclType(
1928 D2CXX, llvm::dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00001929 } else {
1930 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
1931 D->getTagKind(),
1932 DC, StartLoc, Loc,
1933 Name.getAsIdentifierInfo());
1934 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001935 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00001936 D2->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001937
1938 Importer.Imported(D, D2);
1939
1940 if (ClassTemplateDecl *FromDescribed =
1941 DCXX->getDescribedClassTemplate()) {
1942 ClassTemplateDecl *ToDescribed = cast_or_null<ClassTemplateDecl>(
1943 Importer.Import(FromDescribed));
1944 if (!ToDescribed)
1945 return nullptr;
1946 D2CXX->setDescribedClassTemplate(ToDescribed);
1947
1948 } else if (MemberSpecializationInfo *MemberInfo =
1949 DCXX->getMemberSpecializationInfo()) {
1950 TemplateSpecializationKind SK =
1951 MemberInfo->getTemplateSpecializationKind();
1952 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
1953 CXXRecordDecl *ToInst =
1954 cast_or_null<CXXRecordDecl>(Importer.Import(FromInst));
1955 if (FromInst && !ToInst)
1956 return nullptr;
1957 D2CXX->setInstantiationOfMemberClass(ToInst, SK);
1958 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
1959 Importer.Import(MemberInfo->getPointOfInstantiation()));
1960 }
1961
Douglas Gregor25791052010-02-12 00:09:27 +00001962 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00001963 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001964 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001965 }
Douglas Gregor14454802011-02-25 02:25:35 +00001966
1967 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00001968 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001969 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00001970 if (D->isAnonymousStructOrUnion())
1971 D2->setAnonymousStructOrUnion(true);
Sean Callanan9092d472017-05-13 00:46:33 +00001972 if (PrevDecl) {
1973 // FIXME: do this for all Redeclarables, not just RecordDecls.
1974 D2->setPreviousDecl(PrevDecl);
1975 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00001976 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001977
Douglas Gregor3996e242010-02-15 22:01:00 +00001978 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00001979
Douglas Gregor95d82832012-01-24 18:36:04 +00001980 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00001981 return nullptr;
1982
Douglas Gregor3996e242010-02-15 22:01:00 +00001983 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001984}
1985
Douglas Gregor98c10182010-02-12 22:17:39 +00001986Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
1987 // Import the major distinguishing characteristics of this enumerator.
1988 DeclContext *DC, *LexicalDC;
1989 DeclarationName Name;
1990 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001991 NamedDecl *ToD;
1992 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001993 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001994 if (ToD)
1995 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001996
1997 QualType T = Importer.Import(D->getType());
1998 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001999 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002000
Douglas Gregor98c10182010-02-12 22:17:39 +00002001 // Determine whether there are any other declarations with the same name and
2002 // in the same context.
2003 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002004 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002005 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002006 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002007 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002008 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2009 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002010 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002011
2012 if (EnumConstantDecl *FoundEnumConstant
2013 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2014 if (IsStructuralMatch(D, FoundEnumConstant))
2015 return Importer.Imported(D, FoundEnumConstant);
2016 }
2017
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002018 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002019 }
2020
2021 if (!ConflictingDecls.empty()) {
2022 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2023 ConflictingDecls.data(),
2024 ConflictingDecls.size());
2025 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002026 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002027 }
2028 }
2029
2030 Expr *Init = Importer.Import(D->getInitExpr());
2031 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002032 return nullptr;
2033
Douglas Gregor98c10182010-02-12 22:17:39 +00002034 EnumConstantDecl *ToEnumerator
2035 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2036 Name.getAsIdentifierInfo(), T,
2037 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002038 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002039 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002040 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002041 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002042 return ToEnumerator;
2043}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002044
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002045Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2046 // Import the major distinguishing characteristics of this function.
2047 DeclContext *DC, *LexicalDC;
2048 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002049 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002050 NamedDecl *ToD;
2051 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002052 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002053 if (ToD)
2054 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002055
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002056 const FunctionDecl *FoundWithoutBody = nullptr;
2057
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002058 // Try to find a function in our own ("to") context with the same name, same
2059 // type, and in the same context as the function we're importing.
2060 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002061 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002062 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002063 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002064 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002065 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2066 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002067 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002068
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002069 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002070 if (FoundFunction->hasExternalFormalLinkage() &&
2071 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002072 if (Importer.IsStructurallyEquivalent(D->getType(),
2073 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002074 // FIXME: Actually try to merge the body and other attributes.
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002075 const FunctionDecl *FromBodyDecl = nullptr;
2076 D->hasBody(FromBodyDecl);
2077 if (D == FromBodyDecl && !FoundFunction->hasBody()) {
2078 // This function is needed to merge completely.
2079 FoundWithoutBody = FoundFunction;
2080 break;
2081 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002082 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002083 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002084
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002085 // FIXME: Check for overloading more carefully, e.g., by boosting
2086 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002087
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002088 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002089 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002090 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002091
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002092 // Complain about inconsistent function types.
2093 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002094 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002095 Importer.ToDiag(FoundFunction->getLocation(),
2096 diag::note_odr_value_here)
2097 << FoundFunction->getType();
2098 }
2099 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002100
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002101 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002102 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002103
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002104 if (!ConflictingDecls.empty()) {
2105 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2106 ConflictingDecls.data(),
2107 ConflictingDecls.size());
2108 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002109 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002110 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002111 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002112
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002113 DeclarationNameInfo NameInfo(Name, Loc);
2114 // Import additional name location/type info.
2115 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2116
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002117 QualType FromTy = D->getType();
2118 bool usedDifferentExceptionSpec = false;
2119
2120 if (const FunctionProtoType *
2121 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2122 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2123 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2124 // FunctionDecl that we are importing the FunctionProtoType for.
2125 // To avoid an infinite recursion when importing, create the FunctionDecl
2126 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002127 if (FromEPI.ExceptionSpec.SourceDecl ||
2128 FromEPI.ExceptionSpec.SourceTemplate ||
2129 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002130 FunctionProtoType::ExtProtoInfo DefaultEPI;
2131 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002132 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002133 usedDifferentExceptionSpec = true;
2134 }
2135 }
2136
Douglas Gregorb4964f72010-02-15 23:54:17 +00002137 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002138 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002139 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002140 return nullptr;
2141
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002142 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002143 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00002144 for (auto P : D->parameters()) {
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00002145 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002146 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002147 return nullptr;
2148
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002149 Parameters.push_back(ToP);
2150 }
2151
2152 // Create the imported function.
2153 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Craig Topper36250ad2014-05-12 05:36:57 +00002154 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002155 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00002156 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2157 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2158 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002159 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002160 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002161 FromConstructor->isExplicit(),
2162 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002163 D->isImplicit(),
2164 D->isConstexpr());
Sean Callanandd2c1742016-05-16 20:48:03 +00002165 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
2166 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
2167 for (CXXCtorInitializer *I : FromConstructor->inits()) {
2168 CXXCtorInitializer *ToI =
2169 cast_or_null<CXXCtorInitializer>(Importer.Import(I));
2170 if (!ToI && I)
2171 return nullptr;
2172 CtorInitializers.push_back(ToI);
2173 }
2174 CXXCtorInitializer **Memory =
2175 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
2176 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
2177 CXXConstructorDecl *ToCtor = llvm::cast<CXXConstructorDecl>(ToFunction);
2178 ToCtor->setCtorInitializers(Memory);
2179 ToCtor->setNumCtorInitializers(NumInitializers);
2180 }
Douglas Gregor00eace12010-02-21 18:29:16 +00002181 } else if (isa<CXXDestructorDecl>(D)) {
2182 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2183 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002184 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002185 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002186 D->isInlineSpecified(),
2187 D->isImplicit());
2188 } else if (CXXConversionDecl *FromConversion
2189 = dyn_cast<CXXConversionDecl>(D)) {
2190 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2191 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002192 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002193 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002194 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002195 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002196 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002197 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002198 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2199 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2200 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002201 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00002202 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002203 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002204 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002205 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002206 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002207 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002208 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00002209 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002210 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002211 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002212 D->hasWrittenPrototype(),
2213 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002214 }
John McCall3e11ebe2010-03-15 10:12:16 +00002215
2216 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002217 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002218 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002219 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002220 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2221 ToFunction->setTrivial(D->isTrivial());
2222 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002223 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002224
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002225 // Set the parameters.
2226 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002227 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002228 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002229 }
David Blaikie9c70e042011-09-21 18:16:56 +00002230 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002231
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002232 if (FoundWithoutBody) {
2233 auto *Recent = const_cast<FunctionDecl *>(
2234 FoundWithoutBody->getMostRecentDecl());
2235 ToFunction->setPreviousDecl(Recent);
2236 }
2237
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002238 if (usedDifferentExceptionSpec) {
2239 // Update FunctionProtoType::ExtProtoInfo.
2240 QualType T = Importer.Import(D->getType());
2241 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002242 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002243 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002244 }
2245
Sean Callanan59721b32015-04-28 18:41:46 +00002246 // Import the body, if any.
2247 if (Stmt *FromBody = D->getBody()) {
2248 if (Stmt *ToBody = Importer.Import(FromBody)) {
2249 ToFunction->setBody(ToBody);
2250 }
2251 }
2252
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002253 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002254
2255 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002256 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002257
Lang Hames19e07e12017-06-20 21:06:00 +00002258 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2259 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2260
Douglas Gregor43f54792010-02-17 02:12:47 +00002261 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002262}
2263
Douglas Gregor00eace12010-02-21 18:29:16 +00002264Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2265 return VisitFunctionDecl(D);
2266}
2267
2268Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2269 return VisitCXXMethodDecl(D);
2270}
2271
2272Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2273 return VisitCXXMethodDecl(D);
2274}
2275
2276Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2277 return VisitCXXMethodDecl(D);
2278}
2279
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002280static unsigned getFieldIndex(Decl *F) {
2281 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2282 if (!Owner)
2283 return 0;
2284
2285 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002286 for (const auto *D : Owner->noload_decls()) {
2287 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002288 return Index;
2289
2290 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2291 ++Index;
2292 }
2293
2294 return Index;
2295}
2296
Douglas Gregor5c73e912010-02-11 00:48:18 +00002297Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2298 // Import the major distinguishing characteristics of a variable.
2299 DeclContext *DC, *LexicalDC;
2300 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002301 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002302 NamedDecl *ToD;
2303 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002304 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002305 if (ToD)
2306 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002307
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002308 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002309 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002310 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002311 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2312 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002313 // For anonymous fields, match up by index.
2314 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2315 continue;
2316
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002317 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002318 FoundField->getType())) {
2319 Importer.Imported(D, FoundField);
2320 return FoundField;
2321 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002322
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002323 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2324 << Name << D->getType() << FoundField->getType();
2325 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2326 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002327 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002328 }
2329 }
2330
Douglas Gregorb4964f72010-02-15 23:54:17 +00002331 // Import the type.
2332 QualType T = Importer.Import(D->getType());
2333 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002334 return nullptr;
2335
Douglas Gregor5c73e912010-02-11 00:48:18 +00002336 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2337 Expr *BitWidth = Importer.Import(D->getBitWidth());
2338 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002339 return nullptr;
2340
Abramo Bagnaradff19302011-03-08 08:55:46 +00002341 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2342 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002343 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002344 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002345 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002346 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002347 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00002348 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00002349 Expr *ToInitializer = Importer.Import(FromInitializer);
2350 if (ToInitializer)
2351 ToField->setInClassInitializer(ToInitializer);
2352 else
2353 return nullptr;
2354 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002355 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002356 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002357 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002358 return ToField;
2359}
2360
Francois Pichet783dd6e2010-11-21 06:08:52 +00002361Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2362 // Import the major distinguishing characteristics of a variable.
2363 DeclContext *DC, *LexicalDC;
2364 DeclarationName Name;
2365 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002366 NamedDecl *ToD;
2367 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002368 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002369 if (ToD)
2370 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002371
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002372 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002373 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002374 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002375 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002376 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002377 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002378 // For anonymous indirect fields, match up by index.
2379 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2380 continue;
2381
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002382 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002383 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002384 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002385 Importer.Imported(D, FoundField);
2386 return FoundField;
2387 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002388
2389 // If there are more anonymous fields to check, continue.
2390 if (!Name && I < N-1)
2391 continue;
2392
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002393 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2394 << Name << D->getType() << FoundField->getType();
2395 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2396 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002397 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002398 }
2399 }
2400
Francois Pichet783dd6e2010-11-21 06:08:52 +00002401 // Import the type.
2402 QualType T = Importer.Import(D->getType());
2403 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002404 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002405
2406 NamedDecl **NamedChain =
2407 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2408
2409 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00002410 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00002411 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002412 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00002413 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002414 NamedChain[i++] = cast<NamedDecl>(D);
2415 }
2416
2417 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00002418 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
David Majnemer59f77922016-06-24 04:05:48 +00002419 {NamedChain, D->getChainingSize()});
Aaron Ballman260995b2014-10-15 16:58:18 +00002420
2421 for (const auto *Attr : D->attrs())
2422 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
2423
Francois Pichet783dd6e2010-11-21 06:08:52 +00002424 ToIndirectField->setAccess(D->getAccess());
2425 ToIndirectField->setLexicalDeclContext(LexicalDC);
2426 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002427 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002428 return ToIndirectField;
2429}
2430
Aleksei Sidorina693b372016-09-28 10:16:56 +00002431Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2432 // Import the major distinguishing characteristics of a declaration.
2433 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2434 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2435 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2436 if (!DC || !LexicalDC)
2437 return nullptr;
2438
2439 // Determine whether we've already imported this decl.
2440 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2441 auto *RD = cast<CXXRecordDecl>(DC);
2442 FriendDecl *ImportedFriend = RD->getFirstFriend();
2443 StructuralEquivalenceContext Context(
2444 Importer.getFromContext(), Importer.getToContext(),
2445 Importer.getNonEquivalentDecls(), false, false);
2446
2447 while (ImportedFriend) {
2448 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2449 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2450 ImportedFriend->getFriendDecl()))
2451 return Importer.Imported(D, ImportedFriend);
2452
2453 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2454 if (Importer.IsStructurallyEquivalent(
2455 D->getFriendType()->getType(),
2456 ImportedFriend->getFriendType()->getType(), true))
2457 return Importer.Imported(D, ImportedFriend);
2458 }
2459 ImportedFriend = ImportedFriend->getNextFriend();
2460 }
2461
2462 // Not found. Create it.
2463 FriendDecl::FriendUnion ToFU;
2464 if (NamedDecl *FriendD = D->getFriendDecl())
2465 ToFU = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2466 else
2467 ToFU = Importer.Import(D->getFriendType());
2468 if (!ToFU)
2469 return nullptr;
2470
2471 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
2472 TemplateParameterList **FromTPLists =
2473 D->getTrailingObjects<TemplateParameterList *>();
2474 for (unsigned I = 0; I < D->NumTPLists; I++) {
2475 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2476 if (!List)
2477 return nullptr;
2478 ToTPLists[I] = List;
2479 }
2480
2481 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
2482 Importer.Import(D->getLocation()),
2483 ToFU, Importer.Import(D->getFriendLoc()),
2484 ToTPLists);
2485
2486 Importer.Imported(D, FrD);
2487 RD->pushFriendDecl(FrD);
2488
2489 FrD->setAccess(D->getAccess());
2490 FrD->setLexicalDeclContext(LexicalDC);
2491 LexicalDC->addDeclInternal(FrD);
2492 return FrD;
2493}
2494
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002495Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2496 // Import the major distinguishing characteristics of an ivar.
2497 DeclContext *DC, *LexicalDC;
2498 DeclarationName Name;
2499 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002500 NamedDecl *ToD;
2501 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002502 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002503 if (ToD)
2504 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002505
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002506 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002507 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002508 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002509 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2510 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002511 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002512 FoundIvar->getType())) {
2513 Importer.Imported(D, FoundIvar);
2514 return FoundIvar;
2515 }
2516
2517 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2518 << Name << D->getType() << FoundIvar->getType();
2519 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2520 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002521 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002522 }
2523 }
2524
2525 // Import the type.
2526 QualType T = Importer.Import(D->getType());
2527 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002528 return nullptr;
2529
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002530 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2531 Expr *BitWidth = Importer.Import(D->getBitWidth());
2532 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002533 return nullptr;
2534
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002535 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2536 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002537 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002538 Loc, Name.getAsIdentifierInfo(),
2539 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00002540 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002541 ToIvar->setLexicalDeclContext(LexicalDC);
2542 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002543 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002544 return ToIvar;
2545
2546}
2547
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002548Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2549 // Import the major distinguishing characteristics of a variable.
2550 DeclContext *DC, *LexicalDC;
2551 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002552 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002553 NamedDecl *ToD;
2554 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002555 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002556 if (ToD)
2557 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002558
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002559 // Try to find a variable in our own ("to") context with the same name and
2560 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002561 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00002562 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002563 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002564 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002565 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002566 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002567 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2568 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002569 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002570
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002571 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002572 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00002573 if (FoundVar->hasExternalFormalLinkage() &&
2574 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002575 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002576 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002577 MergeWithVar = FoundVar;
2578 break;
2579 }
2580
Douglas Gregor56521c52010-02-12 17:23:39 +00002581 const ArrayType *FoundArray
2582 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2583 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002584 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002585 if (FoundArray && TArray) {
2586 if (isa<IncompleteArrayType>(FoundArray) &&
2587 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002588 // Import the type.
2589 QualType T = Importer.Import(D->getType());
2590 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002591 return nullptr;
2592
Douglas Gregor56521c52010-02-12 17:23:39 +00002593 FoundVar->setType(T);
2594 MergeWithVar = FoundVar;
2595 break;
2596 } else if (isa<IncompleteArrayType>(TArray) &&
2597 isa<ConstantArrayType>(FoundArray)) {
2598 MergeWithVar = FoundVar;
2599 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002600 }
2601 }
2602
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002603 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002604 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002605 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2606 << FoundVar->getType();
2607 }
2608 }
2609
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002610 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002611 }
2612
2613 if (MergeWithVar) {
2614 // An equivalent variable with external linkage has been found. Link
2615 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002616 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002617
2618 if (VarDecl *DDef = D->getDefinition()) {
2619 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2620 Importer.ToDiag(ExistingDef->getLocation(),
2621 diag::err_odr_variable_multiple_def)
2622 << Name;
2623 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2624 } else {
2625 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002626 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002627 if (DDef->isInitKnownICE()) {
2628 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2629 Eval->CheckedICE = true;
2630 Eval->IsICE = DDef->isInitICE();
2631 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002632 }
2633 }
2634
2635 return MergeWithVar;
2636 }
2637
2638 if (!ConflictingDecls.empty()) {
2639 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2640 ConflictingDecls.data(),
2641 ConflictingDecls.size());
2642 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002643 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002644 }
2645 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002646
Douglas Gregorb4964f72010-02-15 23:54:17 +00002647 // Import the type.
2648 QualType T = Importer.Import(D->getType());
2649 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002650 return nullptr;
2651
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002652 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002653 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002654 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2655 Importer.Import(D->getInnerLocStart()),
2656 Loc, Name.getAsIdentifierInfo(),
2657 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002658 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00002659 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002660 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002661 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002662 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002663 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002664
Sean Callanan59721b32015-04-28 18:41:46 +00002665 if (!D->isFileVarDecl() &&
2666 D->isUsed())
2667 ToVar->setIsUsed();
2668
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002669 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002670 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00002671 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002672
Aleksei Sidorin855086d2017-01-23 09:30:36 +00002673 if (D->isConstexpr())
2674 ToVar->setConstexpr(true);
2675
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002676 return ToVar;
2677}
2678
Douglas Gregor8b228d72010-02-17 21:22:52 +00002679Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2680 // Parameters are created in the translation unit's context, then moved
2681 // into the function declaration's context afterward.
2682 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2683
2684 // Import the name of this declaration.
2685 DeclarationName Name = Importer.Import(D->getDeclName());
2686 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002687 return nullptr;
2688
Douglas Gregor8b228d72010-02-17 21:22:52 +00002689 // Import the location of this declaration.
2690 SourceLocation Loc = Importer.Import(D->getLocation());
2691
2692 // Import the parameter's type.
2693 QualType T = Importer.Import(D->getType());
2694 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002695 return nullptr;
2696
Douglas Gregor8b228d72010-02-17 21:22:52 +00002697 // Create the imported parameter.
Alexey Bataev56223232017-06-09 13:40:18 +00002698 auto *ToParm = ImplicitParamDecl::Create(Importer.getToContext(), DC, Loc,
2699 Name.getAsIdentifierInfo(), T,
2700 D->getParameterKind());
Douglas Gregor8b228d72010-02-17 21:22:52 +00002701 return Importer.Imported(D, ToParm);
2702}
2703
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002704Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2705 // Parameters are created in the translation unit's context, then moved
2706 // into the function declaration's context afterward.
2707 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2708
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002709 // Import the name of this declaration.
2710 DeclarationName Name = Importer.Import(D->getDeclName());
2711 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002712 return nullptr;
2713
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002714 // Import the location of this declaration.
2715 SourceLocation Loc = Importer.Import(D->getLocation());
2716
2717 // Import the parameter's type.
2718 QualType T = Importer.Import(D->getType());
2719 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002720 return nullptr;
2721
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002722 // Create the imported parameter.
2723 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2724 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002725 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002726 Loc, Name.getAsIdentifierInfo(),
2727 T, TInfo, D->getStorageClass(),
Aleksei Sidorin55a63502017-02-20 11:57:12 +00002728 /*DefaultArg*/ nullptr);
2729
2730 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00002731 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00002732 ToParm->setKNRPromoted(D->isKNRPromoted());
2733
2734 Expr *ToDefArg = nullptr;
2735 Expr *FromDefArg = nullptr;
2736 if (D->hasUninstantiatedDefaultArg()) {
2737 FromDefArg = D->getUninstantiatedDefaultArg();
2738 ToDefArg = Importer.Import(FromDefArg);
2739 ToParm->setUninstantiatedDefaultArg(ToDefArg);
2740 } else if (D->hasUnparsedDefaultArg()) {
2741 ToParm->setUnparsedDefaultArg();
2742 } else if (D->hasDefaultArg()) {
2743 FromDefArg = D->getDefaultArg();
2744 ToDefArg = Importer.Import(FromDefArg);
2745 ToParm->setDefaultArg(ToDefArg);
2746 }
2747 if (FromDefArg && !ToDefArg)
2748 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002749
2750 if (D->isUsed())
2751 ToParm->setIsUsed();
2752
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002753 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002754}
2755
Douglas Gregor43f54792010-02-17 02:12:47 +00002756Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2757 // Import the major distinguishing characteristics of a method.
2758 DeclContext *DC, *LexicalDC;
2759 DeclarationName Name;
2760 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002761 NamedDecl *ToD;
2762 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002763 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002764 if (ToD)
2765 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002766
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002767 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002768 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002769 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2770 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002771 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2772 continue;
2773
2774 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00002775 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
2776 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002777 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00002778 << D->isInstanceMethod() << Name << D->getReturnType()
2779 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00002780 Importer.ToDiag(FoundMethod->getLocation(),
2781 diag::note_odr_objc_method_here)
2782 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002783 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002784 }
2785
2786 // Check the number of parameters.
2787 if (D->param_size() != FoundMethod->param_size()) {
2788 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2789 << D->isInstanceMethod() << Name
2790 << D->param_size() << FoundMethod->param_size();
2791 Importer.ToDiag(FoundMethod->getLocation(),
2792 diag::note_odr_objc_method_here)
2793 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002794 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002795 }
2796
2797 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002798 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002799 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2800 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002801 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002802 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002803 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002804 diag::err_odr_objc_method_param_type_inconsistent)
2805 << D->isInstanceMethod() << Name
2806 << (*P)->getType() << (*FoundP)->getType();
2807 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2808 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002809 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002810 }
2811 }
2812
2813 // Check variadic/non-variadic.
2814 // Check the number of parameters.
2815 if (D->isVariadic() != FoundMethod->isVariadic()) {
2816 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2817 << D->isInstanceMethod() << Name;
2818 Importer.ToDiag(FoundMethod->getLocation(),
2819 diag::note_odr_objc_method_here)
2820 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002821 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002822 }
2823
2824 // FIXME: Any other bits we need to merge?
2825 return Importer.Imported(D, FoundMethod);
2826 }
2827 }
2828
2829 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00002830 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00002831 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002832 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002833
Alp Toker314cc812014-01-25 16:55:45 +00002834 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00002835
Alp Toker314cc812014-01-25 16:55:45 +00002836 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
2837 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
2838 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
2839 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
2840 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00002841
2842 // FIXME: When we decide to merge method definitions, we'll need to
2843 // deal with implicit parameters.
2844
2845 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002846 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00002847 for (auto *FromP : D->parameters()) {
Aaron Ballman43b68be2014-03-07 17:50:17 +00002848 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00002849 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002850 return nullptr;
2851
Douglas Gregor43f54792010-02-17 02:12:47 +00002852 ToParams.push_back(ToP);
2853 }
2854
2855 // Set the parameters.
2856 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2857 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00002858 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00002859 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002860 SmallVector<SourceLocation, 12> SelLocs;
2861 D->getSelectorLocs(SelLocs);
2862 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00002863
2864 ToMethod->setLexicalDeclContext(LexicalDC);
2865 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00002866 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00002867 return ToMethod;
2868}
2869
Douglas Gregor85f3f952015-07-07 03:57:15 +00002870Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
2871 // Import the major distinguishing characteristics of a category.
2872 DeclContext *DC, *LexicalDC;
2873 DeclarationName Name;
2874 SourceLocation Loc;
2875 NamedDecl *ToD;
2876 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2877 return nullptr;
2878 if (ToD)
2879 return ToD;
2880
2881 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
2882 if (!BoundInfo)
2883 return nullptr;
2884
2885 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
2886 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00002887 D->getVariance(),
2888 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00002889 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00002890 Importer.Import(D->getLocation()),
2891 Name.getAsIdentifierInfo(),
2892 Importer.Import(D->getColonLoc()),
2893 BoundInfo);
2894 Importer.Imported(D, Result);
2895 Result->setLexicalDeclContext(LexicalDC);
2896 return Result;
2897}
2898
Douglas Gregor84c51c32010-02-18 01:47:50 +00002899Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2900 // Import the major distinguishing characteristics of a category.
2901 DeclContext *DC, *LexicalDC;
2902 DeclarationName Name;
2903 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002904 NamedDecl *ToD;
2905 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002906 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002907 if (ToD)
2908 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002909
Douglas Gregor84c51c32010-02-18 01:47:50 +00002910 ObjCInterfaceDecl *ToInterface
2911 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2912 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00002913 return nullptr;
2914
Douglas Gregor84c51c32010-02-18 01:47:50 +00002915 // Determine if we've already encountered this category.
2916 ObjCCategoryDecl *MergeWithCategory
2917 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2918 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2919 if (!ToCategory) {
2920 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00002921 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00002922 Loc,
2923 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00002924 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002925 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00002926 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002927 Importer.Import(D->getIvarLBraceLoc()),
2928 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002929 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002930 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002931 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00002932 // Import the type parameter list after calling Imported, to avoid
2933 // loops when bringing in their DeclContext.
2934 ToCategory->setTypeParamList(ImportObjCTypeParamList(
2935 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002936
Douglas Gregor84c51c32010-02-18 01:47:50 +00002937 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002938 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2939 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00002940 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2941 = D->protocol_loc_begin();
2942 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2943 FromProtoEnd = D->protocol_end();
2944 FromProto != FromProtoEnd;
2945 ++FromProto, ++FromProtoLoc) {
2946 ObjCProtocolDecl *ToProto
2947 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2948 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00002949 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00002950 Protocols.push_back(ToProto);
2951 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2952 }
2953
2954 // FIXME: If we're merging, make sure that the protocol list is the same.
2955 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2956 ProtocolLocs.data(), Importer.getToContext());
2957
2958 } else {
2959 Importer.Imported(D, ToCategory);
2960 }
2961
2962 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00002963 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002964
2965 // If we have an implementation, import it as well.
2966 if (D->getImplementation()) {
2967 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00002968 = cast_or_null<ObjCCategoryImplDecl>(
2969 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002970 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00002971 return nullptr;
2972
Douglas Gregor84c51c32010-02-18 01:47:50 +00002973 ToCategory->setImplementation(Impl);
2974 }
2975
2976 return ToCategory;
2977}
2978
Douglas Gregor2aa53772012-01-24 17:42:07 +00002979bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
2980 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002981 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00002982 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00002983 if (shouldForceImportDeclContext(Kind))
2984 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00002985 return false;
2986 }
2987
2988 // Start the protocol definition
2989 To->startDefinition();
2990
2991 // Import protocols
2992 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2993 SmallVector<SourceLocation, 4> ProtocolLocs;
2994 ObjCProtocolDecl::protocol_loc_iterator
2995 FromProtoLoc = From->protocol_loc_begin();
2996 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
2997 FromProtoEnd = From->protocol_end();
2998 FromProto != FromProtoEnd;
2999 ++FromProto, ++FromProtoLoc) {
3000 ObjCProtocolDecl *ToProto
3001 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3002 if (!ToProto)
3003 return true;
3004 Protocols.push_back(ToProto);
3005 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3006 }
3007
3008 // FIXME: If we're merging, make sure that the protocol list is the same.
3009 To->setProtocolList(Protocols.data(), Protocols.size(),
3010 ProtocolLocs.data(), Importer.getToContext());
3011
Douglas Gregor2e15c842012-02-01 21:00:38 +00003012 if (shouldForceImportDeclContext(Kind)) {
3013 // Import all of the members of this protocol.
3014 ImportDeclContext(From, /*ForceImport=*/true);
3015 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003016 return false;
3017}
3018
Douglas Gregor98d156a2010-02-17 16:12:00 +00003019Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003020 // If this protocol has a definition in the translation unit we're coming
3021 // from, but this particular declaration is not that definition, import the
3022 // definition and map to that.
3023 ObjCProtocolDecl *Definition = D->getDefinition();
3024 if (Definition && Definition != D) {
3025 Decl *ImportedDef = Importer.Import(Definition);
3026 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003027 return nullptr;
3028
Douglas Gregor2aa53772012-01-24 17:42:07 +00003029 return Importer.Imported(D, ImportedDef);
3030 }
3031
Douglas Gregor84c51c32010-02-18 01:47:50 +00003032 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003033 DeclContext *DC, *LexicalDC;
3034 DeclarationName Name;
3035 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003036 NamedDecl *ToD;
3037 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003038 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003039 if (ToD)
3040 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003041
Craig Topper36250ad2014-05-12 05:36:57 +00003042 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003043 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003044 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003045 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3046 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003047 continue;
3048
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003049 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003050 break;
3051 }
3052
3053 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003054 if (!ToProto) {
3055 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3056 Name.getAsIdentifierInfo(), Loc,
3057 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003058 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003059 ToProto->setLexicalDeclContext(LexicalDC);
3060 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003061 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003062
3063 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003064
Douglas Gregor2aa53772012-01-24 17:42:07 +00003065 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003066 return nullptr;
3067
Douglas Gregor98d156a2010-02-17 16:12:00 +00003068 return ToProto;
3069}
3070
Sean Callanan0aae0412014-12-10 00:00:37 +00003071Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3072 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3073 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3074
3075 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3076 SourceLocation LangLoc = Importer.Import(D->getLocation());
3077
3078 bool HasBraces = D->hasBraces();
3079
Sean Callananb12a8552014-12-10 21:22:20 +00003080 LinkageSpecDecl *ToLinkageSpec =
3081 LinkageSpecDecl::Create(Importer.getToContext(),
3082 DC,
3083 ExternLoc,
3084 LangLoc,
3085 D->getLanguage(),
3086 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003087
3088 if (HasBraces) {
3089 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3090 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3091 }
3092
3093 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3094 LexicalDC->addDeclInternal(ToLinkageSpec);
3095
3096 Importer.Imported(D, ToLinkageSpec);
3097
3098 return ToLinkageSpec;
3099}
3100
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003101Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
3102 DeclContext *DC, *LexicalDC;
3103 DeclarationName Name;
3104 SourceLocation Loc;
3105 NamedDecl *ToD = nullptr;
3106 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3107 return nullptr;
3108 if (ToD)
3109 return ToD;
3110
3111 DeclarationNameInfo NameInfo(Name,
3112 Importer.Import(D->getNameInfo().getLoc()));
3113 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3114
3115 UsingDecl *ToUsing = UsingDecl::Create(Importer.getToContext(), DC,
3116 Importer.Import(D->getUsingLoc()),
3117 Importer.Import(D->getQualifierLoc()),
3118 NameInfo, D->hasTypename());
3119 ToUsing->setLexicalDeclContext(LexicalDC);
3120 LexicalDC->addDeclInternal(ToUsing);
3121 Importer.Imported(D, ToUsing);
3122
3123 if (NamedDecl *FromPattern =
3124 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
3125 if (NamedDecl *ToPattern =
3126 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern)))
3127 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern);
3128 else
3129 return nullptr;
3130 }
3131
3132 for (UsingShadowDecl *FromShadow : D->shadows()) {
3133 if (UsingShadowDecl *ToShadow =
3134 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow)))
3135 ToUsing->addShadowDecl(ToShadow);
3136 else
3137 // FIXME: We return a nullptr here but the definition is already created
3138 // and available with lookups. How to fix this?..
3139 return nullptr;
3140 }
3141 return ToUsing;
3142}
3143
3144Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
3145 DeclContext *DC, *LexicalDC;
3146 DeclarationName Name;
3147 SourceLocation Loc;
3148 NamedDecl *ToD = nullptr;
3149 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3150 return nullptr;
3151 if (ToD)
3152 return ToD;
3153
3154 UsingDecl *ToUsing = dyn_cast_or_null<UsingDecl>(
3155 Importer.Import(D->getUsingDecl()));
3156 if (!ToUsing)
3157 return nullptr;
3158
3159 NamedDecl *ToTarget = dyn_cast_or_null<NamedDecl>(
3160 Importer.Import(D->getTargetDecl()));
3161 if (!ToTarget)
3162 return nullptr;
3163
3164 UsingShadowDecl *ToShadow = UsingShadowDecl::Create(
3165 Importer.getToContext(), DC, Loc, ToUsing, ToTarget);
3166
3167 ToShadow->setLexicalDeclContext(LexicalDC);
3168 ToShadow->setAccess(D->getAccess());
3169 Importer.Imported(D, ToShadow);
3170
3171 if (UsingShadowDecl *FromPattern =
3172 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
3173 if (UsingShadowDecl *ToPattern =
3174 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern)))
3175 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow,
3176 ToPattern);
3177 else
3178 // FIXME: We return a nullptr here but the definition is already created
3179 // and available with lookups. How to fix this?..
3180 return nullptr;
3181 }
3182
3183 LexicalDC->addDeclInternal(ToShadow);
3184
3185 return ToShadow;
3186}
3187
3188
3189Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
3190 DeclContext *DC, *LexicalDC;
3191 DeclarationName Name;
3192 SourceLocation Loc;
3193 NamedDecl *ToD = nullptr;
3194 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3195 return nullptr;
3196 if (ToD)
3197 return ToD;
3198
3199 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor());
3200 if (!ToComAncestor)
3201 return nullptr;
3202
3203 NamespaceDecl *ToNominated = cast_or_null<NamespaceDecl>(
3204 Importer.Import(D->getNominatedNamespace()));
3205 if (!ToNominated)
3206 return nullptr;
3207
3208 UsingDirectiveDecl *ToUsingDir = UsingDirectiveDecl::Create(
3209 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3210 Importer.Import(D->getNamespaceKeyLocation()),
3211 Importer.Import(D->getQualifierLoc()),
3212 Importer.Import(D->getIdentLocation()), ToNominated, ToComAncestor);
3213 ToUsingDir->setLexicalDeclContext(LexicalDC);
3214 LexicalDC->addDeclInternal(ToUsingDir);
3215 Importer.Imported(D, ToUsingDir);
3216
3217 return ToUsingDir;
3218}
3219
3220Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl(
3221 UnresolvedUsingValueDecl *D) {
3222 DeclContext *DC, *LexicalDC;
3223 DeclarationName Name;
3224 SourceLocation Loc;
3225 NamedDecl *ToD = nullptr;
3226 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3227 return nullptr;
3228 if (ToD)
3229 return ToD;
3230
3231 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc()));
3232 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3233
3234 UnresolvedUsingValueDecl *ToUsingValue = UnresolvedUsingValueDecl::Create(
3235 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3236 Importer.Import(D->getQualifierLoc()), NameInfo,
3237 Importer.Import(D->getEllipsisLoc()));
3238
3239 Importer.Imported(D, ToUsingValue);
3240 ToUsingValue->setAccess(D->getAccess());
3241 ToUsingValue->setLexicalDeclContext(LexicalDC);
3242 LexicalDC->addDeclInternal(ToUsingValue);
3243
3244 return ToUsingValue;
3245}
3246
3247Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
3248 UnresolvedUsingTypenameDecl *D) {
3249 DeclContext *DC, *LexicalDC;
3250 DeclarationName Name;
3251 SourceLocation Loc;
3252 NamedDecl *ToD = nullptr;
3253 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3254 return nullptr;
3255 if (ToD)
3256 return ToD;
3257
3258 UnresolvedUsingTypenameDecl *ToUsing = UnresolvedUsingTypenameDecl::Create(
3259 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3260 Importer.Import(D->getTypenameLoc()),
3261 Importer.Import(D->getQualifierLoc()), Loc, Name,
3262 Importer.Import(D->getEllipsisLoc()));
3263
3264 Importer.Imported(D, ToUsing);
3265 ToUsing->setAccess(D->getAccess());
3266 ToUsing->setLexicalDeclContext(LexicalDC);
3267 LexicalDC->addDeclInternal(ToUsing);
3268
3269 return ToUsing;
3270}
3271
3272
Douglas Gregor2aa53772012-01-24 17:42:07 +00003273bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3274 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003275 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003276 if (To->getDefinition()) {
3277 // Check consistency of superclass.
3278 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3279 if (FromSuper) {
3280 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3281 if (!FromSuper)
3282 return true;
3283 }
3284
3285 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3286 if ((bool)FromSuper != (bool)ToSuper ||
3287 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3288 Importer.ToDiag(To->getLocation(),
3289 diag::err_odr_objc_superclass_inconsistent)
3290 << To->getDeclName();
3291 if (ToSuper)
3292 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3293 << To->getSuperClass()->getDeclName();
3294 else
3295 Importer.ToDiag(To->getLocation(),
3296 diag::note_odr_objc_missing_superclass);
3297 if (From->getSuperClass())
3298 Importer.FromDiag(From->getSuperClassLoc(),
3299 diag::note_odr_objc_superclass)
3300 << From->getSuperClass()->getDeclName();
3301 else
3302 Importer.FromDiag(From->getLocation(),
3303 diag::note_odr_objc_missing_superclass);
3304 }
3305
Douglas Gregor2e15c842012-02-01 21:00:38 +00003306 if (shouldForceImportDeclContext(Kind))
3307 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003308 return false;
3309 }
3310
3311 // Start the definition.
3312 To->startDefinition();
3313
3314 // If this class has a superclass, import it.
3315 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003316 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3317 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003318 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003319
3320 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003321 }
3322
3323 // Import protocols
3324 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3325 SmallVector<SourceLocation, 4> ProtocolLocs;
3326 ObjCInterfaceDecl::protocol_loc_iterator
3327 FromProtoLoc = From->protocol_loc_begin();
3328
3329 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3330 FromProtoEnd = From->protocol_end();
3331 FromProto != FromProtoEnd;
3332 ++FromProto, ++FromProtoLoc) {
3333 ObjCProtocolDecl *ToProto
3334 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3335 if (!ToProto)
3336 return true;
3337 Protocols.push_back(ToProto);
3338 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3339 }
3340
3341 // FIXME: If we're merging, make sure that the protocol list is the same.
3342 To->setProtocolList(Protocols.data(), Protocols.size(),
3343 ProtocolLocs.data(), Importer.getToContext());
3344
3345 // Import categories. When the categories themselves are imported, they'll
3346 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003347 for (auto *Cat : From->known_categories())
3348 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003349
Douglas Gregor2aa53772012-01-24 17:42:07 +00003350 // If we have an @implementation, import it as well.
3351 if (From->getImplementation()) {
3352 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3353 Importer.Import(From->getImplementation()));
3354 if (!Impl)
3355 return true;
3356
3357 To->setImplementation(Impl);
3358 }
3359
Douglas Gregor2e15c842012-02-01 21:00:38 +00003360 if (shouldForceImportDeclContext(Kind)) {
3361 // Import all of the members of this class.
3362 ImportDeclContext(From, /*ForceImport=*/true);
3363 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003364 return false;
3365}
3366
Douglas Gregor85f3f952015-07-07 03:57:15 +00003367ObjCTypeParamList *
3368ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3369 if (!list)
3370 return nullptr;
3371
3372 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3373 for (auto fromTypeParam : *list) {
3374 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3375 Importer.Import(fromTypeParam));
3376 if (!toTypeParam)
3377 return nullptr;
3378
3379 toTypeParams.push_back(toTypeParam);
3380 }
3381
3382 return ObjCTypeParamList::create(Importer.getToContext(),
3383 Importer.Import(list->getLAngleLoc()),
3384 toTypeParams,
3385 Importer.Import(list->getRAngleLoc()));
3386}
3387
Douglas Gregor45635322010-02-16 01:20:57 +00003388Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003389 // If this class has a definition in the translation unit we're coming from,
3390 // but this particular declaration is not that definition, import the
3391 // definition and map to that.
3392 ObjCInterfaceDecl *Definition = D->getDefinition();
3393 if (Definition && Definition != D) {
3394 Decl *ImportedDef = Importer.Import(Definition);
3395 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003396 return nullptr;
3397
Douglas Gregor2aa53772012-01-24 17:42:07 +00003398 return Importer.Imported(D, ImportedDef);
3399 }
3400
Douglas Gregor45635322010-02-16 01:20:57 +00003401 // Import the major distinguishing characteristics of an @interface.
3402 DeclContext *DC, *LexicalDC;
3403 DeclarationName Name;
3404 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003405 NamedDecl *ToD;
3406 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003407 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003408 if (ToD)
3409 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003410
Douglas Gregor2aa53772012-01-24 17:42:07 +00003411 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003412 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003413 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003414 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003415 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3416 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003417 continue;
3418
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003419 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003420 break;
3421 }
3422
Douglas Gregor2aa53772012-01-24 17:42:07 +00003423 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003424 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003425 if (!ToIface) {
3426 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3427 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003428 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003429 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00003430 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003431 D->isImplicitInterfaceDecl());
3432 ToIface->setLexicalDeclContext(LexicalDC);
3433 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003434 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003435 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003436 // Import the type parameter list after calling Imported, to avoid
3437 // loops when bringing in their DeclContext.
3438 ToIface->setTypeParamList(ImportObjCTypeParamList(
3439 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003440
Douglas Gregor2aa53772012-01-24 17:42:07 +00003441 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003442 return nullptr;
3443
Douglas Gregor98d156a2010-02-17 16:12:00 +00003444 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003445}
3446
Douglas Gregor4da9d682010-12-07 15:32:12 +00003447Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3448 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3449 Importer.Import(D->getCategoryDecl()));
3450 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003451 return nullptr;
3452
Douglas Gregor4da9d682010-12-07 15:32:12 +00003453 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3454 if (!ToImpl) {
3455 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3456 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003457 return nullptr;
3458
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003459 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003460 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003461 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003462 Category->getClassInterface(),
3463 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003464 Importer.Import(D->getAtStartLoc()),
3465 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003466
3467 DeclContext *LexicalDC = DC;
3468 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3469 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3470 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003471 return nullptr;
3472
Douglas Gregor4da9d682010-12-07 15:32:12 +00003473 ToImpl->setLexicalDeclContext(LexicalDC);
3474 }
3475
Sean Callanan95e74be2011-10-21 02:57:43 +00003476 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003477 Category->setImplementation(ToImpl);
3478 }
3479
3480 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003481 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003482 return ToImpl;
3483}
3484
Douglas Gregorda8025c2010-12-07 01:26:03 +00003485Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3486 // Find the corresponding interface.
3487 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3488 Importer.Import(D->getClassInterface()));
3489 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003490 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003491
3492 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003493 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003494 if (D->getSuperClass()) {
3495 Super = cast_or_null<ObjCInterfaceDecl>(
3496 Importer.Import(D->getSuperClass()));
3497 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003498 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003499 }
3500
3501 ObjCImplementationDecl *Impl = Iface->getImplementation();
3502 if (!Impl) {
3503 // We haven't imported an implementation yet. Create a new @implementation
3504 // now.
3505 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3506 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003507 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003508 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003509 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003510 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003511 Importer.Import(D->getIvarLBraceLoc()),
3512 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003513
3514 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3515 DeclContext *LexicalDC
3516 = Importer.ImportContext(D->getLexicalDeclContext());
3517 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003518 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003519 Impl->setLexicalDeclContext(LexicalDC);
3520 }
3521
3522 // Associate the implementation with the class it implements.
3523 Iface->setImplementation(Impl);
3524 Importer.Imported(D, Iface->getImplementation());
3525 } else {
3526 Importer.Imported(D, Iface->getImplementation());
3527
3528 // Verify that the existing @implementation has the same superclass.
3529 if ((Super && !Impl->getSuperClass()) ||
3530 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003531 (Super && Impl->getSuperClass() &&
3532 !declaresSameEntity(Super->getCanonicalDecl(),
3533 Impl->getSuperClass()))) {
3534 Importer.ToDiag(Impl->getLocation(),
3535 diag::err_odr_objc_superclass_inconsistent)
3536 << Iface->getDeclName();
3537 // FIXME: It would be nice to have the location of the superclass
3538 // below.
3539 if (Impl->getSuperClass())
3540 Importer.ToDiag(Impl->getLocation(),
3541 diag::note_odr_objc_superclass)
3542 << Impl->getSuperClass()->getDeclName();
3543 else
3544 Importer.ToDiag(Impl->getLocation(),
3545 diag::note_odr_objc_missing_superclass);
3546 if (D->getSuperClass())
3547 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003548 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003549 << D->getSuperClass()->getDeclName();
3550 else
3551 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003552 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003553 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003554 }
3555 }
3556
3557 // Import all of the members of this @implementation.
3558 ImportDeclContext(D);
3559
3560 return Impl;
3561}
3562
Douglas Gregora11c4582010-02-17 18:02:10 +00003563Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3564 // Import the major distinguishing characteristics of an @property.
3565 DeclContext *DC, *LexicalDC;
3566 DeclarationName Name;
3567 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003568 NamedDecl *ToD;
3569 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003570 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003571 if (ToD)
3572 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00003573
3574 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003575 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003576 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003577 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003578 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003579 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003580 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003581 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00003582 FoundProp->getType())) {
3583 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3584 << Name << D->getType() << FoundProp->getType();
3585 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3586 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003587 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003588 }
3589
3590 // FIXME: Check property attributes, getters, setters, etc.?
3591
3592 // Consider these properties to be equivalent.
3593 Importer.Imported(D, FoundProp);
3594 return FoundProp;
3595 }
3596 }
3597
3598 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00003599 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3600 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00003601 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003602
3603 // Create the new property.
3604 ObjCPropertyDecl *ToProperty
3605 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3606 Name.getAsIdentifierInfo(),
3607 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003608 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00003609 Importer.Import(D->getType()),
3610 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00003611 D->getPropertyImplementation());
3612 Importer.Imported(D, ToProperty);
3613 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003614 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003615
3616 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003617 ToProperty->setPropertyAttributesAsWritten(
3618 D->getPropertyAttributesAsWritten());
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +00003619 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
3620 Importer.Import(D->getGetterNameLoc()));
3621 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
3622 Importer.Import(D->getSetterNameLoc()));
Douglas Gregora11c4582010-02-17 18:02:10 +00003623 ToProperty->setGetterMethodDecl(
3624 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3625 ToProperty->setSetterMethodDecl(
3626 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3627 ToProperty->setPropertyIvarDecl(
3628 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3629 return ToProperty;
3630}
3631
Douglas Gregor14a49e22010-12-07 18:32:03 +00003632Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3633 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3634 Importer.Import(D->getPropertyDecl()));
3635 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00003636 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003637
3638 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3639 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003640 return nullptr;
3641
Douglas Gregor14a49e22010-12-07 18:32:03 +00003642 // Import the lexical declaration context.
3643 DeclContext *LexicalDC = DC;
3644 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3645 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3646 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003647 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003648 }
3649
3650 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3651 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00003652 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003653
3654 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00003655 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003656 if (D->getPropertyIvarDecl()) {
3657 Ivar = cast_or_null<ObjCIvarDecl>(
3658 Importer.Import(D->getPropertyIvarDecl()));
3659 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00003660 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003661 }
3662
3663 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00003664 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
3665 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00003666 if (!ToImpl) {
3667 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3668 Importer.Import(D->getLocStart()),
3669 Importer.Import(D->getLocation()),
3670 Property,
3671 D->getPropertyImplementation(),
3672 Ivar,
3673 Importer.Import(D->getPropertyIvarDeclLoc()));
3674 ToImpl->setLexicalDeclContext(LexicalDC);
3675 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003676 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003677 } else {
3678 // Check that we have the same kind of property implementation (@synthesize
3679 // vs. @dynamic).
3680 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3681 Importer.ToDiag(ToImpl->getLocation(),
3682 diag::err_odr_objc_property_impl_kind_inconsistent)
3683 << Property->getDeclName()
3684 << (ToImpl->getPropertyImplementation()
3685 == ObjCPropertyImplDecl::Dynamic);
3686 Importer.FromDiag(D->getLocation(),
3687 diag::note_odr_objc_property_impl_kind)
3688 << D->getPropertyDecl()->getDeclName()
3689 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00003690 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003691 }
3692
3693 // For @synthesize, check that we have the same
3694 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3695 Ivar != ToImpl->getPropertyIvarDecl()) {
3696 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3697 diag::err_odr_objc_synthesize_ivar_inconsistent)
3698 << Property->getDeclName()
3699 << ToImpl->getPropertyIvarDecl()->getDeclName()
3700 << Ivar->getDeclName();
3701 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3702 diag::note_odr_objc_synthesize_ivar_here)
3703 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00003704 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003705 }
3706
3707 // Merge the existing implementation with the new implementation.
3708 Importer.Imported(D, ToImpl);
3709 }
3710
3711 return ToImpl;
3712}
3713
Douglas Gregora082a492010-11-30 19:14:50 +00003714Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3715 // For template arguments, we adopt the translation unit as our declaration
3716 // context. This context will be fixed when the actual template declaration
3717 // is created.
3718
3719 // FIXME: Import default argument.
3720 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3721 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003722 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003723 Importer.Import(D->getLocation()),
3724 D->getDepth(),
3725 D->getIndex(),
3726 Importer.Import(D->getIdentifier()),
3727 D->wasDeclaredWithTypename(),
3728 D->isParameterPack());
3729}
3730
3731Decl *
3732ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3733 // Import the name of this declaration.
3734 DeclarationName Name = Importer.Import(D->getDeclName());
3735 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003736 return nullptr;
3737
Douglas Gregora082a492010-11-30 19:14:50 +00003738 // Import the location of this declaration.
3739 SourceLocation Loc = Importer.Import(D->getLocation());
3740
3741 // Import the type of this declaration.
3742 QualType T = Importer.Import(D->getType());
3743 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003744 return nullptr;
3745
Douglas Gregora082a492010-11-30 19:14:50 +00003746 // Import type-source information.
3747 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3748 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00003749 return nullptr;
3750
Douglas Gregora082a492010-11-30 19:14:50 +00003751 // FIXME: Import default argument.
3752
3753 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3754 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003755 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003756 Loc, D->getDepth(), D->getPosition(),
3757 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003758 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003759}
3760
3761Decl *
3762ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3763 // Import the name of this declaration.
3764 DeclarationName Name = Importer.Import(D->getDeclName());
3765 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003766 return nullptr;
3767
Douglas Gregora082a492010-11-30 19:14:50 +00003768 // Import the location of this declaration.
3769 SourceLocation Loc = Importer.Import(D->getLocation());
3770
3771 // Import template parameters.
3772 TemplateParameterList *TemplateParams
3773 = ImportTemplateParameterList(D->getTemplateParameters());
3774 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00003775 return nullptr;
3776
Douglas Gregora082a492010-11-30 19:14:50 +00003777 // FIXME: Import default argument.
3778
3779 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3780 Importer.getToContext().getTranslationUnitDecl(),
3781 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003782 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003783 Name.getAsIdentifierInfo(),
3784 TemplateParams);
3785}
3786
3787Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3788 // If this record has a definition in the translation unit we're coming from,
3789 // but this particular declaration is not that definition, import the
3790 // definition and map to that.
3791 CXXRecordDecl *Definition
3792 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3793 if (Definition && Definition != D->getTemplatedDecl()) {
3794 Decl *ImportedDef
3795 = Importer.Import(Definition->getDescribedClassTemplate());
3796 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003797 return nullptr;
3798
Douglas Gregora082a492010-11-30 19:14:50 +00003799 return Importer.Imported(D, ImportedDef);
3800 }
3801
3802 // Import the major distinguishing characteristics of this class template.
3803 DeclContext *DC, *LexicalDC;
3804 DeclarationName Name;
3805 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003806 NamedDecl *ToD;
3807 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003808 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003809 if (ToD)
3810 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003811
Douglas Gregora082a492010-11-30 19:14:50 +00003812 // We may already have a template of the same name; try to find and match it.
3813 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003814 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003815 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003816 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003817 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3818 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00003819 continue;
3820
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003821 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00003822 if (ClassTemplateDecl *FoundTemplate
3823 = dyn_cast<ClassTemplateDecl>(Found)) {
3824 if (IsStructuralMatch(D, FoundTemplate)) {
3825 // The class templates structurally match; call it the same template.
3826 // FIXME: We may be filling in a forward declaration here. Handle
3827 // this case!
3828 Importer.Imported(D->getTemplatedDecl(),
3829 FoundTemplate->getTemplatedDecl());
3830 return Importer.Imported(D, FoundTemplate);
3831 }
3832 }
3833
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003834 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00003835 }
3836
3837 if (!ConflictingDecls.empty()) {
3838 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3839 ConflictingDecls.data(),
3840 ConflictingDecls.size());
3841 }
3842
3843 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003844 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00003845 }
3846
3847 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3848
3849 // Create the declaration that is being templated.
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00003850 CXXRecordDecl *D2Templated = cast_or_null<CXXRecordDecl>(
3851 Importer.Import(DTemplated));
3852 if (!D2Templated)
3853 return nullptr;
3854
3855 // Resolve possible cyclic import.
3856 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
3857 return AlreadyImported;
3858
Douglas Gregora082a492010-11-30 19:14:50 +00003859 // Create the class template declaration itself.
3860 TemplateParameterList *TemplateParams
3861 = ImportTemplateParameterList(D->getTemplateParameters());
3862 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00003863 return nullptr;
3864
Douglas Gregora082a492010-11-30 19:14:50 +00003865 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3866 Loc, Name, TemplateParams,
Vassil Vassilev352e4412017-01-12 09:16:26 +00003867 D2Templated);
Douglas Gregora082a492010-11-30 19:14:50 +00003868 D2Templated->setDescribedClassTemplate(D2);
3869
3870 D2->setAccess(D->getAccess());
3871 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003872 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00003873
3874 // Note the relationship between the class templates.
3875 Importer.Imported(D, D2);
3876 Importer.Imported(DTemplated, D2Templated);
3877
John McCallf937c022011-10-07 06:10:15 +00003878 if (DTemplated->isCompleteDefinition() &&
3879 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00003880 // FIXME: Import definition!
3881 }
3882
3883 return D2;
3884}
3885
Douglas Gregore2e50d332010-12-01 01:36:18 +00003886Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3887 ClassTemplateSpecializationDecl *D) {
3888 // If this record has a definition in the translation unit we're coming from,
3889 // but this particular declaration is not that definition, import the
3890 // definition and map to that.
3891 TagDecl *Definition = D->getDefinition();
3892 if (Definition && Definition != D) {
3893 Decl *ImportedDef = Importer.Import(Definition);
3894 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003895 return nullptr;
3896
Douglas Gregore2e50d332010-12-01 01:36:18 +00003897 return Importer.Imported(D, ImportedDef);
3898 }
3899
3900 ClassTemplateDecl *ClassTemplate
3901 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3902 D->getSpecializedTemplate()));
3903 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00003904 return nullptr;
3905
Douglas Gregore2e50d332010-12-01 01:36:18 +00003906 // Import the context of this declaration.
3907 DeclContext *DC = ClassTemplate->getDeclContext();
3908 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003909 return nullptr;
3910
Douglas Gregore2e50d332010-12-01 01:36:18 +00003911 DeclContext *LexicalDC = DC;
3912 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3913 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3914 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003915 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003916 }
3917
3918 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003919 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3920 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00003921
3922 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003923 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003924 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3925 D->getTemplateArgs().size(),
3926 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00003927 return nullptr;
3928
Douglas Gregore2e50d332010-12-01 01:36:18 +00003929 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00003930 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003931 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00003932 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00003933 if (D2) {
3934 // We already have a class template specialization with these template
3935 // arguments.
3936
3937 // FIXME: Check for specialization vs. instantiation errors.
3938
3939 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00003940 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00003941 // The record types structurally match, or the "from" translation
3942 // unit only had a forward declaration anyway; call it the same
3943 // function.
3944 return Importer.Imported(D, FoundDef);
3945 }
3946 }
3947 } else {
3948 // Create a new specialization.
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003949 if (ClassTemplatePartialSpecializationDecl *PartialSpec =
3950 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
3951
3952 // Import TemplateArgumentListInfo
3953 TemplateArgumentListInfo ToTAInfo;
3954 auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
3955 for (unsigned I = 0, E = ASTTemplateArgs.NumTemplateArgs; I < E; ++I) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003956 if (auto ToLoc = ImportTemplateArgumentLoc(ASTTemplateArgs[I]))
3957 ToTAInfo.addArgument(*ToLoc);
3958 else
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003959 return nullptr;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003960 }
3961
3962 QualType CanonInjType = Importer.Import(
3963 PartialSpec->getInjectedSpecializationType());
3964 if (CanonInjType.isNull())
3965 return nullptr;
3966 CanonInjType = CanonInjType.getCanonicalType();
3967
3968 TemplateParameterList *ToTPList = ImportTemplateParameterList(
3969 PartialSpec->getTemplateParameters());
3970 if (!ToTPList && PartialSpec->getTemplateParameters())
3971 return nullptr;
3972
3973 D2 = ClassTemplatePartialSpecializationDecl::Create(
3974 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
3975 ToTPList, ClassTemplate,
3976 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
3977 ToTAInfo, CanonInjType, nullptr);
3978
3979 } else {
3980 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3981 D->getTagKind(), DC,
3982 StartLoc, IdLoc,
3983 ClassTemplate,
3984 TemplateArgs,
3985 /*PrevDecl=*/nullptr);
3986 }
3987
Douglas Gregore2e50d332010-12-01 01:36:18 +00003988 D2->setSpecializationKind(D->getSpecializationKind());
3989
3990 // Add this specialization to the class template.
3991 ClassTemplate->AddSpecialization(D2, InsertPos);
3992
3993 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003994 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003995
3996 Importer.Imported(D, D2);
3997
3998 if (auto *TSI = D->getTypeAsWritten()) {
3999 TypeSourceInfo *TInfo = Importer.Import(TSI);
4000 if (!TInfo)
4001 return nullptr;
4002 D2->setTypeAsWritten(TInfo);
4003 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
4004 D2->setExternLoc(Importer.Import(D->getExternLoc()));
4005 }
4006
4007 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
4008 if (POI.isValid())
4009 D2->setPointOfInstantiation(POI);
4010 else if (D->getPointOfInstantiation().isValid())
4011 return nullptr;
4012
4013 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
4014
Douglas Gregore2e50d332010-12-01 01:36:18 +00004015 // Add the specialization to this context.
4016 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004017 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004018 }
4019 Importer.Imported(D, D2);
John McCallf937c022011-10-07 06:10:15 +00004020 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004021 return nullptr;
4022
Douglas Gregore2e50d332010-12-01 01:36:18 +00004023 return D2;
4024}
4025
Larisse Voufo39a1e502013-08-06 01:03:05 +00004026Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4027 // If this variable has a definition in the translation unit we're coming
4028 // from,
4029 // but this particular declaration is not that definition, import the
4030 // definition and map to that.
4031 VarDecl *Definition =
4032 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4033 if (Definition && Definition != D->getTemplatedDecl()) {
4034 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4035 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004036 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004037
4038 return Importer.Imported(D, ImportedDef);
4039 }
4040
4041 // Import the major distinguishing characteristics of this variable template.
4042 DeclContext *DC, *LexicalDC;
4043 DeclarationName Name;
4044 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004045 NamedDecl *ToD;
4046 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004047 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004048 if (ToD)
4049 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004050
4051 // We may already have a template of the same name; try to find and match it.
4052 assert(!DC->isFunctionOrMethod() &&
4053 "Variable templates cannot be declared at function scope");
4054 SmallVector<NamedDecl *, 4> ConflictingDecls;
4055 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004056 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004057 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4058 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4059 continue;
4060
4061 Decl *Found = FoundDecls[I];
4062 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4063 if (IsStructuralMatch(D, FoundTemplate)) {
4064 // The variable templates structurally match; call it the same template.
4065 Importer.Imported(D->getTemplatedDecl(),
4066 FoundTemplate->getTemplatedDecl());
4067 return Importer.Imported(D, FoundTemplate);
4068 }
4069 }
4070
4071 ConflictingDecls.push_back(FoundDecls[I]);
4072 }
4073
4074 if (!ConflictingDecls.empty()) {
4075 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4076 ConflictingDecls.data(),
4077 ConflictingDecls.size());
4078 }
4079
4080 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004081 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004082
4083 VarDecl *DTemplated = D->getTemplatedDecl();
4084
4085 // Import the type.
4086 QualType T = Importer.Import(DTemplated->getType());
4087 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004088 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004089
4090 // Create the declaration that is being templated.
4091 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4092 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4093 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4094 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4095 IdLoc, Name.getAsIdentifierInfo(), T,
4096 TInfo, DTemplated->getStorageClass());
4097 D2Templated->setAccess(DTemplated->getAccess());
4098 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4099 D2Templated->setLexicalDeclContext(LexicalDC);
4100
4101 // Importer.Imported(DTemplated, D2Templated);
4102 // LexicalDC->addDeclInternal(D2Templated);
4103
4104 // Merge the initializer.
4105 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00004106 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004107
4108 // Create the variable template declaration itself.
4109 TemplateParameterList *TemplateParams =
4110 ImportTemplateParameterList(D->getTemplateParameters());
4111 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004112 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004113
4114 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00004115 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004116 D2Templated->setDescribedVarTemplate(D2);
4117
4118 D2->setAccess(D->getAccess());
4119 D2->setLexicalDeclContext(LexicalDC);
4120 LexicalDC->addDeclInternal(D2);
4121
4122 // Note the relationship between the variable templates.
4123 Importer.Imported(D, D2);
4124 Importer.Imported(DTemplated, D2Templated);
4125
4126 if (DTemplated->isThisDeclarationADefinition() &&
4127 !D2Templated->isThisDeclarationADefinition()) {
4128 // FIXME: Import definition!
4129 }
4130
4131 return D2;
4132}
4133
4134Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4135 VarTemplateSpecializationDecl *D) {
4136 // If this record has a definition in the translation unit we're coming from,
4137 // but this particular declaration is not that definition, import the
4138 // definition and map to that.
4139 VarDecl *Definition = D->getDefinition();
4140 if (Definition && Definition != D) {
4141 Decl *ImportedDef = Importer.Import(Definition);
4142 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004143 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004144
4145 return Importer.Imported(D, ImportedDef);
4146 }
4147
4148 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4149 Importer.Import(D->getSpecializedTemplate()));
4150 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004151 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004152
4153 // Import the context of this declaration.
4154 DeclContext *DC = VarTemplate->getDeclContext();
4155 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004156 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004157
4158 DeclContext *LexicalDC = DC;
4159 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4160 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4161 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004162 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004163 }
4164
4165 // Import the location of this declaration.
4166 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4167 SourceLocation IdLoc = Importer.Import(D->getLocation());
4168
4169 // Import template arguments.
4170 SmallVector<TemplateArgument, 2> TemplateArgs;
4171 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4172 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004173 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004174
4175 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004176 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004177 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004178 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004179 if (D2) {
4180 // We already have a variable template specialization with these template
4181 // arguments.
4182
4183 // FIXME: Check for specialization vs. instantiation errors.
4184
4185 if (VarDecl *FoundDef = D2->getDefinition()) {
4186 if (!D->isThisDeclarationADefinition() ||
4187 IsStructuralMatch(D, FoundDef)) {
4188 // The record types structurally match, or the "from" translation
4189 // unit only had a forward declaration anyway; call it the same
4190 // variable.
4191 return Importer.Imported(D, FoundDef);
4192 }
4193 }
4194 } else {
4195
4196 // Import the type.
4197 QualType T = Importer.Import(D->getType());
4198 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004199 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004200 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4201
4202 // Create a new specialization.
4203 D2 = VarTemplateSpecializationDecl::Create(
4204 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00004205 D->getStorageClass(), TemplateArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004206 D2->setSpecializationKind(D->getSpecializationKind());
4207 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4208
4209 // Add this specialization to the class template.
4210 VarTemplate->AddSpecialization(D2, InsertPos);
4211
4212 // Import the qualifier, if any.
4213 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4214
4215 // Add the specialization to this context.
4216 D2->setLexicalDeclContext(LexicalDC);
4217 LexicalDC->addDeclInternal(D2);
4218 }
4219 Importer.Imported(D, D2);
4220
4221 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004222 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004223
4224 return D2;
4225}
4226
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004227Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
4228 DeclContext *DC, *LexicalDC;
4229 DeclarationName Name;
4230 SourceLocation Loc;
4231 NamedDecl *ToD;
4232
4233 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4234 return nullptr;
4235
4236 if (ToD)
4237 return ToD;
4238
4239 // Try to find a function in our own ("to") context with the same name, same
4240 // type, and in the same context as the function we're importing.
4241 if (!LexicalDC->isFunctionOrMethod()) {
4242 unsigned IDNS = Decl::IDNS_Ordinary;
4243 SmallVector<NamedDecl *, 2> FoundDecls;
4244 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
4245 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4246 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
4247 continue;
4248
4249 if (FunctionTemplateDecl *FoundFunction =
4250 dyn_cast<FunctionTemplateDecl>(FoundDecls[I])) {
4251 if (FoundFunction->hasExternalFormalLinkage() &&
4252 D->hasExternalFormalLinkage()) {
4253 if (IsStructuralMatch(D, FoundFunction)) {
4254 Importer.Imported(D, FoundFunction);
4255 // FIXME: Actually try to merge the body and other attributes.
4256 return FoundFunction;
4257 }
4258 }
4259 }
4260 }
4261 }
4262
4263 TemplateParameterList *Params =
4264 ImportTemplateParameterList(D->getTemplateParameters());
4265 if (!Params)
4266 return nullptr;
4267
4268 FunctionDecl *TemplatedFD =
4269 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl()));
4270 if (!TemplatedFD)
4271 return nullptr;
4272
4273 FunctionTemplateDecl *ToFunc = FunctionTemplateDecl::Create(
4274 Importer.getToContext(), DC, Loc, Name, Params, TemplatedFD);
4275
4276 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
4277 ToFunc->setAccess(D->getAccess());
4278 ToFunc->setLexicalDeclContext(LexicalDC);
4279 Importer.Imported(D, ToFunc);
4280
4281 LexicalDC->addDeclInternal(ToFunc);
4282 return ToFunc;
4283}
4284
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004285//----------------------------------------------------------------------------
4286// Import Statements
4287//----------------------------------------------------------------------------
4288
Sean Callanan59721b32015-04-28 18:41:46 +00004289DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4290 if (DG.isNull())
4291 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4292 size_t NumDecls = DG.end() - DG.begin();
4293 SmallVector<Decl *, 1> ToDecls(NumDecls);
4294 auto &_Importer = this->Importer;
4295 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4296 [&_Importer](Decl *D) -> Decl * {
4297 return _Importer.Import(D);
4298 });
4299 return DeclGroupRef::Create(Importer.getToContext(),
4300 ToDecls.begin(),
4301 NumDecls);
4302}
4303
4304 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4305 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4306 << S->getStmtClassName();
4307 return nullptr;
4308 }
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004309
4310
4311Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4312 SmallVector<IdentifierInfo *, 4> Names;
4313 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4314 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004315 // ToII is nullptr when no symbolic name is given for output operand
4316 // see ParseStmtAsm::ParseAsmOperandsOpt
4317 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004318 return nullptr;
4319 Names.push_back(ToII);
4320 }
4321 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4322 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004323 // ToII is nullptr when no symbolic name is given for input operand
4324 // see ParseStmtAsm::ParseAsmOperandsOpt
4325 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004326 return nullptr;
4327 Names.push_back(ToII);
4328 }
4329
4330 SmallVector<StringLiteral *, 4> Clobbers;
4331 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
4332 StringLiteral *Clobber = cast_or_null<StringLiteral>(
4333 Importer.Import(S->getClobberStringLiteral(I)));
4334 if (!Clobber)
4335 return nullptr;
4336 Clobbers.push_back(Clobber);
4337 }
4338
4339 SmallVector<StringLiteral *, 4> Constraints;
4340 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4341 StringLiteral *Output = cast_or_null<StringLiteral>(
4342 Importer.Import(S->getOutputConstraintLiteral(I)));
4343 if (!Output)
4344 return nullptr;
4345 Constraints.push_back(Output);
4346 }
4347
4348 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4349 StringLiteral *Input = cast_or_null<StringLiteral>(
4350 Importer.Import(S->getInputConstraintLiteral(I)));
4351 if (!Input)
4352 return nullptr;
4353 Constraints.push_back(Input);
4354 }
4355
4356 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004357 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004358 return nullptr;
4359
Aleksei Sidorina693b372016-09-28 10:16:56 +00004360 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004361 return nullptr;
4362
4363 StringLiteral *AsmStr = cast_or_null<StringLiteral>(
4364 Importer.Import(S->getAsmString()));
4365 if (!AsmStr)
4366 return nullptr;
4367
4368 return new (Importer.getToContext()) GCCAsmStmt(
4369 Importer.getToContext(),
4370 Importer.Import(S->getAsmLoc()),
4371 S->isSimple(),
4372 S->isVolatile(),
4373 S->getNumOutputs(),
4374 S->getNumInputs(),
4375 Names.data(),
4376 Constraints.data(),
4377 Exprs.data(),
4378 AsmStr,
4379 S->getNumClobbers(),
4380 Clobbers.data(),
4381 Importer.Import(S->getRParenLoc()));
4382}
4383
Sean Callanan59721b32015-04-28 18:41:46 +00004384Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4385 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4386 for (Decl *ToD : ToDG) {
4387 if (!ToD)
4388 return nullptr;
4389 }
4390 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4391 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4392 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4393}
4394
4395Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4396 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4397 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4398 S->hasLeadingEmptyMacro());
4399}
4400
4401Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004402 llvm::SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004403
4404 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00004405 return nullptr;
4406
Sean Callanan59721b32015-04-28 18:41:46 +00004407 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4408 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
Benjamin Kramer07420902017-12-24 16:24:20 +00004409 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
4410 ToRBraceLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004411}
4412
4413Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4414 Expr *ToLHS = Importer.Import(S->getLHS());
4415 if (!ToLHS)
4416 return nullptr;
4417 Expr *ToRHS = Importer.Import(S->getRHS());
4418 if (!ToRHS && S->getRHS())
4419 return nullptr;
Gabor Horvath480892b2017-10-18 09:25:18 +00004420 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4421 if (!ToSubStmt && S->getSubStmt())
4422 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004423 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4424 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4425 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
Gabor Horvath480892b2017-10-18 09:25:18 +00004426 CaseStmt *ToStmt = new (Importer.getToContext())
4427 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
4428 ToStmt->setSubStmt(ToSubStmt);
4429 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00004430}
4431
4432Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4433 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4434 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4435 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4436 if (!ToSubStmt && S->getSubStmt())
4437 return nullptr;
4438 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4439 ToSubStmt);
4440}
4441
4442Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4443 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4444 LabelDecl *ToLabelDecl =
4445 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4446 if (!ToLabelDecl && S->getDecl())
4447 return nullptr;
4448 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4449 if (!ToSubStmt && S->getSubStmt())
4450 return nullptr;
4451 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4452 ToSubStmt);
4453}
4454
4455Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4456 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4457 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4458 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4459 ASTContext &_ToContext = Importer.getToContext();
4460 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4461 [&_ToContext](const Attr *A) -> const Attr * {
4462 return A->clone(_ToContext);
4463 });
4464 for (const Attr *ToA : ToAttrs) {
4465 if (!ToA)
4466 return nullptr;
4467 }
4468 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4469 if (!ToSubStmt && S->getSubStmt())
4470 return nullptr;
4471 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4472 ToAttrs, ToSubStmt);
4473}
4474
4475Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4476 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00004477 Stmt *ToInit = Importer.Import(S->getInit());
4478 if (!ToInit && S->getInit())
4479 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004480 VarDecl *ToConditionVariable = nullptr;
4481 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4482 ToConditionVariable =
4483 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4484 if (!ToConditionVariable)
4485 return nullptr;
4486 }
4487 Expr *ToCondition = Importer.Import(S->getCond());
4488 if (!ToCondition && S->getCond())
4489 return nullptr;
4490 Stmt *ToThenStmt = Importer.Import(S->getThen());
4491 if (!ToThenStmt && S->getThen())
4492 return nullptr;
4493 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4494 Stmt *ToElseStmt = Importer.Import(S->getElse());
4495 if (!ToElseStmt && S->getElse())
4496 return nullptr;
4497 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00004498 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00004499 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00004500 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00004501 ToCondition, ToThenStmt,
4502 ToElseLoc, ToElseStmt);
4503}
4504
4505Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00004506 Stmt *ToInit = Importer.Import(S->getInit());
4507 if (!ToInit && S->getInit())
4508 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004509 VarDecl *ToConditionVariable = nullptr;
4510 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4511 ToConditionVariable =
4512 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4513 if (!ToConditionVariable)
4514 return nullptr;
4515 }
4516 Expr *ToCondition = Importer.Import(S->getCond());
4517 if (!ToCondition && S->getCond())
4518 return nullptr;
4519 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00004520 Importer.getToContext(), ToInit,
4521 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00004522 Stmt *ToBody = Importer.Import(S->getBody());
4523 if (!ToBody && S->getBody())
4524 return nullptr;
4525 ToStmt->setBody(ToBody);
4526 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4527 // Now we have to re-chain the cases.
4528 SwitchCase *LastChainedSwitchCase = nullptr;
4529 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4530 SC = SC->getNextSwitchCase()) {
4531 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4532 if (!ToSC)
4533 return nullptr;
4534 if (LastChainedSwitchCase)
4535 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4536 else
4537 ToStmt->setSwitchCaseList(ToSC);
4538 LastChainedSwitchCase = ToSC;
4539 }
4540 return ToStmt;
4541}
4542
4543Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4544 VarDecl *ToConditionVariable = nullptr;
4545 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4546 ToConditionVariable =
4547 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4548 if (!ToConditionVariable)
4549 return nullptr;
4550 }
4551 Expr *ToCondition = Importer.Import(S->getCond());
4552 if (!ToCondition && S->getCond())
4553 return nullptr;
4554 Stmt *ToBody = Importer.Import(S->getBody());
4555 if (!ToBody && S->getBody())
4556 return nullptr;
4557 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4558 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4559 ToConditionVariable,
4560 ToCondition, ToBody,
4561 ToWhileLoc);
4562}
4563
4564Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4565 Stmt *ToBody = Importer.Import(S->getBody());
4566 if (!ToBody && S->getBody())
4567 return nullptr;
4568 Expr *ToCondition = Importer.Import(S->getCond());
4569 if (!ToCondition && S->getCond())
4570 return nullptr;
4571 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4572 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4573 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4574 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4575 ToDoLoc, ToWhileLoc,
4576 ToRParenLoc);
4577}
4578
4579Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4580 Stmt *ToInit = Importer.Import(S->getInit());
4581 if (!ToInit && S->getInit())
4582 return nullptr;
4583 Expr *ToCondition = Importer.Import(S->getCond());
4584 if (!ToCondition && S->getCond())
4585 return nullptr;
4586 VarDecl *ToConditionVariable = nullptr;
4587 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4588 ToConditionVariable =
4589 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4590 if (!ToConditionVariable)
4591 return nullptr;
4592 }
4593 Expr *ToInc = Importer.Import(S->getInc());
4594 if (!ToInc && S->getInc())
4595 return nullptr;
4596 Stmt *ToBody = Importer.Import(S->getBody());
4597 if (!ToBody && S->getBody())
4598 return nullptr;
4599 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4600 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4601 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4602 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4603 ToInit, ToCondition,
4604 ToConditionVariable,
4605 ToInc, ToBody,
4606 ToForLoc, ToLParenLoc,
4607 ToRParenLoc);
4608}
4609
4610Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4611 LabelDecl *ToLabel = nullptr;
4612 if (LabelDecl *FromLabel = S->getLabel()) {
4613 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4614 if (!ToLabel)
4615 return nullptr;
4616 }
4617 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4618 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4619 return new (Importer.getToContext()) GotoStmt(ToLabel,
4620 ToGotoLoc, ToLabelLoc);
4621}
4622
4623Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4624 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4625 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4626 Expr *ToTarget = Importer.Import(S->getTarget());
4627 if (!ToTarget && S->getTarget())
4628 return nullptr;
4629 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4630 ToTarget);
4631}
4632
4633Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4634 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4635 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4636}
4637
4638Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4639 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4640 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4641}
4642
4643Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4644 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4645 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4646 if (!ToRetExpr && S->getRetValue())
4647 return nullptr;
4648 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4649 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4650 if (!ToNRVOCandidate && NRVOCandidate)
4651 return nullptr;
4652 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4653 ToNRVOCandidate);
4654}
4655
4656Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4657 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4658 VarDecl *ToExceptionDecl = nullptr;
4659 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4660 ToExceptionDecl =
4661 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4662 if (!ToExceptionDecl)
4663 return nullptr;
4664 }
4665 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4666 if (!ToHandlerBlock && S->getHandlerBlock())
4667 return nullptr;
4668 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4669 ToExceptionDecl,
4670 ToHandlerBlock);
4671}
4672
4673Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4674 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4675 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4676 if (!ToTryBlock && S->getTryBlock())
4677 return nullptr;
4678 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4679 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4680 CXXCatchStmt *FromHandler = S->getHandler(HI);
4681 if (Stmt *ToHandler = Importer.Import(FromHandler))
4682 ToHandlers[HI] = ToHandler;
4683 else
4684 return nullptr;
4685 }
4686 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4687 ToHandlers);
4688}
4689
4690Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4691 DeclStmt *ToRange =
4692 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4693 if (!ToRange && S->getRangeStmt())
4694 return nullptr;
Richard Smith01694c32016-03-20 10:33:40 +00004695 DeclStmt *ToBegin =
4696 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
4697 if (!ToBegin && S->getBeginStmt())
4698 return nullptr;
4699 DeclStmt *ToEnd =
4700 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
4701 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00004702 return nullptr;
4703 Expr *ToCond = Importer.Import(S->getCond());
4704 if (!ToCond && S->getCond())
4705 return nullptr;
4706 Expr *ToInc = Importer.Import(S->getInc());
4707 if (!ToInc && S->getInc())
4708 return nullptr;
4709 DeclStmt *ToLoopVar =
4710 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4711 if (!ToLoopVar && S->getLoopVarStmt())
4712 return nullptr;
4713 Stmt *ToBody = Importer.Import(S->getBody());
4714 if (!ToBody && S->getBody())
4715 return nullptr;
4716 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00004717 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00004718 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4719 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00004720 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00004721 ToCond, ToInc,
4722 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00004723 ToForLoc, ToCoawaitLoc,
4724 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004725}
4726
4727Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
4728 Stmt *ToElem = Importer.Import(S->getElement());
4729 if (!ToElem && S->getElement())
4730 return nullptr;
4731 Expr *ToCollect = Importer.Import(S->getCollection());
4732 if (!ToCollect && S->getCollection())
4733 return nullptr;
4734 Stmt *ToBody = Importer.Import(S->getBody());
4735 if (!ToBody && S->getBody())
4736 return nullptr;
4737 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4738 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4739 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
4740 ToCollect,
4741 ToBody, ToForLoc,
4742 ToRParenLoc);
4743}
4744
4745Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
4746 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
4747 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4748 VarDecl *ToExceptionDecl = nullptr;
4749 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
4750 ToExceptionDecl =
4751 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4752 if (!ToExceptionDecl)
4753 return nullptr;
4754 }
4755 Stmt *ToBody = Importer.Import(S->getCatchBody());
4756 if (!ToBody && S->getCatchBody())
4757 return nullptr;
4758 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
4759 ToRParenLoc,
4760 ToExceptionDecl,
4761 ToBody);
4762}
4763
4764Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
4765 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
4766 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
4767 if (!ToAtFinallyStmt && S->getFinallyBody())
4768 return nullptr;
4769 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
4770 ToAtFinallyStmt);
4771}
4772
4773Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
4774 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
4775 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
4776 if (!ToAtTryStmt && S->getTryBody())
4777 return nullptr;
4778 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
4779 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
4780 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
4781 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
4782 ToCatchStmts[CI] = ToCatchStmt;
4783 else
4784 return nullptr;
4785 }
4786 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
4787 if (!ToAtFinallyStmt && S->getFinallyStmt())
4788 return nullptr;
4789 return ObjCAtTryStmt::Create(Importer.getToContext(),
4790 ToAtTryLoc, ToAtTryStmt,
4791 ToCatchStmts.begin(), ToCatchStmts.size(),
4792 ToAtFinallyStmt);
4793}
4794
4795Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
4796 (ObjCAtSynchronizedStmt *S) {
4797 SourceLocation ToAtSynchronizedLoc =
4798 Importer.Import(S->getAtSynchronizedLoc());
4799 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
4800 if (!ToSynchExpr && S->getSynchExpr())
4801 return nullptr;
4802 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
4803 if (!ToSynchBody && S->getSynchBody())
4804 return nullptr;
4805 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
4806 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
4807}
4808
4809Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
4810 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
4811 Expr *ToThrow = Importer.Import(S->getThrowExpr());
4812 if (!ToThrow && S->getThrowExpr())
4813 return nullptr;
4814 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
4815}
4816
4817Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
4818 (ObjCAutoreleasePoolStmt *S) {
4819 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
4820 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4821 if (!ToSubStmt && S->getSubStmt())
4822 return nullptr;
4823 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
4824 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004825}
4826
4827//----------------------------------------------------------------------------
4828// Import Expressions
4829//----------------------------------------------------------------------------
4830Expr *ASTNodeImporter::VisitExpr(Expr *E) {
4831 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
4832 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00004833 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004834}
4835
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004836Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
4837 QualType T = Importer.Import(E->getType());
4838 if (T.isNull())
4839 return nullptr;
4840
4841 Expr *SubExpr = Importer.Import(E->getSubExpr());
4842 if (!SubExpr && E->getSubExpr())
4843 return nullptr;
4844
4845 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
4846 if (!TInfo)
4847 return nullptr;
4848
4849 return new (Importer.getToContext()) VAArgExpr(
4850 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
4851 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
4852}
4853
4854
4855Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
4856 QualType T = Importer.Import(E->getType());
4857 if (T.isNull())
4858 return nullptr;
4859
4860 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00004861 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004862}
4863
4864Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
4865 QualType T = Importer.Import(E->getType());
4866 if (T.isNull())
4867 return nullptr;
4868
4869 StringLiteral *SL = cast_or_null<StringLiteral>(
4870 Importer.Import(E->getFunctionName()));
4871 if (!SL && E->getFunctionName())
4872 return nullptr;
4873
4874 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00004875 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004876}
4877
Douglas Gregor52f820e2010-02-19 01:17:02 +00004878Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00004879 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
4880 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00004881 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004882
Craig Topper36250ad2014-05-12 05:36:57 +00004883 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004884 if (E->getDecl() != E->getFoundDecl()) {
4885 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
4886 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00004887 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004888 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00004889
4890 QualType T = Importer.Import(E->getType());
4891 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004892 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004893
Aleksei Sidorina693b372016-09-28 10:16:56 +00004894
4895 TemplateArgumentListInfo ToTAInfo;
4896 TemplateArgumentListInfo *ResInfo = nullptr;
4897 if (E->hasExplicitTemplateArgs()) {
4898 for (const auto &FromLoc : E->template_arguments()) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004899 if (auto ToTALoc = ImportTemplateArgumentLoc(FromLoc))
4900 ToTAInfo.addArgument(*ToTALoc);
4901 else
Aleksei Sidorina693b372016-09-28 10:16:56 +00004902 return nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00004903 }
4904 ResInfo = &ToTAInfo;
4905 }
4906
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004907 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
4908 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004909 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004910 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00004911 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004912 Importer.Import(E->getLocation()),
4913 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00004914 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004915 if (E->hadMultipleCandidates())
4916 DRE->setHadMultipleCandidates(true);
4917 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00004918}
4919
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004920Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
4921 QualType T = Importer.Import(E->getType());
4922 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00004923 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004924
4925 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
4926}
4927
4928ASTNodeImporter::Designator
4929ASTNodeImporter::ImportDesignator(const Designator &D) {
4930 if (D.isFieldDesignator()) {
4931 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
4932 // Caller checks for import error
4933 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
4934 Importer.Import(D.getFieldLoc()));
4935 }
4936 if (D.isArrayDesignator())
4937 return Designator(D.getFirstExprIndex(),
4938 Importer.Import(D.getLBracketLoc()),
4939 Importer.Import(D.getRBracketLoc()));
4940
4941 assert(D.isArrayRangeDesignator());
4942 return Designator(D.getFirstExprIndex(),
4943 Importer.Import(D.getLBracketLoc()),
4944 Importer.Import(D.getEllipsisLoc()),
4945 Importer.Import(D.getRBracketLoc()));
4946}
4947
4948
4949Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
4950 Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
4951 if (!Init)
4952 return nullptr;
4953
4954 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
4955 // List elements from the second, the first is Init itself
4956 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
4957 if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
4958 IndexExprs[I - 1] = Arg;
4959 else
4960 return nullptr;
4961 }
4962
4963 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00004964 llvm::transform(DIE->designators(), Designators.begin(),
4965 [this](const Designator &D) -> Designator {
4966 return ImportDesignator(D);
4967 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004968
David Majnemerf7e36092016-06-23 00:15:04 +00004969 for (const Designator &D : DIE->designators())
4970 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004971 return nullptr;
4972
4973 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00004974 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004975 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
4976 DIE->usesGNUSyntax(), Init);
4977}
4978
4979Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
4980 QualType T = Importer.Import(E->getType());
4981 if (T.isNull())
4982 return nullptr;
4983
4984 return new (Importer.getToContext())
4985 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
4986}
4987
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004988Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
4989 QualType T = Importer.Import(E->getType());
4990 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004991 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004992
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00004993 return IntegerLiteral::Create(Importer.getToContext(),
4994 E->getValue(), T,
4995 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004996}
4997
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004998Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
4999 QualType T = Importer.Import(E->getType());
5000 if (T.isNull())
5001 return nullptr;
5002
5003 return FloatingLiteral::Create(Importer.getToContext(),
5004 E->getValue(), E->isExact(), T,
5005 Importer.Import(E->getLocation()));
5006}
5007
Douglas Gregor623421d2010-02-18 02:21:22 +00005008Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5009 QualType T = Importer.Import(E->getType());
5010 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005011 return nullptr;
5012
Douglas Gregorfb65e592011-07-27 05:40:30 +00005013 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5014 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005015 Importer.Import(E->getLocation()));
5016}
5017
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005018Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5019 QualType T = Importer.Import(E->getType());
5020 if (T.isNull())
5021 return nullptr;
5022
5023 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5024 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5025
5026 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5027 E->getKind(), E->isPascal(), T,
5028 Locations.data(), Locations.size());
5029}
5030
5031Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5032 QualType T = Importer.Import(E->getType());
5033 if (T.isNull())
5034 return nullptr;
5035
5036 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5037 if (!TInfo)
5038 return nullptr;
5039
5040 Expr *Init = Importer.Import(E->getInitializer());
5041 if (!Init)
5042 return nullptr;
5043
5044 return new (Importer.getToContext()) CompoundLiteralExpr(
5045 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5046 Init, E->isFileScope());
5047}
5048
5049Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5050 QualType T = Importer.Import(E->getType());
5051 if (T.isNull())
5052 return nullptr;
5053
5054 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5055 if (ImportArrayChecked(
5056 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5057 Exprs.begin()))
5058 return nullptr;
5059
5060 return new (Importer.getToContext()) AtomicExpr(
5061 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5062 Importer.Import(E->getRParenLoc()));
5063}
5064
5065Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5066 QualType T = Importer.Import(E->getType());
5067 if (T.isNull())
5068 return nullptr;
5069
5070 LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
5071 if (!ToLabel)
5072 return nullptr;
5073
5074 return new (Importer.getToContext()) AddrLabelExpr(
5075 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5076 ToLabel, T);
5077}
5078
Douglas Gregorc74247e2010-02-19 01:07:06 +00005079Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5080 Expr *SubExpr = Importer.Import(E->getSubExpr());
5081 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005082 return nullptr;
5083
Douglas Gregorc74247e2010-02-19 01:07:06 +00005084 return new (Importer.getToContext())
5085 ParenExpr(Importer.Import(E->getLParen()),
5086 Importer.Import(E->getRParen()),
5087 SubExpr);
5088}
5089
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005090Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5091 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005092 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005093 return nullptr;
5094
5095 return new (Importer.getToContext()) ParenListExpr(
5096 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5097 Exprs, Importer.Import(E->getLParenLoc()));
5098}
5099
5100Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5101 QualType T = Importer.Import(E->getType());
5102 if (T.isNull())
5103 return nullptr;
5104
5105 CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>(
5106 Importer.Import(E->getSubStmt()));
5107 if (!ToSubStmt && E->getSubStmt())
5108 return nullptr;
5109
5110 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5111 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5112}
5113
Douglas Gregorc74247e2010-02-19 01:07:06 +00005114Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5115 QualType T = Importer.Import(E->getType());
5116 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005117 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005118
5119 Expr *SubExpr = Importer.Import(E->getSubExpr());
5120 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005121 return nullptr;
5122
Aaron Ballmana5038552018-01-09 13:07:03 +00005123 return new (Importer.getToContext()) UnaryOperator(
5124 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(),
5125 Importer.Import(E->getOperatorLoc()), E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005126}
5127
Aaron Ballmana5038552018-01-09 13:07:03 +00005128Expr *
5129ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005130 QualType ResultType = Importer.Import(E->getType());
5131
5132 if (E->isArgumentType()) {
5133 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5134 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005135 return nullptr;
5136
Peter Collingbournee190dee2011-03-11 19:24:49 +00005137 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5138 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005139 Importer.Import(E->getOperatorLoc()),
5140 Importer.Import(E->getRParenLoc()));
5141 }
5142
5143 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5144 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005145 return nullptr;
5146
Peter Collingbournee190dee2011-03-11 19:24:49 +00005147 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5148 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005149 Importer.Import(E->getOperatorLoc()),
5150 Importer.Import(E->getRParenLoc()));
5151}
5152
Douglas Gregorc74247e2010-02-19 01:07:06 +00005153Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5154 QualType T = Importer.Import(E->getType());
5155 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005156 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005157
5158 Expr *LHS = Importer.Import(E->getLHS());
5159 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005160 return nullptr;
5161
Douglas Gregorc74247e2010-02-19 01:07:06 +00005162 Expr *RHS = Importer.Import(E->getRHS());
5163 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005164 return nullptr;
5165
Douglas Gregorc74247e2010-02-19 01:07:06 +00005166 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005167 T, E->getValueKind(),
5168 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005169 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005170 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005171}
5172
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005173Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5174 QualType T = Importer.Import(E->getType());
5175 if (T.isNull())
5176 return nullptr;
5177
5178 Expr *ToLHS = Importer.Import(E->getLHS());
5179 if (!ToLHS)
5180 return nullptr;
5181
5182 Expr *ToRHS = Importer.Import(E->getRHS());
5183 if (!ToRHS)
5184 return nullptr;
5185
5186 Expr *ToCond = Importer.Import(E->getCond());
5187 if (!ToCond)
5188 return nullptr;
5189
5190 return new (Importer.getToContext()) ConditionalOperator(
5191 ToCond, Importer.Import(E->getQuestionLoc()),
5192 ToLHS, Importer.Import(E->getColonLoc()),
5193 ToRHS, T, E->getValueKind(), E->getObjectKind());
5194}
5195
5196Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5197 BinaryConditionalOperator *E) {
5198 QualType T = Importer.Import(E->getType());
5199 if (T.isNull())
5200 return nullptr;
5201
5202 Expr *Common = Importer.Import(E->getCommon());
5203 if (!Common)
5204 return nullptr;
5205
5206 Expr *Cond = Importer.Import(E->getCond());
5207 if (!Cond)
5208 return nullptr;
5209
5210 OpaqueValueExpr *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5211 Importer.Import(E->getOpaqueValue()));
5212 if (!OpaqueValue)
5213 return nullptr;
5214
5215 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5216 if (!TrueExpr)
5217 return nullptr;
5218
5219 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5220 if (!FalseExpr)
5221 return nullptr;
5222
5223 return new (Importer.getToContext()) BinaryConditionalOperator(
5224 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5225 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5226 T, E->getValueKind(), E->getObjectKind());
5227}
5228
Aleksei Sidorina693b372016-09-28 10:16:56 +00005229Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5230 QualType T = Importer.Import(E->getType());
5231 if (T.isNull())
5232 return nullptr;
5233
5234 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5235 if (!ToQueried)
5236 return nullptr;
5237
5238 Expr *Dim = Importer.Import(E->getDimensionExpression());
5239 if (!Dim && E->getDimensionExpression())
5240 return nullptr;
5241
5242 return new (Importer.getToContext()) ArrayTypeTraitExpr(
5243 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5244 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
5245}
5246
5247Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5248 QualType T = Importer.Import(E->getType());
5249 if (T.isNull())
5250 return nullptr;
5251
5252 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5253 if (!ToQueried)
5254 return nullptr;
5255
5256 return new (Importer.getToContext()) ExpressionTraitExpr(
5257 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5258 E->getValue(), Importer.Import(E->getLocEnd()), T);
5259}
5260
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005261Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5262 QualType T = Importer.Import(E->getType());
5263 if (T.isNull())
5264 return nullptr;
5265
5266 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5267 if (!SourceExpr && E->getSourceExpr())
5268 return nullptr;
5269
5270 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005271 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005272 E->getObjectKind(), SourceExpr);
5273}
5274
Aleksei Sidorina693b372016-09-28 10:16:56 +00005275Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5276 QualType T = Importer.Import(E->getType());
5277 if (T.isNull())
5278 return nullptr;
5279
5280 Expr *ToLHS = Importer.Import(E->getLHS());
5281 if (!ToLHS)
5282 return nullptr;
5283
5284 Expr *ToRHS = Importer.Import(E->getRHS());
5285 if (!ToRHS)
5286 return nullptr;
5287
5288 return new (Importer.getToContext()) ArraySubscriptExpr(
5289 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5290 Importer.Import(E->getRBracketLoc()));
5291}
5292
Douglas Gregorc74247e2010-02-19 01:07:06 +00005293Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5294 QualType T = Importer.Import(E->getType());
5295 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005296 return nullptr;
5297
Douglas Gregorc74247e2010-02-19 01:07:06 +00005298 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5299 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005300 return nullptr;
5301
Douglas Gregorc74247e2010-02-19 01:07:06 +00005302 QualType CompResultType = Importer.Import(E->getComputationResultType());
5303 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005304 return nullptr;
5305
Douglas Gregorc74247e2010-02-19 01:07:06 +00005306 Expr *LHS = Importer.Import(E->getLHS());
5307 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005308 return nullptr;
5309
Douglas Gregorc74247e2010-02-19 01:07:06 +00005310 Expr *RHS = Importer.Import(E->getRHS());
5311 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005312 return nullptr;
5313
Douglas Gregorc74247e2010-02-19 01:07:06 +00005314 return new (Importer.getToContext())
5315 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005316 T, E->getValueKind(),
5317 E->getObjectKind(),
5318 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005319 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005320 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005321}
5322
Aleksei Sidorina693b372016-09-28 10:16:56 +00005323bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
5324 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
5325 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
5326 Path.push_back(Spec);
5327 else
5328 return true;
5329 }
5330 return false;
John McCallcf142162010-08-07 06:22:56 +00005331}
5332
Douglas Gregor98c10182010-02-12 22:17:39 +00005333Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5334 QualType T = Importer.Import(E->getType());
5335 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005336 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005337
5338 Expr *SubExpr = Importer.Import(E->getSubExpr());
5339 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005340 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005341
5342 CXXCastPath BasePath;
5343 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005344 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005345
5346 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005347 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005348}
5349
Aleksei Sidorina693b372016-09-28 10:16:56 +00005350Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00005351 QualType T = Importer.Import(E->getType());
5352 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005353 return nullptr;
5354
Douglas Gregor5481d322010-02-19 01:32:14 +00005355 Expr *SubExpr = Importer.Import(E->getSubExpr());
5356 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005357 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005358
5359 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5360 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005361 return nullptr;
5362
John McCallcf142162010-08-07 06:22:56 +00005363 CXXCastPath BasePath;
5364 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005365 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005366
Aleksei Sidorina693b372016-09-28 10:16:56 +00005367 switch (E->getStmtClass()) {
5368 case Stmt::CStyleCastExprClass: {
5369 CStyleCastExpr *CCE = cast<CStyleCastExpr>(E);
5370 return CStyleCastExpr::Create(Importer.getToContext(), T,
5371 E->getValueKind(), E->getCastKind(),
5372 SubExpr, &BasePath, TInfo,
5373 Importer.Import(CCE->getLParenLoc()),
5374 Importer.Import(CCE->getRParenLoc()));
5375 }
5376
5377 case Stmt::CXXFunctionalCastExprClass: {
5378 CXXFunctionalCastExpr *FCE = cast<CXXFunctionalCastExpr>(E);
5379 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
5380 E->getValueKind(), TInfo,
5381 E->getCastKind(), SubExpr, &BasePath,
5382 Importer.Import(FCE->getLParenLoc()),
5383 Importer.Import(FCE->getRParenLoc()));
5384 }
5385
5386 case Stmt::ObjCBridgedCastExprClass: {
5387 ObjCBridgedCastExpr *OCE = cast<ObjCBridgedCastExpr>(E);
5388 return new (Importer.getToContext()) ObjCBridgedCastExpr(
5389 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
5390 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
5391 TInfo, SubExpr);
5392 }
5393 default:
5394 break; // just fall through
5395 }
5396
5397 CXXNamedCastExpr *Named = cast<CXXNamedCastExpr>(E);
5398 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
5399 RParenLoc = Importer.Import(Named->getRParenLoc());
5400 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
5401
5402 switch (E->getStmtClass()) {
5403 case Stmt::CXXStaticCastExprClass:
5404 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
5405 E->getValueKind(), E->getCastKind(),
5406 SubExpr, &BasePath, TInfo,
5407 ExprLoc, RParenLoc, Brackets);
5408
5409 case Stmt::CXXDynamicCastExprClass:
5410 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
5411 E->getValueKind(), E->getCastKind(),
5412 SubExpr, &BasePath, TInfo,
5413 ExprLoc, RParenLoc, Brackets);
5414
5415 case Stmt::CXXReinterpretCastExprClass:
5416 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
5417 E->getValueKind(), E->getCastKind(),
5418 SubExpr, &BasePath, TInfo,
5419 ExprLoc, RParenLoc, Brackets);
5420
5421 case Stmt::CXXConstCastExprClass:
5422 return CXXConstCastExpr::Create(Importer.getToContext(), T,
5423 E->getValueKind(), SubExpr, TInfo, ExprLoc,
5424 RParenLoc, Brackets);
5425 default:
5426 llvm_unreachable("Cast expression of unsupported type!");
5427 return nullptr;
5428 }
5429}
5430
5431Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
5432 QualType T = Importer.Import(OE->getType());
5433 if (T.isNull())
5434 return nullptr;
5435
5436 SmallVector<OffsetOfNode, 4> Nodes;
5437 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
5438 const OffsetOfNode &Node = OE->getComponent(I);
5439
5440 switch (Node.getKind()) {
5441 case OffsetOfNode::Array:
5442 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
5443 Node.getArrayExprIndex(),
5444 Importer.Import(Node.getLocEnd())));
5445 break;
5446
5447 case OffsetOfNode::Base: {
5448 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
5449 if (!BS && Node.getBase())
5450 return nullptr;
5451 Nodes.push_back(OffsetOfNode(BS));
5452 break;
5453 }
5454 case OffsetOfNode::Field: {
5455 FieldDecl *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
5456 if (!FD)
5457 return nullptr;
5458 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
5459 Importer.Import(Node.getLocEnd())));
5460 break;
5461 }
5462 case OffsetOfNode::Identifier: {
5463 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
5464 if (!ToII)
5465 return nullptr;
5466 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
5467 Importer.Import(Node.getLocEnd())));
5468 break;
5469 }
5470 }
5471 }
5472
5473 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
5474 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
5475 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
5476 if (!ToIndexExpr)
5477 return nullptr;
5478 Exprs[I] = ToIndexExpr;
5479 }
5480
5481 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
5482 if (!TInfo && OE->getTypeSourceInfo())
5483 return nullptr;
5484
5485 return OffsetOfExpr::Create(Importer.getToContext(), T,
5486 Importer.Import(OE->getOperatorLoc()),
5487 TInfo, Nodes, Exprs,
5488 Importer.Import(OE->getRParenLoc()));
5489}
5490
5491Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
5492 QualType T = Importer.Import(E->getType());
5493 if (T.isNull())
5494 return nullptr;
5495
5496 Expr *Operand = Importer.Import(E->getOperand());
5497 if (!Operand)
5498 return nullptr;
5499
5500 CanThrowResult CanThrow;
5501 if (E->isValueDependent())
5502 CanThrow = CT_Dependent;
5503 else
5504 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
5505
5506 return new (Importer.getToContext()) CXXNoexceptExpr(
5507 T, Operand, CanThrow,
5508 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
5509}
5510
5511Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
5512 QualType T = Importer.Import(E->getType());
5513 if (T.isNull())
5514 return nullptr;
5515
5516 Expr *SubExpr = Importer.Import(E->getSubExpr());
5517 if (!SubExpr && E->getSubExpr())
5518 return nullptr;
5519
5520 return new (Importer.getToContext()) CXXThrowExpr(
5521 SubExpr, T, Importer.Import(E->getThrowLoc()),
5522 E->isThrownVariableInScope());
5523}
5524
5525Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
5526 ParmVarDecl *Param = cast_or_null<ParmVarDecl>(
5527 Importer.Import(E->getParam()));
5528 if (!Param)
5529 return nullptr;
5530
5531 return CXXDefaultArgExpr::Create(
5532 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
5533}
5534
5535Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5536 QualType T = Importer.Import(E->getType());
5537 if (T.isNull())
5538 return nullptr;
5539
5540 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
5541 if (!TypeInfo)
5542 return nullptr;
5543
5544 return new (Importer.getToContext()) CXXScalarValueInitExpr(
5545 T, TypeInfo, Importer.Import(E->getRParenLoc()));
5546}
5547
5548Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
5549 Expr *SubExpr = Importer.Import(E->getSubExpr());
5550 if (!SubExpr)
5551 return nullptr;
5552
5553 auto *Dtor = cast_or_null<CXXDestructorDecl>(
5554 Importer.Import(const_cast<CXXDestructorDecl *>(
5555 E->getTemporary()->getDestructor())));
5556 if (!Dtor)
5557 return nullptr;
5558
5559 ASTContext &ToCtx = Importer.getToContext();
5560 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
5561 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
5562}
5563
5564Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
5565 QualType T = Importer.Import(CE->getType());
5566 if (T.isNull())
5567 return nullptr;
5568
5569 SmallVector<Expr *, 8> Args(CE->getNumArgs());
5570 if (ImportContainerChecked(CE->arguments(), Args))
5571 return nullptr;
5572
5573 auto *Ctor = cast_or_null<CXXConstructorDecl>(
5574 Importer.Import(CE->getConstructor()));
5575 if (!Ctor)
5576 return nullptr;
5577
5578 return CXXTemporaryObjectExpr::Create(
5579 Importer.getToContext(), T,
5580 Importer.Import(CE->getLocStart()),
5581 Ctor,
5582 CE->isElidable(),
5583 Args,
5584 CE->hadMultipleCandidates(),
5585 CE->isListInitialization(),
5586 CE->isStdInitListInitialization(),
5587 CE->requiresZeroInitialization(),
5588 CE->getConstructionKind(),
5589 Importer.Import(CE->getParenOrBraceRange()));
5590}
5591
5592Expr *
5593ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
5594 QualType T = Importer.Import(E->getType());
5595 if (T.isNull())
5596 return nullptr;
5597
5598 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
5599 if (!TempE)
5600 return nullptr;
5601
5602 ValueDecl *ExtendedBy = cast_or_null<ValueDecl>(
5603 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
5604 if (!ExtendedBy && E->getExtendingDecl())
5605 return nullptr;
5606
5607 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
5608 T, TempE, E->isBoundToLvalueReference());
5609
5610 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
5611 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
5612 return ToMTE;
5613}
5614
Gabor Horvath7a91c082017-11-14 11:30:38 +00005615Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
5616 QualType T = Importer.Import(E->getType());
5617 if (T.isNull())
5618 return nullptr;
5619
5620 Expr *Pattern = Importer.Import(E->getPattern());
5621 if (!Pattern)
5622 return nullptr;
5623
5624 return new (Importer.getToContext()) PackExpansionExpr(
5625 T, Pattern, Importer.Import(E->getEllipsisLoc()),
5626 E->getNumExpansions());
5627}
5628
Aleksei Sidorina693b372016-09-28 10:16:56 +00005629Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
5630 QualType T = Importer.Import(CE->getType());
5631 if (T.isNull())
5632 return nullptr;
5633
5634 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
5635 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
5636 return nullptr;
5637
5638 FunctionDecl *OperatorNewDecl = cast_or_null<FunctionDecl>(
5639 Importer.Import(CE->getOperatorNew()));
5640 if (!OperatorNewDecl && CE->getOperatorNew())
5641 return nullptr;
5642
5643 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5644 Importer.Import(CE->getOperatorDelete()));
5645 if (!OperatorDeleteDecl && CE->getOperatorDelete())
5646 return nullptr;
5647
5648 Expr *ToInit = Importer.Import(CE->getInitializer());
5649 if (!ToInit && CE->getInitializer())
5650 return nullptr;
5651
5652 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
5653 if (!TInfo)
5654 return nullptr;
5655
5656 Expr *ToArrSize = Importer.Import(CE->getArraySize());
5657 if (!ToArrSize && CE->getArraySize())
5658 return nullptr;
5659
5660 return new (Importer.getToContext()) CXXNewExpr(
5661 Importer.getToContext(),
5662 CE->isGlobalNew(),
5663 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00005664 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005665 CE->doesUsualArrayDeleteWantSize(),
5666 PlacementArgs,
5667 Importer.Import(CE->getTypeIdParens()),
5668 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
5669 Importer.Import(CE->getSourceRange()),
5670 Importer.Import(CE->getDirectInitRange()));
5671}
5672
5673Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
5674 QualType T = Importer.Import(E->getType());
5675 if (T.isNull())
5676 return nullptr;
5677
5678 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5679 Importer.Import(E->getOperatorDelete()));
5680 if (!OperatorDeleteDecl && E->getOperatorDelete())
5681 return nullptr;
5682
5683 Expr *ToArg = Importer.Import(E->getArgument());
5684 if (!ToArg && E->getArgument())
5685 return nullptr;
5686
5687 return new (Importer.getToContext()) CXXDeleteExpr(
5688 T, E->isGlobalDelete(),
5689 E->isArrayForm(),
5690 E->isArrayFormAsWritten(),
5691 E->doesUsualArrayDeleteWantSize(),
5692 OperatorDeleteDecl,
5693 ToArg,
5694 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005695}
5696
Sean Callanan59721b32015-04-28 18:41:46 +00005697Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5698 QualType T = Importer.Import(E->getType());
5699 if (T.isNull())
5700 return nullptr;
5701
5702 CXXConstructorDecl *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00005703 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00005704 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00005705 return nullptr;
5706
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005707 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005708 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00005709 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005710
5711 return CXXConstructExpr::Create(Importer.getToContext(), T,
5712 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00005713 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00005714 ToArgs, E->hadMultipleCandidates(),
5715 E->isListInitialization(),
5716 E->isStdInitListInitialization(),
5717 E->requiresZeroInitialization(),
5718 E->getConstructionKind(),
5719 Importer.Import(E->getParenOrBraceRange()));
5720}
5721
Aleksei Sidorina693b372016-09-28 10:16:56 +00005722Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
5723 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
5724 if (!SubExpr && EWC->getSubExpr())
5725 return nullptr;
5726
5727 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
5728 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
5729 if (ExprWithCleanups::CleanupObject Obj =
5730 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
5731 Objs[I] = Obj;
5732 else
5733 return nullptr;
5734
5735 return ExprWithCleanups::Create(Importer.getToContext(),
5736 SubExpr, EWC->cleanupsHaveSideEffects(),
5737 Objs);
5738}
5739
Sean Callanan8bca9962016-03-28 21:43:01 +00005740Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
5741 QualType T = Importer.Import(E->getType());
5742 if (T.isNull())
5743 return nullptr;
5744
5745 Expr *ToFn = Importer.Import(E->getCallee());
5746 if (!ToFn)
5747 return nullptr;
5748
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005749 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005750 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00005751 return nullptr;
5752
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005753 return new (Importer.getToContext()) CXXMemberCallExpr(
5754 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
5755 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00005756}
5757
5758Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
5759 QualType T = Importer.Import(E->getType());
5760 if (T.isNull())
5761 return nullptr;
5762
5763 return new (Importer.getToContext())
5764 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
5765}
5766
5767Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
5768 QualType T = Importer.Import(E->getType());
5769 if (T.isNull())
5770 return nullptr;
5771
5772 return new (Importer.getToContext())
5773 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
5774}
5775
5776
Sean Callanan59721b32015-04-28 18:41:46 +00005777Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5778 QualType T = Importer.Import(E->getType());
5779 if (T.isNull())
5780 return nullptr;
5781
5782 Expr *ToBase = Importer.Import(E->getBase());
5783 if (!ToBase && E->getBase())
5784 return nullptr;
5785
5786 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5787 if (!ToMember && E->getMemberDecl())
5788 return nullptr;
5789
5790 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5791 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5792 E->getFoundDecl().getAccess());
5793
5794 DeclarationNameInfo ToMemberNameInfo(
5795 Importer.Import(E->getMemberNameInfo().getName()),
5796 Importer.Import(E->getMemberNameInfo().getLoc()));
5797
5798 if (E->hasExplicitTemplateArgs()) {
5799 return nullptr; // FIXME: handle template arguments
5800 }
5801
5802 return MemberExpr::Create(Importer.getToContext(), ToBase,
5803 E->isArrow(),
5804 Importer.Import(E->getOperatorLoc()),
5805 Importer.Import(E->getQualifierLoc()),
5806 Importer.Import(E->getTemplateKeywordLoc()),
5807 ToMember, ToFoundDecl, ToMemberNameInfo,
5808 nullptr, T, E->getValueKind(),
5809 E->getObjectKind());
5810}
5811
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00005812Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr(
5813 CXXPseudoDestructorExpr *E) {
5814
5815 Expr *BaseE = Importer.Import(E->getBase());
5816 if (!BaseE)
5817 return nullptr;
5818
5819 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo());
5820 if (!ScopeInfo && E->getScopeTypeInfo())
5821 return nullptr;
5822
5823 PseudoDestructorTypeStorage Storage;
5824 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
5825 IdentifierInfo *ToII = Importer.Import(FromII);
5826 if (!ToII)
5827 return nullptr;
5828 Storage = PseudoDestructorTypeStorage(
5829 ToII, Importer.Import(E->getDestroyedTypeLoc()));
5830 } else {
5831 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo());
5832 if (!TI)
5833 return nullptr;
5834 Storage = PseudoDestructorTypeStorage(TI);
5835 }
5836
5837 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
5838 Importer.getToContext(), BaseE, E->isArrow(),
5839 Importer.Import(E->getOperatorLoc()),
5840 Importer.Import(E->getQualifierLoc()),
5841 ScopeInfo, Importer.Import(E->getColonColonLoc()),
5842 Importer.Import(E->getTildeLoc()), Storage);
5843}
5844
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005845Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
5846 CXXDependentScopeMemberExpr *E) {
5847 Expr *Base = nullptr;
5848 if (!E->isImplicitAccess()) {
5849 Base = Importer.Import(E->getBase());
5850 if (!Base)
5851 return nullptr;
5852 }
5853
5854 QualType BaseType = Importer.Import(E->getBaseType());
5855 if (BaseType.isNull())
5856 return nullptr;
5857
5858 TemplateArgumentListInfo ToTAInfo(Importer.Import(E->getLAngleLoc()),
5859 Importer.Import(E->getRAngleLoc()));
5860 TemplateArgumentListInfo *ResInfo = nullptr;
5861 if (E->hasExplicitTemplateArgs()) {
5862 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
5863 return nullptr;
5864 ResInfo = &ToTAInfo;
5865 }
5866
5867 DeclarationName Name = Importer.Import(E->getMember());
5868 if (!E->getMember().isEmpty() && Name.isEmpty())
5869 return nullptr;
5870
5871 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc()));
5872 // Import additional name location/type info.
5873 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo);
5874 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope());
5875 if (!ToFQ && E->getFirstQualifierFoundInScope())
5876 return nullptr;
5877
5878 return CXXDependentScopeMemberExpr::Create(
5879 Importer.getToContext(), Base, BaseType, E->isArrow(),
5880 Importer.Import(E->getOperatorLoc()),
5881 Importer.Import(E->getQualifierLoc()),
5882 Importer.Import(E->getTemplateKeywordLoc()),
5883 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo);
5884}
5885
Sean Callanan59721b32015-04-28 18:41:46 +00005886Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5887 QualType T = Importer.Import(E->getType());
5888 if (T.isNull())
5889 return nullptr;
5890
5891 Expr *ToCallee = Importer.Import(E->getCallee());
5892 if (!ToCallee && E->getCallee())
5893 return nullptr;
5894
5895 unsigned NumArgs = E->getNumArgs();
5896
5897 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5898
5899 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5900 Expr *FromArg = E->getArg(ai);
5901 Expr *ToArg = Importer.Import(FromArg);
5902 if (!ToArg)
5903 return nullptr;
5904 ToArgs[ai] = ToArg;
5905 }
5906
5907 Expr **ToArgs_Copied = new (Importer.getToContext())
5908 Expr*[NumArgs];
5909
5910 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5911 ToArgs_Copied[ai] = ToArgs[ai];
5912
5913 return new (Importer.getToContext())
5914 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00005915 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00005916 Importer.Import(E->getRParenLoc()));
5917}
5918
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005919Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
5920 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00005921 if (T.isNull())
5922 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00005923
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005924 llvm::SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005925 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00005926 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00005927
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005928 ASTContext &ToCtx = Importer.getToContext();
5929 InitListExpr *To = new (ToCtx) InitListExpr(
5930 ToCtx, Importer.Import(ILE->getLBraceLoc()),
5931 Exprs, Importer.Import(ILE->getLBraceLoc()));
5932 To->setType(T);
5933
5934 if (ILE->hasArrayFiller()) {
5935 Expr *Filler = Importer.Import(ILE->getArrayFiller());
5936 if (!Filler)
5937 return nullptr;
5938 To->setArrayFiller(Filler);
5939 }
5940
5941 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
5942 FieldDecl *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
5943 if (!ToFD)
5944 return nullptr;
5945 To->setInitializedFieldInUnion(ToFD);
5946 }
5947
5948 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
5949 InitListExpr *ToSyntForm = cast_or_null<InitListExpr>(
5950 Importer.Import(SyntForm));
5951 if (!ToSyntForm)
5952 return nullptr;
5953 To->setSyntacticForm(ToSyntForm);
5954 }
5955
5956 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
5957 To->setValueDependent(ILE->isValueDependent());
5958 To->setInstantiationDependent(ILE->isInstantiationDependent());
5959
5960 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00005961}
5962
Richard Smith30e304e2016-12-14 00:03:17 +00005963Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
5964 QualType ToType = Importer.Import(E->getType());
5965 if (ToType.isNull())
5966 return nullptr;
5967
5968 Expr *ToCommon = Importer.Import(E->getCommonExpr());
5969 if (!ToCommon && E->getCommonExpr())
5970 return nullptr;
5971
5972 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
5973 if (!ToSubExpr && E->getSubExpr())
5974 return nullptr;
5975
5976 return new (Importer.getToContext())
5977 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
5978}
5979
5980Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
5981 QualType ToType = Importer.Import(E->getType());
5982 if (ToType.isNull())
5983 return nullptr;
5984 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
5985}
5986
Sean Callanandd2c1742016-05-16 20:48:03 +00005987Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
5988 FieldDecl *ToField = llvm::dyn_cast_or_null<FieldDecl>(
5989 Importer.Import(DIE->getField()));
5990 if (!ToField && DIE->getField())
5991 return nullptr;
5992
5993 return CXXDefaultInitExpr::Create(
5994 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
5995}
5996
5997Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
5998 QualType ToType = Importer.Import(E->getType());
5999 if (ToType.isNull() && !E->getType().isNull())
6000 return nullptr;
6001 ExprValueKind VK = E->getValueKind();
6002 CastKind CK = E->getCastKind();
6003 Expr *ToOp = Importer.Import(E->getSubExpr());
6004 if (!ToOp && E->getSubExpr())
6005 return nullptr;
6006 CXXCastPath BasePath;
6007 if (ImportCastPath(E, BasePath))
6008 return nullptr;
6009 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6010 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6011 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6012 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
6013
6014 if (isa<CXXStaticCastExpr>(E)) {
6015 return CXXStaticCastExpr::Create(
6016 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6017 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6018 } else if (isa<CXXDynamicCastExpr>(E)) {
6019 return CXXDynamicCastExpr::Create(
6020 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6021 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6022 } else if (isa<CXXReinterpretCastExpr>(E)) {
6023 return CXXReinterpretCastExpr::Create(
6024 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6025 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6026 } else {
6027 return nullptr;
6028 }
6029}
6030
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006031
6032Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
6033 SubstNonTypeTemplateParmExpr *E) {
6034 QualType T = Importer.Import(E->getType());
6035 if (T.isNull())
6036 return nullptr;
6037
6038 NonTypeTemplateParmDecl *Param = cast_or_null<NonTypeTemplateParmDecl>(
6039 Importer.Import(E->getParameter()));
6040 if (!Param)
6041 return nullptr;
6042
6043 Expr *Replacement = Importer.Import(E->getReplacement());
6044 if (!Replacement)
6045 return nullptr;
6046
6047 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
6048 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
6049 Replacement);
6050}
6051
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00006052Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
6053 QualType ToType = Importer.Import(E->getType());
6054 if (ToType.isNull())
6055 return nullptr;
6056
6057 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
6058 if (ImportContainerChecked(E->getArgs(), ToArgs))
6059 return nullptr;
6060
6061 // According to Sema::BuildTypeTrait(), if E is value-dependent,
6062 // Value is always false.
6063 bool ToValue = false;
6064 if (!E->isValueDependent())
6065 ToValue = E->getValue();
6066
6067 return TypeTraitExpr::Create(
6068 Importer.getToContext(), ToType, Importer.Import(E->getLocStart()),
6069 E->getTrait(), ToArgs, Importer.Import(E->getLocEnd()), ToValue);
6070}
6071
Lang Hames19e07e12017-06-20 21:06:00 +00006072void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
6073 CXXMethodDecl *FromMethod) {
6074 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
6075 ToMethod->addOverriddenMethod(
6076 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
6077 FromOverriddenMethod))));
6078}
6079
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006080ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006081 ASTContext &FromContext, FileManager &FromFileManager,
6082 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00006083 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00006084 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006085 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00006086{
Douglas Gregor62d311f2010-02-09 19:21:46 +00006087 ImportedDecls[FromContext.getTranslationUnitDecl()]
6088 = ToContext.getTranslationUnitDecl();
6089}
6090
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00006091ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00006092
6093QualType ASTImporter::Import(QualType FromT) {
6094 if (FromT.isNull())
6095 return QualType();
John McCall424cec92011-01-19 06:33:43 +00006096
6097 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00006098
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006099 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006100 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6101 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006102 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006103 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006104
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006105 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006106 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006107 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006108 if (ToT.isNull())
6109 return ToT;
6110
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006111 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006112 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006113
John McCall424cec92011-01-19 06:33:43 +00006114 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006115}
6116
Douglas Gregor62d311f2010-02-09 19:21:46 +00006117TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006118 if (!FromTSI)
6119 return FromTSI;
6120
6121 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006122 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006123 QualType T = Import(FromTSI->getType());
6124 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006125 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006126
6127 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006128 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006129}
6130
Sean Callanan59721b32015-04-28 18:41:46 +00006131Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6132 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6133 if (Pos != ImportedDecls.end()) {
6134 Decl *ToD = Pos->second;
6135 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6136 return ToD;
6137 } else {
6138 return nullptr;
6139 }
6140}
6141
Douglas Gregor62d311f2010-02-09 19:21:46 +00006142Decl *ASTImporter::Import(Decl *FromD) {
6143 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006144 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006145
Douglas Gregord451ea92011-07-29 23:31:30 +00006146 ASTNodeImporter Importer(*this);
6147
Douglas Gregor62d311f2010-02-09 19:21:46 +00006148 // Check whether we've already imported this declaration.
6149 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006150 if (Pos != ImportedDecls.end()) {
6151 Decl *ToD = Pos->second;
6152 Importer.ImportDefinitionIfNeeded(FromD, ToD);
6153 return ToD;
6154 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00006155
6156 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00006157 Decl *ToD = Importer.Visit(FromD);
6158 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006159 return nullptr;
6160
Douglas Gregor62d311f2010-02-09 19:21:46 +00006161 // Record the imported declaration.
6162 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00006163
6164 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
6165 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00006166 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00006167 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00006168 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006169 // When we've finished transforming a typedef, see whether it was the
6170 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00006171 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00006172 FromTag = AnonTagsWithPendingTypedefs.begin(),
6173 FromTagEnd = AnonTagsWithPendingTypedefs.end();
6174 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00006175 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006176 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
6177 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00006178 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00006179 AnonTagsWithPendingTypedefs.erase(FromTag);
6180 break;
6181 }
6182 }
6183 }
6184 }
6185
Douglas Gregor62d311f2010-02-09 19:21:46 +00006186 return ToD;
6187}
6188
6189DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6190 if (!FromDC)
6191 return FromDC;
6192
Douglas Gregor95d82832012-01-24 18:36:04 +00006193 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006194 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006195 return nullptr;
6196
Douglas Gregor2e15c842012-02-01 21:00:38 +00006197 // When we're using a record/enum/Objective-C class/protocol as a context, we
6198 // need it to have a definition.
6199 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00006200 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006201 if (ToRecord->isCompleteDefinition()) {
6202 // Do nothing.
6203 } else if (FromRecord->isCompleteDefinition()) {
6204 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6205 ASTNodeImporter::IDK_Basic);
6206 } else {
6207 CompleteDecl(ToRecord);
6208 }
6209 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6210 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
6211 if (ToEnum->isCompleteDefinition()) {
6212 // Do nothing.
6213 } else if (FromEnum->isCompleteDefinition()) {
6214 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6215 ASTNodeImporter::IDK_Basic);
6216 } else {
6217 CompleteDecl(ToEnum);
6218 }
6219 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6220 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
6221 if (ToClass->getDefinition()) {
6222 // Do nothing.
6223 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6224 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6225 ASTNodeImporter::IDK_Basic);
6226 } else {
6227 CompleteDecl(ToClass);
6228 }
6229 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6230 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
6231 if (ToProto->getDefinition()) {
6232 // Do nothing.
6233 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6234 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6235 ASTNodeImporter::IDK_Basic);
6236 } else {
6237 CompleteDecl(ToProto);
6238 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006239 }
6240
6241 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006242}
6243
6244Expr *ASTImporter::Import(Expr *FromE) {
6245 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00006246 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006247
6248 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
6249}
6250
6251Stmt *ASTImporter::Import(Stmt *FromS) {
6252 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00006253 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006254
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006255 // Check whether we've already imported this declaration.
6256 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
6257 if (Pos != ImportedStmts.end())
6258 return Pos->second;
6259
6260 // Import the type
6261 ASTNodeImporter Importer(*this);
6262 Stmt *ToS = Importer.Visit(FromS);
6263 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00006264 return nullptr;
6265
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006266 // Record the imported declaration.
6267 ImportedStmts[FromS] = ToS;
6268 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006269}
6270
6271NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
6272 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00006273 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006274
Douglas Gregor90ebf252011-04-27 16:48:40 +00006275 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
6276
6277 switch (FromNNS->getKind()) {
6278 case NestedNameSpecifier::Identifier:
6279 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
6280 return NestedNameSpecifier::Create(ToContext, prefix, II);
6281 }
Craig Topper36250ad2014-05-12 05:36:57 +00006282 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006283
6284 case NestedNameSpecifier::Namespace:
6285 if (NamespaceDecl *NS =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006286 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006287 return NestedNameSpecifier::Create(ToContext, prefix, NS);
6288 }
Craig Topper36250ad2014-05-12 05:36:57 +00006289 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006290
6291 case NestedNameSpecifier::NamespaceAlias:
6292 if (NamespaceAliasDecl *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006293 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006294 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
6295 }
Craig Topper36250ad2014-05-12 05:36:57 +00006296 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006297
6298 case NestedNameSpecifier::Global:
6299 return NestedNameSpecifier::GlobalSpecifier(ToContext);
6300
Nikola Smiljanic67860242014-09-26 00:28:20 +00006301 case NestedNameSpecifier::Super:
6302 if (CXXRecordDecl *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006303 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00006304 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
6305 }
6306 return nullptr;
6307
Douglas Gregor90ebf252011-04-27 16:48:40 +00006308 case NestedNameSpecifier::TypeSpec:
6309 case NestedNameSpecifier::TypeSpecWithTemplate: {
6310 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
6311 if (!T.isNull()) {
6312 bool bTemplate = FromNNS->getKind() ==
6313 NestedNameSpecifier::TypeSpecWithTemplate;
6314 return NestedNameSpecifier::Create(ToContext, prefix,
6315 bTemplate, T.getTypePtr());
6316 }
6317 }
Craig Topper36250ad2014-05-12 05:36:57 +00006318 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006319 }
6320
6321 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00006322}
6323
Douglas Gregor14454802011-02-25 02:25:35 +00006324NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006325 // Copied from NestedNameSpecifier mostly.
6326 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
6327 NestedNameSpecifierLoc NNS = FromNNS;
6328
6329 // Push each of the nested-name-specifiers's onto a stack for
6330 // serialization in reverse order.
6331 while (NNS) {
6332 NestedNames.push_back(NNS);
6333 NNS = NNS.getPrefix();
6334 }
6335
6336 NestedNameSpecifierLocBuilder Builder;
6337
6338 while (!NestedNames.empty()) {
6339 NNS = NestedNames.pop_back_val();
6340 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
6341 if (!Spec)
6342 return NestedNameSpecifierLoc();
6343
6344 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
6345 switch (Kind) {
6346 case NestedNameSpecifier::Identifier:
6347 Builder.Extend(getToContext(),
6348 Spec->getAsIdentifier(),
6349 Import(NNS.getLocalBeginLoc()),
6350 Import(NNS.getLocalEndLoc()));
6351 break;
6352
6353 case NestedNameSpecifier::Namespace:
6354 Builder.Extend(getToContext(),
6355 Spec->getAsNamespace(),
6356 Import(NNS.getLocalBeginLoc()),
6357 Import(NNS.getLocalEndLoc()));
6358 break;
6359
6360 case NestedNameSpecifier::NamespaceAlias:
6361 Builder.Extend(getToContext(),
6362 Spec->getAsNamespaceAlias(),
6363 Import(NNS.getLocalBeginLoc()),
6364 Import(NNS.getLocalEndLoc()));
6365 break;
6366
6367 case NestedNameSpecifier::TypeSpec:
6368 case NestedNameSpecifier::TypeSpecWithTemplate: {
6369 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
6370 QualType(Spec->getAsType(), 0));
6371 Builder.Extend(getToContext(),
6372 Import(NNS.getLocalBeginLoc()),
6373 TSI->getTypeLoc(),
6374 Import(NNS.getLocalEndLoc()));
6375 break;
6376 }
6377
6378 case NestedNameSpecifier::Global:
6379 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
6380 break;
6381
6382 case NestedNameSpecifier::Super: {
6383 SourceRange ToRange = Import(NNS.getSourceRange());
6384 Builder.MakeSuper(getToContext(),
6385 Spec->getAsRecordDecl(),
6386 ToRange.getBegin(),
6387 ToRange.getEnd());
6388 }
6389 }
6390 }
6391
6392 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00006393}
6394
Douglas Gregore2e50d332010-12-01 01:36:18 +00006395TemplateName ASTImporter::Import(TemplateName From) {
6396 switch (From.getKind()) {
6397 case TemplateName::Template:
6398 if (TemplateDecl *ToTemplate
6399 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6400 return TemplateName(ToTemplate);
6401
6402 return TemplateName();
6403
6404 case TemplateName::OverloadedTemplate: {
6405 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
6406 UnresolvedSet<2> ToTemplates;
6407 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
6408 E = FromStorage->end();
6409 I != E; ++I) {
6410 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
6411 ToTemplates.addDecl(To);
6412 else
6413 return TemplateName();
6414 }
6415 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
6416 ToTemplates.end());
6417 }
6418
6419 case TemplateName::QualifiedTemplate: {
6420 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
6421 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
6422 if (!Qualifier)
6423 return TemplateName();
6424
6425 if (TemplateDecl *ToTemplate
6426 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6427 return ToContext.getQualifiedTemplateName(Qualifier,
6428 QTN->hasTemplateKeyword(),
6429 ToTemplate);
6430
6431 return TemplateName();
6432 }
6433
6434 case TemplateName::DependentTemplate: {
6435 DependentTemplateName *DTN = From.getAsDependentTemplateName();
6436 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
6437 if (!Qualifier)
6438 return TemplateName();
6439
6440 if (DTN->isIdentifier()) {
6441 return ToContext.getDependentTemplateName(Qualifier,
6442 Import(DTN->getIdentifier()));
6443 }
6444
6445 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
6446 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006447
6448 case TemplateName::SubstTemplateTemplateParm: {
6449 SubstTemplateTemplateParmStorage *subst
6450 = From.getAsSubstTemplateTemplateParm();
6451 TemplateTemplateParmDecl *param
6452 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
6453 if (!param)
6454 return TemplateName();
6455
6456 TemplateName replacement = Import(subst->getReplacement());
6457 if (replacement.isNull()) return TemplateName();
6458
6459 return ToContext.getSubstTemplateTemplateParm(param, replacement);
6460 }
Douglas Gregor5590be02011-01-15 06:45:20 +00006461
6462 case TemplateName::SubstTemplateTemplateParmPack: {
6463 SubstTemplateTemplateParmPackStorage *SubstPack
6464 = From.getAsSubstTemplateTemplateParmPack();
6465 TemplateTemplateParmDecl *Param
6466 = cast_or_null<TemplateTemplateParmDecl>(
6467 Import(SubstPack->getParameterPack()));
6468 if (!Param)
6469 return TemplateName();
6470
6471 ASTNodeImporter Importer(*this);
6472 TemplateArgument ArgPack
6473 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
6474 if (ArgPack.isNull())
6475 return TemplateName();
6476
6477 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
6478 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00006479 }
6480
6481 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00006482}
6483
Douglas Gregor62d311f2010-02-09 19:21:46 +00006484SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
6485 if (FromLoc.isInvalid())
6486 return SourceLocation();
6487
Douglas Gregor811663e2010-02-10 00:15:17 +00006488 SourceManager &FromSM = FromContext.getSourceManager();
6489
Sean Callanan24c5fe62016-11-07 20:42:25 +00006490 // For now, map everything down to its file location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00006491 // don't have to import macro expansions.
6492 // FIXME: Import macro expansions!
Sean Callanan24c5fe62016-11-07 20:42:25 +00006493 FromLoc = FromSM.getFileLoc(FromLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00006494 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
6495 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00006496 FileID ToFileID = Import(Decomposed.first);
6497 if (ToFileID.isInvalid())
6498 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00006499 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
6500 .getLocWithOffset(Decomposed.second);
6501 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006502}
6503
6504SourceRange ASTImporter::Import(SourceRange FromRange) {
6505 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
6506}
6507
Douglas Gregor811663e2010-02-10 00:15:17 +00006508FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00006509 llvm::DenseMap<FileID, FileID>::iterator Pos
6510 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00006511 if (Pos != ImportedFileIDs.end())
6512 return Pos->second;
6513
6514 SourceManager &FromSM = FromContext.getSourceManager();
6515 SourceManager &ToSM = ToContext.getSourceManager();
6516 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00006517 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00006518
6519 // Include location of this file.
6520 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
6521
6522 // Map the FileID for to the "to" source manager.
6523 FileID ToID;
6524 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00006525 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00006526 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
6527 // disk again
6528 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
6529 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00006530 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00006531 if (!Entry)
6532 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00006533 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
6534 FromSLoc.getFile().getFileCharacteristic());
6535 } else {
6536 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006537 const llvm::MemoryBuffer *
6538 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006539 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00006540 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00006541 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00006542 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006543 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00006544 }
6545
6546
Sebastian Redl99219f12010-09-30 01:03:06 +00006547 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00006548 return ToID;
6549}
6550
Sean Callanandd2c1742016-05-16 20:48:03 +00006551CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
6552 Expr *ToExpr = Import(From->getInit());
6553 if (!ToExpr && From->getInit())
6554 return nullptr;
6555
6556 if (From->isBaseInitializer()) {
6557 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6558 if (!ToTInfo && From->getTypeSourceInfo())
6559 return nullptr;
6560
6561 return new (ToContext) CXXCtorInitializer(
6562 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
6563 ToExpr, Import(From->getRParenLoc()),
6564 From->isPackExpansion() ? Import(From->getEllipsisLoc())
6565 : SourceLocation());
6566 } else if (From->isMemberInitializer()) {
6567 FieldDecl *ToField =
6568 llvm::cast_or_null<FieldDecl>(Import(From->getMember()));
6569 if (!ToField && From->getMember())
6570 return nullptr;
6571
6572 return new (ToContext) CXXCtorInitializer(
6573 ToContext, ToField, Import(From->getMemberLocation()),
6574 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6575 } else if (From->isIndirectMemberInitializer()) {
6576 IndirectFieldDecl *ToIField = llvm::cast_or_null<IndirectFieldDecl>(
6577 Import(From->getIndirectMember()));
6578 if (!ToIField && From->getIndirectMember())
6579 return nullptr;
6580
6581 return new (ToContext) CXXCtorInitializer(
6582 ToContext, ToIField, Import(From->getMemberLocation()),
6583 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6584 } else if (From->isDelegatingInitializer()) {
6585 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6586 if (!ToTInfo && From->getTypeSourceInfo())
6587 return nullptr;
6588
6589 return new (ToContext)
6590 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
6591 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006592 } else {
6593 return nullptr;
6594 }
6595}
6596
6597
Aleksei Sidorina693b372016-09-28 10:16:56 +00006598CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
6599 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
6600 if (Pos != ImportedCXXBaseSpecifiers.end())
6601 return Pos->second;
6602
6603 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
6604 Import(BaseSpec->getSourceRange()),
6605 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
6606 BaseSpec->getAccessSpecifierAsWritten(),
6607 Import(BaseSpec->getTypeSourceInfo()),
6608 Import(BaseSpec->getEllipsisLoc()));
6609 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
6610 return Imported;
6611}
6612
Douglas Gregor0a791672011-01-18 03:11:38 +00006613void ASTImporter::ImportDefinition(Decl *From) {
6614 Decl *To = Import(From);
6615 if (!To)
6616 return;
6617
6618 if (DeclContext *FromDC = cast<DeclContext>(From)) {
6619 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006620
6621 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
6622 if (!ToRecord->getDefinition()) {
6623 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00006624 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006625 return;
6626 }
6627 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006628
6629 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
6630 if (!ToEnum->getDefinition()) {
6631 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006632 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00006633 return;
6634 }
6635 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00006636
6637 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
6638 if (!ToIFace->getDefinition()) {
6639 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006640 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006641 return;
6642 }
6643 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006644
Douglas Gregor2aa53772012-01-24 17:42:07 +00006645 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
6646 if (!ToProto->getDefinition()) {
6647 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006648 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006649 return;
6650 }
6651 }
6652
Douglas Gregor0a791672011-01-18 03:11:38 +00006653 Importer.ImportDeclContext(FromDC, true);
6654 }
6655}
6656
Douglas Gregor96e578d2010-02-05 17:54:41 +00006657DeclarationName ASTImporter::Import(DeclarationName FromName) {
6658 if (!FromName)
6659 return DeclarationName();
6660
6661 switch (FromName.getNameKind()) {
6662 case DeclarationName::Identifier:
6663 return Import(FromName.getAsIdentifierInfo());
6664
6665 case DeclarationName::ObjCZeroArgSelector:
6666 case DeclarationName::ObjCOneArgSelector:
6667 case DeclarationName::ObjCMultiArgSelector:
6668 return Import(FromName.getObjCSelector());
6669
6670 case DeclarationName::CXXConstructorName: {
6671 QualType T = Import(FromName.getCXXNameType());
6672 if (T.isNull())
6673 return DeclarationName();
6674
6675 return ToContext.DeclarationNames.getCXXConstructorName(
6676 ToContext.getCanonicalType(T));
6677 }
6678
6679 case DeclarationName::CXXDestructorName: {
6680 QualType T = Import(FromName.getCXXNameType());
6681 if (T.isNull())
6682 return DeclarationName();
6683
6684 return ToContext.DeclarationNames.getCXXDestructorName(
6685 ToContext.getCanonicalType(T));
6686 }
6687
Richard Smith35845152017-02-07 01:37:30 +00006688 case DeclarationName::CXXDeductionGuideName: {
6689 TemplateDecl *Template = cast_or_null<TemplateDecl>(
6690 Import(FromName.getCXXDeductionGuideTemplate()));
6691 if (!Template)
6692 return DeclarationName();
6693 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
6694 }
6695
Douglas Gregor96e578d2010-02-05 17:54:41 +00006696 case DeclarationName::CXXConversionFunctionName: {
6697 QualType T = Import(FromName.getCXXNameType());
6698 if (T.isNull())
6699 return DeclarationName();
6700
6701 return ToContext.DeclarationNames.getCXXConversionFunctionName(
6702 ToContext.getCanonicalType(T));
6703 }
6704
6705 case DeclarationName::CXXOperatorName:
6706 return ToContext.DeclarationNames.getCXXOperatorName(
6707 FromName.getCXXOverloadedOperator());
6708
6709 case DeclarationName::CXXLiteralOperatorName:
6710 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
6711 Import(FromName.getCXXLiteralIdentifier()));
6712
6713 case DeclarationName::CXXUsingDirective:
6714 // FIXME: STATICS!
6715 return DeclarationName::getUsingDirectiveName();
6716 }
6717
David Blaikiee4d798f2012-01-20 21:50:17 +00006718 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00006719}
6720
Douglas Gregore2e50d332010-12-01 01:36:18 +00006721IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00006722 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00006723 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006724
Sean Callananf94ef1d2016-05-14 06:11:19 +00006725 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
6726
6727 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
6728 ToId->setBuiltinID(FromId->getBuiltinID());
6729
6730 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006731}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006732
Douglas Gregor43f54792010-02-17 02:12:47 +00006733Selector ASTImporter::Import(Selector FromSel) {
6734 if (FromSel.isNull())
6735 return Selector();
6736
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006737 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00006738 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
6739 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
6740 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
6741 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
6742}
6743
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006744DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
6745 DeclContext *DC,
6746 unsigned IDNS,
6747 NamedDecl **Decls,
6748 unsigned NumDecls) {
6749 return Name;
6750}
6751
6752DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006753 if (LastDiagFromFrom)
6754 ToContext.getDiagnostics().notePriorDiagnosticFrom(
6755 FromContext.getDiagnostics());
6756 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006757 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006758}
6759
6760DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006761 if (!LastDiagFromFrom)
6762 FromContext.getDiagnostics().notePriorDiagnosticFrom(
6763 ToContext.getDiagnostics());
6764 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006765 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006766}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006767
Douglas Gregor2e15c842012-02-01 21:00:38 +00006768void ASTImporter::CompleteDecl (Decl *D) {
6769 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
6770 if (!ID->getDefinition())
6771 ID->startDefinition();
6772 }
6773 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
6774 if (!PD->getDefinition())
6775 PD->startDefinition();
6776 }
6777 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
6778 if (!TD->getDefinition() && !TD->isBeingDefined()) {
6779 TD->startDefinition();
6780 TD->setCompleteDefinition(true);
6781 }
6782 }
6783 else {
6784 assert (0 && "CompleteDecl called on a Decl that can't be completed");
6785 }
6786}
6787
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006788Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00006789 if (From->hasAttrs()) {
6790 for (Attr *FromAttr : From->getAttrs())
6791 To->addAttr(FromAttr->clone(To->getASTContext()));
6792 }
6793 if (From->isUsed()) {
6794 To->setIsUsed();
6795 }
Sean Callanandd2c1742016-05-16 20:48:03 +00006796 if (From->isImplicit()) {
6797 To->setImplicit();
6798 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006799 ImportedDecls[From] = To;
6800 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00006801}
Douglas Gregorb4964f72010-02-15 23:54:17 +00006802
Douglas Gregordd6006f2012-07-17 21:16:27 +00006803bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
6804 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00006805 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00006806 = ImportedTypes.find(From.getTypePtr());
6807 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
6808 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00006809
Douglas Gregordd6006f2012-07-17 21:16:27 +00006810 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
6811 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00006812 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00006813}