blob: 805c3871ded07e5b91b0af887eda1397393163f2 [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);
Gabor Horvathc78d99a2018-01-27 16:11:45 +000055 QualType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000056 // 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);
Gabor Horvathc78d99a2018-01-27 16:11:45 +000081 QualType VisitDependentTemplateSpecializationType(
82 const DependentTemplateSpecializationType *T);
John McCall424cec92011-01-19 06:33:43 +000083 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
84 QualType VisitObjCObjectType(const ObjCObjectType *T);
85 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000086
Douglas Gregor95d82832012-01-24 18:36:04 +000087 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000088 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
89 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +000090 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +000091 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000092 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
93 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000094 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000095
Aleksei Sidorina693b372016-09-28 10:16:56 +000096 bool ImportCastPath(CastExpr *E, CXXCastPath &Path);
97
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000098 typedef DesignatedInitExpr::Designator Designator;
99 Designator ImportDesignator(const Designator &D);
100
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000101 Optional<LambdaCapture> ImportLambdaCapture(const LambdaCapture &From);
102
Douglas Gregor2e15c842012-02-01 21:00:38 +0000103
Douglas Gregor95d82832012-01-24 18:36:04 +0000104 /// \brief What we should import from the definition.
105 enum ImportDefinitionKind {
106 /// \brief Import the default subset of the definition, which might be
107 /// nothing (if minimal import is set) or might be everything (if minimal
108 /// import is not set).
109 IDK_Default,
110 /// \brief Import everything.
111 IDK_Everything,
112 /// \brief Import only the bare bones needed to establish a valid
113 /// DeclContext.
114 IDK_Basic
115 };
116
Douglas Gregor2e15c842012-02-01 21:00:38 +0000117 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
118 return IDK == IDK_Everything ||
119 (IDK == IDK_Default && !Importer.isMinimalImport());
120 }
121
Douglas Gregord451ea92011-07-29 23:31:30 +0000122 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000123 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000124 bool ImportDefinition(VarDecl *From, VarDecl *To,
125 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000126 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000127 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000128 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000129 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000130 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000131 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000132 TemplateParameterList *ImportTemplateParameterList(
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000133 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000134 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000135 Optional<TemplateArgumentLoc> ImportTemplateArgumentLoc(
136 const TemplateArgumentLoc &TALoc);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000137 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
138 unsigned NumFromArgs,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000139 SmallVectorImpl<TemplateArgument> &ToArgs);
140
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000141 template <typename InContainerTy>
142 bool ImportTemplateArgumentListInfo(const InContainerTy &Container,
143 TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000144
145 template<typename InContainerTy>
146 bool ImportTemplateArgumentListInfo(SourceLocation FromLAngleLoc,
147 SourceLocation FromRAngleLoc,
148 const InContainerTy &Container,
149 TemplateArgumentListInfo &Result);
150
151 bool ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
152
Douglas Gregordd6006f2012-07-17 21:16:27 +0000153 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
154 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000155 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
156 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000157 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000158 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000159 bool IsStructuralMatch(FunctionTemplateDecl *From,
160 FunctionTemplateDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000161 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000162 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000163 Decl *VisitDecl(Decl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000164 Decl *VisitEmptyDecl(EmptyDecl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000165 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000166 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000167 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000168 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000169 Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000170 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000171 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000172 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000173 Decl *VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000174 Decl *VisitLabelDecl(LabelDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000175 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000176 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000177 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000178 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000179 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
180 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
181 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
182 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000183 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000184 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000185 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000186 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000187 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000188 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000189 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000190 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000191 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000192 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000193 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000194 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000195 Decl *VisitUsingDecl(UsingDecl *D);
196 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
197 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
198 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
199 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
200
Douglas Gregor85f3f952015-07-07 03:57:15 +0000201
202 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000203 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000204 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000205 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000206 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000207 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000208 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
209 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
210 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
211 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000212 Decl *VisitClassTemplateSpecializationDecl(
213 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000214 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
215 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000216 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000217
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000218 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000219 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
220
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000221 Stmt *VisitStmt(Stmt *S);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000222 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000223 Stmt *VisitDeclStmt(DeclStmt *S);
224 Stmt *VisitNullStmt(NullStmt *S);
225 Stmt *VisitCompoundStmt(CompoundStmt *S);
226 Stmt *VisitCaseStmt(CaseStmt *S);
227 Stmt *VisitDefaultStmt(DefaultStmt *S);
228 Stmt *VisitLabelStmt(LabelStmt *S);
229 Stmt *VisitAttributedStmt(AttributedStmt *S);
230 Stmt *VisitIfStmt(IfStmt *S);
231 Stmt *VisitSwitchStmt(SwitchStmt *S);
232 Stmt *VisitWhileStmt(WhileStmt *S);
233 Stmt *VisitDoStmt(DoStmt *S);
234 Stmt *VisitForStmt(ForStmt *S);
235 Stmt *VisitGotoStmt(GotoStmt *S);
236 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
237 Stmt *VisitContinueStmt(ContinueStmt *S);
238 Stmt *VisitBreakStmt(BreakStmt *S);
239 Stmt *VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000240 // FIXME: MSAsmStmt
241 // FIXME: SEHExceptStmt
242 // FIXME: SEHFinallyStmt
243 // FIXME: SEHTryStmt
244 // FIXME: SEHLeaveStmt
245 // FIXME: CapturedStmt
246 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
247 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
248 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
249 // FIXME: MSDependentExistsStmt
250 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
251 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
252 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
253 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
254 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
255 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
256 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000257
258 // Importing expressions
259 Expr *VisitExpr(Expr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000260 Expr *VisitVAArgExpr(VAArgExpr *E);
261 Expr *VisitGNUNullExpr(GNUNullExpr *E);
262 Expr *VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000263 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000264 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
265 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
266 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000267 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000268 Expr *VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000269 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000270 Expr *VisitStringLiteral(StringLiteral *E);
271 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
272 Expr *VisitAtomicExpr(AtomicExpr *E);
273 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000274 Expr *VisitParenExpr(ParenExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000275 Expr *VisitParenListExpr(ParenListExpr *E);
276 Expr *VisitStmtExpr(StmtExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000277 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000278 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000279 Expr *VisitBinaryOperator(BinaryOperator *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000280 Expr *VisitConditionalOperator(ConditionalOperator *E);
281 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
282 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000283 Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
284 Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E);
285 Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000286 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000287 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000288 Expr *VisitExplicitCastExpr(ExplicitCastExpr *E);
289 Expr *VisitOffsetOfExpr(OffsetOfExpr *OE);
290 Expr *VisitCXXThrowExpr(CXXThrowExpr *E);
291 Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
292 Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
293 Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
294 Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
295 Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE);
296 Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000297 Expr *VisitPackExpansionExpr(PackExpansionExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000298 Expr *VisitSizeOfPackExpr(SizeOfPackExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000299 Expr *VisitCXXNewExpr(CXXNewExpr *CE);
300 Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000301 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000302 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000303 Expr *VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +0000304 Expr *VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *CE);
305 Expr *VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000306 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC);
Sean Callanan8bca9962016-03-28 21:43:01 +0000307 Expr *VisitCXXThisExpr(CXXThisExpr *E);
308 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +0000309 Expr *VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000310 Expr *VisitMemberExpr(MemberExpr *E);
311 Expr *VisitCallExpr(CallExpr *E);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000312 Expr *VisitLambdaExpr(LambdaExpr *LE);
Sean Callanan8bca9962016-03-28 21:43:01 +0000313 Expr *VisitInitListExpr(InitListExpr *E);
Richard Smith30e304e2016-12-14 00:03:17 +0000314 Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
315 Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
Sean Callanandd2c1742016-05-16 20:48:03 +0000316 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
317 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000318 Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +0000319 Expr *VisitTypeTraitExpr(TypeTraitExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000320 Expr *VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000321
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000322
323 template<typename IIter, typename OIter>
324 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
325 typedef typename std::remove_reference<decltype(*Obegin)>::type ItemT;
326 ASTImporter &ImporterRef = Importer;
327 std::transform(Ibegin, Iend, Obegin,
328 [&ImporterRef](ItemT From) -> ItemT {
329 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000330 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000331 }
332
333 template<typename IIter, typename OIter>
334 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
335 typedef typename std::remove_reference<decltype(**Obegin)>::type ItemT;
336 ASTImporter &ImporterRef = Importer;
337 bool Failed = false;
338 std::transform(Ibegin, Iend, Obegin,
339 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000340 ItemT *To = cast_or_null<ItemT>(
341 ImporterRef.Import(From));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000342 if (!To && From)
343 Failed = true;
344 return To;
345 });
346 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000347 }
Aleksei Sidorina693b372016-09-28 10:16:56 +0000348
349 template<typename InContainerTy, typename OutContainerTy>
350 bool ImportContainerChecked(const InContainerTy &InContainer,
351 OutContainerTy &OutContainer) {
352 return ImportArrayChecked(InContainer.begin(), InContainer.end(),
353 OutContainer.begin());
354 }
355
356 template<typename InContainerTy, typename OIter>
357 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
358 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
359 }
Lang Hames19e07e12017-06-20 21:06:00 +0000360
361 // Importing overrides.
362 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000363 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000364}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000365
Douglas Gregor3996e242010-02-15 22:01:00 +0000366//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000367// Import Types
368//----------------------------------------------------------------------------
369
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000370using namespace clang;
371
John McCall424cec92011-01-19 06:33:43 +0000372QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000373 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
374 << T->getTypeClassName();
375 return QualType();
376}
377
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000378QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
379 QualType UnderlyingType = Importer.Import(T->getValueType());
380 if(UnderlyingType.isNull())
381 return QualType();
382
383 return Importer.getToContext().getAtomicType(UnderlyingType);
384}
385
John McCall424cec92011-01-19 06:33:43 +0000386QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000387 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000388#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
389 case BuiltinType::Id: \
390 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000391#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000392#define SHARED_SINGLETON_TYPE(Expansion)
393#define BUILTIN_TYPE(Id, SingletonId) \
394 case BuiltinType::Id: return Importer.getToContext().SingletonId;
395#include "clang/AST/BuiltinTypes.def"
396
397 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
398 // context supports C++.
399
400 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
401 // context supports ObjC.
402
Douglas Gregor96e578d2010-02-05 17:54:41 +0000403 case BuiltinType::Char_U:
404 // The context we're importing from has an unsigned 'char'. If we're
405 // importing into a context with a signed 'char', translate to
406 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000407 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000408 return Importer.getToContext().UnsignedCharTy;
409
410 return Importer.getToContext().CharTy;
411
Douglas Gregor96e578d2010-02-05 17:54:41 +0000412 case BuiltinType::Char_S:
413 // The context we're importing from has an unsigned 'char'. If we're
414 // importing into a context with a signed 'char', translate to
415 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000416 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000417 return Importer.getToContext().SignedCharTy;
418
419 return Importer.getToContext().CharTy;
420
Chris Lattnerad3467e2010-12-25 23:25:43 +0000421 case BuiltinType::WChar_S:
422 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000423 // FIXME: If not in C++, shall we translate to the C equivalent of
424 // wchar_t?
425 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000426 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000427
428 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000429}
430
Aleksei Sidorina693b372016-09-28 10:16:56 +0000431QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
432 QualType OrigT = Importer.Import(T->getOriginalType());
433 if (OrigT.isNull())
434 return QualType();
435
436 return Importer.getToContext().getDecayedType(OrigT);
437}
438
John McCall424cec92011-01-19 06:33:43 +0000439QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000440 QualType ToElementType = Importer.Import(T->getElementType());
441 if (ToElementType.isNull())
442 return QualType();
443
444 return Importer.getToContext().getComplexType(ToElementType);
445}
446
John McCall424cec92011-01-19 06:33:43 +0000447QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000448 QualType ToPointeeType = Importer.Import(T->getPointeeType());
449 if (ToPointeeType.isNull())
450 return QualType();
451
452 return Importer.getToContext().getPointerType(ToPointeeType);
453}
454
John McCall424cec92011-01-19 06:33:43 +0000455QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000456 // FIXME: Check for blocks support in "to" context.
457 QualType ToPointeeType = Importer.Import(T->getPointeeType());
458 if (ToPointeeType.isNull())
459 return QualType();
460
461 return Importer.getToContext().getBlockPointerType(ToPointeeType);
462}
463
John McCall424cec92011-01-19 06:33:43 +0000464QualType
465ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000466 // FIXME: Check for C++ support in "to" context.
467 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
468 if (ToPointeeType.isNull())
469 return QualType();
470
471 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
472}
473
John McCall424cec92011-01-19 06:33:43 +0000474QualType
475ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000476 // FIXME: Check for C++0x support in "to" context.
477 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
478 if (ToPointeeType.isNull())
479 return QualType();
480
481 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
482}
483
John McCall424cec92011-01-19 06:33:43 +0000484QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000485 // FIXME: Check for C++ support in "to" context.
486 QualType ToPointeeType = Importer.Import(T->getPointeeType());
487 if (ToPointeeType.isNull())
488 return QualType();
489
490 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
491 return Importer.getToContext().getMemberPointerType(ToPointeeType,
492 ClassType.getTypePtr());
493}
494
John McCall424cec92011-01-19 06:33:43 +0000495QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000496 QualType ToElementType = Importer.Import(T->getElementType());
497 if (ToElementType.isNull())
498 return QualType();
499
500 return Importer.getToContext().getConstantArrayType(ToElementType,
501 T->getSize(),
502 T->getSizeModifier(),
503 T->getIndexTypeCVRQualifiers());
504}
505
John McCall424cec92011-01-19 06:33:43 +0000506QualType
507ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000508 QualType ToElementType = Importer.Import(T->getElementType());
509 if (ToElementType.isNull())
510 return QualType();
511
512 return Importer.getToContext().getIncompleteArrayType(ToElementType,
513 T->getSizeModifier(),
514 T->getIndexTypeCVRQualifiers());
515}
516
John McCall424cec92011-01-19 06:33:43 +0000517QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000518 QualType ToElementType = Importer.Import(T->getElementType());
519 if (ToElementType.isNull())
520 return QualType();
521
522 Expr *Size = Importer.Import(T->getSizeExpr());
523 if (!Size)
524 return QualType();
525
526 SourceRange Brackets = Importer.Import(T->getBracketsRange());
527 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
528 T->getSizeModifier(),
529 T->getIndexTypeCVRQualifiers(),
530 Brackets);
531}
532
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000533QualType ASTNodeImporter::VisitDependentSizedArrayType(
534 const DependentSizedArrayType *T) {
535 QualType ToElementType = Importer.Import(T->getElementType());
536 if (ToElementType.isNull())
537 return QualType();
538
539 // SizeExpr may be null if size is not specified directly.
540 // For example, 'int a[]'.
541 Expr *Size = Importer.Import(T->getSizeExpr());
542 if (!Size && T->getSizeExpr())
543 return QualType();
544
545 SourceRange Brackets = Importer.Import(T->getBracketsRange());
546 return Importer.getToContext().getDependentSizedArrayType(
547 ToElementType, Size, T->getSizeModifier(), T->getIndexTypeCVRQualifiers(),
548 Brackets);
549}
550
John McCall424cec92011-01-19 06:33:43 +0000551QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000552 QualType ToElementType = Importer.Import(T->getElementType());
553 if (ToElementType.isNull())
554 return QualType();
555
556 return Importer.getToContext().getVectorType(ToElementType,
557 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +0000558 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000559}
560
John McCall424cec92011-01-19 06:33:43 +0000561QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000562 QualType ToElementType = Importer.Import(T->getElementType());
563 if (ToElementType.isNull())
564 return QualType();
565
566 return Importer.getToContext().getExtVectorType(ToElementType,
567 T->getNumElements());
568}
569
John McCall424cec92011-01-19 06:33:43 +0000570QualType
571ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000572 // FIXME: What happens if we're importing a function without a prototype
573 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +0000574 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000575 if (ToResultType.isNull())
576 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000577
Douglas Gregor96e578d2010-02-05 17:54:41 +0000578 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000579 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000580}
581
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +0000582QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000583 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000584 if (ToResultType.isNull())
585 return QualType();
586
587 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000588 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000589 for (const auto &A : T->param_types()) {
590 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000591 if (ArgType.isNull())
592 return QualType();
593 ArgTypes.push_back(ArgType);
594 }
595
596 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000597 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000598 for (const auto &E : T->exceptions()) {
599 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000600 if (ExceptionType.isNull())
601 return QualType();
602 ExceptionTypes.push_back(ExceptionType);
603 }
John McCalldb40c7f2010-12-14 08:05:40 +0000604
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000605 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
606 FunctionProtoType::ExtProtoInfo ToEPI;
607
608 ToEPI.ExtInfo = FromEPI.ExtInfo;
609 ToEPI.Variadic = FromEPI.Variadic;
610 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
611 ToEPI.TypeQuals = FromEPI.TypeQuals;
612 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +0000613 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
614 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
615 ToEPI.ExceptionSpec.NoexceptExpr =
616 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
617 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
618 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
619 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
620 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000621
Jordan Rose5c382722013-03-08 21:51:21 +0000622 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000623}
624
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000625QualType ASTNodeImporter::VisitUnresolvedUsingType(
626 const UnresolvedUsingType *T) {
627 UnresolvedUsingTypenameDecl *ToD = cast_or_null<UnresolvedUsingTypenameDecl>(
628 Importer.Import(T->getDecl()));
629 if (!ToD)
630 return QualType();
631
632 UnresolvedUsingTypenameDecl *ToPrevD =
633 cast_or_null<UnresolvedUsingTypenameDecl>(
634 Importer.Import(T->getDecl()->getPreviousDecl()));
635 if (!ToPrevD && T->getDecl()->getPreviousDecl())
636 return QualType();
637
638 return Importer.getToContext().getTypeDeclType(ToD, ToPrevD);
639}
640
Sean Callananda6df8a2011-08-11 16:56:07 +0000641QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
642 QualType ToInnerType = Importer.Import(T->getInnerType());
643 if (ToInnerType.isNull())
644 return QualType();
645
646 return Importer.getToContext().getParenType(ToInnerType);
647}
648
John McCall424cec92011-01-19 06:33:43 +0000649QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +0000650 TypedefNameDecl *ToDecl
651 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000652 if (!ToDecl)
653 return QualType();
654
655 return Importer.getToContext().getTypeDeclType(ToDecl);
656}
657
John McCall424cec92011-01-19 06:33:43 +0000658QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000659 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
660 if (!ToExpr)
661 return QualType();
662
663 return Importer.getToContext().getTypeOfExprType(ToExpr);
664}
665
John McCall424cec92011-01-19 06:33:43 +0000666QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000667 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
668 if (ToUnderlyingType.isNull())
669 return QualType();
670
671 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
672}
673
John McCall424cec92011-01-19 06:33:43 +0000674QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +0000675 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +0000676 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
677 if (!ToExpr)
678 return QualType();
679
Douglas Gregor81495f32012-02-12 18:42:33 +0000680 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
681 if (UnderlyingType.isNull())
682 return QualType();
683
684 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000685}
686
Alexis Hunte852b102011-05-24 22:41:36 +0000687QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
688 QualType ToBaseType = Importer.Import(T->getBaseType());
689 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
690 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
691 return QualType();
692
693 return Importer.getToContext().getUnaryTransformType(ToBaseType,
694 ToUnderlyingType,
695 T->getUTTKind());
696}
697
Richard Smith30482bc2011-02-20 03:19:35 +0000698QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +0000699 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +0000700 QualType FromDeduced = T->getDeducedType();
701 QualType ToDeduced;
702 if (!FromDeduced.isNull()) {
703 ToDeduced = Importer.Import(FromDeduced);
704 if (ToDeduced.isNull())
705 return QualType();
706 }
707
Richard Smithe301ba22015-11-11 02:02:15 +0000708 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +0000709 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +0000710}
711
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000712QualType ASTNodeImporter::VisitInjectedClassNameType(
713 const InjectedClassNameType *T) {
714 CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
715 if (!D)
716 return QualType();
717
718 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
719 if (InjType.isNull())
720 return QualType();
721
722 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
723 // See comments in InjectedClassNameType definition for details
724 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
725 enum {
726 TypeAlignmentInBits = 4,
727 TypeAlignment = 1 << TypeAlignmentInBits
728 };
729
730 return QualType(new (Importer.getToContext(), TypeAlignment)
731 InjectedClassNameType(D, InjType), 0);
732}
733
John McCall424cec92011-01-19 06:33:43 +0000734QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000735 RecordDecl *ToDecl
736 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
737 if (!ToDecl)
738 return QualType();
739
740 return Importer.getToContext().getTagDeclType(ToDecl);
741}
742
John McCall424cec92011-01-19 06:33:43 +0000743QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000744 EnumDecl *ToDecl
745 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
746 if (!ToDecl)
747 return QualType();
748
749 return Importer.getToContext().getTagDeclType(ToDecl);
750}
751
Sean Callanan72fe0852015-04-02 23:50:08 +0000752QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
753 QualType FromModifiedType = T->getModifiedType();
754 QualType FromEquivalentType = T->getEquivalentType();
755 QualType ToModifiedType;
756 QualType ToEquivalentType;
757
758 if (!FromModifiedType.isNull()) {
759 ToModifiedType = Importer.Import(FromModifiedType);
760 if (ToModifiedType.isNull())
761 return QualType();
762 }
763 if (!FromEquivalentType.isNull()) {
764 ToEquivalentType = Importer.Import(FromEquivalentType);
765 if (ToEquivalentType.isNull())
766 return QualType();
767 }
768
769 return Importer.getToContext().getAttributedType(T->getAttrKind(),
770 ToModifiedType, ToEquivalentType);
771}
772
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000773
774QualType ASTNodeImporter::VisitTemplateTypeParmType(
775 const TemplateTypeParmType *T) {
776 TemplateTypeParmDecl *ParmDecl =
777 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
778 if (!ParmDecl && T->getDecl())
779 return QualType();
780
781 return Importer.getToContext().getTemplateTypeParmType(
782 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
783}
784
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000785QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
786 const SubstTemplateTypeParmType *T) {
787 const TemplateTypeParmType *Replaced =
788 cast_or_null<TemplateTypeParmType>(Importer.Import(
789 QualType(T->getReplacedParameter(), 0)).getTypePtr());
790 if (!Replaced)
791 return QualType();
792
793 QualType Replacement = Importer.Import(T->getReplacementType());
794 if (Replacement.isNull())
795 return QualType();
796 Replacement = Replacement.getCanonicalType();
797
798 return Importer.getToContext().getSubstTemplateTypeParmType(
799 Replaced, Replacement);
800}
801
Douglas Gregore2e50d332010-12-01 01:36:18 +0000802QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +0000803 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000804 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
805 if (ToTemplate.isNull())
806 return QualType();
807
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000808 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +0000809 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
810 return QualType();
811
812 QualType ToCanonType;
813 if (!QualType(T, 0).isCanonical()) {
814 QualType FromCanonType
815 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
816 ToCanonType =Importer.Import(FromCanonType);
817 if (ToCanonType.isNull())
818 return QualType();
819 }
820 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +0000821 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +0000822 ToCanonType);
823}
824
John McCall424cec92011-01-19 06:33:43 +0000825QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +0000826 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000827 // Note: the qualifier in an ElaboratedType is optional.
828 if (T->getQualifier()) {
829 ToQualifier = Importer.Import(T->getQualifier());
830 if (!ToQualifier)
831 return QualType();
832 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000833
834 QualType ToNamedType = Importer.Import(T->getNamedType());
835 if (ToNamedType.isNull())
836 return QualType();
837
Abramo Bagnara6150c882010-05-11 21:36:43 +0000838 return Importer.getToContext().getElaboratedType(T->getKeyword(),
839 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000840}
841
Gabor Horvath7a91c082017-11-14 11:30:38 +0000842QualType ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
843 QualType Pattern = Importer.Import(T->getPattern());
844 if (Pattern.isNull())
845 return QualType();
846
847 return Importer.getToContext().getPackExpansionType(Pattern,
848 T->getNumExpansions());
849}
850
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000851QualType ASTNodeImporter::VisitDependentTemplateSpecializationType(
852 const DependentTemplateSpecializationType *T) {
853 NestedNameSpecifier *Qualifier = Importer.Import(T->getQualifier());
854 if (!Qualifier && T->getQualifier())
855 return QualType();
856
857 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
858 if (!Name && T->getIdentifier())
859 return QualType();
860
861 SmallVector<TemplateArgument, 2> ToPack;
862 ToPack.reserve(T->getNumArgs());
863 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToPack))
864 return QualType();
865
866 return Importer.getToContext().getDependentTemplateSpecializationType(
867 T->getKeyword(), Qualifier, Name, ToPack);
868}
869
John McCall424cec92011-01-19 06:33:43 +0000870QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000871 ObjCInterfaceDecl *Class
872 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
873 if (!Class)
874 return QualType();
875
John McCall8b07ec22010-05-15 11:32:37 +0000876 return Importer.getToContext().getObjCInterfaceType(Class);
877}
878
John McCall424cec92011-01-19 06:33:43 +0000879QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000880 QualType ToBaseType = Importer.Import(T->getBaseType());
881 if (ToBaseType.isNull())
882 return QualType();
883
Douglas Gregore9d95f12015-07-07 03:57:35 +0000884 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +0000885 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +0000886 QualType ImportedTypeArg = Importer.Import(TypeArg);
887 if (ImportedTypeArg.isNull())
888 return QualType();
889
890 TypeArgs.push_back(ImportedTypeArg);
891 }
892
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000893 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000894 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000895 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000896 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000897 if (!Protocol)
898 return QualType();
899 Protocols.push_back(Protocol);
900 }
901
Douglas Gregore9d95f12015-07-07 03:57:35 +0000902 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +0000903 Protocols,
904 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000905}
906
John McCall424cec92011-01-19 06:33:43 +0000907QualType
908ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000909 QualType ToPointeeType = Importer.Import(T->getPointeeType());
910 if (ToPointeeType.isNull())
911 return QualType();
912
John McCall8b07ec22010-05-15 11:32:37 +0000913 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000914}
915
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000916//----------------------------------------------------------------------------
917// Import Declarations
918//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000919bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
920 DeclContext *&LexicalDC,
921 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +0000922 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000923 SourceLocation &Loc) {
924 // Import the context of this declaration.
925 DC = Importer.ImportContext(D->getDeclContext());
926 if (!DC)
927 return true;
928
929 LexicalDC = DC;
930 if (D->getDeclContext() != D->getLexicalDeclContext()) {
931 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
932 if (!LexicalDC)
933 return true;
934 }
935
936 // Import the name of this declaration.
937 Name = Importer.Import(D->getDeclName());
938 if (D->getDeclName() && !Name)
939 return true;
940
941 // Import the location of this declaration.
942 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +0000943 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000944 return false;
945}
946
Douglas Gregord451ea92011-07-29 23:31:30 +0000947void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
948 if (!FromD)
949 return;
950
951 if (!ToD) {
952 ToD = Importer.Import(FromD);
953 if (!ToD)
954 return;
955 }
956
957 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
958 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +0000959 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +0000960 ImportDefinition(FromRecord, ToRecord);
961 }
962 }
963 return;
964 }
965
966 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
967 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
968 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
969 ImportDefinition(FromEnum, ToEnum);
970 }
971 }
972 return;
973 }
974}
975
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000976void
977ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
978 DeclarationNameInfo& To) {
979 // NOTE: To.Name and To.Loc are already imported.
980 // We only have to import To.LocInfo.
981 switch (To.getName().getNameKind()) {
982 case DeclarationName::Identifier:
983 case DeclarationName::ObjCZeroArgSelector:
984 case DeclarationName::ObjCOneArgSelector:
985 case DeclarationName::ObjCMultiArgSelector:
986 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +0000987 case DeclarationName::CXXDeductionGuideName:
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000988 return;
989
990 case DeclarationName::CXXOperatorName: {
991 SourceRange Range = From.getCXXOperatorNameRange();
992 To.setCXXOperatorNameRange(Importer.Import(Range));
993 return;
994 }
995 case DeclarationName::CXXLiteralOperatorName: {
996 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
997 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
998 return;
999 }
1000 case DeclarationName::CXXConstructorName:
1001 case DeclarationName::CXXDestructorName:
1002 case DeclarationName::CXXConversionFunctionName: {
1003 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1004 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1005 return;
1006 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001007 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001008 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001009}
1010
Douglas Gregor2e15c842012-02-01 21:00:38 +00001011void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001012 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001013 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001014 return;
1015 }
1016
Aaron Ballman629afae2014-03-07 19:56:05 +00001017 for (auto *From : FromDC->decls())
1018 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00001019}
1020
Douglas Gregord451ea92011-07-29 23:31:30 +00001021bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001022 ImportDefinitionKind Kind) {
1023 if (To->getDefinition() || To->isBeingDefined()) {
1024 if (Kind == IDK_Everything)
1025 ImportDeclContext(From, /*ForceImport=*/true);
1026
Douglas Gregore2e50d332010-12-01 01:36:18 +00001027 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001028 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001029
1030 To->startDefinition();
1031
1032 // Add base classes.
1033 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1034 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001035
1036 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1037 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1038 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001039 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001040 ToData.Aggregate = FromData.Aggregate;
1041 ToData.PlainOldData = FromData.PlainOldData;
1042 ToData.Empty = FromData.Empty;
1043 ToData.Polymorphic = FromData.Polymorphic;
1044 ToData.Abstract = FromData.Abstract;
1045 ToData.IsStandardLayout = FromData.IsStandardLayout;
1046 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1047 ToData.HasPrivateFields = FromData.HasPrivateFields;
1048 ToData.HasProtectedFields = FromData.HasProtectedFields;
1049 ToData.HasPublicFields = FromData.HasPublicFields;
1050 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001051 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001052 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001053 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001054 ToData.HasUninitializedReferenceMember
1055 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001056 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001057 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1058 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001059 ToData.NeedOverloadResolutionForCopyConstructor
1060 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001061 ToData.NeedOverloadResolutionForMoveConstructor
1062 = FromData.NeedOverloadResolutionForMoveConstructor;
1063 ToData.NeedOverloadResolutionForMoveAssignment
1064 = FromData.NeedOverloadResolutionForMoveAssignment;
1065 ToData.NeedOverloadResolutionForDestructor
1066 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001067 ToData.DefaultedCopyConstructorIsDeleted
1068 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001069 ToData.DefaultedMoveConstructorIsDeleted
1070 = FromData.DefaultedMoveConstructorIsDeleted;
1071 ToData.DefaultedMoveAssignmentIsDeleted
1072 = FromData.DefaultedMoveAssignmentIsDeleted;
1073 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001074 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1075 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001076 ToData.HasConstexprNonCopyMoveConstructor
1077 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001078 ToData.HasDefaultedDefaultConstructor
1079 = FromData.HasDefaultedDefaultConstructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001080 ToData.CanPassInRegisters = FromData.CanPassInRegisters;
Richard Smith561fb152012-02-25 07:33:38 +00001081 ToData.DefaultedDefaultConstructorIsConstexpr
1082 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001083 ToData.HasConstexprDefaultConstructor
1084 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001085 ToData.HasNonLiteralTypeFieldsOrBases
1086 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001087 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001088 ToData.UserProvidedDefaultConstructor
1089 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001090 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001091 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1092 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1093 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1094 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001095 ToData.ImplicitCopyAssignmentHasConstParam
1096 = FromData.ImplicitCopyAssignmentHasConstParam;
1097 ToData.HasDeclaredCopyConstructorWithConstParam
1098 = FromData.HasDeclaredCopyConstructorWithConstParam;
1099 ToData.HasDeclaredCopyAssignmentWithConstParam
1100 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001101
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001102 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001103 for (const auto &Base1 : FromCXX->bases()) {
1104 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001105 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001106 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001107
1108 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001109 if (Base1.isPackExpansion())
1110 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001111
1112 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00001113 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00001114
Douglas Gregore2e50d332010-12-01 01:36:18 +00001115 Bases.push_back(
1116 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00001117 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1118 Base1.isVirtual(),
1119 Base1.isBaseOfClass(),
1120 Base1.getAccessSpecifierAsWritten(),
1121 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00001122 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001123 }
1124 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001125 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001126 }
1127
Douglas Gregor2e15c842012-02-01 21:00:38 +00001128 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001129 ImportDeclContext(From, /*ForceImport=*/true);
1130
Douglas Gregore2e50d332010-12-01 01:36:18 +00001131 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001132 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001133}
1134
Larisse Voufo39a1e502013-08-06 01:03:05 +00001135bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1136 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00001137 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00001138 return false;
1139
1140 // FIXME: Can we really import any initializer? Alternatively, we could force
1141 // ourselves to import every declaration of a variable and then only use
1142 // getInit() here.
1143 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1144
1145 // FIXME: Other bits to merge?
1146
1147 return false;
1148}
1149
Douglas Gregord451ea92011-07-29 23:31:30 +00001150bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001151 ImportDefinitionKind Kind) {
1152 if (To->getDefinition() || To->isBeingDefined()) {
1153 if (Kind == IDK_Everything)
1154 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001155 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001156 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001157
1158 To->startDefinition();
1159
1160 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1161 if (T.isNull())
1162 return true;
1163
1164 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1165 if (ToPromotionType.isNull())
1166 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001167
1168 if (shouldForceImportDeclContext(Kind))
1169 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001170
1171 // FIXME: we might need to merge the number of positive or negative bits
1172 // if the enumerator lists don't match.
1173 To->completeDefinition(T, ToPromotionType,
1174 From->getNumPositiveBits(),
1175 From->getNumNegativeBits());
1176 return false;
1177}
1178
Douglas Gregora082a492010-11-30 19:14:50 +00001179TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1180 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001181 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1182 if (ImportContainerChecked(*Params, ToParams))
1183 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00001184
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001185 Expr *ToRequiresClause;
1186 if (Expr *const R = Params->getRequiresClause()) {
1187 ToRequiresClause = Importer.Import(R);
1188 if (!ToRequiresClause)
1189 return nullptr;
1190 } else {
1191 ToRequiresClause = nullptr;
1192 }
1193
Douglas Gregora082a492010-11-30 19:14:50 +00001194 return TemplateParameterList::Create(Importer.getToContext(),
1195 Importer.Import(Params->getTemplateLoc()),
1196 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00001197 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001198 Importer.Import(Params->getRAngleLoc()),
1199 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001200}
1201
Douglas Gregore2e50d332010-12-01 01:36:18 +00001202TemplateArgument
1203ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1204 switch (From.getKind()) {
1205 case TemplateArgument::Null:
1206 return TemplateArgument();
1207
1208 case TemplateArgument::Type: {
1209 QualType ToType = Importer.Import(From.getAsType());
1210 if (ToType.isNull())
1211 return TemplateArgument();
1212 return TemplateArgument(ToType);
1213 }
1214
1215 case TemplateArgument::Integral: {
1216 QualType ToType = Importer.Import(From.getIntegralType());
1217 if (ToType.isNull())
1218 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001219 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001220 }
1221
Eli Friedmanb826a002012-09-26 02:36:12 +00001222 case TemplateArgument::Declaration: {
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001223 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
1224 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1225 if (!To || ToType.isNull())
1226 return TemplateArgument();
1227 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00001228 }
1229
1230 case TemplateArgument::NullPtr: {
1231 QualType ToType = Importer.Import(From.getNullPtrType());
1232 if (ToType.isNull())
1233 return TemplateArgument();
1234 return TemplateArgument(ToType, /*isNullPtr*/true);
1235 }
1236
Douglas Gregore2e50d332010-12-01 01:36:18 +00001237 case TemplateArgument::Template: {
1238 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1239 if (ToTemplate.isNull())
1240 return TemplateArgument();
1241
1242 return TemplateArgument(ToTemplate);
1243 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001244
1245 case TemplateArgument::TemplateExpansion: {
1246 TemplateName ToTemplate
1247 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1248 if (ToTemplate.isNull())
1249 return TemplateArgument();
1250
Douglas Gregore1d60df2011-01-14 23:41:42 +00001251 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001252 }
1253
Douglas Gregore2e50d332010-12-01 01:36:18 +00001254 case TemplateArgument::Expression:
1255 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1256 return TemplateArgument(ToExpr);
1257 return TemplateArgument();
1258
1259 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001260 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001261 ToPack.reserve(From.pack_size());
1262 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1263 return TemplateArgument();
Benjamin Kramercce63472015-08-05 09:40:22 +00001264
1265 return TemplateArgument(
1266 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001267 }
1268 }
1269
1270 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00001271}
1272
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001273Optional<TemplateArgumentLoc>
1274ASTNodeImporter::ImportTemplateArgumentLoc(const TemplateArgumentLoc &TALoc) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001275 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1276 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1277 TemplateArgumentLocInfo ToInfo;
1278 if (Arg.getKind() == TemplateArgument::Expression) {
1279 Expr *E = Importer.Import(FromInfo.getAsExpr());
1280 ToInfo = TemplateArgumentLocInfo(E);
1281 if (!E)
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001282 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001283 } else if (Arg.getKind() == TemplateArgument::Type) {
1284 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1285 ToInfo = TemplateArgumentLocInfo(TSI);
1286 else
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001287 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001288 } else {
1289 ToInfo = TemplateArgumentLocInfo(
1290 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1291 Importer.Import(FromInfo.getTemplateNameLoc()),
1292 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1293 }
1294 return TemplateArgumentLoc(Arg, ToInfo);
1295}
1296
Douglas Gregore2e50d332010-12-01 01:36:18 +00001297bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1298 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001299 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001300 for (unsigned I = 0; I != NumFromArgs; ++I) {
1301 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1302 if (To.isNull() && !FromArgs[I].isNull())
1303 return true;
1304
1305 ToArgs.push_back(To);
1306 }
1307
1308 return false;
1309}
1310
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001311// We cannot use Optional<> pattern here and below because
1312// TemplateArgumentListInfo's operator new is declared as deleted so it cannot
1313// be stored in Optional.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001314template <typename InContainerTy>
1315bool ASTNodeImporter::ImportTemplateArgumentListInfo(
1316 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1317 for (const auto &FromLoc : Container) {
1318 if (auto ToLoc = ImportTemplateArgumentLoc(FromLoc))
1319 ToTAInfo.addArgument(*ToLoc);
1320 else
1321 return true;
1322 }
1323 return false;
1324}
1325
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001326template <typename InContainerTy>
1327bool ASTNodeImporter::ImportTemplateArgumentListInfo(
1328 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
1329 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
1330 TemplateArgumentListInfo ToTAInfo(Importer.Import(FromLAngleLoc),
1331 Importer.Import(FromRAngleLoc));
1332 if (ImportTemplateArgumentListInfo(Container, ToTAInfo))
1333 return true;
1334 Result = ToTAInfo;
1335 return false;
1336}
1337
Douglas Gregor5c73e912010-02-11 00:48:18 +00001338bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001339 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001340 // Eliminate a potential failure point where we attempt to re-import
1341 // something we're trying to import while completing ToRecord.
1342 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1343 if (ToOrigin) {
1344 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
1345 if (ToOriginRecord)
1346 ToRecord = ToOriginRecord;
1347 }
1348
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001349 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001350 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001351 Importer.getNonEquivalentDecls(),
1352 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001353 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001354}
1355
Larisse Voufo39a1e502013-08-06 01:03:05 +00001356bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1357 bool Complain) {
1358 StructuralEquivalenceContext Ctx(
1359 Importer.getFromContext(), Importer.getToContext(),
1360 Importer.getNonEquivalentDecls(), false, Complain);
1361 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
1362}
1363
Douglas Gregor98c10182010-02-12 22:17:39 +00001364bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001365 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001366 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001367 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001368 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001369}
1370
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001371bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1372 FunctionTemplateDecl *To) {
1373 StructuralEquivalenceContext Ctx(
1374 Importer.getFromContext(), Importer.getToContext(),
1375 Importer.getNonEquivalentDecls(), false, false);
1376 return Ctx.IsStructurallyEquivalent(From, To);
1377}
1378
Douglas Gregor91155082012-11-14 22:29:20 +00001379bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
1380 EnumConstantDecl *ToEC)
1381{
1382 const llvm::APSInt &FromVal = FromEC->getInitVal();
1383 const llvm::APSInt &ToVal = ToEC->getInitVal();
1384
1385 return FromVal.isSigned() == ToVal.isSigned() &&
1386 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1387 FromVal == ToVal;
1388}
1389
1390bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001391 ClassTemplateDecl *To) {
1392 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1393 Importer.getToContext(),
1394 Importer.getNonEquivalentDecls());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001395 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00001396}
1397
Larisse Voufo39a1e502013-08-06 01:03:05 +00001398bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1399 VarTemplateDecl *To) {
1400 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1401 Importer.getToContext(),
1402 Importer.getNonEquivalentDecls());
1403 return Ctx.IsStructurallyEquivalent(From, To);
1404}
1405
Douglas Gregore4c83e42010-02-09 22:48:33 +00001406Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001407 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001408 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00001409 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00001410}
1411
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001412Decl *ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
1413 // Import the context of this declaration.
1414 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1415 if (!DC)
1416 return nullptr;
1417
1418 DeclContext *LexicalDC = DC;
1419 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1420 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1421 if (!LexicalDC)
1422 return nullptr;
1423 }
1424
1425 // Import the location of this declaration.
1426 SourceLocation Loc = Importer.Import(D->getLocation());
1427
1428 EmptyDecl *ToD = EmptyDecl::Create(Importer.getToContext(), DC, Loc);
1429 ToD->setLexicalDeclContext(LexicalDC);
1430 Importer.Imported(D, ToD);
1431 LexicalDC->addDeclInternal(ToD);
1432 return ToD;
1433}
1434
Sean Callanan65198272011-11-17 23:20:56 +00001435Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1436 TranslationUnitDecl *ToD =
1437 Importer.getToContext().getTranslationUnitDecl();
1438
1439 Importer.Imported(D, ToD);
1440
1441 return ToD;
1442}
1443
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001444Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
1445
1446 SourceLocation Loc = Importer.Import(D->getLocation());
1447 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1448
1449 // Import the context of this declaration.
1450 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1451 if (!DC)
1452 return nullptr;
1453
1454 AccessSpecDecl *accessSpecDecl
1455 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
1456 DC, Loc, ColonLoc);
1457
1458 if (!accessSpecDecl)
1459 return nullptr;
1460
1461 // Lexical DeclContext and Semantic DeclContext
1462 // is always the same for the accessSpec.
1463 accessSpecDecl->setLexicalDeclContext(DC);
1464 DC->addDeclInternal(accessSpecDecl);
1465
1466 return accessSpecDecl;
1467}
1468
Aleksei Sidorina693b372016-09-28 10:16:56 +00001469Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1470 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1471 if (!DC)
1472 return nullptr;
1473
1474 DeclContext *LexicalDC = DC;
1475
1476 // Import the location of this declaration.
1477 SourceLocation Loc = Importer.Import(D->getLocation());
1478
1479 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1480 if (!AssertExpr)
1481 return nullptr;
1482
1483 StringLiteral *FromMsg = D->getMessage();
1484 StringLiteral *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
1485 if (!ToMsg && FromMsg)
1486 return nullptr;
1487
1488 StaticAssertDecl *ToD = StaticAssertDecl::Create(
1489 Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1490 Importer.Import(D->getRParenLoc()), D->isFailed());
1491
1492 ToD->setLexicalDeclContext(LexicalDC);
1493 LexicalDC->addDeclInternal(ToD);
1494 Importer.Imported(D, ToD);
1495 return ToD;
1496}
1497
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001498Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1499 // Import the major distinguishing characteristics of this namespace.
1500 DeclContext *DC, *LexicalDC;
1501 DeclarationName Name;
1502 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001503 NamedDecl *ToD;
1504 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001505 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001506 if (ToD)
1507 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001508
1509 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001510 if (!Name) {
1511 // This is an anonymous namespace. Adopt an existing anonymous
1512 // namespace if we can.
1513 // FIXME: Not testable.
1514 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1515 MergeWithNamespace = TU->getAnonymousNamespace();
1516 else
1517 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1518 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001519 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001520 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001521 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001522 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1523 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001524 continue;
1525
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001526 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001527 MergeWithNamespace = FoundNS;
1528 ConflictingDecls.clear();
1529 break;
1530 }
1531
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001532 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001533 }
1534
1535 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001536 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001537 ConflictingDecls.data(),
1538 ConflictingDecls.size());
1539 }
1540 }
1541
1542 // Create the "to" namespace, if needed.
1543 NamespaceDecl *ToNamespace = MergeWithNamespace;
1544 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001545 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00001546 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001547 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00001548 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00001549 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001550 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001551 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001552
1553 // If this is an anonymous namespace, register it as the anonymous
1554 // namespace within its context.
1555 if (!Name) {
1556 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1557 TU->setAnonymousNamespace(ToNamespace);
1558 else
1559 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1560 }
1561 }
1562 Importer.Imported(D, ToNamespace);
1563
1564 ImportDeclContext(D);
1565
1566 return ToNamespace;
1567}
1568
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001569Decl *ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1570 // Import the major distinguishing characteristics of this namespace.
1571 DeclContext *DC, *LexicalDC;
1572 DeclarationName Name;
1573 SourceLocation Loc;
1574 NamedDecl *LookupD;
1575 if (ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
1576 return nullptr;
1577 if (LookupD)
1578 return LookupD;
1579
1580 // NOTE: No conflict resolution is done for namespace aliases now.
1581
1582 NamespaceDecl *TargetDecl = cast_or_null<NamespaceDecl>(
1583 Importer.Import(D->getNamespace()));
1584 if (!TargetDecl)
1585 return nullptr;
1586
1587 IdentifierInfo *ToII = Importer.Import(D->getIdentifier());
1588 if (!ToII)
1589 return nullptr;
1590
1591 NestedNameSpecifierLoc ToQLoc = Importer.Import(D->getQualifierLoc());
1592 if (D->getQualifierLoc() && !ToQLoc)
1593 return nullptr;
1594
1595 NamespaceAliasDecl *ToD = NamespaceAliasDecl::Create(
1596 Importer.getToContext(), DC, Importer.Import(D->getNamespaceLoc()),
1597 Importer.Import(D->getAliasLoc()), ToII, ToQLoc,
1598 Importer.Import(D->getTargetNameLoc()), TargetDecl);
1599
1600 ToD->setLexicalDeclContext(LexicalDC);
1601 Importer.Imported(D, ToD);
1602 LexicalDC->addDeclInternal(ToD);
1603
1604 return ToD;
1605}
1606
Richard Smithdda56e42011-04-15 14:24:37 +00001607Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001608 // Import the major distinguishing characteristics of this typedef.
1609 DeclContext *DC, *LexicalDC;
1610 DeclarationName Name;
1611 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001612 NamedDecl *ToD;
1613 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001614 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001615 if (ToD)
1616 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001617
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001618 // If this typedef is not in block scope, determine whether we've
1619 // seen a typedef with the same name (that we can merge with) or any
1620 // other entity by that name (which name lookup could conflict with).
1621 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001622 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001623 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001624 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001625 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001626 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1627 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001628 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00001629 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001630 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001631 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1632 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001633 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001634 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001635
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001636 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001637 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001638
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001639 if (!ConflictingDecls.empty()) {
1640 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1641 ConflictingDecls.data(),
1642 ConflictingDecls.size());
1643 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001644 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001645 }
1646 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001647
Douglas Gregorb4964f72010-02-15 23:54:17 +00001648 // Import the underlying type of this typedef;
1649 QualType T = Importer.Import(D->getUnderlyingType());
1650 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001651 return nullptr;
1652
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001653 // Create the new typedef node.
1654 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00001655 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00001656 TypedefNameDecl *ToTypedef;
1657 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001658 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
1659 StartL, Loc,
1660 Name.getAsIdentifierInfo(),
1661 TInfo);
1662 else
Richard Smithdda56e42011-04-15 14:24:37 +00001663 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1664 StartL, Loc,
1665 Name.getAsIdentifierInfo(),
1666 TInfo);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001667
Douglas Gregordd483172010-02-22 17:42:47 +00001668 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001669 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001670 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00001671 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001672
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001673 return ToTypedef;
1674}
1675
Richard Smithdda56e42011-04-15 14:24:37 +00001676Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1677 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1678}
1679
1680Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1681 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1682}
1683
Gabor Horvath7a91c082017-11-14 11:30:38 +00001684Decl *ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1685 // Import the major distinguishing characteristics of this typedef.
1686 DeclContext *DC, *LexicalDC;
1687 DeclarationName Name;
1688 SourceLocation Loc;
1689 NamedDecl *ToD;
1690 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1691 return nullptr;
1692 if (ToD)
1693 return ToD;
1694
1695 // If this typedef is not in block scope, determine whether we've
1696 // seen a typedef with the same name (that we can merge with) or any
1697 // other entity by that name (which name lookup could conflict with).
1698 if (!DC->isFunctionOrMethod()) {
1699 SmallVector<NamedDecl *, 4> ConflictingDecls;
1700 unsigned IDNS = Decl::IDNS_Ordinary;
1701 SmallVector<NamedDecl *, 2> FoundDecls;
1702 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1703 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1704 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1705 continue;
1706 if (auto *FoundAlias =
1707 dyn_cast<TypeAliasTemplateDecl>(FoundDecls[I]))
1708 return Importer.Imported(D, FoundAlias);
1709 ConflictingDecls.push_back(FoundDecls[I]);
1710 }
1711
1712 if (!ConflictingDecls.empty()) {
1713 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1714 ConflictingDecls.data(),
1715 ConflictingDecls.size());
1716 if (!Name)
1717 return nullptr;
1718 }
1719 }
1720
1721 TemplateParameterList *Params = ImportTemplateParameterList(
1722 D->getTemplateParameters());
1723 if (!Params)
1724 return nullptr;
1725
1726 NamedDecl *TemplDecl = cast_or_null<NamedDecl>(
1727 Importer.Import(D->getTemplatedDecl()));
1728 if (!TemplDecl)
1729 return nullptr;
1730
1731 TypeAliasTemplateDecl *ToAlias = TypeAliasTemplateDecl::Create(
1732 Importer.getToContext(), DC, Loc, Name, Params, TemplDecl);
1733
1734 ToAlias->setAccess(D->getAccess());
1735 ToAlias->setLexicalDeclContext(LexicalDC);
1736 Importer.Imported(D, ToAlias);
1737 LexicalDC->addDeclInternal(ToAlias);
1738 return ToD;
1739}
1740
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001741Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
1742 // Import the major distinguishing characteristics of this label.
1743 DeclContext *DC, *LexicalDC;
1744 DeclarationName Name;
1745 SourceLocation Loc;
1746 NamedDecl *ToD;
1747 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1748 return nullptr;
1749 if (ToD)
1750 return ToD;
1751
1752 assert(LexicalDC->isFunctionOrMethod());
1753
1754 LabelDecl *ToLabel = D->isGnuLocal()
1755 ? LabelDecl::Create(Importer.getToContext(),
1756 DC, Importer.Import(D->getLocation()),
1757 Name.getAsIdentifierInfo(),
1758 Importer.Import(D->getLocStart()))
1759 : LabelDecl::Create(Importer.getToContext(),
1760 DC, Importer.Import(D->getLocation()),
1761 Name.getAsIdentifierInfo());
1762 Importer.Imported(D, ToLabel);
1763
1764 LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
1765 if (!Label)
1766 return nullptr;
1767
1768 ToLabel->setStmt(Label);
1769 ToLabel->setLexicalDeclContext(LexicalDC);
1770 LexicalDC->addDeclInternal(ToLabel);
1771 return ToLabel;
1772}
1773
Douglas Gregor98c10182010-02-12 22:17:39 +00001774Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1775 // Import the major distinguishing characteristics of this enum.
1776 DeclContext *DC, *LexicalDC;
1777 DeclarationName Name;
1778 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001779 NamedDecl *ToD;
1780 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001781 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001782 if (ToD)
1783 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001784
Douglas Gregor98c10182010-02-12 22:17:39 +00001785 // Figure out what enum name we're looking for.
1786 unsigned IDNS = Decl::IDNS_Tag;
1787 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001788 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1789 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00001790 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001791 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00001792 IDNS |= Decl::IDNS_Ordinary;
1793
1794 // We may already have an enum of the same name; try to find and match it.
1795 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001796 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001797 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001798 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001799 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1800 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00001801 continue;
1802
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001803 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00001804 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00001805 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1806 Found = Tag->getDecl();
1807 }
1808
1809 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001810 if (IsStructuralMatch(D, FoundEnum))
1811 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001812 }
1813
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001814 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00001815 }
1816
1817 if (!ConflictingDecls.empty()) {
1818 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1819 ConflictingDecls.data(),
1820 ConflictingDecls.size());
1821 }
1822 }
1823
1824 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001825 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
1826 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00001827 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001828 D->isScoped(), D->isScopedUsingClassTag(),
1829 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00001830 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00001831 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00001832 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00001833 D2->setLexicalDeclContext(LexicalDC);
1834 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00001835 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00001836
1837 // Import the integer type.
1838 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1839 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001840 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00001841 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001842
1843 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00001844 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00001845 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00001846
Douglas Gregor3996e242010-02-15 22:01:00 +00001847 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00001848}
1849
Douglas Gregor5c73e912010-02-11 00:48:18 +00001850Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1851 // If this record has a definition in the translation unit we're coming from,
1852 // but this particular declaration is not that definition, import the
1853 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001854 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001855 if (Definition && Definition != D) {
1856 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001857 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00001858 return nullptr;
1859
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001860 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001861 }
1862
1863 // Import the major distinguishing characteristics of this record.
1864 DeclContext *DC, *LexicalDC;
1865 DeclarationName Name;
1866 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001867 NamedDecl *ToD;
1868 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001869 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001870 if (ToD)
1871 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001872
Douglas Gregor5c73e912010-02-11 00:48:18 +00001873 // Figure out what structure name we're looking for.
1874 unsigned IDNS = Decl::IDNS_Tag;
1875 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001876 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1877 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001878 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001879 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00001880 IDNS |= Decl::IDNS_Ordinary;
1881
1882 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00001883 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00001884 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00001885 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001886 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001887 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001888 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00001889
1890 if (!FoundDecls.empty()) {
1891 // We're going to have to compare D against potentially conflicting Decls, so complete it.
1892 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
1893 D->getASTContext().getExternalSource()->CompleteType(D);
1894 }
1895
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001896 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1897 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00001898 continue;
1899
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001900 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00001901 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00001902 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1903 Found = Tag->getDecl();
1904 }
1905
1906 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Gabor Horvath3b392bb2017-04-03 21:06:45 +00001907 if (D->isAnonymousStructOrUnion() &&
1908 FoundRecord->isAnonymousStructOrUnion()) {
1909 // If both anonymous structs/unions are in a record context, make sure
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001910 // they occur in the same location in the context records.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001911 if (Optional<unsigned> Index1 =
1912 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
1913 D)) {
1914 if (Optional<unsigned> Index2 = StructuralEquivalenceContext::
Sean Callanan488f8612016-07-14 19:53:44 +00001915 findUntaggedStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001916 if (*Index1 != *Index2)
1917 continue;
1918 }
1919 }
1920 }
1921
Sean Callanan9092d472017-05-13 00:46:33 +00001922 PrevDecl = FoundRecord;
1923
Douglas Gregor25791052010-02-12 00:09:27 +00001924 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00001925 if ((SearchName && !D->isCompleteDefinition())
1926 || (D->isCompleteDefinition() &&
1927 D->isAnonymousStructOrUnion()
1928 == FoundDef->isAnonymousStructOrUnion() &&
1929 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00001930 // The record types structurally match, or the "from" translation
1931 // unit only had a forward declaration anyway; call it the same
1932 // function.
1933 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001934 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00001935 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00001936 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00001937 // We have a forward declaration of this type, so adopt that forward
1938 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00001939
1940 // If one or both can be completed from external storage then try one
1941 // last time to complete and compare them before doing this.
1942
1943 if (FoundRecord->hasExternalLexicalStorage() &&
1944 !FoundRecord->isCompleteDefinition())
1945 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
1946 if (D->hasExternalLexicalStorage())
1947 D->getASTContext().getExternalSource()->CompleteType(D);
1948
1949 if (FoundRecord->isCompleteDefinition() &&
1950 D->isCompleteDefinition() &&
1951 !IsStructuralMatch(D, FoundRecord))
1952 continue;
1953
Douglas Gregor25791052010-02-12 00:09:27 +00001954 AdoptDecl = FoundRecord;
1955 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00001956 } else if (!SearchName) {
1957 continue;
1958 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00001959 }
1960
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001961 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001962 }
1963
Douglas Gregordd6006f2012-07-17 21:16:27 +00001964 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00001965 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1966 ConflictingDecls.data(),
1967 ConflictingDecls.size());
1968 }
1969 }
1970
1971 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001972 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001973 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00001974 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00001975 CXXRecordDecl *D2CXX = nullptr;
1976 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) {
1977 if (DCXX->isLambda()) {
1978 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
1979 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
1980 DC, TInfo, Loc,
1981 DCXX->isDependentLambda(),
1982 DCXX->isGenericLambda(),
1983 DCXX->getLambdaCaptureDefault());
1984 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
1985 if (DCXX->getLambdaContextDecl() && !CDecl)
1986 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00001987 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001988 } else if (DCXX->isInjectedClassName()) {
1989 // We have to be careful to do a similar dance to the one in
1990 // Sema::ActOnStartCXXMemberDeclarations
1991 CXXRecordDecl *const PrevDecl = nullptr;
1992 const bool DelayTypeCreation = true;
1993 D2CXX = CXXRecordDecl::Create(
1994 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
1995 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
1996 Importer.getToContext().getTypeDeclType(
1997 D2CXX, llvm::dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00001998 } else {
1999 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2000 D->getTagKind(),
2001 DC, StartLoc, Loc,
2002 Name.getAsIdentifierInfo());
2003 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002004 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002005 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002006 D2->setLexicalDeclContext(LexicalDC);
2007 if (!DCXX->getDescribedClassTemplate())
2008 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002009
2010 Importer.Imported(D, D2);
2011
2012 if (ClassTemplateDecl *FromDescribed =
2013 DCXX->getDescribedClassTemplate()) {
2014 ClassTemplateDecl *ToDescribed = cast_or_null<ClassTemplateDecl>(
2015 Importer.Import(FromDescribed));
2016 if (!ToDescribed)
2017 return nullptr;
2018 D2CXX->setDescribedClassTemplate(ToDescribed);
2019
2020 } else if (MemberSpecializationInfo *MemberInfo =
2021 DCXX->getMemberSpecializationInfo()) {
2022 TemplateSpecializationKind SK =
2023 MemberInfo->getTemplateSpecializationKind();
2024 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
2025 CXXRecordDecl *ToInst =
2026 cast_or_null<CXXRecordDecl>(Importer.Import(FromInst));
2027 if (FromInst && !ToInst)
2028 return nullptr;
2029 D2CXX->setInstantiationOfMemberClass(ToInst, SK);
2030 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2031 Importer.Import(MemberInfo->getPointOfInstantiation()));
2032 }
2033
Douglas Gregor25791052010-02-12 00:09:27 +00002034 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002035 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002036 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002037 D2->setLexicalDeclContext(LexicalDC);
2038 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002039 }
Douglas Gregor14454802011-02-25 02:25:35 +00002040
2041 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd6006f2012-07-17 21:16:27 +00002042 if (D->isAnonymousStructOrUnion())
2043 D2->setAnonymousStructOrUnion(true);
Sean Callanan9092d472017-05-13 00:46:33 +00002044 if (PrevDecl) {
2045 // FIXME: do this for all Redeclarables, not just RecordDecls.
2046 D2->setPreviousDecl(PrevDecl);
2047 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002048 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002049
Douglas Gregor3996e242010-02-15 22:01:00 +00002050 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002051
Douglas Gregor95d82832012-01-24 18:36:04 +00002052 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002053 return nullptr;
2054
Douglas Gregor3996e242010-02-15 22:01:00 +00002055 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002056}
2057
Douglas Gregor98c10182010-02-12 22:17:39 +00002058Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2059 // Import the major distinguishing characteristics of this enumerator.
2060 DeclContext *DC, *LexicalDC;
2061 DeclarationName Name;
2062 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002063 NamedDecl *ToD;
2064 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002065 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002066 if (ToD)
2067 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002068
2069 QualType T = Importer.Import(D->getType());
2070 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002071 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002072
Douglas Gregor98c10182010-02-12 22:17:39 +00002073 // Determine whether there are any other declarations with the same name and
2074 // in the same context.
2075 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002076 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002077 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002078 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002079 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002080 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2081 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002082 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002083
2084 if (EnumConstantDecl *FoundEnumConstant
2085 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2086 if (IsStructuralMatch(D, FoundEnumConstant))
2087 return Importer.Imported(D, FoundEnumConstant);
2088 }
2089
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002090 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002091 }
2092
2093 if (!ConflictingDecls.empty()) {
2094 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2095 ConflictingDecls.data(),
2096 ConflictingDecls.size());
2097 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002098 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002099 }
2100 }
2101
2102 Expr *Init = Importer.Import(D->getInitExpr());
2103 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002104 return nullptr;
2105
Douglas Gregor98c10182010-02-12 22:17:39 +00002106 EnumConstantDecl *ToEnumerator
2107 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2108 Name.getAsIdentifierInfo(), T,
2109 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002110 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002111 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002112 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002113 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002114 return ToEnumerator;
2115}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002116
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002117bool ASTNodeImporter::ImportTemplateInformation(FunctionDecl *FromFD,
2118 FunctionDecl *ToFD) {
2119 switch (FromFD->getTemplatedKind()) {
2120 case FunctionDecl::TK_NonTemplate:
2121 case FunctionDecl::TK_FunctionTemplate:
Sam McCallfdc32072018-01-26 12:06:44 +00002122 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002123
2124 case FunctionDecl::TK_MemberSpecialization: {
2125 auto *InstFD = cast_or_null<FunctionDecl>(
2126 Importer.Import(FromFD->getInstantiatedFromMemberFunction()));
2127 if (!InstFD)
2128 return true;
2129
2130 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
2131 SourceLocation POI = Importer.Import(
2132 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation());
2133 ToFD->setInstantiationOfMemberFunction(InstFD, TSK);
2134 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002135 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002136 }
2137
2138 case FunctionDecl::TK_FunctionTemplateSpecialization: {
2139 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
2140 auto *Template = cast_or_null<FunctionTemplateDecl>(
2141 Importer.Import(FTSInfo->getTemplate()));
2142 if (!Template)
2143 return true;
2144 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
2145
2146 // Import template arguments.
2147 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
2148 SmallVector<TemplateArgument, 8> ToTemplArgs;
2149 if (ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
2150 ToTemplArgs))
2151 return true;
2152
2153 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
2154 Importer.getToContext(), ToTemplArgs);
2155
2156 TemplateArgumentListInfo ToTAInfo;
2157 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
2158 if (FromTAArgsAsWritten) {
2159 if (ImportTemplateArgumentListInfo(
2160 FromTAArgsAsWritten->LAngleLoc, FromTAArgsAsWritten->RAngleLoc,
2161 FromTAArgsAsWritten->arguments(), ToTAInfo))
2162 return true;
2163 }
2164
2165 SourceLocation POI = Importer.Import(FTSInfo->getPointOfInstantiation());
2166
2167 ToFD->setFunctionTemplateSpecialization(
2168 Template, ToTAList, /* InsertPos= */ nullptr,
2169 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002170 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002171 }
2172
2173 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2174 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2175 UnresolvedSet<8> TemplDecls;
2176 unsigned NumTemplates = FromInfo->getNumTemplates();
2177 for (unsigned I = 0; I < NumTemplates; I++) {
2178 if (auto *ToFTD = cast_or_null<FunctionTemplateDecl>(
2179 Importer.Import(FromInfo->getTemplate(I))))
2180 TemplDecls.addDecl(ToFTD);
2181 else
2182 return true;
2183 }
2184
2185 // Import TemplateArgumentListInfo.
2186 TemplateArgumentListInfo ToTAInfo;
2187 if (ImportTemplateArgumentListInfo(
2188 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2189 llvm::makeArrayRef(FromInfo->getTemplateArgs(),
2190 FromInfo->getNumTemplateArgs()),
2191 ToTAInfo))
2192 return true;
2193
2194 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2195 TemplDecls, ToTAInfo);
Sam McCallfdc32072018-01-26 12:06:44 +00002196 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002197 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002198 }
Sam McCallfdc32072018-01-26 12:06:44 +00002199 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002200}
2201
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002202Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2203 // Import the major distinguishing characteristics of this function.
2204 DeclContext *DC, *LexicalDC;
2205 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002206 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002207 NamedDecl *ToD;
2208 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002209 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002210 if (ToD)
2211 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002212
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002213 const FunctionDecl *FoundWithoutBody = nullptr;
2214
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002215 // Try to find a function in our own ("to") context with the same name, same
2216 // type, and in the same context as the function we're importing.
2217 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002218 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002219 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002220 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002221 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002222 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2223 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002224 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002225
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002226 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002227 if (FoundFunction->hasExternalFormalLinkage() &&
2228 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002229 if (Importer.IsStructurallyEquivalent(D->getType(),
2230 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002231 // FIXME: Actually try to merge the body and other attributes.
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002232 const FunctionDecl *FromBodyDecl = nullptr;
2233 D->hasBody(FromBodyDecl);
2234 if (D == FromBodyDecl && !FoundFunction->hasBody()) {
2235 // This function is needed to merge completely.
2236 FoundWithoutBody = FoundFunction;
2237 break;
2238 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002239 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002240 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002241
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002242 // FIXME: Check for overloading more carefully, e.g., by boosting
2243 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002244
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002245 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002246 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002247 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002248
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002249 // Complain about inconsistent function types.
2250 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002251 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002252 Importer.ToDiag(FoundFunction->getLocation(),
2253 diag::note_odr_value_here)
2254 << FoundFunction->getType();
2255 }
2256 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002257
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002258 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002259 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002260
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002261 if (!ConflictingDecls.empty()) {
2262 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2263 ConflictingDecls.data(),
2264 ConflictingDecls.size());
2265 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002266 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002267 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002268 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002269
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002270 DeclarationNameInfo NameInfo(Name, Loc);
2271 // Import additional name location/type info.
2272 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2273
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002274 QualType FromTy = D->getType();
2275 bool usedDifferentExceptionSpec = false;
2276
2277 if (const FunctionProtoType *
2278 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2279 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2280 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2281 // FunctionDecl that we are importing the FunctionProtoType for.
2282 // To avoid an infinite recursion when importing, create the FunctionDecl
2283 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002284 if (FromEPI.ExceptionSpec.SourceDecl ||
2285 FromEPI.ExceptionSpec.SourceTemplate ||
2286 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002287 FunctionProtoType::ExtProtoInfo DefaultEPI;
2288 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002289 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002290 usedDifferentExceptionSpec = true;
2291 }
2292 }
2293
Douglas Gregorb4964f72010-02-15 23:54:17 +00002294 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002295 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002296 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002297 return nullptr;
2298
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002299 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002300 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00002301 for (auto P : D->parameters()) {
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00002302 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002303 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002304 return nullptr;
2305
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002306 Parameters.push_back(ToP);
2307 }
2308
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002309 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002310 if (D->getTypeSourceInfo() && !TInfo)
2311 return nullptr;
2312
2313 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00002314 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002315 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00002316 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2317 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2318 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002319 InnerLocStart,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002320 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002321 FromConstructor->isExplicit(),
2322 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002323 D->isImplicit(),
2324 D->isConstexpr());
Sean Callanandd2c1742016-05-16 20:48:03 +00002325 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
2326 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
2327 for (CXXCtorInitializer *I : FromConstructor->inits()) {
2328 CXXCtorInitializer *ToI =
2329 cast_or_null<CXXCtorInitializer>(Importer.Import(I));
2330 if (!ToI && I)
2331 return nullptr;
2332 CtorInitializers.push_back(ToI);
2333 }
2334 CXXCtorInitializer **Memory =
2335 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
2336 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
2337 CXXConstructorDecl *ToCtor = llvm::cast<CXXConstructorDecl>(ToFunction);
2338 ToCtor->setCtorInitializers(Memory);
2339 ToCtor->setNumCtorInitializers(NumInitializers);
2340 }
Douglas Gregor00eace12010-02-21 18:29:16 +00002341 } else if (isa<CXXDestructorDecl>(D)) {
2342 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2343 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002344 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002345 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002346 D->isInlineSpecified(),
2347 D->isImplicit());
2348 } else if (CXXConversionDecl *FromConversion
2349 = dyn_cast<CXXConversionDecl>(D)) {
2350 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2351 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002352 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002353 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002354 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002355 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002356 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002357 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002358 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2359 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2360 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002361 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00002362 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002363 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002364 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002365 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002366 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002367 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002368 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00002369 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002370 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002371 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002372 D->hasWrittenPrototype(),
2373 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002374 }
John McCall3e11ebe2010-03-15 10:12:16 +00002375
2376 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002377 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002378 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002379 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002380 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2381 ToFunction->setTrivial(D->isTrivial());
2382 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002383 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002384
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002385 // Set the parameters.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002386 for (ParmVarDecl *Param : Parameters) {
2387 Param->setOwningFunction(ToFunction);
2388 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002389 }
David Blaikie9c70e042011-09-21 18:16:56 +00002390 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002391
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002392 if (FoundWithoutBody) {
2393 auto *Recent = const_cast<FunctionDecl *>(
2394 FoundWithoutBody->getMostRecentDecl());
2395 ToFunction->setPreviousDecl(Recent);
2396 }
2397
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002398 // We need to complete creation of FunctionProtoTypeLoc manually with setting
2399 // params it refers to.
2400 if (TInfo) {
2401 if (auto ProtoLoc =
2402 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
2403 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
2404 ProtoLoc.setParam(I, Parameters[I]);
2405 }
2406 }
2407
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002408 if (usedDifferentExceptionSpec) {
2409 // Update FunctionProtoType::ExtProtoInfo.
2410 QualType T = Importer.Import(D->getType());
2411 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002412 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002413 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002414 }
2415
Sean Callanan59721b32015-04-28 18:41:46 +00002416 // Import the body, if any.
2417 if (Stmt *FromBody = D->getBody()) {
2418 if (Stmt *ToBody = Importer.Import(FromBody)) {
2419 ToFunction->setBody(ToBody);
2420 }
2421 }
2422
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002423 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002424
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002425 // If it is a template, import all related things.
2426 if (ImportTemplateInformation(D, ToFunction))
2427 return nullptr;
2428
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002429 // Add this function to the lexical context.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002430 // NOTE: If the function is templated declaration, it should be not added into
2431 // LexicalDC. But described template is imported during import of
2432 // FunctionTemplateDecl (it happens later). So, we use source declaration
2433 // to determine if we should add the result function.
2434 if (!D->getDescribedFunctionTemplate())
2435 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002436
Lang Hames19e07e12017-06-20 21:06:00 +00002437 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2438 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2439
Douglas Gregor43f54792010-02-17 02:12:47 +00002440 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002441}
2442
Douglas Gregor00eace12010-02-21 18:29:16 +00002443Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2444 return VisitFunctionDecl(D);
2445}
2446
2447Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2448 return VisitCXXMethodDecl(D);
2449}
2450
2451Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2452 return VisitCXXMethodDecl(D);
2453}
2454
2455Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2456 return VisitCXXMethodDecl(D);
2457}
2458
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002459static unsigned getFieldIndex(Decl *F) {
2460 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2461 if (!Owner)
2462 return 0;
2463
2464 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002465 for (const auto *D : Owner->noload_decls()) {
2466 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002467 return Index;
2468
2469 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2470 ++Index;
2471 }
2472
2473 return Index;
2474}
2475
Douglas Gregor5c73e912010-02-11 00:48:18 +00002476Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2477 // Import the major distinguishing characteristics of a variable.
2478 DeclContext *DC, *LexicalDC;
2479 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002480 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002481 NamedDecl *ToD;
2482 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002483 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002484 if (ToD)
2485 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002486
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002487 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002488 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002489 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002490 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2491 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002492 // For anonymous fields, match up by index.
2493 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2494 continue;
2495
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002496 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002497 FoundField->getType())) {
2498 Importer.Imported(D, FoundField);
2499 return FoundField;
2500 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002501
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002502 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2503 << Name << D->getType() << FoundField->getType();
2504 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2505 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002506 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002507 }
2508 }
2509
Douglas Gregorb4964f72010-02-15 23:54:17 +00002510 // Import the type.
2511 QualType T = Importer.Import(D->getType());
2512 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002513 return nullptr;
2514
Douglas Gregor5c73e912010-02-11 00:48:18 +00002515 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2516 Expr *BitWidth = Importer.Import(D->getBitWidth());
2517 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002518 return nullptr;
2519
Abramo Bagnaradff19302011-03-08 08:55:46 +00002520 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2521 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002522 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002523 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002524 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002525 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002526 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00002527 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00002528 Expr *ToInitializer = Importer.Import(FromInitializer);
2529 if (ToInitializer)
2530 ToField->setInClassInitializer(ToInitializer);
2531 else
2532 return nullptr;
2533 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002534 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002535 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002536 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002537 return ToField;
2538}
2539
Francois Pichet783dd6e2010-11-21 06:08:52 +00002540Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2541 // Import the major distinguishing characteristics of a variable.
2542 DeclContext *DC, *LexicalDC;
2543 DeclarationName Name;
2544 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002545 NamedDecl *ToD;
2546 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002547 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002548 if (ToD)
2549 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002550
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002551 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002552 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002553 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002554 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002555 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002556 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002557 // For anonymous indirect fields, match up by index.
2558 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2559 continue;
2560
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002561 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002562 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002563 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002564 Importer.Imported(D, FoundField);
2565 return FoundField;
2566 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002567
2568 // If there are more anonymous fields to check, continue.
2569 if (!Name && I < N-1)
2570 continue;
2571
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002572 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2573 << Name << D->getType() << FoundField->getType();
2574 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2575 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002576 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002577 }
2578 }
2579
Francois Pichet783dd6e2010-11-21 06:08:52 +00002580 // Import the type.
2581 QualType T = Importer.Import(D->getType());
2582 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002583 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002584
2585 NamedDecl **NamedChain =
2586 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2587
2588 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00002589 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00002590 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002591 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00002592 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002593 NamedChain[i++] = cast<NamedDecl>(D);
2594 }
2595
2596 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00002597 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
David Majnemer59f77922016-06-24 04:05:48 +00002598 {NamedChain, D->getChainingSize()});
Aaron Ballman260995b2014-10-15 16:58:18 +00002599
2600 for (const auto *Attr : D->attrs())
2601 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
2602
Francois Pichet783dd6e2010-11-21 06:08:52 +00002603 ToIndirectField->setAccess(D->getAccess());
2604 ToIndirectField->setLexicalDeclContext(LexicalDC);
2605 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002606 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002607 return ToIndirectField;
2608}
2609
Aleksei Sidorina693b372016-09-28 10:16:56 +00002610Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2611 // Import the major distinguishing characteristics of a declaration.
2612 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2613 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2614 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2615 if (!DC || !LexicalDC)
2616 return nullptr;
2617
2618 // Determine whether we've already imported this decl.
2619 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2620 auto *RD = cast<CXXRecordDecl>(DC);
2621 FriendDecl *ImportedFriend = RD->getFirstFriend();
2622 StructuralEquivalenceContext Context(
2623 Importer.getFromContext(), Importer.getToContext(),
2624 Importer.getNonEquivalentDecls(), false, false);
2625
2626 while (ImportedFriend) {
2627 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2628 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2629 ImportedFriend->getFriendDecl()))
2630 return Importer.Imported(D, ImportedFriend);
2631
2632 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2633 if (Importer.IsStructurallyEquivalent(
2634 D->getFriendType()->getType(),
2635 ImportedFriend->getFriendType()->getType(), true))
2636 return Importer.Imported(D, ImportedFriend);
2637 }
2638 ImportedFriend = ImportedFriend->getNextFriend();
2639 }
2640
2641 // Not found. Create it.
2642 FriendDecl::FriendUnion ToFU;
2643 if (NamedDecl *FriendD = D->getFriendDecl())
2644 ToFU = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2645 else
2646 ToFU = Importer.Import(D->getFriendType());
2647 if (!ToFU)
2648 return nullptr;
2649
2650 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
2651 TemplateParameterList **FromTPLists =
2652 D->getTrailingObjects<TemplateParameterList *>();
2653 for (unsigned I = 0; I < D->NumTPLists; I++) {
2654 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2655 if (!List)
2656 return nullptr;
2657 ToTPLists[I] = List;
2658 }
2659
2660 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
2661 Importer.Import(D->getLocation()),
2662 ToFU, Importer.Import(D->getFriendLoc()),
2663 ToTPLists);
2664
2665 Importer.Imported(D, FrD);
2666 RD->pushFriendDecl(FrD);
2667
2668 FrD->setAccess(D->getAccess());
2669 FrD->setLexicalDeclContext(LexicalDC);
2670 LexicalDC->addDeclInternal(FrD);
2671 return FrD;
2672}
2673
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002674Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2675 // Import the major distinguishing characteristics of an ivar.
2676 DeclContext *DC, *LexicalDC;
2677 DeclarationName Name;
2678 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002679 NamedDecl *ToD;
2680 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002681 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002682 if (ToD)
2683 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002684
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002685 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002686 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002687 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002688 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2689 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002690 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002691 FoundIvar->getType())) {
2692 Importer.Imported(D, FoundIvar);
2693 return FoundIvar;
2694 }
2695
2696 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2697 << Name << D->getType() << FoundIvar->getType();
2698 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2699 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002700 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002701 }
2702 }
2703
2704 // Import the type.
2705 QualType T = Importer.Import(D->getType());
2706 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002707 return nullptr;
2708
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002709 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2710 Expr *BitWidth = Importer.Import(D->getBitWidth());
2711 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002712 return nullptr;
2713
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002714 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2715 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002716 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002717 Loc, Name.getAsIdentifierInfo(),
2718 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00002719 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002720 ToIvar->setLexicalDeclContext(LexicalDC);
2721 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002722 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002723 return ToIvar;
2724
2725}
2726
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002727Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2728 // Import the major distinguishing characteristics of a variable.
2729 DeclContext *DC, *LexicalDC;
2730 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002731 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002732 NamedDecl *ToD;
2733 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002734 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002735 if (ToD)
2736 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002737
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002738 // Try to find a variable in our own ("to") context with the same name and
2739 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002740 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00002741 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002742 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002743 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002744 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002745 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002746 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2747 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002748 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002749
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002750 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002751 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00002752 if (FoundVar->hasExternalFormalLinkage() &&
2753 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002754 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002755 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002756 MergeWithVar = FoundVar;
2757 break;
2758 }
2759
Douglas Gregor56521c52010-02-12 17:23:39 +00002760 const ArrayType *FoundArray
2761 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2762 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002763 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002764 if (FoundArray && TArray) {
2765 if (isa<IncompleteArrayType>(FoundArray) &&
2766 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002767 // Import the type.
2768 QualType T = Importer.Import(D->getType());
2769 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002770 return nullptr;
2771
Douglas Gregor56521c52010-02-12 17:23:39 +00002772 FoundVar->setType(T);
2773 MergeWithVar = FoundVar;
2774 break;
2775 } else if (isa<IncompleteArrayType>(TArray) &&
2776 isa<ConstantArrayType>(FoundArray)) {
2777 MergeWithVar = FoundVar;
2778 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002779 }
2780 }
2781
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002782 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002783 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002784 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2785 << FoundVar->getType();
2786 }
2787 }
2788
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002789 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002790 }
2791
2792 if (MergeWithVar) {
2793 // An equivalent variable with external linkage has been found. Link
2794 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002795 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002796
2797 if (VarDecl *DDef = D->getDefinition()) {
2798 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2799 Importer.ToDiag(ExistingDef->getLocation(),
2800 diag::err_odr_variable_multiple_def)
2801 << Name;
2802 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2803 } else {
2804 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002805 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002806 if (DDef->isInitKnownICE()) {
2807 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2808 Eval->CheckedICE = true;
2809 Eval->IsICE = DDef->isInitICE();
2810 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002811 }
2812 }
2813
2814 return MergeWithVar;
2815 }
2816
2817 if (!ConflictingDecls.empty()) {
2818 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2819 ConflictingDecls.data(),
2820 ConflictingDecls.size());
2821 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002822 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002823 }
2824 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002825
Douglas Gregorb4964f72010-02-15 23:54:17 +00002826 // Import the type.
2827 QualType T = Importer.Import(D->getType());
2828 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002829 return nullptr;
2830
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002831 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002832 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002833 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2834 Importer.Import(D->getInnerLocStart()),
2835 Loc, Name.getAsIdentifierInfo(),
2836 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002837 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00002838 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002839 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002840 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002841 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002842 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002843
Sean Callanan59721b32015-04-28 18:41:46 +00002844 if (!D->isFileVarDecl() &&
2845 D->isUsed())
2846 ToVar->setIsUsed();
2847
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002848 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002849 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00002850 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002851
Aleksei Sidorin855086d2017-01-23 09:30:36 +00002852 if (D->isConstexpr())
2853 ToVar->setConstexpr(true);
2854
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002855 return ToVar;
2856}
2857
Douglas Gregor8b228d72010-02-17 21:22:52 +00002858Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2859 // Parameters are created in the translation unit's context, then moved
2860 // into the function declaration's context afterward.
2861 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2862
2863 // Import the name of this declaration.
2864 DeclarationName Name = Importer.Import(D->getDeclName());
2865 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002866 return nullptr;
2867
Douglas Gregor8b228d72010-02-17 21:22:52 +00002868 // Import the location of this declaration.
2869 SourceLocation Loc = Importer.Import(D->getLocation());
2870
2871 // Import the parameter's type.
2872 QualType T = Importer.Import(D->getType());
2873 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002874 return nullptr;
2875
Douglas Gregor8b228d72010-02-17 21:22:52 +00002876 // Create the imported parameter.
Alexey Bataev56223232017-06-09 13:40:18 +00002877 auto *ToParm = ImplicitParamDecl::Create(Importer.getToContext(), DC, Loc,
2878 Name.getAsIdentifierInfo(), T,
2879 D->getParameterKind());
Douglas Gregor8b228d72010-02-17 21:22:52 +00002880 return Importer.Imported(D, ToParm);
2881}
2882
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002883Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2884 // Parameters are created in the translation unit's context, then moved
2885 // into the function declaration's context afterward.
2886 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2887
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002888 // Import the name of this declaration.
2889 DeclarationName Name = Importer.Import(D->getDeclName());
2890 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002891 return nullptr;
2892
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002893 // Import the location of this declaration.
2894 SourceLocation Loc = Importer.Import(D->getLocation());
2895
2896 // Import the parameter's type.
2897 QualType T = Importer.Import(D->getType());
2898 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002899 return nullptr;
2900
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002901 // Create the imported parameter.
2902 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2903 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002904 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002905 Loc, Name.getAsIdentifierInfo(),
2906 T, TInfo, D->getStorageClass(),
Aleksei Sidorin55a63502017-02-20 11:57:12 +00002907 /*DefaultArg*/ nullptr);
2908
2909 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00002910 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00002911 ToParm->setKNRPromoted(D->isKNRPromoted());
2912
2913 Expr *ToDefArg = nullptr;
2914 Expr *FromDefArg = nullptr;
2915 if (D->hasUninstantiatedDefaultArg()) {
2916 FromDefArg = D->getUninstantiatedDefaultArg();
2917 ToDefArg = Importer.Import(FromDefArg);
2918 ToParm->setUninstantiatedDefaultArg(ToDefArg);
2919 } else if (D->hasUnparsedDefaultArg()) {
2920 ToParm->setUnparsedDefaultArg();
2921 } else if (D->hasDefaultArg()) {
2922 FromDefArg = D->getDefaultArg();
2923 ToDefArg = Importer.Import(FromDefArg);
2924 ToParm->setDefaultArg(ToDefArg);
2925 }
2926 if (FromDefArg && !ToDefArg)
2927 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002928
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002929 if (D->isObjCMethodParameter()) {
2930 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
2931 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
2932 } else {
2933 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
2934 D->getFunctionScopeIndex());
2935 }
2936
Sean Callanan59721b32015-04-28 18:41:46 +00002937 if (D->isUsed())
2938 ToParm->setIsUsed();
2939
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002940 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002941}
2942
Douglas Gregor43f54792010-02-17 02:12:47 +00002943Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2944 // Import the major distinguishing characteristics of a method.
2945 DeclContext *DC, *LexicalDC;
2946 DeclarationName Name;
2947 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002948 NamedDecl *ToD;
2949 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002950 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002951 if (ToD)
2952 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002953
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002954 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002955 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002956 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2957 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002958 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2959 continue;
2960
2961 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00002962 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
2963 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002964 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00002965 << D->isInstanceMethod() << Name << D->getReturnType()
2966 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00002967 Importer.ToDiag(FoundMethod->getLocation(),
2968 diag::note_odr_objc_method_here)
2969 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002970 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002971 }
2972
2973 // Check the number of parameters.
2974 if (D->param_size() != FoundMethod->param_size()) {
2975 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2976 << D->isInstanceMethod() << Name
2977 << D->param_size() << FoundMethod->param_size();
2978 Importer.ToDiag(FoundMethod->getLocation(),
2979 diag::note_odr_objc_method_here)
2980 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002981 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002982 }
2983
2984 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002985 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002986 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2987 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002988 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002989 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002990 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002991 diag::err_odr_objc_method_param_type_inconsistent)
2992 << D->isInstanceMethod() << Name
2993 << (*P)->getType() << (*FoundP)->getType();
2994 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2995 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002996 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002997 }
2998 }
2999
3000 // Check variadic/non-variadic.
3001 // Check the number of parameters.
3002 if (D->isVariadic() != FoundMethod->isVariadic()) {
3003 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3004 << D->isInstanceMethod() << Name;
3005 Importer.ToDiag(FoundMethod->getLocation(),
3006 diag::note_odr_objc_method_here)
3007 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003008 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003009 }
3010
3011 // FIXME: Any other bits we need to merge?
3012 return Importer.Imported(D, FoundMethod);
3013 }
3014 }
3015
3016 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003017 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003018 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003019 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003020
Alp Toker314cc812014-01-25 16:55:45 +00003021 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003022
Alp Toker314cc812014-01-25 16:55:45 +00003023 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3024 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3025 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3026 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3027 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003028
3029 // FIXME: When we decide to merge method definitions, we'll need to
3030 // deal with implicit parameters.
3031
3032 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003033 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003034 for (auto *FromP : D->parameters()) {
Aaron Ballman43b68be2014-03-07 17:50:17 +00003035 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003036 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003037 return nullptr;
3038
Douglas Gregor43f54792010-02-17 02:12:47 +00003039 ToParams.push_back(ToP);
3040 }
3041
3042 // Set the parameters.
3043 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3044 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003045 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003046 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003047
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003048 SmallVector<SourceLocation, 12> SelLocs;
3049 D->getSelectorLocs(SelLocs);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003050 for (SourceLocation &Loc : SelLocs)
3051 Loc = Importer.Import(Loc);
3052
3053 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003054
3055 ToMethod->setLexicalDeclContext(LexicalDC);
3056 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003057 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003058 return ToMethod;
3059}
3060
Douglas Gregor85f3f952015-07-07 03:57:15 +00003061Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3062 // Import the major distinguishing characteristics of a category.
3063 DeclContext *DC, *LexicalDC;
3064 DeclarationName Name;
3065 SourceLocation Loc;
3066 NamedDecl *ToD;
3067 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3068 return nullptr;
3069 if (ToD)
3070 return ToD;
3071
3072 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3073 if (!BoundInfo)
3074 return nullptr;
3075
3076 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3077 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003078 D->getVariance(),
3079 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003080 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003081 Importer.Import(D->getLocation()),
3082 Name.getAsIdentifierInfo(),
3083 Importer.Import(D->getColonLoc()),
3084 BoundInfo);
3085 Importer.Imported(D, Result);
3086 Result->setLexicalDeclContext(LexicalDC);
3087 return Result;
3088}
3089
Douglas Gregor84c51c32010-02-18 01:47:50 +00003090Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3091 // Import the major distinguishing characteristics of a category.
3092 DeclContext *DC, *LexicalDC;
3093 DeclarationName Name;
3094 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003095 NamedDecl *ToD;
3096 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003097 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003098 if (ToD)
3099 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003100
Douglas Gregor84c51c32010-02-18 01:47:50 +00003101 ObjCInterfaceDecl *ToInterface
3102 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3103 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003104 return nullptr;
3105
Douglas Gregor84c51c32010-02-18 01:47:50 +00003106 // Determine if we've already encountered this category.
3107 ObjCCategoryDecl *MergeWithCategory
3108 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3109 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3110 if (!ToCategory) {
3111 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003112 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003113 Loc,
3114 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003115 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003116 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003117 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003118 Importer.Import(D->getIvarLBraceLoc()),
3119 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003120 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003121 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003122 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003123 // Import the type parameter list after calling Imported, to avoid
3124 // loops when bringing in their DeclContext.
3125 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3126 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003127
Douglas Gregor84c51c32010-02-18 01:47:50 +00003128 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003129 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3130 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003131 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3132 = D->protocol_loc_begin();
3133 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3134 FromProtoEnd = D->protocol_end();
3135 FromProto != FromProtoEnd;
3136 ++FromProto, ++FromProtoLoc) {
3137 ObjCProtocolDecl *ToProto
3138 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3139 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003140 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003141 Protocols.push_back(ToProto);
3142 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3143 }
3144
3145 // FIXME: If we're merging, make sure that the protocol list is the same.
3146 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3147 ProtocolLocs.data(), Importer.getToContext());
3148
3149 } else {
3150 Importer.Imported(D, ToCategory);
3151 }
3152
3153 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003154 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003155
3156 // If we have an implementation, import it as well.
3157 if (D->getImplementation()) {
3158 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003159 = cast_or_null<ObjCCategoryImplDecl>(
3160 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003161 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003162 return nullptr;
3163
Douglas Gregor84c51c32010-02-18 01:47:50 +00003164 ToCategory->setImplementation(Impl);
3165 }
3166
3167 return ToCategory;
3168}
3169
Douglas Gregor2aa53772012-01-24 17:42:07 +00003170bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3171 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003172 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003173 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003174 if (shouldForceImportDeclContext(Kind))
3175 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003176 return false;
3177 }
3178
3179 // Start the protocol definition
3180 To->startDefinition();
3181
3182 // Import protocols
3183 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3184 SmallVector<SourceLocation, 4> ProtocolLocs;
3185 ObjCProtocolDecl::protocol_loc_iterator
3186 FromProtoLoc = From->protocol_loc_begin();
3187 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3188 FromProtoEnd = From->protocol_end();
3189 FromProto != FromProtoEnd;
3190 ++FromProto, ++FromProtoLoc) {
3191 ObjCProtocolDecl *ToProto
3192 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3193 if (!ToProto)
3194 return true;
3195 Protocols.push_back(ToProto);
3196 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3197 }
3198
3199 // FIXME: If we're merging, make sure that the protocol list is the same.
3200 To->setProtocolList(Protocols.data(), Protocols.size(),
3201 ProtocolLocs.data(), Importer.getToContext());
3202
Douglas Gregor2e15c842012-02-01 21:00:38 +00003203 if (shouldForceImportDeclContext(Kind)) {
3204 // Import all of the members of this protocol.
3205 ImportDeclContext(From, /*ForceImport=*/true);
3206 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003207 return false;
3208}
3209
Douglas Gregor98d156a2010-02-17 16:12:00 +00003210Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003211 // If this protocol has a definition in the translation unit we're coming
3212 // from, but this particular declaration is not that definition, import the
3213 // definition and map to that.
3214 ObjCProtocolDecl *Definition = D->getDefinition();
3215 if (Definition && Definition != D) {
3216 Decl *ImportedDef = Importer.Import(Definition);
3217 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003218 return nullptr;
3219
Douglas Gregor2aa53772012-01-24 17:42:07 +00003220 return Importer.Imported(D, ImportedDef);
3221 }
3222
Douglas Gregor84c51c32010-02-18 01:47:50 +00003223 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003224 DeclContext *DC, *LexicalDC;
3225 DeclarationName Name;
3226 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003227 NamedDecl *ToD;
3228 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003229 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003230 if (ToD)
3231 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003232
Craig Topper36250ad2014-05-12 05:36:57 +00003233 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003234 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003235 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003236 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3237 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003238 continue;
3239
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003240 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003241 break;
3242 }
3243
3244 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003245 if (!ToProto) {
3246 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3247 Name.getAsIdentifierInfo(), Loc,
3248 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003249 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003250 ToProto->setLexicalDeclContext(LexicalDC);
3251 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003252 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003253
3254 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003255
Douglas Gregor2aa53772012-01-24 17:42:07 +00003256 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003257 return nullptr;
3258
Douglas Gregor98d156a2010-02-17 16:12:00 +00003259 return ToProto;
3260}
3261
Sean Callanan0aae0412014-12-10 00:00:37 +00003262Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3263 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3264 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3265
3266 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3267 SourceLocation LangLoc = Importer.Import(D->getLocation());
3268
3269 bool HasBraces = D->hasBraces();
3270
Sean Callananb12a8552014-12-10 21:22:20 +00003271 LinkageSpecDecl *ToLinkageSpec =
3272 LinkageSpecDecl::Create(Importer.getToContext(),
3273 DC,
3274 ExternLoc,
3275 LangLoc,
3276 D->getLanguage(),
3277 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003278
3279 if (HasBraces) {
3280 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3281 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3282 }
3283
3284 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3285 LexicalDC->addDeclInternal(ToLinkageSpec);
3286
3287 Importer.Imported(D, ToLinkageSpec);
3288
3289 return ToLinkageSpec;
3290}
3291
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003292Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
3293 DeclContext *DC, *LexicalDC;
3294 DeclarationName Name;
3295 SourceLocation Loc;
3296 NamedDecl *ToD = nullptr;
3297 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3298 return nullptr;
3299 if (ToD)
3300 return ToD;
3301
3302 DeclarationNameInfo NameInfo(Name,
3303 Importer.Import(D->getNameInfo().getLoc()));
3304 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3305
3306 UsingDecl *ToUsing = UsingDecl::Create(Importer.getToContext(), DC,
3307 Importer.Import(D->getUsingLoc()),
3308 Importer.Import(D->getQualifierLoc()),
3309 NameInfo, D->hasTypename());
3310 ToUsing->setLexicalDeclContext(LexicalDC);
3311 LexicalDC->addDeclInternal(ToUsing);
3312 Importer.Imported(D, ToUsing);
3313
3314 if (NamedDecl *FromPattern =
3315 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
3316 if (NamedDecl *ToPattern =
3317 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern)))
3318 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern);
3319 else
3320 return nullptr;
3321 }
3322
3323 for (UsingShadowDecl *FromShadow : D->shadows()) {
3324 if (UsingShadowDecl *ToShadow =
3325 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow)))
3326 ToUsing->addShadowDecl(ToShadow);
3327 else
3328 // FIXME: We return a nullptr here but the definition is already created
3329 // and available with lookups. How to fix this?..
3330 return nullptr;
3331 }
3332 return ToUsing;
3333}
3334
3335Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
3336 DeclContext *DC, *LexicalDC;
3337 DeclarationName Name;
3338 SourceLocation Loc;
3339 NamedDecl *ToD = nullptr;
3340 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3341 return nullptr;
3342 if (ToD)
3343 return ToD;
3344
3345 UsingDecl *ToUsing = dyn_cast_or_null<UsingDecl>(
3346 Importer.Import(D->getUsingDecl()));
3347 if (!ToUsing)
3348 return nullptr;
3349
3350 NamedDecl *ToTarget = dyn_cast_or_null<NamedDecl>(
3351 Importer.Import(D->getTargetDecl()));
3352 if (!ToTarget)
3353 return nullptr;
3354
3355 UsingShadowDecl *ToShadow = UsingShadowDecl::Create(
3356 Importer.getToContext(), DC, Loc, ToUsing, ToTarget);
3357
3358 ToShadow->setLexicalDeclContext(LexicalDC);
3359 ToShadow->setAccess(D->getAccess());
3360 Importer.Imported(D, ToShadow);
3361
3362 if (UsingShadowDecl *FromPattern =
3363 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
3364 if (UsingShadowDecl *ToPattern =
3365 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern)))
3366 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow,
3367 ToPattern);
3368 else
3369 // FIXME: We return a nullptr here but the definition is already created
3370 // and available with lookups. How to fix this?..
3371 return nullptr;
3372 }
3373
3374 LexicalDC->addDeclInternal(ToShadow);
3375
3376 return ToShadow;
3377}
3378
3379
3380Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
3381 DeclContext *DC, *LexicalDC;
3382 DeclarationName Name;
3383 SourceLocation Loc;
3384 NamedDecl *ToD = nullptr;
3385 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3386 return nullptr;
3387 if (ToD)
3388 return ToD;
3389
3390 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor());
3391 if (!ToComAncestor)
3392 return nullptr;
3393
3394 NamespaceDecl *ToNominated = cast_or_null<NamespaceDecl>(
3395 Importer.Import(D->getNominatedNamespace()));
3396 if (!ToNominated)
3397 return nullptr;
3398
3399 UsingDirectiveDecl *ToUsingDir = UsingDirectiveDecl::Create(
3400 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3401 Importer.Import(D->getNamespaceKeyLocation()),
3402 Importer.Import(D->getQualifierLoc()),
3403 Importer.Import(D->getIdentLocation()), ToNominated, ToComAncestor);
3404 ToUsingDir->setLexicalDeclContext(LexicalDC);
3405 LexicalDC->addDeclInternal(ToUsingDir);
3406 Importer.Imported(D, ToUsingDir);
3407
3408 return ToUsingDir;
3409}
3410
3411Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl(
3412 UnresolvedUsingValueDecl *D) {
3413 DeclContext *DC, *LexicalDC;
3414 DeclarationName Name;
3415 SourceLocation Loc;
3416 NamedDecl *ToD = nullptr;
3417 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3418 return nullptr;
3419 if (ToD)
3420 return ToD;
3421
3422 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc()));
3423 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3424
3425 UnresolvedUsingValueDecl *ToUsingValue = UnresolvedUsingValueDecl::Create(
3426 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3427 Importer.Import(D->getQualifierLoc()), NameInfo,
3428 Importer.Import(D->getEllipsisLoc()));
3429
3430 Importer.Imported(D, ToUsingValue);
3431 ToUsingValue->setAccess(D->getAccess());
3432 ToUsingValue->setLexicalDeclContext(LexicalDC);
3433 LexicalDC->addDeclInternal(ToUsingValue);
3434
3435 return ToUsingValue;
3436}
3437
3438Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
3439 UnresolvedUsingTypenameDecl *D) {
3440 DeclContext *DC, *LexicalDC;
3441 DeclarationName Name;
3442 SourceLocation Loc;
3443 NamedDecl *ToD = nullptr;
3444 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3445 return nullptr;
3446 if (ToD)
3447 return ToD;
3448
3449 UnresolvedUsingTypenameDecl *ToUsing = UnresolvedUsingTypenameDecl::Create(
3450 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3451 Importer.Import(D->getTypenameLoc()),
3452 Importer.Import(D->getQualifierLoc()), Loc, Name,
3453 Importer.Import(D->getEllipsisLoc()));
3454
3455 Importer.Imported(D, ToUsing);
3456 ToUsing->setAccess(D->getAccess());
3457 ToUsing->setLexicalDeclContext(LexicalDC);
3458 LexicalDC->addDeclInternal(ToUsing);
3459
3460 return ToUsing;
3461}
3462
3463
Douglas Gregor2aa53772012-01-24 17:42:07 +00003464bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3465 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003466 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003467 if (To->getDefinition()) {
3468 // Check consistency of superclass.
3469 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3470 if (FromSuper) {
3471 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3472 if (!FromSuper)
3473 return true;
3474 }
3475
3476 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3477 if ((bool)FromSuper != (bool)ToSuper ||
3478 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3479 Importer.ToDiag(To->getLocation(),
3480 diag::err_odr_objc_superclass_inconsistent)
3481 << To->getDeclName();
3482 if (ToSuper)
3483 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3484 << To->getSuperClass()->getDeclName();
3485 else
3486 Importer.ToDiag(To->getLocation(),
3487 diag::note_odr_objc_missing_superclass);
3488 if (From->getSuperClass())
3489 Importer.FromDiag(From->getSuperClassLoc(),
3490 diag::note_odr_objc_superclass)
3491 << From->getSuperClass()->getDeclName();
3492 else
3493 Importer.FromDiag(From->getLocation(),
3494 diag::note_odr_objc_missing_superclass);
3495 }
3496
Douglas Gregor2e15c842012-02-01 21:00:38 +00003497 if (shouldForceImportDeclContext(Kind))
3498 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003499 return false;
3500 }
3501
3502 // Start the definition.
3503 To->startDefinition();
3504
3505 // If this class has a superclass, import it.
3506 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003507 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3508 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003509 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003510
3511 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003512 }
3513
3514 // Import protocols
3515 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3516 SmallVector<SourceLocation, 4> ProtocolLocs;
3517 ObjCInterfaceDecl::protocol_loc_iterator
3518 FromProtoLoc = From->protocol_loc_begin();
3519
3520 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3521 FromProtoEnd = From->protocol_end();
3522 FromProto != FromProtoEnd;
3523 ++FromProto, ++FromProtoLoc) {
3524 ObjCProtocolDecl *ToProto
3525 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3526 if (!ToProto)
3527 return true;
3528 Protocols.push_back(ToProto);
3529 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3530 }
3531
3532 // FIXME: If we're merging, make sure that the protocol list is the same.
3533 To->setProtocolList(Protocols.data(), Protocols.size(),
3534 ProtocolLocs.data(), Importer.getToContext());
3535
3536 // Import categories. When the categories themselves are imported, they'll
3537 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003538 for (auto *Cat : From->known_categories())
3539 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003540
Douglas Gregor2aa53772012-01-24 17:42:07 +00003541 // If we have an @implementation, import it as well.
3542 if (From->getImplementation()) {
3543 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3544 Importer.Import(From->getImplementation()));
3545 if (!Impl)
3546 return true;
3547
3548 To->setImplementation(Impl);
3549 }
3550
Douglas Gregor2e15c842012-02-01 21:00:38 +00003551 if (shouldForceImportDeclContext(Kind)) {
3552 // Import all of the members of this class.
3553 ImportDeclContext(From, /*ForceImport=*/true);
3554 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003555 return false;
3556}
3557
Douglas Gregor85f3f952015-07-07 03:57:15 +00003558ObjCTypeParamList *
3559ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3560 if (!list)
3561 return nullptr;
3562
3563 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3564 for (auto fromTypeParam : *list) {
3565 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3566 Importer.Import(fromTypeParam));
3567 if (!toTypeParam)
3568 return nullptr;
3569
3570 toTypeParams.push_back(toTypeParam);
3571 }
3572
3573 return ObjCTypeParamList::create(Importer.getToContext(),
3574 Importer.Import(list->getLAngleLoc()),
3575 toTypeParams,
3576 Importer.Import(list->getRAngleLoc()));
3577}
3578
Douglas Gregor45635322010-02-16 01:20:57 +00003579Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003580 // If this class has a definition in the translation unit we're coming from,
3581 // but this particular declaration is not that definition, import the
3582 // definition and map to that.
3583 ObjCInterfaceDecl *Definition = D->getDefinition();
3584 if (Definition && Definition != D) {
3585 Decl *ImportedDef = Importer.Import(Definition);
3586 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003587 return nullptr;
3588
Douglas Gregor2aa53772012-01-24 17:42:07 +00003589 return Importer.Imported(D, ImportedDef);
3590 }
3591
Douglas Gregor45635322010-02-16 01:20:57 +00003592 // Import the major distinguishing characteristics of an @interface.
3593 DeclContext *DC, *LexicalDC;
3594 DeclarationName Name;
3595 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003596 NamedDecl *ToD;
3597 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003598 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003599 if (ToD)
3600 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003601
Douglas Gregor2aa53772012-01-24 17:42:07 +00003602 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003603 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003604 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003605 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003606 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3607 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003608 continue;
3609
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003610 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003611 break;
3612 }
3613
Douglas Gregor2aa53772012-01-24 17:42:07 +00003614 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003615 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003616 if (!ToIface) {
3617 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3618 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003619 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003620 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00003621 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003622 D->isImplicitInterfaceDecl());
3623 ToIface->setLexicalDeclContext(LexicalDC);
3624 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003625 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003626 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003627 // Import the type parameter list after calling Imported, to avoid
3628 // loops when bringing in their DeclContext.
3629 ToIface->setTypeParamList(ImportObjCTypeParamList(
3630 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003631
Douglas Gregor2aa53772012-01-24 17:42:07 +00003632 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003633 return nullptr;
3634
Douglas Gregor98d156a2010-02-17 16:12:00 +00003635 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003636}
3637
Douglas Gregor4da9d682010-12-07 15:32:12 +00003638Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3639 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3640 Importer.Import(D->getCategoryDecl()));
3641 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003642 return nullptr;
3643
Douglas Gregor4da9d682010-12-07 15:32:12 +00003644 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3645 if (!ToImpl) {
3646 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3647 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003648 return nullptr;
3649
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003650 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003651 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003652 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003653 Category->getClassInterface(),
3654 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003655 Importer.Import(D->getAtStartLoc()),
3656 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003657
3658 DeclContext *LexicalDC = DC;
3659 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3660 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3661 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003662 return nullptr;
3663
Douglas Gregor4da9d682010-12-07 15:32:12 +00003664 ToImpl->setLexicalDeclContext(LexicalDC);
3665 }
3666
Sean Callanan95e74be2011-10-21 02:57:43 +00003667 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003668 Category->setImplementation(ToImpl);
3669 }
3670
3671 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003672 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003673 return ToImpl;
3674}
3675
Douglas Gregorda8025c2010-12-07 01:26:03 +00003676Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3677 // Find the corresponding interface.
3678 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3679 Importer.Import(D->getClassInterface()));
3680 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003681 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003682
3683 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003684 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003685 if (D->getSuperClass()) {
3686 Super = cast_or_null<ObjCInterfaceDecl>(
3687 Importer.Import(D->getSuperClass()));
3688 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003689 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003690 }
3691
3692 ObjCImplementationDecl *Impl = Iface->getImplementation();
3693 if (!Impl) {
3694 // We haven't imported an implementation yet. Create a new @implementation
3695 // now.
3696 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3697 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003698 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003699 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003700 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003701 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003702 Importer.Import(D->getIvarLBraceLoc()),
3703 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003704
3705 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3706 DeclContext *LexicalDC
3707 = Importer.ImportContext(D->getLexicalDeclContext());
3708 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003709 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003710 Impl->setLexicalDeclContext(LexicalDC);
3711 }
3712
3713 // Associate the implementation with the class it implements.
3714 Iface->setImplementation(Impl);
3715 Importer.Imported(D, Iface->getImplementation());
3716 } else {
3717 Importer.Imported(D, Iface->getImplementation());
3718
3719 // Verify that the existing @implementation has the same superclass.
3720 if ((Super && !Impl->getSuperClass()) ||
3721 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003722 (Super && Impl->getSuperClass() &&
3723 !declaresSameEntity(Super->getCanonicalDecl(),
3724 Impl->getSuperClass()))) {
3725 Importer.ToDiag(Impl->getLocation(),
3726 diag::err_odr_objc_superclass_inconsistent)
3727 << Iface->getDeclName();
3728 // FIXME: It would be nice to have the location of the superclass
3729 // below.
3730 if (Impl->getSuperClass())
3731 Importer.ToDiag(Impl->getLocation(),
3732 diag::note_odr_objc_superclass)
3733 << Impl->getSuperClass()->getDeclName();
3734 else
3735 Importer.ToDiag(Impl->getLocation(),
3736 diag::note_odr_objc_missing_superclass);
3737 if (D->getSuperClass())
3738 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003739 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003740 << D->getSuperClass()->getDeclName();
3741 else
3742 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003743 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003744 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003745 }
3746 }
3747
3748 // Import all of the members of this @implementation.
3749 ImportDeclContext(D);
3750
3751 return Impl;
3752}
3753
Douglas Gregora11c4582010-02-17 18:02:10 +00003754Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3755 // Import the major distinguishing characteristics of an @property.
3756 DeclContext *DC, *LexicalDC;
3757 DeclarationName Name;
3758 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003759 NamedDecl *ToD;
3760 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003761 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003762 if (ToD)
3763 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00003764
3765 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003766 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003767 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003768 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003769 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003770 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003771 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003772 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00003773 FoundProp->getType())) {
3774 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3775 << Name << D->getType() << FoundProp->getType();
3776 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3777 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003778 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003779 }
3780
3781 // FIXME: Check property attributes, getters, setters, etc.?
3782
3783 // Consider these properties to be equivalent.
3784 Importer.Imported(D, FoundProp);
3785 return FoundProp;
3786 }
3787 }
3788
3789 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00003790 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3791 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00003792 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003793
3794 // Create the new property.
3795 ObjCPropertyDecl *ToProperty
3796 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3797 Name.getAsIdentifierInfo(),
3798 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003799 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00003800 Importer.Import(D->getType()),
3801 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00003802 D->getPropertyImplementation());
3803 Importer.Imported(D, ToProperty);
3804 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003805 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003806
3807 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003808 ToProperty->setPropertyAttributesAsWritten(
3809 D->getPropertyAttributesAsWritten());
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +00003810 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
3811 Importer.Import(D->getGetterNameLoc()));
3812 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
3813 Importer.Import(D->getSetterNameLoc()));
Douglas Gregora11c4582010-02-17 18:02:10 +00003814 ToProperty->setGetterMethodDecl(
3815 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3816 ToProperty->setSetterMethodDecl(
3817 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3818 ToProperty->setPropertyIvarDecl(
3819 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3820 return ToProperty;
3821}
3822
Douglas Gregor14a49e22010-12-07 18:32:03 +00003823Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3824 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3825 Importer.Import(D->getPropertyDecl()));
3826 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00003827 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003828
3829 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3830 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003831 return nullptr;
3832
Douglas Gregor14a49e22010-12-07 18:32:03 +00003833 // Import the lexical declaration context.
3834 DeclContext *LexicalDC = DC;
3835 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3836 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3837 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003838 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003839 }
3840
3841 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3842 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00003843 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003844
3845 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00003846 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003847 if (D->getPropertyIvarDecl()) {
3848 Ivar = cast_or_null<ObjCIvarDecl>(
3849 Importer.Import(D->getPropertyIvarDecl()));
3850 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00003851 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003852 }
3853
3854 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00003855 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
3856 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00003857 if (!ToImpl) {
3858 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3859 Importer.Import(D->getLocStart()),
3860 Importer.Import(D->getLocation()),
3861 Property,
3862 D->getPropertyImplementation(),
3863 Ivar,
3864 Importer.Import(D->getPropertyIvarDeclLoc()));
3865 ToImpl->setLexicalDeclContext(LexicalDC);
3866 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003867 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003868 } else {
3869 // Check that we have the same kind of property implementation (@synthesize
3870 // vs. @dynamic).
3871 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3872 Importer.ToDiag(ToImpl->getLocation(),
3873 diag::err_odr_objc_property_impl_kind_inconsistent)
3874 << Property->getDeclName()
3875 << (ToImpl->getPropertyImplementation()
3876 == ObjCPropertyImplDecl::Dynamic);
3877 Importer.FromDiag(D->getLocation(),
3878 diag::note_odr_objc_property_impl_kind)
3879 << D->getPropertyDecl()->getDeclName()
3880 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00003881 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003882 }
3883
3884 // For @synthesize, check that we have the same
3885 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3886 Ivar != ToImpl->getPropertyIvarDecl()) {
3887 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3888 diag::err_odr_objc_synthesize_ivar_inconsistent)
3889 << Property->getDeclName()
3890 << ToImpl->getPropertyIvarDecl()->getDeclName()
3891 << Ivar->getDeclName();
3892 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3893 diag::note_odr_objc_synthesize_ivar_here)
3894 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00003895 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003896 }
3897
3898 // Merge the existing implementation with the new implementation.
3899 Importer.Imported(D, ToImpl);
3900 }
3901
3902 return ToImpl;
3903}
3904
Douglas Gregora082a492010-11-30 19:14:50 +00003905Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3906 // For template arguments, we adopt the translation unit as our declaration
3907 // context. This context will be fixed when the actual template declaration
3908 // is created.
3909
3910 // FIXME: Import default argument.
3911 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3912 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003913 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003914 Importer.Import(D->getLocation()),
3915 D->getDepth(),
3916 D->getIndex(),
3917 Importer.Import(D->getIdentifier()),
3918 D->wasDeclaredWithTypename(),
3919 D->isParameterPack());
3920}
3921
3922Decl *
3923ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3924 // Import the name of this declaration.
3925 DeclarationName Name = Importer.Import(D->getDeclName());
3926 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003927 return nullptr;
3928
Douglas Gregora082a492010-11-30 19:14:50 +00003929 // Import the location of this declaration.
3930 SourceLocation Loc = Importer.Import(D->getLocation());
3931
3932 // Import the type of this declaration.
3933 QualType T = Importer.Import(D->getType());
3934 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003935 return nullptr;
3936
Douglas Gregora082a492010-11-30 19:14:50 +00003937 // Import type-source information.
3938 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3939 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00003940 return nullptr;
3941
Douglas Gregora082a492010-11-30 19:14:50 +00003942 // FIXME: Import default argument.
3943
3944 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3945 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003946 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003947 Loc, D->getDepth(), D->getPosition(),
3948 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003949 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003950}
3951
3952Decl *
3953ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3954 // Import the name of this declaration.
3955 DeclarationName Name = Importer.Import(D->getDeclName());
3956 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003957 return nullptr;
3958
Douglas Gregora082a492010-11-30 19:14:50 +00003959 // Import the location of this declaration.
3960 SourceLocation Loc = Importer.Import(D->getLocation());
3961
3962 // Import template parameters.
3963 TemplateParameterList *TemplateParams
3964 = ImportTemplateParameterList(D->getTemplateParameters());
3965 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00003966 return nullptr;
3967
Douglas Gregora082a492010-11-30 19:14:50 +00003968 // FIXME: Import default argument.
3969
3970 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3971 Importer.getToContext().getTranslationUnitDecl(),
3972 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003973 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003974 Name.getAsIdentifierInfo(),
3975 TemplateParams);
3976}
3977
3978Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3979 // If this record has a definition in the translation unit we're coming from,
3980 // but this particular declaration is not that definition, import the
3981 // definition and map to that.
3982 CXXRecordDecl *Definition
3983 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3984 if (Definition && Definition != D->getTemplatedDecl()) {
3985 Decl *ImportedDef
3986 = Importer.Import(Definition->getDescribedClassTemplate());
3987 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003988 return nullptr;
3989
Douglas Gregora082a492010-11-30 19:14:50 +00003990 return Importer.Imported(D, ImportedDef);
3991 }
3992
3993 // Import the major distinguishing characteristics of this class template.
3994 DeclContext *DC, *LexicalDC;
3995 DeclarationName Name;
3996 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003997 NamedDecl *ToD;
3998 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003999 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004000 if (ToD)
4001 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004002
Douglas Gregora082a492010-11-30 19:14:50 +00004003 // We may already have a template of the same name; try to find and match it.
4004 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004005 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004006 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004007 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004008 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4009 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004010 continue;
4011
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004012 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00004013 if (ClassTemplateDecl *FoundTemplate
4014 = dyn_cast<ClassTemplateDecl>(Found)) {
4015 if (IsStructuralMatch(D, FoundTemplate)) {
4016 // The class templates structurally match; call it the same template.
4017 // FIXME: We may be filling in a forward declaration here. Handle
4018 // this case!
4019 Importer.Imported(D->getTemplatedDecl(),
4020 FoundTemplate->getTemplatedDecl());
4021 return Importer.Imported(D, FoundTemplate);
4022 }
4023 }
4024
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004025 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00004026 }
4027
4028 if (!ConflictingDecls.empty()) {
4029 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4030 ConflictingDecls.data(),
4031 ConflictingDecls.size());
4032 }
4033
4034 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004035 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004036 }
4037
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004038 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4039
Douglas Gregora082a492010-11-30 19:14:50 +00004040 // Create the declaration that is being templated.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004041 CXXRecordDecl *ToTemplated = cast_or_null<CXXRecordDecl>(
4042 Importer.Import(FromTemplated));
4043 if (!ToTemplated)
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004044 return nullptr;
4045
4046 // Resolve possible cyclic import.
4047 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
4048 return AlreadyImported;
4049
Douglas Gregora082a492010-11-30 19:14:50 +00004050 // Create the class template declaration itself.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004051 TemplateParameterList *TemplateParams =
4052 ImportTemplateParameterList(D->getTemplateParameters());
Douglas Gregora082a492010-11-30 19:14:50 +00004053 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004054 return nullptr;
4055
Douglas Gregora082a492010-11-30 19:14:50 +00004056 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4057 Loc, Name, TemplateParams,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004058 ToTemplated);
4059 ToTemplated->setDescribedClassTemplate(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004060
4061 D2->setAccess(D->getAccess());
4062 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004063 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004064
4065 // Note the relationship between the class templates.
4066 Importer.Imported(D, D2);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004067 Importer.Imported(FromTemplated, ToTemplated);
Douglas Gregora082a492010-11-30 19:14:50 +00004068
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004069 if (FromTemplated->isCompleteDefinition() &&
4070 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004071 // FIXME: Import definition!
4072 }
4073
4074 return D2;
4075}
4076
Douglas Gregore2e50d332010-12-01 01:36:18 +00004077Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4078 ClassTemplateSpecializationDecl *D) {
4079 // If this record has a definition in the translation unit we're coming from,
4080 // but this particular declaration is not that definition, import the
4081 // definition and map to that.
4082 TagDecl *Definition = D->getDefinition();
4083 if (Definition && Definition != D) {
4084 Decl *ImportedDef = Importer.Import(Definition);
4085 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004086 return nullptr;
4087
Douglas Gregore2e50d332010-12-01 01:36:18 +00004088 return Importer.Imported(D, ImportedDef);
4089 }
4090
4091 ClassTemplateDecl *ClassTemplate
4092 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4093 D->getSpecializedTemplate()));
4094 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004095 return nullptr;
4096
Douglas Gregore2e50d332010-12-01 01:36:18 +00004097 // Import the context of this declaration.
4098 DeclContext *DC = ClassTemplate->getDeclContext();
4099 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004100 return nullptr;
4101
Douglas Gregore2e50d332010-12-01 01:36:18 +00004102 DeclContext *LexicalDC = DC;
4103 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4104 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4105 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004106 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004107 }
4108
4109 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004110 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4111 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004112
4113 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004114 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004115 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4116 D->getTemplateArgs().size(),
4117 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004118 return nullptr;
4119
Douglas Gregore2e50d332010-12-01 01:36:18 +00004120 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004121 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004122 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004123 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004124 if (D2) {
4125 // We already have a class template specialization with these template
4126 // arguments.
4127
4128 // FIXME: Check for specialization vs. instantiation errors.
4129
4130 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004131 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004132 // The record types structurally match, or the "from" translation
4133 // unit only had a forward declaration anyway; call it the same
4134 // function.
4135 return Importer.Imported(D, FoundDef);
4136 }
4137 }
4138 } else {
4139 // Create a new specialization.
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004140 if (ClassTemplatePartialSpecializationDecl *PartialSpec =
4141 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
4142
4143 // Import TemplateArgumentListInfo
4144 TemplateArgumentListInfo ToTAInfo;
4145 auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004146 if (ImportTemplateArgumentListInfo(ASTTemplateArgs.arguments(), ToTAInfo))
4147 return nullptr;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004148
4149 QualType CanonInjType = Importer.Import(
4150 PartialSpec->getInjectedSpecializationType());
4151 if (CanonInjType.isNull())
4152 return nullptr;
4153 CanonInjType = CanonInjType.getCanonicalType();
4154
4155 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4156 PartialSpec->getTemplateParameters());
4157 if (!ToTPList && PartialSpec->getTemplateParameters())
4158 return nullptr;
4159
4160 D2 = ClassTemplatePartialSpecializationDecl::Create(
4161 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
4162 ToTPList, ClassTemplate,
4163 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
4164 ToTAInfo, CanonInjType, nullptr);
4165
4166 } else {
4167 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4168 D->getTagKind(), DC,
4169 StartLoc, IdLoc,
4170 ClassTemplate,
4171 TemplateArgs,
4172 /*PrevDecl=*/nullptr);
4173 }
4174
Douglas Gregore2e50d332010-12-01 01:36:18 +00004175 D2->setSpecializationKind(D->getSpecializationKind());
4176
4177 // Add this specialization to the class template.
4178 ClassTemplate->AddSpecialization(D2, InsertPos);
4179
4180 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004181 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004182
4183 Importer.Imported(D, D2);
4184
4185 if (auto *TSI = D->getTypeAsWritten()) {
4186 TypeSourceInfo *TInfo = Importer.Import(TSI);
4187 if (!TInfo)
4188 return nullptr;
4189 D2->setTypeAsWritten(TInfo);
4190 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
4191 D2->setExternLoc(Importer.Import(D->getExternLoc()));
4192 }
4193
4194 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
4195 if (POI.isValid())
4196 D2->setPointOfInstantiation(POI);
4197 else if (D->getPointOfInstantiation().isValid())
4198 return nullptr;
4199
4200 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
4201
Douglas Gregore2e50d332010-12-01 01:36:18 +00004202 // Add the specialization to this context.
4203 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004204 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004205 }
4206 Importer.Imported(D, D2);
John McCallf937c022011-10-07 06:10:15 +00004207 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004208 return nullptr;
4209
Douglas Gregore2e50d332010-12-01 01:36:18 +00004210 return D2;
4211}
4212
Larisse Voufo39a1e502013-08-06 01:03:05 +00004213Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4214 // If this variable has a definition in the translation unit we're coming
4215 // from,
4216 // but this particular declaration is not that definition, import the
4217 // definition and map to that.
4218 VarDecl *Definition =
4219 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4220 if (Definition && Definition != D->getTemplatedDecl()) {
4221 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4222 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004223 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004224
4225 return Importer.Imported(D, ImportedDef);
4226 }
4227
4228 // Import the major distinguishing characteristics of this variable template.
4229 DeclContext *DC, *LexicalDC;
4230 DeclarationName Name;
4231 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004232 NamedDecl *ToD;
4233 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004234 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004235 if (ToD)
4236 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004237
4238 // We may already have a template of the same name; try to find and match it.
4239 assert(!DC->isFunctionOrMethod() &&
4240 "Variable templates cannot be declared at function scope");
4241 SmallVector<NamedDecl *, 4> ConflictingDecls;
4242 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004243 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004244 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4245 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4246 continue;
4247
4248 Decl *Found = FoundDecls[I];
4249 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4250 if (IsStructuralMatch(D, FoundTemplate)) {
4251 // The variable templates structurally match; call it the same template.
4252 Importer.Imported(D->getTemplatedDecl(),
4253 FoundTemplate->getTemplatedDecl());
4254 return Importer.Imported(D, FoundTemplate);
4255 }
4256 }
4257
4258 ConflictingDecls.push_back(FoundDecls[I]);
4259 }
4260
4261 if (!ConflictingDecls.empty()) {
4262 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4263 ConflictingDecls.data(),
4264 ConflictingDecls.size());
4265 }
4266
4267 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004268 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004269
4270 VarDecl *DTemplated = D->getTemplatedDecl();
4271
4272 // Import the type.
4273 QualType T = Importer.Import(DTemplated->getType());
4274 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004275 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004276
4277 // Create the declaration that is being templated.
4278 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4279 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4280 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4281 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4282 IdLoc, Name.getAsIdentifierInfo(), T,
4283 TInfo, DTemplated->getStorageClass());
4284 D2Templated->setAccess(DTemplated->getAccess());
4285 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4286 D2Templated->setLexicalDeclContext(LexicalDC);
4287
4288 // Importer.Imported(DTemplated, D2Templated);
4289 // LexicalDC->addDeclInternal(D2Templated);
4290
4291 // Merge the initializer.
4292 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00004293 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004294
4295 // Create the variable template declaration itself.
4296 TemplateParameterList *TemplateParams =
4297 ImportTemplateParameterList(D->getTemplateParameters());
4298 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004299 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004300
4301 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00004302 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004303 D2Templated->setDescribedVarTemplate(D2);
4304
4305 D2->setAccess(D->getAccess());
4306 D2->setLexicalDeclContext(LexicalDC);
4307 LexicalDC->addDeclInternal(D2);
4308
4309 // Note the relationship between the variable templates.
4310 Importer.Imported(D, D2);
4311 Importer.Imported(DTemplated, D2Templated);
4312
4313 if (DTemplated->isThisDeclarationADefinition() &&
4314 !D2Templated->isThisDeclarationADefinition()) {
4315 // FIXME: Import definition!
4316 }
4317
4318 return D2;
4319}
4320
4321Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4322 VarTemplateSpecializationDecl *D) {
4323 // If this record has a definition in the translation unit we're coming from,
4324 // but this particular declaration is not that definition, import the
4325 // definition and map to that.
4326 VarDecl *Definition = D->getDefinition();
4327 if (Definition && Definition != D) {
4328 Decl *ImportedDef = Importer.Import(Definition);
4329 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004330 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004331
4332 return Importer.Imported(D, ImportedDef);
4333 }
4334
4335 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4336 Importer.Import(D->getSpecializedTemplate()));
4337 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004338 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004339
4340 // Import the context of this declaration.
4341 DeclContext *DC = VarTemplate->getDeclContext();
4342 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004343 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004344
4345 DeclContext *LexicalDC = DC;
4346 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4347 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4348 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004349 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004350 }
4351
4352 // Import the location of this declaration.
4353 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4354 SourceLocation IdLoc = Importer.Import(D->getLocation());
4355
4356 // Import template arguments.
4357 SmallVector<TemplateArgument, 2> TemplateArgs;
4358 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4359 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004360 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004361
4362 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004363 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004364 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004365 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004366 if (D2) {
4367 // We already have a variable template specialization with these template
4368 // arguments.
4369
4370 // FIXME: Check for specialization vs. instantiation errors.
4371
4372 if (VarDecl *FoundDef = D2->getDefinition()) {
4373 if (!D->isThisDeclarationADefinition() ||
4374 IsStructuralMatch(D, FoundDef)) {
4375 // The record types structurally match, or the "from" translation
4376 // unit only had a forward declaration anyway; call it the same
4377 // variable.
4378 return Importer.Imported(D, FoundDef);
4379 }
4380 }
4381 } else {
4382
4383 // Import the type.
4384 QualType T = Importer.Import(D->getType());
4385 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004386 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004387 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4388
4389 // Create a new specialization.
4390 D2 = VarTemplateSpecializationDecl::Create(
4391 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00004392 D->getStorageClass(), TemplateArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004393 D2->setSpecializationKind(D->getSpecializationKind());
4394 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4395
4396 // Add this specialization to the class template.
4397 VarTemplate->AddSpecialization(D2, InsertPos);
4398
4399 // Import the qualifier, if any.
4400 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4401
4402 // Add the specialization to this context.
4403 D2->setLexicalDeclContext(LexicalDC);
4404 LexicalDC->addDeclInternal(D2);
4405 }
4406 Importer.Imported(D, D2);
4407
4408 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004409 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004410
4411 return D2;
4412}
4413
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004414Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
4415 DeclContext *DC, *LexicalDC;
4416 DeclarationName Name;
4417 SourceLocation Loc;
4418 NamedDecl *ToD;
4419
4420 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4421 return nullptr;
4422
4423 if (ToD)
4424 return ToD;
4425
4426 // Try to find a function in our own ("to") context with the same name, same
4427 // type, and in the same context as the function we're importing.
4428 if (!LexicalDC->isFunctionOrMethod()) {
4429 unsigned IDNS = Decl::IDNS_Ordinary;
4430 SmallVector<NamedDecl *, 2> FoundDecls;
4431 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
4432 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4433 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
4434 continue;
4435
4436 if (FunctionTemplateDecl *FoundFunction =
4437 dyn_cast<FunctionTemplateDecl>(FoundDecls[I])) {
4438 if (FoundFunction->hasExternalFormalLinkage() &&
4439 D->hasExternalFormalLinkage()) {
4440 if (IsStructuralMatch(D, FoundFunction)) {
4441 Importer.Imported(D, FoundFunction);
4442 // FIXME: Actually try to merge the body and other attributes.
4443 return FoundFunction;
4444 }
4445 }
4446 }
4447 }
4448 }
4449
4450 TemplateParameterList *Params =
4451 ImportTemplateParameterList(D->getTemplateParameters());
4452 if (!Params)
4453 return nullptr;
4454
4455 FunctionDecl *TemplatedFD =
4456 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl()));
4457 if (!TemplatedFD)
4458 return nullptr;
4459
4460 FunctionTemplateDecl *ToFunc = FunctionTemplateDecl::Create(
4461 Importer.getToContext(), DC, Loc, Name, Params, TemplatedFD);
4462
4463 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
4464 ToFunc->setAccess(D->getAccess());
4465 ToFunc->setLexicalDeclContext(LexicalDC);
4466 Importer.Imported(D, ToFunc);
4467
4468 LexicalDC->addDeclInternal(ToFunc);
4469 return ToFunc;
4470}
4471
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004472//----------------------------------------------------------------------------
4473// Import Statements
4474//----------------------------------------------------------------------------
4475
Sean Callanan59721b32015-04-28 18:41:46 +00004476DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4477 if (DG.isNull())
4478 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4479 size_t NumDecls = DG.end() - DG.begin();
4480 SmallVector<Decl *, 1> ToDecls(NumDecls);
4481 auto &_Importer = this->Importer;
4482 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4483 [&_Importer](Decl *D) -> Decl * {
4484 return _Importer.Import(D);
4485 });
4486 return DeclGroupRef::Create(Importer.getToContext(),
4487 ToDecls.begin(),
4488 NumDecls);
4489}
4490
4491 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4492 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4493 << S->getStmtClassName();
4494 return nullptr;
4495 }
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004496
4497
4498Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4499 SmallVector<IdentifierInfo *, 4> Names;
4500 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4501 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004502 // ToII is nullptr when no symbolic name is given for output operand
4503 // see ParseStmtAsm::ParseAsmOperandsOpt
4504 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004505 return nullptr;
4506 Names.push_back(ToII);
4507 }
4508 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4509 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004510 // ToII is nullptr when no symbolic name is given for input operand
4511 // see ParseStmtAsm::ParseAsmOperandsOpt
4512 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004513 return nullptr;
4514 Names.push_back(ToII);
4515 }
4516
4517 SmallVector<StringLiteral *, 4> Clobbers;
4518 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
4519 StringLiteral *Clobber = cast_or_null<StringLiteral>(
4520 Importer.Import(S->getClobberStringLiteral(I)));
4521 if (!Clobber)
4522 return nullptr;
4523 Clobbers.push_back(Clobber);
4524 }
4525
4526 SmallVector<StringLiteral *, 4> Constraints;
4527 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4528 StringLiteral *Output = cast_or_null<StringLiteral>(
4529 Importer.Import(S->getOutputConstraintLiteral(I)));
4530 if (!Output)
4531 return nullptr;
4532 Constraints.push_back(Output);
4533 }
4534
4535 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4536 StringLiteral *Input = cast_or_null<StringLiteral>(
4537 Importer.Import(S->getInputConstraintLiteral(I)));
4538 if (!Input)
4539 return nullptr;
4540 Constraints.push_back(Input);
4541 }
4542
4543 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004544 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004545 return nullptr;
4546
Aleksei Sidorina693b372016-09-28 10:16:56 +00004547 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004548 return nullptr;
4549
4550 StringLiteral *AsmStr = cast_or_null<StringLiteral>(
4551 Importer.Import(S->getAsmString()));
4552 if (!AsmStr)
4553 return nullptr;
4554
4555 return new (Importer.getToContext()) GCCAsmStmt(
4556 Importer.getToContext(),
4557 Importer.Import(S->getAsmLoc()),
4558 S->isSimple(),
4559 S->isVolatile(),
4560 S->getNumOutputs(),
4561 S->getNumInputs(),
4562 Names.data(),
4563 Constraints.data(),
4564 Exprs.data(),
4565 AsmStr,
4566 S->getNumClobbers(),
4567 Clobbers.data(),
4568 Importer.Import(S->getRParenLoc()));
4569}
4570
Sean Callanan59721b32015-04-28 18:41:46 +00004571Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4572 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4573 for (Decl *ToD : ToDG) {
4574 if (!ToD)
4575 return nullptr;
4576 }
4577 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4578 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4579 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4580}
4581
4582Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4583 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4584 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4585 S->hasLeadingEmptyMacro());
4586}
4587
4588Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004589 llvm::SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004590
4591 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00004592 return nullptr;
4593
Sean Callanan59721b32015-04-28 18:41:46 +00004594 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4595 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
Benjamin Kramer07420902017-12-24 16:24:20 +00004596 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
4597 ToRBraceLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004598}
4599
4600Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4601 Expr *ToLHS = Importer.Import(S->getLHS());
4602 if (!ToLHS)
4603 return nullptr;
4604 Expr *ToRHS = Importer.Import(S->getRHS());
4605 if (!ToRHS && S->getRHS())
4606 return nullptr;
Gabor Horvath480892b2017-10-18 09:25:18 +00004607 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4608 if (!ToSubStmt && S->getSubStmt())
4609 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004610 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4611 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4612 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
Gabor Horvath480892b2017-10-18 09:25:18 +00004613 CaseStmt *ToStmt = new (Importer.getToContext())
4614 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
4615 ToStmt->setSubStmt(ToSubStmt);
4616 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00004617}
4618
4619Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4620 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4621 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4622 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4623 if (!ToSubStmt && S->getSubStmt())
4624 return nullptr;
4625 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4626 ToSubStmt);
4627}
4628
4629Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4630 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4631 LabelDecl *ToLabelDecl =
4632 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4633 if (!ToLabelDecl && S->getDecl())
4634 return nullptr;
4635 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4636 if (!ToSubStmt && S->getSubStmt())
4637 return nullptr;
4638 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4639 ToSubStmt);
4640}
4641
4642Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4643 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4644 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4645 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4646 ASTContext &_ToContext = Importer.getToContext();
4647 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4648 [&_ToContext](const Attr *A) -> const Attr * {
4649 return A->clone(_ToContext);
4650 });
4651 for (const Attr *ToA : ToAttrs) {
4652 if (!ToA)
4653 return nullptr;
4654 }
4655 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4656 if (!ToSubStmt && S->getSubStmt())
4657 return nullptr;
4658 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4659 ToAttrs, ToSubStmt);
4660}
4661
4662Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4663 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00004664 Stmt *ToInit = Importer.Import(S->getInit());
4665 if (!ToInit && S->getInit())
4666 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004667 VarDecl *ToConditionVariable = nullptr;
4668 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4669 ToConditionVariable =
4670 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4671 if (!ToConditionVariable)
4672 return nullptr;
4673 }
4674 Expr *ToCondition = Importer.Import(S->getCond());
4675 if (!ToCondition && S->getCond())
4676 return nullptr;
4677 Stmt *ToThenStmt = Importer.Import(S->getThen());
4678 if (!ToThenStmt && S->getThen())
4679 return nullptr;
4680 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4681 Stmt *ToElseStmt = Importer.Import(S->getElse());
4682 if (!ToElseStmt && S->getElse())
4683 return nullptr;
4684 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00004685 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00004686 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00004687 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00004688 ToCondition, ToThenStmt,
4689 ToElseLoc, ToElseStmt);
4690}
4691
4692Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00004693 Stmt *ToInit = Importer.Import(S->getInit());
4694 if (!ToInit && S->getInit())
4695 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004696 VarDecl *ToConditionVariable = nullptr;
4697 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4698 ToConditionVariable =
4699 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4700 if (!ToConditionVariable)
4701 return nullptr;
4702 }
4703 Expr *ToCondition = Importer.Import(S->getCond());
4704 if (!ToCondition && S->getCond())
4705 return nullptr;
4706 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00004707 Importer.getToContext(), ToInit,
4708 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00004709 Stmt *ToBody = Importer.Import(S->getBody());
4710 if (!ToBody && S->getBody())
4711 return nullptr;
4712 ToStmt->setBody(ToBody);
4713 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4714 // Now we have to re-chain the cases.
4715 SwitchCase *LastChainedSwitchCase = nullptr;
4716 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4717 SC = SC->getNextSwitchCase()) {
4718 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4719 if (!ToSC)
4720 return nullptr;
4721 if (LastChainedSwitchCase)
4722 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4723 else
4724 ToStmt->setSwitchCaseList(ToSC);
4725 LastChainedSwitchCase = ToSC;
4726 }
4727 return ToStmt;
4728}
4729
4730Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4731 VarDecl *ToConditionVariable = nullptr;
4732 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4733 ToConditionVariable =
4734 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4735 if (!ToConditionVariable)
4736 return nullptr;
4737 }
4738 Expr *ToCondition = Importer.Import(S->getCond());
4739 if (!ToCondition && S->getCond())
4740 return nullptr;
4741 Stmt *ToBody = Importer.Import(S->getBody());
4742 if (!ToBody && S->getBody())
4743 return nullptr;
4744 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4745 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4746 ToConditionVariable,
4747 ToCondition, ToBody,
4748 ToWhileLoc);
4749}
4750
4751Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4752 Stmt *ToBody = Importer.Import(S->getBody());
4753 if (!ToBody && S->getBody())
4754 return nullptr;
4755 Expr *ToCondition = Importer.Import(S->getCond());
4756 if (!ToCondition && S->getCond())
4757 return nullptr;
4758 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4759 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4760 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4761 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4762 ToDoLoc, ToWhileLoc,
4763 ToRParenLoc);
4764}
4765
4766Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4767 Stmt *ToInit = Importer.Import(S->getInit());
4768 if (!ToInit && S->getInit())
4769 return nullptr;
4770 Expr *ToCondition = Importer.Import(S->getCond());
4771 if (!ToCondition && S->getCond())
4772 return nullptr;
4773 VarDecl *ToConditionVariable = nullptr;
4774 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4775 ToConditionVariable =
4776 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4777 if (!ToConditionVariable)
4778 return nullptr;
4779 }
4780 Expr *ToInc = Importer.Import(S->getInc());
4781 if (!ToInc && S->getInc())
4782 return nullptr;
4783 Stmt *ToBody = Importer.Import(S->getBody());
4784 if (!ToBody && S->getBody())
4785 return nullptr;
4786 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4787 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4788 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4789 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4790 ToInit, ToCondition,
4791 ToConditionVariable,
4792 ToInc, ToBody,
4793 ToForLoc, ToLParenLoc,
4794 ToRParenLoc);
4795}
4796
4797Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4798 LabelDecl *ToLabel = nullptr;
4799 if (LabelDecl *FromLabel = S->getLabel()) {
4800 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4801 if (!ToLabel)
4802 return nullptr;
4803 }
4804 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4805 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4806 return new (Importer.getToContext()) GotoStmt(ToLabel,
4807 ToGotoLoc, ToLabelLoc);
4808}
4809
4810Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4811 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4812 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4813 Expr *ToTarget = Importer.Import(S->getTarget());
4814 if (!ToTarget && S->getTarget())
4815 return nullptr;
4816 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4817 ToTarget);
4818}
4819
4820Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4821 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4822 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4823}
4824
4825Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4826 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4827 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4828}
4829
4830Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4831 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4832 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4833 if (!ToRetExpr && S->getRetValue())
4834 return nullptr;
4835 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4836 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4837 if (!ToNRVOCandidate && NRVOCandidate)
4838 return nullptr;
4839 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4840 ToNRVOCandidate);
4841}
4842
4843Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4844 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4845 VarDecl *ToExceptionDecl = nullptr;
4846 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4847 ToExceptionDecl =
4848 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4849 if (!ToExceptionDecl)
4850 return nullptr;
4851 }
4852 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4853 if (!ToHandlerBlock && S->getHandlerBlock())
4854 return nullptr;
4855 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4856 ToExceptionDecl,
4857 ToHandlerBlock);
4858}
4859
4860Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4861 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4862 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4863 if (!ToTryBlock && S->getTryBlock())
4864 return nullptr;
4865 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4866 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4867 CXXCatchStmt *FromHandler = S->getHandler(HI);
4868 if (Stmt *ToHandler = Importer.Import(FromHandler))
4869 ToHandlers[HI] = ToHandler;
4870 else
4871 return nullptr;
4872 }
4873 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4874 ToHandlers);
4875}
4876
4877Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4878 DeclStmt *ToRange =
4879 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4880 if (!ToRange && S->getRangeStmt())
4881 return nullptr;
Richard Smith01694c32016-03-20 10:33:40 +00004882 DeclStmt *ToBegin =
4883 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
4884 if (!ToBegin && S->getBeginStmt())
4885 return nullptr;
4886 DeclStmt *ToEnd =
4887 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
4888 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00004889 return nullptr;
4890 Expr *ToCond = Importer.Import(S->getCond());
4891 if (!ToCond && S->getCond())
4892 return nullptr;
4893 Expr *ToInc = Importer.Import(S->getInc());
4894 if (!ToInc && S->getInc())
4895 return nullptr;
4896 DeclStmt *ToLoopVar =
4897 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4898 if (!ToLoopVar && S->getLoopVarStmt())
4899 return nullptr;
4900 Stmt *ToBody = Importer.Import(S->getBody());
4901 if (!ToBody && S->getBody())
4902 return nullptr;
4903 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00004904 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00004905 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4906 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00004907 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00004908 ToCond, ToInc,
4909 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00004910 ToForLoc, ToCoawaitLoc,
4911 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004912}
4913
4914Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
4915 Stmt *ToElem = Importer.Import(S->getElement());
4916 if (!ToElem && S->getElement())
4917 return nullptr;
4918 Expr *ToCollect = Importer.Import(S->getCollection());
4919 if (!ToCollect && S->getCollection())
4920 return nullptr;
4921 Stmt *ToBody = Importer.Import(S->getBody());
4922 if (!ToBody && S->getBody())
4923 return nullptr;
4924 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4925 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4926 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
4927 ToCollect,
4928 ToBody, ToForLoc,
4929 ToRParenLoc);
4930}
4931
4932Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
4933 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
4934 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4935 VarDecl *ToExceptionDecl = nullptr;
4936 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
4937 ToExceptionDecl =
4938 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4939 if (!ToExceptionDecl)
4940 return nullptr;
4941 }
4942 Stmt *ToBody = Importer.Import(S->getCatchBody());
4943 if (!ToBody && S->getCatchBody())
4944 return nullptr;
4945 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
4946 ToRParenLoc,
4947 ToExceptionDecl,
4948 ToBody);
4949}
4950
4951Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
4952 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
4953 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
4954 if (!ToAtFinallyStmt && S->getFinallyBody())
4955 return nullptr;
4956 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
4957 ToAtFinallyStmt);
4958}
4959
4960Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
4961 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
4962 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
4963 if (!ToAtTryStmt && S->getTryBody())
4964 return nullptr;
4965 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
4966 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
4967 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
4968 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
4969 ToCatchStmts[CI] = ToCatchStmt;
4970 else
4971 return nullptr;
4972 }
4973 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
4974 if (!ToAtFinallyStmt && S->getFinallyStmt())
4975 return nullptr;
4976 return ObjCAtTryStmt::Create(Importer.getToContext(),
4977 ToAtTryLoc, ToAtTryStmt,
4978 ToCatchStmts.begin(), ToCatchStmts.size(),
4979 ToAtFinallyStmt);
4980}
4981
4982Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
4983 (ObjCAtSynchronizedStmt *S) {
4984 SourceLocation ToAtSynchronizedLoc =
4985 Importer.Import(S->getAtSynchronizedLoc());
4986 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
4987 if (!ToSynchExpr && S->getSynchExpr())
4988 return nullptr;
4989 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
4990 if (!ToSynchBody && S->getSynchBody())
4991 return nullptr;
4992 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
4993 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
4994}
4995
4996Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
4997 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
4998 Expr *ToThrow = Importer.Import(S->getThrowExpr());
4999 if (!ToThrow && S->getThrowExpr())
5000 return nullptr;
5001 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5002}
5003
5004Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5005 (ObjCAutoreleasePoolStmt *S) {
5006 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5007 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5008 if (!ToSubStmt && S->getSubStmt())
5009 return nullptr;
5010 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5011 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005012}
5013
5014//----------------------------------------------------------------------------
5015// Import Expressions
5016//----------------------------------------------------------------------------
5017Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5018 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5019 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005020 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005021}
5022
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005023Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5024 QualType T = Importer.Import(E->getType());
5025 if (T.isNull())
5026 return nullptr;
5027
5028 Expr *SubExpr = Importer.Import(E->getSubExpr());
5029 if (!SubExpr && E->getSubExpr())
5030 return nullptr;
5031
5032 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5033 if (!TInfo)
5034 return nullptr;
5035
5036 return new (Importer.getToContext()) VAArgExpr(
5037 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5038 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5039}
5040
5041
5042Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5043 QualType T = Importer.Import(E->getType());
5044 if (T.isNull())
5045 return nullptr;
5046
5047 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005048 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005049}
5050
5051Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5052 QualType T = Importer.Import(E->getType());
5053 if (T.isNull())
5054 return nullptr;
5055
5056 StringLiteral *SL = cast_or_null<StringLiteral>(
5057 Importer.Import(E->getFunctionName()));
5058 if (!SL && E->getFunctionName())
5059 return nullptr;
5060
5061 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005062 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005063}
5064
Douglas Gregor52f820e2010-02-19 01:17:02 +00005065Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00005066 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
5067 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005068 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005069
Craig Topper36250ad2014-05-12 05:36:57 +00005070 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005071 if (E->getDecl() != E->getFoundDecl()) {
5072 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5073 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005074 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005075 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005076
5077 QualType T = Importer.Import(E->getType());
5078 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005079 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005080
Aleksei Sidorina693b372016-09-28 10:16:56 +00005081
5082 TemplateArgumentListInfo ToTAInfo;
5083 TemplateArgumentListInfo *ResInfo = nullptr;
5084 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005085 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
5086 return nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00005087 ResInfo = &ToTAInfo;
5088 }
5089
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005090 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5091 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005092 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005093 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005094 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005095 Importer.Import(E->getLocation()),
5096 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005097 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005098 if (E->hadMultipleCandidates())
5099 DRE->setHadMultipleCandidates(true);
5100 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005101}
5102
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005103Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5104 QualType T = Importer.Import(E->getType());
5105 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00005106 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005107
5108 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5109}
5110
5111ASTNodeImporter::Designator
5112ASTNodeImporter::ImportDesignator(const Designator &D) {
5113 if (D.isFieldDesignator()) {
5114 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5115 // Caller checks for import error
5116 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5117 Importer.Import(D.getFieldLoc()));
5118 }
5119 if (D.isArrayDesignator())
5120 return Designator(D.getFirstExprIndex(),
5121 Importer.Import(D.getLBracketLoc()),
5122 Importer.Import(D.getRBracketLoc()));
5123
5124 assert(D.isArrayRangeDesignator());
5125 return Designator(D.getFirstExprIndex(),
5126 Importer.Import(D.getLBracketLoc()),
5127 Importer.Import(D.getEllipsisLoc()),
5128 Importer.Import(D.getRBracketLoc()));
5129}
5130
5131
5132Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
5133 Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
5134 if (!Init)
5135 return nullptr;
5136
5137 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5138 // List elements from the second, the first is Init itself
5139 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
5140 if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
5141 IndexExprs[I - 1] = Arg;
5142 else
5143 return nullptr;
5144 }
5145
5146 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005147 llvm::transform(DIE->designators(), Designators.begin(),
5148 [this](const Designator &D) -> Designator {
5149 return ImportDesignator(D);
5150 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005151
David Majnemerf7e36092016-06-23 00:15:04 +00005152 for (const Designator &D : DIE->designators())
5153 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005154 return nullptr;
5155
5156 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005157 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005158 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5159 DIE->usesGNUSyntax(), Init);
5160}
5161
5162Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5163 QualType T = Importer.Import(E->getType());
5164 if (T.isNull())
5165 return nullptr;
5166
5167 return new (Importer.getToContext())
5168 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5169}
5170
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005171Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5172 QualType T = Importer.Import(E->getType());
5173 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005174 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005175
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005176 return IntegerLiteral::Create(Importer.getToContext(),
5177 E->getValue(), T,
5178 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005179}
5180
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005181Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5182 QualType T = Importer.Import(E->getType());
5183 if (T.isNull())
5184 return nullptr;
5185
5186 return FloatingLiteral::Create(Importer.getToContext(),
5187 E->getValue(), E->isExact(), T,
5188 Importer.Import(E->getLocation()));
5189}
5190
Douglas Gregor623421d2010-02-18 02:21:22 +00005191Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5192 QualType T = Importer.Import(E->getType());
5193 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005194 return nullptr;
5195
Douglas Gregorfb65e592011-07-27 05:40:30 +00005196 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5197 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005198 Importer.Import(E->getLocation()));
5199}
5200
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005201Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5202 QualType T = Importer.Import(E->getType());
5203 if (T.isNull())
5204 return nullptr;
5205
5206 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5207 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5208
5209 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5210 E->getKind(), E->isPascal(), T,
5211 Locations.data(), Locations.size());
5212}
5213
5214Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5215 QualType T = Importer.Import(E->getType());
5216 if (T.isNull())
5217 return nullptr;
5218
5219 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5220 if (!TInfo)
5221 return nullptr;
5222
5223 Expr *Init = Importer.Import(E->getInitializer());
5224 if (!Init)
5225 return nullptr;
5226
5227 return new (Importer.getToContext()) CompoundLiteralExpr(
5228 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5229 Init, E->isFileScope());
5230}
5231
5232Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5233 QualType T = Importer.Import(E->getType());
5234 if (T.isNull())
5235 return nullptr;
5236
5237 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5238 if (ImportArrayChecked(
5239 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5240 Exprs.begin()))
5241 return nullptr;
5242
5243 return new (Importer.getToContext()) AtomicExpr(
5244 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5245 Importer.Import(E->getRParenLoc()));
5246}
5247
5248Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5249 QualType T = Importer.Import(E->getType());
5250 if (T.isNull())
5251 return nullptr;
5252
5253 LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
5254 if (!ToLabel)
5255 return nullptr;
5256
5257 return new (Importer.getToContext()) AddrLabelExpr(
5258 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5259 ToLabel, T);
5260}
5261
Douglas Gregorc74247e2010-02-19 01:07:06 +00005262Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5263 Expr *SubExpr = Importer.Import(E->getSubExpr());
5264 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005265 return nullptr;
5266
Douglas Gregorc74247e2010-02-19 01:07:06 +00005267 return new (Importer.getToContext())
5268 ParenExpr(Importer.Import(E->getLParen()),
5269 Importer.Import(E->getRParen()),
5270 SubExpr);
5271}
5272
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005273Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5274 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005275 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005276 return nullptr;
5277
5278 return new (Importer.getToContext()) ParenListExpr(
5279 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5280 Exprs, Importer.Import(E->getLParenLoc()));
5281}
5282
5283Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5284 QualType T = Importer.Import(E->getType());
5285 if (T.isNull())
5286 return nullptr;
5287
5288 CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>(
5289 Importer.Import(E->getSubStmt()));
5290 if (!ToSubStmt && E->getSubStmt())
5291 return nullptr;
5292
5293 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5294 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5295}
5296
Douglas Gregorc74247e2010-02-19 01:07:06 +00005297Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5298 QualType T = Importer.Import(E->getType());
5299 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005300 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005301
5302 Expr *SubExpr = Importer.Import(E->getSubExpr());
5303 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005304 return nullptr;
5305
Aaron Ballmana5038552018-01-09 13:07:03 +00005306 return new (Importer.getToContext()) UnaryOperator(
5307 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(),
5308 Importer.Import(E->getOperatorLoc()), E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005309}
5310
Aaron Ballmana5038552018-01-09 13:07:03 +00005311Expr *
5312ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005313 QualType ResultType = Importer.Import(E->getType());
5314
5315 if (E->isArgumentType()) {
5316 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5317 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005318 return nullptr;
5319
Peter Collingbournee190dee2011-03-11 19:24:49 +00005320 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5321 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005322 Importer.Import(E->getOperatorLoc()),
5323 Importer.Import(E->getRParenLoc()));
5324 }
5325
5326 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5327 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005328 return nullptr;
5329
Peter Collingbournee190dee2011-03-11 19:24:49 +00005330 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5331 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005332 Importer.Import(E->getOperatorLoc()),
5333 Importer.Import(E->getRParenLoc()));
5334}
5335
Douglas Gregorc74247e2010-02-19 01:07:06 +00005336Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5337 QualType T = Importer.Import(E->getType());
5338 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005339 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005340
5341 Expr *LHS = Importer.Import(E->getLHS());
5342 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005343 return nullptr;
5344
Douglas Gregorc74247e2010-02-19 01:07:06 +00005345 Expr *RHS = Importer.Import(E->getRHS());
5346 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005347 return nullptr;
5348
Douglas Gregorc74247e2010-02-19 01:07:06 +00005349 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005350 T, E->getValueKind(),
5351 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005352 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005353 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005354}
5355
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005356Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5357 QualType T = Importer.Import(E->getType());
5358 if (T.isNull())
5359 return nullptr;
5360
5361 Expr *ToLHS = Importer.Import(E->getLHS());
5362 if (!ToLHS)
5363 return nullptr;
5364
5365 Expr *ToRHS = Importer.Import(E->getRHS());
5366 if (!ToRHS)
5367 return nullptr;
5368
5369 Expr *ToCond = Importer.Import(E->getCond());
5370 if (!ToCond)
5371 return nullptr;
5372
5373 return new (Importer.getToContext()) ConditionalOperator(
5374 ToCond, Importer.Import(E->getQuestionLoc()),
5375 ToLHS, Importer.Import(E->getColonLoc()),
5376 ToRHS, T, E->getValueKind(), E->getObjectKind());
5377}
5378
5379Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5380 BinaryConditionalOperator *E) {
5381 QualType T = Importer.Import(E->getType());
5382 if (T.isNull())
5383 return nullptr;
5384
5385 Expr *Common = Importer.Import(E->getCommon());
5386 if (!Common)
5387 return nullptr;
5388
5389 Expr *Cond = Importer.Import(E->getCond());
5390 if (!Cond)
5391 return nullptr;
5392
5393 OpaqueValueExpr *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5394 Importer.Import(E->getOpaqueValue()));
5395 if (!OpaqueValue)
5396 return nullptr;
5397
5398 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5399 if (!TrueExpr)
5400 return nullptr;
5401
5402 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5403 if (!FalseExpr)
5404 return nullptr;
5405
5406 return new (Importer.getToContext()) BinaryConditionalOperator(
5407 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5408 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5409 T, E->getValueKind(), E->getObjectKind());
5410}
5411
Aleksei Sidorina693b372016-09-28 10:16:56 +00005412Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5413 QualType T = Importer.Import(E->getType());
5414 if (T.isNull())
5415 return nullptr;
5416
5417 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5418 if (!ToQueried)
5419 return nullptr;
5420
5421 Expr *Dim = Importer.Import(E->getDimensionExpression());
5422 if (!Dim && E->getDimensionExpression())
5423 return nullptr;
5424
5425 return new (Importer.getToContext()) ArrayTypeTraitExpr(
5426 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5427 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
5428}
5429
5430Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5431 QualType T = Importer.Import(E->getType());
5432 if (T.isNull())
5433 return nullptr;
5434
5435 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5436 if (!ToQueried)
5437 return nullptr;
5438
5439 return new (Importer.getToContext()) ExpressionTraitExpr(
5440 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5441 E->getValue(), Importer.Import(E->getLocEnd()), T);
5442}
5443
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005444Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5445 QualType T = Importer.Import(E->getType());
5446 if (T.isNull())
5447 return nullptr;
5448
5449 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5450 if (!SourceExpr && E->getSourceExpr())
5451 return nullptr;
5452
5453 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005454 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005455 E->getObjectKind(), SourceExpr);
5456}
5457
Aleksei Sidorina693b372016-09-28 10:16:56 +00005458Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5459 QualType T = Importer.Import(E->getType());
5460 if (T.isNull())
5461 return nullptr;
5462
5463 Expr *ToLHS = Importer.Import(E->getLHS());
5464 if (!ToLHS)
5465 return nullptr;
5466
5467 Expr *ToRHS = Importer.Import(E->getRHS());
5468 if (!ToRHS)
5469 return nullptr;
5470
5471 return new (Importer.getToContext()) ArraySubscriptExpr(
5472 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5473 Importer.Import(E->getRBracketLoc()));
5474}
5475
Douglas Gregorc74247e2010-02-19 01:07:06 +00005476Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5477 QualType T = Importer.Import(E->getType());
5478 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005479 return nullptr;
5480
Douglas Gregorc74247e2010-02-19 01:07:06 +00005481 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5482 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005483 return nullptr;
5484
Douglas Gregorc74247e2010-02-19 01:07:06 +00005485 QualType CompResultType = Importer.Import(E->getComputationResultType());
5486 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005487 return nullptr;
5488
Douglas Gregorc74247e2010-02-19 01:07:06 +00005489 Expr *LHS = Importer.Import(E->getLHS());
5490 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005491 return nullptr;
5492
Douglas Gregorc74247e2010-02-19 01:07:06 +00005493 Expr *RHS = Importer.Import(E->getRHS());
5494 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005495 return nullptr;
5496
Douglas Gregorc74247e2010-02-19 01:07:06 +00005497 return new (Importer.getToContext())
5498 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005499 T, E->getValueKind(),
5500 E->getObjectKind(),
5501 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005502 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005503 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005504}
5505
Aleksei Sidorina693b372016-09-28 10:16:56 +00005506bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
5507 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
5508 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
5509 Path.push_back(Spec);
5510 else
5511 return true;
5512 }
5513 return false;
John McCallcf142162010-08-07 06:22:56 +00005514}
5515
Douglas Gregor98c10182010-02-12 22:17:39 +00005516Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5517 QualType T = Importer.Import(E->getType());
5518 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005519 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005520
5521 Expr *SubExpr = Importer.Import(E->getSubExpr());
5522 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005523 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005524
5525 CXXCastPath BasePath;
5526 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005527 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005528
5529 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005530 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005531}
5532
Aleksei Sidorina693b372016-09-28 10:16:56 +00005533Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00005534 QualType T = Importer.Import(E->getType());
5535 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005536 return nullptr;
5537
Douglas Gregor5481d322010-02-19 01:32:14 +00005538 Expr *SubExpr = Importer.Import(E->getSubExpr());
5539 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005540 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005541
5542 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5543 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005544 return nullptr;
5545
John McCallcf142162010-08-07 06:22:56 +00005546 CXXCastPath BasePath;
5547 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005548 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005549
Aleksei Sidorina693b372016-09-28 10:16:56 +00005550 switch (E->getStmtClass()) {
5551 case Stmt::CStyleCastExprClass: {
5552 CStyleCastExpr *CCE = cast<CStyleCastExpr>(E);
5553 return CStyleCastExpr::Create(Importer.getToContext(), T,
5554 E->getValueKind(), E->getCastKind(),
5555 SubExpr, &BasePath, TInfo,
5556 Importer.Import(CCE->getLParenLoc()),
5557 Importer.Import(CCE->getRParenLoc()));
5558 }
5559
5560 case Stmt::CXXFunctionalCastExprClass: {
5561 CXXFunctionalCastExpr *FCE = cast<CXXFunctionalCastExpr>(E);
5562 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
5563 E->getValueKind(), TInfo,
5564 E->getCastKind(), SubExpr, &BasePath,
5565 Importer.Import(FCE->getLParenLoc()),
5566 Importer.Import(FCE->getRParenLoc()));
5567 }
5568
5569 case Stmt::ObjCBridgedCastExprClass: {
5570 ObjCBridgedCastExpr *OCE = cast<ObjCBridgedCastExpr>(E);
5571 return new (Importer.getToContext()) ObjCBridgedCastExpr(
5572 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
5573 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
5574 TInfo, SubExpr);
5575 }
5576 default:
5577 break; // just fall through
5578 }
5579
5580 CXXNamedCastExpr *Named = cast<CXXNamedCastExpr>(E);
5581 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
5582 RParenLoc = Importer.Import(Named->getRParenLoc());
5583 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
5584
5585 switch (E->getStmtClass()) {
5586 case Stmt::CXXStaticCastExprClass:
5587 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
5588 E->getValueKind(), E->getCastKind(),
5589 SubExpr, &BasePath, TInfo,
5590 ExprLoc, RParenLoc, Brackets);
5591
5592 case Stmt::CXXDynamicCastExprClass:
5593 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
5594 E->getValueKind(), E->getCastKind(),
5595 SubExpr, &BasePath, TInfo,
5596 ExprLoc, RParenLoc, Brackets);
5597
5598 case Stmt::CXXReinterpretCastExprClass:
5599 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
5600 E->getValueKind(), E->getCastKind(),
5601 SubExpr, &BasePath, TInfo,
5602 ExprLoc, RParenLoc, Brackets);
5603
5604 case Stmt::CXXConstCastExprClass:
5605 return CXXConstCastExpr::Create(Importer.getToContext(), T,
5606 E->getValueKind(), SubExpr, TInfo, ExprLoc,
5607 RParenLoc, Brackets);
5608 default:
5609 llvm_unreachable("Cast expression of unsupported type!");
5610 return nullptr;
5611 }
5612}
5613
5614Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
5615 QualType T = Importer.Import(OE->getType());
5616 if (T.isNull())
5617 return nullptr;
5618
5619 SmallVector<OffsetOfNode, 4> Nodes;
5620 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
5621 const OffsetOfNode &Node = OE->getComponent(I);
5622
5623 switch (Node.getKind()) {
5624 case OffsetOfNode::Array:
5625 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
5626 Node.getArrayExprIndex(),
5627 Importer.Import(Node.getLocEnd())));
5628 break;
5629
5630 case OffsetOfNode::Base: {
5631 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
5632 if (!BS && Node.getBase())
5633 return nullptr;
5634 Nodes.push_back(OffsetOfNode(BS));
5635 break;
5636 }
5637 case OffsetOfNode::Field: {
5638 FieldDecl *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
5639 if (!FD)
5640 return nullptr;
5641 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
5642 Importer.Import(Node.getLocEnd())));
5643 break;
5644 }
5645 case OffsetOfNode::Identifier: {
5646 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
5647 if (!ToII)
5648 return nullptr;
5649 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
5650 Importer.Import(Node.getLocEnd())));
5651 break;
5652 }
5653 }
5654 }
5655
5656 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
5657 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
5658 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
5659 if (!ToIndexExpr)
5660 return nullptr;
5661 Exprs[I] = ToIndexExpr;
5662 }
5663
5664 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
5665 if (!TInfo && OE->getTypeSourceInfo())
5666 return nullptr;
5667
5668 return OffsetOfExpr::Create(Importer.getToContext(), T,
5669 Importer.Import(OE->getOperatorLoc()),
5670 TInfo, Nodes, Exprs,
5671 Importer.Import(OE->getRParenLoc()));
5672}
5673
5674Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
5675 QualType T = Importer.Import(E->getType());
5676 if (T.isNull())
5677 return nullptr;
5678
5679 Expr *Operand = Importer.Import(E->getOperand());
5680 if (!Operand)
5681 return nullptr;
5682
5683 CanThrowResult CanThrow;
5684 if (E->isValueDependent())
5685 CanThrow = CT_Dependent;
5686 else
5687 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
5688
5689 return new (Importer.getToContext()) CXXNoexceptExpr(
5690 T, Operand, CanThrow,
5691 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
5692}
5693
5694Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
5695 QualType T = Importer.Import(E->getType());
5696 if (T.isNull())
5697 return nullptr;
5698
5699 Expr *SubExpr = Importer.Import(E->getSubExpr());
5700 if (!SubExpr && E->getSubExpr())
5701 return nullptr;
5702
5703 return new (Importer.getToContext()) CXXThrowExpr(
5704 SubExpr, T, Importer.Import(E->getThrowLoc()),
5705 E->isThrownVariableInScope());
5706}
5707
5708Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
5709 ParmVarDecl *Param = cast_or_null<ParmVarDecl>(
5710 Importer.Import(E->getParam()));
5711 if (!Param)
5712 return nullptr;
5713
5714 return CXXDefaultArgExpr::Create(
5715 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
5716}
5717
5718Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5719 QualType T = Importer.Import(E->getType());
5720 if (T.isNull())
5721 return nullptr;
5722
5723 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
5724 if (!TypeInfo)
5725 return nullptr;
5726
5727 return new (Importer.getToContext()) CXXScalarValueInitExpr(
5728 T, TypeInfo, Importer.Import(E->getRParenLoc()));
5729}
5730
5731Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
5732 Expr *SubExpr = Importer.Import(E->getSubExpr());
5733 if (!SubExpr)
5734 return nullptr;
5735
5736 auto *Dtor = cast_or_null<CXXDestructorDecl>(
5737 Importer.Import(const_cast<CXXDestructorDecl *>(
5738 E->getTemporary()->getDestructor())));
5739 if (!Dtor)
5740 return nullptr;
5741
5742 ASTContext &ToCtx = Importer.getToContext();
5743 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
5744 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
5745}
5746
5747Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
5748 QualType T = Importer.Import(CE->getType());
5749 if (T.isNull())
5750 return nullptr;
5751
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005752
5753 TypeSourceInfo *TInfo = Importer.Import(CE->getTypeSourceInfo());
5754 if (!TInfo)
5755 return nullptr;
5756
Aleksei Sidorina693b372016-09-28 10:16:56 +00005757 SmallVector<Expr *, 8> Args(CE->getNumArgs());
5758 if (ImportContainerChecked(CE->arguments(), Args))
5759 return nullptr;
5760
5761 auto *Ctor = cast_or_null<CXXConstructorDecl>(
5762 Importer.Import(CE->getConstructor()));
5763 if (!Ctor)
5764 return nullptr;
5765
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005766 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
5767 Importer.getToContext(), Ctor, T, TInfo, Args,
5768 Importer.Import(CE->getParenOrBraceRange()), CE->hadMultipleCandidates(),
5769 CE->isListInitialization(), CE->isStdInitListInitialization(),
5770 CE->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005771}
5772
5773Expr *
5774ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
5775 QualType T = Importer.Import(E->getType());
5776 if (T.isNull())
5777 return nullptr;
5778
5779 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
5780 if (!TempE)
5781 return nullptr;
5782
5783 ValueDecl *ExtendedBy = cast_or_null<ValueDecl>(
5784 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
5785 if (!ExtendedBy && E->getExtendingDecl())
5786 return nullptr;
5787
5788 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
5789 T, TempE, E->isBoundToLvalueReference());
5790
5791 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
5792 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
5793 return ToMTE;
5794}
5795
Gabor Horvath7a91c082017-11-14 11:30:38 +00005796Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
5797 QualType T = Importer.Import(E->getType());
5798 if (T.isNull())
5799 return nullptr;
5800
5801 Expr *Pattern = Importer.Import(E->getPattern());
5802 if (!Pattern)
5803 return nullptr;
5804
5805 return new (Importer.getToContext()) PackExpansionExpr(
5806 T, Pattern, Importer.Import(E->getEllipsisLoc()),
5807 E->getNumExpansions());
5808}
5809
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005810Expr *ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
5811 auto *Pack = cast_or_null<NamedDecl>(Importer.Import(E->getPack()));
5812 if (!Pack)
5813 return nullptr;
5814
5815 Optional<unsigned> Length;
5816
5817 if (!E->isValueDependent())
5818 Length = E->getPackLength();
5819
5820 SmallVector<TemplateArgument, 8> PartialArguments;
5821 if (E->isPartiallySubstituted()) {
5822 if (ImportTemplateArguments(E->getPartialArguments().data(),
5823 E->getPartialArguments().size(),
5824 PartialArguments))
5825 return nullptr;
5826 }
5827
5828 return SizeOfPackExpr::Create(
5829 Importer.getToContext(), Importer.Import(E->getOperatorLoc()), Pack,
5830 Importer.Import(E->getPackLoc()), Importer.Import(E->getRParenLoc()),
5831 Length, PartialArguments);
5832}
5833
5834
Aleksei Sidorina693b372016-09-28 10:16:56 +00005835Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
5836 QualType T = Importer.Import(CE->getType());
5837 if (T.isNull())
5838 return nullptr;
5839
5840 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
5841 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
5842 return nullptr;
5843
5844 FunctionDecl *OperatorNewDecl = cast_or_null<FunctionDecl>(
5845 Importer.Import(CE->getOperatorNew()));
5846 if (!OperatorNewDecl && CE->getOperatorNew())
5847 return nullptr;
5848
5849 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5850 Importer.Import(CE->getOperatorDelete()));
5851 if (!OperatorDeleteDecl && CE->getOperatorDelete())
5852 return nullptr;
5853
5854 Expr *ToInit = Importer.Import(CE->getInitializer());
5855 if (!ToInit && CE->getInitializer())
5856 return nullptr;
5857
5858 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
5859 if (!TInfo)
5860 return nullptr;
5861
5862 Expr *ToArrSize = Importer.Import(CE->getArraySize());
5863 if (!ToArrSize && CE->getArraySize())
5864 return nullptr;
5865
5866 return new (Importer.getToContext()) CXXNewExpr(
5867 Importer.getToContext(),
5868 CE->isGlobalNew(),
5869 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00005870 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005871 CE->doesUsualArrayDeleteWantSize(),
5872 PlacementArgs,
5873 Importer.Import(CE->getTypeIdParens()),
5874 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
5875 Importer.Import(CE->getSourceRange()),
5876 Importer.Import(CE->getDirectInitRange()));
5877}
5878
5879Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
5880 QualType T = Importer.Import(E->getType());
5881 if (T.isNull())
5882 return nullptr;
5883
5884 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5885 Importer.Import(E->getOperatorDelete()));
5886 if (!OperatorDeleteDecl && E->getOperatorDelete())
5887 return nullptr;
5888
5889 Expr *ToArg = Importer.Import(E->getArgument());
5890 if (!ToArg && E->getArgument())
5891 return nullptr;
5892
5893 return new (Importer.getToContext()) CXXDeleteExpr(
5894 T, E->isGlobalDelete(),
5895 E->isArrayForm(),
5896 E->isArrayFormAsWritten(),
5897 E->doesUsualArrayDeleteWantSize(),
5898 OperatorDeleteDecl,
5899 ToArg,
5900 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005901}
5902
Sean Callanan59721b32015-04-28 18:41:46 +00005903Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5904 QualType T = Importer.Import(E->getType());
5905 if (T.isNull())
5906 return nullptr;
5907
5908 CXXConstructorDecl *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00005909 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00005910 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00005911 return nullptr;
5912
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005913 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005914 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00005915 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005916
5917 return CXXConstructExpr::Create(Importer.getToContext(), T,
5918 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00005919 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00005920 ToArgs, E->hadMultipleCandidates(),
5921 E->isListInitialization(),
5922 E->isStdInitListInitialization(),
5923 E->requiresZeroInitialization(),
5924 E->getConstructionKind(),
5925 Importer.Import(E->getParenOrBraceRange()));
5926}
5927
Aleksei Sidorina693b372016-09-28 10:16:56 +00005928Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
5929 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
5930 if (!SubExpr && EWC->getSubExpr())
5931 return nullptr;
5932
5933 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
5934 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
5935 if (ExprWithCleanups::CleanupObject Obj =
5936 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
5937 Objs[I] = Obj;
5938 else
5939 return nullptr;
5940
5941 return ExprWithCleanups::Create(Importer.getToContext(),
5942 SubExpr, EWC->cleanupsHaveSideEffects(),
5943 Objs);
5944}
5945
Sean Callanan8bca9962016-03-28 21:43:01 +00005946Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
5947 QualType T = Importer.Import(E->getType());
5948 if (T.isNull())
5949 return nullptr;
5950
5951 Expr *ToFn = Importer.Import(E->getCallee());
5952 if (!ToFn)
5953 return nullptr;
5954
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005955 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005956 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00005957 return nullptr;
5958
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005959 return new (Importer.getToContext()) CXXMemberCallExpr(
5960 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
5961 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00005962}
5963
5964Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
5965 QualType T = Importer.Import(E->getType());
5966 if (T.isNull())
5967 return nullptr;
5968
5969 return new (Importer.getToContext())
5970 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
5971}
5972
5973Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
5974 QualType T = Importer.Import(E->getType());
5975 if (T.isNull())
5976 return nullptr;
5977
5978 return new (Importer.getToContext())
5979 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
5980}
5981
5982
Sean Callanan59721b32015-04-28 18:41:46 +00005983Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5984 QualType T = Importer.Import(E->getType());
5985 if (T.isNull())
5986 return nullptr;
5987
5988 Expr *ToBase = Importer.Import(E->getBase());
5989 if (!ToBase && E->getBase())
5990 return nullptr;
5991
5992 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5993 if (!ToMember && E->getMemberDecl())
5994 return nullptr;
5995
5996 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5997 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5998 E->getFoundDecl().getAccess());
5999
6000 DeclarationNameInfo ToMemberNameInfo(
6001 Importer.Import(E->getMemberNameInfo().getName()),
6002 Importer.Import(E->getMemberNameInfo().getLoc()));
6003
6004 if (E->hasExplicitTemplateArgs()) {
6005 return nullptr; // FIXME: handle template arguments
6006 }
6007
6008 return MemberExpr::Create(Importer.getToContext(), ToBase,
6009 E->isArrow(),
6010 Importer.Import(E->getOperatorLoc()),
6011 Importer.Import(E->getQualifierLoc()),
6012 Importer.Import(E->getTemplateKeywordLoc()),
6013 ToMember, ToFoundDecl, ToMemberNameInfo,
6014 nullptr, T, E->getValueKind(),
6015 E->getObjectKind());
6016}
6017
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006018Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr(
6019 CXXPseudoDestructorExpr *E) {
6020
6021 Expr *BaseE = Importer.Import(E->getBase());
6022 if (!BaseE)
6023 return nullptr;
6024
6025 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo());
6026 if (!ScopeInfo && E->getScopeTypeInfo())
6027 return nullptr;
6028
6029 PseudoDestructorTypeStorage Storage;
6030 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
6031 IdentifierInfo *ToII = Importer.Import(FromII);
6032 if (!ToII)
6033 return nullptr;
6034 Storage = PseudoDestructorTypeStorage(
6035 ToII, Importer.Import(E->getDestroyedTypeLoc()));
6036 } else {
6037 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo());
6038 if (!TI)
6039 return nullptr;
6040 Storage = PseudoDestructorTypeStorage(TI);
6041 }
6042
6043 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
6044 Importer.getToContext(), BaseE, E->isArrow(),
6045 Importer.Import(E->getOperatorLoc()),
6046 Importer.Import(E->getQualifierLoc()),
6047 ScopeInfo, Importer.Import(E->getColonColonLoc()),
6048 Importer.Import(E->getTildeLoc()), Storage);
6049}
6050
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006051Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
6052 CXXDependentScopeMemberExpr *E) {
6053 Expr *Base = nullptr;
6054 if (!E->isImplicitAccess()) {
6055 Base = Importer.Import(E->getBase());
6056 if (!Base)
6057 return nullptr;
6058 }
6059
6060 QualType BaseType = Importer.Import(E->getBaseType());
6061 if (BaseType.isNull())
6062 return nullptr;
6063
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006064 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006065 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006066 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6067 E->template_arguments(), ToTAInfo))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006068 return nullptr;
6069 ResInfo = &ToTAInfo;
6070 }
6071
6072 DeclarationName Name = Importer.Import(E->getMember());
6073 if (!E->getMember().isEmpty() && Name.isEmpty())
6074 return nullptr;
6075
6076 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc()));
6077 // Import additional name location/type info.
6078 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo);
6079 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope());
6080 if (!ToFQ && E->getFirstQualifierFoundInScope())
6081 return nullptr;
6082
6083 return CXXDependentScopeMemberExpr::Create(
6084 Importer.getToContext(), Base, BaseType, E->isArrow(),
6085 Importer.Import(E->getOperatorLoc()),
6086 Importer.Import(E->getQualifierLoc()),
6087 Importer.Import(E->getTemplateKeywordLoc()),
6088 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo);
6089}
6090
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006091Expr *ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
6092 CXXUnresolvedConstructExpr *CE) {
6093
6094 unsigned NumArgs = CE->arg_size();
6095
6096 llvm::SmallVector<Expr *, 8> ToArgs(NumArgs);
6097 if (ImportArrayChecked(CE->arg_begin(), CE->arg_end(), ToArgs.begin()))
6098 return nullptr;
6099
6100 return CXXUnresolvedConstructExpr::Create(
6101 Importer.getToContext(), Importer.Import(CE->getTypeSourceInfo()),
6102 Importer.Import(CE->getLParenLoc()), llvm::makeArrayRef(ToArgs),
6103 Importer.Import(CE->getRParenLoc()));
6104}
6105
6106Expr *ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
6107 CXXRecordDecl *NamingClass =
6108 cast_or_null<CXXRecordDecl>(Importer.Import(E->getNamingClass()));
6109 if (E->getNamingClass() && !NamingClass)
6110 return nullptr;
6111
6112 DeclarationName Name = Importer.Import(E->getName());
6113 if (E->getName() && !Name)
6114 return nullptr;
6115
6116 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6117 // Import additional name location/type info.
6118 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6119
6120 UnresolvedSet<8> ToDecls;
6121 for (Decl *D : E->decls()) {
6122 if (NamedDecl *To = cast_or_null<NamedDecl>(Importer.Import(D)))
6123 ToDecls.addDecl(To);
6124 else
6125 return nullptr;
6126 }
6127
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006128 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006129 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006130 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6131 E->template_arguments(), ToTAInfo))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006132 return nullptr;
6133 ResInfo = &ToTAInfo;
6134 }
6135
6136 if (ResInfo || E->getTemplateKeywordLoc().isValid())
6137 return UnresolvedLookupExpr::Create(
6138 Importer.getToContext(), NamingClass,
6139 Importer.Import(E->getQualifierLoc()),
6140 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, E->requiresADL(),
6141 ResInfo, ToDecls.begin(), ToDecls.end());
6142
6143 return UnresolvedLookupExpr::Create(
6144 Importer.getToContext(), NamingClass,
6145 Importer.Import(E->getQualifierLoc()), NameInfo, E->requiresADL(),
6146 E->isOverloaded(), ToDecls.begin(), ToDecls.end());
6147}
6148
Sean Callanan59721b32015-04-28 18:41:46 +00006149Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
6150 QualType T = Importer.Import(E->getType());
6151 if (T.isNull())
6152 return nullptr;
6153
6154 Expr *ToCallee = Importer.Import(E->getCallee());
6155 if (!ToCallee && E->getCallee())
6156 return nullptr;
6157
6158 unsigned NumArgs = E->getNumArgs();
Sean Callanan59721b32015-04-28 18:41:46 +00006159 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006160 if (ImportContainerChecked(E->arguments(), ToArgs))
6161 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006162
6163 Expr **ToArgs_Copied = new (Importer.getToContext())
6164 Expr*[NumArgs];
6165
6166 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
6167 ToArgs_Copied[ai] = ToArgs[ai];
6168
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006169 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
6170 return new (Importer.getToContext()) CXXOperatorCallExpr(
6171 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, T,
6172 OCE->getValueKind(), Importer.Import(OCE->getRParenLoc()),
6173 OCE->getFPFeatures());
6174 }
6175
Sean Callanan59721b32015-04-28 18:41:46 +00006176 return new (Importer.getToContext())
6177 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00006178 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00006179 Importer.Import(E->getRParenLoc()));
6180}
6181
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006182Optional<LambdaCapture>
6183ASTNodeImporter::ImportLambdaCapture(const LambdaCapture &From) {
6184 VarDecl *Var = nullptr;
6185 if (From.capturesVariable()) {
6186 Var = cast_or_null<VarDecl>(Importer.Import(From.getCapturedVar()));
6187 if (!Var)
6188 return None;
6189 }
6190
6191 return LambdaCapture(Importer.Import(From.getLocation()), From.isImplicit(),
6192 From.getCaptureKind(), Var,
6193 From.isPackExpansion()
6194 ? Importer.Import(From.getEllipsisLoc())
6195 : SourceLocation());
6196}
6197
6198Expr *ASTNodeImporter::VisitLambdaExpr(LambdaExpr *LE) {
6199 CXXRecordDecl *FromClass = LE->getLambdaClass();
6200 auto *ToClass = dyn_cast_or_null<CXXRecordDecl>(Importer.Import(FromClass));
6201 if (!ToClass)
6202 return nullptr;
6203
6204 // NOTE: lambda classes are created with BeingDefined flag set up.
6205 // It means that ImportDefinition doesn't work for them and we should fill it
6206 // manually.
6207 if (ToClass->isBeingDefined()) {
6208 for (auto FromField : FromClass->fields()) {
6209 auto *ToField = cast_or_null<FieldDecl>(Importer.Import(FromField));
6210 if (!ToField)
6211 return nullptr;
6212 }
6213 }
6214
6215 auto *ToCallOp = dyn_cast_or_null<CXXMethodDecl>(
6216 Importer.Import(LE->getCallOperator()));
6217 if (!ToCallOp)
6218 return nullptr;
6219
6220 ToClass->completeDefinition();
6221
6222 unsigned NumCaptures = LE->capture_size();
6223 SmallVector<LambdaCapture, 8> Captures;
6224 Captures.reserve(NumCaptures);
6225 for (const auto &FromCapture : LE->captures()) {
6226 if (auto ToCapture = ImportLambdaCapture(FromCapture))
6227 Captures.push_back(*ToCapture);
6228 else
6229 return nullptr;
6230 }
6231
6232 SmallVector<Expr *, 8> InitCaptures(NumCaptures);
6233 if (ImportContainerChecked(LE->capture_inits(), InitCaptures))
6234 return nullptr;
6235
6236 return LambdaExpr::Create(Importer.getToContext(), ToClass,
6237 Importer.Import(LE->getIntroducerRange()),
6238 LE->getCaptureDefault(),
6239 Importer.Import(LE->getCaptureDefaultLoc()),
6240 Captures,
6241 LE->hasExplicitParameters(),
6242 LE->hasExplicitResultType(),
6243 InitCaptures,
6244 Importer.Import(LE->getLocEnd()),
6245 LE->containsUnexpandedParameterPack());
6246}
6247
6248
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006249Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
6250 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00006251 if (T.isNull())
6252 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006253
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006254 llvm::SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006255 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006256 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006257
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006258 ASTContext &ToCtx = Importer.getToContext();
6259 InitListExpr *To = new (ToCtx) InitListExpr(
6260 ToCtx, Importer.Import(ILE->getLBraceLoc()),
6261 Exprs, Importer.Import(ILE->getLBraceLoc()));
6262 To->setType(T);
6263
6264 if (ILE->hasArrayFiller()) {
6265 Expr *Filler = Importer.Import(ILE->getArrayFiller());
6266 if (!Filler)
6267 return nullptr;
6268 To->setArrayFiller(Filler);
6269 }
6270
6271 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
6272 FieldDecl *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
6273 if (!ToFD)
6274 return nullptr;
6275 To->setInitializedFieldInUnion(ToFD);
6276 }
6277
6278 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
6279 InitListExpr *ToSyntForm = cast_or_null<InitListExpr>(
6280 Importer.Import(SyntForm));
6281 if (!ToSyntForm)
6282 return nullptr;
6283 To->setSyntacticForm(ToSyntForm);
6284 }
6285
6286 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
6287 To->setValueDependent(ILE->isValueDependent());
6288 To->setInstantiationDependent(ILE->isInstantiationDependent());
6289
6290 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00006291}
6292
Richard Smith30e304e2016-12-14 00:03:17 +00006293Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
6294 QualType ToType = Importer.Import(E->getType());
6295 if (ToType.isNull())
6296 return nullptr;
6297
6298 Expr *ToCommon = Importer.Import(E->getCommonExpr());
6299 if (!ToCommon && E->getCommonExpr())
6300 return nullptr;
6301
6302 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
6303 if (!ToSubExpr && E->getSubExpr())
6304 return nullptr;
6305
6306 return new (Importer.getToContext())
6307 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
6308}
6309
6310Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
6311 QualType ToType = Importer.Import(E->getType());
6312 if (ToType.isNull())
6313 return nullptr;
6314 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
6315}
6316
Sean Callanandd2c1742016-05-16 20:48:03 +00006317Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
6318 FieldDecl *ToField = llvm::dyn_cast_or_null<FieldDecl>(
6319 Importer.Import(DIE->getField()));
6320 if (!ToField && DIE->getField())
6321 return nullptr;
6322
6323 return CXXDefaultInitExpr::Create(
6324 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
6325}
6326
6327Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
6328 QualType ToType = Importer.Import(E->getType());
6329 if (ToType.isNull() && !E->getType().isNull())
6330 return nullptr;
6331 ExprValueKind VK = E->getValueKind();
6332 CastKind CK = E->getCastKind();
6333 Expr *ToOp = Importer.Import(E->getSubExpr());
6334 if (!ToOp && E->getSubExpr())
6335 return nullptr;
6336 CXXCastPath BasePath;
6337 if (ImportCastPath(E, BasePath))
6338 return nullptr;
6339 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6340 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6341 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6342 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
6343
6344 if (isa<CXXStaticCastExpr>(E)) {
6345 return CXXStaticCastExpr::Create(
6346 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6347 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6348 } else if (isa<CXXDynamicCastExpr>(E)) {
6349 return CXXDynamicCastExpr::Create(
6350 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6351 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6352 } else if (isa<CXXReinterpretCastExpr>(E)) {
6353 return CXXReinterpretCastExpr::Create(
6354 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6355 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6356 } else {
6357 return nullptr;
6358 }
6359}
6360
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006361
6362Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
6363 SubstNonTypeTemplateParmExpr *E) {
6364 QualType T = Importer.Import(E->getType());
6365 if (T.isNull())
6366 return nullptr;
6367
6368 NonTypeTemplateParmDecl *Param = cast_or_null<NonTypeTemplateParmDecl>(
6369 Importer.Import(E->getParameter()));
6370 if (!Param)
6371 return nullptr;
6372
6373 Expr *Replacement = Importer.Import(E->getReplacement());
6374 if (!Replacement)
6375 return nullptr;
6376
6377 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
6378 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
6379 Replacement);
6380}
6381
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00006382Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
6383 QualType ToType = Importer.Import(E->getType());
6384 if (ToType.isNull())
6385 return nullptr;
6386
6387 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
6388 if (ImportContainerChecked(E->getArgs(), ToArgs))
6389 return nullptr;
6390
6391 // According to Sema::BuildTypeTrait(), if E is value-dependent,
6392 // Value is always false.
6393 bool ToValue = false;
6394 if (!E->isValueDependent())
6395 ToValue = E->getValue();
6396
6397 return TypeTraitExpr::Create(
6398 Importer.getToContext(), ToType, Importer.Import(E->getLocStart()),
6399 E->getTrait(), ToArgs, Importer.Import(E->getLocEnd()), ToValue);
6400}
6401
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006402Expr *ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
6403 QualType ToType = Importer.Import(E->getType());
6404 if (ToType.isNull())
6405 return nullptr;
6406
6407 if (E->isTypeOperand()) {
6408 TypeSourceInfo *TSI = Importer.Import(E->getTypeOperandSourceInfo());
6409 if (!TSI)
6410 return nullptr;
6411
6412 return new (Importer.getToContext())
6413 CXXTypeidExpr(ToType, TSI, Importer.Import(E->getSourceRange()));
6414 }
6415
6416 Expr *Op = Importer.Import(E->getExprOperand());
6417 if (!Op)
6418 return nullptr;
6419
6420 return new (Importer.getToContext())
6421 CXXTypeidExpr(ToType, Op, Importer.Import(E->getSourceRange()));
6422}
6423
Lang Hames19e07e12017-06-20 21:06:00 +00006424void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
6425 CXXMethodDecl *FromMethod) {
6426 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
6427 ToMethod->addOverriddenMethod(
6428 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
6429 FromOverriddenMethod))));
6430}
6431
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006432ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006433 ASTContext &FromContext, FileManager &FromFileManager,
6434 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00006435 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00006436 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006437 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00006438{
Douglas Gregor62d311f2010-02-09 19:21:46 +00006439 ImportedDecls[FromContext.getTranslationUnitDecl()]
6440 = ToContext.getTranslationUnitDecl();
6441}
6442
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00006443ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00006444
6445QualType ASTImporter::Import(QualType FromT) {
6446 if (FromT.isNull())
6447 return QualType();
John McCall424cec92011-01-19 06:33:43 +00006448
6449 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00006450
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006451 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006452 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6453 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006454 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006455 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006456
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006457 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006458 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006459 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006460 if (ToT.isNull())
6461 return ToT;
6462
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006463 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006464 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006465
John McCall424cec92011-01-19 06:33:43 +00006466 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006467}
6468
Douglas Gregor62d311f2010-02-09 19:21:46 +00006469TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006470 if (!FromTSI)
6471 return FromTSI;
6472
6473 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006474 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006475 QualType T = Import(FromTSI->getType());
6476 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006477 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006478
6479 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006480 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006481}
6482
Sean Callanan59721b32015-04-28 18:41:46 +00006483Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6484 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6485 if (Pos != ImportedDecls.end()) {
6486 Decl *ToD = Pos->second;
6487 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6488 return ToD;
6489 } else {
6490 return nullptr;
6491 }
6492}
6493
Douglas Gregor62d311f2010-02-09 19:21:46 +00006494Decl *ASTImporter::Import(Decl *FromD) {
6495 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006496 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006497
Douglas Gregord451ea92011-07-29 23:31:30 +00006498 ASTNodeImporter Importer(*this);
6499
Douglas Gregor62d311f2010-02-09 19:21:46 +00006500 // Check whether we've already imported this declaration.
6501 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006502 if (Pos != ImportedDecls.end()) {
6503 Decl *ToD = Pos->second;
6504 Importer.ImportDefinitionIfNeeded(FromD, ToD);
6505 return ToD;
6506 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00006507
6508 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00006509 Decl *ToD = Importer.Visit(FromD);
6510 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006511 return nullptr;
6512
Douglas Gregor62d311f2010-02-09 19:21:46 +00006513 // Record the imported declaration.
6514 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00006515
6516 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
6517 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00006518 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00006519 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00006520 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006521 // When we've finished transforming a typedef, see whether it was the
6522 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00006523 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00006524 FromTag = AnonTagsWithPendingTypedefs.begin(),
6525 FromTagEnd = AnonTagsWithPendingTypedefs.end();
6526 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00006527 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006528 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
6529 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00006530 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00006531 AnonTagsWithPendingTypedefs.erase(FromTag);
6532 break;
6533 }
6534 }
6535 }
6536 }
6537
Douglas Gregor62d311f2010-02-09 19:21:46 +00006538 return ToD;
6539}
6540
6541DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6542 if (!FromDC)
6543 return FromDC;
6544
Douglas Gregor95d82832012-01-24 18:36:04 +00006545 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006546 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006547 return nullptr;
6548
Douglas Gregor2e15c842012-02-01 21:00:38 +00006549 // When we're using a record/enum/Objective-C class/protocol as a context, we
6550 // need it to have a definition.
6551 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00006552 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006553 if (ToRecord->isCompleteDefinition()) {
6554 // Do nothing.
6555 } else if (FromRecord->isCompleteDefinition()) {
6556 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6557 ASTNodeImporter::IDK_Basic);
6558 } else {
6559 CompleteDecl(ToRecord);
6560 }
6561 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6562 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
6563 if (ToEnum->isCompleteDefinition()) {
6564 // Do nothing.
6565 } else if (FromEnum->isCompleteDefinition()) {
6566 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6567 ASTNodeImporter::IDK_Basic);
6568 } else {
6569 CompleteDecl(ToEnum);
6570 }
6571 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6572 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
6573 if (ToClass->getDefinition()) {
6574 // Do nothing.
6575 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6576 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6577 ASTNodeImporter::IDK_Basic);
6578 } else {
6579 CompleteDecl(ToClass);
6580 }
6581 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6582 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
6583 if (ToProto->getDefinition()) {
6584 // Do nothing.
6585 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6586 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6587 ASTNodeImporter::IDK_Basic);
6588 } else {
6589 CompleteDecl(ToProto);
6590 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006591 }
6592
6593 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006594}
6595
6596Expr *ASTImporter::Import(Expr *FromE) {
6597 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00006598 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006599
6600 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
6601}
6602
6603Stmt *ASTImporter::Import(Stmt *FromS) {
6604 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00006605 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006606
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006607 // Check whether we've already imported this declaration.
6608 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
6609 if (Pos != ImportedStmts.end())
6610 return Pos->second;
6611
6612 // Import the type
6613 ASTNodeImporter Importer(*this);
6614 Stmt *ToS = Importer.Visit(FromS);
6615 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00006616 return nullptr;
6617
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006618 // Record the imported declaration.
6619 ImportedStmts[FromS] = ToS;
6620 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006621}
6622
6623NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
6624 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00006625 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006626
Douglas Gregor90ebf252011-04-27 16:48:40 +00006627 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
6628
6629 switch (FromNNS->getKind()) {
6630 case NestedNameSpecifier::Identifier:
6631 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
6632 return NestedNameSpecifier::Create(ToContext, prefix, II);
6633 }
Craig Topper36250ad2014-05-12 05:36:57 +00006634 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006635
6636 case NestedNameSpecifier::Namespace:
6637 if (NamespaceDecl *NS =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006638 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006639 return NestedNameSpecifier::Create(ToContext, prefix, NS);
6640 }
Craig Topper36250ad2014-05-12 05:36:57 +00006641 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006642
6643 case NestedNameSpecifier::NamespaceAlias:
6644 if (NamespaceAliasDecl *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006645 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006646 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
6647 }
Craig Topper36250ad2014-05-12 05:36:57 +00006648 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006649
6650 case NestedNameSpecifier::Global:
6651 return NestedNameSpecifier::GlobalSpecifier(ToContext);
6652
Nikola Smiljanic67860242014-09-26 00:28:20 +00006653 case NestedNameSpecifier::Super:
6654 if (CXXRecordDecl *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006655 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00006656 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
6657 }
6658 return nullptr;
6659
Douglas Gregor90ebf252011-04-27 16:48:40 +00006660 case NestedNameSpecifier::TypeSpec:
6661 case NestedNameSpecifier::TypeSpecWithTemplate: {
6662 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
6663 if (!T.isNull()) {
6664 bool bTemplate = FromNNS->getKind() ==
6665 NestedNameSpecifier::TypeSpecWithTemplate;
6666 return NestedNameSpecifier::Create(ToContext, prefix,
6667 bTemplate, T.getTypePtr());
6668 }
6669 }
Craig Topper36250ad2014-05-12 05:36:57 +00006670 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006671 }
6672
6673 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00006674}
6675
Douglas Gregor14454802011-02-25 02:25:35 +00006676NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006677 // Copied from NestedNameSpecifier mostly.
6678 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
6679 NestedNameSpecifierLoc NNS = FromNNS;
6680
6681 // Push each of the nested-name-specifiers's onto a stack for
6682 // serialization in reverse order.
6683 while (NNS) {
6684 NestedNames.push_back(NNS);
6685 NNS = NNS.getPrefix();
6686 }
6687
6688 NestedNameSpecifierLocBuilder Builder;
6689
6690 while (!NestedNames.empty()) {
6691 NNS = NestedNames.pop_back_val();
6692 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
6693 if (!Spec)
6694 return NestedNameSpecifierLoc();
6695
6696 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
6697 switch (Kind) {
6698 case NestedNameSpecifier::Identifier:
6699 Builder.Extend(getToContext(),
6700 Spec->getAsIdentifier(),
6701 Import(NNS.getLocalBeginLoc()),
6702 Import(NNS.getLocalEndLoc()));
6703 break;
6704
6705 case NestedNameSpecifier::Namespace:
6706 Builder.Extend(getToContext(),
6707 Spec->getAsNamespace(),
6708 Import(NNS.getLocalBeginLoc()),
6709 Import(NNS.getLocalEndLoc()));
6710 break;
6711
6712 case NestedNameSpecifier::NamespaceAlias:
6713 Builder.Extend(getToContext(),
6714 Spec->getAsNamespaceAlias(),
6715 Import(NNS.getLocalBeginLoc()),
6716 Import(NNS.getLocalEndLoc()));
6717 break;
6718
6719 case NestedNameSpecifier::TypeSpec:
6720 case NestedNameSpecifier::TypeSpecWithTemplate: {
6721 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
6722 QualType(Spec->getAsType(), 0));
6723 Builder.Extend(getToContext(),
6724 Import(NNS.getLocalBeginLoc()),
6725 TSI->getTypeLoc(),
6726 Import(NNS.getLocalEndLoc()));
6727 break;
6728 }
6729
6730 case NestedNameSpecifier::Global:
6731 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
6732 break;
6733
6734 case NestedNameSpecifier::Super: {
6735 SourceRange ToRange = Import(NNS.getSourceRange());
6736 Builder.MakeSuper(getToContext(),
6737 Spec->getAsRecordDecl(),
6738 ToRange.getBegin(),
6739 ToRange.getEnd());
6740 }
6741 }
6742 }
6743
6744 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00006745}
6746
Douglas Gregore2e50d332010-12-01 01:36:18 +00006747TemplateName ASTImporter::Import(TemplateName From) {
6748 switch (From.getKind()) {
6749 case TemplateName::Template:
6750 if (TemplateDecl *ToTemplate
6751 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6752 return TemplateName(ToTemplate);
6753
6754 return TemplateName();
6755
6756 case TemplateName::OverloadedTemplate: {
6757 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
6758 UnresolvedSet<2> ToTemplates;
6759 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
6760 E = FromStorage->end();
6761 I != E; ++I) {
6762 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
6763 ToTemplates.addDecl(To);
6764 else
6765 return TemplateName();
6766 }
6767 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
6768 ToTemplates.end());
6769 }
6770
6771 case TemplateName::QualifiedTemplate: {
6772 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
6773 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
6774 if (!Qualifier)
6775 return TemplateName();
6776
6777 if (TemplateDecl *ToTemplate
6778 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6779 return ToContext.getQualifiedTemplateName(Qualifier,
6780 QTN->hasTemplateKeyword(),
6781 ToTemplate);
6782
6783 return TemplateName();
6784 }
6785
6786 case TemplateName::DependentTemplate: {
6787 DependentTemplateName *DTN = From.getAsDependentTemplateName();
6788 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
6789 if (!Qualifier)
6790 return TemplateName();
6791
6792 if (DTN->isIdentifier()) {
6793 return ToContext.getDependentTemplateName(Qualifier,
6794 Import(DTN->getIdentifier()));
6795 }
6796
6797 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
6798 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006799
6800 case TemplateName::SubstTemplateTemplateParm: {
6801 SubstTemplateTemplateParmStorage *subst
6802 = From.getAsSubstTemplateTemplateParm();
6803 TemplateTemplateParmDecl *param
6804 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
6805 if (!param)
6806 return TemplateName();
6807
6808 TemplateName replacement = Import(subst->getReplacement());
6809 if (replacement.isNull()) return TemplateName();
6810
6811 return ToContext.getSubstTemplateTemplateParm(param, replacement);
6812 }
Douglas Gregor5590be02011-01-15 06:45:20 +00006813
6814 case TemplateName::SubstTemplateTemplateParmPack: {
6815 SubstTemplateTemplateParmPackStorage *SubstPack
6816 = From.getAsSubstTemplateTemplateParmPack();
6817 TemplateTemplateParmDecl *Param
6818 = cast_or_null<TemplateTemplateParmDecl>(
6819 Import(SubstPack->getParameterPack()));
6820 if (!Param)
6821 return TemplateName();
6822
6823 ASTNodeImporter Importer(*this);
6824 TemplateArgument ArgPack
6825 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
6826 if (ArgPack.isNull())
6827 return TemplateName();
6828
6829 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
6830 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00006831 }
6832
6833 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00006834}
6835
Douglas Gregor62d311f2010-02-09 19:21:46 +00006836SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
6837 if (FromLoc.isInvalid())
6838 return SourceLocation();
6839
Douglas Gregor811663e2010-02-10 00:15:17 +00006840 SourceManager &FromSM = FromContext.getSourceManager();
6841
Sean Callanan24c5fe62016-11-07 20:42:25 +00006842 // For now, map everything down to its file location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00006843 // don't have to import macro expansions.
6844 // FIXME: Import macro expansions!
Sean Callanan24c5fe62016-11-07 20:42:25 +00006845 FromLoc = FromSM.getFileLoc(FromLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00006846 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
6847 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00006848 FileID ToFileID = Import(Decomposed.first);
6849 if (ToFileID.isInvalid())
6850 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00006851 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
6852 .getLocWithOffset(Decomposed.second);
6853 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006854}
6855
6856SourceRange ASTImporter::Import(SourceRange FromRange) {
6857 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
6858}
6859
Douglas Gregor811663e2010-02-10 00:15:17 +00006860FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00006861 llvm::DenseMap<FileID, FileID>::iterator Pos
6862 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00006863 if (Pos != ImportedFileIDs.end())
6864 return Pos->second;
6865
6866 SourceManager &FromSM = FromContext.getSourceManager();
6867 SourceManager &ToSM = ToContext.getSourceManager();
6868 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00006869 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00006870
6871 // Include location of this file.
6872 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
6873
6874 // Map the FileID for to the "to" source manager.
6875 FileID ToID;
6876 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00006877 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00006878 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
6879 // disk again
6880 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
6881 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00006882 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00006883 if (!Entry)
6884 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00006885 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
6886 FromSLoc.getFile().getFileCharacteristic());
6887 } else {
6888 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006889 const llvm::MemoryBuffer *
6890 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006891 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00006892 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00006893 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00006894 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006895 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00006896 }
6897
6898
Sebastian Redl99219f12010-09-30 01:03:06 +00006899 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00006900 return ToID;
6901}
6902
Sean Callanandd2c1742016-05-16 20:48:03 +00006903CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
6904 Expr *ToExpr = Import(From->getInit());
6905 if (!ToExpr && From->getInit())
6906 return nullptr;
6907
6908 if (From->isBaseInitializer()) {
6909 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6910 if (!ToTInfo && From->getTypeSourceInfo())
6911 return nullptr;
6912
6913 return new (ToContext) CXXCtorInitializer(
6914 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
6915 ToExpr, Import(From->getRParenLoc()),
6916 From->isPackExpansion() ? Import(From->getEllipsisLoc())
6917 : SourceLocation());
6918 } else if (From->isMemberInitializer()) {
6919 FieldDecl *ToField =
6920 llvm::cast_or_null<FieldDecl>(Import(From->getMember()));
6921 if (!ToField && From->getMember())
6922 return nullptr;
6923
6924 return new (ToContext) CXXCtorInitializer(
6925 ToContext, ToField, Import(From->getMemberLocation()),
6926 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6927 } else if (From->isIndirectMemberInitializer()) {
6928 IndirectFieldDecl *ToIField = llvm::cast_or_null<IndirectFieldDecl>(
6929 Import(From->getIndirectMember()));
6930 if (!ToIField && From->getIndirectMember())
6931 return nullptr;
6932
6933 return new (ToContext) CXXCtorInitializer(
6934 ToContext, ToIField, Import(From->getMemberLocation()),
6935 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6936 } else if (From->isDelegatingInitializer()) {
6937 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6938 if (!ToTInfo && From->getTypeSourceInfo())
6939 return nullptr;
6940
6941 return new (ToContext)
6942 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
6943 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006944 } else {
6945 return nullptr;
6946 }
6947}
6948
6949
Aleksei Sidorina693b372016-09-28 10:16:56 +00006950CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
6951 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
6952 if (Pos != ImportedCXXBaseSpecifiers.end())
6953 return Pos->second;
6954
6955 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
6956 Import(BaseSpec->getSourceRange()),
6957 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
6958 BaseSpec->getAccessSpecifierAsWritten(),
6959 Import(BaseSpec->getTypeSourceInfo()),
6960 Import(BaseSpec->getEllipsisLoc()));
6961 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
6962 return Imported;
6963}
6964
Douglas Gregor0a791672011-01-18 03:11:38 +00006965void ASTImporter::ImportDefinition(Decl *From) {
6966 Decl *To = Import(From);
6967 if (!To)
6968 return;
6969
6970 if (DeclContext *FromDC = cast<DeclContext>(From)) {
6971 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006972
6973 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
6974 if (!ToRecord->getDefinition()) {
6975 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00006976 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006977 return;
6978 }
6979 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006980
6981 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
6982 if (!ToEnum->getDefinition()) {
6983 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006984 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00006985 return;
6986 }
6987 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00006988
6989 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
6990 if (!ToIFace->getDefinition()) {
6991 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006992 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006993 return;
6994 }
6995 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006996
Douglas Gregor2aa53772012-01-24 17:42:07 +00006997 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
6998 if (!ToProto->getDefinition()) {
6999 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007000 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007001 return;
7002 }
7003 }
7004
Douglas Gregor0a791672011-01-18 03:11:38 +00007005 Importer.ImportDeclContext(FromDC, true);
7006 }
7007}
7008
Douglas Gregor96e578d2010-02-05 17:54:41 +00007009DeclarationName ASTImporter::Import(DeclarationName FromName) {
7010 if (!FromName)
7011 return DeclarationName();
7012
7013 switch (FromName.getNameKind()) {
7014 case DeclarationName::Identifier:
7015 return Import(FromName.getAsIdentifierInfo());
7016
7017 case DeclarationName::ObjCZeroArgSelector:
7018 case DeclarationName::ObjCOneArgSelector:
7019 case DeclarationName::ObjCMultiArgSelector:
7020 return Import(FromName.getObjCSelector());
7021
7022 case DeclarationName::CXXConstructorName: {
7023 QualType T = Import(FromName.getCXXNameType());
7024 if (T.isNull())
7025 return DeclarationName();
7026
7027 return ToContext.DeclarationNames.getCXXConstructorName(
7028 ToContext.getCanonicalType(T));
7029 }
7030
7031 case DeclarationName::CXXDestructorName: {
7032 QualType T = Import(FromName.getCXXNameType());
7033 if (T.isNull())
7034 return DeclarationName();
7035
7036 return ToContext.DeclarationNames.getCXXDestructorName(
7037 ToContext.getCanonicalType(T));
7038 }
7039
Richard Smith35845152017-02-07 01:37:30 +00007040 case DeclarationName::CXXDeductionGuideName: {
7041 TemplateDecl *Template = cast_or_null<TemplateDecl>(
7042 Import(FromName.getCXXDeductionGuideTemplate()));
7043 if (!Template)
7044 return DeclarationName();
7045 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
7046 }
7047
Douglas Gregor96e578d2010-02-05 17:54:41 +00007048 case DeclarationName::CXXConversionFunctionName: {
7049 QualType T = Import(FromName.getCXXNameType());
7050 if (T.isNull())
7051 return DeclarationName();
7052
7053 return ToContext.DeclarationNames.getCXXConversionFunctionName(
7054 ToContext.getCanonicalType(T));
7055 }
7056
7057 case DeclarationName::CXXOperatorName:
7058 return ToContext.DeclarationNames.getCXXOperatorName(
7059 FromName.getCXXOverloadedOperator());
7060
7061 case DeclarationName::CXXLiteralOperatorName:
7062 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
7063 Import(FromName.getCXXLiteralIdentifier()));
7064
7065 case DeclarationName::CXXUsingDirective:
7066 // FIXME: STATICS!
7067 return DeclarationName::getUsingDirectiveName();
7068 }
7069
David Blaikiee4d798f2012-01-20 21:50:17 +00007070 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00007071}
7072
Douglas Gregore2e50d332010-12-01 01:36:18 +00007073IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007074 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00007075 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007076
Sean Callananf94ef1d2016-05-14 06:11:19 +00007077 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
7078
7079 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
7080 ToId->setBuiltinID(FromId->getBuiltinID());
7081
7082 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007083}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007084
Douglas Gregor43f54792010-02-17 02:12:47 +00007085Selector ASTImporter::Import(Selector FromSel) {
7086 if (FromSel.isNull())
7087 return Selector();
7088
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007089 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00007090 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
7091 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
7092 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
7093 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
7094}
7095
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007096DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
7097 DeclContext *DC,
7098 unsigned IDNS,
7099 NamedDecl **Decls,
7100 unsigned NumDecls) {
7101 return Name;
7102}
7103
7104DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007105 if (LastDiagFromFrom)
7106 ToContext.getDiagnostics().notePriorDiagnosticFrom(
7107 FromContext.getDiagnostics());
7108 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007109 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007110}
7111
7112DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007113 if (!LastDiagFromFrom)
7114 FromContext.getDiagnostics().notePriorDiagnosticFrom(
7115 ToContext.getDiagnostics());
7116 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007117 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007118}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007119
Douglas Gregor2e15c842012-02-01 21:00:38 +00007120void ASTImporter::CompleteDecl (Decl *D) {
7121 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
7122 if (!ID->getDefinition())
7123 ID->startDefinition();
7124 }
7125 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
7126 if (!PD->getDefinition())
7127 PD->startDefinition();
7128 }
7129 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
7130 if (!TD->getDefinition() && !TD->isBeingDefined()) {
7131 TD->startDefinition();
7132 TD->setCompleteDefinition(true);
7133 }
7134 }
7135 else {
7136 assert (0 && "CompleteDecl called on a Decl that can't be completed");
7137 }
7138}
7139
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007140Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00007141 if (From->hasAttrs()) {
7142 for (Attr *FromAttr : From->getAttrs())
7143 To->addAttr(FromAttr->clone(To->getASTContext()));
7144 }
7145 if (From->isUsed()) {
7146 To->setIsUsed();
7147 }
Sean Callanandd2c1742016-05-16 20:48:03 +00007148 if (From->isImplicit()) {
7149 To->setImplicit();
7150 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007151 ImportedDecls[From] = To;
7152 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00007153}
Douglas Gregorb4964f72010-02-15 23:54:17 +00007154
Douglas Gregordd6006f2012-07-17 21:16:27 +00007155bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
7156 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00007157 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00007158 = ImportedTypes.find(From.getTypePtr());
7159 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
7160 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00007161
Douglas Gregordd6006f2012-07-17 21:16:27 +00007162 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
7163 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00007164 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00007165}