blob: 0753f265577bc50002e88a2c798bcd8542b4fe3c [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 Gregor96e578d2010-02-05 17:54:41 +000026
Douglas Gregor3c2404b2011-11-03 18:07:07 +000027namespace clang {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000028 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000029 public DeclVisitor<ASTNodeImporter, Decl *>,
30 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000031 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000032
Douglas Gregor96e578d2010-02-05 17:54:41 +000033 public:
34 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
35
36 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000037 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000038 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000039
40 // Importing types
John McCall424cec92011-01-19 06:33:43 +000041 QualType VisitType(const Type *T);
Gabor Horvath0866c2f2016-11-23 15:24:23 +000042 QualType VisitAtomicType(const AtomicType *T);
John McCall424cec92011-01-19 06:33:43 +000043 QualType VisitBuiltinType(const BuiltinType *T);
Aleksei Sidorina693b372016-09-28 10:16:56 +000044 QualType VisitDecayedType(const DecayedType *T);
John McCall424cec92011-01-19 06:33:43 +000045 QualType VisitComplexType(const ComplexType *T);
46 QualType VisitPointerType(const PointerType *T);
47 QualType VisitBlockPointerType(const BlockPointerType *T);
48 QualType VisitLValueReferenceType(const LValueReferenceType *T);
49 QualType VisitRValueReferenceType(const RValueReferenceType *T);
50 QualType VisitMemberPointerType(const MemberPointerType *T);
51 QualType VisitConstantArrayType(const ConstantArrayType *T);
52 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
53 QualType VisitVariableArrayType(const VariableArrayType *T);
Gabor Horvathc78d99a2018-01-27 16:11:45 +000054 QualType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000055 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +000056 QualType VisitVectorType(const VectorType *T);
57 QualType VisitExtVectorType(const ExtVectorType *T);
58 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
59 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +000060 QualType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
Sean Callananda6df8a2011-08-11 16:56:07 +000061 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +000062 QualType VisitTypedefType(const TypedefType *T);
63 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000064 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +000065 QualType VisitTypeOfType(const TypeOfType *T);
66 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +000067 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +000068 QualType VisitAutoType(const AutoType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000069 QualType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000070 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +000071 QualType VisitRecordType(const RecordType *T);
72 QualType VisitEnumType(const EnumType *T);
Sean Callanan72fe0852015-04-02 23:50:08 +000073 QualType VisitAttributedType(const AttributedType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000074 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
Aleksei Sidorin855086d2017-01-23 09:30:36 +000075 QualType VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T);
John McCall424cec92011-01-19 06:33:43 +000076 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
77 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000078 // FIXME: DependentNameType
Gabor Horvath7a91c082017-11-14 11:30:38 +000079 QualType VisitPackExpansionType(const PackExpansionType *T);
Gabor Horvathc78d99a2018-01-27 16:11:45 +000080 QualType VisitDependentTemplateSpecializationType(
81 const DependentTemplateSpecializationType *T);
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
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000100 Optional<LambdaCapture> ImportLambdaCapture(const LambdaCapture &From);
101
Douglas Gregor2e15c842012-02-01 21:00:38 +0000102
Douglas Gregor95d82832012-01-24 18:36:04 +0000103 /// \brief What we should import from the definition.
104 enum ImportDefinitionKind {
105 /// \brief Import the default subset of the definition, which might be
106 /// nothing (if minimal import is set) or might be everything (if minimal
107 /// import is not set).
108 IDK_Default,
109 /// \brief Import everything.
110 IDK_Everything,
111 /// \brief Import only the bare bones needed to establish a valid
112 /// DeclContext.
113 IDK_Basic
114 };
115
Douglas Gregor2e15c842012-02-01 21:00:38 +0000116 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
117 return IDK == IDK_Everything ||
118 (IDK == IDK_Default && !Importer.isMinimalImport());
119 }
120
Douglas Gregord451ea92011-07-29 23:31:30 +0000121 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000122 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000123 bool ImportDefinition(VarDecl *From, VarDecl *To,
124 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000125 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000126 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000127 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000128 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000129 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000130 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000131 TemplateParameterList *ImportTemplateParameterList(
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000132 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000133 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000134 Optional<TemplateArgumentLoc> ImportTemplateArgumentLoc(
135 const TemplateArgumentLoc &TALoc);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000136 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
137 unsigned NumFromArgs,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000138 SmallVectorImpl<TemplateArgument> &ToArgs);
139
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000140 template <typename InContainerTy>
141 bool ImportTemplateArgumentListInfo(const InContainerTy &Container,
142 TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000143
144 template<typename InContainerTy>
145 bool ImportTemplateArgumentListInfo(SourceLocation FromLAngleLoc,
146 SourceLocation FromRAngleLoc,
147 const InContainerTy &Container,
148 TemplateArgumentListInfo &Result);
149
150 bool ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
151
Douglas Gregordd6006f2012-07-17 21:16:27 +0000152 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
153 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000154 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
155 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000156 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000157 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000158 bool IsStructuralMatch(FunctionTemplateDecl *From,
159 FunctionTemplateDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000160 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000161 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000162 Decl *VisitDecl(Decl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000163 Decl *VisitEmptyDecl(EmptyDecl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000164 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000165 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000166 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000167 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000168 Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000169 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000170 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000171 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000172 Decl *VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000173 Decl *VisitLabelDecl(LabelDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000174 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000175 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000176 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000177 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000178 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
179 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
180 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
181 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000182 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000183 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000184 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000185 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000186 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000187 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000188 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000189 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000190 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000191 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000192 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000193 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000194 Decl *VisitUsingDecl(UsingDecl *D);
195 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
196 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
197 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
198 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
199
Douglas Gregor85f3f952015-07-07 03:57:15 +0000200
201 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000202 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000203 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000204 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000205 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000206 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000207 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
208 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
209 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
210 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000211 Decl *VisitClassTemplateSpecializationDecl(
212 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000213 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
214 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000215 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000216
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000217 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000218 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
219
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000220 Stmt *VisitStmt(Stmt *S);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000221 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000222 Stmt *VisitDeclStmt(DeclStmt *S);
223 Stmt *VisitNullStmt(NullStmt *S);
224 Stmt *VisitCompoundStmt(CompoundStmt *S);
225 Stmt *VisitCaseStmt(CaseStmt *S);
226 Stmt *VisitDefaultStmt(DefaultStmt *S);
227 Stmt *VisitLabelStmt(LabelStmt *S);
228 Stmt *VisitAttributedStmt(AttributedStmt *S);
229 Stmt *VisitIfStmt(IfStmt *S);
230 Stmt *VisitSwitchStmt(SwitchStmt *S);
231 Stmt *VisitWhileStmt(WhileStmt *S);
232 Stmt *VisitDoStmt(DoStmt *S);
233 Stmt *VisitForStmt(ForStmt *S);
234 Stmt *VisitGotoStmt(GotoStmt *S);
235 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
236 Stmt *VisitContinueStmt(ContinueStmt *S);
237 Stmt *VisitBreakStmt(BreakStmt *S);
238 Stmt *VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000239 // FIXME: MSAsmStmt
240 // FIXME: SEHExceptStmt
241 // FIXME: SEHFinallyStmt
242 // FIXME: SEHTryStmt
243 // FIXME: SEHLeaveStmt
244 // FIXME: CapturedStmt
245 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
246 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
247 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
248 // FIXME: MSDependentExistsStmt
249 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
250 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
251 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
252 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
253 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
254 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
255 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000256
257 // Importing expressions
258 Expr *VisitExpr(Expr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000259 Expr *VisitVAArgExpr(VAArgExpr *E);
260 Expr *VisitGNUNullExpr(GNUNullExpr *E);
261 Expr *VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000262 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000263 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
264 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
265 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000266 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000267 Expr *VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000268 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000269 Expr *VisitStringLiteral(StringLiteral *E);
270 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
271 Expr *VisitAtomicExpr(AtomicExpr *E);
272 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000273 Expr *VisitParenExpr(ParenExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000274 Expr *VisitParenListExpr(ParenListExpr *E);
275 Expr *VisitStmtExpr(StmtExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000276 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000277 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000278 Expr *VisitBinaryOperator(BinaryOperator *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000279 Expr *VisitConditionalOperator(ConditionalOperator *E);
280 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
281 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000282 Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
283 Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E);
284 Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000285 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000286 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000287 Expr *VisitExplicitCastExpr(ExplicitCastExpr *E);
288 Expr *VisitOffsetOfExpr(OffsetOfExpr *OE);
289 Expr *VisitCXXThrowExpr(CXXThrowExpr *E);
290 Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
291 Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
292 Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
293 Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
294 Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE);
295 Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000296 Expr *VisitPackExpansionExpr(PackExpansionExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000297 Expr *VisitSizeOfPackExpr(SizeOfPackExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000298 Expr *VisitCXXNewExpr(CXXNewExpr *CE);
299 Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000300 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000301 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000302 Expr *VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +0000303 Expr *VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *CE);
304 Expr *VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000305 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC);
Sean Callanan8bca9962016-03-28 21:43:01 +0000306 Expr *VisitCXXThisExpr(CXXThisExpr *E);
307 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +0000308 Expr *VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000309 Expr *VisitMemberExpr(MemberExpr *E);
310 Expr *VisitCallExpr(CallExpr *E);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000311 Expr *VisitLambdaExpr(LambdaExpr *LE);
Sean Callanan8bca9962016-03-28 21:43:01 +0000312 Expr *VisitInitListExpr(InitListExpr *E);
Richard Smith30e304e2016-12-14 00:03:17 +0000313 Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
314 Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
Sean Callanandd2c1742016-05-16 20:48:03 +0000315 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
316 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000317 Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +0000318 Expr *VisitTypeTraitExpr(TypeTraitExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000319 Expr *VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000320
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000321
322 template<typename IIter, typename OIter>
323 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
324 typedef typename std::remove_reference<decltype(*Obegin)>::type ItemT;
325 ASTImporter &ImporterRef = Importer;
326 std::transform(Ibegin, Iend, Obegin,
327 [&ImporterRef](ItemT From) -> ItemT {
328 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000329 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000330 }
331
332 template<typename IIter, typename OIter>
333 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
334 typedef typename std::remove_reference<decltype(**Obegin)>::type ItemT;
335 ASTImporter &ImporterRef = Importer;
336 bool Failed = false;
337 std::transform(Ibegin, Iend, Obegin,
338 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000339 ItemT *To = cast_or_null<ItemT>(
340 ImporterRef.Import(From));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000341 if (!To && From)
342 Failed = true;
343 return To;
344 });
345 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000346 }
Aleksei Sidorina693b372016-09-28 10:16:56 +0000347
348 template<typename InContainerTy, typename OutContainerTy>
349 bool ImportContainerChecked(const InContainerTy &InContainer,
350 OutContainerTy &OutContainer) {
351 return ImportArrayChecked(InContainer.begin(), InContainer.end(),
352 OutContainer.begin());
353 }
354
355 template<typename InContainerTy, typename OIter>
356 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
357 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
358 }
Lang Hames19e07e12017-06-20 21:06:00 +0000359
360 // Importing overrides.
361 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000362 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000363}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000364
Douglas Gregor3996e242010-02-15 22:01:00 +0000365//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000366// Import Types
367//----------------------------------------------------------------------------
368
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000369using namespace clang;
370
John McCall424cec92011-01-19 06:33:43 +0000371QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000372 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
373 << T->getTypeClassName();
374 return QualType();
375}
376
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000377QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
378 QualType UnderlyingType = Importer.Import(T->getValueType());
379 if(UnderlyingType.isNull())
380 return QualType();
381
382 return Importer.getToContext().getAtomicType(UnderlyingType);
383}
384
John McCall424cec92011-01-19 06:33:43 +0000385QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000386 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000387#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
388 case BuiltinType::Id: \
389 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000390#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000391#define SHARED_SINGLETON_TYPE(Expansion)
392#define BUILTIN_TYPE(Id, SingletonId) \
393 case BuiltinType::Id: return Importer.getToContext().SingletonId;
394#include "clang/AST/BuiltinTypes.def"
395
396 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
397 // context supports C++.
398
399 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
400 // context supports ObjC.
401
Douglas Gregor96e578d2010-02-05 17:54:41 +0000402 case BuiltinType::Char_U:
403 // The context we're importing from has an unsigned 'char'. If we're
404 // importing into a context with a signed 'char', translate to
405 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000406 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000407 return Importer.getToContext().UnsignedCharTy;
408
409 return Importer.getToContext().CharTy;
410
Douglas Gregor96e578d2010-02-05 17:54:41 +0000411 case BuiltinType::Char_S:
412 // The context we're importing from has an unsigned 'char'. If we're
413 // importing into a context with a signed 'char', translate to
414 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000415 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000416 return Importer.getToContext().SignedCharTy;
417
418 return Importer.getToContext().CharTy;
419
Chris Lattnerad3467e2010-12-25 23:25:43 +0000420 case BuiltinType::WChar_S:
421 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000422 // FIXME: If not in C++, shall we translate to the C equivalent of
423 // wchar_t?
424 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000425 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000426
427 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000428}
429
Aleksei Sidorina693b372016-09-28 10:16:56 +0000430QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
431 QualType OrigT = Importer.Import(T->getOriginalType());
432 if (OrigT.isNull())
433 return QualType();
434
435 return Importer.getToContext().getDecayedType(OrigT);
436}
437
John McCall424cec92011-01-19 06:33:43 +0000438QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000439 QualType ToElementType = Importer.Import(T->getElementType());
440 if (ToElementType.isNull())
441 return QualType();
442
443 return Importer.getToContext().getComplexType(ToElementType);
444}
445
John McCall424cec92011-01-19 06:33:43 +0000446QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000447 QualType ToPointeeType = Importer.Import(T->getPointeeType());
448 if (ToPointeeType.isNull())
449 return QualType();
450
451 return Importer.getToContext().getPointerType(ToPointeeType);
452}
453
John McCall424cec92011-01-19 06:33:43 +0000454QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000455 // FIXME: Check for blocks support in "to" context.
456 QualType ToPointeeType = Importer.Import(T->getPointeeType());
457 if (ToPointeeType.isNull())
458 return QualType();
459
460 return Importer.getToContext().getBlockPointerType(ToPointeeType);
461}
462
John McCall424cec92011-01-19 06:33:43 +0000463QualType
464ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000465 // FIXME: Check for C++ support in "to" context.
466 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
467 if (ToPointeeType.isNull())
468 return QualType();
469
470 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
471}
472
John McCall424cec92011-01-19 06:33:43 +0000473QualType
474ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000475 // FIXME: Check for C++0x support in "to" context.
476 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
477 if (ToPointeeType.isNull())
478 return QualType();
479
480 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
481}
482
John McCall424cec92011-01-19 06:33:43 +0000483QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000484 // FIXME: Check for C++ support in "to" context.
485 QualType ToPointeeType = Importer.Import(T->getPointeeType());
486 if (ToPointeeType.isNull())
487 return QualType();
488
489 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
490 return Importer.getToContext().getMemberPointerType(ToPointeeType,
491 ClassType.getTypePtr());
492}
493
John McCall424cec92011-01-19 06:33:43 +0000494QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000495 QualType ToElementType = Importer.Import(T->getElementType());
496 if (ToElementType.isNull())
497 return QualType();
498
499 return Importer.getToContext().getConstantArrayType(ToElementType,
500 T->getSize(),
501 T->getSizeModifier(),
502 T->getIndexTypeCVRQualifiers());
503}
504
John McCall424cec92011-01-19 06:33:43 +0000505QualType
506ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000507 QualType ToElementType = Importer.Import(T->getElementType());
508 if (ToElementType.isNull())
509 return QualType();
510
511 return Importer.getToContext().getIncompleteArrayType(ToElementType,
512 T->getSizeModifier(),
513 T->getIndexTypeCVRQualifiers());
514}
515
John McCall424cec92011-01-19 06:33:43 +0000516QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000517 QualType ToElementType = Importer.Import(T->getElementType());
518 if (ToElementType.isNull())
519 return QualType();
520
521 Expr *Size = Importer.Import(T->getSizeExpr());
522 if (!Size)
523 return QualType();
524
525 SourceRange Brackets = Importer.Import(T->getBracketsRange());
526 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
527 T->getSizeModifier(),
528 T->getIndexTypeCVRQualifiers(),
529 Brackets);
530}
531
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000532QualType ASTNodeImporter::VisitDependentSizedArrayType(
533 const DependentSizedArrayType *T) {
534 QualType ToElementType = Importer.Import(T->getElementType());
535 if (ToElementType.isNull())
536 return QualType();
537
538 // SizeExpr may be null if size is not specified directly.
539 // For example, 'int a[]'.
540 Expr *Size = Importer.Import(T->getSizeExpr());
541 if (!Size && T->getSizeExpr())
542 return QualType();
543
544 SourceRange Brackets = Importer.Import(T->getBracketsRange());
545 return Importer.getToContext().getDependentSizedArrayType(
546 ToElementType, Size, T->getSizeModifier(), T->getIndexTypeCVRQualifiers(),
547 Brackets);
548}
549
John McCall424cec92011-01-19 06:33:43 +0000550QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000551 QualType ToElementType = Importer.Import(T->getElementType());
552 if (ToElementType.isNull())
553 return QualType();
554
555 return Importer.getToContext().getVectorType(ToElementType,
556 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +0000557 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000558}
559
John McCall424cec92011-01-19 06:33:43 +0000560QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000561 QualType ToElementType = Importer.Import(T->getElementType());
562 if (ToElementType.isNull())
563 return QualType();
564
565 return Importer.getToContext().getExtVectorType(ToElementType,
566 T->getNumElements());
567}
568
John McCall424cec92011-01-19 06:33:43 +0000569QualType
570ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000571 // FIXME: What happens if we're importing a function without a prototype
572 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +0000573 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000574 if (ToResultType.isNull())
575 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000576
Douglas Gregor96e578d2010-02-05 17:54:41 +0000577 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000578 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000579}
580
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +0000581QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000582 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000583 if (ToResultType.isNull())
584 return QualType();
585
586 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000587 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000588 for (const auto &A : T->param_types()) {
589 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000590 if (ArgType.isNull())
591 return QualType();
592 ArgTypes.push_back(ArgType);
593 }
594
595 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000596 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000597 for (const auto &E : T->exceptions()) {
598 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000599 if (ExceptionType.isNull())
600 return QualType();
601 ExceptionTypes.push_back(ExceptionType);
602 }
John McCalldb40c7f2010-12-14 08:05:40 +0000603
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000604 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
605 FunctionProtoType::ExtProtoInfo ToEPI;
606
607 ToEPI.ExtInfo = FromEPI.ExtInfo;
608 ToEPI.Variadic = FromEPI.Variadic;
609 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
610 ToEPI.TypeQuals = FromEPI.TypeQuals;
611 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +0000612 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
613 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
614 ToEPI.ExceptionSpec.NoexceptExpr =
615 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
616 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
617 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
618 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
619 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000620
Jordan Rose5c382722013-03-08 21:51:21 +0000621 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000622}
623
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000624QualType ASTNodeImporter::VisitUnresolvedUsingType(
625 const UnresolvedUsingType *T) {
626 UnresolvedUsingTypenameDecl *ToD = cast_or_null<UnresolvedUsingTypenameDecl>(
627 Importer.Import(T->getDecl()));
628 if (!ToD)
629 return QualType();
630
631 UnresolvedUsingTypenameDecl *ToPrevD =
632 cast_or_null<UnresolvedUsingTypenameDecl>(
633 Importer.Import(T->getDecl()->getPreviousDecl()));
634 if (!ToPrevD && T->getDecl()->getPreviousDecl())
635 return QualType();
636
637 return Importer.getToContext().getTypeDeclType(ToD, ToPrevD);
638}
639
Sean Callananda6df8a2011-08-11 16:56:07 +0000640QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
641 QualType ToInnerType = Importer.Import(T->getInnerType());
642 if (ToInnerType.isNull())
643 return QualType();
644
645 return Importer.getToContext().getParenType(ToInnerType);
646}
647
John McCall424cec92011-01-19 06:33:43 +0000648QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +0000649 TypedefNameDecl *ToDecl
650 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000651 if (!ToDecl)
652 return QualType();
653
654 return Importer.getToContext().getTypeDeclType(ToDecl);
655}
656
John McCall424cec92011-01-19 06:33:43 +0000657QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000658 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
659 if (!ToExpr)
660 return QualType();
661
662 return Importer.getToContext().getTypeOfExprType(ToExpr);
663}
664
John McCall424cec92011-01-19 06:33:43 +0000665QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000666 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
667 if (ToUnderlyingType.isNull())
668 return QualType();
669
670 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
671}
672
John McCall424cec92011-01-19 06:33:43 +0000673QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +0000674 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +0000675 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
676 if (!ToExpr)
677 return QualType();
678
Douglas Gregor81495f32012-02-12 18:42:33 +0000679 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
680 if (UnderlyingType.isNull())
681 return QualType();
682
683 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000684}
685
Alexis Hunte852b102011-05-24 22:41:36 +0000686QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
687 QualType ToBaseType = Importer.Import(T->getBaseType());
688 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
689 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
690 return QualType();
691
692 return Importer.getToContext().getUnaryTransformType(ToBaseType,
693 ToUnderlyingType,
694 T->getUTTKind());
695}
696
Richard Smith30482bc2011-02-20 03:19:35 +0000697QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +0000698 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +0000699 QualType FromDeduced = T->getDeducedType();
700 QualType ToDeduced;
701 if (!FromDeduced.isNull()) {
702 ToDeduced = Importer.Import(FromDeduced);
703 if (ToDeduced.isNull())
704 return QualType();
705 }
706
Richard Smithe301ba22015-11-11 02:02:15 +0000707 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +0000708 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +0000709}
710
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000711QualType ASTNodeImporter::VisitInjectedClassNameType(
712 const InjectedClassNameType *T) {
713 CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
714 if (!D)
715 return QualType();
716
717 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
718 if (InjType.isNull())
719 return QualType();
720
721 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
722 // See comments in InjectedClassNameType definition for details
723 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
724 enum {
725 TypeAlignmentInBits = 4,
726 TypeAlignment = 1 << TypeAlignmentInBits
727 };
728
729 return QualType(new (Importer.getToContext(), TypeAlignment)
730 InjectedClassNameType(D, InjType), 0);
731}
732
John McCall424cec92011-01-19 06:33:43 +0000733QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000734 RecordDecl *ToDecl
735 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
736 if (!ToDecl)
737 return QualType();
738
739 return Importer.getToContext().getTagDeclType(ToDecl);
740}
741
John McCall424cec92011-01-19 06:33:43 +0000742QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000743 EnumDecl *ToDecl
744 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
745 if (!ToDecl)
746 return QualType();
747
748 return Importer.getToContext().getTagDeclType(ToDecl);
749}
750
Sean Callanan72fe0852015-04-02 23:50:08 +0000751QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
752 QualType FromModifiedType = T->getModifiedType();
753 QualType FromEquivalentType = T->getEquivalentType();
754 QualType ToModifiedType;
755 QualType ToEquivalentType;
756
757 if (!FromModifiedType.isNull()) {
758 ToModifiedType = Importer.Import(FromModifiedType);
759 if (ToModifiedType.isNull())
760 return QualType();
761 }
762 if (!FromEquivalentType.isNull()) {
763 ToEquivalentType = Importer.Import(FromEquivalentType);
764 if (ToEquivalentType.isNull())
765 return QualType();
766 }
767
768 return Importer.getToContext().getAttributedType(T->getAttrKind(),
769 ToModifiedType, ToEquivalentType);
770}
771
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000772
773QualType ASTNodeImporter::VisitTemplateTypeParmType(
774 const TemplateTypeParmType *T) {
775 TemplateTypeParmDecl *ParmDecl =
776 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
777 if (!ParmDecl && T->getDecl())
778 return QualType();
779
780 return Importer.getToContext().getTemplateTypeParmType(
781 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
782}
783
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000784QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
785 const SubstTemplateTypeParmType *T) {
786 const TemplateTypeParmType *Replaced =
787 cast_or_null<TemplateTypeParmType>(Importer.Import(
788 QualType(T->getReplacedParameter(), 0)).getTypePtr());
789 if (!Replaced)
790 return QualType();
791
792 QualType Replacement = Importer.Import(T->getReplacementType());
793 if (Replacement.isNull())
794 return QualType();
795 Replacement = Replacement.getCanonicalType();
796
797 return Importer.getToContext().getSubstTemplateTypeParmType(
798 Replaced, Replacement);
799}
800
Douglas Gregore2e50d332010-12-01 01:36:18 +0000801QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +0000802 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000803 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
804 if (ToTemplate.isNull())
805 return QualType();
806
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000807 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +0000808 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
809 return QualType();
810
811 QualType ToCanonType;
812 if (!QualType(T, 0).isCanonical()) {
813 QualType FromCanonType
814 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
815 ToCanonType =Importer.Import(FromCanonType);
816 if (ToCanonType.isNull())
817 return QualType();
818 }
819 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +0000820 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +0000821 ToCanonType);
822}
823
John McCall424cec92011-01-19 06:33:43 +0000824QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +0000825 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000826 // Note: the qualifier in an ElaboratedType is optional.
827 if (T->getQualifier()) {
828 ToQualifier = Importer.Import(T->getQualifier());
829 if (!ToQualifier)
830 return QualType();
831 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000832
833 QualType ToNamedType = Importer.Import(T->getNamedType());
834 if (ToNamedType.isNull())
835 return QualType();
836
Abramo Bagnara6150c882010-05-11 21:36:43 +0000837 return Importer.getToContext().getElaboratedType(T->getKeyword(),
838 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000839}
840
Gabor Horvath7a91c082017-11-14 11:30:38 +0000841QualType ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
842 QualType Pattern = Importer.Import(T->getPattern());
843 if (Pattern.isNull())
844 return QualType();
845
846 return Importer.getToContext().getPackExpansionType(Pattern,
847 T->getNumExpansions());
848}
849
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000850QualType ASTNodeImporter::VisitDependentTemplateSpecializationType(
851 const DependentTemplateSpecializationType *T) {
852 NestedNameSpecifier *Qualifier = Importer.Import(T->getQualifier());
853 if (!Qualifier && T->getQualifier())
854 return QualType();
855
856 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
857 if (!Name && T->getIdentifier())
858 return QualType();
859
860 SmallVector<TemplateArgument, 2> ToPack;
861 ToPack.reserve(T->getNumArgs());
862 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToPack))
863 return QualType();
864
865 return Importer.getToContext().getDependentTemplateSpecializationType(
866 T->getKeyword(), Qualifier, Name, ToPack);
867}
868
John McCall424cec92011-01-19 06:33:43 +0000869QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000870 ObjCInterfaceDecl *Class
871 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
872 if (!Class)
873 return QualType();
874
John McCall8b07ec22010-05-15 11:32:37 +0000875 return Importer.getToContext().getObjCInterfaceType(Class);
876}
877
John McCall424cec92011-01-19 06:33:43 +0000878QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000879 QualType ToBaseType = Importer.Import(T->getBaseType());
880 if (ToBaseType.isNull())
881 return QualType();
882
Douglas Gregore9d95f12015-07-07 03:57:35 +0000883 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +0000884 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +0000885 QualType ImportedTypeArg = Importer.Import(TypeArg);
886 if (ImportedTypeArg.isNull())
887 return QualType();
888
889 TypeArgs.push_back(ImportedTypeArg);
890 }
891
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000892 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000893 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000894 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000895 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000896 if (!Protocol)
897 return QualType();
898 Protocols.push_back(Protocol);
899 }
900
Douglas Gregore9d95f12015-07-07 03:57:35 +0000901 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +0000902 Protocols,
903 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000904}
905
John McCall424cec92011-01-19 06:33:43 +0000906QualType
907ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000908 QualType ToPointeeType = Importer.Import(T->getPointeeType());
909 if (ToPointeeType.isNull())
910 return QualType();
911
John McCall8b07ec22010-05-15 11:32:37 +0000912 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000913}
914
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000915//----------------------------------------------------------------------------
916// Import Declarations
917//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000918bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
919 DeclContext *&LexicalDC,
920 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +0000921 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000922 SourceLocation &Loc) {
923 // Import the context of this declaration.
924 DC = Importer.ImportContext(D->getDeclContext());
925 if (!DC)
926 return true;
927
928 LexicalDC = DC;
929 if (D->getDeclContext() != D->getLexicalDeclContext()) {
930 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
931 if (!LexicalDC)
932 return true;
933 }
934
935 // Import the name of this declaration.
936 Name = Importer.Import(D->getDeclName());
937 if (D->getDeclName() && !Name)
938 return true;
939
940 // Import the location of this declaration.
941 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +0000942 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000943 return false;
944}
945
Douglas Gregord451ea92011-07-29 23:31:30 +0000946void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
947 if (!FromD)
948 return;
949
950 if (!ToD) {
951 ToD = Importer.Import(FromD);
952 if (!ToD)
953 return;
954 }
955
956 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
957 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +0000958 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +0000959 ImportDefinition(FromRecord, ToRecord);
960 }
961 }
962 return;
963 }
964
965 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
966 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
967 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
968 ImportDefinition(FromEnum, ToEnum);
969 }
970 }
971 return;
972 }
973}
974
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000975void
976ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
977 DeclarationNameInfo& To) {
978 // NOTE: To.Name and To.Loc are already imported.
979 // We only have to import To.LocInfo.
980 switch (To.getName().getNameKind()) {
981 case DeclarationName::Identifier:
982 case DeclarationName::ObjCZeroArgSelector:
983 case DeclarationName::ObjCOneArgSelector:
984 case DeclarationName::ObjCMultiArgSelector:
985 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +0000986 case DeclarationName::CXXDeductionGuideName:
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000987 return;
988
989 case DeclarationName::CXXOperatorName: {
990 SourceRange Range = From.getCXXOperatorNameRange();
991 To.setCXXOperatorNameRange(Importer.Import(Range));
992 return;
993 }
994 case DeclarationName::CXXLiteralOperatorName: {
995 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
996 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
997 return;
998 }
999 case DeclarationName::CXXConstructorName:
1000 case DeclarationName::CXXDestructorName:
1001 case DeclarationName::CXXConversionFunctionName: {
1002 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1003 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1004 return;
1005 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001006 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001007 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001008}
1009
Douglas Gregor2e15c842012-02-01 21:00:38 +00001010void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001011 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001012 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001013 return;
1014 }
1015
Aaron Ballman629afae2014-03-07 19:56:05 +00001016 for (auto *From : FromDC->decls())
1017 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00001018}
1019
Douglas Gregord451ea92011-07-29 23:31:30 +00001020bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001021 ImportDefinitionKind Kind) {
1022 if (To->getDefinition() || To->isBeingDefined()) {
1023 if (Kind == IDK_Everything)
1024 ImportDeclContext(From, /*ForceImport=*/true);
1025
Douglas Gregore2e50d332010-12-01 01:36:18 +00001026 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001027 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001028
1029 To->startDefinition();
1030
1031 // Add base classes.
1032 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1033 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001034
1035 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1036 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1037 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001038 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001039 ToData.Aggregate = FromData.Aggregate;
1040 ToData.PlainOldData = FromData.PlainOldData;
1041 ToData.Empty = FromData.Empty;
1042 ToData.Polymorphic = FromData.Polymorphic;
1043 ToData.Abstract = FromData.Abstract;
1044 ToData.IsStandardLayout = FromData.IsStandardLayout;
1045 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1046 ToData.HasPrivateFields = FromData.HasPrivateFields;
1047 ToData.HasProtectedFields = FromData.HasProtectedFields;
1048 ToData.HasPublicFields = FromData.HasPublicFields;
1049 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001050 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001051 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001052 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001053 ToData.HasUninitializedReferenceMember
1054 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001055 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001056 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1057 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001058 ToData.NeedOverloadResolutionForCopyConstructor
1059 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001060 ToData.NeedOverloadResolutionForMoveConstructor
1061 = FromData.NeedOverloadResolutionForMoveConstructor;
1062 ToData.NeedOverloadResolutionForMoveAssignment
1063 = FromData.NeedOverloadResolutionForMoveAssignment;
1064 ToData.NeedOverloadResolutionForDestructor
1065 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001066 ToData.DefaultedCopyConstructorIsDeleted
1067 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001068 ToData.DefaultedMoveConstructorIsDeleted
1069 = FromData.DefaultedMoveConstructorIsDeleted;
1070 ToData.DefaultedMoveAssignmentIsDeleted
1071 = FromData.DefaultedMoveAssignmentIsDeleted;
1072 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001073 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1074 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001075 ToData.HasConstexprNonCopyMoveConstructor
1076 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001077 ToData.HasDefaultedDefaultConstructor
1078 = FromData.HasDefaultedDefaultConstructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001079 ToData.CanPassInRegisters = FromData.CanPassInRegisters;
Richard Smith561fb152012-02-25 07:33:38 +00001080 ToData.DefaultedDefaultConstructorIsConstexpr
1081 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001082 ToData.HasConstexprDefaultConstructor
1083 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001084 ToData.HasNonLiteralTypeFieldsOrBases
1085 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001086 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001087 ToData.UserProvidedDefaultConstructor
1088 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001089 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001090 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1091 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1092 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1093 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001094 ToData.ImplicitCopyAssignmentHasConstParam
1095 = FromData.ImplicitCopyAssignmentHasConstParam;
1096 ToData.HasDeclaredCopyConstructorWithConstParam
1097 = FromData.HasDeclaredCopyConstructorWithConstParam;
1098 ToData.HasDeclaredCopyAssignmentWithConstParam
1099 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001100
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001101 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001102 for (const auto &Base1 : FromCXX->bases()) {
1103 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001104 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001105 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001106
1107 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001108 if (Base1.isPackExpansion())
1109 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001110
1111 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00001112 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00001113
Douglas Gregore2e50d332010-12-01 01:36:18 +00001114 Bases.push_back(
1115 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00001116 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1117 Base1.isVirtual(),
1118 Base1.isBaseOfClass(),
1119 Base1.getAccessSpecifierAsWritten(),
1120 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00001121 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001122 }
1123 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001124 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001125 }
1126
Douglas Gregor2e15c842012-02-01 21:00:38 +00001127 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001128 ImportDeclContext(From, /*ForceImport=*/true);
1129
Douglas Gregore2e50d332010-12-01 01:36:18 +00001130 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001131 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001132}
1133
Larisse Voufo39a1e502013-08-06 01:03:05 +00001134bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1135 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00001136 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00001137 return false;
1138
1139 // FIXME: Can we really import any initializer? Alternatively, we could force
1140 // ourselves to import every declaration of a variable and then only use
1141 // getInit() here.
1142 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1143
1144 // FIXME: Other bits to merge?
1145
1146 return false;
1147}
1148
Douglas Gregord451ea92011-07-29 23:31:30 +00001149bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001150 ImportDefinitionKind Kind) {
1151 if (To->getDefinition() || To->isBeingDefined()) {
1152 if (Kind == IDK_Everything)
1153 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001154 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001155 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001156
1157 To->startDefinition();
1158
1159 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1160 if (T.isNull())
1161 return true;
1162
1163 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1164 if (ToPromotionType.isNull())
1165 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001166
1167 if (shouldForceImportDeclContext(Kind))
1168 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001169
1170 // FIXME: we might need to merge the number of positive or negative bits
1171 // if the enumerator lists don't match.
1172 To->completeDefinition(T, ToPromotionType,
1173 From->getNumPositiveBits(),
1174 From->getNumNegativeBits());
1175 return false;
1176}
1177
Douglas Gregora082a492010-11-30 19:14:50 +00001178TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1179 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001180 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1181 if (ImportContainerChecked(*Params, ToParams))
1182 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00001183
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001184 Expr *ToRequiresClause;
1185 if (Expr *const R = Params->getRequiresClause()) {
1186 ToRequiresClause = Importer.Import(R);
1187 if (!ToRequiresClause)
1188 return nullptr;
1189 } else {
1190 ToRequiresClause = nullptr;
1191 }
1192
Douglas Gregora082a492010-11-30 19:14:50 +00001193 return TemplateParameterList::Create(Importer.getToContext(),
1194 Importer.Import(Params->getTemplateLoc()),
1195 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00001196 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001197 Importer.Import(Params->getRAngleLoc()),
1198 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001199}
1200
Douglas Gregore2e50d332010-12-01 01:36:18 +00001201TemplateArgument
1202ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1203 switch (From.getKind()) {
1204 case TemplateArgument::Null:
1205 return TemplateArgument();
1206
1207 case TemplateArgument::Type: {
1208 QualType ToType = Importer.Import(From.getAsType());
1209 if (ToType.isNull())
1210 return TemplateArgument();
1211 return TemplateArgument(ToType);
1212 }
1213
1214 case TemplateArgument::Integral: {
1215 QualType ToType = Importer.Import(From.getIntegralType());
1216 if (ToType.isNull())
1217 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001218 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001219 }
1220
Eli Friedmanb826a002012-09-26 02:36:12 +00001221 case TemplateArgument::Declaration: {
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001222 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
1223 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1224 if (!To || ToType.isNull())
1225 return TemplateArgument();
1226 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00001227 }
1228
1229 case TemplateArgument::NullPtr: {
1230 QualType ToType = Importer.Import(From.getNullPtrType());
1231 if (ToType.isNull())
1232 return TemplateArgument();
1233 return TemplateArgument(ToType, /*isNullPtr*/true);
1234 }
1235
Douglas Gregore2e50d332010-12-01 01:36:18 +00001236 case TemplateArgument::Template: {
1237 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1238 if (ToTemplate.isNull())
1239 return TemplateArgument();
1240
1241 return TemplateArgument(ToTemplate);
1242 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001243
1244 case TemplateArgument::TemplateExpansion: {
1245 TemplateName ToTemplate
1246 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1247 if (ToTemplate.isNull())
1248 return TemplateArgument();
1249
Douglas Gregore1d60df2011-01-14 23:41:42 +00001250 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001251 }
1252
Douglas Gregore2e50d332010-12-01 01:36:18 +00001253 case TemplateArgument::Expression:
1254 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1255 return TemplateArgument(ToExpr);
1256 return TemplateArgument();
1257
1258 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001259 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001260 ToPack.reserve(From.pack_size());
1261 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1262 return TemplateArgument();
Benjamin Kramercce63472015-08-05 09:40:22 +00001263
1264 return TemplateArgument(
1265 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001266 }
1267 }
1268
1269 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00001270}
1271
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001272Optional<TemplateArgumentLoc>
1273ASTNodeImporter::ImportTemplateArgumentLoc(const TemplateArgumentLoc &TALoc) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001274 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1275 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1276 TemplateArgumentLocInfo ToInfo;
1277 if (Arg.getKind() == TemplateArgument::Expression) {
1278 Expr *E = Importer.Import(FromInfo.getAsExpr());
1279 ToInfo = TemplateArgumentLocInfo(E);
1280 if (!E)
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001281 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001282 } else if (Arg.getKind() == TemplateArgument::Type) {
1283 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1284 ToInfo = TemplateArgumentLocInfo(TSI);
1285 else
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001286 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001287 } else {
1288 ToInfo = TemplateArgumentLocInfo(
1289 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1290 Importer.Import(FromInfo.getTemplateNameLoc()),
1291 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1292 }
1293 return TemplateArgumentLoc(Arg, ToInfo);
1294}
1295
Douglas Gregore2e50d332010-12-01 01:36:18 +00001296bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1297 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001298 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001299 for (unsigned I = 0; I != NumFromArgs; ++I) {
1300 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1301 if (To.isNull() && !FromArgs[I].isNull())
1302 return true;
1303
1304 ToArgs.push_back(To);
1305 }
1306
1307 return false;
1308}
1309
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001310// We cannot use Optional<> pattern here and below because
1311// TemplateArgumentListInfo's operator new is declared as deleted so it cannot
1312// be stored in Optional.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001313template <typename InContainerTy>
1314bool ASTNodeImporter::ImportTemplateArgumentListInfo(
1315 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1316 for (const auto &FromLoc : Container) {
1317 if (auto ToLoc = ImportTemplateArgumentLoc(FromLoc))
1318 ToTAInfo.addArgument(*ToLoc);
1319 else
1320 return true;
1321 }
1322 return false;
1323}
1324
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001325template <typename InContainerTy>
1326bool ASTNodeImporter::ImportTemplateArgumentListInfo(
1327 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
1328 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
1329 TemplateArgumentListInfo ToTAInfo(Importer.Import(FromLAngleLoc),
1330 Importer.Import(FromRAngleLoc));
1331 if (ImportTemplateArgumentListInfo(Container, ToTAInfo))
1332 return true;
1333 Result = ToTAInfo;
1334 return false;
1335}
1336
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001337template <>
1338bool ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
1339 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
1340 return ImportTemplateArgumentListInfo(
1341 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
1342}
1343
1344template <>
1345bool ASTNodeImporter::ImportTemplateArgumentListInfo<
1346 ASTTemplateArgumentListInfo>(const ASTTemplateArgumentListInfo &From,
1347 TemplateArgumentListInfo &Result) {
1348 return ImportTemplateArgumentListInfo(From.LAngleLoc, From.RAngleLoc,
1349 From.arguments(), Result);
1350}
1351
Douglas Gregor5c73e912010-02-11 00:48:18 +00001352bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001353 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001354 // Eliminate a potential failure point where we attempt to re-import
1355 // something we're trying to import while completing ToRecord.
1356 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1357 if (ToOrigin) {
1358 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
1359 if (ToOriginRecord)
1360 ToRecord = ToOriginRecord;
1361 }
1362
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001363 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001364 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001365 Importer.getNonEquivalentDecls(),
1366 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001367 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001368}
1369
Larisse Voufo39a1e502013-08-06 01:03:05 +00001370bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1371 bool Complain) {
1372 StructuralEquivalenceContext Ctx(
1373 Importer.getFromContext(), Importer.getToContext(),
1374 Importer.getNonEquivalentDecls(), false, Complain);
1375 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
1376}
1377
Douglas Gregor98c10182010-02-12 22:17:39 +00001378bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001379 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001380 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001381 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001382 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001383}
1384
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001385bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1386 FunctionTemplateDecl *To) {
1387 StructuralEquivalenceContext Ctx(
1388 Importer.getFromContext(), Importer.getToContext(),
1389 Importer.getNonEquivalentDecls(), false, false);
1390 return Ctx.IsStructurallyEquivalent(From, To);
1391}
1392
Douglas Gregor91155082012-11-14 22:29:20 +00001393bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
1394 EnumConstantDecl *ToEC)
1395{
1396 const llvm::APSInt &FromVal = FromEC->getInitVal();
1397 const llvm::APSInt &ToVal = ToEC->getInitVal();
1398
1399 return FromVal.isSigned() == ToVal.isSigned() &&
1400 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1401 FromVal == ToVal;
1402}
1403
1404bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001405 ClassTemplateDecl *To) {
1406 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1407 Importer.getToContext(),
1408 Importer.getNonEquivalentDecls());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001409 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00001410}
1411
Larisse Voufo39a1e502013-08-06 01:03:05 +00001412bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1413 VarTemplateDecl *To) {
1414 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1415 Importer.getToContext(),
1416 Importer.getNonEquivalentDecls());
1417 return Ctx.IsStructurallyEquivalent(From, To);
1418}
1419
Douglas Gregore4c83e42010-02-09 22:48:33 +00001420Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001421 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001422 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00001423 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00001424}
1425
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001426Decl *ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
1427 // Import the context of this declaration.
1428 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1429 if (!DC)
1430 return nullptr;
1431
1432 DeclContext *LexicalDC = DC;
1433 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1434 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1435 if (!LexicalDC)
1436 return nullptr;
1437 }
1438
1439 // Import the location of this declaration.
1440 SourceLocation Loc = Importer.Import(D->getLocation());
1441
1442 EmptyDecl *ToD = EmptyDecl::Create(Importer.getToContext(), DC, Loc);
1443 ToD->setLexicalDeclContext(LexicalDC);
1444 Importer.Imported(D, ToD);
1445 LexicalDC->addDeclInternal(ToD);
1446 return ToD;
1447}
1448
Sean Callanan65198272011-11-17 23:20:56 +00001449Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1450 TranslationUnitDecl *ToD =
1451 Importer.getToContext().getTranslationUnitDecl();
1452
1453 Importer.Imported(D, ToD);
1454
1455 return ToD;
1456}
1457
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001458Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
1459
1460 SourceLocation Loc = Importer.Import(D->getLocation());
1461 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1462
1463 // Import the context of this declaration.
1464 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1465 if (!DC)
1466 return nullptr;
1467
1468 AccessSpecDecl *accessSpecDecl
1469 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
1470 DC, Loc, ColonLoc);
1471
1472 if (!accessSpecDecl)
1473 return nullptr;
1474
1475 // Lexical DeclContext and Semantic DeclContext
1476 // is always the same for the accessSpec.
1477 accessSpecDecl->setLexicalDeclContext(DC);
1478 DC->addDeclInternal(accessSpecDecl);
1479
1480 return accessSpecDecl;
1481}
1482
Aleksei Sidorina693b372016-09-28 10:16:56 +00001483Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1484 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1485 if (!DC)
1486 return nullptr;
1487
1488 DeclContext *LexicalDC = DC;
1489
1490 // Import the location of this declaration.
1491 SourceLocation Loc = Importer.Import(D->getLocation());
1492
1493 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1494 if (!AssertExpr)
1495 return nullptr;
1496
1497 StringLiteral *FromMsg = D->getMessage();
1498 StringLiteral *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
1499 if (!ToMsg && FromMsg)
1500 return nullptr;
1501
1502 StaticAssertDecl *ToD = StaticAssertDecl::Create(
1503 Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1504 Importer.Import(D->getRParenLoc()), D->isFailed());
1505
1506 ToD->setLexicalDeclContext(LexicalDC);
1507 LexicalDC->addDeclInternal(ToD);
1508 Importer.Imported(D, ToD);
1509 return ToD;
1510}
1511
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001512Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1513 // Import the major distinguishing characteristics of this namespace.
1514 DeclContext *DC, *LexicalDC;
1515 DeclarationName Name;
1516 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001517 NamedDecl *ToD;
1518 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001519 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001520 if (ToD)
1521 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001522
1523 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001524 if (!Name) {
1525 // This is an anonymous namespace. Adopt an existing anonymous
1526 // namespace if we can.
1527 // FIXME: Not testable.
1528 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1529 MergeWithNamespace = TU->getAnonymousNamespace();
1530 else
1531 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1532 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001533 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001534 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001535 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001536 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1537 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001538 continue;
1539
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001540 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001541 MergeWithNamespace = FoundNS;
1542 ConflictingDecls.clear();
1543 break;
1544 }
1545
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001546 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001547 }
1548
1549 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001550 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001551 ConflictingDecls.data(),
1552 ConflictingDecls.size());
1553 }
1554 }
1555
1556 // Create the "to" namespace, if needed.
1557 NamespaceDecl *ToNamespace = MergeWithNamespace;
1558 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001559 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00001560 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001561 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00001562 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00001563 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001564 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001565 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001566
1567 // If this is an anonymous namespace, register it as the anonymous
1568 // namespace within its context.
1569 if (!Name) {
1570 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1571 TU->setAnonymousNamespace(ToNamespace);
1572 else
1573 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1574 }
1575 }
1576 Importer.Imported(D, ToNamespace);
1577
1578 ImportDeclContext(D);
1579
1580 return ToNamespace;
1581}
1582
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001583Decl *ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1584 // Import the major distinguishing characteristics of this namespace.
1585 DeclContext *DC, *LexicalDC;
1586 DeclarationName Name;
1587 SourceLocation Loc;
1588 NamedDecl *LookupD;
1589 if (ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
1590 return nullptr;
1591 if (LookupD)
1592 return LookupD;
1593
1594 // NOTE: No conflict resolution is done for namespace aliases now.
1595
1596 NamespaceDecl *TargetDecl = cast_or_null<NamespaceDecl>(
1597 Importer.Import(D->getNamespace()));
1598 if (!TargetDecl)
1599 return nullptr;
1600
1601 IdentifierInfo *ToII = Importer.Import(D->getIdentifier());
1602 if (!ToII)
1603 return nullptr;
1604
1605 NestedNameSpecifierLoc ToQLoc = Importer.Import(D->getQualifierLoc());
1606 if (D->getQualifierLoc() && !ToQLoc)
1607 return nullptr;
1608
1609 NamespaceAliasDecl *ToD = NamespaceAliasDecl::Create(
1610 Importer.getToContext(), DC, Importer.Import(D->getNamespaceLoc()),
1611 Importer.Import(D->getAliasLoc()), ToII, ToQLoc,
1612 Importer.Import(D->getTargetNameLoc()), TargetDecl);
1613
1614 ToD->setLexicalDeclContext(LexicalDC);
1615 Importer.Imported(D, ToD);
1616 LexicalDC->addDeclInternal(ToD);
1617
1618 return ToD;
1619}
1620
Richard Smithdda56e42011-04-15 14:24:37 +00001621Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001622 // Import the major distinguishing characteristics of this typedef.
1623 DeclContext *DC, *LexicalDC;
1624 DeclarationName Name;
1625 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001626 NamedDecl *ToD;
1627 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001628 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001629 if (ToD)
1630 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001631
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001632 // If this typedef is not in block scope, determine whether we've
1633 // seen a typedef with the same name (that we can merge with) or any
1634 // other entity by that name (which name lookup could conflict with).
1635 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001636 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001637 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001638 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001639 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001640 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1641 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001642 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00001643 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001644 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001645 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1646 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001647 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001648 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001649
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001650 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001651 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001652
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001653 if (!ConflictingDecls.empty()) {
1654 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1655 ConflictingDecls.data(),
1656 ConflictingDecls.size());
1657 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001658 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001659 }
1660 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001661
Douglas Gregorb4964f72010-02-15 23:54:17 +00001662 // Import the underlying type of this typedef;
1663 QualType T = Importer.Import(D->getUnderlyingType());
1664 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001665 return nullptr;
1666
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001667 // Create the new typedef node.
1668 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00001669 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00001670 TypedefNameDecl *ToTypedef;
1671 if (IsAlias)
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001672 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC, StartL, Loc,
1673 Name.getAsIdentifierInfo(), TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001674 else
Richard Smithdda56e42011-04-15 14:24:37 +00001675 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1676 StartL, Loc,
1677 Name.getAsIdentifierInfo(),
1678 TInfo);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001679
Douglas Gregordd483172010-02-22 17:42:47 +00001680 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001681 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001682 Importer.Imported(D, ToTypedef);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001683
1684 // Templated declarations should not appear in DeclContext.
1685 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
1686 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
1687 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001688
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001689 return ToTypedef;
1690}
1691
Richard Smithdda56e42011-04-15 14:24:37 +00001692Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1693 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1694}
1695
1696Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1697 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1698}
1699
Gabor Horvath7a91c082017-11-14 11:30:38 +00001700Decl *ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1701 // Import the major distinguishing characteristics of this typedef.
1702 DeclContext *DC, *LexicalDC;
1703 DeclarationName Name;
1704 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001705 NamedDecl *FoundD;
1706 if (ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001707 return nullptr;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001708 if (FoundD)
1709 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00001710
1711 // If this typedef is not in block scope, determine whether we've
1712 // seen a typedef with the same name (that we can merge with) or any
1713 // other entity by that name (which name lookup could conflict with).
1714 if (!DC->isFunctionOrMethod()) {
1715 SmallVector<NamedDecl *, 4> ConflictingDecls;
1716 unsigned IDNS = Decl::IDNS_Ordinary;
1717 SmallVector<NamedDecl *, 2> FoundDecls;
1718 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1719 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1720 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1721 continue;
1722 if (auto *FoundAlias =
1723 dyn_cast<TypeAliasTemplateDecl>(FoundDecls[I]))
1724 return Importer.Imported(D, FoundAlias);
1725 ConflictingDecls.push_back(FoundDecls[I]);
1726 }
1727
1728 if (!ConflictingDecls.empty()) {
1729 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1730 ConflictingDecls.data(),
1731 ConflictingDecls.size());
1732 if (!Name)
1733 return nullptr;
1734 }
1735 }
1736
1737 TemplateParameterList *Params = ImportTemplateParameterList(
1738 D->getTemplateParameters());
1739 if (!Params)
1740 return nullptr;
1741
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001742 auto *TemplDecl = cast_or_null<TypeAliasDecl>(
Gabor Horvath7a91c082017-11-14 11:30:38 +00001743 Importer.Import(D->getTemplatedDecl()));
1744 if (!TemplDecl)
1745 return nullptr;
1746
1747 TypeAliasTemplateDecl *ToAlias = TypeAliasTemplateDecl::Create(
1748 Importer.getToContext(), DC, Loc, Name, Params, TemplDecl);
1749
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001750 TemplDecl->setDescribedAliasTemplate(ToAlias);
1751
Gabor Horvath7a91c082017-11-14 11:30:38 +00001752 ToAlias->setAccess(D->getAccess());
1753 ToAlias->setLexicalDeclContext(LexicalDC);
1754 Importer.Imported(D, ToAlias);
1755 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001756 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00001757}
1758
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001759Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
1760 // Import the major distinguishing characteristics of this label.
1761 DeclContext *DC, *LexicalDC;
1762 DeclarationName Name;
1763 SourceLocation Loc;
1764 NamedDecl *ToD;
1765 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1766 return nullptr;
1767 if (ToD)
1768 return ToD;
1769
1770 assert(LexicalDC->isFunctionOrMethod());
1771
1772 LabelDecl *ToLabel = D->isGnuLocal()
1773 ? LabelDecl::Create(Importer.getToContext(),
1774 DC, Importer.Import(D->getLocation()),
1775 Name.getAsIdentifierInfo(),
1776 Importer.Import(D->getLocStart()))
1777 : LabelDecl::Create(Importer.getToContext(),
1778 DC, Importer.Import(D->getLocation()),
1779 Name.getAsIdentifierInfo());
1780 Importer.Imported(D, ToLabel);
1781
1782 LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
1783 if (!Label)
1784 return nullptr;
1785
1786 ToLabel->setStmt(Label);
1787 ToLabel->setLexicalDeclContext(LexicalDC);
1788 LexicalDC->addDeclInternal(ToLabel);
1789 return ToLabel;
1790}
1791
Douglas Gregor98c10182010-02-12 22:17:39 +00001792Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1793 // Import the major distinguishing characteristics of this enum.
1794 DeclContext *DC, *LexicalDC;
1795 DeclarationName Name;
1796 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001797 NamedDecl *ToD;
1798 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001799 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001800 if (ToD)
1801 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001802
Douglas Gregor98c10182010-02-12 22:17:39 +00001803 // Figure out what enum name we're looking for.
1804 unsigned IDNS = Decl::IDNS_Tag;
1805 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001806 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1807 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00001808 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001809 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00001810 IDNS |= Decl::IDNS_Ordinary;
1811
1812 // We may already have an enum of the same name; try to find and match it.
1813 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001814 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001815 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001816 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001817 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1818 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00001819 continue;
1820
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001821 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00001822 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00001823 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1824 Found = Tag->getDecl();
1825 }
1826
1827 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001828 if (IsStructuralMatch(D, FoundEnum))
1829 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001830 }
1831
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001832 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00001833 }
1834
1835 if (!ConflictingDecls.empty()) {
1836 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1837 ConflictingDecls.data(),
1838 ConflictingDecls.size());
1839 }
1840 }
1841
1842 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001843 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
1844 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00001845 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001846 D->isScoped(), D->isScopedUsingClassTag(),
1847 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00001848 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00001849 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00001850 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00001851 D2->setLexicalDeclContext(LexicalDC);
1852 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00001853 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00001854
1855 // Import the integer type.
1856 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1857 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001858 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00001859 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001860
1861 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00001862 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00001863 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00001864
Douglas Gregor3996e242010-02-15 22:01:00 +00001865 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00001866}
1867
Douglas Gregor5c73e912010-02-11 00:48:18 +00001868Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1869 // If this record has a definition in the translation unit we're coming from,
1870 // but this particular declaration is not that definition, import the
1871 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001872 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001873 if (Definition && Definition != D) {
1874 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001875 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00001876 return nullptr;
1877
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001878 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001879 }
1880
1881 // Import the major distinguishing characteristics of this record.
1882 DeclContext *DC, *LexicalDC;
1883 DeclarationName Name;
1884 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001885 NamedDecl *ToD;
1886 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001887 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001888 if (ToD)
1889 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001890
Douglas Gregor5c73e912010-02-11 00:48:18 +00001891 // Figure out what structure name we're looking for.
1892 unsigned IDNS = Decl::IDNS_Tag;
1893 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001894 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1895 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001896 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001897 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00001898 IDNS |= Decl::IDNS_Ordinary;
1899
1900 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00001901 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00001902 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00001903 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001904 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001905 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001906 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00001907
1908 if (!FoundDecls.empty()) {
1909 // We're going to have to compare D against potentially conflicting Decls, so complete it.
1910 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
1911 D->getASTContext().getExternalSource()->CompleteType(D);
1912 }
1913
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001914 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1915 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00001916 continue;
1917
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001918 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00001919 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00001920 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1921 Found = Tag->getDecl();
1922 }
1923
1924 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Gabor Horvath3b392bb2017-04-03 21:06:45 +00001925 if (D->isAnonymousStructOrUnion() &&
1926 FoundRecord->isAnonymousStructOrUnion()) {
1927 // If both anonymous structs/unions are in a record context, make sure
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001928 // they occur in the same location in the context records.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001929 if (Optional<unsigned> Index1 =
1930 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
1931 D)) {
1932 if (Optional<unsigned> Index2 = StructuralEquivalenceContext::
Sean Callanan488f8612016-07-14 19:53:44 +00001933 findUntaggedStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001934 if (*Index1 != *Index2)
1935 continue;
1936 }
1937 }
1938 }
1939
Sean Callanan9092d472017-05-13 00:46:33 +00001940 PrevDecl = FoundRecord;
1941
Douglas Gregor25791052010-02-12 00:09:27 +00001942 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00001943 if ((SearchName && !D->isCompleteDefinition())
1944 || (D->isCompleteDefinition() &&
1945 D->isAnonymousStructOrUnion()
1946 == FoundDef->isAnonymousStructOrUnion() &&
1947 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00001948 // The record types structurally match, or the "from" translation
1949 // unit only had a forward declaration anyway; call it the same
1950 // function.
1951 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001952 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00001953 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00001954 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00001955 // We have a forward declaration of this type, so adopt that forward
1956 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00001957
1958 // If one or both can be completed from external storage then try one
1959 // last time to complete and compare them before doing this.
1960
1961 if (FoundRecord->hasExternalLexicalStorage() &&
1962 !FoundRecord->isCompleteDefinition())
1963 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
1964 if (D->hasExternalLexicalStorage())
1965 D->getASTContext().getExternalSource()->CompleteType(D);
1966
1967 if (FoundRecord->isCompleteDefinition() &&
1968 D->isCompleteDefinition() &&
1969 !IsStructuralMatch(D, FoundRecord))
1970 continue;
1971
Douglas Gregor25791052010-02-12 00:09:27 +00001972 AdoptDecl = FoundRecord;
1973 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00001974 } else if (!SearchName) {
1975 continue;
1976 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00001977 }
1978
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001979 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001980 }
1981
Douglas Gregordd6006f2012-07-17 21:16:27 +00001982 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00001983 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1984 ConflictingDecls.data(),
1985 ConflictingDecls.size());
1986 }
1987 }
1988
1989 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001990 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001991 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00001992 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00001993 CXXRecordDecl *D2CXX = nullptr;
1994 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) {
1995 if (DCXX->isLambda()) {
1996 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
1997 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
1998 DC, TInfo, Loc,
1999 DCXX->isDependentLambda(),
2000 DCXX->isGenericLambda(),
2001 DCXX->getLambdaCaptureDefault());
2002 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2003 if (DCXX->getLambdaContextDecl() && !CDecl)
2004 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00002005 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002006 } else if (DCXX->isInjectedClassName()) {
2007 // We have to be careful to do a similar dance to the one in
2008 // Sema::ActOnStartCXXMemberDeclarations
2009 CXXRecordDecl *const PrevDecl = nullptr;
2010 const bool DelayTypeCreation = true;
2011 D2CXX = CXXRecordDecl::Create(
2012 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
2013 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
2014 Importer.getToContext().getTypeDeclType(
2015 D2CXX, llvm::dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002016 } else {
2017 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2018 D->getTagKind(),
2019 DC, StartLoc, Loc,
2020 Name.getAsIdentifierInfo());
2021 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002022 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002023 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002024 D2->setLexicalDeclContext(LexicalDC);
2025 if (!DCXX->getDescribedClassTemplate())
2026 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002027
2028 Importer.Imported(D, D2);
2029
2030 if (ClassTemplateDecl *FromDescribed =
2031 DCXX->getDescribedClassTemplate()) {
2032 ClassTemplateDecl *ToDescribed = cast_or_null<ClassTemplateDecl>(
2033 Importer.Import(FromDescribed));
2034 if (!ToDescribed)
2035 return nullptr;
2036 D2CXX->setDescribedClassTemplate(ToDescribed);
2037
2038 } else if (MemberSpecializationInfo *MemberInfo =
2039 DCXX->getMemberSpecializationInfo()) {
2040 TemplateSpecializationKind SK =
2041 MemberInfo->getTemplateSpecializationKind();
2042 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
2043 CXXRecordDecl *ToInst =
2044 cast_or_null<CXXRecordDecl>(Importer.Import(FromInst));
2045 if (FromInst && !ToInst)
2046 return nullptr;
2047 D2CXX->setInstantiationOfMemberClass(ToInst, SK);
2048 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2049 Importer.Import(MemberInfo->getPointOfInstantiation()));
2050 }
2051
Douglas Gregor25791052010-02-12 00:09:27 +00002052 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002053 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002054 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002055 D2->setLexicalDeclContext(LexicalDC);
2056 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002057 }
Douglas Gregor14454802011-02-25 02:25:35 +00002058
2059 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd6006f2012-07-17 21:16:27 +00002060 if (D->isAnonymousStructOrUnion())
2061 D2->setAnonymousStructOrUnion(true);
Sean Callanan9092d472017-05-13 00:46:33 +00002062 if (PrevDecl) {
2063 // FIXME: do this for all Redeclarables, not just RecordDecls.
2064 D2->setPreviousDecl(PrevDecl);
2065 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002066 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002067
Douglas Gregor3996e242010-02-15 22:01:00 +00002068 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002069
Douglas Gregor95d82832012-01-24 18:36:04 +00002070 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002071 return nullptr;
2072
Douglas Gregor3996e242010-02-15 22:01:00 +00002073 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002074}
2075
Douglas Gregor98c10182010-02-12 22:17:39 +00002076Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2077 // Import the major distinguishing characteristics of this enumerator.
2078 DeclContext *DC, *LexicalDC;
2079 DeclarationName Name;
2080 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002081 NamedDecl *ToD;
2082 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002083 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002084 if (ToD)
2085 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002086
2087 QualType T = Importer.Import(D->getType());
2088 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002089 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002090
Douglas Gregor98c10182010-02-12 22:17:39 +00002091 // Determine whether there are any other declarations with the same name and
2092 // in the same context.
2093 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002094 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002095 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002096 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002097 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002098 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2099 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002100 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002101
2102 if (EnumConstantDecl *FoundEnumConstant
2103 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2104 if (IsStructuralMatch(D, FoundEnumConstant))
2105 return Importer.Imported(D, FoundEnumConstant);
2106 }
2107
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002108 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002109 }
2110
2111 if (!ConflictingDecls.empty()) {
2112 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2113 ConflictingDecls.data(),
2114 ConflictingDecls.size());
2115 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002116 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002117 }
2118 }
2119
2120 Expr *Init = Importer.Import(D->getInitExpr());
2121 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002122 return nullptr;
2123
Douglas Gregor98c10182010-02-12 22:17:39 +00002124 EnumConstantDecl *ToEnumerator
2125 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2126 Name.getAsIdentifierInfo(), T,
2127 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002128 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002129 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002130 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002131 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002132 return ToEnumerator;
2133}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002134
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002135bool ASTNodeImporter::ImportTemplateInformation(FunctionDecl *FromFD,
2136 FunctionDecl *ToFD) {
2137 switch (FromFD->getTemplatedKind()) {
2138 case FunctionDecl::TK_NonTemplate:
2139 case FunctionDecl::TK_FunctionTemplate:
Sam McCallfdc32072018-01-26 12:06:44 +00002140 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002141
2142 case FunctionDecl::TK_MemberSpecialization: {
2143 auto *InstFD = cast_or_null<FunctionDecl>(
2144 Importer.Import(FromFD->getInstantiatedFromMemberFunction()));
2145 if (!InstFD)
2146 return true;
2147
2148 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
2149 SourceLocation POI = Importer.Import(
2150 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation());
2151 ToFD->setInstantiationOfMemberFunction(InstFD, TSK);
2152 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002153 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002154 }
2155
2156 case FunctionDecl::TK_FunctionTemplateSpecialization: {
2157 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
2158 auto *Template = cast_or_null<FunctionTemplateDecl>(
2159 Importer.Import(FTSInfo->getTemplate()));
2160 if (!Template)
2161 return true;
2162 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
2163
2164 // Import template arguments.
2165 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
2166 SmallVector<TemplateArgument, 8> ToTemplArgs;
2167 if (ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
2168 ToTemplArgs))
2169 return true;
2170
2171 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
2172 Importer.getToContext(), ToTemplArgs);
2173
2174 TemplateArgumentListInfo ToTAInfo;
2175 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002176 if (FromTAArgsAsWritten)
2177 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002178 return true;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002179
2180 SourceLocation POI = Importer.Import(FTSInfo->getPointOfInstantiation());
2181
2182 ToFD->setFunctionTemplateSpecialization(
2183 Template, ToTAList, /* InsertPos= */ nullptr,
2184 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002185 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002186 }
2187
2188 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2189 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2190 UnresolvedSet<8> TemplDecls;
2191 unsigned NumTemplates = FromInfo->getNumTemplates();
2192 for (unsigned I = 0; I < NumTemplates; I++) {
2193 if (auto *ToFTD = cast_or_null<FunctionTemplateDecl>(
2194 Importer.Import(FromInfo->getTemplate(I))))
2195 TemplDecls.addDecl(ToFTD);
2196 else
2197 return true;
2198 }
2199
2200 // Import TemplateArgumentListInfo.
2201 TemplateArgumentListInfo ToTAInfo;
2202 if (ImportTemplateArgumentListInfo(
2203 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2204 llvm::makeArrayRef(FromInfo->getTemplateArgs(),
2205 FromInfo->getNumTemplateArgs()),
2206 ToTAInfo))
2207 return true;
2208
2209 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2210 TemplDecls, ToTAInfo);
Sam McCallfdc32072018-01-26 12:06:44 +00002211 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002212 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002213 }
Sam McCallfdc32072018-01-26 12:06:44 +00002214 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002215}
2216
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002217Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2218 // Import the major distinguishing characteristics of this function.
2219 DeclContext *DC, *LexicalDC;
2220 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002221 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002222 NamedDecl *ToD;
2223 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002224 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002225 if (ToD)
2226 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002227
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002228 const FunctionDecl *FoundWithoutBody = nullptr;
2229
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002230 // Try to find a function in our own ("to") context with the same name, same
2231 // type, and in the same context as the function we're importing.
2232 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002233 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002234 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002235 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002236 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002237 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2238 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002239 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002240
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002241 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002242 if (FoundFunction->hasExternalFormalLinkage() &&
2243 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002244 if (Importer.IsStructurallyEquivalent(D->getType(),
2245 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002246 // FIXME: Actually try to merge the body and other attributes.
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002247 const FunctionDecl *FromBodyDecl = nullptr;
2248 D->hasBody(FromBodyDecl);
2249 if (D == FromBodyDecl && !FoundFunction->hasBody()) {
2250 // This function is needed to merge completely.
2251 FoundWithoutBody = FoundFunction;
2252 break;
2253 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002254 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002255 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002256
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002257 // FIXME: Check for overloading more carefully, e.g., by boosting
2258 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002259
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002260 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002261 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002262 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002263
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002264 // Complain about inconsistent function types.
2265 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002266 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002267 Importer.ToDiag(FoundFunction->getLocation(),
2268 diag::note_odr_value_here)
2269 << FoundFunction->getType();
2270 }
2271 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002272
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002273 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002274 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002275
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002276 if (!ConflictingDecls.empty()) {
2277 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2278 ConflictingDecls.data(),
2279 ConflictingDecls.size());
2280 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002281 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002282 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002283 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002284
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002285 DeclarationNameInfo NameInfo(Name, Loc);
2286 // Import additional name location/type info.
2287 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2288
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002289 QualType FromTy = D->getType();
2290 bool usedDifferentExceptionSpec = false;
2291
2292 if (const FunctionProtoType *
2293 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2294 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2295 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2296 // FunctionDecl that we are importing the FunctionProtoType for.
2297 // To avoid an infinite recursion when importing, create the FunctionDecl
2298 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002299 if (FromEPI.ExceptionSpec.SourceDecl ||
2300 FromEPI.ExceptionSpec.SourceTemplate ||
2301 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002302 FunctionProtoType::ExtProtoInfo DefaultEPI;
2303 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002304 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002305 usedDifferentExceptionSpec = true;
2306 }
2307 }
2308
Douglas Gregorb4964f72010-02-15 23:54:17 +00002309 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002310 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002311 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002312 return nullptr;
2313
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002314 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002315 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00002316 for (auto P : D->parameters()) {
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00002317 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002318 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002319 return nullptr;
2320
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002321 Parameters.push_back(ToP);
2322 }
2323
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002324 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002325 if (D->getTypeSourceInfo() && !TInfo)
2326 return nullptr;
2327
2328 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00002329 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002330 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00002331 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2332 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2333 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002334 InnerLocStart,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002335 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002336 FromConstructor->isExplicit(),
2337 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002338 D->isImplicit(),
2339 D->isConstexpr());
Sean Callanandd2c1742016-05-16 20:48:03 +00002340 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
2341 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
2342 for (CXXCtorInitializer *I : FromConstructor->inits()) {
2343 CXXCtorInitializer *ToI =
2344 cast_or_null<CXXCtorInitializer>(Importer.Import(I));
2345 if (!ToI && I)
2346 return nullptr;
2347 CtorInitializers.push_back(ToI);
2348 }
2349 CXXCtorInitializer **Memory =
2350 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
2351 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
2352 CXXConstructorDecl *ToCtor = llvm::cast<CXXConstructorDecl>(ToFunction);
2353 ToCtor->setCtorInitializers(Memory);
2354 ToCtor->setNumCtorInitializers(NumInitializers);
2355 }
Douglas Gregor00eace12010-02-21 18:29:16 +00002356 } else if (isa<CXXDestructorDecl>(D)) {
2357 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2358 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002359 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002360 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002361 D->isInlineSpecified(),
2362 D->isImplicit());
2363 } else if (CXXConversionDecl *FromConversion
2364 = dyn_cast<CXXConversionDecl>(D)) {
2365 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2366 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002367 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002368 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002369 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002370 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002371 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002372 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002373 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2374 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2375 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002376 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00002377 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002378 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002379 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002380 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002381 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002382 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002383 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00002384 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002385 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002386 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002387 D->hasWrittenPrototype(),
2388 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002389 }
John McCall3e11ebe2010-03-15 10:12:16 +00002390
2391 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002392 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002393 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002394 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002395 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2396 ToFunction->setTrivial(D->isTrivial());
2397 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002398 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002399
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002400 // Set the parameters.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002401 for (ParmVarDecl *Param : Parameters) {
2402 Param->setOwningFunction(ToFunction);
2403 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002404 }
David Blaikie9c70e042011-09-21 18:16:56 +00002405 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002406
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002407 if (FoundWithoutBody) {
2408 auto *Recent = const_cast<FunctionDecl *>(
2409 FoundWithoutBody->getMostRecentDecl());
2410 ToFunction->setPreviousDecl(Recent);
2411 }
2412
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002413 // We need to complete creation of FunctionProtoTypeLoc manually with setting
2414 // params it refers to.
2415 if (TInfo) {
2416 if (auto ProtoLoc =
2417 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
2418 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
2419 ProtoLoc.setParam(I, Parameters[I]);
2420 }
2421 }
2422
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002423 if (usedDifferentExceptionSpec) {
2424 // Update FunctionProtoType::ExtProtoInfo.
2425 QualType T = Importer.Import(D->getType());
2426 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002427 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002428 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002429 }
2430
Sean Callanan59721b32015-04-28 18:41:46 +00002431 // Import the body, if any.
2432 if (Stmt *FromBody = D->getBody()) {
2433 if (Stmt *ToBody = Importer.Import(FromBody)) {
2434 ToFunction->setBody(ToBody);
2435 }
2436 }
2437
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002438 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002439
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002440 // If it is a template, import all related things.
2441 if (ImportTemplateInformation(D, ToFunction))
2442 return nullptr;
2443
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002444 // Add this function to the lexical context.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002445 // NOTE: If the function is templated declaration, it should be not added into
2446 // LexicalDC. But described template is imported during import of
2447 // FunctionTemplateDecl (it happens later). So, we use source declaration
2448 // to determine if we should add the result function.
2449 if (!D->getDescribedFunctionTemplate())
2450 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002451
Lang Hames19e07e12017-06-20 21:06:00 +00002452 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2453 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2454
Douglas Gregor43f54792010-02-17 02:12:47 +00002455 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002456}
2457
Douglas Gregor00eace12010-02-21 18:29:16 +00002458Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2459 return VisitFunctionDecl(D);
2460}
2461
2462Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2463 return VisitCXXMethodDecl(D);
2464}
2465
2466Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2467 return VisitCXXMethodDecl(D);
2468}
2469
2470Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2471 return VisitCXXMethodDecl(D);
2472}
2473
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002474static unsigned getFieldIndex(Decl *F) {
2475 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2476 if (!Owner)
2477 return 0;
2478
2479 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002480 for (const auto *D : Owner->noload_decls()) {
2481 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002482 return Index;
2483
2484 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2485 ++Index;
2486 }
2487
2488 return Index;
2489}
2490
Douglas Gregor5c73e912010-02-11 00:48:18 +00002491Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2492 // Import the major distinguishing characteristics of a variable.
2493 DeclContext *DC, *LexicalDC;
2494 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002495 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002496 NamedDecl *ToD;
2497 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002498 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002499 if (ToD)
2500 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002501
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002502 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002503 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002504 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002505 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2506 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002507 // For anonymous fields, match up by index.
2508 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2509 continue;
2510
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002511 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002512 FoundField->getType())) {
2513 Importer.Imported(D, FoundField);
2514 return FoundField;
2515 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002516
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002517 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2518 << Name << D->getType() << FoundField->getType();
2519 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2520 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002521 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002522 }
2523 }
2524
Douglas Gregorb4964f72010-02-15 23:54:17 +00002525 // 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 Gregor5c73e912010-02-11 00:48:18 +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
Abramo Bagnaradff19302011-03-08 08:55:46 +00002535 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2536 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002537 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002538 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002539 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002540 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002541 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00002542 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00002543 Expr *ToInitializer = Importer.Import(FromInitializer);
2544 if (ToInitializer)
2545 ToField->setInClassInitializer(ToInitializer);
2546 else
2547 return nullptr;
2548 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002549 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002550 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002551 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002552 return ToField;
2553}
2554
Francois Pichet783dd6e2010-11-21 06:08:52 +00002555Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2556 // Import the major distinguishing characteristics of a variable.
2557 DeclContext *DC, *LexicalDC;
2558 DeclarationName Name;
2559 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002560 NamedDecl *ToD;
2561 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002562 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002563 if (ToD)
2564 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002565
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002566 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002567 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002568 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002569 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002570 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002571 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002572 // For anonymous indirect fields, match up by index.
2573 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2574 continue;
2575
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002576 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002577 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002578 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002579 Importer.Imported(D, FoundField);
2580 return FoundField;
2581 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002582
2583 // If there are more anonymous fields to check, continue.
2584 if (!Name && I < N-1)
2585 continue;
2586
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002587 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2588 << Name << D->getType() << FoundField->getType();
2589 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2590 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002591 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002592 }
2593 }
2594
Francois Pichet783dd6e2010-11-21 06:08:52 +00002595 // Import the type.
2596 QualType T = Importer.Import(D->getType());
2597 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002598 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002599
2600 NamedDecl **NamedChain =
2601 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2602
2603 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00002604 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00002605 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002606 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00002607 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002608 NamedChain[i++] = cast<NamedDecl>(D);
2609 }
2610
2611 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00002612 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
David Majnemer59f77922016-06-24 04:05:48 +00002613 {NamedChain, D->getChainingSize()});
Aaron Ballman260995b2014-10-15 16:58:18 +00002614
2615 for (const auto *Attr : D->attrs())
2616 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
2617
Francois Pichet783dd6e2010-11-21 06:08:52 +00002618 ToIndirectField->setAccess(D->getAccess());
2619 ToIndirectField->setLexicalDeclContext(LexicalDC);
2620 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002621 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002622 return ToIndirectField;
2623}
2624
Aleksei Sidorina693b372016-09-28 10:16:56 +00002625Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2626 // Import the major distinguishing characteristics of a declaration.
2627 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2628 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2629 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2630 if (!DC || !LexicalDC)
2631 return nullptr;
2632
2633 // Determine whether we've already imported this decl.
2634 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2635 auto *RD = cast<CXXRecordDecl>(DC);
2636 FriendDecl *ImportedFriend = RD->getFirstFriend();
2637 StructuralEquivalenceContext Context(
2638 Importer.getFromContext(), Importer.getToContext(),
2639 Importer.getNonEquivalentDecls(), false, false);
2640
2641 while (ImportedFriend) {
2642 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2643 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2644 ImportedFriend->getFriendDecl()))
2645 return Importer.Imported(D, ImportedFriend);
2646
2647 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2648 if (Importer.IsStructurallyEquivalent(
2649 D->getFriendType()->getType(),
2650 ImportedFriend->getFriendType()->getType(), true))
2651 return Importer.Imported(D, ImportedFriend);
2652 }
2653 ImportedFriend = ImportedFriend->getNextFriend();
2654 }
2655
2656 // Not found. Create it.
2657 FriendDecl::FriendUnion ToFU;
2658 if (NamedDecl *FriendD = D->getFriendDecl())
2659 ToFU = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2660 else
2661 ToFU = Importer.Import(D->getFriendType());
2662 if (!ToFU)
2663 return nullptr;
2664
2665 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
2666 TemplateParameterList **FromTPLists =
2667 D->getTrailingObjects<TemplateParameterList *>();
2668 for (unsigned I = 0; I < D->NumTPLists; I++) {
2669 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2670 if (!List)
2671 return nullptr;
2672 ToTPLists[I] = List;
2673 }
2674
2675 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
2676 Importer.Import(D->getLocation()),
2677 ToFU, Importer.Import(D->getFriendLoc()),
2678 ToTPLists);
2679
2680 Importer.Imported(D, FrD);
2681 RD->pushFriendDecl(FrD);
2682
2683 FrD->setAccess(D->getAccess());
2684 FrD->setLexicalDeclContext(LexicalDC);
2685 LexicalDC->addDeclInternal(FrD);
2686 return FrD;
2687}
2688
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002689Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2690 // Import the major distinguishing characteristics of an ivar.
2691 DeclContext *DC, *LexicalDC;
2692 DeclarationName Name;
2693 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002694 NamedDecl *ToD;
2695 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002696 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002697 if (ToD)
2698 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002699
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002700 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002701 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002702 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002703 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2704 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002705 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002706 FoundIvar->getType())) {
2707 Importer.Imported(D, FoundIvar);
2708 return FoundIvar;
2709 }
2710
2711 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2712 << Name << D->getType() << FoundIvar->getType();
2713 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2714 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002715 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002716 }
2717 }
2718
2719 // Import the type.
2720 QualType T = Importer.Import(D->getType());
2721 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002722 return nullptr;
2723
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002724 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2725 Expr *BitWidth = Importer.Import(D->getBitWidth());
2726 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002727 return nullptr;
2728
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002729 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2730 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002731 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002732 Loc, Name.getAsIdentifierInfo(),
2733 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00002734 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002735 ToIvar->setLexicalDeclContext(LexicalDC);
2736 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002737 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002738 return ToIvar;
2739
2740}
2741
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002742Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2743 // Import the major distinguishing characteristics of a variable.
2744 DeclContext *DC, *LexicalDC;
2745 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002746 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002747 NamedDecl *ToD;
2748 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002749 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002750 if (ToD)
2751 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002752
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002753 // Try to find a variable in our own ("to") context with the same name and
2754 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002755 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00002756 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002757 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002758 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002759 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002760 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002761 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2762 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002763 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002764
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002765 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002766 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00002767 if (FoundVar->hasExternalFormalLinkage() &&
2768 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002769 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002770 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002771 MergeWithVar = FoundVar;
2772 break;
2773 }
2774
Douglas Gregor56521c52010-02-12 17:23:39 +00002775 const ArrayType *FoundArray
2776 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2777 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002778 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002779 if (FoundArray && TArray) {
2780 if (isa<IncompleteArrayType>(FoundArray) &&
2781 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002782 // Import the type.
2783 QualType T = Importer.Import(D->getType());
2784 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002785 return nullptr;
2786
Douglas Gregor56521c52010-02-12 17:23:39 +00002787 FoundVar->setType(T);
2788 MergeWithVar = FoundVar;
2789 break;
2790 } else if (isa<IncompleteArrayType>(TArray) &&
2791 isa<ConstantArrayType>(FoundArray)) {
2792 MergeWithVar = FoundVar;
2793 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002794 }
2795 }
2796
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002797 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002798 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002799 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2800 << FoundVar->getType();
2801 }
2802 }
2803
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002804 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002805 }
2806
2807 if (MergeWithVar) {
2808 // An equivalent variable with external linkage has been found. Link
2809 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002810 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002811
2812 if (VarDecl *DDef = D->getDefinition()) {
2813 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2814 Importer.ToDiag(ExistingDef->getLocation(),
2815 diag::err_odr_variable_multiple_def)
2816 << Name;
2817 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2818 } else {
2819 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002820 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002821 if (DDef->isInitKnownICE()) {
2822 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2823 Eval->CheckedICE = true;
2824 Eval->IsICE = DDef->isInitICE();
2825 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002826 }
2827 }
2828
2829 return MergeWithVar;
2830 }
2831
2832 if (!ConflictingDecls.empty()) {
2833 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2834 ConflictingDecls.data(),
2835 ConflictingDecls.size());
2836 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002837 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002838 }
2839 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002840
Douglas Gregorb4964f72010-02-15 23:54:17 +00002841 // Import the type.
2842 QualType T = Importer.Import(D->getType());
2843 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002844 return nullptr;
2845
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002846 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002847 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002848 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2849 Importer.Import(D->getInnerLocStart()),
2850 Loc, Name.getAsIdentifierInfo(),
2851 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002852 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00002853 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002854 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002855 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002856 Importer.Imported(D, ToVar);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002857
2858 // Templated declarations should never appear in the enclosing DeclContext.
2859 if (!D->getDescribedVarTemplate())
2860 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002861
Sean Callanan59721b32015-04-28 18:41:46 +00002862 if (!D->isFileVarDecl() &&
2863 D->isUsed())
2864 ToVar->setIsUsed();
2865
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002866 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002867 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00002868 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002869
Aleksei Sidorin855086d2017-01-23 09:30:36 +00002870 if (D->isConstexpr())
2871 ToVar->setConstexpr(true);
2872
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002873 return ToVar;
2874}
2875
Douglas Gregor8b228d72010-02-17 21:22:52 +00002876Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2877 // Parameters are created in the translation unit's context, then moved
2878 // into the function declaration's context afterward.
2879 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2880
2881 // Import the name of this declaration.
2882 DeclarationName Name = Importer.Import(D->getDeclName());
2883 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002884 return nullptr;
2885
Douglas Gregor8b228d72010-02-17 21:22:52 +00002886 // Import the location of this declaration.
2887 SourceLocation Loc = Importer.Import(D->getLocation());
2888
2889 // Import the parameter's type.
2890 QualType T = Importer.Import(D->getType());
2891 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002892 return nullptr;
2893
Douglas Gregor8b228d72010-02-17 21:22:52 +00002894 // Create the imported parameter.
Alexey Bataev56223232017-06-09 13:40:18 +00002895 auto *ToParm = ImplicitParamDecl::Create(Importer.getToContext(), DC, Loc,
2896 Name.getAsIdentifierInfo(), T,
2897 D->getParameterKind());
Douglas Gregor8b228d72010-02-17 21:22:52 +00002898 return Importer.Imported(D, ToParm);
2899}
2900
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002901Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2902 // Parameters are created in the translation unit's context, then moved
2903 // into the function declaration's context afterward.
2904 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2905
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002906 // Import the name of this declaration.
2907 DeclarationName Name = Importer.Import(D->getDeclName());
2908 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002909 return nullptr;
2910
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002911 // Import the location of this declaration.
2912 SourceLocation Loc = Importer.Import(D->getLocation());
2913
2914 // Import the parameter's type.
2915 QualType T = Importer.Import(D->getType());
2916 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002917 return nullptr;
2918
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002919 // Create the imported parameter.
2920 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2921 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002922 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002923 Loc, Name.getAsIdentifierInfo(),
2924 T, TInfo, D->getStorageClass(),
Aleksei Sidorin55a63502017-02-20 11:57:12 +00002925 /*DefaultArg*/ nullptr);
2926
2927 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00002928 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00002929 ToParm->setKNRPromoted(D->isKNRPromoted());
2930
2931 Expr *ToDefArg = nullptr;
2932 Expr *FromDefArg = nullptr;
2933 if (D->hasUninstantiatedDefaultArg()) {
2934 FromDefArg = D->getUninstantiatedDefaultArg();
2935 ToDefArg = Importer.Import(FromDefArg);
2936 ToParm->setUninstantiatedDefaultArg(ToDefArg);
2937 } else if (D->hasUnparsedDefaultArg()) {
2938 ToParm->setUnparsedDefaultArg();
2939 } else if (D->hasDefaultArg()) {
2940 FromDefArg = D->getDefaultArg();
2941 ToDefArg = Importer.Import(FromDefArg);
2942 ToParm->setDefaultArg(ToDefArg);
2943 }
2944 if (FromDefArg && !ToDefArg)
2945 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002946
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002947 if (D->isObjCMethodParameter()) {
2948 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
2949 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
2950 } else {
2951 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
2952 D->getFunctionScopeIndex());
2953 }
2954
Sean Callanan59721b32015-04-28 18:41:46 +00002955 if (D->isUsed())
2956 ToParm->setIsUsed();
2957
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002958 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002959}
2960
Douglas Gregor43f54792010-02-17 02:12:47 +00002961Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2962 // Import the major distinguishing characteristics of a method.
2963 DeclContext *DC, *LexicalDC;
2964 DeclarationName Name;
2965 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002966 NamedDecl *ToD;
2967 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002968 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002969 if (ToD)
2970 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002971
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002972 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002973 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002974 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2975 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002976 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2977 continue;
2978
2979 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00002980 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
2981 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002982 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00002983 << D->isInstanceMethod() << Name << D->getReturnType()
2984 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00002985 Importer.ToDiag(FoundMethod->getLocation(),
2986 diag::note_odr_objc_method_here)
2987 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002988 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002989 }
2990
2991 // Check the number of parameters.
2992 if (D->param_size() != FoundMethod->param_size()) {
2993 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2994 << D->isInstanceMethod() << Name
2995 << D->param_size() << FoundMethod->param_size();
2996 Importer.ToDiag(FoundMethod->getLocation(),
2997 diag::note_odr_objc_method_here)
2998 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002999 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003000 }
3001
3002 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003003 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003004 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3005 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003006 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003007 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003008 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003009 diag::err_odr_objc_method_param_type_inconsistent)
3010 << D->isInstanceMethod() << Name
3011 << (*P)->getType() << (*FoundP)->getType();
3012 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3013 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003014 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003015 }
3016 }
3017
3018 // Check variadic/non-variadic.
3019 // Check the number of parameters.
3020 if (D->isVariadic() != FoundMethod->isVariadic()) {
3021 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3022 << D->isInstanceMethod() << Name;
3023 Importer.ToDiag(FoundMethod->getLocation(),
3024 diag::note_odr_objc_method_here)
3025 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003026 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003027 }
3028
3029 // FIXME: Any other bits we need to merge?
3030 return Importer.Imported(D, FoundMethod);
3031 }
3032 }
3033
3034 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003035 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003036 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003037 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003038
Alp Toker314cc812014-01-25 16:55:45 +00003039 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003040
Alp Toker314cc812014-01-25 16:55:45 +00003041 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3042 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3043 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3044 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3045 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003046
3047 // FIXME: When we decide to merge method definitions, we'll need to
3048 // deal with implicit parameters.
3049
3050 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003051 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003052 for (auto *FromP : D->parameters()) {
Aaron Ballman43b68be2014-03-07 17:50:17 +00003053 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003054 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003055 return nullptr;
3056
Douglas Gregor43f54792010-02-17 02:12:47 +00003057 ToParams.push_back(ToP);
3058 }
3059
3060 // Set the parameters.
3061 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3062 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003063 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003064 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003065
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003066 SmallVector<SourceLocation, 12> SelLocs;
3067 D->getSelectorLocs(SelLocs);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003068 for (SourceLocation &Loc : SelLocs)
3069 Loc = Importer.Import(Loc);
3070
3071 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003072
3073 ToMethod->setLexicalDeclContext(LexicalDC);
3074 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003075 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003076 return ToMethod;
3077}
3078
Douglas Gregor85f3f952015-07-07 03:57:15 +00003079Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3080 // Import the major distinguishing characteristics of a category.
3081 DeclContext *DC, *LexicalDC;
3082 DeclarationName Name;
3083 SourceLocation Loc;
3084 NamedDecl *ToD;
3085 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3086 return nullptr;
3087 if (ToD)
3088 return ToD;
3089
3090 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3091 if (!BoundInfo)
3092 return nullptr;
3093
3094 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3095 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003096 D->getVariance(),
3097 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003098 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003099 Importer.Import(D->getLocation()),
3100 Name.getAsIdentifierInfo(),
3101 Importer.Import(D->getColonLoc()),
3102 BoundInfo);
3103 Importer.Imported(D, Result);
3104 Result->setLexicalDeclContext(LexicalDC);
3105 return Result;
3106}
3107
Douglas Gregor84c51c32010-02-18 01:47:50 +00003108Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3109 // Import the major distinguishing characteristics of a category.
3110 DeclContext *DC, *LexicalDC;
3111 DeclarationName Name;
3112 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003113 NamedDecl *ToD;
3114 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003115 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003116 if (ToD)
3117 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003118
Douglas Gregor84c51c32010-02-18 01:47:50 +00003119 ObjCInterfaceDecl *ToInterface
3120 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3121 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003122 return nullptr;
3123
Douglas Gregor84c51c32010-02-18 01:47:50 +00003124 // Determine if we've already encountered this category.
3125 ObjCCategoryDecl *MergeWithCategory
3126 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3127 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3128 if (!ToCategory) {
3129 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003130 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003131 Loc,
3132 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003133 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003134 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003135 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003136 Importer.Import(D->getIvarLBraceLoc()),
3137 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003138 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003139 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003140 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003141 // Import the type parameter list after calling Imported, to avoid
3142 // loops when bringing in their DeclContext.
3143 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3144 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003145
Douglas Gregor84c51c32010-02-18 01:47:50 +00003146 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003147 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3148 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003149 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3150 = D->protocol_loc_begin();
3151 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3152 FromProtoEnd = D->protocol_end();
3153 FromProto != FromProtoEnd;
3154 ++FromProto, ++FromProtoLoc) {
3155 ObjCProtocolDecl *ToProto
3156 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3157 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003158 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003159 Protocols.push_back(ToProto);
3160 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3161 }
3162
3163 // FIXME: If we're merging, make sure that the protocol list is the same.
3164 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3165 ProtocolLocs.data(), Importer.getToContext());
3166
3167 } else {
3168 Importer.Imported(D, ToCategory);
3169 }
3170
3171 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003172 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003173
3174 // If we have an implementation, import it as well.
3175 if (D->getImplementation()) {
3176 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003177 = cast_or_null<ObjCCategoryImplDecl>(
3178 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003179 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003180 return nullptr;
3181
Douglas Gregor84c51c32010-02-18 01:47:50 +00003182 ToCategory->setImplementation(Impl);
3183 }
3184
3185 return ToCategory;
3186}
3187
Douglas Gregor2aa53772012-01-24 17:42:07 +00003188bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3189 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003190 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003191 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003192 if (shouldForceImportDeclContext(Kind))
3193 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003194 return false;
3195 }
3196
3197 // Start the protocol definition
3198 To->startDefinition();
3199
3200 // Import protocols
3201 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3202 SmallVector<SourceLocation, 4> ProtocolLocs;
3203 ObjCProtocolDecl::protocol_loc_iterator
3204 FromProtoLoc = From->protocol_loc_begin();
3205 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3206 FromProtoEnd = From->protocol_end();
3207 FromProto != FromProtoEnd;
3208 ++FromProto, ++FromProtoLoc) {
3209 ObjCProtocolDecl *ToProto
3210 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3211 if (!ToProto)
3212 return true;
3213 Protocols.push_back(ToProto);
3214 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3215 }
3216
3217 // FIXME: If we're merging, make sure that the protocol list is the same.
3218 To->setProtocolList(Protocols.data(), Protocols.size(),
3219 ProtocolLocs.data(), Importer.getToContext());
3220
Douglas Gregor2e15c842012-02-01 21:00:38 +00003221 if (shouldForceImportDeclContext(Kind)) {
3222 // Import all of the members of this protocol.
3223 ImportDeclContext(From, /*ForceImport=*/true);
3224 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003225 return false;
3226}
3227
Douglas Gregor98d156a2010-02-17 16:12:00 +00003228Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003229 // If this protocol has a definition in the translation unit we're coming
3230 // from, but this particular declaration is not that definition, import the
3231 // definition and map to that.
3232 ObjCProtocolDecl *Definition = D->getDefinition();
3233 if (Definition && Definition != D) {
3234 Decl *ImportedDef = Importer.Import(Definition);
3235 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003236 return nullptr;
3237
Douglas Gregor2aa53772012-01-24 17:42:07 +00003238 return Importer.Imported(D, ImportedDef);
3239 }
3240
Douglas Gregor84c51c32010-02-18 01:47:50 +00003241 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003242 DeclContext *DC, *LexicalDC;
3243 DeclarationName Name;
3244 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003245 NamedDecl *ToD;
3246 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003247 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003248 if (ToD)
3249 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003250
Craig Topper36250ad2014-05-12 05:36:57 +00003251 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003252 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003253 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003254 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3255 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003256 continue;
3257
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003258 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003259 break;
3260 }
3261
3262 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003263 if (!ToProto) {
3264 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3265 Name.getAsIdentifierInfo(), Loc,
3266 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003267 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003268 ToProto->setLexicalDeclContext(LexicalDC);
3269 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003270 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003271
3272 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003273
Douglas Gregor2aa53772012-01-24 17:42:07 +00003274 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003275 return nullptr;
3276
Douglas Gregor98d156a2010-02-17 16:12:00 +00003277 return ToProto;
3278}
3279
Sean Callanan0aae0412014-12-10 00:00:37 +00003280Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3281 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3282 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3283
3284 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3285 SourceLocation LangLoc = Importer.Import(D->getLocation());
3286
3287 bool HasBraces = D->hasBraces();
3288
Sean Callananb12a8552014-12-10 21:22:20 +00003289 LinkageSpecDecl *ToLinkageSpec =
3290 LinkageSpecDecl::Create(Importer.getToContext(),
3291 DC,
3292 ExternLoc,
3293 LangLoc,
3294 D->getLanguage(),
3295 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003296
3297 if (HasBraces) {
3298 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3299 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3300 }
3301
3302 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3303 LexicalDC->addDeclInternal(ToLinkageSpec);
3304
3305 Importer.Imported(D, ToLinkageSpec);
3306
3307 return ToLinkageSpec;
3308}
3309
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003310Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
3311 DeclContext *DC, *LexicalDC;
3312 DeclarationName Name;
3313 SourceLocation Loc;
3314 NamedDecl *ToD = nullptr;
3315 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3316 return nullptr;
3317 if (ToD)
3318 return ToD;
3319
3320 DeclarationNameInfo NameInfo(Name,
3321 Importer.Import(D->getNameInfo().getLoc()));
3322 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3323
3324 UsingDecl *ToUsing = UsingDecl::Create(Importer.getToContext(), DC,
3325 Importer.Import(D->getUsingLoc()),
3326 Importer.Import(D->getQualifierLoc()),
3327 NameInfo, D->hasTypename());
3328 ToUsing->setLexicalDeclContext(LexicalDC);
3329 LexicalDC->addDeclInternal(ToUsing);
3330 Importer.Imported(D, ToUsing);
3331
3332 if (NamedDecl *FromPattern =
3333 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
3334 if (NamedDecl *ToPattern =
3335 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern)))
3336 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern);
3337 else
3338 return nullptr;
3339 }
3340
3341 for (UsingShadowDecl *FromShadow : D->shadows()) {
3342 if (UsingShadowDecl *ToShadow =
3343 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow)))
3344 ToUsing->addShadowDecl(ToShadow);
3345 else
3346 // FIXME: We return a nullptr here but the definition is already created
3347 // and available with lookups. How to fix this?..
3348 return nullptr;
3349 }
3350 return ToUsing;
3351}
3352
3353Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
3354 DeclContext *DC, *LexicalDC;
3355 DeclarationName Name;
3356 SourceLocation Loc;
3357 NamedDecl *ToD = nullptr;
3358 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3359 return nullptr;
3360 if (ToD)
3361 return ToD;
3362
3363 UsingDecl *ToUsing = dyn_cast_or_null<UsingDecl>(
3364 Importer.Import(D->getUsingDecl()));
3365 if (!ToUsing)
3366 return nullptr;
3367
3368 NamedDecl *ToTarget = dyn_cast_or_null<NamedDecl>(
3369 Importer.Import(D->getTargetDecl()));
3370 if (!ToTarget)
3371 return nullptr;
3372
3373 UsingShadowDecl *ToShadow = UsingShadowDecl::Create(
3374 Importer.getToContext(), DC, Loc, ToUsing, ToTarget);
3375
3376 ToShadow->setLexicalDeclContext(LexicalDC);
3377 ToShadow->setAccess(D->getAccess());
3378 Importer.Imported(D, ToShadow);
3379
3380 if (UsingShadowDecl *FromPattern =
3381 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
3382 if (UsingShadowDecl *ToPattern =
3383 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern)))
3384 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow,
3385 ToPattern);
3386 else
3387 // FIXME: We return a nullptr here but the definition is already created
3388 // and available with lookups. How to fix this?..
3389 return nullptr;
3390 }
3391
3392 LexicalDC->addDeclInternal(ToShadow);
3393
3394 return ToShadow;
3395}
3396
3397
3398Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
3399 DeclContext *DC, *LexicalDC;
3400 DeclarationName Name;
3401 SourceLocation Loc;
3402 NamedDecl *ToD = nullptr;
3403 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3404 return nullptr;
3405 if (ToD)
3406 return ToD;
3407
3408 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor());
3409 if (!ToComAncestor)
3410 return nullptr;
3411
3412 NamespaceDecl *ToNominated = cast_or_null<NamespaceDecl>(
3413 Importer.Import(D->getNominatedNamespace()));
3414 if (!ToNominated)
3415 return nullptr;
3416
3417 UsingDirectiveDecl *ToUsingDir = UsingDirectiveDecl::Create(
3418 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3419 Importer.Import(D->getNamespaceKeyLocation()),
3420 Importer.Import(D->getQualifierLoc()),
3421 Importer.Import(D->getIdentLocation()), ToNominated, ToComAncestor);
3422 ToUsingDir->setLexicalDeclContext(LexicalDC);
3423 LexicalDC->addDeclInternal(ToUsingDir);
3424 Importer.Imported(D, ToUsingDir);
3425
3426 return ToUsingDir;
3427}
3428
3429Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl(
3430 UnresolvedUsingValueDecl *D) {
3431 DeclContext *DC, *LexicalDC;
3432 DeclarationName Name;
3433 SourceLocation Loc;
3434 NamedDecl *ToD = nullptr;
3435 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3436 return nullptr;
3437 if (ToD)
3438 return ToD;
3439
3440 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc()));
3441 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3442
3443 UnresolvedUsingValueDecl *ToUsingValue = UnresolvedUsingValueDecl::Create(
3444 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3445 Importer.Import(D->getQualifierLoc()), NameInfo,
3446 Importer.Import(D->getEllipsisLoc()));
3447
3448 Importer.Imported(D, ToUsingValue);
3449 ToUsingValue->setAccess(D->getAccess());
3450 ToUsingValue->setLexicalDeclContext(LexicalDC);
3451 LexicalDC->addDeclInternal(ToUsingValue);
3452
3453 return ToUsingValue;
3454}
3455
3456Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
3457 UnresolvedUsingTypenameDecl *D) {
3458 DeclContext *DC, *LexicalDC;
3459 DeclarationName Name;
3460 SourceLocation Loc;
3461 NamedDecl *ToD = nullptr;
3462 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3463 return nullptr;
3464 if (ToD)
3465 return ToD;
3466
3467 UnresolvedUsingTypenameDecl *ToUsing = UnresolvedUsingTypenameDecl::Create(
3468 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3469 Importer.Import(D->getTypenameLoc()),
3470 Importer.Import(D->getQualifierLoc()), Loc, Name,
3471 Importer.Import(D->getEllipsisLoc()));
3472
3473 Importer.Imported(D, ToUsing);
3474 ToUsing->setAccess(D->getAccess());
3475 ToUsing->setLexicalDeclContext(LexicalDC);
3476 LexicalDC->addDeclInternal(ToUsing);
3477
3478 return ToUsing;
3479}
3480
3481
Douglas Gregor2aa53772012-01-24 17:42:07 +00003482bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3483 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003484 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003485 if (To->getDefinition()) {
3486 // Check consistency of superclass.
3487 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3488 if (FromSuper) {
3489 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3490 if (!FromSuper)
3491 return true;
3492 }
3493
3494 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3495 if ((bool)FromSuper != (bool)ToSuper ||
3496 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3497 Importer.ToDiag(To->getLocation(),
3498 diag::err_odr_objc_superclass_inconsistent)
3499 << To->getDeclName();
3500 if (ToSuper)
3501 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3502 << To->getSuperClass()->getDeclName();
3503 else
3504 Importer.ToDiag(To->getLocation(),
3505 diag::note_odr_objc_missing_superclass);
3506 if (From->getSuperClass())
3507 Importer.FromDiag(From->getSuperClassLoc(),
3508 diag::note_odr_objc_superclass)
3509 << From->getSuperClass()->getDeclName();
3510 else
3511 Importer.FromDiag(From->getLocation(),
3512 diag::note_odr_objc_missing_superclass);
3513 }
3514
Douglas Gregor2e15c842012-02-01 21:00:38 +00003515 if (shouldForceImportDeclContext(Kind))
3516 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003517 return false;
3518 }
3519
3520 // Start the definition.
3521 To->startDefinition();
3522
3523 // If this class has a superclass, import it.
3524 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003525 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3526 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003527 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003528
3529 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003530 }
3531
3532 // Import protocols
3533 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3534 SmallVector<SourceLocation, 4> ProtocolLocs;
3535 ObjCInterfaceDecl::protocol_loc_iterator
3536 FromProtoLoc = From->protocol_loc_begin();
3537
3538 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3539 FromProtoEnd = From->protocol_end();
3540 FromProto != FromProtoEnd;
3541 ++FromProto, ++FromProtoLoc) {
3542 ObjCProtocolDecl *ToProto
3543 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3544 if (!ToProto)
3545 return true;
3546 Protocols.push_back(ToProto);
3547 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3548 }
3549
3550 // FIXME: If we're merging, make sure that the protocol list is the same.
3551 To->setProtocolList(Protocols.data(), Protocols.size(),
3552 ProtocolLocs.data(), Importer.getToContext());
3553
3554 // Import categories. When the categories themselves are imported, they'll
3555 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003556 for (auto *Cat : From->known_categories())
3557 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003558
Douglas Gregor2aa53772012-01-24 17:42:07 +00003559 // If we have an @implementation, import it as well.
3560 if (From->getImplementation()) {
3561 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3562 Importer.Import(From->getImplementation()));
3563 if (!Impl)
3564 return true;
3565
3566 To->setImplementation(Impl);
3567 }
3568
Douglas Gregor2e15c842012-02-01 21:00:38 +00003569 if (shouldForceImportDeclContext(Kind)) {
3570 // Import all of the members of this class.
3571 ImportDeclContext(From, /*ForceImport=*/true);
3572 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003573 return false;
3574}
3575
Douglas Gregor85f3f952015-07-07 03:57:15 +00003576ObjCTypeParamList *
3577ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3578 if (!list)
3579 return nullptr;
3580
3581 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3582 for (auto fromTypeParam : *list) {
3583 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3584 Importer.Import(fromTypeParam));
3585 if (!toTypeParam)
3586 return nullptr;
3587
3588 toTypeParams.push_back(toTypeParam);
3589 }
3590
3591 return ObjCTypeParamList::create(Importer.getToContext(),
3592 Importer.Import(list->getLAngleLoc()),
3593 toTypeParams,
3594 Importer.Import(list->getRAngleLoc()));
3595}
3596
Douglas Gregor45635322010-02-16 01:20:57 +00003597Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003598 // If this class has a definition in the translation unit we're coming from,
3599 // but this particular declaration is not that definition, import the
3600 // definition and map to that.
3601 ObjCInterfaceDecl *Definition = D->getDefinition();
3602 if (Definition && Definition != D) {
3603 Decl *ImportedDef = Importer.Import(Definition);
3604 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003605 return nullptr;
3606
Douglas Gregor2aa53772012-01-24 17:42:07 +00003607 return Importer.Imported(D, ImportedDef);
3608 }
3609
Douglas Gregor45635322010-02-16 01:20:57 +00003610 // Import the major distinguishing characteristics of an @interface.
3611 DeclContext *DC, *LexicalDC;
3612 DeclarationName Name;
3613 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003614 NamedDecl *ToD;
3615 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003616 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003617 if (ToD)
3618 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003619
Douglas Gregor2aa53772012-01-24 17:42:07 +00003620 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003621 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003622 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003623 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003624 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3625 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003626 continue;
3627
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003628 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003629 break;
3630 }
3631
Douglas Gregor2aa53772012-01-24 17:42:07 +00003632 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003633 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003634 if (!ToIface) {
3635 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3636 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003637 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003638 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00003639 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003640 D->isImplicitInterfaceDecl());
3641 ToIface->setLexicalDeclContext(LexicalDC);
3642 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003643 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003644 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003645 // Import the type parameter list after calling Imported, to avoid
3646 // loops when bringing in their DeclContext.
3647 ToIface->setTypeParamList(ImportObjCTypeParamList(
3648 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003649
Douglas Gregor2aa53772012-01-24 17:42:07 +00003650 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003651 return nullptr;
3652
Douglas Gregor98d156a2010-02-17 16:12:00 +00003653 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003654}
3655
Douglas Gregor4da9d682010-12-07 15:32:12 +00003656Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3657 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3658 Importer.Import(D->getCategoryDecl()));
3659 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003660 return nullptr;
3661
Douglas Gregor4da9d682010-12-07 15:32:12 +00003662 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3663 if (!ToImpl) {
3664 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3665 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003666 return nullptr;
3667
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003668 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003669 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003670 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003671 Category->getClassInterface(),
3672 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003673 Importer.Import(D->getAtStartLoc()),
3674 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003675
3676 DeclContext *LexicalDC = DC;
3677 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3678 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3679 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003680 return nullptr;
3681
Douglas Gregor4da9d682010-12-07 15:32:12 +00003682 ToImpl->setLexicalDeclContext(LexicalDC);
3683 }
3684
Sean Callanan95e74be2011-10-21 02:57:43 +00003685 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003686 Category->setImplementation(ToImpl);
3687 }
3688
3689 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003690 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003691 return ToImpl;
3692}
3693
Douglas Gregorda8025c2010-12-07 01:26:03 +00003694Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3695 // Find the corresponding interface.
3696 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3697 Importer.Import(D->getClassInterface()));
3698 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003699 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003700
3701 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003702 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003703 if (D->getSuperClass()) {
3704 Super = cast_or_null<ObjCInterfaceDecl>(
3705 Importer.Import(D->getSuperClass()));
3706 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003707 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003708 }
3709
3710 ObjCImplementationDecl *Impl = Iface->getImplementation();
3711 if (!Impl) {
3712 // We haven't imported an implementation yet. Create a new @implementation
3713 // now.
3714 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3715 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003716 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003717 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003718 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003719 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003720 Importer.Import(D->getIvarLBraceLoc()),
3721 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003722
3723 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3724 DeclContext *LexicalDC
3725 = Importer.ImportContext(D->getLexicalDeclContext());
3726 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003727 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003728 Impl->setLexicalDeclContext(LexicalDC);
3729 }
3730
3731 // Associate the implementation with the class it implements.
3732 Iface->setImplementation(Impl);
3733 Importer.Imported(D, Iface->getImplementation());
3734 } else {
3735 Importer.Imported(D, Iface->getImplementation());
3736
3737 // Verify that the existing @implementation has the same superclass.
3738 if ((Super && !Impl->getSuperClass()) ||
3739 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003740 (Super && Impl->getSuperClass() &&
3741 !declaresSameEntity(Super->getCanonicalDecl(),
3742 Impl->getSuperClass()))) {
3743 Importer.ToDiag(Impl->getLocation(),
3744 diag::err_odr_objc_superclass_inconsistent)
3745 << Iface->getDeclName();
3746 // FIXME: It would be nice to have the location of the superclass
3747 // below.
3748 if (Impl->getSuperClass())
3749 Importer.ToDiag(Impl->getLocation(),
3750 diag::note_odr_objc_superclass)
3751 << Impl->getSuperClass()->getDeclName();
3752 else
3753 Importer.ToDiag(Impl->getLocation(),
3754 diag::note_odr_objc_missing_superclass);
3755 if (D->getSuperClass())
3756 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003757 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003758 << D->getSuperClass()->getDeclName();
3759 else
3760 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003761 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003762 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003763 }
3764 }
3765
3766 // Import all of the members of this @implementation.
3767 ImportDeclContext(D);
3768
3769 return Impl;
3770}
3771
Douglas Gregora11c4582010-02-17 18:02:10 +00003772Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3773 // Import the major distinguishing characteristics of an @property.
3774 DeclContext *DC, *LexicalDC;
3775 DeclarationName Name;
3776 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003777 NamedDecl *ToD;
3778 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003779 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003780 if (ToD)
3781 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00003782
3783 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003784 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003785 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003786 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003787 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003788 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003789 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003790 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00003791 FoundProp->getType())) {
3792 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3793 << Name << D->getType() << FoundProp->getType();
3794 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3795 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003796 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003797 }
3798
3799 // FIXME: Check property attributes, getters, setters, etc.?
3800
3801 // Consider these properties to be equivalent.
3802 Importer.Imported(D, FoundProp);
3803 return FoundProp;
3804 }
3805 }
3806
3807 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00003808 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3809 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00003810 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003811
3812 // Create the new property.
3813 ObjCPropertyDecl *ToProperty
3814 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3815 Name.getAsIdentifierInfo(),
3816 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003817 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00003818 Importer.Import(D->getType()),
3819 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00003820 D->getPropertyImplementation());
3821 Importer.Imported(D, ToProperty);
3822 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003823 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003824
3825 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003826 ToProperty->setPropertyAttributesAsWritten(
3827 D->getPropertyAttributesAsWritten());
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +00003828 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
3829 Importer.Import(D->getGetterNameLoc()));
3830 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
3831 Importer.Import(D->getSetterNameLoc()));
Douglas Gregora11c4582010-02-17 18:02:10 +00003832 ToProperty->setGetterMethodDecl(
3833 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3834 ToProperty->setSetterMethodDecl(
3835 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3836 ToProperty->setPropertyIvarDecl(
3837 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3838 return ToProperty;
3839}
3840
Douglas Gregor14a49e22010-12-07 18:32:03 +00003841Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3842 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3843 Importer.Import(D->getPropertyDecl()));
3844 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00003845 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003846
3847 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3848 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003849 return nullptr;
3850
Douglas Gregor14a49e22010-12-07 18:32:03 +00003851 // Import the lexical declaration context.
3852 DeclContext *LexicalDC = DC;
3853 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3854 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3855 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003856 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003857 }
3858
3859 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3860 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00003861 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003862
3863 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00003864 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003865 if (D->getPropertyIvarDecl()) {
3866 Ivar = cast_or_null<ObjCIvarDecl>(
3867 Importer.Import(D->getPropertyIvarDecl()));
3868 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00003869 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003870 }
3871
3872 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00003873 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
3874 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00003875 if (!ToImpl) {
3876 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3877 Importer.Import(D->getLocStart()),
3878 Importer.Import(D->getLocation()),
3879 Property,
3880 D->getPropertyImplementation(),
3881 Ivar,
3882 Importer.Import(D->getPropertyIvarDeclLoc()));
3883 ToImpl->setLexicalDeclContext(LexicalDC);
3884 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003885 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003886 } else {
3887 // Check that we have the same kind of property implementation (@synthesize
3888 // vs. @dynamic).
3889 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3890 Importer.ToDiag(ToImpl->getLocation(),
3891 diag::err_odr_objc_property_impl_kind_inconsistent)
3892 << Property->getDeclName()
3893 << (ToImpl->getPropertyImplementation()
3894 == ObjCPropertyImplDecl::Dynamic);
3895 Importer.FromDiag(D->getLocation(),
3896 diag::note_odr_objc_property_impl_kind)
3897 << D->getPropertyDecl()->getDeclName()
3898 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00003899 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003900 }
3901
3902 // For @synthesize, check that we have the same
3903 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3904 Ivar != ToImpl->getPropertyIvarDecl()) {
3905 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3906 diag::err_odr_objc_synthesize_ivar_inconsistent)
3907 << Property->getDeclName()
3908 << ToImpl->getPropertyIvarDecl()->getDeclName()
3909 << Ivar->getDeclName();
3910 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3911 diag::note_odr_objc_synthesize_ivar_here)
3912 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00003913 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003914 }
3915
3916 // Merge the existing implementation with the new implementation.
3917 Importer.Imported(D, ToImpl);
3918 }
3919
3920 return ToImpl;
3921}
3922
Douglas Gregora082a492010-11-30 19:14:50 +00003923Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3924 // For template arguments, we adopt the translation unit as our declaration
3925 // context. This context will be fixed when the actual template declaration
3926 // is created.
3927
3928 // FIXME: Import default argument.
3929 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3930 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003931 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003932 Importer.Import(D->getLocation()),
3933 D->getDepth(),
3934 D->getIndex(),
3935 Importer.Import(D->getIdentifier()),
3936 D->wasDeclaredWithTypename(),
3937 D->isParameterPack());
3938}
3939
3940Decl *
3941ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3942 // Import the name of this declaration.
3943 DeclarationName Name = Importer.Import(D->getDeclName());
3944 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003945 return nullptr;
3946
Douglas Gregora082a492010-11-30 19:14:50 +00003947 // Import the location of this declaration.
3948 SourceLocation Loc = Importer.Import(D->getLocation());
3949
3950 // Import the type of this declaration.
3951 QualType T = Importer.Import(D->getType());
3952 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003953 return nullptr;
3954
Douglas Gregora082a492010-11-30 19:14:50 +00003955 // Import type-source information.
3956 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3957 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00003958 return nullptr;
3959
Douglas Gregora082a492010-11-30 19:14:50 +00003960 // FIXME: Import default argument.
3961
3962 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3963 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003964 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003965 Loc, D->getDepth(), D->getPosition(),
3966 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003967 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003968}
3969
3970Decl *
3971ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3972 // Import the name of this declaration.
3973 DeclarationName Name = Importer.Import(D->getDeclName());
3974 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003975 return nullptr;
3976
Douglas Gregora082a492010-11-30 19:14:50 +00003977 // Import the location of this declaration.
3978 SourceLocation Loc = Importer.Import(D->getLocation());
3979
3980 // Import template parameters.
3981 TemplateParameterList *TemplateParams
3982 = ImportTemplateParameterList(D->getTemplateParameters());
3983 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00003984 return nullptr;
3985
Douglas Gregora082a492010-11-30 19:14:50 +00003986 // FIXME: Import default argument.
3987
3988 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3989 Importer.getToContext().getTranslationUnitDecl(),
3990 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003991 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003992 Name.getAsIdentifierInfo(),
3993 TemplateParams);
3994}
3995
3996Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3997 // If this record has a definition in the translation unit we're coming from,
3998 // but this particular declaration is not that definition, import the
3999 // definition and map to that.
4000 CXXRecordDecl *Definition
4001 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4002 if (Definition && Definition != D->getTemplatedDecl()) {
4003 Decl *ImportedDef
4004 = Importer.Import(Definition->getDescribedClassTemplate());
4005 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004006 return nullptr;
4007
Douglas Gregora082a492010-11-30 19:14:50 +00004008 return Importer.Imported(D, ImportedDef);
4009 }
4010
4011 // Import the major distinguishing characteristics of this class template.
4012 DeclContext *DC, *LexicalDC;
4013 DeclarationName Name;
4014 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004015 NamedDecl *ToD;
4016 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004017 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004018 if (ToD)
4019 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004020
Douglas Gregora082a492010-11-30 19:14:50 +00004021 // We may already have a template of the same name; try to find and match it.
4022 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004023 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004024 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004025 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004026 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4027 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004028 continue;
4029
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004030 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00004031 if (ClassTemplateDecl *FoundTemplate
4032 = dyn_cast<ClassTemplateDecl>(Found)) {
4033 if (IsStructuralMatch(D, FoundTemplate)) {
4034 // The class templates structurally match; call it the same template.
4035 // FIXME: We may be filling in a forward declaration here. Handle
4036 // this case!
4037 Importer.Imported(D->getTemplatedDecl(),
4038 FoundTemplate->getTemplatedDecl());
4039 return Importer.Imported(D, FoundTemplate);
4040 }
4041 }
4042
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004043 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00004044 }
4045
4046 if (!ConflictingDecls.empty()) {
4047 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4048 ConflictingDecls.data(),
4049 ConflictingDecls.size());
4050 }
4051
4052 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004053 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004054 }
4055
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004056 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4057
Douglas Gregora082a492010-11-30 19:14:50 +00004058 // Create the declaration that is being templated.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004059 CXXRecordDecl *ToTemplated = cast_or_null<CXXRecordDecl>(
4060 Importer.Import(FromTemplated));
4061 if (!ToTemplated)
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004062 return nullptr;
4063
4064 // Resolve possible cyclic import.
4065 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
4066 return AlreadyImported;
4067
Douglas Gregora082a492010-11-30 19:14:50 +00004068 // Create the class template declaration itself.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004069 TemplateParameterList *TemplateParams =
4070 ImportTemplateParameterList(D->getTemplateParameters());
Douglas Gregora082a492010-11-30 19:14:50 +00004071 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004072 return nullptr;
4073
Douglas Gregora082a492010-11-30 19:14:50 +00004074 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4075 Loc, Name, TemplateParams,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004076 ToTemplated);
4077 ToTemplated->setDescribedClassTemplate(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004078
4079 D2->setAccess(D->getAccess());
4080 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004081 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004082
4083 // Note the relationship between the class templates.
4084 Importer.Imported(D, D2);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004085 Importer.Imported(FromTemplated, ToTemplated);
Douglas Gregora082a492010-11-30 19:14:50 +00004086
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004087 if (FromTemplated->isCompleteDefinition() &&
4088 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004089 // FIXME: Import definition!
4090 }
4091
4092 return D2;
4093}
4094
Douglas Gregore2e50d332010-12-01 01:36:18 +00004095Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4096 ClassTemplateSpecializationDecl *D) {
4097 // If this record has a definition in the translation unit we're coming from,
4098 // but this particular declaration is not that definition, import the
4099 // definition and map to that.
4100 TagDecl *Definition = D->getDefinition();
4101 if (Definition && Definition != D) {
4102 Decl *ImportedDef = Importer.Import(Definition);
4103 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004104 return nullptr;
4105
Douglas Gregore2e50d332010-12-01 01:36:18 +00004106 return Importer.Imported(D, ImportedDef);
4107 }
4108
4109 ClassTemplateDecl *ClassTemplate
4110 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4111 D->getSpecializedTemplate()));
4112 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004113 return nullptr;
4114
Douglas Gregore2e50d332010-12-01 01:36:18 +00004115 // Import the context of this declaration.
4116 DeclContext *DC = ClassTemplate->getDeclContext();
4117 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004118 return nullptr;
4119
Douglas Gregore2e50d332010-12-01 01:36:18 +00004120 DeclContext *LexicalDC = DC;
4121 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4122 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4123 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004124 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004125 }
4126
4127 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004128 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4129 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004130
4131 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004132 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004133 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4134 D->getTemplateArgs().size(),
4135 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004136 return nullptr;
4137
Douglas Gregore2e50d332010-12-01 01:36:18 +00004138 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004139 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004140 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004141 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004142 if (D2) {
4143 // We already have a class template specialization with these template
4144 // arguments.
4145
4146 // FIXME: Check for specialization vs. instantiation errors.
4147
4148 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004149 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004150 // The record types structurally match, or the "from" translation
4151 // unit only had a forward declaration anyway; call it the same
4152 // function.
4153 return Importer.Imported(D, FoundDef);
4154 }
4155 }
4156 } else {
4157 // Create a new specialization.
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004158 if (ClassTemplatePartialSpecializationDecl *PartialSpec =
4159 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
4160
4161 // Import TemplateArgumentListInfo
4162 TemplateArgumentListInfo ToTAInfo;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004163 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
4164 if (ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004165 return nullptr;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004166
4167 QualType CanonInjType = Importer.Import(
4168 PartialSpec->getInjectedSpecializationType());
4169 if (CanonInjType.isNull())
4170 return nullptr;
4171 CanonInjType = CanonInjType.getCanonicalType();
4172
4173 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4174 PartialSpec->getTemplateParameters());
4175 if (!ToTPList && PartialSpec->getTemplateParameters())
4176 return nullptr;
4177
4178 D2 = ClassTemplatePartialSpecializationDecl::Create(
4179 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
4180 ToTPList, ClassTemplate,
4181 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
4182 ToTAInfo, CanonInjType, nullptr);
4183
4184 } else {
4185 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4186 D->getTagKind(), DC,
4187 StartLoc, IdLoc,
4188 ClassTemplate,
4189 TemplateArgs,
4190 /*PrevDecl=*/nullptr);
4191 }
4192
Douglas Gregore2e50d332010-12-01 01:36:18 +00004193 D2->setSpecializationKind(D->getSpecializationKind());
4194
4195 // Add this specialization to the class template.
4196 ClassTemplate->AddSpecialization(D2, InsertPos);
4197
4198 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004199 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004200
4201 Importer.Imported(D, D2);
4202
4203 if (auto *TSI = D->getTypeAsWritten()) {
4204 TypeSourceInfo *TInfo = Importer.Import(TSI);
4205 if (!TInfo)
4206 return nullptr;
4207 D2->setTypeAsWritten(TInfo);
4208 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
4209 D2->setExternLoc(Importer.Import(D->getExternLoc()));
4210 }
4211
4212 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
4213 if (POI.isValid())
4214 D2->setPointOfInstantiation(POI);
4215 else if (D->getPointOfInstantiation().isValid())
4216 return nullptr;
4217
4218 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
4219
Douglas Gregore2e50d332010-12-01 01:36:18 +00004220 // Add the specialization to this context.
4221 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004222 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004223 }
4224 Importer.Imported(D, D2);
John McCallf937c022011-10-07 06:10:15 +00004225 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004226 return nullptr;
4227
Douglas Gregore2e50d332010-12-01 01:36:18 +00004228 return D2;
4229}
4230
Larisse Voufo39a1e502013-08-06 01:03:05 +00004231Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4232 // If this variable has a definition in the translation unit we're coming
4233 // from,
4234 // but this particular declaration is not that definition, import the
4235 // definition and map to that.
4236 VarDecl *Definition =
4237 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4238 if (Definition && Definition != D->getTemplatedDecl()) {
4239 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4240 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004241 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004242
4243 return Importer.Imported(D, ImportedDef);
4244 }
4245
4246 // Import the major distinguishing characteristics of this variable template.
4247 DeclContext *DC, *LexicalDC;
4248 DeclarationName Name;
4249 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004250 NamedDecl *ToD;
4251 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004252 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004253 if (ToD)
4254 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004255
4256 // We may already have a template of the same name; try to find and match it.
4257 assert(!DC->isFunctionOrMethod() &&
4258 "Variable templates cannot be declared at function scope");
4259 SmallVector<NamedDecl *, 4> ConflictingDecls;
4260 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004261 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004262 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4263 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4264 continue;
4265
4266 Decl *Found = FoundDecls[I];
4267 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4268 if (IsStructuralMatch(D, FoundTemplate)) {
4269 // The variable templates structurally match; call it the same template.
4270 Importer.Imported(D->getTemplatedDecl(),
4271 FoundTemplate->getTemplatedDecl());
4272 return Importer.Imported(D, FoundTemplate);
4273 }
4274 }
4275
4276 ConflictingDecls.push_back(FoundDecls[I]);
4277 }
4278
4279 if (!ConflictingDecls.empty()) {
4280 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4281 ConflictingDecls.data(),
4282 ConflictingDecls.size());
4283 }
4284
4285 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004286 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004287
4288 VarDecl *DTemplated = D->getTemplatedDecl();
4289
4290 // Import the type.
4291 QualType T = Importer.Import(DTemplated->getType());
4292 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004293 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004294
4295 // Create the declaration that is being templated.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004296 auto *ToTemplated = dyn_cast_or_null<VarDecl>(Importer.Import(DTemplated));
4297 if (!ToTemplated)
Craig Topper36250ad2014-05-12 05:36:57 +00004298 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004299
4300 // Create the variable template declaration itself.
4301 TemplateParameterList *TemplateParams =
4302 ImportTemplateParameterList(D->getTemplateParameters());
4303 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004304 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004305
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004306 VarTemplateDecl *ToVarTD = VarTemplateDecl::Create(
4307 Importer.getToContext(), DC, Loc, Name, TemplateParams, ToTemplated);
4308 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004309
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004310 ToVarTD->setAccess(D->getAccess());
4311 ToVarTD->setLexicalDeclContext(LexicalDC);
4312 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004313
4314 // Note the relationship between the variable templates.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004315 Importer.Imported(D, ToVarTD);
4316 Importer.Imported(DTemplated, ToTemplated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004317
4318 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004319 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004320 // FIXME: Import definition!
4321 }
4322
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004323 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004324}
4325
4326Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4327 VarTemplateSpecializationDecl *D) {
4328 // If this record has a definition in the translation unit we're coming from,
4329 // but this particular declaration is not that definition, import the
4330 // definition and map to that.
4331 VarDecl *Definition = D->getDefinition();
4332 if (Definition && Definition != D) {
4333 Decl *ImportedDef = Importer.Import(Definition);
4334 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004335 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004336
4337 return Importer.Imported(D, ImportedDef);
4338 }
4339
4340 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4341 Importer.Import(D->getSpecializedTemplate()));
4342 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004343 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004344
4345 // Import the context of this declaration.
4346 DeclContext *DC = VarTemplate->getDeclContext();
4347 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004348 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004349
4350 DeclContext *LexicalDC = DC;
4351 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4352 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4353 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004354 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004355 }
4356
4357 // Import the location of this declaration.
4358 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4359 SourceLocation IdLoc = Importer.Import(D->getLocation());
4360
4361 // Import template arguments.
4362 SmallVector<TemplateArgument, 2> TemplateArgs;
4363 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4364 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004365 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004366
4367 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004368 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004369 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004370 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004371 if (D2) {
4372 // We already have a variable template specialization with these template
4373 // arguments.
4374
4375 // FIXME: Check for specialization vs. instantiation errors.
4376
4377 if (VarDecl *FoundDef = D2->getDefinition()) {
4378 if (!D->isThisDeclarationADefinition() ||
4379 IsStructuralMatch(D, FoundDef)) {
4380 // The record types structurally match, or the "from" translation
4381 // unit only had a forward declaration anyway; call it the same
4382 // variable.
4383 return Importer.Imported(D, FoundDef);
4384 }
4385 }
4386 } else {
4387
4388 // Import the type.
4389 QualType T = Importer.Import(D->getType());
4390 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004391 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004392
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004393 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4394 if (D->getTypeSourceInfo() && !TInfo)
4395 return nullptr;
4396
4397 TemplateArgumentListInfo ToTAInfo;
4398 if (ImportTemplateArgumentListInfo(D->getTemplateArgsInfo(), ToTAInfo))
4399 return nullptr;
4400
4401 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004402 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004403 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
4404 // Import TemplateArgumentListInfo
4405 TemplateArgumentListInfo ArgInfos;
4406 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
4407 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
4408 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ArgInfos))
4409 return nullptr;
4410
4411 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4412 FromPartial->getTemplateParameters());
4413 if (!ToTPList)
4414 return nullptr;
4415
4416 auto *ToPartial = PartVarSpecDecl::Create(
4417 Importer.getToContext(), DC, StartLoc, IdLoc, ToTPList, VarTemplate,
4418 T, TInfo, D->getStorageClass(), TemplateArgs, ArgInfos);
4419
4420 auto *FromInst = FromPartial->getInstantiatedFromMember();
4421 auto *ToInst = cast_or_null<PartVarSpecDecl>(Importer.Import(FromInst));
4422 if (FromInst && !ToInst)
4423 return nullptr;
4424
4425 ToPartial->setInstantiatedFromMember(ToInst);
4426 if (FromPartial->isMemberSpecialization())
4427 ToPartial->setMemberSpecialization();
4428
4429 D2 = ToPartial;
4430
4431 } else { // Full specialization
4432 D2 = VarTemplateSpecializationDecl::Create(
4433 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4434 D->getStorageClass(), TemplateArgs);
4435 }
4436
4437 SourceLocation POI = D->getPointOfInstantiation();
4438 if (POI.isValid())
4439 D2->setPointOfInstantiation(Importer.Import(POI));
4440
Larisse Voufo39a1e502013-08-06 01:03:05 +00004441 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004442 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004443
4444 // Add this specialization to the class template.
4445 VarTemplate->AddSpecialization(D2, InsertPos);
4446
4447 // Import the qualifier, if any.
4448 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4449
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004450 if (D->isConstexpr())
4451 D2->setConstexpr(true);
4452
Larisse Voufo39a1e502013-08-06 01:03:05 +00004453 // Add the specialization to this context.
4454 D2->setLexicalDeclContext(LexicalDC);
4455 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004456
4457 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00004458 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004459
Larisse Voufo39a1e502013-08-06 01:03:05 +00004460 Importer.Imported(D, D2);
4461
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004462 // NOTE: isThisDeclarationADefinition() can return DeclarationOnly even if
4463 // declaration has initializer. Should this be fixed in the AST?.. Anyway,
4464 // we have to check the declaration for initializer - otherwise, it won't be
4465 // imported.
4466 if ((D->isThisDeclarationADefinition() || D->hasInit()) &&
4467 ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004468 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004469
4470 return D2;
4471}
4472
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004473Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
4474 DeclContext *DC, *LexicalDC;
4475 DeclarationName Name;
4476 SourceLocation Loc;
4477 NamedDecl *ToD;
4478
4479 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4480 return nullptr;
4481
4482 if (ToD)
4483 return ToD;
4484
4485 // Try to find a function in our own ("to") context with the same name, same
4486 // type, and in the same context as the function we're importing.
4487 if (!LexicalDC->isFunctionOrMethod()) {
4488 unsigned IDNS = Decl::IDNS_Ordinary;
4489 SmallVector<NamedDecl *, 2> FoundDecls;
4490 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
4491 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4492 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
4493 continue;
4494
4495 if (FunctionTemplateDecl *FoundFunction =
4496 dyn_cast<FunctionTemplateDecl>(FoundDecls[I])) {
4497 if (FoundFunction->hasExternalFormalLinkage() &&
4498 D->hasExternalFormalLinkage()) {
4499 if (IsStructuralMatch(D, FoundFunction)) {
4500 Importer.Imported(D, FoundFunction);
4501 // FIXME: Actually try to merge the body and other attributes.
4502 return FoundFunction;
4503 }
4504 }
4505 }
4506 }
4507 }
4508
4509 TemplateParameterList *Params =
4510 ImportTemplateParameterList(D->getTemplateParameters());
4511 if (!Params)
4512 return nullptr;
4513
4514 FunctionDecl *TemplatedFD =
4515 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl()));
4516 if (!TemplatedFD)
4517 return nullptr;
4518
4519 FunctionTemplateDecl *ToFunc = FunctionTemplateDecl::Create(
4520 Importer.getToContext(), DC, Loc, Name, Params, TemplatedFD);
4521
4522 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
4523 ToFunc->setAccess(D->getAccess());
4524 ToFunc->setLexicalDeclContext(LexicalDC);
4525 Importer.Imported(D, ToFunc);
4526
4527 LexicalDC->addDeclInternal(ToFunc);
4528 return ToFunc;
4529}
4530
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004531//----------------------------------------------------------------------------
4532// Import Statements
4533//----------------------------------------------------------------------------
4534
Sean Callanan59721b32015-04-28 18:41:46 +00004535DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4536 if (DG.isNull())
4537 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4538 size_t NumDecls = DG.end() - DG.begin();
4539 SmallVector<Decl *, 1> ToDecls(NumDecls);
4540 auto &_Importer = this->Importer;
4541 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4542 [&_Importer](Decl *D) -> Decl * {
4543 return _Importer.Import(D);
4544 });
4545 return DeclGroupRef::Create(Importer.getToContext(),
4546 ToDecls.begin(),
4547 NumDecls);
4548}
4549
4550 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4551 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4552 << S->getStmtClassName();
4553 return nullptr;
4554 }
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004555
4556
4557Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4558 SmallVector<IdentifierInfo *, 4> Names;
4559 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4560 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004561 // ToII is nullptr when no symbolic name is given for output operand
4562 // see ParseStmtAsm::ParseAsmOperandsOpt
4563 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004564 return nullptr;
4565 Names.push_back(ToII);
4566 }
4567 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4568 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004569 // ToII is nullptr when no symbolic name is given for input operand
4570 // see ParseStmtAsm::ParseAsmOperandsOpt
4571 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004572 return nullptr;
4573 Names.push_back(ToII);
4574 }
4575
4576 SmallVector<StringLiteral *, 4> Clobbers;
4577 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
4578 StringLiteral *Clobber = cast_or_null<StringLiteral>(
4579 Importer.Import(S->getClobberStringLiteral(I)));
4580 if (!Clobber)
4581 return nullptr;
4582 Clobbers.push_back(Clobber);
4583 }
4584
4585 SmallVector<StringLiteral *, 4> Constraints;
4586 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4587 StringLiteral *Output = cast_or_null<StringLiteral>(
4588 Importer.Import(S->getOutputConstraintLiteral(I)));
4589 if (!Output)
4590 return nullptr;
4591 Constraints.push_back(Output);
4592 }
4593
4594 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4595 StringLiteral *Input = cast_or_null<StringLiteral>(
4596 Importer.Import(S->getInputConstraintLiteral(I)));
4597 if (!Input)
4598 return nullptr;
4599 Constraints.push_back(Input);
4600 }
4601
4602 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004603 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004604 return nullptr;
4605
Aleksei Sidorina693b372016-09-28 10:16:56 +00004606 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004607 return nullptr;
4608
4609 StringLiteral *AsmStr = cast_or_null<StringLiteral>(
4610 Importer.Import(S->getAsmString()));
4611 if (!AsmStr)
4612 return nullptr;
4613
4614 return new (Importer.getToContext()) GCCAsmStmt(
4615 Importer.getToContext(),
4616 Importer.Import(S->getAsmLoc()),
4617 S->isSimple(),
4618 S->isVolatile(),
4619 S->getNumOutputs(),
4620 S->getNumInputs(),
4621 Names.data(),
4622 Constraints.data(),
4623 Exprs.data(),
4624 AsmStr,
4625 S->getNumClobbers(),
4626 Clobbers.data(),
4627 Importer.Import(S->getRParenLoc()));
4628}
4629
Sean Callanan59721b32015-04-28 18:41:46 +00004630Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4631 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4632 for (Decl *ToD : ToDG) {
4633 if (!ToD)
4634 return nullptr;
4635 }
4636 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4637 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4638 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4639}
4640
4641Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4642 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4643 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4644 S->hasLeadingEmptyMacro());
4645}
4646
4647Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004648 llvm::SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004649
4650 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00004651 return nullptr;
4652
Sean Callanan59721b32015-04-28 18:41:46 +00004653 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4654 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
Benjamin Kramer07420902017-12-24 16:24:20 +00004655 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
4656 ToRBraceLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004657}
4658
4659Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4660 Expr *ToLHS = Importer.Import(S->getLHS());
4661 if (!ToLHS)
4662 return nullptr;
4663 Expr *ToRHS = Importer.Import(S->getRHS());
4664 if (!ToRHS && S->getRHS())
4665 return nullptr;
Gabor Horvath480892b2017-10-18 09:25:18 +00004666 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4667 if (!ToSubStmt && S->getSubStmt())
4668 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004669 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4670 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4671 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
Gabor Horvath480892b2017-10-18 09:25:18 +00004672 CaseStmt *ToStmt = new (Importer.getToContext())
4673 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
4674 ToStmt->setSubStmt(ToSubStmt);
4675 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00004676}
4677
4678Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4679 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4680 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4681 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4682 if (!ToSubStmt && S->getSubStmt())
4683 return nullptr;
4684 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4685 ToSubStmt);
4686}
4687
4688Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4689 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4690 LabelDecl *ToLabelDecl =
4691 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4692 if (!ToLabelDecl && S->getDecl())
4693 return nullptr;
4694 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4695 if (!ToSubStmt && S->getSubStmt())
4696 return nullptr;
4697 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4698 ToSubStmt);
4699}
4700
4701Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4702 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4703 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4704 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4705 ASTContext &_ToContext = Importer.getToContext();
4706 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4707 [&_ToContext](const Attr *A) -> const Attr * {
4708 return A->clone(_ToContext);
4709 });
4710 for (const Attr *ToA : ToAttrs) {
4711 if (!ToA)
4712 return nullptr;
4713 }
4714 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4715 if (!ToSubStmt && S->getSubStmt())
4716 return nullptr;
4717 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4718 ToAttrs, ToSubStmt);
4719}
4720
4721Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4722 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00004723 Stmt *ToInit = Importer.Import(S->getInit());
4724 if (!ToInit && S->getInit())
4725 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004726 VarDecl *ToConditionVariable = nullptr;
4727 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4728 ToConditionVariable =
4729 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4730 if (!ToConditionVariable)
4731 return nullptr;
4732 }
4733 Expr *ToCondition = Importer.Import(S->getCond());
4734 if (!ToCondition && S->getCond())
4735 return nullptr;
4736 Stmt *ToThenStmt = Importer.Import(S->getThen());
4737 if (!ToThenStmt && S->getThen())
4738 return nullptr;
4739 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4740 Stmt *ToElseStmt = Importer.Import(S->getElse());
4741 if (!ToElseStmt && S->getElse())
4742 return nullptr;
4743 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00004744 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00004745 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00004746 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00004747 ToCondition, ToThenStmt,
4748 ToElseLoc, ToElseStmt);
4749}
4750
4751Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00004752 Stmt *ToInit = Importer.Import(S->getInit());
4753 if (!ToInit && S->getInit())
4754 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004755 VarDecl *ToConditionVariable = nullptr;
4756 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4757 ToConditionVariable =
4758 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4759 if (!ToConditionVariable)
4760 return nullptr;
4761 }
4762 Expr *ToCondition = Importer.Import(S->getCond());
4763 if (!ToCondition && S->getCond())
4764 return nullptr;
4765 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00004766 Importer.getToContext(), ToInit,
4767 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00004768 Stmt *ToBody = Importer.Import(S->getBody());
4769 if (!ToBody && S->getBody())
4770 return nullptr;
4771 ToStmt->setBody(ToBody);
4772 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4773 // Now we have to re-chain the cases.
4774 SwitchCase *LastChainedSwitchCase = nullptr;
4775 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4776 SC = SC->getNextSwitchCase()) {
4777 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4778 if (!ToSC)
4779 return nullptr;
4780 if (LastChainedSwitchCase)
4781 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4782 else
4783 ToStmt->setSwitchCaseList(ToSC);
4784 LastChainedSwitchCase = ToSC;
4785 }
4786 return ToStmt;
4787}
4788
4789Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4790 VarDecl *ToConditionVariable = nullptr;
4791 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4792 ToConditionVariable =
4793 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4794 if (!ToConditionVariable)
4795 return nullptr;
4796 }
4797 Expr *ToCondition = Importer.Import(S->getCond());
4798 if (!ToCondition && S->getCond())
4799 return nullptr;
4800 Stmt *ToBody = Importer.Import(S->getBody());
4801 if (!ToBody && S->getBody())
4802 return nullptr;
4803 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4804 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4805 ToConditionVariable,
4806 ToCondition, ToBody,
4807 ToWhileLoc);
4808}
4809
4810Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4811 Stmt *ToBody = Importer.Import(S->getBody());
4812 if (!ToBody && S->getBody())
4813 return nullptr;
4814 Expr *ToCondition = Importer.Import(S->getCond());
4815 if (!ToCondition && S->getCond())
4816 return nullptr;
4817 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4818 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4819 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4820 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4821 ToDoLoc, ToWhileLoc,
4822 ToRParenLoc);
4823}
4824
4825Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4826 Stmt *ToInit = Importer.Import(S->getInit());
4827 if (!ToInit && S->getInit())
4828 return nullptr;
4829 Expr *ToCondition = Importer.Import(S->getCond());
4830 if (!ToCondition && S->getCond())
4831 return nullptr;
4832 VarDecl *ToConditionVariable = nullptr;
4833 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4834 ToConditionVariable =
4835 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4836 if (!ToConditionVariable)
4837 return nullptr;
4838 }
4839 Expr *ToInc = Importer.Import(S->getInc());
4840 if (!ToInc && S->getInc())
4841 return nullptr;
4842 Stmt *ToBody = Importer.Import(S->getBody());
4843 if (!ToBody && S->getBody())
4844 return nullptr;
4845 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4846 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4847 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4848 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4849 ToInit, ToCondition,
4850 ToConditionVariable,
4851 ToInc, ToBody,
4852 ToForLoc, ToLParenLoc,
4853 ToRParenLoc);
4854}
4855
4856Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4857 LabelDecl *ToLabel = nullptr;
4858 if (LabelDecl *FromLabel = S->getLabel()) {
4859 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4860 if (!ToLabel)
4861 return nullptr;
4862 }
4863 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4864 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4865 return new (Importer.getToContext()) GotoStmt(ToLabel,
4866 ToGotoLoc, ToLabelLoc);
4867}
4868
4869Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4870 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4871 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4872 Expr *ToTarget = Importer.Import(S->getTarget());
4873 if (!ToTarget && S->getTarget())
4874 return nullptr;
4875 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4876 ToTarget);
4877}
4878
4879Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4880 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4881 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4882}
4883
4884Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4885 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4886 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4887}
4888
4889Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4890 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4891 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4892 if (!ToRetExpr && S->getRetValue())
4893 return nullptr;
4894 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4895 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4896 if (!ToNRVOCandidate && NRVOCandidate)
4897 return nullptr;
4898 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4899 ToNRVOCandidate);
4900}
4901
4902Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4903 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4904 VarDecl *ToExceptionDecl = nullptr;
4905 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4906 ToExceptionDecl =
4907 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4908 if (!ToExceptionDecl)
4909 return nullptr;
4910 }
4911 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4912 if (!ToHandlerBlock && S->getHandlerBlock())
4913 return nullptr;
4914 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4915 ToExceptionDecl,
4916 ToHandlerBlock);
4917}
4918
4919Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4920 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4921 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4922 if (!ToTryBlock && S->getTryBlock())
4923 return nullptr;
4924 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4925 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4926 CXXCatchStmt *FromHandler = S->getHandler(HI);
4927 if (Stmt *ToHandler = Importer.Import(FromHandler))
4928 ToHandlers[HI] = ToHandler;
4929 else
4930 return nullptr;
4931 }
4932 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4933 ToHandlers);
4934}
4935
4936Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4937 DeclStmt *ToRange =
4938 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4939 if (!ToRange && S->getRangeStmt())
4940 return nullptr;
Richard Smith01694c32016-03-20 10:33:40 +00004941 DeclStmt *ToBegin =
4942 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
4943 if (!ToBegin && S->getBeginStmt())
4944 return nullptr;
4945 DeclStmt *ToEnd =
4946 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
4947 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00004948 return nullptr;
4949 Expr *ToCond = Importer.Import(S->getCond());
4950 if (!ToCond && S->getCond())
4951 return nullptr;
4952 Expr *ToInc = Importer.Import(S->getInc());
4953 if (!ToInc && S->getInc())
4954 return nullptr;
4955 DeclStmt *ToLoopVar =
4956 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4957 if (!ToLoopVar && S->getLoopVarStmt())
4958 return nullptr;
4959 Stmt *ToBody = Importer.Import(S->getBody());
4960 if (!ToBody && S->getBody())
4961 return nullptr;
4962 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00004963 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00004964 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4965 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00004966 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00004967 ToCond, ToInc,
4968 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00004969 ToForLoc, ToCoawaitLoc,
4970 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004971}
4972
4973Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
4974 Stmt *ToElem = Importer.Import(S->getElement());
4975 if (!ToElem && S->getElement())
4976 return nullptr;
4977 Expr *ToCollect = Importer.Import(S->getCollection());
4978 if (!ToCollect && S->getCollection())
4979 return nullptr;
4980 Stmt *ToBody = Importer.Import(S->getBody());
4981 if (!ToBody && S->getBody())
4982 return nullptr;
4983 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4984 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4985 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
4986 ToCollect,
4987 ToBody, ToForLoc,
4988 ToRParenLoc);
4989}
4990
4991Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
4992 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
4993 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4994 VarDecl *ToExceptionDecl = nullptr;
4995 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
4996 ToExceptionDecl =
4997 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4998 if (!ToExceptionDecl)
4999 return nullptr;
5000 }
5001 Stmt *ToBody = Importer.Import(S->getCatchBody());
5002 if (!ToBody && S->getCatchBody())
5003 return nullptr;
5004 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5005 ToRParenLoc,
5006 ToExceptionDecl,
5007 ToBody);
5008}
5009
5010Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5011 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5012 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5013 if (!ToAtFinallyStmt && S->getFinallyBody())
5014 return nullptr;
5015 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5016 ToAtFinallyStmt);
5017}
5018
5019Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5020 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5021 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5022 if (!ToAtTryStmt && S->getTryBody())
5023 return nullptr;
5024 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5025 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5026 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5027 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5028 ToCatchStmts[CI] = ToCatchStmt;
5029 else
5030 return nullptr;
5031 }
5032 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5033 if (!ToAtFinallyStmt && S->getFinallyStmt())
5034 return nullptr;
5035 return ObjCAtTryStmt::Create(Importer.getToContext(),
5036 ToAtTryLoc, ToAtTryStmt,
5037 ToCatchStmts.begin(), ToCatchStmts.size(),
5038 ToAtFinallyStmt);
5039}
5040
5041Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5042 (ObjCAtSynchronizedStmt *S) {
5043 SourceLocation ToAtSynchronizedLoc =
5044 Importer.Import(S->getAtSynchronizedLoc());
5045 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5046 if (!ToSynchExpr && S->getSynchExpr())
5047 return nullptr;
5048 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5049 if (!ToSynchBody && S->getSynchBody())
5050 return nullptr;
5051 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5052 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5053}
5054
5055Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5056 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5057 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5058 if (!ToThrow && S->getThrowExpr())
5059 return nullptr;
5060 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5061}
5062
5063Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5064 (ObjCAutoreleasePoolStmt *S) {
5065 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5066 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5067 if (!ToSubStmt && S->getSubStmt())
5068 return nullptr;
5069 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5070 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005071}
5072
5073//----------------------------------------------------------------------------
5074// Import Expressions
5075//----------------------------------------------------------------------------
5076Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5077 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5078 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005079 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005080}
5081
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005082Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5083 QualType T = Importer.Import(E->getType());
5084 if (T.isNull())
5085 return nullptr;
5086
5087 Expr *SubExpr = Importer.Import(E->getSubExpr());
5088 if (!SubExpr && E->getSubExpr())
5089 return nullptr;
5090
5091 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5092 if (!TInfo)
5093 return nullptr;
5094
5095 return new (Importer.getToContext()) VAArgExpr(
5096 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5097 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5098}
5099
5100
5101Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5102 QualType T = Importer.Import(E->getType());
5103 if (T.isNull())
5104 return nullptr;
5105
5106 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005107 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005108}
5109
5110Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5111 QualType T = Importer.Import(E->getType());
5112 if (T.isNull())
5113 return nullptr;
5114
5115 StringLiteral *SL = cast_or_null<StringLiteral>(
5116 Importer.Import(E->getFunctionName()));
5117 if (!SL && E->getFunctionName())
5118 return nullptr;
5119
5120 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005121 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005122}
5123
Douglas Gregor52f820e2010-02-19 01:17:02 +00005124Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00005125 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
5126 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005127 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005128
Craig Topper36250ad2014-05-12 05:36:57 +00005129 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005130 if (E->getDecl() != E->getFoundDecl()) {
5131 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5132 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005133 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005134 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005135
5136 QualType T = Importer.Import(E->getType());
5137 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005138 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005139
Aleksei Sidorina693b372016-09-28 10:16:56 +00005140
5141 TemplateArgumentListInfo ToTAInfo;
5142 TemplateArgumentListInfo *ResInfo = nullptr;
5143 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005144 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
5145 return nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00005146 ResInfo = &ToTAInfo;
5147 }
5148
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005149 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5150 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005151 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005152 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005153 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005154 Importer.Import(E->getLocation()),
5155 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005156 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005157 if (E->hadMultipleCandidates())
5158 DRE->setHadMultipleCandidates(true);
5159 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005160}
5161
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005162Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5163 QualType T = Importer.Import(E->getType());
5164 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00005165 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005166
5167 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5168}
5169
5170ASTNodeImporter::Designator
5171ASTNodeImporter::ImportDesignator(const Designator &D) {
5172 if (D.isFieldDesignator()) {
5173 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5174 // Caller checks for import error
5175 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5176 Importer.Import(D.getFieldLoc()));
5177 }
5178 if (D.isArrayDesignator())
5179 return Designator(D.getFirstExprIndex(),
5180 Importer.Import(D.getLBracketLoc()),
5181 Importer.Import(D.getRBracketLoc()));
5182
5183 assert(D.isArrayRangeDesignator());
5184 return Designator(D.getFirstExprIndex(),
5185 Importer.Import(D.getLBracketLoc()),
5186 Importer.Import(D.getEllipsisLoc()),
5187 Importer.Import(D.getRBracketLoc()));
5188}
5189
5190
5191Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
5192 Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
5193 if (!Init)
5194 return nullptr;
5195
5196 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5197 // List elements from the second, the first is Init itself
5198 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
5199 if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
5200 IndexExprs[I - 1] = Arg;
5201 else
5202 return nullptr;
5203 }
5204
5205 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005206 llvm::transform(DIE->designators(), Designators.begin(),
5207 [this](const Designator &D) -> Designator {
5208 return ImportDesignator(D);
5209 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005210
David Majnemerf7e36092016-06-23 00:15:04 +00005211 for (const Designator &D : DIE->designators())
5212 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005213 return nullptr;
5214
5215 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005216 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005217 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5218 DIE->usesGNUSyntax(), Init);
5219}
5220
5221Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5222 QualType T = Importer.Import(E->getType());
5223 if (T.isNull())
5224 return nullptr;
5225
5226 return new (Importer.getToContext())
5227 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5228}
5229
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005230Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5231 QualType T = Importer.Import(E->getType());
5232 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005233 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005234
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005235 return IntegerLiteral::Create(Importer.getToContext(),
5236 E->getValue(), T,
5237 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005238}
5239
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005240Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5241 QualType T = Importer.Import(E->getType());
5242 if (T.isNull())
5243 return nullptr;
5244
5245 return FloatingLiteral::Create(Importer.getToContext(),
5246 E->getValue(), E->isExact(), T,
5247 Importer.Import(E->getLocation()));
5248}
5249
Douglas Gregor623421d2010-02-18 02:21:22 +00005250Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5251 QualType T = Importer.Import(E->getType());
5252 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005253 return nullptr;
5254
Douglas Gregorfb65e592011-07-27 05:40:30 +00005255 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5256 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005257 Importer.Import(E->getLocation()));
5258}
5259
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005260Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5261 QualType T = Importer.Import(E->getType());
5262 if (T.isNull())
5263 return nullptr;
5264
5265 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5266 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5267
5268 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5269 E->getKind(), E->isPascal(), T,
5270 Locations.data(), Locations.size());
5271}
5272
5273Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5274 QualType T = Importer.Import(E->getType());
5275 if (T.isNull())
5276 return nullptr;
5277
5278 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5279 if (!TInfo)
5280 return nullptr;
5281
5282 Expr *Init = Importer.Import(E->getInitializer());
5283 if (!Init)
5284 return nullptr;
5285
5286 return new (Importer.getToContext()) CompoundLiteralExpr(
5287 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5288 Init, E->isFileScope());
5289}
5290
5291Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5292 QualType T = Importer.Import(E->getType());
5293 if (T.isNull())
5294 return nullptr;
5295
5296 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5297 if (ImportArrayChecked(
5298 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5299 Exprs.begin()))
5300 return nullptr;
5301
5302 return new (Importer.getToContext()) AtomicExpr(
5303 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5304 Importer.Import(E->getRParenLoc()));
5305}
5306
5307Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5308 QualType T = Importer.Import(E->getType());
5309 if (T.isNull())
5310 return nullptr;
5311
5312 LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
5313 if (!ToLabel)
5314 return nullptr;
5315
5316 return new (Importer.getToContext()) AddrLabelExpr(
5317 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5318 ToLabel, T);
5319}
5320
Douglas Gregorc74247e2010-02-19 01:07:06 +00005321Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5322 Expr *SubExpr = Importer.Import(E->getSubExpr());
5323 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005324 return nullptr;
5325
Douglas Gregorc74247e2010-02-19 01:07:06 +00005326 return new (Importer.getToContext())
5327 ParenExpr(Importer.Import(E->getLParen()),
5328 Importer.Import(E->getRParen()),
5329 SubExpr);
5330}
5331
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005332Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5333 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005334 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005335 return nullptr;
5336
5337 return new (Importer.getToContext()) ParenListExpr(
5338 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5339 Exprs, Importer.Import(E->getLParenLoc()));
5340}
5341
5342Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5343 QualType T = Importer.Import(E->getType());
5344 if (T.isNull())
5345 return nullptr;
5346
5347 CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>(
5348 Importer.Import(E->getSubStmt()));
5349 if (!ToSubStmt && E->getSubStmt())
5350 return nullptr;
5351
5352 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5353 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5354}
5355
Douglas Gregorc74247e2010-02-19 01:07:06 +00005356Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5357 QualType T = Importer.Import(E->getType());
5358 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005359 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005360
5361 Expr *SubExpr = Importer.Import(E->getSubExpr());
5362 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005363 return nullptr;
5364
Aaron Ballmana5038552018-01-09 13:07:03 +00005365 return new (Importer.getToContext()) UnaryOperator(
5366 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(),
5367 Importer.Import(E->getOperatorLoc()), E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005368}
5369
Aaron Ballmana5038552018-01-09 13:07:03 +00005370Expr *
5371ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005372 QualType ResultType = Importer.Import(E->getType());
5373
5374 if (E->isArgumentType()) {
5375 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5376 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005377 return nullptr;
5378
Peter Collingbournee190dee2011-03-11 19:24:49 +00005379 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5380 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005381 Importer.Import(E->getOperatorLoc()),
5382 Importer.Import(E->getRParenLoc()));
5383 }
5384
5385 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5386 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005387 return nullptr;
5388
Peter Collingbournee190dee2011-03-11 19:24:49 +00005389 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5390 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005391 Importer.Import(E->getOperatorLoc()),
5392 Importer.Import(E->getRParenLoc()));
5393}
5394
Douglas Gregorc74247e2010-02-19 01:07:06 +00005395Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5396 QualType T = Importer.Import(E->getType());
5397 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005398 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005399
5400 Expr *LHS = Importer.Import(E->getLHS());
5401 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005402 return nullptr;
5403
Douglas Gregorc74247e2010-02-19 01:07:06 +00005404 Expr *RHS = Importer.Import(E->getRHS());
5405 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005406 return nullptr;
5407
Douglas Gregorc74247e2010-02-19 01:07:06 +00005408 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005409 T, E->getValueKind(),
5410 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005411 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005412 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005413}
5414
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005415Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5416 QualType T = Importer.Import(E->getType());
5417 if (T.isNull())
5418 return nullptr;
5419
5420 Expr *ToLHS = Importer.Import(E->getLHS());
5421 if (!ToLHS)
5422 return nullptr;
5423
5424 Expr *ToRHS = Importer.Import(E->getRHS());
5425 if (!ToRHS)
5426 return nullptr;
5427
5428 Expr *ToCond = Importer.Import(E->getCond());
5429 if (!ToCond)
5430 return nullptr;
5431
5432 return new (Importer.getToContext()) ConditionalOperator(
5433 ToCond, Importer.Import(E->getQuestionLoc()),
5434 ToLHS, Importer.Import(E->getColonLoc()),
5435 ToRHS, T, E->getValueKind(), E->getObjectKind());
5436}
5437
5438Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5439 BinaryConditionalOperator *E) {
5440 QualType T = Importer.Import(E->getType());
5441 if (T.isNull())
5442 return nullptr;
5443
5444 Expr *Common = Importer.Import(E->getCommon());
5445 if (!Common)
5446 return nullptr;
5447
5448 Expr *Cond = Importer.Import(E->getCond());
5449 if (!Cond)
5450 return nullptr;
5451
5452 OpaqueValueExpr *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5453 Importer.Import(E->getOpaqueValue()));
5454 if (!OpaqueValue)
5455 return nullptr;
5456
5457 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5458 if (!TrueExpr)
5459 return nullptr;
5460
5461 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5462 if (!FalseExpr)
5463 return nullptr;
5464
5465 return new (Importer.getToContext()) BinaryConditionalOperator(
5466 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5467 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5468 T, E->getValueKind(), E->getObjectKind());
5469}
5470
Aleksei Sidorina693b372016-09-28 10:16:56 +00005471Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5472 QualType T = Importer.Import(E->getType());
5473 if (T.isNull())
5474 return nullptr;
5475
5476 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5477 if (!ToQueried)
5478 return nullptr;
5479
5480 Expr *Dim = Importer.Import(E->getDimensionExpression());
5481 if (!Dim && E->getDimensionExpression())
5482 return nullptr;
5483
5484 return new (Importer.getToContext()) ArrayTypeTraitExpr(
5485 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5486 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
5487}
5488
5489Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5490 QualType T = Importer.Import(E->getType());
5491 if (T.isNull())
5492 return nullptr;
5493
5494 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5495 if (!ToQueried)
5496 return nullptr;
5497
5498 return new (Importer.getToContext()) ExpressionTraitExpr(
5499 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5500 E->getValue(), Importer.Import(E->getLocEnd()), T);
5501}
5502
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005503Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5504 QualType T = Importer.Import(E->getType());
5505 if (T.isNull())
5506 return nullptr;
5507
5508 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5509 if (!SourceExpr && E->getSourceExpr())
5510 return nullptr;
5511
5512 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005513 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005514 E->getObjectKind(), SourceExpr);
5515}
5516
Aleksei Sidorina693b372016-09-28 10:16:56 +00005517Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5518 QualType T = Importer.Import(E->getType());
5519 if (T.isNull())
5520 return nullptr;
5521
5522 Expr *ToLHS = Importer.Import(E->getLHS());
5523 if (!ToLHS)
5524 return nullptr;
5525
5526 Expr *ToRHS = Importer.Import(E->getRHS());
5527 if (!ToRHS)
5528 return nullptr;
5529
5530 return new (Importer.getToContext()) ArraySubscriptExpr(
5531 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5532 Importer.Import(E->getRBracketLoc()));
5533}
5534
Douglas Gregorc74247e2010-02-19 01:07:06 +00005535Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5536 QualType T = Importer.Import(E->getType());
5537 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005538 return nullptr;
5539
Douglas Gregorc74247e2010-02-19 01:07:06 +00005540 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5541 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005542 return nullptr;
5543
Douglas Gregorc74247e2010-02-19 01:07:06 +00005544 QualType CompResultType = Importer.Import(E->getComputationResultType());
5545 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005546 return nullptr;
5547
Douglas Gregorc74247e2010-02-19 01:07:06 +00005548 Expr *LHS = Importer.Import(E->getLHS());
5549 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005550 return nullptr;
5551
Douglas Gregorc74247e2010-02-19 01:07:06 +00005552 Expr *RHS = Importer.Import(E->getRHS());
5553 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005554 return nullptr;
5555
Douglas Gregorc74247e2010-02-19 01:07:06 +00005556 return new (Importer.getToContext())
5557 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005558 T, E->getValueKind(),
5559 E->getObjectKind(),
5560 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005561 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005562 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005563}
5564
Aleksei Sidorina693b372016-09-28 10:16:56 +00005565bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
5566 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
5567 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
5568 Path.push_back(Spec);
5569 else
5570 return true;
5571 }
5572 return false;
John McCallcf142162010-08-07 06:22:56 +00005573}
5574
Douglas Gregor98c10182010-02-12 22:17:39 +00005575Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5576 QualType T = Importer.Import(E->getType());
5577 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005578 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005579
5580 Expr *SubExpr = Importer.Import(E->getSubExpr());
5581 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005582 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005583
5584 CXXCastPath BasePath;
5585 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005586 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005587
5588 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005589 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005590}
5591
Aleksei Sidorina693b372016-09-28 10:16:56 +00005592Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00005593 QualType T = Importer.Import(E->getType());
5594 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005595 return nullptr;
5596
Douglas Gregor5481d322010-02-19 01:32:14 +00005597 Expr *SubExpr = Importer.Import(E->getSubExpr());
5598 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005599 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005600
5601 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5602 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005603 return nullptr;
5604
John McCallcf142162010-08-07 06:22:56 +00005605 CXXCastPath BasePath;
5606 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005607 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005608
Aleksei Sidorina693b372016-09-28 10:16:56 +00005609 switch (E->getStmtClass()) {
5610 case Stmt::CStyleCastExprClass: {
5611 CStyleCastExpr *CCE = cast<CStyleCastExpr>(E);
5612 return CStyleCastExpr::Create(Importer.getToContext(), T,
5613 E->getValueKind(), E->getCastKind(),
5614 SubExpr, &BasePath, TInfo,
5615 Importer.Import(CCE->getLParenLoc()),
5616 Importer.Import(CCE->getRParenLoc()));
5617 }
5618
5619 case Stmt::CXXFunctionalCastExprClass: {
5620 CXXFunctionalCastExpr *FCE = cast<CXXFunctionalCastExpr>(E);
5621 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
5622 E->getValueKind(), TInfo,
5623 E->getCastKind(), SubExpr, &BasePath,
5624 Importer.Import(FCE->getLParenLoc()),
5625 Importer.Import(FCE->getRParenLoc()));
5626 }
5627
5628 case Stmt::ObjCBridgedCastExprClass: {
5629 ObjCBridgedCastExpr *OCE = cast<ObjCBridgedCastExpr>(E);
5630 return new (Importer.getToContext()) ObjCBridgedCastExpr(
5631 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
5632 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
5633 TInfo, SubExpr);
5634 }
5635 default:
5636 break; // just fall through
5637 }
5638
5639 CXXNamedCastExpr *Named = cast<CXXNamedCastExpr>(E);
5640 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
5641 RParenLoc = Importer.Import(Named->getRParenLoc());
5642 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
5643
5644 switch (E->getStmtClass()) {
5645 case Stmt::CXXStaticCastExprClass:
5646 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
5647 E->getValueKind(), E->getCastKind(),
5648 SubExpr, &BasePath, TInfo,
5649 ExprLoc, RParenLoc, Brackets);
5650
5651 case Stmt::CXXDynamicCastExprClass:
5652 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
5653 E->getValueKind(), E->getCastKind(),
5654 SubExpr, &BasePath, TInfo,
5655 ExprLoc, RParenLoc, Brackets);
5656
5657 case Stmt::CXXReinterpretCastExprClass:
5658 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
5659 E->getValueKind(), E->getCastKind(),
5660 SubExpr, &BasePath, TInfo,
5661 ExprLoc, RParenLoc, Brackets);
5662
5663 case Stmt::CXXConstCastExprClass:
5664 return CXXConstCastExpr::Create(Importer.getToContext(), T,
5665 E->getValueKind(), SubExpr, TInfo, ExprLoc,
5666 RParenLoc, Brackets);
5667 default:
5668 llvm_unreachable("Cast expression of unsupported type!");
5669 return nullptr;
5670 }
5671}
5672
5673Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
5674 QualType T = Importer.Import(OE->getType());
5675 if (T.isNull())
5676 return nullptr;
5677
5678 SmallVector<OffsetOfNode, 4> Nodes;
5679 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
5680 const OffsetOfNode &Node = OE->getComponent(I);
5681
5682 switch (Node.getKind()) {
5683 case OffsetOfNode::Array:
5684 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
5685 Node.getArrayExprIndex(),
5686 Importer.Import(Node.getLocEnd())));
5687 break;
5688
5689 case OffsetOfNode::Base: {
5690 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
5691 if (!BS && Node.getBase())
5692 return nullptr;
5693 Nodes.push_back(OffsetOfNode(BS));
5694 break;
5695 }
5696 case OffsetOfNode::Field: {
5697 FieldDecl *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
5698 if (!FD)
5699 return nullptr;
5700 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
5701 Importer.Import(Node.getLocEnd())));
5702 break;
5703 }
5704 case OffsetOfNode::Identifier: {
5705 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
5706 if (!ToII)
5707 return nullptr;
5708 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
5709 Importer.Import(Node.getLocEnd())));
5710 break;
5711 }
5712 }
5713 }
5714
5715 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
5716 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
5717 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
5718 if (!ToIndexExpr)
5719 return nullptr;
5720 Exprs[I] = ToIndexExpr;
5721 }
5722
5723 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
5724 if (!TInfo && OE->getTypeSourceInfo())
5725 return nullptr;
5726
5727 return OffsetOfExpr::Create(Importer.getToContext(), T,
5728 Importer.Import(OE->getOperatorLoc()),
5729 TInfo, Nodes, Exprs,
5730 Importer.Import(OE->getRParenLoc()));
5731}
5732
5733Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
5734 QualType T = Importer.Import(E->getType());
5735 if (T.isNull())
5736 return nullptr;
5737
5738 Expr *Operand = Importer.Import(E->getOperand());
5739 if (!Operand)
5740 return nullptr;
5741
5742 CanThrowResult CanThrow;
5743 if (E->isValueDependent())
5744 CanThrow = CT_Dependent;
5745 else
5746 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
5747
5748 return new (Importer.getToContext()) CXXNoexceptExpr(
5749 T, Operand, CanThrow,
5750 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
5751}
5752
5753Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
5754 QualType T = Importer.Import(E->getType());
5755 if (T.isNull())
5756 return nullptr;
5757
5758 Expr *SubExpr = Importer.Import(E->getSubExpr());
5759 if (!SubExpr && E->getSubExpr())
5760 return nullptr;
5761
5762 return new (Importer.getToContext()) CXXThrowExpr(
5763 SubExpr, T, Importer.Import(E->getThrowLoc()),
5764 E->isThrownVariableInScope());
5765}
5766
5767Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
5768 ParmVarDecl *Param = cast_or_null<ParmVarDecl>(
5769 Importer.Import(E->getParam()));
5770 if (!Param)
5771 return nullptr;
5772
5773 return CXXDefaultArgExpr::Create(
5774 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
5775}
5776
5777Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5778 QualType T = Importer.Import(E->getType());
5779 if (T.isNull())
5780 return nullptr;
5781
5782 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
5783 if (!TypeInfo)
5784 return nullptr;
5785
5786 return new (Importer.getToContext()) CXXScalarValueInitExpr(
5787 T, TypeInfo, Importer.Import(E->getRParenLoc()));
5788}
5789
5790Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
5791 Expr *SubExpr = Importer.Import(E->getSubExpr());
5792 if (!SubExpr)
5793 return nullptr;
5794
5795 auto *Dtor = cast_or_null<CXXDestructorDecl>(
5796 Importer.Import(const_cast<CXXDestructorDecl *>(
5797 E->getTemporary()->getDestructor())));
5798 if (!Dtor)
5799 return nullptr;
5800
5801 ASTContext &ToCtx = Importer.getToContext();
5802 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
5803 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
5804}
5805
5806Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
5807 QualType T = Importer.Import(CE->getType());
5808 if (T.isNull())
5809 return nullptr;
5810
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005811
5812 TypeSourceInfo *TInfo = Importer.Import(CE->getTypeSourceInfo());
5813 if (!TInfo)
5814 return nullptr;
5815
Aleksei Sidorina693b372016-09-28 10:16:56 +00005816 SmallVector<Expr *, 8> Args(CE->getNumArgs());
5817 if (ImportContainerChecked(CE->arguments(), Args))
5818 return nullptr;
5819
5820 auto *Ctor = cast_or_null<CXXConstructorDecl>(
5821 Importer.Import(CE->getConstructor()));
5822 if (!Ctor)
5823 return nullptr;
5824
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005825 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
5826 Importer.getToContext(), Ctor, T, TInfo, Args,
5827 Importer.Import(CE->getParenOrBraceRange()), CE->hadMultipleCandidates(),
5828 CE->isListInitialization(), CE->isStdInitListInitialization(),
5829 CE->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005830}
5831
5832Expr *
5833ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
5834 QualType T = Importer.Import(E->getType());
5835 if (T.isNull())
5836 return nullptr;
5837
5838 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
5839 if (!TempE)
5840 return nullptr;
5841
5842 ValueDecl *ExtendedBy = cast_or_null<ValueDecl>(
5843 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
5844 if (!ExtendedBy && E->getExtendingDecl())
5845 return nullptr;
5846
5847 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
5848 T, TempE, E->isBoundToLvalueReference());
5849
5850 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
5851 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
5852 return ToMTE;
5853}
5854
Gabor Horvath7a91c082017-11-14 11:30:38 +00005855Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
5856 QualType T = Importer.Import(E->getType());
5857 if (T.isNull())
5858 return nullptr;
5859
5860 Expr *Pattern = Importer.Import(E->getPattern());
5861 if (!Pattern)
5862 return nullptr;
5863
5864 return new (Importer.getToContext()) PackExpansionExpr(
5865 T, Pattern, Importer.Import(E->getEllipsisLoc()),
5866 E->getNumExpansions());
5867}
5868
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005869Expr *ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
5870 auto *Pack = cast_or_null<NamedDecl>(Importer.Import(E->getPack()));
5871 if (!Pack)
5872 return nullptr;
5873
5874 Optional<unsigned> Length;
5875
5876 if (!E->isValueDependent())
5877 Length = E->getPackLength();
5878
5879 SmallVector<TemplateArgument, 8> PartialArguments;
5880 if (E->isPartiallySubstituted()) {
5881 if (ImportTemplateArguments(E->getPartialArguments().data(),
5882 E->getPartialArguments().size(),
5883 PartialArguments))
5884 return nullptr;
5885 }
5886
5887 return SizeOfPackExpr::Create(
5888 Importer.getToContext(), Importer.Import(E->getOperatorLoc()), Pack,
5889 Importer.Import(E->getPackLoc()), Importer.Import(E->getRParenLoc()),
5890 Length, PartialArguments);
5891}
5892
5893
Aleksei Sidorina693b372016-09-28 10:16:56 +00005894Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
5895 QualType T = Importer.Import(CE->getType());
5896 if (T.isNull())
5897 return nullptr;
5898
5899 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
5900 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
5901 return nullptr;
5902
5903 FunctionDecl *OperatorNewDecl = cast_or_null<FunctionDecl>(
5904 Importer.Import(CE->getOperatorNew()));
5905 if (!OperatorNewDecl && CE->getOperatorNew())
5906 return nullptr;
5907
5908 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5909 Importer.Import(CE->getOperatorDelete()));
5910 if (!OperatorDeleteDecl && CE->getOperatorDelete())
5911 return nullptr;
5912
5913 Expr *ToInit = Importer.Import(CE->getInitializer());
5914 if (!ToInit && CE->getInitializer())
5915 return nullptr;
5916
5917 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
5918 if (!TInfo)
5919 return nullptr;
5920
5921 Expr *ToArrSize = Importer.Import(CE->getArraySize());
5922 if (!ToArrSize && CE->getArraySize())
5923 return nullptr;
5924
5925 return new (Importer.getToContext()) CXXNewExpr(
5926 Importer.getToContext(),
5927 CE->isGlobalNew(),
5928 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00005929 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005930 CE->doesUsualArrayDeleteWantSize(),
5931 PlacementArgs,
5932 Importer.Import(CE->getTypeIdParens()),
5933 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
5934 Importer.Import(CE->getSourceRange()),
5935 Importer.Import(CE->getDirectInitRange()));
5936}
5937
5938Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
5939 QualType T = Importer.Import(E->getType());
5940 if (T.isNull())
5941 return nullptr;
5942
5943 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5944 Importer.Import(E->getOperatorDelete()));
5945 if (!OperatorDeleteDecl && E->getOperatorDelete())
5946 return nullptr;
5947
5948 Expr *ToArg = Importer.Import(E->getArgument());
5949 if (!ToArg && E->getArgument())
5950 return nullptr;
5951
5952 return new (Importer.getToContext()) CXXDeleteExpr(
5953 T, E->isGlobalDelete(),
5954 E->isArrayForm(),
5955 E->isArrayFormAsWritten(),
5956 E->doesUsualArrayDeleteWantSize(),
5957 OperatorDeleteDecl,
5958 ToArg,
5959 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005960}
5961
Sean Callanan59721b32015-04-28 18:41:46 +00005962Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5963 QualType T = Importer.Import(E->getType());
5964 if (T.isNull())
5965 return nullptr;
5966
5967 CXXConstructorDecl *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00005968 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00005969 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00005970 return nullptr;
5971
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005972 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005973 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00005974 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005975
5976 return CXXConstructExpr::Create(Importer.getToContext(), T,
5977 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00005978 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00005979 ToArgs, E->hadMultipleCandidates(),
5980 E->isListInitialization(),
5981 E->isStdInitListInitialization(),
5982 E->requiresZeroInitialization(),
5983 E->getConstructionKind(),
5984 Importer.Import(E->getParenOrBraceRange()));
5985}
5986
Aleksei Sidorina693b372016-09-28 10:16:56 +00005987Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
5988 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
5989 if (!SubExpr && EWC->getSubExpr())
5990 return nullptr;
5991
5992 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
5993 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
5994 if (ExprWithCleanups::CleanupObject Obj =
5995 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
5996 Objs[I] = Obj;
5997 else
5998 return nullptr;
5999
6000 return ExprWithCleanups::Create(Importer.getToContext(),
6001 SubExpr, EWC->cleanupsHaveSideEffects(),
6002 Objs);
6003}
6004
Sean Callanan8bca9962016-03-28 21:43:01 +00006005Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6006 QualType T = Importer.Import(E->getType());
6007 if (T.isNull())
6008 return nullptr;
6009
6010 Expr *ToFn = Importer.Import(E->getCallee());
6011 if (!ToFn)
6012 return nullptr;
6013
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006014 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006015 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006016 return nullptr;
6017
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006018 return new (Importer.getToContext()) CXXMemberCallExpr(
6019 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
6020 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00006021}
6022
6023Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6024 QualType T = Importer.Import(E->getType());
6025 if (T.isNull())
6026 return nullptr;
6027
6028 return new (Importer.getToContext())
6029 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
6030}
6031
6032Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
6033 QualType T = Importer.Import(E->getType());
6034 if (T.isNull())
6035 return nullptr;
6036
6037 return new (Importer.getToContext())
6038 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
6039}
6040
6041
Sean Callanan59721b32015-04-28 18:41:46 +00006042Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
6043 QualType T = Importer.Import(E->getType());
6044 if (T.isNull())
6045 return nullptr;
6046
6047 Expr *ToBase = Importer.Import(E->getBase());
6048 if (!ToBase && E->getBase())
6049 return nullptr;
6050
6051 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
6052 if (!ToMember && E->getMemberDecl())
6053 return nullptr;
6054
6055 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
6056 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
6057 E->getFoundDecl().getAccess());
6058
6059 DeclarationNameInfo ToMemberNameInfo(
6060 Importer.Import(E->getMemberNameInfo().getName()),
6061 Importer.Import(E->getMemberNameInfo().getLoc()));
6062
6063 if (E->hasExplicitTemplateArgs()) {
6064 return nullptr; // FIXME: handle template arguments
6065 }
6066
6067 return MemberExpr::Create(Importer.getToContext(), ToBase,
6068 E->isArrow(),
6069 Importer.Import(E->getOperatorLoc()),
6070 Importer.Import(E->getQualifierLoc()),
6071 Importer.Import(E->getTemplateKeywordLoc()),
6072 ToMember, ToFoundDecl, ToMemberNameInfo,
6073 nullptr, T, E->getValueKind(),
6074 E->getObjectKind());
6075}
6076
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006077Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr(
6078 CXXPseudoDestructorExpr *E) {
6079
6080 Expr *BaseE = Importer.Import(E->getBase());
6081 if (!BaseE)
6082 return nullptr;
6083
6084 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo());
6085 if (!ScopeInfo && E->getScopeTypeInfo())
6086 return nullptr;
6087
6088 PseudoDestructorTypeStorage Storage;
6089 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
6090 IdentifierInfo *ToII = Importer.Import(FromII);
6091 if (!ToII)
6092 return nullptr;
6093 Storage = PseudoDestructorTypeStorage(
6094 ToII, Importer.Import(E->getDestroyedTypeLoc()));
6095 } else {
6096 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo());
6097 if (!TI)
6098 return nullptr;
6099 Storage = PseudoDestructorTypeStorage(TI);
6100 }
6101
6102 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
6103 Importer.getToContext(), BaseE, E->isArrow(),
6104 Importer.Import(E->getOperatorLoc()),
6105 Importer.Import(E->getQualifierLoc()),
6106 ScopeInfo, Importer.Import(E->getColonColonLoc()),
6107 Importer.Import(E->getTildeLoc()), Storage);
6108}
6109
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006110Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
6111 CXXDependentScopeMemberExpr *E) {
6112 Expr *Base = nullptr;
6113 if (!E->isImplicitAccess()) {
6114 Base = Importer.Import(E->getBase());
6115 if (!Base)
6116 return nullptr;
6117 }
6118
6119 QualType BaseType = Importer.Import(E->getBaseType());
6120 if (BaseType.isNull())
6121 return nullptr;
6122
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006123 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006124 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006125 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6126 E->template_arguments(), ToTAInfo))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006127 return nullptr;
6128 ResInfo = &ToTAInfo;
6129 }
6130
6131 DeclarationName Name = Importer.Import(E->getMember());
6132 if (!E->getMember().isEmpty() && Name.isEmpty())
6133 return nullptr;
6134
6135 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc()));
6136 // Import additional name location/type info.
6137 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo);
6138 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope());
6139 if (!ToFQ && E->getFirstQualifierFoundInScope())
6140 return nullptr;
6141
6142 return CXXDependentScopeMemberExpr::Create(
6143 Importer.getToContext(), Base, BaseType, E->isArrow(),
6144 Importer.Import(E->getOperatorLoc()),
6145 Importer.Import(E->getQualifierLoc()),
6146 Importer.Import(E->getTemplateKeywordLoc()),
6147 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo);
6148}
6149
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006150Expr *ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
6151 CXXUnresolvedConstructExpr *CE) {
6152
6153 unsigned NumArgs = CE->arg_size();
6154
6155 llvm::SmallVector<Expr *, 8> ToArgs(NumArgs);
6156 if (ImportArrayChecked(CE->arg_begin(), CE->arg_end(), ToArgs.begin()))
6157 return nullptr;
6158
6159 return CXXUnresolvedConstructExpr::Create(
6160 Importer.getToContext(), Importer.Import(CE->getTypeSourceInfo()),
6161 Importer.Import(CE->getLParenLoc()), llvm::makeArrayRef(ToArgs),
6162 Importer.Import(CE->getRParenLoc()));
6163}
6164
6165Expr *ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
6166 CXXRecordDecl *NamingClass =
6167 cast_or_null<CXXRecordDecl>(Importer.Import(E->getNamingClass()));
6168 if (E->getNamingClass() && !NamingClass)
6169 return nullptr;
6170
6171 DeclarationName Name = Importer.Import(E->getName());
6172 if (E->getName() && !Name)
6173 return nullptr;
6174
6175 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6176 // Import additional name location/type info.
6177 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6178
6179 UnresolvedSet<8> ToDecls;
6180 for (Decl *D : E->decls()) {
6181 if (NamedDecl *To = cast_or_null<NamedDecl>(Importer.Import(D)))
6182 ToDecls.addDecl(To);
6183 else
6184 return nullptr;
6185 }
6186
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006187 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006188 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006189 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6190 E->template_arguments(), ToTAInfo))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006191 return nullptr;
6192 ResInfo = &ToTAInfo;
6193 }
6194
6195 if (ResInfo || E->getTemplateKeywordLoc().isValid())
6196 return UnresolvedLookupExpr::Create(
6197 Importer.getToContext(), NamingClass,
6198 Importer.Import(E->getQualifierLoc()),
6199 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, E->requiresADL(),
6200 ResInfo, ToDecls.begin(), ToDecls.end());
6201
6202 return UnresolvedLookupExpr::Create(
6203 Importer.getToContext(), NamingClass,
6204 Importer.Import(E->getQualifierLoc()), NameInfo, E->requiresADL(),
6205 E->isOverloaded(), ToDecls.begin(), ToDecls.end());
6206}
6207
Sean Callanan59721b32015-04-28 18:41:46 +00006208Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
6209 QualType T = Importer.Import(E->getType());
6210 if (T.isNull())
6211 return nullptr;
6212
6213 Expr *ToCallee = Importer.Import(E->getCallee());
6214 if (!ToCallee && E->getCallee())
6215 return nullptr;
6216
6217 unsigned NumArgs = E->getNumArgs();
Sean Callanan59721b32015-04-28 18:41:46 +00006218 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006219 if (ImportContainerChecked(E->arguments(), ToArgs))
6220 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006221
6222 Expr **ToArgs_Copied = new (Importer.getToContext())
6223 Expr*[NumArgs];
6224
6225 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
6226 ToArgs_Copied[ai] = ToArgs[ai];
6227
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006228 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
6229 return new (Importer.getToContext()) CXXOperatorCallExpr(
6230 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, T,
6231 OCE->getValueKind(), Importer.Import(OCE->getRParenLoc()),
6232 OCE->getFPFeatures());
6233 }
6234
Sean Callanan59721b32015-04-28 18:41:46 +00006235 return new (Importer.getToContext())
6236 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00006237 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00006238 Importer.Import(E->getRParenLoc()));
6239}
6240
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006241Optional<LambdaCapture>
6242ASTNodeImporter::ImportLambdaCapture(const LambdaCapture &From) {
6243 VarDecl *Var = nullptr;
6244 if (From.capturesVariable()) {
6245 Var = cast_or_null<VarDecl>(Importer.Import(From.getCapturedVar()));
6246 if (!Var)
6247 return None;
6248 }
6249
6250 return LambdaCapture(Importer.Import(From.getLocation()), From.isImplicit(),
6251 From.getCaptureKind(), Var,
6252 From.isPackExpansion()
6253 ? Importer.Import(From.getEllipsisLoc())
6254 : SourceLocation());
6255}
6256
6257Expr *ASTNodeImporter::VisitLambdaExpr(LambdaExpr *LE) {
6258 CXXRecordDecl *FromClass = LE->getLambdaClass();
6259 auto *ToClass = dyn_cast_or_null<CXXRecordDecl>(Importer.Import(FromClass));
6260 if (!ToClass)
6261 return nullptr;
6262
6263 // NOTE: lambda classes are created with BeingDefined flag set up.
6264 // It means that ImportDefinition doesn't work for them and we should fill it
6265 // manually.
6266 if (ToClass->isBeingDefined()) {
6267 for (auto FromField : FromClass->fields()) {
6268 auto *ToField = cast_or_null<FieldDecl>(Importer.Import(FromField));
6269 if (!ToField)
6270 return nullptr;
6271 }
6272 }
6273
6274 auto *ToCallOp = dyn_cast_or_null<CXXMethodDecl>(
6275 Importer.Import(LE->getCallOperator()));
6276 if (!ToCallOp)
6277 return nullptr;
6278
6279 ToClass->completeDefinition();
6280
6281 unsigned NumCaptures = LE->capture_size();
6282 SmallVector<LambdaCapture, 8> Captures;
6283 Captures.reserve(NumCaptures);
6284 for (const auto &FromCapture : LE->captures()) {
6285 if (auto ToCapture = ImportLambdaCapture(FromCapture))
6286 Captures.push_back(*ToCapture);
6287 else
6288 return nullptr;
6289 }
6290
6291 SmallVector<Expr *, 8> InitCaptures(NumCaptures);
6292 if (ImportContainerChecked(LE->capture_inits(), InitCaptures))
6293 return nullptr;
6294
6295 return LambdaExpr::Create(Importer.getToContext(), ToClass,
6296 Importer.Import(LE->getIntroducerRange()),
6297 LE->getCaptureDefault(),
6298 Importer.Import(LE->getCaptureDefaultLoc()),
6299 Captures,
6300 LE->hasExplicitParameters(),
6301 LE->hasExplicitResultType(),
6302 InitCaptures,
6303 Importer.Import(LE->getLocEnd()),
6304 LE->containsUnexpandedParameterPack());
6305}
6306
6307
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006308Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
6309 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00006310 if (T.isNull())
6311 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006312
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006313 llvm::SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006314 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006315 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006316
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006317 ASTContext &ToCtx = Importer.getToContext();
6318 InitListExpr *To = new (ToCtx) InitListExpr(
6319 ToCtx, Importer.Import(ILE->getLBraceLoc()),
6320 Exprs, Importer.Import(ILE->getLBraceLoc()));
6321 To->setType(T);
6322
6323 if (ILE->hasArrayFiller()) {
6324 Expr *Filler = Importer.Import(ILE->getArrayFiller());
6325 if (!Filler)
6326 return nullptr;
6327 To->setArrayFiller(Filler);
6328 }
6329
6330 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
6331 FieldDecl *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
6332 if (!ToFD)
6333 return nullptr;
6334 To->setInitializedFieldInUnion(ToFD);
6335 }
6336
6337 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
6338 InitListExpr *ToSyntForm = cast_or_null<InitListExpr>(
6339 Importer.Import(SyntForm));
6340 if (!ToSyntForm)
6341 return nullptr;
6342 To->setSyntacticForm(ToSyntForm);
6343 }
6344
6345 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
6346 To->setValueDependent(ILE->isValueDependent());
6347 To->setInstantiationDependent(ILE->isInstantiationDependent());
6348
6349 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00006350}
6351
Richard Smith30e304e2016-12-14 00:03:17 +00006352Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
6353 QualType ToType = Importer.Import(E->getType());
6354 if (ToType.isNull())
6355 return nullptr;
6356
6357 Expr *ToCommon = Importer.Import(E->getCommonExpr());
6358 if (!ToCommon && E->getCommonExpr())
6359 return nullptr;
6360
6361 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
6362 if (!ToSubExpr && E->getSubExpr())
6363 return nullptr;
6364
6365 return new (Importer.getToContext())
6366 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
6367}
6368
6369Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
6370 QualType ToType = Importer.Import(E->getType());
6371 if (ToType.isNull())
6372 return nullptr;
6373 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
6374}
6375
Sean Callanandd2c1742016-05-16 20:48:03 +00006376Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
6377 FieldDecl *ToField = llvm::dyn_cast_or_null<FieldDecl>(
6378 Importer.Import(DIE->getField()));
6379 if (!ToField && DIE->getField())
6380 return nullptr;
6381
6382 return CXXDefaultInitExpr::Create(
6383 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
6384}
6385
6386Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
6387 QualType ToType = Importer.Import(E->getType());
6388 if (ToType.isNull() && !E->getType().isNull())
6389 return nullptr;
6390 ExprValueKind VK = E->getValueKind();
6391 CastKind CK = E->getCastKind();
6392 Expr *ToOp = Importer.Import(E->getSubExpr());
6393 if (!ToOp && E->getSubExpr())
6394 return nullptr;
6395 CXXCastPath BasePath;
6396 if (ImportCastPath(E, BasePath))
6397 return nullptr;
6398 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6399 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6400 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6401 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
6402
6403 if (isa<CXXStaticCastExpr>(E)) {
6404 return CXXStaticCastExpr::Create(
6405 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6406 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6407 } else if (isa<CXXDynamicCastExpr>(E)) {
6408 return CXXDynamicCastExpr::Create(
6409 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6410 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6411 } else if (isa<CXXReinterpretCastExpr>(E)) {
6412 return CXXReinterpretCastExpr::Create(
6413 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6414 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6415 } else {
6416 return nullptr;
6417 }
6418}
6419
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006420
6421Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
6422 SubstNonTypeTemplateParmExpr *E) {
6423 QualType T = Importer.Import(E->getType());
6424 if (T.isNull())
6425 return nullptr;
6426
6427 NonTypeTemplateParmDecl *Param = cast_or_null<NonTypeTemplateParmDecl>(
6428 Importer.Import(E->getParameter()));
6429 if (!Param)
6430 return nullptr;
6431
6432 Expr *Replacement = Importer.Import(E->getReplacement());
6433 if (!Replacement)
6434 return nullptr;
6435
6436 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
6437 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
6438 Replacement);
6439}
6440
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00006441Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
6442 QualType ToType = Importer.Import(E->getType());
6443 if (ToType.isNull())
6444 return nullptr;
6445
6446 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
6447 if (ImportContainerChecked(E->getArgs(), ToArgs))
6448 return nullptr;
6449
6450 // According to Sema::BuildTypeTrait(), if E is value-dependent,
6451 // Value is always false.
6452 bool ToValue = false;
6453 if (!E->isValueDependent())
6454 ToValue = E->getValue();
6455
6456 return TypeTraitExpr::Create(
6457 Importer.getToContext(), ToType, Importer.Import(E->getLocStart()),
6458 E->getTrait(), ToArgs, Importer.Import(E->getLocEnd()), ToValue);
6459}
6460
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006461Expr *ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
6462 QualType ToType = Importer.Import(E->getType());
6463 if (ToType.isNull())
6464 return nullptr;
6465
6466 if (E->isTypeOperand()) {
6467 TypeSourceInfo *TSI = Importer.Import(E->getTypeOperandSourceInfo());
6468 if (!TSI)
6469 return nullptr;
6470
6471 return new (Importer.getToContext())
6472 CXXTypeidExpr(ToType, TSI, Importer.Import(E->getSourceRange()));
6473 }
6474
6475 Expr *Op = Importer.Import(E->getExprOperand());
6476 if (!Op)
6477 return nullptr;
6478
6479 return new (Importer.getToContext())
6480 CXXTypeidExpr(ToType, Op, Importer.Import(E->getSourceRange()));
6481}
6482
Lang Hames19e07e12017-06-20 21:06:00 +00006483void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
6484 CXXMethodDecl *FromMethod) {
6485 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
6486 ToMethod->addOverriddenMethod(
6487 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
6488 FromOverriddenMethod))));
6489}
6490
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006491ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006492 ASTContext &FromContext, FileManager &FromFileManager,
6493 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00006494 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00006495 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006496 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00006497{
Douglas Gregor62d311f2010-02-09 19:21:46 +00006498 ImportedDecls[FromContext.getTranslationUnitDecl()]
6499 = ToContext.getTranslationUnitDecl();
6500}
6501
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00006502ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00006503
6504QualType ASTImporter::Import(QualType FromT) {
6505 if (FromT.isNull())
6506 return QualType();
John McCall424cec92011-01-19 06:33:43 +00006507
6508 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00006509
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006510 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006511 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6512 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006513 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006514 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006515
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006516 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006517 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006518 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006519 if (ToT.isNull())
6520 return ToT;
6521
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006522 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006523 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006524
John McCall424cec92011-01-19 06:33:43 +00006525 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006526}
6527
Douglas Gregor62d311f2010-02-09 19:21:46 +00006528TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006529 if (!FromTSI)
6530 return FromTSI;
6531
6532 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006533 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006534 QualType T = Import(FromTSI->getType());
6535 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006536 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006537
6538 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006539 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006540}
6541
Sean Callanan59721b32015-04-28 18:41:46 +00006542Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6543 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6544 if (Pos != ImportedDecls.end()) {
6545 Decl *ToD = Pos->second;
6546 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6547 return ToD;
6548 } else {
6549 return nullptr;
6550 }
6551}
6552
Douglas Gregor62d311f2010-02-09 19:21:46 +00006553Decl *ASTImporter::Import(Decl *FromD) {
6554 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006555 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006556
Douglas Gregord451ea92011-07-29 23:31:30 +00006557 ASTNodeImporter Importer(*this);
6558
Douglas Gregor62d311f2010-02-09 19:21:46 +00006559 // Check whether we've already imported this declaration.
6560 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006561 if (Pos != ImportedDecls.end()) {
6562 Decl *ToD = Pos->second;
6563 Importer.ImportDefinitionIfNeeded(FromD, ToD);
6564 return ToD;
6565 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00006566
6567 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00006568 Decl *ToD = Importer.Visit(FromD);
6569 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006570 return nullptr;
6571
Douglas Gregor62d311f2010-02-09 19:21:46 +00006572 // Record the imported declaration.
6573 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00006574
6575 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
6576 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00006577 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00006578 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00006579 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006580 // When we've finished transforming a typedef, see whether it was the
6581 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00006582 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00006583 FromTag = AnonTagsWithPendingTypedefs.begin(),
6584 FromTagEnd = AnonTagsWithPendingTypedefs.end();
6585 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00006586 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006587 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
6588 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00006589 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00006590 AnonTagsWithPendingTypedefs.erase(FromTag);
6591 break;
6592 }
6593 }
6594 }
6595 }
6596
Douglas Gregor62d311f2010-02-09 19:21:46 +00006597 return ToD;
6598}
6599
6600DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6601 if (!FromDC)
6602 return FromDC;
6603
Douglas Gregor95d82832012-01-24 18:36:04 +00006604 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006605 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006606 return nullptr;
6607
Douglas Gregor2e15c842012-02-01 21:00:38 +00006608 // When we're using a record/enum/Objective-C class/protocol as a context, we
6609 // need it to have a definition.
6610 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00006611 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006612 if (ToRecord->isCompleteDefinition()) {
6613 // Do nothing.
6614 } else if (FromRecord->isCompleteDefinition()) {
6615 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6616 ASTNodeImporter::IDK_Basic);
6617 } else {
6618 CompleteDecl(ToRecord);
6619 }
6620 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6621 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
6622 if (ToEnum->isCompleteDefinition()) {
6623 // Do nothing.
6624 } else if (FromEnum->isCompleteDefinition()) {
6625 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6626 ASTNodeImporter::IDK_Basic);
6627 } else {
6628 CompleteDecl(ToEnum);
6629 }
6630 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6631 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
6632 if (ToClass->getDefinition()) {
6633 // Do nothing.
6634 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6635 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6636 ASTNodeImporter::IDK_Basic);
6637 } else {
6638 CompleteDecl(ToClass);
6639 }
6640 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6641 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
6642 if (ToProto->getDefinition()) {
6643 // Do nothing.
6644 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6645 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6646 ASTNodeImporter::IDK_Basic);
6647 } else {
6648 CompleteDecl(ToProto);
6649 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006650 }
6651
6652 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006653}
6654
6655Expr *ASTImporter::Import(Expr *FromE) {
6656 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00006657 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006658
6659 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
6660}
6661
6662Stmt *ASTImporter::Import(Stmt *FromS) {
6663 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00006664 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006665
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006666 // Check whether we've already imported this declaration.
6667 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
6668 if (Pos != ImportedStmts.end())
6669 return Pos->second;
6670
6671 // Import the type
6672 ASTNodeImporter Importer(*this);
6673 Stmt *ToS = Importer.Visit(FromS);
6674 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00006675 return nullptr;
6676
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006677 // Record the imported declaration.
6678 ImportedStmts[FromS] = ToS;
6679 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006680}
6681
6682NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
6683 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00006684 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006685
Douglas Gregor90ebf252011-04-27 16:48:40 +00006686 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
6687
6688 switch (FromNNS->getKind()) {
6689 case NestedNameSpecifier::Identifier:
6690 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
6691 return NestedNameSpecifier::Create(ToContext, prefix, II);
6692 }
Craig Topper36250ad2014-05-12 05:36:57 +00006693 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006694
6695 case NestedNameSpecifier::Namespace:
6696 if (NamespaceDecl *NS =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006697 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006698 return NestedNameSpecifier::Create(ToContext, prefix, NS);
6699 }
Craig Topper36250ad2014-05-12 05:36:57 +00006700 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006701
6702 case NestedNameSpecifier::NamespaceAlias:
6703 if (NamespaceAliasDecl *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006704 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006705 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
6706 }
Craig Topper36250ad2014-05-12 05:36:57 +00006707 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006708
6709 case NestedNameSpecifier::Global:
6710 return NestedNameSpecifier::GlobalSpecifier(ToContext);
6711
Nikola Smiljanic67860242014-09-26 00:28:20 +00006712 case NestedNameSpecifier::Super:
6713 if (CXXRecordDecl *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006714 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00006715 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
6716 }
6717 return nullptr;
6718
Douglas Gregor90ebf252011-04-27 16:48:40 +00006719 case NestedNameSpecifier::TypeSpec:
6720 case NestedNameSpecifier::TypeSpecWithTemplate: {
6721 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
6722 if (!T.isNull()) {
6723 bool bTemplate = FromNNS->getKind() ==
6724 NestedNameSpecifier::TypeSpecWithTemplate;
6725 return NestedNameSpecifier::Create(ToContext, prefix,
6726 bTemplate, T.getTypePtr());
6727 }
6728 }
Craig Topper36250ad2014-05-12 05:36:57 +00006729 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006730 }
6731
6732 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00006733}
6734
Douglas Gregor14454802011-02-25 02:25:35 +00006735NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006736 // Copied from NestedNameSpecifier mostly.
6737 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
6738 NestedNameSpecifierLoc NNS = FromNNS;
6739
6740 // Push each of the nested-name-specifiers's onto a stack for
6741 // serialization in reverse order.
6742 while (NNS) {
6743 NestedNames.push_back(NNS);
6744 NNS = NNS.getPrefix();
6745 }
6746
6747 NestedNameSpecifierLocBuilder Builder;
6748
6749 while (!NestedNames.empty()) {
6750 NNS = NestedNames.pop_back_val();
6751 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
6752 if (!Spec)
6753 return NestedNameSpecifierLoc();
6754
6755 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
6756 switch (Kind) {
6757 case NestedNameSpecifier::Identifier:
6758 Builder.Extend(getToContext(),
6759 Spec->getAsIdentifier(),
6760 Import(NNS.getLocalBeginLoc()),
6761 Import(NNS.getLocalEndLoc()));
6762 break;
6763
6764 case NestedNameSpecifier::Namespace:
6765 Builder.Extend(getToContext(),
6766 Spec->getAsNamespace(),
6767 Import(NNS.getLocalBeginLoc()),
6768 Import(NNS.getLocalEndLoc()));
6769 break;
6770
6771 case NestedNameSpecifier::NamespaceAlias:
6772 Builder.Extend(getToContext(),
6773 Spec->getAsNamespaceAlias(),
6774 Import(NNS.getLocalBeginLoc()),
6775 Import(NNS.getLocalEndLoc()));
6776 break;
6777
6778 case NestedNameSpecifier::TypeSpec:
6779 case NestedNameSpecifier::TypeSpecWithTemplate: {
6780 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
6781 QualType(Spec->getAsType(), 0));
6782 Builder.Extend(getToContext(),
6783 Import(NNS.getLocalBeginLoc()),
6784 TSI->getTypeLoc(),
6785 Import(NNS.getLocalEndLoc()));
6786 break;
6787 }
6788
6789 case NestedNameSpecifier::Global:
6790 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
6791 break;
6792
6793 case NestedNameSpecifier::Super: {
6794 SourceRange ToRange = Import(NNS.getSourceRange());
6795 Builder.MakeSuper(getToContext(),
6796 Spec->getAsRecordDecl(),
6797 ToRange.getBegin(),
6798 ToRange.getEnd());
6799 }
6800 }
6801 }
6802
6803 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00006804}
6805
Douglas Gregore2e50d332010-12-01 01:36:18 +00006806TemplateName ASTImporter::Import(TemplateName From) {
6807 switch (From.getKind()) {
6808 case TemplateName::Template:
6809 if (TemplateDecl *ToTemplate
6810 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6811 return TemplateName(ToTemplate);
6812
6813 return TemplateName();
6814
6815 case TemplateName::OverloadedTemplate: {
6816 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
6817 UnresolvedSet<2> ToTemplates;
6818 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
6819 E = FromStorage->end();
6820 I != E; ++I) {
6821 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
6822 ToTemplates.addDecl(To);
6823 else
6824 return TemplateName();
6825 }
6826 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
6827 ToTemplates.end());
6828 }
6829
6830 case TemplateName::QualifiedTemplate: {
6831 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
6832 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
6833 if (!Qualifier)
6834 return TemplateName();
6835
6836 if (TemplateDecl *ToTemplate
6837 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6838 return ToContext.getQualifiedTemplateName(Qualifier,
6839 QTN->hasTemplateKeyword(),
6840 ToTemplate);
6841
6842 return TemplateName();
6843 }
6844
6845 case TemplateName::DependentTemplate: {
6846 DependentTemplateName *DTN = From.getAsDependentTemplateName();
6847 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
6848 if (!Qualifier)
6849 return TemplateName();
6850
6851 if (DTN->isIdentifier()) {
6852 return ToContext.getDependentTemplateName(Qualifier,
6853 Import(DTN->getIdentifier()));
6854 }
6855
6856 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
6857 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006858
6859 case TemplateName::SubstTemplateTemplateParm: {
6860 SubstTemplateTemplateParmStorage *subst
6861 = From.getAsSubstTemplateTemplateParm();
6862 TemplateTemplateParmDecl *param
6863 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
6864 if (!param)
6865 return TemplateName();
6866
6867 TemplateName replacement = Import(subst->getReplacement());
6868 if (replacement.isNull()) return TemplateName();
6869
6870 return ToContext.getSubstTemplateTemplateParm(param, replacement);
6871 }
Douglas Gregor5590be02011-01-15 06:45:20 +00006872
6873 case TemplateName::SubstTemplateTemplateParmPack: {
6874 SubstTemplateTemplateParmPackStorage *SubstPack
6875 = From.getAsSubstTemplateTemplateParmPack();
6876 TemplateTemplateParmDecl *Param
6877 = cast_or_null<TemplateTemplateParmDecl>(
6878 Import(SubstPack->getParameterPack()));
6879 if (!Param)
6880 return TemplateName();
6881
6882 ASTNodeImporter Importer(*this);
6883 TemplateArgument ArgPack
6884 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
6885 if (ArgPack.isNull())
6886 return TemplateName();
6887
6888 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
6889 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00006890 }
6891
6892 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00006893}
6894
Douglas Gregor62d311f2010-02-09 19:21:46 +00006895SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
6896 if (FromLoc.isInvalid())
6897 return SourceLocation();
6898
Douglas Gregor811663e2010-02-10 00:15:17 +00006899 SourceManager &FromSM = FromContext.getSourceManager();
6900
Sean Callanan24c5fe62016-11-07 20:42:25 +00006901 // For now, map everything down to its file location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00006902 // don't have to import macro expansions.
6903 // FIXME: Import macro expansions!
Sean Callanan24c5fe62016-11-07 20:42:25 +00006904 FromLoc = FromSM.getFileLoc(FromLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00006905 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
6906 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00006907 FileID ToFileID = Import(Decomposed.first);
6908 if (ToFileID.isInvalid())
6909 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00006910 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
6911 .getLocWithOffset(Decomposed.second);
6912 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006913}
6914
6915SourceRange ASTImporter::Import(SourceRange FromRange) {
6916 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
6917}
6918
Douglas Gregor811663e2010-02-10 00:15:17 +00006919FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00006920 llvm::DenseMap<FileID, FileID>::iterator Pos
6921 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00006922 if (Pos != ImportedFileIDs.end())
6923 return Pos->second;
6924
6925 SourceManager &FromSM = FromContext.getSourceManager();
6926 SourceManager &ToSM = ToContext.getSourceManager();
6927 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00006928 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00006929
6930 // Include location of this file.
6931 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
6932
6933 // Map the FileID for to the "to" source manager.
6934 FileID ToID;
6935 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00006936 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00006937 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
6938 // disk again
6939 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
6940 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00006941 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00006942 if (!Entry)
6943 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00006944 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
6945 FromSLoc.getFile().getFileCharacteristic());
6946 } else {
6947 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006948 const llvm::MemoryBuffer *
6949 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006950 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00006951 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00006952 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00006953 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006954 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00006955 }
6956
6957
Sebastian Redl99219f12010-09-30 01:03:06 +00006958 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00006959 return ToID;
6960}
6961
Sean Callanandd2c1742016-05-16 20:48:03 +00006962CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
6963 Expr *ToExpr = Import(From->getInit());
6964 if (!ToExpr && From->getInit())
6965 return nullptr;
6966
6967 if (From->isBaseInitializer()) {
6968 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6969 if (!ToTInfo && From->getTypeSourceInfo())
6970 return nullptr;
6971
6972 return new (ToContext) CXXCtorInitializer(
6973 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
6974 ToExpr, Import(From->getRParenLoc()),
6975 From->isPackExpansion() ? Import(From->getEllipsisLoc())
6976 : SourceLocation());
6977 } else if (From->isMemberInitializer()) {
6978 FieldDecl *ToField =
6979 llvm::cast_or_null<FieldDecl>(Import(From->getMember()));
6980 if (!ToField && From->getMember())
6981 return nullptr;
6982
6983 return new (ToContext) CXXCtorInitializer(
6984 ToContext, ToField, Import(From->getMemberLocation()),
6985 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6986 } else if (From->isIndirectMemberInitializer()) {
6987 IndirectFieldDecl *ToIField = llvm::cast_or_null<IndirectFieldDecl>(
6988 Import(From->getIndirectMember()));
6989 if (!ToIField && From->getIndirectMember())
6990 return nullptr;
6991
6992 return new (ToContext) CXXCtorInitializer(
6993 ToContext, ToIField, Import(From->getMemberLocation()),
6994 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6995 } else if (From->isDelegatingInitializer()) {
6996 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6997 if (!ToTInfo && From->getTypeSourceInfo())
6998 return nullptr;
6999
7000 return new (ToContext)
7001 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
7002 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007003 } else {
7004 return nullptr;
7005 }
7006}
7007
7008
Aleksei Sidorina693b372016-09-28 10:16:56 +00007009CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
7010 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
7011 if (Pos != ImportedCXXBaseSpecifiers.end())
7012 return Pos->second;
7013
7014 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
7015 Import(BaseSpec->getSourceRange()),
7016 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
7017 BaseSpec->getAccessSpecifierAsWritten(),
7018 Import(BaseSpec->getTypeSourceInfo()),
7019 Import(BaseSpec->getEllipsisLoc()));
7020 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
7021 return Imported;
7022}
7023
Douglas Gregor0a791672011-01-18 03:11:38 +00007024void ASTImporter::ImportDefinition(Decl *From) {
7025 Decl *To = Import(From);
7026 if (!To)
7027 return;
7028
7029 if (DeclContext *FromDC = cast<DeclContext>(From)) {
7030 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007031
7032 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
7033 if (!ToRecord->getDefinition()) {
7034 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00007035 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007036 return;
7037 }
7038 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007039
7040 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
7041 if (!ToEnum->getDefinition()) {
7042 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007043 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00007044 return;
7045 }
7046 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00007047
7048 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
7049 if (!ToIFace->getDefinition()) {
7050 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007051 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007052 return;
7053 }
7054 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007055
Douglas Gregor2aa53772012-01-24 17:42:07 +00007056 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
7057 if (!ToProto->getDefinition()) {
7058 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007059 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007060 return;
7061 }
7062 }
7063
Douglas Gregor0a791672011-01-18 03:11:38 +00007064 Importer.ImportDeclContext(FromDC, true);
7065 }
7066}
7067
Douglas Gregor96e578d2010-02-05 17:54:41 +00007068DeclarationName ASTImporter::Import(DeclarationName FromName) {
7069 if (!FromName)
7070 return DeclarationName();
7071
7072 switch (FromName.getNameKind()) {
7073 case DeclarationName::Identifier:
7074 return Import(FromName.getAsIdentifierInfo());
7075
7076 case DeclarationName::ObjCZeroArgSelector:
7077 case DeclarationName::ObjCOneArgSelector:
7078 case DeclarationName::ObjCMultiArgSelector:
7079 return Import(FromName.getObjCSelector());
7080
7081 case DeclarationName::CXXConstructorName: {
7082 QualType T = Import(FromName.getCXXNameType());
7083 if (T.isNull())
7084 return DeclarationName();
7085
7086 return ToContext.DeclarationNames.getCXXConstructorName(
7087 ToContext.getCanonicalType(T));
7088 }
7089
7090 case DeclarationName::CXXDestructorName: {
7091 QualType T = Import(FromName.getCXXNameType());
7092 if (T.isNull())
7093 return DeclarationName();
7094
7095 return ToContext.DeclarationNames.getCXXDestructorName(
7096 ToContext.getCanonicalType(T));
7097 }
7098
Richard Smith35845152017-02-07 01:37:30 +00007099 case DeclarationName::CXXDeductionGuideName: {
7100 TemplateDecl *Template = cast_or_null<TemplateDecl>(
7101 Import(FromName.getCXXDeductionGuideTemplate()));
7102 if (!Template)
7103 return DeclarationName();
7104 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
7105 }
7106
Douglas Gregor96e578d2010-02-05 17:54:41 +00007107 case DeclarationName::CXXConversionFunctionName: {
7108 QualType T = Import(FromName.getCXXNameType());
7109 if (T.isNull())
7110 return DeclarationName();
7111
7112 return ToContext.DeclarationNames.getCXXConversionFunctionName(
7113 ToContext.getCanonicalType(T));
7114 }
7115
7116 case DeclarationName::CXXOperatorName:
7117 return ToContext.DeclarationNames.getCXXOperatorName(
7118 FromName.getCXXOverloadedOperator());
7119
7120 case DeclarationName::CXXLiteralOperatorName:
7121 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
7122 Import(FromName.getCXXLiteralIdentifier()));
7123
7124 case DeclarationName::CXXUsingDirective:
7125 // FIXME: STATICS!
7126 return DeclarationName::getUsingDirectiveName();
7127 }
7128
David Blaikiee4d798f2012-01-20 21:50:17 +00007129 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00007130}
7131
Douglas Gregore2e50d332010-12-01 01:36:18 +00007132IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007133 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00007134 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007135
Sean Callananf94ef1d2016-05-14 06:11:19 +00007136 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
7137
7138 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
7139 ToId->setBuiltinID(FromId->getBuiltinID());
7140
7141 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007142}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007143
Douglas Gregor43f54792010-02-17 02:12:47 +00007144Selector ASTImporter::Import(Selector FromSel) {
7145 if (FromSel.isNull())
7146 return Selector();
7147
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007148 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00007149 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
7150 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
7151 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
7152 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
7153}
7154
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007155DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
7156 DeclContext *DC,
7157 unsigned IDNS,
7158 NamedDecl **Decls,
7159 unsigned NumDecls) {
7160 return Name;
7161}
7162
7163DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007164 if (LastDiagFromFrom)
7165 ToContext.getDiagnostics().notePriorDiagnosticFrom(
7166 FromContext.getDiagnostics());
7167 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007168 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007169}
7170
7171DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007172 if (!LastDiagFromFrom)
7173 FromContext.getDiagnostics().notePriorDiagnosticFrom(
7174 ToContext.getDiagnostics());
7175 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007176 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007177}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007178
Douglas Gregor2e15c842012-02-01 21:00:38 +00007179void ASTImporter::CompleteDecl (Decl *D) {
7180 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
7181 if (!ID->getDefinition())
7182 ID->startDefinition();
7183 }
7184 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
7185 if (!PD->getDefinition())
7186 PD->startDefinition();
7187 }
7188 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
7189 if (!TD->getDefinition() && !TD->isBeingDefined()) {
7190 TD->startDefinition();
7191 TD->setCompleteDefinition(true);
7192 }
7193 }
7194 else {
7195 assert (0 && "CompleteDecl called on a Decl that can't be completed");
7196 }
7197}
7198
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007199Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00007200 if (From->hasAttrs()) {
7201 for (Attr *FromAttr : From->getAttrs())
7202 To->addAttr(FromAttr->clone(To->getASTContext()));
7203 }
7204 if (From->isUsed()) {
7205 To->setIsUsed();
7206 }
Sean Callanandd2c1742016-05-16 20:48:03 +00007207 if (From->isImplicit()) {
7208 To->setImplicit();
7209 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007210 ImportedDecls[From] = To;
7211 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00007212}
Douglas Gregorb4964f72010-02-15 23:54:17 +00007213
Douglas Gregordd6006f2012-07-17 21:16:27 +00007214bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
7215 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00007216 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00007217 = ImportedTypes.find(From.getTypePtr());
7218 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
7219 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00007220
Douglas Gregordd6006f2012-07-17 21:16:27 +00007221 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
7222 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00007223 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00007224}