blob: 07b6723300928d64aae78fa66fbcfcf0e1120a86 [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
Gabor Marton26f72a92018-07-12 09:42:05 +000093 void updateFlags(const Decl *From, Decl *To) {
94 // Check if some flags or attrs are new in 'From' and copy into 'To'.
95 // FIXME: Other flags or attrs?
96 if (From->isUsed(false) && !To->isUsed(false))
97 To->setIsUsed();
98 }
99
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000100 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000101 public DeclVisitor<ASTNodeImporter, Decl *>,
102 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000103 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000104
Gabor Marton26f72a92018-07-12 09:42:05 +0000105 // Wrapper for an overload set.
106 template <typename ToDeclT> struct CallOverloadedCreateFun {
107 template <typename... Args>
108 auto operator()(Args &&... args)
109 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) {
110 return ToDeclT::Create(std::forward<Args>(args)...);
111 }
112 };
113
114 // Always use these functions to create a Decl during import. There are
115 // certain tasks which must be done after the Decl was created, e.g. we
116 // must immediately register that as an imported Decl. The parameter `ToD`
117 // will be set to the newly created Decl or if had been imported before
118 // then to the already imported Decl. Returns a bool value set to true if
119 // the `FromD` had been imported before.
120 template <typename ToDeclT, typename FromDeclT, typename... Args>
121 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
122 Args &&... args) {
123 // There may be several overloads of ToDeclT::Create. We must make sure
124 // to call the one which would be chosen by the arguments, thus we use a
125 // wrapper for the overload set.
126 CallOverloadedCreateFun<ToDeclT> OC;
127 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
128 std::forward<Args>(args)...);
129 }
130 // Use this overload if a special Type is needed to be created. E.g if we
131 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl`
132 // then:
133 // TypedefNameDecl *ToTypedef;
134 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...);
135 template <typename NewDeclT, typename ToDeclT, typename FromDeclT,
136 typename... Args>
137 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
138 Args &&... args) {
139 CallOverloadedCreateFun<NewDeclT> OC;
140 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
141 std::forward<Args>(args)...);
142 }
143 // Use this version if a special create function must be
144 // used, e.g. CXXRecordDecl::CreateLambda .
145 template <typename ToDeclT, typename CreateFunT, typename FromDeclT,
146 typename... Args>
147 LLVM_NODISCARD bool
148 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
149 FromDeclT *FromD, Args &&... args) {
150 ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD));
151 if (ToD)
152 return true; // Already imported.
153 ToD = CreateFun(std::forward<Args>(args)...);
154 InitializeImportedDecl(FromD, ToD);
155 return false; // A new Decl is created.
156 }
157
158 void InitializeImportedDecl(Decl *FromD, Decl *ToD) {
159 Importer.MapImported(FromD, ToD);
160 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
161 if (FromD->hasAttrs())
162 for (const Attr *FromAttr : FromD->getAttrs())
163 ToD->addAttr(Importer.Import(FromAttr));
164 if (FromD->isUsed())
165 ToD->setIsUsed();
166 if (FromD->isImplicit())
167 ToD->setImplicit();
168 }
169
Douglas Gregor96e578d2010-02-05 17:54:41 +0000170 public:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000171 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
Gabor Marton344b0992018-05-16 11:48:11 +0000172
Douglas Gregor96e578d2010-02-05 17:54:41 +0000173 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +0000174 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000175 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000176
177 // Importing types
John McCall424cec92011-01-19 06:33:43 +0000178 QualType VisitType(const Type *T);
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000179 QualType VisitAtomicType(const AtomicType *T);
John McCall424cec92011-01-19 06:33:43 +0000180 QualType VisitBuiltinType(const BuiltinType *T);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000181 QualType VisitDecayedType(const DecayedType *T);
John McCall424cec92011-01-19 06:33:43 +0000182 QualType VisitComplexType(const ComplexType *T);
183 QualType VisitPointerType(const PointerType *T);
184 QualType VisitBlockPointerType(const BlockPointerType *T);
185 QualType VisitLValueReferenceType(const LValueReferenceType *T);
186 QualType VisitRValueReferenceType(const RValueReferenceType *T);
187 QualType VisitMemberPointerType(const MemberPointerType *T);
188 QualType VisitConstantArrayType(const ConstantArrayType *T);
189 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
190 QualType VisitVariableArrayType(const VariableArrayType *T);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000191 QualType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000192 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +0000193 QualType VisitVectorType(const VectorType *T);
194 QualType VisitExtVectorType(const ExtVectorType *T);
195 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
196 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000197 QualType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
Sean Callananda6df8a2011-08-11 16:56:07 +0000198 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +0000199 QualType VisitTypedefType(const TypedefType *T);
200 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000201 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +0000202 QualType VisitTypeOfType(const TypeOfType *T);
203 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +0000204 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +0000205 QualType VisitAutoType(const AutoType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000206 QualType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000207 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +0000208 QualType VisitRecordType(const RecordType *T);
209 QualType VisitEnumType(const EnumType *T);
Sean Callanan72fe0852015-04-02 23:50:08 +0000210 QualType VisitAttributedType(const AttributedType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000211 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000212 QualType VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T);
John McCall424cec92011-01-19 06:33:43 +0000213 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
214 QualType VisitElaboratedType(const ElaboratedType *T);
Peter Szecsice7f3182018-05-07 12:08:27 +0000215 QualType VisitDependentNameType(const DependentNameType *T);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000216 QualType VisitPackExpansionType(const PackExpansionType *T);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000217 QualType VisitDependentTemplateSpecializationType(
218 const DependentTemplateSpecializationType *T);
John McCall424cec92011-01-19 06:33:43 +0000219 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
220 QualType VisitObjCObjectType(const ObjCObjectType *T);
221 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Rafael Stahldf556202018-05-29 08:12:15 +0000222
223 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000224 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
225 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +0000226 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +0000227 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000228 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
229 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +0000230 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000231
Aleksei Sidorina693b372016-09-28 10:16:56 +0000232 bool ImportCastPath(CastExpr *E, CXXCastPath &Path);
233
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000234 using Designator = DesignatedInitExpr::Designator;
235
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000236 Designator ImportDesignator(const Designator &D);
237
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000238 Optional<LambdaCapture> ImportLambdaCapture(const LambdaCapture &From);
Douglas Gregor2e15c842012-02-01 21:00:38 +0000239
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000240 /// What we should import from the definition.
Douglas Gregor95d82832012-01-24 18:36:04 +0000241 enum ImportDefinitionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000242 /// Import the default subset of the definition, which might be
Douglas Gregor95d82832012-01-24 18:36:04 +0000243 /// nothing (if minimal import is set) or might be everything (if minimal
244 /// import is not set).
245 IDK_Default,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000246
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000247 /// Import everything.
Douglas Gregor95d82832012-01-24 18:36:04 +0000248 IDK_Everything,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000249
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000250 /// Import only the bare bones needed to establish a valid
Douglas Gregor95d82832012-01-24 18:36:04 +0000251 /// DeclContext.
252 IDK_Basic
253 };
254
Douglas Gregor2e15c842012-02-01 21:00:38 +0000255 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
256 return IDK == IDK_Everything ||
257 (IDK == IDK_Default && !Importer.isMinimalImport());
258 }
259
Douglas Gregord451ea92011-07-29 23:31:30 +0000260 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000261 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000262 bool ImportDefinition(VarDecl *From, VarDecl *To,
263 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000264 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000265 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000266 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000267 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000268 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000269 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000270 TemplateParameterList *ImportTemplateParameterList(
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000271 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000272 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000273 Optional<TemplateArgumentLoc> ImportTemplateArgumentLoc(
274 const TemplateArgumentLoc &TALoc);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000275 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
276 unsigned NumFromArgs,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000277 SmallVectorImpl<TemplateArgument> &ToArgs);
278
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000279 template <typename InContainerTy>
280 bool ImportTemplateArgumentListInfo(const InContainerTy &Container,
281 TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000282
283 template<typename InContainerTy>
284 bool ImportTemplateArgumentListInfo(SourceLocation FromLAngleLoc,
285 SourceLocation FromRAngleLoc,
286 const InContainerTy &Container,
287 TemplateArgumentListInfo &Result);
288
Gabor Marton5254e642018-06-27 13:32:50 +0000289 using TemplateArgsTy = SmallVector<TemplateArgument, 8>;
290 using OptionalTemplateArgsTy = Optional<TemplateArgsTy>;
291 std::tuple<FunctionTemplateDecl *, OptionalTemplateArgsTy>
292 ImportFunctionTemplateWithTemplateArgsFromSpecialization(
293 FunctionDecl *FromFD);
294
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000295 bool ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
296
Douglas Gregordd6006f2012-07-17 21:16:27 +0000297 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
298 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000299 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
300 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000301 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000302 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000303 bool IsStructuralMatch(FunctionTemplateDecl *From,
304 FunctionTemplateDecl *To);
Balazs Keric7797c42018-07-11 09:37:24 +0000305 bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000306 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000307 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000308 Decl *VisitDecl(Decl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000309 Decl *VisitEmptyDecl(EmptyDecl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000310 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000311 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000312 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000313 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000314 Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000315 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000316 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000317 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000318 Decl *VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000319 Decl *VisitLabelDecl(LabelDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000320 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000321 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000322 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000323 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000324 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
325 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
326 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
327 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000328 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000329 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000330 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000331 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000332 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000333 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000334 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000335 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000336 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000337 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000338 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000339 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000340 Decl *VisitUsingDecl(UsingDecl *D);
341 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
342 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
343 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
344 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
345
Douglas Gregor85f3f952015-07-07 03:57:15 +0000346 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000347 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000348 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000349 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000350 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000351 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000352 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
353 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
354 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
355 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000356 Decl *VisitClassTemplateSpecializationDecl(
357 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000358 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
359 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000360 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000361
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000362 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000363 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
364
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000365 Stmt *VisitStmt(Stmt *S);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000366 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000367 Stmt *VisitDeclStmt(DeclStmt *S);
368 Stmt *VisitNullStmt(NullStmt *S);
369 Stmt *VisitCompoundStmt(CompoundStmt *S);
370 Stmt *VisitCaseStmt(CaseStmt *S);
371 Stmt *VisitDefaultStmt(DefaultStmt *S);
372 Stmt *VisitLabelStmt(LabelStmt *S);
373 Stmt *VisitAttributedStmt(AttributedStmt *S);
374 Stmt *VisitIfStmt(IfStmt *S);
375 Stmt *VisitSwitchStmt(SwitchStmt *S);
376 Stmt *VisitWhileStmt(WhileStmt *S);
377 Stmt *VisitDoStmt(DoStmt *S);
378 Stmt *VisitForStmt(ForStmt *S);
379 Stmt *VisitGotoStmt(GotoStmt *S);
380 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
381 Stmt *VisitContinueStmt(ContinueStmt *S);
382 Stmt *VisitBreakStmt(BreakStmt *S);
383 Stmt *VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000384 // FIXME: MSAsmStmt
385 // FIXME: SEHExceptStmt
386 // FIXME: SEHFinallyStmt
387 // FIXME: SEHTryStmt
388 // FIXME: SEHLeaveStmt
389 // FIXME: CapturedStmt
390 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
391 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
392 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
393 // FIXME: MSDependentExistsStmt
394 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
395 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
396 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
397 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
398 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
399 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
400 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000401
402 // Importing expressions
403 Expr *VisitExpr(Expr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000404 Expr *VisitVAArgExpr(VAArgExpr *E);
405 Expr *VisitGNUNullExpr(GNUNullExpr *E);
406 Expr *VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000407 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000408 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
409 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
410 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000411 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000412 Expr *VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000413 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000414 Expr *VisitStringLiteral(StringLiteral *E);
415 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
416 Expr *VisitAtomicExpr(AtomicExpr *E);
417 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000418 Expr *VisitParenExpr(ParenExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000419 Expr *VisitParenListExpr(ParenListExpr *E);
420 Expr *VisitStmtExpr(StmtExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000421 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000422 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000423 Expr *VisitBinaryOperator(BinaryOperator *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000424 Expr *VisitConditionalOperator(ConditionalOperator *E);
425 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
426 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000427 Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
428 Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E);
429 Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000430 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000431 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000432 Expr *VisitExplicitCastExpr(ExplicitCastExpr *E);
433 Expr *VisitOffsetOfExpr(OffsetOfExpr *OE);
434 Expr *VisitCXXThrowExpr(CXXThrowExpr *E);
435 Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
436 Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
437 Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
438 Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
439 Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE);
440 Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000441 Expr *VisitPackExpansionExpr(PackExpansionExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000442 Expr *VisitSizeOfPackExpr(SizeOfPackExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000443 Expr *VisitCXXNewExpr(CXXNewExpr *CE);
444 Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000445 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000446 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000447 Expr *VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
Peter Szecsice7f3182018-05-07 12:08:27 +0000448 Expr *VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +0000449 Expr *VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *CE);
450 Expr *VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
Peter Szecsice7f3182018-05-07 12:08:27 +0000451 Expr *VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000452 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC);
Sean Callanan8bca9962016-03-28 21:43:01 +0000453 Expr *VisitCXXThisExpr(CXXThisExpr *E);
454 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +0000455 Expr *VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000456 Expr *VisitMemberExpr(MemberExpr *E);
457 Expr *VisitCallExpr(CallExpr *E);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000458 Expr *VisitLambdaExpr(LambdaExpr *LE);
Sean Callanan8bca9962016-03-28 21:43:01 +0000459 Expr *VisitInitListExpr(InitListExpr *E);
Gabor Marton07b01ff2018-06-29 12:17:34 +0000460 Expr *VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
Richard Smith30e304e2016-12-14 00:03:17 +0000461 Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
462 Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
Sean Callanandd2c1742016-05-16 20:48:03 +0000463 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
464 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000465 Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +0000466 Expr *VisitTypeTraitExpr(TypeTraitExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000467 Expr *VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000468
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000469 template<typename IIter, typename OIter>
470 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000471 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
472
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000473 ASTImporter &ImporterRef = Importer;
474 std::transform(Ibegin, Iend, Obegin,
475 [&ImporterRef](ItemT From) -> ItemT {
476 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000477 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000478 }
479
480 template<typename IIter, typename OIter>
481 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000482 using ItemT = typename std::remove_reference<decltype(**Obegin)>::type;
483
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000484 ASTImporter &ImporterRef = Importer;
485 bool Failed = false;
486 std::transform(Ibegin, Iend, Obegin,
487 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000488 auto *To = cast_or_null<ItemT>(ImporterRef.Import(From));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000489 if (!To && From)
490 Failed = true;
491 return To;
492 });
493 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000494 }
Aleksei Sidorina693b372016-09-28 10:16:56 +0000495
496 template<typename InContainerTy, typename OutContainerTy>
497 bool ImportContainerChecked(const InContainerTy &InContainer,
498 OutContainerTy &OutContainer) {
499 return ImportArrayChecked(InContainer.begin(), InContainer.end(),
500 OutContainer.begin());
501 }
502
503 template<typename InContainerTy, typename OIter>
504 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
505 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
506 }
Lang Hames19e07e12017-06-20 21:06:00 +0000507
508 // Importing overrides.
509 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000510
511 FunctionDecl *FindFunctionTemplateSpecialization(FunctionDecl *FromFD);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000512 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000513
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000514template <typename InContainerTy>
515bool ASTNodeImporter::ImportTemplateArgumentListInfo(
516 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
517 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
518 TemplateArgumentListInfo ToTAInfo(Importer.Import(FromLAngleLoc),
519 Importer.Import(FromRAngleLoc));
520 if (ImportTemplateArgumentListInfo(Container, ToTAInfo))
521 return true;
522 Result = ToTAInfo;
523 return false;
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000524}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000525
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000526template <>
527bool ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
528 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
529 return ImportTemplateArgumentListInfo(
530 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
531}
532
533template <>
534bool ASTNodeImporter::ImportTemplateArgumentListInfo<
535 ASTTemplateArgumentListInfo>(const ASTTemplateArgumentListInfo &From,
536 TemplateArgumentListInfo &Result) {
537 return ImportTemplateArgumentListInfo(From.LAngleLoc, From.RAngleLoc,
538 From.arguments(), Result);
539}
540
Gabor Marton5254e642018-06-27 13:32:50 +0000541std::tuple<FunctionTemplateDecl *, ASTNodeImporter::OptionalTemplateArgsTy>
542ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
543 FunctionDecl *FromFD) {
544 assert(FromFD->getTemplatedKind() ==
545 FunctionDecl::TK_FunctionTemplateSpecialization);
546 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
547 auto *Template = cast_or_null<FunctionTemplateDecl>(
548 Importer.Import(FTSInfo->getTemplate()));
549
550 // Import template arguments.
551 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
552 TemplateArgsTy ToTemplArgs;
553 if (ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
554 ToTemplArgs)) // Error during import.
555 return std::make_tuple(Template, OptionalTemplateArgsTy());
556
557 return std::make_tuple(Template, ToTemplArgs);
558}
559
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000560} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000561
Douglas Gregor3996e242010-02-15 22:01:00 +0000562//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000563// Import Types
564//----------------------------------------------------------------------------
565
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000566using namespace clang;
567
John McCall424cec92011-01-19 06:33:43 +0000568QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000569 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
570 << T->getTypeClassName();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000571 return {};
Douglas Gregore4c83e42010-02-09 22:48:33 +0000572}
573
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000574QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
575 QualType UnderlyingType = Importer.Import(T->getValueType());
576 if(UnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000577 return {};
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000578
579 return Importer.getToContext().getAtomicType(UnderlyingType);
580}
581
John McCall424cec92011-01-19 06:33:43 +0000582QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000583 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000584#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
585 case BuiltinType::Id: \
586 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000587#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000588#define SHARED_SINGLETON_TYPE(Expansion)
589#define BUILTIN_TYPE(Id, SingletonId) \
590 case BuiltinType::Id: return Importer.getToContext().SingletonId;
591#include "clang/AST/BuiltinTypes.def"
592
593 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
594 // context supports C++.
595
596 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
597 // context supports ObjC.
598
Douglas Gregor96e578d2010-02-05 17:54:41 +0000599 case BuiltinType::Char_U:
600 // The context we're importing from has an unsigned 'char'. If we're
601 // importing into a context with a signed 'char', translate to
602 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000603 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000604 return Importer.getToContext().UnsignedCharTy;
605
606 return Importer.getToContext().CharTy;
607
Douglas Gregor96e578d2010-02-05 17:54:41 +0000608 case BuiltinType::Char_S:
609 // The context we're importing from has an unsigned 'char'. If we're
610 // importing into a context with a signed 'char', translate to
611 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000612 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000613 return Importer.getToContext().SignedCharTy;
614
615 return Importer.getToContext().CharTy;
616
Chris Lattnerad3467e2010-12-25 23:25:43 +0000617 case BuiltinType::WChar_S:
618 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000619 // FIXME: If not in C++, shall we translate to the C equivalent of
620 // wchar_t?
621 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000622 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000623
624 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000625}
626
Aleksei Sidorina693b372016-09-28 10:16:56 +0000627QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
628 QualType OrigT = Importer.Import(T->getOriginalType());
629 if (OrigT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000630 return {};
Aleksei Sidorina693b372016-09-28 10:16:56 +0000631
632 return Importer.getToContext().getDecayedType(OrigT);
633}
634
John McCall424cec92011-01-19 06:33:43 +0000635QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000636 QualType ToElementType = Importer.Import(T->getElementType());
637 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000638 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000639
640 return Importer.getToContext().getComplexType(ToElementType);
641}
642
John McCall424cec92011-01-19 06:33:43 +0000643QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000644 QualType ToPointeeType = Importer.Import(T->getPointeeType());
645 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000646 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000647
648 return Importer.getToContext().getPointerType(ToPointeeType);
649}
650
John McCall424cec92011-01-19 06:33:43 +0000651QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000652 // FIXME: Check for blocks support in "to" context.
653 QualType ToPointeeType = Importer.Import(T->getPointeeType());
654 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000655 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000656
657 return Importer.getToContext().getBlockPointerType(ToPointeeType);
658}
659
John McCall424cec92011-01-19 06:33:43 +0000660QualType
661ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000662 // FIXME: Check for C++ support in "to" context.
663 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
664 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000665 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000666
667 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
668}
669
John McCall424cec92011-01-19 06:33:43 +0000670QualType
671ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000672 // FIXME: Check for C++0x support in "to" context.
673 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
674 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000675 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000676
677 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
678}
679
John McCall424cec92011-01-19 06:33:43 +0000680QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000681 // FIXME: Check for C++ support in "to" context.
682 QualType ToPointeeType = Importer.Import(T->getPointeeType());
683 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000684 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000685
686 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
687 return Importer.getToContext().getMemberPointerType(ToPointeeType,
688 ClassType.getTypePtr());
689}
690
John McCall424cec92011-01-19 06:33:43 +0000691QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000692 QualType ToElementType = Importer.Import(T->getElementType());
693 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000694 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000695
696 return Importer.getToContext().getConstantArrayType(ToElementType,
697 T->getSize(),
698 T->getSizeModifier(),
699 T->getIndexTypeCVRQualifiers());
700}
701
John McCall424cec92011-01-19 06:33:43 +0000702QualType
703ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000704 QualType ToElementType = Importer.Import(T->getElementType());
705 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000706 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000707
708 return Importer.getToContext().getIncompleteArrayType(ToElementType,
709 T->getSizeModifier(),
710 T->getIndexTypeCVRQualifiers());
711}
712
John McCall424cec92011-01-19 06:33:43 +0000713QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000714 QualType ToElementType = Importer.Import(T->getElementType());
715 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000716 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000717
718 Expr *Size = Importer.Import(T->getSizeExpr());
719 if (!Size)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000720 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000721
722 SourceRange Brackets = Importer.Import(T->getBracketsRange());
723 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
724 T->getSizeModifier(),
725 T->getIndexTypeCVRQualifiers(),
726 Brackets);
727}
728
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000729QualType ASTNodeImporter::VisitDependentSizedArrayType(
730 const DependentSizedArrayType *T) {
731 QualType ToElementType = Importer.Import(T->getElementType());
732 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000733 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000734
735 // SizeExpr may be null if size is not specified directly.
736 // For example, 'int a[]'.
737 Expr *Size = Importer.Import(T->getSizeExpr());
738 if (!Size && T->getSizeExpr())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000739 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000740
741 SourceRange Brackets = Importer.Import(T->getBracketsRange());
742 return Importer.getToContext().getDependentSizedArrayType(
743 ToElementType, Size, T->getSizeModifier(), T->getIndexTypeCVRQualifiers(),
744 Brackets);
745}
746
John McCall424cec92011-01-19 06:33:43 +0000747QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000748 QualType ToElementType = Importer.Import(T->getElementType());
749 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000750 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000751
752 return Importer.getToContext().getVectorType(ToElementType,
753 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +0000754 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000755}
756
John McCall424cec92011-01-19 06:33:43 +0000757QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000758 QualType ToElementType = Importer.Import(T->getElementType());
759 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000760 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000761
762 return Importer.getToContext().getExtVectorType(ToElementType,
763 T->getNumElements());
764}
765
John McCall424cec92011-01-19 06:33:43 +0000766QualType
767ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000768 // FIXME: What happens if we're importing a function without a prototype
769 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +0000770 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000771 if (ToResultType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000772 return {};
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000773
Douglas Gregor96e578d2010-02-05 17:54:41 +0000774 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000775 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000776}
777
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +0000778QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000779 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000780 if (ToResultType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000781 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000782
783 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000784 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000785 for (const auto &A : T->param_types()) {
786 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000787 if (ArgType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000788 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000789 ArgTypes.push_back(ArgType);
790 }
791
792 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000793 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000794 for (const auto &E : T->exceptions()) {
795 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000796 if (ExceptionType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000797 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000798 ExceptionTypes.push_back(ExceptionType);
799 }
John McCalldb40c7f2010-12-14 08:05:40 +0000800
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000801 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
802 FunctionProtoType::ExtProtoInfo ToEPI;
803
804 ToEPI.ExtInfo = FromEPI.ExtInfo;
805 ToEPI.Variadic = FromEPI.Variadic;
806 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
807 ToEPI.TypeQuals = FromEPI.TypeQuals;
808 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +0000809 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
810 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
811 ToEPI.ExceptionSpec.NoexceptExpr =
812 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
813 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
814 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
815 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
816 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000817
Jordan Rose5c382722013-03-08 21:51:21 +0000818 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000819}
820
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000821QualType ASTNodeImporter::VisitUnresolvedUsingType(
822 const UnresolvedUsingType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000823 const auto *ToD =
824 cast_or_null<UnresolvedUsingTypenameDecl>(Importer.Import(T->getDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000825 if (!ToD)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000826 return {};
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000827
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000828 auto *ToPrevD =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000829 cast_or_null<UnresolvedUsingTypenameDecl>(
830 Importer.Import(T->getDecl()->getPreviousDecl()));
831 if (!ToPrevD && T->getDecl()->getPreviousDecl())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000832 return {};
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000833
834 return Importer.getToContext().getTypeDeclType(ToD, ToPrevD);
835}
836
Sean Callananda6df8a2011-08-11 16:56:07 +0000837QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
838 QualType ToInnerType = Importer.Import(T->getInnerType());
839 if (ToInnerType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000840 return {};
Sean Callananda6df8a2011-08-11 16:56:07 +0000841
842 return Importer.getToContext().getParenType(ToInnerType);
843}
844
John McCall424cec92011-01-19 06:33:43 +0000845QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000846 auto *ToDecl =
847 dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000848 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000849 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000850
851 return Importer.getToContext().getTypeDeclType(ToDecl);
852}
853
John McCall424cec92011-01-19 06:33:43 +0000854QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000855 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
856 if (!ToExpr)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000857 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000858
859 return Importer.getToContext().getTypeOfExprType(ToExpr);
860}
861
John McCall424cec92011-01-19 06:33:43 +0000862QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000863 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
864 if (ToUnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000865 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000866
867 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
868}
869
John McCall424cec92011-01-19 06:33:43 +0000870QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +0000871 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +0000872 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
873 if (!ToExpr)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000874 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000875
Douglas Gregor81495f32012-02-12 18:42:33 +0000876 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
877 if (UnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000878 return {};
Douglas Gregor81495f32012-02-12 18:42:33 +0000879
880 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000881}
882
Alexis Hunte852b102011-05-24 22:41:36 +0000883QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
884 QualType ToBaseType = Importer.Import(T->getBaseType());
885 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
886 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000887 return {};
Alexis Hunte852b102011-05-24 22:41:36 +0000888
889 return Importer.getToContext().getUnaryTransformType(ToBaseType,
890 ToUnderlyingType,
891 T->getUTTKind());
892}
893
Richard Smith30482bc2011-02-20 03:19:35 +0000894QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +0000895 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +0000896 QualType FromDeduced = T->getDeducedType();
897 QualType ToDeduced;
898 if (!FromDeduced.isNull()) {
899 ToDeduced = Importer.Import(FromDeduced);
900 if (ToDeduced.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000901 return {};
Richard Smith30482bc2011-02-20 03:19:35 +0000902 }
903
Richard Smithe301ba22015-11-11 02:02:15 +0000904 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +0000905 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +0000906}
907
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000908QualType ASTNodeImporter::VisitInjectedClassNameType(
909 const InjectedClassNameType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000910 auto *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000911 if (!D)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000912 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000913
914 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
915 if (InjType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000916 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000917
918 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
919 // See comments in InjectedClassNameType definition for details
920 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
921 enum {
922 TypeAlignmentInBits = 4,
923 TypeAlignment = 1 << TypeAlignmentInBits
924 };
925
926 return QualType(new (Importer.getToContext(), TypeAlignment)
927 InjectedClassNameType(D, InjType), 0);
928}
929
John McCall424cec92011-01-19 06:33:43 +0000930QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000931 auto *ToDecl = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000932 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000933 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000934
935 return Importer.getToContext().getTagDeclType(ToDecl);
936}
937
John McCall424cec92011-01-19 06:33:43 +0000938QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000939 auto *ToDecl = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000940 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000941 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000942
943 return Importer.getToContext().getTagDeclType(ToDecl);
944}
945
Sean Callanan72fe0852015-04-02 23:50:08 +0000946QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
947 QualType FromModifiedType = T->getModifiedType();
948 QualType FromEquivalentType = T->getEquivalentType();
949 QualType ToModifiedType;
950 QualType ToEquivalentType;
951
952 if (!FromModifiedType.isNull()) {
953 ToModifiedType = Importer.Import(FromModifiedType);
954 if (ToModifiedType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000955 return {};
Sean Callanan72fe0852015-04-02 23:50:08 +0000956 }
957 if (!FromEquivalentType.isNull()) {
958 ToEquivalentType = Importer.Import(FromEquivalentType);
959 if (ToEquivalentType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000960 return {};
Sean Callanan72fe0852015-04-02 23:50:08 +0000961 }
962
963 return Importer.getToContext().getAttributedType(T->getAttrKind(),
964 ToModifiedType, ToEquivalentType);
965}
966
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000967QualType ASTNodeImporter::VisitTemplateTypeParmType(
968 const TemplateTypeParmType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000969 auto *ParmDecl =
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000970 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
971 if (!ParmDecl && T->getDecl())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000972 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000973
974 return Importer.getToContext().getTemplateTypeParmType(
975 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
976}
977
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000978QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
979 const SubstTemplateTypeParmType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000980 const auto *Replaced =
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000981 cast_or_null<TemplateTypeParmType>(Importer.Import(
982 QualType(T->getReplacedParameter(), 0)).getTypePtr());
983 if (!Replaced)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000984 return {};
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000985
986 QualType Replacement = Importer.Import(T->getReplacementType());
987 if (Replacement.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000988 return {};
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000989 Replacement = Replacement.getCanonicalType();
990
991 return Importer.getToContext().getSubstTemplateTypeParmType(
992 Replaced, Replacement);
993}
994
Douglas Gregore2e50d332010-12-01 01:36:18 +0000995QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +0000996 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000997 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
998 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000999 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001000
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001001 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001002 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001003 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001004
1005 QualType ToCanonType;
1006 if (!QualType(T, 0).isCanonical()) {
1007 QualType FromCanonType
1008 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1009 ToCanonType =Importer.Import(FromCanonType);
1010 if (ToCanonType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001011 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001012 }
1013 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +00001014 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001015 ToCanonType);
1016}
1017
John McCall424cec92011-01-19 06:33:43 +00001018QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +00001019 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001020 // Note: the qualifier in an ElaboratedType is optional.
1021 if (T->getQualifier()) {
1022 ToQualifier = Importer.Import(T->getQualifier());
1023 if (!ToQualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001024 return {};
Abramo Bagnara6150c882010-05-11 21:36:43 +00001025 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001026
1027 QualType ToNamedType = Importer.Import(T->getNamedType());
1028 if (ToNamedType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001029 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001030
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001031 TagDecl *OwnedTagDecl =
1032 cast_or_null<TagDecl>(Importer.Import(T->getOwnedTagDecl()));
1033 if (!OwnedTagDecl && T->getOwnedTagDecl())
1034 return {};
1035
Abramo Bagnara6150c882010-05-11 21:36:43 +00001036 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001037 ToQualifier, ToNamedType,
1038 OwnedTagDecl);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001039}
1040
Gabor Horvath7a91c082017-11-14 11:30:38 +00001041QualType ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
1042 QualType Pattern = Importer.Import(T->getPattern());
1043 if (Pattern.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001044 return {};
Gabor Horvath7a91c082017-11-14 11:30:38 +00001045
1046 return Importer.getToContext().getPackExpansionType(Pattern,
1047 T->getNumExpansions());
1048}
1049
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001050QualType ASTNodeImporter::VisitDependentTemplateSpecializationType(
1051 const DependentTemplateSpecializationType *T) {
1052 NestedNameSpecifier *Qualifier = Importer.Import(T->getQualifier());
1053 if (!Qualifier && T->getQualifier())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001054 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001055
1056 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
1057 if (!Name && T->getIdentifier())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001058 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001059
1060 SmallVector<TemplateArgument, 2> ToPack;
1061 ToPack.reserve(T->getNumArgs());
1062 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToPack))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001063 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001064
1065 return Importer.getToContext().getDependentTemplateSpecializationType(
1066 T->getKeyword(), Qualifier, Name, ToPack);
1067}
1068
Peter Szecsice7f3182018-05-07 12:08:27 +00001069QualType ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
1070 NestedNameSpecifier *NNS = Importer.Import(T->getQualifier());
1071 if (!NNS && T->getQualifier())
1072 return QualType();
1073
1074 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
1075 if (!Name && T->getIdentifier())
1076 return QualType();
1077
1078 QualType Canon = (T == T->getCanonicalTypeInternal().getTypePtr())
1079 ? QualType()
1080 : Importer.Import(T->getCanonicalTypeInternal());
1081 if (!Canon.isNull())
1082 Canon = Canon.getCanonicalType();
1083
1084 return Importer.getToContext().getDependentNameType(T->getKeyword(), NNS,
1085 Name, Canon);
1086}
1087
John McCall424cec92011-01-19 06:33:43 +00001088QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001089 auto *Class =
1090 dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001091 if (!Class)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001092 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001093
John McCall8b07ec22010-05-15 11:32:37 +00001094 return Importer.getToContext().getObjCInterfaceType(Class);
1095}
1096
John McCall424cec92011-01-19 06:33:43 +00001097QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001098 QualType ToBaseType = Importer.Import(T->getBaseType());
1099 if (ToBaseType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001100 return {};
John McCall8b07ec22010-05-15 11:32:37 +00001101
Douglas Gregore9d95f12015-07-07 03:57:35 +00001102 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001103 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001104 QualType ImportedTypeArg = Importer.Import(TypeArg);
1105 if (ImportedTypeArg.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001106 return {};
Douglas Gregore9d95f12015-07-07 03:57:35 +00001107
1108 TypeArgs.push_back(ImportedTypeArg);
1109 }
1110
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001111 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001112 for (auto *P : T->quals()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001113 auto *Protocol = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001114 if (!Protocol)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001115 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001116 Protocols.push_back(Protocol);
1117 }
1118
Douglas Gregore9d95f12015-07-07 03:57:35 +00001119 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001120 Protocols,
1121 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001122}
1123
John McCall424cec92011-01-19 06:33:43 +00001124QualType
1125ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001126 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1127 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001128 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001129
John McCall8b07ec22010-05-15 11:32:37 +00001130 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001131}
1132
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001133//----------------------------------------------------------------------------
1134// Import Declarations
1135//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001136bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1137 DeclContext *&LexicalDC,
1138 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00001139 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001140 SourceLocation &Loc) {
1141 // Import the context of this declaration.
1142 DC = Importer.ImportContext(D->getDeclContext());
1143 if (!DC)
1144 return true;
1145
1146 LexicalDC = DC;
1147 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1148 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1149 if (!LexicalDC)
1150 return true;
1151 }
1152
1153 // Import the name of this declaration.
1154 Name = Importer.Import(D->getDeclName());
1155 if (D->getDeclName() && !Name)
1156 return true;
1157
1158 // Import the location of this declaration.
1159 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00001160 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001161 return false;
1162}
1163
Douglas Gregord451ea92011-07-29 23:31:30 +00001164void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1165 if (!FromD)
1166 return;
1167
1168 if (!ToD) {
1169 ToD = Importer.Import(FromD);
1170 if (!ToD)
1171 return;
1172 }
1173
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001174 if (auto *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1175 if (auto *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00001176 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001177 ImportDefinition(FromRecord, ToRecord);
1178 }
1179 }
1180 return;
1181 }
1182
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001183 if (auto *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1184 if (auto *ToEnum = cast_or_null<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001185 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1186 ImportDefinition(FromEnum, ToEnum);
1187 }
1188 }
1189 return;
1190 }
1191}
1192
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001193void
1194ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1195 DeclarationNameInfo& To) {
1196 // NOTE: To.Name and To.Loc are already imported.
1197 // We only have to import To.LocInfo.
1198 switch (To.getName().getNameKind()) {
1199 case DeclarationName::Identifier:
1200 case DeclarationName::ObjCZeroArgSelector:
1201 case DeclarationName::ObjCOneArgSelector:
1202 case DeclarationName::ObjCMultiArgSelector:
1203 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001204 case DeclarationName::CXXDeductionGuideName:
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001205 return;
1206
1207 case DeclarationName::CXXOperatorName: {
1208 SourceRange Range = From.getCXXOperatorNameRange();
1209 To.setCXXOperatorNameRange(Importer.Import(Range));
1210 return;
1211 }
1212 case DeclarationName::CXXLiteralOperatorName: {
1213 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1214 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1215 return;
1216 }
1217 case DeclarationName::CXXConstructorName:
1218 case DeclarationName::CXXDestructorName:
1219 case DeclarationName::CXXConversionFunctionName: {
1220 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1221 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1222 return;
1223 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001224 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001225 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001226}
1227
Douglas Gregor2e15c842012-02-01 21:00:38 +00001228void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001229 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001230 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001231 return;
1232 }
1233
Aaron Ballman629afae2014-03-07 19:56:05 +00001234 for (auto *From : FromDC->decls())
1235 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00001236}
1237
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001238static void setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1239 ASTImporter &Importer) {
1240 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
1241 auto *ToTypedef =
1242 cast_or_null<TypedefNameDecl>(Importer.Import(FromTypedef));
1243 assert (ToTypedef && "Failed to import typedef of an anonymous structure");
1244
1245 To->setTypedefNameForAnonDecl(ToTypedef);
1246 }
1247}
1248
Douglas Gregord451ea92011-07-29 23:31:30 +00001249bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001250 ImportDefinitionKind Kind) {
1251 if (To->getDefinition() || To->isBeingDefined()) {
1252 if (Kind == IDK_Everything)
1253 ImportDeclContext(From, /*ForceImport=*/true);
1254
Douglas Gregore2e50d332010-12-01 01:36:18 +00001255 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001256 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001257
1258 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001259
1260 setTypedefNameForAnonDecl(From, To, Importer);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001261
1262 // Add base classes.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001263 if (auto *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1264 auto *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001265
1266 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1267 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1268 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001269 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001270 ToData.Aggregate = FromData.Aggregate;
1271 ToData.PlainOldData = FromData.PlainOldData;
1272 ToData.Empty = FromData.Empty;
1273 ToData.Polymorphic = FromData.Polymorphic;
1274 ToData.Abstract = FromData.Abstract;
1275 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001276 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1277 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1278 ToData.HasBasesWithNonStaticDataMembers =
1279 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001280 ToData.HasPrivateFields = FromData.HasPrivateFields;
1281 ToData.HasProtectedFields = FromData.HasProtectedFields;
1282 ToData.HasPublicFields = FromData.HasPublicFields;
1283 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001284 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001285 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001286 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001287 ToData.HasUninitializedReferenceMember
1288 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001289 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001290 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1291 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001292 ToData.NeedOverloadResolutionForCopyConstructor
1293 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001294 ToData.NeedOverloadResolutionForMoveConstructor
1295 = FromData.NeedOverloadResolutionForMoveConstructor;
1296 ToData.NeedOverloadResolutionForMoveAssignment
1297 = FromData.NeedOverloadResolutionForMoveAssignment;
1298 ToData.NeedOverloadResolutionForDestructor
1299 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001300 ToData.DefaultedCopyConstructorIsDeleted
1301 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001302 ToData.DefaultedMoveConstructorIsDeleted
1303 = FromData.DefaultedMoveConstructorIsDeleted;
1304 ToData.DefaultedMoveAssignmentIsDeleted
1305 = FromData.DefaultedMoveAssignmentIsDeleted;
1306 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001307 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1308 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001309 ToData.HasConstexprNonCopyMoveConstructor
1310 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001311 ToData.HasDefaultedDefaultConstructor
1312 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001313 ToData.DefaultedDefaultConstructorIsConstexpr
1314 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001315 ToData.HasConstexprDefaultConstructor
1316 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001317 ToData.HasNonLiteralTypeFieldsOrBases
1318 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001319 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001320 ToData.UserProvidedDefaultConstructor
1321 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001322 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001323 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1324 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1325 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1326 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001327 ToData.ImplicitCopyAssignmentHasConstParam
1328 = FromData.ImplicitCopyAssignmentHasConstParam;
1329 ToData.HasDeclaredCopyConstructorWithConstParam
1330 = FromData.HasDeclaredCopyConstructorWithConstParam;
1331 ToData.HasDeclaredCopyAssignmentWithConstParam
1332 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001333
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001334 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001335 for (const auto &Base1 : FromCXX->bases()) {
1336 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001337 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001338 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001339
1340 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001341 if (Base1.isPackExpansion())
1342 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001343
1344 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00001345 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00001346
Douglas Gregore2e50d332010-12-01 01:36:18 +00001347 Bases.push_back(
1348 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00001349 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1350 Base1.isVirtual(),
1351 Base1.isBaseOfClass(),
1352 Base1.getAccessSpecifierAsWritten(),
1353 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00001354 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001355 }
1356 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001357 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001358 }
1359
Douglas Gregor2e15c842012-02-01 21:00:38 +00001360 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001361 ImportDeclContext(From, /*ForceImport=*/true);
1362
Douglas Gregore2e50d332010-12-01 01:36:18 +00001363 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001364 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001365}
1366
Larisse Voufo39a1e502013-08-06 01:03:05 +00001367bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1368 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00001369 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00001370 return false;
1371
1372 // FIXME: Can we really import any initializer? Alternatively, we could force
1373 // ourselves to import every declaration of a variable and then only use
1374 // getInit() here.
1375 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1376
1377 // FIXME: Other bits to merge?
1378
1379 return false;
1380}
1381
Douglas Gregord451ea92011-07-29 23:31:30 +00001382bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001383 ImportDefinitionKind Kind) {
1384 if (To->getDefinition() || To->isBeingDefined()) {
1385 if (Kind == IDK_Everything)
1386 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001387 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001388 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001389
1390 To->startDefinition();
1391
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001392 setTypedefNameForAnonDecl(From, To, Importer);
1393
Douglas Gregord451ea92011-07-29 23:31:30 +00001394 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1395 if (T.isNull())
1396 return true;
1397
1398 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1399 if (ToPromotionType.isNull())
1400 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001401
1402 if (shouldForceImportDeclContext(Kind))
1403 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001404
1405 // FIXME: we might need to merge the number of positive or negative bits
1406 // if the enumerator lists don't match.
1407 To->completeDefinition(T, ToPromotionType,
1408 From->getNumPositiveBits(),
1409 From->getNumNegativeBits());
1410 return false;
1411}
1412
Douglas Gregora082a492010-11-30 19:14:50 +00001413TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1414 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001415 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1416 if (ImportContainerChecked(*Params, ToParams))
1417 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00001418
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001419 Expr *ToRequiresClause;
1420 if (Expr *const R = Params->getRequiresClause()) {
1421 ToRequiresClause = Importer.Import(R);
1422 if (!ToRequiresClause)
1423 return nullptr;
1424 } else {
1425 ToRequiresClause = nullptr;
1426 }
1427
Douglas Gregora082a492010-11-30 19:14:50 +00001428 return TemplateParameterList::Create(Importer.getToContext(),
1429 Importer.Import(Params->getTemplateLoc()),
1430 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00001431 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001432 Importer.Import(Params->getRAngleLoc()),
1433 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001434}
1435
Douglas Gregore2e50d332010-12-01 01:36:18 +00001436TemplateArgument
1437ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1438 switch (From.getKind()) {
1439 case TemplateArgument::Null:
1440 return TemplateArgument();
1441
1442 case TemplateArgument::Type: {
1443 QualType ToType = Importer.Import(From.getAsType());
1444 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001445 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001446 return TemplateArgument(ToType);
1447 }
1448
1449 case TemplateArgument::Integral: {
1450 QualType ToType = Importer.Import(From.getIntegralType());
1451 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001452 return {};
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001453 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001454 }
1455
Eli Friedmanb826a002012-09-26 02:36:12 +00001456 case TemplateArgument::Declaration: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001457 auto *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001458 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1459 if (!To || ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001460 return {};
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001461 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00001462 }
1463
1464 case TemplateArgument::NullPtr: {
1465 QualType ToType = Importer.Import(From.getNullPtrType());
1466 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001467 return {};
Eli Friedmanb826a002012-09-26 02:36:12 +00001468 return TemplateArgument(ToType, /*isNullPtr*/true);
1469 }
1470
Douglas Gregore2e50d332010-12-01 01:36:18 +00001471 case TemplateArgument::Template: {
1472 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1473 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001474 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001475
1476 return TemplateArgument(ToTemplate);
1477 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001478
1479 case TemplateArgument::TemplateExpansion: {
1480 TemplateName ToTemplate
1481 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1482 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001483 return {};
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001484
Douglas Gregore1d60df2011-01-14 23:41:42 +00001485 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001486 }
1487
Douglas Gregore2e50d332010-12-01 01:36:18 +00001488 case TemplateArgument::Expression:
1489 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1490 return TemplateArgument(ToExpr);
1491 return TemplateArgument();
1492
1493 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001494 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001495 ToPack.reserve(From.pack_size());
1496 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001497 return {};
Benjamin Kramercce63472015-08-05 09:40:22 +00001498
1499 return TemplateArgument(
1500 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001501 }
1502 }
1503
1504 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00001505}
1506
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001507Optional<TemplateArgumentLoc>
1508ASTNodeImporter::ImportTemplateArgumentLoc(const TemplateArgumentLoc &TALoc) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001509 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1510 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1511 TemplateArgumentLocInfo ToInfo;
1512 if (Arg.getKind() == TemplateArgument::Expression) {
1513 Expr *E = Importer.Import(FromInfo.getAsExpr());
1514 ToInfo = TemplateArgumentLocInfo(E);
1515 if (!E)
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001516 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001517 } else if (Arg.getKind() == TemplateArgument::Type) {
1518 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1519 ToInfo = TemplateArgumentLocInfo(TSI);
1520 else
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001521 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001522 } else {
1523 ToInfo = TemplateArgumentLocInfo(
1524 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1525 Importer.Import(FromInfo.getTemplateNameLoc()),
1526 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1527 }
1528 return TemplateArgumentLoc(Arg, ToInfo);
1529}
1530
Douglas Gregore2e50d332010-12-01 01:36:18 +00001531bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1532 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001533 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001534 for (unsigned I = 0; I != NumFromArgs; ++I) {
1535 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1536 if (To.isNull() && !FromArgs[I].isNull())
1537 return true;
1538
1539 ToArgs.push_back(To);
1540 }
1541
1542 return false;
1543}
1544
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001545// We cannot use Optional<> pattern here and below because
1546// TemplateArgumentListInfo's operator new is declared as deleted so it cannot
1547// be stored in Optional.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001548template <typename InContainerTy>
1549bool ASTNodeImporter::ImportTemplateArgumentListInfo(
1550 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1551 for (const auto &FromLoc : Container) {
1552 if (auto ToLoc = ImportTemplateArgumentLoc(FromLoc))
1553 ToTAInfo.addArgument(*ToLoc);
1554 else
1555 return true;
1556 }
1557 return false;
1558}
1559
Gabor Marton26f72a92018-07-12 09:42:05 +00001560static StructuralEquivalenceKind
1561getStructuralEquivalenceKind(const ASTImporter &Importer) {
1562 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
1563 : StructuralEquivalenceKind::Default;
1564}
1565
Douglas Gregor5c73e912010-02-11 00:48:18 +00001566bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001567 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001568 // Eliminate a potential failure point where we attempt to re-import
1569 // something we're trying to import while completing ToRecord.
1570 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1571 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001572 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00001573 if (ToOriginRecord)
1574 ToRecord = ToOriginRecord;
1575 }
1576
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001577 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001578 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001579 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001580 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001581 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001582 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001583}
1584
Larisse Voufo39a1e502013-08-06 01:03:05 +00001585bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1586 bool Complain) {
1587 StructuralEquivalenceContext Ctx(
1588 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001589 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1590 false, Complain);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001591 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
1592}
1593
Douglas Gregor98c10182010-02-12 22:17:39 +00001594bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Gabor Marton26f72a92018-07-12 09:42:05 +00001595 StructuralEquivalenceContext Ctx(
1596 Importer.getFromContext(), Importer.getToContext(),
1597 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001598 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001599}
1600
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001601bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1602 FunctionTemplateDecl *To) {
1603 StructuralEquivalenceContext Ctx(
1604 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001605 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1606 false, false);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001607 return Ctx.IsStructurallyEquivalent(From, To);
1608}
1609
Balazs Keric7797c42018-07-11 09:37:24 +00001610bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
1611 StructuralEquivalenceContext Ctx(
1612 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001613 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1614 false, false);
Balazs Keric7797c42018-07-11 09:37:24 +00001615 return Ctx.IsStructurallyEquivalent(From, To);
1616}
1617
Douglas Gregor91155082012-11-14 22:29:20 +00001618bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001619 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00001620 const llvm::APSInt &FromVal = FromEC->getInitVal();
1621 const llvm::APSInt &ToVal = ToEC->getInitVal();
1622
1623 return FromVal.isSigned() == ToVal.isSigned() &&
1624 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1625 FromVal == ToVal;
1626}
1627
1628bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001629 ClassTemplateDecl *To) {
1630 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1631 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001632 Importer.getNonEquivalentDecls(),
1633 getStructuralEquivalenceKind(Importer));
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001634 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00001635}
1636
Larisse Voufo39a1e502013-08-06 01:03:05 +00001637bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1638 VarTemplateDecl *To) {
1639 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1640 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001641 Importer.getNonEquivalentDecls(),
1642 getStructuralEquivalenceKind(Importer));
Larisse Voufo39a1e502013-08-06 01:03:05 +00001643 return Ctx.IsStructurallyEquivalent(From, To);
1644}
1645
Douglas Gregore4c83e42010-02-09 22:48:33 +00001646Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001647 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001648 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00001649 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00001650}
1651
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001652Decl *ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
1653 // Import the context of this declaration.
1654 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1655 if (!DC)
1656 return nullptr;
1657
1658 DeclContext *LexicalDC = DC;
1659 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1660 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1661 if (!LexicalDC)
1662 return nullptr;
1663 }
1664
1665 // Import the location of this declaration.
1666 SourceLocation Loc = Importer.Import(D->getLocation());
1667
Gabor Marton26f72a92018-07-12 09:42:05 +00001668 EmptyDecl *ToD;
1669 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, Loc))
1670 return ToD;
1671
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001672 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001673 LexicalDC->addDeclInternal(ToD);
1674 return ToD;
1675}
1676
Sean Callanan65198272011-11-17 23:20:56 +00001677Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1678 TranslationUnitDecl *ToD =
1679 Importer.getToContext().getTranslationUnitDecl();
1680
Gabor Marton26f72a92018-07-12 09:42:05 +00001681 Importer.MapImported(D, ToD);
Sean Callanan65198272011-11-17 23:20:56 +00001682
1683 return ToD;
1684}
1685
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001686Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001687 SourceLocation Loc = Importer.Import(D->getLocation());
1688 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1689
1690 // Import the context of this declaration.
1691 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1692 if (!DC)
1693 return nullptr;
1694
Gabor Marton26f72a92018-07-12 09:42:05 +00001695 AccessSpecDecl *ToD;
1696 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
1697 DC, Loc, ColonLoc))
1698 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001699
1700 // Lexical DeclContext and Semantic DeclContext
1701 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00001702 ToD->setLexicalDeclContext(DC);
1703 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001704
Gabor Marton26f72a92018-07-12 09:42:05 +00001705 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001706}
1707
Aleksei Sidorina693b372016-09-28 10:16:56 +00001708Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1709 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1710 if (!DC)
1711 return nullptr;
1712
1713 DeclContext *LexicalDC = DC;
1714
1715 // Import the location of this declaration.
1716 SourceLocation Loc = Importer.Import(D->getLocation());
1717
1718 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1719 if (!AssertExpr)
1720 return nullptr;
1721
1722 StringLiteral *FromMsg = D->getMessage();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001723 auto *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
Aleksei Sidorina693b372016-09-28 10:16:56 +00001724 if (!ToMsg && FromMsg)
1725 return nullptr;
1726
Gabor Marton26f72a92018-07-12 09:42:05 +00001727 StaticAssertDecl *ToD;
1728 if (GetImportedOrCreateDecl(
1729 ToD, D, Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1730 Importer.Import(D->getRParenLoc()), D->isFailed()))
1731 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001732
1733 ToD->setLexicalDeclContext(LexicalDC);
1734 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00001735 return ToD;
1736}
1737
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001738Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1739 // Import the major distinguishing characteristics of this namespace.
1740 DeclContext *DC, *LexicalDC;
1741 DeclarationName Name;
1742 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001743 NamedDecl *ToD;
1744 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001745 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001746 if (ToD)
1747 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001748
1749 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001750 if (!Name) {
1751 // This is an anonymous namespace. Adopt an existing anonymous
1752 // namespace if we can.
1753 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001754 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001755 MergeWithNamespace = TU->getAnonymousNamespace();
1756 else
1757 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1758 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001759 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001760 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001761 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001762 for (auto *FoundDecl : FoundDecls) {
1763 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001764 continue;
1765
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001766 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001767 MergeWithNamespace = FoundNS;
1768 ConflictingDecls.clear();
1769 break;
1770 }
1771
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001772 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001773 }
1774
1775 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001776 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001777 ConflictingDecls.data(),
1778 ConflictingDecls.size());
1779 }
1780 }
1781
1782 // Create the "to" namespace, if needed.
1783 NamespaceDecl *ToNamespace = MergeWithNamespace;
1784 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00001785 if (GetImportedOrCreateDecl(
1786 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
1787 Importer.Import(D->getLocStart()), Loc, Name.getAsIdentifierInfo(),
1788 /*PrevDecl=*/nullptr))
1789 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001790 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001791 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001792
1793 // If this is an anonymous namespace, register it as the anonymous
1794 // namespace within its context.
1795 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001796 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001797 TU->setAnonymousNamespace(ToNamespace);
1798 else
1799 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1800 }
1801 }
Gabor Marton26f72a92018-07-12 09:42:05 +00001802 Importer.MapImported(D, ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001803
1804 ImportDeclContext(D);
1805
1806 return ToNamespace;
1807}
1808
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001809Decl *ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1810 // Import the major distinguishing characteristics of this namespace.
1811 DeclContext *DC, *LexicalDC;
1812 DeclarationName Name;
1813 SourceLocation Loc;
1814 NamedDecl *LookupD;
1815 if (ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
1816 return nullptr;
1817 if (LookupD)
1818 return LookupD;
1819
1820 // NOTE: No conflict resolution is done for namespace aliases now.
1821
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001822 auto *TargetDecl = cast_or_null<NamespaceDecl>(
1823 Importer.Import(D->getNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001824 if (!TargetDecl)
1825 return nullptr;
1826
1827 IdentifierInfo *ToII = Importer.Import(D->getIdentifier());
1828 if (!ToII)
1829 return nullptr;
1830
1831 NestedNameSpecifierLoc ToQLoc = Importer.Import(D->getQualifierLoc());
1832 if (D->getQualifierLoc() && !ToQLoc)
1833 return nullptr;
1834
Gabor Marton26f72a92018-07-12 09:42:05 +00001835 NamespaceAliasDecl *ToD;
1836 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC,
1837 Importer.Import(D->getNamespaceLoc()),
1838 Importer.Import(D->getAliasLoc()), ToII, ToQLoc,
1839 Importer.Import(D->getTargetNameLoc()),
1840 TargetDecl))
1841 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001842
1843 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001844 LexicalDC->addDeclInternal(ToD);
1845
1846 return ToD;
1847}
1848
Richard Smithdda56e42011-04-15 14:24:37 +00001849Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001850 // Import the major distinguishing characteristics of this typedef.
1851 DeclContext *DC, *LexicalDC;
1852 DeclarationName Name;
1853 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001854 NamedDecl *ToD;
1855 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001856 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001857 if (ToD)
1858 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001859
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001860 // If this typedef is not in block scope, determine whether we've
1861 // seen a typedef with the same name (that we can merge with) or any
1862 // other entity by that name (which name lookup could conflict with).
1863 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001864 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001865 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001866 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001867 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001868 for (auto *FoundDecl : FoundDecls) {
1869 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001870 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001871 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001872 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1873 FoundTypedef->getUnderlyingType()))
Gabor Marton26f72a92018-07-12 09:42:05 +00001874 return Importer.MapImported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001875 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001876
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001877 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001878 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001879
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001880 if (!ConflictingDecls.empty()) {
1881 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1882 ConflictingDecls.data(),
1883 ConflictingDecls.size());
1884 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001885 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001886 }
1887 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001888
Douglas Gregorb4964f72010-02-15 23:54:17 +00001889 // Import the underlying type of this typedef;
1890 QualType T = Importer.Import(D->getUnderlyingType());
1891 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001892 return nullptr;
1893
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001894 // Create the new typedef node.
1895 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00001896 SourceLocation StartL = Importer.Import(D->getLocStart());
Gabor Marton26f72a92018-07-12 09:42:05 +00001897
Richard Smithdda56e42011-04-15 14:24:37 +00001898 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00001899 if (IsAlias) {
1900 if (GetImportedOrCreateDecl<TypeAliasDecl>(
1901 ToTypedef, D, Importer.getToContext(), DC, StartL, Loc,
1902 Name.getAsIdentifierInfo(), TInfo))
1903 return ToTypedef;
1904 } else if (GetImportedOrCreateDecl<TypedefDecl>(
1905 ToTypedef, D, Importer.getToContext(), DC, StartL, Loc,
1906 Name.getAsIdentifierInfo(), TInfo))
1907 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001908
Douglas Gregordd483172010-02-22 17:42:47 +00001909 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001910 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001911
1912 // Templated declarations should not appear in DeclContext.
1913 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
1914 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
1915 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001916
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001917 return ToTypedef;
1918}
1919
Richard Smithdda56e42011-04-15 14:24:37 +00001920Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1921 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1922}
1923
1924Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1925 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1926}
1927
Gabor Horvath7a91c082017-11-14 11:30:38 +00001928Decl *ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1929 // Import the major distinguishing characteristics of this typedef.
1930 DeclContext *DC, *LexicalDC;
1931 DeclarationName Name;
1932 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001933 NamedDecl *FoundD;
1934 if (ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001935 return nullptr;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001936 if (FoundD)
1937 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00001938
1939 // If this typedef is not in block scope, determine whether we've
1940 // seen a typedef with the same name (that we can merge with) or any
1941 // other entity by that name (which name lookup could conflict with).
1942 if (!DC->isFunctionOrMethod()) {
1943 SmallVector<NamedDecl *, 4> ConflictingDecls;
1944 unsigned IDNS = Decl::IDNS_Ordinary;
1945 SmallVector<NamedDecl *, 2> FoundDecls;
1946 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001947 for (auto *FoundDecl : FoundDecls) {
1948 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001949 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001950 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00001951 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001952 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00001953 }
1954
1955 if (!ConflictingDecls.empty()) {
1956 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1957 ConflictingDecls.data(),
1958 ConflictingDecls.size());
1959 if (!Name)
1960 return nullptr;
1961 }
1962 }
1963
1964 TemplateParameterList *Params = ImportTemplateParameterList(
1965 D->getTemplateParameters());
1966 if (!Params)
1967 return nullptr;
1968
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001969 auto *TemplDecl = cast_or_null<TypeAliasDecl>(
Gabor Horvath7a91c082017-11-14 11:30:38 +00001970 Importer.Import(D->getTemplatedDecl()));
1971 if (!TemplDecl)
1972 return nullptr;
1973
Gabor Marton26f72a92018-07-12 09:42:05 +00001974 TypeAliasTemplateDecl *ToAlias;
1975 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
1976 Name, Params, TemplDecl))
1977 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00001978
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001979 TemplDecl->setDescribedAliasTemplate(ToAlias);
1980
Gabor Horvath7a91c082017-11-14 11:30:38 +00001981 ToAlias->setAccess(D->getAccess());
1982 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00001983 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001984 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00001985}
1986
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001987Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
1988 // Import the major distinguishing characteristics of this label.
1989 DeclContext *DC, *LexicalDC;
1990 DeclarationName Name;
1991 SourceLocation Loc;
1992 NamedDecl *ToD;
1993 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1994 return nullptr;
1995 if (ToD)
1996 return ToD;
1997
1998 assert(LexicalDC->isFunctionOrMethod());
1999
Gabor Marton26f72a92018-07-12 09:42:05 +00002000 LabelDecl *ToLabel;
2001 if (D->isGnuLocal()
2002 ? GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC,
2003 Importer.Import(D->getLocation()),
2004 Name.getAsIdentifierInfo(),
2005 Importer.Import(D->getLocStart()))
2006 : GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC,
2007 Importer.Import(D->getLocation()),
2008 Name.getAsIdentifierInfo()))
2009 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002010
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002011 auto *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002012 if (!Label)
2013 return nullptr;
2014
2015 ToLabel->setStmt(Label);
2016 ToLabel->setLexicalDeclContext(LexicalDC);
2017 LexicalDC->addDeclInternal(ToLabel);
2018 return ToLabel;
2019}
2020
Douglas Gregor98c10182010-02-12 22:17:39 +00002021Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2022 // Import the major distinguishing characteristics of this enum.
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 Gregor98c10182010-02-12 22:17:39 +00002032 // Figure out what enum 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 Gregor98c10182010-02-12 22:17:39 +00002037 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002038 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002039 IDNS |= Decl::IDNS_Ordinary;
2040
2041 // We may already have an enum of the same name; try to find and match it.
2042 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002043 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002044 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002045 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002046 for (auto *FoundDecl : FoundDecls) {
2047 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002048 continue;
2049
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002050 Decl *Found = FoundDecl;
2051 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2052 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor98c10182010-02-12 22:17:39 +00002053 Found = Tag->getDecl();
2054 }
2055
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002056 if (auto *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002057 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002058 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002059 }
2060
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002061 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002062 }
2063
2064 if (!ConflictingDecls.empty()) {
2065 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2066 ConflictingDecls.data(),
2067 ConflictingDecls.size());
2068 }
2069 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002070
Douglas Gregor98c10182010-02-12 22:17:39 +00002071 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002072 EnumDecl *D2;
2073 if (GetImportedOrCreateDecl(
2074 D2, D, Importer.getToContext(), DC, Importer.Import(D->getLocStart()),
2075 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2076 D->isScopedUsingClassTag(), D->isFixed()))
2077 return D2;
2078
John McCall3e11ebe2010-03-15 10:12:16 +00002079 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002080 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002081 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002082 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002083 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002084
2085 // Import the integer type.
2086 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2087 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002088 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00002089 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002090
2091 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002092 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00002093 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002094
Douglas Gregor3996e242010-02-15 22:01:00 +00002095 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002096}
2097
Douglas Gregor5c73e912010-02-11 00:48:18 +00002098Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2099 // If this record has a definition in the translation unit we're coming from,
2100 // but this particular declaration is not that definition, import the
2101 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002102 TagDecl *Definition = D->getDefinition();
Gabor Martona3af5672018-05-23 14:24:02 +00002103 if (Definition && Definition != D &&
2104 // In contrast to a normal CXXRecordDecl, the implicit
2105 // CXXRecordDecl of ClassTemplateSpecializationDecl is its redeclaration.
2106 // The definition of the implicit CXXRecordDecl in this case is the
2107 // ClassTemplateSpecializationDecl itself. Thus, we start with an extra
2108 // condition in order to be able to import the implict Decl.
2109 !D->isImplicit()) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002110 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002111 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002112 return nullptr;
2113
Gabor Marton26f72a92018-07-12 09:42:05 +00002114 return Importer.MapImported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002115 }
Gabor Martona3af5672018-05-23 14:24:02 +00002116
Douglas Gregor5c73e912010-02-11 00:48:18 +00002117 // Import the major distinguishing characteristics of this record.
2118 DeclContext *DC, *LexicalDC;
2119 DeclarationName Name;
2120 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002121 NamedDecl *ToD;
2122 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002123 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002124 if (ToD)
2125 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002126
Douglas Gregor5c73e912010-02-11 00:48:18 +00002127 // Figure out what structure name we're looking for.
2128 unsigned IDNS = Decl::IDNS_Tag;
2129 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002130 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2131 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002132 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002133 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002134 IDNS |= Decl::IDNS_Ordinary;
2135
2136 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002137 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00002138 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002139 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002140 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002141 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002142 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00002143
2144 if (!FoundDecls.empty()) {
2145 // We're going to have to compare D against potentially conflicting Decls, so complete it.
2146 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2147 D->getASTContext().getExternalSource()->CompleteType(D);
2148 }
2149
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002150 for (auto *FoundDecl : FoundDecls) {
2151 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002152 continue;
2153
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002154 Decl *Found = FoundDecl;
2155 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2156 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002157 Found = Tag->getDecl();
2158 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002159
2160 if (D->getDescribedTemplate()) {
2161 if (auto *Template = dyn_cast<ClassTemplateDecl>(Found))
2162 Found = Template->getTemplatedDecl();
2163 else
2164 continue;
2165 }
2166
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002167 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Aleksei Sidorin499de6c2018-04-05 15:31:49 +00002168 if (!SearchName) {
Gabor Marton0bebf952018-07-05 09:51:13 +00002169 if (!IsStructuralMatch(D, FoundRecord, false))
2170 continue;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002171 }
2172
Sean Callanan9092d472017-05-13 00:46:33 +00002173 PrevDecl = FoundRecord;
2174
Douglas Gregor25791052010-02-12 00:09:27 +00002175 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002176 if ((SearchName && !D->isCompleteDefinition())
2177 || (D->isCompleteDefinition() &&
2178 D->isAnonymousStructOrUnion()
2179 == FoundDef->isAnonymousStructOrUnion() &&
2180 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002181 // The record types structurally match, or the "from" translation
2182 // unit only had a forward declaration anyway; call it the same
2183 // function.
2184 // FIXME: For C++, we should also merge methods here.
Gabor Marton26f72a92018-07-12 09:42:05 +00002185 return Importer.MapImported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002186 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002187 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002188 // We have a forward declaration of this type, so adopt that forward
2189 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002190
2191 // If one or both can be completed from external storage then try one
2192 // last time to complete and compare them before doing this.
2193
2194 if (FoundRecord->hasExternalLexicalStorage() &&
2195 !FoundRecord->isCompleteDefinition())
2196 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2197 if (D->hasExternalLexicalStorage())
2198 D->getASTContext().getExternalSource()->CompleteType(D);
2199
2200 if (FoundRecord->isCompleteDefinition() &&
2201 D->isCompleteDefinition() &&
2202 !IsStructuralMatch(D, FoundRecord))
2203 continue;
2204
Douglas Gregor25791052010-02-12 00:09:27 +00002205 AdoptDecl = FoundRecord;
2206 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002207 } else if (!SearchName) {
2208 continue;
2209 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002210 }
2211
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002212 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002213 }
2214
Douglas Gregordd6006f2012-07-17 21:16:27 +00002215 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002216 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2217 ConflictingDecls.data(),
2218 ConflictingDecls.size());
2219 }
2220 }
2221
2222 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002223 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002224 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002225 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002226 CXXRecordDecl *D2CXX = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002227 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002228 if (DCXX->isLambda()) {
2229 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
Gabor Marton26f72a92018-07-12 09:42:05 +00002230 if (GetImportedOrCreateSpecialDecl(
2231 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
2232 DC, TInfo, Loc, DCXX->isDependentLambda(),
2233 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2234 return D2CXX;
Sean Callanan8bca9962016-03-28 21:43:01 +00002235 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2236 if (DCXX->getLambdaContextDecl() && !CDecl)
2237 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00002238 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002239 } else if (DCXX->isInjectedClassName()) {
2240 // We have to be careful to do a similar dance to the one in
2241 // Sema::ActOnStartCXXMemberDeclarations
2242 CXXRecordDecl *const PrevDecl = nullptr;
2243 const bool DelayTypeCreation = true;
Gabor Marton26f72a92018-07-12 09:42:05 +00002244 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2245 D->getTagKind(), DC, StartLoc, Loc,
2246 Name.getAsIdentifierInfo(), PrevDecl,
2247 DelayTypeCreation))
2248 return D2CXX;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002249 Importer.getToContext().getTypeDeclType(
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002250 D2CXX, dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002251 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002252 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2253 D->getTagKind(), DC, StartLoc, Loc,
2254 Name.getAsIdentifierInfo(),
2255 cast_or_null<CXXRecordDecl>(PrevDecl)))
2256 return D2CXX;
Sean Callanan8bca9962016-03-28 21:43:01 +00002257 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002258
Douglas Gregor3996e242010-02-15 22:01:00 +00002259 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002260 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002261 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonde8bf262018-05-17 09:46:07 +00002262 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002263 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002264
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002265 if (ClassTemplateDecl *FromDescribed =
2266 DCXX->getDescribedClassTemplate()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002267 auto *ToDescribed = cast_or_null<ClassTemplateDecl>(
2268 Importer.Import(FromDescribed));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002269 if (!ToDescribed)
2270 return nullptr;
2271 D2CXX->setDescribedClassTemplate(ToDescribed);
Gabor Marton5915777e2018-06-26 13:44:24 +00002272 if (!DCXX->isInjectedClassName()) {
2273 // In a record describing a template the type should be an
2274 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2275 // previously set type to the correct value here (ToDescribed is not
2276 // available at record create).
2277 // FIXME: The previous type is cleared but not removed from
2278 // ASTContext's internal storage.
2279 CXXRecordDecl *Injected = nullptr;
2280 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2281 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2282 if (Record && Record->isInjectedClassName()) {
2283 Injected = Record;
2284 break;
2285 }
2286 }
2287 D2CXX->setTypeForDecl(nullptr);
2288 Importer.getToContext().getInjectedClassNameType(D2CXX,
2289 ToDescribed->getInjectedClassNameSpecialization());
2290 if (Injected) {
2291 Injected->setTypeForDecl(nullptr);
2292 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2293 }
2294 }
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002295 } else if (MemberSpecializationInfo *MemberInfo =
2296 DCXX->getMemberSpecializationInfo()) {
2297 TemplateSpecializationKind SK =
2298 MemberInfo->getTemplateSpecializationKind();
2299 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002300 auto *ToInst =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002301 cast_or_null<CXXRecordDecl>(Importer.Import(FromInst));
2302 if (FromInst && !ToInst)
2303 return nullptr;
2304 D2CXX->setInstantiationOfMemberClass(ToInst, SK);
2305 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2306 Importer.Import(MemberInfo->getPointOfInstantiation()));
2307 }
Douglas Gregor25791052010-02-12 00:09:27 +00002308 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002309 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
2310 D->getTagKind(), DC, StartLoc, Loc,
2311 Name.getAsIdentifierInfo(), PrevDecl))
2312 return D2;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002313 D2->setLexicalDeclContext(LexicalDC);
2314 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002315 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002316
Douglas Gregor14454802011-02-25 02:25:35 +00002317 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd6006f2012-07-17 21:16:27 +00002318 if (D->isAnonymousStructOrUnion())
2319 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002320 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002321
2322 Importer.MapImported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002323
Douglas Gregor95d82832012-01-24 18:36:04 +00002324 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002325 return nullptr;
2326
Douglas Gregor3996e242010-02-15 22:01:00 +00002327 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002328}
2329
Douglas Gregor98c10182010-02-12 22:17:39 +00002330Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2331 // Import the major distinguishing characteristics of this enumerator.
2332 DeclContext *DC, *LexicalDC;
2333 DeclarationName Name;
2334 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002335 NamedDecl *ToD;
2336 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002337 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002338 if (ToD)
2339 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002340
2341 QualType T = Importer.Import(D->getType());
2342 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002343 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002344
Douglas Gregor98c10182010-02-12 22:17:39 +00002345 // Determine whether there are any other declarations with the same name and
2346 // in the same context.
2347 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002348 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002349 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002350 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002351 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002352 for (auto *FoundDecl : FoundDecls) {
2353 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002354 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002355
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002356 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002357 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002358 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002359 }
2360
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002361 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002362 }
2363
2364 if (!ConflictingDecls.empty()) {
2365 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2366 ConflictingDecls.data(),
2367 ConflictingDecls.size());
2368 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002369 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002370 }
2371 }
2372
2373 Expr *Init = Importer.Import(D->getInitExpr());
2374 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002375 return nullptr;
2376
Gabor Marton26f72a92018-07-12 09:42:05 +00002377 EnumConstantDecl *ToEnumerator;
2378 if (GetImportedOrCreateDecl(
2379 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2380 Name.getAsIdentifierInfo(), T, Init, D->getInitVal()))
2381 return ToEnumerator;
2382
Douglas Gregordd483172010-02-22 17:42:47 +00002383 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002384 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002385 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002386 return ToEnumerator;
2387}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002388
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002389bool ASTNodeImporter::ImportTemplateInformation(FunctionDecl *FromFD,
2390 FunctionDecl *ToFD) {
2391 switch (FromFD->getTemplatedKind()) {
2392 case FunctionDecl::TK_NonTemplate:
2393 case FunctionDecl::TK_FunctionTemplate:
Sam McCallfdc32072018-01-26 12:06:44 +00002394 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002395
2396 case FunctionDecl::TK_MemberSpecialization: {
2397 auto *InstFD = cast_or_null<FunctionDecl>(
2398 Importer.Import(FromFD->getInstantiatedFromMemberFunction()));
2399 if (!InstFD)
2400 return true;
2401
2402 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
2403 SourceLocation POI = Importer.Import(
2404 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation());
2405 ToFD->setInstantiationOfMemberFunction(InstFD, TSK);
2406 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002407 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002408 }
2409
2410 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Gabor Marton5254e642018-06-27 13:32:50 +00002411 FunctionTemplateDecl* Template;
2412 OptionalTemplateArgsTy ToTemplArgs;
2413 std::tie(Template, ToTemplArgs) =
2414 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
2415 if (!Template || !ToTemplArgs)
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002416 return true;
2417
2418 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Gabor Marton5254e642018-06-27 13:32:50 +00002419 Importer.getToContext(), *ToTemplArgs);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002420
Gabor Marton5254e642018-06-27 13:32:50 +00002421 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002422 TemplateArgumentListInfo ToTAInfo;
2423 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002424 if (FromTAArgsAsWritten)
2425 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002426 return true;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002427
2428 SourceLocation POI = Importer.Import(FTSInfo->getPointOfInstantiation());
2429
Gabor Marton5254e642018-06-27 13:32:50 +00002430 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002431 ToFD->setFunctionTemplateSpecialization(
2432 Template, ToTAList, /* InsertPos= */ nullptr,
2433 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002434 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002435 }
2436
2437 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2438 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2439 UnresolvedSet<8> TemplDecls;
2440 unsigned NumTemplates = FromInfo->getNumTemplates();
2441 for (unsigned I = 0; I < NumTemplates; I++) {
2442 if (auto *ToFTD = cast_or_null<FunctionTemplateDecl>(
2443 Importer.Import(FromInfo->getTemplate(I))))
2444 TemplDecls.addDecl(ToFTD);
2445 else
2446 return true;
2447 }
2448
2449 // Import TemplateArgumentListInfo.
2450 TemplateArgumentListInfo ToTAInfo;
2451 if (ImportTemplateArgumentListInfo(
2452 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2453 llvm::makeArrayRef(FromInfo->getTemplateArgs(),
2454 FromInfo->getNumTemplateArgs()),
2455 ToTAInfo))
2456 return true;
2457
2458 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2459 TemplDecls, ToTAInfo);
Sam McCallfdc32072018-01-26 12:06:44 +00002460 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002461 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002462 }
Sam McCallfdc32072018-01-26 12:06:44 +00002463 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002464}
2465
Gabor Marton5254e642018-06-27 13:32:50 +00002466FunctionDecl *
2467ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
2468 FunctionTemplateDecl* Template;
2469 OptionalTemplateArgsTy ToTemplArgs;
2470 std::tie(Template, ToTemplArgs) =
2471 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
2472 if (!Template || !ToTemplArgs)
2473 return nullptr;
2474
2475 void *InsertPos = nullptr;
2476 auto *FoundSpec = Template->findSpecialization(*ToTemplArgs, InsertPos);
2477 return FoundSpec;
2478}
2479
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002480Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00002481
2482 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
2483 auto RedeclIt = Redecls.begin();
2484 // Import the first part of the decl chain. I.e. import all previous
2485 // declarations starting from the canonical decl.
2486 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt)
2487 if (!Importer.Import(*RedeclIt))
2488 return nullptr;
2489 assert(*RedeclIt == D);
2490
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002491 // Import the major distinguishing characteristics of this function.
2492 DeclContext *DC, *LexicalDC;
2493 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002494 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002495 NamedDecl *ToD;
2496 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002497 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002498 if (ToD)
2499 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002500
Gabor Marton5254e642018-06-27 13:32:50 +00002501 const FunctionDecl *FoundByLookup = nullptr;
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002502
Gabor Marton5254e642018-06-27 13:32:50 +00002503 // If this is a function template specialization, then try to find the same
2504 // existing specialization in the "to" context. The localUncachedLookup
2505 // below will not find any specialization, but would find the primary
2506 // template; thus, we have to skip normal lookup in case of specializations.
2507 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
2508 if (D->getTemplatedKind() ==
2509 FunctionDecl::TK_FunctionTemplateSpecialization) {
2510 if (FunctionDecl *FoundFunction = FindFunctionTemplateSpecialization(D)) {
2511 if (D->doesThisDeclarationHaveABody() &&
2512 FoundFunction->hasBody())
2513 return Importer.Imported(D, FoundFunction);
2514 FoundByLookup = FoundFunction;
2515 }
2516 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002517 // Try to find a function in our own ("to") context with the same name, same
2518 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00002519 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002520 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00002521 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002522 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002523 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002524 for (auto *FoundDecl : FoundDecls) {
2525 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002526 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002527
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002528 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002529 if (FoundFunction->hasExternalFormalLinkage() &&
2530 D->hasExternalFormalLinkage()) {
Balazs Keric7797c42018-07-11 09:37:24 +00002531 if (IsStructuralMatch(D, FoundFunction)) {
2532 const FunctionDecl *Definition = nullptr;
2533 if (D->doesThisDeclarationHaveABody() &&
2534 FoundFunction->hasBody(Definition)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002535 return Importer.MapImported(
Balazs Keric7797c42018-07-11 09:37:24 +00002536 D, const_cast<FunctionDecl *>(Definition));
2537 }
2538 FoundByLookup = FoundFunction;
2539 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002540 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002541
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002542 // FIXME: Check for overloading more carefully, e.g., by boosting
2543 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002544
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002545 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002546 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002547 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002548
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002549 // Complain about inconsistent function types.
2550 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002551 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002552 Importer.ToDiag(FoundFunction->getLocation(),
2553 diag::note_odr_value_here)
2554 << FoundFunction->getType();
2555 }
2556 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002557
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002558 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002559 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002560
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002561 if (!ConflictingDecls.empty()) {
2562 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2563 ConflictingDecls.data(),
2564 ConflictingDecls.size());
2565 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002566 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002567 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002568 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002569
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002570 DeclarationNameInfo NameInfo(Name, Loc);
2571 // Import additional name location/type info.
2572 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2573
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002574 QualType FromTy = D->getType();
2575 bool usedDifferentExceptionSpec = false;
2576
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002577 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002578 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2579 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2580 // FunctionDecl that we are importing the FunctionProtoType for.
2581 // To avoid an infinite recursion when importing, create the FunctionDecl
2582 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002583 if (FromEPI.ExceptionSpec.SourceDecl ||
2584 FromEPI.ExceptionSpec.SourceTemplate ||
2585 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002586 FunctionProtoType::ExtProtoInfo DefaultEPI;
2587 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002588 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002589 usedDifferentExceptionSpec = true;
2590 }
2591 }
2592
Douglas Gregorb4964f72010-02-15 23:54:17 +00002593 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002594 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002595 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002596 return nullptr;
2597
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002598 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002599 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00002600 for (auto P : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002601 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002602 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002603 return nullptr;
2604
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002605 Parameters.push_back(ToP);
2606 }
2607
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002608 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002609 if (D->getTypeSourceInfo() && !TInfo)
2610 return nullptr;
2611
2612 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00002613 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002614 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002615 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002616 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
2617 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
2618 InnerLocStart, NameInfo, T, TInfo, FromConstructor->isExplicit(),
2619 D->isInlineSpecified(), D->isImplicit(), D->isConstexpr()))
2620 return ToFunction;
Sean Callanandd2c1742016-05-16 20:48:03 +00002621 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
2622 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002623 for (auto *I : FromConstructor->inits()) {
2624 auto *ToI = cast_or_null<CXXCtorInitializer>(Importer.Import(I));
Sean Callanandd2c1742016-05-16 20:48:03 +00002625 if (!ToI && I)
2626 return nullptr;
2627 CtorInitializers.push_back(ToI);
2628 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002629 auto **Memory =
Sean Callanandd2c1742016-05-16 20:48:03 +00002630 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
2631 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002632 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
Sean Callanandd2c1742016-05-16 20:48:03 +00002633 ToCtor->setCtorInitializers(Memory);
2634 ToCtor->setNumCtorInitializers(NumInitializers);
2635 }
Douglas Gregor00eace12010-02-21 18:29:16 +00002636 } else if (isa<CXXDestructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002637 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
2638 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
2639 InnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
2640 D->isImplicit()))
2641 return ToFunction;
2642 } else if (CXXConversionDecl *FromConversion =
2643 dyn_cast<CXXConversionDecl>(D)) {
2644 if (GetImportedOrCreateDecl<CXXConversionDecl>(
2645 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
2646 InnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
2647 FromConversion->isExplicit(), D->isConstexpr(), SourceLocation()))
2648 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002649 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002650 if (GetImportedOrCreateDecl<CXXMethodDecl>(
2651 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
2652 InnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
2653 Method->isInlineSpecified(), D->isConstexpr(), SourceLocation()))
2654 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00002655 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002656 if (GetImportedOrCreateDecl(ToFunction, D, Importer.getToContext(), DC,
2657 InnerLocStart, NameInfo, T, TInfo,
2658 D->getStorageClass(), D->isInlineSpecified(),
2659 D->hasWrittenPrototype(), D->isConstexpr()))
2660 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00002661 }
John McCall3e11ebe2010-03-15 10:12:16 +00002662
2663 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002664 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002665 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002666 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002667 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2668 ToFunction->setTrivial(D->isTrivial());
2669 ToFunction->setPure(D->isPure());
Rafael Stahla0010472018-07-09 08:40:17 +00002670 ToFunction->setRangeEnd(Importer.Import(D->getLocEnd()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00002671
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002672 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002673 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002674 Param->setOwningFunction(ToFunction);
2675 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002676 }
David Blaikie9c70e042011-09-21 18:16:56 +00002677 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002678
Gabor Marton5254e642018-06-27 13:32:50 +00002679 if (FoundByLookup) {
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002680 auto *Recent = const_cast<FunctionDecl *>(
Gabor Marton5254e642018-06-27 13:32:50 +00002681 FoundByLookup->getMostRecentDecl());
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002682 ToFunction->setPreviousDecl(Recent);
2683 }
2684
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002685 // We need to complete creation of FunctionProtoTypeLoc manually with setting
2686 // params it refers to.
2687 if (TInfo) {
2688 if (auto ProtoLoc =
2689 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
2690 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
2691 ProtoLoc.setParam(I, Parameters[I]);
2692 }
2693 }
2694
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002695 if (usedDifferentExceptionSpec) {
2696 // Update FunctionProtoType::ExtProtoInfo.
2697 QualType T = Importer.Import(D->getType());
2698 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002699 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002700 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002701 }
2702
Gabor Marton5254e642018-06-27 13:32:50 +00002703 if (D->doesThisDeclarationHaveABody()) {
2704 if (Stmt *FromBody = D->getBody()) {
2705 if (Stmt *ToBody = Importer.Import(FromBody)) {
2706 ToFunction->setBody(ToBody);
2707 }
Sean Callanan59721b32015-04-28 18:41:46 +00002708 }
2709 }
2710
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002711 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002712
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002713 // If it is a template, import all related things.
2714 if (ImportTemplateInformation(D, ToFunction))
2715 return nullptr;
2716
Gabor Marton5254e642018-06-27 13:32:50 +00002717 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
2718
2719 // TODO Can we generalize this approach to other AST nodes as well?
2720 if (D->getDeclContext()->containsDeclAndLoad(D))
2721 DC->addDeclInternal(ToFunction);
2722 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002723 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002724
Gabor Marton5254e642018-06-27 13:32:50 +00002725 // Friend declaration's lexical context is the befriending class, but the
2726 // semantic context is the enclosing scope of the befriending class.
2727 // We want the friend functions to be found in the semantic context by lookup.
2728 // FIXME should we handle this generically in VisitFriendDecl?
2729 // In Other cases when LexicalDC != DC we don't want it to be added,
2730 // e.g out-of-class definitions like void B::f() {} .
2731 if (LexicalDC != DC && IsFriend) {
2732 DC->makeDeclVisibleInContext(ToFunction);
2733 }
2734
2735 // Import the rest of the chain. I.e. import all subsequent declarations.
2736 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt)
2737 if (!Importer.Import(*RedeclIt))
2738 return nullptr;
2739
Lang Hames19e07e12017-06-20 21:06:00 +00002740 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2741 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2742
Douglas Gregor43f54792010-02-17 02:12:47 +00002743 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002744}
2745
Douglas Gregor00eace12010-02-21 18:29:16 +00002746Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2747 return VisitFunctionDecl(D);
2748}
2749
2750Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2751 return VisitCXXMethodDecl(D);
2752}
2753
2754Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2755 return VisitCXXMethodDecl(D);
2756}
2757
2758Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2759 return VisitCXXMethodDecl(D);
2760}
2761
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002762static unsigned getFieldIndex(Decl *F) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002763 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002764 if (!Owner)
2765 return 0;
2766
2767 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002768 for (const auto *D : Owner->noload_decls()) {
2769 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002770 return Index;
2771
2772 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2773 ++Index;
2774 }
2775
2776 return Index;
2777}
2778
Douglas Gregor5c73e912010-02-11 00:48:18 +00002779Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2780 // Import the major distinguishing characteristics of a variable.
2781 DeclContext *DC, *LexicalDC;
2782 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002783 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002784 NamedDecl *ToD;
2785 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002786 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002787 if (ToD)
2788 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002789
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002790 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002791 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002792 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002793 for (auto *FoundDecl : FoundDecls) {
2794 if (auto *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002795 // For anonymous fields, match up by index.
2796 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2797 continue;
2798
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002799 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002800 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002801 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002802 return FoundField;
2803 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002804
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002805 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2806 << Name << D->getType() << FoundField->getType();
2807 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2808 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002809 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002810 }
2811 }
2812
Douglas Gregorb4964f72010-02-15 23:54:17 +00002813 // Import the type.
2814 QualType T = Importer.Import(D->getType());
2815 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002816 return nullptr;
2817
Douglas Gregor5c73e912010-02-11 00:48:18 +00002818 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2819 Expr *BitWidth = Importer.Import(D->getBitWidth());
2820 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002821 return nullptr;
2822
Gabor Marton26f72a92018-07-12 09:42:05 +00002823 FieldDecl *ToField;
2824 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
2825 Importer.Import(D->getInnerLocStart()), Loc,
2826 Name.getAsIdentifierInfo(), T, TInfo, BitWidth,
2827 D->isMutable(), D->getInClassInitStyle()))
2828 return ToField;
2829
Douglas Gregordd483172010-02-22 17:42:47 +00002830 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002831 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00002832 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00002833 Expr *ToInitializer = Importer.Import(FromInitializer);
2834 if (ToInitializer)
2835 ToField->setInClassInitializer(ToInitializer);
2836 else
2837 return nullptr;
2838 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002839 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00002840 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002841 return ToField;
2842}
2843
Francois Pichet783dd6e2010-11-21 06:08:52 +00002844Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2845 // Import the major distinguishing characteristics of a variable.
2846 DeclContext *DC, *LexicalDC;
2847 DeclarationName Name;
2848 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002849 NamedDecl *ToD;
2850 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002851 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002852 if (ToD)
2853 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002854
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002855 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002856 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002857 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002858 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002859 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002860 // For anonymous indirect fields, match up by index.
2861 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2862 continue;
2863
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002864 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002865 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002866 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002867 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002868 return FoundField;
2869 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002870
2871 // If there are more anonymous fields to check, continue.
2872 if (!Name && I < N-1)
2873 continue;
2874
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002875 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2876 << Name << D->getType() << FoundField->getType();
2877 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2878 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002879 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002880 }
2881 }
2882
Francois Pichet783dd6e2010-11-21 06:08:52 +00002883 // Import the type.
2884 QualType T = Importer.Import(D->getType());
2885 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002886 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002887
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002888 auto **NamedChain =
2889 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00002890
2891 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00002892 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00002893 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002894 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00002895 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002896 NamedChain[i++] = cast<NamedDecl>(D);
2897 }
2898
Gabor Marton26f72a92018-07-12 09:42:05 +00002899 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
2900 IndirectFieldDecl *ToIndirectField;
2901 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
2902 Loc, Name.getAsIdentifierInfo(), T, CH))
2903 // FIXME here we leak `NamedChain` which is allocated before
2904 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00002905
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00002906 for (const auto *A : D->attrs())
2907 ToIndirectField->addAttr(Importer.Import(A));
Aaron Ballman260995b2014-10-15 16:58:18 +00002908
Francois Pichet783dd6e2010-11-21 06:08:52 +00002909 ToIndirectField->setAccess(D->getAccess());
2910 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002911 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002912 return ToIndirectField;
2913}
2914
Aleksei Sidorina693b372016-09-28 10:16:56 +00002915Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2916 // Import the major distinguishing characteristics of a declaration.
2917 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2918 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2919 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2920 if (!DC || !LexicalDC)
2921 return nullptr;
2922
2923 // Determine whether we've already imported this decl.
2924 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2925 auto *RD = cast<CXXRecordDecl>(DC);
2926 FriendDecl *ImportedFriend = RD->getFirstFriend();
2927 StructuralEquivalenceContext Context(
2928 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002929 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2930 false, false);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002931
2932 while (ImportedFriend) {
2933 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2934 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2935 ImportedFriend->getFriendDecl()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002936 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002937
2938 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2939 if (Importer.IsStructurallyEquivalent(
2940 D->getFriendType()->getType(),
2941 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00002942 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002943 }
2944 ImportedFriend = ImportedFriend->getNextFriend();
2945 }
2946
2947 // Not found. Create it.
2948 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00002949 if (NamedDecl *FriendD = D->getFriendDecl()) {
2950 auto *ToFriendD = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2951 if (ToFriendD && FriendD->getFriendObjectKind() != Decl::FOK_None &&
2952 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
2953 ToFriendD->setObjectOfFriendDecl(false);
2954
2955 ToFU = ToFriendD;
2956 } else // The friend is a type, not a decl.
Aleksei Sidorina693b372016-09-28 10:16:56 +00002957 ToFU = Importer.Import(D->getFriendType());
2958 if (!ToFU)
2959 return nullptr;
2960
2961 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002962 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002963 for (unsigned I = 0; I < D->NumTPLists; I++) {
2964 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2965 if (!List)
2966 return nullptr;
2967 ToTPLists[I] = List;
2968 }
2969
Gabor Marton26f72a92018-07-12 09:42:05 +00002970 FriendDecl *FrD;
2971 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
2972 Importer.Import(D->getLocation()), ToFU,
2973 Importer.Import(D->getFriendLoc()), ToTPLists))
2974 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002975
2976 FrD->setAccess(D->getAccess());
2977 FrD->setLexicalDeclContext(LexicalDC);
2978 LexicalDC->addDeclInternal(FrD);
2979 return FrD;
2980}
2981
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002982Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2983 // Import the major distinguishing characteristics of an ivar.
2984 DeclContext *DC, *LexicalDC;
2985 DeclarationName Name;
2986 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002987 NamedDecl *ToD;
2988 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002989 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002990 if (ToD)
2991 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002992
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002993 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002994 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002995 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002996 for (auto *FoundDecl : FoundDecls) {
2997 if (auto *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002998 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002999 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003000 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003001 return FoundIvar;
3002 }
3003
3004 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3005 << Name << D->getType() << FoundIvar->getType();
3006 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3007 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003008 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003009 }
3010 }
3011
3012 // Import the type.
3013 QualType T = Importer.Import(D->getType());
3014 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003015 return nullptr;
3016
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003017 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3018 Expr *BitWidth = Importer.Import(D->getBitWidth());
3019 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003020 return nullptr;
3021
Gabor Marton26f72a92018-07-12 09:42:05 +00003022 ObjCIvarDecl *ToIvar;
3023 if (GetImportedOrCreateDecl(
3024 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
3025 Importer.Import(D->getInnerLocStart()), Loc,
3026 Name.getAsIdentifierInfo(), T, TInfo, D->getAccessControl(), BitWidth,
3027 D->getSynthesize()))
3028 return ToIvar;
3029
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003030 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003031 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003032 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003033}
3034
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003035Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3036 // Import the major distinguishing characteristics of a variable.
3037 DeclContext *DC, *LexicalDC;
3038 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003039 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003040 NamedDecl *ToD;
3041 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003042 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003043 if (ToD)
3044 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003045
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003046 // Try to find a variable in our own ("to") context with the same name and
3047 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00003048 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00003049 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003050 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003051 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003052 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003053 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003054 for (auto *FoundDecl : FoundDecls) {
3055 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003056 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003057
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003058 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003059 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003060 if (FoundVar->hasExternalFormalLinkage() &&
3061 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003062 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00003063 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003064 MergeWithVar = FoundVar;
3065 break;
3066 }
3067
Douglas Gregor56521c52010-02-12 17:23:39 +00003068 const ArrayType *FoundArray
3069 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3070 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003071 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003072 if (FoundArray && TArray) {
3073 if (isa<IncompleteArrayType>(FoundArray) &&
3074 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003075 // Import the type.
3076 QualType T = Importer.Import(D->getType());
3077 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003078 return nullptr;
3079
Douglas Gregor56521c52010-02-12 17:23:39 +00003080 FoundVar->setType(T);
3081 MergeWithVar = FoundVar;
3082 break;
3083 } else if (isa<IncompleteArrayType>(TArray) &&
3084 isa<ConstantArrayType>(FoundArray)) {
3085 MergeWithVar = FoundVar;
3086 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003087 }
3088 }
3089
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003090 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003091 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003092 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3093 << FoundVar->getType();
3094 }
3095 }
3096
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003097 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003098 }
3099
3100 if (MergeWithVar) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003101 // An equivalent variable with external linkage has been found. Link
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003102 // the two declarations, then merge them.
Gabor Marton26f72a92018-07-12 09:42:05 +00003103 Importer.MapImported(D, MergeWithVar);
3104 updateFlags(D, MergeWithVar);
3105
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003106 if (VarDecl *DDef = D->getDefinition()) {
3107 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3108 Importer.ToDiag(ExistingDef->getLocation(),
3109 diag::err_odr_variable_multiple_def)
3110 << Name;
3111 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3112 } else {
3113 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003114 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003115 if (DDef->isInitKnownICE()) {
3116 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3117 Eval->CheckedICE = true;
3118 Eval->IsICE = DDef->isInitICE();
3119 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003120 }
3121 }
3122
3123 return MergeWithVar;
3124 }
3125
3126 if (!ConflictingDecls.empty()) {
3127 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3128 ConflictingDecls.data(),
3129 ConflictingDecls.size());
3130 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003131 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003132 }
3133 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003134
Douglas Gregorb4964f72010-02-15 23:54:17 +00003135 // Import the type.
3136 QualType T = Importer.Import(D->getType());
3137 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003138 return nullptr;
3139
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003140 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003141 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Gabor Marton26f72a92018-07-12 09:42:05 +00003142 VarDecl *ToVar;
3143 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
3144 Importer.Import(D->getInnerLocStart()), Loc,
3145 Name.getAsIdentifierInfo(), T, TInfo,
3146 D->getStorageClass()))
3147 return ToVar;
3148
Douglas Gregor14454802011-02-25 02:25:35 +00003149 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003150 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003151 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003152
3153 // Templated declarations should never appear in the enclosing DeclContext.
3154 if (!D->getDescribedVarTemplate())
3155 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003156
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003157 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003158 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00003159 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003160
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003161 if (D->isConstexpr())
3162 ToVar->setConstexpr(true);
3163
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003164 return ToVar;
3165}
3166
Douglas Gregor8b228d72010-02-17 21:22:52 +00003167Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3168 // Parameters are created in the translation unit's context, then moved
3169 // into the function declaration's context afterward.
3170 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3171
3172 // Import the name of this declaration.
3173 DeclarationName Name = Importer.Import(D->getDeclName());
3174 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003175 return nullptr;
3176
Douglas Gregor8b228d72010-02-17 21:22:52 +00003177 // Import the location of this declaration.
3178 SourceLocation Loc = Importer.Import(D->getLocation());
3179
3180 // Import the parameter's type.
3181 QualType T = Importer.Import(D->getType());
3182 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003183 return nullptr;
3184
Douglas Gregor8b228d72010-02-17 21:22:52 +00003185 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003186 ImplicitParamDecl *ToParm = nullptr;
3187 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC, Loc,
3188 Name.getAsIdentifierInfo(), T,
3189 D->getParameterKind()))
3190 return ToParm;
3191 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003192}
3193
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003194Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3195 // Parameters are created in the translation unit's context, then moved
3196 // into the function declaration's context afterward.
3197 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3198
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003199 // Import the name of this declaration.
3200 DeclarationName Name = Importer.Import(D->getDeclName());
3201 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003202 return nullptr;
3203
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003204 // Import the location of this declaration.
3205 SourceLocation Loc = Importer.Import(D->getLocation());
3206
3207 // Import the parameter's type.
3208 QualType T = Importer.Import(D->getType());
3209 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003210 return nullptr;
3211
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003212 // Create the imported parameter.
3213 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Gabor Marton26f72a92018-07-12 09:42:05 +00003214 ParmVarDecl *ToParm;
3215 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3216 Importer.Import(D->getInnerLocStart()), Loc,
3217 Name.getAsIdentifierInfo(), T, TInfo,
3218 D->getStorageClass(),
3219 /*DefaultArg*/ nullptr))
3220 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003221
3222 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003223 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003224 ToParm->setKNRPromoted(D->isKNRPromoted());
3225
3226 Expr *ToDefArg = nullptr;
3227 Expr *FromDefArg = nullptr;
3228 if (D->hasUninstantiatedDefaultArg()) {
3229 FromDefArg = D->getUninstantiatedDefaultArg();
3230 ToDefArg = Importer.Import(FromDefArg);
3231 ToParm->setUninstantiatedDefaultArg(ToDefArg);
3232 } else if (D->hasUnparsedDefaultArg()) {
3233 ToParm->setUnparsedDefaultArg();
3234 } else if (D->hasDefaultArg()) {
3235 FromDefArg = D->getDefaultArg();
3236 ToDefArg = Importer.Import(FromDefArg);
3237 ToParm->setDefaultArg(ToDefArg);
3238 }
3239 if (FromDefArg && !ToDefArg)
3240 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003241
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003242 if (D->isObjCMethodParameter()) {
3243 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3244 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3245 } else {
3246 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3247 D->getFunctionScopeIndex());
3248 }
3249
Gabor Marton26f72a92018-07-12 09:42:05 +00003250 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003251}
3252
Douglas Gregor43f54792010-02-17 02:12:47 +00003253Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3254 // Import the major distinguishing characteristics of a method.
3255 DeclContext *DC, *LexicalDC;
3256 DeclarationName Name;
3257 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003258 NamedDecl *ToD;
3259 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003260 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003261 if (ToD)
3262 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003263
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003264 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003265 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003266 for (auto *FoundDecl : FoundDecls) {
3267 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003268 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3269 continue;
3270
3271 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003272 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3273 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003274 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003275 << D->isInstanceMethod() << Name << D->getReturnType()
3276 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003277 Importer.ToDiag(FoundMethod->getLocation(),
3278 diag::note_odr_objc_method_here)
3279 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003280 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003281 }
3282
3283 // Check the number of parameters.
3284 if (D->param_size() != FoundMethod->param_size()) {
3285 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3286 << D->isInstanceMethod() << Name
3287 << D->param_size() << FoundMethod->param_size();
3288 Importer.ToDiag(FoundMethod->getLocation(),
3289 diag::note_odr_objc_method_here)
3290 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003291 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003292 }
3293
3294 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003295 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003296 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3297 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003298 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003299 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003300 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003301 diag::err_odr_objc_method_param_type_inconsistent)
3302 << D->isInstanceMethod() << Name
3303 << (*P)->getType() << (*FoundP)->getType();
3304 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3305 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003306 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003307 }
3308 }
3309
3310 // Check variadic/non-variadic.
3311 // Check the number of parameters.
3312 if (D->isVariadic() != FoundMethod->isVariadic()) {
3313 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3314 << D->isInstanceMethod() << Name;
3315 Importer.ToDiag(FoundMethod->getLocation(),
3316 diag::note_odr_objc_method_here)
3317 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003318 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003319 }
3320
3321 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003322 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003323 }
3324 }
3325
3326 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003327 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003328 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003329 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003330
Alp Toker314cc812014-01-25 16:55:45 +00003331 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003332
Gabor Marton26f72a92018-07-12 09:42:05 +00003333 ObjCMethodDecl *ToMethod;
3334 if (GetImportedOrCreateDecl(
3335 ToMethod, D, Importer.getToContext(), Loc,
3336 Importer.Import(D->getLocEnd()), Name.getObjCSelector(), ResultTy,
3337 ReturnTInfo, DC, D->isInstanceMethod(), D->isVariadic(),
3338 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3339 D->getImplementationControl(), D->hasRelatedResultType()))
3340 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003341
3342 // FIXME: When we decide to merge method definitions, we'll need to
3343 // deal with implicit parameters.
3344
3345 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003346 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003347 for (auto *FromP : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003348 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003349 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003350 return nullptr;
3351
Douglas Gregor43f54792010-02-17 02:12:47 +00003352 ToParams.push_back(ToP);
3353 }
3354
3355 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003356 for (auto *ToParam : ToParams) {
3357 ToParam->setOwningFunction(ToMethod);
3358 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003359 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003360
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003361 SmallVector<SourceLocation, 12> SelLocs;
3362 D->getSelectorLocs(SelLocs);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003363 for (auto &Loc : SelLocs)
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003364 Loc = Importer.Import(Loc);
3365
3366 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003367
3368 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003369 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003370 return ToMethod;
3371}
3372
Douglas Gregor85f3f952015-07-07 03:57:15 +00003373Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3374 // Import the major distinguishing characteristics of a category.
3375 DeclContext *DC, *LexicalDC;
3376 DeclarationName Name;
3377 SourceLocation Loc;
3378 NamedDecl *ToD;
3379 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3380 return nullptr;
3381 if (ToD)
3382 return ToD;
3383
3384 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3385 if (!BoundInfo)
3386 return nullptr;
3387
Gabor Marton26f72a92018-07-12 09:42:05 +00003388 ObjCTypeParamDecl *Result;
3389 if (GetImportedOrCreateDecl(
3390 Result, D, Importer.getToContext(), DC, D->getVariance(),
3391 Importer.Import(D->getVarianceLoc()), D->getIndex(),
3392 Importer.Import(D->getLocation()), Name.getAsIdentifierInfo(),
3393 Importer.Import(D->getColonLoc()), BoundInfo))
3394 return Result;
3395
Douglas Gregor85f3f952015-07-07 03:57:15 +00003396 Result->setLexicalDeclContext(LexicalDC);
3397 return Result;
3398}
3399
Douglas Gregor84c51c32010-02-18 01:47:50 +00003400Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3401 // Import the major distinguishing characteristics of a category.
3402 DeclContext *DC, *LexicalDC;
3403 DeclarationName Name;
3404 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003405 NamedDecl *ToD;
3406 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003407 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003408 if (ToD)
3409 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003410
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003411 auto *ToInterface =
3412 cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003413 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003414 return nullptr;
3415
Douglas Gregor84c51c32010-02-18 01:47:50 +00003416 // Determine if we've already encountered this category.
3417 ObjCCategoryDecl *MergeWithCategory
3418 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3419 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3420 if (!ToCategory) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003421
3422 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
3423 Importer.Import(D->getAtStartLoc()), Loc,
3424 Importer.Import(D->getCategoryNameLoc()),
3425 Name.getAsIdentifierInfo(), ToInterface,
3426 /*TypeParamList=*/nullptr,
3427 Importer.Import(D->getIvarLBraceLoc()),
3428 Importer.Import(D->getIvarRBraceLoc())))
3429 return ToCategory;
3430
Douglas Gregor84c51c32010-02-18 01:47:50 +00003431 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003432 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003433 // Import the type parameter list after calling Imported, to avoid
3434 // loops when bringing in their DeclContext.
3435 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3436 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003437
Douglas Gregor84c51c32010-02-18 01:47:50 +00003438 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003439 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3440 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003441 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3442 = D->protocol_loc_begin();
3443 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3444 FromProtoEnd = D->protocol_end();
3445 FromProto != FromProtoEnd;
3446 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003447 auto *ToProto =
3448 cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003449 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003450 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003451 Protocols.push_back(ToProto);
3452 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3453 }
3454
3455 // FIXME: If we're merging, make sure that the protocol list is the same.
3456 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3457 ProtocolLocs.data(), Importer.getToContext());
Douglas Gregor84c51c32010-02-18 01:47:50 +00003458 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003459 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003460 }
3461
3462 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003463 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003464
3465 // If we have an implementation, import it as well.
3466 if (D->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003467 auto *Impl =
3468 cast_or_null<ObjCCategoryImplDecl>(
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003469 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003470 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003471 return nullptr;
3472
Douglas Gregor84c51c32010-02-18 01:47:50 +00003473 ToCategory->setImplementation(Impl);
3474 }
3475
3476 return ToCategory;
3477}
3478
Douglas Gregor2aa53772012-01-24 17:42:07 +00003479bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3480 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003481 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003482 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003483 if (shouldForceImportDeclContext(Kind))
3484 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003485 return false;
3486 }
3487
3488 // Start the protocol definition
3489 To->startDefinition();
3490
3491 // Import protocols
3492 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3493 SmallVector<SourceLocation, 4> ProtocolLocs;
3494 ObjCProtocolDecl::protocol_loc_iterator
3495 FromProtoLoc = From->protocol_loc_begin();
3496 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3497 FromProtoEnd = From->protocol_end();
3498 FromProto != FromProtoEnd;
3499 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003500 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003501 if (!ToProto)
3502 return true;
3503 Protocols.push_back(ToProto);
3504 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3505 }
3506
3507 // FIXME: If we're merging, make sure that the protocol list is the same.
3508 To->setProtocolList(Protocols.data(), Protocols.size(),
3509 ProtocolLocs.data(), Importer.getToContext());
3510
Douglas Gregor2e15c842012-02-01 21:00:38 +00003511 if (shouldForceImportDeclContext(Kind)) {
3512 // Import all of the members of this protocol.
3513 ImportDeclContext(From, /*ForceImport=*/true);
3514 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003515 return false;
3516}
3517
Douglas Gregor98d156a2010-02-17 16:12:00 +00003518Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003519 // If this protocol has a definition in the translation unit we're coming
3520 // from, but this particular declaration is not that definition, import the
3521 // definition and map to that.
3522 ObjCProtocolDecl *Definition = D->getDefinition();
3523 if (Definition && Definition != D) {
3524 Decl *ImportedDef = Importer.Import(Definition);
3525 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003526 return nullptr;
3527
Gabor Marton26f72a92018-07-12 09:42:05 +00003528 return Importer.MapImported(D, ImportedDef);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003529 }
3530
Douglas Gregor84c51c32010-02-18 01:47:50 +00003531 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003532 DeclContext *DC, *LexicalDC;
3533 DeclarationName Name;
3534 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003535 NamedDecl *ToD;
3536 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003537 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003538 if (ToD)
3539 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003540
Craig Topper36250ad2014-05-12 05:36:57 +00003541 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003542 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003543 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003544 for (auto *FoundDecl : FoundDecls) {
3545 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003546 continue;
3547
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003548 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003549 break;
3550 }
3551
3552 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003553 if (!ToProto) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003554 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
3555 Name.getAsIdentifierInfo(), Loc,
3556 Importer.Import(D->getAtStartLoc()),
3557 /*PrevDecl=*/nullptr))
3558 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003559 ToProto->setLexicalDeclContext(LexicalDC);
3560 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003561 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003562
3563 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003564
Douglas Gregor2aa53772012-01-24 17:42:07 +00003565 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003566 return nullptr;
3567
Douglas Gregor98d156a2010-02-17 16:12:00 +00003568 return ToProto;
3569}
3570
Sean Callanan0aae0412014-12-10 00:00:37 +00003571Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3572 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3573 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3574
3575 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3576 SourceLocation LangLoc = Importer.Import(D->getLocation());
3577
3578 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00003579
3580 LinkageSpecDecl *ToLinkageSpec;
3581 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
3582 ExternLoc, LangLoc, D->getLanguage(), HasBraces))
3583 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00003584
3585 if (HasBraces) {
3586 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3587 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3588 }
3589
3590 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3591 LexicalDC->addDeclInternal(ToLinkageSpec);
3592
Sean Callanan0aae0412014-12-10 00:00:37 +00003593 return ToLinkageSpec;
3594}
3595
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003596Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
3597 DeclContext *DC, *LexicalDC;
3598 DeclarationName Name;
3599 SourceLocation Loc;
3600 NamedDecl *ToD = nullptr;
3601 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3602 return nullptr;
3603 if (ToD)
3604 return ToD;
3605
3606 DeclarationNameInfo NameInfo(Name,
3607 Importer.Import(D->getNameInfo().getLoc()));
3608 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3609
Gabor Marton26f72a92018-07-12 09:42:05 +00003610 UsingDecl *ToUsing;
3611 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
3612 Importer.Import(D->getUsingLoc()),
3613 Importer.Import(D->getQualifierLoc()), NameInfo,
3614 D->hasTypename()))
3615 return ToUsing;
3616
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003617 ToUsing->setLexicalDeclContext(LexicalDC);
3618 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003619
3620 if (NamedDecl *FromPattern =
3621 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003622 if (auto *ToPattern =
3623 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003624 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern);
3625 else
3626 return nullptr;
3627 }
3628
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003629 for (auto *FromShadow : D->shadows()) {
3630 if (auto *ToShadow =
3631 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003632 ToUsing->addShadowDecl(ToShadow);
3633 else
3634 // FIXME: We return a nullptr here but the definition is already created
3635 // and available with lookups. How to fix this?..
3636 return nullptr;
3637 }
3638 return ToUsing;
3639}
3640
3641Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
3642 DeclContext *DC, *LexicalDC;
3643 DeclarationName Name;
3644 SourceLocation Loc;
3645 NamedDecl *ToD = nullptr;
3646 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3647 return nullptr;
3648 if (ToD)
3649 return ToD;
3650
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003651 auto *ToUsing = dyn_cast_or_null<UsingDecl>(
3652 Importer.Import(D->getUsingDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003653 if (!ToUsing)
3654 return nullptr;
3655
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003656 auto *ToTarget = dyn_cast_or_null<NamedDecl>(
3657 Importer.Import(D->getTargetDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003658 if (!ToTarget)
3659 return nullptr;
3660
Gabor Marton26f72a92018-07-12 09:42:05 +00003661 UsingShadowDecl *ToShadow;
3662 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
3663 ToUsing, ToTarget))
3664 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003665
3666 ToShadow->setLexicalDeclContext(LexicalDC);
3667 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003668
3669 if (UsingShadowDecl *FromPattern =
3670 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003671 if (auto *ToPattern =
3672 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003673 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow,
3674 ToPattern);
3675 else
3676 // FIXME: We return a nullptr here but the definition is already created
3677 // and available with lookups. How to fix this?..
3678 return nullptr;
3679 }
3680
3681 LexicalDC->addDeclInternal(ToShadow);
3682
3683 return ToShadow;
3684}
3685
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003686Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
3687 DeclContext *DC, *LexicalDC;
3688 DeclarationName Name;
3689 SourceLocation Loc;
3690 NamedDecl *ToD = nullptr;
3691 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3692 return nullptr;
3693 if (ToD)
3694 return ToD;
3695
3696 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor());
3697 if (!ToComAncestor)
3698 return nullptr;
3699
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003700 auto *ToNominated = cast_or_null<NamespaceDecl>(
3701 Importer.Import(D->getNominatedNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003702 if (!ToNominated)
3703 return nullptr;
3704
Gabor Marton26f72a92018-07-12 09:42:05 +00003705 UsingDirectiveDecl *ToUsingDir;
3706 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
3707 Importer.Import(D->getUsingLoc()),
3708 Importer.Import(D->getNamespaceKeyLocation()),
3709 Importer.Import(D->getQualifierLoc()),
3710 Importer.Import(D->getIdentLocation()),
3711 ToNominated, ToComAncestor))
3712 return ToUsingDir;
3713
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003714 ToUsingDir->setLexicalDeclContext(LexicalDC);
3715 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003716
3717 return ToUsingDir;
3718}
3719
3720Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl(
3721 UnresolvedUsingValueDecl *D) {
3722 DeclContext *DC, *LexicalDC;
3723 DeclarationName Name;
3724 SourceLocation Loc;
3725 NamedDecl *ToD = nullptr;
3726 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3727 return nullptr;
3728 if (ToD)
3729 return ToD;
3730
3731 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc()));
3732 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3733
Gabor Marton26f72a92018-07-12 09:42:05 +00003734 UnresolvedUsingValueDecl *ToUsingValue;
3735 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
3736 Importer.Import(D->getUsingLoc()),
3737 Importer.Import(D->getQualifierLoc()), NameInfo,
3738 Importer.Import(D->getEllipsisLoc())))
3739 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003740
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003741 ToUsingValue->setAccess(D->getAccess());
3742 ToUsingValue->setLexicalDeclContext(LexicalDC);
3743 LexicalDC->addDeclInternal(ToUsingValue);
3744
3745 return ToUsingValue;
3746}
3747
3748Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
3749 UnresolvedUsingTypenameDecl *D) {
3750 DeclContext *DC, *LexicalDC;
3751 DeclarationName Name;
3752 SourceLocation Loc;
3753 NamedDecl *ToD = nullptr;
3754 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3755 return nullptr;
3756 if (ToD)
3757 return ToD;
3758
Gabor Marton26f72a92018-07-12 09:42:05 +00003759 UnresolvedUsingTypenameDecl *ToUsing;
3760 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
3761 Importer.Import(D->getUsingLoc()),
3762 Importer.Import(D->getTypenameLoc()),
3763 Importer.Import(D->getQualifierLoc()), Loc, Name,
3764 Importer.Import(D->getEllipsisLoc())))
3765 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003766
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003767 ToUsing->setAccess(D->getAccess());
3768 ToUsing->setLexicalDeclContext(LexicalDC);
3769 LexicalDC->addDeclInternal(ToUsing);
3770
3771 return ToUsing;
3772}
3773
Douglas Gregor2aa53772012-01-24 17:42:07 +00003774bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3775 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003776 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003777 if (To->getDefinition()) {
3778 // Check consistency of superclass.
3779 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3780 if (FromSuper) {
3781 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3782 if (!FromSuper)
3783 return true;
3784 }
3785
3786 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3787 if ((bool)FromSuper != (bool)ToSuper ||
3788 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3789 Importer.ToDiag(To->getLocation(),
3790 diag::err_odr_objc_superclass_inconsistent)
3791 << To->getDeclName();
3792 if (ToSuper)
3793 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3794 << To->getSuperClass()->getDeclName();
3795 else
3796 Importer.ToDiag(To->getLocation(),
3797 diag::note_odr_objc_missing_superclass);
3798 if (From->getSuperClass())
3799 Importer.FromDiag(From->getSuperClassLoc(),
3800 diag::note_odr_objc_superclass)
3801 << From->getSuperClass()->getDeclName();
3802 else
3803 Importer.FromDiag(From->getLocation(),
3804 diag::note_odr_objc_missing_superclass);
3805 }
3806
Douglas Gregor2e15c842012-02-01 21:00:38 +00003807 if (shouldForceImportDeclContext(Kind))
3808 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003809 return false;
3810 }
3811
3812 // Start the definition.
3813 To->startDefinition();
3814
3815 // If this class has a superclass, import it.
3816 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003817 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3818 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003819 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003820
3821 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003822 }
3823
3824 // Import protocols
3825 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3826 SmallVector<SourceLocation, 4> ProtocolLocs;
3827 ObjCInterfaceDecl::protocol_loc_iterator
3828 FromProtoLoc = From->protocol_loc_begin();
3829
3830 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3831 FromProtoEnd = From->protocol_end();
3832 FromProto != FromProtoEnd;
3833 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003834 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003835 if (!ToProto)
3836 return true;
3837 Protocols.push_back(ToProto);
3838 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3839 }
3840
3841 // FIXME: If we're merging, make sure that the protocol list is the same.
3842 To->setProtocolList(Protocols.data(), Protocols.size(),
3843 ProtocolLocs.data(), Importer.getToContext());
3844
3845 // Import categories. When the categories themselves are imported, they'll
3846 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003847 for (auto *Cat : From->known_categories())
3848 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003849
Douglas Gregor2aa53772012-01-24 17:42:07 +00003850 // If we have an @implementation, import it as well.
3851 if (From->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003852 auto *Impl = cast_or_null<ObjCImplementationDecl>(
3853 Importer.Import(From->getImplementation()));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003854 if (!Impl)
3855 return true;
3856
3857 To->setImplementation(Impl);
3858 }
3859
Douglas Gregor2e15c842012-02-01 21:00:38 +00003860 if (shouldForceImportDeclContext(Kind)) {
3861 // Import all of the members of this class.
3862 ImportDeclContext(From, /*ForceImport=*/true);
3863 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003864 return false;
3865}
3866
Douglas Gregor85f3f952015-07-07 03:57:15 +00003867ObjCTypeParamList *
3868ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3869 if (!list)
3870 return nullptr;
3871
3872 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3873 for (auto fromTypeParam : *list) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003874 auto *toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3875 Importer.Import(fromTypeParam));
Douglas Gregor85f3f952015-07-07 03:57:15 +00003876 if (!toTypeParam)
3877 return nullptr;
3878
3879 toTypeParams.push_back(toTypeParam);
3880 }
3881
3882 return ObjCTypeParamList::create(Importer.getToContext(),
3883 Importer.Import(list->getLAngleLoc()),
3884 toTypeParams,
3885 Importer.Import(list->getRAngleLoc()));
3886}
3887
Douglas Gregor45635322010-02-16 01:20:57 +00003888Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003889 // If this class has a definition in the translation unit we're coming from,
3890 // but this particular declaration is not that definition, import the
3891 // definition and map to that.
3892 ObjCInterfaceDecl *Definition = D->getDefinition();
3893 if (Definition && Definition != D) {
3894 Decl *ImportedDef = Importer.Import(Definition);
3895 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003896 return nullptr;
3897
Gabor Marton26f72a92018-07-12 09:42:05 +00003898 return Importer.MapImported(D, ImportedDef);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003899 }
3900
Douglas Gregor45635322010-02-16 01:20:57 +00003901 // Import the major distinguishing characteristics of an @interface.
3902 DeclContext *DC, *LexicalDC;
3903 DeclarationName Name;
3904 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003905 NamedDecl *ToD;
3906 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003907 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003908 if (ToD)
3909 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003910
Douglas Gregor2aa53772012-01-24 17:42:07 +00003911 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003912 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003913 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003914 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003915 for (auto *FoundDecl : FoundDecls) {
3916 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003917 continue;
3918
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003919 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00003920 break;
3921 }
3922
Douglas Gregor2aa53772012-01-24 17:42:07 +00003923 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003924 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003925 if (!ToIface) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003926 if (GetImportedOrCreateDecl(
3927 ToIface, D, Importer.getToContext(), DC,
3928 Importer.Import(D->getAtStartLoc()), Name.getAsIdentifierInfo(),
3929 /*TypeParamList=*/nullptr,
3930 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
3931 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003932 ToIface->setLexicalDeclContext(LexicalDC);
3933 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003934 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003935 Importer.MapImported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003936 // Import the type parameter list after calling Imported, to avoid
3937 // loops when bringing in their DeclContext.
3938 ToIface->setTypeParamList(ImportObjCTypeParamList(
3939 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003940
Douglas Gregor2aa53772012-01-24 17:42:07 +00003941 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003942 return nullptr;
3943
Douglas Gregor98d156a2010-02-17 16:12:00 +00003944 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003945}
3946
Douglas Gregor4da9d682010-12-07 15:32:12 +00003947Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003948 auto *Category = cast_or_null<ObjCCategoryDecl>(
3949 Importer.Import(D->getCategoryDecl()));
Douglas Gregor4da9d682010-12-07 15:32:12 +00003950 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003951 return nullptr;
3952
Douglas Gregor4da9d682010-12-07 15:32:12 +00003953 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3954 if (!ToImpl) {
3955 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3956 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003957 return nullptr;
3958
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003959 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Gabor Marton26f72a92018-07-12 09:42:05 +00003960 if (GetImportedOrCreateDecl(
3961 ToImpl, D, Importer.getToContext(), DC,
3962 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
3963 Importer.Import(D->getLocation()),
3964 Importer.Import(D->getAtStartLoc()), CategoryNameLoc))
3965 return ToImpl;
3966
Douglas Gregor4da9d682010-12-07 15:32:12 +00003967 DeclContext *LexicalDC = DC;
3968 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3969 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3970 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003971 return nullptr;
3972
Douglas Gregor4da9d682010-12-07 15:32:12 +00003973 ToImpl->setLexicalDeclContext(LexicalDC);
3974 }
3975
Sean Callanan95e74be2011-10-21 02:57:43 +00003976 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003977 Category->setImplementation(ToImpl);
3978 }
3979
Gabor Marton26f72a92018-07-12 09:42:05 +00003980 Importer.MapImported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003981 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003982 return ToImpl;
3983}
3984
Douglas Gregorda8025c2010-12-07 01:26:03 +00003985Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3986 // Find the corresponding interface.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003987 auto *Iface = cast_or_null<ObjCInterfaceDecl>(
3988 Importer.Import(D->getClassInterface()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003989 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003990 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003991
3992 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003993 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003994 if (D->getSuperClass()) {
3995 Super = cast_or_null<ObjCInterfaceDecl>(
3996 Importer.Import(D->getSuperClass()));
3997 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003998 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003999 }
4000
4001 ObjCImplementationDecl *Impl = Iface->getImplementation();
4002 if (!Impl) {
4003 // We haven't imported an implementation yet. Create a new @implementation
4004 // now.
Gabor Marton26f72a92018-07-12 09:42:05 +00004005 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
4006 Importer.ImportContext(D->getDeclContext()),
4007 Iface, Super, Importer.Import(D->getLocation()),
4008 Importer.Import(D->getAtStartLoc()),
4009 Importer.Import(D->getSuperClassLoc()),
4010 Importer.Import(D->getIvarLBraceLoc()),
4011 Importer.Import(D->getIvarRBraceLoc())))
4012 return Impl;
4013
Douglas Gregorda8025c2010-12-07 01:26:03 +00004014 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4015 DeclContext *LexicalDC
4016 = Importer.ImportContext(D->getLexicalDeclContext());
4017 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004018 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004019 Impl->setLexicalDeclContext(LexicalDC);
4020 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004021
Douglas Gregorda8025c2010-12-07 01:26:03 +00004022 // Associate the implementation with the class it implements.
4023 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004024 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004025 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004026 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004027
4028 // Verify that the existing @implementation has the same superclass.
4029 if ((Super && !Impl->getSuperClass()) ||
4030 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004031 (Super && Impl->getSuperClass() &&
4032 !declaresSameEntity(Super->getCanonicalDecl(),
4033 Impl->getSuperClass()))) {
4034 Importer.ToDiag(Impl->getLocation(),
4035 diag::err_odr_objc_superclass_inconsistent)
4036 << Iface->getDeclName();
4037 // FIXME: It would be nice to have the location of the superclass
4038 // below.
4039 if (Impl->getSuperClass())
4040 Importer.ToDiag(Impl->getLocation(),
4041 diag::note_odr_objc_superclass)
4042 << Impl->getSuperClass()->getDeclName();
4043 else
4044 Importer.ToDiag(Impl->getLocation(),
4045 diag::note_odr_objc_missing_superclass);
4046 if (D->getSuperClass())
4047 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004048 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004049 << D->getSuperClass()->getDeclName();
4050 else
4051 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004052 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00004053 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004054 }
4055 }
4056
4057 // Import all of the members of this @implementation.
4058 ImportDeclContext(D);
4059
4060 return Impl;
4061}
4062
Douglas Gregora11c4582010-02-17 18:02:10 +00004063Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
4064 // Import the major distinguishing characteristics of an @property.
4065 DeclContext *DC, *LexicalDC;
4066 DeclarationName Name;
4067 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004068 NamedDecl *ToD;
4069 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004070 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004071 if (ToD)
4072 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004073
4074 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004075 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004076 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004077 for (auto *FoundDecl : FoundDecls) {
4078 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004079 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004080 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004081 FoundProp->getType())) {
4082 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4083 << Name << D->getType() << FoundProp->getType();
4084 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4085 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00004086 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004087 }
4088
4089 // FIXME: Check property attributes, getters, setters, etc.?
4090
4091 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004092 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004093 return FoundProp;
4094 }
4095 }
4096
4097 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00004098 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
4099 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00004100 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004101
4102 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004103 ObjCPropertyDecl *ToProperty;
4104 if (GetImportedOrCreateDecl(
4105 ToProperty, D, Importer.getToContext(), DC, Loc,
4106 Name.getAsIdentifierInfo(), Importer.Import(D->getAtLoc()),
4107 Importer.Import(D->getLParenLoc()), Importer.Import(D->getType()),
4108 TSI, D->getPropertyImplementation()))
4109 return ToProperty;
4110
Douglas Gregora11c4582010-02-17 18:02:10 +00004111 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004112 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004113
4114 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004115 ToProperty->setPropertyAttributesAsWritten(
4116 D->getPropertyAttributesAsWritten());
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +00004117 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
4118 Importer.Import(D->getGetterNameLoc()));
4119 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
4120 Importer.Import(D->getSetterNameLoc()));
Douglas Gregora11c4582010-02-17 18:02:10 +00004121 ToProperty->setGetterMethodDecl(
4122 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
4123 ToProperty->setSetterMethodDecl(
4124 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
4125 ToProperty->setPropertyIvarDecl(
4126 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
4127 return ToProperty;
4128}
4129
Douglas Gregor14a49e22010-12-07 18:32:03 +00004130Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004131 auto *Property = cast_or_null<ObjCPropertyDecl>(
4132 Importer.Import(D->getPropertyDecl()));
Douglas Gregor14a49e22010-12-07 18:32:03 +00004133 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00004134 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004135
4136 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4137 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004138 return nullptr;
4139
Douglas Gregor14a49e22010-12-07 18:32:03 +00004140 // Import the lexical declaration context.
4141 DeclContext *LexicalDC = DC;
4142 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4143 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4144 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004145 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004146 }
4147
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004148 auto *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004149 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00004150 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004151
4152 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004153 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004154 if (D->getPropertyIvarDecl()) {
4155 Ivar = cast_or_null<ObjCIvarDecl>(
4156 Importer.Import(D->getPropertyIvarDecl()));
4157 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00004158 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004159 }
4160
4161 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004162 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4163 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004164 if (!ToImpl) {
4165 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
4166 Importer.Import(D->getLocStart()),
4167 Importer.Import(D->getLocation()), Property,
4168 D->getPropertyImplementation(), Ivar,
4169 Importer.Import(D->getPropertyIvarDeclLoc())))
4170 return ToImpl;
4171
Douglas Gregor14a49e22010-12-07 18:32:03 +00004172 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004173 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004174 } else {
4175 // Check that we have the same kind of property implementation (@synthesize
4176 // vs. @dynamic).
4177 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4178 Importer.ToDiag(ToImpl->getLocation(),
4179 diag::err_odr_objc_property_impl_kind_inconsistent)
4180 << Property->getDeclName()
4181 << (ToImpl->getPropertyImplementation()
4182 == ObjCPropertyImplDecl::Dynamic);
4183 Importer.FromDiag(D->getLocation(),
4184 diag::note_odr_objc_property_impl_kind)
4185 << D->getPropertyDecl()->getDeclName()
4186 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004187 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004188 }
4189
4190 // For @synthesize, check that we have the same
4191 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4192 Ivar != ToImpl->getPropertyIvarDecl()) {
4193 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4194 diag::err_odr_objc_synthesize_ivar_inconsistent)
4195 << Property->getDeclName()
4196 << ToImpl->getPropertyIvarDecl()->getDeclName()
4197 << Ivar->getDeclName();
4198 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4199 diag::note_odr_objc_synthesize_ivar_here)
4200 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004201 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004202 }
4203
4204 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004205 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004206 }
4207
4208 return ToImpl;
4209}
4210
Douglas Gregora082a492010-11-30 19:14:50 +00004211Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4212 // For template arguments, we adopt the translation unit as our declaration
4213 // context. This context will be fixed when the actual template declaration
4214 // is created.
4215
4216 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004217 TemplateTypeParmDecl *ToD = nullptr;
4218 (void)GetImportedOrCreateDecl(
4219 ToD, D, Importer.getToContext(),
4220 Importer.getToContext().getTranslationUnitDecl(),
4221 Importer.Import(D->getLocStart()), Importer.Import(D->getLocation()),
4222 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4223 D->wasDeclaredWithTypename(), D->isParameterPack());
4224 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004225}
4226
4227Decl *
4228ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4229 // Import the name of this declaration.
4230 DeclarationName Name = Importer.Import(D->getDeclName());
4231 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004232 return nullptr;
4233
Douglas Gregora082a492010-11-30 19:14:50 +00004234 // Import the location of this declaration.
4235 SourceLocation Loc = Importer.Import(D->getLocation());
4236
4237 // Import the type of this declaration.
4238 QualType T = Importer.Import(D->getType());
4239 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004240 return nullptr;
4241
Douglas Gregora082a492010-11-30 19:14:50 +00004242 // Import type-source information.
4243 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4244 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004245 return nullptr;
4246
Douglas Gregora082a492010-11-30 19:14:50 +00004247 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004248
4249 NonTypeTemplateParmDecl *ToD = nullptr;
4250 (void)GetImportedOrCreateDecl(
4251 ToD, D, Importer.getToContext(),
4252 Importer.getToContext().getTranslationUnitDecl(),
4253 Importer.Import(D->getInnerLocStart()), Loc, D->getDepth(),
4254 D->getPosition(), Name.getAsIdentifierInfo(), T, D->isParameterPack(),
4255 TInfo);
4256 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004257}
4258
4259Decl *
4260ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4261 // Import the name of this declaration.
4262 DeclarationName Name = Importer.Import(D->getDeclName());
4263 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004264 return nullptr;
4265
Douglas Gregora082a492010-11-30 19:14:50 +00004266 // Import the location of this declaration.
4267 SourceLocation Loc = Importer.Import(D->getLocation());
Gabor Marton26f72a92018-07-12 09:42:05 +00004268
Douglas Gregora082a492010-11-30 19:14:50 +00004269 // Import template parameters.
4270 TemplateParameterList *TemplateParams
4271 = ImportTemplateParameterList(D->getTemplateParameters());
4272 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004273 return nullptr;
4274
Douglas Gregora082a492010-11-30 19:14:50 +00004275 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004276
4277 TemplateTemplateParmDecl *ToD = nullptr;
4278 (void)GetImportedOrCreateDecl(
4279 ToD, D, Importer.getToContext(),
4280 Importer.getToContext().getTranslationUnitDecl(), Loc, D->getDepth(),
4281 D->getPosition(), D->isParameterPack(), Name.getAsIdentifierInfo(),
4282 TemplateParams);
4283 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004284}
4285
Gabor Marton9581c332018-05-23 13:53:36 +00004286// Returns the definition for a (forward) declaration of a ClassTemplateDecl, if
4287// it has any definition in the redecl chain.
4288static ClassTemplateDecl *getDefinition(ClassTemplateDecl *D) {
4289 CXXRecordDecl *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
4290 if (!ToTemplatedDef)
4291 return nullptr;
4292 ClassTemplateDecl *TemplateWithDef =
4293 ToTemplatedDef->getDescribedClassTemplate();
4294 return TemplateWithDef;
4295}
4296
Douglas Gregora082a492010-11-30 19:14:50 +00004297Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4298 // If this record has a definition in the translation unit we're coming from,
4299 // but this particular declaration is not that definition, import the
4300 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004301 auto *Definition =
4302 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
Douglas Gregora082a492010-11-30 19:14:50 +00004303 if (Definition && Definition != D->getTemplatedDecl()) {
4304 Decl *ImportedDef
4305 = Importer.Import(Definition->getDescribedClassTemplate());
4306 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004307 return nullptr;
4308
Gabor Marton26f72a92018-07-12 09:42:05 +00004309 return Importer.MapImported(D, ImportedDef);
Douglas Gregora082a492010-11-30 19:14:50 +00004310 }
Gabor Marton9581c332018-05-23 13:53:36 +00004311
Douglas Gregora082a492010-11-30 19:14:50 +00004312 // Import the major distinguishing characteristics of this class template.
4313 DeclContext *DC, *LexicalDC;
4314 DeclarationName Name;
4315 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004316 NamedDecl *ToD;
4317 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004318 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004319 if (ToD)
4320 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004321
Douglas Gregora082a492010-11-30 19:14:50 +00004322 // We may already have a template of the same name; try to find and match it.
4323 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004324 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004325 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004326 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004327 for (auto *FoundDecl : FoundDecls) {
4328 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004329 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00004330
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004331 Decl *Found = FoundDecl;
4332 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Marton9581c332018-05-23 13:53:36 +00004333
4334 // The class to be imported is a definition.
4335 if (D->isThisDeclarationADefinition()) {
4336 // Lookup will find the fwd decl only if that is more recent than the
4337 // definition. So, try to get the definition if that is available in
4338 // the redecl chain.
4339 ClassTemplateDecl *TemplateWithDef = getDefinition(FoundTemplate);
4340 if (!TemplateWithDef)
4341 continue;
4342 FoundTemplate = TemplateWithDef; // Continue with the definition.
4343 }
4344
Douglas Gregora082a492010-11-30 19:14:50 +00004345 if (IsStructuralMatch(D, FoundTemplate)) {
4346 // The class templates structurally match; call it the same template.
Aleksei Sidorin761c2242018-05-15 11:09:07 +00004347
Gabor Marton26f72a92018-07-12 09:42:05 +00004348 Importer.MapImported(D->getTemplatedDecl(),
4349 FoundTemplate->getTemplatedDecl());
4350 return Importer.MapImported(D, FoundTemplate);
Gabor Marton9581c332018-05-23 13:53:36 +00004351 }
Douglas Gregora082a492010-11-30 19:14:50 +00004352 }
Gabor Marton9581c332018-05-23 13:53:36 +00004353
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004354 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00004355 }
Gabor Marton9581c332018-05-23 13:53:36 +00004356
Douglas Gregora082a492010-11-30 19:14:50 +00004357 if (!ConflictingDecls.empty()) {
4358 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00004359 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00004360 ConflictingDecls.size());
4361 }
Gabor Marton9581c332018-05-23 13:53:36 +00004362
Douglas Gregora082a492010-11-30 19:14:50 +00004363 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004364 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004365 }
4366
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004367 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4368
Douglas Gregora082a492010-11-30 19:14:50 +00004369 // Create the declaration that is being templated.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004370 auto *ToTemplated = cast_or_null<CXXRecordDecl>(
4371 Importer.Import(FromTemplated));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004372 if (!ToTemplated)
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004373 return nullptr;
4374
Douglas Gregora082a492010-11-30 19:14:50 +00004375 // Create the class template declaration itself.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004376 TemplateParameterList *TemplateParams =
4377 ImportTemplateParameterList(D->getTemplateParameters());
Douglas Gregora082a492010-11-30 19:14:50 +00004378 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004379 return nullptr;
4380
Gabor Marton26f72a92018-07-12 09:42:05 +00004381 ClassTemplateDecl *D2;
4382 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
4383 TemplateParams, ToTemplated))
4384 return D2;
4385
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004386 ToTemplated->setDescribedClassTemplate(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004387
4388 D2->setAccess(D->getAccess());
4389 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004390 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004391
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004392 if (FromTemplated->isCompleteDefinition() &&
4393 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004394 // FIXME: Import definition!
4395 }
4396
4397 return D2;
4398}
4399
Douglas Gregore2e50d332010-12-01 01:36:18 +00004400Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4401 ClassTemplateSpecializationDecl *D) {
4402 // If this record has a definition in the translation unit we're coming from,
4403 // but this particular declaration is not that definition, import the
4404 // definition and map to that.
4405 TagDecl *Definition = D->getDefinition();
4406 if (Definition && Definition != D) {
4407 Decl *ImportedDef = Importer.Import(Definition);
4408 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004409 return nullptr;
4410
Gabor Marton26f72a92018-07-12 09:42:05 +00004411 return Importer.MapImported(D, ImportedDef);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004412 }
4413
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004414 auto *ClassTemplate =
4415 cast_or_null<ClassTemplateDecl>(Importer.Import(
Douglas Gregore2e50d332010-12-01 01:36:18 +00004416 D->getSpecializedTemplate()));
4417 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004418 return nullptr;
4419
Douglas Gregore2e50d332010-12-01 01:36:18 +00004420 // Import the context of this declaration.
4421 DeclContext *DC = ClassTemplate->getDeclContext();
4422 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004423 return nullptr;
4424
Douglas Gregore2e50d332010-12-01 01:36:18 +00004425 DeclContext *LexicalDC = DC;
4426 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4427 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4428 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004429 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004430 }
4431
4432 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004433 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4434 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004435
4436 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004437 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004438 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4439 D->getTemplateArgs().size(),
4440 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004441 return nullptr;
4442
Douglas Gregore2e50d332010-12-01 01:36:18 +00004443 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004444 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004445 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004446 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004447 if (D2) {
4448 // We already have a class template specialization with these template
4449 // arguments.
4450
4451 // FIXME: Check for specialization vs. instantiation errors.
4452
4453 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004454 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004455 // The record types structurally match, or the "from" translation
4456 // unit only had a forward declaration anyway; call it the same
4457 // function.
Gabor Marton26f72a92018-07-12 09:42:05 +00004458 return Importer.MapImported(D, FoundDef);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004459 }
4460 }
4461 } else {
4462 // Create a new specialization.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004463 if (auto *PartialSpec =
4464 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004465 // Import TemplateArgumentListInfo
4466 TemplateArgumentListInfo ToTAInfo;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004467 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
4468 if (ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004469 return nullptr;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004470
4471 QualType CanonInjType = Importer.Import(
4472 PartialSpec->getInjectedSpecializationType());
4473 if (CanonInjType.isNull())
4474 return nullptr;
4475 CanonInjType = CanonInjType.getCanonicalType();
4476
4477 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4478 PartialSpec->getTemplateParameters());
4479 if (!ToTPList && PartialSpec->getTemplateParameters())
4480 return nullptr;
4481
Gabor Marton26f72a92018-07-12 09:42:05 +00004482 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
4483 D2, D, Importer.getToContext(), D->getTagKind(), DC, StartLoc,
4484 IdLoc, ToTPList, ClassTemplate,
4485 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
4486 ToTAInfo, CanonInjType, nullptr))
4487 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004488
4489 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004490 if (GetImportedOrCreateDecl(
4491 D2, D, Importer.getToContext(), D->getTagKind(), DC, StartLoc,
4492 IdLoc, ClassTemplate, TemplateArgs, /*PrevDecl=*/nullptr))
4493 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004494 }
4495
Douglas Gregore2e50d332010-12-01 01:36:18 +00004496 D2->setSpecializationKind(D->getSpecializationKind());
4497
4498 // Add this specialization to the class template.
4499 ClassTemplate->AddSpecialization(D2, InsertPos);
4500
4501 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004502 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004503
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004504 if (auto *TSI = D->getTypeAsWritten()) {
4505 TypeSourceInfo *TInfo = Importer.Import(TSI);
4506 if (!TInfo)
4507 return nullptr;
4508 D2->setTypeAsWritten(TInfo);
4509 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
4510 D2->setExternLoc(Importer.Import(D->getExternLoc()));
4511 }
4512
4513 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
4514 if (POI.isValid())
4515 D2->setPointOfInstantiation(POI);
4516 else if (D->getPointOfInstantiation().isValid())
4517 return nullptr;
4518
4519 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
4520
Gabor Martonb14056b2018-05-25 11:21:24 +00004521 // Set the context of this specialization/instantiation.
Douglas Gregore2e50d332010-12-01 01:36:18 +00004522 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonb14056b2018-05-25 11:21:24 +00004523
4524 // Add to the DC only if it was an explicit specialization/instantiation.
4525 if (D2->isExplicitInstantiationOrSpecialization()) {
4526 LexicalDC->addDeclInternal(D2);
4527 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004528 }
John McCallf937c022011-10-07 06:10:15 +00004529 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004530 return nullptr;
4531
Douglas Gregore2e50d332010-12-01 01:36:18 +00004532 return D2;
4533}
4534
Larisse Voufo39a1e502013-08-06 01:03:05 +00004535Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4536 // If this variable has a definition in the translation unit we're coming
4537 // from,
4538 // but this particular declaration is not that definition, import the
4539 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004540 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00004541 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4542 if (Definition && Definition != D->getTemplatedDecl()) {
4543 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4544 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004545 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004546
Gabor Marton26f72a92018-07-12 09:42:05 +00004547 return Importer.MapImported(D, ImportedDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004548 }
4549
4550 // Import the major distinguishing characteristics of this variable template.
4551 DeclContext *DC, *LexicalDC;
4552 DeclarationName Name;
4553 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004554 NamedDecl *ToD;
4555 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004556 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004557 if (ToD)
4558 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004559
4560 // We may already have a template of the same name; try to find and match it.
4561 assert(!DC->isFunctionOrMethod() &&
4562 "Variable templates cannot be declared at function scope");
4563 SmallVector<NamedDecl *, 4> ConflictingDecls;
4564 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004565 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004566 for (auto *FoundDecl : FoundDecls) {
4567 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00004568 continue;
4569
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004570 Decl *Found = FoundDecl;
4571 if (auto *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004572 if (IsStructuralMatch(D, FoundTemplate)) {
4573 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00004574 Importer.MapImported(D->getTemplatedDecl(),
4575 FoundTemplate->getTemplatedDecl());
4576 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004577 }
4578 }
4579
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004580 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004581 }
4582
4583 if (!ConflictingDecls.empty()) {
4584 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4585 ConflictingDecls.data(),
4586 ConflictingDecls.size());
4587 }
4588
4589 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004590 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004591
4592 VarDecl *DTemplated = D->getTemplatedDecl();
4593
4594 // Import the type.
4595 QualType T = Importer.Import(DTemplated->getType());
4596 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004597 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004598
4599 // Create the declaration that is being templated.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004600 auto *ToTemplated = dyn_cast_or_null<VarDecl>(Importer.Import(DTemplated));
4601 if (!ToTemplated)
Craig Topper36250ad2014-05-12 05:36:57 +00004602 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004603
4604 // Create the variable template declaration itself.
4605 TemplateParameterList *TemplateParams =
4606 ImportTemplateParameterList(D->getTemplateParameters());
4607 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004608 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004609
Gabor Marton26f72a92018-07-12 09:42:05 +00004610 VarTemplateDecl *ToVarTD;
4611 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
4612 Name, TemplateParams, ToTemplated))
4613 return ToVarTD;
4614
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004615 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004616
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004617 ToVarTD->setAccess(D->getAccess());
4618 ToVarTD->setLexicalDeclContext(LexicalDC);
4619 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004620
Larisse Voufo39a1e502013-08-06 01:03:05 +00004621 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004622 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004623 // FIXME: Import definition!
4624 }
4625
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004626 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004627}
4628
4629Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4630 VarTemplateSpecializationDecl *D) {
4631 // If this record has a definition in the translation unit we're coming from,
4632 // but this particular declaration is not that definition, import the
4633 // definition and map to that.
4634 VarDecl *Definition = D->getDefinition();
4635 if (Definition && Definition != D) {
4636 Decl *ImportedDef = Importer.Import(Definition);
4637 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004638 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004639
Gabor Marton26f72a92018-07-12 09:42:05 +00004640 return Importer.MapImported(D, ImportedDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004641 }
4642
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004643 auto *VarTemplate = cast_or_null<VarTemplateDecl>(
Larisse Voufo39a1e502013-08-06 01:03:05 +00004644 Importer.Import(D->getSpecializedTemplate()));
4645 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004646 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004647
4648 // Import the context of this declaration.
4649 DeclContext *DC = VarTemplate->getDeclContext();
4650 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004651 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004652
4653 DeclContext *LexicalDC = DC;
4654 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4655 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4656 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004657 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004658 }
4659
4660 // Import the location of this declaration.
4661 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4662 SourceLocation IdLoc = Importer.Import(D->getLocation());
4663
4664 // Import template arguments.
4665 SmallVector<TemplateArgument, 2> TemplateArgs;
4666 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4667 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004668 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004669
4670 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004671 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004672 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004673 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004674 if (D2) {
4675 // We already have a variable template specialization with these template
4676 // arguments.
4677
4678 // FIXME: Check for specialization vs. instantiation errors.
4679
4680 if (VarDecl *FoundDef = D2->getDefinition()) {
4681 if (!D->isThisDeclarationADefinition() ||
4682 IsStructuralMatch(D, FoundDef)) {
4683 // The record types structurally match, or the "from" translation
4684 // unit only had a forward declaration anyway; call it the same
4685 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00004686 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004687 }
4688 }
4689 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004690 // Import the type.
4691 QualType T = Importer.Import(D->getType());
4692 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004693 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004694
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004695 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4696 if (D->getTypeSourceInfo() && !TInfo)
4697 return nullptr;
4698
4699 TemplateArgumentListInfo ToTAInfo;
4700 if (ImportTemplateArgumentListInfo(D->getTemplateArgsInfo(), ToTAInfo))
4701 return nullptr;
4702
4703 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004704 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004705 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
4706 // Import TemplateArgumentListInfo
4707 TemplateArgumentListInfo ArgInfos;
4708 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
4709 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
4710 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ArgInfos))
4711 return nullptr;
4712
4713 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4714 FromPartial->getTemplateParameters());
4715 if (!ToTPList)
4716 return nullptr;
4717
Gabor Marton26f72a92018-07-12 09:42:05 +00004718 PartVarSpecDecl *ToPartial;
4719 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
4720 StartLoc, IdLoc, ToTPList, VarTemplate, T,
4721 TInfo, D->getStorageClass(), TemplateArgs,
4722 ArgInfos))
4723 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004724
4725 auto *FromInst = FromPartial->getInstantiatedFromMember();
4726 auto *ToInst = cast_or_null<PartVarSpecDecl>(Importer.Import(FromInst));
4727 if (FromInst && !ToInst)
4728 return nullptr;
4729
4730 ToPartial->setInstantiatedFromMember(ToInst);
4731 if (FromPartial->isMemberSpecialization())
4732 ToPartial->setMemberSpecialization();
4733
4734 D2 = ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004735 } else { // Full specialization
Gabor Marton26f72a92018-07-12 09:42:05 +00004736 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, StartLoc,
4737 IdLoc, VarTemplate, T, TInfo,
4738 D->getStorageClass(), TemplateArgs))
4739 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004740 }
4741
4742 SourceLocation POI = D->getPointOfInstantiation();
4743 if (POI.isValid())
4744 D2->setPointOfInstantiation(Importer.Import(POI));
4745
Larisse Voufo39a1e502013-08-06 01:03:05 +00004746 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004747 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004748
4749 // Add this specialization to the class template.
4750 VarTemplate->AddSpecialization(D2, InsertPos);
4751
4752 // Import the qualifier, if any.
4753 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4754
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004755 if (D->isConstexpr())
4756 D2->setConstexpr(true);
4757
Larisse Voufo39a1e502013-08-06 01:03:05 +00004758 // Add the specialization to this context.
4759 D2->setLexicalDeclContext(LexicalDC);
4760 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004761
4762 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00004763 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004764
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004765 // NOTE: isThisDeclarationADefinition() can return DeclarationOnly even if
4766 // declaration has initializer. Should this be fixed in the AST?.. Anyway,
4767 // we have to check the declaration for initializer - otherwise, it won't be
4768 // imported.
4769 if ((D->isThisDeclarationADefinition() || D->hasInit()) &&
4770 ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004771 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004772
4773 return D2;
4774}
4775
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004776Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
4777 DeclContext *DC, *LexicalDC;
4778 DeclarationName Name;
4779 SourceLocation Loc;
4780 NamedDecl *ToD;
4781
4782 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4783 return nullptr;
4784
4785 if (ToD)
4786 return ToD;
4787
4788 // Try to find a function in our own ("to") context with the same name, same
4789 // type, and in the same context as the function we're importing.
4790 if (!LexicalDC->isFunctionOrMethod()) {
4791 unsigned IDNS = Decl::IDNS_Ordinary;
4792 SmallVector<NamedDecl *, 2> FoundDecls;
4793 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004794 for (auto *FoundDecl : FoundDecls) {
4795 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004796 continue;
4797
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004798 if (auto *FoundFunction = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004799 if (FoundFunction->hasExternalFormalLinkage() &&
4800 D->hasExternalFormalLinkage()) {
4801 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00004802 Importer.MapImported(D, FoundFunction);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004803 // FIXME: Actually try to merge the body and other attributes.
4804 return FoundFunction;
4805 }
4806 }
4807 }
4808 }
4809 }
4810
4811 TemplateParameterList *Params =
4812 ImportTemplateParameterList(D->getTemplateParameters());
4813 if (!Params)
4814 return nullptr;
4815
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004816 auto *TemplatedFD =
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004817 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl()));
4818 if (!TemplatedFD)
4819 return nullptr;
4820
Gabor Marton26f72a92018-07-12 09:42:05 +00004821 FunctionTemplateDecl *ToFunc;
4822 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
4823 Params, TemplatedFD))
4824 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004825
4826 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
4827 ToFunc->setAccess(D->getAccess());
4828 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004829
4830 LexicalDC->addDeclInternal(ToFunc);
4831 return ToFunc;
4832}
4833
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004834//----------------------------------------------------------------------------
4835// Import Statements
4836//----------------------------------------------------------------------------
4837
Sean Callanan59721b32015-04-28 18:41:46 +00004838DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4839 if (DG.isNull())
4840 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4841 size_t NumDecls = DG.end() - DG.begin();
4842 SmallVector<Decl *, 1> ToDecls(NumDecls);
4843 auto &_Importer = this->Importer;
4844 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4845 [&_Importer](Decl *D) -> Decl * {
4846 return _Importer.Import(D);
4847 });
4848 return DeclGroupRef::Create(Importer.getToContext(),
4849 ToDecls.begin(),
4850 NumDecls);
4851}
4852
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004853Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4854 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4855 << S->getStmtClassName();
4856 return nullptr;
4857}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004858
4859Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4860 SmallVector<IdentifierInfo *, 4> Names;
4861 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4862 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004863 // ToII is nullptr when no symbolic name is given for output operand
4864 // see ParseStmtAsm::ParseAsmOperandsOpt
4865 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004866 return nullptr;
4867 Names.push_back(ToII);
4868 }
4869 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4870 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004871 // ToII is nullptr when no symbolic name is given for input operand
4872 // see ParseStmtAsm::ParseAsmOperandsOpt
4873 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004874 return nullptr;
4875 Names.push_back(ToII);
4876 }
4877
4878 SmallVector<StringLiteral *, 4> Clobbers;
4879 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004880 auto *Clobber = cast_or_null<StringLiteral>(
4881 Importer.Import(S->getClobberStringLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004882 if (!Clobber)
4883 return nullptr;
4884 Clobbers.push_back(Clobber);
4885 }
4886
4887 SmallVector<StringLiteral *, 4> Constraints;
4888 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004889 auto *Output = cast_or_null<StringLiteral>(
4890 Importer.Import(S->getOutputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004891 if (!Output)
4892 return nullptr;
4893 Constraints.push_back(Output);
4894 }
4895
4896 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004897 auto *Input = cast_or_null<StringLiteral>(
4898 Importer.Import(S->getInputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004899 if (!Input)
4900 return nullptr;
4901 Constraints.push_back(Input);
4902 }
4903
4904 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004905 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004906 return nullptr;
4907
Aleksei Sidorina693b372016-09-28 10:16:56 +00004908 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004909 return nullptr;
4910
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004911 auto *AsmStr = cast_or_null<StringLiteral>(
4912 Importer.Import(S->getAsmString()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004913 if (!AsmStr)
4914 return nullptr;
4915
4916 return new (Importer.getToContext()) GCCAsmStmt(
4917 Importer.getToContext(),
4918 Importer.Import(S->getAsmLoc()),
4919 S->isSimple(),
4920 S->isVolatile(),
4921 S->getNumOutputs(),
4922 S->getNumInputs(),
4923 Names.data(),
4924 Constraints.data(),
4925 Exprs.data(),
4926 AsmStr,
4927 S->getNumClobbers(),
4928 Clobbers.data(),
4929 Importer.Import(S->getRParenLoc()));
4930}
4931
Sean Callanan59721b32015-04-28 18:41:46 +00004932Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4933 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004934 for (auto *ToD : ToDG) {
Sean Callanan59721b32015-04-28 18:41:46 +00004935 if (!ToD)
4936 return nullptr;
4937 }
4938 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4939 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4940 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4941}
4942
4943Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4944 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4945 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4946 S->hasLeadingEmptyMacro());
4947}
4948
4949Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004950 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004951
4952 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00004953 return nullptr;
4954
Sean Callanan59721b32015-04-28 18:41:46 +00004955 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4956 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
Benjamin Kramer07420902017-12-24 16:24:20 +00004957 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
4958 ToRBraceLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004959}
4960
4961Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4962 Expr *ToLHS = Importer.Import(S->getLHS());
4963 if (!ToLHS)
4964 return nullptr;
4965 Expr *ToRHS = Importer.Import(S->getRHS());
4966 if (!ToRHS && S->getRHS())
4967 return nullptr;
Gabor Horvath480892b2017-10-18 09:25:18 +00004968 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4969 if (!ToSubStmt && S->getSubStmt())
4970 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004971 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4972 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4973 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004974 auto *ToStmt = new (Importer.getToContext())
Gabor Horvath480892b2017-10-18 09:25:18 +00004975 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
4976 ToStmt->setSubStmt(ToSubStmt);
4977 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00004978}
4979
4980Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4981 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4982 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4983 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4984 if (!ToSubStmt && S->getSubStmt())
4985 return nullptr;
4986 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4987 ToSubStmt);
4988}
4989
4990Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4991 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004992 auto *ToLabelDecl = cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00004993 if (!ToLabelDecl && S->getDecl())
4994 return nullptr;
4995 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4996 if (!ToSubStmt && S->getSubStmt())
4997 return nullptr;
4998 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4999 ToSubStmt);
5000}
5001
5002Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5003 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
5004 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5005 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00005006 if (ImportContainerChecked(FromAttrs, ToAttrs))
5007 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005008 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5009 if (!ToSubStmt && S->getSubStmt())
5010 return nullptr;
5011 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
5012 ToAttrs, ToSubStmt);
5013}
5014
5015Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5016 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00005017 Stmt *ToInit = Importer.Import(S->getInit());
5018 if (!ToInit && S->getInit())
5019 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005020 VarDecl *ToConditionVariable = nullptr;
5021 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5022 ToConditionVariable =
5023 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5024 if (!ToConditionVariable)
5025 return nullptr;
5026 }
5027 Expr *ToCondition = Importer.Import(S->getCond());
5028 if (!ToCondition && S->getCond())
5029 return nullptr;
5030 Stmt *ToThenStmt = Importer.Import(S->getThen());
5031 if (!ToThenStmt && S->getThen())
5032 return nullptr;
5033 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
5034 Stmt *ToElseStmt = Importer.Import(S->getElse());
5035 if (!ToElseStmt && S->getElse())
5036 return nullptr;
5037 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00005038 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00005039 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00005040 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00005041 ToCondition, ToThenStmt,
5042 ToElseLoc, ToElseStmt);
5043}
5044
5045Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00005046 Stmt *ToInit = Importer.Import(S->getInit());
5047 if (!ToInit && S->getInit())
5048 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005049 VarDecl *ToConditionVariable = nullptr;
5050 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5051 ToConditionVariable =
5052 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5053 if (!ToConditionVariable)
5054 return nullptr;
5055 }
5056 Expr *ToCondition = Importer.Import(S->getCond());
5057 if (!ToCondition && S->getCond())
5058 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005059 auto *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00005060 Importer.getToContext(), ToInit,
5061 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00005062 Stmt *ToBody = Importer.Import(S->getBody());
5063 if (!ToBody && S->getBody())
5064 return nullptr;
5065 ToStmt->setBody(ToBody);
5066 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
5067 // Now we have to re-chain the cases.
5068 SwitchCase *LastChainedSwitchCase = nullptr;
5069 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5070 SC = SC->getNextSwitchCase()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005071 auto *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
Sean Callanan59721b32015-04-28 18:41:46 +00005072 if (!ToSC)
5073 return nullptr;
5074 if (LastChainedSwitchCase)
5075 LastChainedSwitchCase->setNextSwitchCase(ToSC);
5076 else
5077 ToStmt->setSwitchCaseList(ToSC);
5078 LastChainedSwitchCase = ToSC;
5079 }
5080 return ToStmt;
5081}
5082
5083Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5084 VarDecl *ToConditionVariable = nullptr;
5085 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5086 ToConditionVariable =
5087 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5088 if (!ToConditionVariable)
5089 return nullptr;
5090 }
5091 Expr *ToCondition = Importer.Import(S->getCond());
5092 if (!ToCondition && S->getCond())
5093 return nullptr;
5094 Stmt *ToBody = Importer.Import(S->getBody());
5095 if (!ToBody && S->getBody())
5096 return nullptr;
5097 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5098 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
5099 ToConditionVariable,
5100 ToCondition, ToBody,
5101 ToWhileLoc);
5102}
5103
5104Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5105 Stmt *ToBody = Importer.Import(S->getBody());
5106 if (!ToBody && S->getBody())
5107 return nullptr;
5108 Expr *ToCondition = Importer.Import(S->getCond());
5109 if (!ToCondition && S->getCond())
5110 return nullptr;
5111 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
5112 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5113 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5114 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
5115 ToDoLoc, ToWhileLoc,
5116 ToRParenLoc);
5117}
5118
5119Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
5120 Stmt *ToInit = Importer.Import(S->getInit());
5121 if (!ToInit && S->getInit())
5122 return nullptr;
5123 Expr *ToCondition = Importer.Import(S->getCond());
5124 if (!ToCondition && S->getCond())
5125 return nullptr;
5126 VarDecl *ToConditionVariable = nullptr;
5127 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5128 ToConditionVariable =
5129 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5130 if (!ToConditionVariable)
5131 return nullptr;
5132 }
5133 Expr *ToInc = Importer.Import(S->getInc());
5134 if (!ToInc && S->getInc())
5135 return nullptr;
5136 Stmt *ToBody = Importer.Import(S->getBody());
5137 if (!ToBody && S->getBody())
5138 return nullptr;
5139 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5140 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
5141 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5142 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
5143 ToInit, ToCondition,
5144 ToConditionVariable,
5145 ToInc, ToBody,
5146 ToForLoc, ToLParenLoc,
5147 ToRParenLoc);
5148}
5149
5150Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5151 LabelDecl *ToLabel = nullptr;
5152 if (LabelDecl *FromLabel = S->getLabel()) {
5153 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
5154 if (!ToLabel)
5155 return nullptr;
5156 }
5157 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5158 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
5159 return new (Importer.getToContext()) GotoStmt(ToLabel,
5160 ToGotoLoc, ToLabelLoc);
5161}
5162
5163Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5164 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5165 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
5166 Expr *ToTarget = Importer.Import(S->getTarget());
5167 if (!ToTarget && S->getTarget())
5168 return nullptr;
5169 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
5170 ToTarget);
5171}
5172
5173Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5174 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
5175 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
5176}
5177
5178Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5179 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
5180 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
5181}
5182
5183Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5184 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
5185 Expr *ToRetExpr = Importer.Import(S->getRetValue());
5186 if (!ToRetExpr && S->getRetValue())
5187 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005188 auto *NRVOCandidate = const_cast<VarDecl *>(S->getNRVOCandidate());
5189 auto *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
Sean Callanan59721b32015-04-28 18:41:46 +00005190 if (!ToNRVOCandidate && NRVOCandidate)
5191 return nullptr;
5192 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
5193 ToNRVOCandidate);
5194}
5195
5196Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5197 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
5198 VarDecl *ToExceptionDecl = nullptr;
5199 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
5200 ToExceptionDecl =
5201 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5202 if (!ToExceptionDecl)
5203 return nullptr;
5204 }
5205 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
5206 if (!ToHandlerBlock && S->getHandlerBlock())
5207 return nullptr;
5208 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
5209 ToExceptionDecl,
5210 ToHandlerBlock);
5211}
5212
5213Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5214 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
5215 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
5216 if (!ToTryBlock && S->getTryBlock())
5217 return nullptr;
5218 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5219 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5220 CXXCatchStmt *FromHandler = S->getHandler(HI);
5221 if (Stmt *ToHandler = Importer.Import(FromHandler))
5222 ToHandlers[HI] = ToHandler;
5223 else
5224 return nullptr;
5225 }
5226 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
5227 ToHandlers);
5228}
5229
5230Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005231 auto *ToRange =
Sean Callanan59721b32015-04-28 18:41:46 +00005232 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
5233 if (!ToRange && S->getRangeStmt())
5234 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005235 auto *ToBegin =
Richard Smith01694c32016-03-20 10:33:40 +00005236 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
5237 if (!ToBegin && S->getBeginStmt())
5238 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005239 auto *ToEnd =
Richard Smith01694c32016-03-20 10:33:40 +00005240 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
5241 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00005242 return nullptr;
5243 Expr *ToCond = Importer.Import(S->getCond());
5244 if (!ToCond && S->getCond())
5245 return nullptr;
5246 Expr *ToInc = Importer.Import(S->getInc());
5247 if (!ToInc && S->getInc())
5248 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005249 auto *ToLoopVar =
Sean Callanan59721b32015-04-28 18:41:46 +00005250 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
5251 if (!ToLoopVar && S->getLoopVarStmt())
5252 return nullptr;
5253 Stmt *ToBody = Importer.Import(S->getBody());
5254 if (!ToBody && S->getBody())
5255 return nullptr;
5256 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00005257 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005258 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5259 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005260 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005261 ToCond, ToInc,
5262 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005263 ToForLoc, ToCoawaitLoc,
5264 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005265}
5266
5267Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5268 Stmt *ToElem = Importer.Import(S->getElement());
5269 if (!ToElem && S->getElement())
5270 return nullptr;
5271 Expr *ToCollect = Importer.Import(S->getCollection());
5272 if (!ToCollect && S->getCollection())
5273 return nullptr;
5274 Stmt *ToBody = Importer.Import(S->getBody());
5275 if (!ToBody && S->getBody())
5276 return nullptr;
5277 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5278 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5279 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5280 ToCollect,
5281 ToBody, ToForLoc,
5282 ToRParenLoc);
5283}
5284
5285Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5286 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5287 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5288 VarDecl *ToExceptionDecl = nullptr;
5289 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5290 ToExceptionDecl =
5291 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5292 if (!ToExceptionDecl)
5293 return nullptr;
5294 }
5295 Stmt *ToBody = Importer.Import(S->getCatchBody());
5296 if (!ToBody && S->getCatchBody())
5297 return nullptr;
5298 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5299 ToRParenLoc,
5300 ToExceptionDecl,
5301 ToBody);
5302}
5303
5304Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5305 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5306 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5307 if (!ToAtFinallyStmt && S->getFinallyBody())
5308 return nullptr;
5309 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5310 ToAtFinallyStmt);
5311}
5312
5313Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5314 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5315 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5316 if (!ToAtTryStmt && S->getTryBody())
5317 return nullptr;
5318 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5319 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5320 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5321 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5322 ToCatchStmts[CI] = ToCatchStmt;
5323 else
5324 return nullptr;
5325 }
5326 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5327 if (!ToAtFinallyStmt && S->getFinallyStmt())
5328 return nullptr;
5329 return ObjCAtTryStmt::Create(Importer.getToContext(),
5330 ToAtTryLoc, ToAtTryStmt,
5331 ToCatchStmts.begin(), ToCatchStmts.size(),
5332 ToAtFinallyStmt);
5333}
5334
5335Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5336 (ObjCAtSynchronizedStmt *S) {
5337 SourceLocation ToAtSynchronizedLoc =
5338 Importer.Import(S->getAtSynchronizedLoc());
5339 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5340 if (!ToSynchExpr && S->getSynchExpr())
5341 return nullptr;
5342 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5343 if (!ToSynchBody && S->getSynchBody())
5344 return nullptr;
5345 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5346 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5347}
5348
5349Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5350 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5351 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5352 if (!ToThrow && S->getThrowExpr())
5353 return nullptr;
5354 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5355}
5356
5357Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5358 (ObjCAutoreleasePoolStmt *S) {
5359 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5360 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5361 if (!ToSubStmt && S->getSubStmt())
5362 return nullptr;
5363 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5364 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005365}
5366
5367//----------------------------------------------------------------------------
5368// Import Expressions
5369//----------------------------------------------------------------------------
5370Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5371 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5372 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005373 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005374}
5375
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005376Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5377 QualType T = Importer.Import(E->getType());
5378 if (T.isNull())
5379 return nullptr;
5380
5381 Expr *SubExpr = Importer.Import(E->getSubExpr());
5382 if (!SubExpr && E->getSubExpr())
5383 return nullptr;
5384
5385 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5386 if (!TInfo)
5387 return nullptr;
5388
5389 return new (Importer.getToContext()) VAArgExpr(
5390 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5391 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5392}
5393
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005394Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5395 QualType T = Importer.Import(E->getType());
5396 if (T.isNull())
5397 return nullptr;
5398
5399 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005400 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005401}
5402
5403Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5404 QualType T = Importer.Import(E->getType());
5405 if (T.isNull())
5406 return nullptr;
5407
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005408 auto *SL = cast_or_null<StringLiteral>(Importer.Import(E->getFunctionName()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005409 if (!SL && E->getFunctionName())
5410 return nullptr;
5411
5412 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005413 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005414}
5415
Douglas Gregor52f820e2010-02-19 01:17:02 +00005416Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005417 auto *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
Douglas Gregor52f820e2010-02-19 01:17:02 +00005418 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005419 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005420
Craig Topper36250ad2014-05-12 05:36:57 +00005421 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005422 if (E->getDecl() != E->getFoundDecl()) {
5423 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5424 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005425 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005426 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005427
5428 QualType T = Importer.Import(E->getType());
5429 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005430 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005431
Aleksei Sidorina693b372016-09-28 10:16:56 +00005432 TemplateArgumentListInfo ToTAInfo;
5433 TemplateArgumentListInfo *ResInfo = nullptr;
5434 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005435 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
5436 return nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00005437 ResInfo = &ToTAInfo;
5438 }
5439
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005440 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5441 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005442 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005443 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005444 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005445 Importer.Import(E->getLocation()),
5446 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005447 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005448 if (E->hadMultipleCandidates())
5449 DRE->setHadMultipleCandidates(true);
5450 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005451}
5452
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005453Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5454 QualType T = Importer.Import(E->getType());
5455 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00005456 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005457
5458 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5459}
5460
5461ASTNodeImporter::Designator
5462ASTNodeImporter::ImportDesignator(const Designator &D) {
5463 if (D.isFieldDesignator()) {
5464 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5465 // Caller checks for import error
5466 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5467 Importer.Import(D.getFieldLoc()));
5468 }
5469 if (D.isArrayDesignator())
5470 return Designator(D.getFirstExprIndex(),
5471 Importer.Import(D.getLBracketLoc()),
5472 Importer.Import(D.getRBracketLoc()));
5473
5474 assert(D.isArrayRangeDesignator());
5475 return Designator(D.getFirstExprIndex(),
5476 Importer.Import(D.getLBracketLoc()),
5477 Importer.Import(D.getEllipsisLoc()),
5478 Importer.Import(D.getRBracketLoc()));
5479}
5480
5481
5482Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005483 auto *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005484 if (!Init)
5485 return nullptr;
5486
5487 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5488 // List elements from the second, the first is Init itself
5489 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005490 if (auto *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005491 IndexExprs[I - 1] = Arg;
5492 else
5493 return nullptr;
5494 }
5495
5496 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005497 llvm::transform(DIE->designators(), Designators.begin(),
5498 [this](const Designator &D) -> Designator {
5499 return ImportDesignator(D);
5500 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005501
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005502 for (const auto &D : DIE->designators())
David Majnemerf7e36092016-06-23 00:15:04 +00005503 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005504 return nullptr;
5505
5506 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005507 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005508 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5509 DIE->usesGNUSyntax(), Init);
5510}
5511
5512Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5513 QualType T = Importer.Import(E->getType());
5514 if (T.isNull())
5515 return nullptr;
5516
5517 return new (Importer.getToContext())
5518 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5519}
5520
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005521Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5522 QualType T = Importer.Import(E->getType());
5523 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005524 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005525
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005526 return IntegerLiteral::Create(Importer.getToContext(),
5527 E->getValue(), T,
5528 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005529}
5530
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005531Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5532 QualType T = Importer.Import(E->getType());
5533 if (T.isNull())
5534 return nullptr;
5535
5536 return FloatingLiteral::Create(Importer.getToContext(),
5537 E->getValue(), E->isExact(), T,
5538 Importer.Import(E->getLocation()));
5539}
5540
Douglas Gregor623421d2010-02-18 02:21:22 +00005541Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5542 QualType T = Importer.Import(E->getType());
5543 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005544 return nullptr;
5545
Douglas Gregorfb65e592011-07-27 05:40:30 +00005546 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5547 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005548 Importer.Import(E->getLocation()));
5549}
5550
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005551Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5552 QualType T = Importer.Import(E->getType());
5553 if (T.isNull())
5554 return nullptr;
5555
5556 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5557 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5558
5559 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5560 E->getKind(), E->isPascal(), T,
5561 Locations.data(), Locations.size());
5562}
5563
5564Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5565 QualType T = Importer.Import(E->getType());
5566 if (T.isNull())
5567 return nullptr;
5568
5569 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5570 if (!TInfo)
5571 return nullptr;
5572
5573 Expr *Init = Importer.Import(E->getInitializer());
5574 if (!Init)
5575 return nullptr;
5576
5577 return new (Importer.getToContext()) CompoundLiteralExpr(
5578 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5579 Init, E->isFileScope());
5580}
5581
5582Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5583 QualType T = Importer.Import(E->getType());
5584 if (T.isNull())
5585 return nullptr;
5586
5587 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5588 if (ImportArrayChecked(
5589 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5590 Exprs.begin()))
5591 return nullptr;
5592
5593 return new (Importer.getToContext()) AtomicExpr(
5594 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5595 Importer.Import(E->getRParenLoc()));
5596}
5597
5598Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5599 QualType T = Importer.Import(E->getType());
5600 if (T.isNull())
5601 return nullptr;
5602
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005603 auto *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005604 if (!ToLabel)
5605 return nullptr;
5606
5607 return new (Importer.getToContext()) AddrLabelExpr(
5608 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5609 ToLabel, T);
5610}
5611
Douglas Gregorc74247e2010-02-19 01:07:06 +00005612Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5613 Expr *SubExpr = Importer.Import(E->getSubExpr());
5614 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005615 return nullptr;
5616
Douglas Gregorc74247e2010-02-19 01:07:06 +00005617 return new (Importer.getToContext())
5618 ParenExpr(Importer.Import(E->getLParen()),
5619 Importer.Import(E->getRParen()),
5620 SubExpr);
5621}
5622
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005623Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5624 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005625 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005626 return nullptr;
5627
5628 return new (Importer.getToContext()) ParenListExpr(
5629 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5630 Exprs, Importer.Import(E->getLParenLoc()));
5631}
5632
5633Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5634 QualType T = Importer.Import(E->getType());
5635 if (T.isNull())
5636 return nullptr;
5637
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005638 auto *ToSubStmt = cast_or_null<CompoundStmt>(
5639 Importer.Import(E->getSubStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005640 if (!ToSubStmt && E->getSubStmt())
5641 return nullptr;
5642
5643 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5644 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5645}
5646
Douglas Gregorc74247e2010-02-19 01:07:06 +00005647Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5648 QualType T = Importer.Import(E->getType());
5649 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005650 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005651
5652 Expr *SubExpr = Importer.Import(E->getSubExpr());
5653 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005654 return nullptr;
5655
Aaron Ballmana5038552018-01-09 13:07:03 +00005656 return new (Importer.getToContext()) UnaryOperator(
5657 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(),
5658 Importer.Import(E->getOperatorLoc()), E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005659}
5660
Aaron Ballmana5038552018-01-09 13:07:03 +00005661Expr *
5662ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005663 QualType ResultType = Importer.Import(E->getType());
5664
5665 if (E->isArgumentType()) {
5666 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5667 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005668 return nullptr;
5669
Peter Collingbournee190dee2011-03-11 19:24:49 +00005670 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5671 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005672 Importer.Import(E->getOperatorLoc()),
5673 Importer.Import(E->getRParenLoc()));
5674 }
5675
5676 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5677 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005678 return nullptr;
5679
Peter Collingbournee190dee2011-03-11 19:24:49 +00005680 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5681 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005682 Importer.Import(E->getOperatorLoc()),
5683 Importer.Import(E->getRParenLoc()));
5684}
5685
Douglas Gregorc74247e2010-02-19 01:07:06 +00005686Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5687 QualType T = Importer.Import(E->getType());
5688 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005689 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005690
5691 Expr *LHS = Importer.Import(E->getLHS());
5692 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005693 return nullptr;
5694
Douglas Gregorc74247e2010-02-19 01:07:06 +00005695 Expr *RHS = Importer.Import(E->getRHS());
5696 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005697 return nullptr;
5698
Douglas Gregorc74247e2010-02-19 01:07:06 +00005699 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005700 T, E->getValueKind(),
5701 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005702 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005703 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005704}
5705
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005706Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5707 QualType T = Importer.Import(E->getType());
5708 if (T.isNull())
5709 return nullptr;
5710
5711 Expr *ToLHS = Importer.Import(E->getLHS());
5712 if (!ToLHS)
5713 return nullptr;
5714
5715 Expr *ToRHS = Importer.Import(E->getRHS());
5716 if (!ToRHS)
5717 return nullptr;
5718
5719 Expr *ToCond = Importer.Import(E->getCond());
5720 if (!ToCond)
5721 return nullptr;
5722
5723 return new (Importer.getToContext()) ConditionalOperator(
5724 ToCond, Importer.Import(E->getQuestionLoc()),
5725 ToLHS, Importer.Import(E->getColonLoc()),
5726 ToRHS, T, E->getValueKind(), E->getObjectKind());
5727}
5728
5729Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5730 BinaryConditionalOperator *E) {
5731 QualType T = Importer.Import(E->getType());
5732 if (T.isNull())
5733 return nullptr;
5734
5735 Expr *Common = Importer.Import(E->getCommon());
5736 if (!Common)
5737 return nullptr;
5738
5739 Expr *Cond = Importer.Import(E->getCond());
5740 if (!Cond)
5741 return nullptr;
5742
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005743 auto *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5744 Importer.Import(E->getOpaqueValue()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005745 if (!OpaqueValue)
5746 return nullptr;
5747
5748 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5749 if (!TrueExpr)
5750 return nullptr;
5751
5752 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5753 if (!FalseExpr)
5754 return nullptr;
5755
5756 return new (Importer.getToContext()) BinaryConditionalOperator(
5757 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5758 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5759 T, E->getValueKind(), E->getObjectKind());
5760}
5761
Aleksei Sidorina693b372016-09-28 10:16:56 +00005762Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5763 QualType T = Importer.Import(E->getType());
5764 if (T.isNull())
5765 return nullptr;
5766
5767 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5768 if (!ToQueried)
5769 return nullptr;
5770
5771 Expr *Dim = Importer.Import(E->getDimensionExpression());
5772 if (!Dim && E->getDimensionExpression())
5773 return nullptr;
5774
5775 return new (Importer.getToContext()) ArrayTypeTraitExpr(
5776 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5777 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
5778}
5779
5780Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5781 QualType T = Importer.Import(E->getType());
5782 if (T.isNull())
5783 return nullptr;
5784
5785 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5786 if (!ToQueried)
5787 return nullptr;
5788
5789 return new (Importer.getToContext()) ExpressionTraitExpr(
5790 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5791 E->getValue(), Importer.Import(E->getLocEnd()), T);
5792}
5793
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005794Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5795 QualType T = Importer.Import(E->getType());
5796 if (T.isNull())
5797 return nullptr;
5798
5799 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5800 if (!SourceExpr && E->getSourceExpr())
5801 return nullptr;
5802
5803 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005804 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005805 E->getObjectKind(), SourceExpr);
5806}
5807
Aleksei Sidorina693b372016-09-28 10:16:56 +00005808Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5809 QualType T = Importer.Import(E->getType());
5810 if (T.isNull())
5811 return nullptr;
5812
5813 Expr *ToLHS = Importer.Import(E->getLHS());
5814 if (!ToLHS)
5815 return nullptr;
5816
5817 Expr *ToRHS = Importer.Import(E->getRHS());
5818 if (!ToRHS)
5819 return nullptr;
5820
5821 return new (Importer.getToContext()) ArraySubscriptExpr(
5822 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5823 Importer.Import(E->getRBracketLoc()));
5824}
5825
Douglas Gregorc74247e2010-02-19 01:07:06 +00005826Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5827 QualType T = Importer.Import(E->getType());
5828 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005829 return nullptr;
5830
Douglas Gregorc74247e2010-02-19 01:07:06 +00005831 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5832 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005833 return nullptr;
5834
Douglas Gregorc74247e2010-02-19 01:07:06 +00005835 QualType CompResultType = Importer.Import(E->getComputationResultType());
5836 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005837 return nullptr;
5838
Douglas Gregorc74247e2010-02-19 01:07:06 +00005839 Expr *LHS = Importer.Import(E->getLHS());
5840 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005841 return nullptr;
5842
Douglas Gregorc74247e2010-02-19 01:07:06 +00005843 Expr *RHS = Importer.Import(E->getRHS());
5844 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005845 return nullptr;
5846
Douglas Gregorc74247e2010-02-19 01:07:06 +00005847 return new (Importer.getToContext())
5848 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005849 T, E->getValueKind(),
5850 E->getObjectKind(),
5851 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005852 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005853 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005854}
5855
Aleksei Sidorina693b372016-09-28 10:16:56 +00005856bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
5857 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
5858 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
5859 Path.push_back(Spec);
5860 else
5861 return true;
5862 }
5863 return false;
John McCallcf142162010-08-07 06:22:56 +00005864}
5865
Douglas Gregor98c10182010-02-12 22:17:39 +00005866Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5867 QualType T = Importer.Import(E->getType());
5868 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005869 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005870
5871 Expr *SubExpr = Importer.Import(E->getSubExpr());
5872 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005873 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005874
5875 CXXCastPath BasePath;
5876 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005877 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005878
5879 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005880 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005881}
5882
Aleksei Sidorina693b372016-09-28 10:16:56 +00005883Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00005884 QualType T = Importer.Import(E->getType());
5885 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005886 return nullptr;
5887
Douglas Gregor5481d322010-02-19 01:32:14 +00005888 Expr *SubExpr = Importer.Import(E->getSubExpr());
5889 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005890 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005891
5892 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5893 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005894 return nullptr;
5895
John McCallcf142162010-08-07 06:22:56 +00005896 CXXCastPath BasePath;
5897 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005898 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005899
Aleksei Sidorina693b372016-09-28 10:16:56 +00005900 switch (E->getStmtClass()) {
5901 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005902 auto *CCE = cast<CStyleCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005903 return CStyleCastExpr::Create(Importer.getToContext(), T,
5904 E->getValueKind(), E->getCastKind(),
5905 SubExpr, &BasePath, TInfo,
5906 Importer.Import(CCE->getLParenLoc()),
5907 Importer.Import(CCE->getRParenLoc()));
5908 }
5909
5910 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005911 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005912 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
5913 E->getValueKind(), TInfo,
5914 E->getCastKind(), SubExpr, &BasePath,
5915 Importer.Import(FCE->getLParenLoc()),
5916 Importer.Import(FCE->getRParenLoc()));
5917 }
5918
5919 case Stmt::ObjCBridgedCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005920 auto *OCE = cast<ObjCBridgedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005921 return new (Importer.getToContext()) ObjCBridgedCastExpr(
5922 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
5923 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
5924 TInfo, SubExpr);
5925 }
5926 default:
5927 break; // just fall through
5928 }
5929
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005930 auto *Named = cast<CXXNamedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005931 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
5932 RParenLoc = Importer.Import(Named->getRParenLoc());
5933 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
5934
5935 switch (E->getStmtClass()) {
5936 case Stmt::CXXStaticCastExprClass:
5937 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
5938 E->getValueKind(), E->getCastKind(),
5939 SubExpr, &BasePath, TInfo,
5940 ExprLoc, RParenLoc, Brackets);
5941
5942 case Stmt::CXXDynamicCastExprClass:
5943 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
5944 E->getValueKind(), E->getCastKind(),
5945 SubExpr, &BasePath, TInfo,
5946 ExprLoc, RParenLoc, Brackets);
5947
5948 case Stmt::CXXReinterpretCastExprClass:
5949 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
5950 E->getValueKind(), E->getCastKind(),
5951 SubExpr, &BasePath, TInfo,
5952 ExprLoc, RParenLoc, Brackets);
5953
5954 case Stmt::CXXConstCastExprClass:
5955 return CXXConstCastExpr::Create(Importer.getToContext(), T,
5956 E->getValueKind(), SubExpr, TInfo, ExprLoc,
5957 RParenLoc, Brackets);
5958 default:
5959 llvm_unreachable("Cast expression of unsupported type!");
5960 return nullptr;
5961 }
5962}
5963
5964Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
5965 QualType T = Importer.Import(OE->getType());
5966 if (T.isNull())
5967 return nullptr;
5968
5969 SmallVector<OffsetOfNode, 4> Nodes;
5970 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
5971 const OffsetOfNode &Node = OE->getComponent(I);
5972
5973 switch (Node.getKind()) {
5974 case OffsetOfNode::Array:
5975 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
5976 Node.getArrayExprIndex(),
5977 Importer.Import(Node.getLocEnd())));
5978 break;
5979
5980 case OffsetOfNode::Base: {
5981 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
5982 if (!BS && Node.getBase())
5983 return nullptr;
5984 Nodes.push_back(OffsetOfNode(BS));
5985 break;
5986 }
5987 case OffsetOfNode::Field: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005988 auto *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00005989 if (!FD)
5990 return nullptr;
5991 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
5992 Importer.Import(Node.getLocEnd())));
5993 break;
5994 }
5995 case OffsetOfNode::Identifier: {
5996 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
5997 if (!ToII)
5998 return nullptr;
5999 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
6000 Importer.Import(Node.getLocEnd())));
6001 break;
6002 }
6003 }
6004 }
6005
6006 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
6007 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
6008 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
6009 if (!ToIndexExpr)
6010 return nullptr;
6011 Exprs[I] = ToIndexExpr;
6012 }
6013
6014 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
6015 if (!TInfo && OE->getTypeSourceInfo())
6016 return nullptr;
6017
6018 return OffsetOfExpr::Create(Importer.getToContext(), T,
6019 Importer.Import(OE->getOperatorLoc()),
6020 TInfo, Nodes, Exprs,
6021 Importer.Import(OE->getRParenLoc()));
6022}
6023
6024Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6025 QualType T = Importer.Import(E->getType());
6026 if (T.isNull())
6027 return nullptr;
6028
6029 Expr *Operand = Importer.Import(E->getOperand());
6030 if (!Operand)
6031 return nullptr;
6032
6033 CanThrowResult CanThrow;
6034 if (E->isValueDependent())
6035 CanThrow = CT_Dependent;
6036 else
6037 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
6038
6039 return new (Importer.getToContext()) CXXNoexceptExpr(
6040 T, Operand, CanThrow,
6041 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
6042}
6043
6044Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6045 QualType T = Importer.Import(E->getType());
6046 if (T.isNull())
6047 return nullptr;
6048
6049 Expr *SubExpr = Importer.Import(E->getSubExpr());
6050 if (!SubExpr && E->getSubExpr())
6051 return nullptr;
6052
6053 return new (Importer.getToContext()) CXXThrowExpr(
6054 SubExpr, T, Importer.Import(E->getThrowLoc()),
6055 E->isThrownVariableInScope());
6056}
6057
6058Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006059 auto *Param = cast_or_null<ParmVarDecl>(Importer.Import(E->getParam()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006060 if (!Param)
6061 return nullptr;
6062
6063 return CXXDefaultArgExpr::Create(
6064 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
6065}
6066
6067Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6068 QualType T = Importer.Import(E->getType());
6069 if (T.isNull())
6070 return nullptr;
6071
6072 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
6073 if (!TypeInfo)
6074 return nullptr;
6075
6076 return new (Importer.getToContext()) CXXScalarValueInitExpr(
6077 T, TypeInfo, Importer.Import(E->getRParenLoc()));
6078}
6079
6080Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6081 Expr *SubExpr = Importer.Import(E->getSubExpr());
6082 if (!SubExpr)
6083 return nullptr;
6084
6085 auto *Dtor = cast_or_null<CXXDestructorDecl>(
6086 Importer.Import(const_cast<CXXDestructorDecl *>(
6087 E->getTemporary()->getDestructor())));
6088 if (!Dtor)
6089 return nullptr;
6090
6091 ASTContext &ToCtx = Importer.getToContext();
6092 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
6093 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
6094}
6095
6096Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
6097 QualType T = Importer.Import(CE->getType());
6098 if (T.isNull())
6099 return nullptr;
6100
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006101 TypeSourceInfo *TInfo = Importer.Import(CE->getTypeSourceInfo());
6102 if (!TInfo)
6103 return nullptr;
6104
Aleksei Sidorina693b372016-09-28 10:16:56 +00006105 SmallVector<Expr *, 8> Args(CE->getNumArgs());
6106 if (ImportContainerChecked(CE->arguments(), Args))
6107 return nullptr;
6108
6109 auto *Ctor = cast_or_null<CXXConstructorDecl>(
6110 Importer.Import(CE->getConstructor()));
6111 if (!Ctor)
6112 return nullptr;
6113
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006114 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
6115 Importer.getToContext(), Ctor, T, TInfo, Args,
6116 Importer.Import(CE->getParenOrBraceRange()), CE->hadMultipleCandidates(),
6117 CE->isListInitialization(), CE->isStdInitListInitialization(),
6118 CE->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006119}
6120
6121Expr *
6122ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
6123 QualType T = Importer.Import(E->getType());
6124 if (T.isNull())
6125 return nullptr;
6126
6127 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
6128 if (!TempE)
6129 return nullptr;
6130
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006131 auto *ExtendedBy = cast_or_null<ValueDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006132 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
6133 if (!ExtendedBy && E->getExtendingDecl())
6134 return nullptr;
6135
6136 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
6137 T, TempE, E->isBoundToLvalueReference());
6138
6139 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
6140 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
6141 return ToMTE;
6142}
6143
Gabor Horvath7a91c082017-11-14 11:30:38 +00006144Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6145 QualType T = Importer.Import(E->getType());
6146 if (T.isNull())
6147 return nullptr;
6148
6149 Expr *Pattern = Importer.Import(E->getPattern());
6150 if (!Pattern)
6151 return nullptr;
6152
6153 return new (Importer.getToContext()) PackExpansionExpr(
6154 T, Pattern, Importer.Import(E->getEllipsisLoc()),
6155 E->getNumExpansions());
6156}
6157
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006158Expr *ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6159 auto *Pack = cast_or_null<NamedDecl>(Importer.Import(E->getPack()));
6160 if (!Pack)
6161 return nullptr;
6162
6163 Optional<unsigned> Length;
6164
6165 if (!E->isValueDependent())
6166 Length = E->getPackLength();
6167
6168 SmallVector<TemplateArgument, 8> PartialArguments;
6169 if (E->isPartiallySubstituted()) {
6170 if (ImportTemplateArguments(E->getPartialArguments().data(),
6171 E->getPartialArguments().size(),
6172 PartialArguments))
6173 return nullptr;
6174 }
6175
6176 return SizeOfPackExpr::Create(
6177 Importer.getToContext(), Importer.Import(E->getOperatorLoc()), Pack,
6178 Importer.Import(E->getPackLoc()), Importer.Import(E->getRParenLoc()),
6179 Length, PartialArguments);
6180}
6181
Aleksei Sidorina693b372016-09-28 10:16:56 +00006182Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
6183 QualType T = Importer.Import(CE->getType());
6184 if (T.isNull())
6185 return nullptr;
6186
6187 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
6188 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
6189 return nullptr;
6190
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006191 auto *OperatorNewDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006192 Importer.Import(CE->getOperatorNew()));
6193 if (!OperatorNewDecl && CE->getOperatorNew())
6194 return nullptr;
6195
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006196 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006197 Importer.Import(CE->getOperatorDelete()));
6198 if (!OperatorDeleteDecl && CE->getOperatorDelete())
6199 return nullptr;
6200
6201 Expr *ToInit = Importer.Import(CE->getInitializer());
6202 if (!ToInit && CE->getInitializer())
6203 return nullptr;
6204
6205 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
6206 if (!TInfo)
6207 return nullptr;
6208
6209 Expr *ToArrSize = Importer.Import(CE->getArraySize());
6210 if (!ToArrSize && CE->getArraySize())
6211 return nullptr;
6212
6213 return new (Importer.getToContext()) CXXNewExpr(
6214 Importer.getToContext(),
6215 CE->isGlobalNew(),
6216 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00006217 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00006218 CE->doesUsualArrayDeleteWantSize(),
6219 PlacementArgs,
6220 Importer.Import(CE->getTypeIdParens()),
6221 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
6222 Importer.Import(CE->getSourceRange()),
6223 Importer.Import(CE->getDirectInitRange()));
6224}
6225
6226Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6227 QualType T = Importer.Import(E->getType());
6228 if (T.isNull())
6229 return nullptr;
6230
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006231 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006232 Importer.Import(E->getOperatorDelete()));
6233 if (!OperatorDeleteDecl && E->getOperatorDelete())
6234 return nullptr;
6235
6236 Expr *ToArg = Importer.Import(E->getArgument());
6237 if (!ToArg && E->getArgument())
6238 return nullptr;
6239
6240 return new (Importer.getToContext()) CXXDeleteExpr(
6241 T, E->isGlobalDelete(),
6242 E->isArrayForm(),
6243 E->isArrayFormAsWritten(),
6244 E->doesUsualArrayDeleteWantSize(),
6245 OperatorDeleteDecl,
6246 ToArg,
6247 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00006248}
6249
Sean Callanan59721b32015-04-28 18:41:46 +00006250Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6251 QualType T = Importer.Import(E->getType());
6252 if (T.isNull())
6253 return nullptr;
6254
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006255 auto *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00006256 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00006257 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00006258 return nullptr;
6259
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006260 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006261 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006262 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006263
6264 return CXXConstructExpr::Create(Importer.getToContext(), T,
6265 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00006266 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00006267 ToArgs, E->hadMultipleCandidates(),
6268 E->isListInitialization(),
6269 E->isStdInitListInitialization(),
6270 E->requiresZeroInitialization(),
6271 E->getConstructionKind(),
6272 Importer.Import(E->getParenOrBraceRange()));
6273}
6274
Aleksei Sidorina693b372016-09-28 10:16:56 +00006275Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
6276 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
6277 if (!SubExpr && EWC->getSubExpr())
6278 return nullptr;
6279
6280 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
6281 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
6282 if (ExprWithCleanups::CleanupObject Obj =
6283 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
6284 Objs[I] = Obj;
6285 else
6286 return nullptr;
6287
6288 return ExprWithCleanups::Create(Importer.getToContext(),
6289 SubExpr, EWC->cleanupsHaveSideEffects(),
6290 Objs);
6291}
6292
Sean Callanan8bca9962016-03-28 21:43:01 +00006293Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6294 QualType T = Importer.Import(E->getType());
6295 if (T.isNull())
6296 return nullptr;
6297
6298 Expr *ToFn = Importer.Import(E->getCallee());
6299 if (!ToFn)
6300 return nullptr;
6301
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006302 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006303 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006304 return nullptr;
6305
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006306 return new (Importer.getToContext()) CXXMemberCallExpr(
6307 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
6308 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00006309}
6310
6311Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6312 QualType T = Importer.Import(E->getType());
6313 if (T.isNull())
6314 return nullptr;
6315
6316 return new (Importer.getToContext())
6317 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
6318}
6319
6320Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
6321 QualType T = Importer.Import(E->getType());
6322 if (T.isNull())
6323 return nullptr;
6324
6325 return new (Importer.getToContext())
6326 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
6327}
6328
6329
Sean Callanan59721b32015-04-28 18:41:46 +00006330Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
6331 QualType T = Importer.Import(E->getType());
6332 if (T.isNull())
6333 return nullptr;
6334
6335 Expr *ToBase = Importer.Import(E->getBase());
6336 if (!ToBase && E->getBase())
6337 return nullptr;
6338
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006339 auto *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00006340 if (!ToMember && E->getMemberDecl())
6341 return nullptr;
6342
Peter Szecsief972522018-05-02 11:52:54 +00006343 auto *ToDecl =
6344 dyn_cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl()));
6345 if (!ToDecl && E->getFoundDecl().getDecl())
6346 return nullptr;
6347
6348 DeclAccessPair ToFoundDecl =
6349 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00006350
6351 DeclarationNameInfo ToMemberNameInfo(
6352 Importer.Import(E->getMemberNameInfo().getName()),
6353 Importer.Import(E->getMemberNameInfo().getLoc()));
6354
6355 if (E->hasExplicitTemplateArgs()) {
6356 return nullptr; // FIXME: handle template arguments
6357 }
6358
6359 return MemberExpr::Create(Importer.getToContext(), ToBase,
6360 E->isArrow(),
6361 Importer.Import(E->getOperatorLoc()),
6362 Importer.Import(E->getQualifierLoc()),
6363 Importer.Import(E->getTemplateKeywordLoc()),
6364 ToMember, ToFoundDecl, ToMemberNameInfo,
6365 nullptr, T, E->getValueKind(),
6366 E->getObjectKind());
6367}
6368
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006369Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr(
6370 CXXPseudoDestructorExpr *E) {
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006371 Expr *BaseE = Importer.Import(E->getBase());
6372 if (!BaseE)
6373 return nullptr;
6374
6375 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo());
6376 if (!ScopeInfo && E->getScopeTypeInfo())
6377 return nullptr;
6378
6379 PseudoDestructorTypeStorage Storage;
6380 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
6381 IdentifierInfo *ToII = Importer.Import(FromII);
6382 if (!ToII)
6383 return nullptr;
6384 Storage = PseudoDestructorTypeStorage(
6385 ToII, Importer.Import(E->getDestroyedTypeLoc()));
6386 } else {
6387 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo());
6388 if (!TI)
6389 return nullptr;
6390 Storage = PseudoDestructorTypeStorage(TI);
6391 }
6392
6393 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
6394 Importer.getToContext(), BaseE, E->isArrow(),
6395 Importer.Import(E->getOperatorLoc()),
6396 Importer.Import(E->getQualifierLoc()),
6397 ScopeInfo, Importer.Import(E->getColonColonLoc()),
6398 Importer.Import(E->getTildeLoc()), Storage);
6399}
6400
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006401Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
6402 CXXDependentScopeMemberExpr *E) {
6403 Expr *Base = nullptr;
6404 if (!E->isImplicitAccess()) {
6405 Base = Importer.Import(E->getBase());
6406 if (!Base)
6407 return nullptr;
6408 }
6409
6410 QualType BaseType = Importer.Import(E->getBaseType());
6411 if (BaseType.isNull())
6412 return nullptr;
6413
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006414 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006415 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006416 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6417 E->template_arguments(), ToTAInfo))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006418 return nullptr;
6419 ResInfo = &ToTAInfo;
6420 }
6421
6422 DeclarationName Name = Importer.Import(E->getMember());
6423 if (!E->getMember().isEmpty() && Name.isEmpty())
6424 return nullptr;
6425
6426 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc()));
6427 // Import additional name location/type info.
6428 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo);
6429 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope());
6430 if (!ToFQ && E->getFirstQualifierFoundInScope())
6431 return nullptr;
6432
6433 return CXXDependentScopeMemberExpr::Create(
6434 Importer.getToContext(), Base, BaseType, E->isArrow(),
6435 Importer.Import(E->getOperatorLoc()),
6436 Importer.Import(E->getQualifierLoc()),
6437 Importer.Import(E->getTemplateKeywordLoc()),
6438 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo);
6439}
6440
Peter Szecsice7f3182018-05-07 12:08:27 +00006441Expr *
6442ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
6443 DeclarationName Name = Importer.Import(E->getDeclName());
6444 if (!E->getDeclName().isEmpty() && Name.isEmpty())
6445 return nullptr;
6446
6447 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getExprLoc()));
6448 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6449
6450 TemplateArgumentListInfo ToTAInfo(Importer.Import(E->getLAngleLoc()),
6451 Importer.Import(E->getRAngleLoc()));
6452 TemplateArgumentListInfo *ResInfo = nullptr;
6453 if (E->hasExplicitTemplateArgs()) {
6454 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6455 return nullptr;
6456 ResInfo = &ToTAInfo;
6457 }
6458
6459 return DependentScopeDeclRefExpr::Create(
6460 Importer.getToContext(), Importer.Import(E->getQualifierLoc()),
6461 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo);
6462}
6463
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006464Expr *ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
6465 CXXUnresolvedConstructExpr *CE) {
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006466 unsigned NumArgs = CE->arg_size();
6467
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006468 SmallVector<Expr *, 8> ToArgs(NumArgs);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006469 if (ImportArrayChecked(CE->arg_begin(), CE->arg_end(), ToArgs.begin()))
6470 return nullptr;
6471
6472 return CXXUnresolvedConstructExpr::Create(
6473 Importer.getToContext(), Importer.Import(CE->getTypeSourceInfo()),
6474 Importer.Import(CE->getLParenLoc()), llvm::makeArrayRef(ToArgs),
6475 Importer.Import(CE->getRParenLoc()));
6476}
6477
6478Expr *ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006479 auto *NamingClass =
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006480 cast_or_null<CXXRecordDecl>(Importer.Import(E->getNamingClass()));
6481 if (E->getNamingClass() && !NamingClass)
6482 return nullptr;
6483
6484 DeclarationName Name = Importer.Import(E->getName());
6485 if (E->getName() && !Name)
6486 return nullptr;
6487
6488 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6489 // Import additional name location/type info.
6490 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6491
6492 UnresolvedSet<8> ToDecls;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006493 for (auto *D : E->decls()) {
6494 if (auto *To = cast_or_null<NamedDecl>(Importer.Import(D)))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006495 ToDecls.addDecl(To);
6496 else
6497 return nullptr;
6498 }
6499
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006500 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006501 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006502 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6503 E->template_arguments(), ToTAInfo))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006504 return nullptr;
6505 ResInfo = &ToTAInfo;
6506 }
6507
6508 if (ResInfo || E->getTemplateKeywordLoc().isValid())
6509 return UnresolvedLookupExpr::Create(
6510 Importer.getToContext(), NamingClass,
6511 Importer.Import(E->getQualifierLoc()),
6512 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, E->requiresADL(),
6513 ResInfo, ToDecls.begin(), ToDecls.end());
6514
6515 return UnresolvedLookupExpr::Create(
6516 Importer.getToContext(), NamingClass,
6517 Importer.Import(E->getQualifierLoc()), NameInfo, E->requiresADL(),
6518 E->isOverloaded(), ToDecls.begin(), ToDecls.end());
6519}
6520
Peter Szecsice7f3182018-05-07 12:08:27 +00006521Expr *ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
6522 DeclarationName Name = Importer.Import(E->getName());
6523 if (!E->getName().isEmpty() && Name.isEmpty())
6524 return nullptr;
6525 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6526 // Import additional name location/type info.
6527 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6528
6529 QualType BaseType = Importer.Import(E->getType());
6530 if (!E->getType().isNull() && BaseType.isNull())
6531 return nullptr;
6532
6533 UnresolvedSet<8> ToDecls;
6534 for (Decl *D : E->decls()) {
6535 if (NamedDecl *To = cast_or_null<NamedDecl>(Importer.Import(D)))
6536 ToDecls.addDecl(To);
6537 else
6538 return nullptr;
6539 }
6540
6541 TemplateArgumentListInfo ToTAInfo;
6542 TemplateArgumentListInfo *ResInfo = nullptr;
6543 if (E->hasExplicitTemplateArgs()) {
6544 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6545 return nullptr;
6546 ResInfo = &ToTAInfo;
6547 }
6548
6549 Expr *BaseE = E->isImplicitAccess() ? nullptr : Importer.Import(E->getBase());
6550 if (!BaseE && !E->isImplicitAccess() && E->getBase()) {
6551 return nullptr;
6552 }
6553
6554 return UnresolvedMemberExpr::Create(
6555 Importer.getToContext(), E->hasUnresolvedUsing(), BaseE, BaseType,
6556 E->isArrow(), Importer.Import(E->getOperatorLoc()),
6557 Importer.Import(E->getQualifierLoc()),
6558 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo,
6559 ToDecls.begin(), ToDecls.end());
6560}
6561
Sean Callanan59721b32015-04-28 18:41:46 +00006562Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
6563 QualType T = Importer.Import(E->getType());
6564 if (T.isNull())
6565 return nullptr;
6566
6567 Expr *ToCallee = Importer.Import(E->getCallee());
6568 if (!ToCallee && E->getCallee())
6569 return nullptr;
6570
6571 unsigned NumArgs = E->getNumArgs();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006572 SmallVector<Expr *, 2> ToArgs(NumArgs);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006573 if (ImportContainerChecked(E->arguments(), ToArgs))
6574 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006575
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006576 auto **ToArgs_Copied = new (Importer.getToContext()) Expr*[NumArgs];
Sean Callanan59721b32015-04-28 18:41:46 +00006577
6578 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
6579 ToArgs_Copied[ai] = ToArgs[ai];
6580
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006581 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
6582 return new (Importer.getToContext()) CXXOperatorCallExpr(
6583 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, T,
6584 OCE->getValueKind(), Importer.Import(OCE->getRParenLoc()),
6585 OCE->getFPFeatures());
6586 }
6587
Sean Callanan59721b32015-04-28 18:41:46 +00006588 return new (Importer.getToContext())
6589 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00006590 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00006591 Importer.Import(E->getRParenLoc()));
6592}
6593
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006594Optional<LambdaCapture>
6595ASTNodeImporter::ImportLambdaCapture(const LambdaCapture &From) {
6596 VarDecl *Var = nullptr;
6597 if (From.capturesVariable()) {
6598 Var = cast_or_null<VarDecl>(Importer.Import(From.getCapturedVar()));
6599 if (!Var)
6600 return None;
6601 }
6602
6603 return LambdaCapture(Importer.Import(From.getLocation()), From.isImplicit(),
6604 From.getCaptureKind(), Var,
6605 From.isPackExpansion()
6606 ? Importer.Import(From.getEllipsisLoc())
6607 : SourceLocation());
6608}
6609
6610Expr *ASTNodeImporter::VisitLambdaExpr(LambdaExpr *LE) {
6611 CXXRecordDecl *FromClass = LE->getLambdaClass();
6612 auto *ToClass = dyn_cast_or_null<CXXRecordDecl>(Importer.Import(FromClass));
6613 if (!ToClass)
6614 return nullptr;
6615
6616 // NOTE: lambda classes are created with BeingDefined flag set up.
6617 // It means that ImportDefinition doesn't work for them and we should fill it
6618 // manually.
6619 if (ToClass->isBeingDefined()) {
6620 for (auto FromField : FromClass->fields()) {
6621 auto *ToField = cast_or_null<FieldDecl>(Importer.Import(FromField));
6622 if (!ToField)
6623 return nullptr;
6624 }
6625 }
6626
6627 auto *ToCallOp = dyn_cast_or_null<CXXMethodDecl>(
6628 Importer.Import(LE->getCallOperator()));
6629 if (!ToCallOp)
6630 return nullptr;
6631
6632 ToClass->completeDefinition();
6633
6634 unsigned NumCaptures = LE->capture_size();
6635 SmallVector<LambdaCapture, 8> Captures;
6636 Captures.reserve(NumCaptures);
6637 for (const auto &FromCapture : LE->captures()) {
6638 if (auto ToCapture = ImportLambdaCapture(FromCapture))
6639 Captures.push_back(*ToCapture);
6640 else
6641 return nullptr;
6642 }
6643
6644 SmallVector<Expr *, 8> InitCaptures(NumCaptures);
6645 if (ImportContainerChecked(LE->capture_inits(), InitCaptures))
6646 return nullptr;
6647
6648 return LambdaExpr::Create(Importer.getToContext(), ToClass,
6649 Importer.Import(LE->getIntroducerRange()),
6650 LE->getCaptureDefault(),
6651 Importer.Import(LE->getCaptureDefaultLoc()),
6652 Captures,
6653 LE->hasExplicitParameters(),
6654 LE->hasExplicitResultType(),
6655 InitCaptures,
6656 Importer.Import(LE->getLocEnd()),
6657 LE->containsUnexpandedParameterPack());
6658}
6659
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006660Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
6661 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00006662 if (T.isNull())
6663 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006664
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006665 SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006666 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006667 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006668
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006669 ASTContext &ToCtx = Importer.getToContext();
6670 InitListExpr *To = new (ToCtx) InitListExpr(
6671 ToCtx, Importer.Import(ILE->getLBraceLoc()),
6672 Exprs, Importer.Import(ILE->getLBraceLoc()));
6673 To->setType(T);
6674
6675 if (ILE->hasArrayFiller()) {
6676 Expr *Filler = Importer.Import(ILE->getArrayFiller());
6677 if (!Filler)
6678 return nullptr;
6679 To->setArrayFiller(Filler);
6680 }
6681
6682 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006683 auto *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006684 if (!ToFD)
6685 return nullptr;
6686 To->setInitializedFieldInUnion(ToFD);
6687 }
6688
6689 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006690 auto *ToSyntForm = cast_or_null<InitListExpr>(Importer.Import(SyntForm));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006691 if (!ToSyntForm)
6692 return nullptr;
6693 To->setSyntacticForm(ToSyntForm);
6694 }
6695
6696 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
6697 To->setValueDependent(ILE->isValueDependent());
6698 To->setInstantiationDependent(ILE->isInstantiationDependent());
6699
6700 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00006701}
6702
Gabor Marton07b01ff2018-06-29 12:17:34 +00006703Expr *ASTNodeImporter::VisitCXXStdInitializerListExpr(
6704 CXXStdInitializerListExpr *E) {
6705 QualType T = Importer.Import(E->getType());
6706 if (T.isNull())
6707 return nullptr;
6708
6709 Expr *SE = Importer.Import(E->getSubExpr());
6710 if (!SE)
6711 return nullptr;
6712
6713 return new (Importer.getToContext()) CXXStdInitializerListExpr(T, SE);
6714}
6715
Richard Smith30e304e2016-12-14 00:03:17 +00006716Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
6717 QualType ToType = Importer.Import(E->getType());
6718 if (ToType.isNull())
6719 return nullptr;
6720
6721 Expr *ToCommon = Importer.Import(E->getCommonExpr());
6722 if (!ToCommon && E->getCommonExpr())
6723 return nullptr;
6724
6725 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
6726 if (!ToSubExpr && E->getSubExpr())
6727 return nullptr;
6728
6729 return new (Importer.getToContext())
6730 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
6731}
6732
6733Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
6734 QualType ToType = Importer.Import(E->getType());
6735 if (ToType.isNull())
6736 return nullptr;
6737 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
6738}
6739
Sean Callanandd2c1742016-05-16 20:48:03 +00006740Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006741 auto *ToField = dyn_cast_or_null<FieldDecl>(Importer.Import(DIE->getField()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006742 if (!ToField && DIE->getField())
6743 return nullptr;
6744
6745 return CXXDefaultInitExpr::Create(
6746 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
6747}
6748
6749Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
6750 QualType ToType = Importer.Import(E->getType());
6751 if (ToType.isNull() && !E->getType().isNull())
6752 return nullptr;
6753 ExprValueKind VK = E->getValueKind();
6754 CastKind CK = E->getCastKind();
6755 Expr *ToOp = Importer.Import(E->getSubExpr());
6756 if (!ToOp && E->getSubExpr())
6757 return nullptr;
6758 CXXCastPath BasePath;
6759 if (ImportCastPath(E, BasePath))
6760 return nullptr;
6761 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6762 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6763 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6764 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
6765
6766 if (isa<CXXStaticCastExpr>(E)) {
6767 return CXXStaticCastExpr::Create(
6768 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6769 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6770 } else if (isa<CXXDynamicCastExpr>(E)) {
6771 return CXXDynamicCastExpr::Create(
6772 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6773 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6774 } else if (isa<CXXReinterpretCastExpr>(E)) {
6775 return CXXReinterpretCastExpr::Create(
6776 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6777 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6778 } else {
6779 return nullptr;
6780 }
6781}
6782
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006783Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
6784 SubstNonTypeTemplateParmExpr *E) {
6785 QualType T = Importer.Import(E->getType());
6786 if (T.isNull())
6787 return nullptr;
6788
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006789 auto *Param = cast_or_null<NonTypeTemplateParmDecl>(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006790 Importer.Import(E->getParameter()));
6791 if (!Param)
6792 return nullptr;
6793
6794 Expr *Replacement = Importer.Import(E->getReplacement());
6795 if (!Replacement)
6796 return nullptr;
6797
6798 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
6799 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
6800 Replacement);
6801}
6802
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00006803Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
6804 QualType ToType = Importer.Import(E->getType());
6805 if (ToType.isNull())
6806 return nullptr;
6807
6808 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
6809 if (ImportContainerChecked(E->getArgs(), ToArgs))
6810 return nullptr;
6811
6812 // According to Sema::BuildTypeTrait(), if E is value-dependent,
6813 // Value is always false.
6814 bool ToValue = false;
6815 if (!E->isValueDependent())
6816 ToValue = E->getValue();
6817
6818 return TypeTraitExpr::Create(
6819 Importer.getToContext(), ToType, Importer.Import(E->getLocStart()),
6820 E->getTrait(), ToArgs, Importer.Import(E->getLocEnd()), ToValue);
6821}
6822
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006823Expr *ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
6824 QualType ToType = Importer.Import(E->getType());
6825 if (ToType.isNull())
6826 return nullptr;
6827
6828 if (E->isTypeOperand()) {
6829 TypeSourceInfo *TSI = Importer.Import(E->getTypeOperandSourceInfo());
6830 if (!TSI)
6831 return nullptr;
6832
6833 return new (Importer.getToContext())
6834 CXXTypeidExpr(ToType, TSI, Importer.Import(E->getSourceRange()));
6835 }
6836
6837 Expr *Op = Importer.Import(E->getExprOperand());
6838 if (!Op)
6839 return nullptr;
6840
6841 return new (Importer.getToContext())
6842 CXXTypeidExpr(ToType, Op, Importer.Import(E->getSourceRange()));
6843}
6844
Lang Hames19e07e12017-06-20 21:06:00 +00006845void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
6846 CXXMethodDecl *FromMethod) {
6847 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
6848 ToMethod->addOverriddenMethod(
6849 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
6850 FromOverriddenMethod))));
6851}
6852
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006853ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006854 ASTContext &FromContext, FileManager &FromFileManager,
6855 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006856 : ToContext(ToContext), FromContext(FromContext),
6857 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
6858 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00006859 ImportedDecls[FromContext.getTranslationUnitDecl()]
6860 = ToContext.getTranslationUnitDecl();
6861}
6862
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006863ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006864
6865QualType ASTImporter::Import(QualType FromT) {
6866 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006867 return {};
John McCall424cec92011-01-19 06:33:43 +00006868
6869 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00006870
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006871 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006872 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6873 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006874 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006875 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006876
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006877 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006878 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006879 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006880 if (ToT.isNull())
6881 return ToT;
6882
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006883 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006884 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006885
John McCall424cec92011-01-19 06:33:43 +00006886 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006887}
6888
Douglas Gregor62d311f2010-02-09 19:21:46 +00006889TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006890 if (!FromTSI)
6891 return FromTSI;
6892
6893 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006894 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006895 QualType T = Import(FromTSI->getType());
6896 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006897 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006898
6899 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006900 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006901}
6902
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00006903Attr *ASTImporter::Import(const Attr *FromAttr) {
6904 Attr *ToAttr = FromAttr->clone(ToContext);
6905 ToAttr->setRange(Import(FromAttr->getRange()));
6906 return ToAttr;
6907}
6908
Sean Callanan59721b32015-04-28 18:41:46 +00006909Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6910 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6911 if (Pos != ImportedDecls.end()) {
6912 Decl *ToD = Pos->second;
Gabor Marton26f72a92018-07-12 09:42:05 +00006913 // FIXME: move this call to ImportDeclParts().
Sean Callanan59721b32015-04-28 18:41:46 +00006914 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6915 return ToD;
6916 } else {
6917 return nullptr;
6918 }
6919}
6920
Douglas Gregor62d311f2010-02-09 19:21:46 +00006921Decl *ASTImporter::Import(Decl *FromD) {
6922 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006923 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006924
Douglas Gregord451ea92011-07-29 23:31:30 +00006925 ASTNodeImporter Importer(*this);
6926
Gabor Marton26f72a92018-07-12 09:42:05 +00006927 // Check whether we've already imported this declaration.
6928 Decl *ToD = GetAlreadyImportedOrNull(FromD);
6929 if (ToD) {
6930 // If FromD has some updated flags after last import, apply it
6931 updateFlags(FromD, ToD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006932 return ToD;
6933 }
Gabor Marton26f72a92018-07-12 09:42:05 +00006934
6935 // Import the type.
6936 ToD = Importer.Visit(FromD);
Douglas Gregor62d311f2010-02-09 19:21:46 +00006937 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006938 return nullptr;
6939
Gabor Marton26f72a92018-07-12 09:42:05 +00006940 // Notify subclasses.
6941 Imported(FromD, ToD);
6942
Douglas Gregor62d311f2010-02-09 19:21:46 +00006943 return ToD;
6944}
6945
6946DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6947 if (!FromDC)
6948 return FromDC;
6949
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006950 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006951 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006952 return nullptr;
6953
Douglas Gregor2e15c842012-02-01 21:00:38 +00006954 // When we're using a record/enum/Objective-C class/protocol as a context, we
6955 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006956 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
6957 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006958 if (ToRecord->isCompleteDefinition()) {
6959 // Do nothing.
6960 } else if (FromRecord->isCompleteDefinition()) {
6961 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6962 ASTNodeImporter::IDK_Basic);
6963 } else {
6964 CompleteDecl(ToRecord);
6965 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006966 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6967 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006968 if (ToEnum->isCompleteDefinition()) {
6969 // Do nothing.
6970 } else if (FromEnum->isCompleteDefinition()) {
6971 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6972 ASTNodeImporter::IDK_Basic);
6973 } else {
6974 CompleteDecl(ToEnum);
6975 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006976 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6977 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006978 if (ToClass->getDefinition()) {
6979 // Do nothing.
6980 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6981 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6982 ASTNodeImporter::IDK_Basic);
6983 } else {
6984 CompleteDecl(ToClass);
6985 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006986 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6987 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006988 if (ToProto->getDefinition()) {
6989 // Do nothing.
6990 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6991 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6992 ASTNodeImporter::IDK_Basic);
6993 } else {
6994 CompleteDecl(ToProto);
6995 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006996 }
6997
6998 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006999}
7000
7001Expr *ASTImporter::Import(Expr *FromE) {
7002 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00007003 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007004
7005 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
7006}
7007
7008Stmt *ASTImporter::Import(Stmt *FromS) {
7009 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007010 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007011
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007012 // Check whether we've already imported this declaration.
7013 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7014 if (Pos != ImportedStmts.end())
7015 return Pos->second;
7016
7017 // Import the type
7018 ASTNodeImporter Importer(*this);
7019 Stmt *ToS = Importer.Visit(FromS);
7020 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00007021 return nullptr;
7022
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007023 // Record the imported declaration.
7024 ImportedStmts[FromS] = ToS;
7025 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007026}
7027
7028NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
7029 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00007030 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007031
Douglas Gregor90ebf252011-04-27 16:48:40 +00007032 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
7033
7034 switch (FromNNS->getKind()) {
7035 case NestedNameSpecifier::Identifier:
7036 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
7037 return NestedNameSpecifier::Create(ToContext, prefix, II);
7038 }
Craig Topper36250ad2014-05-12 05:36:57 +00007039 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007040
7041 case NestedNameSpecifier::Namespace:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007042 if (auto *NS =
7043 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007044 return NestedNameSpecifier::Create(ToContext, prefix, NS);
7045 }
Craig Topper36250ad2014-05-12 05:36:57 +00007046 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007047
7048 case NestedNameSpecifier::NamespaceAlias:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007049 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007050 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007051 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
7052 }
Craig Topper36250ad2014-05-12 05:36:57 +00007053 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007054
7055 case NestedNameSpecifier::Global:
7056 return NestedNameSpecifier::GlobalSpecifier(ToContext);
7057
Nikola Smiljanic67860242014-09-26 00:28:20 +00007058 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007059 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007060 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00007061 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
7062 }
7063 return nullptr;
7064
Douglas Gregor90ebf252011-04-27 16:48:40 +00007065 case NestedNameSpecifier::TypeSpec:
7066 case NestedNameSpecifier::TypeSpecWithTemplate: {
7067 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
7068 if (!T.isNull()) {
7069 bool bTemplate = FromNNS->getKind() ==
7070 NestedNameSpecifier::TypeSpecWithTemplate;
7071 return NestedNameSpecifier::Create(ToContext, prefix,
7072 bTemplate, T.getTypePtr());
7073 }
7074 }
Craig Topper36250ad2014-05-12 05:36:57 +00007075 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007076 }
7077
7078 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00007079}
7080
Douglas Gregor14454802011-02-25 02:25:35 +00007081NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007082 // Copied from NestedNameSpecifier mostly.
7083 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
7084 NestedNameSpecifierLoc NNS = FromNNS;
7085
7086 // Push each of the nested-name-specifiers's onto a stack for
7087 // serialization in reverse order.
7088 while (NNS) {
7089 NestedNames.push_back(NNS);
7090 NNS = NNS.getPrefix();
7091 }
7092
7093 NestedNameSpecifierLocBuilder Builder;
7094
7095 while (!NestedNames.empty()) {
7096 NNS = NestedNames.pop_back_val();
7097 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
7098 if (!Spec)
7099 return NestedNameSpecifierLoc();
7100
7101 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
7102 switch (Kind) {
7103 case NestedNameSpecifier::Identifier:
7104 Builder.Extend(getToContext(),
7105 Spec->getAsIdentifier(),
7106 Import(NNS.getLocalBeginLoc()),
7107 Import(NNS.getLocalEndLoc()));
7108 break;
7109
7110 case NestedNameSpecifier::Namespace:
7111 Builder.Extend(getToContext(),
7112 Spec->getAsNamespace(),
7113 Import(NNS.getLocalBeginLoc()),
7114 Import(NNS.getLocalEndLoc()));
7115 break;
7116
7117 case NestedNameSpecifier::NamespaceAlias:
7118 Builder.Extend(getToContext(),
7119 Spec->getAsNamespaceAlias(),
7120 Import(NNS.getLocalBeginLoc()),
7121 Import(NNS.getLocalEndLoc()));
7122 break;
7123
7124 case NestedNameSpecifier::TypeSpec:
7125 case NestedNameSpecifier::TypeSpecWithTemplate: {
7126 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
7127 QualType(Spec->getAsType(), 0));
7128 Builder.Extend(getToContext(),
7129 Import(NNS.getLocalBeginLoc()),
7130 TSI->getTypeLoc(),
7131 Import(NNS.getLocalEndLoc()));
7132 break;
7133 }
7134
7135 case NestedNameSpecifier::Global:
7136 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
7137 break;
7138
7139 case NestedNameSpecifier::Super: {
7140 SourceRange ToRange = Import(NNS.getSourceRange());
7141 Builder.MakeSuper(getToContext(),
7142 Spec->getAsRecordDecl(),
7143 ToRange.getBegin(),
7144 ToRange.getEnd());
7145 }
7146 }
7147 }
7148
7149 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00007150}
7151
Douglas Gregore2e50d332010-12-01 01:36:18 +00007152TemplateName ASTImporter::Import(TemplateName From) {
7153 switch (From.getKind()) {
7154 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007155 if (auto *ToTemplate =
7156 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007157 return TemplateName(ToTemplate);
7158
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007159 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007160
7161 case TemplateName::OverloadedTemplate: {
7162 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
7163 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007164 for (auto *I : *FromStorage) {
7165 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007166 ToTemplates.addDecl(To);
7167 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007168 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007169 }
7170 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
7171 ToTemplates.end());
7172 }
7173
7174 case TemplateName::QualifiedTemplate: {
7175 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
7176 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
7177 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007178 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007179
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007180 if (auto *ToTemplate =
7181 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007182 return ToContext.getQualifiedTemplateName(Qualifier,
7183 QTN->hasTemplateKeyword(),
7184 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007185
7186 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007187 }
7188
7189 case TemplateName::DependentTemplate: {
7190 DependentTemplateName *DTN = From.getAsDependentTemplateName();
7191 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
7192 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007193 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007194
7195 if (DTN->isIdentifier()) {
7196 return ToContext.getDependentTemplateName(Qualifier,
7197 Import(DTN->getIdentifier()));
7198 }
7199
7200 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
7201 }
John McCalld9dfe3a2011-06-30 08:33:18 +00007202
7203 case TemplateName::SubstTemplateTemplateParm: {
7204 SubstTemplateTemplateParmStorage *subst
7205 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007206 auto *param =
7207 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00007208 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007209 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00007210
7211 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007212 if (replacement.isNull())
7213 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00007214
7215 return ToContext.getSubstTemplateTemplateParm(param, replacement);
7216 }
Douglas Gregor5590be02011-01-15 06:45:20 +00007217
7218 case TemplateName::SubstTemplateTemplateParmPack: {
7219 SubstTemplateTemplateParmPackStorage *SubstPack
7220 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007221 auto *Param =
7222 cast_or_null<TemplateTemplateParmDecl>(
7223 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00007224 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007225 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00007226
7227 ASTNodeImporter Importer(*this);
7228 TemplateArgument ArgPack
7229 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
7230 if (ArgPack.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007231 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00007232
7233 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
7234 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00007235 }
7236
7237 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00007238}
7239
Douglas Gregor62d311f2010-02-09 19:21:46 +00007240SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
7241 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007242 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00007243
Douglas Gregor811663e2010-02-10 00:15:17 +00007244 SourceManager &FromSM = FromContext.getSourceManager();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007245
Douglas Gregor811663e2010-02-10 00:15:17 +00007246 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Sean Callanan238d8972014-12-10 01:26:39 +00007247 FileID ToFileID = Import(Decomposed.first);
7248 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007249 return {};
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007250 SourceManager &ToSM = ToContext.getSourceManager();
7251 return ToSM.getComposedLoc(ToFileID, Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00007252}
7253
7254SourceRange ASTImporter::Import(SourceRange FromRange) {
7255 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
7256}
7257
Douglas Gregor811663e2010-02-10 00:15:17 +00007258FileID ASTImporter::Import(FileID FromID) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007259 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00007260 if (Pos != ImportedFileIDs.end())
7261 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007262
Douglas Gregor811663e2010-02-10 00:15:17 +00007263 SourceManager &FromSM = FromContext.getSourceManager();
7264 SourceManager &ToSM = ToContext.getSourceManager();
7265 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007266
7267 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00007268 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007269 if (FromSLoc.isExpansion()) {
7270 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
7271 SourceLocation ToSpLoc = Import(FromEx.getSpellingLoc());
7272 SourceLocation ToExLocS = Import(FromEx.getExpansionLocStart());
7273 unsigned TokenLen = FromSM.getFileIDSize(FromID);
7274 SourceLocation MLoc;
7275 if (FromEx.isMacroArgExpansion()) {
7276 MLoc = ToSM.createMacroArgExpansionLoc(ToSpLoc, ToExLocS, TokenLen);
7277 } else {
7278 SourceLocation ToExLocE = Import(FromEx.getExpansionLocEnd());
7279 MLoc = ToSM.createExpansionLoc(ToSpLoc, ToExLocS, ToExLocE, TokenLen,
7280 FromEx.isExpansionTokenRange());
7281 }
7282 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00007283 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007284 // Include location of this file.
7285 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
7286
7287 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
7288 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
7289 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
7290 // disk again
7291 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
7292 // than mmap the files several times.
7293 const FileEntry *Entry =
7294 ToFileManager.getFile(Cache->OrigEntry->getName());
7295 if (!Entry)
7296 return {};
7297 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
7298 FromSLoc.getFile().getFileCharacteristic());
7299 } else {
7300 // FIXME: We want to re-use the existing MemoryBuffer!
7301 const llvm::MemoryBuffer *FromBuf =
7302 Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
7303 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
7304 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
7305 FromBuf->getBufferIdentifier());
7306 ToID = ToSM.createFileID(std::move(ToBuf),
7307 FromSLoc.getFile().getFileCharacteristic());
7308 }
Douglas Gregor811663e2010-02-10 00:15:17 +00007309 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007310
Sebastian Redl99219f12010-09-30 01:03:06 +00007311 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00007312 return ToID;
7313}
7314
Sean Callanandd2c1742016-05-16 20:48:03 +00007315CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
7316 Expr *ToExpr = Import(From->getInit());
7317 if (!ToExpr && From->getInit())
7318 return nullptr;
7319
7320 if (From->isBaseInitializer()) {
7321 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7322 if (!ToTInfo && From->getTypeSourceInfo())
7323 return nullptr;
7324
7325 return new (ToContext) CXXCtorInitializer(
7326 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
7327 ToExpr, Import(From->getRParenLoc()),
7328 From->isPackExpansion() ? Import(From->getEllipsisLoc())
7329 : SourceLocation());
7330 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007331 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007332 if (!ToField && From->getMember())
7333 return nullptr;
7334
7335 return new (ToContext) CXXCtorInitializer(
7336 ToContext, ToField, Import(From->getMemberLocation()),
7337 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7338 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007339 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00007340 Import(From->getIndirectMember()));
7341 if (!ToIField && From->getIndirectMember())
7342 return nullptr;
7343
7344 return new (ToContext) CXXCtorInitializer(
7345 ToContext, ToIField, Import(From->getMemberLocation()),
7346 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7347 } else if (From->isDelegatingInitializer()) {
7348 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7349 if (!ToTInfo && From->getTypeSourceInfo())
7350 return nullptr;
7351
7352 return new (ToContext)
7353 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
7354 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007355 } else {
7356 return nullptr;
7357 }
7358}
7359
Aleksei Sidorina693b372016-09-28 10:16:56 +00007360CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
7361 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
7362 if (Pos != ImportedCXXBaseSpecifiers.end())
7363 return Pos->second;
7364
7365 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
7366 Import(BaseSpec->getSourceRange()),
7367 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
7368 BaseSpec->getAccessSpecifierAsWritten(),
7369 Import(BaseSpec->getTypeSourceInfo()),
7370 Import(BaseSpec->getEllipsisLoc()));
7371 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
7372 return Imported;
7373}
7374
Douglas Gregor0a791672011-01-18 03:11:38 +00007375void ASTImporter::ImportDefinition(Decl *From) {
7376 Decl *To = Import(From);
7377 if (!To)
7378 return;
7379
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007380 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00007381 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007382
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007383 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00007384 if (!ToRecord->getDefinition()) {
7385 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00007386 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007387 return;
7388 }
7389 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007390
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007391 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00007392 if (!ToEnum->getDefinition()) {
7393 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007394 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00007395 return;
7396 }
7397 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00007398
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007399 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007400 if (!ToIFace->getDefinition()) {
7401 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007402 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007403 return;
7404 }
7405 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007406
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007407 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007408 if (!ToProto->getDefinition()) {
7409 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007410 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007411 return;
7412 }
7413 }
7414
Douglas Gregor0a791672011-01-18 03:11:38 +00007415 Importer.ImportDeclContext(FromDC, true);
7416 }
7417}
7418
Douglas Gregor96e578d2010-02-05 17:54:41 +00007419DeclarationName ASTImporter::Import(DeclarationName FromName) {
7420 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007421 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007422
7423 switch (FromName.getNameKind()) {
7424 case DeclarationName::Identifier:
7425 return Import(FromName.getAsIdentifierInfo());
7426
7427 case DeclarationName::ObjCZeroArgSelector:
7428 case DeclarationName::ObjCOneArgSelector:
7429 case DeclarationName::ObjCMultiArgSelector:
7430 return Import(FromName.getObjCSelector());
7431
7432 case DeclarationName::CXXConstructorName: {
7433 QualType T = Import(FromName.getCXXNameType());
7434 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007435 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007436
7437 return ToContext.DeclarationNames.getCXXConstructorName(
7438 ToContext.getCanonicalType(T));
7439 }
7440
7441 case DeclarationName::CXXDestructorName: {
7442 QualType T = Import(FromName.getCXXNameType());
7443 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007444 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007445
7446 return ToContext.DeclarationNames.getCXXDestructorName(
7447 ToContext.getCanonicalType(T));
7448 }
7449
Richard Smith35845152017-02-07 01:37:30 +00007450 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007451 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00007452 Import(FromName.getCXXDeductionGuideTemplate()));
7453 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007454 return {};
Richard Smith35845152017-02-07 01:37:30 +00007455 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
7456 }
7457
Douglas Gregor96e578d2010-02-05 17:54:41 +00007458 case DeclarationName::CXXConversionFunctionName: {
7459 QualType T = Import(FromName.getCXXNameType());
7460 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007461 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007462
7463 return ToContext.DeclarationNames.getCXXConversionFunctionName(
7464 ToContext.getCanonicalType(T));
7465 }
7466
7467 case DeclarationName::CXXOperatorName:
7468 return ToContext.DeclarationNames.getCXXOperatorName(
7469 FromName.getCXXOverloadedOperator());
7470
7471 case DeclarationName::CXXLiteralOperatorName:
7472 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
7473 Import(FromName.getCXXLiteralIdentifier()));
7474
7475 case DeclarationName::CXXUsingDirective:
7476 // FIXME: STATICS!
7477 return DeclarationName::getUsingDirectiveName();
7478 }
7479
David Blaikiee4d798f2012-01-20 21:50:17 +00007480 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00007481}
7482
Douglas Gregore2e50d332010-12-01 01:36:18 +00007483IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007484 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00007485 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007486
Sean Callananf94ef1d2016-05-14 06:11:19 +00007487 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
7488
7489 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
7490 ToId->setBuiltinID(FromId->getBuiltinID());
7491
7492 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007493}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007494
Douglas Gregor43f54792010-02-17 02:12:47 +00007495Selector ASTImporter::Import(Selector FromSel) {
7496 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007497 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00007498
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007499 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00007500 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
7501 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
7502 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
7503 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
7504}
7505
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007506DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
7507 DeclContext *DC,
7508 unsigned IDNS,
7509 NamedDecl **Decls,
7510 unsigned NumDecls) {
7511 return Name;
7512}
7513
7514DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007515 if (LastDiagFromFrom)
7516 ToContext.getDiagnostics().notePriorDiagnosticFrom(
7517 FromContext.getDiagnostics());
7518 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007519 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007520}
7521
7522DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007523 if (!LastDiagFromFrom)
7524 FromContext.getDiagnostics().notePriorDiagnosticFrom(
7525 ToContext.getDiagnostics());
7526 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007527 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007528}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007529
Douglas Gregor2e15c842012-02-01 21:00:38 +00007530void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007531 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007532 if (!ID->getDefinition())
7533 ID->startDefinition();
7534 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007535 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007536 if (!PD->getDefinition())
7537 PD->startDefinition();
7538 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007539 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007540 if (!TD->getDefinition() && !TD->isBeingDefined()) {
7541 TD->startDefinition();
7542 TD->setCompleteDefinition(true);
7543 }
7544 }
7545 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007546 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00007547 }
7548}
7549
Gabor Marton26f72a92018-07-12 09:42:05 +00007550Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
7551 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
7552 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
7553 "Try to import an already imported Decl");
7554 if (Pos != ImportedDecls.end())
7555 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007556 ImportedDecls[From] = To;
7557 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00007558}
Douglas Gregorb4964f72010-02-15 23:54:17 +00007559
Douglas Gregordd6006f2012-07-17 21:16:27 +00007560bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
7561 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00007562 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00007563 = ImportedTypes.find(From.getTypePtr());
7564 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
7565 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00007566
Douglas Gregordd6006f2012-07-17 21:16:27 +00007567 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00007568 getStructuralEquivalenceKind(*this), false,
7569 Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00007570 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00007571}