blob: 89c58ceaee41b499924f6296606662455d8b88cf [file] [log] [blame]
Douglas Gregor96e578d2010-02-05 17:54:41 +00001//===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTImporter class which imports AST nodes from one
11// context into another context.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTImporter.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000015#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000016#include "clang/AST/ASTDiagnostic.h"
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +000017#include "clang/AST/ASTStructuralEquivalence.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000022#include "clang/AST/TypeVisitor.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000023#include "clang/Basic/FileManager.h"
24#include "clang/Basic/SourceManager.h"
25#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor3996e242010-02-15 22:01:00 +000026#include <deque>
Douglas Gregor96e578d2010-02-05 17:54:41 +000027
Douglas Gregor3c2404b2011-11-03 18:07:07 +000028namespace clang {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000029 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000030 public DeclVisitor<ASTNodeImporter, Decl *>,
31 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000032 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000033
Douglas Gregor96e578d2010-02-05 17:54:41 +000034 public:
35 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
36
37 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000038 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000039 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000040
41 // Importing types
John McCall424cec92011-01-19 06:33:43 +000042 QualType VisitType(const Type *T);
Gabor Horvath0866c2f2016-11-23 15:24:23 +000043 QualType VisitAtomicType(const AtomicType *T);
John McCall424cec92011-01-19 06:33:43 +000044 QualType VisitBuiltinType(const BuiltinType *T);
Aleksei Sidorina693b372016-09-28 10:16:56 +000045 QualType VisitDecayedType(const DecayedType *T);
John McCall424cec92011-01-19 06:33:43 +000046 QualType VisitComplexType(const ComplexType *T);
47 QualType VisitPointerType(const PointerType *T);
48 QualType VisitBlockPointerType(const BlockPointerType *T);
49 QualType VisitLValueReferenceType(const LValueReferenceType *T);
50 QualType VisitRValueReferenceType(const RValueReferenceType *T);
51 QualType VisitMemberPointerType(const MemberPointerType *T);
52 QualType VisitConstantArrayType(const ConstantArrayType *T);
53 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
54 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000055 // FIXME: DependentSizedArrayType
56 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +000057 QualType VisitVectorType(const VectorType *T);
58 QualType VisitExtVectorType(const ExtVectorType *T);
59 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
60 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +000061 QualType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
Sean Callananda6df8a2011-08-11 16:56:07 +000062 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +000063 QualType VisitTypedefType(const TypedefType *T);
64 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000065 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +000066 QualType VisitTypeOfType(const TypeOfType *T);
67 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +000068 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +000069 QualType VisitAutoType(const AutoType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000070 QualType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000071 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +000072 QualType VisitRecordType(const RecordType *T);
73 QualType VisitEnumType(const EnumType *T);
Sean Callanan72fe0852015-04-02 23:50:08 +000074 QualType VisitAttributedType(const AttributedType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000075 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
Aleksei Sidorin855086d2017-01-23 09:30:36 +000076 QualType VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T);
John McCall424cec92011-01-19 06:33:43 +000077 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
78 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000079 // FIXME: DependentNameType
Gabor Horvath7a91c082017-11-14 11:30:38 +000080 QualType VisitPackExpansionType(const PackExpansionType *T);
John McCallc392f372010-06-11 00:33:02 +000081 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000082 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
83 QualType VisitObjCObjectType(const ObjCObjectType *T);
84 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000085
Douglas Gregor95d82832012-01-24 18:36:04 +000086 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000087 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
88 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +000089 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +000090 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000091 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
92 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000093 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000094
Aleksei Sidorina693b372016-09-28 10:16:56 +000095 bool ImportCastPath(CastExpr *E, CXXCastPath &Path);
96
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000097 typedef DesignatedInitExpr::Designator Designator;
98 Designator ImportDesignator(const Designator &D);
99
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);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000297 Expr *VisitCXXNewExpr(CXXNewExpr *CE);
298 Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000299 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000300 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000301 Expr *VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +0000302 Expr *VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *CE);
303 Expr *VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000304 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC);
Sean Callanan8bca9962016-03-28 21:43:01 +0000305 Expr *VisitCXXThisExpr(CXXThisExpr *E);
306 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +0000307 Expr *VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000308 Expr *VisitMemberExpr(MemberExpr *E);
309 Expr *VisitCallExpr(CallExpr *E);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000310 Expr *VisitLambdaExpr(LambdaExpr *LE);
Sean Callanan8bca9962016-03-28 21:43:01 +0000311 Expr *VisitInitListExpr(InitListExpr *E);
Richard Smith30e304e2016-12-14 00:03:17 +0000312 Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
313 Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
Sean Callanandd2c1742016-05-16 20:48:03 +0000314 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
315 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000316 Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +0000317 Expr *VisitTypeTraitExpr(TypeTraitExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000318
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000319
320 template<typename IIter, typename OIter>
321 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
322 typedef typename std::remove_reference<decltype(*Obegin)>::type ItemT;
323 ASTImporter &ImporterRef = Importer;
324 std::transform(Ibegin, Iend, Obegin,
325 [&ImporterRef](ItemT From) -> ItemT {
326 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000327 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000328 }
329
330 template<typename IIter, typename OIter>
331 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
332 typedef typename std::remove_reference<decltype(**Obegin)>::type ItemT;
333 ASTImporter &ImporterRef = Importer;
334 bool Failed = false;
335 std::transform(Ibegin, Iend, Obegin,
336 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000337 ItemT *To = cast_or_null<ItemT>(
338 ImporterRef.Import(From));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000339 if (!To && From)
340 Failed = true;
341 return To;
342 });
343 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000344 }
Aleksei Sidorina693b372016-09-28 10:16:56 +0000345
346 template<typename InContainerTy, typename OutContainerTy>
347 bool ImportContainerChecked(const InContainerTy &InContainer,
348 OutContainerTy &OutContainer) {
349 return ImportArrayChecked(InContainer.begin(), InContainer.end(),
350 OutContainer.begin());
351 }
352
353 template<typename InContainerTy, typename OIter>
354 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
355 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
356 }
Lang Hames19e07e12017-06-20 21:06:00 +0000357
358 // Importing overrides.
359 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000360 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000361}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000362
Douglas Gregor3996e242010-02-15 22:01:00 +0000363//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000364// Import Types
365//----------------------------------------------------------------------------
366
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000367using namespace clang;
368
John McCall424cec92011-01-19 06:33:43 +0000369QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000370 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
371 << T->getTypeClassName();
372 return QualType();
373}
374
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000375QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
376 QualType UnderlyingType = Importer.Import(T->getValueType());
377 if(UnderlyingType.isNull())
378 return QualType();
379
380 return Importer.getToContext().getAtomicType(UnderlyingType);
381}
382
John McCall424cec92011-01-19 06:33:43 +0000383QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000384 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000385#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
386 case BuiltinType::Id: \
387 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000388#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000389#define SHARED_SINGLETON_TYPE(Expansion)
390#define BUILTIN_TYPE(Id, SingletonId) \
391 case BuiltinType::Id: return Importer.getToContext().SingletonId;
392#include "clang/AST/BuiltinTypes.def"
393
394 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
395 // context supports C++.
396
397 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
398 // context supports ObjC.
399
Douglas Gregor96e578d2010-02-05 17:54:41 +0000400 case BuiltinType::Char_U:
401 // The context we're importing from has an unsigned 'char'. If we're
402 // importing into a context with a signed 'char', translate to
403 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000404 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000405 return Importer.getToContext().UnsignedCharTy;
406
407 return Importer.getToContext().CharTy;
408
Douglas Gregor96e578d2010-02-05 17:54:41 +0000409 case BuiltinType::Char_S:
410 // The context we're importing from has an unsigned 'char'. If we're
411 // importing into a context with a signed 'char', translate to
412 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000413 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000414 return Importer.getToContext().SignedCharTy;
415
416 return Importer.getToContext().CharTy;
417
Chris Lattnerad3467e2010-12-25 23:25:43 +0000418 case BuiltinType::WChar_S:
419 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000420 // FIXME: If not in C++, shall we translate to the C equivalent of
421 // wchar_t?
422 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000423 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000424
425 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000426}
427
Aleksei Sidorina693b372016-09-28 10:16:56 +0000428QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
429 QualType OrigT = Importer.Import(T->getOriginalType());
430 if (OrigT.isNull())
431 return QualType();
432
433 return Importer.getToContext().getDecayedType(OrigT);
434}
435
John McCall424cec92011-01-19 06:33:43 +0000436QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000437 QualType ToElementType = Importer.Import(T->getElementType());
438 if (ToElementType.isNull())
439 return QualType();
440
441 return Importer.getToContext().getComplexType(ToElementType);
442}
443
John McCall424cec92011-01-19 06:33:43 +0000444QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000445 QualType ToPointeeType = Importer.Import(T->getPointeeType());
446 if (ToPointeeType.isNull())
447 return QualType();
448
449 return Importer.getToContext().getPointerType(ToPointeeType);
450}
451
John McCall424cec92011-01-19 06:33:43 +0000452QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000453 // FIXME: Check for blocks support in "to" context.
454 QualType ToPointeeType = Importer.Import(T->getPointeeType());
455 if (ToPointeeType.isNull())
456 return QualType();
457
458 return Importer.getToContext().getBlockPointerType(ToPointeeType);
459}
460
John McCall424cec92011-01-19 06:33:43 +0000461QualType
462ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000463 // FIXME: Check for C++ support in "to" context.
464 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
465 if (ToPointeeType.isNull())
466 return QualType();
467
468 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
469}
470
John McCall424cec92011-01-19 06:33:43 +0000471QualType
472ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000473 // FIXME: Check for C++0x support in "to" context.
474 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
475 if (ToPointeeType.isNull())
476 return QualType();
477
478 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
479}
480
John McCall424cec92011-01-19 06:33:43 +0000481QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000482 // FIXME: Check for C++ support in "to" context.
483 QualType ToPointeeType = Importer.Import(T->getPointeeType());
484 if (ToPointeeType.isNull())
485 return QualType();
486
487 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
488 return Importer.getToContext().getMemberPointerType(ToPointeeType,
489 ClassType.getTypePtr());
490}
491
John McCall424cec92011-01-19 06:33:43 +0000492QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000493 QualType ToElementType = Importer.Import(T->getElementType());
494 if (ToElementType.isNull())
495 return QualType();
496
497 return Importer.getToContext().getConstantArrayType(ToElementType,
498 T->getSize(),
499 T->getSizeModifier(),
500 T->getIndexTypeCVRQualifiers());
501}
502
John McCall424cec92011-01-19 06:33:43 +0000503QualType
504ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000505 QualType ToElementType = Importer.Import(T->getElementType());
506 if (ToElementType.isNull())
507 return QualType();
508
509 return Importer.getToContext().getIncompleteArrayType(ToElementType,
510 T->getSizeModifier(),
511 T->getIndexTypeCVRQualifiers());
512}
513
John McCall424cec92011-01-19 06:33:43 +0000514QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000515 QualType ToElementType = Importer.Import(T->getElementType());
516 if (ToElementType.isNull())
517 return QualType();
518
519 Expr *Size = Importer.Import(T->getSizeExpr());
520 if (!Size)
521 return QualType();
522
523 SourceRange Brackets = Importer.Import(T->getBracketsRange());
524 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
525 T->getSizeModifier(),
526 T->getIndexTypeCVRQualifiers(),
527 Brackets);
528}
529
John McCall424cec92011-01-19 06:33:43 +0000530QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000531 QualType ToElementType = Importer.Import(T->getElementType());
532 if (ToElementType.isNull())
533 return QualType();
534
535 return Importer.getToContext().getVectorType(ToElementType,
536 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +0000537 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000538}
539
John McCall424cec92011-01-19 06:33:43 +0000540QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000541 QualType ToElementType = Importer.Import(T->getElementType());
542 if (ToElementType.isNull())
543 return QualType();
544
545 return Importer.getToContext().getExtVectorType(ToElementType,
546 T->getNumElements());
547}
548
John McCall424cec92011-01-19 06:33:43 +0000549QualType
550ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000551 // FIXME: What happens if we're importing a function without a prototype
552 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +0000553 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000554 if (ToResultType.isNull())
555 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000556
Douglas Gregor96e578d2010-02-05 17:54:41 +0000557 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000558 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000559}
560
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +0000561QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000562 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000563 if (ToResultType.isNull())
564 return QualType();
565
566 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000567 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000568 for (const auto &A : T->param_types()) {
569 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000570 if (ArgType.isNull())
571 return QualType();
572 ArgTypes.push_back(ArgType);
573 }
574
575 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000576 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000577 for (const auto &E : T->exceptions()) {
578 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000579 if (ExceptionType.isNull())
580 return QualType();
581 ExceptionTypes.push_back(ExceptionType);
582 }
John McCalldb40c7f2010-12-14 08:05:40 +0000583
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000584 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
585 FunctionProtoType::ExtProtoInfo ToEPI;
586
587 ToEPI.ExtInfo = FromEPI.ExtInfo;
588 ToEPI.Variadic = FromEPI.Variadic;
589 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
590 ToEPI.TypeQuals = FromEPI.TypeQuals;
591 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +0000592 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
593 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
594 ToEPI.ExceptionSpec.NoexceptExpr =
595 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
596 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
597 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
598 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
599 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000600
Jordan Rose5c382722013-03-08 21:51:21 +0000601 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000602}
603
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000604QualType ASTNodeImporter::VisitUnresolvedUsingType(
605 const UnresolvedUsingType *T) {
606 UnresolvedUsingTypenameDecl *ToD = cast_or_null<UnresolvedUsingTypenameDecl>(
607 Importer.Import(T->getDecl()));
608 if (!ToD)
609 return QualType();
610
611 UnresolvedUsingTypenameDecl *ToPrevD =
612 cast_or_null<UnresolvedUsingTypenameDecl>(
613 Importer.Import(T->getDecl()->getPreviousDecl()));
614 if (!ToPrevD && T->getDecl()->getPreviousDecl())
615 return QualType();
616
617 return Importer.getToContext().getTypeDeclType(ToD, ToPrevD);
618}
619
Sean Callananda6df8a2011-08-11 16:56:07 +0000620QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
621 QualType ToInnerType = Importer.Import(T->getInnerType());
622 if (ToInnerType.isNull())
623 return QualType();
624
625 return Importer.getToContext().getParenType(ToInnerType);
626}
627
John McCall424cec92011-01-19 06:33:43 +0000628QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +0000629 TypedefNameDecl *ToDecl
630 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000631 if (!ToDecl)
632 return QualType();
633
634 return Importer.getToContext().getTypeDeclType(ToDecl);
635}
636
John McCall424cec92011-01-19 06:33:43 +0000637QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000638 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
639 if (!ToExpr)
640 return QualType();
641
642 return Importer.getToContext().getTypeOfExprType(ToExpr);
643}
644
John McCall424cec92011-01-19 06:33:43 +0000645QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000646 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
647 if (ToUnderlyingType.isNull())
648 return QualType();
649
650 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
651}
652
John McCall424cec92011-01-19 06:33:43 +0000653QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +0000654 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +0000655 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
656 if (!ToExpr)
657 return QualType();
658
Douglas Gregor81495f32012-02-12 18:42:33 +0000659 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
660 if (UnderlyingType.isNull())
661 return QualType();
662
663 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000664}
665
Alexis Hunte852b102011-05-24 22:41:36 +0000666QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
667 QualType ToBaseType = Importer.Import(T->getBaseType());
668 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
669 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
670 return QualType();
671
672 return Importer.getToContext().getUnaryTransformType(ToBaseType,
673 ToUnderlyingType,
674 T->getUTTKind());
675}
676
Richard Smith30482bc2011-02-20 03:19:35 +0000677QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +0000678 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +0000679 QualType FromDeduced = T->getDeducedType();
680 QualType ToDeduced;
681 if (!FromDeduced.isNull()) {
682 ToDeduced = Importer.Import(FromDeduced);
683 if (ToDeduced.isNull())
684 return QualType();
685 }
686
Richard Smithe301ba22015-11-11 02:02:15 +0000687 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +0000688 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +0000689}
690
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000691QualType ASTNodeImporter::VisitInjectedClassNameType(
692 const InjectedClassNameType *T) {
693 CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
694 if (!D)
695 return QualType();
696
697 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
698 if (InjType.isNull())
699 return QualType();
700
701 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
702 // See comments in InjectedClassNameType definition for details
703 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
704 enum {
705 TypeAlignmentInBits = 4,
706 TypeAlignment = 1 << TypeAlignmentInBits
707 };
708
709 return QualType(new (Importer.getToContext(), TypeAlignment)
710 InjectedClassNameType(D, InjType), 0);
711}
712
John McCall424cec92011-01-19 06:33:43 +0000713QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000714 RecordDecl *ToDecl
715 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
716 if (!ToDecl)
717 return QualType();
718
719 return Importer.getToContext().getTagDeclType(ToDecl);
720}
721
John McCall424cec92011-01-19 06:33:43 +0000722QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000723 EnumDecl *ToDecl
724 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
725 if (!ToDecl)
726 return QualType();
727
728 return Importer.getToContext().getTagDeclType(ToDecl);
729}
730
Sean Callanan72fe0852015-04-02 23:50:08 +0000731QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
732 QualType FromModifiedType = T->getModifiedType();
733 QualType FromEquivalentType = T->getEquivalentType();
734 QualType ToModifiedType;
735 QualType ToEquivalentType;
736
737 if (!FromModifiedType.isNull()) {
738 ToModifiedType = Importer.Import(FromModifiedType);
739 if (ToModifiedType.isNull())
740 return QualType();
741 }
742 if (!FromEquivalentType.isNull()) {
743 ToEquivalentType = Importer.Import(FromEquivalentType);
744 if (ToEquivalentType.isNull())
745 return QualType();
746 }
747
748 return Importer.getToContext().getAttributedType(T->getAttrKind(),
749 ToModifiedType, ToEquivalentType);
750}
751
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000752
753QualType ASTNodeImporter::VisitTemplateTypeParmType(
754 const TemplateTypeParmType *T) {
755 TemplateTypeParmDecl *ParmDecl =
756 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
757 if (!ParmDecl && T->getDecl())
758 return QualType();
759
760 return Importer.getToContext().getTemplateTypeParmType(
761 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
762}
763
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000764QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
765 const SubstTemplateTypeParmType *T) {
766 const TemplateTypeParmType *Replaced =
767 cast_or_null<TemplateTypeParmType>(Importer.Import(
768 QualType(T->getReplacedParameter(), 0)).getTypePtr());
769 if (!Replaced)
770 return QualType();
771
772 QualType Replacement = Importer.Import(T->getReplacementType());
773 if (Replacement.isNull())
774 return QualType();
775 Replacement = Replacement.getCanonicalType();
776
777 return Importer.getToContext().getSubstTemplateTypeParmType(
778 Replaced, Replacement);
779}
780
Douglas Gregore2e50d332010-12-01 01:36:18 +0000781QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +0000782 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000783 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
784 if (ToTemplate.isNull())
785 return QualType();
786
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000787 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +0000788 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
789 return QualType();
790
791 QualType ToCanonType;
792 if (!QualType(T, 0).isCanonical()) {
793 QualType FromCanonType
794 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
795 ToCanonType =Importer.Import(FromCanonType);
796 if (ToCanonType.isNull())
797 return QualType();
798 }
799 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +0000800 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +0000801 ToCanonType);
802}
803
John McCall424cec92011-01-19 06:33:43 +0000804QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +0000805 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000806 // Note: the qualifier in an ElaboratedType is optional.
807 if (T->getQualifier()) {
808 ToQualifier = Importer.Import(T->getQualifier());
809 if (!ToQualifier)
810 return QualType();
811 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000812
813 QualType ToNamedType = Importer.Import(T->getNamedType());
814 if (ToNamedType.isNull())
815 return QualType();
816
Abramo Bagnara6150c882010-05-11 21:36:43 +0000817 return Importer.getToContext().getElaboratedType(T->getKeyword(),
818 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000819}
820
Gabor Horvath7a91c082017-11-14 11:30:38 +0000821QualType ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
822 QualType Pattern = Importer.Import(T->getPattern());
823 if (Pattern.isNull())
824 return QualType();
825
826 return Importer.getToContext().getPackExpansionType(Pattern,
827 T->getNumExpansions());
828}
829
John McCall424cec92011-01-19 06:33:43 +0000830QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000831 ObjCInterfaceDecl *Class
832 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
833 if (!Class)
834 return QualType();
835
John McCall8b07ec22010-05-15 11:32:37 +0000836 return Importer.getToContext().getObjCInterfaceType(Class);
837}
838
John McCall424cec92011-01-19 06:33:43 +0000839QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000840 QualType ToBaseType = Importer.Import(T->getBaseType());
841 if (ToBaseType.isNull())
842 return QualType();
843
Douglas Gregore9d95f12015-07-07 03:57:35 +0000844 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +0000845 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +0000846 QualType ImportedTypeArg = Importer.Import(TypeArg);
847 if (ImportedTypeArg.isNull())
848 return QualType();
849
850 TypeArgs.push_back(ImportedTypeArg);
851 }
852
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000853 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000854 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000855 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000856 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000857 if (!Protocol)
858 return QualType();
859 Protocols.push_back(Protocol);
860 }
861
Douglas Gregore9d95f12015-07-07 03:57:35 +0000862 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +0000863 Protocols,
864 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000865}
866
John McCall424cec92011-01-19 06:33:43 +0000867QualType
868ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000869 QualType ToPointeeType = Importer.Import(T->getPointeeType());
870 if (ToPointeeType.isNull())
871 return QualType();
872
John McCall8b07ec22010-05-15 11:32:37 +0000873 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000874}
875
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000876//----------------------------------------------------------------------------
877// Import Declarations
878//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000879bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
880 DeclContext *&LexicalDC,
881 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +0000882 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000883 SourceLocation &Loc) {
884 // Import the context of this declaration.
885 DC = Importer.ImportContext(D->getDeclContext());
886 if (!DC)
887 return true;
888
889 LexicalDC = DC;
890 if (D->getDeclContext() != D->getLexicalDeclContext()) {
891 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
892 if (!LexicalDC)
893 return true;
894 }
895
896 // Import the name of this declaration.
897 Name = Importer.Import(D->getDeclName());
898 if (D->getDeclName() && !Name)
899 return true;
900
901 // Import the location of this declaration.
902 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +0000903 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000904 return false;
905}
906
Douglas Gregord451ea92011-07-29 23:31:30 +0000907void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
908 if (!FromD)
909 return;
910
911 if (!ToD) {
912 ToD = Importer.Import(FromD);
913 if (!ToD)
914 return;
915 }
916
917 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
918 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +0000919 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +0000920 ImportDefinition(FromRecord, ToRecord);
921 }
922 }
923 return;
924 }
925
926 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
927 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
928 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
929 ImportDefinition(FromEnum, ToEnum);
930 }
931 }
932 return;
933 }
934}
935
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000936void
937ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
938 DeclarationNameInfo& To) {
939 // NOTE: To.Name and To.Loc are already imported.
940 // We only have to import To.LocInfo.
941 switch (To.getName().getNameKind()) {
942 case DeclarationName::Identifier:
943 case DeclarationName::ObjCZeroArgSelector:
944 case DeclarationName::ObjCOneArgSelector:
945 case DeclarationName::ObjCMultiArgSelector:
946 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +0000947 case DeclarationName::CXXDeductionGuideName:
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000948 return;
949
950 case DeclarationName::CXXOperatorName: {
951 SourceRange Range = From.getCXXOperatorNameRange();
952 To.setCXXOperatorNameRange(Importer.Import(Range));
953 return;
954 }
955 case DeclarationName::CXXLiteralOperatorName: {
956 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
957 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
958 return;
959 }
960 case DeclarationName::CXXConstructorName:
961 case DeclarationName::CXXDestructorName:
962 case DeclarationName::CXXConversionFunctionName: {
963 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
964 To.setNamedTypeInfo(Importer.Import(FromTInfo));
965 return;
966 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000967 }
Douglas Gregor07216d12011-11-02 20:52:01 +0000968 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000969}
970
Douglas Gregor2e15c842012-02-01 21:00:38 +0000971void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +0000972 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +0000973 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +0000974 return;
975 }
976
Aaron Ballman629afae2014-03-07 19:56:05 +0000977 for (auto *From : FromDC->decls())
978 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +0000979}
980
Douglas Gregord451ea92011-07-29 23:31:30 +0000981bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000982 ImportDefinitionKind Kind) {
983 if (To->getDefinition() || To->isBeingDefined()) {
984 if (Kind == IDK_Everything)
985 ImportDeclContext(From, /*ForceImport=*/true);
986
Douglas Gregore2e50d332010-12-01 01:36:18 +0000987 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +0000988 }
Douglas Gregore2e50d332010-12-01 01:36:18 +0000989
990 To->startDefinition();
991
992 // Add base classes.
993 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
994 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000995
996 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
997 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
998 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +0000999 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001000 ToData.Aggregate = FromData.Aggregate;
1001 ToData.PlainOldData = FromData.PlainOldData;
1002 ToData.Empty = FromData.Empty;
1003 ToData.Polymorphic = FromData.Polymorphic;
1004 ToData.Abstract = FromData.Abstract;
1005 ToData.IsStandardLayout = FromData.IsStandardLayout;
1006 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1007 ToData.HasPrivateFields = FromData.HasPrivateFields;
1008 ToData.HasProtectedFields = FromData.HasProtectedFields;
1009 ToData.HasPublicFields = FromData.HasPublicFields;
1010 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001011 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001012 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001013 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001014 ToData.HasUninitializedReferenceMember
1015 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001016 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001017 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1018 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001019 ToData.NeedOverloadResolutionForCopyConstructor
1020 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001021 ToData.NeedOverloadResolutionForMoveConstructor
1022 = FromData.NeedOverloadResolutionForMoveConstructor;
1023 ToData.NeedOverloadResolutionForMoveAssignment
1024 = FromData.NeedOverloadResolutionForMoveAssignment;
1025 ToData.NeedOverloadResolutionForDestructor
1026 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001027 ToData.DefaultedCopyConstructorIsDeleted
1028 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001029 ToData.DefaultedMoveConstructorIsDeleted
1030 = FromData.DefaultedMoveConstructorIsDeleted;
1031 ToData.DefaultedMoveAssignmentIsDeleted
1032 = FromData.DefaultedMoveAssignmentIsDeleted;
1033 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001034 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1035 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001036 ToData.HasConstexprNonCopyMoveConstructor
1037 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001038 ToData.HasDefaultedDefaultConstructor
1039 = FromData.HasDefaultedDefaultConstructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001040 ToData.CanPassInRegisters = FromData.CanPassInRegisters;
Richard Smith561fb152012-02-25 07:33:38 +00001041 ToData.DefaultedDefaultConstructorIsConstexpr
1042 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001043 ToData.HasConstexprDefaultConstructor
1044 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001045 ToData.HasNonLiteralTypeFieldsOrBases
1046 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001047 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001048 ToData.UserProvidedDefaultConstructor
1049 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001050 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001051 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1052 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1053 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1054 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001055 ToData.ImplicitCopyAssignmentHasConstParam
1056 = FromData.ImplicitCopyAssignmentHasConstParam;
1057 ToData.HasDeclaredCopyConstructorWithConstParam
1058 = FromData.HasDeclaredCopyConstructorWithConstParam;
1059 ToData.HasDeclaredCopyAssignmentWithConstParam
1060 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001061
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001062 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001063 for (const auto &Base1 : FromCXX->bases()) {
1064 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001065 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001066 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001067
1068 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001069 if (Base1.isPackExpansion())
1070 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001071
1072 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00001073 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00001074
Douglas Gregore2e50d332010-12-01 01:36:18 +00001075 Bases.push_back(
1076 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00001077 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1078 Base1.isVirtual(),
1079 Base1.isBaseOfClass(),
1080 Base1.getAccessSpecifierAsWritten(),
1081 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00001082 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001083 }
1084 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001085 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001086 }
1087
Douglas Gregor2e15c842012-02-01 21:00:38 +00001088 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001089 ImportDeclContext(From, /*ForceImport=*/true);
1090
Douglas Gregore2e50d332010-12-01 01:36:18 +00001091 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001092 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001093}
1094
Larisse Voufo39a1e502013-08-06 01:03:05 +00001095bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1096 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00001097 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00001098 return false;
1099
1100 // FIXME: Can we really import any initializer? Alternatively, we could force
1101 // ourselves to import every declaration of a variable and then only use
1102 // getInit() here.
1103 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1104
1105 // FIXME: Other bits to merge?
1106
1107 return false;
1108}
1109
Douglas Gregord451ea92011-07-29 23:31:30 +00001110bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001111 ImportDefinitionKind Kind) {
1112 if (To->getDefinition() || To->isBeingDefined()) {
1113 if (Kind == IDK_Everything)
1114 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001115 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001116 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001117
1118 To->startDefinition();
1119
1120 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1121 if (T.isNull())
1122 return true;
1123
1124 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1125 if (ToPromotionType.isNull())
1126 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001127
1128 if (shouldForceImportDeclContext(Kind))
1129 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001130
1131 // FIXME: we might need to merge the number of positive or negative bits
1132 // if the enumerator lists don't match.
1133 To->completeDefinition(T, ToPromotionType,
1134 From->getNumPositiveBits(),
1135 From->getNumNegativeBits());
1136 return false;
1137}
1138
Douglas Gregora082a492010-11-30 19:14:50 +00001139TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1140 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001141 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1142 if (ImportContainerChecked(*Params, ToParams))
1143 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00001144
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001145 Expr *ToRequiresClause;
1146 if (Expr *const R = Params->getRequiresClause()) {
1147 ToRequiresClause = Importer.Import(R);
1148 if (!ToRequiresClause)
1149 return nullptr;
1150 } else {
1151 ToRequiresClause = nullptr;
1152 }
1153
Douglas Gregora082a492010-11-30 19:14:50 +00001154 return TemplateParameterList::Create(Importer.getToContext(),
1155 Importer.Import(Params->getTemplateLoc()),
1156 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00001157 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001158 Importer.Import(Params->getRAngleLoc()),
1159 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001160}
1161
Douglas Gregore2e50d332010-12-01 01:36:18 +00001162TemplateArgument
1163ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1164 switch (From.getKind()) {
1165 case TemplateArgument::Null:
1166 return TemplateArgument();
1167
1168 case TemplateArgument::Type: {
1169 QualType ToType = Importer.Import(From.getAsType());
1170 if (ToType.isNull())
1171 return TemplateArgument();
1172 return TemplateArgument(ToType);
1173 }
1174
1175 case TemplateArgument::Integral: {
1176 QualType ToType = Importer.Import(From.getIntegralType());
1177 if (ToType.isNull())
1178 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001179 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001180 }
1181
Eli Friedmanb826a002012-09-26 02:36:12 +00001182 case TemplateArgument::Declaration: {
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001183 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
1184 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1185 if (!To || ToType.isNull())
1186 return TemplateArgument();
1187 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00001188 }
1189
1190 case TemplateArgument::NullPtr: {
1191 QualType ToType = Importer.Import(From.getNullPtrType());
1192 if (ToType.isNull())
1193 return TemplateArgument();
1194 return TemplateArgument(ToType, /*isNullPtr*/true);
1195 }
1196
Douglas Gregore2e50d332010-12-01 01:36:18 +00001197 case TemplateArgument::Template: {
1198 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1199 if (ToTemplate.isNull())
1200 return TemplateArgument();
1201
1202 return TemplateArgument(ToTemplate);
1203 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001204
1205 case TemplateArgument::TemplateExpansion: {
1206 TemplateName ToTemplate
1207 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1208 if (ToTemplate.isNull())
1209 return TemplateArgument();
1210
Douglas Gregore1d60df2011-01-14 23:41:42 +00001211 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001212 }
1213
Douglas Gregore2e50d332010-12-01 01:36:18 +00001214 case TemplateArgument::Expression:
1215 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1216 return TemplateArgument(ToExpr);
1217 return TemplateArgument();
1218
1219 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001220 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001221 ToPack.reserve(From.pack_size());
1222 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1223 return TemplateArgument();
Benjamin Kramercce63472015-08-05 09:40:22 +00001224
1225 return TemplateArgument(
1226 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001227 }
1228 }
1229
1230 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00001231}
1232
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001233Optional<TemplateArgumentLoc>
1234ASTNodeImporter::ImportTemplateArgumentLoc(const TemplateArgumentLoc &TALoc) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001235 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1236 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1237 TemplateArgumentLocInfo ToInfo;
1238 if (Arg.getKind() == TemplateArgument::Expression) {
1239 Expr *E = Importer.Import(FromInfo.getAsExpr());
1240 ToInfo = TemplateArgumentLocInfo(E);
1241 if (!E)
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001242 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001243 } else if (Arg.getKind() == TemplateArgument::Type) {
1244 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1245 ToInfo = TemplateArgumentLocInfo(TSI);
1246 else
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001247 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001248 } else {
1249 ToInfo = TemplateArgumentLocInfo(
1250 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1251 Importer.Import(FromInfo.getTemplateNameLoc()),
1252 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1253 }
1254 return TemplateArgumentLoc(Arg, ToInfo);
1255}
1256
Douglas Gregore2e50d332010-12-01 01:36:18 +00001257bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1258 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001259 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001260 for (unsigned I = 0; I != NumFromArgs; ++I) {
1261 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1262 if (To.isNull() && !FromArgs[I].isNull())
1263 return true;
1264
1265 ToArgs.push_back(To);
1266 }
1267
1268 return false;
1269}
1270
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001271// We cannot use Optional<> pattern here and below because
1272// TemplateArgumentListInfo's operator new is declared as deleted so it cannot
1273// be stored in Optional.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001274template <typename InContainerTy>
1275bool ASTNodeImporter::ImportTemplateArgumentListInfo(
1276 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1277 for (const auto &FromLoc : Container) {
1278 if (auto ToLoc = ImportTemplateArgumentLoc(FromLoc))
1279 ToTAInfo.addArgument(*ToLoc);
1280 else
1281 return true;
1282 }
1283 return false;
1284}
1285
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001286template <typename InContainerTy>
1287bool ASTNodeImporter::ImportTemplateArgumentListInfo(
1288 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
1289 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
1290 TemplateArgumentListInfo ToTAInfo(Importer.Import(FromLAngleLoc),
1291 Importer.Import(FromRAngleLoc));
1292 if (ImportTemplateArgumentListInfo(Container, ToTAInfo))
1293 return true;
1294 Result = ToTAInfo;
1295 return false;
1296}
1297
Douglas Gregor5c73e912010-02-11 00:48:18 +00001298bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001299 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001300 // Eliminate a potential failure point where we attempt to re-import
1301 // something we're trying to import while completing ToRecord.
1302 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1303 if (ToOrigin) {
1304 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
1305 if (ToOriginRecord)
1306 ToRecord = ToOriginRecord;
1307 }
1308
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001309 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001310 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001311 Importer.getNonEquivalentDecls(),
1312 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001313 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001314}
1315
Larisse Voufo39a1e502013-08-06 01:03:05 +00001316bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1317 bool Complain) {
1318 StructuralEquivalenceContext Ctx(
1319 Importer.getFromContext(), Importer.getToContext(),
1320 Importer.getNonEquivalentDecls(), false, Complain);
1321 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
1322}
1323
Douglas Gregor98c10182010-02-12 22:17:39 +00001324bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001325 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001326 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001327 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001328 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001329}
1330
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001331bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1332 FunctionTemplateDecl *To) {
1333 StructuralEquivalenceContext Ctx(
1334 Importer.getFromContext(), Importer.getToContext(),
1335 Importer.getNonEquivalentDecls(), false, false);
1336 return Ctx.IsStructurallyEquivalent(From, To);
1337}
1338
Douglas Gregor91155082012-11-14 22:29:20 +00001339bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
1340 EnumConstantDecl *ToEC)
1341{
1342 const llvm::APSInt &FromVal = FromEC->getInitVal();
1343 const llvm::APSInt &ToVal = ToEC->getInitVal();
1344
1345 return FromVal.isSigned() == ToVal.isSigned() &&
1346 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1347 FromVal == ToVal;
1348}
1349
1350bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001351 ClassTemplateDecl *To) {
1352 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1353 Importer.getToContext(),
1354 Importer.getNonEquivalentDecls());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001355 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00001356}
1357
Larisse Voufo39a1e502013-08-06 01:03:05 +00001358bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1359 VarTemplateDecl *To) {
1360 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1361 Importer.getToContext(),
1362 Importer.getNonEquivalentDecls());
1363 return Ctx.IsStructurallyEquivalent(From, To);
1364}
1365
Douglas Gregore4c83e42010-02-09 22:48:33 +00001366Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001367 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001368 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00001369 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00001370}
1371
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001372Decl *ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
1373 // Import the context of this declaration.
1374 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1375 if (!DC)
1376 return nullptr;
1377
1378 DeclContext *LexicalDC = DC;
1379 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1380 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1381 if (!LexicalDC)
1382 return nullptr;
1383 }
1384
1385 // Import the location of this declaration.
1386 SourceLocation Loc = Importer.Import(D->getLocation());
1387
1388 EmptyDecl *ToD = EmptyDecl::Create(Importer.getToContext(), DC, Loc);
1389 ToD->setLexicalDeclContext(LexicalDC);
1390 Importer.Imported(D, ToD);
1391 LexicalDC->addDeclInternal(ToD);
1392 return ToD;
1393}
1394
Sean Callanan65198272011-11-17 23:20:56 +00001395Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1396 TranslationUnitDecl *ToD =
1397 Importer.getToContext().getTranslationUnitDecl();
1398
1399 Importer.Imported(D, ToD);
1400
1401 return ToD;
1402}
1403
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001404Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
1405
1406 SourceLocation Loc = Importer.Import(D->getLocation());
1407 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1408
1409 // Import the context of this declaration.
1410 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1411 if (!DC)
1412 return nullptr;
1413
1414 AccessSpecDecl *accessSpecDecl
1415 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
1416 DC, Loc, ColonLoc);
1417
1418 if (!accessSpecDecl)
1419 return nullptr;
1420
1421 // Lexical DeclContext and Semantic DeclContext
1422 // is always the same for the accessSpec.
1423 accessSpecDecl->setLexicalDeclContext(DC);
1424 DC->addDeclInternal(accessSpecDecl);
1425
1426 return accessSpecDecl;
1427}
1428
Aleksei Sidorina693b372016-09-28 10:16:56 +00001429Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1430 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1431 if (!DC)
1432 return nullptr;
1433
1434 DeclContext *LexicalDC = DC;
1435
1436 // Import the location of this declaration.
1437 SourceLocation Loc = Importer.Import(D->getLocation());
1438
1439 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1440 if (!AssertExpr)
1441 return nullptr;
1442
1443 StringLiteral *FromMsg = D->getMessage();
1444 StringLiteral *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
1445 if (!ToMsg && FromMsg)
1446 return nullptr;
1447
1448 StaticAssertDecl *ToD = StaticAssertDecl::Create(
1449 Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1450 Importer.Import(D->getRParenLoc()), D->isFailed());
1451
1452 ToD->setLexicalDeclContext(LexicalDC);
1453 LexicalDC->addDeclInternal(ToD);
1454 Importer.Imported(D, ToD);
1455 return ToD;
1456}
1457
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001458Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1459 // Import the major distinguishing characteristics of this namespace.
1460 DeclContext *DC, *LexicalDC;
1461 DeclarationName Name;
1462 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001463 NamedDecl *ToD;
1464 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001465 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001466 if (ToD)
1467 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001468
1469 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001470 if (!Name) {
1471 // This is an anonymous namespace. Adopt an existing anonymous
1472 // namespace if we can.
1473 // FIXME: Not testable.
1474 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1475 MergeWithNamespace = TU->getAnonymousNamespace();
1476 else
1477 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1478 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001479 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001480 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001481 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001482 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1483 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001484 continue;
1485
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001486 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001487 MergeWithNamespace = FoundNS;
1488 ConflictingDecls.clear();
1489 break;
1490 }
1491
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001492 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001493 }
1494
1495 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001496 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001497 ConflictingDecls.data(),
1498 ConflictingDecls.size());
1499 }
1500 }
1501
1502 // Create the "to" namespace, if needed.
1503 NamespaceDecl *ToNamespace = MergeWithNamespace;
1504 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001505 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00001506 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001507 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00001508 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00001509 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001510 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001511 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001512
1513 // If this is an anonymous namespace, register it as the anonymous
1514 // namespace within its context.
1515 if (!Name) {
1516 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1517 TU->setAnonymousNamespace(ToNamespace);
1518 else
1519 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1520 }
1521 }
1522 Importer.Imported(D, ToNamespace);
1523
1524 ImportDeclContext(D);
1525
1526 return ToNamespace;
1527}
1528
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001529Decl *ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1530 // Import the major distinguishing characteristics of this namespace.
1531 DeclContext *DC, *LexicalDC;
1532 DeclarationName Name;
1533 SourceLocation Loc;
1534 NamedDecl *LookupD;
1535 if (ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
1536 return nullptr;
1537 if (LookupD)
1538 return LookupD;
1539
1540 // NOTE: No conflict resolution is done for namespace aliases now.
1541
1542 NamespaceDecl *TargetDecl = cast_or_null<NamespaceDecl>(
1543 Importer.Import(D->getNamespace()));
1544 if (!TargetDecl)
1545 return nullptr;
1546
1547 IdentifierInfo *ToII = Importer.Import(D->getIdentifier());
1548 if (!ToII)
1549 return nullptr;
1550
1551 NestedNameSpecifierLoc ToQLoc = Importer.Import(D->getQualifierLoc());
1552 if (D->getQualifierLoc() && !ToQLoc)
1553 return nullptr;
1554
1555 NamespaceAliasDecl *ToD = NamespaceAliasDecl::Create(
1556 Importer.getToContext(), DC, Importer.Import(D->getNamespaceLoc()),
1557 Importer.Import(D->getAliasLoc()), ToII, ToQLoc,
1558 Importer.Import(D->getTargetNameLoc()), TargetDecl);
1559
1560 ToD->setLexicalDeclContext(LexicalDC);
1561 Importer.Imported(D, ToD);
1562 LexicalDC->addDeclInternal(ToD);
1563
1564 return ToD;
1565}
1566
Richard Smithdda56e42011-04-15 14:24:37 +00001567Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001568 // Import the major distinguishing characteristics of this typedef.
1569 DeclContext *DC, *LexicalDC;
1570 DeclarationName Name;
1571 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001572 NamedDecl *ToD;
1573 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001574 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001575 if (ToD)
1576 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001577
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001578 // If this typedef is not in block scope, determine whether we've
1579 // seen a typedef with the same name (that we can merge with) or any
1580 // other entity by that name (which name lookup could conflict with).
1581 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001582 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001583 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001584 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001585 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001586 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1587 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001588 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00001589 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001590 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001591 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1592 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001593 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001594 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001595
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001596 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001597 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001598
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001599 if (!ConflictingDecls.empty()) {
1600 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1601 ConflictingDecls.data(),
1602 ConflictingDecls.size());
1603 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001604 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001605 }
1606 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001607
Douglas Gregorb4964f72010-02-15 23:54:17 +00001608 // Import the underlying type of this typedef;
1609 QualType T = Importer.Import(D->getUnderlyingType());
1610 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001611 return nullptr;
1612
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001613 // Create the new typedef node.
1614 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00001615 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00001616 TypedefNameDecl *ToTypedef;
1617 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001618 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
1619 StartL, Loc,
1620 Name.getAsIdentifierInfo(),
1621 TInfo);
1622 else
Richard Smithdda56e42011-04-15 14:24:37 +00001623 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1624 StartL, Loc,
1625 Name.getAsIdentifierInfo(),
1626 TInfo);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001627
Douglas Gregordd483172010-02-22 17:42:47 +00001628 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001629 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001630 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00001631 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001632
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001633 return ToTypedef;
1634}
1635
Richard Smithdda56e42011-04-15 14:24:37 +00001636Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1637 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1638}
1639
1640Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1641 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1642}
1643
Gabor Horvath7a91c082017-11-14 11:30:38 +00001644Decl *ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1645 // Import the major distinguishing characteristics of this typedef.
1646 DeclContext *DC, *LexicalDC;
1647 DeclarationName Name;
1648 SourceLocation Loc;
1649 NamedDecl *ToD;
1650 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1651 return nullptr;
1652 if (ToD)
1653 return ToD;
1654
1655 // If this typedef is not in block scope, determine whether we've
1656 // seen a typedef with the same name (that we can merge with) or any
1657 // other entity by that name (which name lookup could conflict with).
1658 if (!DC->isFunctionOrMethod()) {
1659 SmallVector<NamedDecl *, 4> ConflictingDecls;
1660 unsigned IDNS = Decl::IDNS_Ordinary;
1661 SmallVector<NamedDecl *, 2> FoundDecls;
1662 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1663 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1664 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1665 continue;
1666 if (auto *FoundAlias =
1667 dyn_cast<TypeAliasTemplateDecl>(FoundDecls[I]))
1668 return Importer.Imported(D, FoundAlias);
1669 ConflictingDecls.push_back(FoundDecls[I]);
1670 }
1671
1672 if (!ConflictingDecls.empty()) {
1673 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1674 ConflictingDecls.data(),
1675 ConflictingDecls.size());
1676 if (!Name)
1677 return nullptr;
1678 }
1679 }
1680
1681 TemplateParameterList *Params = ImportTemplateParameterList(
1682 D->getTemplateParameters());
1683 if (!Params)
1684 return nullptr;
1685
1686 NamedDecl *TemplDecl = cast_or_null<NamedDecl>(
1687 Importer.Import(D->getTemplatedDecl()));
1688 if (!TemplDecl)
1689 return nullptr;
1690
1691 TypeAliasTemplateDecl *ToAlias = TypeAliasTemplateDecl::Create(
1692 Importer.getToContext(), DC, Loc, Name, Params, TemplDecl);
1693
1694 ToAlias->setAccess(D->getAccess());
1695 ToAlias->setLexicalDeclContext(LexicalDC);
1696 Importer.Imported(D, ToAlias);
1697 LexicalDC->addDeclInternal(ToAlias);
1698 return ToD;
1699}
1700
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001701Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
1702 // Import the major distinguishing characteristics of this label.
1703 DeclContext *DC, *LexicalDC;
1704 DeclarationName Name;
1705 SourceLocation Loc;
1706 NamedDecl *ToD;
1707 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1708 return nullptr;
1709 if (ToD)
1710 return ToD;
1711
1712 assert(LexicalDC->isFunctionOrMethod());
1713
1714 LabelDecl *ToLabel = D->isGnuLocal()
1715 ? LabelDecl::Create(Importer.getToContext(),
1716 DC, Importer.Import(D->getLocation()),
1717 Name.getAsIdentifierInfo(),
1718 Importer.Import(D->getLocStart()))
1719 : LabelDecl::Create(Importer.getToContext(),
1720 DC, Importer.Import(D->getLocation()),
1721 Name.getAsIdentifierInfo());
1722 Importer.Imported(D, ToLabel);
1723
1724 LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
1725 if (!Label)
1726 return nullptr;
1727
1728 ToLabel->setStmt(Label);
1729 ToLabel->setLexicalDeclContext(LexicalDC);
1730 LexicalDC->addDeclInternal(ToLabel);
1731 return ToLabel;
1732}
1733
Douglas Gregor98c10182010-02-12 22:17:39 +00001734Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1735 // Import the major distinguishing characteristics of this enum.
1736 DeclContext *DC, *LexicalDC;
1737 DeclarationName Name;
1738 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001739 NamedDecl *ToD;
1740 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001741 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001742 if (ToD)
1743 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001744
Douglas Gregor98c10182010-02-12 22:17:39 +00001745 // Figure out what enum name we're looking for.
1746 unsigned IDNS = Decl::IDNS_Tag;
1747 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001748 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1749 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00001750 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001751 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00001752 IDNS |= Decl::IDNS_Ordinary;
1753
1754 // We may already have an enum of the same name; try to find and match it.
1755 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001756 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001757 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001758 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001759 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1760 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00001761 continue;
1762
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001763 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00001764 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00001765 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1766 Found = Tag->getDecl();
1767 }
1768
1769 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001770 if (IsStructuralMatch(D, FoundEnum))
1771 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001772 }
1773
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001774 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00001775 }
1776
1777 if (!ConflictingDecls.empty()) {
1778 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1779 ConflictingDecls.data(),
1780 ConflictingDecls.size());
1781 }
1782 }
1783
1784 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001785 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
1786 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00001787 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001788 D->isScoped(), D->isScopedUsingClassTag(),
1789 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00001790 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00001791 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00001792 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00001793 D2->setLexicalDeclContext(LexicalDC);
1794 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00001795 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00001796
1797 // Import the integer type.
1798 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1799 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001800 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00001801 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001802
1803 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00001804 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00001805 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00001806
Douglas Gregor3996e242010-02-15 22:01:00 +00001807 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00001808}
1809
Douglas Gregor5c73e912010-02-11 00:48:18 +00001810Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1811 // If this record has a definition in the translation unit we're coming from,
1812 // but this particular declaration is not that definition, import the
1813 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001814 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001815 if (Definition && Definition != D) {
1816 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001817 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00001818 return nullptr;
1819
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001820 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001821 }
1822
1823 // Import the major distinguishing characteristics of this record.
1824 DeclContext *DC, *LexicalDC;
1825 DeclarationName Name;
1826 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001827 NamedDecl *ToD;
1828 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001829 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001830 if (ToD)
1831 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001832
Douglas Gregor5c73e912010-02-11 00:48:18 +00001833 // Figure out what structure name we're looking for.
1834 unsigned IDNS = Decl::IDNS_Tag;
1835 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001836 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1837 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001838 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001839 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00001840 IDNS |= Decl::IDNS_Ordinary;
1841
1842 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00001843 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00001844 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00001845 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001846 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001847 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001848 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00001849
1850 if (!FoundDecls.empty()) {
1851 // We're going to have to compare D against potentially conflicting Decls, so complete it.
1852 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
1853 D->getASTContext().getExternalSource()->CompleteType(D);
1854 }
1855
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001856 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1857 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00001858 continue;
1859
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001860 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00001861 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00001862 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1863 Found = Tag->getDecl();
1864 }
1865
1866 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Gabor Horvath3b392bb2017-04-03 21:06:45 +00001867 if (D->isAnonymousStructOrUnion() &&
1868 FoundRecord->isAnonymousStructOrUnion()) {
1869 // If both anonymous structs/unions are in a record context, make sure
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001870 // they occur in the same location in the context records.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001871 if (Optional<unsigned> Index1 =
1872 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
1873 D)) {
1874 if (Optional<unsigned> Index2 = StructuralEquivalenceContext::
Sean Callanan488f8612016-07-14 19:53:44 +00001875 findUntaggedStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001876 if (*Index1 != *Index2)
1877 continue;
1878 }
1879 }
1880 }
1881
Sean Callanan9092d472017-05-13 00:46:33 +00001882 PrevDecl = FoundRecord;
1883
Douglas Gregor25791052010-02-12 00:09:27 +00001884 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00001885 if ((SearchName && !D->isCompleteDefinition())
1886 || (D->isCompleteDefinition() &&
1887 D->isAnonymousStructOrUnion()
1888 == FoundDef->isAnonymousStructOrUnion() &&
1889 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00001890 // The record types structurally match, or the "from" translation
1891 // unit only had a forward declaration anyway; call it the same
1892 // function.
1893 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001894 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00001895 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00001896 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00001897 // We have a forward declaration of this type, so adopt that forward
1898 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00001899
1900 // If one or both can be completed from external storage then try one
1901 // last time to complete and compare them before doing this.
1902
1903 if (FoundRecord->hasExternalLexicalStorage() &&
1904 !FoundRecord->isCompleteDefinition())
1905 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
1906 if (D->hasExternalLexicalStorage())
1907 D->getASTContext().getExternalSource()->CompleteType(D);
1908
1909 if (FoundRecord->isCompleteDefinition() &&
1910 D->isCompleteDefinition() &&
1911 !IsStructuralMatch(D, FoundRecord))
1912 continue;
1913
Douglas Gregor25791052010-02-12 00:09:27 +00001914 AdoptDecl = FoundRecord;
1915 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00001916 } else if (!SearchName) {
1917 continue;
1918 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00001919 }
1920
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001921 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001922 }
1923
Douglas Gregordd6006f2012-07-17 21:16:27 +00001924 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00001925 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1926 ConflictingDecls.data(),
1927 ConflictingDecls.size());
1928 }
1929 }
1930
1931 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001932 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001933 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00001934 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00001935 CXXRecordDecl *D2CXX = nullptr;
1936 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) {
1937 if (DCXX->isLambda()) {
1938 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
1939 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
1940 DC, TInfo, Loc,
1941 DCXX->isDependentLambda(),
1942 DCXX->isGenericLambda(),
1943 DCXX->getLambdaCaptureDefault());
1944 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
1945 if (DCXX->getLambdaContextDecl() && !CDecl)
1946 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00001947 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001948 } else if (DCXX->isInjectedClassName()) {
1949 // We have to be careful to do a similar dance to the one in
1950 // Sema::ActOnStartCXXMemberDeclarations
1951 CXXRecordDecl *const PrevDecl = nullptr;
1952 const bool DelayTypeCreation = true;
1953 D2CXX = CXXRecordDecl::Create(
1954 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
1955 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
1956 Importer.getToContext().getTypeDeclType(
1957 D2CXX, llvm::dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00001958 } else {
1959 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
1960 D->getTagKind(),
1961 DC, StartLoc, Loc,
1962 Name.getAsIdentifierInfo());
1963 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001964 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00001965 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001966 D2->setLexicalDeclContext(LexicalDC);
1967 if (!DCXX->getDescribedClassTemplate())
1968 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001969
1970 Importer.Imported(D, D2);
1971
1972 if (ClassTemplateDecl *FromDescribed =
1973 DCXX->getDescribedClassTemplate()) {
1974 ClassTemplateDecl *ToDescribed = cast_or_null<ClassTemplateDecl>(
1975 Importer.Import(FromDescribed));
1976 if (!ToDescribed)
1977 return nullptr;
1978 D2CXX->setDescribedClassTemplate(ToDescribed);
1979
1980 } else if (MemberSpecializationInfo *MemberInfo =
1981 DCXX->getMemberSpecializationInfo()) {
1982 TemplateSpecializationKind SK =
1983 MemberInfo->getTemplateSpecializationKind();
1984 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
1985 CXXRecordDecl *ToInst =
1986 cast_or_null<CXXRecordDecl>(Importer.Import(FromInst));
1987 if (FromInst && !ToInst)
1988 return nullptr;
1989 D2CXX->setInstantiationOfMemberClass(ToInst, SK);
1990 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
1991 Importer.Import(MemberInfo->getPointOfInstantiation()));
1992 }
1993
Douglas Gregor25791052010-02-12 00:09:27 +00001994 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00001995 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001996 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001997 D2->setLexicalDeclContext(LexicalDC);
1998 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001999 }
Douglas Gregor14454802011-02-25 02:25:35 +00002000
2001 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd6006f2012-07-17 21:16:27 +00002002 if (D->isAnonymousStructOrUnion())
2003 D2->setAnonymousStructOrUnion(true);
Sean Callanan9092d472017-05-13 00:46:33 +00002004 if (PrevDecl) {
2005 // FIXME: do this for all Redeclarables, not just RecordDecls.
2006 D2->setPreviousDecl(PrevDecl);
2007 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002008 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002009
Douglas Gregor3996e242010-02-15 22:01:00 +00002010 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002011
Douglas Gregor95d82832012-01-24 18:36:04 +00002012 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002013 return nullptr;
2014
Douglas Gregor3996e242010-02-15 22:01:00 +00002015 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002016}
2017
Douglas Gregor98c10182010-02-12 22:17:39 +00002018Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2019 // Import the major distinguishing characteristics of this enumerator.
2020 DeclContext *DC, *LexicalDC;
2021 DeclarationName Name;
2022 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002023 NamedDecl *ToD;
2024 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002025 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002026 if (ToD)
2027 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002028
2029 QualType T = Importer.Import(D->getType());
2030 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002031 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002032
Douglas Gregor98c10182010-02-12 22:17:39 +00002033 // Determine whether there are any other declarations with the same name and
2034 // in the same context.
2035 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002036 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002037 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002038 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002039 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002040 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2041 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002042 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002043
2044 if (EnumConstantDecl *FoundEnumConstant
2045 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2046 if (IsStructuralMatch(D, FoundEnumConstant))
2047 return Importer.Imported(D, FoundEnumConstant);
2048 }
2049
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002050 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002051 }
2052
2053 if (!ConflictingDecls.empty()) {
2054 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2055 ConflictingDecls.data(),
2056 ConflictingDecls.size());
2057 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002058 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002059 }
2060 }
2061
2062 Expr *Init = Importer.Import(D->getInitExpr());
2063 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002064 return nullptr;
2065
Douglas Gregor98c10182010-02-12 22:17:39 +00002066 EnumConstantDecl *ToEnumerator
2067 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2068 Name.getAsIdentifierInfo(), T,
2069 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002070 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002071 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002072 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002073 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002074 return ToEnumerator;
2075}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002076
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002077bool ASTNodeImporter::ImportTemplateInformation(FunctionDecl *FromFD,
2078 FunctionDecl *ToFD) {
2079 switch (FromFD->getTemplatedKind()) {
2080 case FunctionDecl::TK_NonTemplate:
2081 case FunctionDecl::TK_FunctionTemplate:
Sam McCallfdc32072018-01-26 12:06:44 +00002082 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002083
2084 case FunctionDecl::TK_MemberSpecialization: {
2085 auto *InstFD = cast_or_null<FunctionDecl>(
2086 Importer.Import(FromFD->getInstantiatedFromMemberFunction()));
2087 if (!InstFD)
2088 return true;
2089
2090 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
2091 SourceLocation POI = Importer.Import(
2092 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation());
2093 ToFD->setInstantiationOfMemberFunction(InstFD, TSK);
2094 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002095 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002096 }
2097
2098 case FunctionDecl::TK_FunctionTemplateSpecialization: {
2099 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
2100 auto *Template = cast_or_null<FunctionTemplateDecl>(
2101 Importer.Import(FTSInfo->getTemplate()));
2102 if (!Template)
2103 return true;
2104 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
2105
2106 // Import template arguments.
2107 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
2108 SmallVector<TemplateArgument, 8> ToTemplArgs;
2109 if (ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
2110 ToTemplArgs))
2111 return true;
2112
2113 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
2114 Importer.getToContext(), ToTemplArgs);
2115
2116 TemplateArgumentListInfo ToTAInfo;
2117 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
2118 if (FromTAArgsAsWritten) {
2119 if (ImportTemplateArgumentListInfo(
2120 FromTAArgsAsWritten->LAngleLoc, FromTAArgsAsWritten->RAngleLoc,
2121 FromTAArgsAsWritten->arguments(), ToTAInfo))
2122 return true;
2123 }
2124
2125 SourceLocation POI = Importer.Import(FTSInfo->getPointOfInstantiation());
2126
2127 ToFD->setFunctionTemplateSpecialization(
2128 Template, ToTAList, /* InsertPos= */ nullptr,
2129 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002130 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002131 }
2132
2133 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2134 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2135 UnresolvedSet<8> TemplDecls;
2136 unsigned NumTemplates = FromInfo->getNumTemplates();
2137 for (unsigned I = 0; I < NumTemplates; I++) {
2138 if (auto *ToFTD = cast_or_null<FunctionTemplateDecl>(
2139 Importer.Import(FromInfo->getTemplate(I))))
2140 TemplDecls.addDecl(ToFTD);
2141 else
2142 return true;
2143 }
2144
2145 // Import TemplateArgumentListInfo.
2146 TemplateArgumentListInfo ToTAInfo;
2147 if (ImportTemplateArgumentListInfo(
2148 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2149 llvm::makeArrayRef(FromInfo->getTemplateArgs(),
2150 FromInfo->getNumTemplateArgs()),
2151 ToTAInfo))
2152 return true;
2153
2154 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2155 TemplDecls, ToTAInfo);
Sam McCallfdc32072018-01-26 12:06:44 +00002156 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002157 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002158 }
Sam McCallfdc32072018-01-26 12:06:44 +00002159 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002160}
2161
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002162Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2163 // Import the major distinguishing characteristics of this function.
2164 DeclContext *DC, *LexicalDC;
2165 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002166 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002167 NamedDecl *ToD;
2168 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002169 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002170 if (ToD)
2171 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002172
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002173 const FunctionDecl *FoundWithoutBody = nullptr;
2174
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002175 // Try to find a function in our own ("to") context with the same name, same
2176 // type, and in the same context as the function we're importing.
2177 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002178 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002179 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002180 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002181 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002182 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2183 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002184 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002185
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002186 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002187 if (FoundFunction->hasExternalFormalLinkage() &&
2188 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002189 if (Importer.IsStructurallyEquivalent(D->getType(),
2190 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002191 // FIXME: Actually try to merge the body and other attributes.
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002192 const FunctionDecl *FromBodyDecl = nullptr;
2193 D->hasBody(FromBodyDecl);
2194 if (D == FromBodyDecl && !FoundFunction->hasBody()) {
2195 // This function is needed to merge completely.
2196 FoundWithoutBody = FoundFunction;
2197 break;
2198 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002199 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002200 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002201
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002202 // FIXME: Check for overloading more carefully, e.g., by boosting
2203 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002204
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002205 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002206 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002207 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002208
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002209 // Complain about inconsistent function types.
2210 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002211 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002212 Importer.ToDiag(FoundFunction->getLocation(),
2213 diag::note_odr_value_here)
2214 << FoundFunction->getType();
2215 }
2216 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002217
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002218 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002219 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002220
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002221 if (!ConflictingDecls.empty()) {
2222 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2223 ConflictingDecls.data(),
2224 ConflictingDecls.size());
2225 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002226 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002227 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002228 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002229
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002230 DeclarationNameInfo NameInfo(Name, Loc);
2231 // Import additional name location/type info.
2232 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2233
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002234 QualType FromTy = D->getType();
2235 bool usedDifferentExceptionSpec = false;
2236
2237 if (const FunctionProtoType *
2238 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2239 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2240 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2241 // FunctionDecl that we are importing the FunctionProtoType for.
2242 // To avoid an infinite recursion when importing, create the FunctionDecl
2243 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002244 if (FromEPI.ExceptionSpec.SourceDecl ||
2245 FromEPI.ExceptionSpec.SourceTemplate ||
2246 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002247 FunctionProtoType::ExtProtoInfo DefaultEPI;
2248 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002249 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002250 usedDifferentExceptionSpec = true;
2251 }
2252 }
2253
Douglas Gregorb4964f72010-02-15 23:54:17 +00002254 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002255 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002256 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002257 return nullptr;
2258
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002259 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002260 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00002261 for (auto P : D->parameters()) {
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00002262 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002263 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002264 return nullptr;
2265
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002266 Parameters.push_back(ToP);
2267 }
2268
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002269 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002270 if (D->getTypeSourceInfo() && !TInfo)
2271 return nullptr;
2272
2273 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00002274 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002275 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00002276 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2277 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2278 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002279 InnerLocStart,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002280 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002281 FromConstructor->isExplicit(),
2282 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002283 D->isImplicit(),
2284 D->isConstexpr());
Sean Callanandd2c1742016-05-16 20:48:03 +00002285 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
2286 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
2287 for (CXXCtorInitializer *I : FromConstructor->inits()) {
2288 CXXCtorInitializer *ToI =
2289 cast_or_null<CXXCtorInitializer>(Importer.Import(I));
2290 if (!ToI && I)
2291 return nullptr;
2292 CtorInitializers.push_back(ToI);
2293 }
2294 CXXCtorInitializer **Memory =
2295 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
2296 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
2297 CXXConstructorDecl *ToCtor = llvm::cast<CXXConstructorDecl>(ToFunction);
2298 ToCtor->setCtorInitializers(Memory);
2299 ToCtor->setNumCtorInitializers(NumInitializers);
2300 }
Douglas Gregor00eace12010-02-21 18:29:16 +00002301 } else if (isa<CXXDestructorDecl>(D)) {
2302 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2303 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002304 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002305 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002306 D->isInlineSpecified(),
2307 D->isImplicit());
2308 } else if (CXXConversionDecl *FromConversion
2309 = dyn_cast<CXXConversionDecl>(D)) {
2310 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2311 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002312 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002313 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002314 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002315 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002316 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002317 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002318 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2319 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2320 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002321 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00002322 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002323 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002324 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002325 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002326 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002327 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002328 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00002329 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002330 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002331 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002332 D->hasWrittenPrototype(),
2333 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002334 }
John McCall3e11ebe2010-03-15 10:12:16 +00002335
2336 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002337 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002338 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002339 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002340 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2341 ToFunction->setTrivial(D->isTrivial());
2342 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002343 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002344
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002345 // Set the parameters.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002346 for (ParmVarDecl *Param : Parameters) {
2347 Param->setOwningFunction(ToFunction);
2348 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002349 }
David Blaikie9c70e042011-09-21 18:16:56 +00002350 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002351
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002352 if (FoundWithoutBody) {
2353 auto *Recent = const_cast<FunctionDecl *>(
2354 FoundWithoutBody->getMostRecentDecl());
2355 ToFunction->setPreviousDecl(Recent);
2356 }
2357
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002358 // We need to complete creation of FunctionProtoTypeLoc manually with setting
2359 // params it refers to.
2360 if (TInfo) {
2361 if (auto ProtoLoc =
2362 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
2363 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
2364 ProtoLoc.setParam(I, Parameters[I]);
2365 }
2366 }
2367
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002368 if (usedDifferentExceptionSpec) {
2369 // Update FunctionProtoType::ExtProtoInfo.
2370 QualType T = Importer.Import(D->getType());
2371 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002372 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002373 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002374 }
2375
Sean Callanan59721b32015-04-28 18:41:46 +00002376 // Import the body, if any.
2377 if (Stmt *FromBody = D->getBody()) {
2378 if (Stmt *ToBody = Importer.Import(FromBody)) {
2379 ToFunction->setBody(ToBody);
2380 }
2381 }
2382
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002383 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002384
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002385 // If it is a template, import all related things.
2386 if (ImportTemplateInformation(D, ToFunction))
2387 return nullptr;
2388
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002389 // Add this function to the lexical context.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002390 // NOTE: If the function is templated declaration, it should be not added into
2391 // LexicalDC. But described template is imported during import of
2392 // FunctionTemplateDecl (it happens later). So, we use source declaration
2393 // to determine if we should add the result function.
2394 if (!D->getDescribedFunctionTemplate())
2395 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002396
Lang Hames19e07e12017-06-20 21:06:00 +00002397 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2398 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2399
Douglas Gregor43f54792010-02-17 02:12:47 +00002400 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002401}
2402
Douglas Gregor00eace12010-02-21 18:29:16 +00002403Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2404 return VisitFunctionDecl(D);
2405}
2406
2407Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2408 return VisitCXXMethodDecl(D);
2409}
2410
2411Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2412 return VisitCXXMethodDecl(D);
2413}
2414
2415Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2416 return VisitCXXMethodDecl(D);
2417}
2418
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002419static unsigned getFieldIndex(Decl *F) {
2420 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2421 if (!Owner)
2422 return 0;
2423
2424 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002425 for (const auto *D : Owner->noload_decls()) {
2426 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002427 return Index;
2428
2429 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2430 ++Index;
2431 }
2432
2433 return Index;
2434}
2435
Douglas Gregor5c73e912010-02-11 00:48:18 +00002436Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2437 // Import the major distinguishing characteristics of a variable.
2438 DeclContext *DC, *LexicalDC;
2439 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002440 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002441 NamedDecl *ToD;
2442 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002443 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002444 if (ToD)
2445 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002446
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002447 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002448 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002449 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002450 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2451 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002452 // For anonymous fields, match up by index.
2453 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2454 continue;
2455
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002456 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002457 FoundField->getType())) {
2458 Importer.Imported(D, FoundField);
2459 return FoundField;
2460 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002461
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002462 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2463 << Name << D->getType() << FoundField->getType();
2464 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2465 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002466 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002467 }
2468 }
2469
Douglas Gregorb4964f72010-02-15 23:54:17 +00002470 // Import the type.
2471 QualType T = Importer.Import(D->getType());
2472 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002473 return nullptr;
2474
Douglas Gregor5c73e912010-02-11 00:48:18 +00002475 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2476 Expr *BitWidth = Importer.Import(D->getBitWidth());
2477 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002478 return nullptr;
2479
Abramo Bagnaradff19302011-03-08 08:55:46 +00002480 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2481 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002482 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002483 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002484 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002485 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002486 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00002487 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00002488 Expr *ToInitializer = Importer.Import(FromInitializer);
2489 if (ToInitializer)
2490 ToField->setInClassInitializer(ToInitializer);
2491 else
2492 return nullptr;
2493 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002494 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002495 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002496 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002497 return ToField;
2498}
2499
Francois Pichet783dd6e2010-11-21 06:08:52 +00002500Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2501 // Import the major distinguishing characteristics of a variable.
2502 DeclContext *DC, *LexicalDC;
2503 DeclarationName Name;
2504 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002505 NamedDecl *ToD;
2506 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002507 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002508 if (ToD)
2509 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002510
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002511 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002512 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002513 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002514 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002515 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002516 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002517 // For anonymous indirect fields, match up by index.
2518 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2519 continue;
2520
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002521 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002522 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002523 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002524 Importer.Imported(D, FoundField);
2525 return FoundField;
2526 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002527
2528 // If there are more anonymous fields to check, continue.
2529 if (!Name && I < N-1)
2530 continue;
2531
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002532 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2533 << Name << D->getType() << FoundField->getType();
2534 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2535 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002536 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002537 }
2538 }
2539
Francois Pichet783dd6e2010-11-21 06:08:52 +00002540 // Import the type.
2541 QualType T = Importer.Import(D->getType());
2542 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002543 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002544
2545 NamedDecl **NamedChain =
2546 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2547
2548 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00002549 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00002550 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002551 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00002552 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002553 NamedChain[i++] = cast<NamedDecl>(D);
2554 }
2555
2556 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00002557 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
David Majnemer59f77922016-06-24 04:05:48 +00002558 {NamedChain, D->getChainingSize()});
Aaron Ballman260995b2014-10-15 16:58:18 +00002559
2560 for (const auto *Attr : D->attrs())
2561 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
2562
Francois Pichet783dd6e2010-11-21 06:08:52 +00002563 ToIndirectField->setAccess(D->getAccess());
2564 ToIndirectField->setLexicalDeclContext(LexicalDC);
2565 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002566 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002567 return ToIndirectField;
2568}
2569
Aleksei Sidorina693b372016-09-28 10:16:56 +00002570Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2571 // Import the major distinguishing characteristics of a declaration.
2572 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2573 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2574 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2575 if (!DC || !LexicalDC)
2576 return nullptr;
2577
2578 // Determine whether we've already imported this decl.
2579 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2580 auto *RD = cast<CXXRecordDecl>(DC);
2581 FriendDecl *ImportedFriend = RD->getFirstFriend();
2582 StructuralEquivalenceContext Context(
2583 Importer.getFromContext(), Importer.getToContext(),
2584 Importer.getNonEquivalentDecls(), false, false);
2585
2586 while (ImportedFriend) {
2587 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2588 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2589 ImportedFriend->getFriendDecl()))
2590 return Importer.Imported(D, ImportedFriend);
2591
2592 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2593 if (Importer.IsStructurallyEquivalent(
2594 D->getFriendType()->getType(),
2595 ImportedFriend->getFriendType()->getType(), true))
2596 return Importer.Imported(D, ImportedFriend);
2597 }
2598 ImportedFriend = ImportedFriend->getNextFriend();
2599 }
2600
2601 // Not found. Create it.
2602 FriendDecl::FriendUnion ToFU;
2603 if (NamedDecl *FriendD = D->getFriendDecl())
2604 ToFU = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2605 else
2606 ToFU = Importer.Import(D->getFriendType());
2607 if (!ToFU)
2608 return nullptr;
2609
2610 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
2611 TemplateParameterList **FromTPLists =
2612 D->getTrailingObjects<TemplateParameterList *>();
2613 for (unsigned I = 0; I < D->NumTPLists; I++) {
2614 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2615 if (!List)
2616 return nullptr;
2617 ToTPLists[I] = List;
2618 }
2619
2620 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
2621 Importer.Import(D->getLocation()),
2622 ToFU, Importer.Import(D->getFriendLoc()),
2623 ToTPLists);
2624
2625 Importer.Imported(D, FrD);
2626 RD->pushFriendDecl(FrD);
2627
2628 FrD->setAccess(D->getAccess());
2629 FrD->setLexicalDeclContext(LexicalDC);
2630 LexicalDC->addDeclInternal(FrD);
2631 return FrD;
2632}
2633
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002634Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2635 // Import the major distinguishing characteristics of an ivar.
2636 DeclContext *DC, *LexicalDC;
2637 DeclarationName Name;
2638 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002639 NamedDecl *ToD;
2640 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002641 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002642 if (ToD)
2643 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002644
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002645 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002646 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002647 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002648 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2649 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002650 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002651 FoundIvar->getType())) {
2652 Importer.Imported(D, FoundIvar);
2653 return FoundIvar;
2654 }
2655
2656 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2657 << Name << D->getType() << FoundIvar->getType();
2658 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2659 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002660 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002661 }
2662 }
2663
2664 // Import the type.
2665 QualType T = Importer.Import(D->getType());
2666 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002667 return nullptr;
2668
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002669 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2670 Expr *BitWidth = Importer.Import(D->getBitWidth());
2671 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002672 return nullptr;
2673
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002674 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2675 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002676 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002677 Loc, Name.getAsIdentifierInfo(),
2678 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00002679 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002680 ToIvar->setLexicalDeclContext(LexicalDC);
2681 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002682 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002683 return ToIvar;
2684
2685}
2686
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002687Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2688 // Import the major distinguishing characteristics of a variable.
2689 DeclContext *DC, *LexicalDC;
2690 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002691 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002692 NamedDecl *ToD;
2693 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002694 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002695 if (ToD)
2696 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002697
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002698 // Try to find a variable in our own ("to") context with the same name and
2699 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002700 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00002701 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002702 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002703 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002704 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002705 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002706 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2707 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002708 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002709
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002710 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002711 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00002712 if (FoundVar->hasExternalFormalLinkage() &&
2713 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002714 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002715 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002716 MergeWithVar = FoundVar;
2717 break;
2718 }
2719
Douglas Gregor56521c52010-02-12 17:23:39 +00002720 const ArrayType *FoundArray
2721 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2722 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002723 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002724 if (FoundArray && TArray) {
2725 if (isa<IncompleteArrayType>(FoundArray) &&
2726 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002727 // Import the type.
2728 QualType T = Importer.Import(D->getType());
2729 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002730 return nullptr;
2731
Douglas Gregor56521c52010-02-12 17:23:39 +00002732 FoundVar->setType(T);
2733 MergeWithVar = FoundVar;
2734 break;
2735 } else if (isa<IncompleteArrayType>(TArray) &&
2736 isa<ConstantArrayType>(FoundArray)) {
2737 MergeWithVar = FoundVar;
2738 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002739 }
2740 }
2741
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002742 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002743 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002744 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2745 << FoundVar->getType();
2746 }
2747 }
2748
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002749 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002750 }
2751
2752 if (MergeWithVar) {
2753 // An equivalent variable with external linkage has been found. Link
2754 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002755 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002756
2757 if (VarDecl *DDef = D->getDefinition()) {
2758 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2759 Importer.ToDiag(ExistingDef->getLocation(),
2760 diag::err_odr_variable_multiple_def)
2761 << Name;
2762 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2763 } else {
2764 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002765 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002766 if (DDef->isInitKnownICE()) {
2767 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2768 Eval->CheckedICE = true;
2769 Eval->IsICE = DDef->isInitICE();
2770 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002771 }
2772 }
2773
2774 return MergeWithVar;
2775 }
2776
2777 if (!ConflictingDecls.empty()) {
2778 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2779 ConflictingDecls.data(),
2780 ConflictingDecls.size());
2781 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002782 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002783 }
2784 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002785
Douglas Gregorb4964f72010-02-15 23:54:17 +00002786 // Import the type.
2787 QualType T = Importer.Import(D->getType());
2788 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002789 return nullptr;
2790
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002791 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002792 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002793 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2794 Importer.Import(D->getInnerLocStart()),
2795 Loc, Name.getAsIdentifierInfo(),
2796 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002797 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00002798 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002799 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002800 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002801 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002802 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002803
Sean Callanan59721b32015-04-28 18:41:46 +00002804 if (!D->isFileVarDecl() &&
2805 D->isUsed())
2806 ToVar->setIsUsed();
2807
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002808 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002809 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00002810 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002811
Aleksei Sidorin855086d2017-01-23 09:30:36 +00002812 if (D->isConstexpr())
2813 ToVar->setConstexpr(true);
2814
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002815 return ToVar;
2816}
2817
Douglas Gregor8b228d72010-02-17 21:22:52 +00002818Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2819 // Parameters are created in the translation unit's context, then moved
2820 // into the function declaration's context afterward.
2821 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2822
2823 // Import the name of this declaration.
2824 DeclarationName Name = Importer.Import(D->getDeclName());
2825 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002826 return nullptr;
2827
Douglas Gregor8b228d72010-02-17 21:22:52 +00002828 // Import the location of this declaration.
2829 SourceLocation Loc = Importer.Import(D->getLocation());
2830
2831 // Import the parameter's type.
2832 QualType T = Importer.Import(D->getType());
2833 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002834 return nullptr;
2835
Douglas Gregor8b228d72010-02-17 21:22:52 +00002836 // Create the imported parameter.
Alexey Bataev56223232017-06-09 13:40:18 +00002837 auto *ToParm = ImplicitParamDecl::Create(Importer.getToContext(), DC, Loc,
2838 Name.getAsIdentifierInfo(), T,
2839 D->getParameterKind());
Douglas Gregor8b228d72010-02-17 21:22:52 +00002840 return Importer.Imported(D, ToParm);
2841}
2842
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002843Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2844 // Parameters are created in the translation unit's context, then moved
2845 // into the function declaration's context afterward.
2846 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2847
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002848 // Import the name of this declaration.
2849 DeclarationName Name = Importer.Import(D->getDeclName());
2850 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002851 return nullptr;
2852
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002853 // Import the location of this declaration.
2854 SourceLocation Loc = Importer.Import(D->getLocation());
2855
2856 // Import the parameter's type.
2857 QualType T = Importer.Import(D->getType());
2858 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002859 return nullptr;
2860
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002861 // Create the imported parameter.
2862 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2863 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002864 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002865 Loc, Name.getAsIdentifierInfo(),
2866 T, TInfo, D->getStorageClass(),
Aleksei Sidorin55a63502017-02-20 11:57:12 +00002867 /*DefaultArg*/ nullptr);
2868
2869 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00002870 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00002871 ToParm->setKNRPromoted(D->isKNRPromoted());
2872
2873 Expr *ToDefArg = nullptr;
2874 Expr *FromDefArg = nullptr;
2875 if (D->hasUninstantiatedDefaultArg()) {
2876 FromDefArg = D->getUninstantiatedDefaultArg();
2877 ToDefArg = Importer.Import(FromDefArg);
2878 ToParm->setUninstantiatedDefaultArg(ToDefArg);
2879 } else if (D->hasUnparsedDefaultArg()) {
2880 ToParm->setUnparsedDefaultArg();
2881 } else if (D->hasDefaultArg()) {
2882 FromDefArg = D->getDefaultArg();
2883 ToDefArg = Importer.Import(FromDefArg);
2884 ToParm->setDefaultArg(ToDefArg);
2885 }
2886 if (FromDefArg && !ToDefArg)
2887 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002888
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002889 if (D->isObjCMethodParameter()) {
2890 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
2891 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
2892 } else {
2893 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
2894 D->getFunctionScopeIndex());
2895 }
2896
Sean Callanan59721b32015-04-28 18:41:46 +00002897 if (D->isUsed())
2898 ToParm->setIsUsed();
2899
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002900 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002901}
2902
Douglas Gregor43f54792010-02-17 02:12:47 +00002903Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2904 // Import the major distinguishing characteristics of a method.
2905 DeclContext *DC, *LexicalDC;
2906 DeclarationName Name;
2907 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002908 NamedDecl *ToD;
2909 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002910 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002911 if (ToD)
2912 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002913
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002914 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002915 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002916 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2917 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002918 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2919 continue;
2920
2921 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00002922 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
2923 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002924 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00002925 << D->isInstanceMethod() << Name << D->getReturnType()
2926 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00002927 Importer.ToDiag(FoundMethod->getLocation(),
2928 diag::note_odr_objc_method_here)
2929 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002930 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002931 }
2932
2933 // Check the number of parameters.
2934 if (D->param_size() != FoundMethod->param_size()) {
2935 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2936 << D->isInstanceMethod() << Name
2937 << D->param_size() << FoundMethod->param_size();
2938 Importer.ToDiag(FoundMethod->getLocation(),
2939 diag::note_odr_objc_method_here)
2940 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002941 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002942 }
2943
2944 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002945 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002946 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2947 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002948 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002949 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002950 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002951 diag::err_odr_objc_method_param_type_inconsistent)
2952 << D->isInstanceMethod() << Name
2953 << (*P)->getType() << (*FoundP)->getType();
2954 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2955 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002956 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002957 }
2958 }
2959
2960 // Check variadic/non-variadic.
2961 // Check the number of parameters.
2962 if (D->isVariadic() != FoundMethod->isVariadic()) {
2963 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2964 << D->isInstanceMethod() << Name;
2965 Importer.ToDiag(FoundMethod->getLocation(),
2966 diag::note_odr_objc_method_here)
2967 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002968 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002969 }
2970
2971 // FIXME: Any other bits we need to merge?
2972 return Importer.Imported(D, FoundMethod);
2973 }
2974 }
2975
2976 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00002977 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00002978 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002979 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002980
Alp Toker314cc812014-01-25 16:55:45 +00002981 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00002982
Alp Toker314cc812014-01-25 16:55:45 +00002983 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
2984 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
2985 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
2986 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
2987 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00002988
2989 // FIXME: When we decide to merge method definitions, we'll need to
2990 // deal with implicit parameters.
2991
2992 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002993 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00002994 for (auto *FromP : D->parameters()) {
Aaron Ballman43b68be2014-03-07 17:50:17 +00002995 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00002996 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002997 return nullptr;
2998
Douglas Gregor43f54792010-02-17 02:12:47 +00002999 ToParams.push_back(ToP);
3000 }
3001
3002 // Set the parameters.
3003 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3004 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003005 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003006 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003007
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003008 SmallVector<SourceLocation, 12> SelLocs;
3009 D->getSelectorLocs(SelLocs);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003010 for (SourceLocation &Loc : SelLocs)
3011 Loc = Importer.Import(Loc);
3012
3013 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003014
3015 ToMethod->setLexicalDeclContext(LexicalDC);
3016 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003017 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003018 return ToMethod;
3019}
3020
Douglas Gregor85f3f952015-07-07 03:57:15 +00003021Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3022 // Import the major distinguishing characteristics of a category.
3023 DeclContext *DC, *LexicalDC;
3024 DeclarationName Name;
3025 SourceLocation Loc;
3026 NamedDecl *ToD;
3027 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3028 return nullptr;
3029 if (ToD)
3030 return ToD;
3031
3032 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3033 if (!BoundInfo)
3034 return nullptr;
3035
3036 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3037 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003038 D->getVariance(),
3039 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003040 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003041 Importer.Import(D->getLocation()),
3042 Name.getAsIdentifierInfo(),
3043 Importer.Import(D->getColonLoc()),
3044 BoundInfo);
3045 Importer.Imported(D, Result);
3046 Result->setLexicalDeclContext(LexicalDC);
3047 return Result;
3048}
3049
Douglas Gregor84c51c32010-02-18 01:47:50 +00003050Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3051 // Import the major distinguishing characteristics of a category.
3052 DeclContext *DC, *LexicalDC;
3053 DeclarationName Name;
3054 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003055 NamedDecl *ToD;
3056 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003057 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003058 if (ToD)
3059 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003060
Douglas Gregor84c51c32010-02-18 01:47:50 +00003061 ObjCInterfaceDecl *ToInterface
3062 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3063 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003064 return nullptr;
3065
Douglas Gregor84c51c32010-02-18 01:47:50 +00003066 // Determine if we've already encountered this category.
3067 ObjCCategoryDecl *MergeWithCategory
3068 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3069 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3070 if (!ToCategory) {
3071 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003072 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003073 Loc,
3074 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003075 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003076 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003077 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003078 Importer.Import(D->getIvarLBraceLoc()),
3079 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003080 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003081 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003082 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003083 // Import the type parameter list after calling Imported, to avoid
3084 // loops when bringing in their DeclContext.
3085 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3086 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003087
Douglas Gregor84c51c32010-02-18 01:47:50 +00003088 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003089 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3090 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003091 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3092 = D->protocol_loc_begin();
3093 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3094 FromProtoEnd = D->protocol_end();
3095 FromProto != FromProtoEnd;
3096 ++FromProto, ++FromProtoLoc) {
3097 ObjCProtocolDecl *ToProto
3098 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3099 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003100 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003101 Protocols.push_back(ToProto);
3102 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3103 }
3104
3105 // FIXME: If we're merging, make sure that the protocol list is the same.
3106 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3107 ProtocolLocs.data(), Importer.getToContext());
3108
3109 } else {
3110 Importer.Imported(D, ToCategory);
3111 }
3112
3113 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003114 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003115
3116 // If we have an implementation, import it as well.
3117 if (D->getImplementation()) {
3118 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003119 = cast_or_null<ObjCCategoryImplDecl>(
3120 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003121 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003122 return nullptr;
3123
Douglas Gregor84c51c32010-02-18 01:47:50 +00003124 ToCategory->setImplementation(Impl);
3125 }
3126
3127 return ToCategory;
3128}
3129
Douglas Gregor2aa53772012-01-24 17:42:07 +00003130bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3131 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003132 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003133 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003134 if (shouldForceImportDeclContext(Kind))
3135 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003136 return false;
3137 }
3138
3139 // Start the protocol definition
3140 To->startDefinition();
3141
3142 // Import protocols
3143 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3144 SmallVector<SourceLocation, 4> ProtocolLocs;
3145 ObjCProtocolDecl::protocol_loc_iterator
3146 FromProtoLoc = From->protocol_loc_begin();
3147 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3148 FromProtoEnd = From->protocol_end();
3149 FromProto != FromProtoEnd;
3150 ++FromProto, ++FromProtoLoc) {
3151 ObjCProtocolDecl *ToProto
3152 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3153 if (!ToProto)
3154 return true;
3155 Protocols.push_back(ToProto);
3156 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3157 }
3158
3159 // FIXME: If we're merging, make sure that the protocol list is the same.
3160 To->setProtocolList(Protocols.data(), Protocols.size(),
3161 ProtocolLocs.data(), Importer.getToContext());
3162
Douglas Gregor2e15c842012-02-01 21:00:38 +00003163 if (shouldForceImportDeclContext(Kind)) {
3164 // Import all of the members of this protocol.
3165 ImportDeclContext(From, /*ForceImport=*/true);
3166 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003167 return false;
3168}
3169
Douglas Gregor98d156a2010-02-17 16:12:00 +00003170Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003171 // If this protocol has a definition in the translation unit we're coming
3172 // from, but this particular declaration is not that definition, import the
3173 // definition and map to that.
3174 ObjCProtocolDecl *Definition = D->getDefinition();
3175 if (Definition && Definition != D) {
3176 Decl *ImportedDef = Importer.Import(Definition);
3177 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003178 return nullptr;
3179
Douglas Gregor2aa53772012-01-24 17:42:07 +00003180 return Importer.Imported(D, ImportedDef);
3181 }
3182
Douglas Gregor84c51c32010-02-18 01:47:50 +00003183 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003184 DeclContext *DC, *LexicalDC;
3185 DeclarationName Name;
3186 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003187 NamedDecl *ToD;
3188 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003189 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003190 if (ToD)
3191 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003192
Craig Topper36250ad2014-05-12 05:36:57 +00003193 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003194 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003195 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003196 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3197 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003198 continue;
3199
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003200 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003201 break;
3202 }
3203
3204 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003205 if (!ToProto) {
3206 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3207 Name.getAsIdentifierInfo(), Loc,
3208 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003209 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003210 ToProto->setLexicalDeclContext(LexicalDC);
3211 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003212 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003213
3214 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003215
Douglas Gregor2aa53772012-01-24 17:42:07 +00003216 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003217 return nullptr;
3218
Douglas Gregor98d156a2010-02-17 16:12:00 +00003219 return ToProto;
3220}
3221
Sean Callanan0aae0412014-12-10 00:00:37 +00003222Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3223 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3224 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3225
3226 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3227 SourceLocation LangLoc = Importer.Import(D->getLocation());
3228
3229 bool HasBraces = D->hasBraces();
3230
Sean Callananb12a8552014-12-10 21:22:20 +00003231 LinkageSpecDecl *ToLinkageSpec =
3232 LinkageSpecDecl::Create(Importer.getToContext(),
3233 DC,
3234 ExternLoc,
3235 LangLoc,
3236 D->getLanguage(),
3237 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003238
3239 if (HasBraces) {
3240 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3241 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3242 }
3243
3244 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3245 LexicalDC->addDeclInternal(ToLinkageSpec);
3246
3247 Importer.Imported(D, ToLinkageSpec);
3248
3249 return ToLinkageSpec;
3250}
3251
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003252Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
3253 DeclContext *DC, *LexicalDC;
3254 DeclarationName Name;
3255 SourceLocation Loc;
3256 NamedDecl *ToD = nullptr;
3257 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3258 return nullptr;
3259 if (ToD)
3260 return ToD;
3261
3262 DeclarationNameInfo NameInfo(Name,
3263 Importer.Import(D->getNameInfo().getLoc()));
3264 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3265
3266 UsingDecl *ToUsing = UsingDecl::Create(Importer.getToContext(), DC,
3267 Importer.Import(D->getUsingLoc()),
3268 Importer.Import(D->getQualifierLoc()),
3269 NameInfo, D->hasTypename());
3270 ToUsing->setLexicalDeclContext(LexicalDC);
3271 LexicalDC->addDeclInternal(ToUsing);
3272 Importer.Imported(D, ToUsing);
3273
3274 if (NamedDecl *FromPattern =
3275 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
3276 if (NamedDecl *ToPattern =
3277 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern)))
3278 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern);
3279 else
3280 return nullptr;
3281 }
3282
3283 for (UsingShadowDecl *FromShadow : D->shadows()) {
3284 if (UsingShadowDecl *ToShadow =
3285 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow)))
3286 ToUsing->addShadowDecl(ToShadow);
3287 else
3288 // FIXME: We return a nullptr here but the definition is already created
3289 // and available with lookups. How to fix this?..
3290 return nullptr;
3291 }
3292 return ToUsing;
3293}
3294
3295Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
3296 DeclContext *DC, *LexicalDC;
3297 DeclarationName Name;
3298 SourceLocation Loc;
3299 NamedDecl *ToD = nullptr;
3300 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3301 return nullptr;
3302 if (ToD)
3303 return ToD;
3304
3305 UsingDecl *ToUsing = dyn_cast_or_null<UsingDecl>(
3306 Importer.Import(D->getUsingDecl()));
3307 if (!ToUsing)
3308 return nullptr;
3309
3310 NamedDecl *ToTarget = dyn_cast_or_null<NamedDecl>(
3311 Importer.Import(D->getTargetDecl()));
3312 if (!ToTarget)
3313 return nullptr;
3314
3315 UsingShadowDecl *ToShadow = UsingShadowDecl::Create(
3316 Importer.getToContext(), DC, Loc, ToUsing, ToTarget);
3317
3318 ToShadow->setLexicalDeclContext(LexicalDC);
3319 ToShadow->setAccess(D->getAccess());
3320 Importer.Imported(D, ToShadow);
3321
3322 if (UsingShadowDecl *FromPattern =
3323 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
3324 if (UsingShadowDecl *ToPattern =
3325 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern)))
3326 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow,
3327 ToPattern);
3328 else
3329 // FIXME: We return a nullptr here but the definition is already created
3330 // and available with lookups. How to fix this?..
3331 return nullptr;
3332 }
3333
3334 LexicalDC->addDeclInternal(ToShadow);
3335
3336 return ToShadow;
3337}
3338
3339
3340Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
3341 DeclContext *DC, *LexicalDC;
3342 DeclarationName Name;
3343 SourceLocation Loc;
3344 NamedDecl *ToD = nullptr;
3345 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3346 return nullptr;
3347 if (ToD)
3348 return ToD;
3349
3350 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor());
3351 if (!ToComAncestor)
3352 return nullptr;
3353
3354 NamespaceDecl *ToNominated = cast_or_null<NamespaceDecl>(
3355 Importer.Import(D->getNominatedNamespace()));
3356 if (!ToNominated)
3357 return nullptr;
3358
3359 UsingDirectiveDecl *ToUsingDir = UsingDirectiveDecl::Create(
3360 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3361 Importer.Import(D->getNamespaceKeyLocation()),
3362 Importer.Import(D->getQualifierLoc()),
3363 Importer.Import(D->getIdentLocation()), ToNominated, ToComAncestor);
3364 ToUsingDir->setLexicalDeclContext(LexicalDC);
3365 LexicalDC->addDeclInternal(ToUsingDir);
3366 Importer.Imported(D, ToUsingDir);
3367
3368 return ToUsingDir;
3369}
3370
3371Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl(
3372 UnresolvedUsingValueDecl *D) {
3373 DeclContext *DC, *LexicalDC;
3374 DeclarationName Name;
3375 SourceLocation Loc;
3376 NamedDecl *ToD = nullptr;
3377 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3378 return nullptr;
3379 if (ToD)
3380 return ToD;
3381
3382 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc()));
3383 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3384
3385 UnresolvedUsingValueDecl *ToUsingValue = UnresolvedUsingValueDecl::Create(
3386 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3387 Importer.Import(D->getQualifierLoc()), NameInfo,
3388 Importer.Import(D->getEllipsisLoc()));
3389
3390 Importer.Imported(D, ToUsingValue);
3391 ToUsingValue->setAccess(D->getAccess());
3392 ToUsingValue->setLexicalDeclContext(LexicalDC);
3393 LexicalDC->addDeclInternal(ToUsingValue);
3394
3395 return ToUsingValue;
3396}
3397
3398Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
3399 UnresolvedUsingTypenameDecl *D) {
3400 DeclContext *DC, *LexicalDC;
3401 DeclarationName Name;
3402 SourceLocation Loc;
3403 NamedDecl *ToD = nullptr;
3404 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3405 return nullptr;
3406 if (ToD)
3407 return ToD;
3408
3409 UnresolvedUsingTypenameDecl *ToUsing = UnresolvedUsingTypenameDecl::Create(
3410 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3411 Importer.Import(D->getTypenameLoc()),
3412 Importer.Import(D->getQualifierLoc()), Loc, Name,
3413 Importer.Import(D->getEllipsisLoc()));
3414
3415 Importer.Imported(D, ToUsing);
3416 ToUsing->setAccess(D->getAccess());
3417 ToUsing->setLexicalDeclContext(LexicalDC);
3418 LexicalDC->addDeclInternal(ToUsing);
3419
3420 return ToUsing;
3421}
3422
3423
Douglas Gregor2aa53772012-01-24 17:42:07 +00003424bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3425 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003426 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003427 if (To->getDefinition()) {
3428 // Check consistency of superclass.
3429 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3430 if (FromSuper) {
3431 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3432 if (!FromSuper)
3433 return true;
3434 }
3435
3436 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3437 if ((bool)FromSuper != (bool)ToSuper ||
3438 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3439 Importer.ToDiag(To->getLocation(),
3440 diag::err_odr_objc_superclass_inconsistent)
3441 << To->getDeclName();
3442 if (ToSuper)
3443 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3444 << To->getSuperClass()->getDeclName();
3445 else
3446 Importer.ToDiag(To->getLocation(),
3447 diag::note_odr_objc_missing_superclass);
3448 if (From->getSuperClass())
3449 Importer.FromDiag(From->getSuperClassLoc(),
3450 diag::note_odr_objc_superclass)
3451 << From->getSuperClass()->getDeclName();
3452 else
3453 Importer.FromDiag(From->getLocation(),
3454 diag::note_odr_objc_missing_superclass);
3455 }
3456
Douglas Gregor2e15c842012-02-01 21:00:38 +00003457 if (shouldForceImportDeclContext(Kind))
3458 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003459 return false;
3460 }
3461
3462 // Start the definition.
3463 To->startDefinition();
3464
3465 // If this class has a superclass, import it.
3466 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003467 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3468 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003469 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003470
3471 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003472 }
3473
3474 // Import protocols
3475 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3476 SmallVector<SourceLocation, 4> ProtocolLocs;
3477 ObjCInterfaceDecl::protocol_loc_iterator
3478 FromProtoLoc = From->protocol_loc_begin();
3479
3480 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3481 FromProtoEnd = From->protocol_end();
3482 FromProto != FromProtoEnd;
3483 ++FromProto, ++FromProtoLoc) {
3484 ObjCProtocolDecl *ToProto
3485 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3486 if (!ToProto)
3487 return true;
3488 Protocols.push_back(ToProto);
3489 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3490 }
3491
3492 // FIXME: If we're merging, make sure that the protocol list is the same.
3493 To->setProtocolList(Protocols.data(), Protocols.size(),
3494 ProtocolLocs.data(), Importer.getToContext());
3495
3496 // Import categories. When the categories themselves are imported, they'll
3497 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003498 for (auto *Cat : From->known_categories())
3499 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003500
Douglas Gregor2aa53772012-01-24 17:42:07 +00003501 // If we have an @implementation, import it as well.
3502 if (From->getImplementation()) {
3503 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3504 Importer.Import(From->getImplementation()));
3505 if (!Impl)
3506 return true;
3507
3508 To->setImplementation(Impl);
3509 }
3510
Douglas Gregor2e15c842012-02-01 21:00:38 +00003511 if (shouldForceImportDeclContext(Kind)) {
3512 // Import all of the members of this class.
3513 ImportDeclContext(From, /*ForceImport=*/true);
3514 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003515 return false;
3516}
3517
Douglas Gregor85f3f952015-07-07 03:57:15 +00003518ObjCTypeParamList *
3519ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3520 if (!list)
3521 return nullptr;
3522
3523 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3524 for (auto fromTypeParam : *list) {
3525 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3526 Importer.Import(fromTypeParam));
3527 if (!toTypeParam)
3528 return nullptr;
3529
3530 toTypeParams.push_back(toTypeParam);
3531 }
3532
3533 return ObjCTypeParamList::create(Importer.getToContext(),
3534 Importer.Import(list->getLAngleLoc()),
3535 toTypeParams,
3536 Importer.Import(list->getRAngleLoc()));
3537}
3538
Douglas Gregor45635322010-02-16 01:20:57 +00003539Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003540 // If this class has a definition in the translation unit we're coming from,
3541 // but this particular declaration is not that definition, import the
3542 // definition and map to that.
3543 ObjCInterfaceDecl *Definition = D->getDefinition();
3544 if (Definition && Definition != D) {
3545 Decl *ImportedDef = Importer.Import(Definition);
3546 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003547 return nullptr;
3548
Douglas Gregor2aa53772012-01-24 17:42:07 +00003549 return Importer.Imported(D, ImportedDef);
3550 }
3551
Douglas Gregor45635322010-02-16 01:20:57 +00003552 // Import the major distinguishing characteristics of an @interface.
3553 DeclContext *DC, *LexicalDC;
3554 DeclarationName Name;
3555 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003556 NamedDecl *ToD;
3557 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003558 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003559 if (ToD)
3560 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003561
Douglas Gregor2aa53772012-01-24 17:42:07 +00003562 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003563 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003564 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003565 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003566 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3567 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003568 continue;
3569
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003570 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003571 break;
3572 }
3573
Douglas Gregor2aa53772012-01-24 17:42:07 +00003574 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003575 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003576 if (!ToIface) {
3577 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3578 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003579 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003580 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00003581 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003582 D->isImplicitInterfaceDecl());
3583 ToIface->setLexicalDeclContext(LexicalDC);
3584 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003585 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003586 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003587 // Import the type parameter list after calling Imported, to avoid
3588 // loops when bringing in their DeclContext.
3589 ToIface->setTypeParamList(ImportObjCTypeParamList(
3590 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003591
Douglas Gregor2aa53772012-01-24 17:42:07 +00003592 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003593 return nullptr;
3594
Douglas Gregor98d156a2010-02-17 16:12:00 +00003595 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003596}
3597
Douglas Gregor4da9d682010-12-07 15:32:12 +00003598Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3599 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3600 Importer.Import(D->getCategoryDecl()));
3601 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003602 return nullptr;
3603
Douglas Gregor4da9d682010-12-07 15:32:12 +00003604 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3605 if (!ToImpl) {
3606 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3607 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003608 return nullptr;
3609
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003610 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003611 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003612 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003613 Category->getClassInterface(),
3614 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003615 Importer.Import(D->getAtStartLoc()),
3616 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003617
3618 DeclContext *LexicalDC = DC;
3619 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3620 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3621 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003622 return nullptr;
3623
Douglas Gregor4da9d682010-12-07 15:32:12 +00003624 ToImpl->setLexicalDeclContext(LexicalDC);
3625 }
3626
Sean Callanan95e74be2011-10-21 02:57:43 +00003627 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003628 Category->setImplementation(ToImpl);
3629 }
3630
3631 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003632 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003633 return ToImpl;
3634}
3635
Douglas Gregorda8025c2010-12-07 01:26:03 +00003636Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3637 // Find the corresponding interface.
3638 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3639 Importer.Import(D->getClassInterface()));
3640 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003641 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003642
3643 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003644 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003645 if (D->getSuperClass()) {
3646 Super = cast_or_null<ObjCInterfaceDecl>(
3647 Importer.Import(D->getSuperClass()));
3648 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003649 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003650 }
3651
3652 ObjCImplementationDecl *Impl = Iface->getImplementation();
3653 if (!Impl) {
3654 // We haven't imported an implementation yet. Create a new @implementation
3655 // now.
3656 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3657 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003658 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003659 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003660 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003661 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003662 Importer.Import(D->getIvarLBraceLoc()),
3663 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003664
3665 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3666 DeclContext *LexicalDC
3667 = Importer.ImportContext(D->getLexicalDeclContext());
3668 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003669 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003670 Impl->setLexicalDeclContext(LexicalDC);
3671 }
3672
3673 // Associate the implementation with the class it implements.
3674 Iface->setImplementation(Impl);
3675 Importer.Imported(D, Iface->getImplementation());
3676 } else {
3677 Importer.Imported(D, Iface->getImplementation());
3678
3679 // Verify that the existing @implementation has the same superclass.
3680 if ((Super && !Impl->getSuperClass()) ||
3681 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003682 (Super && Impl->getSuperClass() &&
3683 !declaresSameEntity(Super->getCanonicalDecl(),
3684 Impl->getSuperClass()))) {
3685 Importer.ToDiag(Impl->getLocation(),
3686 diag::err_odr_objc_superclass_inconsistent)
3687 << Iface->getDeclName();
3688 // FIXME: It would be nice to have the location of the superclass
3689 // below.
3690 if (Impl->getSuperClass())
3691 Importer.ToDiag(Impl->getLocation(),
3692 diag::note_odr_objc_superclass)
3693 << Impl->getSuperClass()->getDeclName();
3694 else
3695 Importer.ToDiag(Impl->getLocation(),
3696 diag::note_odr_objc_missing_superclass);
3697 if (D->getSuperClass())
3698 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003699 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003700 << D->getSuperClass()->getDeclName();
3701 else
3702 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003703 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003704 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003705 }
3706 }
3707
3708 // Import all of the members of this @implementation.
3709 ImportDeclContext(D);
3710
3711 return Impl;
3712}
3713
Douglas Gregora11c4582010-02-17 18:02:10 +00003714Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3715 // Import the major distinguishing characteristics of an @property.
3716 DeclContext *DC, *LexicalDC;
3717 DeclarationName Name;
3718 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003719 NamedDecl *ToD;
3720 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003721 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003722 if (ToD)
3723 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00003724
3725 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003726 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003727 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003728 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003729 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003730 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003731 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003732 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00003733 FoundProp->getType())) {
3734 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3735 << Name << D->getType() << FoundProp->getType();
3736 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3737 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003738 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003739 }
3740
3741 // FIXME: Check property attributes, getters, setters, etc.?
3742
3743 // Consider these properties to be equivalent.
3744 Importer.Imported(D, FoundProp);
3745 return FoundProp;
3746 }
3747 }
3748
3749 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00003750 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3751 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00003752 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003753
3754 // Create the new property.
3755 ObjCPropertyDecl *ToProperty
3756 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3757 Name.getAsIdentifierInfo(),
3758 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003759 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00003760 Importer.Import(D->getType()),
3761 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00003762 D->getPropertyImplementation());
3763 Importer.Imported(D, ToProperty);
3764 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003765 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003766
3767 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003768 ToProperty->setPropertyAttributesAsWritten(
3769 D->getPropertyAttributesAsWritten());
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +00003770 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
3771 Importer.Import(D->getGetterNameLoc()));
3772 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
3773 Importer.Import(D->getSetterNameLoc()));
Douglas Gregora11c4582010-02-17 18:02:10 +00003774 ToProperty->setGetterMethodDecl(
3775 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3776 ToProperty->setSetterMethodDecl(
3777 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3778 ToProperty->setPropertyIvarDecl(
3779 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3780 return ToProperty;
3781}
3782
Douglas Gregor14a49e22010-12-07 18:32:03 +00003783Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3784 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3785 Importer.Import(D->getPropertyDecl()));
3786 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00003787 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003788
3789 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3790 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003791 return nullptr;
3792
Douglas Gregor14a49e22010-12-07 18:32:03 +00003793 // Import the lexical declaration context.
3794 DeclContext *LexicalDC = DC;
3795 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3796 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3797 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003798 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003799 }
3800
3801 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3802 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00003803 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003804
3805 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00003806 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003807 if (D->getPropertyIvarDecl()) {
3808 Ivar = cast_or_null<ObjCIvarDecl>(
3809 Importer.Import(D->getPropertyIvarDecl()));
3810 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00003811 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003812 }
3813
3814 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00003815 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
3816 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00003817 if (!ToImpl) {
3818 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3819 Importer.Import(D->getLocStart()),
3820 Importer.Import(D->getLocation()),
3821 Property,
3822 D->getPropertyImplementation(),
3823 Ivar,
3824 Importer.Import(D->getPropertyIvarDeclLoc()));
3825 ToImpl->setLexicalDeclContext(LexicalDC);
3826 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003827 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003828 } else {
3829 // Check that we have the same kind of property implementation (@synthesize
3830 // vs. @dynamic).
3831 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3832 Importer.ToDiag(ToImpl->getLocation(),
3833 diag::err_odr_objc_property_impl_kind_inconsistent)
3834 << Property->getDeclName()
3835 << (ToImpl->getPropertyImplementation()
3836 == ObjCPropertyImplDecl::Dynamic);
3837 Importer.FromDiag(D->getLocation(),
3838 diag::note_odr_objc_property_impl_kind)
3839 << D->getPropertyDecl()->getDeclName()
3840 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00003841 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003842 }
3843
3844 // For @synthesize, check that we have the same
3845 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3846 Ivar != ToImpl->getPropertyIvarDecl()) {
3847 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3848 diag::err_odr_objc_synthesize_ivar_inconsistent)
3849 << Property->getDeclName()
3850 << ToImpl->getPropertyIvarDecl()->getDeclName()
3851 << Ivar->getDeclName();
3852 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3853 diag::note_odr_objc_synthesize_ivar_here)
3854 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00003855 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003856 }
3857
3858 // Merge the existing implementation with the new implementation.
3859 Importer.Imported(D, ToImpl);
3860 }
3861
3862 return ToImpl;
3863}
3864
Douglas Gregora082a492010-11-30 19:14:50 +00003865Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3866 // For template arguments, we adopt the translation unit as our declaration
3867 // context. This context will be fixed when the actual template declaration
3868 // is created.
3869
3870 // FIXME: Import default argument.
3871 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3872 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003873 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003874 Importer.Import(D->getLocation()),
3875 D->getDepth(),
3876 D->getIndex(),
3877 Importer.Import(D->getIdentifier()),
3878 D->wasDeclaredWithTypename(),
3879 D->isParameterPack());
3880}
3881
3882Decl *
3883ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3884 // Import the name of this declaration.
3885 DeclarationName Name = Importer.Import(D->getDeclName());
3886 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003887 return nullptr;
3888
Douglas Gregora082a492010-11-30 19:14:50 +00003889 // Import the location of this declaration.
3890 SourceLocation Loc = Importer.Import(D->getLocation());
3891
3892 // Import the type of this declaration.
3893 QualType T = Importer.Import(D->getType());
3894 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003895 return nullptr;
3896
Douglas Gregora082a492010-11-30 19:14:50 +00003897 // Import type-source information.
3898 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3899 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00003900 return nullptr;
3901
Douglas Gregora082a492010-11-30 19:14:50 +00003902 // FIXME: Import default argument.
3903
3904 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3905 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003906 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003907 Loc, D->getDepth(), D->getPosition(),
3908 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003909 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003910}
3911
3912Decl *
3913ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3914 // Import the name of this declaration.
3915 DeclarationName Name = Importer.Import(D->getDeclName());
3916 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003917 return nullptr;
3918
Douglas Gregora082a492010-11-30 19:14:50 +00003919 // Import the location of this declaration.
3920 SourceLocation Loc = Importer.Import(D->getLocation());
3921
3922 // Import template parameters.
3923 TemplateParameterList *TemplateParams
3924 = ImportTemplateParameterList(D->getTemplateParameters());
3925 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00003926 return nullptr;
3927
Douglas Gregora082a492010-11-30 19:14:50 +00003928 // FIXME: Import default argument.
3929
3930 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3931 Importer.getToContext().getTranslationUnitDecl(),
3932 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003933 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003934 Name.getAsIdentifierInfo(),
3935 TemplateParams);
3936}
3937
3938Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3939 // If this record has a definition in the translation unit we're coming from,
3940 // but this particular declaration is not that definition, import the
3941 // definition and map to that.
3942 CXXRecordDecl *Definition
3943 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3944 if (Definition && Definition != D->getTemplatedDecl()) {
3945 Decl *ImportedDef
3946 = Importer.Import(Definition->getDescribedClassTemplate());
3947 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003948 return nullptr;
3949
Douglas Gregora082a492010-11-30 19:14:50 +00003950 return Importer.Imported(D, ImportedDef);
3951 }
3952
3953 // Import the major distinguishing characteristics of this class template.
3954 DeclContext *DC, *LexicalDC;
3955 DeclarationName Name;
3956 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003957 NamedDecl *ToD;
3958 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003959 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003960 if (ToD)
3961 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003962
Douglas Gregora082a492010-11-30 19:14:50 +00003963 // We may already have a template of the same name; try to find and match it.
3964 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003965 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003966 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003967 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003968 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3969 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00003970 continue;
3971
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003972 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00003973 if (ClassTemplateDecl *FoundTemplate
3974 = dyn_cast<ClassTemplateDecl>(Found)) {
3975 if (IsStructuralMatch(D, FoundTemplate)) {
3976 // The class templates structurally match; call it the same template.
3977 // FIXME: We may be filling in a forward declaration here. Handle
3978 // this case!
3979 Importer.Imported(D->getTemplatedDecl(),
3980 FoundTemplate->getTemplatedDecl());
3981 return Importer.Imported(D, FoundTemplate);
3982 }
3983 }
3984
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003985 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00003986 }
3987
3988 if (!ConflictingDecls.empty()) {
3989 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3990 ConflictingDecls.data(),
3991 ConflictingDecls.size());
3992 }
3993
3994 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003995 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00003996 }
3997
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003998 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
3999
Douglas Gregora082a492010-11-30 19:14:50 +00004000 // Create the declaration that is being templated.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004001 CXXRecordDecl *ToTemplated = cast_or_null<CXXRecordDecl>(
4002 Importer.Import(FromTemplated));
4003 if (!ToTemplated)
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004004 return nullptr;
4005
4006 // Resolve possible cyclic import.
4007 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
4008 return AlreadyImported;
4009
Douglas Gregora082a492010-11-30 19:14:50 +00004010 // Create the class template declaration itself.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004011 TemplateParameterList *TemplateParams =
4012 ImportTemplateParameterList(D->getTemplateParameters());
Douglas Gregora082a492010-11-30 19:14:50 +00004013 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004014 return nullptr;
4015
Douglas Gregora082a492010-11-30 19:14:50 +00004016 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4017 Loc, Name, TemplateParams,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004018 ToTemplated);
4019 ToTemplated->setDescribedClassTemplate(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004020
4021 D2->setAccess(D->getAccess());
4022 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004023 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004024
4025 // Note the relationship between the class templates.
4026 Importer.Imported(D, D2);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004027 Importer.Imported(FromTemplated, ToTemplated);
Douglas Gregora082a492010-11-30 19:14:50 +00004028
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004029 if (FromTemplated->isCompleteDefinition() &&
4030 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004031 // FIXME: Import definition!
4032 }
4033
4034 return D2;
4035}
4036
Douglas Gregore2e50d332010-12-01 01:36:18 +00004037Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4038 ClassTemplateSpecializationDecl *D) {
4039 // If this record has a definition in the translation unit we're coming from,
4040 // but this particular declaration is not that definition, import the
4041 // definition and map to that.
4042 TagDecl *Definition = D->getDefinition();
4043 if (Definition && Definition != D) {
4044 Decl *ImportedDef = Importer.Import(Definition);
4045 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004046 return nullptr;
4047
Douglas Gregore2e50d332010-12-01 01:36:18 +00004048 return Importer.Imported(D, ImportedDef);
4049 }
4050
4051 ClassTemplateDecl *ClassTemplate
4052 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4053 D->getSpecializedTemplate()));
4054 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004055 return nullptr;
4056
Douglas Gregore2e50d332010-12-01 01:36:18 +00004057 // Import the context of this declaration.
4058 DeclContext *DC = ClassTemplate->getDeclContext();
4059 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004060 return nullptr;
4061
Douglas Gregore2e50d332010-12-01 01:36:18 +00004062 DeclContext *LexicalDC = DC;
4063 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4064 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4065 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004066 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004067 }
4068
4069 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004070 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4071 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004072
4073 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004074 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004075 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4076 D->getTemplateArgs().size(),
4077 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004078 return nullptr;
4079
Douglas Gregore2e50d332010-12-01 01:36:18 +00004080 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004081 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004082 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004083 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004084 if (D2) {
4085 // We already have a class template specialization with these template
4086 // arguments.
4087
4088 // FIXME: Check for specialization vs. instantiation errors.
4089
4090 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004091 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004092 // The record types structurally match, or the "from" translation
4093 // unit only had a forward declaration anyway; call it the same
4094 // function.
4095 return Importer.Imported(D, FoundDef);
4096 }
4097 }
4098 } else {
4099 // Create a new specialization.
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004100 if (ClassTemplatePartialSpecializationDecl *PartialSpec =
4101 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
4102
4103 // Import TemplateArgumentListInfo
4104 TemplateArgumentListInfo ToTAInfo;
4105 auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004106 if (ImportTemplateArgumentListInfo(ASTTemplateArgs.arguments(), ToTAInfo))
4107 return nullptr;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004108
4109 QualType CanonInjType = Importer.Import(
4110 PartialSpec->getInjectedSpecializationType());
4111 if (CanonInjType.isNull())
4112 return nullptr;
4113 CanonInjType = CanonInjType.getCanonicalType();
4114
4115 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4116 PartialSpec->getTemplateParameters());
4117 if (!ToTPList && PartialSpec->getTemplateParameters())
4118 return nullptr;
4119
4120 D2 = ClassTemplatePartialSpecializationDecl::Create(
4121 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
4122 ToTPList, ClassTemplate,
4123 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
4124 ToTAInfo, CanonInjType, nullptr);
4125
4126 } else {
4127 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4128 D->getTagKind(), DC,
4129 StartLoc, IdLoc,
4130 ClassTemplate,
4131 TemplateArgs,
4132 /*PrevDecl=*/nullptr);
4133 }
4134
Douglas Gregore2e50d332010-12-01 01:36:18 +00004135 D2->setSpecializationKind(D->getSpecializationKind());
4136
4137 // Add this specialization to the class template.
4138 ClassTemplate->AddSpecialization(D2, InsertPos);
4139
4140 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004141 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004142
4143 Importer.Imported(D, D2);
4144
4145 if (auto *TSI = D->getTypeAsWritten()) {
4146 TypeSourceInfo *TInfo = Importer.Import(TSI);
4147 if (!TInfo)
4148 return nullptr;
4149 D2->setTypeAsWritten(TInfo);
4150 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
4151 D2->setExternLoc(Importer.Import(D->getExternLoc()));
4152 }
4153
4154 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
4155 if (POI.isValid())
4156 D2->setPointOfInstantiation(POI);
4157 else if (D->getPointOfInstantiation().isValid())
4158 return nullptr;
4159
4160 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
4161
Douglas Gregore2e50d332010-12-01 01:36:18 +00004162 // Add the specialization to this context.
4163 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004164 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004165 }
4166 Importer.Imported(D, D2);
John McCallf937c022011-10-07 06:10:15 +00004167 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004168 return nullptr;
4169
Douglas Gregore2e50d332010-12-01 01:36:18 +00004170 return D2;
4171}
4172
Larisse Voufo39a1e502013-08-06 01:03:05 +00004173Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4174 // If this variable has a definition in the translation unit we're coming
4175 // from,
4176 // but this particular declaration is not that definition, import the
4177 // definition and map to that.
4178 VarDecl *Definition =
4179 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4180 if (Definition && Definition != D->getTemplatedDecl()) {
4181 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4182 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004183 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004184
4185 return Importer.Imported(D, ImportedDef);
4186 }
4187
4188 // Import the major distinguishing characteristics of this variable template.
4189 DeclContext *DC, *LexicalDC;
4190 DeclarationName Name;
4191 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004192 NamedDecl *ToD;
4193 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004194 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004195 if (ToD)
4196 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004197
4198 // We may already have a template of the same name; try to find and match it.
4199 assert(!DC->isFunctionOrMethod() &&
4200 "Variable templates cannot be declared at function scope");
4201 SmallVector<NamedDecl *, 4> ConflictingDecls;
4202 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004203 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004204 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4205 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4206 continue;
4207
4208 Decl *Found = FoundDecls[I];
4209 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4210 if (IsStructuralMatch(D, FoundTemplate)) {
4211 // The variable templates structurally match; call it the same template.
4212 Importer.Imported(D->getTemplatedDecl(),
4213 FoundTemplate->getTemplatedDecl());
4214 return Importer.Imported(D, FoundTemplate);
4215 }
4216 }
4217
4218 ConflictingDecls.push_back(FoundDecls[I]);
4219 }
4220
4221 if (!ConflictingDecls.empty()) {
4222 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4223 ConflictingDecls.data(),
4224 ConflictingDecls.size());
4225 }
4226
4227 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004228 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004229
4230 VarDecl *DTemplated = D->getTemplatedDecl();
4231
4232 // Import the type.
4233 QualType T = Importer.Import(DTemplated->getType());
4234 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004235 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004236
4237 // Create the declaration that is being templated.
4238 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4239 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4240 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4241 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4242 IdLoc, Name.getAsIdentifierInfo(), T,
4243 TInfo, DTemplated->getStorageClass());
4244 D2Templated->setAccess(DTemplated->getAccess());
4245 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4246 D2Templated->setLexicalDeclContext(LexicalDC);
4247
4248 // Importer.Imported(DTemplated, D2Templated);
4249 // LexicalDC->addDeclInternal(D2Templated);
4250
4251 // Merge the initializer.
4252 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00004253 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004254
4255 // Create the variable template declaration itself.
4256 TemplateParameterList *TemplateParams =
4257 ImportTemplateParameterList(D->getTemplateParameters());
4258 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004259 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004260
4261 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00004262 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004263 D2Templated->setDescribedVarTemplate(D2);
4264
4265 D2->setAccess(D->getAccess());
4266 D2->setLexicalDeclContext(LexicalDC);
4267 LexicalDC->addDeclInternal(D2);
4268
4269 // Note the relationship between the variable templates.
4270 Importer.Imported(D, D2);
4271 Importer.Imported(DTemplated, D2Templated);
4272
4273 if (DTemplated->isThisDeclarationADefinition() &&
4274 !D2Templated->isThisDeclarationADefinition()) {
4275 // FIXME: Import definition!
4276 }
4277
4278 return D2;
4279}
4280
4281Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4282 VarTemplateSpecializationDecl *D) {
4283 // If this record has a definition in the translation unit we're coming from,
4284 // but this particular declaration is not that definition, import the
4285 // definition and map to that.
4286 VarDecl *Definition = D->getDefinition();
4287 if (Definition && Definition != D) {
4288 Decl *ImportedDef = Importer.Import(Definition);
4289 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004290 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004291
4292 return Importer.Imported(D, ImportedDef);
4293 }
4294
4295 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4296 Importer.Import(D->getSpecializedTemplate()));
4297 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004298 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004299
4300 // Import the context of this declaration.
4301 DeclContext *DC = VarTemplate->getDeclContext();
4302 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004303 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004304
4305 DeclContext *LexicalDC = DC;
4306 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4307 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4308 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004309 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004310 }
4311
4312 // Import the location of this declaration.
4313 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4314 SourceLocation IdLoc = Importer.Import(D->getLocation());
4315
4316 // Import template arguments.
4317 SmallVector<TemplateArgument, 2> TemplateArgs;
4318 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4319 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004320 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004321
4322 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004323 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004324 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004325 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004326 if (D2) {
4327 // We already have a variable template specialization with these template
4328 // arguments.
4329
4330 // FIXME: Check for specialization vs. instantiation errors.
4331
4332 if (VarDecl *FoundDef = D2->getDefinition()) {
4333 if (!D->isThisDeclarationADefinition() ||
4334 IsStructuralMatch(D, FoundDef)) {
4335 // The record types structurally match, or the "from" translation
4336 // unit only had a forward declaration anyway; call it the same
4337 // variable.
4338 return Importer.Imported(D, FoundDef);
4339 }
4340 }
4341 } else {
4342
4343 // Import the type.
4344 QualType T = Importer.Import(D->getType());
4345 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004346 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004347 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4348
4349 // Create a new specialization.
4350 D2 = VarTemplateSpecializationDecl::Create(
4351 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00004352 D->getStorageClass(), TemplateArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004353 D2->setSpecializationKind(D->getSpecializationKind());
4354 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4355
4356 // Add this specialization to the class template.
4357 VarTemplate->AddSpecialization(D2, InsertPos);
4358
4359 // Import the qualifier, if any.
4360 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4361
4362 // Add the specialization to this context.
4363 D2->setLexicalDeclContext(LexicalDC);
4364 LexicalDC->addDeclInternal(D2);
4365 }
4366 Importer.Imported(D, D2);
4367
4368 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004369 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004370
4371 return D2;
4372}
4373
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004374Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
4375 DeclContext *DC, *LexicalDC;
4376 DeclarationName Name;
4377 SourceLocation Loc;
4378 NamedDecl *ToD;
4379
4380 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4381 return nullptr;
4382
4383 if (ToD)
4384 return ToD;
4385
4386 // Try to find a function in our own ("to") context with the same name, same
4387 // type, and in the same context as the function we're importing.
4388 if (!LexicalDC->isFunctionOrMethod()) {
4389 unsigned IDNS = Decl::IDNS_Ordinary;
4390 SmallVector<NamedDecl *, 2> FoundDecls;
4391 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
4392 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4393 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
4394 continue;
4395
4396 if (FunctionTemplateDecl *FoundFunction =
4397 dyn_cast<FunctionTemplateDecl>(FoundDecls[I])) {
4398 if (FoundFunction->hasExternalFormalLinkage() &&
4399 D->hasExternalFormalLinkage()) {
4400 if (IsStructuralMatch(D, FoundFunction)) {
4401 Importer.Imported(D, FoundFunction);
4402 // FIXME: Actually try to merge the body and other attributes.
4403 return FoundFunction;
4404 }
4405 }
4406 }
4407 }
4408 }
4409
4410 TemplateParameterList *Params =
4411 ImportTemplateParameterList(D->getTemplateParameters());
4412 if (!Params)
4413 return nullptr;
4414
4415 FunctionDecl *TemplatedFD =
4416 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl()));
4417 if (!TemplatedFD)
4418 return nullptr;
4419
4420 FunctionTemplateDecl *ToFunc = FunctionTemplateDecl::Create(
4421 Importer.getToContext(), DC, Loc, Name, Params, TemplatedFD);
4422
4423 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
4424 ToFunc->setAccess(D->getAccess());
4425 ToFunc->setLexicalDeclContext(LexicalDC);
4426 Importer.Imported(D, ToFunc);
4427
4428 LexicalDC->addDeclInternal(ToFunc);
4429 return ToFunc;
4430}
4431
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004432//----------------------------------------------------------------------------
4433// Import Statements
4434//----------------------------------------------------------------------------
4435
Sean Callanan59721b32015-04-28 18:41:46 +00004436DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4437 if (DG.isNull())
4438 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4439 size_t NumDecls = DG.end() - DG.begin();
4440 SmallVector<Decl *, 1> ToDecls(NumDecls);
4441 auto &_Importer = this->Importer;
4442 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4443 [&_Importer](Decl *D) -> Decl * {
4444 return _Importer.Import(D);
4445 });
4446 return DeclGroupRef::Create(Importer.getToContext(),
4447 ToDecls.begin(),
4448 NumDecls);
4449}
4450
4451 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4452 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4453 << S->getStmtClassName();
4454 return nullptr;
4455 }
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004456
4457
4458Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4459 SmallVector<IdentifierInfo *, 4> Names;
4460 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4461 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004462 // ToII is nullptr when no symbolic name is given for output operand
4463 // see ParseStmtAsm::ParseAsmOperandsOpt
4464 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004465 return nullptr;
4466 Names.push_back(ToII);
4467 }
4468 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4469 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004470 // ToII is nullptr when no symbolic name is given for input operand
4471 // see ParseStmtAsm::ParseAsmOperandsOpt
4472 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004473 return nullptr;
4474 Names.push_back(ToII);
4475 }
4476
4477 SmallVector<StringLiteral *, 4> Clobbers;
4478 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
4479 StringLiteral *Clobber = cast_or_null<StringLiteral>(
4480 Importer.Import(S->getClobberStringLiteral(I)));
4481 if (!Clobber)
4482 return nullptr;
4483 Clobbers.push_back(Clobber);
4484 }
4485
4486 SmallVector<StringLiteral *, 4> Constraints;
4487 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4488 StringLiteral *Output = cast_or_null<StringLiteral>(
4489 Importer.Import(S->getOutputConstraintLiteral(I)));
4490 if (!Output)
4491 return nullptr;
4492 Constraints.push_back(Output);
4493 }
4494
4495 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4496 StringLiteral *Input = cast_or_null<StringLiteral>(
4497 Importer.Import(S->getInputConstraintLiteral(I)));
4498 if (!Input)
4499 return nullptr;
4500 Constraints.push_back(Input);
4501 }
4502
4503 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004504 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004505 return nullptr;
4506
Aleksei Sidorina693b372016-09-28 10:16:56 +00004507 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004508 return nullptr;
4509
4510 StringLiteral *AsmStr = cast_or_null<StringLiteral>(
4511 Importer.Import(S->getAsmString()));
4512 if (!AsmStr)
4513 return nullptr;
4514
4515 return new (Importer.getToContext()) GCCAsmStmt(
4516 Importer.getToContext(),
4517 Importer.Import(S->getAsmLoc()),
4518 S->isSimple(),
4519 S->isVolatile(),
4520 S->getNumOutputs(),
4521 S->getNumInputs(),
4522 Names.data(),
4523 Constraints.data(),
4524 Exprs.data(),
4525 AsmStr,
4526 S->getNumClobbers(),
4527 Clobbers.data(),
4528 Importer.Import(S->getRParenLoc()));
4529}
4530
Sean Callanan59721b32015-04-28 18:41:46 +00004531Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4532 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4533 for (Decl *ToD : ToDG) {
4534 if (!ToD)
4535 return nullptr;
4536 }
4537 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4538 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4539 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4540}
4541
4542Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4543 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4544 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4545 S->hasLeadingEmptyMacro());
4546}
4547
4548Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004549 llvm::SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004550
4551 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00004552 return nullptr;
4553
Sean Callanan59721b32015-04-28 18:41:46 +00004554 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4555 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
Benjamin Kramer07420902017-12-24 16:24:20 +00004556 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
4557 ToRBraceLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004558}
4559
4560Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4561 Expr *ToLHS = Importer.Import(S->getLHS());
4562 if (!ToLHS)
4563 return nullptr;
4564 Expr *ToRHS = Importer.Import(S->getRHS());
4565 if (!ToRHS && S->getRHS())
4566 return nullptr;
Gabor Horvath480892b2017-10-18 09:25:18 +00004567 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4568 if (!ToSubStmt && S->getSubStmt())
4569 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004570 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4571 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4572 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
Gabor Horvath480892b2017-10-18 09:25:18 +00004573 CaseStmt *ToStmt = new (Importer.getToContext())
4574 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
4575 ToStmt->setSubStmt(ToSubStmt);
4576 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00004577}
4578
4579Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4580 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4581 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4582 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4583 if (!ToSubStmt && S->getSubStmt())
4584 return nullptr;
4585 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4586 ToSubStmt);
4587}
4588
4589Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4590 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4591 LabelDecl *ToLabelDecl =
4592 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4593 if (!ToLabelDecl && S->getDecl())
4594 return nullptr;
4595 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4596 if (!ToSubStmt && S->getSubStmt())
4597 return nullptr;
4598 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4599 ToSubStmt);
4600}
4601
4602Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4603 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4604 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4605 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4606 ASTContext &_ToContext = Importer.getToContext();
4607 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4608 [&_ToContext](const Attr *A) -> const Attr * {
4609 return A->clone(_ToContext);
4610 });
4611 for (const Attr *ToA : ToAttrs) {
4612 if (!ToA)
4613 return nullptr;
4614 }
4615 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4616 if (!ToSubStmt && S->getSubStmt())
4617 return nullptr;
4618 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4619 ToAttrs, ToSubStmt);
4620}
4621
4622Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4623 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00004624 Stmt *ToInit = Importer.Import(S->getInit());
4625 if (!ToInit && S->getInit())
4626 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004627 VarDecl *ToConditionVariable = nullptr;
4628 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4629 ToConditionVariable =
4630 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4631 if (!ToConditionVariable)
4632 return nullptr;
4633 }
4634 Expr *ToCondition = Importer.Import(S->getCond());
4635 if (!ToCondition && S->getCond())
4636 return nullptr;
4637 Stmt *ToThenStmt = Importer.Import(S->getThen());
4638 if (!ToThenStmt && S->getThen())
4639 return nullptr;
4640 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4641 Stmt *ToElseStmt = Importer.Import(S->getElse());
4642 if (!ToElseStmt && S->getElse())
4643 return nullptr;
4644 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00004645 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00004646 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00004647 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00004648 ToCondition, ToThenStmt,
4649 ToElseLoc, ToElseStmt);
4650}
4651
4652Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00004653 Stmt *ToInit = Importer.Import(S->getInit());
4654 if (!ToInit && S->getInit())
4655 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004656 VarDecl *ToConditionVariable = nullptr;
4657 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4658 ToConditionVariable =
4659 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4660 if (!ToConditionVariable)
4661 return nullptr;
4662 }
4663 Expr *ToCondition = Importer.Import(S->getCond());
4664 if (!ToCondition && S->getCond())
4665 return nullptr;
4666 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00004667 Importer.getToContext(), ToInit,
4668 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00004669 Stmt *ToBody = Importer.Import(S->getBody());
4670 if (!ToBody && S->getBody())
4671 return nullptr;
4672 ToStmt->setBody(ToBody);
4673 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4674 // Now we have to re-chain the cases.
4675 SwitchCase *LastChainedSwitchCase = nullptr;
4676 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4677 SC = SC->getNextSwitchCase()) {
4678 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4679 if (!ToSC)
4680 return nullptr;
4681 if (LastChainedSwitchCase)
4682 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4683 else
4684 ToStmt->setSwitchCaseList(ToSC);
4685 LastChainedSwitchCase = ToSC;
4686 }
4687 return ToStmt;
4688}
4689
4690Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4691 VarDecl *ToConditionVariable = nullptr;
4692 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4693 ToConditionVariable =
4694 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4695 if (!ToConditionVariable)
4696 return nullptr;
4697 }
4698 Expr *ToCondition = Importer.Import(S->getCond());
4699 if (!ToCondition && S->getCond())
4700 return nullptr;
4701 Stmt *ToBody = Importer.Import(S->getBody());
4702 if (!ToBody && S->getBody())
4703 return nullptr;
4704 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4705 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4706 ToConditionVariable,
4707 ToCondition, ToBody,
4708 ToWhileLoc);
4709}
4710
4711Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4712 Stmt *ToBody = Importer.Import(S->getBody());
4713 if (!ToBody && S->getBody())
4714 return nullptr;
4715 Expr *ToCondition = Importer.Import(S->getCond());
4716 if (!ToCondition && S->getCond())
4717 return nullptr;
4718 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4719 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4720 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4721 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4722 ToDoLoc, ToWhileLoc,
4723 ToRParenLoc);
4724}
4725
4726Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4727 Stmt *ToInit = Importer.Import(S->getInit());
4728 if (!ToInit && S->getInit())
4729 return nullptr;
4730 Expr *ToCondition = Importer.Import(S->getCond());
4731 if (!ToCondition && S->getCond())
4732 return nullptr;
4733 VarDecl *ToConditionVariable = nullptr;
4734 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4735 ToConditionVariable =
4736 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4737 if (!ToConditionVariable)
4738 return nullptr;
4739 }
4740 Expr *ToInc = Importer.Import(S->getInc());
4741 if (!ToInc && S->getInc())
4742 return nullptr;
4743 Stmt *ToBody = Importer.Import(S->getBody());
4744 if (!ToBody && S->getBody())
4745 return nullptr;
4746 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4747 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4748 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4749 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4750 ToInit, ToCondition,
4751 ToConditionVariable,
4752 ToInc, ToBody,
4753 ToForLoc, ToLParenLoc,
4754 ToRParenLoc);
4755}
4756
4757Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4758 LabelDecl *ToLabel = nullptr;
4759 if (LabelDecl *FromLabel = S->getLabel()) {
4760 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4761 if (!ToLabel)
4762 return nullptr;
4763 }
4764 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4765 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4766 return new (Importer.getToContext()) GotoStmt(ToLabel,
4767 ToGotoLoc, ToLabelLoc);
4768}
4769
4770Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4771 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4772 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4773 Expr *ToTarget = Importer.Import(S->getTarget());
4774 if (!ToTarget && S->getTarget())
4775 return nullptr;
4776 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4777 ToTarget);
4778}
4779
4780Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4781 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4782 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4783}
4784
4785Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4786 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4787 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4788}
4789
4790Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4791 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4792 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4793 if (!ToRetExpr && S->getRetValue())
4794 return nullptr;
4795 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4796 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4797 if (!ToNRVOCandidate && NRVOCandidate)
4798 return nullptr;
4799 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4800 ToNRVOCandidate);
4801}
4802
4803Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4804 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4805 VarDecl *ToExceptionDecl = nullptr;
4806 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4807 ToExceptionDecl =
4808 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4809 if (!ToExceptionDecl)
4810 return nullptr;
4811 }
4812 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4813 if (!ToHandlerBlock && S->getHandlerBlock())
4814 return nullptr;
4815 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4816 ToExceptionDecl,
4817 ToHandlerBlock);
4818}
4819
4820Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4821 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4822 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4823 if (!ToTryBlock && S->getTryBlock())
4824 return nullptr;
4825 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4826 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4827 CXXCatchStmt *FromHandler = S->getHandler(HI);
4828 if (Stmt *ToHandler = Importer.Import(FromHandler))
4829 ToHandlers[HI] = ToHandler;
4830 else
4831 return nullptr;
4832 }
4833 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4834 ToHandlers);
4835}
4836
4837Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4838 DeclStmt *ToRange =
4839 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4840 if (!ToRange && S->getRangeStmt())
4841 return nullptr;
Richard Smith01694c32016-03-20 10:33:40 +00004842 DeclStmt *ToBegin =
4843 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
4844 if (!ToBegin && S->getBeginStmt())
4845 return nullptr;
4846 DeclStmt *ToEnd =
4847 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
4848 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00004849 return nullptr;
4850 Expr *ToCond = Importer.Import(S->getCond());
4851 if (!ToCond && S->getCond())
4852 return nullptr;
4853 Expr *ToInc = Importer.Import(S->getInc());
4854 if (!ToInc && S->getInc())
4855 return nullptr;
4856 DeclStmt *ToLoopVar =
4857 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4858 if (!ToLoopVar && S->getLoopVarStmt())
4859 return nullptr;
4860 Stmt *ToBody = Importer.Import(S->getBody());
4861 if (!ToBody && S->getBody())
4862 return nullptr;
4863 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00004864 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00004865 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4866 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00004867 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00004868 ToCond, ToInc,
4869 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00004870 ToForLoc, ToCoawaitLoc,
4871 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004872}
4873
4874Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
4875 Stmt *ToElem = Importer.Import(S->getElement());
4876 if (!ToElem && S->getElement())
4877 return nullptr;
4878 Expr *ToCollect = Importer.Import(S->getCollection());
4879 if (!ToCollect && S->getCollection())
4880 return nullptr;
4881 Stmt *ToBody = Importer.Import(S->getBody());
4882 if (!ToBody && S->getBody())
4883 return nullptr;
4884 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4885 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4886 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
4887 ToCollect,
4888 ToBody, ToForLoc,
4889 ToRParenLoc);
4890}
4891
4892Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
4893 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
4894 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4895 VarDecl *ToExceptionDecl = nullptr;
4896 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
4897 ToExceptionDecl =
4898 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4899 if (!ToExceptionDecl)
4900 return nullptr;
4901 }
4902 Stmt *ToBody = Importer.Import(S->getCatchBody());
4903 if (!ToBody && S->getCatchBody())
4904 return nullptr;
4905 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
4906 ToRParenLoc,
4907 ToExceptionDecl,
4908 ToBody);
4909}
4910
4911Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
4912 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
4913 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
4914 if (!ToAtFinallyStmt && S->getFinallyBody())
4915 return nullptr;
4916 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
4917 ToAtFinallyStmt);
4918}
4919
4920Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
4921 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
4922 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
4923 if (!ToAtTryStmt && S->getTryBody())
4924 return nullptr;
4925 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
4926 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
4927 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
4928 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
4929 ToCatchStmts[CI] = ToCatchStmt;
4930 else
4931 return nullptr;
4932 }
4933 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
4934 if (!ToAtFinallyStmt && S->getFinallyStmt())
4935 return nullptr;
4936 return ObjCAtTryStmt::Create(Importer.getToContext(),
4937 ToAtTryLoc, ToAtTryStmt,
4938 ToCatchStmts.begin(), ToCatchStmts.size(),
4939 ToAtFinallyStmt);
4940}
4941
4942Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
4943 (ObjCAtSynchronizedStmt *S) {
4944 SourceLocation ToAtSynchronizedLoc =
4945 Importer.Import(S->getAtSynchronizedLoc());
4946 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
4947 if (!ToSynchExpr && S->getSynchExpr())
4948 return nullptr;
4949 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
4950 if (!ToSynchBody && S->getSynchBody())
4951 return nullptr;
4952 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
4953 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
4954}
4955
4956Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
4957 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
4958 Expr *ToThrow = Importer.Import(S->getThrowExpr());
4959 if (!ToThrow && S->getThrowExpr())
4960 return nullptr;
4961 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
4962}
4963
4964Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
4965 (ObjCAutoreleasePoolStmt *S) {
4966 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
4967 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4968 if (!ToSubStmt && S->getSubStmt())
4969 return nullptr;
4970 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
4971 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004972}
4973
4974//----------------------------------------------------------------------------
4975// Import Expressions
4976//----------------------------------------------------------------------------
4977Expr *ASTNodeImporter::VisitExpr(Expr *E) {
4978 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
4979 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00004980 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004981}
4982
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004983Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
4984 QualType T = Importer.Import(E->getType());
4985 if (T.isNull())
4986 return nullptr;
4987
4988 Expr *SubExpr = Importer.Import(E->getSubExpr());
4989 if (!SubExpr && E->getSubExpr())
4990 return nullptr;
4991
4992 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
4993 if (!TInfo)
4994 return nullptr;
4995
4996 return new (Importer.getToContext()) VAArgExpr(
4997 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
4998 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
4999}
5000
5001
5002Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5003 QualType T = Importer.Import(E->getType());
5004 if (T.isNull())
5005 return nullptr;
5006
5007 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005008 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005009}
5010
5011Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5012 QualType T = Importer.Import(E->getType());
5013 if (T.isNull())
5014 return nullptr;
5015
5016 StringLiteral *SL = cast_or_null<StringLiteral>(
5017 Importer.Import(E->getFunctionName()));
5018 if (!SL && E->getFunctionName())
5019 return nullptr;
5020
5021 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005022 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005023}
5024
Douglas Gregor52f820e2010-02-19 01:17:02 +00005025Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00005026 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
5027 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005028 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005029
Craig Topper36250ad2014-05-12 05:36:57 +00005030 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005031 if (E->getDecl() != E->getFoundDecl()) {
5032 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5033 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005034 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005035 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005036
5037 QualType T = Importer.Import(E->getType());
5038 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005039 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005040
Aleksei Sidorina693b372016-09-28 10:16:56 +00005041
5042 TemplateArgumentListInfo ToTAInfo;
5043 TemplateArgumentListInfo *ResInfo = nullptr;
5044 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005045 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
5046 return nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00005047 ResInfo = &ToTAInfo;
5048 }
5049
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005050 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5051 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005052 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005053 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005054 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005055 Importer.Import(E->getLocation()),
5056 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005057 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005058 if (E->hadMultipleCandidates())
5059 DRE->setHadMultipleCandidates(true);
5060 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005061}
5062
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005063Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5064 QualType T = Importer.Import(E->getType());
5065 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00005066 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005067
5068 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5069}
5070
5071ASTNodeImporter::Designator
5072ASTNodeImporter::ImportDesignator(const Designator &D) {
5073 if (D.isFieldDesignator()) {
5074 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5075 // Caller checks for import error
5076 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5077 Importer.Import(D.getFieldLoc()));
5078 }
5079 if (D.isArrayDesignator())
5080 return Designator(D.getFirstExprIndex(),
5081 Importer.Import(D.getLBracketLoc()),
5082 Importer.Import(D.getRBracketLoc()));
5083
5084 assert(D.isArrayRangeDesignator());
5085 return Designator(D.getFirstExprIndex(),
5086 Importer.Import(D.getLBracketLoc()),
5087 Importer.Import(D.getEllipsisLoc()),
5088 Importer.Import(D.getRBracketLoc()));
5089}
5090
5091
5092Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
5093 Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
5094 if (!Init)
5095 return nullptr;
5096
5097 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5098 // List elements from the second, the first is Init itself
5099 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
5100 if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
5101 IndexExprs[I - 1] = Arg;
5102 else
5103 return nullptr;
5104 }
5105
5106 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005107 llvm::transform(DIE->designators(), Designators.begin(),
5108 [this](const Designator &D) -> Designator {
5109 return ImportDesignator(D);
5110 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005111
David Majnemerf7e36092016-06-23 00:15:04 +00005112 for (const Designator &D : DIE->designators())
5113 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005114 return nullptr;
5115
5116 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005117 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005118 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5119 DIE->usesGNUSyntax(), Init);
5120}
5121
5122Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5123 QualType T = Importer.Import(E->getType());
5124 if (T.isNull())
5125 return nullptr;
5126
5127 return new (Importer.getToContext())
5128 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5129}
5130
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005131Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5132 QualType T = Importer.Import(E->getType());
5133 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005134 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005135
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005136 return IntegerLiteral::Create(Importer.getToContext(),
5137 E->getValue(), T,
5138 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005139}
5140
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005141Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5142 QualType T = Importer.Import(E->getType());
5143 if (T.isNull())
5144 return nullptr;
5145
5146 return FloatingLiteral::Create(Importer.getToContext(),
5147 E->getValue(), E->isExact(), T,
5148 Importer.Import(E->getLocation()));
5149}
5150
Douglas Gregor623421d2010-02-18 02:21:22 +00005151Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5152 QualType T = Importer.Import(E->getType());
5153 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005154 return nullptr;
5155
Douglas Gregorfb65e592011-07-27 05:40:30 +00005156 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5157 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005158 Importer.Import(E->getLocation()));
5159}
5160
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005161Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5162 QualType T = Importer.Import(E->getType());
5163 if (T.isNull())
5164 return nullptr;
5165
5166 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5167 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5168
5169 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5170 E->getKind(), E->isPascal(), T,
5171 Locations.data(), Locations.size());
5172}
5173
5174Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5175 QualType T = Importer.Import(E->getType());
5176 if (T.isNull())
5177 return nullptr;
5178
5179 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5180 if (!TInfo)
5181 return nullptr;
5182
5183 Expr *Init = Importer.Import(E->getInitializer());
5184 if (!Init)
5185 return nullptr;
5186
5187 return new (Importer.getToContext()) CompoundLiteralExpr(
5188 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5189 Init, E->isFileScope());
5190}
5191
5192Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5193 QualType T = Importer.Import(E->getType());
5194 if (T.isNull())
5195 return nullptr;
5196
5197 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5198 if (ImportArrayChecked(
5199 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5200 Exprs.begin()))
5201 return nullptr;
5202
5203 return new (Importer.getToContext()) AtomicExpr(
5204 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5205 Importer.Import(E->getRParenLoc()));
5206}
5207
5208Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5209 QualType T = Importer.Import(E->getType());
5210 if (T.isNull())
5211 return nullptr;
5212
5213 LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
5214 if (!ToLabel)
5215 return nullptr;
5216
5217 return new (Importer.getToContext()) AddrLabelExpr(
5218 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5219 ToLabel, T);
5220}
5221
Douglas Gregorc74247e2010-02-19 01:07:06 +00005222Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5223 Expr *SubExpr = Importer.Import(E->getSubExpr());
5224 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005225 return nullptr;
5226
Douglas Gregorc74247e2010-02-19 01:07:06 +00005227 return new (Importer.getToContext())
5228 ParenExpr(Importer.Import(E->getLParen()),
5229 Importer.Import(E->getRParen()),
5230 SubExpr);
5231}
5232
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005233Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5234 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005235 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005236 return nullptr;
5237
5238 return new (Importer.getToContext()) ParenListExpr(
5239 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5240 Exprs, Importer.Import(E->getLParenLoc()));
5241}
5242
5243Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5244 QualType T = Importer.Import(E->getType());
5245 if (T.isNull())
5246 return nullptr;
5247
5248 CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>(
5249 Importer.Import(E->getSubStmt()));
5250 if (!ToSubStmt && E->getSubStmt())
5251 return nullptr;
5252
5253 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5254 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5255}
5256
Douglas Gregorc74247e2010-02-19 01:07:06 +00005257Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5258 QualType T = Importer.Import(E->getType());
5259 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005260 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005261
5262 Expr *SubExpr = Importer.Import(E->getSubExpr());
5263 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005264 return nullptr;
5265
Aaron Ballmana5038552018-01-09 13:07:03 +00005266 return new (Importer.getToContext()) UnaryOperator(
5267 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(),
5268 Importer.Import(E->getOperatorLoc()), E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005269}
5270
Aaron Ballmana5038552018-01-09 13:07:03 +00005271Expr *
5272ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005273 QualType ResultType = Importer.Import(E->getType());
5274
5275 if (E->isArgumentType()) {
5276 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5277 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005278 return nullptr;
5279
Peter Collingbournee190dee2011-03-11 19:24:49 +00005280 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5281 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005282 Importer.Import(E->getOperatorLoc()),
5283 Importer.Import(E->getRParenLoc()));
5284 }
5285
5286 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5287 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005288 return nullptr;
5289
Peter Collingbournee190dee2011-03-11 19:24:49 +00005290 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5291 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005292 Importer.Import(E->getOperatorLoc()),
5293 Importer.Import(E->getRParenLoc()));
5294}
5295
Douglas Gregorc74247e2010-02-19 01:07:06 +00005296Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5297 QualType T = Importer.Import(E->getType());
5298 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005299 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005300
5301 Expr *LHS = Importer.Import(E->getLHS());
5302 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005303 return nullptr;
5304
Douglas Gregorc74247e2010-02-19 01:07:06 +00005305 Expr *RHS = Importer.Import(E->getRHS());
5306 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005307 return nullptr;
5308
Douglas Gregorc74247e2010-02-19 01:07:06 +00005309 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005310 T, E->getValueKind(),
5311 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005312 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005313 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005314}
5315
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005316Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5317 QualType T = Importer.Import(E->getType());
5318 if (T.isNull())
5319 return nullptr;
5320
5321 Expr *ToLHS = Importer.Import(E->getLHS());
5322 if (!ToLHS)
5323 return nullptr;
5324
5325 Expr *ToRHS = Importer.Import(E->getRHS());
5326 if (!ToRHS)
5327 return nullptr;
5328
5329 Expr *ToCond = Importer.Import(E->getCond());
5330 if (!ToCond)
5331 return nullptr;
5332
5333 return new (Importer.getToContext()) ConditionalOperator(
5334 ToCond, Importer.Import(E->getQuestionLoc()),
5335 ToLHS, Importer.Import(E->getColonLoc()),
5336 ToRHS, T, E->getValueKind(), E->getObjectKind());
5337}
5338
5339Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5340 BinaryConditionalOperator *E) {
5341 QualType T = Importer.Import(E->getType());
5342 if (T.isNull())
5343 return nullptr;
5344
5345 Expr *Common = Importer.Import(E->getCommon());
5346 if (!Common)
5347 return nullptr;
5348
5349 Expr *Cond = Importer.Import(E->getCond());
5350 if (!Cond)
5351 return nullptr;
5352
5353 OpaqueValueExpr *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5354 Importer.Import(E->getOpaqueValue()));
5355 if (!OpaqueValue)
5356 return nullptr;
5357
5358 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5359 if (!TrueExpr)
5360 return nullptr;
5361
5362 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5363 if (!FalseExpr)
5364 return nullptr;
5365
5366 return new (Importer.getToContext()) BinaryConditionalOperator(
5367 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5368 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5369 T, E->getValueKind(), E->getObjectKind());
5370}
5371
Aleksei Sidorina693b372016-09-28 10:16:56 +00005372Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5373 QualType T = Importer.Import(E->getType());
5374 if (T.isNull())
5375 return nullptr;
5376
5377 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5378 if (!ToQueried)
5379 return nullptr;
5380
5381 Expr *Dim = Importer.Import(E->getDimensionExpression());
5382 if (!Dim && E->getDimensionExpression())
5383 return nullptr;
5384
5385 return new (Importer.getToContext()) ArrayTypeTraitExpr(
5386 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5387 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
5388}
5389
5390Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5391 QualType T = Importer.Import(E->getType());
5392 if (T.isNull())
5393 return nullptr;
5394
5395 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5396 if (!ToQueried)
5397 return nullptr;
5398
5399 return new (Importer.getToContext()) ExpressionTraitExpr(
5400 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5401 E->getValue(), Importer.Import(E->getLocEnd()), T);
5402}
5403
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005404Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5405 QualType T = Importer.Import(E->getType());
5406 if (T.isNull())
5407 return nullptr;
5408
5409 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5410 if (!SourceExpr && E->getSourceExpr())
5411 return nullptr;
5412
5413 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005414 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005415 E->getObjectKind(), SourceExpr);
5416}
5417
Aleksei Sidorina693b372016-09-28 10:16:56 +00005418Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5419 QualType T = Importer.Import(E->getType());
5420 if (T.isNull())
5421 return nullptr;
5422
5423 Expr *ToLHS = Importer.Import(E->getLHS());
5424 if (!ToLHS)
5425 return nullptr;
5426
5427 Expr *ToRHS = Importer.Import(E->getRHS());
5428 if (!ToRHS)
5429 return nullptr;
5430
5431 return new (Importer.getToContext()) ArraySubscriptExpr(
5432 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5433 Importer.Import(E->getRBracketLoc()));
5434}
5435
Douglas Gregorc74247e2010-02-19 01:07:06 +00005436Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5437 QualType T = Importer.Import(E->getType());
5438 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005439 return nullptr;
5440
Douglas Gregorc74247e2010-02-19 01:07:06 +00005441 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5442 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005443 return nullptr;
5444
Douglas Gregorc74247e2010-02-19 01:07:06 +00005445 QualType CompResultType = Importer.Import(E->getComputationResultType());
5446 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005447 return nullptr;
5448
Douglas Gregorc74247e2010-02-19 01:07:06 +00005449 Expr *LHS = Importer.Import(E->getLHS());
5450 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005451 return nullptr;
5452
Douglas Gregorc74247e2010-02-19 01:07:06 +00005453 Expr *RHS = Importer.Import(E->getRHS());
5454 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005455 return nullptr;
5456
Douglas Gregorc74247e2010-02-19 01:07:06 +00005457 return new (Importer.getToContext())
5458 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005459 T, E->getValueKind(),
5460 E->getObjectKind(),
5461 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005462 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005463 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005464}
5465
Aleksei Sidorina693b372016-09-28 10:16:56 +00005466bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
5467 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
5468 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
5469 Path.push_back(Spec);
5470 else
5471 return true;
5472 }
5473 return false;
John McCallcf142162010-08-07 06:22:56 +00005474}
5475
Douglas Gregor98c10182010-02-12 22:17:39 +00005476Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5477 QualType T = Importer.Import(E->getType());
5478 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005479 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005480
5481 Expr *SubExpr = Importer.Import(E->getSubExpr());
5482 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005483 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005484
5485 CXXCastPath BasePath;
5486 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005487 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005488
5489 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005490 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005491}
5492
Aleksei Sidorina693b372016-09-28 10:16:56 +00005493Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00005494 QualType T = Importer.Import(E->getType());
5495 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005496 return nullptr;
5497
Douglas Gregor5481d322010-02-19 01:32:14 +00005498 Expr *SubExpr = Importer.Import(E->getSubExpr());
5499 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005500 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005501
5502 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5503 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005504 return nullptr;
5505
John McCallcf142162010-08-07 06:22:56 +00005506 CXXCastPath BasePath;
5507 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005508 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005509
Aleksei Sidorina693b372016-09-28 10:16:56 +00005510 switch (E->getStmtClass()) {
5511 case Stmt::CStyleCastExprClass: {
5512 CStyleCastExpr *CCE = cast<CStyleCastExpr>(E);
5513 return CStyleCastExpr::Create(Importer.getToContext(), T,
5514 E->getValueKind(), E->getCastKind(),
5515 SubExpr, &BasePath, TInfo,
5516 Importer.Import(CCE->getLParenLoc()),
5517 Importer.Import(CCE->getRParenLoc()));
5518 }
5519
5520 case Stmt::CXXFunctionalCastExprClass: {
5521 CXXFunctionalCastExpr *FCE = cast<CXXFunctionalCastExpr>(E);
5522 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
5523 E->getValueKind(), TInfo,
5524 E->getCastKind(), SubExpr, &BasePath,
5525 Importer.Import(FCE->getLParenLoc()),
5526 Importer.Import(FCE->getRParenLoc()));
5527 }
5528
5529 case Stmt::ObjCBridgedCastExprClass: {
5530 ObjCBridgedCastExpr *OCE = cast<ObjCBridgedCastExpr>(E);
5531 return new (Importer.getToContext()) ObjCBridgedCastExpr(
5532 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
5533 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
5534 TInfo, SubExpr);
5535 }
5536 default:
5537 break; // just fall through
5538 }
5539
5540 CXXNamedCastExpr *Named = cast<CXXNamedCastExpr>(E);
5541 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
5542 RParenLoc = Importer.Import(Named->getRParenLoc());
5543 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
5544
5545 switch (E->getStmtClass()) {
5546 case Stmt::CXXStaticCastExprClass:
5547 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
5548 E->getValueKind(), E->getCastKind(),
5549 SubExpr, &BasePath, TInfo,
5550 ExprLoc, RParenLoc, Brackets);
5551
5552 case Stmt::CXXDynamicCastExprClass:
5553 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
5554 E->getValueKind(), E->getCastKind(),
5555 SubExpr, &BasePath, TInfo,
5556 ExprLoc, RParenLoc, Brackets);
5557
5558 case Stmt::CXXReinterpretCastExprClass:
5559 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
5560 E->getValueKind(), E->getCastKind(),
5561 SubExpr, &BasePath, TInfo,
5562 ExprLoc, RParenLoc, Brackets);
5563
5564 case Stmt::CXXConstCastExprClass:
5565 return CXXConstCastExpr::Create(Importer.getToContext(), T,
5566 E->getValueKind(), SubExpr, TInfo, ExprLoc,
5567 RParenLoc, Brackets);
5568 default:
5569 llvm_unreachable("Cast expression of unsupported type!");
5570 return nullptr;
5571 }
5572}
5573
5574Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
5575 QualType T = Importer.Import(OE->getType());
5576 if (T.isNull())
5577 return nullptr;
5578
5579 SmallVector<OffsetOfNode, 4> Nodes;
5580 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
5581 const OffsetOfNode &Node = OE->getComponent(I);
5582
5583 switch (Node.getKind()) {
5584 case OffsetOfNode::Array:
5585 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
5586 Node.getArrayExprIndex(),
5587 Importer.Import(Node.getLocEnd())));
5588 break;
5589
5590 case OffsetOfNode::Base: {
5591 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
5592 if (!BS && Node.getBase())
5593 return nullptr;
5594 Nodes.push_back(OffsetOfNode(BS));
5595 break;
5596 }
5597 case OffsetOfNode::Field: {
5598 FieldDecl *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
5599 if (!FD)
5600 return nullptr;
5601 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
5602 Importer.Import(Node.getLocEnd())));
5603 break;
5604 }
5605 case OffsetOfNode::Identifier: {
5606 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
5607 if (!ToII)
5608 return nullptr;
5609 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
5610 Importer.Import(Node.getLocEnd())));
5611 break;
5612 }
5613 }
5614 }
5615
5616 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
5617 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
5618 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
5619 if (!ToIndexExpr)
5620 return nullptr;
5621 Exprs[I] = ToIndexExpr;
5622 }
5623
5624 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
5625 if (!TInfo && OE->getTypeSourceInfo())
5626 return nullptr;
5627
5628 return OffsetOfExpr::Create(Importer.getToContext(), T,
5629 Importer.Import(OE->getOperatorLoc()),
5630 TInfo, Nodes, Exprs,
5631 Importer.Import(OE->getRParenLoc()));
5632}
5633
5634Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
5635 QualType T = Importer.Import(E->getType());
5636 if (T.isNull())
5637 return nullptr;
5638
5639 Expr *Operand = Importer.Import(E->getOperand());
5640 if (!Operand)
5641 return nullptr;
5642
5643 CanThrowResult CanThrow;
5644 if (E->isValueDependent())
5645 CanThrow = CT_Dependent;
5646 else
5647 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
5648
5649 return new (Importer.getToContext()) CXXNoexceptExpr(
5650 T, Operand, CanThrow,
5651 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
5652}
5653
5654Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
5655 QualType T = Importer.Import(E->getType());
5656 if (T.isNull())
5657 return nullptr;
5658
5659 Expr *SubExpr = Importer.Import(E->getSubExpr());
5660 if (!SubExpr && E->getSubExpr())
5661 return nullptr;
5662
5663 return new (Importer.getToContext()) CXXThrowExpr(
5664 SubExpr, T, Importer.Import(E->getThrowLoc()),
5665 E->isThrownVariableInScope());
5666}
5667
5668Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
5669 ParmVarDecl *Param = cast_or_null<ParmVarDecl>(
5670 Importer.Import(E->getParam()));
5671 if (!Param)
5672 return nullptr;
5673
5674 return CXXDefaultArgExpr::Create(
5675 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
5676}
5677
5678Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5679 QualType T = Importer.Import(E->getType());
5680 if (T.isNull())
5681 return nullptr;
5682
5683 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
5684 if (!TypeInfo)
5685 return nullptr;
5686
5687 return new (Importer.getToContext()) CXXScalarValueInitExpr(
5688 T, TypeInfo, Importer.Import(E->getRParenLoc()));
5689}
5690
5691Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
5692 Expr *SubExpr = Importer.Import(E->getSubExpr());
5693 if (!SubExpr)
5694 return nullptr;
5695
5696 auto *Dtor = cast_or_null<CXXDestructorDecl>(
5697 Importer.Import(const_cast<CXXDestructorDecl *>(
5698 E->getTemporary()->getDestructor())));
5699 if (!Dtor)
5700 return nullptr;
5701
5702 ASTContext &ToCtx = Importer.getToContext();
5703 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
5704 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
5705}
5706
5707Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
5708 QualType T = Importer.Import(CE->getType());
5709 if (T.isNull())
5710 return nullptr;
5711
5712 SmallVector<Expr *, 8> Args(CE->getNumArgs());
5713 if (ImportContainerChecked(CE->arguments(), Args))
5714 return nullptr;
5715
5716 auto *Ctor = cast_or_null<CXXConstructorDecl>(
5717 Importer.Import(CE->getConstructor()));
5718 if (!Ctor)
5719 return nullptr;
5720
5721 return CXXTemporaryObjectExpr::Create(
5722 Importer.getToContext(), T,
5723 Importer.Import(CE->getLocStart()),
5724 Ctor,
5725 CE->isElidable(),
5726 Args,
5727 CE->hadMultipleCandidates(),
5728 CE->isListInitialization(),
5729 CE->isStdInitListInitialization(),
5730 CE->requiresZeroInitialization(),
5731 CE->getConstructionKind(),
5732 Importer.Import(CE->getParenOrBraceRange()));
5733}
5734
5735Expr *
5736ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
5737 QualType T = Importer.Import(E->getType());
5738 if (T.isNull())
5739 return nullptr;
5740
5741 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
5742 if (!TempE)
5743 return nullptr;
5744
5745 ValueDecl *ExtendedBy = cast_or_null<ValueDecl>(
5746 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
5747 if (!ExtendedBy && E->getExtendingDecl())
5748 return nullptr;
5749
5750 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
5751 T, TempE, E->isBoundToLvalueReference());
5752
5753 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
5754 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
5755 return ToMTE;
5756}
5757
Gabor Horvath7a91c082017-11-14 11:30:38 +00005758Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
5759 QualType T = Importer.Import(E->getType());
5760 if (T.isNull())
5761 return nullptr;
5762
5763 Expr *Pattern = Importer.Import(E->getPattern());
5764 if (!Pattern)
5765 return nullptr;
5766
5767 return new (Importer.getToContext()) PackExpansionExpr(
5768 T, Pattern, Importer.Import(E->getEllipsisLoc()),
5769 E->getNumExpansions());
5770}
5771
Aleksei Sidorina693b372016-09-28 10:16:56 +00005772Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
5773 QualType T = Importer.Import(CE->getType());
5774 if (T.isNull())
5775 return nullptr;
5776
5777 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
5778 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
5779 return nullptr;
5780
5781 FunctionDecl *OperatorNewDecl = cast_or_null<FunctionDecl>(
5782 Importer.Import(CE->getOperatorNew()));
5783 if (!OperatorNewDecl && CE->getOperatorNew())
5784 return nullptr;
5785
5786 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5787 Importer.Import(CE->getOperatorDelete()));
5788 if (!OperatorDeleteDecl && CE->getOperatorDelete())
5789 return nullptr;
5790
5791 Expr *ToInit = Importer.Import(CE->getInitializer());
5792 if (!ToInit && CE->getInitializer())
5793 return nullptr;
5794
5795 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
5796 if (!TInfo)
5797 return nullptr;
5798
5799 Expr *ToArrSize = Importer.Import(CE->getArraySize());
5800 if (!ToArrSize && CE->getArraySize())
5801 return nullptr;
5802
5803 return new (Importer.getToContext()) CXXNewExpr(
5804 Importer.getToContext(),
5805 CE->isGlobalNew(),
5806 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00005807 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005808 CE->doesUsualArrayDeleteWantSize(),
5809 PlacementArgs,
5810 Importer.Import(CE->getTypeIdParens()),
5811 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
5812 Importer.Import(CE->getSourceRange()),
5813 Importer.Import(CE->getDirectInitRange()));
5814}
5815
5816Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
5817 QualType T = Importer.Import(E->getType());
5818 if (T.isNull())
5819 return nullptr;
5820
5821 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5822 Importer.Import(E->getOperatorDelete()));
5823 if (!OperatorDeleteDecl && E->getOperatorDelete())
5824 return nullptr;
5825
5826 Expr *ToArg = Importer.Import(E->getArgument());
5827 if (!ToArg && E->getArgument())
5828 return nullptr;
5829
5830 return new (Importer.getToContext()) CXXDeleteExpr(
5831 T, E->isGlobalDelete(),
5832 E->isArrayForm(),
5833 E->isArrayFormAsWritten(),
5834 E->doesUsualArrayDeleteWantSize(),
5835 OperatorDeleteDecl,
5836 ToArg,
5837 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005838}
5839
Sean Callanan59721b32015-04-28 18:41:46 +00005840Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5841 QualType T = Importer.Import(E->getType());
5842 if (T.isNull())
5843 return nullptr;
5844
5845 CXXConstructorDecl *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00005846 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00005847 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00005848 return nullptr;
5849
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005850 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005851 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00005852 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005853
5854 return CXXConstructExpr::Create(Importer.getToContext(), T,
5855 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00005856 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00005857 ToArgs, E->hadMultipleCandidates(),
5858 E->isListInitialization(),
5859 E->isStdInitListInitialization(),
5860 E->requiresZeroInitialization(),
5861 E->getConstructionKind(),
5862 Importer.Import(E->getParenOrBraceRange()));
5863}
5864
Aleksei Sidorina693b372016-09-28 10:16:56 +00005865Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
5866 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
5867 if (!SubExpr && EWC->getSubExpr())
5868 return nullptr;
5869
5870 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
5871 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
5872 if (ExprWithCleanups::CleanupObject Obj =
5873 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
5874 Objs[I] = Obj;
5875 else
5876 return nullptr;
5877
5878 return ExprWithCleanups::Create(Importer.getToContext(),
5879 SubExpr, EWC->cleanupsHaveSideEffects(),
5880 Objs);
5881}
5882
Sean Callanan8bca9962016-03-28 21:43:01 +00005883Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
5884 QualType T = Importer.Import(E->getType());
5885 if (T.isNull())
5886 return nullptr;
5887
5888 Expr *ToFn = Importer.Import(E->getCallee());
5889 if (!ToFn)
5890 return nullptr;
5891
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005892 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005893 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00005894 return nullptr;
5895
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005896 return new (Importer.getToContext()) CXXMemberCallExpr(
5897 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
5898 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00005899}
5900
5901Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
5902 QualType T = Importer.Import(E->getType());
5903 if (T.isNull())
5904 return nullptr;
5905
5906 return new (Importer.getToContext())
5907 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
5908}
5909
5910Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
5911 QualType T = Importer.Import(E->getType());
5912 if (T.isNull())
5913 return nullptr;
5914
5915 return new (Importer.getToContext())
5916 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
5917}
5918
5919
Sean Callanan59721b32015-04-28 18:41:46 +00005920Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5921 QualType T = Importer.Import(E->getType());
5922 if (T.isNull())
5923 return nullptr;
5924
5925 Expr *ToBase = Importer.Import(E->getBase());
5926 if (!ToBase && E->getBase())
5927 return nullptr;
5928
5929 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5930 if (!ToMember && E->getMemberDecl())
5931 return nullptr;
5932
5933 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5934 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5935 E->getFoundDecl().getAccess());
5936
5937 DeclarationNameInfo ToMemberNameInfo(
5938 Importer.Import(E->getMemberNameInfo().getName()),
5939 Importer.Import(E->getMemberNameInfo().getLoc()));
5940
5941 if (E->hasExplicitTemplateArgs()) {
5942 return nullptr; // FIXME: handle template arguments
5943 }
5944
5945 return MemberExpr::Create(Importer.getToContext(), ToBase,
5946 E->isArrow(),
5947 Importer.Import(E->getOperatorLoc()),
5948 Importer.Import(E->getQualifierLoc()),
5949 Importer.Import(E->getTemplateKeywordLoc()),
5950 ToMember, ToFoundDecl, ToMemberNameInfo,
5951 nullptr, T, E->getValueKind(),
5952 E->getObjectKind());
5953}
5954
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00005955Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr(
5956 CXXPseudoDestructorExpr *E) {
5957
5958 Expr *BaseE = Importer.Import(E->getBase());
5959 if (!BaseE)
5960 return nullptr;
5961
5962 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo());
5963 if (!ScopeInfo && E->getScopeTypeInfo())
5964 return nullptr;
5965
5966 PseudoDestructorTypeStorage Storage;
5967 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
5968 IdentifierInfo *ToII = Importer.Import(FromII);
5969 if (!ToII)
5970 return nullptr;
5971 Storage = PseudoDestructorTypeStorage(
5972 ToII, Importer.Import(E->getDestroyedTypeLoc()));
5973 } else {
5974 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo());
5975 if (!TI)
5976 return nullptr;
5977 Storage = PseudoDestructorTypeStorage(TI);
5978 }
5979
5980 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
5981 Importer.getToContext(), BaseE, E->isArrow(),
5982 Importer.Import(E->getOperatorLoc()),
5983 Importer.Import(E->getQualifierLoc()),
5984 ScopeInfo, Importer.Import(E->getColonColonLoc()),
5985 Importer.Import(E->getTildeLoc()), Storage);
5986}
5987
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005988Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
5989 CXXDependentScopeMemberExpr *E) {
5990 Expr *Base = nullptr;
5991 if (!E->isImplicitAccess()) {
5992 Base = Importer.Import(E->getBase());
5993 if (!Base)
5994 return nullptr;
5995 }
5996
5997 QualType BaseType = Importer.Import(E->getBaseType());
5998 if (BaseType.isNull())
5999 return nullptr;
6000
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006001 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006002 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006003 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6004 E->template_arguments(), ToTAInfo))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006005 return nullptr;
6006 ResInfo = &ToTAInfo;
6007 }
6008
6009 DeclarationName Name = Importer.Import(E->getMember());
6010 if (!E->getMember().isEmpty() && Name.isEmpty())
6011 return nullptr;
6012
6013 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc()));
6014 // Import additional name location/type info.
6015 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo);
6016 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope());
6017 if (!ToFQ && E->getFirstQualifierFoundInScope())
6018 return nullptr;
6019
6020 return CXXDependentScopeMemberExpr::Create(
6021 Importer.getToContext(), Base, BaseType, E->isArrow(),
6022 Importer.Import(E->getOperatorLoc()),
6023 Importer.Import(E->getQualifierLoc()),
6024 Importer.Import(E->getTemplateKeywordLoc()),
6025 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo);
6026}
6027
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006028Expr *ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
6029 CXXUnresolvedConstructExpr *CE) {
6030
6031 unsigned NumArgs = CE->arg_size();
6032
6033 llvm::SmallVector<Expr *, 8> ToArgs(NumArgs);
6034 if (ImportArrayChecked(CE->arg_begin(), CE->arg_end(), ToArgs.begin()))
6035 return nullptr;
6036
6037 return CXXUnresolvedConstructExpr::Create(
6038 Importer.getToContext(), Importer.Import(CE->getTypeSourceInfo()),
6039 Importer.Import(CE->getLParenLoc()), llvm::makeArrayRef(ToArgs),
6040 Importer.Import(CE->getRParenLoc()));
6041}
6042
6043Expr *ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
6044 CXXRecordDecl *NamingClass =
6045 cast_or_null<CXXRecordDecl>(Importer.Import(E->getNamingClass()));
6046 if (E->getNamingClass() && !NamingClass)
6047 return nullptr;
6048
6049 DeclarationName Name = Importer.Import(E->getName());
6050 if (E->getName() && !Name)
6051 return nullptr;
6052
6053 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6054 // Import additional name location/type info.
6055 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6056
6057 UnresolvedSet<8> ToDecls;
6058 for (Decl *D : E->decls()) {
6059 if (NamedDecl *To = cast_or_null<NamedDecl>(Importer.Import(D)))
6060 ToDecls.addDecl(To);
6061 else
6062 return nullptr;
6063 }
6064
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006065 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006066 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006067 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6068 E->template_arguments(), ToTAInfo))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006069 return nullptr;
6070 ResInfo = &ToTAInfo;
6071 }
6072
6073 if (ResInfo || E->getTemplateKeywordLoc().isValid())
6074 return UnresolvedLookupExpr::Create(
6075 Importer.getToContext(), NamingClass,
6076 Importer.Import(E->getQualifierLoc()),
6077 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, E->requiresADL(),
6078 ResInfo, ToDecls.begin(), ToDecls.end());
6079
6080 return UnresolvedLookupExpr::Create(
6081 Importer.getToContext(), NamingClass,
6082 Importer.Import(E->getQualifierLoc()), NameInfo, E->requiresADL(),
6083 E->isOverloaded(), ToDecls.begin(), ToDecls.end());
6084}
6085
Sean Callanan59721b32015-04-28 18:41:46 +00006086Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
6087 QualType T = Importer.Import(E->getType());
6088 if (T.isNull())
6089 return nullptr;
6090
6091 Expr *ToCallee = Importer.Import(E->getCallee());
6092 if (!ToCallee && E->getCallee())
6093 return nullptr;
6094
6095 unsigned NumArgs = E->getNumArgs();
6096
6097 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
6098
6099 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
6100 Expr *FromArg = E->getArg(ai);
6101 Expr *ToArg = Importer.Import(FromArg);
6102 if (!ToArg)
6103 return nullptr;
6104 ToArgs[ai] = ToArg;
6105 }
6106
6107 Expr **ToArgs_Copied = new (Importer.getToContext())
6108 Expr*[NumArgs];
6109
6110 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
6111 ToArgs_Copied[ai] = ToArgs[ai];
6112
6113 return new (Importer.getToContext())
6114 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00006115 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00006116 Importer.Import(E->getRParenLoc()));
6117}
6118
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006119Optional<LambdaCapture>
6120ASTNodeImporter::ImportLambdaCapture(const LambdaCapture &From) {
6121 VarDecl *Var = nullptr;
6122 if (From.capturesVariable()) {
6123 Var = cast_or_null<VarDecl>(Importer.Import(From.getCapturedVar()));
6124 if (!Var)
6125 return None;
6126 }
6127
6128 return LambdaCapture(Importer.Import(From.getLocation()), From.isImplicit(),
6129 From.getCaptureKind(), Var,
6130 From.isPackExpansion()
6131 ? Importer.Import(From.getEllipsisLoc())
6132 : SourceLocation());
6133}
6134
6135Expr *ASTNodeImporter::VisitLambdaExpr(LambdaExpr *LE) {
6136 CXXRecordDecl *FromClass = LE->getLambdaClass();
6137 auto *ToClass = dyn_cast_or_null<CXXRecordDecl>(Importer.Import(FromClass));
6138 if (!ToClass)
6139 return nullptr;
6140
6141 // NOTE: lambda classes are created with BeingDefined flag set up.
6142 // It means that ImportDefinition doesn't work for them and we should fill it
6143 // manually.
6144 if (ToClass->isBeingDefined()) {
6145 for (auto FromField : FromClass->fields()) {
6146 auto *ToField = cast_or_null<FieldDecl>(Importer.Import(FromField));
6147 if (!ToField)
6148 return nullptr;
6149 }
6150 }
6151
6152 auto *ToCallOp = dyn_cast_or_null<CXXMethodDecl>(
6153 Importer.Import(LE->getCallOperator()));
6154 if (!ToCallOp)
6155 return nullptr;
6156
6157 ToClass->completeDefinition();
6158
6159 unsigned NumCaptures = LE->capture_size();
6160 SmallVector<LambdaCapture, 8> Captures;
6161 Captures.reserve(NumCaptures);
6162 for (const auto &FromCapture : LE->captures()) {
6163 if (auto ToCapture = ImportLambdaCapture(FromCapture))
6164 Captures.push_back(*ToCapture);
6165 else
6166 return nullptr;
6167 }
6168
6169 SmallVector<Expr *, 8> InitCaptures(NumCaptures);
6170 if (ImportContainerChecked(LE->capture_inits(), InitCaptures))
6171 return nullptr;
6172
6173 return LambdaExpr::Create(Importer.getToContext(), ToClass,
6174 Importer.Import(LE->getIntroducerRange()),
6175 LE->getCaptureDefault(),
6176 Importer.Import(LE->getCaptureDefaultLoc()),
6177 Captures,
6178 LE->hasExplicitParameters(),
6179 LE->hasExplicitResultType(),
6180 InitCaptures,
6181 Importer.Import(LE->getLocEnd()),
6182 LE->containsUnexpandedParameterPack());
6183}
6184
6185
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006186Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
6187 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00006188 if (T.isNull())
6189 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006190
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006191 llvm::SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006192 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006193 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006194
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006195 ASTContext &ToCtx = Importer.getToContext();
6196 InitListExpr *To = new (ToCtx) InitListExpr(
6197 ToCtx, Importer.Import(ILE->getLBraceLoc()),
6198 Exprs, Importer.Import(ILE->getLBraceLoc()));
6199 To->setType(T);
6200
6201 if (ILE->hasArrayFiller()) {
6202 Expr *Filler = Importer.Import(ILE->getArrayFiller());
6203 if (!Filler)
6204 return nullptr;
6205 To->setArrayFiller(Filler);
6206 }
6207
6208 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
6209 FieldDecl *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
6210 if (!ToFD)
6211 return nullptr;
6212 To->setInitializedFieldInUnion(ToFD);
6213 }
6214
6215 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
6216 InitListExpr *ToSyntForm = cast_or_null<InitListExpr>(
6217 Importer.Import(SyntForm));
6218 if (!ToSyntForm)
6219 return nullptr;
6220 To->setSyntacticForm(ToSyntForm);
6221 }
6222
6223 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
6224 To->setValueDependent(ILE->isValueDependent());
6225 To->setInstantiationDependent(ILE->isInstantiationDependent());
6226
6227 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00006228}
6229
Richard Smith30e304e2016-12-14 00:03:17 +00006230Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
6231 QualType ToType = Importer.Import(E->getType());
6232 if (ToType.isNull())
6233 return nullptr;
6234
6235 Expr *ToCommon = Importer.Import(E->getCommonExpr());
6236 if (!ToCommon && E->getCommonExpr())
6237 return nullptr;
6238
6239 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
6240 if (!ToSubExpr && E->getSubExpr())
6241 return nullptr;
6242
6243 return new (Importer.getToContext())
6244 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
6245}
6246
6247Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
6248 QualType ToType = Importer.Import(E->getType());
6249 if (ToType.isNull())
6250 return nullptr;
6251 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
6252}
6253
Sean Callanandd2c1742016-05-16 20:48:03 +00006254Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
6255 FieldDecl *ToField = llvm::dyn_cast_or_null<FieldDecl>(
6256 Importer.Import(DIE->getField()));
6257 if (!ToField && DIE->getField())
6258 return nullptr;
6259
6260 return CXXDefaultInitExpr::Create(
6261 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
6262}
6263
6264Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
6265 QualType ToType = Importer.Import(E->getType());
6266 if (ToType.isNull() && !E->getType().isNull())
6267 return nullptr;
6268 ExprValueKind VK = E->getValueKind();
6269 CastKind CK = E->getCastKind();
6270 Expr *ToOp = Importer.Import(E->getSubExpr());
6271 if (!ToOp && E->getSubExpr())
6272 return nullptr;
6273 CXXCastPath BasePath;
6274 if (ImportCastPath(E, BasePath))
6275 return nullptr;
6276 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6277 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6278 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6279 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
6280
6281 if (isa<CXXStaticCastExpr>(E)) {
6282 return CXXStaticCastExpr::Create(
6283 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6284 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6285 } else if (isa<CXXDynamicCastExpr>(E)) {
6286 return CXXDynamicCastExpr::Create(
6287 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6288 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6289 } else if (isa<CXXReinterpretCastExpr>(E)) {
6290 return CXXReinterpretCastExpr::Create(
6291 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6292 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6293 } else {
6294 return nullptr;
6295 }
6296}
6297
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006298
6299Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
6300 SubstNonTypeTemplateParmExpr *E) {
6301 QualType T = Importer.Import(E->getType());
6302 if (T.isNull())
6303 return nullptr;
6304
6305 NonTypeTemplateParmDecl *Param = cast_or_null<NonTypeTemplateParmDecl>(
6306 Importer.Import(E->getParameter()));
6307 if (!Param)
6308 return nullptr;
6309
6310 Expr *Replacement = Importer.Import(E->getReplacement());
6311 if (!Replacement)
6312 return nullptr;
6313
6314 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
6315 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
6316 Replacement);
6317}
6318
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00006319Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
6320 QualType ToType = Importer.Import(E->getType());
6321 if (ToType.isNull())
6322 return nullptr;
6323
6324 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
6325 if (ImportContainerChecked(E->getArgs(), ToArgs))
6326 return nullptr;
6327
6328 // According to Sema::BuildTypeTrait(), if E is value-dependent,
6329 // Value is always false.
6330 bool ToValue = false;
6331 if (!E->isValueDependent())
6332 ToValue = E->getValue();
6333
6334 return TypeTraitExpr::Create(
6335 Importer.getToContext(), ToType, Importer.Import(E->getLocStart()),
6336 E->getTrait(), ToArgs, Importer.Import(E->getLocEnd()), ToValue);
6337}
6338
Lang Hames19e07e12017-06-20 21:06:00 +00006339void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
6340 CXXMethodDecl *FromMethod) {
6341 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
6342 ToMethod->addOverriddenMethod(
6343 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
6344 FromOverriddenMethod))));
6345}
6346
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006347ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006348 ASTContext &FromContext, FileManager &FromFileManager,
6349 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00006350 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00006351 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006352 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00006353{
Douglas Gregor62d311f2010-02-09 19:21:46 +00006354 ImportedDecls[FromContext.getTranslationUnitDecl()]
6355 = ToContext.getTranslationUnitDecl();
6356}
6357
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00006358ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00006359
6360QualType ASTImporter::Import(QualType FromT) {
6361 if (FromT.isNull())
6362 return QualType();
John McCall424cec92011-01-19 06:33:43 +00006363
6364 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00006365
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006366 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006367 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6368 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006369 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006370 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006371
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006372 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006373 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006374 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006375 if (ToT.isNull())
6376 return ToT;
6377
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006378 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006379 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006380
John McCall424cec92011-01-19 06:33:43 +00006381 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006382}
6383
Douglas Gregor62d311f2010-02-09 19:21:46 +00006384TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006385 if (!FromTSI)
6386 return FromTSI;
6387
6388 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006389 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006390 QualType T = Import(FromTSI->getType());
6391 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006392 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006393
6394 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006395 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006396}
6397
Sean Callanan59721b32015-04-28 18:41:46 +00006398Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6399 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6400 if (Pos != ImportedDecls.end()) {
6401 Decl *ToD = Pos->second;
6402 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6403 return ToD;
6404 } else {
6405 return nullptr;
6406 }
6407}
6408
Douglas Gregor62d311f2010-02-09 19:21:46 +00006409Decl *ASTImporter::Import(Decl *FromD) {
6410 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006411 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006412
Douglas Gregord451ea92011-07-29 23:31:30 +00006413 ASTNodeImporter Importer(*this);
6414
Douglas Gregor62d311f2010-02-09 19:21:46 +00006415 // Check whether we've already imported this declaration.
6416 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006417 if (Pos != ImportedDecls.end()) {
6418 Decl *ToD = Pos->second;
6419 Importer.ImportDefinitionIfNeeded(FromD, ToD);
6420 return ToD;
6421 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00006422
6423 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00006424 Decl *ToD = Importer.Visit(FromD);
6425 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006426 return nullptr;
6427
Douglas Gregor62d311f2010-02-09 19:21:46 +00006428 // Record the imported declaration.
6429 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00006430
6431 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
6432 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00006433 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00006434 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00006435 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006436 // When we've finished transforming a typedef, see whether it was the
6437 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00006438 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00006439 FromTag = AnonTagsWithPendingTypedefs.begin(),
6440 FromTagEnd = AnonTagsWithPendingTypedefs.end();
6441 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00006442 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006443 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
6444 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00006445 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00006446 AnonTagsWithPendingTypedefs.erase(FromTag);
6447 break;
6448 }
6449 }
6450 }
6451 }
6452
Douglas Gregor62d311f2010-02-09 19:21:46 +00006453 return ToD;
6454}
6455
6456DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6457 if (!FromDC)
6458 return FromDC;
6459
Douglas Gregor95d82832012-01-24 18:36:04 +00006460 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006461 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006462 return nullptr;
6463
Douglas Gregor2e15c842012-02-01 21:00:38 +00006464 // When we're using a record/enum/Objective-C class/protocol as a context, we
6465 // need it to have a definition.
6466 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00006467 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006468 if (ToRecord->isCompleteDefinition()) {
6469 // Do nothing.
6470 } else if (FromRecord->isCompleteDefinition()) {
6471 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6472 ASTNodeImporter::IDK_Basic);
6473 } else {
6474 CompleteDecl(ToRecord);
6475 }
6476 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6477 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
6478 if (ToEnum->isCompleteDefinition()) {
6479 // Do nothing.
6480 } else if (FromEnum->isCompleteDefinition()) {
6481 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6482 ASTNodeImporter::IDK_Basic);
6483 } else {
6484 CompleteDecl(ToEnum);
6485 }
6486 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6487 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
6488 if (ToClass->getDefinition()) {
6489 // Do nothing.
6490 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6491 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6492 ASTNodeImporter::IDK_Basic);
6493 } else {
6494 CompleteDecl(ToClass);
6495 }
6496 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6497 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
6498 if (ToProto->getDefinition()) {
6499 // Do nothing.
6500 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6501 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6502 ASTNodeImporter::IDK_Basic);
6503 } else {
6504 CompleteDecl(ToProto);
6505 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006506 }
6507
6508 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006509}
6510
6511Expr *ASTImporter::Import(Expr *FromE) {
6512 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00006513 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006514
6515 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
6516}
6517
6518Stmt *ASTImporter::Import(Stmt *FromS) {
6519 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00006520 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006521
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006522 // Check whether we've already imported this declaration.
6523 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
6524 if (Pos != ImportedStmts.end())
6525 return Pos->second;
6526
6527 // Import the type
6528 ASTNodeImporter Importer(*this);
6529 Stmt *ToS = Importer.Visit(FromS);
6530 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00006531 return nullptr;
6532
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006533 // Record the imported declaration.
6534 ImportedStmts[FromS] = ToS;
6535 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006536}
6537
6538NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
6539 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00006540 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006541
Douglas Gregor90ebf252011-04-27 16:48:40 +00006542 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
6543
6544 switch (FromNNS->getKind()) {
6545 case NestedNameSpecifier::Identifier:
6546 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
6547 return NestedNameSpecifier::Create(ToContext, prefix, II);
6548 }
Craig Topper36250ad2014-05-12 05:36:57 +00006549 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006550
6551 case NestedNameSpecifier::Namespace:
6552 if (NamespaceDecl *NS =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006553 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006554 return NestedNameSpecifier::Create(ToContext, prefix, NS);
6555 }
Craig Topper36250ad2014-05-12 05:36:57 +00006556 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006557
6558 case NestedNameSpecifier::NamespaceAlias:
6559 if (NamespaceAliasDecl *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006560 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006561 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
6562 }
Craig Topper36250ad2014-05-12 05:36:57 +00006563 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006564
6565 case NestedNameSpecifier::Global:
6566 return NestedNameSpecifier::GlobalSpecifier(ToContext);
6567
Nikola Smiljanic67860242014-09-26 00:28:20 +00006568 case NestedNameSpecifier::Super:
6569 if (CXXRecordDecl *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006570 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00006571 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
6572 }
6573 return nullptr;
6574
Douglas Gregor90ebf252011-04-27 16:48:40 +00006575 case NestedNameSpecifier::TypeSpec:
6576 case NestedNameSpecifier::TypeSpecWithTemplate: {
6577 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
6578 if (!T.isNull()) {
6579 bool bTemplate = FromNNS->getKind() ==
6580 NestedNameSpecifier::TypeSpecWithTemplate;
6581 return NestedNameSpecifier::Create(ToContext, prefix,
6582 bTemplate, T.getTypePtr());
6583 }
6584 }
Craig Topper36250ad2014-05-12 05:36:57 +00006585 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006586 }
6587
6588 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00006589}
6590
Douglas Gregor14454802011-02-25 02:25:35 +00006591NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006592 // Copied from NestedNameSpecifier mostly.
6593 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
6594 NestedNameSpecifierLoc NNS = FromNNS;
6595
6596 // Push each of the nested-name-specifiers's onto a stack for
6597 // serialization in reverse order.
6598 while (NNS) {
6599 NestedNames.push_back(NNS);
6600 NNS = NNS.getPrefix();
6601 }
6602
6603 NestedNameSpecifierLocBuilder Builder;
6604
6605 while (!NestedNames.empty()) {
6606 NNS = NestedNames.pop_back_val();
6607 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
6608 if (!Spec)
6609 return NestedNameSpecifierLoc();
6610
6611 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
6612 switch (Kind) {
6613 case NestedNameSpecifier::Identifier:
6614 Builder.Extend(getToContext(),
6615 Spec->getAsIdentifier(),
6616 Import(NNS.getLocalBeginLoc()),
6617 Import(NNS.getLocalEndLoc()));
6618 break;
6619
6620 case NestedNameSpecifier::Namespace:
6621 Builder.Extend(getToContext(),
6622 Spec->getAsNamespace(),
6623 Import(NNS.getLocalBeginLoc()),
6624 Import(NNS.getLocalEndLoc()));
6625 break;
6626
6627 case NestedNameSpecifier::NamespaceAlias:
6628 Builder.Extend(getToContext(),
6629 Spec->getAsNamespaceAlias(),
6630 Import(NNS.getLocalBeginLoc()),
6631 Import(NNS.getLocalEndLoc()));
6632 break;
6633
6634 case NestedNameSpecifier::TypeSpec:
6635 case NestedNameSpecifier::TypeSpecWithTemplate: {
6636 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
6637 QualType(Spec->getAsType(), 0));
6638 Builder.Extend(getToContext(),
6639 Import(NNS.getLocalBeginLoc()),
6640 TSI->getTypeLoc(),
6641 Import(NNS.getLocalEndLoc()));
6642 break;
6643 }
6644
6645 case NestedNameSpecifier::Global:
6646 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
6647 break;
6648
6649 case NestedNameSpecifier::Super: {
6650 SourceRange ToRange = Import(NNS.getSourceRange());
6651 Builder.MakeSuper(getToContext(),
6652 Spec->getAsRecordDecl(),
6653 ToRange.getBegin(),
6654 ToRange.getEnd());
6655 }
6656 }
6657 }
6658
6659 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00006660}
6661
Douglas Gregore2e50d332010-12-01 01:36:18 +00006662TemplateName ASTImporter::Import(TemplateName From) {
6663 switch (From.getKind()) {
6664 case TemplateName::Template:
6665 if (TemplateDecl *ToTemplate
6666 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6667 return TemplateName(ToTemplate);
6668
6669 return TemplateName();
6670
6671 case TemplateName::OverloadedTemplate: {
6672 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
6673 UnresolvedSet<2> ToTemplates;
6674 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
6675 E = FromStorage->end();
6676 I != E; ++I) {
6677 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
6678 ToTemplates.addDecl(To);
6679 else
6680 return TemplateName();
6681 }
6682 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
6683 ToTemplates.end());
6684 }
6685
6686 case TemplateName::QualifiedTemplate: {
6687 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
6688 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
6689 if (!Qualifier)
6690 return TemplateName();
6691
6692 if (TemplateDecl *ToTemplate
6693 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6694 return ToContext.getQualifiedTemplateName(Qualifier,
6695 QTN->hasTemplateKeyword(),
6696 ToTemplate);
6697
6698 return TemplateName();
6699 }
6700
6701 case TemplateName::DependentTemplate: {
6702 DependentTemplateName *DTN = From.getAsDependentTemplateName();
6703 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
6704 if (!Qualifier)
6705 return TemplateName();
6706
6707 if (DTN->isIdentifier()) {
6708 return ToContext.getDependentTemplateName(Qualifier,
6709 Import(DTN->getIdentifier()));
6710 }
6711
6712 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
6713 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006714
6715 case TemplateName::SubstTemplateTemplateParm: {
6716 SubstTemplateTemplateParmStorage *subst
6717 = From.getAsSubstTemplateTemplateParm();
6718 TemplateTemplateParmDecl *param
6719 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
6720 if (!param)
6721 return TemplateName();
6722
6723 TemplateName replacement = Import(subst->getReplacement());
6724 if (replacement.isNull()) return TemplateName();
6725
6726 return ToContext.getSubstTemplateTemplateParm(param, replacement);
6727 }
Douglas Gregor5590be02011-01-15 06:45:20 +00006728
6729 case TemplateName::SubstTemplateTemplateParmPack: {
6730 SubstTemplateTemplateParmPackStorage *SubstPack
6731 = From.getAsSubstTemplateTemplateParmPack();
6732 TemplateTemplateParmDecl *Param
6733 = cast_or_null<TemplateTemplateParmDecl>(
6734 Import(SubstPack->getParameterPack()));
6735 if (!Param)
6736 return TemplateName();
6737
6738 ASTNodeImporter Importer(*this);
6739 TemplateArgument ArgPack
6740 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
6741 if (ArgPack.isNull())
6742 return TemplateName();
6743
6744 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
6745 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00006746 }
6747
6748 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00006749}
6750
Douglas Gregor62d311f2010-02-09 19:21:46 +00006751SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
6752 if (FromLoc.isInvalid())
6753 return SourceLocation();
6754
Douglas Gregor811663e2010-02-10 00:15:17 +00006755 SourceManager &FromSM = FromContext.getSourceManager();
6756
Sean Callanan24c5fe62016-11-07 20:42:25 +00006757 // For now, map everything down to its file location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00006758 // don't have to import macro expansions.
6759 // FIXME: Import macro expansions!
Sean Callanan24c5fe62016-11-07 20:42:25 +00006760 FromLoc = FromSM.getFileLoc(FromLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00006761 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
6762 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00006763 FileID ToFileID = Import(Decomposed.first);
6764 if (ToFileID.isInvalid())
6765 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00006766 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
6767 .getLocWithOffset(Decomposed.second);
6768 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006769}
6770
6771SourceRange ASTImporter::Import(SourceRange FromRange) {
6772 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
6773}
6774
Douglas Gregor811663e2010-02-10 00:15:17 +00006775FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00006776 llvm::DenseMap<FileID, FileID>::iterator Pos
6777 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00006778 if (Pos != ImportedFileIDs.end())
6779 return Pos->second;
6780
6781 SourceManager &FromSM = FromContext.getSourceManager();
6782 SourceManager &ToSM = ToContext.getSourceManager();
6783 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00006784 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00006785
6786 // Include location of this file.
6787 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
6788
6789 // Map the FileID for to the "to" source manager.
6790 FileID ToID;
6791 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00006792 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00006793 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
6794 // disk again
6795 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
6796 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00006797 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00006798 if (!Entry)
6799 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00006800 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
6801 FromSLoc.getFile().getFileCharacteristic());
6802 } else {
6803 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006804 const llvm::MemoryBuffer *
6805 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006806 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00006807 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00006808 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00006809 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006810 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00006811 }
6812
6813
Sebastian Redl99219f12010-09-30 01:03:06 +00006814 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00006815 return ToID;
6816}
6817
Sean Callanandd2c1742016-05-16 20:48:03 +00006818CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
6819 Expr *ToExpr = Import(From->getInit());
6820 if (!ToExpr && From->getInit())
6821 return nullptr;
6822
6823 if (From->isBaseInitializer()) {
6824 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6825 if (!ToTInfo && From->getTypeSourceInfo())
6826 return nullptr;
6827
6828 return new (ToContext) CXXCtorInitializer(
6829 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
6830 ToExpr, Import(From->getRParenLoc()),
6831 From->isPackExpansion() ? Import(From->getEllipsisLoc())
6832 : SourceLocation());
6833 } else if (From->isMemberInitializer()) {
6834 FieldDecl *ToField =
6835 llvm::cast_or_null<FieldDecl>(Import(From->getMember()));
6836 if (!ToField && From->getMember())
6837 return nullptr;
6838
6839 return new (ToContext) CXXCtorInitializer(
6840 ToContext, ToField, Import(From->getMemberLocation()),
6841 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6842 } else if (From->isIndirectMemberInitializer()) {
6843 IndirectFieldDecl *ToIField = llvm::cast_or_null<IndirectFieldDecl>(
6844 Import(From->getIndirectMember()));
6845 if (!ToIField && From->getIndirectMember())
6846 return nullptr;
6847
6848 return new (ToContext) CXXCtorInitializer(
6849 ToContext, ToIField, Import(From->getMemberLocation()),
6850 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6851 } else if (From->isDelegatingInitializer()) {
6852 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6853 if (!ToTInfo && From->getTypeSourceInfo())
6854 return nullptr;
6855
6856 return new (ToContext)
6857 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
6858 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006859 } else {
6860 return nullptr;
6861 }
6862}
6863
6864
Aleksei Sidorina693b372016-09-28 10:16:56 +00006865CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
6866 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
6867 if (Pos != ImportedCXXBaseSpecifiers.end())
6868 return Pos->second;
6869
6870 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
6871 Import(BaseSpec->getSourceRange()),
6872 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
6873 BaseSpec->getAccessSpecifierAsWritten(),
6874 Import(BaseSpec->getTypeSourceInfo()),
6875 Import(BaseSpec->getEllipsisLoc()));
6876 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
6877 return Imported;
6878}
6879
Douglas Gregor0a791672011-01-18 03:11:38 +00006880void ASTImporter::ImportDefinition(Decl *From) {
6881 Decl *To = Import(From);
6882 if (!To)
6883 return;
6884
6885 if (DeclContext *FromDC = cast<DeclContext>(From)) {
6886 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006887
6888 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
6889 if (!ToRecord->getDefinition()) {
6890 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00006891 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006892 return;
6893 }
6894 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006895
6896 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
6897 if (!ToEnum->getDefinition()) {
6898 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006899 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00006900 return;
6901 }
6902 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00006903
6904 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
6905 if (!ToIFace->getDefinition()) {
6906 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006907 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006908 return;
6909 }
6910 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006911
Douglas Gregor2aa53772012-01-24 17:42:07 +00006912 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
6913 if (!ToProto->getDefinition()) {
6914 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006915 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006916 return;
6917 }
6918 }
6919
Douglas Gregor0a791672011-01-18 03:11:38 +00006920 Importer.ImportDeclContext(FromDC, true);
6921 }
6922}
6923
Douglas Gregor96e578d2010-02-05 17:54:41 +00006924DeclarationName ASTImporter::Import(DeclarationName FromName) {
6925 if (!FromName)
6926 return DeclarationName();
6927
6928 switch (FromName.getNameKind()) {
6929 case DeclarationName::Identifier:
6930 return Import(FromName.getAsIdentifierInfo());
6931
6932 case DeclarationName::ObjCZeroArgSelector:
6933 case DeclarationName::ObjCOneArgSelector:
6934 case DeclarationName::ObjCMultiArgSelector:
6935 return Import(FromName.getObjCSelector());
6936
6937 case DeclarationName::CXXConstructorName: {
6938 QualType T = Import(FromName.getCXXNameType());
6939 if (T.isNull())
6940 return DeclarationName();
6941
6942 return ToContext.DeclarationNames.getCXXConstructorName(
6943 ToContext.getCanonicalType(T));
6944 }
6945
6946 case DeclarationName::CXXDestructorName: {
6947 QualType T = Import(FromName.getCXXNameType());
6948 if (T.isNull())
6949 return DeclarationName();
6950
6951 return ToContext.DeclarationNames.getCXXDestructorName(
6952 ToContext.getCanonicalType(T));
6953 }
6954
Richard Smith35845152017-02-07 01:37:30 +00006955 case DeclarationName::CXXDeductionGuideName: {
6956 TemplateDecl *Template = cast_or_null<TemplateDecl>(
6957 Import(FromName.getCXXDeductionGuideTemplate()));
6958 if (!Template)
6959 return DeclarationName();
6960 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
6961 }
6962
Douglas Gregor96e578d2010-02-05 17:54:41 +00006963 case DeclarationName::CXXConversionFunctionName: {
6964 QualType T = Import(FromName.getCXXNameType());
6965 if (T.isNull())
6966 return DeclarationName();
6967
6968 return ToContext.DeclarationNames.getCXXConversionFunctionName(
6969 ToContext.getCanonicalType(T));
6970 }
6971
6972 case DeclarationName::CXXOperatorName:
6973 return ToContext.DeclarationNames.getCXXOperatorName(
6974 FromName.getCXXOverloadedOperator());
6975
6976 case DeclarationName::CXXLiteralOperatorName:
6977 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
6978 Import(FromName.getCXXLiteralIdentifier()));
6979
6980 case DeclarationName::CXXUsingDirective:
6981 // FIXME: STATICS!
6982 return DeclarationName::getUsingDirectiveName();
6983 }
6984
David Blaikiee4d798f2012-01-20 21:50:17 +00006985 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00006986}
6987
Douglas Gregore2e50d332010-12-01 01:36:18 +00006988IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00006989 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00006990 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006991
Sean Callananf94ef1d2016-05-14 06:11:19 +00006992 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
6993
6994 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
6995 ToId->setBuiltinID(FromId->getBuiltinID());
6996
6997 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006998}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006999
Douglas Gregor43f54792010-02-17 02:12:47 +00007000Selector ASTImporter::Import(Selector FromSel) {
7001 if (FromSel.isNull())
7002 return Selector();
7003
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007004 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00007005 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
7006 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
7007 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
7008 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
7009}
7010
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007011DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
7012 DeclContext *DC,
7013 unsigned IDNS,
7014 NamedDecl **Decls,
7015 unsigned NumDecls) {
7016 return Name;
7017}
7018
7019DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007020 if (LastDiagFromFrom)
7021 ToContext.getDiagnostics().notePriorDiagnosticFrom(
7022 FromContext.getDiagnostics());
7023 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007024 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007025}
7026
7027DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007028 if (!LastDiagFromFrom)
7029 FromContext.getDiagnostics().notePriorDiagnosticFrom(
7030 ToContext.getDiagnostics());
7031 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007032 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007033}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007034
Douglas Gregor2e15c842012-02-01 21:00:38 +00007035void ASTImporter::CompleteDecl (Decl *D) {
7036 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
7037 if (!ID->getDefinition())
7038 ID->startDefinition();
7039 }
7040 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
7041 if (!PD->getDefinition())
7042 PD->startDefinition();
7043 }
7044 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
7045 if (!TD->getDefinition() && !TD->isBeingDefined()) {
7046 TD->startDefinition();
7047 TD->setCompleteDefinition(true);
7048 }
7049 }
7050 else {
7051 assert (0 && "CompleteDecl called on a Decl that can't be completed");
7052 }
7053}
7054
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007055Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00007056 if (From->hasAttrs()) {
7057 for (Attr *FromAttr : From->getAttrs())
7058 To->addAttr(FromAttr->clone(To->getASTContext()));
7059 }
7060 if (From->isUsed()) {
7061 To->setIsUsed();
7062 }
Sean Callanandd2c1742016-05-16 20:48:03 +00007063 if (From->isImplicit()) {
7064 To->setImplicit();
7065 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007066 ImportedDecls[From] = To;
7067 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00007068}
Douglas Gregorb4964f72010-02-15 23:54:17 +00007069
Douglas Gregordd6006f2012-07-17 21:16:27 +00007070bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
7071 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00007072 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00007073 = ImportedTypes.find(From.getTypePtr());
7074 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
7075 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00007076
Douglas Gregordd6006f2012-07-17 21:16:27 +00007077 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
7078 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00007079 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00007080}