blob: 5fa222cf4a1a2b49a9a562ea6ce30e69db1e4bee [file] [log] [blame]
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001//===- ASTImporter.cpp - Importing ASTs from other Contexts ---------------===//
Douglas Gregor96e578d2010-02-05 17:54:41 +00002//
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//===----------------------------------------------------------------------===//
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000014
Douglas Gregor96e578d2010-02-05 17:54:41 +000015#include "clang/AST/ASTImporter.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000016#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +000018#include "clang/AST/ASTStructuralEquivalence.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000019#include "clang/AST/Attr.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclAccessPair.h"
22#include "clang/AST/DeclBase.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000023#include "clang/AST/DeclCXX.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000024#include "clang/AST/DeclFriend.h"
25#include "clang/AST/DeclGroup.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000026#include "clang/AST/DeclObjC.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000027#include "clang/AST/DeclTemplate.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000028#include "clang/AST/DeclVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000029#include "clang/AST/DeclarationName.h"
30#include "clang/AST/Expr.h"
31#include "clang/AST/ExprCXX.h"
32#include "clang/AST/ExprObjC.h"
33#include "clang/AST/ExternalASTSource.h"
34#include "clang/AST/LambdaCapture.h"
35#include "clang/AST/NestedNameSpecifier.h"
36#include "clang/AST/OperationKinds.h"
37#include "clang/AST/Stmt.h"
38#include "clang/AST/StmtCXX.h"
39#include "clang/AST/StmtObjC.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000040#include "clang/AST/StmtVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000041#include "clang/AST/TemplateBase.h"
42#include "clang/AST/TemplateName.h"
43#include "clang/AST/Type.h"
44#include "clang/AST/TypeLoc.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000045#include "clang/AST/TypeVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000046#include "clang/AST/UnresolvedSet.h"
47#include "clang/Basic/ExceptionSpecificationType.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000048#include "clang/Basic/FileManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000049#include "clang/Basic/IdentifierTable.h"
50#include "clang/Basic/LLVM.h"
51#include "clang/Basic/LangOptions.h"
52#include "clang/Basic/SourceLocation.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000053#include "clang/Basic/SourceManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000054#include "clang/Basic/Specifiers.h"
55#include "llvm/ADT/APSInt.h"
56#include "llvm/ADT/ArrayRef.h"
57#include "llvm/ADT/DenseMap.h"
58#include "llvm/ADT/None.h"
59#include "llvm/ADT/Optional.h"
60#include "llvm/ADT/STLExtras.h"
61#include "llvm/ADT/SmallVector.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/ErrorHandling.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000064#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000065#include <algorithm>
66#include <cassert>
67#include <cstddef>
68#include <memory>
69#include <type_traits>
70#include <utility>
Douglas Gregor96e578d2010-02-05 17:54:41 +000071
Douglas Gregor3c2404b2011-11-03 18:07:07 +000072namespace clang {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000073
Gabor Marton5254e642018-06-27 13:32:50 +000074 template <class T>
75 SmallVector<Decl*, 2>
76 getCanonicalForwardRedeclChain(Redeclarable<T>* D) {
77 SmallVector<Decl*, 2> Redecls;
78 for (auto *R : D->getFirstDecl()->redecls()) {
79 if (R != D->getFirstDecl())
80 Redecls.push_back(R);
81 }
82 Redecls.push_back(D->getFirstDecl());
83 std::reverse(Redecls.begin(), Redecls.end());
84 return Redecls;
85 }
86
87 SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) {
88 // Currently only FunctionDecl is supported
89 auto FD = cast<FunctionDecl>(D);
90 return getCanonicalForwardRedeclChain<FunctionDecl>(FD);
91 }
92
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000093 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000094 public DeclVisitor<ASTNodeImporter, Decl *>,
95 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000096 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000097
Douglas Gregor96e578d2010-02-05 17:54:41 +000098 public:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000099 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
Gabor Marton344b0992018-05-16 11:48:11 +0000100
Douglas Gregor96e578d2010-02-05 17:54:41 +0000101 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +0000102 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000103 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000104
105 // Importing types
John McCall424cec92011-01-19 06:33:43 +0000106 QualType VisitType(const Type *T);
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000107 QualType VisitAtomicType(const AtomicType *T);
John McCall424cec92011-01-19 06:33:43 +0000108 QualType VisitBuiltinType(const BuiltinType *T);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000109 QualType VisitDecayedType(const DecayedType *T);
John McCall424cec92011-01-19 06:33:43 +0000110 QualType VisitComplexType(const ComplexType *T);
111 QualType VisitPointerType(const PointerType *T);
112 QualType VisitBlockPointerType(const BlockPointerType *T);
113 QualType VisitLValueReferenceType(const LValueReferenceType *T);
114 QualType VisitRValueReferenceType(const RValueReferenceType *T);
115 QualType VisitMemberPointerType(const MemberPointerType *T);
116 QualType VisitConstantArrayType(const ConstantArrayType *T);
117 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
118 QualType VisitVariableArrayType(const VariableArrayType *T);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000119 QualType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000120 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +0000121 QualType VisitVectorType(const VectorType *T);
122 QualType VisitExtVectorType(const ExtVectorType *T);
123 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
124 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000125 QualType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
Sean Callananda6df8a2011-08-11 16:56:07 +0000126 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +0000127 QualType VisitTypedefType(const TypedefType *T);
128 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000129 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +0000130 QualType VisitTypeOfType(const TypeOfType *T);
131 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +0000132 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +0000133 QualType VisitAutoType(const AutoType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000134 QualType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000135 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +0000136 QualType VisitRecordType(const RecordType *T);
137 QualType VisitEnumType(const EnumType *T);
Sean Callanan72fe0852015-04-02 23:50:08 +0000138 QualType VisitAttributedType(const AttributedType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000139 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000140 QualType VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T);
John McCall424cec92011-01-19 06:33:43 +0000141 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
142 QualType VisitElaboratedType(const ElaboratedType *T);
Peter Szecsice7f3182018-05-07 12:08:27 +0000143 QualType VisitDependentNameType(const DependentNameType *T);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000144 QualType VisitPackExpansionType(const PackExpansionType *T);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000145 QualType VisitDependentTemplateSpecializationType(
146 const DependentTemplateSpecializationType *T);
John McCall424cec92011-01-19 06:33:43 +0000147 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
148 QualType VisitObjCObjectType(const ObjCObjectType *T);
149 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Rafael Stahldf556202018-05-29 08:12:15 +0000150
151 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000152 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
153 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +0000154 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +0000155 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000156 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
157 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +0000158 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000159
Aleksei Sidorina693b372016-09-28 10:16:56 +0000160 bool ImportCastPath(CastExpr *E, CXXCastPath &Path);
161
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000162 using Designator = DesignatedInitExpr::Designator;
163
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000164 Designator ImportDesignator(const Designator &D);
165
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000166 Optional<LambdaCapture> ImportLambdaCapture(const LambdaCapture &From);
Douglas Gregor2e15c842012-02-01 21:00:38 +0000167
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000168 /// What we should import from the definition.
Douglas Gregor95d82832012-01-24 18:36:04 +0000169 enum ImportDefinitionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000170 /// Import the default subset of the definition, which might be
Douglas Gregor95d82832012-01-24 18:36:04 +0000171 /// nothing (if minimal import is set) or might be everything (if minimal
172 /// import is not set).
173 IDK_Default,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000174
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000175 /// Import everything.
Douglas Gregor95d82832012-01-24 18:36:04 +0000176 IDK_Everything,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000177
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000178 /// Import only the bare bones needed to establish a valid
Douglas Gregor95d82832012-01-24 18:36:04 +0000179 /// DeclContext.
180 IDK_Basic
181 };
182
Douglas Gregor2e15c842012-02-01 21:00:38 +0000183 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
184 return IDK == IDK_Everything ||
185 (IDK == IDK_Default && !Importer.isMinimalImport());
186 }
187
Douglas Gregord451ea92011-07-29 23:31:30 +0000188 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000189 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000190 bool ImportDefinition(VarDecl *From, VarDecl *To,
191 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000192 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000193 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000194 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000195 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000196 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000197 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000198 TemplateParameterList *ImportTemplateParameterList(
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000199 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000200 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000201 Optional<TemplateArgumentLoc> ImportTemplateArgumentLoc(
202 const TemplateArgumentLoc &TALoc);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000203 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
204 unsigned NumFromArgs,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000205 SmallVectorImpl<TemplateArgument> &ToArgs);
206
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000207 template <typename InContainerTy>
208 bool ImportTemplateArgumentListInfo(const InContainerTy &Container,
209 TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000210
211 template<typename InContainerTy>
212 bool ImportTemplateArgumentListInfo(SourceLocation FromLAngleLoc,
213 SourceLocation FromRAngleLoc,
214 const InContainerTy &Container,
215 TemplateArgumentListInfo &Result);
216
Gabor Marton5254e642018-06-27 13:32:50 +0000217 using TemplateArgsTy = SmallVector<TemplateArgument, 8>;
218 using OptionalTemplateArgsTy = Optional<TemplateArgsTy>;
219 std::tuple<FunctionTemplateDecl *, OptionalTemplateArgsTy>
220 ImportFunctionTemplateWithTemplateArgsFromSpecialization(
221 FunctionDecl *FromFD);
222
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000223 bool ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
224
Douglas Gregordd6006f2012-07-17 21:16:27 +0000225 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
226 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000227 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
228 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000229 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000230 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000231 bool IsStructuralMatch(FunctionTemplateDecl *From,
232 FunctionTemplateDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000233 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000234 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000235 Decl *VisitDecl(Decl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000236 Decl *VisitEmptyDecl(EmptyDecl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000237 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000238 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000239 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000240 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000241 Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000242 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000243 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000244 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000245 Decl *VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000246 Decl *VisitLabelDecl(LabelDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000247 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000248 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000249 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000250 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000251 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
252 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
253 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
254 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000255 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000256 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000257 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000258 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000259 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000260 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000261 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000262 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000263 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000264 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000265 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000266 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000267 Decl *VisitUsingDecl(UsingDecl *D);
268 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
269 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
270 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
271 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
272
Douglas Gregor85f3f952015-07-07 03:57:15 +0000273 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000274 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000275 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000276 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000277 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000278 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000279 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
280 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
281 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
282 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000283 Decl *VisitClassTemplateSpecializationDecl(
284 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000285 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
286 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000287 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000288
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000289 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000290 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
291
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000292 Stmt *VisitStmt(Stmt *S);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000293 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000294 Stmt *VisitDeclStmt(DeclStmt *S);
295 Stmt *VisitNullStmt(NullStmt *S);
296 Stmt *VisitCompoundStmt(CompoundStmt *S);
297 Stmt *VisitCaseStmt(CaseStmt *S);
298 Stmt *VisitDefaultStmt(DefaultStmt *S);
299 Stmt *VisitLabelStmt(LabelStmt *S);
300 Stmt *VisitAttributedStmt(AttributedStmt *S);
301 Stmt *VisitIfStmt(IfStmt *S);
302 Stmt *VisitSwitchStmt(SwitchStmt *S);
303 Stmt *VisitWhileStmt(WhileStmt *S);
304 Stmt *VisitDoStmt(DoStmt *S);
305 Stmt *VisitForStmt(ForStmt *S);
306 Stmt *VisitGotoStmt(GotoStmt *S);
307 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
308 Stmt *VisitContinueStmt(ContinueStmt *S);
309 Stmt *VisitBreakStmt(BreakStmt *S);
310 Stmt *VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000311 // FIXME: MSAsmStmt
312 // FIXME: SEHExceptStmt
313 // FIXME: SEHFinallyStmt
314 // FIXME: SEHTryStmt
315 // FIXME: SEHLeaveStmt
316 // FIXME: CapturedStmt
317 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
318 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
319 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
320 // FIXME: MSDependentExistsStmt
321 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
322 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
323 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
324 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
325 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
326 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
327 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000328
329 // Importing expressions
330 Expr *VisitExpr(Expr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000331 Expr *VisitVAArgExpr(VAArgExpr *E);
332 Expr *VisitGNUNullExpr(GNUNullExpr *E);
333 Expr *VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000334 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000335 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
336 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
337 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000338 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000339 Expr *VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000340 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000341 Expr *VisitStringLiteral(StringLiteral *E);
342 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
343 Expr *VisitAtomicExpr(AtomicExpr *E);
344 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000345 Expr *VisitParenExpr(ParenExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000346 Expr *VisitParenListExpr(ParenListExpr *E);
347 Expr *VisitStmtExpr(StmtExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000348 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000349 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000350 Expr *VisitBinaryOperator(BinaryOperator *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000351 Expr *VisitConditionalOperator(ConditionalOperator *E);
352 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
353 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000354 Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
355 Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E);
356 Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000357 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000358 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000359 Expr *VisitExplicitCastExpr(ExplicitCastExpr *E);
360 Expr *VisitOffsetOfExpr(OffsetOfExpr *OE);
361 Expr *VisitCXXThrowExpr(CXXThrowExpr *E);
362 Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
363 Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
364 Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
365 Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
366 Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE);
367 Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000368 Expr *VisitPackExpansionExpr(PackExpansionExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000369 Expr *VisitSizeOfPackExpr(SizeOfPackExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000370 Expr *VisitCXXNewExpr(CXXNewExpr *CE);
371 Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000372 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000373 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000374 Expr *VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
Peter Szecsice7f3182018-05-07 12:08:27 +0000375 Expr *VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +0000376 Expr *VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *CE);
377 Expr *VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
Peter Szecsice7f3182018-05-07 12:08:27 +0000378 Expr *VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000379 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC);
Sean Callanan8bca9962016-03-28 21:43:01 +0000380 Expr *VisitCXXThisExpr(CXXThisExpr *E);
381 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +0000382 Expr *VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000383 Expr *VisitMemberExpr(MemberExpr *E);
384 Expr *VisitCallExpr(CallExpr *E);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000385 Expr *VisitLambdaExpr(LambdaExpr *LE);
Sean Callanan8bca9962016-03-28 21:43:01 +0000386 Expr *VisitInitListExpr(InitListExpr *E);
Richard Smith30e304e2016-12-14 00:03:17 +0000387 Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
388 Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
Sean Callanandd2c1742016-05-16 20:48:03 +0000389 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
390 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000391 Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +0000392 Expr *VisitTypeTraitExpr(TypeTraitExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000393 Expr *VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000394
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000395 template<typename IIter, typename OIter>
396 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000397 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
398
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000399 ASTImporter &ImporterRef = Importer;
400 std::transform(Ibegin, Iend, Obegin,
401 [&ImporterRef](ItemT From) -> ItemT {
402 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000403 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000404 }
405
406 template<typename IIter, typename OIter>
407 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000408 using ItemT = typename std::remove_reference<decltype(**Obegin)>::type;
409
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000410 ASTImporter &ImporterRef = Importer;
411 bool Failed = false;
412 std::transform(Ibegin, Iend, Obegin,
413 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000414 auto *To = cast_or_null<ItemT>(ImporterRef.Import(From));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000415 if (!To && From)
416 Failed = true;
417 return To;
418 });
419 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000420 }
Aleksei Sidorina693b372016-09-28 10:16:56 +0000421
422 template<typename InContainerTy, typename OutContainerTy>
423 bool ImportContainerChecked(const InContainerTy &InContainer,
424 OutContainerTy &OutContainer) {
425 return ImportArrayChecked(InContainer.begin(), InContainer.end(),
426 OutContainer.begin());
427 }
428
429 template<typename InContainerTy, typename OIter>
430 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
431 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
432 }
Lang Hames19e07e12017-06-20 21:06:00 +0000433
434 // Importing overrides.
435 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000436
437 FunctionDecl *FindFunctionTemplateSpecialization(FunctionDecl *FromFD);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000438 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000439
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000440template <typename InContainerTy>
441bool ASTNodeImporter::ImportTemplateArgumentListInfo(
442 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
443 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
444 TemplateArgumentListInfo ToTAInfo(Importer.Import(FromLAngleLoc),
445 Importer.Import(FromRAngleLoc));
446 if (ImportTemplateArgumentListInfo(Container, ToTAInfo))
447 return true;
448 Result = ToTAInfo;
449 return false;
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000450}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000451
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000452template <>
453bool ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
454 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
455 return ImportTemplateArgumentListInfo(
456 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
457}
458
459template <>
460bool ASTNodeImporter::ImportTemplateArgumentListInfo<
461 ASTTemplateArgumentListInfo>(const ASTTemplateArgumentListInfo &From,
462 TemplateArgumentListInfo &Result) {
463 return ImportTemplateArgumentListInfo(From.LAngleLoc, From.RAngleLoc,
464 From.arguments(), Result);
465}
466
Gabor Marton5254e642018-06-27 13:32:50 +0000467std::tuple<FunctionTemplateDecl *, ASTNodeImporter::OptionalTemplateArgsTy>
468ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
469 FunctionDecl *FromFD) {
470 assert(FromFD->getTemplatedKind() ==
471 FunctionDecl::TK_FunctionTemplateSpecialization);
472 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
473 auto *Template = cast_or_null<FunctionTemplateDecl>(
474 Importer.Import(FTSInfo->getTemplate()));
475
476 // Import template arguments.
477 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
478 TemplateArgsTy ToTemplArgs;
479 if (ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
480 ToTemplArgs)) // Error during import.
481 return std::make_tuple(Template, OptionalTemplateArgsTy());
482
483 return std::make_tuple(Template, ToTemplArgs);
484}
485
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000486} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000487
Douglas Gregor3996e242010-02-15 22:01:00 +0000488//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000489// Import Types
490//----------------------------------------------------------------------------
491
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000492using namespace clang;
493
John McCall424cec92011-01-19 06:33:43 +0000494QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000495 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
496 << T->getTypeClassName();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000497 return {};
Douglas Gregore4c83e42010-02-09 22:48:33 +0000498}
499
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000500QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
501 QualType UnderlyingType = Importer.Import(T->getValueType());
502 if(UnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000503 return {};
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000504
505 return Importer.getToContext().getAtomicType(UnderlyingType);
506}
507
John McCall424cec92011-01-19 06:33:43 +0000508QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000509 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000510#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
511 case BuiltinType::Id: \
512 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000513#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000514#define SHARED_SINGLETON_TYPE(Expansion)
515#define BUILTIN_TYPE(Id, SingletonId) \
516 case BuiltinType::Id: return Importer.getToContext().SingletonId;
517#include "clang/AST/BuiltinTypes.def"
518
519 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
520 // context supports C++.
521
522 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
523 // context supports ObjC.
524
Douglas Gregor96e578d2010-02-05 17:54:41 +0000525 case BuiltinType::Char_U:
526 // The context we're importing from has an unsigned 'char'. If we're
527 // importing into a context with a signed 'char', translate to
528 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000529 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000530 return Importer.getToContext().UnsignedCharTy;
531
532 return Importer.getToContext().CharTy;
533
Douglas Gregor96e578d2010-02-05 17:54:41 +0000534 case BuiltinType::Char_S:
535 // The context we're importing from has an unsigned 'char'. If we're
536 // importing into a context with a signed 'char', translate to
537 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000538 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000539 return Importer.getToContext().SignedCharTy;
540
541 return Importer.getToContext().CharTy;
542
Chris Lattnerad3467e2010-12-25 23:25:43 +0000543 case BuiltinType::WChar_S:
544 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000545 // FIXME: If not in C++, shall we translate to the C equivalent of
546 // wchar_t?
547 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000548 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000549
550 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000551}
552
Aleksei Sidorina693b372016-09-28 10:16:56 +0000553QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
554 QualType OrigT = Importer.Import(T->getOriginalType());
555 if (OrigT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000556 return {};
Aleksei Sidorina693b372016-09-28 10:16:56 +0000557
558 return Importer.getToContext().getDecayedType(OrigT);
559}
560
John McCall424cec92011-01-19 06:33:43 +0000561QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000562 QualType ToElementType = Importer.Import(T->getElementType());
563 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000564 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000565
566 return Importer.getToContext().getComplexType(ToElementType);
567}
568
John McCall424cec92011-01-19 06:33:43 +0000569QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000570 QualType ToPointeeType = Importer.Import(T->getPointeeType());
571 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000572 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000573
574 return Importer.getToContext().getPointerType(ToPointeeType);
575}
576
John McCall424cec92011-01-19 06:33:43 +0000577QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000578 // FIXME: Check for blocks support in "to" context.
579 QualType ToPointeeType = Importer.Import(T->getPointeeType());
580 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000581 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000582
583 return Importer.getToContext().getBlockPointerType(ToPointeeType);
584}
585
John McCall424cec92011-01-19 06:33:43 +0000586QualType
587ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000588 // FIXME: Check for C++ support in "to" context.
589 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
590 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000591 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000592
593 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
594}
595
John McCall424cec92011-01-19 06:33:43 +0000596QualType
597ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000598 // FIXME: Check for C++0x support in "to" context.
599 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
600 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000601 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000602
603 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
604}
605
John McCall424cec92011-01-19 06:33:43 +0000606QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000607 // FIXME: Check for C++ support in "to" context.
608 QualType ToPointeeType = Importer.Import(T->getPointeeType());
609 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000610 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000611
612 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
613 return Importer.getToContext().getMemberPointerType(ToPointeeType,
614 ClassType.getTypePtr());
615}
616
John McCall424cec92011-01-19 06:33:43 +0000617QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000618 QualType ToElementType = Importer.Import(T->getElementType());
619 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000620 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000621
622 return Importer.getToContext().getConstantArrayType(ToElementType,
623 T->getSize(),
624 T->getSizeModifier(),
625 T->getIndexTypeCVRQualifiers());
626}
627
John McCall424cec92011-01-19 06:33:43 +0000628QualType
629ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000630 QualType ToElementType = Importer.Import(T->getElementType());
631 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000632 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000633
634 return Importer.getToContext().getIncompleteArrayType(ToElementType,
635 T->getSizeModifier(),
636 T->getIndexTypeCVRQualifiers());
637}
638
John McCall424cec92011-01-19 06:33:43 +0000639QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000640 QualType ToElementType = Importer.Import(T->getElementType());
641 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000642 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000643
644 Expr *Size = Importer.Import(T->getSizeExpr());
645 if (!Size)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000646 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000647
648 SourceRange Brackets = Importer.Import(T->getBracketsRange());
649 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
650 T->getSizeModifier(),
651 T->getIndexTypeCVRQualifiers(),
652 Brackets);
653}
654
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000655QualType ASTNodeImporter::VisitDependentSizedArrayType(
656 const DependentSizedArrayType *T) {
657 QualType ToElementType = Importer.Import(T->getElementType());
658 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000659 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000660
661 // SizeExpr may be null if size is not specified directly.
662 // For example, 'int a[]'.
663 Expr *Size = Importer.Import(T->getSizeExpr());
664 if (!Size && T->getSizeExpr())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000665 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000666
667 SourceRange Brackets = Importer.Import(T->getBracketsRange());
668 return Importer.getToContext().getDependentSizedArrayType(
669 ToElementType, Size, T->getSizeModifier(), T->getIndexTypeCVRQualifiers(),
670 Brackets);
671}
672
John McCall424cec92011-01-19 06:33:43 +0000673QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000674 QualType ToElementType = Importer.Import(T->getElementType());
675 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000676 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000677
678 return Importer.getToContext().getVectorType(ToElementType,
679 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +0000680 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000681}
682
John McCall424cec92011-01-19 06:33:43 +0000683QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000684 QualType ToElementType = Importer.Import(T->getElementType());
685 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000686 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000687
688 return Importer.getToContext().getExtVectorType(ToElementType,
689 T->getNumElements());
690}
691
John McCall424cec92011-01-19 06:33:43 +0000692QualType
693ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000694 // FIXME: What happens if we're importing a function without a prototype
695 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +0000696 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000697 if (ToResultType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000698 return {};
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000699
Douglas Gregor96e578d2010-02-05 17:54:41 +0000700 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000701 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000702}
703
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +0000704QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000705 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000706 if (ToResultType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000707 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000708
709 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000710 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000711 for (const auto &A : T->param_types()) {
712 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000713 if (ArgType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000714 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000715 ArgTypes.push_back(ArgType);
716 }
717
718 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000719 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000720 for (const auto &E : T->exceptions()) {
721 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000722 if (ExceptionType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000723 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000724 ExceptionTypes.push_back(ExceptionType);
725 }
John McCalldb40c7f2010-12-14 08:05:40 +0000726
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000727 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
728 FunctionProtoType::ExtProtoInfo ToEPI;
729
730 ToEPI.ExtInfo = FromEPI.ExtInfo;
731 ToEPI.Variadic = FromEPI.Variadic;
732 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
733 ToEPI.TypeQuals = FromEPI.TypeQuals;
734 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +0000735 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
736 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
737 ToEPI.ExceptionSpec.NoexceptExpr =
738 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
739 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
740 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
741 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
742 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000743
Jordan Rose5c382722013-03-08 21:51:21 +0000744 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000745}
746
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000747QualType ASTNodeImporter::VisitUnresolvedUsingType(
748 const UnresolvedUsingType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000749 const auto *ToD =
750 cast_or_null<UnresolvedUsingTypenameDecl>(Importer.Import(T->getDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000751 if (!ToD)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000752 return {};
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000753
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000754 auto *ToPrevD =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000755 cast_or_null<UnresolvedUsingTypenameDecl>(
756 Importer.Import(T->getDecl()->getPreviousDecl()));
757 if (!ToPrevD && T->getDecl()->getPreviousDecl())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000758 return {};
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000759
760 return Importer.getToContext().getTypeDeclType(ToD, ToPrevD);
761}
762
Sean Callananda6df8a2011-08-11 16:56:07 +0000763QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
764 QualType ToInnerType = Importer.Import(T->getInnerType());
765 if (ToInnerType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000766 return {};
Sean Callananda6df8a2011-08-11 16:56:07 +0000767
768 return Importer.getToContext().getParenType(ToInnerType);
769}
770
John McCall424cec92011-01-19 06:33:43 +0000771QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000772 auto *ToDecl =
773 dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000774 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000775 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000776
777 return Importer.getToContext().getTypeDeclType(ToDecl);
778}
779
John McCall424cec92011-01-19 06:33:43 +0000780QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000781 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
782 if (!ToExpr)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000783 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000784
785 return Importer.getToContext().getTypeOfExprType(ToExpr);
786}
787
John McCall424cec92011-01-19 06:33:43 +0000788QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000789 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
790 if (ToUnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000791 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000792
793 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
794}
795
John McCall424cec92011-01-19 06:33:43 +0000796QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +0000797 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +0000798 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
799 if (!ToExpr)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000800 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000801
Douglas Gregor81495f32012-02-12 18:42:33 +0000802 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
803 if (UnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000804 return {};
Douglas Gregor81495f32012-02-12 18:42:33 +0000805
806 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000807}
808
Alexis Hunte852b102011-05-24 22:41:36 +0000809QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
810 QualType ToBaseType = Importer.Import(T->getBaseType());
811 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
812 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000813 return {};
Alexis Hunte852b102011-05-24 22:41:36 +0000814
815 return Importer.getToContext().getUnaryTransformType(ToBaseType,
816 ToUnderlyingType,
817 T->getUTTKind());
818}
819
Richard Smith30482bc2011-02-20 03:19:35 +0000820QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +0000821 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +0000822 QualType FromDeduced = T->getDeducedType();
823 QualType ToDeduced;
824 if (!FromDeduced.isNull()) {
825 ToDeduced = Importer.Import(FromDeduced);
826 if (ToDeduced.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000827 return {};
Richard Smith30482bc2011-02-20 03:19:35 +0000828 }
829
Richard Smithe301ba22015-11-11 02:02:15 +0000830 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +0000831 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +0000832}
833
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000834QualType ASTNodeImporter::VisitInjectedClassNameType(
835 const InjectedClassNameType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000836 auto *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000837 if (!D)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000838 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000839
840 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
841 if (InjType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000842 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000843
844 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
845 // See comments in InjectedClassNameType definition for details
846 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
847 enum {
848 TypeAlignmentInBits = 4,
849 TypeAlignment = 1 << TypeAlignmentInBits
850 };
851
852 return QualType(new (Importer.getToContext(), TypeAlignment)
853 InjectedClassNameType(D, InjType), 0);
854}
855
John McCall424cec92011-01-19 06:33:43 +0000856QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000857 auto *ToDecl = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000858 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000859 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000860
861 return Importer.getToContext().getTagDeclType(ToDecl);
862}
863
John McCall424cec92011-01-19 06:33:43 +0000864QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000865 auto *ToDecl = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000866 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000867 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000868
869 return Importer.getToContext().getTagDeclType(ToDecl);
870}
871
Sean Callanan72fe0852015-04-02 23:50:08 +0000872QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
873 QualType FromModifiedType = T->getModifiedType();
874 QualType FromEquivalentType = T->getEquivalentType();
875 QualType ToModifiedType;
876 QualType ToEquivalentType;
877
878 if (!FromModifiedType.isNull()) {
879 ToModifiedType = Importer.Import(FromModifiedType);
880 if (ToModifiedType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000881 return {};
Sean Callanan72fe0852015-04-02 23:50:08 +0000882 }
883 if (!FromEquivalentType.isNull()) {
884 ToEquivalentType = Importer.Import(FromEquivalentType);
885 if (ToEquivalentType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000886 return {};
Sean Callanan72fe0852015-04-02 23:50:08 +0000887 }
888
889 return Importer.getToContext().getAttributedType(T->getAttrKind(),
890 ToModifiedType, ToEquivalentType);
891}
892
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000893QualType ASTNodeImporter::VisitTemplateTypeParmType(
894 const TemplateTypeParmType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000895 auto *ParmDecl =
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000896 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
897 if (!ParmDecl && T->getDecl())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000898 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000899
900 return Importer.getToContext().getTemplateTypeParmType(
901 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
902}
903
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000904QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
905 const SubstTemplateTypeParmType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000906 const auto *Replaced =
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000907 cast_or_null<TemplateTypeParmType>(Importer.Import(
908 QualType(T->getReplacedParameter(), 0)).getTypePtr());
909 if (!Replaced)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000910 return {};
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000911
912 QualType Replacement = Importer.Import(T->getReplacementType());
913 if (Replacement.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000914 return {};
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000915 Replacement = Replacement.getCanonicalType();
916
917 return Importer.getToContext().getSubstTemplateTypeParmType(
918 Replaced, Replacement);
919}
920
Douglas Gregore2e50d332010-12-01 01:36:18 +0000921QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +0000922 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000923 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
924 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000925 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +0000926
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000927 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +0000928 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000929 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +0000930
931 QualType ToCanonType;
932 if (!QualType(T, 0).isCanonical()) {
933 QualType FromCanonType
934 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
935 ToCanonType =Importer.Import(FromCanonType);
936 if (ToCanonType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000937 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +0000938 }
939 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +0000940 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +0000941 ToCanonType);
942}
943
John McCall424cec92011-01-19 06:33:43 +0000944QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +0000945 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000946 // Note: the qualifier in an ElaboratedType is optional.
947 if (T->getQualifier()) {
948 ToQualifier = Importer.Import(T->getQualifier());
949 if (!ToQualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000950 return {};
Abramo Bagnara6150c882010-05-11 21:36:43 +0000951 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000952
953 QualType ToNamedType = Importer.Import(T->getNamedType());
954 if (ToNamedType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000955 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000956
Joel E. Denny7509a2f2018-05-14 19:36:45 +0000957 TagDecl *OwnedTagDecl =
958 cast_or_null<TagDecl>(Importer.Import(T->getOwnedTagDecl()));
959 if (!OwnedTagDecl && T->getOwnedTagDecl())
960 return {};
961
Abramo Bagnara6150c882010-05-11 21:36:43 +0000962 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Joel E. Denny7509a2f2018-05-14 19:36:45 +0000963 ToQualifier, ToNamedType,
964 OwnedTagDecl);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000965}
966
Gabor Horvath7a91c082017-11-14 11:30:38 +0000967QualType ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
968 QualType Pattern = Importer.Import(T->getPattern());
969 if (Pattern.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000970 return {};
Gabor Horvath7a91c082017-11-14 11:30:38 +0000971
972 return Importer.getToContext().getPackExpansionType(Pattern,
973 T->getNumExpansions());
974}
975
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000976QualType ASTNodeImporter::VisitDependentTemplateSpecializationType(
977 const DependentTemplateSpecializationType *T) {
978 NestedNameSpecifier *Qualifier = Importer.Import(T->getQualifier());
979 if (!Qualifier && T->getQualifier())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000980 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000981
982 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
983 if (!Name && T->getIdentifier())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000984 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000985
986 SmallVector<TemplateArgument, 2> ToPack;
987 ToPack.reserve(T->getNumArgs());
988 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToPack))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000989 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000990
991 return Importer.getToContext().getDependentTemplateSpecializationType(
992 T->getKeyword(), Qualifier, Name, ToPack);
993}
994
Peter Szecsice7f3182018-05-07 12:08:27 +0000995QualType ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
996 NestedNameSpecifier *NNS = Importer.Import(T->getQualifier());
997 if (!NNS && T->getQualifier())
998 return QualType();
999
1000 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
1001 if (!Name && T->getIdentifier())
1002 return QualType();
1003
1004 QualType Canon = (T == T->getCanonicalTypeInternal().getTypePtr())
1005 ? QualType()
1006 : Importer.Import(T->getCanonicalTypeInternal());
1007 if (!Canon.isNull())
1008 Canon = Canon.getCanonicalType();
1009
1010 return Importer.getToContext().getDependentNameType(T->getKeyword(), NNS,
1011 Name, Canon);
1012}
1013
John McCall424cec92011-01-19 06:33:43 +00001014QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001015 auto *Class =
1016 dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001017 if (!Class)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001018 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001019
John McCall8b07ec22010-05-15 11:32:37 +00001020 return Importer.getToContext().getObjCInterfaceType(Class);
1021}
1022
John McCall424cec92011-01-19 06:33:43 +00001023QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001024 QualType ToBaseType = Importer.Import(T->getBaseType());
1025 if (ToBaseType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001026 return {};
John McCall8b07ec22010-05-15 11:32:37 +00001027
Douglas Gregore9d95f12015-07-07 03:57:35 +00001028 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001029 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001030 QualType ImportedTypeArg = Importer.Import(TypeArg);
1031 if (ImportedTypeArg.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001032 return {};
Douglas Gregore9d95f12015-07-07 03:57:35 +00001033
1034 TypeArgs.push_back(ImportedTypeArg);
1035 }
1036
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001037 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001038 for (auto *P : T->quals()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001039 auto *Protocol = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001040 if (!Protocol)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001041 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001042 Protocols.push_back(Protocol);
1043 }
1044
Douglas Gregore9d95f12015-07-07 03:57:35 +00001045 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001046 Protocols,
1047 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001048}
1049
John McCall424cec92011-01-19 06:33:43 +00001050QualType
1051ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001052 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1053 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001054 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001055
John McCall8b07ec22010-05-15 11:32:37 +00001056 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001057}
1058
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001059//----------------------------------------------------------------------------
1060// Import Declarations
1061//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001062bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1063 DeclContext *&LexicalDC,
1064 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00001065 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001066 SourceLocation &Loc) {
1067 // Import the context of this declaration.
1068 DC = Importer.ImportContext(D->getDeclContext());
1069 if (!DC)
1070 return true;
1071
1072 LexicalDC = DC;
1073 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1074 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1075 if (!LexicalDC)
1076 return true;
1077 }
1078
1079 // Import the name of this declaration.
1080 Name = Importer.Import(D->getDeclName());
1081 if (D->getDeclName() && !Name)
1082 return true;
1083
1084 // Import the location of this declaration.
1085 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00001086 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001087 return false;
1088}
1089
Douglas Gregord451ea92011-07-29 23:31:30 +00001090void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1091 if (!FromD)
1092 return;
1093
1094 if (!ToD) {
1095 ToD = Importer.Import(FromD);
1096 if (!ToD)
1097 return;
1098 }
1099
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001100 if (auto *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1101 if (auto *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00001102 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001103 ImportDefinition(FromRecord, ToRecord);
1104 }
1105 }
1106 return;
1107 }
1108
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001109 if (auto *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1110 if (auto *ToEnum = cast_or_null<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001111 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1112 ImportDefinition(FromEnum, ToEnum);
1113 }
1114 }
1115 return;
1116 }
1117}
1118
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001119void
1120ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1121 DeclarationNameInfo& To) {
1122 // NOTE: To.Name and To.Loc are already imported.
1123 // We only have to import To.LocInfo.
1124 switch (To.getName().getNameKind()) {
1125 case DeclarationName::Identifier:
1126 case DeclarationName::ObjCZeroArgSelector:
1127 case DeclarationName::ObjCOneArgSelector:
1128 case DeclarationName::ObjCMultiArgSelector:
1129 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001130 case DeclarationName::CXXDeductionGuideName:
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001131 return;
1132
1133 case DeclarationName::CXXOperatorName: {
1134 SourceRange Range = From.getCXXOperatorNameRange();
1135 To.setCXXOperatorNameRange(Importer.Import(Range));
1136 return;
1137 }
1138 case DeclarationName::CXXLiteralOperatorName: {
1139 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1140 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1141 return;
1142 }
1143 case DeclarationName::CXXConstructorName:
1144 case DeclarationName::CXXDestructorName:
1145 case DeclarationName::CXXConversionFunctionName: {
1146 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1147 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1148 return;
1149 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001150 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001151 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001152}
1153
Douglas Gregor2e15c842012-02-01 21:00:38 +00001154void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001155 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001156 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001157 return;
1158 }
1159
Aaron Ballman629afae2014-03-07 19:56:05 +00001160 for (auto *From : FromDC->decls())
1161 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00001162}
1163
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001164static void setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1165 ASTImporter &Importer) {
1166 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
1167 auto *ToTypedef =
1168 cast_or_null<TypedefNameDecl>(Importer.Import(FromTypedef));
1169 assert (ToTypedef && "Failed to import typedef of an anonymous structure");
1170
1171 To->setTypedefNameForAnonDecl(ToTypedef);
1172 }
1173}
1174
Douglas Gregord451ea92011-07-29 23:31:30 +00001175bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001176 ImportDefinitionKind Kind) {
1177 if (To->getDefinition() || To->isBeingDefined()) {
1178 if (Kind == IDK_Everything)
1179 ImportDeclContext(From, /*ForceImport=*/true);
1180
Douglas Gregore2e50d332010-12-01 01:36:18 +00001181 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001182 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001183
1184 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001185
1186 setTypedefNameForAnonDecl(From, To, Importer);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001187
1188 // Add base classes.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001189 if (auto *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1190 auto *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001191
1192 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1193 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1194 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001195 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001196 ToData.Aggregate = FromData.Aggregate;
1197 ToData.PlainOldData = FromData.PlainOldData;
1198 ToData.Empty = FromData.Empty;
1199 ToData.Polymorphic = FromData.Polymorphic;
1200 ToData.Abstract = FromData.Abstract;
1201 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001202 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1203 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1204 ToData.HasBasesWithNonStaticDataMembers =
1205 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001206 ToData.HasPrivateFields = FromData.HasPrivateFields;
1207 ToData.HasProtectedFields = FromData.HasProtectedFields;
1208 ToData.HasPublicFields = FromData.HasPublicFields;
1209 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001210 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001211 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001212 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001213 ToData.HasUninitializedReferenceMember
1214 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001215 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001216 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1217 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001218 ToData.NeedOverloadResolutionForCopyConstructor
1219 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001220 ToData.NeedOverloadResolutionForMoveConstructor
1221 = FromData.NeedOverloadResolutionForMoveConstructor;
1222 ToData.NeedOverloadResolutionForMoveAssignment
1223 = FromData.NeedOverloadResolutionForMoveAssignment;
1224 ToData.NeedOverloadResolutionForDestructor
1225 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001226 ToData.DefaultedCopyConstructorIsDeleted
1227 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001228 ToData.DefaultedMoveConstructorIsDeleted
1229 = FromData.DefaultedMoveConstructorIsDeleted;
1230 ToData.DefaultedMoveAssignmentIsDeleted
1231 = FromData.DefaultedMoveAssignmentIsDeleted;
1232 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001233 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1234 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001235 ToData.HasConstexprNonCopyMoveConstructor
1236 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001237 ToData.HasDefaultedDefaultConstructor
1238 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001239 ToData.DefaultedDefaultConstructorIsConstexpr
1240 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001241 ToData.HasConstexprDefaultConstructor
1242 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001243 ToData.HasNonLiteralTypeFieldsOrBases
1244 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001245 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001246 ToData.UserProvidedDefaultConstructor
1247 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001248 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001249 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1250 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1251 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1252 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001253 ToData.ImplicitCopyAssignmentHasConstParam
1254 = FromData.ImplicitCopyAssignmentHasConstParam;
1255 ToData.HasDeclaredCopyConstructorWithConstParam
1256 = FromData.HasDeclaredCopyConstructorWithConstParam;
1257 ToData.HasDeclaredCopyAssignmentWithConstParam
1258 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001259
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001260 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001261 for (const auto &Base1 : FromCXX->bases()) {
1262 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001263 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001264 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001265
1266 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001267 if (Base1.isPackExpansion())
1268 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001269
1270 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00001271 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00001272
Douglas Gregore2e50d332010-12-01 01:36:18 +00001273 Bases.push_back(
1274 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00001275 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1276 Base1.isVirtual(),
1277 Base1.isBaseOfClass(),
1278 Base1.getAccessSpecifierAsWritten(),
1279 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00001280 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001281 }
1282 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001283 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001284 }
1285
Douglas Gregor2e15c842012-02-01 21:00:38 +00001286 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001287 ImportDeclContext(From, /*ForceImport=*/true);
1288
Douglas Gregore2e50d332010-12-01 01:36:18 +00001289 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001290 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001291}
1292
Larisse Voufo39a1e502013-08-06 01:03:05 +00001293bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1294 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00001295 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00001296 return false;
1297
1298 // FIXME: Can we really import any initializer? Alternatively, we could force
1299 // ourselves to import every declaration of a variable and then only use
1300 // getInit() here.
1301 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1302
1303 // FIXME: Other bits to merge?
1304
1305 return false;
1306}
1307
Douglas Gregord451ea92011-07-29 23:31:30 +00001308bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001309 ImportDefinitionKind Kind) {
1310 if (To->getDefinition() || To->isBeingDefined()) {
1311 if (Kind == IDK_Everything)
1312 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001313 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001314 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001315
1316 To->startDefinition();
1317
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001318 setTypedefNameForAnonDecl(From, To, Importer);
1319
Douglas Gregord451ea92011-07-29 23:31:30 +00001320 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1321 if (T.isNull())
1322 return true;
1323
1324 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1325 if (ToPromotionType.isNull())
1326 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001327
1328 if (shouldForceImportDeclContext(Kind))
1329 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001330
1331 // FIXME: we might need to merge the number of positive or negative bits
1332 // if the enumerator lists don't match.
1333 To->completeDefinition(T, ToPromotionType,
1334 From->getNumPositiveBits(),
1335 From->getNumNegativeBits());
1336 return false;
1337}
1338
Douglas Gregora082a492010-11-30 19:14:50 +00001339TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1340 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001341 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1342 if (ImportContainerChecked(*Params, ToParams))
1343 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00001344
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001345 Expr *ToRequiresClause;
1346 if (Expr *const R = Params->getRequiresClause()) {
1347 ToRequiresClause = Importer.Import(R);
1348 if (!ToRequiresClause)
1349 return nullptr;
1350 } else {
1351 ToRequiresClause = nullptr;
1352 }
1353
Douglas Gregora082a492010-11-30 19:14:50 +00001354 return TemplateParameterList::Create(Importer.getToContext(),
1355 Importer.Import(Params->getTemplateLoc()),
1356 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00001357 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001358 Importer.Import(Params->getRAngleLoc()),
1359 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001360}
1361
Douglas Gregore2e50d332010-12-01 01:36:18 +00001362TemplateArgument
1363ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1364 switch (From.getKind()) {
1365 case TemplateArgument::Null:
1366 return TemplateArgument();
1367
1368 case TemplateArgument::Type: {
1369 QualType ToType = Importer.Import(From.getAsType());
1370 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001371 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001372 return TemplateArgument(ToType);
1373 }
1374
1375 case TemplateArgument::Integral: {
1376 QualType ToType = Importer.Import(From.getIntegralType());
1377 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001378 return {};
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001379 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001380 }
1381
Eli Friedmanb826a002012-09-26 02:36:12 +00001382 case TemplateArgument::Declaration: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001383 auto *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001384 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1385 if (!To || ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001386 return {};
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001387 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00001388 }
1389
1390 case TemplateArgument::NullPtr: {
1391 QualType ToType = Importer.Import(From.getNullPtrType());
1392 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001393 return {};
Eli Friedmanb826a002012-09-26 02:36:12 +00001394 return TemplateArgument(ToType, /*isNullPtr*/true);
1395 }
1396
Douglas Gregore2e50d332010-12-01 01:36:18 +00001397 case TemplateArgument::Template: {
1398 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1399 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001400 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001401
1402 return TemplateArgument(ToTemplate);
1403 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001404
1405 case TemplateArgument::TemplateExpansion: {
1406 TemplateName ToTemplate
1407 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1408 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001409 return {};
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001410
Douglas Gregore1d60df2011-01-14 23:41:42 +00001411 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001412 }
1413
Douglas Gregore2e50d332010-12-01 01:36:18 +00001414 case TemplateArgument::Expression:
1415 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1416 return TemplateArgument(ToExpr);
1417 return TemplateArgument();
1418
1419 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001420 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001421 ToPack.reserve(From.pack_size());
1422 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001423 return {};
Benjamin Kramercce63472015-08-05 09:40:22 +00001424
1425 return TemplateArgument(
1426 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001427 }
1428 }
1429
1430 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00001431}
1432
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001433Optional<TemplateArgumentLoc>
1434ASTNodeImporter::ImportTemplateArgumentLoc(const TemplateArgumentLoc &TALoc) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001435 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1436 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1437 TemplateArgumentLocInfo ToInfo;
1438 if (Arg.getKind() == TemplateArgument::Expression) {
1439 Expr *E = Importer.Import(FromInfo.getAsExpr());
1440 ToInfo = TemplateArgumentLocInfo(E);
1441 if (!E)
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001442 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001443 } else if (Arg.getKind() == TemplateArgument::Type) {
1444 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1445 ToInfo = TemplateArgumentLocInfo(TSI);
1446 else
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001447 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001448 } else {
1449 ToInfo = TemplateArgumentLocInfo(
1450 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1451 Importer.Import(FromInfo.getTemplateNameLoc()),
1452 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1453 }
1454 return TemplateArgumentLoc(Arg, ToInfo);
1455}
1456
Douglas Gregore2e50d332010-12-01 01:36:18 +00001457bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1458 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001459 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001460 for (unsigned I = 0; I != NumFromArgs; ++I) {
1461 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1462 if (To.isNull() && !FromArgs[I].isNull())
1463 return true;
1464
1465 ToArgs.push_back(To);
1466 }
1467
1468 return false;
1469}
1470
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001471// We cannot use Optional<> pattern here and below because
1472// TemplateArgumentListInfo's operator new is declared as deleted so it cannot
1473// be stored in Optional.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001474template <typename InContainerTy>
1475bool ASTNodeImporter::ImportTemplateArgumentListInfo(
1476 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1477 for (const auto &FromLoc : Container) {
1478 if (auto ToLoc = ImportTemplateArgumentLoc(FromLoc))
1479 ToTAInfo.addArgument(*ToLoc);
1480 else
1481 return true;
1482 }
1483 return false;
1484}
1485
Douglas Gregor5c73e912010-02-11 00:48:18 +00001486bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001487 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001488 // Eliminate a potential failure point where we attempt to re-import
1489 // something we're trying to import while completing ToRecord.
1490 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1491 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001492 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00001493 if (ToOriginRecord)
1494 ToRecord = ToOriginRecord;
1495 }
1496
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001497 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001498 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001499 Importer.getNonEquivalentDecls(),
1500 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001501 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001502}
1503
Larisse Voufo39a1e502013-08-06 01:03:05 +00001504bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1505 bool Complain) {
1506 StructuralEquivalenceContext Ctx(
1507 Importer.getFromContext(), Importer.getToContext(),
1508 Importer.getNonEquivalentDecls(), false, Complain);
1509 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
1510}
1511
Douglas Gregor98c10182010-02-12 22:17:39 +00001512bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001513 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001514 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001515 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001516 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001517}
1518
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001519bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1520 FunctionTemplateDecl *To) {
1521 StructuralEquivalenceContext Ctx(
1522 Importer.getFromContext(), Importer.getToContext(),
1523 Importer.getNonEquivalentDecls(), false, false);
1524 return Ctx.IsStructurallyEquivalent(From, To);
1525}
1526
Douglas Gregor91155082012-11-14 22:29:20 +00001527bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001528 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00001529 const llvm::APSInt &FromVal = FromEC->getInitVal();
1530 const llvm::APSInt &ToVal = ToEC->getInitVal();
1531
1532 return FromVal.isSigned() == ToVal.isSigned() &&
1533 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1534 FromVal == ToVal;
1535}
1536
1537bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001538 ClassTemplateDecl *To) {
1539 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1540 Importer.getToContext(),
1541 Importer.getNonEquivalentDecls());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001542 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00001543}
1544
Larisse Voufo39a1e502013-08-06 01:03:05 +00001545bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1546 VarTemplateDecl *To) {
1547 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1548 Importer.getToContext(),
1549 Importer.getNonEquivalentDecls());
1550 return Ctx.IsStructurallyEquivalent(From, To);
1551}
1552
Douglas Gregore4c83e42010-02-09 22:48:33 +00001553Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001554 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001555 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00001556 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00001557}
1558
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001559Decl *ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
1560 // Import the context of this declaration.
1561 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1562 if (!DC)
1563 return nullptr;
1564
1565 DeclContext *LexicalDC = DC;
1566 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1567 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1568 if (!LexicalDC)
1569 return nullptr;
1570 }
1571
1572 // Import the location of this declaration.
1573 SourceLocation Loc = Importer.Import(D->getLocation());
1574
1575 EmptyDecl *ToD = EmptyDecl::Create(Importer.getToContext(), DC, Loc);
1576 ToD->setLexicalDeclContext(LexicalDC);
1577 Importer.Imported(D, ToD);
1578 LexicalDC->addDeclInternal(ToD);
1579 return ToD;
1580}
1581
Sean Callanan65198272011-11-17 23:20:56 +00001582Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1583 TranslationUnitDecl *ToD =
1584 Importer.getToContext().getTranslationUnitDecl();
1585
1586 Importer.Imported(D, ToD);
1587
1588 return ToD;
1589}
1590
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001591Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001592 SourceLocation Loc = Importer.Import(D->getLocation());
1593 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1594
1595 // Import the context of this declaration.
1596 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1597 if (!DC)
1598 return nullptr;
1599
1600 AccessSpecDecl *accessSpecDecl
1601 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
1602 DC, Loc, ColonLoc);
1603
1604 if (!accessSpecDecl)
1605 return nullptr;
1606
1607 // Lexical DeclContext and Semantic DeclContext
1608 // is always the same for the accessSpec.
1609 accessSpecDecl->setLexicalDeclContext(DC);
1610 DC->addDeclInternal(accessSpecDecl);
1611
1612 return accessSpecDecl;
1613}
1614
Aleksei Sidorina693b372016-09-28 10:16:56 +00001615Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1616 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1617 if (!DC)
1618 return nullptr;
1619
1620 DeclContext *LexicalDC = DC;
1621
1622 // Import the location of this declaration.
1623 SourceLocation Loc = Importer.Import(D->getLocation());
1624
1625 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1626 if (!AssertExpr)
1627 return nullptr;
1628
1629 StringLiteral *FromMsg = D->getMessage();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001630 auto *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
Aleksei Sidorina693b372016-09-28 10:16:56 +00001631 if (!ToMsg && FromMsg)
1632 return nullptr;
1633
1634 StaticAssertDecl *ToD = StaticAssertDecl::Create(
1635 Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1636 Importer.Import(D->getRParenLoc()), D->isFailed());
1637
1638 ToD->setLexicalDeclContext(LexicalDC);
1639 LexicalDC->addDeclInternal(ToD);
1640 Importer.Imported(D, ToD);
1641 return ToD;
1642}
1643
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001644Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1645 // Import the major distinguishing characteristics of this namespace.
1646 DeclContext *DC, *LexicalDC;
1647 DeclarationName Name;
1648 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001649 NamedDecl *ToD;
1650 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001651 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001652 if (ToD)
1653 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001654
1655 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001656 if (!Name) {
1657 // This is an anonymous namespace. Adopt an existing anonymous
1658 // namespace if we can.
1659 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001660 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001661 MergeWithNamespace = TU->getAnonymousNamespace();
1662 else
1663 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1664 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001665 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001666 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001667 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001668 for (auto *FoundDecl : FoundDecls) {
1669 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001670 continue;
1671
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001672 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001673 MergeWithNamespace = FoundNS;
1674 ConflictingDecls.clear();
1675 break;
1676 }
1677
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001678 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001679 }
1680
1681 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001682 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001683 ConflictingDecls.data(),
1684 ConflictingDecls.size());
1685 }
1686 }
1687
1688 // Create the "to" namespace, if needed.
1689 NamespaceDecl *ToNamespace = MergeWithNamespace;
1690 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001691 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00001692 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001693 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00001694 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00001695 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001696 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001697 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001698
1699 // If this is an anonymous namespace, register it as the anonymous
1700 // namespace within its context.
1701 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001702 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001703 TU->setAnonymousNamespace(ToNamespace);
1704 else
1705 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1706 }
1707 }
1708 Importer.Imported(D, ToNamespace);
1709
1710 ImportDeclContext(D);
1711
1712 return ToNamespace;
1713}
1714
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001715Decl *ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1716 // Import the major distinguishing characteristics of this namespace.
1717 DeclContext *DC, *LexicalDC;
1718 DeclarationName Name;
1719 SourceLocation Loc;
1720 NamedDecl *LookupD;
1721 if (ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
1722 return nullptr;
1723 if (LookupD)
1724 return LookupD;
1725
1726 // NOTE: No conflict resolution is done for namespace aliases now.
1727
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001728 auto *TargetDecl = cast_or_null<NamespaceDecl>(
1729 Importer.Import(D->getNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001730 if (!TargetDecl)
1731 return nullptr;
1732
1733 IdentifierInfo *ToII = Importer.Import(D->getIdentifier());
1734 if (!ToII)
1735 return nullptr;
1736
1737 NestedNameSpecifierLoc ToQLoc = Importer.Import(D->getQualifierLoc());
1738 if (D->getQualifierLoc() && !ToQLoc)
1739 return nullptr;
1740
1741 NamespaceAliasDecl *ToD = NamespaceAliasDecl::Create(
1742 Importer.getToContext(), DC, Importer.Import(D->getNamespaceLoc()),
1743 Importer.Import(D->getAliasLoc()), ToII, ToQLoc,
1744 Importer.Import(D->getTargetNameLoc()), TargetDecl);
1745
1746 ToD->setLexicalDeclContext(LexicalDC);
1747 Importer.Imported(D, ToD);
1748 LexicalDC->addDeclInternal(ToD);
1749
1750 return ToD;
1751}
1752
Richard Smithdda56e42011-04-15 14:24:37 +00001753Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001754 // Import the major distinguishing characteristics of this typedef.
1755 DeclContext *DC, *LexicalDC;
1756 DeclarationName Name;
1757 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001758 NamedDecl *ToD;
1759 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001760 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001761 if (ToD)
1762 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001763
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001764 // If this typedef is not in block scope, determine whether we've
1765 // seen a typedef with the same name (that we can merge with) or any
1766 // other entity by that name (which name lookup could conflict with).
1767 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001768 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001769 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001770 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001771 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001772 for (auto *FoundDecl : FoundDecls) {
1773 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001774 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001775 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001776 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1777 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001778 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001779 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001780
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001781 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001782 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001783
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001784 if (!ConflictingDecls.empty()) {
1785 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1786 ConflictingDecls.data(),
1787 ConflictingDecls.size());
1788 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001789 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001790 }
1791 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001792
Douglas Gregorb4964f72010-02-15 23:54:17 +00001793 // Import the underlying type of this typedef;
1794 QualType T = Importer.Import(D->getUnderlyingType());
1795 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001796 return nullptr;
1797
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001798 // Some nodes (like anonymous tags referred by typedefs) are allowed to
1799 // import their enclosing typedef directly. Check if this is the case.
1800 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
1801 return AlreadyImported;
1802
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001803 // Create the new typedef node.
1804 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00001805 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00001806 TypedefNameDecl *ToTypedef;
1807 if (IsAlias)
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001808 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC, StartL, Loc,
1809 Name.getAsIdentifierInfo(), TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001810 else
Richard Smithdda56e42011-04-15 14:24:37 +00001811 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1812 StartL, Loc,
1813 Name.getAsIdentifierInfo(),
1814 TInfo);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001815
Douglas Gregordd483172010-02-22 17:42:47 +00001816 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001817 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001818 Importer.Imported(D, ToTypedef);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001819
1820 // Templated declarations should not appear in DeclContext.
1821 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
1822 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
1823 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001824
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001825 return ToTypedef;
1826}
1827
Richard Smithdda56e42011-04-15 14:24:37 +00001828Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1829 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1830}
1831
1832Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1833 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1834}
1835
Gabor Horvath7a91c082017-11-14 11:30:38 +00001836Decl *ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1837 // Import the major distinguishing characteristics of this typedef.
1838 DeclContext *DC, *LexicalDC;
1839 DeclarationName Name;
1840 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001841 NamedDecl *FoundD;
1842 if (ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001843 return nullptr;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001844 if (FoundD)
1845 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00001846
1847 // If this typedef is not in block scope, determine whether we've
1848 // seen a typedef with the same name (that we can merge with) or any
1849 // other entity by that name (which name lookup could conflict with).
1850 if (!DC->isFunctionOrMethod()) {
1851 SmallVector<NamedDecl *, 4> ConflictingDecls;
1852 unsigned IDNS = Decl::IDNS_Ordinary;
1853 SmallVector<NamedDecl *, 2> FoundDecls;
1854 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001855 for (auto *FoundDecl : FoundDecls) {
1856 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001857 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001858 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001859 return Importer.Imported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001860 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00001861 }
1862
1863 if (!ConflictingDecls.empty()) {
1864 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1865 ConflictingDecls.data(),
1866 ConflictingDecls.size());
1867 if (!Name)
1868 return nullptr;
1869 }
1870 }
1871
1872 TemplateParameterList *Params = ImportTemplateParameterList(
1873 D->getTemplateParameters());
1874 if (!Params)
1875 return nullptr;
1876
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001877 auto *TemplDecl = cast_or_null<TypeAliasDecl>(
Gabor Horvath7a91c082017-11-14 11:30:38 +00001878 Importer.Import(D->getTemplatedDecl()));
1879 if (!TemplDecl)
1880 return nullptr;
1881
1882 TypeAliasTemplateDecl *ToAlias = TypeAliasTemplateDecl::Create(
1883 Importer.getToContext(), DC, Loc, Name, Params, TemplDecl);
1884
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001885 TemplDecl->setDescribedAliasTemplate(ToAlias);
1886
Gabor Horvath7a91c082017-11-14 11:30:38 +00001887 ToAlias->setAccess(D->getAccess());
1888 ToAlias->setLexicalDeclContext(LexicalDC);
1889 Importer.Imported(D, ToAlias);
1890 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001891 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00001892}
1893
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001894Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
1895 // Import the major distinguishing characteristics of this label.
1896 DeclContext *DC, *LexicalDC;
1897 DeclarationName Name;
1898 SourceLocation Loc;
1899 NamedDecl *ToD;
1900 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1901 return nullptr;
1902 if (ToD)
1903 return ToD;
1904
1905 assert(LexicalDC->isFunctionOrMethod());
1906
1907 LabelDecl *ToLabel = D->isGnuLocal()
1908 ? LabelDecl::Create(Importer.getToContext(),
1909 DC, Importer.Import(D->getLocation()),
1910 Name.getAsIdentifierInfo(),
1911 Importer.Import(D->getLocStart()))
1912 : LabelDecl::Create(Importer.getToContext(),
1913 DC, Importer.Import(D->getLocation()),
1914 Name.getAsIdentifierInfo());
1915 Importer.Imported(D, ToLabel);
1916
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001917 auto *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001918 if (!Label)
1919 return nullptr;
1920
1921 ToLabel->setStmt(Label);
1922 ToLabel->setLexicalDeclContext(LexicalDC);
1923 LexicalDC->addDeclInternal(ToLabel);
1924 return ToLabel;
1925}
1926
Douglas Gregor98c10182010-02-12 22:17:39 +00001927Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1928 // Import the major distinguishing characteristics of this enum.
1929 DeclContext *DC, *LexicalDC;
1930 DeclarationName Name;
1931 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001932 NamedDecl *ToD;
1933 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001934 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001935 if (ToD)
1936 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001937
Douglas Gregor98c10182010-02-12 22:17:39 +00001938 // Figure out what enum name we're looking for.
1939 unsigned IDNS = Decl::IDNS_Tag;
1940 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001941 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1942 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00001943 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001944 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00001945 IDNS |= Decl::IDNS_Ordinary;
1946
1947 // We may already have an enum of the same name; try to find and match it.
1948 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001949 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001950 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001951 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001952 for (auto *FoundDecl : FoundDecls) {
1953 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00001954 continue;
1955
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001956 Decl *Found = FoundDecl;
1957 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
1958 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor98c10182010-02-12 22:17:39 +00001959 Found = Tag->getDecl();
1960 }
1961
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001962 if (auto *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001963 if (IsStructuralMatch(D, FoundEnum))
1964 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001965 }
1966
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001967 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00001968 }
1969
1970 if (!ConflictingDecls.empty()) {
1971 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1972 ConflictingDecls.data(),
1973 ConflictingDecls.size());
1974 }
1975 }
1976
1977 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001978 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
1979 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00001980 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001981 D->isScoped(), D->isScopedUsingClassTag(),
1982 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00001983 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00001984 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00001985 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00001986 D2->setLexicalDeclContext(LexicalDC);
1987 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00001988 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00001989
1990 // Import the integer type.
1991 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1992 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001993 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00001994 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001995
1996 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00001997 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00001998 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00001999
Douglas Gregor3996e242010-02-15 22:01:00 +00002000 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002001}
2002
Douglas Gregor5c73e912010-02-11 00:48:18 +00002003Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2004 // If this record has a definition in the translation unit we're coming from,
2005 // but this particular declaration is not that definition, import the
2006 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002007 TagDecl *Definition = D->getDefinition();
Gabor Martona3af5672018-05-23 14:24:02 +00002008 if (Definition && Definition != D &&
2009 // In contrast to a normal CXXRecordDecl, the implicit
2010 // CXXRecordDecl of ClassTemplateSpecializationDecl is its redeclaration.
2011 // The definition of the implicit CXXRecordDecl in this case is the
2012 // ClassTemplateSpecializationDecl itself. Thus, we start with an extra
2013 // condition in order to be able to import the implict Decl.
2014 !D->isImplicit()) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002015 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002016 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002017 return nullptr;
2018
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002019 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002020 }
Gabor Martona3af5672018-05-23 14:24:02 +00002021
Douglas Gregor5c73e912010-02-11 00:48:18 +00002022 // Import the major distinguishing characteristics of this record.
2023 DeclContext *DC, *LexicalDC;
2024 DeclarationName Name;
2025 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002026 NamedDecl *ToD;
2027 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002028 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002029 if (ToD)
2030 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002031
Douglas Gregor5c73e912010-02-11 00:48:18 +00002032 // Figure out what structure name we're looking for.
2033 unsigned IDNS = Decl::IDNS_Tag;
2034 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002035 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2036 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002037 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002038 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002039 IDNS |= Decl::IDNS_Ordinary;
2040
2041 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002042 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00002043 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002044 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002045 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002046 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002047 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00002048
2049 if (!FoundDecls.empty()) {
2050 // We're going to have to compare D against potentially conflicting Decls, so complete it.
2051 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2052 D->getASTContext().getExternalSource()->CompleteType(D);
2053 }
2054
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002055 for (auto *FoundDecl : FoundDecls) {
2056 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002057 continue;
2058
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002059 Decl *Found = FoundDecl;
2060 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2061 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002062 Found = Tag->getDecl();
2063 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002064
2065 if (D->getDescribedTemplate()) {
2066 if (auto *Template = dyn_cast<ClassTemplateDecl>(Found))
2067 Found = Template->getTemplatedDecl();
2068 else
2069 continue;
2070 }
2071
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002072 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Aleksei Sidorin499de6c2018-04-05 15:31:49 +00002073 if (!SearchName) {
2074 // If both unnamed structs/unions are in a record context, make sure
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002075 // they occur in the same location in the context records.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002076 if (Optional<unsigned> Index1 =
2077 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
2078 D)) {
2079 if (Optional<unsigned> Index2 = StructuralEquivalenceContext::
Sean Callanan488f8612016-07-14 19:53:44 +00002080 findUntaggedStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002081 if (*Index1 != *Index2)
2082 continue;
2083 }
2084 }
2085 }
2086
Sean Callanan9092d472017-05-13 00:46:33 +00002087 PrevDecl = FoundRecord;
2088
Douglas Gregor25791052010-02-12 00:09:27 +00002089 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002090 if ((SearchName && !D->isCompleteDefinition())
2091 || (D->isCompleteDefinition() &&
2092 D->isAnonymousStructOrUnion()
2093 == FoundDef->isAnonymousStructOrUnion() &&
2094 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002095 // The record types structurally match, or the "from" translation
2096 // unit only had a forward declaration anyway; call it the same
2097 // function.
2098 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002099 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002100 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002101 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002102 // We have a forward declaration of this type, so adopt that forward
2103 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002104
2105 // If one or both can be completed from external storage then try one
2106 // last time to complete and compare them before doing this.
2107
2108 if (FoundRecord->hasExternalLexicalStorage() &&
2109 !FoundRecord->isCompleteDefinition())
2110 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2111 if (D->hasExternalLexicalStorage())
2112 D->getASTContext().getExternalSource()->CompleteType(D);
2113
2114 if (FoundRecord->isCompleteDefinition() &&
2115 D->isCompleteDefinition() &&
2116 !IsStructuralMatch(D, FoundRecord))
2117 continue;
2118
Douglas Gregor25791052010-02-12 00:09:27 +00002119 AdoptDecl = FoundRecord;
2120 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002121 } else if (!SearchName) {
2122 continue;
2123 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002124 }
2125
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002126 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002127 }
2128
Douglas Gregordd6006f2012-07-17 21:16:27 +00002129 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002130 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2131 ConflictingDecls.data(),
2132 ConflictingDecls.size());
2133 }
2134 }
2135
2136 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002137 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002138 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002139 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002140 CXXRecordDecl *D2CXX = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002141 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002142 if (DCXX->isLambda()) {
2143 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
2144 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
2145 DC, TInfo, Loc,
2146 DCXX->isDependentLambda(),
2147 DCXX->isGenericLambda(),
2148 DCXX->getLambdaCaptureDefault());
2149 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2150 if (DCXX->getLambdaContextDecl() && !CDecl)
2151 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00002152 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002153 } else if (DCXX->isInjectedClassName()) {
2154 // We have to be careful to do a similar dance to the one in
2155 // Sema::ActOnStartCXXMemberDeclarations
2156 CXXRecordDecl *const PrevDecl = nullptr;
2157 const bool DelayTypeCreation = true;
2158 D2CXX = CXXRecordDecl::Create(
2159 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
2160 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
2161 Importer.getToContext().getTypeDeclType(
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002162 D2CXX, dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002163 } else {
2164 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2165 D->getTagKind(),
2166 DC, StartLoc, Loc,
2167 Name.getAsIdentifierInfo());
2168 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002169 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002170 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002171 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonde8bf262018-05-17 09:46:07 +00002172 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002173 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002174
2175 Importer.Imported(D, D2);
2176
2177 if (ClassTemplateDecl *FromDescribed =
2178 DCXX->getDescribedClassTemplate()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002179 auto *ToDescribed = cast_or_null<ClassTemplateDecl>(
2180 Importer.Import(FromDescribed));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002181 if (!ToDescribed)
2182 return nullptr;
2183 D2CXX->setDescribedClassTemplate(ToDescribed);
Gabor Marton5915777e2018-06-26 13:44:24 +00002184 if (!DCXX->isInjectedClassName()) {
2185 // In a record describing a template the type should be an
2186 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2187 // previously set type to the correct value here (ToDescribed is not
2188 // available at record create).
2189 // FIXME: The previous type is cleared but not removed from
2190 // ASTContext's internal storage.
2191 CXXRecordDecl *Injected = nullptr;
2192 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2193 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2194 if (Record && Record->isInjectedClassName()) {
2195 Injected = Record;
2196 break;
2197 }
2198 }
2199 D2CXX->setTypeForDecl(nullptr);
2200 Importer.getToContext().getInjectedClassNameType(D2CXX,
2201 ToDescribed->getInjectedClassNameSpecialization());
2202 if (Injected) {
2203 Injected->setTypeForDecl(nullptr);
2204 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2205 }
2206 }
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002207 } else if (MemberSpecializationInfo *MemberInfo =
2208 DCXX->getMemberSpecializationInfo()) {
2209 TemplateSpecializationKind SK =
2210 MemberInfo->getTemplateSpecializationKind();
2211 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002212 auto *ToInst =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002213 cast_or_null<CXXRecordDecl>(Importer.Import(FromInst));
2214 if (FromInst && !ToInst)
2215 return nullptr;
2216 D2CXX->setInstantiationOfMemberClass(ToInst, SK);
2217 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2218 Importer.Import(MemberInfo->getPointOfInstantiation()));
2219 }
Douglas Gregor25791052010-02-12 00:09:27 +00002220 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002221 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002222 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002223 D2->setLexicalDeclContext(LexicalDC);
2224 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002225 }
Douglas Gregor14454802011-02-25 02:25:35 +00002226
2227 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd6006f2012-07-17 21:16:27 +00002228 if (D->isAnonymousStructOrUnion())
2229 D2->setAnonymousStructOrUnion(true);
Sean Callanan9092d472017-05-13 00:46:33 +00002230 if (PrevDecl) {
2231 // FIXME: do this for all Redeclarables, not just RecordDecls.
2232 D2->setPreviousDecl(PrevDecl);
2233 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002234 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002235
Douglas Gregor3996e242010-02-15 22:01:00 +00002236 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002237
Douglas Gregor95d82832012-01-24 18:36:04 +00002238 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002239 return nullptr;
2240
Douglas Gregor3996e242010-02-15 22:01:00 +00002241 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002242}
2243
Douglas Gregor98c10182010-02-12 22:17:39 +00002244Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2245 // Import the major distinguishing characteristics of this enumerator.
2246 DeclContext *DC, *LexicalDC;
2247 DeclarationName Name;
2248 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002249 NamedDecl *ToD;
2250 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002251 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002252 if (ToD)
2253 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002254
2255 QualType T = Importer.Import(D->getType());
2256 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002257 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002258
Douglas Gregor98c10182010-02-12 22:17:39 +00002259 // Determine whether there are any other declarations with the same name and
2260 // in the same context.
2261 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002262 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002263 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002264 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002265 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002266 for (auto *FoundDecl : FoundDecls) {
2267 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002268 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002269
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002270 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002271 if (IsStructuralMatch(D, FoundEnumConstant))
2272 return Importer.Imported(D, FoundEnumConstant);
2273 }
2274
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002275 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002276 }
2277
2278 if (!ConflictingDecls.empty()) {
2279 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2280 ConflictingDecls.data(),
2281 ConflictingDecls.size());
2282 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002283 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002284 }
2285 }
2286
2287 Expr *Init = Importer.Import(D->getInitExpr());
2288 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002289 return nullptr;
2290
Douglas Gregor98c10182010-02-12 22:17:39 +00002291 EnumConstantDecl *ToEnumerator
2292 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2293 Name.getAsIdentifierInfo(), T,
2294 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002295 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002296 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002297 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002298 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002299 return ToEnumerator;
2300}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002301
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002302bool ASTNodeImporter::ImportTemplateInformation(FunctionDecl *FromFD,
2303 FunctionDecl *ToFD) {
2304 switch (FromFD->getTemplatedKind()) {
2305 case FunctionDecl::TK_NonTemplate:
2306 case FunctionDecl::TK_FunctionTemplate:
Sam McCallfdc32072018-01-26 12:06:44 +00002307 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002308
2309 case FunctionDecl::TK_MemberSpecialization: {
2310 auto *InstFD = cast_or_null<FunctionDecl>(
2311 Importer.Import(FromFD->getInstantiatedFromMemberFunction()));
2312 if (!InstFD)
2313 return true;
2314
2315 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
2316 SourceLocation POI = Importer.Import(
2317 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation());
2318 ToFD->setInstantiationOfMemberFunction(InstFD, TSK);
2319 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002320 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002321 }
2322
2323 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Gabor Marton5254e642018-06-27 13:32:50 +00002324 FunctionTemplateDecl* Template;
2325 OptionalTemplateArgsTy ToTemplArgs;
2326 std::tie(Template, ToTemplArgs) =
2327 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
2328 if (!Template || !ToTemplArgs)
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002329 return true;
2330
2331 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Gabor Marton5254e642018-06-27 13:32:50 +00002332 Importer.getToContext(), *ToTemplArgs);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002333
Gabor Marton5254e642018-06-27 13:32:50 +00002334 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002335 TemplateArgumentListInfo ToTAInfo;
2336 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002337 if (FromTAArgsAsWritten)
2338 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002339 return true;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002340
2341 SourceLocation POI = Importer.Import(FTSInfo->getPointOfInstantiation());
2342
Gabor Marton5254e642018-06-27 13:32:50 +00002343 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002344 ToFD->setFunctionTemplateSpecialization(
2345 Template, ToTAList, /* InsertPos= */ nullptr,
2346 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002347 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002348 }
2349
2350 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2351 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2352 UnresolvedSet<8> TemplDecls;
2353 unsigned NumTemplates = FromInfo->getNumTemplates();
2354 for (unsigned I = 0; I < NumTemplates; I++) {
2355 if (auto *ToFTD = cast_or_null<FunctionTemplateDecl>(
2356 Importer.Import(FromInfo->getTemplate(I))))
2357 TemplDecls.addDecl(ToFTD);
2358 else
2359 return true;
2360 }
2361
2362 // Import TemplateArgumentListInfo.
2363 TemplateArgumentListInfo ToTAInfo;
2364 if (ImportTemplateArgumentListInfo(
2365 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2366 llvm::makeArrayRef(FromInfo->getTemplateArgs(),
2367 FromInfo->getNumTemplateArgs()),
2368 ToTAInfo))
2369 return true;
2370
2371 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2372 TemplDecls, ToTAInfo);
Sam McCallfdc32072018-01-26 12:06:44 +00002373 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002374 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002375 }
Sam McCallfdc32072018-01-26 12:06:44 +00002376 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002377}
2378
Gabor Marton5254e642018-06-27 13:32:50 +00002379FunctionDecl *
2380ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
2381 FunctionTemplateDecl* Template;
2382 OptionalTemplateArgsTy ToTemplArgs;
2383 std::tie(Template, ToTemplArgs) =
2384 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
2385 if (!Template || !ToTemplArgs)
2386 return nullptr;
2387
2388 void *InsertPos = nullptr;
2389 auto *FoundSpec = Template->findSpecialization(*ToTemplArgs, InsertPos);
2390 return FoundSpec;
2391}
2392
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002393Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00002394
2395 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
2396 auto RedeclIt = Redecls.begin();
2397 // Import the first part of the decl chain. I.e. import all previous
2398 // declarations starting from the canonical decl.
2399 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt)
2400 if (!Importer.Import(*RedeclIt))
2401 return nullptr;
2402 assert(*RedeclIt == D);
2403
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002404 // Import the major distinguishing characteristics of this function.
2405 DeclContext *DC, *LexicalDC;
2406 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002407 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002408 NamedDecl *ToD;
2409 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002410 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002411 if (ToD)
2412 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002413
Gabor Marton5254e642018-06-27 13:32:50 +00002414 const FunctionDecl *FoundByLookup = nullptr;
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002415
Gabor Marton5254e642018-06-27 13:32:50 +00002416 // If this is a function template specialization, then try to find the same
2417 // existing specialization in the "to" context. The localUncachedLookup
2418 // below will not find any specialization, but would find the primary
2419 // template; thus, we have to skip normal lookup in case of specializations.
2420 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
2421 if (D->getTemplatedKind() ==
2422 FunctionDecl::TK_FunctionTemplateSpecialization) {
2423 if (FunctionDecl *FoundFunction = FindFunctionTemplateSpecialization(D)) {
2424 if (D->doesThisDeclarationHaveABody() &&
2425 FoundFunction->hasBody())
2426 return Importer.Imported(D, FoundFunction);
2427 FoundByLookup = FoundFunction;
2428 }
2429 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002430 // Try to find a function in our own ("to") context with the same name, same
2431 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00002432 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002433 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00002434 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002435 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002436 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002437 for (auto *FoundDecl : FoundDecls) {
2438 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002439 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002440
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002441 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002442 if (FoundFunction->hasExternalFormalLinkage() &&
2443 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002444 if (Importer.IsStructurallyEquivalent(D->getType(),
2445 FoundFunction->getType())) {
Gabor Marton5254e642018-06-27 13:32:50 +00002446 if (D->doesThisDeclarationHaveABody() &&
2447 FoundFunction->hasBody())
2448 return Importer.Imported(D, FoundFunction);
2449 FoundByLookup = FoundFunction;
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002450 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002451 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002452
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002453 // FIXME: Check for overloading more carefully, e.g., by boosting
2454 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002455
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002456 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002457 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002458 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002459
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002460 // Complain about inconsistent function types.
2461 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002462 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002463 Importer.ToDiag(FoundFunction->getLocation(),
2464 diag::note_odr_value_here)
2465 << FoundFunction->getType();
2466 }
2467 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002468
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002469 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002470 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002471
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002472 if (!ConflictingDecls.empty()) {
2473 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2474 ConflictingDecls.data(),
2475 ConflictingDecls.size());
2476 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002477 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002478 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002479 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002480
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002481 DeclarationNameInfo NameInfo(Name, Loc);
2482 // Import additional name location/type info.
2483 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2484
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002485 QualType FromTy = D->getType();
2486 bool usedDifferentExceptionSpec = false;
2487
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002488 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002489 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2490 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2491 // FunctionDecl that we are importing the FunctionProtoType for.
2492 // To avoid an infinite recursion when importing, create the FunctionDecl
2493 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002494 if (FromEPI.ExceptionSpec.SourceDecl ||
2495 FromEPI.ExceptionSpec.SourceTemplate ||
2496 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002497 FunctionProtoType::ExtProtoInfo DefaultEPI;
2498 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002499 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002500 usedDifferentExceptionSpec = true;
2501 }
2502 }
2503
Douglas Gregorb4964f72010-02-15 23:54:17 +00002504 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002505 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002506 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002507 return nullptr;
2508
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002509 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002510 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00002511 for (auto P : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002512 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002513 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002514 return nullptr;
2515
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002516 Parameters.push_back(ToP);
2517 }
2518
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002519 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002520 if (D->getTypeSourceInfo() && !TInfo)
2521 return nullptr;
2522
2523 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00002524 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002525 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002526 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregor00eace12010-02-21 18:29:16 +00002527 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2528 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002529 InnerLocStart,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002530 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002531 FromConstructor->isExplicit(),
2532 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002533 D->isImplicit(),
2534 D->isConstexpr());
Sean Callanandd2c1742016-05-16 20:48:03 +00002535 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
2536 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002537 for (auto *I : FromConstructor->inits()) {
2538 auto *ToI = cast_or_null<CXXCtorInitializer>(Importer.Import(I));
Sean Callanandd2c1742016-05-16 20:48:03 +00002539 if (!ToI && I)
2540 return nullptr;
2541 CtorInitializers.push_back(ToI);
2542 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002543 auto **Memory =
Sean Callanandd2c1742016-05-16 20:48:03 +00002544 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
2545 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002546 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
Sean Callanandd2c1742016-05-16 20:48:03 +00002547 ToCtor->setCtorInitializers(Memory);
2548 ToCtor->setNumCtorInitializers(NumInitializers);
2549 }
Douglas Gregor00eace12010-02-21 18:29:16 +00002550 } else if (isa<CXXDestructorDecl>(D)) {
2551 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2552 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002553 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002554 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002555 D->isInlineSpecified(),
2556 D->isImplicit());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002557 } else if (auto *FromConversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor00eace12010-02-21 18:29:16 +00002558 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2559 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002560 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002561 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002562 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002563 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002564 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002565 Importer.Import(D->getLocEnd()));
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002566 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregora50ad132010-11-29 16:04:58 +00002567 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2568 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002569 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00002570 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002571 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002572 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002573 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002574 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002575 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002576 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00002577 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002578 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002579 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002580 D->hasWrittenPrototype(),
2581 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002582 }
John McCall3e11ebe2010-03-15 10:12:16 +00002583
2584 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002585 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002586 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002587 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002588 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2589 ToFunction->setTrivial(D->isTrivial());
2590 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002591 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002592
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002593 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002594 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002595 Param->setOwningFunction(ToFunction);
2596 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002597 }
David Blaikie9c70e042011-09-21 18:16:56 +00002598 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002599
Gabor Marton5254e642018-06-27 13:32:50 +00002600 if (FoundByLookup) {
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002601 auto *Recent = const_cast<FunctionDecl *>(
Gabor Marton5254e642018-06-27 13:32:50 +00002602 FoundByLookup->getMostRecentDecl());
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002603 ToFunction->setPreviousDecl(Recent);
2604 }
2605
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002606 // We need to complete creation of FunctionProtoTypeLoc manually with setting
2607 // params it refers to.
2608 if (TInfo) {
2609 if (auto ProtoLoc =
2610 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
2611 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
2612 ProtoLoc.setParam(I, Parameters[I]);
2613 }
2614 }
2615
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002616 if (usedDifferentExceptionSpec) {
2617 // Update FunctionProtoType::ExtProtoInfo.
2618 QualType T = Importer.Import(D->getType());
2619 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002620 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002621 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002622 }
2623
Gabor Marton5254e642018-06-27 13:32:50 +00002624 if (D->doesThisDeclarationHaveABody()) {
2625 if (Stmt *FromBody = D->getBody()) {
2626 if (Stmt *ToBody = Importer.Import(FromBody)) {
2627 ToFunction->setBody(ToBody);
2628 }
Sean Callanan59721b32015-04-28 18:41:46 +00002629 }
2630 }
2631
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002632 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002633
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002634 // If it is a template, import all related things.
2635 if (ImportTemplateInformation(D, ToFunction))
2636 return nullptr;
2637
Gabor Marton5254e642018-06-27 13:32:50 +00002638 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
2639
2640 // TODO Can we generalize this approach to other AST nodes as well?
2641 if (D->getDeclContext()->containsDeclAndLoad(D))
2642 DC->addDeclInternal(ToFunction);
2643 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002644 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002645
Gabor Marton5254e642018-06-27 13:32:50 +00002646 // Friend declaration's lexical context is the befriending class, but the
2647 // semantic context is the enclosing scope of the befriending class.
2648 // We want the friend functions to be found in the semantic context by lookup.
2649 // FIXME should we handle this generically in VisitFriendDecl?
2650 // In Other cases when LexicalDC != DC we don't want it to be added,
2651 // e.g out-of-class definitions like void B::f() {} .
2652 if (LexicalDC != DC && IsFriend) {
2653 DC->makeDeclVisibleInContext(ToFunction);
2654 }
2655
2656 // Import the rest of the chain. I.e. import all subsequent declarations.
2657 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt)
2658 if (!Importer.Import(*RedeclIt))
2659 return nullptr;
2660
Lang Hames19e07e12017-06-20 21:06:00 +00002661 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2662 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2663
Douglas Gregor43f54792010-02-17 02:12:47 +00002664 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002665}
2666
Douglas Gregor00eace12010-02-21 18:29:16 +00002667Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2668 return VisitFunctionDecl(D);
2669}
2670
2671Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2672 return VisitCXXMethodDecl(D);
2673}
2674
2675Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2676 return VisitCXXMethodDecl(D);
2677}
2678
2679Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2680 return VisitCXXMethodDecl(D);
2681}
2682
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002683static unsigned getFieldIndex(Decl *F) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002684 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002685 if (!Owner)
2686 return 0;
2687
2688 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002689 for (const auto *D : Owner->noload_decls()) {
2690 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002691 return Index;
2692
2693 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2694 ++Index;
2695 }
2696
2697 return Index;
2698}
2699
Douglas Gregor5c73e912010-02-11 00:48:18 +00002700Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2701 // Import the major distinguishing characteristics of a variable.
2702 DeclContext *DC, *LexicalDC;
2703 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002704 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002705 NamedDecl *ToD;
2706 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002707 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002708 if (ToD)
2709 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002710
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002711 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002712 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002713 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002714 for (auto *FoundDecl : FoundDecls) {
2715 if (auto *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002716 // For anonymous fields, match up by index.
2717 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2718 continue;
2719
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002720 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002721 FoundField->getType())) {
2722 Importer.Imported(D, FoundField);
2723 return FoundField;
2724 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002725
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002726 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2727 << Name << D->getType() << FoundField->getType();
2728 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2729 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002730 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002731 }
2732 }
2733
Douglas Gregorb4964f72010-02-15 23:54:17 +00002734 // Import the type.
2735 QualType T = Importer.Import(D->getType());
2736 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002737 return nullptr;
2738
Douglas Gregor5c73e912010-02-11 00:48:18 +00002739 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2740 Expr *BitWidth = Importer.Import(D->getBitWidth());
2741 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002742 return nullptr;
2743
Abramo Bagnaradff19302011-03-08 08:55:46 +00002744 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2745 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002746 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002747 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002748 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002749 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002750 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00002751 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00002752 Expr *ToInitializer = Importer.Import(FromInitializer);
2753 if (ToInitializer)
2754 ToField->setInClassInitializer(ToInitializer);
2755 else
2756 return nullptr;
2757 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002758 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002759 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002760 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002761 return ToField;
2762}
2763
Francois Pichet783dd6e2010-11-21 06:08:52 +00002764Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2765 // Import the major distinguishing characteristics of a variable.
2766 DeclContext *DC, *LexicalDC;
2767 DeclarationName Name;
2768 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002769 NamedDecl *ToD;
2770 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002771 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002772 if (ToD)
2773 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002774
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002775 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002776 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002777 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002778 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002779 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002780 // For anonymous indirect fields, match up by index.
2781 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2782 continue;
2783
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002784 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002785 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002786 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002787 Importer.Imported(D, FoundField);
2788 return FoundField;
2789 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002790
2791 // If there are more anonymous fields to check, continue.
2792 if (!Name && I < N-1)
2793 continue;
2794
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002795 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2796 << Name << D->getType() << FoundField->getType();
2797 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2798 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002799 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002800 }
2801 }
2802
Francois Pichet783dd6e2010-11-21 06:08:52 +00002803 // Import the type.
2804 QualType T = Importer.Import(D->getType());
2805 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002806 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002807
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002808 auto **NamedChain =
2809 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00002810
2811 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00002812 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00002813 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002814 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00002815 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002816 NamedChain[i++] = cast<NamedDecl>(D);
2817 }
2818
2819 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00002820 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
David Majnemer59f77922016-06-24 04:05:48 +00002821 {NamedChain, D->getChainingSize()});
Aaron Ballman260995b2014-10-15 16:58:18 +00002822
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00002823 for (const auto *A : D->attrs())
2824 ToIndirectField->addAttr(Importer.Import(A));
Aaron Ballman260995b2014-10-15 16:58:18 +00002825
Francois Pichet783dd6e2010-11-21 06:08:52 +00002826 ToIndirectField->setAccess(D->getAccess());
2827 ToIndirectField->setLexicalDeclContext(LexicalDC);
2828 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002829 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002830 return ToIndirectField;
2831}
2832
Aleksei Sidorina693b372016-09-28 10:16:56 +00002833Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2834 // Import the major distinguishing characteristics of a declaration.
2835 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2836 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2837 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2838 if (!DC || !LexicalDC)
2839 return nullptr;
2840
2841 // Determine whether we've already imported this decl.
2842 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2843 auto *RD = cast<CXXRecordDecl>(DC);
2844 FriendDecl *ImportedFriend = RD->getFirstFriend();
2845 StructuralEquivalenceContext Context(
2846 Importer.getFromContext(), Importer.getToContext(),
2847 Importer.getNonEquivalentDecls(), false, false);
2848
2849 while (ImportedFriend) {
2850 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2851 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2852 ImportedFriend->getFriendDecl()))
2853 return Importer.Imported(D, ImportedFriend);
2854
2855 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2856 if (Importer.IsStructurallyEquivalent(
2857 D->getFriendType()->getType(),
2858 ImportedFriend->getFriendType()->getType(), true))
2859 return Importer.Imported(D, ImportedFriend);
2860 }
2861 ImportedFriend = ImportedFriend->getNextFriend();
2862 }
2863
2864 // Not found. Create it.
2865 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00002866 if (NamedDecl *FriendD = D->getFriendDecl()) {
2867 auto *ToFriendD = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2868 if (ToFriendD && FriendD->getFriendObjectKind() != Decl::FOK_None &&
2869 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
2870 ToFriendD->setObjectOfFriendDecl(false);
2871
2872 ToFU = ToFriendD;
2873 } else // The friend is a type, not a decl.
Aleksei Sidorina693b372016-09-28 10:16:56 +00002874 ToFU = Importer.Import(D->getFriendType());
2875 if (!ToFU)
2876 return nullptr;
2877
2878 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002879 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002880 for (unsigned I = 0; I < D->NumTPLists; I++) {
2881 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2882 if (!List)
2883 return nullptr;
2884 ToTPLists[I] = List;
2885 }
2886
2887 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
2888 Importer.Import(D->getLocation()),
2889 ToFU, Importer.Import(D->getFriendLoc()),
2890 ToTPLists);
2891
2892 Importer.Imported(D, FrD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002893
2894 FrD->setAccess(D->getAccess());
2895 FrD->setLexicalDeclContext(LexicalDC);
2896 LexicalDC->addDeclInternal(FrD);
2897 return FrD;
2898}
2899
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002900Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2901 // Import the major distinguishing characteristics of an ivar.
2902 DeclContext *DC, *LexicalDC;
2903 DeclarationName Name;
2904 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002905 NamedDecl *ToD;
2906 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002907 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002908 if (ToD)
2909 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002910
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002911 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002912 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002913 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002914 for (auto *FoundDecl : FoundDecls) {
2915 if (auto *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002916 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002917 FoundIvar->getType())) {
2918 Importer.Imported(D, FoundIvar);
2919 return FoundIvar;
2920 }
2921
2922 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2923 << Name << D->getType() << FoundIvar->getType();
2924 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2925 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002926 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002927 }
2928 }
2929
2930 // Import the type.
2931 QualType T = Importer.Import(D->getType());
2932 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002933 return nullptr;
2934
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002935 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2936 Expr *BitWidth = Importer.Import(D->getBitWidth());
2937 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002938 return nullptr;
2939
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002940 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2941 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002942 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002943 Loc, Name.getAsIdentifierInfo(),
2944 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00002945 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002946 ToIvar->setLexicalDeclContext(LexicalDC);
2947 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002948 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002949 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002950}
2951
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002952Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2953 // Import the major distinguishing characteristics of a variable.
2954 DeclContext *DC, *LexicalDC;
2955 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002956 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002957 NamedDecl *ToD;
2958 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002959 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002960 if (ToD)
2961 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002962
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002963 // Try to find a variable in our own ("to") context with the same name and
2964 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002965 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00002966 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002967 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002968 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002969 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002970 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002971 for (auto *FoundDecl : FoundDecls) {
2972 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002973 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002974
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002975 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002976 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00002977 if (FoundVar->hasExternalFormalLinkage() &&
2978 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002979 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002980 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002981 MergeWithVar = FoundVar;
2982 break;
2983 }
2984
Douglas Gregor56521c52010-02-12 17:23:39 +00002985 const ArrayType *FoundArray
2986 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2987 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002988 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002989 if (FoundArray && TArray) {
2990 if (isa<IncompleteArrayType>(FoundArray) &&
2991 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002992 // Import the type.
2993 QualType T = Importer.Import(D->getType());
2994 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002995 return nullptr;
2996
Douglas Gregor56521c52010-02-12 17:23:39 +00002997 FoundVar->setType(T);
2998 MergeWithVar = FoundVar;
2999 break;
3000 } else if (isa<IncompleteArrayType>(TArray) &&
3001 isa<ConstantArrayType>(FoundArray)) {
3002 MergeWithVar = FoundVar;
3003 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003004 }
3005 }
3006
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003007 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003008 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003009 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3010 << FoundVar->getType();
3011 }
3012 }
3013
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003014 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003015 }
3016
3017 if (MergeWithVar) {
3018 // An equivalent variable with external linkage has been found. Link
3019 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003020 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003021
3022 if (VarDecl *DDef = D->getDefinition()) {
3023 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3024 Importer.ToDiag(ExistingDef->getLocation(),
3025 diag::err_odr_variable_multiple_def)
3026 << Name;
3027 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3028 } else {
3029 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003030 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003031 if (DDef->isInitKnownICE()) {
3032 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3033 Eval->CheckedICE = true;
3034 Eval->IsICE = DDef->isInitICE();
3035 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003036 }
3037 }
3038
3039 return MergeWithVar;
3040 }
3041
3042 if (!ConflictingDecls.empty()) {
3043 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3044 ConflictingDecls.data(),
3045 ConflictingDecls.size());
3046 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003047 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003048 }
3049 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003050
Douglas Gregorb4964f72010-02-15 23:54:17 +00003051 // Import the type.
3052 QualType T = Importer.Import(D->getType());
3053 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003054 return nullptr;
3055
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003056 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003057 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003058 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3059 Importer.Import(D->getInnerLocStart()),
3060 Loc, Name.getAsIdentifierInfo(),
3061 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003062 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00003063 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003064 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003065 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003066 Importer.Imported(D, ToVar);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003067
3068 // Templated declarations should never appear in the enclosing DeclContext.
3069 if (!D->getDescribedVarTemplate())
3070 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003071
Sean Callanan59721b32015-04-28 18:41:46 +00003072 if (!D->isFileVarDecl() &&
3073 D->isUsed())
3074 ToVar->setIsUsed();
3075
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003076 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003077 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00003078 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003079
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003080 if (D->isConstexpr())
3081 ToVar->setConstexpr(true);
3082
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003083 return ToVar;
3084}
3085
Douglas Gregor8b228d72010-02-17 21:22:52 +00003086Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3087 // Parameters are created in the translation unit's context, then moved
3088 // into the function declaration's context afterward.
3089 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3090
3091 // Import the name of this declaration.
3092 DeclarationName Name = Importer.Import(D->getDeclName());
3093 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003094 return nullptr;
3095
Douglas Gregor8b228d72010-02-17 21:22:52 +00003096 // Import the location of this declaration.
3097 SourceLocation Loc = Importer.Import(D->getLocation());
3098
3099 // Import the parameter's type.
3100 QualType T = Importer.Import(D->getType());
3101 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003102 return nullptr;
3103
Douglas Gregor8b228d72010-02-17 21:22:52 +00003104 // Create the imported parameter.
Alexey Bataev56223232017-06-09 13:40:18 +00003105 auto *ToParm = ImplicitParamDecl::Create(Importer.getToContext(), DC, Loc,
3106 Name.getAsIdentifierInfo(), T,
3107 D->getParameterKind());
Douglas Gregor8b228d72010-02-17 21:22:52 +00003108 return Importer.Imported(D, ToParm);
3109}
3110
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003111Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3112 // Parameters are created in the translation unit's context, then moved
3113 // into the function declaration's context afterward.
3114 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3115
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003116 // Import the name of this declaration.
3117 DeclarationName Name = Importer.Import(D->getDeclName());
3118 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003119 return nullptr;
3120
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003121 // Import the location of this declaration.
3122 SourceLocation Loc = Importer.Import(D->getLocation());
3123
3124 // Import the parameter's type.
3125 QualType T = Importer.Import(D->getType());
3126 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003127 return nullptr;
3128
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003129 // Create the imported parameter.
3130 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3131 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003132 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003133 Loc, Name.getAsIdentifierInfo(),
3134 T, TInfo, D->getStorageClass(),
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003135 /*DefaultArg*/ nullptr);
3136
3137 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003138 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003139 ToParm->setKNRPromoted(D->isKNRPromoted());
3140
3141 Expr *ToDefArg = nullptr;
3142 Expr *FromDefArg = nullptr;
3143 if (D->hasUninstantiatedDefaultArg()) {
3144 FromDefArg = D->getUninstantiatedDefaultArg();
3145 ToDefArg = Importer.Import(FromDefArg);
3146 ToParm->setUninstantiatedDefaultArg(ToDefArg);
3147 } else if (D->hasUnparsedDefaultArg()) {
3148 ToParm->setUnparsedDefaultArg();
3149 } else if (D->hasDefaultArg()) {
3150 FromDefArg = D->getDefaultArg();
3151 ToDefArg = Importer.Import(FromDefArg);
3152 ToParm->setDefaultArg(ToDefArg);
3153 }
3154 if (FromDefArg && !ToDefArg)
3155 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003156
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003157 if (D->isObjCMethodParameter()) {
3158 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3159 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3160 } else {
3161 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3162 D->getFunctionScopeIndex());
3163 }
3164
Sean Callanan59721b32015-04-28 18:41:46 +00003165 if (D->isUsed())
3166 ToParm->setIsUsed();
3167
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003168 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003169}
3170
Douglas Gregor43f54792010-02-17 02:12:47 +00003171Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3172 // Import the major distinguishing characteristics of a method.
3173 DeclContext *DC, *LexicalDC;
3174 DeclarationName Name;
3175 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003176 NamedDecl *ToD;
3177 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003178 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003179 if (ToD)
3180 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003181
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003182 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003183 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003184 for (auto *FoundDecl : FoundDecls) {
3185 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003186 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3187 continue;
3188
3189 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003190 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3191 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003192 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003193 << D->isInstanceMethod() << Name << D->getReturnType()
3194 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003195 Importer.ToDiag(FoundMethod->getLocation(),
3196 diag::note_odr_objc_method_here)
3197 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003198 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003199 }
3200
3201 // Check the number of parameters.
3202 if (D->param_size() != FoundMethod->param_size()) {
3203 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3204 << D->isInstanceMethod() << Name
3205 << D->param_size() << FoundMethod->param_size();
3206 Importer.ToDiag(FoundMethod->getLocation(),
3207 diag::note_odr_objc_method_here)
3208 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003209 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003210 }
3211
3212 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003213 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003214 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3215 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003216 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003217 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003218 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003219 diag::err_odr_objc_method_param_type_inconsistent)
3220 << D->isInstanceMethod() << Name
3221 << (*P)->getType() << (*FoundP)->getType();
3222 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3223 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003224 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003225 }
3226 }
3227
3228 // Check variadic/non-variadic.
3229 // Check the number of parameters.
3230 if (D->isVariadic() != FoundMethod->isVariadic()) {
3231 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3232 << D->isInstanceMethod() << Name;
3233 Importer.ToDiag(FoundMethod->getLocation(),
3234 diag::note_odr_objc_method_here)
3235 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003236 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003237 }
3238
3239 // FIXME: Any other bits we need to merge?
3240 return Importer.Imported(D, FoundMethod);
3241 }
3242 }
3243
3244 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003245 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003246 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003247 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003248
Alp Toker314cc812014-01-25 16:55:45 +00003249 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003250
Alp Toker314cc812014-01-25 16:55:45 +00003251 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3252 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3253 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3254 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3255 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003256
3257 // FIXME: When we decide to merge method definitions, we'll need to
3258 // deal with implicit parameters.
3259
3260 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003261 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003262 for (auto *FromP : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003263 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003264 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003265 return nullptr;
3266
Douglas Gregor43f54792010-02-17 02:12:47 +00003267 ToParams.push_back(ToP);
3268 }
3269
3270 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003271 for (auto *ToParam : ToParams) {
3272 ToParam->setOwningFunction(ToMethod);
3273 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003274 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003275
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003276 SmallVector<SourceLocation, 12> SelLocs;
3277 D->getSelectorLocs(SelLocs);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003278 for (auto &Loc : SelLocs)
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003279 Loc = Importer.Import(Loc);
3280
3281 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003282
3283 ToMethod->setLexicalDeclContext(LexicalDC);
3284 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003285 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003286 return ToMethod;
3287}
3288
Douglas Gregor85f3f952015-07-07 03:57:15 +00003289Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3290 // Import the major distinguishing characteristics of a category.
3291 DeclContext *DC, *LexicalDC;
3292 DeclarationName Name;
3293 SourceLocation Loc;
3294 NamedDecl *ToD;
3295 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3296 return nullptr;
3297 if (ToD)
3298 return ToD;
3299
3300 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3301 if (!BoundInfo)
3302 return nullptr;
3303
3304 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3305 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003306 D->getVariance(),
3307 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003308 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003309 Importer.Import(D->getLocation()),
3310 Name.getAsIdentifierInfo(),
3311 Importer.Import(D->getColonLoc()),
3312 BoundInfo);
3313 Importer.Imported(D, Result);
3314 Result->setLexicalDeclContext(LexicalDC);
3315 return Result;
3316}
3317
Douglas Gregor84c51c32010-02-18 01:47:50 +00003318Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3319 // Import the major distinguishing characteristics of a category.
3320 DeclContext *DC, *LexicalDC;
3321 DeclarationName Name;
3322 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003323 NamedDecl *ToD;
3324 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003325 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003326 if (ToD)
3327 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003328
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003329 auto *ToInterface =
3330 cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003331 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003332 return nullptr;
3333
Douglas Gregor84c51c32010-02-18 01:47:50 +00003334 // Determine if we've already encountered this category.
3335 ObjCCategoryDecl *MergeWithCategory
3336 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3337 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3338 if (!ToCategory) {
3339 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003340 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003341 Loc,
3342 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003343 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003344 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003345 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003346 Importer.Import(D->getIvarLBraceLoc()),
3347 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003348 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003349 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003350 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003351 // Import the type parameter list after calling Imported, to avoid
3352 // loops when bringing in their DeclContext.
3353 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3354 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003355
Douglas Gregor84c51c32010-02-18 01:47:50 +00003356 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003357 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3358 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003359 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3360 = D->protocol_loc_begin();
3361 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3362 FromProtoEnd = D->protocol_end();
3363 FromProto != FromProtoEnd;
3364 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003365 auto *ToProto =
3366 cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003367 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003368 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003369 Protocols.push_back(ToProto);
3370 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3371 }
3372
3373 // FIXME: If we're merging, make sure that the protocol list is the same.
3374 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3375 ProtocolLocs.data(), Importer.getToContext());
Douglas Gregor84c51c32010-02-18 01:47:50 +00003376 } else {
3377 Importer.Imported(D, ToCategory);
3378 }
3379
3380 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003381 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003382
3383 // If we have an implementation, import it as well.
3384 if (D->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003385 auto *Impl =
3386 cast_or_null<ObjCCategoryImplDecl>(
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003387 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003388 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003389 return nullptr;
3390
Douglas Gregor84c51c32010-02-18 01:47:50 +00003391 ToCategory->setImplementation(Impl);
3392 }
3393
3394 return ToCategory;
3395}
3396
Douglas Gregor2aa53772012-01-24 17:42:07 +00003397bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3398 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003399 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003400 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003401 if (shouldForceImportDeclContext(Kind))
3402 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003403 return false;
3404 }
3405
3406 // Start the protocol definition
3407 To->startDefinition();
3408
3409 // Import protocols
3410 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3411 SmallVector<SourceLocation, 4> ProtocolLocs;
3412 ObjCProtocolDecl::protocol_loc_iterator
3413 FromProtoLoc = From->protocol_loc_begin();
3414 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3415 FromProtoEnd = From->protocol_end();
3416 FromProto != FromProtoEnd;
3417 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003418 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003419 if (!ToProto)
3420 return true;
3421 Protocols.push_back(ToProto);
3422 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3423 }
3424
3425 // FIXME: If we're merging, make sure that the protocol list is the same.
3426 To->setProtocolList(Protocols.data(), Protocols.size(),
3427 ProtocolLocs.data(), Importer.getToContext());
3428
Douglas Gregor2e15c842012-02-01 21:00:38 +00003429 if (shouldForceImportDeclContext(Kind)) {
3430 // Import all of the members of this protocol.
3431 ImportDeclContext(From, /*ForceImport=*/true);
3432 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003433 return false;
3434}
3435
Douglas Gregor98d156a2010-02-17 16:12:00 +00003436Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003437 // If this protocol has a definition in the translation unit we're coming
3438 // from, but this particular declaration is not that definition, import the
3439 // definition and map to that.
3440 ObjCProtocolDecl *Definition = D->getDefinition();
3441 if (Definition && Definition != D) {
3442 Decl *ImportedDef = Importer.Import(Definition);
3443 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003444 return nullptr;
3445
Douglas Gregor2aa53772012-01-24 17:42:07 +00003446 return Importer.Imported(D, ImportedDef);
3447 }
3448
Douglas Gregor84c51c32010-02-18 01:47:50 +00003449 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003450 DeclContext *DC, *LexicalDC;
3451 DeclarationName Name;
3452 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003453 NamedDecl *ToD;
3454 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003455 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003456 if (ToD)
3457 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003458
Craig Topper36250ad2014-05-12 05:36:57 +00003459 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003460 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003461 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003462 for (auto *FoundDecl : FoundDecls) {
3463 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003464 continue;
3465
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003466 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003467 break;
3468 }
3469
3470 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003471 if (!ToProto) {
3472 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3473 Name.getAsIdentifierInfo(), Loc,
3474 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003475 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003476 ToProto->setLexicalDeclContext(LexicalDC);
3477 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003478 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003479
3480 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003481
Douglas Gregor2aa53772012-01-24 17:42:07 +00003482 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003483 return nullptr;
3484
Douglas Gregor98d156a2010-02-17 16:12:00 +00003485 return ToProto;
3486}
3487
Sean Callanan0aae0412014-12-10 00:00:37 +00003488Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3489 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3490 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3491
3492 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3493 SourceLocation LangLoc = Importer.Import(D->getLocation());
3494
3495 bool HasBraces = D->hasBraces();
3496
Sean Callananb12a8552014-12-10 21:22:20 +00003497 LinkageSpecDecl *ToLinkageSpec =
3498 LinkageSpecDecl::Create(Importer.getToContext(),
3499 DC,
3500 ExternLoc,
3501 LangLoc,
3502 D->getLanguage(),
3503 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003504
3505 if (HasBraces) {
3506 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3507 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3508 }
3509
3510 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3511 LexicalDC->addDeclInternal(ToLinkageSpec);
3512
3513 Importer.Imported(D, ToLinkageSpec);
3514
3515 return ToLinkageSpec;
3516}
3517
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003518Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
3519 DeclContext *DC, *LexicalDC;
3520 DeclarationName Name;
3521 SourceLocation Loc;
3522 NamedDecl *ToD = nullptr;
3523 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3524 return nullptr;
3525 if (ToD)
3526 return ToD;
3527
3528 DeclarationNameInfo NameInfo(Name,
3529 Importer.Import(D->getNameInfo().getLoc()));
3530 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3531
3532 UsingDecl *ToUsing = UsingDecl::Create(Importer.getToContext(), DC,
3533 Importer.Import(D->getUsingLoc()),
3534 Importer.Import(D->getQualifierLoc()),
3535 NameInfo, D->hasTypename());
3536 ToUsing->setLexicalDeclContext(LexicalDC);
3537 LexicalDC->addDeclInternal(ToUsing);
3538 Importer.Imported(D, ToUsing);
3539
3540 if (NamedDecl *FromPattern =
3541 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003542 if (auto *ToPattern =
3543 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003544 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern);
3545 else
3546 return nullptr;
3547 }
3548
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003549 for (auto *FromShadow : D->shadows()) {
3550 if (auto *ToShadow =
3551 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003552 ToUsing->addShadowDecl(ToShadow);
3553 else
3554 // FIXME: We return a nullptr here but the definition is already created
3555 // and available with lookups. How to fix this?..
3556 return nullptr;
3557 }
3558 return ToUsing;
3559}
3560
3561Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
3562 DeclContext *DC, *LexicalDC;
3563 DeclarationName Name;
3564 SourceLocation Loc;
3565 NamedDecl *ToD = nullptr;
3566 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3567 return nullptr;
3568 if (ToD)
3569 return ToD;
3570
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003571 auto *ToUsing = dyn_cast_or_null<UsingDecl>(
3572 Importer.Import(D->getUsingDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003573 if (!ToUsing)
3574 return nullptr;
3575
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003576 auto *ToTarget = dyn_cast_or_null<NamedDecl>(
3577 Importer.Import(D->getTargetDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003578 if (!ToTarget)
3579 return nullptr;
3580
3581 UsingShadowDecl *ToShadow = UsingShadowDecl::Create(
3582 Importer.getToContext(), DC, Loc, ToUsing, ToTarget);
3583
3584 ToShadow->setLexicalDeclContext(LexicalDC);
3585 ToShadow->setAccess(D->getAccess());
3586 Importer.Imported(D, ToShadow);
3587
3588 if (UsingShadowDecl *FromPattern =
3589 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003590 if (auto *ToPattern =
3591 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003592 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow,
3593 ToPattern);
3594 else
3595 // FIXME: We return a nullptr here but the definition is already created
3596 // and available with lookups. How to fix this?..
3597 return nullptr;
3598 }
3599
3600 LexicalDC->addDeclInternal(ToShadow);
3601
3602 return ToShadow;
3603}
3604
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003605Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
3606 DeclContext *DC, *LexicalDC;
3607 DeclarationName Name;
3608 SourceLocation Loc;
3609 NamedDecl *ToD = nullptr;
3610 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3611 return nullptr;
3612 if (ToD)
3613 return ToD;
3614
3615 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor());
3616 if (!ToComAncestor)
3617 return nullptr;
3618
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003619 auto *ToNominated = cast_or_null<NamespaceDecl>(
3620 Importer.Import(D->getNominatedNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003621 if (!ToNominated)
3622 return nullptr;
3623
3624 UsingDirectiveDecl *ToUsingDir = UsingDirectiveDecl::Create(
3625 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3626 Importer.Import(D->getNamespaceKeyLocation()),
3627 Importer.Import(D->getQualifierLoc()),
3628 Importer.Import(D->getIdentLocation()), ToNominated, ToComAncestor);
3629 ToUsingDir->setLexicalDeclContext(LexicalDC);
3630 LexicalDC->addDeclInternal(ToUsingDir);
3631 Importer.Imported(D, ToUsingDir);
3632
3633 return ToUsingDir;
3634}
3635
3636Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl(
3637 UnresolvedUsingValueDecl *D) {
3638 DeclContext *DC, *LexicalDC;
3639 DeclarationName Name;
3640 SourceLocation Loc;
3641 NamedDecl *ToD = nullptr;
3642 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3643 return nullptr;
3644 if (ToD)
3645 return ToD;
3646
3647 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc()));
3648 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3649
3650 UnresolvedUsingValueDecl *ToUsingValue = UnresolvedUsingValueDecl::Create(
3651 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3652 Importer.Import(D->getQualifierLoc()), NameInfo,
3653 Importer.Import(D->getEllipsisLoc()));
3654
3655 Importer.Imported(D, ToUsingValue);
3656 ToUsingValue->setAccess(D->getAccess());
3657 ToUsingValue->setLexicalDeclContext(LexicalDC);
3658 LexicalDC->addDeclInternal(ToUsingValue);
3659
3660 return ToUsingValue;
3661}
3662
3663Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
3664 UnresolvedUsingTypenameDecl *D) {
3665 DeclContext *DC, *LexicalDC;
3666 DeclarationName Name;
3667 SourceLocation Loc;
3668 NamedDecl *ToD = nullptr;
3669 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3670 return nullptr;
3671 if (ToD)
3672 return ToD;
3673
3674 UnresolvedUsingTypenameDecl *ToUsing = UnresolvedUsingTypenameDecl::Create(
3675 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3676 Importer.Import(D->getTypenameLoc()),
3677 Importer.Import(D->getQualifierLoc()), Loc, Name,
3678 Importer.Import(D->getEllipsisLoc()));
3679
3680 Importer.Imported(D, ToUsing);
3681 ToUsing->setAccess(D->getAccess());
3682 ToUsing->setLexicalDeclContext(LexicalDC);
3683 LexicalDC->addDeclInternal(ToUsing);
3684
3685 return ToUsing;
3686}
3687
Douglas Gregor2aa53772012-01-24 17:42:07 +00003688bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3689 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003690 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003691 if (To->getDefinition()) {
3692 // Check consistency of superclass.
3693 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3694 if (FromSuper) {
3695 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3696 if (!FromSuper)
3697 return true;
3698 }
3699
3700 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3701 if ((bool)FromSuper != (bool)ToSuper ||
3702 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3703 Importer.ToDiag(To->getLocation(),
3704 diag::err_odr_objc_superclass_inconsistent)
3705 << To->getDeclName();
3706 if (ToSuper)
3707 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3708 << To->getSuperClass()->getDeclName();
3709 else
3710 Importer.ToDiag(To->getLocation(),
3711 diag::note_odr_objc_missing_superclass);
3712 if (From->getSuperClass())
3713 Importer.FromDiag(From->getSuperClassLoc(),
3714 diag::note_odr_objc_superclass)
3715 << From->getSuperClass()->getDeclName();
3716 else
3717 Importer.FromDiag(From->getLocation(),
3718 diag::note_odr_objc_missing_superclass);
3719 }
3720
Douglas Gregor2e15c842012-02-01 21:00:38 +00003721 if (shouldForceImportDeclContext(Kind))
3722 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003723 return false;
3724 }
3725
3726 // Start the definition.
3727 To->startDefinition();
3728
3729 // If this class has a superclass, import it.
3730 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003731 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3732 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003733 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003734
3735 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003736 }
3737
3738 // Import protocols
3739 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3740 SmallVector<SourceLocation, 4> ProtocolLocs;
3741 ObjCInterfaceDecl::protocol_loc_iterator
3742 FromProtoLoc = From->protocol_loc_begin();
3743
3744 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3745 FromProtoEnd = From->protocol_end();
3746 FromProto != FromProtoEnd;
3747 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003748 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003749 if (!ToProto)
3750 return true;
3751 Protocols.push_back(ToProto);
3752 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3753 }
3754
3755 // FIXME: If we're merging, make sure that the protocol list is the same.
3756 To->setProtocolList(Protocols.data(), Protocols.size(),
3757 ProtocolLocs.data(), Importer.getToContext());
3758
3759 // Import categories. When the categories themselves are imported, they'll
3760 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003761 for (auto *Cat : From->known_categories())
3762 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003763
Douglas Gregor2aa53772012-01-24 17:42:07 +00003764 // If we have an @implementation, import it as well.
3765 if (From->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003766 auto *Impl = cast_or_null<ObjCImplementationDecl>(
3767 Importer.Import(From->getImplementation()));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003768 if (!Impl)
3769 return true;
3770
3771 To->setImplementation(Impl);
3772 }
3773
Douglas Gregor2e15c842012-02-01 21:00:38 +00003774 if (shouldForceImportDeclContext(Kind)) {
3775 // Import all of the members of this class.
3776 ImportDeclContext(From, /*ForceImport=*/true);
3777 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003778 return false;
3779}
3780
Douglas Gregor85f3f952015-07-07 03:57:15 +00003781ObjCTypeParamList *
3782ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3783 if (!list)
3784 return nullptr;
3785
3786 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3787 for (auto fromTypeParam : *list) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003788 auto *toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3789 Importer.Import(fromTypeParam));
Douglas Gregor85f3f952015-07-07 03:57:15 +00003790 if (!toTypeParam)
3791 return nullptr;
3792
3793 toTypeParams.push_back(toTypeParam);
3794 }
3795
3796 return ObjCTypeParamList::create(Importer.getToContext(),
3797 Importer.Import(list->getLAngleLoc()),
3798 toTypeParams,
3799 Importer.Import(list->getRAngleLoc()));
3800}
3801
Douglas Gregor45635322010-02-16 01:20:57 +00003802Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003803 // If this class has a definition in the translation unit we're coming from,
3804 // but this particular declaration is not that definition, import the
3805 // definition and map to that.
3806 ObjCInterfaceDecl *Definition = D->getDefinition();
3807 if (Definition && Definition != D) {
3808 Decl *ImportedDef = Importer.Import(Definition);
3809 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003810 return nullptr;
3811
Douglas Gregor2aa53772012-01-24 17:42:07 +00003812 return Importer.Imported(D, ImportedDef);
3813 }
3814
Douglas Gregor45635322010-02-16 01:20:57 +00003815 // Import the major distinguishing characteristics of an @interface.
3816 DeclContext *DC, *LexicalDC;
3817 DeclarationName Name;
3818 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003819 NamedDecl *ToD;
3820 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003821 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003822 if (ToD)
3823 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003824
Douglas Gregor2aa53772012-01-24 17:42:07 +00003825 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003826 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003827 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003828 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003829 for (auto *FoundDecl : FoundDecls) {
3830 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003831 continue;
3832
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003833 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00003834 break;
3835 }
3836
Douglas Gregor2aa53772012-01-24 17:42:07 +00003837 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003838 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003839 if (!ToIface) {
3840 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3841 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003842 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003843 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00003844 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003845 D->isImplicitInterfaceDecl());
3846 ToIface->setLexicalDeclContext(LexicalDC);
3847 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003848 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003849 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003850 // Import the type parameter list after calling Imported, to avoid
3851 // loops when bringing in their DeclContext.
3852 ToIface->setTypeParamList(ImportObjCTypeParamList(
3853 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003854
Douglas Gregor2aa53772012-01-24 17:42:07 +00003855 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003856 return nullptr;
3857
Douglas Gregor98d156a2010-02-17 16:12:00 +00003858 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003859}
3860
Douglas Gregor4da9d682010-12-07 15:32:12 +00003861Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003862 auto *Category = cast_or_null<ObjCCategoryDecl>(
3863 Importer.Import(D->getCategoryDecl()));
Douglas Gregor4da9d682010-12-07 15:32:12 +00003864 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003865 return nullptr;
3866
Douglas Gregor4da9d682010-12-07 15:32:12 +00003867 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3868 if (!ToImpl) {
3869 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3870 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003871 return nullptr;
3872
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003873 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003874 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003875 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003876 Category->getClassInterface(),
3877 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003878 Importer.Import(D->getAtStartLoc()),
3879 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003880
3881 DeclContext *LexicalDC = DC;
3882 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3883 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3884 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003885 return nullptr;
3886
Douglas Gregor4da9d682010-12-07 15:32:12 +00003887 ToImpl->setLexicalDeclContext(LexicalDC);
3888 }
3889
Sean Callanan95e74be2011-10-21 02:57:43 +00003890 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003891 Category->setImplementation(ToImpl);
3892 }
3893
3894 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003895 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003896 return ToImpl;
3897}
3898
Douglas Gregorda8025c2010-12-07 01:26:03 +00003899Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3900 // Find the corresponding interface.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003901 auto *Iface = cast_or_null<ObjCInterfaceDecl>(
3902 Importer.Import(D->getClassInterface()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003903 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003904 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003905
3906 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003907 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003908 if (D->getSuperClass()) {
3909 Super = cast_or_null<ObjCInterfaceDecl>(
3910 Importer.Import(D->getSuperClass()));
3911 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003912 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003913 }
3914
3915 ObjCImplementationDecl *Impl = Iface->getImplementation();
3916 if (!Impl) {
3917 // We haven't imported an implementation yet. Create a new @implementation
3918 // now.
3919 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3920 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003921 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003922 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003923 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003924 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003925 Importer.Import(D->getIvarLBraceLoc()),
3926 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003927
3928 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3929 DeclContext *LexicalDC
3930 = Importer.ImportContext(D->getLexicalDeclContext());
3931 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003932 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003933 Impl->setLexicalDeclContext(LexicalDC);
3934 }
3935
3936 // Associate the implementation with the class it implements.
3937 Iface->setImplementation(Impl);
3938 Importer.Imported(D, Iface->getImplementation());
3939 } else {
3940 Importer.Imported(D, Iface->getImplementation());
3941
3942 // Verify that the existing @implementation has the same superclass.
3943 if ((Super && !Impl->getSuperClass()) ||
3944 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003945 (Super && Impl->getSuperClass() &&
3946 !declaresSameEntity(Super->getCanonicalDecl(),
3947 Impl->getSuperClass()))) {
3948 Importer.ToDiag(Impl->getLocation(),
3949 diag::err_odr_objc_superclass_inconsistent)
3950 << Iface->getDeclName();
3951 // FIXME: It would be nice to have the location of the superclass
3952 // below.
3953 if (Impl->getSuperClass())
3954 Importer.ToDiag(Impl->getLocation(),
3955 diag::note_odr_objc_superclass)
3956 << Impl->getSuperClass()->getDeclName();
3957 else
3958 Importer.ToDiag(Impl->getLocation(),
3959 diag::note_odr_objc_missing_superclass);
3960 if (D->getSuperClass())
3961 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003962 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003963 << D->getSuperClass()->getDeclName();
3964 else
3965 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003966 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003967 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003968 }
3969 }
3970
3971 // Import all of the members of this @implementation.
3972 ImportDeclContext(D);
3973
3974 return Impl;
3975}
3976
Douglas Gregora11c4582010-02-17 18:02:10 +00003977Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3978 // Import the major distinguishing characteristics of an @property.
3979 DeclContext *DC, *LexicalDC;
3980 DeclarationName Name;
3981 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003982 NamedDecl *ToD;
3983 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003984 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003985 if (ToD)
3986 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00003987
3988 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003989 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003990 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003991 for (auto *FoundDecl : FoundDecls) {
3992 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003993 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003994 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00003995 FoundProp->getType())) {
3996 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3997 << Name << D->getType() << FoundProp->getType();
3998 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3999 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00004000 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004001 }
4002
4003 // FIXME: Check property attributes, getters, setters, etc.?
4004
4005 // Consider these properties to be equivalent.
4006 Importer.Imported(D, FoundProp);
4007 return FoundProp;
4008 }
4009 }
4010
4011 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00004012 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
4013 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00004014 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004015
4016 // Create the new property.
4017 ObjCPropertyDecl *ToProperty
4018 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
4019 Name.getAsIdentifierInfo(),
4020 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00004021 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00004022 Importer.Import(D->getType()),
4023 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00004024 D->getPropertyImplementation());
4025 Importer.Imported(D, ToProperty);
4026 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004027 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004028
4029 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004030 ToProperty->setPropertyAttributesAsWritten(
4031 D->getPropertyAttributesAsWritten());
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +00004032 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
4033 Importer.Import(D->getGetterNameLoc()));
4034 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
4035 Importer.Import(D->getSetterNameLoc()));
Douglas Gregora11c4582010-02-17 18:02:10 +00004036 ToProperty->setGetterMethodDecl(
4037 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
4038 ToProperty->setSetterMethodDecl(
4039 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
4040 ToProperty->setPropertyIvarDecl(
4041 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
4042 return ToProperty;
4043}
4044
Douglas Gregor14a49e22010-12-07 18:32:03 +00004045Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004046 auto *Property = cast_or_null<ObjCPropertyDecl>(
4047 Importer.Import(D->getPropertyDecl()));
Douglas Gregor14a49e22010-12-07 18:32:03 +00004048 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00004049 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004050
4051 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4052 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004053 return nullptr;
4054
Douglas Gregor14a49e22010-12-07 18:32:03 +00004055 // Import the lexical declaration context.
4056 DeclContext *LexicalDC = DC;
4057 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4058 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4059 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004060 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004061 }
4062
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004063 auto *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004064 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00004065 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004066
4067 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004068 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004069 if (D->getPropertyIvarDecl()) {
4070 Ivar = cast_or_null<ObjCIvarDecl>(
4071 Importer.Import(D->getPropertyIvarDecl()));
4072 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00004073 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004074 }
4075
4076 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004077 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4078 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00004079 if (!ToImpl) {
4080 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
4081 Importer.Import(D->getLocStart()),
4082 Importer.Import(D->getLocation()),
4083 Property,
4084 D->getPropertyImplementation(),
4085 Ivar,
4086 Importer.Import(D->getPropertyIvarDeclLoc()));
4087 ToImpl->setLexicalDeclContext(LexicalDC);
4088 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00004089 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004090 } else {
4091 // Check that we have the same kind of property implementation (@synthesize
4092 // vs. @dynamic).
4093 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4094 Importer.ToDiag(ToImpl->getLocation(),
4095 diag::err_odr_objc_property_impl_kind_inconsistent)
4096 << Property->getDeclName()
4097 << (ToImpl->getPropertyImplementation()
4098 == ObjCPropertyImplDecl::Dynamic);
4099 Importer.FromDiag(D->getLocation(),
4100 diag::note_odr_objc_property_impl_kind)
4101 << D->getPropertyDecl()->getDeclName()
4102 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004103 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004104 }
4105
4106 // For @synthesize, check that we have the same
4107 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4108 Ivar != ToImpl->getPropertyIvarDecl()) {
4109 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4110 diag::err_odr_objc_synthesize_ivar_inconsistent)
4111 << Property->getDeclName()
4112 << ToImpl->getPropertyIvarDecl()->getDeclName()
4113 << Ivar->getDeclName();
4114 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4115 diag::note_odr_objc_synthesize_ivar_here)
4116 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004117 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004118 }
4119
4120 // Merge the existing implementation with the new implementation.
4121 Importer.Imported(D, ToImpl);
4122 }
4123
4124 return ToImpl;
4125}
4126
Douglas Gregora082a492010-11-30 19:14:50 +00004127Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4128 // For template arguments, we adopt the translation unit as our declaration
4129 // context. This context will be fixed when the actual template declaration
4130 // is created.
4131
4132 // FIXME: Import default argument.
4133 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4134 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004135 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004136 Importer.Import(D->getLocation()),
4137 D->getDepth(),
4138 D->getIndex(),
4139 Importer.Import(D->getIdentifier()),
4140 D->wasDeclaredWithTypename(),
4141 D->isParameterPack());
4142}
4143
4144Decl *
4145ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4146 // Import the name of this declaration.
4147 DeclarationName Name = Importer.Import(D->getDeclName());
4148 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004149 return nullptr;
4150
Douglas Gregora082a492010-11-30 19:14:50 +00004151 // Import the location of this declaration.
4152 SourceLocation Loc = Importer.Import(D->getLocation());
4153
4154 // Import the type of this declaration.
4155 QualType T = Importer.Import(D->getType());
4156 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004157 return nullptr;
4158
Douglas Gregora082a492010-11-30 19:14:50 +00004159 // Import type-source information.
4160 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4161 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004162 return nullptr;
4163
Douglas Gregora082a492010-11-30 19:14:50 +00004164 // FIXME: Import default argument.
4165
4166 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4167 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004168 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004169 Loc, D->getDepth(), D->getPosition(),
4170 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004171 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004172}
4173
4174Decl *
4175ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4176 // Import the name of this declaration.
4177 DeclarationName Name = Importer.Import(D->getDeclName());
4178 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004179 return nullptr;
4180
Douglas Gregora082a492010-11-30 19:14:50 +00004181 // Import the location of this declaration.
4182 SourceLocation Loc = Importer.Import(D->getLocation());
4183
4184 // Import template parameters.
4185 TemplateParameterList *TemplateParams
4186 = ImportTemplateParameterList(D->getTemplateParameters());
4187 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004188 return nullptr;
4189
Douglas Gregora082a492010-11-30 19:14:50 +00004190 // FIXME: Import default argument.
4191
4192 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4193 Importer.getToContext().getTranslationUnitDecl(),
4194 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004195 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004196 Name.getAsIdentifierInfo(),
4197 TemplateParams);
4198}
4199
Gabor Marton9581c332018-05-23 13:53:36 +00004200// Returns the definition for a (forward) declaration of a ClassTemplateDecl, if
4201// it has any definition in the redecl chain.
4202static ClassTemplateDecl *getDefinition(ClassTemplateDecl *D) {
4203 CXXRecordDecl *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
4204 if (!ToTemplatedDef)
4205 return nullptr;
4206 ClassTemplateDecl *TemplateWithDef =
4207 ToTemplatedDef->getDescribedClassTemplate();
4208 return TemplateWithDef;
4209}
4210
Douglas Gregora082a492010-11-30 19:14:50 +00004211Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4212 // If this record has a definition in the translation unit we're coming from,
4213 // but this particular declaration is not that definition, import the
4214 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004215 auto *Definition =
4216 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
Douglas Gregora082a492010-11-30 19:14:50 +00004217 if (Definition && Definition != D->getTemplatedDecl()) {
4218 Decl *ImportedDef
4219 = Importer.Import(Definition->getDescribedClassTemplate());
4220 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004221 return nullptr;
4222
Douglas Gregora082a492010-11-30 19:14:50 +00004223 return Importer.Imported(D, ImportedDef);
4224 }
Gabor Marton9581c332018-05-23 13:53:36 +00004225
Douglas Gregora082a492010-11-30 19:14:50 +00004226 // Import the major distinguishing characteristics of this class template.
4227 DeclContext *DC, *LexicalDC;
4228 DeclarationName Name;
4229 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004230 NamedDecl *ToD;
4231 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004232 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004233 if (ToD)
4234 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004235
Douglas Gregora082a492010-11-30 19:14:50 +00004236 // We may already have a template of the same name; try to find and match it.
4237 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004238 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004239 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004240 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004241 for (auto *FoundDecl : FoundDecls) {
4242 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004243 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00004244
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004245 Decl *Found = FoundDecl;
4246 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Marton9581c332018-05-23 13:53:36 +00004247
4248 // The class to be imported is a definition.
4249 if (D->isThisDeclarationADefinition()) {
4250 // Lookup will find the fwd decl only if that is more recent than the
4251 // definition. So, try to get the definition if that is available in
4252 // the redecl chain.
4253 ClassTemplateDecl *TemplateWithDef = getDefinition(FoundTemplate);
4254 if (!TemplateWithDef)
4255 continue;
4256 FoundTemplate = TemplateWithDef; // Continue with the definition.
4257 }
4258
Douglas Gregora082a492010-11-30 19:14:50 +00004259 if (IsStructuralMatch(D, FoundTemplate)) {
4260 // The class templates structurally match; call it the same template.
Aleksei Sidorin761c2242018-05-15 11:09:07 +00004261
Gabor Marton9581c332018-05-23 13:53:36 +00004262 Importer.Imported(D->getTemplatedDecl(),
Douglas Gregora082a492010-11-30 19:14:50 +00004263 FoundTemplate->getTemplatedDecl());
4264 return Importer.Imported(D, FoundTemplate);
Gabor Marton9581c332018-05-23 13:53:36 +00004265 }
Douglas Gregora082a492010-11-30 19:14:50 +00004266 }
Gabor Marton9581c332018-05-23 13:53:36 +00004267
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004268 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00004269 }
Gabor Marton9581c332018-05-23 13:53:36 +00004270
Douglas Gregora082a492010-11-30 19:14:50 +00004271 if (!ConflictingDecls.empty()) {
4272 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00004273 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00004274 ConflictingDecls.size());
4275 }
Gabor Marton9581c332018-05-23 13:53:36 +00004276
Douglas Gregora082a492010-11-30 19:14:50 +00004277 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004278 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004279 }
4280
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004281 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4282
Douglas Gregora082a492010-11-30 19:14:50 +00004283 // Create the declaration that is being templated.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004284 auto *ToTemplated = cast_or_null<CXXRecordDecl>(
4285 Importer.Import(FromTemplated));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004286 if (!ToTemplated)
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004287 return nullptr;
4288
4289 // Resolve possible cyclic import.
4290 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
4291 return AlreadyImported;
4292
Douglas Gregora082a492010-11-30 19:14:50 +00004293 // Create the class template declaration itself.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004294 TemplateParameterList *TemplateParams =
4295 ImportTemplateParameterList(D->getTemplateParameters());
Douglas Gregora082a492010-11-30 19:14:50 +00004296 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004297 return nullptr;
4298
Douglas Gregora082a492010-11-30 19:14:50 +00004299 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4300 Loc, Name, TemplateParams,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004301 ToTemplated);
4302 ToTemplated->setDescribedClassTemplate(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004303
4304 D2->setAccess(D->getAccess());
4305 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004306 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004307
4308 // Note the relationship between the class templates.
4309 Importer.Imported(D, D2);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004310 Importer.Imported(FromTemplated, ToTemplated);
Douglas Gregora082a492010-11-30 19:14:50 +00004311
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004312 if (FromTemplated->isCompleteDefinition() &&
4313 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004314 // FIXME: Import definition!
4315 }
4316
4317 return D2;
4318}
4319
Douglas Gregore2e50d332010-12-01 01:36:18 +00004320Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4321 ClassTemplateSpecializationDecl *D) {
4322 // If this record has a definition in the translation unit we're coming from,
4323 // but this particular declaration is not that definition, import the
4324 // definition and map to that.
4325 TagDecl *Definition = D->getDefinition();
4326 if (Definition && Definition != D) {
4327 Decl *ImportedDef = Importer.Import(Definition);
4328 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004329 return nullptr;
4330
Douglas Gregore2e50d332010-12-01 01:36:18 +00004331 return Importer.Imported(D, ImportedDef);
4332 }
4333
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004334 auto *ClassTemplate =
4335 cast_or_null<ClassTemplateDecl>(Importer.Import(
Douglas Gregore2e50d332010-12-01 01:36:18 +00004336 D->getSpecializedTemplate()));
4337 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004338 return nullptr;
4339
Douglas Gregore2e50d332010-12-01 01:36:18 +00004340 // Import the context of this declaration.
4341 DeclContext *DC = ClassTemplate->getDeclContext();
4342 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004343 return nullptr;
4344
Douglas Gregore2e50d332010-12-01 01:36:18 +00004345 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;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004350 }
4351
4352 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004353 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4354 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004355
4356 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004357 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004358 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4359 D->getTemplateArgs().size(),
4360 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004361 return nullptr;
4362
Douglas Gregore2e50d332010-12-01 01:36:18 +00004363 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004364 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004365 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004366 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004367 if (D2) {
4368 // We already have a class template specialization with these template
4369 // arguments.
4370
4371 // FIXME: Check for specialization vs. instantiation errors.
4372
4373 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004374 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004375 // The record types structurally match, or the "from" translation
4376 // unit only had a forward declaration anyway; call it the same
4377 // function.
4378 return Importer.Imported(D, FoundDef);
4379 }
4380 }
4381 } else {
4382 // Create a new specialization.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004383 if (auto *PartialSpec =
4384 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004385 // Import TemplateArgumentListInfo
4386 TemplateArgumentListInfo ToTAInfo;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004387 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
4388 if (ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004389 return nullptr;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004390
4391 QualType CanonInjType = Importer.Import(
4392 PartialSpec->getInjectedSpecializationType());
4393 if (CanonInjType.isNull())
4394 return nullptr;
4395 CanonInjType = CanonInjType.getCanonicalType();
4396
4397 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4398 PartialSpec->getTemplateParameters());
4399 if (!ToTPList && PartialSpec->getTemplateParameters())
4400 return nullptr;
4401
4402 D2 = ClassTemplatePartialSpecializationDecl::Create(
4403 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
4404 ToTPList, ClassTemplate,
4405 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
4406 ToTAInfo, CanonInjType, nullptr);
4407
4408 } else {
4409 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4410 D->getTagKind(), DC,
4411 StartLoc, IdLoc,
4412 ClassTemplate,
4413 TemplateArgs,
4414 /*PrevDecl=*/nullptr);
4415 }
4416
Douglas Gregore2e50d332010-12-01 01:36:18 +00004417 D2->setSpecializationKind(D->getSpecializationKind());
4418
4419 // Add this specialization to the class template.
4420 ClassTemplate->AddSpecialization(D2, InsertPos);
4421
4422 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004423 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004424
4425 Importer.Imported(D, D2);
4426
4427 if (auto *TSI = D->getTypeAsWritten()) {
4428 TypeSourceInfo *TInfo = Importer.Import(TSI);
4429 if (!TInfo)
4430 return nullptr;
4431 D2->setTypeAsWritten(TInfo);
4432 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
4433 D2->setExternLoc(Importer.Import(D->getExternLoc()));
4434 }
4435
4436 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
4437 if (POI.isValid())
4438 D2->setPointOfInstantiation(POI);
4439 else if (D->getPointOfInstantiation().isValid())
4440 return nullptr;
4441
4442 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
4443
Gabor Martonb14056b2018-05-25 11:21:24 +00004444 // Set the context of this specialization/instantiation.
Douglas Gregore2e50d332010-12-01 01:36:18 +00004445 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonb14056b2018-05-25 11:21:24 +00004446
4447 // Add to the DC only if it was an explicit specialization/instantiation.
4448 if (D2->isExplicitInstantiationOrSpecialization()) {
4449 LexicalDC->addDeclInternal(D2);
4450 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004451 }
4452 Importer.Imported(D, D2);
John McCallf937c022011-10-07 06:10:15 +00004453 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004454 return nullptr;
4455
Douglas Gregore2e50d332010-12-01 01:36:18 +00004456 return D2;
4457}
4458
Larisse Voufo39a1e502013-08-06 01:03:05 +00004459Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4460 // If this variable has a definition in the translation unit we're coming
4461 // from,
4462 // but this particular declaration is not that definition, import the
4463 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004464 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00004465 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4466 if (Definition && Definition != D->getTemplatedDecl()) {
4467 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4468 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004469 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004470
4471 return Importer.Imported(D, ImportedDef);
4472 }
4473
4474 // Import the major distinguishing characteristics of this variable template.
4475 DeclContext *DC, *LexicalDC;
4476 DeclarationName Name;
4477 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004478 NamedDecl *ToD;
4479 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004480 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004481 if (ToD)
4482 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004483
4484 // We may already have a template of the same name; try to find and match it.
4485 assert(!DC->isFunctionOrMethod() &&
4486 "Variable templates cannot be declared at function scope");
4487 SmallVector<NamedDecl *, 4> ConflictingDecls;
4488 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004489 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004490 for (auto *FoundDecl : FoundDecls) {
4491 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00004492 continue;
4493
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004494 Decl *Found = FoundDecl;
4495 if (auto *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004496 if (IsStructuralMatch(D, FoundTemplate)) {
4497 // The variable templates structurally match; call it the same template.
4498 Importer.Imported(D->getTemplatedDecl(),
4499 FoundTemplate->getTemplatedDecl());
4500 return Importer.Imported(D, FoundTemplate);
4501 }
4502 }
4503
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004504 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004505 }
4506
4507 if (!ConflictingDecls.empty()) {
4508 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4509 ConflictingDecls.data(),
4510 ConflictingDecls.size());
4511 }
4512
4513 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004514 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004515
4516 VarDecl *DTemplated = D->getTemplatedDecl();
4517
4518 // Import the type.
4519 QualType T = Importer.Import(DTemplated->getType());
4520 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004521 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004522
4523 // Create the declaration that is being templated.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004524 auto *ToTemplated = dyn_cast_or_null<VarDecl>(Importer.Import(DTemplated));
4525 if (!ToTemplated)
Craig Topper36250ad2014-05-12 05:36:57 +00004526 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004527
4528 // Create the variable template declaration itself.
4529 TemplateParameterList *TemplateParams =
4530 ImportTemplateParameterList(D->getTemplateParameters());
4531 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004532 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004533
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004534 VarTemplateDecl *ToVarTD = VarTemplateDecl::Create(
4535 Importer.getToContext(), DC, Loc, Name, TemplateParams, ToTemplated);
4536 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004537
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004538 ToVarTD->setAccess(D->getAccess());
4539 ToVarTD->setLexicalDeclContext(LexicalDC);
4540 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004541
4542 // Note the relationship between the variable templates.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004543 Importer.Imported(D, ToVarTD);
4544 Importer.Imported(DTemplated, ToTemplated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004545
4546 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004547 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004548 // FIXME: Import definition!
4549 }
4550
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004551 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004552}
4553
4554Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4555 VarTemplateSpecializationDecl *D) {
4556 // If this record has a definition in the translation unit we're coming from,
4557 // but this particular declaration is not that definition, import the
4558 // definition and map to that.
4559 VarDecl *Definition = D->getDefinition();
4560 if (Definition && Definition != D) {
4561 Decl *ImportedDef = Importer.Import(Definition);
4562 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004563 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004564
4565 return Importer.Imported(D, ImportedDef);
4566 }
4567
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004568 auto *VarTemplate = cast_or_null<VarTemplateDecl>(
Larisse Voufo39a1e502013-08-06 01:03:05 +00004569 Importer.Import(D->getSpecializedTemplate()));
4570 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004571 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004572
4573 // Import the context of this declaration.
4574 DeclContext *DC = VarTemplate->getDeclContext();
4575 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004576 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004577
4578 DeclContext *LexicalDC = DC;
4579 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4580 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4581 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004582 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004583 }
4584
4585 // Import the location of this declaration.
4586 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4587 SourceLocation IdLoc = Importer.Import(D->getLocation());
4588
4589 // Import template arguments.
4590 SmallVector<TemplateArgument, 2> TemplateArgs;
4591 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4592 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004593 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004594
4595 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004596 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004597 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004598 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004599 if (D2) {
4600 // We already have a variable template specialization with these template
4601 // arguments.
4602
4603 // FIXME: Check for specialization vs. instantiation errors.
4604
4605 if (VarDecl *FoundDef = D2->getDefinition()) {
4606 if (!D->isThisDeclarationADefinition() ||
4607 IsStructuralMatch(D, FoundDef)) {
4608 // The record types structurally match, or the "from" translation
4609 // unit only had a forward declaration anyway; call it the same
4610 // variable.
4611 return Importer.Imported(D, FoundDef);
4612 }
4613 }
4614 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004615 // Import the type.
4616 QualType T = Importer.Import(D->getType());
4617 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004618 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004619
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004620 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4621 if (D->getTypeSourceInfo() && !TInfo)
4622 return nullptr;
4623
4624 TemplateArgumentListInfo ToTAInfo;
4625 if (ImportTemplateArgumentListInfo(D->getTemplateArgsInfo(), ToTAInfo))
4626 return nullptr;
4627
4628 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004629 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004630 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
4631 // Import TemplateArgumentListInfo
4632 TemplateArgumentListInfo ArgInfos;
4633 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
4634 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
4635 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ArgInfos))
4636 return nullptr;
4637
4638 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4639 FromPartial->getTemplateParameters());
4640 if (!ToTPList)
4641 return nullptr;
4642
4643 auto *ToPartial = PartVarSpecDecl::Create(
4644 Importer.getToContext(), DC, StartLoc, IdLoc, ToTPList, VarTemplate,
4645 T, TInfo, D->getStorageClass(), TemplateArgs, ArgInfos);
4646
4647 auto *FromInst = FromPartial->getInstantiatedFromMember();
4648 auto *ToInst = cast_or_null<PartVarSpecDecl>(Importer.Import(FromInst));
4649 if (FromInst && !ToInst)
4650 return nullptr;
4651
4652 ToPartial->setInstantiatedFromMember(ToInst);
4653 if (FromPartial->isMemberSpecialization())
4654 ToPartial->setMemberSpecialization();
4655
4656 D2 = ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004657 } else { // Full specialization
4658 D2 = VarTemplateSpecializationDecl::Create(
4659 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4660 D->getStorageClass(), TemplateArgs);
4661 }
4662
4663 SourceLocation POI = D->getPointOfInstantiation();
4664 if (POI.isValid())
4665 D2->setPointOfInstantiation(Importer.Import(POI));
4666
Larisse Voufo39a1e502013-08-06 01:03:05 +00004667 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004668 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004669
4670 // Add this specialization to the class template.
4671 VarTemplate->AddSpecialization(D2, InsertPos);
4672
4673 // Import the qualifier, if any.
4674 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4675
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004676 if (D->isConstexpr())
4677 D2->setConstexpr(true);
4678
Larisse Voufo39a1e502013-08-06 01:03:05 +00004679 // Add the specialization to this context.
4680 D2->setLexicalDeclContext(LexicalDC);
4681 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004682
4683 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00004684 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004685
Larisse Voufo39a1e502013-08-06 01:03:05 +00004686 Importer.Imported(D, D2);
4687
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004688 // NOTE: isThisDeclarationADefinition() can return DeclarationOnly even if
4689 // declaration has initializer. Should this be fixed in the AST?.. Anyway,
4690 // we have to check the declaration for initializer - otherwise, it won't be
4691 // imported.
4692 if ((D->isThisDeclarationADefinition() || D->hasInit()) &&
4693 ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004694 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004695
4696 return D2;
4697}
4698
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004699Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
4700 DeclContext *DC, *LexicalDC;
4701 DeclarationName Name;
4702 SourceLocation Loc;
4703 NamedDecl *ToD;
4704
4705 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4706 return nullptr;
4707
4708 if (ToD)
4709 return ToD;
4710
4711 // Try to find a function in our own ("to") context with the same name, same
4712 // type, and in the same context as the function we're importing.
4713 if (!LexicalDC->isFunctionOrMethod()) {
4714 unsigned IDNS = Decl::IDNS_Ordinary;
4715 SmallVector<NamedDecl *, 2> FoundDecls;
4716 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004717 for (auto *FoundDecl : FoundDecls) {
4718 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004719 continue;
4720
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004721 if (auto *FoundFunction = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004722 if (FoundFunction->hasExternalFormalLinkage() &&
4723 D->hasExternalFormalLinkage()) {
4724 if (IsStructuralMatch(D, FoundFunction)) {
4725 Importer.Imported(D, FoundFunction);
4726 // FIXME: Actually try to merge the body and other attributes.
4727 return FoundFunction;
4728 }
4729 }
4730 }
4731 }
4732 }
4733
4734 TemplateParameterList *Params =
4735 ImportTemplateParameterList(D->getTemplateParameters());
4736 if (!Params)
4737 return nullptr;
4738
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004739 auto *TemplatedFD =
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004740 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl()));
4741 if (!TemplatedFD)
4742 return nullptr;
4743
4744 FunctionTemplateDecl *ToFunc = FunctionTemplateDecl::Create(
4745 Importer.getToContext(), DC, Loc, Name, Params, TemplatedFD);
4746
4747 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
4748 ToFunc->setAccess(D->getAccess());
4749 ToFunc->setLexicalDeclContext(LexicalDC);
4750 Importer.Imported(D, ToFunc);
4751
4752 LexicalDC->addDeclInternal(ToFunc);
4753 return ToFunc;
4754}
4755
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004756//----------------------------------------------------------------------------
4757// Import Statements
4758//----------------------------------------------------------------------------
4759
Sean Callanan59721b32015-04-28 18:41:46 +00004760DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4761 if (DG.isNull())
4762 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4763 size_t NumDecls = DG.end() - DG.begin();
4764 SmallVector<Decl *, 1> ToDecls(NumDecls);
4765 auto &_Importer = this->Importer;
4766 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4767 [&_Importer](Decl *D) -> Decl * {
4768 return _Importer.Import(D);
4769 });
4770 return DeclGroupRef::Create(Importer.getToContext(),
4771 ToDecls.begin(),
4772 NumDecls);
4773}
4774
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004775Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4776 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4777 << S->getStmtClassName();
4778 return nullptr;
4779}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004780
4781Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4782 SmallVector<IdentifierInfo *, 4> Names;
4783 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4784 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004785 // ToII is nullptr when no symbolic name is given for output operand
4786 // see ParseStmtAsm::ParseAsmOperandsOpt
4787 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004788 return nullptr;
4789 Names.push_back(ToII);
4790 }
4791 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4792 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004793 // ToII is nullptr when no symbolic name is given for input operand
4794 // see ParseStmtAsm::ParseAsmOperandsOpt
4795 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004796 return nullptr;
4797 Names.push_back(ToII);
4798 }
4799
4800 SmallVector<StringLiteral *, 4> Clobbers;
4801 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004802 auto *Clobber = cast_or_null<StringLiteral>(
4803 Importer.Import(S->getClobberStringLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004804 if (!Clobber)
4805 return nullptr;
4806 Clobbers.push_back(Clobber);
4807 }
4808
4809 SmallVector<StringLiteral *, 4> Constraints;
4810 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004811 auto *Output = cast_or_null<StringLiteral>(
4812 Importer.Import(S->getOutputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004813 if (!Output)
4814 return nullptr;
4815 Constraints.push_back(Output);
4816 }
4817
4818 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004819 auto *Input = cast_or_null<StringLiteral>(
4820 Importer.Import(S->getInputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004821 if (!Input)
4822 return nullptr;
4823 Constraints.push_back(Input);
4824 }
4825
4826 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004827 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004828 return nullptr;
4829
Aleksei Sidorina693b372016-09-28 10:16:56 +00004830 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004831 return nullptr;
4832
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004833 auto *AsmStr = cast_or_null<StringLiteral>(
4834 Importer.Import(S->getAsmString()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004835 if (!AsmStr)
4836 return nullptr;
4837
4838 return new (Importer.getToContext()) GCCAsmStmt(
4839 Importer.getToContext(),
4840 Importer.Import(S->getAsmLoc()),
4841 S->isSimple(),
4842 S->isVolatile(),
4843 S->getNumOutputs(),
4844 S->getNumInputs(),
4845 Names.data(),
4846 Constraints.data(),
4847 Exprs.data(),
4848 AsmStr,
4849 S->getNumClobbers(),
4850 Clobbers.data(),
4851 Importer.Import(S->getRParenLoc()));
4852}
4853
Sean Callanan59721b32015-04-28 18:41:46 +00004854Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4855 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004856 for (auto *ToD : ToDG) {
Sean Callanan59721b32015-04-28 18:41:46 +00004857 if (!ToD)
4858 return nullptr;
4859 }
4860 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4861 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4862 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4863}
4864
4865Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4866 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4867 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4868 S->hasLeadingEmptyMacro());
4869}
4870
4871Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004872 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004873
4874 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00004875 return nullptr;
4876
Sean Callanan59721b32015-04-28 18:41:46 +00004877 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4878 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
Benjamin Kramer07420902017-12-24 16:24:20 +00004879 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
4880 ToRBraceLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004881}
4882
4883Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4884 Expr *ToLHS = Importer.Import(S->getLHS());
4885 if (!ToLHS)
4886 return nullptr;
4887 Expr *ToRHS = Importer.Import(S->getRHS());
4888 if (!ToRHS && S->getRHS())
4889 return nullptr;
Gabor Horvath480892b2017-10-18 09:25:18 +00004890 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4891 if (!ToSubStmt && S->getSubStmt())
4892 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004893 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4894 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4895 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004896 auto *ToStmt = new (Importer.getToContext())
Gabor Horvath480892b2017-10-18 09:25:18 +00004897 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
4898 ToStmt->setSubStmt(ToSubStmt);
4899 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00004900}
4901
4902Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4903 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4904 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4905 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4906 if (!ToSubStmt && S->getSubStmt())
4907 return nullptr;
4908 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4909 ToSubStmt);
4910}
4911
4912Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4913 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004914 auto *ToLabelDecl = cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00004915 if (!ToLabelDecl && S->getDecl())
4916 return nullptr;
4917 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4918 if (!ToSubStmt && S->getSubStmt())
4919 return nullptr;
4920 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4921 ToSubStmt);
4922}
4923
4924Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4925 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4926 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4927 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00004928 if (ImportContainerChecked(FromAttrs, ToAttrs))
4929 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004930 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4931 if (!ToSubStmt && S->getSubStmt())
4932 return nullptr;
4933 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4934 ToAttrs, ToSubStmt);
4935}
4936
4937Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4938 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00004939 Stmt *ToInit = Importer.Import(S->getInit());
4940 if (!ToInit && S->getInit())
4941 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004942 VarDecl *ToConditionVariable = nullptr;
4943 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4944 ToConditionVariable =
4945 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4946 if (!ToConditionVariable)
4947 return nullptr;
4948 }
4949 Expr *ToCondition = Importer.Import(S->getCond());
4950 if (!ToCondition && S->getCond())
4951 return nullptr;
4952 Stmt *ToThenStmt = Importer.Import(S->getThen());
4953 if (!ToThenStmt && S->getThen())
4954 return nullptr;
4955 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4956 Stmt *ToElseStmt = Importer.Import(S->getElse());
4957 if (!ToElseStmt && S->getElse())
4958 return nullptr;
4959 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00004960 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00004961 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00004962 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00004963 ToCondition, ToThenStmt,
4964 ToElseLoc, ToElseStmt);
4965}
4966
4967Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00004968 Stmt *ToInit = Importer.Import(S->getInit());
4969 if (!ToInit && S->getInit())
4970 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004971 VarDecl *ToConditionVariable = nullptr;
4972 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4973 ToConditionVariable =
4974 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4975 if (!ToConditionVariable)
4976 return nullptr;
4977 }
4978 Expr *ToCondition = Importer.Import(S->getCond());
4979 if (!ToCondition && S->getCond())
4980 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004981 auto *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00004982 Importer.getToContext(), ToInit,
4983 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00004984 Stmt *ToBody = Importer.Import(S->getBody());
4985 if (!ToBody && S->getBody())
4986 return nullptr;
4987 ToStmt->setBody(ToBody);
4988 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4989 // Now we have to re-chain the cases.
4990 SwitchCase *LastChainedSwitchCase = nullptr;
4991 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4992 SC = SC->getNextSwitchCase()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004993 auto *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
Sean Callanan59721b32015-04-28 18:41:46 +00004994 if (!ToSC)
4995 return nullptr;
4996 if (LastChainedSwitchCase)
4997 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4998 else
4999 ToStmt->setSwitchCaseList(ToSC);
5000 LastChainedSwitchCase = ToSC;
5001 }
5002 return ToStmt;
5003}
5004
5005Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5006 VarDecl *ToConditionVariable = nullptr;
5007 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5008 ToConditionVariable =
5009 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5010 if (!ToConditionVariable)
5011 return nullptr;
5012 }
5013 Expr *ToCondition = Importer.Import(S->getCond());
5014 if (!ToCondition && S->getCond())
5015 return nullptr;
5016 Stmt *ToBody = Importer.Import(S->getBody());
5017 if (!ToBody && S->getBody())
5018 return nullptr;
5019 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5020 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
5021 ToConditionVariable,
5022 ToCondition, ToBody,
5023 ToWhileLoc);
5024}
5025
5026Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5027 Stmt *ToBody = Importer.Import(S->getBody());
5028 if (!ToBody && S->getBody())
5029 return nullptr;
5030 Expr *ToCondition = Importer.Import(S->getCond());
5031 if (!ToCondition && S->getCond())
5032 return nullptr;
5033 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
5034 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5035 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5036 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
5037 ToDoLoc, ToWhileLoc,
5038 ToRParenLoc);
5039}
5040
5041Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
5042 Stmt *ToInit = Importer.Import(S->getInit());
5043 if (!ToInit && S->getInit())
5044 return nullptr;
5045 Expr *ToCondition = Importer.Import(S->getCond());
5046 if (!ToCondition && S->getCond())
5047 return nullptr;
5048 VarDecl *ToConditionVariable = nullptr;
5049 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5050 ToConditionVariable =
5051 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5052 if (!ToConditionVariable)
5053 return nullptr;
5054 }
5055 Expr *ToInc = Importer.Import(S->getInc());
5056 if (!ToInc && S->getInc())
5057 return nullptr;
5058 Stmt *ToBody = Importer.Import(S->getBody());
5059 if (!ToBody && S->getBody())
5060 return nullptr;
5061 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5062 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
5063 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5064 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
5065 ToInit, ToCondition,
5066 ToConditionVariable,
5067 ToInc, ToBody,
5068 ToForLoc, ToLParenLoc,
5069 ToRParenLoc);
5070}
5071
5072Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5073 LabelDecl *ToLabel = nullptr;
5074 if (LabelDecl *FromLabel = S->getLabel()) {
5075 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
5076 if (!ToLabel)
5077 return nullptr;
5078 }
5079 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5080 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
5081 return new (Importer.getToContext()) GotoStmt(ToLabel,
5082 ToGotoLoc, ToLabelLoc);
5083}
5084
5085Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5086 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5087 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
5088 Expr *ToTarget = Importer.Import(S->getTarget());
5089 if (!ToTarget && S->getTarget())
5090 return nullptr;
5091 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
5092 ToTarget);
5093}
5094
5095Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5096 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
5097 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
5098}
5099
5100Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5101 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
5102 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
5103}
5104
5105Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5106 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
5107 Expr *ToRetExpr = Importer.Import(S->getRetValue());
5108 if (!ToRetExpr && S->getRetValue())
5109 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005110 auto *NRVOCandidate = const_cast<VarDecl *>(S->getNRVOCandidate());
5111 auto *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
Sean Callanan59721b32015-04-28 18:41:46 +00005112 if (!ToNRVOCandidate && NRVOCandidate)
5113 return nullptr;
5114 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
5115 ToNRVOCandidate);
5116}
5117
5118Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5119 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
5120 VarDecl *ToExceptionDecl = nullptr;
5121 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
5122 ToExceptionDecl =
5123 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5124 if (!ToExceptionDecl)
5125 return nullptr;
5126 }
5127 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
5128 if (!ToHandlerBlock && S->getHandlerBlock())
5129 return nullptr;
5130 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
5131 ToExceptionDecl,
5132 ToHandlerBlock);
5133}
5134
5135Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5136 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
5137 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
5138 if (!ToTryBlock && S->getTryBlock())
5139 return nullptr;
5140 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5141 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5142 CXXCatchStmt *FromHandler = S->getHandler(HI);
5143 if (Stmt *ToHandler = Importer.Import(FromHandler))
5144 ToHandlers[HI] = ToHandler;
5145 else
5146 return nullptr;
5147 }
5148 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
5149 ToHandlers);
5150}
5151
5152Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005153 auto *ToRange =
Sean Callanan59721b32015-04-28 18:41:46 +00005154 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
5155 if (!ToRange && S->getRangeStmt())
5156 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005157 auto *ToBegin =
Richard Smith01694c32016-03-20 10:33:40 +00005158 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
5159 if (!ToBegin && S->getBeginStmt())
5160 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005161 auto *ToEnd =
Richard Smith01694c32016-03-20 10:33:40 +00005162 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
5163 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00005164 return nullptr;
5165 Expr *ToCond = Importer.Import(S->getCond());
5166 if (!ToCond && S->getCond())
5167 return nullptr;
5168 Expr *ToInc = Importer.Import(S->getInc());
5169 if (!ToInc && S->getInc())
5170 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005171 auto *ToLoopVar =
Sean Callanan59721b32015-04-28 18:41:46 +00005172 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
5173 if (!ToLoopVar && S->getLoopVarStmt())
5174 return nullptr;
5175 Stmt *ToBody = Importer.Import(S->getBody());
5176 if (!ToBody && S->getBody())
5177 return nullptr;
5178 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00005179 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005180 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5181 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005182 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005183 ToCond, ToInc,
5184 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005185 ToForLoc, ToCoawaitLoc,
5186 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005187}
5188
5189Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5190 Stmt *ToElem = Importer.Import(S->getElement());
5191 if (!ToElem && S->getElement())
5192 return nullptr;
5193 Expr *ToCollect = Importer.Import(S->getCollection());
5194 if (!ToCollect && S->getCollection())
5195 return nullptr;
5196 Stmt *ToBody = Importer.Import(S->getBody());
5197 if (!ToBody && S->getBody())
5198 return nullptr;
5199 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5200 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5201 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5202 ToCollect,
5203 ToBody, ToForLoc,
5204 ToRParenLoc);
5205}
5206
5207Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5208 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5209 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5210 VarDecl *ToExceptionDecl = nullptr;
5211 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5212 ToExceptionDecl =
5213 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5214 if (!ToExceptionDecl)
5215 return nullptr;
5216 }
5217 Stmt *ToBody = Importer.Import(S->getCatchBody());
5218 if (!ToBody && S->getCatchBody())
5219 return nullptr;
5220 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5221 ToRParenLoc,
5222 ToExceptionDecl,
5223 ToBody);
5224}
5225
5226Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5227 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5228 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5229 if (!ToAtFinallyStmt && S->getFinallyBody())
5230 return nullptr;
5231 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5232 ToAtFinallyStmt);
5233}
5234
5235Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5236 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5237 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5238 if (!ToAtTryStmt && S->getTryBody())
5239 return nullptr;
5240 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5241 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5242 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5243 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5244 ToCatchStmts[CI] = ToCatchStmt;
5245 else
5246 return nullptr;
5247 }
5248 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5249 if (!ToAtFinallyStmt && S->getFinallyStmt())
5250 return nullptr;
5251 return ObjCAtTryStmt::Create(Importer.getToContext(),
5252 ToAtTryLoc, ToAtTryStmt,
5253 ToCatchStmts.begin(), ToCatchStmts.size(),
5254 ToAtFinallyStmt);
5255}
5256
5257Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5258 (ObjCAtSynchronizedStmt *S) {
5259 SourceLocation ToAtSynchronizedLoc =
5260 Importer.Import(S->getAtSynchronizedLoc());
5261 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5262 if (!ToSynchExpr && S->getSynchExpr())
5263 return nullptr;
5264 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5265 if (!ToSynchBody && S->getSynchBody())
5266 return nullptr;
5267 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5268 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5269}
5270
5271Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5272 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5273 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5274 if (!ToThrow && S->getThrowExpr())
5275 return nullptr;
5276 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5277}
5278
5279Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5280 (ObjCAutoreleasePoolStmt *S) {
5281 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5282 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5283 if (!ToSubStmt && S->getSubStmt())
5284 return nullptr;
5285 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5286 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005287}
5288
5289//----------------------------------------------------------------------------
5290// Import Expressions
5291//----------------------------------------------------------------------------
5292Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5293 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5294 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005295 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005296}
5297
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005298Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5299 QualType T = Importer.Import(E->getType());
5300 if (T.isNull())
5301 return nullptr;
5302
5303 Expr *SubExpr = Importer.Import(E->getSubExpr());
5304 if (!SubExpr && E->getSubExpr())
5305 return nullptr;
5306
5307 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5308 if (!TInfo)
5309 return nullptr;
5310
5311 return new (Importer.getToContext()) VAArgExpr(
5312 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5313 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5314}
5315
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005316Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5317 QualType T = Importer.Import(E->getType());
5318 if (T.isNull())
5319 return nullptr;
5320
5321 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005322 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005323}
5324
5325Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5326 QualType T = Importer.Import(E->getType());
5327 if (T.isNull())
5328 return nullptr;
5329
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005330 auto *SL = cast_or_null<StringLiteral>(Importer.Import(E->getFunctionName()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005331 if (!SL && E->getFunctionName())
5332 return nullptr;
5333
5334 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005335 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005336}
5337
Douglas Gregor52f820e2010-02-19 01:17:02 +00005338Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005339 auto *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
Douglas Gregor52f820e2010-02-19 01:17:02 +00005340 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005341 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005342
Craig Topper36250ad2014-05-12 05:36:57 +00005343 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005344 if (E->getDecl() != E->getFoundDecl()) {
5345 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5346 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005347 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005348 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005349
5350 QualType T = Importer.Import(E->getType());
5351 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005352 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005353
Aleksei Sidorina693b372016-09-28 10:16:56 +00005354 TemplateArgumentListInfo ToTAInfo;
5355 TemplateArgumentListInfo *ResInfo = nullptr;
5356 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005357 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
5358 return nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00005359 ResInfo = &ToTAInfo;
5360 }
5361
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005362 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5363 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005364 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005365 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005366 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005367 Importer.Import(E->getLocation()),
5368 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005369 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005370 if (E->hadMultipleCandidates())
5371 DRE->setHadMultipleCandidates(true);
5372 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005373}
5374
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005375Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5376 QualType T = Importer.Import(E->getType());
5377 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00005378 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005379
5380 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5381}
5382
5383ASTNodeImporter::Designator
5384ASTNodeImporter::ImportDesignator(const Designator &D) {
5385 if (D.isFieldDesignator()) {
5386 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5387 // Caller checks for import error
5388 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5389 Importer.Import(D.getFieldLoc()));
5390 }
5391 if (D.isArrayDesignator())
5392 return Designator(D.getFirstExprIndex(),
5393 Importer.Import(D.getLBracketLoc()),
5394 Importer.Import(D.getRBracketLoc()));
5395
5396 assert(D.isArrayRangeDesignator());
5397 return Designator(D.getFirstExprIndex(),
5398 Importer.Import(D.getLBracketLoc()),
5399 Importer.Import(D.getEllipsisLoc()),
5400 Importer.Import(D.getRBracketLoc()));
5401}
5402
5403
5404Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005405 auto *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005406 if (!Init)
5407 return nullptr;
5408
5409 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5410 // List elements from the second, the first is Init itself
5411 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005412 if (auto *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005413 IndexExprs[I - 1] = Arg;
5414 else
5415 return nullptr;
5416 }
5417
5418 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005419 llvm::transform(DIE->designators(), Designators.begin(),
5420 [this](const Designator &D) -> Designator {
5421 return ImportDesignator(D);
5422 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005423
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005424 for (const auto &D : DIE->designators())
David Majnemerf7e36092016-06-23 00:15:04 +00005425 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005426 return nullptr;
5427
5428 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005429 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005430 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5431 DIE->usesGNUSyntax(), Init);
5432}
5433
5434Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5435 QualType T = Importer.Import(E->getType());
5436 if (T.isNull())
5437 return nullptr;
5438
5439 return new (Importer.getToContext())
5440 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5441}
5442
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005443Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5444 QualType T = Importer.Import(E->getType());
5445 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005446 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005447
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005448 return IntegerLiteral::Create(Importer.getToContext(),
5449 E->getValue(), T,
5450 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005451}
5452
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005453Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5454 QualType T = Importer.Import(E->getType());
5455 if (T.isNull())
5456 return nullptr;
5457
5458 return FloatingLiteral::Create(Importer.getToContext(),
5459 E->getValue(), E->isExact(), T,
5460 Importer.Import(E->getLocation()));
5461}
5462
Douglas Gregor623421d2010-02-18 02:21:22 +00005463Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5464 QualType T = Importer.Import(E->getType());
5465 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005466 return nullptr;
5467
Douglas Gregorfb65e592011-07-27 05:40:30 +00005468 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5469 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005470 Importer.Import(E->getLocation()));
5471}
5472
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005473Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5474 QualType T = Importer.Import(E->getType());
5475 if (T.isNull())
5476 return nullptr;
5477
5478 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5479 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5480
5481 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5482 E->getKind(), E->isPascal(), T,
5483 Locations.data(), Locations.size());
5484}
5485
5486Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5487 QualType T = Importer.Import(E->getType());
5488 if (T.isNull())
5489 return nullptr;
5490
5491 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5492 if (!TInfo)
5493 return nullptr;
5494
5495 Expr *Init = Importer.Import(E->getInitializer());
5496 if (!Init)
5497 return nullptr;
5498
5499 return new (Importer.getToContext()) CompoundLiteralExpr(
5500 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5501 Init, E->isFileScope());
5502}
5503
5504Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5505 QualType T = Importer.Import(E->getType());
5506 if (T.isNull())
5507 return nullptr;
5508
5509 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5510 if (ImportArrayChecked(
5511 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5512 Exprs.begin()))
5513 return nullptr;
5514
5515 return new (Importer.getToContext()) AtomicExpr(
5516 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5517 Importer.Import(E->getRParenLoc()));
5518}
5519
5520Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5521 QualType T = Importer.Import(E->getType());
5522 if (T.isNull())
5523 return nullptr;
5524
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005525 auto *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005526 if (!ToLabel)
5527 return nullptr;
5528
5529 return new (Importer.getToContext()) AddrLabelExpr(
5530 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5531 ToLabel, T);
5532}
5533
Douglas Gregorc74247e2010-02-19 01:07:06 +00005534Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5535 Expr *SubExpr = Importer.Import(E->getSubExpr());
5536 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005537 return nullptr;
5538
Douglas Gregorc74247e2010-02-19 01:07:06 +00005539 return new (Importer.getToContext())
5540 ParenExpr(Importer.Import(E->getLParen()),
5541 Importer.Import(E->getRParen()),
5542 SubExpr);
5543}
5544
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005545Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5546 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005547 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005548 return nullptr;
5549
5550 return new (Importer.getToContext()) ParenListExpr(
5551 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5552 Exprs, Importer.Import(E->getLParenLoc()));
5553}
5554
5555Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5556 QualType T = Importer.Import(E->getType());
5557 if (T.isNull())
5558 return nullptr;
5559
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005560 auto *ToSubStmt = cast_or_null<CompoundStmt>(
5561 Importer.Import(E->getSubStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005562 if (!ToSubStmt && E->getSubStmt())
5563 return nullptr;
5564
5565 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5566 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5567}
5568
Douglas Gregorc74247e2010-02-19 01:07:06 +00005569Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5570 QualType T = Importer.Import(E->getType());
5571 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005572 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005573
5574 Expr *SubExpr = Importer.Import(E->getSubExpr());
5575 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005576 return nullptr;
5577
Aaron Ballmana5038552018-01-09 13:07:03 +00005578 return new (Importer.getToContext()) UnaryOperator(
5579 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(),
5580 Importer.Import(E->getOperatorLoc()), E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005581}
5582
Aaron Ballmana5038552018-01-09 13:07:03 +00005583Expr *
5584ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005585 QualType ResultType = Importer.Import(E->getType());
5586
5587 if (E->isArgumentType()) {
5588 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5589 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005590 return nullptr;
5591
Peter Collingbournee190dee2011-03-11 19:24:49 +00005592 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5593 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005594 Importer.Import(E->getOperatorLoc()),
5595 Importer.Import(E->getRParenLoc()));
5596 }
5597
5598 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5599 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005600 return nullptr;
5601
Peter Collingbournee190dee2011-03-11 19:24:49 +00005602 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5603 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005604 Importer.Import(E->getOperatorLoc()),
5605 Importer.Import(E->getRParenLoc()));
5606}
5607
Douglas Gregorc74247e2010-02-19 01:07:06 +00005608Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5609 QualType T = Importer.Import(E->getType());
5610 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005611 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005612
5613 Expr *LHS = Importer.Import(E->getLHS());
5614 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005615 return nullptr;
5616
Douglas Gregorc74247e2010-02-19 01:07:06 +00005617 Expr *RHS = Importer.Import(E->getRHS());
5618 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005619 return nullptr;
5620
Douglas Gregorc74247e2010-02-19 01:07:06 +00005621 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005622 T, E->getValueKind(),
5623 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005624 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005625 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005626}
5627
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005628Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5629 QualType T = Importer.Import(E->getType());
5630 if (T.isNull())
5631 return nullptr;
5632
5633 Expr *ToLHS = Importer.Import(E->getLHS());
5634 if (!ToLHS)
5635 return nullptr;
5636
5637 Expr *ToRHS = Importer.Import(E->getRHS());
5638 if (!ToRHS)
5639 return nullptr;
5640
5641 Expr *ToCond = Importer.Import(E->getCond());
5642 if (!ToCond)
5643 return nullptr;
5644
5645 return new (Importer.getToContext()) ConditionalOperator(
5646 ToCond, Importer.Import(E->getQuestionLoc()),
5647 ToLHS, Importer.Import(E->getColonLoc()),
5648 ToRHS, T, E->getValueKind(), E->getObjectKind());
5649}
5650
5651Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5652 BinaryConditionalOperator *E) {
5653 QualType T = Importer.Import(E->getType());
5654 if (T.isNull())
5655 return nullptr;
5656
5657 Expr *Common = Importer.Import(E->getCommon());
5658 if (!Common)
5659 return nullptr;
5660
5661 Expr *Cond = Importer.Import(E->getCond());
5662 if (!Cond)
5663 return nullptr;
5664
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005665 auto *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5666 Importer.Import(E->getOpaqueValue()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005667 if (!OpaqueValue)
5668 return nullptr;
5669
5670 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5671 if (!TrueExpr)
5672 return nullptr;
5673
5674 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5675 if (!FalseExpr)
5676 return nullptr;
5677
5678 return new (Importer.getToContext()) BinaryConditionalOperator(
5679 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5680 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5681 T, E->getValueKind(), E->getObjectKind());
5682}
5683
Aleksei Sidorina693b372016-09-28 10:16:56 +00005684Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5685 QualType T = Importer.Import(E->getType());
5686 if (T.isNull())
5687 return nullptr;
5688
5689 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5690 if (!ToQueried)
5691 return nullptr;
5692
5693 Expr *Dim = Importer.Import(E->getDimensionExpression());
5694 if (!Dim && E->getDimensionExpression())
5695 return nullptr;
5696
5697 return new (Importer.getToContext()) ArrayTypeTraitExpr(
5698 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5699 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
5700}
5701
5702Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5703 QualType T = Importer.Import(E->getType());
5704 if (T.isNull())
5705 return nullptr;
5706
5707 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5708 if (!ToQueried)
5709 return nullptr;
5710
5711 return new (Importer.getToContext()) ExpressionTraitExpr(
5712 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5713 E->getValue(), Importer.Import(E->getLocEnd()), T);
5714}
5715
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005716Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5717 QualType T = Importer.Import(E->getType());
5718 if (T.isNull())
5719 return nullptr;
5720
5721 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5722 if (!SourceExpr && E->getSourceExpr())
5723 return nullptr;
5724
5725 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005726 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005727 E->getObjectKind(), SourceExpr);
5728}
5729
Aleksei Sidorina693b372016-09-28 10:16:56 +00005730Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5731 QualType T = Importer.Import(E->getType());
5732 if (T.isNull())
5733 return nullptr;
5734
5735 Expr *ToLHS = Importer.Import(E->getLHS());
5736 if (!ToLHS)
5737 return nullptr;
5738
5739 Expr *ToRHS = Importer.Import(E->getRHS());
5740 if (!ToRHS)
5741 return nullptr;
5742
5743 return new (Importer.getToContext()) ArraySubscriptExpr(
5744 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5745 Importer.Import(E->getRBracketLoc()));
5746}
5747
Douglas Gregorc74247e2010-02-19 01:07:06 +00005748Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5749 QualType T = Importer.Import(E->getType());
5750 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005751 return nullptr;
5752
Douglas Gregorc74247e2010-02-19 01:07:06 +00005753 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5754 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005755 return nullptr;
5756
Douglas Gregorc74247e2010-02-19 01:07:06 +00005757 QualType CompResultType = Importer.Import(E->getComputationResultType());
5758 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005759 return nullptr;
5760
Douglas Gregorc74247e2010-02-19 01:07:06 +00005761 Expr *LHS = Importer.Import(E->getLHS());
5762 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005763 return nullptr;
5764
Douglas Gregorc74247e2010-02-19 01:07:06 +00005765 Expr *RHS = Importer.Import(E->getRHS());
5766 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005767 return nullptr;
5768
Douglas Gregorc74247e2010-02-19 01:07:06 +00005769 return new (Importer.getToContext())
5770 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005771 T, E->getValueKind(),
5772 E->getObjectKind(),
5773 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005774 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005775 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005776}
5777
Aleksei Sidorina693b372016-09-28 10:16:56 +00005778bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
5779 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
5780 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
5781 Path.push_back(Spec);
5782 else
5783 return true;
5784 }
5785 return false;
John McCallcf142162010-08-07 06:22:56 +00005786}
5787
Douglas Gregor98c10182010-02-12 22:17:39 +00005788Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5789 QualType T = Importer.Import(E->getType());
5790 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005791 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005792
5793 Expr *SubExpr = Importer.Import(E->getSubExpr());
5794 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005795 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005796
5797 CXXCastPath BasePath;
5798 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005799 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005800
5801 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005802 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005803}
5804
Aleksei Sidorina693b372016-09-28 10:16:56 +00005805Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00005806 QualType T = Importer.Import(E->getType());
5807 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005808 return nullptr;
5809
Douglas Gregor5481d322010-02-19 01:32:14 +00005810 Expr *SubExpr = Importer.Import(E->getSubExpr());
5811 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005812 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005813
5814 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5815 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005816 return nullptr;
5817
John McCallcf142162010-08-07 06:22:56 +00005818 CXXCastPath BasePath;
5819 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005820 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005821
Aleksei Sidorina693b372016-09-28 10:16:56 +00005822 switch (E->getStmtClass()) {
5823 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005824 auto *CCE = cast<CStyleCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005825 return CStyleCastExpr::Create(Importer.getToContext(), T,
5826 E->getValueKind(), E->getCastKind(),
5827 SubExpr, &BasePath, TInfo,
5828 Importer.Import(CCE->getLParenLoc()),
5829 Importer.Import(CCE->getRParenLoc()));
5830 }
5831
5832 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005833 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005834 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
5835 E->getValueKind(), TInfo,
5836 E->getCastKind(), SubExpr, &BasePath,
5837 Importer.Import(FCE->getLParenLoc()),
5838 Importer.Import(FCE->getRParenLoc()));
5839 }
5840
5841 case Stmt::ObjCBridgedCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005842 auto *OCE = cast<ObjCBridgedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005843 return new (Importer.getToContext()) ObjCBridgedCastExpr(
5844 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
5845 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
5846 TInfo, SubExpr);
5847 }
5848 default:
5849 break; // just fall through
5850 }
5851
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005852 auto *Named = cast<CXXNamedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005853 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
5854 RParenLoc = Importer.Import(Named->getRParenLoc());
5855 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
5856
5857 switch (E->getStmtClass()) {
5858 case Stmt::CXXStaticCastExprClass:
5859 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
5860 E->getValueKind(), E->getCastKind(),
5861 SubExpr, &BasePath, TInfo,
5862 ExprLoc, RParenLoc, Brackets);
5863
5864 case Stmt::CXXDynamicCastExprClass:
5865 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
5866 E->getValueKind(), E->getCastKind(),
5867 SubExpr, &BasePath, TInfo,
5868 ExprLoc, RParenLoc, Brackets);
5869
5870 case Stmt::CXXReinterpretCastExprClass:
5871 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
5872 E->getValueKind(), E->getCastKind(),
5873 SubExpr, &BasePath, TInfo,
5874 ExprLoc, RParenLoc, Brackets);
5875
5876 case Stmt::CXXConstCastExprClass:
5877 return CXXConstCastExpr::Create(Importer.getToContext(), T,
5878 E->getValueKind(), SubExpr, TInfo, ExprLoc,
5879 RParenLoc, Brackets);
5880 default:
5881 llvm_unreachable("Cast expression of unsupported type!");
5882 return nullptr;
5883 }
5884}
5885
5886Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
5887 QualType T = Importer.Import(OE->getType());
5888 if (T.isNull())
5889 return nullptr;
5890
5891 SmallVector<OffsetOfNode, 4> Nodes;
5892 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
5893 const OffsetOfNode &Node = OE->getComponent(I);
5894
5895 switch (Node.getKind()) {
5896 case OffsetOfNode::Array:
5897 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
5898 Node.getArrayExprIndex(),
5899 Importer.Import(Node.getLocEnd())));
5900 break;
5901
5902 case OffsetOfNode::Base: {
5903 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
5904 if (!BS && Node.getBase())
5905 return nullptr;
5906 Nodes.push_back(OffsetOfNode(BS));
5907 break;
5908 }
5909 case OffsetOfNode::Field: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005910 auto *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00005911 if (!FD)
5912 return nullptr;
5913 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
5914 Importer.Import(Node.getLocEnd())));
5915 break;
5916 }
5917 case OffsetOfNode::Identifier: {
5918 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
5919 if (!ToII)
5920 return nullptr;
5921 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
5922 Importer.Import(Node.getLocEnd())));
5923 break;
5924 }
5925 }
5926 }
5927
5928 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
5929 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
5930 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
5931 if (!ToIndexExpr)
5932 return nullptr;
5933 Exprs[I] = ToIndexExpr;
5934 }
5935
5936 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
5937 if (!TInfo && OE->getTypeSourceInfo())
5938 return nullptr;
5939
5940 return OffsetOfExpr::Create(Importer.getToContext(), T,
5941 Importer.Import(OE->getOperatorLoc()),
5942 TInfo, Nodes, Exprs,
5943 Importer.Import(OE->getRParenLoc()));
5944}
5945
5946Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
5947 QualType T = Importer.Import(E->getType());
5948 if (T.isNull())
5949 return nullptr;
5950
5951 Expr *Operand = Importer.Import(E->getOperand());
5952 if (!Operand)
5953 return nullptr;
5954
5955 CanThrowResult CanThrow;
5956 if (E->isValueDependent())
5957 CanThrow = CT_Dependent;
5958 else
5959 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
5960
5961 return new (Importer.getToContext()) CXXNoexceptExpr(
5962 T, Operand, CanThrow,
5963 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
5964}
5965
5966Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
5967 QualType T = Importer.Import(E->getType());
5968 if (T.isNull())
5969 return nullptr;
5970
5971 Expr *SubExpr = Importer.Import(E->getSubExpr());
5972 if (!SubExpr && E->getSubExpr())
5973 return nullptr;
5974
5975 return new (Importer.getToContext()) CXXThrowExpr(
5976 SubExpr, T, Importer.Import(E->getThrowLoc()),
5977 E->isThrownVariableInScope());
5978}
5979
5980Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005981 auto *Param = cast_or_null<ParmVarDecl>(Importer.Import(E->getParam()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00005982 if (!Param)
5983 return nullptr;
5984
5985 return CXXDefaultArgExpr::Create(
5986 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
5987}
5988
5989Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5990 QualType T = Importer.Import(E->getType());
5991 if (T.isNull())
5992 return nullptr;
5993
5994 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
5995 if (!TypeInfo)
5996 return nullptr;
5997
5998 return new (Importer.getToContext()) CXXScalarValueInitExpr(
5999 T, TypeInfo, Importer.Import(E->getRParenLoc()));
6000}
6001
6002Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6003 Expr *SubExpr = Importer.Import(E->getSubExpr());
6004 if (!SubExpr)
6005 return nullptr;
6006
6007 auto *Dtor = cast_or_null<CXXDestructorDecl>(
6008 Importer.Import(const_cast<CXXDestructorDecl *>(
6009 E->getTemporary()->getDestructor())));
6010 if (!Dtor)
6011 return nullptr;
6012
6013 ASTContext &ToCtx = Importer.getToContext();
6014 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
6015 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
6016}
6017
6018Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
6019 QualType T = Importer.Import(CE->getType());
6020 if (T.isNull())
6021 return nullptr;
6022
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006023 TypeSourceInfo *TInfo = Importer.Import(CE->getTypeSourceInfo());
6024 if (!TInfo)
6025 return nullptr;
6026
Aleksei Sidorina693b372016-09-28 10:16:56 +00006027 SmallVector<Expr *, 8> Args(CE->getNumArgs());
6028 if (ImportContainerChecked(CE->arguments(), Args))
6029 return nullptr;
6030
6031 auto *Ctor = cast_or_null<CXXConstructorDecl>(
6032 Importer.Import(CE->getConstructor()));
6033 if (!Ctor)
6034 return nullptr;
6035
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006036 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
6037 Importer.getToContext(), Ctor, T, TInfo, Args,
6038 Importer.Import(CE->getParenOrBraceRange()), CE->hadMultipleCandidates(),
6039 CE->isListInitialization(), CE->isStdInitListInitialization(),
6040 CE->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006041}
6042
6043Expr *
6044ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
6045 QualType T = Importer.Import(E->getType());
6046 if (T.isNull())
6047 return nullptr;
6048
6049 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
6050 if (!TempE)
6051 return nullptr;
6052
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006053 auto *ExtendedBy = cast_or_null<ValueDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006054 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
6055 if (!ExtendedBy && E->getExtendingDecl())
6056 return nullptr;
6057
6058 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
6059 T, TempE, E->isBoundToLvalueReference());
6060
6061 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
6062 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
6063 return ToMTE;
6064}
6065
Gabor Horvath7a91c082017-11-14 11:30:38 +00006066Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6067 QualType T = Importer.Import(E->getType());
6068 if (T.isNull())
6069 return nullptr;
6070
6071 Expr *Pattern = Importer.Import(E->getPattern());
6072 if (!Pattern)
6073 return nullptr;
6074
6075 return new (Importer.getToContext()) PackExpansionExpr(
6076 T, Pattern, Importer.Import(E->getEllipsisLoc()),
6077 E->getNumExpansions());
6078}
6079
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006080Expr *ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6081 auto *Pack = cast_or_null<NamedDecl>(Importer.Import(E->getPack()));
6082 if (!Pack)
6083 return nullptr;
6084
6085 Optional<unsigned> Length;
6086
6087 if (!E->isValueDependent())
6088 Length = E->getPackLength();
6089
6090 SmallVector<TemplateArgument, 8> PartialArguments;
6091 if (E->isPartiallySubstituted()) {
6092 if (ImportTemplateArguments(E->getPartialArguments().data(),
6093 E->getPartialArguments().size(),
6094 PartialArguments))
6095 return nullptr;
6096 }
6097
6098 return SizeOfPackExpr::Create(
6099 Importer.getToContext(), Importer.Import(E->getOperatorLoc()), Pack,
6100 Importer.Import(E->getPackLoc()), Importer.Import(E->getRParenLoc()),
6101 Length, PartialArguments);
6102}
6103
Aleksei Sidorina693b372016-09-28 10:16:56 +00006104Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
6105 QualType T = Importer.Import(CE->getType());
6106 if (T.isNull())
6107 return nullptr;
6108
6109 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
6110 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
6111 return nullptr;
6112
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006113 auto *OperatorNewDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006114 Importer.Import(CE->getOperatorNew()));
6115 if (!OperatorNewDecl && CE->getOperatorNew())
6116 return nullptr;
6117
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006118 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006119 Importer.Import(CE->getOperatorDelete()));
6120 if (!OperatorDeleteDecl && CE->getOperatorDelete())
6121 return nullptr;
6122
6123 Expr *ToInit = Importer.Import(CE->getInitializer());
6124 if (!ToInit && CE->getInitializer())
6125 return nullptr;
6126
6127 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
6128 if (!TInfo)
6129 return nullptr;
6130
6131 Expr *ToArrSize = Importer.Import(CE->getArraySize());
6132 if (!ToArrSize && CE->getArraySize())
6133 return nullptr;
6134
6135 return new (Importer.getToContext()) CXXNewExpr(
6136 Importer.getToContext(),
6137 CE->isGlobalNew(),
6138 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00006139 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00006140 CE->doesUsualArrayDeleteWantSize(),
6141 PlacementArgs,
6142 Importer.Import(CE->getTypeIdParens()),
6143 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
6144 Importer.Import(CE->getSourceRange()),
6145 Importer.Import(CE->getDirectInitRange()));
6146}
6147
6148Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6149 QualType T = Importer.Import(E->getType());
6150 if (T.isNull())
6151 return nullptr;
6152
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006153 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006154 Importer.Import(E->getOperatorDelete()));
6155 if (!OperatorDeleteDecl && E->getOperatorDelete())
6156 return nullptr;
6157
6158 Expr *ToArg = Importer.Import(E->getArgument());
6159 if (!ToArg && E->getArgument())
6160 return nullptr;
6161
6162 return new (Importer.getToContext()) CXXDeleteExpr(
6163 T, E->isGlobalDelete(),
6164 E->isArrayForm(),
6165 E->isArrayFormAsWritten(),
6166 E->doesUsualArrayDeleteWantSize(),
6167 OperatorDeleteDecl,
6168 ToArg,
6169 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00006170}
6171
Sean Callanan59721b32015-04-28 18:41:46 +00006172Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6173 QualType T = Importer.Import(E->getType());
6174 if (T.isNull())
6175 return nullptr;
6176
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006177 auto *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00006178 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00006179 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00006180 return nullptr;
6181
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006182 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006183 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006184 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006185
6186 return CXXConstructExpr::Create(Importer.getToContext(), T,
6187 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00006188 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00006189 ToArgs, E->hadMultipleCandidates(),
6190 E->isListInitialization(),
6191 E->isStdInitListInitialization(),
6192 E->requiresZeroInitialization(),
6193 E->getConstructionKind(),
6194 Importer.Import(E->getParenOrBraceRange()));
6195}
6196
Aleksei Sidorina693b372016-09-28 10:16:56 +00006197Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
6198 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
6199 if (!SubExpr && EWC->getSubExpr())
6200 return nullptr;
6201
6202 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
6203 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
6204 if (ExprWithCleanups::CleanupObject Obj =
6205 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
6206 Objs[I] = Obj;
6207 else
6208 return nullptr;
6209
6210 return ExprWithCleanups::Create(Importer.getToContext(),
6211 SubExpr, EWC->cleanupsHaveSideEffects(),
6212 Objs);
6213}
6214
Sean Callanan8bca9962016-03-28 21:43:01 +00006215Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6216 QualType T = Importer.Import(E->getType());
6217 if (T.isNull())
6218 return nullptr;
6219
6220 Expr *ToFn = Importer.Import(E->getCallee());
6221 if (!ToFn)
6222 return nullptr;
6223
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006224 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006225 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006226 return nullptr;
6227
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006228 return new (Importer.getToContext()) CXXMemberCallExpr(
6229 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
6230 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00006231}
6232
6233Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6234 QualType T = Importer.Import(E->getType());
6235 if (T.isNull())
6236 return nullptr;
6237
6238 return new (Importer.getToContext())
6239 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
6240}
6241
6242Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
6243 QualType T = Importer.Import(E->getType());
6244 if (T.isNull())
6245 return nullptr;
6246
6247 return new (Importer.getToContext())
6248 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
6249}
6250
6251
Sean Callanan59721b32015-04-28 18:41:46 +00006252Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
6253 QualType T = Importer.Import(E->getType());
6254 if (T.isNull())
6255 return nullptr;
6256
6257 Expr *ToBase = Importer.Import(E->getBase());
6258 if (!ToBase && E->getBase())
6259 return nullptr;
6260
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006261 auto *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00006262 if (!ToMember && E->getMemberDecl())
6263 return nullptr;
6264
Peter Szecsief972522018-05-02 11:52:54 +00006265 auto *ToDecl =
6266 dyn_cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl()));
6267 if (!ToDecl && E->getFoundDecl().getDecl())
6268 return nullptr;
6269
6270 DeclAccessPair ToFoundDecl =
6271 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00006272
6273 DeclarationNameInfo ToMemberNameInfo(
6274 Importer.Import(E->getMemberNameInfo().getName()),
6275 Importer.Import(E->getMemberNameInfo().getLoc()));
6276
6277 if (E->hasExplicitTemplateArgs()) {
6278 return nullptr; // FIXME: handle template arguments
6279 }
6280
6281 return MemberExpr::Create(Importer.getToContext(), ToBase,
6282 E->isArrow(),
6283 Importer.Import(E->getOperatorLoc()),
6284 Importer.Import(E->getQualifierLoc()),
6285 Importer.Import(E->getTemplateKeywordLoc()),
6286 ToMember, ToFoundDecl, ToMemberNameInfo,
6287 nullptr, T, E->getValueKind(),
6288 E->getObjectKind());
6289}
6290
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006291Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr(
6292 CXXPseudoDestructorExpr *E) {
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006293 Expr *BaseE = Importer.Import(E->getBase());
6294 if (!BaseE)
6295 return nullptr;
6296
6297 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo());
6298 if (!ScopeInfo && E->getScopeTypeInfo())
6299 return nullptr;
6300
6301 PseudoDestructorTypeStorage Storage;
6302 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
6303 IdentifierInfo *ToII = Importer.Import(FromII);
6304 if (!ToII)
6305 return nullptr;
6306 Storage = PseudoDestructorTypeStorage(
6307 ToII, Importer.Import(E->getDestroyedTypeLoc()));
6308 } else {
6309 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo());
6310 if (!TI)
6311 return nullptr;
6312 Storage = PseudoDestructorTypeStorage(TI);
6313 }
6314
6315 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
6316 Importer.getToContext(), BaseE, E->isArrow(),
6317 Importer.Import(E->getOperatorLoc()),
6318 Importer.Import(E->getQualifierLoc()),
6319 ScopeInfo, Importer.Import(E->getColonColonLoc()),
6320 Importer.Import(E->getTildeLoc()), Storage);
6321}
6322
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006323Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
6324 CXXDependentScopeMemberExpr *E) {
6325 Expr *Base = nullptr;
6326 if (!E->isImplicitAccess()) {
6327 Base = Importer.Import(E->getBase());
6328 if (!Base)
6329 return nullptr;
6330 }
6331
6332 QualType BaseType = Importer.Import(E->getBaseType());
6333 if (BaseType.isNull())
6334 return nullptr;
6335
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006336 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006337 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006338 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6339 E->template_arguments(), ToTAInfo))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006340 return nullptr;
6341 ResInfo = &ToTAInfo;
6342 }
6343
6344 DeclarationName Name = Importer.Import(E->getMember());
6345 if (!E->getMember().isEmpty() && Name.isEmpty())
6346 return nullptr;
6347
6348 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc()));
6349 // Import additional name location/type info.
6350 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo);
6351 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope());
6352 if (!ToFQ && E->getFirstQualifierFoundInScope())
6353 return nullptr;
6354
6355 return CXXDependentScopeMemberExpr::Create(
6356 Importer.getToContext(), Base, BaseType, E->isArrow(),
6357 Importer.Import(E->getOperatorLoc()),
6358 Importer.Import(E->getQualifierLoc()),
6359 Importer.Import(E->getTemplateKeywordLoc()),
6360 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo);
6361}
6362
Peter Szecsice7f3182018-05-07 12:08:27 +00006363Expr *
6364ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
6365 DeclarationName Name = Importer.Import(E->getDeclName());
6366 if (!E->getDeclName().isEmpty() && Name.isEmpty())
6367 return nullptr;
6368
6369 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getExprLoc()));
6370 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6371
6372 TemplateArgumentListInfo ToTAInfo(Importer.Import(E->getLAngleLoc()),
6373 Importer.Import(E->getRAngleLoc()));
6374 TemplateArgumentListInfo *ResInfo = nullptr;
6375 if (E->hasExplicitTemplateArgs()) {
6376 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6377 return nullptr;
6378 ResInfo = &ToTAInfo;
6379 }
6380
6381 return DependentScopeDeclRefExpr::Create(
6382 Importer.getToContext(), Importer.Import(E->getQualifierLoc()),
6383 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo);
6384}
6385
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006386Expr *ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
6387 CXXUnresolvedConstructExpr *CE) {
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006388 unsigned NumArgs = CE->arg_size();
6389
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006390 SmallVector<Expr *, 8> ToArgs(NumArgs);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006391 if (ImportArrayChecked(CE->arg_begin(), CE->arg_end(), ToArgs.begin()))
6392 return nullptr;
6393
6394 return CXXUnresolvedConstructExpr::Create(
6395 Importer.getToContext(), Importer.Import(CE->getTypeSourceInfo()),
6396 Importer.Import(CE->getLParenLoc()), llvm::makeArrayRef(ToArgs),
6397 Importer.Import(CE->getRParenLoc()));
6398}
6399
6400Expr *ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006401 auto *NamingClass =
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006402 cast_or_null<CXXRecordDecl>(Importer.Import(E->getNamingClass()));
6403 if (E->getNamingClass() && !NamingClass)
6404 return nullptr;
6405
6406 DeclarationName Name = Importer.Import(E->getName());
6407 if (E->getName() && !Name)
6408 return nullptr;
6409
6410 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6411 // Import additional name location/type info.
6412 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6413
6414 UnresolvedSet<8> ToDecls;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006415 for (auto *D : E->decls()) {
6416 if (auto *To = cast_or_null<NamedDecl>(Importer.Import(D)))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006417 ToDecls.addDecl(To);
6418 else
6419 return nullptr;
6420 }
6421
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006422 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006423 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006424 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6425 E->template_arguments(), ToTAInfo))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006426 return nullptr;
6427 ResInfo = &ToTAInfo;
6428 }
6429
6430 if (ResInfo || E->getTemplateKeywordLoc().isValid())
6431 return UnresolvedLookupExpr::Create(
6432 Importer.getToContext(), NamingClass,
6433 Importer.Import(E->getQualifierLoc()),
6434 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, E->requiresADL(),
6435 ResInfo, ToDecls.begin(), ToDecls.end());
6436
6437 return UnresolvedLookupExpr::Create(
6438 Importer.getToContext(), NamingClass,
6439 Importer.Import(E->getQualifierLoc()), NameInfo, E->requiresADL(),
6440 E->isOverloaded(), ToDecls.begin(), ToDecls.end());
6441}
6442
Peter Szecsice7f3182018-05-07 12:08:27 +00006443Expr *ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
6444 DeclarationName Name = Importer.Import(E->getName());
6445 if (!E->getName().isEmpty() && Name.isEmpty())
6446 return nullptr;
6447 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6448 // Import additional name location/type info.
6449 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6450
6451 QualType BaseType = Importer.Import(E->getType());
6452 if (!E->getType().isNull() && BaseType.isNull())
6453 return nullptr;
6454
6455 UnresolvedSet<8> ToDecls;
6456 for (Decl *D : E->decls()) {
6457 if (NamedDecl *To = cast_or_null<NamedDecl>(Importer.Import(D)))
6458 ToDecls.addDecl(To);
6459 else
6460 return nullptr;
6461 }
6462
6463 TemplateArgumentListInfo ToTAInfo;
6464 TemplateArgumentListInfo *ResInfo = nullptr;
6465 if (E->hasExplicitTemplateArgs()) {
6466 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6467 return nullptr;
6468 ResInfo = &ToTAInfo;
6469 }
6470
6471 Expr *BaseE = E->isImplicitAccess() ? nullptr : Importer.Import(E->getBase());
6472 if (!BaseE && !E->isImplicitAccess() && E->getBase()) {
6473 return nullptr;
6474 }
6475
6476 return UnresolvedMemberExpr::Create(
6477 Importer.getToContext(), E->hasUnresolvedUsing(), BaseE, BaseType,
6478 E->isArrow(), Importer.Import(E->getOperatorLoc()),
6479 Importer.Import(E->getQualifierLoc()),
6480 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo,
6481 ToDecls.begin(), ToDecls.end());
6482}
6483
Sean Callanan59721b32015-04-28 18:41:46 +00006484Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
6485 QualType T = Importer.Import(E->getType());
6486 if (T.isNull())
6487 return nullptr;
6488
6489 Expr *ToCallee = Importer.Import(E->getCallee());
6490 if (!ToCallee && E->getCallee())
6491 return nullptr;
6492
6493 unsigned NumArgs = E->getNumArgs();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006494 SmallVector<Expr *, 2> ToArgs(NumArgs);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006495 if (ImportContainerChecked(E->arguments(), ToArgs))
6496 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006497
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006498 auto **ToArgs_Copied = new (Importer.getToContext()) Expr*[NumArgs];
Sean Callanan59721b32015-04-28 18:41:46 +00006499
6500 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
6501 ToArgs_Copied[ai] = ToArgs[ai];
6502
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006503 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
6504 return new (Importer.getToContext()) CXXOperatorCallExpr(
6505 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, T,
6506 OCE->getValueKind(), Importer.Import(OCE->getRParenLoc()),
6507 OCE->getFPFeatures());
6508 }
6509
Sean Callanan59721b32015-04-28 18:41:46 +00006510 return new (Importer.getToContext())
6511 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00006512 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00006513 Importer.Import(E->getRParenLoc()));
6514}
6515
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006516Optional<LambdaCapture>
6517ASTNodeImporter::ImportLambdaCapture(const LambdaCapture &From) {
6518 VarDecl *Var = nullptr;
6519 if (From.capturesVariable()) {
6520 Var = cast_or_null<VarDecl>(Importer.Import(From.getCapturedVar()));
6521 if (!Var)
6522 return None;
6523 }
6524
6525 return LambdaCapture(Importer.Import(From.getLocation()), From.isImplicit(),
6526 From.getCaptureKind(), Var,
6527 From.isPackExpansion()
6528 ? Importer.Import(From.getEllipsisLoc())
6529 : SourceLocation());
6530}
6531
6532Expr *ASTNodeImporter::VisitLambdaExpr(LambdaExpr *LE) {
6533 CXXRecordDecl *FromClass = LE->getLambdaClass();
6534 auto *ToClass = dyn_cast_or_null<CXXRecordDecl>(Importer.Import(FromClass));
6535 if (!ToClass)
6536 return nullptr;
6537
6538 // NOTE: lambda classes are created with BeingDefined flag set up.
6539 // It means that ImportDefinition doesn't work for them and we should fill it
6540 // manually.
6541 if (ToClass->isBeingDefined()) {
6542 for (auto FromField : FromClass->fields()) {
6543 auto *ToField = cast_or_null<FieldDecl>(Importer.Import(FromField));
6544 if (!ToField)
6545 return nullptr;
6546 }
6547 }
6548
6549 auto *ToCallOp = dyn_cast_or_null<CXXMethodDecl>(
6550 Importer.Import(LE->getCallOperator()));
6551 if (!ToCallOp)
6552 return nullptr;
6553
6554 ToClass->completeDefinition();
6555
6556 unsigned NumCaptures = LE->capture_size();
6557 SmallVector<LambdaCapture, 8> Captures;
6558 Captures.reserve(NumCaptures);
6559 for (const auto &FromCapture : LE->captures()) {
6560 if (auto ToCapture = ImportLambdaCapture(FromCapture))
6561 Captures.push_back(*ToCapture);
6562 else
6563 return nullptr;
6564 }
6565
6566 SmallVector<Expr *, 8> InitCaptures(NumCaptures);
6567 if (ImportContainerChecked(LE->capture_inits(), InitCaptures))
6568 return nullptr;
6569
6570 return LambdaExpr::Create(Importer.getToContext(), ToClass,
6571 Importer.Import(LE->getIntroducerRange()),
6572 LE->getCaptureDefault(),
6573 Importer.Import(LE->getCaptureDefaultLoc()),
6574 Captures,
6575 LE->hasExplicitParameters(),
6576 LE->hasExplicitResultType(),
6577 InitCaptures,
6578 Importer.Import(LE->getLocEnd()),
6579 LE->containsUnexpandedParameterPack());
6580}
6581
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006582Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
6583 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00006584 if (T.isNull())
6585 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006586
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006587 SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006588 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006589 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006590
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006591 ASTContext &ToCtx = Importer.getToContext();
6592 InitListExpr *To = new (ToCtx) InitListExpr(
6593 ToCtx, Importer.Import(ILE->getLBraceLoc()),
6594 Exprs, Importer.Import(ILE->getLBraceLoc()));
6595 To->setType(T);
6596
6597 if (ILE->hasArrayFiller()) {
6598 Expr *Filler = Importer.Import(ILE->getArrayFiller());
6599 if (!Filler)
6600 return nullptr;
6601 To->setArrayFiller(Filler);
6602 }
6603
6604 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006605 auto *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006606 if (!ToFD)
6607 return nullptr;
6608 To->setInitializedFieldInUnion(ToFD);
6609 }
6610
6611 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006612 auto *ToSyntForm = cast_or_null<InitListExpr>(Importer.Import(SyntForm));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006613 if (!ToSyntForm)
6614 return nullptr;
6615 To->setSyntacticForm(ToSyntForm);
6616 }
6617
6618 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
6619 To->setValueDependent(ILE->isValueDependent());
6620 To->setInstantiationDependent(ILE->isInstantiationDependent());
6621
6622 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00006623}
6624
Richard Smith30e304e2016-12-14 00:03:17 +00006625Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
6626 QualType ToType = Importer.Import(E->getType());
6627 if (ToType.isNull())
6628 return nullptr;
6629
6630 Expr *ToCommon = Importer.Import(E->getCommonExpr());
6631 if (!ToCommon && E->getCommonExpr())
6632 return nullptr;
6633
6634 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
6635 if (!ToSubExpr && E->getSubExpr())
6636 return nullptr;
6637
6638 return new (Importer.getToContext())
6639 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
6640}
6641
6642Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
6643 QualType ToType = Importer.Import(E->getType());
6644 if (ToType.isNull())
6645 return nullptr;
6646 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
6647}
6648
Sean Callanandd2c1742016-05-16 20:48:03 +00006649Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006650 auto *ToField = dyn_cast_or_null<FieldDecl>(Importer.Import(DIE->getField()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006651 if (!ToField && DIE->getField())
6652 return nullptr;
6653
6654 return CXXDefaultInitExpr::Create(
6655 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
6656}
6657
6658Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
6659 QualType ToType = Importer.Import(E->getType());
6660 if (ToType.isNull() && !E->getType().isNull())
6661 return nullptr;
6662 ExprValueKind VK = E->getValueKind();
6663 CastKind CK = E->getCastKind();
6664 Expr *ToOp = Importer.Import(E->getSubExpr());
6665 if (!ToOp && E->getSubExpr())
6666 return nullptr;
6667 CXXCastPath BasePath;
6668 if (ImportCastPath(E, BasePath))
6669 return nullptr;
6670 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6671 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6672 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6673 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
6674
6675 if (isa<CXXStaticCastExpr>(E)) {
6676 return CXXStaticCastExpr::Create(
6677 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6678 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6679 } else if (isa<CXXDynamicCastExpr>(E)) {
6680 return CXXDynamicCastExpr::Create(
6681 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6682 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6683 } else if (isa<CXXReinterpretCastExpr>(E)) {
6684 return CXXReinterpretCastExpr::Create(
6685 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6686 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6687 } else {
6688 return nullptr;
6689 }
6690}
6691
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006692Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
6693 SubstNonTypeTemplateParmExpr *E) {
6694 QualType T = Importer.Import(E->getType());
6695 if (T.isNull())
6696 return nullptr;
6697
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006698 auto *Param = cast_or_null<NonTypeTemplateParmDecl>(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006699 Importer.Import(E->getParameter()));
6700 if (!Param)
6701 return nullptr;
6702
6703 Expr *Replacement = Importer.Import(E->getReplacement());
6704 if (!Replacement)
6705 return nullptr;
6706
6707 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
6708 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
6709 Replacement);
6710}
6711
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00006712Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
6713 QualType ToType = Importer.Import(E->getType());
6714 if (ToType.isNull())
6715 return nullptr;
6716
6717 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
6718 if (ImportContainerChecked(E->getArgs(), ToArgs))
6719 return nullptr;
6720
6721 // According to Sema::BuildTypeTrait(), if E is value-dependent,
6722 // Value is always false.
6723 bool ToValue = false;
6724 if (!E->isValueDependent())
6725 ToValue = E->getValue();
6726
6727 return TypeTraitExpr::Create(
6728 Importer.getToContext(), ToType, Importer.Import(E->getLocStart()),
6729 E->getTrait(), ToArgs, Importer.Import(E->getLocEnd()), ToValue);
6730}
6731
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006732Expr *ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
6733 QualType ToType = Importer.Import(E->getType());
6734 if (ToType.isNull())
6735 return nullptr;
6736
6737 if (E->isTypeOperand()) {
6738 TypeSourceInfo *TSI = Importer.Import(E->getTypeOperandSourceInfo());
6739 if (!TSI)
6740 return nullptr;
6741
6742 return new (Importer.getToContext())
6743 CXXTypeidExpr(ToType, TSI, Importer.Import(E->getSourceRange()));
6744 }
6745
6746 Expr *Op = Importer.Import(E->getExprOperand());
6747 if (!Op)
6748 return nullptr;
6749
6750 return new (Importer.getToContext())
6751 CXXTypeidExpr(ToType, Op, Importer.Import(E->getSourceRange()));
6752}
6753
Lang Hames19e07e12017-06-20 21:06:00 +00006754void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
6755 CXXMethodDecl *FromMethod) {
6756 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
6757 ToMethod->addOverriddenMethod(
6758 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
6759 FromOverriddenMethod))));
6760}
6761
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006762ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006763 ASTContext &FromContext, FileManager &FromFileManager,
6764 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006765 : ToContext(ToContext), FromContext(FromContext),
6766 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
6767 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00006768 ImportedDecls[FromContext.getTranslationUnitDecl()]
6769 = ToContext.getTranslationUnitDecl();
6770}
6771
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006772ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006773
6774QualType ASTImporter::Import(QualType FromT) {
6775 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006776 return {};
John McCall424cec92011-01-19 06:33:43 +00006777
6778 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00006779
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006780 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006781 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6782 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006783 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006784 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006785
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006786 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006787 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006788 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006789 if (ToT.isNull())
6790 return ToT;
6791
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006792 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006793 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006794
John McCall424cec92011-01-19 06:33:43 +00006795 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006796}
6797
Douglas Gregor62d311f2010-02-09 19:21:46 +00006798TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006799 if (!FromTSI)
6800 return FromTSI;
6801
6802 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006803 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006804 QualType T = Import(FromTSI->getType());
6805 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006806 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006807
6808 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006809 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006810}
6811
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00006812Attr *ASTImporter::Import(const Attr *FromAttr) {
6813 Attr *ToAttr = FromAttr->clone(ToContext);
6814 ToAttr->setRange(Import(FromAttr->getRange()));
6815 return ToAttr;
6816}
6817
Sean Callanan59721b32015-04-28 18:41:46 +00006818Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6819 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6820 if (Pos != ImportedDecls.end()) {
6821 Decl *ToD = Pos->second;
6822 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6823 return ToD;
6824 } else {
6825 return nullptr;
6826 }
6827}
6828
Douglas Gregor62d311f2010-02-09 19:21:46 +00006829Decl *ASTImporter::Import(Decl *FromD) {
6830 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006831 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006832
Douglas Gregord451ea92011-07-29 23:31:30 +00006833 ASTNodeImporter Importer(*this);
6834
Douglas Gregor62d311f2010-02-09 19:21:46 +00006835 // Check whether we've already imported this declaration.
6836 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006837 if (Pos != ImportedDecls.end()) {
6838 Decl *ToD = Pos->second;
6839 Importer.ImportDefinitionIfNeeded(FromD, ToD);
6840 return ToD;
6841 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00006842
6843 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00006844 Decl *ToD = Importer.Visit(FromD);
6845 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006846 return nullptr;
6847
Douglas Gregor62d311f2010-02-09 19:21:46 +00006848 // Record the imported declaration.
6849 ImportedDecls[FromD] = ToD;
Peter Szecsib180eeb2018-04-25 17:28:03 +00006850 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006851 return ToD;
6852}
6853
6854DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6855 if (!FromDC)
6856 return FromDC;
6857
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006858 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006859 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006860 return nullptr;
6861
Douglas Gregor2e15c842012-02-01 21:00:38 +00006862 // When we're using a record/enum/Objective-C class/protocol as a context, we
6863 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006864 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
6865 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006866 if (ToRecord->isCompleteDefinition()) {
6867 // Do nothing.
6868 } else if (FromRecord->isCompleteDefinition()) {
6869 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6870 ASTNodeImporter::IDK_Basic);
6871 } else {
6872 CompleteDecl(ToRecord);
6873 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006874 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6875 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006876 if (ToEnum->isCompleteDefinition()) {
6877 // Do nothing.
6878 } else if (FromEnum->isCompleteDefinition()) {
6879 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6880 ASTNodeImporter::IDK_Basic);
6881 } else {
6882 CompleteDecl(ToEnum);
6883 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006884 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6885 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006886 if (ToClass->getDefinition()) {
6887 // Do nothing.
6888 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6889 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6890 ASTNodeImporter::IDK_Basic);
6891 } else {
6892 CompleteDecl(ToClass);
6893 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006894 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6895 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006896 if (ToProto->getDefinition()) {
6897 // Do nothing.
6898 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6899 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6900 ASTNodeImporter::IDK_Basic);
6901 } else {
6902 CompleteDecl(ToProto);
6903 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006904 }
6905
6906 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006907}
6908
6909Expr *ASTImporter::Import(Expr *FromE) {
6910 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00006911 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006912
6913 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
6914}
6915
6916Stmt *ASTImporter::Import(Stmt *FromS) {
6917 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00006918 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006919
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006920 // Check whether we've already imported this declaration.
6921 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
6922 if (Pos != ImportedStmts.end())
6923 return Pos->second;
6924
6925 // Import the type
6926 ASTNodeImporter Importer(*this);
6927 Stmt *ToS = Importer.Visit(FromS);
6928 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00006929 return nullptr;
6930
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006931 // Record the imported declaration.
6932 ImportedStmts[FromS] = ToS;
6933 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006934}
6935
6936NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
6937 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00006938 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006939
Douglas Gregor90ebf252011-04-27 16:48:40 +00006940 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
6941
6942 switch (FromNNS->getKind()) {
6943 case NestedNameSpecifier::Identifier:
6944 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
6945 return NestedNameSpecifier::Create(ToContext, prefix, II);
6946 }
Craig Topper36250ad2014-05-12 05:36:57 +00006947 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006948
6949 case NestedNameSpecifier::Namespace:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006950 if (auto *NS =
6951 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006952 return NestedNameSpecifier::Create(ToContext, prefix, NS);
6953 }
Craig Topper36250ad2014-05-12 05:36:57 +00006954 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006955
6956 case NestedNameSpecifier::NamespaceAlias:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006957 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006958 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006959 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
6960 }
Craig Topper36250ad2014-05-12 05:36:57 +00006961 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006962
6963 case NestedNameSpecifier::Global:
6964 return NestedNameSpecifier::GlobalSpecifier(ToContext);
6965
Nikola Smiljanic67860242014-09-26 00:28:20 +00006966 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006967 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006968 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00006969 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
6970 }
6971 return nullptr;
6972
Douglas Gregor90ebf252011-04-27 16:48:40 +00006973 case NestedNameSpecifier::TypeSpec:
6974 case NestedNameSpecifier::TypeSpecWithTemplate: {
6975 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
6976 if (!T.isNull()) {
6977 bool bTemplate = FromNNS->getKind() ==
6978 NestedNameSpecifier::TypeSpecWithTemplate;
6979 return NestedNameSpecifier::Create(ToContext, prefix,
6980 bTemplate, T.getTypePtr());
6981 }
6982 }
Craig Topper36250ad2014-05-12 05:36:57 +00006983 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006984 }
6985
6986 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00006987}
6988
Douglas Gregor14454802011-02-25 02:25:35 +00006989NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006990 // Copied from NestedNameSpecifier mostly.
6991 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
6992 NestedNameSpecifierLoc NNS = FromNNS;
6993
6994 // Push each of the nested-name-specifiers's onto a stack for
6995 // serialization in reverse order.
6996 while (NNS) {
6997 NestedNames.push_back(NNS);
6998 NNS = NNS.getPrefix();
6999 }
7000
7001 NestedNameSpecifierLocBuilder Builder;
7002
7003 while (!NestedNames.empty()) {
7004 NNS = NestedNames.pop_back_val();
7005 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
7006 if (!Spec)
7007 return NestedNameSpecifierLoc();
7008
7009 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
7010 switch (Kind) {
7011 case NestedNameSpecifier::Identifier:
7012 Builder.Extend(getToContext(),
7013 Spec->getAsIdentifier(),
7014 Import(NNS.getLocalBeginLoc()),
7015 Import(NNS.getLocalEndLoc()));
7016 break;
7017
7018 case NestedNameSpecifier::Namespace:
7019 Builder.Extend(getToContext(),
7020 Spec->getAsNamespace(),
7021 Import(NNS.getLocalBeginLoc()),
7022 Import(NNS.getLocalEndLoc()));
7023 break;
7024
7025 case NestedNameSpecifier::NamespaceAlias:
7026 Builder.Extend(getToContext(),
7027 Spec->getAsNamespaceAlias(),
7028 Import(NNS.getLocalBeginLoc()),
7029 Import(NNS.getLocalEndLoc()));
7030 break;
7031
7032 case NestedNameSpecifier::TypeSpec:
7033 case NestedNameSpecifier::TypeSpecWithTemplate: {
7034 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
7035 QualType(Spec->getAsType(), 0));
7036 Builder.Extend(getToContext(),
7037 Import(NNS.getLocalBeginLoc()),
7038 TSI->getTypeLoc(),
7039 Import(NNS.getLocalEndLoc()));
7040 break;
7041 }
7042
7043 case NestedNameSpecifier::Global:
7044 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
7045 break;
7046
7047 case NestedNameSpecifier::Super: {
7048 SourceRange ToRange = Import(NNS.getSourceRange());
7049 Builder.MakeSuper(getToContext(),
7050 Spec->getAsRecordDecl(),
7051 ToRange.getBegin(),
7052 ToRange.getEnd());
7053 }
7054 }
7055 }
7056
7057 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00007058}
7059
Douglas Gregore2e50d332010-12-01 01:36:18 +00007060TemplateName ASTImporter::Import(TemplateName From) {
7061 switch (From.getKind()) {
7062 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007063 if (auto *ToTemplate =
7064 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007065 return TemplateName(ToTemplate);
7066
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007067 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007068
7069 case TemplateName::OverloadedTemplate: {
7070 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
7071 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007072 for (auto *I : *FromStorage) {
7073 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007074 ToTemplates.addDecl(To);
7075 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007076 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007077 }
7078 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
7079 ToTemplates.end());
7080 }
7081
7082 case TemplateName::QualifiedTemplate: {
7083 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
7084 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
7085 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007086 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007087
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007088 if (auto *ToTemplate =
7089 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007090 return ToContext.getQualifiedTemplateName(Qualifier,
7091 QTN->hasTemplateKeyword(),
7092 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007093
7094 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007095 }
7096
7097 case TemplateName::DependentTemplate: {
7098 DependentTemplateName *DTN = From.getAsDependentTemplateName();
7099 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
7100 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007101 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007102
7103 if (DTN->isIdentifier()) {
7104 return ToContext.getDependentTemplateName(Qualifier,
7105 Import(DTN->getIdentifier()));
7106 }
7107
7108 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
7109 }
John McCalld9dfe3a2011-06-30 08:33:18 +00007110
7111 case TemplateName::SubstTemplateTemplateParm: {
7112 SubstTemplateTemplateParmStorage *subst
7113 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007114 auto *param =
7115 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00007116 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007117 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00007118
7119 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007120 if (replacement.isNull())
7121 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00007122
7123 return ToContext.getSubstTemplateTemplateParm(param, replacement);
7124 }
Douglas Gregor5590be02011-01-15 06:45:20 +00007125
7126 case TemplateName::SubstTemplateTemplateParmPack: {
7127 SubstTemplateTemplateParmPackStorage *SubstPack
7128 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007129 auto *Param =
7130 cast_or_null<TemplateTemplateParmDecl>(
7131 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00007132 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007133 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00007134
7135 ASTNodeImporter Importer(*this);
7136 TemplateArgument ArgPack
7137 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
7138 if (ArgPack.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007139 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00007140
7141 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
7142 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00007143 }
7144
7145 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00007146}
7147
Douglas Gregor62d311f2010-02-09 19:21:46 +00007148SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
7149 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007150 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00007151
Douglas Gregor811663e2010-02-10 00:15:17 +00007152 SourceManager &FromSM = FromContext.getSourceManager();
7153
Sean Callanan24c5fe62016-11-07 20:42:25 +00007154 // For now, map everything down to its file location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00007155 // don't have to import macro expansions.
7156 // FIXME: Import macro expansions!
Sean Callanan24c5fe62016-11-07 20:42:25 +00007157 FromLoc = FromSM.getFileLoc(FromLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00007158 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
7159 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00007160 FileID ToFileID = Import(Decomposed.first);
7161 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007162 return {};
Sean Callanan59721b32015-04-28 18:41:46 +00007163 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
7164 .getLocWithOffset(Decomposed.second);
7165 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007166}
7167
7168SourceRange ASTImporter::Import(SourceRange FromRange) {
7169 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
7170}
7171
Douglas Gregor811663e2010-02-10 00:15:17 +00007172FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00007173 llvm::DenseMap<FileID, FileID>::iterator Pos
7174 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00007175 if (Pos != ImportedFileIDs.end())
7176 return Pos->second;
7177
7178 SourceManager &FromSM = FromContext.getSourceManager();
7179 SourceManager &ToSM = ToContext.getSourceManager();
7180 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00007181 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00007182
7183 // Include location of this file.
7184 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
7185
7186 // Map the FileID for to the "to" source manager.
7187 FileID ToID;
7188 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00007189 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00007190 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
7191 // disk again
7192 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
7193 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00007194 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00007195 if (!Entry)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007196 return {};
Douglas Gregor811663e2010-02-10 00:15:17 +00007197 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
7198 FromSLoc.getFile().getFileCharacteristic());
7199 } else {
7200 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007201 const llvm::MemoryBuffer *
7202 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00007203 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00007204 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00007205 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00007206 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00007207 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00007208 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007209
Sebastian Redl99219f12010-09-30 01:03:06 +00007210 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00007211 return ToID;
7212}
7213
Sean Callanandd2c1742016-05-16 20:48:03 +00007214CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
7215 Expr *ToExpr = Import(From->getInit());
7216 if (!ToExpr && From->getInit())
7217 return nullptr;
7218
7219 if (From->isBaseInitializer()) {
7220 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7221 if (!ToTInfo && From->getTypeSourceInfo())
7222 return nullptr;
7223
7224 return new (ToContext) CXXCtorInitializer(
7225 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
7226 ToExpr, Import(From->getRParenLoc()),
7227 From->isPackExpansion() ? Import(From->getEllipsisLoc())
7228 : SourceLocation());
7229 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007230 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007231 if (!ToField && From->getMember())
7232 return nullptr;
7233
7234 return new (ToContext) CXXCtorInitializer(
7235 ToContext, ToField, Import(From->getMemberLocation()),
7236 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7237 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007238 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00007239 Import(From->getIndirectMember()));
7240 if (!ToIField && From->getIndirectMember())
7241 return nullptr;
7242
7243 return new (ToContext) CXXCtorInitializer(
7244 ToContext, ToIField, Import(From->getMemberLocation()),
7245 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7246 } else if (From->isDelegatingInitializer()) {
7247 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7248 if (!ToTInfo && From->getTypeSourceInfo())
7249 return nullptr;
7250
7251 return new (ToContext)
7252 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
7253 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007254 } else {
7255 return nullptr;
7256 }
7257}
7258
Aleksei Sidorina693b372016-09-28 10:16:56 +00007259CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
7260 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
7261 if (Pos != ImportedCXXBaseSpecifiers.end())
7262 return Pos->second;
7263
7264 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
7265 Import(BaseSpec->getSourceRange()),
7266 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
7267 BaseSpec->getAccessSpecifierAsWritten(),
7268 Import(BaseSpec->getTypeSourceInfo()),
7269 Import(BaseSpec->getEllipsisLoc()));
7270 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
7271 return Imported;
7272}
7273
Douglas Gregor0a791672011-01-18 03:11:38 +00007274void ASTImporter::ImportDefinition(Decl *From) {
7275 Decl *To = Import(From);
7276 if (!To)
7277 return;
7278
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007279 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00007280 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007281
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007282 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00007283 if (!ToRecord->getDefinition()) {
7284 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00007285 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007286 return;
7287 }
7288 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007289
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007290 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00007291 if (!ToEnum->getDefinition()) {
7292 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007293 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00007294 return;
7295 }
7296 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00007297
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007298 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007299 if (!ToIFace->getDefinition()) {
7300 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007301 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007302 return;
7303 }
7304 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007305
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007306 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007307 if (!ToProto->getDefinition()) {
7308 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007309 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007310 return;
7311 }
7312 }
7313
Douglas Gregor0a791672011-01-18 03:11:38 +00007314 Importer.ImportDeclContext(FromDC, true);
7315 }
7316}
7317
Douglas Gregor96e578d2010-02-05 17:54:41 +00007318DeclarationName ASTImporter::Import(DeclarationName FromName) {
7319 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007320 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007321
7322 switch (FromName.getNameKind()) {
7323 case DeclarationName::Identifier:
7324 return Import(FromName.getAsIdentifierInfo());
7325
7326 case DeclarationName::ObjCZeroArgSelector:
7327 case DeclarationName::ObjCOneArgSelector:
7328 case DeclarationName::ObjCMultiArgSelector:
7329 return Import(FromName.getObjCSelector());
7330
7331 case DeclarationName::CXXConstructorName: {
7332 QualType T = Import(FromName.getCXXNameType());
7333 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007334 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007335
7336 return ToContext.DeclarationNames.getCXXConstructorName(
7337 ToContext.getCanonicalType(T));
7338 }
7339
7340 case DeclarationName::CXXDestructorName: {
7341 QualType T = Import(FromName.getCXXNameType());
7342 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007343 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007344
7345 return ToContext.DeclarationNames.getCXXDestructorName(
7346 ToContext.getCanonicalType(T));
7347 }
7348
Richard Smith35845152017-02-07 01:37:30 +00007349 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007350 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00007351 Import(FromName.getCXXDeductionGuideTemplate()));
7352 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007353 return {};
Richard Smith35845152017-02-07 01:37:30 +00007354 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
7355 }
7356
Douglas Gregor96e578d2010-02-05 17:54:41 +00007357 case DeclarationName::CXXConversionFunctionName: {
7358 QualType T = Import(FromName.getCXXNameType());
7359 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007360 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007361
7362 return ToContext.DeclarationNames.getCXXConversionFunctionName(
7363 ToContext.getCanonicalType(T));
7364 }
7365
7366 case DeclarationName::CXXOperatorName:
7367 return ToContext.DeclarationNames.getCXXOperatorName(
7368 FromName.getCXXOverloadedOperator());
7369
7370 case DeclarationName::CXXLiteralOperatorName:
7371 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
7372 Import(FromName.getCXXLiteralIdentifier()));
7373
7374 case DeclarationName::CXXUsingDirective:
7375 // FIXME: STATICS!
7376 return DeclarationName::getUsingDirectiveName();
7377 }
7378
David Blaikiee4d798f2012-01-20 21:50:17 +00007379 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00007380}
7381
Douglas Gregore2e50d332010-12-01 01:36:18 +00007382IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007383 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00007384 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007385
Sean Callananf94ef1d2016-05-14 06:11:19 +00007386 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
7387
7388 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
7389 ToId->setBuiltinID(FromId->getBuiltinID());
7390
7391 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007392}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007393
Douglas Gregor43f54792010-02-17 02:12:47 +00007394Selector ASTImporter::Import(Selector FromSel) {
7395 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007396 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00007397
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007398 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00007399 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
7400 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
7401 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
7402 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
7403}
7404
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007405DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
7406 DeclContext *DC,
7407 unsigned IDNS,
7408 NamedDecl **Decls,
7409 unsigned NumDecls) {
7410 return Name;
7411}
7412
7413DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007414 if (LastDiagFromFrom)
7415 ToContext.getDiagnostics().notePriorDiagnosticFrom(
7416 FromContext.getDiagnostics());
7417 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007418 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007419}
7420
7421DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007422 if (!LastDiagFromFrom)
7423 FromContext.getDiagnostics().notePriorDiagnosticFrom(
7424 ToContext.getDiagnostics());
7425 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007426 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007427}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007428
Douglas Gregor2e15c842012-02-01 21:00:38 +00007429void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007430 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007431 if (!ID->getDefinition())
7432 ID->startDefinition();
7433 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007434 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007435 if (!PD->getDefinition())
7436 PD->startDefinition();
7437 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007438 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007439 if (!TD->getDefinition() && !TD->isBeingDefined()) {
7440 TD->startDefinition();
7441 TD->setCompleteDefinition(true);
7442 }
7443 }
7444 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007445 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00007446 }
7447}
7448
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007449Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00007450 if (From->hasAttrs()) {
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007451 for (const auto *FromAttr : From->getAttrs())
7452 To->addAttr(Import(FromAttr));
Sean Callanan8bca9962016-03-28 21:43:01 +00007453 }
7454 if (From->isUsed()) {
7455 To->setIsUsed();
7456 }
Sean Callanandd2c1742016-05-16 20:48:03 +00007457 if (From->isImplicit()) {
7458 To->setImplicit();
7459 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007460 ImportedDecls[From] = To;
7461 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00007462}
Douglas Gregorb4964f72010-02-15 23:54:17 +00007463
Douglas Gregordd6006f2012-07-17 21:16:27 +00007464bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
7465 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00007466 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00007467 = ImportedTypes.find(From.getTypePtr());
7468 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
7469 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00007470
Douglas Gregordd6006f2012-07-17 21:16:27 +00007471 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
7472 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00007473 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00007474}