blob: 8a8b3213e25452e2475aa19c9fa78ab83ff160ea [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
Balazs Keri2544b4b2018-08-08 09:40:57 +000074 unsigned ASTImporter::getFieldIndex(Decl *F) {
75 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
76 "Try to get field index for non-field.");
77
78 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
79 if (!Owner)
80 return 0;
81
82 unsigned Index = 1;
83 for (const auto *D : Owner->decls()) {
84 if (D == F)
85 return Index;
86
87 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
88 ++Index;
89 }
90
91 llvm_unreachable("Field was not found in its parent context.");
92
93 return 0;
94 }
95
Gabor Marton5254e642018-06-27 13:32:50 +000096 template <class T>
97 SmallVector<Decl*, 2>
98 getCanonicalForwardRedeclChain(Redeclarable<T>* D) {
99 SmallVector<Decl*, 2> Redecls;
100 for (auto *R : D->getFirstDecl()->redecls()) {
101 if (R != D->getFirstDecl())
102 Redecls.push_back(R);
103 }
104 Redecls.push_back(D->getFirstDecl());
105 std::reverse(Redecls.begin(), Redecls.end());
106 return Redecls;
107 }
108
109 SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) {
110 // Currently only FunctionDecl is supported
111 auto FD = cast<FunctionDecl>(D);
112 return getCanonicalForwardRedeclChain<FunctionDecl>(FD);
113 }
114
Gabor Marton26f72a92018-07-12 09:42:05 +0000115 void updateFlags(const Decl *From, Decl *To) {
116 // Check if some flags or attrs are new in 'From' and copy into 'To'.
117 // FIXME: Other flags or attrs?
118 if (From->isUsed(false) && !To->isUsed(false))
119 To->setIsUsed();
120 }
121
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000122 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000123 public DeclVisitor<ASTNodeImporter, Decl *>,
124 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000125 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000126
Gabor Marton26f72a92018-07-12 09:42:05 +0000127 // Wrapper for an overload set.
128 template <typename ToDeclT> struct CallOverloadedCreateFun {
129 template <typename... Args>
130 auto operator()(Args &&... args)
131 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) {
132 return ToDeclT::Create(std::forward<Args>(args)...);
133 }
134 };
135
136 // Always use these functions to create a Decl during import. There are
137 // certain tasks which must be done after the Decl was created, e.g. we
138 // must immediately register that as an imported Decl. The parameter `ToD`
139 // will be set to the newly created Decl or if had been imported before
140 // then to the already imported Decl. Returns a bool value set to true if
141 // the `FromD` had been imported before.
142 template <typename ToDeclT, typename FromDeclT, typename... Args>
143 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
144 Args &&... args) {
145 // There may be several overloads of ToDeclT::Create. We must make sure
146 // to call the one which would be chosen by the arguments, thus we use a
147 // wrapper for the overload set.
148 CallOverloadedCreateFun<ToDeclT> OC;
149 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
150 std::forward<Args>(args)...);
151 }
152 // Use this overload if a special Type is needed to be created. E.g if we
153 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl`
154 // then:
155 // TypedefNameDecl *ToTypedef;
156 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...);
157 template <typename NewDeclT, typename ToDeclT, typename FromDeclT,
158 typename... Args>
159 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
160 Args &&... args) {
161 CallOverloadedCreateFun<NewDeclT> OC;
162 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
163 std::forward<Args>(args)...);
164 }
165 // Use this version if a special create function must be
166 // used, e.g. CXXRecordDecl::CreateLambda .
167 template <typename ToDeclT, typename CreateFunT, typename FromDeclT,
168 typename... Args>
169 LLVM_NODISCARD bool
170 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
171 FromDeclT *FromD, Args &&... args) {
172 ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD));
173 if (ToD)
174 return true; // Already imported.
175 ToD = CreateFun(std::forward<Args>(args)...);
176 InitializeImportedDecl(FromD, ToD);
177 return false; // A new Decl is created.
178 }
179
180 void InitializeImportedDecl(Decl *FromD, Decl *ToD) {
181 Importer.MapImported(FromD, ToD);
182 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
183 if (FromD->hasAttrs())
184 for (const Attr *FromAttr : FromD->getAttrs())
185 ToD->addAttr(Importer.Import(FromAttr));
186 if (FromD->isUsed())
187 ToD->setIsUsed();
188 if (FromD->isImplicit())
189 ToD->setImplicit();
190 }
191
Douglas Gregor96e578d2010-02-05 17:54:41 +0000192 public:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000193 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
Gabor Marton344b0992018-05-16 11:48:11 +0000194
Douglas Gregor96e578d2010-02-05 17:54:41 +0000195 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +0000196 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000197 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000198
199 // Importing types
John McCall424cec92011-01-19 06:33:43 +0000200 QualType VisitType(const Type *T);
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000201 QualType VisitAtomicType(const AtomicType *T);
John McCall424cec92011-01-19 06:33:43 +0000202 QualType VisitBuiltinType(const BuiltinType *T);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000203 QualType VisitDecayedType(const DecayedType *T);
John McCall424cec92011-01-19 06:33:43 +0000204 QualType VisitComplexType(const ComplexType *T);
205 QualType VisitPointerType(const PointerType *T);
206 QualType VisitBlockPointerType(const BlockPointerType *T);
207 QualType VisitLValueReferenceType(const LValueReferenceType *T);
208 QualType VisitRValueReferenceType(const RValueReferenceType *T);
209 QualType VisitMemberPointerType(const MemberPointerType *T);
210 QualType VisitConstantArrayType(const ConstantArrayType *T);
211 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
212 QualType VisitVariableArrayType(const VariableArrayType *T);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000213 QualType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000214 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +0000215 QualType VisitVectorType(const VectorType *T);
216 QualType VisitExtVectorType(const ExtVectorType *T);
217 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
218 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000219 QualType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
Sean Callananda6df8a2011-08-11 16:56:07 +0000220 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +0000221 QualType VisitTypedefType(const TypedefType *T);
222 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000223 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +0000224 QualType VisitTypeOfType(const TypeOfType *T);
225 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +0000226 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +0000227 QualType VisitAutoType(const AutoType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000228 QualType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000229 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +0000230 QualType VisitRecordType(const RecordType *T);
231 QualType VisitEnumType(const EnumType *T);
Sean Callanan72fe0852015-04-02 23:50:08 +0000232 QualType VisitAttributedType(const AttributedType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000233 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000234 QualType VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T);
John McCall424cec92011-01-19 06:33:43 +0000235 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
236 QualType VisitElaboratedType(const ElaboratedType *T);
Peter Szecsice7f3182018-05-07 12:08:27 +0000237 QualType VisitDependentNameType(const DependentNameType *T);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000238 QualType VisitPackExpansionType(const PackExpansionType *T);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000239 QualType VisitDependentTemplateSpecializationType(
240 const DependentTemplateSpecializationType *T);
John McCall424cec92011-01-19 06:33:43 +0000241 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
242 QualType VisitObjCObjectType(const ObjCObjectType *T);
243 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Rafael Stahldf556202018-05-29 08:12:15 +0000244
245 // Importing declarations
Fangrui Song6907ce22018-07-30 19:24:48 +0000246 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
247 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +0000248 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +0000249 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000250 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
251 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +0000252 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Balazs Keri1d20cc22018-07-16 12:16:39 +0000253 void ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000254
Aleksei Sidorina693b372016-09-28 10:16:56 +0000255 bool ImportCastPath(CastExpr *E, CXXCastPath &Path);
256
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000257 using Designator = DesignatedInitExpr::Designator;
258
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000259 Designator ImportDesignator(const Designator &D);
260
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000261 Optional<LambdaCapture> ImportLambdaCapture(const LambdaCapture &From);
Fangrui Song6907ce22018-07-30 19:24:48 +0000262
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000263 /// What we should import from the definition.
Fangrui Song6907ce22018-07-30 19:24:48 +0000264 enum ImportDefinitionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000265 /// Import the default subset of the definition, which might be
Douglas Gregor95d82832012-01-24 18:36:04 +0000266 /// nothing (if minimal import is set) or might be everything (if minimal
267 /// import is not set).
268 IDK_Default,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000269
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000270 /// Import everything.
Douglas Gregor95d82832012-01-24 18:36:04 +0000271 IDK_Everything,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000272
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000273 /// Import only the bare bones needed to establish a valid
Douglas Gregor95d82832012-01-24 18:36:04 +0000274 /// DeclContext.
275 IDK_Basic
276 };
277
Douglas Gregor2e15c842012-02-01 21:00:38 +0000278 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
279 return IDK == IDK_Everything ||
280 (IDK == IDK_Default && !Importer.isMinimalImport());
281 }
282
Fangrui Song6907ce22018-07-30 19:24:48 +0000283 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000284 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000285 bool ImportDefinition(VarDecl *From, VarDecl *To,
286 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000287 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000288 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000289 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000290 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000291 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000292 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000293 TemplateParameterList *ImportTemplateParameterList(
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000294 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000295 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000296 Optional<TemplateArgumentLoc> ImportTemplateArgumentLoc(
297 const TemplateArgumentLoc &TALoc);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000298 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
299 unsigned NumFromArgs,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000300 SmallVectorImpl<TemplateArgument> &ToArgs);
301
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000302 template <typename InContainerTy>
303 bool ImportTemplateArgumentListInfo(const InContainerTy &Container,
304 TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000305
306 template<typename InContainerTy>
307 bool ImportTemplateArgumentListInfo(SourceLocation FromLAngleLoc,
308 SourceLocation FromRAngleLoc,
309 const InContainerTy &Container,
310 TemplateArgumentListInfo &Result);
311
Gabor Marton5254e642018-06-27 13:32:50 +0000312 using TemplateArgsTy = SmallVector<TemplateArgument, 8>;
313 using OptionalTemplateArgsTy = Optional<TemplateArgsTy>;
314 std::tuple<FunctionTemplateDecl *, OptionalTemplateArgsTy>
315 ImportFunctionTemplateWithTemplateArgsFromSpecialization(
316 FunctionDecl *FromFD);
317
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000318 bool ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
319
Gabor Marton950fb572018-07-17 12:39:27 +0000320 bool IsStructuralMatch(Decl *From, Decl *To, bool Complain);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000321 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
322 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000323 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
324 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000325 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000326 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000327 bool IsStructuralMatch(FunctionTemplateDecl *From,
328 FunctionTemplateDecl *To);
Balazs Keric7797c42018-07-11 09:37:24 +0000329 bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000330 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000331 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000332 Decl *VisitDecl(Decl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000333 Decl *VisitEmptyDecl(EmptyDecl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000334 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000335 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000336 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000337 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000338 Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000339 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000340 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000341 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000342 Decl *VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000343 Decl *VisitLabelDecl(LabelDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000344 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000345 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000346 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000347 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000348 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
349 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
350 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
351 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000352 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000353 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000354 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000355 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000356 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000357 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000358 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000359 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000360 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000361 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000362 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000363 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000364 Decl *VisitUsingDecl(UsingDecl *D);
365 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
366 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
367 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
368 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
369
Douglas Gregor85f3f952015-07-07 03:57:15 +0000370 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000371 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000372 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000373 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000374 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000375 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000376 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
377 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
378 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
379 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000380 Decl *VisitClassTemplateSpecializationDecl(
381 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000382 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
383 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000384 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000385
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000386 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000387 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
388
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000389 Stmt *VisitStmt(Stmt *S);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000390 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000391 Stmt *VisitDeclStmt(DeclStmt *S);
392 Stmt *VisitNullStmt(NullStmt *S);
393 Stmt *VisitCompoundStmt(CompoundStmt *S);
394 Stmt *VisitCaseStmt(CaseStmt *S);
395 Stmt *VisitDefaultStmt(DefaultStmt *S);
396 Stmt *VisitLabelStmt(LabelStmt *S);
397 Stmt *VisitAttributedStmt(AttributedStmt *S);
398 Stmt *VisitIfStmt(IfStmt *S);
399 Stmt *VisitSwitchStmt(SwitchStmt *S);
400 Stmt *VisitWhileStmt(WhileStmt *S);
401 Stmt *VisitDoStmt(DoStmt *S);
402 Stmt *VisitForStmt(ForStmt *S);
403 Stmt *VisitGotoStmt(GotoStmt *S);
404 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
405 Stmt *VisitContinueStmt(ContinueStmt *S);
406 Stmt *VisitBreakStmt(BreakStmt *S);
407 Stmt *VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000408 // FIXME: MSAsmStmt
409 // FIXME: SEHExceptStmt
410 // FIXME: SEHFinallyStmt
411 // FIXME: SEHTryStmt
412 // FIXME: SEHLeaveStmt
413 // FIXME: CapturedStmt
414 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
415 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
416 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
417 // FIXME: MSDependentExistsStmt
418 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
419 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
420 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
421 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
422 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
423 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
424 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000425
426 // Importing expressions
427 Expr *VisitExpr(Expr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000428 Expr *VisitVAArgExpr(VAArgExpr *E);
429 Expr *VisitGNUNullExpr(GNUNullExpr *E);
430 Expr *VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000431 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000432 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
433 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
434 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000435 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000436 Expr *VisitFloatingLiteral(FloatingLiteral *E);
Gabor Martonbf7f18b2018-08-09 12:18:07 +0000437 Expr *VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000438 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000439 Expr *VisitStringLiteral(StringLiteral *E);
440 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
441 Expr *VisitAtomicExpr(AtomicExpr *E);
442 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000443 Expr *VisitParenExpr(ParenExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000444 Expr *VisitParenListExpr(ParenListExpr *E);
445 Expr *VisitStmtExpr(StmtExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000446 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000447 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000448 Expr *VisitBinaryOperator(BinaryOperator *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000449 Expr *VisitConditionalOperator(ConditionalOperator *E);
450 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
451 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000452 Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
453 Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E);
454 Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000455 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000456 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000457 Expr *VisitExplicitCastExpr(ExplicitCastExpr *E);
458 Expr *VisitOffsetOfExpr(OffsetOfExpr *OE);
459 Expr *VisitCXXThrowExpr(CXXThrowExpr *E);
460 Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
461 Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
462 Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
463 Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
464 Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE);
465 Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000466 Expr *VisitPackExpansionExpr(PackExpansionExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000467 Expr *VisitSizeOfPackExpr(SizeOfPackExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000468 Expr *VisitCXXNewExpr(CXXNewExpr *CE);
469 Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000470 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000471 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000472 Expr *VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
Peter Szecsice7f3182018-05-07 12:08:27 +0000473 Expr *VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +0000474 Expr *VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *CE);
475 Expr *VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
Peter Szecsice7f3182018-05-07 12:08:27 +0000476 Expr *VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000477 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC);
Sean Callanan8bca9962016-03-28 21:43:01 +0000478 Expr *VisitCXXThisExpr(CXXThisExpr *E);
479 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +0000480 Expr *VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000481 Expr *VisitMemberExpr(MemberExpr *E);
482 Expr *VisitCallExpr(CallExpr *E);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000483 Expr *VisitLambdaExpr(LambdaExpr *LE);
Sean Callanan8bca9962016-03-28 21:43:01 +0000484 Expr *VisitInitListExpr(InitListExpr *E);
Gabor Marton07b01ff2018-06-29 12:17:34 +0000485 Expr *VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
Balazs Keri95baa842018-07-25 10:21:06 +0000486 Expr *VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E);
Richard Smith30e304e2016-12-14 00:03:17 +0000487 Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
488 Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
Sean Callanandd2c1742016-05-16 20:48:03 +0000489 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
490 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000491 Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +0000492 Expr *VisitTypeTraitExpr(TypeTraitExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000493 Expr *VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000494
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000495 template<typename IIter, typename OIter>
496 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000497 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
498
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000499 ASTImporter &ImporterRef = Importer;
500 std::transform(Ibegin, Iend, Obegin,
501 [&ImporterRef](ItemT From) -> ItemT {
502 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000503 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000504 }
505
506 template<typename IIter, typename OIter>
507 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000508 using ItemT = typename std::remove_reference<decltype(**Obegin)>::type;
509
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000510 ASTImporter &ImporterRef = Importer;
511 bool Failed = false;
512 std::transform(Ibegin, Iend, Obegin,
513 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000514 auto *To = cast_or_null<ItemT>(ImporterRef.Import(From));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000515 if (!To && From)
516 Failed = true;
517 return To;
518 });
519 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000520 }
Aleksei Sidorina693b372016-09-28 10:16:56 +0000521
522 template<typename InContainerTy, typename OutContainerTy>
523 bool ImportContainerChecked(const InContainerTy &InContainer,
524 OutContainerTy &OutContainer) {
525 return ImportArrayChecked(InContainer.begin(), InContainer.end(),
526 OutContainer.begin());
527 }
528
529 template<typename InContainerTy, typename OIter>
530 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
531 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
532 }
Lang Hames19e07e12017-06-20 21:06:00 +0000533
534 // Importing overrides.
535 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000536
537 FunctionDecl *FindFunctionTemplateSpecialization(FunctionDecl *FromFD);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000538 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000539
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000540template <typename InContainerTy>
541bool ASTNodeImporter::ImportTemplateArgumentListInfo(
542 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
543 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
544 TemplateArgumentListInfo ToTAInfo(Importer.Import(FromLAngleLoc),
545 Importer.Import(FromRAngleLoc));
546 if (ImportTemplateArgumentListInfo(Container, ToTAInfo))
547 return true;
548 Result = ToTAInfo;
549 return false;
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000550}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000551
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000552template <>
553bool ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
554 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
555 return ImportTemplateArgumentListInfo(
556 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
557}
558
559template <>
560bool ASTNodeImporter::ImportTemplateArgumentListInfo<
561 ASTTemplateArgumentListInfo>(const ASTTemplateArgumentListInfo &From,
562 TemplateArgumentListInfo &Result) {
563 return ImportTemplateArgumentListInfo(From.LAngleLoc, From.RAngleLoc,
564 From.arguments(), Result);
565}
566
Gabor Marton5254e642018-06-27 13:32:50 +0000567std::tuple<FunctionTemplateDecl *, ASTNodeImporter::OptionalTemplateArgsTy>
568ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
569 FunctionDecl *FromFD) {
570 assert(FromFD->getTemplatedKind() ==
571 FunctionDecl::TK_FunctionTemplateSpecialization);
572 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
573 auto *Template = cast_or_null<FunctionTemplateDecl>(
574 Importer.Import(FTSInfo->getTemplate()));
575
576 // Import template arguments.
577 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
578 TemplateArgsTy ToTemplArgs;
579 if (ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
580 ToTemplArgs)) // Error during import.
581 return std::make_tuple(Template, OptionalTemplateArgsTy());
582
583 return std::make_tuple(Template, ToTemplArgs);
584}
585
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000586} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000587
Douglas Gregor3996e242010-02-15 22:01:00 +0000588//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000589// Import Types
590//----------------------------------------------------------------------------
591
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000592using namespace clang;
593
John McCall424cec92011-01-19 06:33:43 +0000594QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000595 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
596 << T->getTypeClassName();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000597 return {};
Douglas Gregore4c83e42010-02-09 22:48:33 +0000598}
599
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000600QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
601 QualType UnderlyingType = Importer.Import(T->getValueType());
602 if(UnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000603 return {};
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000604
605 return Importer.getToContext().getAtomicType(UnderlyingType);
606}
607
John McCall424cec92011-01-19 06:33:43 +0000608QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000609 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000610#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
611 case BuiltinType::Id: \
612 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000613#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000614#define SHARED_SINGLETON_TYPE(Expansion)
615#define BUILTIN_TYPE(Id, SingletonId) \
616 case BuiltinType::Id: return Importer.getToContext().SingletonId;
617#include "clang/AST/BuiltinTypes.def"
618
619 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
620 // context supports C++.
621
622 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
623 // context supports ObjC.
624
Douglas Gregor96e578d2010-02-05 17:54:41 +0000625 case BuiltinType::Char_U:
Fangrui Song6907ce22018-07-30 19:24:48 +0000626 // The context we're importing from has an unsigned 'char'. If we're
627 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +0000628 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000629 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000630 return Importer.getToContext().UnsignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +0000631
Douglas Gregor96e578d2010-02-05 17:54:41 +0000632 return Importer.getToContext().CharTy;
633
Douglas Gregor96e578d2010-02-05 17:54:41 +0000634 case BuiltinType::Char_S:
Fangrui Song6907ce22018-07-30 19:24:48 +0000635 // The context we're importing from has an unsigned 'char'. If we're
636 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +0000637 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000638 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000639 return Importer.getToContext().SignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +0000640
Douglas Gregor96e578d2010-02-05 17:54:41 +0000641 return Importer.getToContext().CharTy;
642
Chris Lattnerad3467e2010-12-25 23:25:43 +0000643 case BuiltinType::WChar_S:
644 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000645 // FIXME: If not in C++, shall we translate to the C equivalent of
646 // wchar_t?
647 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000648 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000649
650 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000651}
652
Aleksei Sidorina693b372016-09-28 10:16:56 +0000653QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
654 QualType OrigT = Importer.Import(T->getOriginalType());
655 if (OrigT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000656 return {};
Aleksei Sidorina693b372016-09-28 10:16:56 +0000657
658 return Importer.getToContext().getDecayedType(OrigT);
659}
660
John McCall424cec92011-01-19 06:33:43 +0000661QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000662 QualType ToElementType = Importer.Import(T->getElementType());
663 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000664 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000665
Douglas Gregor96e578d2010-02-05 17:54:41 +0000666 return Importer.getToContext().getComplexType(ToElementType);
667}
668
John McCall424cec92011-01-19 06:33:43 +0000669QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000670 QualType ToPointeeType = Importer.Import(T->getPointeeType());
671 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000672 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000673
Douglas Gregor96e578d2010-02-05 17:54:41 +0000674 return Importer.getToContext().getPointerType(ToPointeeType);
675}
676
John McCall424cec92011-01-19 06:33:43 +0000677QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000678 // FIXME: Check for blocks support in "to" context.
679 QualType ToPointeeType = Importer.Import(T->getPointeeType());
680 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000681 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000682
Douglas Gregor96e578d2010-02-05 17:54:41 +0000683 return Importer.getToContext().getBlockPointerType(ToPointeeType);
684}
685
John McCall424cec92011-01-19 06:33:43 +0000686QualType
687ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000688 // FIXME: Check for C++ support in "to" context.
689 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
690 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000691 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000692
Douglas Gregor96e578d2010-02-05 17:54:41 +0000693 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
694}
695
John McCall424cec92011-01-19 06:33:43 +0000696QualType
697ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000698 // FIXME: Check for C++0x support in "to" context.
699 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
700 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000701 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000702
703 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000704}
705
John McCall424cec92011-01-19 06:33:43 +0000706QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000707 // FIXME: Check for C++ support in "to" context.
708 QualType ToPointeeType = Importer.Import(T->getPointeeType());
709 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000710 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000711
Douglas Gregor96e578d2010-02-05 17:54:41 +0000712 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
Fangrui Song6907ce22018-07-30 19:24:48 +0000713 return Importer.getToContext().getMemberPointerType(ToPointeeType,
Douglas Gregor96e578d2010-02-05 17:54:41 +0000714 ClassType.getTypePtr());
715}
716
John McCall424cec92011-01-19 06:33:43 +0000717QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000718 QualType ToElementType = Importer.Import(T->getElementType());
719 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000720 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000721
722 return Importer.getToContext().getConstantArrayType(ToElementType,
Douglas Gregor96e578d2010-02-05 17:54:41 +0000723 T->getSize(),
724 T->getSizeModifier(),
725 T->getIndexTypeCVRQualifiers());
726}
727
John McCall424cec92011-01-19 06:33:43 +0000728QualType
729ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000730 QualType ToElementType = Importer.Import(T->getElementType());
731 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000732 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000733
734 return Importer.getToContext().getIncompleteArrayType(ToElementType,
Douglas Gregor96e578d2010-02-05 17:54:41 +0000735 T->getSizeModifier(),
736 T->getIndexTypeCVRQualifiers());
737}
738
John McCall424cec92011-01-19 06:33:43 +0000739QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000740 QualType ToElementType = Importer.Import(T->getElementType());
741 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000742 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000743
744 Expr *Size = Importer.Import(T->getSizeExpr());
745 if (!Size)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000746 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000747
Douglas Gregor96e578d2010-02-05 17:54:41 +0000748 SourceRange Brackets = Importer.Import(T->getBracketsRange());
749 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
750 T->getSizeModifier(),
751 T->getIndexTypeCVRQualifiers(),
752 Brackets);
753}
754
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000755QualType ASTNodeImporter::VisitDependentSizedArrayType(
756 const DependentSizedArrayType *T) {
757 QualType ToElementType = Importer.Import(T->getElementType());
758 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000759 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000760
761 // SizeExpr may be null if size is not specified directly.
762 // For example, 'int a[]'.
763 Expr *Size = Importer.Import(T->getSizeExpr());
764 if (!Size && T->getSizeExpr())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000765 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000766
767 SourceRange Brackets = Importer.Import(T->getBracketsRange());
768 return Importer.getToContext().getDependentSizedArrayType(
769 ToElementType, Size, T->getSizeModifier(), T->getIndexTypeCVRQualifiers(),
770 Brackets);
771}
772
John McCall424cec92011-01-19 06:33:43 +0000773QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000774 QualType ToElementType = Importer.Import(T->getElementType());
775 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000776 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000777
778 return Importer.getToContext().getVectorType(ToElementType,
Douglas Gregor96e578d2010-02-05 17:54:41 +0000779 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +0000780 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000781}
782
John McCall424cec92011-01-19 06:33:43 +0000783QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000784 QualType ToElementType = Importer.Import(T->getElementType());
785 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000786 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000787
788 return Importer.getToContext().getExtVectorType(ToElementType,
Douglas Gregor96e578d2010-02-05 17:54:41 +0000789 T->getNumElements());
790}
791
John McCall424cec92011-01-19 06:33:43 +0000792QualType
793ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000794 // FIXME: What happens if we're importing a function without a prototype
Douglas Gregor96e578d2010-02-05 17:54:41 +0000795 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +0000796 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000797 if (ToResultType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000798 return {};
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000799
Douglas Gregor96e578d2010-02-05 17:54:41 +0000800 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000801 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000802}
803
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +0000804QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000805 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000806 if (ToResultType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000807 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000808
Douglas Gregor96e578d2010-02-05 17:54:41 +0000809 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000810 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000811 for (const auto &A : T->param_types()) {
812 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000813 if (ArgType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000814 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000815 ArgTypes.push_back(ArgType);
816 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000817
Douglas Gregor96e578d2010-02-05 17:54:41 +0000818 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000819 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000820 for (const auto &E : T->exceptions()) {
821 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000822 if (ExceptionType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000823 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000824 ExceptionTypes.push_back(ExceptionType);
825 }
John McCalldb40c7f2010-12-14 08:05:40 +0000826
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000827 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
828 FunctionProtoType::ExtProtoInfo ToEPI;
829
830 ToEPI.ExtInfo = FromEPI.ExtInfo;
831 ToEPI.Variadic = FromEPI.Variadic;
832 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
833 ToEPI.TypeQuals = FromEPI.TypeQuals;
834 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +0000835 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
836 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
837 ToEPI.ExceptionSpec.NoexceptExpr =
838 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
839 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
840 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
841 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
842 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000843
Jordan Rose5c382722013-03-08 21:51:21 +0000844 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000845}
846
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000847QualType ASTNodeImporter::VisitUnresolvedUsingType(
848 const UnresolvedUsingType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000849 const auto *ToD =
850 cast_or_null<UnresolvedUsingTypenameDecl>(Importer.Import(T->getDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000851 if (!ToD)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000852 return {};
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000853
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000854 auto *ToPrevD =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000855 cast_or_null<UnresolvedUsingTypenameDecl>(
856 Importer.Import(T->getDecl()->getPreviousDecl()));
857 if (!ToPrevD && T->getDecl()->getPreviousDecl())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000858 return {};
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000859
860 return Importer.getToContext().getTypeDeclType(ToD, ToPrevD);
861}
862
Sean Callananda6df8a2011-08-11 16:56:07 +0000863QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
864 QualType ToInnerType = Importer.Import(T->getInnerType());
865 if (ToInnerType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000866 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000867
Sean Callananda6df8a2011-08-11 16:56:07 +0000868 return Importer.getToContext().getParenType(ToInnerType);
869}
870
John McCall424cec92011-01-19 06:33:43 +0000871QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000872 auto *ToDecl =
873 dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000874 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000875 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000876
Douglas Gregor96e578d2010-02-05 17:54:41 +0000877 return Importer.getToContext().getTypeDeclType(ToDecl);
878}
879
John McCall424cec92011-01-19 06:33:43 +0000880QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000881 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
882 if (!ToExpr)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000883 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000884
Douglas Gregor96e578d2010-02-05 17:54:41 +0000885 return Importer.getToContext().getTypeOfExprType(ToExpr);
886}
887
John McCall424cec92011-01-19 06:33:43 +0000888QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000889 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
890 if (ToUnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000891 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000892
Douglas Gregor96e578d2010-02-05 17:54:41 +0000893 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
894}
895
John McCall424cec92011-01-19 06:33:43 +0000896QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +0000897 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +0000898 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
899 if (!ToExpr)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000900 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +0000901
Douglas Gregor81495f32012-02-12 18:42:33 +0000902 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
903 if (UnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000904 return {};
Douglas Gregor81495f32012-02-12 18:42:33 +0000905
906 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000907}
908
Alexis Hunte852b102011-05-24 22:41:36 +0000909QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
910 QualType ToBaseType = Importer.Import(T->getBaseType());
911 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
912 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000913 return {};
Alexis Hunte852b102011-05-24 22:41:36 +0000914
915 return Importer.getToContext().getUnaryTransformType(ToBaseType,
916 ToUnderlyingType,
917 T->getUTTKind());
918}
919
Richard Smith30482bc2011-02-20 03:19:35 +0000920QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +0000921 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +0000922 QualType FromDeduced = T->getDeducedType();
923 QualType ToDeduced;
924 if (!FromDeduced.isNull()) {
925 ToDeduced = Importer.Import(FromDeduced);
926 if (ToDeduced.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000927 return {};
Richard Smith30482bc2011-02-20 03:19:35 +0000928 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000929
Richard Smithe301ba22015-11-11 02:02:15 +0000930 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +0000931 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +0000932}
933
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000934QualType ASTNodeImporter::VisitInjectedClassNameType(
935 const InjectedClassNameType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000936 auto *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000937 if (!D)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000938 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000939
940 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
941 if (InjType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000942 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000943
944 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
945 // See comments in InjectedClassNameType definition for details
946 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
947 enum {
948 TypeAlignmentInBits = 4,
949 TypeAlignment = 1 << TypeAlignmentInBits
950 };
951
952 return QualType(new (Importer.getToContext(), TypeAlignment)
953 InjectedClassNameType(D, InjType), 0);
954}
955
John McCall424cec92011-01-19 06:33:43 +0000956QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000957 auto *ToDecl = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000958 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000959 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000960
961 return Importer.getToContext().getTagDeclType(ToDecl);
962}
963
John McCall424cec92011-01-19 06:33:43 +0000964QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000965 auto *ToDecl = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000966 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000967 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000968
969 return Importer.getToContext().getTagDeclType(ToDecl);
970}
971
Sean Callanan72fe0852015-04-02 23:50:08 +0000972QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
973 QualType FromModifiedType = T->getModifiedType();
974 QualType FromEquivalentType = T->getEquivalentType();
975 QualType ToModifiedType;
976 QualType ToEquivalentType;
977
978 if (!FromModifiedType.isNull()) {
979 ToModifiedType = Importer.Import(FromModifiedType);
980 if (ToModifiedType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000981 return {};
Sean Callanan72fe0852015-04-02 23:50:08 +0000982 }
983 if (!FromEquivalentType.isNull()) {
984 ToEquivalentType = Importer.Import(FromEquivalentType);
985 if (ToEquivalentType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000986 return {};
Sean Callanan72fe0852015-04-02 23:50:08 +0000987 }
988
989 return Importer.getToContext().getAttributedType(T->getAttrKind(),
990 ToModifiedType, ToEquivalentType);
991}
992
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000993QualType ASTNodeImporter::VisitTemplateTypeParmType(
994 const TemplateTypeParmType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000995 auto *ParmDecl =
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000996 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
997 if (!ParmDecl && T->getDecl())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000998 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000999
1000 return Importer.getToContext().getTemplateTypeParmType(
1001 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
1002}
1003
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001004QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
1005 const SubstTemplateTypeParmType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001006 const auto *Replaced =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001007 cast_or_null<TemplateTypeParmType>(Importer.Import(
1008 QualType(T->getReplacedParameter(), 0)).getTypePtr());
1009 if (!Replaced)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001010 return {};
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001011
1012 QualType Replacement = Importer.Import(T->getReplacementType());
1013 if (Replacement.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001014 return {};
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001015 Replacement = Replacement.getCanonicalType();
1016
1017 return Importer.getToContext().getSubstTemplateTypeParmType(
1018 Replaced, Replacement);
1019}
1020
Douglas Gregore2e50d332010-12-01 01:36:18 +00001021QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001022 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001023 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1024 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001025 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00001026
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001027 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001028 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001029 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00001030
Douglas Gregore2e50d332010-12-01 01:36:18 +00001031 QualType ToCanonType;
1032 if (!QualType(T, 0).isCanonical()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001033 QualType FromCanonType
Douglas Gregore2e50d332010-12-01 01:36:18 +00001034 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1035 ToCanonType =Importer.Import(FromCanonType);
1036 if (ToCanonType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001037 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001038 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001039 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +00001040 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001041 ToCanonType);
1042}
1043
John McCall424cec92011-01-19 06:33:43 +00001044QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +00001045 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001046 // Note: the qualifier in an ElaboratedType is optional.
1047 if (T->getQualifier()) {
1048 ToQualifier = Importer.Import(T->getQualifier());
1049 if (!ToQualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001050 return {};
Abramo Bagnara6150c882010-05-11 21:36:43 +00001051 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001052
1053 QualType ToNamedType = Importer.Import(T->getNamedType());
1054 if (ToNamedType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001055 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001056
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001057 TagDecl *OwnedTagDecl =
1058 cast_or_null<TagDecl>(Importer.Import(T->getOwnedTagDecl()));
1059 if (!OwnedTagDecl && T->getOwnedTagDecl())
1060 return {};
1061
Abramo Bagnara6150c882010-05-11 21:36:43 +00001062 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001063 ToQualifier, ToNamedType,
1064 OwnedTagDecl);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001065}
1066
Gabor Horvath7a91c082017-11-14 11:30:38 +00001067QualType ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
1068 QualType Pattern = Importer.Import(T->getPattern());
1069 if (Pattern.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001070 return {};
Gabor Horvath7a91c082017-11-14 11:30:38 +00001071
1072 return Importer.getToContext().getPackExpansionType(Pattern,
1073 T->getNumExpansions());
1074}
1075
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001076QualType ASTNodeImporter::VisitDependentTemplateSpecializationType(
1077 const DependentTemplateSpecializationType *T) {
1078 NestedNameSpecifier *Qualifier = Importer.Import(T->getQualifier());
1079 if (!Qualifier && T->getQualifier())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001080 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001081
1082 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
1083 if (!Name && T->getIdentifier())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001084 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001085
1086 SmallVector<TemplateArgument, 2> ToPack;
1087 ToPack.reserve(T->getNumArgs());
1088 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToPack))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001089 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001090
1091 return Importer.getToContext().getDependentTemplateSpecializationType(
1092 T->getKeyword(), Qualifier, Name, ToPack);
1093}
1094
Peter Szecsice7f3182018-05-07 12:08:27 +00001095QualType ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
1096 NestedNameSpecifier *NNS = Importer.Import(T->getQualifier());
1097 if (!NNS && T->getQualifier())
1098 return QualType();
1099
1100 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
1101 if (!Name && T->getIdentifier())
1102 return QualType();
1103
1104 QualType Canon = (T == T->getCanonicalTypeInternal().getTypePtr())
1105 ? QualType()
1106 : Importer.Import(T->getCanonicalTypeInternal());
1107 if (!Canon.isNull())
1108 Canon = Canon.getCanonicalType();
1109
1110 return Importer.getToContext().getDependentNameType(T->getKeyword(), NNS,
1111 Name, Canon);
1112}
1113
John McCall424cec92011-01-19 06:33:43 +00001114QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001115 auto *Class =
1116 dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001117 if (!Class)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001118 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001119
John McCall8b07ec22010-05-15 11:32:37 +00001120 return Importer.getToContext().getObjCInterfaceType(Class);
1121}
1122
John McCall424cec92011-01-19 06:33:43 +00001123QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001124 QualType ToBaseType = Importer.Import(T->getBaseType());
1125 if (ToBaseType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001126 return {};
John McCall8b07ec22010-05-15 11:32:37 +00001127
Douglas Gregore9d95f12015-07-07 03:57:35 +00001128 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001129 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001130 QualType ImportedTypeArg = Importer.Import(TypeArg);
1131 if (ImportedTypeArg.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001132 return {};
Douglas Gregore9d95f12015-07-07 03:57:35 +00001133
1134 TypeArgs.push_back(ImportedTypeArg);
1135 }
1136
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001137 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001138 for (auto *P : T->quals()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001139 auto *Protocol = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001140 if (!Protocol)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001141 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001142 Protocols.push_back(Protocol);
1143 }
1144
Douglas Gregore9d95f12015-07-07 03:57:35 +00001145 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001146 Protocols,
1147 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001148}
1149
John McCall424cec92011-01-19 06:33:43 +00001150QualType
1151ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001152 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1153 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001154 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001155
John McCall8b07ec22010-05-15 11:32:37 +00001156 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001157}
1158
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001159//----------------------------------------------------------------------------
1160// Import Declarations
1161//----------------------------------------------------------------------------
Fangrui Song6907ce22018-07-30 19:24:48 +00001162bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1163 DeclContext *&LexicalDC,
1164 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00001165 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001166 SourceLocation &Loc) {
Gabor Marton6e1510c2018-07-12 11:50:21 +00001167 // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop.
1168 // example: int struct_in_proto(struct data_t{int a;int b;} *d);
1169 DeclContext *OrigDC = D->getDeclContext();
1170 FunctionDecl *FunDecl;
1171 if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) &&
1172 FunDecl->hasBody()) {
Gabor Martonfe68e292018-08-06 14:38:37 +00001173 auto getLeafPointeeType = [](const Type *T) {
1174 while (T->isPointerType() || T->isArrayType()) {
1175 T = T->getPointeeOrArrayElementType();
1176 }
1177 return T;
1178 };
1179 for (const ParmVarDecl *P : FunDecl->parameters()) {
1180 const Type *LeafT =
1181 getLeafPointeeType(P->getType().getCanonicalType().getTypePtr());
1182 auto *RT = dyn_cast<RecordType>(LeafT);
1183 if (RT && RT->getDecl() == D) {
1184 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1185 << D->getDeclKindName();
1186 return true;
1187 }
Gabor Marton6e1510c2018-07-12 11:50:21 +00001188 }
1189 }
1190
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001191 // Import the context of this declaration.
Gabor Marton6e1510c2018-07-12 11:50:21 +00001192 DC = Importer.ImportContext(OrigDC);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001193 if (!DC)
1194 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001195
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001196 LexicalDC = DC;
1197 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1198 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1199 if (!LexicalDC)
1200 return true;
1201 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001202
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001203 // Import the name of this declaration.
1204 Name = Importer.Import(D->getDeclName());
1205 if (D->getDeclName() && !Name)
1206 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001207
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001208 // Import the location of this declaration.
1209 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00001210 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001211 return false;
1212}
1213
Douglas Gregord451ea92011-07-29 23:31:30 +00001214void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1215 if (!FromD)
1216 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001217
Douglas Gregord451ea92011-07-29 23:31:30 +00001218 if (!ToD) {
1219 ToD = Importer.Import(FromD);
1220 if (!ToD)
1221 return;
1222 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001223
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001224 if (auto *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1225 if (auto *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00001226 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001227 ImportDefinition(FromRecord, ToRecord);
1228 }
1229 }
1230 return;
1231 }
1232
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001233 if (auto *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1234 if (auto *ToEnum = cast_or_null<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001235 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1236 ImportDefinition(FromEnum, ToEnum);
1237 }
1238 }
1239 return;
1240 }
1241}
1242
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001243void
1244ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1245 DeclarationNameInfo& To) {
1246 // NOTE: To.Name and To.Loc are already imported.
1247 // We only have to import To.LocInfo.
1248 switch (To.getName().getNameKind()) {
1249 case DeclarationName::Identifier:
1250 case DeclarationName::ObjCZeroArgSelector:
1251 case DeclarationName::ObjCOneArgSelector:
1252 case DeclarationName::ObjCMultiArgSelector:
1253 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001254 case DeclarationName::CXXDeductionGuideName:
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001255 return;
1256
1257 case DeclarationName::CXXOperatorName: {
1258 SourceRange Range = From.getCXXOperatorNameRange();
1259 To.setCXXOperatorNameRange(Importer.Import(Range));
1260 return;
1261 }
1262 case DeclarationName::CXXLiteralOperatorName: {
1263 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1264 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1265 return;
1266 }
1267 case DeclarationName::CXXConstructorName:
1268 case DeclarationName::CXXDestructorName:
1269 case DeclarationName::CXXConversionFunctionName: {
1270 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1271 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1272 return;
1273 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001274 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001275 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001276}
1277
Fangrui Song6907ce22018-07-30 19:24:48 +00001278void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001279 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001280 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001281 return;
1282 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001283
Aaron Ballman629afae2014-03-07 19:56:05 +00001284 for (auto *From : FromDC->decls())
1285 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00001286}
1287
Balazs Keri1d20cc22018-07-16 12:16:39 +00001288void ASTNodeImporter::ImportImplicitMethods(
1289 const CXXRecordDecl *From, CXXRecordDecl *To) {
1290 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1291 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001292
Balazs Keri1d20cc22018-07-16 12:16:39 +00001293 for (CXXMethodDecl *FromM : From->methods())
1294 if (FromM->isImplicit())
1295 Importer.Import(FromM);
1296}
1297
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001298static void setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1299 ASTImporter &Importer) {
1300 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
1301 auto *ToTypedef =
1302 cast_or_null<TypedefNameDecl>(Importer.Import(FromTypedef));
1303 assert (ToTypedef && "Failed to import typedef of an anonymous structure");
1304
1305 To->setTypedefNameForAnonDecl(ToTypedef);
1306 }
1307}
1308
Fangrui Song6907ce22018-07-30 19:24:48 +00001309bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001310 ImportDefinitionKind Kind) {
1311 if (To->getDefinition() || To->isBeingDefined()) {
1312 if (Kind == IDK_Everything)
1313 ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001314
Douglas Gregore2e50d332010-12-01 01:36:18 +00001315 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001316 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001317
Douglas Gregore2e50d332010-12-01 01:36:18 +00001318 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001319
1320 setTypedefNameForAnonDecl(From, To, Importer);
Fangrui Song6907ce22018-07-30 19:24:48 +00001321
Douglas Gregore2e50d332010-12-01 01:36:18 +00001322 // Add base classes.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001323 if (auto *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1324 auto *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001325
1326 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1327 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1328 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001329 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001330 ToData.Aggregate = FromData.Aggregate;
1331 ToData.PlainOldData = FromData.PlainOldData;
1332 ToData.Empty = FromData.Empty;
1333 ToData.Polymorphic = FromData.Polymorphic;
1334 ToData.Abstract = FromData.Abstract;
1335 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001336 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1337 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1338 ToData.HasBasesWithNonStaticDataMembers =
1339 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001340 ToData.HasPrivateFields = FromData.HasPrivateFields;
1341 ToData.HasProtectedFields = FromData.HasProtectedFields;
1342 ToData.HasPublicFields = FromData.HasPublicFields;
1343 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001344 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001345 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001346 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001347 ToData.HasUninitializedReferenceMember
1348 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001349 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001350 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1351 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001352 ToData.NeedOverloadResolutionForCopyConstructor
1353 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001354 ToData.NeedOverloadResolutionForMoveConstructor
1355 = FromData.NeedOverloadResolutionForMoveConstructor;
1356 ToData.NeedOverloadResolutionForMoveAssignment
1357 = FromData.NeedOverloadResolutionForMoveAssignment;
1358 ToData.NeedOverloadResolutionForDestructor
1359 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001360 ToData.DefaultedCopyConstructorIsDeleted
1361 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001362 ToData.DefaultedMoveConstructorIsDeleted
1363 = FromData.DefaultedMoveConstructorIsDeleted;
1364 ToData.DefaultedMoveAssignmentIsDeleted
1365 = FromData.DefaultedMoveAssignmentIsDeleted;
1366 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001367 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1368 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001369 ToData.HasConstexprNonCopyMoveConstructor
1370 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001371 ToData.HasDefaultedDefaultConstructor
1372 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001373 ToData.DefaultedDefaultConstructorIsConstexpr
1374 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001375 ToData.HasConstexprDefaultConstructor
1376 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001377 ToData.HasNonLiteralTypeFieldsOrBases
1378 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001379 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001380 ToData.UserProvidedDefaultConstructor
1381 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001382 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001383 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1384 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1385 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1386 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001387 ToData.ImplicitCopyAssignmentHasConstParam
1388 = FromData.ImplicitCopyAssignmentHasConstParam;
1389 ToData.HasDeclaredCopyConstructorWithConstParam
1390 = FromData.HasDeclaredCopyConstructorWithConstParam;
1391 ToData.HasDeclaredCopyAssignmentWithConstParam
1392 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001393
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001394 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001395 for (const auto &Base1 : FromCXX->bases()) {
1396 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001397 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001398 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001399
1400 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001401 if (Base1.isPackExpansion())
1402 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001403
1404 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00001405 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Fangrui Song6907ce22018-07-30 19:24:48 +00001406
Douglas Gregore2e50d332010-12-01 01:36:18 +00001407 Bases.push_back(
Fangrui Song6907ce22018-07-30 19:24:48 +00001408 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00001409 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1410 Base1.isVirtual(),
1411 Base1.isBaseOfClass(),
1412 Base1.getAccessSpecifierAsWritten(),
1413 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00001414 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001415 }
1416 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001417 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001418 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001419
Douglas Gregor2e15c842012-02-01 21:00:38 +00001420 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001421 ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001422
Douglas Gregore2e50d332010-12-01 01:36:18 +00001423 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001424 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001425}
1426
Larisse Voufo39a1e502013-08-06 01:03:05 +00001427bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1428 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00001429 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00001430 return false;
1431
1432 // FIXME: Can we really import any initializer? Alternatively, we could force
1433 // ourselves to import every declaration of a variable and then only use
1434 // getInit() here.
1435 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1436
1437 // FIXME: Other bits to merge?
1438
1439 return false;
1440}
1441
Fangrui Song6907ce22018-07-30 19:24:48 +00001442bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001443 ImportDefinitionKind Kind) {
1444 if (To->getDefinition() || To->isBeingDefined()) {
1445 if (Kind == IDK_Everything)
1446 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001447 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001448 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001449
Douglas Gregord451ea92011-07-29 23:31:30 +00001450 To->startDefinition();
1451
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001452 setTypedefNameForAnonDecl(From, To, Importer);
1453
Douglas Gregord451ea92011-07-29 23:31:30 +00001454 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1455 if (T.isNull())
1456 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001457
Douglas Gregord451ea92011-07-29 23:31:30 +00001458 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1459 if (ToPromotionType.isNull())
1460 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001461
1462 if (shouldForceImportDeclContext(Kind))
1463 ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001464
Douglas Gregord451ea92011-07-29 23:31:30 +00001465 // FIXME: we might need to merge the number of positive or negative bits
1466 // if the enumerator lists don't match.
1467 To->completeDefinition(T, ToPromotionType,
1468 From->getNumPositiveBits(),
1469 From->getNumNegativeBits());
1470 return false;
1471}
1472
Douglas Gregora082a492010-11-30 19:14:50 +00001473TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1474 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001475 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1476 if (ImportContainerChecked(*Params, ToParams))
1477 return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001478
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001479 Expr *ToRequiresClause;
1480 if (Expr *const R = Params->getRequiresClause()) {
1481 ToRequiresClause = Importer.Import(R);
1482 if (!ToRequiresClause)
1483 return nullptr;
1484 } else {
1485 ToRequiresClause = nullptr;
1486 }
1487
Douglas Gregora082a492010-11-30 19:14:50 +00001488 return TemplateParameterList::Create(Importer.getToContext(),
1489 Importer.Import(Params->getTemplateLoc()),
1490 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00001491 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001492 Importer.Import(Params->getRAngleLoc()),
1493 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001494}
1495
Fangrui Song6907ce22018-07-30 19:24:48 +00001496TemplateArgument
Douglas Gregore2e50d332010-12-01 01:36:18 +00001497ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1498 switch (From.getKind()) {
1499 case TemplateArgument::Null:
1500 return TemplateArgument();
Fangrui Song6907ce22018-07-30 19:24:48 +00001501
Douglas Gregore2e50d332010-12-01 01:36:18 +00001502 case TemplateArgument::Type: {
1503 QualType ToType = Importer.Import(From.getAsType());
1504 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001505 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001506 return TemplateArgument(ToType);
1507 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001508
Douglas Gregore2e50d332010-12-01 01:36:18 +00001509 case TemplateArgument::Integral: {
1510 QualType ToType = Importer.Import(From.getIntegralType());
1511 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001512 return {};
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001513 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001514 }
1515
Eli Friedmanb826a002012-09-26 02:36:12 +00001516 case TemplateArgument::Declaration: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001517 auto *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001518 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1519 if (!To || ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001520 return {};
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001521 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00001522 }
1523
1524 case TemplateArgument::NullPtr: {
1525 QualType ToType = Importer.Import(From.getNullPtrType());
1526 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001527 return {};
Eli Friedmanb826a002012-09-26 02:36:12 +00001528 return TemplateArgument(ToType, /*isNullPtr*/true);
1529 }
1530
Douglas Gregore2e50d332010-12-01 01:36:18 +00001531 case TemplateArgument::Template: {
1532 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1533 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001534 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00001535
Douglas Gregore2e50d332010-12-01 01:36:18 +00001536 return TemplateArgument(ToTemplate);
1537 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001538
1539 case TemplateArgument::TemplateExpansion: {
Fangrui Song6907ce22018-07-30 19:24:48 +00001540 TemplateName ToTemplate
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001541 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1542 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001543 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00001544
Douglas Gregore1d60df2011-01-14 23:41:42 +00001545 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001546 }
1547
Douglas Gregore2e50d332010-12-01 01:36:18 +00001548 case TemplateArgument::Expression:
1549 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1550 return TemplateArgument(ToExpr);
1551 return TemplateArgument();
Fangrui Song6907ce22018-07-30 19:24:48 +00001552
Douglas Gregore2e50d332010-12-01 01:36:18 +00001553 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001554 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001555 ToPack.reserve(From.pack_size());
1556 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001557 return {};
Benjamin Kramercce63472015-08-05 09:40:22 +00001558
1559 return TemplateArgument(
1560 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001561 }
1562 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001563
Douglas Gregore2e50d332010-12-01 01:36:18 +00001564 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00001565}
1566
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001567Optional<TemplateArgumentLoc>
1568ASTNodeImporter::ImportTemplateArgumentLoc(const TemplateArgumentLoc &TALoc) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001569 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1570 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1571 TemplateArgumentLocInfo ToInfo;
1572 if (Arg.getKind() == TemplateArgument::Expression) {
1573 Expr *E = Importer.Import(FromInfo.getAsExpr());
1574 ToInfo = TemplateArgumentLocInfo(E);
1575 if (!E)
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001576 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001577 } else if (Arg.getKind() == TemplateArgument::Type) {
1578 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1579 ToInfo = TemplateArgumentLocInfo(TSI);
1580 else
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001581 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001582 } else {
1583 ToInfo = TemplateArgumentLocInfo(
1584 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1585 Importer.Import(FromInfo.getTemplateNameLoc()),
1586 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1587 }
1588 return TemplateArgumentLoc(Arg, ToInfo);
1589}
1590
Douglas Gregore2e50d332010-12-01 01:36:18 +00001591bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1592 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001593 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001594 for (unsigned I = 0; I != NumFromArgs; ++I) {
1595 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1596 if (To.isNull() && !FromArgs[I].isNull())
1597 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001598
Douglas Gregore2e50d332010-12-01 01:36:18 +00001599 ToArgs.push_back(To);
1600 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001601
Douglas Gregore2e50d332010-12-01 01:36:18 +00001602 return false;
1603}
1604
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001605// We cannot use Optional<> pattern here and below because
1606// TemplateArgumentListInfo's operator new is declared as deleted so it cannot
1607// be stored in Optional.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001608template <typename InContainerTy>
1609bool ASTNodeImporter::ImportTemplateArgumentListInfo(
1610 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1611 for (const auto &FromLoc : Container) {
1612 if (auto ToLoc = ImportTemplateArgumentLoc(FromLoc))
1613 ToTAInfo.addArgument(*ToLoc);
1614 else
1615 return true;
1616 }
1617 return false;
1618}
1619
Gabor Marton26f72a92018-07-12 09:42:05 +00001620static StructuralEquivalenceKind
1621getStructuralEquivalenceKind(const ASTImporter &Importer) {
1622 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
1623 : StructuralEquivalenceKind::Default;
1624}
1625
Gabor Marton950fb572018-07-17 12:39:27 +00001626bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
1627 StructuralEquivalenceContext Ctx(
1628 Importer.getFromContext(), Importer.getToContext(),
1629 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1630 false, Complain);
1631 return Ctx.IsEquivalent(From, To);
1632}
1633
1634bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001635 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001636 // Eliminate a potential failure point where we attempt to re-import
1637 // something we're trying to import while completing ToRecord.
1638 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1639 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001640 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00001641 if (ToOriginRecord)
1642 ToRecord = ToOriginRecord;
1643 }
1644
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001645 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001646 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001647 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001648 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001649 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001650 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001651}
1652
Larisse Voufo39a1e502013-08-06 01:03:05 +00001653bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1654 bool Complain) {
1655 StructuralEquivalenceContext Ctx(
1656 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001657 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1658 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001659 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001660}
1661
Douglas Gregor98c10182010-02-12 22:17:39 +00001662bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Gabor Marton26f72a92018-07-12 09:42:05 +00001663 StructuralEquivalenceContext Ctx(
1664 Importer.getFromContext(), Importer.getToContext(),
1665 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00001666 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001667}
1668
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001669bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1670 FunctionTemplateDecl *To) {
1671 StructuralEquivalenceContext Ctx(
1672 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001673 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1674 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00001675 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001676}
1677
Balazs Keric7797c42018-07-11 09:37:24 +00001678bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
1679 StructuralEquivalenceContext Ctx(
1680 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001681 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1682 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00001683 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00001684}
1685
Douglas Gregor91155082012-11-14 22:29:20 +00001686bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001687 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00001688 const llvm::APSInt &FromVal = FromEC->getInitVal();
1689 const llvm::APSInt &ToVal = ToEC->getInitVal();
1690
1691 return FromVal.isSigned() == ToVal.isSigned() &&
1692 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1693 FromVal == ToVal;
1694}
1695
1696bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001697 ClassTemplateDecl *To) {
1698 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1699 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001700 Importer.getNonEquivalentDecls(),
1701 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00001702 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00001703}
1704
Larisse Voufo39a1e502013-08-06 01:03:05 +00001705bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1706 VarTemplateDecl *To) {
1707 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1708 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001709 Importer.getNonEquivalentDecls(),
1710 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00001711 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001712}
1713
Douglas Gregore4c83e42010-02-09 22:48:33 +00001714Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001715 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001716 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00001717 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00001718}
1719
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001720Decl *ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
1721 // Import the context of this declaration.
1722 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1723 if (!DC)
1724 return nullptr;
1725
1726 DeclContext *LexicalDC = DC;
1727 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1728 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1729 if (!LexicalDC)
1730 return nullptr;
1731 }
1732
1733 // Import the location of this declaration.
1734 SourceLocation Loc = Importer.Import(D->getLocation());
1735
Gabor Marton26f72a92018-07-12 09:42:05 +00001736 EmptyDecl *ToD;
1737 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, Loc))
1738 return ToD;
1739
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001740 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001741 LexicalDC->addDeclInternal(ToD);
1742 return ToD;
1743}
1744
Sean Callanan65198272011-11-17 23:20:56 +00001745Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001746 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00001747 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00001748
Gabor Marton26f72a92018-07-12 09:42:05 +00001749 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00001750
Sean Callanan65198272011-11-17 23:20:56 +00001751 return ToD;
1752}
1753
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001754Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001755 SourceLocation Loc = Importer.Import(D->getLocation());
1756 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1757
1758 // Import the context of this declaration.
1759 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1760 if (!DC)
1761 return nullptr;
1762
Gabor Marton26f72a92018-07-12 09:42:05 +00001763 AccessSpecDecl *ToD;
1764 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
1765 DC, Loc, ColonLoc))
1766 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001767
1768 // Lexical DeclContext and Semantic DeclContext
1769 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00001770 ToD->setLexicalDeclContext(DC);
1771 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001772
Gabor Marton26f72a92018-07-12 09:42:05 +00001773 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001774}
1775
Aleksei Sidorina693b372016-09-28 10:16:56 +00001776Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1777 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1778 if (!DC)
1779 return nullptr;
1780
1781 DeclContext *LexicalDC = DC;
1782
1783 // Import the location of this declaration.
1784 SourceLocation Loc = Importer.Import(D->getLocation());
1785
1786 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1787 if (!AssertExpr)
1788 return nullptr;
1789
1790 StringLiteral *FromMsg = D->getMessage();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001791 auto *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
Aleksei Sidorina693b372016-09-28 10:16:56 +00001792 if (!ToMsg && FromMsg)
1793 return nullptr;
1794
Gabor Marton26f72a92018-07-12 09:42:05 +00001795 StaticAssertDecl *ToD;
1796 if (GetImportedOrCreateDecl(
1797 ToD, D, Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1798 Importer.Import(D->getRParenLoc()), D->isFailed()))
1799 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001800
1801 ToD->setLexicalDeclContext(LexicalDC);
1802 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00001803 return ToD;
1804}
1805
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001806Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1807 // Import the major distinguishing characteristics of this namespace.
1808 DeclContext *DC, *LexicalDC;
1809 DeclarationName Name;
1810 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001811 NamedDecl *ToD;
1812 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001813 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001814 if (ToD)
1815 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001816
1817 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001818 if (!Name) {
1819 // This is an anonymous namespace. Adopt an existing anonymous
1820 // namespace if we can.
1821 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001822 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001823 MergeWithNamespace = TU->getAnonymousNamespace();
1824 else
1825 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1826 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001827 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001828 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001829 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001830 for (auto *FoundDecl : FoundDecls) {
1831 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001832 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00001833
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001834 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001835 MergeWithNamespace = FoundNS;
1836 ConflictingDecls.clear();
1837 break;
1838 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001839
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001840 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001841 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001842
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001843 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001844 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +00001845 ConflictingDecls.data(),
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001846 ConflictingDecls.size());
1847 }
1848 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001849
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001850 // Create the "to" namespace, if needed.
1851 NamespaceDecl *ToNamespace = MergeWithNamespace;
1852 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00001853 if (GetImportedOrCreateDecl(
1854 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001855 Importer.Import(D->getBeginLoc()), Loc, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001856 /*PrevDecl=*/nullptr))
1857 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001858 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001859 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00001860
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001861 // If this is an anonymous namespace, register it as the anonymous
1862 // namespace within its context.
1863 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001864 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001865 TU->setAnonymousNamespace(ToNamespace);
1866 else
1867 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1868 }
1869 }
Gabor Marton26f72a92018-07-12 09:42:05 +00001870 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00001871
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001872 ImportDeclContext(D);
Fangrui Song6907ce22018-07-30 19:24:48 +00001873
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001874 return ToNamespace;
1875}
1876
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001877Decl *ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1878 // Import the major distinguishing characteristics of this namespace.
1879 DeclContext *DC, *LexicalDC;
1880 DeclarationName Name;
1881 SourceLocation Loc;
1882 NamedDecl *LookupD;
1883 if (ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
1884 return nullptr;
1885 if (LookupD)
1886 return LookupD;
1887
1888 // NOTE: No conflict resolution is done for namespace aliases now.
1889
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001890 auto *TargetDecl = cast_or_null<NamespaceDecl>(
1891 Importer.Import(D->getNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001892 if (!TargetDecl)
1893 return nullptr;
1894
1895 IdentifierInfo *ToII = Importer.Import(D->getIdentifier());
1896 if (!ToII)
1897 return nullptr;
1898
1899 NestedNameSpecifierLoc ToQLoc = Importer.Import(D->getQualifierLoc());
1900 if (D->getQualifierLoc() && !ToQLoc)
1901 return nullptr;
1902
Gabor Marton26f72a92018-07-12 09:42:05 +00001903 NamespaceAliasDecl *ToD;
1904 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC,
1905 Importer.Import(D->getNamespaceLoc()),
1906 Importer.Import(D->getAliasLoc()), ToII, ToQLoc,
1907 Importer.Import(D->getTargetNameLoc()),
1908 TargetDecl))
1909 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001910
1911 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001912 LexicalDC->addDeclInternal(ToD);
1913
1914 return ToD;
1915}
1916
Richard Smithdda56e42011-04-15 14:24:37 +00001917Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001918 // Import the major distinguishing characteristics of this typedef.
1919 DeclContext *DC, *LexicalDC;
1920 DeclarationName Name;
1921 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001922 NamedDecl *ToD;
1923 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001924 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001925 if (ToD)
1926 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001927
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001928 // If this typedef is not in block scope, determine whether we've
1929 // seen a typedef with the same name (that we can merge with) or any
1930 // other entity by that name (which name lookup could conflict with).
1931 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001932 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001933 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001934 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001935 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001936 for (auto *FoundDecl : FoundDecls) {
1937 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001938 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001939 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001940 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1941 FoundTypedef->getUnderlyingType()))
Gabor Marton26f72a92018-07-12 09:42:05 +00001942 return Importer.MapImported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001943 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001944
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001945 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001946 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001947
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001948 if (!ConflictingDecls.empty()) {
1949 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00001950 ConflictingDecls.data(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001951 ConflictingDecls.size());
1952 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001953 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001954 }
1955 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001956
Douglas Gregorb4964f72010-02-15 23:54:17 +00001957 // Import the underlying type of this typedef;
1958 QualType T = Importer.Import(D->getUnderlyingType());
1959 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001960 return nullptr;
1961
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001962 // Create the new typedef node.
1963 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001964 SourceLocation StartL = Importer.Import(D->getBeginLoc());
Gabor Marton26f72a92018-07-12 09:42:05 +00001965
Richard Smithdda56e42011-04-15 14:24:37 +00001966 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00001967 if (IsAlias) {
1968 if (GetImportedOrCreateDecl<TypeAliasDecl>(
1969 ToTypedef, D, Importer.getToContext(), DC, StartL, Loc,
1970 Name.getAsIdentifierInfo(), TInfo))
1971 return ToTypedef;
1972 } else if (GetImportedOrCreateDecl<TypedefDecl>(
1973 ToTypedef, D, Importer.getToContext(), DC, StartL, Loc,
1974 Name.getAsIdentifierInfo(), TInfo))
1975 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001976
Douglas Gregordd483172010-02-22 17:42:47 +00001977 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001978 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001979
1980 // Templated declarations should not appear in DeclContext.
1981 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
1982 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
1983 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001984
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001985 return ToTypedef;
1986}
1987
Richard Smithdda56e42011-04-15 14:24:37 +00001988Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1989 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1990}
1991
1992Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1993 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1994}
1995
Gabor Horvath7a91c082017-11-14 11:30:38 +00001996Decl *ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1997 // Import the major distinguishing characteristics of this typedef.
1998 DeclContext *DC, *LexicalDC;
1999 DeclarationName Name;
2000 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002001 NamedDecl *FoundD;
2002 if (ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002003 return nullptr;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002004 if (FoundD)
2005 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002006
2007 // If this typedef is not in block scope, determine whether we've
2008 // seen a typedef with the same name (that we can merge with) or any
2009 // other entity by that name (which name lookup could conflict with).
2010 if (!DC->isFunctionOrMethod()) {
2011 SmallVector<NamedDecl *, 4> ConflictingDecls;
2012 unsigned IDNS = Decl::IDNS_Ordinary;
2013 SmallVector<NamedDecl *, 2> FoundDecls;
2014 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002015 for (auto *FoundDecl : FoundDecls) {
2016 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002017 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002018 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002019 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002020 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002021 }
2022
2023 if (!ConflictingDecls.empty()) {
2024 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2025 ConflictingDecls.data(),
2026 ConflictingDecls.size());
2027 if (!Name)
2028 return nullptr;
2029 }
2030 }
2031
2032 TemplateParameterList *Params = ImportTemplateParameterList(
2033 D->getTemplateParameters());
2034 if (!Params)
2035 return nullptr;
2036
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002037 auto *TemplDecl = cast_or_null<TypeAliasDecl>(
Gabor Horvath7a91c082017-11-14 11:30:38 +00002038 Importer.Import(D->getTemplatedDecl()));
2039 if (!TemplDecl)
2040 return nullptr;
2041
Gabor Marton26f72a92018-07-12 09:42:05 +00002042 TypeAliasTemplateDecl *ToAlias;
2043 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
2044 Name, Params, TemplDecl))
2045 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002046
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002047 TemplDecl->setDescribedAliasTemplate(ToAlias);
2048
Gabor Horvath7a91c082017-11-14 11:30:38 +00002049 ToAlias->setAccess(D->getAccess());
2050 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002051 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002052 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002053}
2054
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002055Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
2056 // Import the major distinguishing characteristics of this label.
2057 DeclContext *DC, *LexicalDC;
2058 DeclarationName Name;
2059 SourceLocation Loc;
2060 NamedDecl *ToD;
2061 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2062 return nullptr;
2063 if (ToD)
2064 return ToD;
2065
2066 assert(LexicalDC->isFunctionOrMethod());
2067
Gabor Marton26f72a92018-07-12 09:42:05 +00002068 LabelDecl *ToLabel;
2069 if (D->isGnuLocal()
2070 ? GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC,
2071 Importer.Import(D->getLocation()),
2072 Name.getAsIdentifierInfo(),
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002073 Importer.Import(D->getBeginLoc()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002074 : GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC,
2075 Importer.Import(D->getLocation()),
2076 Name.getAsIdentifierInfo()))
2077 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002078
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002079 auto *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002080 if (!Label)
2081 return nullptr;
2082
2083 ToLabel->setStmt(Label);
2084 ToLabel->setLexicalDeclContext(LexicalDC);
2085 LexicalDC->addDeclInternal(ToLabel);
2086 return ToLabel;
2087}
2088
Douglas Gregor98c10182010-02-12 22:17:39 +00002089Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2090 // Import the major distinguishing characteristics of this enum.
2091 DeclContext *DC, *LexicalDC;
2092 DeclarationName Name;
2093 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002094 NamedDecl *ToD;
2095 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002096 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002097 if (ToD)
2098 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002099
Douglas Gregor98c10182010-02-12 22:17:39 +00002100 // Figure out what enum name we're looking for.
2101 unsigned IDNS = Decl::IDNS_Tag;
2102 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002103 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2104 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002105 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002106 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002107 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002108
Douglas Gregor98c10182010-02-12 22:17:39 +00002109 // We may already have an enum of the same name; try to find and match it.
2110 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002111 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002112 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002113 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002114 for (auto *FoundDecl : FoundDecls) {
2115 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002116 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002117
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002118 Decl *Found = FoundDecl;
2119 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2120 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor98c10182010-02-12 22:17:39 +00002121 Found = Tag->getDecl();
2122 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002123
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002124 if (auto *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002125 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002126 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002127 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002128
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002129 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002130 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002131
Douglas Gregor98c10182010-02-12 22:17:39 +00002132 if (!ConflictingDecls.empty()) {
2133 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002134 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002135 ConflictingDecls.size());
2136 }
2137 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002138
Douglas Gregor98c10182010-02-12 22:17:39 +00002139 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002140 EnumDecl *D2;
2141 if (GetImportedOrCreateDecl(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002142 D2, D, Importer.getToContext(), DC, Importer.Import(D->getBeginLoc()),
Gabor Marton26f72a92018-07-12 09:42:05 +00002143 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2144 D->isScopedUsingClassTag(), D->isFixed()))
2145 return D2;
2146
John McCall3e11ebe2010-03-15 10:12:16 +00002147 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002148 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002149 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002150 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002151 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002152
2153 // Import the integer type.
2154 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2155 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002156 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00002157 D2->setIntegerType(ToIntegerType);
Fangrui Song6907ce22018-07-30 19:24:48 +00002158
Douglas Gregor98c10182010-02-12 22:17:39 +00002159 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002160 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00002161 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002162
Douglas Gregor3996e242010-02-15 22:01:00 +00002163 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002164}
2165
Douglas Gregor5c73e912010-02-11 00:48:18 +00002166Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002167 bool IsFriendTemplate = false;
2168 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2169 IsFriendTemplate =
2170 DCXX->getDescribedClassTemplate() &&
2171 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
2172 Decl::FOK_None;
2173 }
2174
Douglas Gregor5c73e912010-02-11 00:48:18 +00002175 // If this record has a definition in the translation unit we're coming from,
2176 // but this particular declaration is not that definition, import the
2177 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002178 TagDecl *Definition = D->getDefinition();
Gabor Martona3af5672018-05-23 14:24:02 +00002179 if (Definition && Definition != D &&
Balazs Keri0c23dc52018-08-13 13:08:37 +00002180 // Friend template declaration must be imported on its own.
2181 !IsFriendTemplate &&
Gabor Martona3af5672018-05-23 14:24:02 +00002182 // In contrast to a normal CXXRecordDecl, the implicit
2183 // CXXRecordDecl of ClassTemplateSpecializationDecl is its redeclaration.
2184 // The definition of the implicit CXXRecordDecl in this case is the
2185 // ClassTemplateSpecializationDecl itself. Thus, we start with an extra
2186 // condition in order to be able to import the implict Decl.
2187 !D->isImplicit()) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002188 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002189 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002190 return nullptr;
2191
Gabor Marton26f72a92018-07-12 09:42:05 +00002192 return Importer.MapImported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002193 }
Gabor Martona3af5672018-05-23 14:24:02 +00002194
Douglas Gregor5c73e912010-02-11 00:48:18 +00002195 // Import the major distinguishing characteristics of this record.
2196 DeclContext *DC, *LexicalDC;
2197 DeclarationName Name;
2198 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002199 NamedDecl *ToD;
2200 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002201 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002202 if (ToD)
2203 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002204
Douglas Gregor5c73e912010-02-11 00:48:18 +00002205 // Figure out what structure name we're looking for.
2206 unsigned IDNS = Decl::IDNS_Tag;
2207 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002208 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2209 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002210 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002211 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002212 IDNS |= Decl::IDNS_Ordinary;
2213
2214 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002215 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00002216 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002217 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002218 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002219 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002220 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00002221
2222 if (!FoundDecls.empty()) {
2223 // We're going to have to compare D against potentially conflicting Decls, so complete it.
2224 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2225 D->getASTContext().getExternalSource()->CompleteType(D);
2226 }
2227
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002228 for (auto *FoundDecl : FoundDecls) {
2229 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002230 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002231
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002232 Decl *Found = FoundDecl;
2233 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2234 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002235 Found = Tag->getDecl();
2236 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002237
2238 if (D->getDescribedTemplate()) {
2239 if (auto *Template = dyn_cast<ClassTemplateDecl>(Found))
2240 Found = Template->getTemplatedDecl();
2241 else
2242 continue;
2243 }
2244
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002245 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Aleksei Sidorin499de6c2018-04-05 15:31:49 +00002246 if (!SearchName) {
Gabor Marton0bebf952018-07-05 09:51:13 +00002247 if (!IsStructuralMatch(D, FoundRecord, false))
2248 continue;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002249 }
2250
Sean Callanan9092d472017-05-13 00:46:33 +00002251 PrevDecl = FoundRecord;
2252
Douglas Gregor25791052010-02-12 00:09:27 +00002253 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002254 if ((SearchName && !D->isCompleteDefinition() && !IsFriendTemplate)
Douglas Gregordd6006f2012-07-17 21:16:27 +00002255 || (D->isCompleteDefinition() &&
2256 D->isAnonymousStructOrUnion()
2257 == FoundDef->isAnonymousStructOrUnion() &&
2258 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002259 // The record types structurally match, or the "from" translation
2260 // unit only had a forward declaration anyway; call it the same
2261 // function.
Balazs Keri1d20cc22018-07-16 12:16:39 +00002262 // FIXME: Structural equivalence check should check for same
2263 // user-defined methods.
2264 Importer.MapImported(D, FoundDef);
2265 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2266 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2267 assert(FoundCXX && "Record type mismatch");
2268
2269 if (D->isCompleteDefinition() && !Importer.isMinimalImport())
2270 // FoundDef may not have every implicit method that D has
2271 // because implicit methods are created only if they are used.
2272 ImportImplicitMethods(DCXX, FoundCXX);
2273 }
2274 return FoundDef;
Douglas Gregor25791052010-02-12 00:09:27 +00002275 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002276 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002277 // We have a forward declaration of this type, so adopt that forward
2278 // declaration rather than building a new one.
Fangrui Song6907ce22018-07-30 19:24:48 +00002279
Sean Callananc94711c2014-03-04 18:11:50 +00002280 // If one or both can be completed from external storage then try one
2281 // last time to complete and compare them before doing this.
Fangrui Song6907ce22018-07-30 19:24:48 +00002282
Sean Callananc94711c2014-03-04 18:11:50 +00002283 if (FoundRecord->hasExternalLexicalStorage() &&
2284 !FoundRecord->isCompleteDefinition())
2285 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2286 if (D->hasExternalLexicalStorage())
2287 D->getASTContext().getExternalSource()->CompleteType(D);
Fangrui Song6907ce22018-07-30 19:24:48 +00002288
Sean Callananc94711c2014-03-04 18:11:50 +00002289 if (FoundRecord->isCompleteDefinition() &&
2290 D->isCompleteDefinition() &&
2291 !IsStructuralMatch(D, FoundRecord))
2292 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002293
Balazs Keri0c23dc52018-08-13 13:08:37 +00002294 if (IsFriendTemplate)
2295 continue;
2296
Douglas Gregor25791052010-02-12 00:09:27 +00002297 AdoptDecl = FoundRecord;
2298 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002299 } else if (!SearchName) {
2300 continue;
2301 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002302 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002303
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002304 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002305 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002306
Douglas Gregordd6006f2012-07-17 21:16:27 +00002307 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002308 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002309 ConflictingDecls.data(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002310 ConflictingDecls.size());
2311 }
2312 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002313
Douglas Gregor5c73e912010-02-11 00:48:18 +00002314 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002315 RecordDecl *D2 = AdoptDecl;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002316 SourceLocation StartLoc = Importer.Import(D->getBeginLoc());
Douglas Gregor3996e242010-02-15 22:01:00 +00002317 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002318 CXXRecordDecl *D2CXX = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002319 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002320 if (DCXX->isLambda()) {
2321 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
Gabor Marton26f72a92018-07-12 09:42:05 +00002322 if (GetImportedOrCreateSpecialDecl(
2323 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
2324 DC, TInfo, Loc, DCXX->isDependentLambda(),
2325 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2326 return D2CXX;
Sean Callanan8bca9962016-03-28 21:43:01 +00002327 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2328 if (DCXX->getLambdaContextDecl() && !CDecl)
2329 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00002330 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002331 } else if (DCXX->isInjectedClassName()) {
2332 // We have to be careful to do a similar dance to the one in
2333 // Sema::ActOnStartCXXMemberDeclarations
2334 CXXRecordDecl *const PrevDecl = nullptr;
2335 const bool DelayTypeCreation = true;
Gabor Marton26f72a92018-07-12 09:42:05 +00002336 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2337 D->getTagKind(), DC, StartLoc, Loc,
2338 Name.getAsIdentifierInfo(), PrevDecl,
2339 DelayTypeCreation))
2340 return D2CXX;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002341 Importer.getToContext().getTypeDeclType(
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002342 D2CXX, dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002343 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002344 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2345 D->getTagKind(), DC, StartLoc, Loc,
2346 Name.getAsIdentifierInfo(),
2347 cast_or_null<CXXRecordDecl>(PrevDecl)))
2348 return D2CXX;
Sean Callanan8bca9962016-03-28 21:43:01 +00002349 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002350
Douglas Gregor3996e242010-02-15 22:01:00 +00002351 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002352 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002353 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonde8bf262018-05-17 09:46:07 +00002354 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002355 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002356
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002357 if (ClassTemplateDecl *FromDescribed =
2358 DCXX->getDescribedClassTemplate()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002359 auto *ToDescribed = cast_or_null<ClassTemplateDecl>(
2360 Importer.Import(FromDescribed));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002361 if (!ToDescribed)
2362 return nullptr;
2363 D2CXX->setDescribedClassTemplate(ToDescribed);
Balazs Keri0c23dc52018-08-13 13:08:37 +00002364 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
Gabor Marton5915777e2018-06-26 13:44:24 +00002365 // In a record describing a template the type should be an
2366 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2367 // previously set type to the correct value here (ToDescribed is not
2368 // available at record create).
2369 // FIXME: The previous type is cleared but not removed from
2370 // ASTContext's internal storage.
2371 CXXRecordDecl *Injected = nullptr;
2372 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2373 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2374 if (Record && Record->isInjectedClassName()) {
2375 Injected = Record;
2376 break;
2377 }
2378 }
2379 D2CXX->setTypeForDecl(nullptr);
2380 Importer.getToContext().getInjectedClassNameType(D2CXX,
2381 ToDescribed->getInjectedClassNameSpecialization());
2382 if (Injected) {
2383 Injected->setTypeForDecl(nullptr);
2384 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2385 }
2386 }
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002387 } else if (MemberSpecializationInfo *MemberInfo =
2388 DCXX->getMemberSpecializationInfo()) {
2389 TemplateSpecializationKind SK =
2390 MemberInfo->getTemplateSpecializationKind();
2391 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002392 auto *ToInst =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002393 cast_or_null<CXXRecordDecl>(Importer.Import(FromInst));
2394 if (FromInst && !ToInst)
2395 return nullptr;
2396 D2CXX->setInstantiationOfMemberClass(ToInst, SK);
2397 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2398 Importer.Import(MemberInfo->getPointOfInstantiation()));
2399 }
Douglas Gregor25791052010-02-12 00:09:27 +00002400 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002401 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
2402 D->getTagKind(), DC, StartLoc, Loc,
2403 Name.getAsIdentifierInfo(), PrevDecl))
2404 return D2;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002405 D2->setLexicalDeclContext(LexicalDC);
2406 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002407 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002408
Douglas Gregor14454802011-02-25 02:25:35 +00002409 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd6006f2012-07-17 21:16:27 +00002410 if (D->isAnonymousStructOrUnion())
2411 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002412 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002413
2414 Importer.MapImported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002415
Douglas Gregor95d82832012-01-24 18:36:04 +00002416 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002417 return nullptr;
2418
Douglas Gregor3996e242010-02-15 22:01:00 +00002419 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002420}
2421
Douglas Gregor98c10182010-02-12 22:17:39 +00002422Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2423 // Import the major distinguishing characteristics of this enumerator.
2424 DeclContext *DC, *LexicalDC;
2425 DeclarationName Name;
2426 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002427 NamedDecl *ToD;
2428 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002429 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002430 if (ToD)
2431 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002432
2433 QualType T = Importer.Import(D->getType());
2434 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002435 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002436
Fangrui Song6907ce22018-07-30 19:24:48 +00002437 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002438 // in the same context.
2439 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002440 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002441 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002442 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002443 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002444 for (auto *FoundDecl : FoundDecls) {
2445 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002446 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002447
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002448 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002449 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002450 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002451 }
2452
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002453 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002454 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002455
Douglas Gregor98c10182010-02-12 22:17:39 +00002456 if (!ConflictingDecls.empty()) {
2457 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002458 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002459 ConflictingDecls.size());
2460 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002461 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002462 }
2463 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002464
Douglas Gregor98c10182010-02-12 22:17:39 +00002465 Expr *Init = Importer.Import(D->getInitExpr());
2466 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002467 return nullptr;
2468
Gabor Marton26f72a92018-07-12 09:42:05 +00002469 EnumConstantDecl *ToEnumerator;
2470 if (GetImportedOrCreateDecl(
2471 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2472 Name.getAsIdentifierInfo(), T, Init, D->getInitVal()))
2473 return ToEnumerator;
2474
Douglas Gregordd483172010-02-22 17:42:47 +00002475 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002476 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002477 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002478 return ToEnumerator;
2479}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002480
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002481bool ASTNodeImporter::ImportTemplateInformation(FunctionDecl *FromFD,
2482 FunctionDecl *ToFD) {
2483 switch (FromFD->getTemplatedKind()) {
2484 case FunctionDecl::TK_NonTemplate:
2485 case FunctionDecl::TK_FunctionTemplate:
Sam McCallfdc32072018-01-26 12:06:44 +00002486 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002487
2488 case FunctionDecl::TK_MemberSpecialization: {
2489 auto *InstFD = cast_or_null<FunctionDecl>(
2490 Importer.Import(FromFD->getInstantiatedFromMemberFunction()));
2491 if (!InstFD)
2492 return true;
2493
2494 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
2495 SourceLocation POI = Importer.Import(
2496 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation());
2497 ToFD->setInstantiationOfMemberFunction(InstFD, TSK);
2498 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002499 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002500 }
2501
2502 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Gabor Marton5254e642018-06-27 13:32:50 +00002503 FunctionTemplateDecl* Template;
2504 OptionalTemplateArgsTy ToTemplArgs;
2505 std::tie(Template, ToTemplArgs) =
2506 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
2507 if (!Template || !ToTemplArgs)
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002508 return true;
2509
2510 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Gabor Marton5254e642018-06-27 13:32:50 +00002511 Importer.getToContext(), *ToTemplArgs);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002512
Gabor Marton5254e642018-06-27 13:32:50 +00002513 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002514 TemplateArgumentListInfo ToTAInfo;
2515 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002516 if (FromTAArgsAsWritten)
2517 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002518 return true;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002519
2520 SourceLocation POI = Importer.Import(FTSInfo->getPointOfInstantiation());
2521
Gabor Marton5254e642018-06-27 13:32:50 +00002522 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002523 ToFD->setFunctionTemplateSpecialization(
2524 Template, ToTAList, /* InsertPos= */ nullptr,
2525 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002526 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002527 }
2528
2529 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2530 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2531 UnresolvedSet<8> TemplDecls;
2532 unsigned NumTemplates = FromInfo->getNumTemplates();
2533 for (unsigned I = 0; I < NumTemplates; I++) {
2534 if (auto *ToFTD = cast_or_null<FunctionTemplateDecl>(
2535 Importer.Import(FromInfo->getTemplate(I))))
2536 TemplDecls.addDecl(ToFTD);
2537 else
2538 return true;
2539 }
2540
2541 // Import TemplateArgumentListInfo.
2542 TemplateArgumentListInfo ToTAInfo;
2543 if (ImportTemplateArgumentListInfo(
2544 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2545 llvm::makeArrayRef(FromInfo->getTemplateArgs(),
2546 FromInfo->getNumTemplateArgs()),
2547 ToTAInfo))
2548 return true;
2549
2550 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2551 TemplDecls, ToTAInfo);
Sam McCallfdc32072018-01-26 12:06:44 +00002552 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002553 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002554 }
Sam McCallfdc32072018-01-26 12:06:44 +00002555 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002556}
2557
Gabor Marton5254e642018-06-27 13:32:50 +00002558FunctionDecl *
2559ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
2560 FunctionTemplateDecl* Template;
2561 OptionalTemplateArgsTy ToTemplArgs;
2562 std::tie(Template, ToTemplArgs) =
2563 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
2564 if (!Template || !ToTemplArgs)
2565 return nullptr;
2566
2567 void *InsertPos = nullptr;
2568 auto *FoundSpec = Template->findSpecialization(*ToTemplArgs, InsertPos);
2569 return FoundSpec;
2570}
2571
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002572Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00002573
2574 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
2575 auto RedeclIt = Redecls.begin();
2576 // Import the first part of the decl chain. I.e. import all previous
2577 // declarations starting from the canonical decl.
2578 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt)
2579 if (!Importer.Import(*RedeclIt))
2580 return nullptr;
2581 assert(*RedeclIt == D);
2582
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002583 // Import the major distinguishing characteristics of this function.
2584 DeclContext *DC, *LexicalDC;
2585 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002586 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002587 NamedDecl *ToD;
2588 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002589 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002590 if (ToD)
2591 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002592
Gabor Marton5254e642018-06-27 13:32:50 +00002593 const FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00002594 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002595
Gabor Marton5254e642018-06-27 13:32:50 +00002596 // If this is a function template specialization, then try to find the same
2597 // existing specialization in the "to" context. The localUncachedLookup
2598 // below will not find any specialization, but would find the primary
2599 // template; thus, we have to skip normal lookup in case of specializations.
2600 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
2601 if (D->getTemplatedKind() ==
2602 FunctionDecl::TK_FunctionTemplateSpecialization) {
2603 if (FunctionDecl *FoundFunction = FindFunctionTemplateSpecialization(D)) {
2604 if (D->doesThisDeclarationHaveABody() &&
2605 FoundFunction->hasBody())
2606 return Importer.Imported(D, FoundFunction);
2607 FoundByLookup = FoundFunction;
2608 }
2609 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002610 // Try to find a function in our own ("to") context with the same name, same
2611 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00002612 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002613 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00002614 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002615 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002616 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002617 for (auto *FoundDecl : FoundDecls) {
2618 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002619 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002620
Balazs Keria35798d2018-07-17 09:52:41 +00002621 // If template was found, look at the templated function.
2622 if (FromFT) {
2623 if (auto *Template = dyn_cast<FunctionTemplateDecl>(FoundDecl))
2624 FoundDecl = Template->getTemplatedDecl();
2625 else
2626 continue;
2627 }
2628
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002629 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002630 if (FoundFunction->hasExternalFormalLinkage() &&
2631 D->hasExternalFormalLinkage()) {
Balazs Keric7797c42018-07-11 09:37:24 +00002632 if (IsStructuralMatch(D, FoundFunction)) {
2633 const FunctionDecl *Definition = nullptr;
2634 if (D->doesThisDeclarationHaveABody() &&
2635 FoundFunction->hasBody(Definition)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002636 return Importer.MapImported(
Balazs Keric7797c42018-07-11 09:37:24 +00002637 D, const_cast<FunctionDecl *>(Definition));
2638 }
2639 FoundByLookup = FoundFunction;
2640 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002641 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002642
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002643 // FIXME: Check for overloading more carefully, e.g., by boosting
2644 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002645
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002646 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002647 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002648 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002649
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002650 // Complain about inconsistent function types.
2651 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002652 << Name << D->getType() << FoundFunction->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +00002653 Importer.ToDiag(FoundFunction->getLocation(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002654 diag::note_odr_value_here)
2655 << FoundFunction->getType();
2656 }
2657 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002658
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002659 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002660 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002661
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002662 if (!ConflictingDecls.empty()) {
2663 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002664 ConflictingDecls.data(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002665 ConflictingDecls.size());
2666 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002667 return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00002668 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002669 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002670
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002671 DeclarationNameInfo NameInfo(Name, Loc);
2672 // Import additional name location/type info.
2673 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2674
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002675 QualType FromTy = D->getType();
2676 bool usedDifferentExceptionSpec = false;
2677
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002678 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002679 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2680 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2681 // FunctionDecl that we are importing the FunctionProtoType for.
2682 // To avoid an infinite recursion when importing, create the FunctionDecl
2683 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002684 if (FromEPI.ExceptionSpec.SourceDecl ||
2685 FromEPI.ExceptionSpec.SourceTemplate ||
2686 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002687 FunctionProtoType::ExtProtoInfo DefaultEPI;
2688 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002689 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002690 usedDifferentExceptionSpec = true;
2691 }
2692 }
2693
Douglas Gregorb4964f72010-02-15 23:54:17 +00002694 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002695 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002696 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002697 return nullptr;
2698
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002699 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002700 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00002701 for (auto P : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002702 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002703 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002704 return nullptr;
2705
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002706 Parameters.push_back(ToP);
2707 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002708
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002709 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002710 if (D->getTypeSourceInfo() && !TInfo)
2711 return nullptr;
2712
2713 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00002714 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002715 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002716 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002717 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
2718 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
2719 InnerLocStart, NameInfo, T, TInfo, FromConstructor->isExplicit(),
2720 D->isInlineSpecified(), D->isImplicit(), D->isConstexpr()))
2721 return ToFunction;
Sean Callanandd2c1742016-05-16 20:48:03 +00002722 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
2723 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002724 for (auto *I : FromConstructor->inits()) {
2725 auto *ToI = cast_or_null<CXXCtorInitializer>(Importer.Import(I));
Sean Callanandd2c1742016-05-16 20:48:03 +00002726 if (!ToI && I)
2727 return nullptr;
2728 CtorInitializers.push_back(ToI);
2729 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002730 auto **Memory =
Sean Callanandd2c1742016-05-16 20:48:03 +00002731 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
2732 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002733 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
Sean Callanandd2c1742016-05-16 20:48:03 +00002734 ToCtor->setCtorInitializers(Memory);
2735 ToCtor->setNumCtorInitializers(NumInitializers);
2736 }
Douglas Gregor00eace12010-02-21 18:29:16 +00002737 } else if (isa<CXXDestructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002738 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
2739 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
2740 InnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
2741 D->isImplicit()))
2742 return ToFunction;
2743 } else if (CXXConversionDecl *FromConversion =
2744 dyn_cast<CXXConversionDecl>(D)) {
2745 if (GetImportedOrCreateDecl<CXXConversionDecl>(
2746 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
2747 InnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
2748 FromConversion->isExplicit(), D->isConstexpr(), SourceLocation()))
2749 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002750 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002751 if (GetImportedOrCreateDecl<CXXMethodDecl>(
2752 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
2753 InnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
2754 Method->isInlineSpecified(), D->isConstexpr(), SourceLocation()))
2755 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00002756 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00002757 if (GetImportedOrCreateDecl(ToFunction, D, Importer.getToContext(), DC,
2758 InnerLocStart, NameInfo, T, TInfo,
2759 D->getStorageClass(), D->isInlineSpecified(),
2760 D->hasWrittenPrototype(), D->isConstexpr()))
2761 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00002762 }
John McCall3e11ebe2010-03-15 10:12:16 +00002763
2764 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002765 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002766 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002767 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002768 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2769 ToFunction->setTrivial(D->isTrivial());
2770 ToFunction->setPure(D->isPure());
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002771 ToFunction->setRangeEnd(Importer.Import(D->getEndLoc()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00002772
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002773 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002774 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002775 Param->setOwningFunction(ToFunction);
2776 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002777 }
David Blaikie9c70e042011-09-21 18:16:56 +00002778 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002779
Gabor Marton5254e642018-06-27 13:32:50 +00002780 if (FoundByLookup) {
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002781 auto *Recent = const_cast<FunctionDecl *>(
Gabor Marton5254e642018-06-27 13:32:50 +00002782 FoundByLookup->getMostRecentDecl());
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002783 ToFunction->setPreviousDecl(Recent);
2784 }
2785
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002786 // We need to complete creation of FunctionProtoTypeLoc manually with setting
2787 // params it refers to.
2788 if (TInfo) {
2789 if (auto ProtoLoc =
2790 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
2791 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
2792 ProtoLoc.setParam(I, Parameters[I]);
2793 }
2794 }
2795
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002796 if (usedDifferentExceptionSpec) {
2797 // Update FunctionProtoType::ExtProtoInfo.
2798 QualType T = Importer.Import(D->getType());
2799 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002800 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002801 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002802 }
2803
Balazs Keria35798d2018-07-17 09:52:41 +00002804 // Import the describing template function, if any.
2805 if (FromFT)
2806 if (!Importer.Import(FromFT))
2807 return nullptr;
2808
Gabor Marton5254e642018-06-27 13:32:50 +00002809 if (D->doesThisDeclarationHaveABody()) {
2810 if (Stmt *FromBody = D->getBody()) {
2811 if (Stmt *ToBody = Importer.Import(FromBody)) {
2812 ToFunction->setBody(ToBody);
2813 }
Sean Callanan59721b32015-04-28 18:41:46 +00002814 }
2815 }
2816
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002817 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002818
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002819 // If it is a template, import all related things.
2820 if (ImportTemplateInformation(D, ToFunction))
2821 return nullptr;
2822
Gabor Marton5254e642018-06-27 13:32:50 +00002823 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
2824
2825 // TODO Can we generalize this approach to other AST nodes as well?
2826 if (D->getDeclContext()->containsDeclAndLoad(D))
2827 DC->addDeclInternal(ToFunction);
2828 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002829 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002830
Gabor Marton5254e642018-06-27 13:32:50 +00002831 // Friend declaration's lexical context is the befriending class, but the
2832 // semantic context is the enclosing scope of the befriending class.
2833 // We want the friend functions to be found in the semantic context by lookup.
2834 // FIXME should we handle this generically in VisitFriendDecl?
2835 // In Other cases when LexicalDC != DC we don't want it to be added,
2836 // e.g out-of-class definitions like void B::f() {} .
2837 if (LexicalDC != DC && IsFriend) {
2838 DC->makeDeclVisibleInContext(ToFunction);
2839 }
2840
2841 // Import the rest of the chain. I.e. import all subsequent declarations.
2842 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt)
2843 if (!Importer.Import(*RedeclIt))
2844 return nullptr;
2845
Lang Hames19e07e12017-06-20 21:06:00 +00002846 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2847 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2848
Douglas Gregor43f54792010-02-17 02:12:47 +00002849 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002850}
2851
Douglas Gregor00eace12010-02-21 18:29:16 +00002852Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2853 return VisitFunctionDecl(D);
2854}
2855
2856Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2857 return VisitCXXMethodDecl(D);
2858}
2859
2860Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2861 return VisitCXXMethodDecl(D);
2862}
2863
2864Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2865 return VisitCXXMethodDecl(D);
2866}
2867
Douglas Gregor5c73e912010-02-11 00:48:18 +00002868Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2869 // Import the major distinguishing characteristics of a variable.
2870 DeclContext *DC, *LexicalDC;
2871 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002872 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002873 NamedDecl *ToD;
2874 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002875 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002876 if (ToD)
2877 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002878
Fangrui Song6907ce22018-07-30 19:24:48 +00002879 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002880 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002881 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002882 for (auto *FoundDecl : FoundDecls) {
2883 if (auto *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002884 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00002885 if (!Name &&
2886 ASTImporter::getFieldIndex(D) !=
2887 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002888 continue;
2889
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002890 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002891 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002892 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002893 return FoundField;
2894 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002895
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002896 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2897 << Name << D->getType() << FoundField->getType();
2898 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2899 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002900 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002901 }
2902 }
2903
Douglas Gregorb4964f72010-02-15 23:54:17 +00002904 // Import the type.
2905 QualType T = Importer.Import(D->getType());
2906 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002907 return nullptr;
2908
Douglas Gregor5c73e912010-02-11 00:48:18 +00002909 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2910 Expr *BitWidth = Importer.Import(D->getBitWidth());
2911 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002912 return nullptr;
2913
Gabor Marton26f72a92018-07-12 09:42:05 +00002914 FieldDecl *ToField;
2915 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
2916 Importer.Import(D->getInnerLocStart()), Loc,
2917 Name.getAsIdentifierInfo(), T, TInfo, BitWidth,
2918 D->isMutable(), D->getInClassInitStyle()))
2919 return ToField;
2920
Douglas Gregordd483172010-02-22 17:42:47 +00002921 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002922 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00002923 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00002924 Expr *ToInitializer = Importer.Import(FromInitializer);
2925 if (ToInitializer)
2926 ToField->setInClassInitializer(ToInitializer);
2927 else
2928 return nullptr;
2929 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002930 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00002931 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002932 return ToField;
2933}
2934
Francois Pichet783dd6e2010-11-21 06:08:52 +00002935Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2936 // Import the major distinguishing characteristics of a variable.
2937 DeclContext *DC, *LexicalDC;
2938 DeclarationName Name;
2939 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002940 NamedDecl *ToD;
2941 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002942 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002943 if (ToD)
2944 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002945
Fangrui Song6907ce22018-07-30 19:24:48 +00002946 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002947 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002948 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002949 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002950 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002951 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00002952 if (!Name &&
2953 ASTImporter::getFieldIndex(D) !=
2954 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002955 continue;
2956
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002957 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002958 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002959 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002960 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002961 return FoundField;
2962 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002963
2964 // If there are more anonymous fields to check, continue.
2965 if (!Name && I < N-1)
2966 continue;
2967
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002968 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2969 << Name << D->getType() << FoundField->getType();
2970 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2971 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002972 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002973 }
2974 }
2975
Francois Pichet783dd6e2010-11-21 06:08:52 +00002976 // Import the type.
2977 QualType T = Importer.Import(D->getType());
2978 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002979 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002980
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002981 auto **NamedChain =
2982 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00002983
2984 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00002985 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00002986 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002987 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00002988 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002989 NamedChain[i++] = cast<NamedDecl>(D);
2990 }
2991
Gabor Marton26f72a92018-07-12 09:42:05 +00002992 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
2993 IndirectFieldDecl *ToIndirectField;
2994 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
2995 Loc, Name.getAsIdentifierInfo(), T, CH))
2996 // FIXME here we leak `NamedChain` which is allocated before
2997 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00002998
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00002999 for (const auto *A : D->attrs())
3000 ToIndirectField->addAttr(Importer.Import(A));
Aaron Ballman260995b2014-10-15 16:58:18 +00003001
Francois Pichet783dd6e2010-11-21 06:08:52 +00003002 ToIndirectField->setAccess(D->getAccess());
3003 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003004 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003005 return ToIndirectField;
3006}
3007
Aleksei Sidorina693b372016-09-28 10:16:56 +00003008Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
3009 // Import the major distinguishing characteristics of a declaration.
3010 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3011 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
3012 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
3013 if (!DC || !LexicalDC)
3014 return nullptr;
3015
3016 // Determine whether we've already imported this decl.
3017 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
3018 auto *RD = cast<CXXRecordDecl>(DC);
3019 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003020
3021 while (ImportedFriend) {
3022 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003023 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3024 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003025 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003026
3027 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3028 if (Importer.IsStructurallyEquivalent(
3029 D->getFriendType()->getType(),
3030 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003031 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003032 }
3033 ImportedFriend = ImportedFriend->getNextFriend();
3034 }
3035
3036 // Not found. Create it.
3037 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003038 if (NamedDecl *FriendD = D->getFriendDecl()) {
3039 auto *ToFriendD = cast_or_null<NamedDecl>(Importer.Import(FriendD));
3040 if (ToFriendD && FriendD->getFriendObjectKind() != Decl::FOK_None &&
3041 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3042 ToFriendD->setObjectOfFriendDecl(false);
3043
3044 ToFU = ToFriendD;
3045 } else // The friend is a type, not a decl.
Aleksei Sidorina693b372016-09-28 10:16:56 +00003046 ToFU = Importer.Import(D->getFriendType());
3047 if (!ToFU)
3048 return nullptr;
3049
3050 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003051 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003052 for (unsigned I = 0; I < D->NumTPLists; I++) {
3053 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
3054 if (!List)
3055 return nullptr;
3056 ToTPLists[I] = List;
3057 }
3058
Gabor Marton26f72a92018-07-12 09:42:05 +00003059 FriendDecl *FrD;
3060 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
3061 Importer.Import(D->getLocation()), ToFU,
3062 Importer.Import(D->getFriendLoc()), ToTPLists))
3063 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003064
3065 FrD->setAccess(D->getAccess());
3066 FrD->setLexicalDeclContext(LexicalDC);
3067 LexicalDC->addDeclInternal(FrD);
3068 return FrD;
3069}
3070
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003071Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
3072 // Import the major distinguishing characteristics of an ivar.
3073 DeclContext *DC, *LexicalDC;
3074 DeclarationName Name;
3075 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003076 NamedDecl *ToD;
3077 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003078 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003079 if (ToD)
3080 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003081
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003082 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003083 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003084 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003085 for (auto *FoundDecl : FoundDecls) {
3086 if (auto *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003087 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003088 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003089 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003090 return FoundIvar;
3091 }
3092
3093 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3094 << Name << D->getType() << FoundIvar->getType();
3095 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3096 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003097 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003098 }
3099 }
3100
3101 // Import the type.
3102 QualType T = Importer.Import(D->getType());
3103 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003104 return nullptr;
3105
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003106 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3107 Expr *BitWidth = Importer.Import(D->getBitWidth());
3108 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003109 return nullptr;
3110
Gabor Marton26f72a92018-07-12 09:42:05 +00003111 ObjCIvarDecl *ToIvar;
3112 if (GetImportedOrCreateDecl(
3113 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
3114 Importer.Import(D->getInnerLocStart()), Loc,
3115 Name.getAsIdentifierInfo(), T, TInfo, D->getAccessControl(), BitWidth,
3116 D->getSynthesize()))
3117 return ToIvar;
3118
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003119 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003120 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003121 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003122}
3123
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003124Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3125 // Import the major distinguishing characteristics of a variable.
3126 DeclContext *DC, *LexicalDC;
3127 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003128 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003129 NamedDecl *ToD;
3130 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003131 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003132 if (ToD)
3133 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003134
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003135 // Try to find a variable in our own ("to") context with the same name and
3136 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00003137 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00003138 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003139 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003140 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003141 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003142 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003143 for (auto *FoundDecl : FoundDecls) {
3144 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003145 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003146
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003147 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003148 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003149 if (FoundVar->hasExternalFormalLinkage() &&
3150 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003151 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00003152 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003153 MergeWithVar = FoundVar;
3154 break;
3155 }
3156
Douglas Gregor56521c52010-02-12 17:23:39 +00003157 const ArrayType *FoundArray
3158 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3159 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003160 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003161 if (FoundArray && TArray) {
3162 if (isa<IncompleteArrayType>(FoundArray) &&
3163 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003164 // Import the type.
3165 QualType T = Importer.Import(D->getType());
3166 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003167 return nullptr;
3168
Douglas Gregor56521c52010-02-12 17:23:39 +00003169 FoundVar->setType(T);
3170 MergeWithVar = FoundVar;
3171 break;
3172 } else if (isa<IncompleteArrayType>(TArray) &&
3173 isa<ConstantArrayType>(FoundArray)) {
3174 MergeWithVar = FoundVar;
3175 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003176 }
3177 }
3178
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003179 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003180 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003181 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3182 << FoundVar->getType();
3183 }
3184 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003185
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003186 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003187 }
3188
3189 if (MergeWithVar) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003190 // An equivalent variable with external linkage has been found. Link
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003191 // the two declarations, then merge them.
Gabor Marton26f72a92018-07-12 09:42:05 +00003192 Importer.MapImported(D, MergeWithVar);
3193 updateFlags(D, MergeWithVar);
3194
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003195 if (VarDecl *DDef = D->getDefinition()) {
3196 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00003197 Importer.ToDiag(ExistingDef->getLocation(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003198 diag::err_odr_variable_multiple_def)
3199 << Name;
3200 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3201 } else {
3202 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003203 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003204 if (DDef->isInitKnownICE()) {
3205 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3206 Eval->CheckedICE = true;
3207 Eval->IsICE = DDef->isInitICE();
3208 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003209 }
3210 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003211
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003212 return MergeWithVar;
3213 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003214
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003215 if (!ConflictingDecls.empty()) {
3216 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003217 ConflictingDecls.data(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003218 ConflictingDecls.size());
3219 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003220 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003221 }
3222 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003223
Douglas Gregorb4964f72010-02-15 23:54:17 +00003224 // Import the type.
3225 QualType T = Importer.Import(D->getType());
3226 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003227 return nullptr;
3228
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003229 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003230 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Gabor Marton26f72a92018-07-12 09:42:05 +00003231 VarDecl *ToVar;
3232 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
3233 Importer.Import(D->getInnerLocStart()), Loc,
3234 Name.getAsIdentifierInfo(), T, TInfo,
3235 D->getStorageClass()))
3236 return ToVar;
3237
Douglas Gregor14454802011-02-25 02:25:35 +00003238 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003239 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003240 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003241
3242 // Templated declarations should never appear in the enclosing DeclContext.
3243 if (!D->getDescribedVarTemplate())
3244 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003245
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003246 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003247 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00003248 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003249
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003250 if (D->isConstexpr())
3251 ToVar->setConstexpr(true);
3252
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003253 return ToVar;
3254}
3255
Douglas Gregor8b228d72010-02-17 21:22:52 +00003256Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3257 // Parameters are created in the translation unit's context, then moved
3258 // into the function declaration's context afterward.
3259 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003260
Douglas Gregor8b228d72010-02-17 21:22:52 +00003261 // Import the name of this declaration.
3262 DeclarationName Name = Importer.Import(D->getDeclName());
3263 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003264 return nullptr;
3265
Douglas Gregor8b228d72010-02-17 21:22:52 +00003266 // Import the location of this declaration.
3267 SourceLocation Loc = Importer.Import(D->getLocation());
Fangrui Song6907ce22018-07-30 19:24:48 +00003268
Douglas Gregor8b228d72010-02-17 21:22:52 +00003269 // Import the parameter's type.
3270 QualType T = Importer.Import(D->getType());
3271 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003272 return nullptr;
3273
Douglas Gregor8b228d72010-02-17 21:22:52 +00003274 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003275 ImplicitParamDecl *ToParm = nullptr;
3276 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC, Loc,
3277 Name.getAsIdentifierInfo(), T,
3278 D->getParameterKind()))
3279 return ToParm;
3280 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003281}
3282
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003283Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3284 // Parameters are created in the translation unit's context, then moved
3285 // into the function declaration's context afterward.
3286 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003287
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003288 // Import the name of this declaration.
3289 DeclarationName Name = Importer.Import(D->getDeclName());
3290 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003291 return nullptr;
3292
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003293 // Import the location of this declaration.
3294 SourceLocation Loc = Importer.Import(D->getLocation());
Fangrui Song6907ce22018-07-30 19:24:48 +00003295
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003296 // Import the parameter's type.
3297 QualType T = Importer.Import(D->getType());
3298 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003299 return nullptr;
3300
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003301 // Create the imported parameter.
3302 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Gabor Marton26f72a92018-07-12 09:42:05 +00003303 ParmVarDecl *ToParm;
3304 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3305 Importer.Import(D->getInnerLocStart()), Loc,
3306 Name.getAsIdentifierInfo(), T, TInfo,
3307 D->getStorageClass(),
3308 /*DefaultArg*/ nullptr))
3309 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003310
3311 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003312 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003313 ToParm->setKNRPromoted(D->isKNRPromoted());
3314
3315 Expr *ToDefArg = nullptr;
3316 Expr *FromDefArg = nullptr;
3317 if (D->hasUninstantiatedDefaultArg()) {
3318 FromDefArg = D->getUninstantiatedDefaultArg();
3319 ToDefArg = Importer.Import(FromDefArg);
3320 ToParm->setUninstantiatedDefaultArg(ToDefArg);
3321 } else if (D->hasUnparsedDefaultArg()) {
3322 ToParm->setUnparsedDefaultArg();
3323 } else if (D->hasDefaultArg()) {
3324 FromDefArg = D->getDefaultArg();
3325 ToDefArg = Importer.Import(FromDefArg);
3326 ToParm->setDefaultArg(ToDefArg);
3327 }
3328 if (FromDefArg && !ToDefArg)
3329 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003330
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003331 if (D->isObjCMethodParameter()) {
3332 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3333 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3334 } else {
3335 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3336 D->getFunctionScopeIndex());
3337 }
3338
Gabor Marton26f72a92018-07-12 09:42:05 +00003339 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003340}
3341
Douglas Gregor43f54792010-02-17 02:12:47 +00003342Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3343 // Import the major distinguishing characteristics of a method.
3344 DeclContext *DC, *LexicalDC;
3345 DeclarationName Name;
3346 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003347 NamedDecl *ToD;
3348 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003349 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003350 if (ToD)
3351 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003352
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003353 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003354 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003355 for (auto *FoundDecl : FoundDecls) {
3356 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003357 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3358 continue;
3359
3360 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003361 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3362 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003363 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003364 << D->isInstanceMethod() << Name << D->getReturnType()
3365 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003366 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003367 diag::note_odr_objc_method_here)
3368 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003369 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003370 }
3371
3372 // Check the number of parameters.
3373 if (D->param_size() != FoundMethod->param_size()) {
3374 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3375 << D->isInstanceMethod() << Name
3376 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00003377 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003378 diag::note_odr_objc_method_here)
3379 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003380 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003381 }
3382
3383 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003384 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003385 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3386 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003387 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003388 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003389 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003390 diag::err_odr_objc_method_param_type_inconsistent)
3391 << D->isInstanceMethod() << Name
3392 << (*P)->getType() << (*FoundP)->getType();
3393 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3394 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003395 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003396 }
3397 }
3398
3399 // Check variadic/non-variadic.
3400 // Check the number of parameters.
3401 if (D->isVariadic() != FoundMethod->isVariadic()) {
3402 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3403 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00003404 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003405 diag::note_odr_objc_method_here)
3406 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003407 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003408 }
3409
3410 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003411 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003412 }
3413 }
3414
3415 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003416 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003417 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003418 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003419
Alp Toker314cc812014-01-25 16:55:45 +00003420 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003421
Gabor Marton26f72a92018-07-12 09:42:05 +00003422 ObjCMethodDecl *ToMethod;
3423 if (GetImportedOrCreateDecl(
3424 ToMethod, D, Importer.getToContext(), Loc,
Stephen Kelly1c301dc2018-08-09 21:09:38 +00003425 Importer.Import(D->getEndLoc()), Name.getObjCSelector(), ResultTy,
Gabor Marton26f72a92018-07-12 09:42:05 +00003426 ReturnTInfo, DC, D->isInstanceMethod(), D->isVariadic(),
3427 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3428 D->getImplementationControl(), D->hasRelatedResultType()))
3429 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003430
3431 // FIXME: When we decide to merge method definitions, we'll need to
3432 // deal with implicit parameters.
3433
3434 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003435 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003436 for (auto *FromP : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003437 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003438 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003439 return nullptr;
3440
Douglas Gregor43f54792010-02-17 02:12:47 +00003441 ToParams.push_back(ToP);
3442 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003443
Douglas Gregor43f54792010-02-17 02:12:47 +00003444 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003445 for (auto *ToParam : ToParams) {
3446 ToParam->setOwningFunction(ToMethod);
3447 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003448 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003449
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003450 SmallVector<SourceLocation, 12> SelLocs;
3451 D->getSelectorLocs(SelLocs);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003452 for (auto &Loc : SelLocs)
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003453 Loc = Importer.Import(Loc);
3454
3455 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003456
3457 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003458 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003459 return ToMethod;
3460}
3461
Douglas Gregor85f3f952015-07-07 03:57:15 +00003462Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3463 // Import the major distinguishing characteristics of a category.
3464 DeclContext *DC, *LexicalDC;
3465 DeclarationName Name;
3466 SourceLocation Loc;
3467 NamedDecl *ToD;
3468 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3469 return nullptr;
3470 if (ToD)
3471 return ToD;
3472
3473 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3474 if (!BoundInfo)
3475 return nullptr;
3476
Gabor Marton26f72a92018-07-12 09:42:05 +00003477 ObjCTypeParamDecl *Result;
3478 if (GetImportedOrCreateDecl(
3479 Result, D, Importer.getToContext(), DC, D->getVariance(),
3480 Importer.Import(D->getVarianceLoc()), D->getIndex(),
3481 Importer.Import(D->getLocation()), Name.getAsIdentifierInfo(),
3482 Importer.Import(D->getColonLoc()), BoundInfo))
3483 return Result;
3484
Douglas Gregor85f3f952015-07-07 03:57:15 +00003485 Result->setLexicalDeclContext(LexicalDC);
3486 return Result;
3487}
3488
Douglas Gregor84c51c32010-02-18 01:47:50 +00003489Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3490 // Import the major distinguishing characteristics of a category.
3491 DeclContext *DC, *LexicalDC;
3492 DeclarationName Name;
3493 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003494 NamedDecl *ToD;
3495 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003496 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003497 if (ToD)
3498 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003499
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003500 auto *ToInterface =
3501 cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003502 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003503 return nullptr;
3504
Douglas Gregor84c51c32010-02-18 01:47:50 +00003505 // Determine if we've already encountered this category.
3506 ObjCCategoryDecl *MergeWithCategory
3507 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3508 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3509 if (!ToCategory) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003510
3511 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
3512 Importer.Import(D->getAtStartLoc()), Loc,
3513 Importer.Import(D->getCategoryNameLoc()),
3514 Name.getAsIdentifierInfo(), ToInterface,
3515 /*TypeParamList=*/nullptr,
3516 Importer.Import(D->getIvarLBraceLoc()),
3517 Importer.Import(D->getIvarRBraceLoc())))
3518 return ToCategory;
3519
Douglas Gregor84c51c32010-02-18 01:47:50 +00003520 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003521 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003522 // Import the type parameter list after calling Imported, to avoid
3523 // loops when bringing in their DeclContext.
3524 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3525 D->getTypeParamList()));
Fangrui Song6907ce22018-07-30 19:24:48 +00003526
Douglas Gregor84c51c32010-02-18 01:47:50 +00003527 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003528 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3529 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003530 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3531 = D->protocol_loc_begin();
3532 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3533 FromProtoEnd = D->protocol_end();
3534 FromProto != FromProtoEnd;
3535 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003536 auto *ToProto =
3537 cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003538 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003539 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003540 Protocols.push_back(ToProto);
3541 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3542 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003543
Douglas Gregor84c51c32010-02-18 01:47:50 +00003544 // FIXME: If we're merging, make sure that the protocol list is the same.
3545 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3546 ProtocolLocs.data(), Importer.getToContext());
Douglas Gregor84c51c32010-02-18 01:47:50 +00003547 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003548 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003549 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003550
Douglas Gregor84c51c32010-02-18 01:47:50 +00003551 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003552 ImportDeclContext(D);
Fangrui Song6907ce22018-07-30 19:24:48 +00003553
Douglas Gregor84c51c32010-02-18 01:47:50 +00003554 // If we have an implementation, import it as well.
3555 if (D->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003556 auto *Impl =
3557 cast_or_null<ObjCCategoryImplDecl>(
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003558 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003559 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003560 return nullptr;
3561
Douglas Gregor84c51c32010-02-18 01:47:50 +00003562 ToCategory->setImplementation(Impl);
3563 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003564
Douglas Gregor84c51c32010-02-18 01:47:50 +00003565 return ToCategory;
3566}
3567
Fangrui Song6907ce22018-07-30 19:24:48 +00003568bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003569 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003570 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003571 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003572 if (shouldForceImportDeclContext(Kind))
3573 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003574 return false;
3575 }
3576
3577 // Start the protocol definition
3578 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00003579
Douglas Gregor2aa53772012-01-24 17:42:07 +00003580 // Import protocols
3581 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3582 SmallVector<SourceLocation, 4> ProtocolLocs;
Fangrui Song6907ce22018-07-30 19:24:48 +00003583 ObjCProtocolDecl::protocol_loc_iterator
Douglas Gregor2aa53772012-01-24 17:42:07 +00003584 FromProtoLoc = From->protocol_loc_begin();
3585 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3586 FromProtoEnd = From->protocol_end();
3587 FromProto != FromProtoEnd;
3588 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003589 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003590 if (!ToProto)
3591 return true;
3592 Protocols.push_back(ToProto);
3593 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3594 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003595
Douglas Gregor2aa53772012-01-24 17:42:07 +00003596 // FIXME: If we're merging, make sure that the protocol list is the same.
3597 To->setProtocolList(Protocols.data(), Protocols.size(),
3598 ProtocolLocs.data(), Importer.getToContext());
3599
Douglas Gregor2e15c842012-02-01 21:00:38 +00003600 if (shouldForceImportDeclContext(Kind)) {
3601 // Import all of the members of this protocol.
3602 ImportDeclContext(From, /*ForceImport=*/true);
3603 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003604 return false;
3605}
3606
Douglas Gregor98d156a2010-02-17 16:12:00 +00003607Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00003608 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00003609 // from, but this particular declaration is not that definition, import the
3610 // definition and map to that.
3611 ObjCProtocolDecl *Definition = D->getDefinition();
3612 if (Definition && Definition != D) {
3613 Decl *ImportedDef = Importer.Import(Definition);
3614 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003615 return nullptr;
3616
Gabor Marton26f72a92018-07-12 09:42:05 +00003617 return Importer.MapImported(D, ImportedDef);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003618 }
3619
Douglas Gregor84c51c32010-02-18 01:47:50 +00003620 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003621 DeclContext *DC, *LexicalDC;
3622 DeclarationName Name;
3623 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003624 NamedDecl *ToD;
3625 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003626 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003627 if (ToD)
3628 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003629
Craig Topper36250ad2014-05-12 05:36:57 +00003630 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003631 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003632 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003633 for (auto *FoundDecl : FoundDecls) {
3634 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003635 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00003636
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003637 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003638 break;
3639 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003640
Douglas Gregor98d156a2010-02-17 16:12:00 +00003641 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003642 if (!ToProto) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003643 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
3644 Name.getAsIdentifierInfo(), Loc,
3645 Importer.Import(D->getAtStartLoc()),
3646 /*PrevDecl=*/nullptr))
3647 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003648 ToProto->setLexicalDeclContext(LexicalDC);
3649 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003650 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003651
3652 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003653
Douglas Gregor2aa53772012-01-24 17:42:07 +00003654 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003655 return nullptr;
3656
Douglas Gregor98d156a2010-02-17 16:12:00 +00003657 return ToProto;
3658}
3659
Sean Callanan0aae0412014-12-10 00:00:37 +00003660Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3661 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3662 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3663
3664 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3665 SourceLocation LangLoc = Importer.Import(D->getLocation());
3666
3667 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00003668
3669 LinkageSpecDecl *ToLinkageSpec;
3670 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
3671 ExternLoc, LangLoc, D->getLanguage(), HasBraces))
3672 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00003673
3674 if (HasBraces) {
3675 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3676 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3677 }
3678
3679 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3680 LexicalDC->addDeclInternal(ToLinkageSpec);
3681
Sean Callanan0aae0412014-12-10 00:00:37 +00003682 return ToLinkageSpec;
3683}
3684
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003685Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
3686 DeclContext *DC, *LexicalDC;
3687 DeclarationName Name;
3688 SourceLocation Loc;
3689 NamedDecl *ToD = nullptr;
3690 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3691 return nullptr;
3692 if (ToD)
3693 return ToD;
3694
3695 DeclarationNameInfo NameInfo(Name,
3696 Importer.Import(D->getNameInfo().getLoc()));
3697 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3698
Gabor Marton26f72a92018-07-12 09:42:05 +00003699 UsingDecl *ToUsing;
3700 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
3701 Importer.Import(D->getUsingLoc()),
3702 Importer.Import(D->getQualifierLoc()), NameInfo,
3703 D->hasTypename()))
3704 return ToUsing;
3705
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003706 ToUsing->setLexicalDeclContext(LexicalDC);
3707 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003708
3709 if (NamedDecl *FromPattern =
3710 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003711 if (auto *ToPattern =
3712 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003713 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern);
3714 else
3715 return nullptr;
3716 }
3717
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003718 for (auto *FromShadow : D->shadows()) {
3719 if (auto *ToShadow =
3720 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003721 ToUsing->addShadowDecl(ToShadow);
3722 else
3723 // FIXME: We return a nullptr here but the definition is already created
3724 // and available with lookups. How to fix this?..
3725 return nullptr;
3726 }
3727 return ToUsing;
3728}
3729
3730Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
3731 DeclContext *DC, *LexicalDC;
3732 DeclarationName Name;
3733 SourceLocation Loc;
3734 NamedDecl *ToD = nullptr;
3735 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3736 return nullptr;
3737 if (ToD)
3738 return ToD;
3739
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003740 auto *ToUsing = dyn_cast_or_null<UsingDecl>(
3741 Importer.Import(D->getUsingDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003742 if (!ToUsing)
3743 return nullptr;
3744
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003745 auto *ToTarget = dyn_cast_or_null<NamedDecl>(
3746 Importer.Import(D->getTargetDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003747 if (!ToTarget)
3748 return nullptr;
3749
Gabor Marton26f72a92018-07-12 09:42:05 +00003750 UsingShadowDecl *ToShadow;
3751 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
3752 ToUsing, ToTarget))
3753 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003754
3755 ToShadow->setLexicalDeclContext(LexicalDC);
3756 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003757
3758 if (UsingShadowDecl *FromPattern =
3759 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003760 if (auto *ToPattern =
3761 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003762 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow,
3763 ToPattern);
3764 else
3765 // FIXME: We return a nullptr here but the definition is already created
3766 // and available with lookups. How to fix this?..
3767 return nullptr;
3768 }
3769
3770 LexicalDC->addDeclInternal(ToShadow);
3771
3772 return ToShadow;
3773}
3774
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003775Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
3776 DeclContext *DC, *LexicalDC;
3777 DeclarationName Name;
3778 SourceLocation Loc;
3779 NamedDecl *ToD = nullptr;
3780 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3781 return nullptr;
3782 if (ToD)
3783 return ToD;
3784
3785 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor());
3786 if (!ToComAncestor)
3787 return nullptr;
3788
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003789 auto *ToNominated = cast_or_null<NamespaceDecl>(
3790 Importer.Import(D->getNominatedNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003791 if (!ToNominated)
3792 return nullptr;
3793
Gabor Marton26f72a92018-07-12 09:42:05 +00003794 UsingDirectiveDecl *ToUsingDir;
3795 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
3796 Importer.Import(D->getUsingLoc()),
3797 Importer.Import(D->getNamespaceKeyLocation()),
3798 Importer.Import(D->getQualifierLoc()),
3799 Importer.Import(D->getIdentLocation()),
3800 ToNominated, ToComAncestor))
3801 return ToUsingDir;
3802
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003803 ToUsingDir->setLexicalDeclContext(LexicalDC);
3804 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003805
3806 return ToUsingDir;
3807}
3808
3809Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl(
3810 UnresolvedUsingValueDecl *D) {
3811 DeclContext *DC, *LexicalDC;
3812 DeclarationName Name;
3813 SourceLocation Loc;
3814 NamedDecl *ToD = nullptr;
3815 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3816 return nullptr;
3817 if (ToD)
3818 return ToD;
3819
3820 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc()));
3821 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3822
Gabor Marton26f72a92018-07-12 09:42:05 +00003823 UnresolvedUsingValueDecl *ToUsingValue;
3824 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
3825 Importer.Import(D->getUsingLoc()),
3826 Importer.Import(D->getQualifierLoc()), NameInfo,
3827 Importer.Import(D->getEllipsisLoc())))
3828 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003829
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003830 ToUsingValue->setAccess(D->getAccess());
3831 ToUsingValue->setLexicalDeclContext(LexicalDC);
3832 LexicalDC->addDeclInternal(ToUsingValue);
3833
3834 return ToUsingValue;
3835}
3836
3837Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
3838 UnresolvedUsingTypenameDecl *D) {
3839 DeclContext *DC, *LexicalDC;
3840 DeclarationName Name;
3841 SourceLocation Loc;
3842 NamedDecl *ToD = nullptr;
3843 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3844 return nullptr;
3845 if (ToD)
3846 return ToD;
3847
Gabor Marton26f72a92018-07-12 09:42:05 +00003848 UnresolvedUsingTypenameDecl *ToUsing;
3849 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
3850 Importer.Import(D->getUsingLoc()),
3851 Importer.Import(D->getTypenameLoc()),
3852 Importer.Import(D->getQualifierLoc()), Loc, Name,
3853 Importer.Import(D->getEllipsisLoc())))
3854 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003855
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003856 ToUsing->setAccess(D->getAccess());
3857 ToUsing->setLexicalDeclContext(LexicalDC);
3858 LexicalDC->addDeclInternal(ToUsing);
3859
3860 return ToUsing;
3861}
3862
Fangrui Song6907ce22018-07-30 19:24:48 +00003863bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003864 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003865 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003866 if (To->getDefinition()) {
3867 // Check consistency of superclass.
3868 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3869 if (FromSuper) {
3870 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3871 if (!FromSuper)
3872 return true;
3873 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003874
3875 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00003876 if ((bool)FromSuper != (bool)ToSuper ||
3877 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00003878 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00003879 diag::err_odr_objc_superclass_inconsistent)
3880 << To->getDeclName();
3881 if (ToSuper)
3882 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3883 << To->getSuperClass()->getDeclName();
3884 else
Fangrui Song6907ce22018-07-30 19:24:48 +00003885 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00003886 diag::note_odr_objc_missing_superclass);
3887 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00003888 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00003889 diag::note_odr_objc_superclass)
3890 << From->getSuperClass()->getDeclName();
3891 else
Fangrui Song6907ce22018-07-30 19:24:48 +00003892 Importer.FromDiag(From->getLocation(),
3893 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003894 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003895
Douglas Gregor2e15c842012-02-01 21:00:38 +00003896 if (shouldForceImportDeclContext(Kind))
3897 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003898 return false;
3899 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003900
Douglas Gregor2aa53772012-01-24 17:42:07 +00003901 // Start the definition.
3902 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00003903
Douglas Gregor2aa53772012-01-24 17:42:07 +00003904 // If this class has a superclass, import it.
3905 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003906 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3907 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003908 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003909
3910 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003911 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003912
Douglas Gregor2aa53772012-01-24 17:42:07 +00003913 // Import protocols
3914 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3915 SmallVector<SourceLocation, 4> ProtocolLocs;
Fangrui Song6907ce22018-07-30 19:24:48 +00003916 ObjCInterfaceDecl::protocol_loc_iterator
Douglas Gregor2aa53772012-01-24 17:42:07 +00003917 FromProtoLoc = From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00003918
Douglas Gregor2aa53772012-01-24 17:42:07 +00003919 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3920 FromProtoEnd = From->protocol_end();
3921 FromProto != FromProtoEnd;
3922 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003923 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003924 if (!ToProto)
3925 return true;
3926 Protocols.push_back(ToProto);
3927 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3928 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003929
Douglas Gregor2aa53772012-01-24 17:42:07 +00003930 // FIXME: If we're merging, make sure that the protocol list is the same.
3931 To->setProtocolList(Protocols.data(), Protocols.size(),
3932 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00003933
Douglas Gregor2aa53772012-01-24 17:42:07 +00003934 // Import categories. When the categories themselves are imported, they'll
3935 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003936 for (auto *Cat : From->known_categories())
3937 Importer.Import(Cat);
Fangrui Song6907ce22018-07-30 19:24:48 +00003938
Douglas Gregor2aa53772012-01-24 17:42:07 +00003939 // If we have an @implementation, import it as well.
3940 if (From->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003941 auto *Impl = cast_or_null<ObjCImplementationDecl>(
3942 Importer.Import(From->getImplementation()));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003943 if (!Impl)
3944 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00003945
Douglas Gregor2aa53772012-01-24 17:42:07 +00003946 To->setImplementation(Impl);
3947 }
3948
Douglas Gregor2e15c842012-02-01 21:00:38 +00003949 if (shouldForceImportDeclContext(Kind)) {
3950 // Import all of the members of this class.
3951 ImportDeclContext(From, /*ForceImport=*/true);
3952 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003953 return false;
3954}
3955
Douglas Gregor85f3f952015-07-07 03:57:15 +00003956ObjCTypeParamList *
3957ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3958 if (!list)
3959 return nullptr;
3960
3961 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3962 for (auto fromTypeParam : *list) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003963 auto *toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3964 Importer.Import(fromTypeParam));
Douglas Gregor85f3f952015-07-07 03:57:15 +00003965 if (!toTypeParam)
3966 return nullptr;
3967
3968 toTypeParams.push_back(toTypeParam);
3969 }
3970
3971 return ObjCTypeParamList::create(Importer.getToContext(),
3972 Importer.Import(list->getLAngleLoc()),
3973 toTypeParams,
3974 Importer.Import(list->getRAngleLoc()));
3975}
3976
Douglas Gregor45635322010-02-16 01:20:57 +00003977Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003978 // If this class has a definition in the translation unit we're coming from,
3979 // but this particular declaration is not that definition, import the
3980 // definition and map to that.
3981 ObjCInterfaceDecl *Definition = D->getDefinition();
3982 if (Definition && Definition != D) {
3983 Decl *ImportedDef = Importer.Import(Definition);
3984 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003985 return nullptr;
3986
Gabor Marton26f72a92018-07-12 09:42:05 +00003987 return Importer.MapImported(D, ImportedDef);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003988 }
3989
Douglas Gregor45635322010-02-16 01:20:57 +00003990 // Import the major distinguishing characteristics of an @interface.
3991 DeclContext *DC, *LexicalDC;
3992 DeclarationName Name;
3993 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003994 NamedDecl *ToD;
3995 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003996 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003997 if (ToD)
3998 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003999
Douglas Gregor2aa53772012-01-24 17:42:07 +00004000 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004001 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004002 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004003 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004004 for (auto *FoundDecl : FoundDecls) {
4005 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004006 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004007
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004008 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004009 break;
4010 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004011
Douglas Gregor2aa53772012-01-24 17:42:07 +00004012 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004013 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004014 if (!ToIface) {
Gabor Marton26f72a92018-07-12 09:42:05 +00004015 if (GetImportedOrCreateDecl(
4016 ToIface, D, Importer.getToContext(), DC,
4017 Importer.Import(D->getAtStartLoc()), Name.getAsIdentifierInfo(),
4018 /*TypeParamList=*/nullptr,
4019 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4020 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004021 ToIface->setLexicalDeclContext(LexicalDC);
4022 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004023 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004024 Importer.MapImported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004025 // Import the type parameter list after calling Imported, to avoid
4026 // loops when bringing in their DeclContext.
4027 ToIface->setTypeParamList(ImportObjCTypeParamList(
4028 D->getTypeParamListAsWritten()));
Fangrui Song6907ce22018-07-30 19:24:48 +00004029
Douglas Gregor2aa53772012-01-24 17:42:07 +00004030 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00004031 return nullptr;
4032
Douglas Gregor98d156a2010-02-17 16:12:00 +00004033 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004034}
4035
Douglas Gregor4da9d682010-12-07 15:32:12 +00004036Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004037 auto *Category = cast_or_null<ObjCCategoryDecl>(
4038 Importer.Import(D->getCategoryDecl()));
Douglas Gregor4da9d682010-12-07 15:32:12 +00004039 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00004040 return nullptr;
4041
Douglas Gregor4da9d682010-12-07 15:32:12 +00004042 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4043 if (!ToImpl) {
4044 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4045 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004046 return nullptr;
4047
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00004048 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Gabor Marton26f72a92018-07-12 09:42:05 +00004049 if (GetImportedOrCreateDecl(
4050 ToImpl, D, Importer.getToContext(), DC,
4051 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
4052 Importer.Import(D->getLocation()),
4053 Importer.Import(D->getAtStartLoc()), CategoryNameLoc))
4054 return ToImpl;
4055
Douglas Gregor4da9d682010-12-07 15:32:12 +00004056 DeclContext *LexicalDC = DC;
4057 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4058 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4059 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004060 return nullptr;
4061
Douglas Gregor4da9d682010-12-07 15:32:12 +00004062 ToImpl->setLexicalDeclContext(LexicalDC);
4063 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004064
Sean Callanan95e74be2011-10-21 02:57:43 +00004065 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004066 Category->setImplementation(ToImpl);
4067 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004068
Gabor Marton26f72a92018-07-12 09:42:05 +00004069 Importer.MapImported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00004070 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004071 return ToImpl;
4072}
4073
Douglas Gregorda8025c2010-12-07 01:26:03 +00004074Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
4075 // Find the corresponding interface.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004076 auto *Iface = cast_or_null<ObjCInterfaceDecl>(
4077 Importer.Import(D->getClassInterface()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00004078 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00004079 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004080
4081 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00004082 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004083 if (D->getSuperClass()) {
4084 Super = cast_or_null<ObjCInterfaceDecl>(
4085 Importer.Import(D->getSuperClass()));
4086 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00004087 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004088 }
4089
4090 ObjCImplementationDecl *Impl = Iface->getImplementation();
4091 if (!Impl) {
4092 // We haven't imported an implementation yet. Create a new @implementation
4093 // now.
Gabor Marton26f72a92018-07-12 09:42:05 +00004094 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
4095 Importer.ImportContext(D->getDeclContext()),
4096 Iface, Super, Importer.Import(D->getLocation()),
4097 Importer.Import(D->getAtStartLoc()),
4098 Importer.Import(D->getSuperClassLoc()),
4099 Importer.Import(D->getIvarLBraceLoc()),
4100 Importer.Import(D->getIvarRBraceLoc())))
4101 return Impl;
4102
Douglas Gregorda8025c2010-12-07 01:26:03 +00004103 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4104 DeclContext *LexicalDC
4105 = Importer.ImportContext(D->getLexicalDeclContext());
4106 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004107 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004108 Impl->setLexicalDeclContext(LexicalDC);
4109 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004110
Douglas Gregorda8025c2010-12-07 01:26:03 +00004111 // Associate the implementation with the class it implements.
4112 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004113 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004114 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004115 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004116
4117 // Verify that the existing @implementation has the same superclass.
4118 if ((Super && !Impl->getSuperClass()) ||
4119 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004120 (Super && Impl->getSuperClass() &&
4121 !declaresSameEntity(Super->getCanonicalDecl(),
4122 Impl->getSuperClass()))) {
4123 Importer.ToDiag(Impl->getLocation(),
4124 diag::err_odr_objc_superclass_inconsistent)
4125 << Iface->getDeclName();
4126 // FIXME: It would be nice to have the location of the superclass
4127 // below.
4128 if (Impl->getSuperClass())
4129 Importer.ToDiag(Impl->getLocation(),
4130 diag::note_odr_objc_superclass)
4131 << Impl->getSuperClass()->getDeclName();
4132 else
4133 Importer.ToDiag(Impl->getLocation(),
4134 diag::note_odr_objc_missing_superclass);
4135 if (D->getSuperClass())
4136 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004137 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004138 << D->getSuperClass()->getDeclName();
4139 else
4140 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004141 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00004142 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004143 }
4144 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004145
Douglas Gregorda8025c2010-12-07 01:26:03 +00004146 // Import all of the members of this @implementation.
4147 ImportDeclContext(D);
4148
4149 return Impl;
4150}
4151
Douglas Gregora11c4582010-02-17 18:02:10 +00004152Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
4153 // Import the major distinguishing characteristics of an @property.
4154 DeclContext *DC, *LexicalDC;
4155 DeclarationName Name;
4156 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004157 NamedDecl *ToD;
4158 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004159 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004160 if (ToD)
4161 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004162
4163 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004164 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004165 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004166 for (auto *FoundDecl : FoundDecls) {
4167 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004168 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004169 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004170 FoundProp->getType())) {
4171 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4172 << Name << D->getType() << FoundProp->getType();
4173 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4174 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00004175 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004176 }
4177
4178 // FIXME: Check property attributes, getters, setters, etc.?
4179
4180 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004181 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004182 return FoundProp;
4183 }
4184 }
4185
4186 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00004187 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
4188 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00004189 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004190
4191 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004192 ObjCPropertyDecl *ToProperty;
4193 if (GetImportedOrCreateDecl(
4194 ToProperty, D, Importer.getToContext(), DC, Loc,
4195 Name.getAsIdentifierInfo(), Importer.Import(D->getAtLoc()),
4196 Importer.Import(D->getLParenLoc()), Importer.Import(D->getType()),
4197 TSI, D->getPropertyImplementation()))
4198 return ToProperty;
4199
Douglas Gregora11c4582010-02-17 18:02:10 +00004200 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004201 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004202
4203 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004204 ToProperty->setPropertyAttributesAsWritten(
4205 D->getPropertyAttributesAsWritten());
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +00004206 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
4207 Importer.Import(D->getGetterNameLoc()));
4208 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
4209 Importer.Import(D->getSetterNameLoc()));
Douglas Gregora11c4582010-02-17 18:02:10 +00004210 ToProperty->setGetterMethodDecl(
4211 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
4212 ToProperty->setSetterMethodDecl(
4213 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
4214 ToProperty->setPropertyIvarDecl(
4215 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
4216 return ToProperty;
4217}
4218
Douglas Gregor14a49e22010-12-07 18:32:03 +00004219Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004220 auto *Property = cast_or_null<ObjCPropertyDecl>(
4221 Importer.Import(D->getPropertyDecl()));
Douglas Gregor14a49e22010-12-07 18:32:03 +00004222 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00004223 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004224
4225 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4226 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004227 return nullptr;
4228
Douglas Gregor14a49e22010-12-07 18:32:03 +00004229 // Import the lexical declaration context.
4230 DeclContext *LexicalDC = DC;
4231 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4232 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4233 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004234 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004235 }
4236
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004237 auto *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004238 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00004239 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004240
4241 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004242 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004243 if (D->getPropertyIvarDecl()) {
4244 Ivar = cast_or_null<ObjCIvarDecl>(
4245 Importer.Import(D->getPropertyIvarDecl()));
4246 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00004247 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004248 }
4249
4250 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004251 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4252 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004253 if (!ToImpl) {
4254 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004255 Importer.Import(D->getBeginLoc()),
Gabor Marton26f72a92018-07-12 09:42:05 +00004256 Importer.Import(D->getLocation()), Property,
4257 D->getPropertyImplementation(), Ivar,
4258 Importer.Import(D->getPropertyIvarDeclLoc())))
4259 return ToImpl;
4260
Douglas Gregor14a49e22010-12-07 18:32:03 +00004261 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004262 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004263 } else {
4264 // Check that we have the same kind of property implementation (@synthesize
4265 // vs. @dynamic).
4266 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004267 Importer.ToDiag(ToImpl->getLocation(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004268 diag::err_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00004269 << Property->getDeclName()
4270 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00004271 == ObjCPropertyImplDecl::Dynamic);
4272 Importer.FromDiag(D->getLocation(),
4273 diag::note_odr_objc_property_impl_kind)
4274 << D->getPropertyDecl()->getDeclName()
4275 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004276 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004277 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004278
4279 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00004280 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4281 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004282 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004283 diag::err_odr_objc_synthesize_ivar_inconsistent)
4284 << Property->getDeclName()
4285 << ToImpl->getPropertyIvarDecl()->getDeclName()
4286 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00004287 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004288 diag::note_odr_objc_synthesize_ivar_here)
4289 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004290 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004291 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004292
Douglas Gregor14a49e22010-12-07 18:32:03 +00004293 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004294 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004295 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004296
Douglas Gregor14a49e22010-12-07 18:32:03 +00004297 return ToImpl;
4298}
4299
Douglas Gregora082a492010-11-30 19:14:50 +00004300Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4301 // For template arguments, we adopt the translation unit as our declaration
4302 // context. This context will be fixed when the actual template declaration
4303 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00004304
Douglas Gregora082a492010-11-30 19:14:50 +00004305 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004306 TemplateTypeParmDecl *ToD = nullptr;
4307 (void)GetImportedOrCreateDecl(
4308 ToD, D, Importer.getToContext(),
4309 Importer.getToContext().getTranslationUnitDecl(),
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004310 Importer.Import(D->getBeginLoc()), Importer.Import(D->getLocation()),
Gabor Marton26f72a92018-07-12 09:42:05 +00004311 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4312 D->wasDeclaredWithTypename(), D->isParameterPack());
4313 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004314}
4315
4316Decl *
4317ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4318 // Import the name of this declaration.
4319 DeclarationName Name = Importer.Import(D->getDeclName());
4320 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004321 return nullptr;
4322
Douglas Gregora082a492010-11-30 19:14:50 +00004323 // Import the location of this declaration.
4324 SourceLocation Loc = Importer.Import(D->getLocation());
4325
4326 // Import the type of this declaration.
4327 QualType T = Importer.Import(D->getType());
4328 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004329 return nullptr;
4330
Douglas Gregora082a492010-11-30 19:14:50 +00004331 // Import type-source information.
4332 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4333 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004334 return nullptr;
4335
Douglas Gregora082a492010-11-30 19:14:50 +00004336 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004337
4338 NonTypeTemplateParmDecl *ToD = nullptr;
4339 (void)GetImportedOrCreateDecl(
4340 ToD, D, Importer.getToContext(),
4341 Importer.getToContext().getTranslationUnitDecl(),
4342 Importer.Import(D->getInnerLocStart()), Loc, D->getDepth(),
4343 D->getPosition(), Name.getAsIdentifierInfo(), T, D->isParameterPack(),
4344 TInfo);
4345 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004346}
4347
4348Decl *
4349ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4350 // Import the name of this declaration.
4351 DeclarationName Name = Importer.Import(D->getDeclName());
4352 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004353 return nullptr;
4354
Douglas Gregora082a492010-11-30 19:14:50 +00004355 // Import the location of this declaration.
4356 SourceLocation Loc = Importer.Import(D->getLocation());
Gabor Marton26f72a92018-07-12 09:42:05 +00004357
Douglas Gregora082a492010-11-30 19:14:50 +00004358 // Import template parameters.
4359 TemplateParameterList *TemplateParams
4360 = ImportTemplateParameterList(D->getTemplateParameters());
4361 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004362 return nullptr;
4363
Douglas Gregora082a492010-11-30 19:14:50 +00004364 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004365
4366 TemplateTemplateParmDecl *ToD = nullptr;
4367 (void)GetImportedOrCreateDecl(
4368 ToD, D, Importer.getToContext(),
4369 Importer.getToContext().getTranslationUnitDecl(), Loc, D->getDepth(),
4370 D->getPosition(), D->isParameterPack(), Name.getAsIdentifierInfo(),
4371 TemplateParams);
4372 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004373}
4374
Gabor Marton9581c332018-05-23 13:53:36 +00004375// Returns the definition for a (forward) declaration of a ClassTemplateDecl, if
4376// it has any definition in the redecl chain.
4377static ClassTemplateDecl *getDefinition(ClassTemplateDecl *D) {
4378 CXXRecordDecl *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
4379 if (!ToTemplatedDef)
4380 return nullptr;
4381 ClassTemplateDecl *TemplateWithDef =
4382 ToTemplatedDef->getDescribedClassTemplate();
4383 return TemplateWithDef;
4384}
4385
Douglas Gregora082a492010-11-30 19:14:50 +00004386Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00004387 bool IsFriend = D->getFriendObjectKind() != Decl::FOK_None;
4388
Douglas Gregora082a492010-11-30 19:14:50 +00004389 // If this record has a definition in the translation unit we're coming from,
4390 // but this particular declaration is not that definition, import the
4391 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004392 auto *Definition =
4393 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
Balazs Keri0c23dc52018-08-13 13:08:37 +00004394 if (Definition && Definition != D->getTemplatedDecl() && !IsFriend) {
Douglas Gregora082a492010-11-30 19:14:50 +00004395 Decl *ImportedDef
4396 = Importer.Import(Definition->getDescribedClassTemplate());
4397 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004398 return nullptr;
4399
Gabor Marton26f72a92018-07-12 09:42:05 +00004400 return Importer.MapImported(D, ImportedDef);
Douglas Gregora082a492010-11-30 19:14:50 +00004401 }
Gabor Marton9581c332018-05-23 13:53:36 +00004402
Douglas Gregora082a492010-11-30 19:14:50 +00004403 // Import the major distinguishing characteristics of this class template.
4404 DeclContext *DC, *LexicalDC;
4405 DeclarationName Name;
4406 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004407 NamedDecl *ToD;
4408 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004409 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004410 if (ToD)
4411 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004412
Douglas Gregora082a492010-11-30 19:14:50 +00004413 // We may already have a template of the same name; try to find and match it.
4414 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004415 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004416 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004417 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004418 for (auto *FoundDecl : FoundDecls) {
4419 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004420 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00004421
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004422 Decl *Found = FoundDecl;
4423 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Marton9581c332018-05-23 13:53:36 +00004424
4425 // The class to be imported is a definition.
4426 if (D->isThisDeclarationADefinition()) {
4427 // Lookup will find the fwd decl only if that is more recent than the
4428 // definition. So, try to get the definition if that is available in
4429 // the redecl chain.
4430 ClassTemplateDecl *TemplateWithDef = getDefinition(FoundTemplate);
Balazs Keri0c23dc52018-08-13 13:08:37 +00004431 if (TemplateWithDef)
4432 FoundTemplate = TemplateWithDef;
4433 else
Gabor Marton9581c332018-05-23 13:53:36 +00004434 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00004435 }
4436
Douglas Gregora082a492010-11-30 19:14:50 +00004437 if (IsStructuralMatch(D, FoundTemplate)) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00004438 if (!IsFriend) {
4439 Importer.MapImported(D->getTemplatedDecl(),
4440 FoundTemplate->getTemplatedDecl());
4441 return Importer.MapImported(D, FoundTemplate);
4442 }
Aleksei Sidorin761c2242018-05-15 11:09:07 +00004443
Balazs Keri0c23dc52018-08-13 13:08:37 +00004444 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00004445 }
Douglas Gregora082a492010-11-30 19:14:50 +00004446 }
Gabor Marton9581c332018-05-23 13:53:36 +00004447
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004448 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00004449 }
Gabor Marton9581c332018-05-23 13:53:36 +00004450
Douglas Gregora082a492010-11-30 19:14:50 +00004451 if (!ConflictingDecls.empty()) {
4452 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00004453 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00004454 ConflictingDecls.size());
4455 }
Gabor Marton9581c332018-05-23 13:53:36 +00004456
Douglas Gregora082a492010-11-30 19:14:50 +00004457 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004458 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004459 }
4460
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004461 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4462
Douglas Gregora082a492010-11-30 19:14:50 +00004463 // Create the declaration that is being templated.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004464 auto *ToTemplated = cast_or_null<CXXRecordDecl>(
4465 Importer.Import(FromTemplated));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004466 if (!ToTemplated)
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004467 return nullptr;
4468
Douglas Gregora082a492010-11-30 19:14:50 +00004469 // Create the class template declaration itself.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004470 TemplateParameterList *TemplateParams =
4471 ImportTemplateParameterList(D->getTemplateParameters());
Douglas Gregora082a492010-11-30 19:14:50 +00004472 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004473 return nullptr;
4474
Gabor Marton26f72a92018-07-12 09:42:05 +00004475 ClassTemplateDecl *D2;
4476 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
4477 TemplateParams, ToTemplated))
4478 return D2;
4479
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004480 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00004481
Balazs Keri0c23dc52018-08-13 13:08:37 +00004482 if (ToTemplated->getPreviousDecl()) {
4483 assert(
4484 ToTemplated->getPreviousDecl()->getDescribedClassTemplate() &&
4485 "Missing described template");
4486 D2->setPreviousDecl(
4487 ToTemplated->getPreviousDecl()->getDescribedClassTemplate());
4488 }
Douglas Gregora082a492010-11-30 19:14:50 +00004489 D2->setAccess(D->getAccess());
4490 D2->setLexicalDeclContext(LexicalDC);
Balazs Keri0c23dc52018-08-13 13:08:37 +00004491 if (!IsFriend)
4492 LexicalDC->addDeclInternal(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00004493
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004494 if (FromTemplated->isCompleteDefinition() &&
4495 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004496 // FIXME: Import definition!
4497 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004498
Douglas Gregora082a492010-11-30 19:14:50 +00004499 return D2;
4500}
4501
Douglas Gregore2e50d332010-12-01 01:36:18 +00004502Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4503 ClassTemplateSpecializationDecl *D) {
4504 // If this record has a definition in the translation unit we're coming from,
4505 // but this particular declaration is not that definition, import the
4506 // definition and map to that.
4507 TagDecl *Definition = D->getDefinition();
4508 if (Definition && Definition != D) {
4509 Decl *ImportedDef = Importer.Import(Definition);
4510 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004511 return nullptr;
4512
Gabor Marton26f72a92018-07-12 09:42:05 +00004513 return Importer.MapImported(D, ImportedDef);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004514 }
4515
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004516 auto *ClassTemplate =
4517 cast_or_null<ClassTemplateDecl>(Importer.Import(
Douglas Gregore2e50d332010-12-01 01:36:18 +00004518 D->getSpecializedTemplate()));
4519 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004520 return nullptr;
4521
Douglas Gregore2e50d332010-12-01 01:36:18 +00004522 // Import the context of this declaration.
4523 DeclContext *DC = ClassTemplate->getDeclContext();
4524 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004525 return nullptr;
4526
Douglas Gregore2e50d332010-12-01 01:36:18 +00004527 DeclContext *LexicalDC = DC;
4528 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4529 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4530 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004531 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004532 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004533
Douglas Gregore2e50d332010-12-01 01:36:18 +00004534 // Import the location of this declaration.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004535 SourceLocation StartLoc = Importer.Import(D->getBeginLoc());
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004536 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004537
4538 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004539 SmallVector<TemplateArgument, 2> TemplateArgs;
Fangrui Song6907ce22018-07-30 19:24:48 +00004540 if (ImportTemplateArguments(D->getTemplateArgs().data(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00004541 D->getTemplateArgs().size(),
4542 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004543 return nullptr;
4544
Douglas Gregore2e50d332010-12-01 01:36:18 +00004545 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004546 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004547 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004548 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004549 if (D2) {
4550 // We already have a class template specialization with these template
4551 // arguments.
Fangrui Song6907ce22018-07-30 19:24:48 +00004552
Douglas Gregore2e50d332010-12-01 01:36:18 +00004553 // FIXME: Check for specialization vs. instantiation errors.
Fangrui Song6907ce22018-07-30 19:24:48 +00004554
Douglas Gregore2e50d332010-12-01 01:36:18 +00004555 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004556 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004557 // The record types structurally match, or the "from" translation
4558 // unit only had a forward declaration anyway; call it the same
4559 // function.
Gabor Marton26f72a92018-07-12 09:42:05 +00004560 return Importer.MapImported(D, FoundDef);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004561 }
4562 }
4563 } else {
4564 // Create a new specialization.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004565 if (auto *PartialSpec =
4566 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004567 // Import TemplateArgumentListInfo
4568 TemplateArgumentListInfo ToTAInfo;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004569 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
4570 if (ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004571 return nullptr;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004572
4573 QualType CanonInjType = Importer.Import(
4574 PartialSpec->getInjectedSpecializationType());
4575 if (CanonInjType.isNull())
4576 return nullptr;
4577 CanonInjType = CanonInjType.getCanonicalType();
4578
4579 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4580 PartialSpec->getTemplateParameters());
4581 if (!ToTPList && PartialSpec->getTemplateParameters())
4582 return nullptr;
4583
Gabor Marton26f72a92018-07-12 09:42:05 +00004584 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
4585 D2, D, Importer.getToContext(), D->getTagKind(), DC, StartLoc,
4586 IdLoc, ToTPList, ClassTemplate,
4587 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
4588 ToTAInfo, CanonInjType, nullptr))
4589 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004590
4591 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004592 if (GetImportedOrCreateDecl(
4593 D2, D, Importer.getToContext(), D->getTagKind(), DC, StartLoc,
4594 IdLoc, ClassTemplate, TemplateArgs, /*PrevDecl=*/nullptr))
4595 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004596 }
4597
Douglas Gregore2e50d332010-12-01 01:36:18 +00004598 D2->setSpecializationKind(D->getSpecializationKind());
4599
4600 // Add this specialization to the class template.
4601 ClassTemplate->AddSpecialization(D2, InsertPos);
Fangrui Song6907ce22018-07-30 19:24:48 +00004602
Douglas Gregore2e50d332010-12-01 01:36:18 +00004603 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004604 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004605
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004606 if (auto *TSI = D->getTypeAsWritten()) {
4607 TypeSourceInfo *TInfo = Importer.Import(TSI);
4608 if (!TInfo)
4609 return nullptr;
4610 D2->setTypeAsWritten(TInfo);
4611 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
4612 D2->setExternLoc(Importer.Import(D->getExternLoc()));
4613 }
4614
4615 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
4616 if (POI.isValid())
4617 D2->setPointOfInstantiation(POI);
4618 else if (D->getPointOfInstantiation().isValid())
4619 return nullptr;
4620
4621 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
4622
Gabor Martonb14056b2018-05-25 11:21:24 +00004623 // Set the context of this specialization/instantiation.
Douglas Gregore2e50d332010-12-01 01:36:18 +00004624 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonb14056b2018-05-25 11:21:24 +00004625
4626 // Add to the DC only if it was an explicit specialization/instantiation.
4627 if (D2->isExplicitInstantiationOrSpecialization()) {
4628 LexicalDC->addDeclInternal(D2);
4629 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004630 }
John McCallf937c022011-10-07 06:10:15 +00004631 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004632 return nullptr;
4633
Douglas Gregore2e50d332010-12-01 01:36:18 +00004634 return D2;
4635}
4636
Larisse Voufo39a1e502013-08-06 01:03:05 +00004637Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4638 // If this variable has a definition in the translation unit we're coming
4639 // from,
4640 // but this particular declaration is not that definition, import the
4641 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004642 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00004643 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4644 if (Definition && Definition != D->getTemplatedDecl()) {
4645 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4646 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004647 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004648
Gabor Marton26f72a92018-07-12 09:42:05 +00004649 return Importer.MapImported(D, ImportedDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004650 }
4651
4652 // Import the major distinguishing characteristics of this variable template.
4653 DeclContext *DC, *LexicalDC;
4654 DeclarationName Name;
4655 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004656 NamedDecl *ToD;
4657 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004658 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004659 if (ToD)
4660 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004661
4662 // We may already have a template of the same name; try to find and match it.
4663 assert(!DC->isFunctionOrMethod() &&
4664 "Variable templates cannot be declared at function scope");
4665 SmallVector<NamedDecl *, 4> ConflictingDecls;
4666 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004667 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004668 for (auto *FoundDecl : FoundDecls) {
4669 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00004670 continue;
4671
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004672 Decl *Found = FoundDecl;
4673 if (auto *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004674 if (IsStructuralMatch(D, FoundTemplate)) {
4675 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00004676 Importer.MapImported(D->getTemplatedDecl(),
4677 FoundTemplate->getTemplatedDecl());
4678 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004679 }
4680 }
4681
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004682 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004683 }
4684
4685 if (!ConflictingDecls.empty()) {
4686 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4687 ConflictingDecls.data(),
4688 ConflictingDecls.size());
4689 }
4690
4691 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004692 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004693
4694 VarDecl *DTemplated = D->getTemplatedDecl();
4695
4696 // Import the type.
4697 QualType T = Importer.Import(DTemplated->getType());
4698 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004699 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004700
4701 // Create the declaration that is being templated.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004702 auto *ToTemplated = dyn_cast_or_null<VarDecl>(Importer.Import(DTemplated));
4703 if (!ToTemplated)
Craig Topper36250ad2014-05-12 05:36:57 +00004704 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004705
4706 // Create the variable template declaration itself.
4707 TemplateParameterList *TemplateParams =
4708 ImportTemplateParameterList(D->getTemplateParameters());
4709 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004710 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004711
Gabor Marton26f72a92018-07-12 09:42:05 +00004712 VarTemplateDecl *ToVarTD;
4713 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
4714 Name, TemplateParams, ToTemplated))
4715 return ToVarTD;
4716
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004717 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004718
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004719 ToVarTD->setAccess(D->getAccess());
4720 ToVarTD->setLexicalDeclContext(LexicalDC);
4721 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004722
Larisse Voufo39a1e502013-08-06 01:03:05 +00004723 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004724 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004725 // FIXME: Import definition!
4726 }
4727
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004728 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004729}
4730
4731Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4732 VarTemplateSpecializationDecl *D) {
4733 // If this record has a definition in the translation unit we're coming from,
4734 // but this particular declaration is not that definition, import the
4735 // definition and map to that.
4736 VarDecl *Definition = D->getDefinition();
4737 if (Definition && Definition != D) {
4738 Decl *ImportedDef = Importer.Import(Definition);
4739 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004740 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004741
Gabor Marton26f72a92018-07-12 09:42:05 +00004742 return Importer.MapImported(D, ImportedDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004743 }
4744
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004745 auto *VarTemplate = cast_or_null<VarTemplateDecl>(
Larisse Voufo39a1e502013-08-06 01:03:05 +00004746 Importer.Import(D->getSpecializedTemplate()));
4747 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004748 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004749
4750 // Import the context of this declaration.
4751 DeclContext *DC = VarTemplate->getDeclContext();
4752 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004753 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004754
4755 DeclContext *LexicalDC = DC;
4756 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4757 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4758 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004759 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004760 }
4761
4762 // Import the location of this declaration.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004763 SourceLocation StartLoc = Importer.Import(D->getBeginLoc());
Larisse Voufo39a1e502013-08-06 01:03:05 +00004764 SourceLocation IdLoc = Importer.Import(D->getLocation());
4765
4766 // Import template arguments.
4767 SmallVector<TemplateArgument, 2> TemplateArgs;
4768 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4769 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004770 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004771
4772 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004773 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004774 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004775 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004776 if (D2) {
4777 // We already have a variable template specialization with these template
4778 // arguments.
4779
4780 // FIXME: Check for specialization vs. instantiation errors.
4781
4782 if (VarDecl *FoundDef = D2->getDefinition()) {
4783 if (!D->isThisDeclarationADefinition() ||
4784 IsStructuralMatch(D, FoundDef)) {
4785 // The record types structurally match, or the "from" translation
4786 // unit only had a forward declaration anyway; call it the same
4787 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00004788 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004789 }
4790 }
4791 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004792 // Import the type.
4793 QualType T = Importer.Import(D->getType());
4794 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004795 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004796
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004797 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4798 if (D->getTypeSourceInfo() && !TInfo)
4799 return nullptr;
4800
4801 TemplateArgumentListInfo ToTAInfo;
4802 if (ImportTemplateArgumentListInfo(D->getTemplateArgsInfo(), ToTAInfo))
4803 return nullptr;
4804
4805 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004806 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004807 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
4808 // Import TemplateArgumentListInfo
4809 TemplateArgumentListInfo ArgInfos;
4810 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
4811 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
4812 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ArgInfos))
4813 return nullptr;
4814
4815 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4816 FromPartial->getTemplateParameters());
4817 if (!ToTPList)
4818 return nullptr;
4819
Gabor Marton26f72a92018-07-12 09:42:05 +00004820 PartVarSpecDecl *ToPartial;
4821 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
4822 StartLoc, IdLoc, ToTPList, VarTemplate, T,
4823 TInfo, D->getStorageClass(), TemplateArgs,
4824 ArgInfos))
4825 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004826
4827 auto *FromInst = FromPartial->getInstantiatedFromMember();
4828 auto *ToInst = cast_or_null<PartVarSpecDecl>(Importer.Import(FromInst));
4829 if (FromInst && !ToInst)
4830 return nullptr;
4831
4832 ToPartial->setInstantiatedFromMember(ToInst);
4833 if (FromPartial->isMemberSpecialization())
4834 ToPartial->setMemberSpecialization();
4835
4836 D2 = ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004837 } else { // Full specialization
Gabor Marton26f72a92018-07-12 09:42:05 +00004838 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, StartLoc,
4839 IdLoc, VarTemplate, T, TInfo,
4840 D->getStorageClass(), TemplateArgs))
4841 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004842 }
4843
4844 SourceLocation POI = D->getPointOfInstantiation();
4845 if (POI.isValid())
4846 D2->setPointOfInstantiation(Importer.Import(POI));
4847
Larisse Voufo39a1e502013-08-06 01:03:05 +00004848 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004849 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004850
4851 // Add this specialization to the class template.
4852 VarTemplate->AddSpecialization(D2, InsertPos);
4853
4854 // Import the qualifier, if any.
4855 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4856
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004857 if (D->isConstexpr())
4858 D2->setConstexpr(true);
4859
Larisse Voufo39a1e502013-08-06 01:03:05 +00004860 // Add the specialization to this context.
4861 D2->setLexicalDeclContext(LexicalDC);
4862 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004863
4864 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00004865 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004866
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004867 // NOTE: isThisDeclarationADefinition() can return DeclarationOnly even if
4868 // declaration has initializer. Should this be fixed in the AST?.. Anyway,
4869 // we have to check the declaration for initializer - otherwise, it won't be
4870 // imported.
4871 if ((D->isThisDeclarationADefinition() || D->hasInit()) &&
4872 ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004873 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004874
4875 return D2;
4876}
4877
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004878Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
4879 DeclContext *DC, *LexicalDC;
4880 DeclarationName Name;
4881 SourceLocation Loc;
4882 NamedDecl *ToD;
4883
4884 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4885 return nullptr;
4886
4887 if (ToD)
4888 return ToD;
4889
4890 // Try to find a function in our own ("to") context with the same name, same
4891 // type, and in the same context as the function we're importing.
4892 if (!LexicalDC->isFunctionOrMethod()) {
4893 unsigned IDNS = Decl::IDNS_Ordinary;
4894 SmallVector<NamedDecl *, 2> FoundDecls;
4895 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004896 for (auto *FoundDecl : FoundDecls) {
4897 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004898 continue;
4899
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004900 if (auto *FoundFunction = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004901 if (FoundFunction->hasExternalFormalLinkage() &&
4902 D->hasExternalFormalLinkage()) {
4903 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00004904 Importer.MapImported(D, FoundFunction);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004905 // FIXME: Actually try to merge the body and other attributes.
4906 return FoundFunction;
4907 }
4908 }
4909 }
4910 }
4911 }
4912
4913 TemplateParameterList *Params =
4914 ImportTemplateParameterList(D->getTemplateParameters());
4915 if (!Params)
4916 return nullptr;
4917
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004918 auto *TemplatedFD =
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004919 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl()));
4920 if (!TemplatedFD)
4921 return nullptr;
4922
Gabor Marton26f72a92018-07-12 09:42:05 +00004923 FunctionTemplateDecl *ToFunc;
4924 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
4925 Params, TemplatedFD))
4926 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004927
4928 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
4929 ToFunc->setAccess(D->getAccess());
4930 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004931
4932 LexicalDC->addDeclInternal(ToFunc);
4933 return ToFunc;
4934}
4935
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004936//----------------------------------------------------------------------------
4937// Import Statements
4938//----------------------------------------------------------------------------
4939
Sean Callanan59721b32015-04-28 18:41:46 +00004940DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4941 if (DG.isNull())
4942 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4943 size_t NumDecls = DG.end() - DG.begin();
4944 SmallVector<Decl *, 1> ToDecls(NumDecls);
4945 auto &_Importer = this->Importer;
4946 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4947 [&_Importer](Decl *D) -> Decl * {
4948 return _Importer.Import(D);
4949 });
4950 return DeclGroupRef::Create(Importer.getToContext(),
4951 ToDecls.begin(),
4952 NumDecls);
4953}
4954
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004955Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004956 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
4957 << S->getStmtClassName();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004958 return nullptr;
4959}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004960
4961Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4962 SmallVector<IdentifierInfo *, 4> Names;
4963 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4964 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004965 // ToII is nullptr when no symbolic name is given for output operand
4966 // see ParseStmtAsm::ParseAsmOperandsOpt
4967 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004968 return nullptr;
4969 Names.push_back(ToII);
4970 }
4971 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4972 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004973 // ToII is nullptr when no symbolic name is given for input operand
4974 // see ParseStmtAsm::ParseAsmOperandsOpt
4975 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004976 return nullptr;
4977 Names.push_back(ToII);
4978 }
4979
4980 SmallVector<StringLiteral *, 4> Clobbers;
4981 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004982 auto *Clobber = cast_or_null<StringLiteral>(
4983 Importer.Import(S->getClobberStringLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004984 if (!Clobber)
4985 return nullptr;
4986 Clobbers.push_back(Clobber);
4987 }
4988
4989 SmallVector<StringLiteral *, 4> Constraints;
4990 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004991 auto *Output = cast_or_null<StringLiteral>(
4992 Importer.Import(S->getOutputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004993 if (!Output)
4994 return nullptr;
4995 Constraints.push_back(Output);
4996 }
4997
4998 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004999 auto *Input = cast_or_null<StringLiteral>(
5000 Importer.Import(S->getInputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005001 if (!Input)
5002 return nullptr;
5003 Constraints.push_back(Input);
5004 }
5005
5006 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005007 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005008 return nullptr;
5009
Aleksei Sidorina693b372016-09-28 10:16:56 +00005010 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005011 return nullptr;
5012
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005013 auto *AsmStr = cast_or_null<StringLiteral>(
5014 Importer.Import(S->getAsmString()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005015 if (!AsmStr)
5016 return nullptr;
5017
5018 return new (Importer.getToContext()) GCCAsmStmt(
5019 Importer.getToContext(),
5020 Importer.Import(S->getAsmLoc()),
5021 S->isSimple(),
5022 S->isVolatile(),
5023 S->getNumOutputs(),
5024 S->getNumInputs(),
5025 Names.data(),
5026 Constraints.data(),
5027 Exprs.data(),
5028 AsmStr,
5029 S->getNumClobbers(),
5030 Clobbers.data(),
5031 Importer.Import(S->getRParenLoc()));
5032}
5033
Sean Callanan59721b32015-04-28 18:41:46 +00005034Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5035 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005036 for (auto *ToD : ToDG) {
Sean Callanan59721b32015-04-28 18:41:46 +00005037 if (!ToD)
5038 return nullptr;
5039 }
Stephen Kellya6e43582018-08-09 21:05:56 +00005040 SourceLocation ToStartLoc = Importer.Import(S->getBeginLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005041 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
5042 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
5043}
5044
5045Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5046 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
5047 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
5048 S->hasLeadingEmptyMacro());
5049}
5050
5051Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005052 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005053
5054 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00005055 return nullptr;
5056
Sean Callanan59721b32015-04-28 18:41:46 +00005057 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
5058 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
Benjamin Kramer07420902017-12-24 16:24:20 +00005059 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
5060 ToRBraceLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005061}
5062
5063Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5064 Expr *ToLHS = Importer.Import(S->getLHS());
5065 if (!ToLHS)
5066 return nullptr;
5067 Expr *ToRHS = Importer.Import(S->getRHS());
5068 if (!ToRHS && S->getRHS())
5069 return nullptr;
Gabor Horvath480892b2017-10-18 09:25:18 +00005070 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5071 if (!ToSubStmt && S->getSubStmt())
5072 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005073 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
5074 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
5075 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005076 auto *ToStmt = new (Importer.getToContext())
Gabor Horvath480892b2017-10-18 09:25:18 +00005077 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
5078 ToStmt->setSubStmt(ToSubStmt);
5079 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005080}
5081
5082Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5083 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
5084 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5085 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5086 if (!ToSubStmt && S->getSubStmt())
5087 return nullptr;
5088 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
5089 ToSubStmt);
5090}
5091
5092Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5093 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005094 auto *ToLabelDecl = cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00005095 if (!ToLabelDecl && S->getDecl())
5096 return nullptr;
5097 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5098 if (!ToSubStmt && S->getSubStmt())
5099 return nullptr;
5100 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
5101 ToSubStmt);
5102}
5103
5104Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5105 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
5106 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5107 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00005108 if (ImportContainerChecked(FromAttrs, ToAttrs))
5109 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005110 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5111 if (!ToSubStmt && S->getSubStmt())
5112 return nullptr;
5113 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
5114 ToAttrs, ToSubStmt);
5115}
5116
5117Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5118 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00005119 Stmt *ToInit = Importer.Import(S->getInit());
5120 if (!ToInit && S->getInit())
5121 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005122 VarDecl *ToConditionVariable = nullptr;
5123 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5124 ToConditionVariable =
5125 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5126 if (!ToConditionVariable)
5127 return nullptr;
5128 }
5129 Expr *ToCondition = Importer.Import(S->getCond());
5130 if (!ToCondition && S->getCond())
5131 return nullptr;
5132 Stmt *ToThenStmt = Importer.Import(S->getThen());
5133 if (!ToThenStmt && S->getThen())
5134 return nullptr;
5135 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
5136 Stmt *ToElseStmt = Importer.Import(S->getElse());
5137 if (!ToElseStmt && S->getElse())
5138 return nullptr;
5139 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00005140 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00005141 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00005142 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00005143 ToCondition, ToThenStmt,
5144 ToElseLoc, ToElseStmt);
5145}
5146
5147Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00005148 Stmt *ToInit = Importer.Import(S->getInit());
5149 if (!ToInit && S->getInit())
5150 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005151 VarDecl *ToConditionVariable = nullptr;
5152 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5153 ToConditionVariable =
5154 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5155 if (!ToConditionVariable)
5156 return nullptr;
5157 }
5158 Expr *ToCondition = Importer.Import(S->getCond());
5159 if (!ToCondition && S->getCond())
5160 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005161 auto *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00005162 Importer.getToContext(), ToInit,
5163 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00005164 Stmt *ToBody = Importer.Import(S->getBody());
5165 if (!ToBody && S->getBody())
5166 return nullptr;
5167 ToStmt->setBody(ToBody);
5168 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
5169 // Now we have to re-chain the cases.
5170 SwitchCase *LastChainedSwitchCase = nullptr;
5171 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5172 SC = SC->getNextSwitchCase()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005173 auto *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
Sean Callanan59721b32015-04-28 18:41:46 +00005174 if (!ToSC)
5175 return nullptr;
5176 if (LastChainedSwitchCase)
5177 LastChainedSwitchCase->setNextSwitchCase(ToSC);
5178 else
5179 ToStmt->setSwitchCaseList(ToSC);
5180 LastChainedSwitchCase = ToSC;
5181 }
5182 return ToStmt;
5183}
5184
5185Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5186 VarDecl *ToConditionVariable = nullptr;
5187 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5188 ToConditionVariable =
5189 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5190 if (!ToConditionVariable)
5191 return nullptr;
5192 }
5193 Expr *ToCondition = Importer.Import(S->getCond());
5194 if (!ToCondition && S->getCond())
5195 return nullptr;
5196 Stmt *ToBody = Importer.Import(S->getBody());
5197 if (!ToBody && S->getBody())
5198 return nullptr;
5199 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5200 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
5201 ToConditionVariable,
5202 ToCondition, ToBody,
5203 ToWhileLoc);
5204}
5205
5206Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5207 Stmt *ToBody = Importer.Import(S->getBody());
5208 if (!ToBody && S->getBody())
5209 return nullptr;
5210 Expr *ToCondition = Importer.Import(S->getCond());
5211 if (!ToCondition && S->getCond())
5212 return nullptr;
5213 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
5214 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5215 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5216 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
5217 ToDoLoc, ToWhileLoc,
5218 ToRParenLoc);
5219}
5220
5221Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
5222 Stmt *ToInit = Importer.Import(S->getInit());
5223 if (!ToInit && S->getInit())
5224 return nullptr;
5225 Expr *ToCondition = Importer.Import(S->getCond());
5226 if (!ToCondition && S->getCond())
5227 return nullptr;
5228 VarDecl *ToConditionVariable = nullptr;
5229 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5230 ToConditionVariable =
5231 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5232 if (!ToConditionVariable)
5233 return nullptr;
5234 }
5235 Expr *ToInc = Importer.Import(S->getInc());
5236 if (!ToInc && S->getInc())
5237 return nullptr;
5238 Stmt *ToBody = Importer.Import(S->getBody());
5239 if (!ToBody && S->getBody())
5240 return nullptr;
5241 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5242 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
5243 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5244 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
5245 ToInit, ToCondition,
5246 ToConditionVariable,
5247 ToInc, ToBody,
5248 ToForLoc, ToLParenLoc,
5249 ToRParenLoc);
5250}
5251
5252Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5253 LabelDecl *ToLabel = nullptr;
5254 if (LabelDecl *FromLabel = S->getLabel()) {
5255 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
5256 if (!ToLabel)
5257 return nullptr;
5258 }
5259 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5260 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
5261 return new (Importer.getToContext()) GotoStmt(ToLabel,
5262 ToGotoLoc, ToLabelLoc);
5263}
5264
5265Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5266 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5267 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
5268 Expr *ToTarget = Importer.Import(S->getTarget());
5269 if (!ToTarget && S->getTarget())
5270 return nullptr;
5271 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
5272 ToTarget);
5273}
5274
5275Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5276 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
5277 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
5278}
5279
5280Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5281 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
5282 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
5283}
5284
5285Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5286 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
5287 Expr *ToRetExpr = Importer.Import(S->getRetValue());
5288 if (!ToRetExpr && S->getRetValue())
5289 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005290 auto *NRVOCandidate = const_cast<VarDecl *>(S->getNRVOCandidate());
5291 auto *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
Sean Callanan59721b32015-04-28 18:41:46 +00005292 if (!ToNRVOCandidate && NRVOCandidate)
5293 return nullptr;
5294 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
5295 ToNRVOCandidate);
5296}
5297
5298Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5299 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
5300 VarDecl *ToExceptionDecl = nullptr;
5301 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
5302 ToExceptionDecl =
5303 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5304 if (!ToExceptionDecl)
5305 return nullptr;
5306 }
5307 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
5308 if (!ToHandlerBlock && S->getHandlerBlock())
5309 return nullptr;
5310 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
5311 ToExceptionDecl,
5312 ToHandlerBlock);
5313}
5314
5315Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5316 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
5317 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
5318 if (!ToTryBlock && S->getTryBlock())
5319 return nullptr;
5320 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5321 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5322 CXXCatchStmt *FromHandler = S->getHandler(HI);
5323 if (Stmt *ToHandler = Importer.Import(FromHandler))
5324 ToHandlers[HI] = ToHandler;
5325 else
5326 return nullptr;
5327 }
5328 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
5329 ToHandlers);
5330}
5331
5332Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005333 auto *ToRange =
Sean Callanan59721b32015-04-28 18:41:46 +00005334 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
5335 if (!ToRange && S->getRangeStmt())
5336 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005337 auto *ToBegin =
Richard Smith01694c32016-03-20 10:33:40 +00005338 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
5339 if (!ToBegin && S->getBeginStmt())
5340 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005341 auto *ToEnd =
Richard Smith01694c32016-03-20 10:33:40 +00005342 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
5343 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00005344 return nullptr;
5345 Expr *ToCond = Importer.Import(S->getCond());
5346 if (!ToCond && S->getCond())
5347 return nullptr;
5348 Expr *ToInc = Importer.Import(S->getInc());
5349 if (!ToInc && S->getInc())
5350 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005351 auto *ToLoopVar =
Sean Callanan59721b32015-04-28 18:41:46 +00005352 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
5353 if (!ToLoopVar && S->getLoopVarStmt())
5354 return nullptr;
5355 Stmt *ToBody = Importer.Import(S->getBody());
5356 if (!ToBody && S->getBody())
5357 return nullptr;
5358 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00005359 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005360 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5361 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005362 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005363 ToCond, ToInc,
5364 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005365 ToForLoc, ToCoawaitLoc,
5366 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005367}
5368
5369Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5370 Stmt *ToElem = Importer.Import(S->getElement());
5371 if (!ToElem && S->getElement())
5372 return nullptr;
5373 Expr *ToCollect = Importer.Import(S->getCollection());
5374 if (!ToCollect && S->getCollection())
5375 return nullptr;
5376 Stmt *ToBody = Importer.Import(S->getBody());
5377 if (!ToBody && S->getBody())
5378 return nullptr;
5379 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5380 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5381 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5382 ToCollect,
5383 ToBody, ToForLoc,
5384 ToRParenLoc);
5385}
5386
5387Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5388 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5389 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5390 VarDecl *ToExceptionDecl = nullptr;
5391 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5392 ToExceptionDecl =
5393 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5394 if (!ToExceptionDecl)
5395 return nullptr;
5396 }
5397 Stmt *ToBody = Importer.Import(S->getCatchBody());
5398 if (!ToBody && S->getCatchBody())
5399 return nullptr;
5400 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5401 ToRParenLoc,
5402 ToExceptionDecl,
5403 ToBody);
5404}
5405
5406Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5407 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5408 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5409 if (!ToAtFinallyStmt && S->getFinallyBody())
5410 return nullptr;
5411 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5412 ToAtFinallyStmt);
5413}
5414
5415Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5416 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5417 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5418 if (!ToAtTryStmt && S->getTryBody())
5419 return nullptr;
5420 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5421 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5422 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5423 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5424 ToCatchStmts[CI] = ToCatchStmt;
5425 else
5426 return nullptr;
5427 }
5428 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5429 if (!ToAtFinallyStmt && S->getFinallyStmt())
5430 return nullptr;
5431 return ObjCAtTryStmt::Create(Importer.getToContext(),
5432 ToAtTryLoc, ToAtTryStmt,
5433 ToCatchStmts.begin(), ToCatchStmts.size(),
5434 ToAtFinallyStmt);
5435}
5436
5437Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5438 (ObjCAtSynchronizedStmt *S) {
5439 SourceLocation ToAtSynchronizedLoc =
5440 Importer.Import(S->getAtSynchronizedLoc());
5441 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5442 if (!ToSynchExpr && S->getSynchExpr())
5443 return nullptr;
5444 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5445 if (!ToSynchBody && S->getSynchBody())
5446 return nullptr;
5447 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5448 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5449}
5450
5451Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5452 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5453 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5454 if (!ToThrow && S->getThrowExpr())
5455 return nullptr;
5456 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5457}
5458
5459Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5460 (ObjCAutoreleasePoolStmt *S) {
5461 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5462 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5463 if (!ToSubStmt && S->getSubStmt())
5464 return nullptr;
5465 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5466 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005467}
5468
5469//----------------------------------------------------------------------------
5470// Import Expressions
5471//----------------------------------------------------------------------------
5472Expr *ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005473 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
5474 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005475 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005476}
5477
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005478Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5479 QualType T = Importer.Import(E->getType());
5480 if (T.isNull())
5481 return nullptr;
5482
5483 Expr *SubExpr = Importer.Import(E->getSubExpr());
5484 if (!SubExpr && E->getSubExpr())
5485 return nullptr;
5486
5487 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5488 if (!TInfo)
5489 return nullptr;
5490
5491 return new (Importer.getToContext()) VAArgExpr(
5492 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5493 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5494}
5495
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005496Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5497 QualType T = Importer.Import(E->getType());
5498 if (T.isNull())
5499 return nullptr;
5500
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005501 return new (Importer.getToContext())
5502 GNUNullExpr(T, Importer.Import(E->getBeginLoc()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005503}
5504
5505Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5506 QualType T = Importer.Import(E->getType());
5507 if (T.isNull())
5508 return nullptr;
5509
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005510 auto *SL = cast_or_null<StringLiteral>(Importer.Import(E->getFunctionName()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005511 if (!SL && E->getFunctionName())
5512 return nullptr;
5513
5514 return new (Importer.getToContext()) PredefinedExpr(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005515 Importer.Import(E->getBeginLoc()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005516}
5517
Douglas Gregor52f820e2010-02-19 01:17:02 +00005518Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005519 auto *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
Douglas Gregor52f820e2010-02-19 01:17:02 +00005520 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005521 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005522
Craig Topper36250ad2014-05-12 05:36:57 +00005523 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005524 if (E->getDecl() != E->getFoundDecl()) {
5525 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5526 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005527 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005528 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005529
Douglas Gregor52f820e2010-02-19 01:17:02 +00005530 QualType T = Importer.Import(E->getType());
5531 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005532 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005533
Aleksei Sidorina693b372016-09-28 10:16:56 +00005534 TemplateArgumentListInfo ToTAInfo;
5535 TemplateArgumentListInfo *ResInfo = nullptr;
5536 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005537 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
5538 return nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00005539 ResInfo = &ToTAInfo;
5540 }
5541
Fangrui Song6907ce22018-07-30 19:24:48 +00005542 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005543 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005544 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005545 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005546 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005547 Importer.Import(E->getLocation()),
5548 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005549 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005550 if (E->hadMultipleCandidates())
5551 DRE->setHadMultipleCandidates(true);
5552 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005553}
5554
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005555Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5556 QualType T = Importer.Import(E->getType());
5557 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00005558 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005559
5560 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5561}
5562
5563ASTNodeImporter::Designator
5564ASTNodeImporter::ImportDesignator(const Designator &D) {
5565 if (D.isFieldDesignator()) {
5566 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5567 // Caller checks for import error
5568 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5569 Importer.Import(D.getFieldLoc()));
5570 }
5571 if (D.isArrayDesignator())
5572 return Designator(D.getFirstExprIndex(),
5573 Importer.Import(D.getLBracketLoc()),
5574 Importer.Import(D.getRBracketLoc()));
5575
5576 assert(D.isArrayRangeDesignator());
5577 return Designator(D.getFirstExprIndex(),
5578 Importer.Import(D.getLBracketLoc()),
5579 Importer.Import(D.getEllipsisLoc()),
5580 Importer.Import(D.getRBracketLoc()));
5581}
5582
5583
5584Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005585 auto *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005586 if (!Init)
5587 return nullptr;
5588
5589 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5590 // List elements from the second, the first is Init itself
5591 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005592 if (auto *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005593 IndexExprs[I - 1] = Arg;
5594 else
5595 return nullptr;
5596 }
5597
5598 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005599 llvm::transform(DIE->designators(), Designators.begin(),
5600 [this](const Designator &D) -> Designator {
5601 return ImportDesignator(D);
5602 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005603
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005604 for (const auto &D : DIE->designators())
David Majnemerf7e36092016-06-23 00:15:04 +00005605 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005606 return nullptr;
5607
5608 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005609 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005610 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5611 DIE->usesGNUSyntax(), Init);
5612}
5613
5614Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5615 QualType T = Importer.Import(E->getType());
5616 if (T.isNull())
5617 return nullptr;
5618
5619 return new (Importer.getToContext())
5620 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5621}
5622
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005623Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5624 QualType T = Importer.Import(E->getType());
5625 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005626 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005627
Fangrui Song6907ce22018-07-30 19:24:48 +00005628 return IntegerLiteral::Create(Importer.getToContext(),
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005629 E->getValue(), T,
5630 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005631}
5632
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005633Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5634 QualType T = Importer.Import(E->getType());
5635 if (T.isNull())
5636 return nullptr;
5637
5638 return FloatingLiteral::Create(Importer.getToContext(),
5639 E->getValue(), E->isExact(), T,
5640 Importer.Import(E->getLocation()));
5641}
5642
Gabor Martonbf7f18b2018-08-09 12:18:07 +00005643Expr *ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
5644 QualType T = Importer.Import(E->getType());
5645 if (T.isNull())
5646 return nullptr;
5647
5648 Expr *SubE = Importer.Import(E->getSubExpr());
5649 if (!SubE)
5650 return nullptr;
5651
5652 return new (Importer.getToContext()) ImaginaryLiteral(SubE, T);
5653}
5654
Douglas Gregor623421d2010-02-18 02:21:22 +00005655Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5656 QualType T = Importer.Import(E->getType());
5657 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005658 return nullptr;
5659
Douglas Gregorfb65e592011-07-27 05:40:30 +00005660 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5661 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005662 Importer.Import(E->getLocation()));
5663}
5664
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005665Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5666 QualType T = Importer.Import(E->getType());
5667 if (T.isNull())
5668 return nullptr;
5669
5670 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5671 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5672
5673 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5674 E->getKind(), E->isPascal(), T,
5675 Locations.data(), Locations.size());
5676}
5677
5678Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5679 QualType T = Importer.Import(E->getType());
5680 if (T.isNull())
5681 return nullptr;
5682
5683 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5684 if (!TInfo)
5685 return nullptr;
5686
5687 Expr *Init = Importer.Import(E->getInitializer());
5688 if (!Init)
5689 return nullptr;
5690
5691 return new (Importer.getToContext()) CompoundLiteralExpr(
5692 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5693 Init, E->isFileScope());
5694}
5695
5696Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5697 QualType T = Importer.Import(E->getType());
5698 if (T.isNull())
5699 return nullptr;
5700
5701 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5702 if (ImportArrayChecked(
5703 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5704 Exprs.begin()))
5705 return nullptr;
5706
5707 return new (Importer.getToContext()) AtomicExpr(
5708 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5709 Importer.Import(E->getRParenLoc()));
5710}
5711
5712Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5713 QualType T = Importer.Import(E->getType());
5714 if (T.isNull())
5715 return nullptr;
5716
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005717 auto *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005718 if (!ToLabel)
5719 return nullptr;
5720
5721 return new (Importer.getToContext()) AddrLabelExpr(
5722 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5723 ToLabel, T);
5724}
5725
Douglas Gregorc74247e2010-02-19 01:07:06 +00005726Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5727 Expr *SubExpr = Importer.Import(E->getSubExpr());
5728 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005729 return nullptr;
5730
Fangrui Song6907ce22018-07-30 19:24:48 +00005731 return new (Importer.getToContext())
Douglas Gregorc74247e2010-02-19 01:07:06 +00005732 ParenExpr(Importer.Import(E->getLParen()),
5733 Importer.Import(E->getRParen()),
5734 SubExpr);
5735}
5736
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005737Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5738 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005739 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005740 return nullptr;
5741
5742 return new (Importer.getToContext()) ParenListExpr(
5743 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5744 Exprs, Importer.Import(E->getLParenLoc()));
5745}
5746
5747Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5748 QualType T = Importer.Import(E->getType());
5749 if (T.isNull())
5750 return nullptr;
5751
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005752 auto *ToSubStmt = cast_or_null<CompoundStmt>(
5753 Importer.Import(E->getSubStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005754 if (!ToSubStmt && E->getSubStmt())
5755 return nullptr;
5756
5757 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5758 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5759}
5760
Douglas Gregorc74247e2010-02-19 01:07:06 +00005761Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5762 QualType T = Importer.Import(E->getType());
5763 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005764 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005765
5766 Expr *SubExpr = Importer.Import(E->getSubExpr());
5767 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005768 return nullptr;
5769
Aaron Ballmana5038552018-01-09 13:07:03 +00005770 return new (Importer.getToContext()) UnaryOperator(
5771 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(),
5772 Importer.Import(E->getOperatorLoc()), E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005773}
5774
Aaron Ballmana5038552018-01-09 13:07:03 +00005775Expr *
5776ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005777 QualType ResultType = Importer.Import(E->getType());
Fangrui Song6907ce22018-07-30 19:24:48 +00005778
Douglas Gregord8552cd2010-02-19 01:24:23 +00005779 if (E->isArgumentType()) {
5780 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5781 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005782 return nullptr;
5783
Peter Collingbournee190dee2011-03-11 19:24:49 +00005784 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5785 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005786 Importer.Import(E->getOperatorLoc()),
5787 Importer.Import(E->getRParenLoc()));
5788 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005789
Douglas Gregord8552cd2010-02-19 01:24:23 +00005790 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5791 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005792 return nullptr;
5793
Peter Collingbournee190dee2011-03-11 19:24:49 +00005794 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5795 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005796 Importer.Import(E->getOperatorLoc()),
5797 Importer.Import(E->getRParenLoc()));
5798}
5799
Douglas Gregorc74247e2010-02-19 01:07:06 +00005800Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5801 QualType T = Importer.Import(E->getType());
5802 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005803 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005804
5805 Expr *LHS = Importer.Import(E->getLHS());
5806 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005807 return nullptr;
5808
Douglas Gregorc74247e2010-02-19 01:07:06 +00005809 Expr *RHS = Importer.Import(E->getRHS());
5810 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005811 return nullptr;
5812
Douglas Gregorc74247e2010-02-19 01:07:06 +00005813 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005814 T, E->getValueKind(),
5815 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005816 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005817 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005818}
5819
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005820Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5821 QualType T = Importer.Import(E->getType());
5822 if (T.isNull())
5823 return nullptr;
5824
5825 Expr *ToLHS = Importer.Import(E->getLHS());
5826 if (!ToLHS)
5827 return nullptr;
5828
5829 Expr *ToRHS = Importer.Import(E->getRHS());
5830 if (!ToRHS)
5831 return nullptr;
5832
5833 Expr *ToCond = Importer.Import(E->getCond());
5834 if (!ToCond)
5835 return nullptr;
5836
5837 return new (Importer.getToContext()) ConditionalOperator(
5838 ToCond, Importer.Import(E->getQuestionLoc()),
5839 ToLHS, Importer.Import(E->getColonLoc()),
5840 ToRHS, T, E->getValueKind(), E->getObjectKind());
5841}
5842
5843Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5844 BinaryConditionalOperator *E) {
5845 QualType T = Importer.Import(E->getType());
5846 if (T.isNull())
5847 return nullptr;
5848
5849 Expr *Common = Importer.Import(E->getCommon());
5850 if (!Common)
5851 return nullptr;
5852
5853 Expr *Cond = Importer.Import(E->getCond());
5854 if (!Cond)
5855 return nullptr;
5856
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005857 auto *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5858 Importer.Import(E->getOpaqueValue()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005859 if (!OpaqueValue)
5860 return nullptr;
5861
5862 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5863 if (!TrueExpr)
5864 return nullptr;
5865
5866 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5867 if (!FalseExpr)
5868 return nullptr;
5869
5870 return new (Importer.getToContext()) BinaryConditionalOperator(
5871 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5872 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5873 T, E->getValueKind(), E->getObjectKind());
5874}
5875
Aleksei Sidorina693b372016-09-28 10:16:56 +00005876Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5877 QualType T = Importer.Import(E->getType());
5878 if (T.isNull())
5879 return nullptr;
5880
5881 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5882 if (!ToQueried)
5883 return nullptr;
5884
5885 Expr *Dim = Importer.Import(E->getDimensionExpression());
5886 if (!Dim && E->getDimensionExpression())
5887 return nullptr;
5888
5889 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005890 Importer.Import(E->getBeginLoc()), E->getTrait(), ToQueried,
Stephen Kelly1c301dc2018-08-09 21:09:38 +00005891 E->getValue(), Dim, Importer.Import(E->getEndLoc()), T);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005892}
5893
5894Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5895 QualType T = Importer.Import(E->getType());
5896 if (T.isNull())
5897 return nullptr;
5898
5899 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5900 if (!ToQueried)
5901 return nullptr;
5902
5903 return new (Importer.getToContext()) ExpressionTraitExpr(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005904 Importer.Import(E->getBeginLoc()), E->getTrait(), ToQueried,
Stephen Kelly1c301dc2018-08-09 21:09:38 +00005905 E->getValue(), Importer.Import(E->getEndLoc()), T);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005906}
5907
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005908Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5909 QualType T = Importer.Import(E->getType());
5910 if (T.isNull())
5911 return nullptr;
5912
5913 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5914 if (!SourceExpr && E->getSourceExpr())
5915 return nullptr;
5916
5917 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005918 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005919 E->getObjectKind(), SourceExpr);
5920}
5921
Aleksei Sidorina693b372016-09-28 10:16:56 +00005922Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5923 QualType T = Importer.Import(E->getType());
5924 if (T.isNull())
5925 return nullptr;
5926
5927 Expr *ToLHS = Importer.Import(E->getLHS());
5928 if (!ToLHS)
5929 return nullptr;
5930
5931 Expr *ToRHS = Importer.Import(E->getRHS());
5932 if (!ToRHS)
5933 return nullptr;
5934
5935 return new (Importer.getToContext()) ArraySubscriptExpr(
5936 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5937 Importer.Import(E->getRBracketLoc()));
5938}
5939
Douglas Gregorc74247e2010-02-19 01:07:06 +00005940Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5941 QualType T = Importer.Import(E->getType());
5942 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005943 return nullptr;
5944
Douglas Gregorc74247e2010-02-19 01:07:06 +00005945 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5946 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005947 return nullptr;
5948
Douglas Gregorc74247e2010-02-19 01:07:06 +00005949 QualType CompResultType = Importer.Import(E->getComputationResultType());
5950 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005951 return nullptr;
5952
Douglas Gregorc74247e2010-02-19 01:07:06 +00005953 Expr *LHS = Importer.Import(E->getLHS());
5954 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005955 return nullptr;
5956
Douglas Gregorc74247e2010-02-19 01:07:06 +00005957 Expr *RHS = Importer.Import(E->getRHS());
5958 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005959 return nullptr;
5960
Fangrui Song6907ce22018-07-30 19:24:48 +00005961 return new (Importer.getToContext())
Douglas Gregorc74247e2010-02-19 01:07:06 +00005962 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005963 T, E->getValueKind(),
5964 E->getObjectKind(),
5965 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005966 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005967 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005968}
5969
Aleksei Sidorina693b372016-09-28 10:16:56 +00005970bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
5971 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
5972 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
5973 Path.push_back(Spec);
5974 else
5975 return true;
5976 }
5977 return false;
John McCallcf142162010-08-07 06:22:56 +00005978}
5979
Douglas Gregor98c10182010-02-12 22:17:39 +00005980Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5981 QualType T = Importer.Import(E->getType());
5982 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005983 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005984
5985 Expr *SubExpr = Importer.Import(E->getSubExpr());
5986 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005987 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005988
5989 CXXCastPath BasePath;
5990 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005991 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005992
5993 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005994 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005995}
5996
Aleksei Sidorina693b372016-09-28 10:16:56 +00005997Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00005998 QualType T = Importer.Import(E->getType());
5999 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006000 return nullptr;
6001
Douglas Gregor5481d322010-02-19 01:32:14 +00006002 Expr *SubExpr = Importer.Import(E->getSubExpr());
6003 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00006004 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00006005
6006 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
6007 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00006008 return nullptr;
6009
John McCallcf142162010-08-07 06:22:56 +00006010 CXXCastPath BasePath;
6011 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00006012 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00006013
Aleksei Sidorina693b372016-09-28 10:16:56 +00006014 switch (E->getStmtClass()) {
6015 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006016 auto *CCE = cast<CStyleCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006017 return CStyleCastExpr::Create(Importer.getToContext(), T,
6018 E->getValueKind(), E->getCastKind(),
6019 SubExpr, &BasePath, TInfo,
6020 Importer.Import(CCE->getLParenLoc()),
6021 Importer.Import(CCE->getRParenLoc()));
6022 }
6023
6024 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006025 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006026 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
6027 E->getValueKind(), TInfo,
6028 E->getCastKind(), SubExpr, &BasePath,
6029 Importer.Import(FCE->getLParenLoc()),
6030 Importer.Import(FCE->getRParenLoc()));
6031 }
6032
6033 case Stmt::ObjCBridgedCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006034 auto *OCE = cast<ObjCBridgedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006035 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6036 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
6037 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
6038 TInfo, SubExpr);
6039 }
6040 default:
6041 break; // just fall through
6042 }
6043
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006044 auto *Named = cast<CXXNamedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006045 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
6046 RParenLoc = Importer.Import(Named->getRParenLoc());
6047 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
6048
6049 switch (E->getStmtClass()) {
6050 case Stmt::CXXStaticCastExprClass:
6051 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
6052 E->getValueKind(), E->getCastKind(),
6053 SubExpr, &BasePath, TInfo,
6054 ExprLoc, RParenLoc, Brackets);
6055
6056 case Stmt::CXXDynamicCastExprClass:
6057 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
6058 E->getValueKind(), E->getCastKind(),
6059 SubExpr, &BasePath, TInfo,
6060 ExprLoc, RParenLoc, Brackets);
6061
6062 case Stmt::CXXReinterpretCastExprClass:
6063 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
6064 E->getValueKind(), E->getCastKind(),
6065 SubExpr, &BasePath, TInfo,
6066 ExprLoc, RParenLoc, Brackets);
6067
6068 case Stmt::CXXConstCastExprClass:
6069 return CXXConstCastExpr::Create(Importer.getToContext(), T,
6070 E->getValueKind(), SubExpr, TInfo, ExprLoc,
6071 RParenLoc, Brackets);
6072 default:
6073 llvm_unreachable("Cast expression of unsupported type!");
6074 return nullptr;
6075 }
6076}
6077
6078Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
6079 QualType T = Importer.Import(OE->getType());
6080 if (T.isNull())
6081 return nullptr;
6082
6083 SmallVector<OffsetOfNode, 4> Nodes;
6084 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
6085 const OffsetOfNode &Node = OE->getComponent(I);
6086
6087 switch (Node.getKind()) {
6088 case OffsetOfNode::Array:
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006089 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getBeginLoc()),
Aleksei Sidorina693b372016-09-28 10:16:56 +00006090 Node.getArrayExprIndex(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006091 Importer.Import(Node.getEndLoc())));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006092 break;
6093
6094 case OffsetOfNode::Base: {
6095 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
6096 if (!BS && Node.getBase())
6097 return nullptr;
6098 Nodes.push_back(OffsetOfNode(BS));
6099 break;
6100 }
6101 case OffsetOfNode::Field: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006102 auto *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006103 if (!FD)
6104 return nullptr;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006105 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getBeginLoc()), FD,
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006106 Importer.Import(Node.getEndLoc())));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006107 break;
6108 }
6109 case OffsetOfNode::Identifier: {
6110 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
6111 if (!ToII)
6112 return nullptr;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006113 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getBeginLoc()), ToII,
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006114 Importer.Import(Node.getEndLoc())));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006115 break;
6116 }
6117 }
6118 }
6119
6120 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
6121 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
6122 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
6123 if (!ToIndexExpr)
6124 return nullptr;
6125 Exprs[I] = ToIndexExpr;
6126 }
6127
6128 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
6129 if (!TInfo && OE->getTypeSourceInfo())
6130 return nullptr;
6131
6132 return OffsetOfExpr::Create(Importer.getToContext(), T,
6133 Importer.Import(OE->getOperatorLoc()),
6134 TInfo, Nodes, Exprs,
6135 Importer.Import(OE->getRParenLoc()));
6136}
6137
6138Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6139 QualType T = Importer.Import(E->getType());
6140 if (T.isNull())
6141 return nullptr;
6142
6143 Expr *Operand = Importer.Import(E->getOperand());
6144 if (!Operand)
6145 return nullptr;
6146
6147 CanThrowResult CanThrow;
6148 if (E->isValueDependent())
6149 CanThrow = CT_Dependent;
6150 else
6151 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
6152
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006153 return new (Importer.getToContext())
6154 CXXNoexceptExpr(T, Operand, CanThrow, Importer.Import(E->getBeginLoc()),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006155 Importer.Import(E->getEndLoc()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006156}
6157
6158Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6159 QualType T = Importer.Import(E->getType());
6160 if (T.isNull())
6161 return nullptr;
6162
6163 Expr *SubExpr = Importer.Import(E->getSubExpr());
6164 if (!SubExpr && E->getSubExpr())
6165 return nullptr;
6166
6167 return new (Importer.getToContext()) CXXThrowExpr(
6168 SubExpr, T, Importer.Import(E->getThrowLoc()),
6169 E->isThrownVariableInScope());
6170}
6171
6172Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006173 auto *Param = cast_or_null<ParmVarDecl>(Importer.Import(E->getParam()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006174 if (!Param)
6175 return nullptr;
6176
6177 return CXXDefaultArgExpr::Create(
6178 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
6179}
6180
6181Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6182 QualType T = Importer.Import(E->getType());
6183 if (T.isNull())
6184 return nullptr;
6185
6186 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
6187 if (!TypeInfo)
6188 return nullptr;
6189
6190 return new (Importer.getToContext()) CXXScalarValueInitExpr(
6191 T, TypeInfo, Importer.Import(E->getRParenLoc()));
6192}
6193
6194Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6195 Expr *SubExpr = Importer.Import(E->getSubExpr());
6196 if (!SubExpr)
6197 return nullptr;
6198
6199 auto *Dtor = cast_or_null<CXXDestructorDecl>(
6200 Importer.Import(const_cast<CXXDestructorDecl *>(
6201 E->getTemporary()->getDestructor())));
6202 if (!Dtor)
6203 return nullptr;
6204
6205 ASTContext &ToCtx = Importer.getToContext();
6206 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
6207 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
6208}
6209
6210Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
6211 QualType T = Importer.Import(CE->getType());
6212 if (T.isNull())
6213 return nullptr;
6214
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006215 TypeSourceInfo *TInfo = Importer.Import(CE->getTypeSourceInfo());
6216 if (!TInfo)
6217 return nullptr;
6218
Aleksei Sidorina693b372016-09-28 10:16:56 +00006219 SmallVector<Expr *, 8> Args(CE->getNumArgs());
6220 if (ImportContainerChecked(CE->arguments(), Args))
6221 return nullptr;
6222
6223 auto *Ctor = cast_or_null<CXXConstructorDecl>(
6224 Importer.Import(CE->getConstructor()));
6225 if (!Ctor)
6226 return nullptr;
6227
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006228 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
6229 Importer.getToContext(), Ctor, T, TInfo, Args,
6230 Importer.Import(CE->getParenOrBraceRange()), CE->hadMultipleCandidates(),
6231 CE->isListInitialization(), CE->isStdInitListInitialization(),
6232 CE->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006233}
6234
6235Expr *
6236ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
6237 QualType T = Importer.Import(E->getType());
6238 if (T.isNull())
6239 return nullptr;
6240
6241 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
6242 if (!TempE)
6243 return nullptr;
6244
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006245 auto *ExtendedBy = cast_or_null<ValueDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006246 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
6247 if (!ExtendedBy && E->getExtendingDecl())
6248 return nullptr;
6249
6250 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
6251 T, TempE, E->isBoundToLvalueReference());
6252
6253 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
6254 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
6255 return ToMTE;
6256}
6257
Gabor Horvath7a91c082017-11-14 11:30:38 +00006258Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6259 QualType T = Importer.Import(E->getType());
6260 if (T.isNull())
6261 return nullptr;
6262
6263 Expr *Pattern = Importer.Import(E->getPattern());
6264 if (!Pattern)
6265 return nullptr;
6266
6267 return new (Importer.getToContext()) PackExpansionExpr(
6268 T, Pattern, Importer.Import(E->getEllipsisLoc()),
6269 E->getNumExpansions());
6270}
6271
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006272Expr *ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6273 auto *Pack = cast_or_null<NamedDecl>(Importer.Import(E->getPack()));
6274 if (!Pack)
6275 return nullptr;
6276
6277 Optional<unsigned> Length;
6278
6279 if (!E->isValueDependent())
6280 Length = E->getPackLength();
6281
6282 SmallVector<TemplateArgument, 8> PartialArguments;
6283 if (E->isPartiallySubstituted()) {
6284 if (ImportTemplateArguments(E->getPartialArguments().data(),
6285 E->getPartialArguments().size(),
6286 PartialArguments))
6287 return nullptr;
6288 }
6289
6290 return SizeOfPackExpr::Create(
6291 Importer.getToContext(), Importer.Import(E->getOperatorLoc()), Pack,
6292 Importer.Import(E->getPackLoc()), Importer.Import(E->getRParenLoc()),
6293 Length, PartialArguments);
6294}
6295
Aleksei Sidorina693b372016-09-28 10:16:56 +00006296Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
6297 QualType T = Importer.Import(CE->getType());
6298 if (T.isNull())
6299 return nullptr;
6300
6301 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
6302 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
6303 return nullptr;
6304
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006305 auto *OperatorNewDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006306 Importer.Import(CE->getOperatorNew()));
6307 if (!OperatorNewDecl && CE->getOperatorNew())
6308 return nullptr;
6309
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006310 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006311 Importer.Import(CE->getOperatorDelete()));
6312 if (!OperatorDeleteDecl && CE->getOperatorDelete())
6313 return nullptr;
6314
6315 Expr *ToInit = Importer.Import(CE->getInitializer());
6316 if (!ToInit && CE->getInitializer())
6317 return nullptr;
6318
6319 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
6320 if (!TInfo)
6321 return nullptr;
6322
6323 Expr *ToArrSize = Importer.Import(CE->getArraySize());
6324 if (!ToArrSize && CE->getArraySize())
6325 return nullptr;
6326
6327 return new (Importer.getToContext()) CXXNewExpr(
6328 Importer.getToContext(),
6329 CE->isGlobalNew(),
6330 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00006331 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00006332 CE->doesUsualArrayDeleteWantSize(),
6333 PlacementArgs,
6334 Importer.Import(CE->getTypeIdParens()),
6335 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
6336 Importer.Import(CE->getSourceRange()),
6337 Importer.Import(CE->getDirectInitRange()));
6338}
6339
6340Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6341 QualType T = Importer.Import(E->getType());
6342 if (T.isNull())
6343 return nullptr;
6344
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006345 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006346 Importer.Import(E->getOperatorDelete()));
6347 if (!OperatorDeleteDecl && E->getOperatorDelete())
6348 return nullptr;
6349
6350 Expr *ToArg = Importer.Import(E->getArgument());
6351 if (!ToArg && E->getArgument())
6352 return nullptr;
6353
6354 return new (Importer.getToContext()) CXXDeleteExpr(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006355 T, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
6356 E->doesUsualArrayDeleteWantSize(), OperatorDeleteDecl, ToArg,
6357 Importer.Import(E->getBeginLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00006358}
6359
Sean Callanan59721b32015-04-28 18:41:46 +00006360Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6361 QualType T = Importer.Import(E->getType());
6362 if (T.isNull())
6363 return nullptr;
6364
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006365 auto *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00006366 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00006367 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00006368 return nullptr;
6369
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006370 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006371 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006372 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006373
6374 return CXXConstructExpr::Create(Importer.getToContext(), T,
6375 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00006376 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00006377 ToArgs, E->hadMultipleCandidates(),
6378 E->isListInitialization(),
6379 E->isStdInitListInitialization(),
6380 E->requiresZeroInitialization(),
6381 E->getConstructionKind(),
6382 Importer.Import(E->getParenOrBraceRange()));
6383}
6384
Aleksei Sidorina693b372016-09-28 10:16:56 +00006385Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
6386 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
6387 if (!SubExpr && EWC->getSubExpr())
6388 return nullptr;
6389
6390 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
6391 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
6392 if (ExprWithCleanups::CleanupObject Obj =
6393 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
6394 Objs[I] = Obj;
6395 else
6396 return nullptr;
6397
6398 return ExprWithCleanups::Create(Importer.getToContext(),
6399 SubExpr, EWC->cleanupsHaveSideEffects(),
6400 Objs);
6401}
6402
Sean Callanan8bca9962016-03-28 21:43:01 +00006403Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6404 QualType T = Importer.Import(E->getType());
6405 if (T.isNull())
6406 return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00006407
Sean Callanan8bca9962016-03-28 21:43:01 +00006408 Expr *ToFn = Importer.Import(E->getCallee());
6409 if (!ToFn)
6410 return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00006411
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006412 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006413 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006414 return nullptr;
6415
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006416 return new (Importer.getToContext()) CXXMemberCallExpr(
6417 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
6418 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00006419}
6420
6421Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6422 QualType T = Importer.Import(E->getType());
6423 if (T.isNull())
6424 return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00006425
Sean Callanan8bca9962016-03-28 21:43:01 +00006426 return new (Importer.getToContext())
6427 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
6428}
6429
6430Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
6431 QualType T = Importer.Import(E->getType());
6432 if (T.isNull())
6433 return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00006434
Sean Callanan8bca9962016-03-28 21:43:01 +00006435 return new (Importer.getToContext())
6436 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
6437}
6438
6439
Sean Callanan59721b32015-04-28 18:41:46 +00006440Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
6441 QualType T = Importer.Import(E->getType());
6442 if (T.isNull())
6443 return nullptr;
6444
6445 Expr *ToBase = Importer.Import(E->getBase());
6446 if (!ToBase && E->getBase())
6447 return nullptr;
6448
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006449 auto *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00006450 if (!ToMember && E->getMemberDecl())
6451 return nullptr;
6452
Peter Szecsief972522018-05-02 11:52:54 +00006453 auto *ToDecl =
6454 dyn_cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl()));
6455 if (!ToDecl && E->getFoundDecl().getDecl())
6456 return nullptr;
6457
6458 DeclAccessPair ToFoundDecl =
6459 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00006460
6461 DeclarationNameInfo ToMemberNameInfo(
6462 Importer.Import(E->getMemberNameInfo().getName()),
6463 Importer.Import(E->getMemberNameInfo().getLoc()));
6464
6465 if (E->hasExplicitTemplateArgs()) {
6466 return nullptr; // FIXME: handle template arguments
6467 }
6468
6469 return MemberExpr::Create(Importer.getToContext(), ToBase,
6470 E->isArrow(),
6471 Importer.Import(E->getOperatorLoc()),
6472 Importer.Import(E->getQualifierLoc()),
6473 Importer.Import(E->getTemplateKeywordLoc()),
6474 ToMember, ToFoundDecl, ToMemberNameInfo,
6475 nullptr, T, E->getValueKind(),
6476 E->getObjectKind());
6477}
6478
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006479Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr(
6480 CXXPseudoDestructorExpr *E) {
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006481 Expr *BaseE = Importer.Import(E->getBase());
6482 if (!BaseE)
6483 return nullptr;
6484
6485 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo());
6486 if (!ScopeInfo && E->getScopeTypeInfo())
6487 return nullptr;
6488
6489 PseudoDestructorTypeStorage Storage;
6490 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
6491 IdentifierInfo *ToII = Importer.Import(FromII);
6492 if (!ToII)
6493 return nullptr;
6494 Storage = PseudoDestructorTypeStorage(
6495 ToII, Importer.Import(E->getDestroyedTypeLoc()));
6496 } else {
6497 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo());
6498 if (!TI)
6499 return nullptr;
6500 Storage = PseudoDestructorTypeStorage(TI);
6501 }
6502
6503 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
6504 Importer.getToContext(), BaseE, E->isArrow(),
6505 Importer.Import(E->getOperatorLoc()),
6506 Importer.Import(E->getQualifierLoc()),
6507 ScopeInfo, Importer.Import(E->getColonColonLoc()),
6508 Importer.Import(E->getTildeLoc()), Storage);
6509}
6510
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006511Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
6512 CXXDependentScopeMemberExpr *E) {
6513 Expr *Base = nullptr;
6514 if (!E->isImplicitAccess()) {
6515 Base = Importer.Import(E->getBase());
6516 if (!Base)
6517 return nullptr;
6518 }
6519
6520 QualType BaseType = Importer.Import(E->getBaseType());
6521 if (BaseType.isNull())
6522 return nullptr;
6523
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006524 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006525 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006526 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6527 E->template_arguments(), ToTAInfo))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006528 return nullptr;
6529 ResInfo = &ToTAInfo;
6530 }
6531
6532 DeclarationName Name = Importer.Import(E->getMember());
6533 if (!E->getMember().isEmpty() && Name.isEmpty())
6534 return nullptr;
6535
6536 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc()));
6537 // Import additional name location/type info.
6538 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo);
6539 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope());
6540 if (!ToFQ && E->getFirstQualifierFoundInScope())
6541 return nullptr;
6542
6543 return CXXDependentScopeMemberExpr::Create(
6544 Importer.getToContext(), Base, BaseType, E->isArrow(),
6545 Importer.Import(E->getOperatorLoc()),
6546 Importer.Import(E->getQualifierLoc()),
6547 Importer.Import(E->getTemplateKeywordLoc()),
6548 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo);
6549}
6550
Peter Szecsice7f3182018-05-07 12:08:27 +00006551Expr *
6552ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
6553 DeclarationName Name = Importer.Import(E->getDeclName());
6554 if (!E->getDeclName().isEmpty() && Name.isEmpty())
6555 return nullptr;
6556
6557 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getExprLoc()));
6558 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6559
6560 TemplateArgumentListInfo ToTAInfo(Importer.Import(E->getLAngleLoc()),
6561 Importer.Import(E->getRAngleLoc()));
6562 TemplateArgumentListInfo *ResInfo = nullptr;
6563 if (E->hasExplicitTemplateArgs()) {
6564 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6565 return nullptr;
6566 ResInfo = &ToTAInfo;
6567 }
6568
6569 return DependentScopeDeclRefExpr::Create(
6570 Importer.getToContext(), Importer.Import(E->getQualifierLoc()),
6571 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo);
6572}
6573
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006574Expr *ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
6575 CXXUnresolvedConstructExpr *CE) {
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006576 unsigned NumArgs = CE->arg_size();
6577
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006578 SmallVector<Expr *, 8> ToArgs(NumArgs);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006579 if (ImportArrayChecked(CE->arg_begin(), CE->arg_end(), ToArgs.begin()))
6580 return nullptr;
6581
6582 return CXXUnresolvedConstructExpr::Create(
6583 Importer.getToContext(), Importer.Import(CE->getTypeSourceInfo()),
6584 Importer.Import(CE->getLParenLoc()), llvm::makeArrayRef(ToArgs),
6585 Importer.Import(CE->getRParenLoc()));
6586}
6587
6588Expr *ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006589 auto *NamingClass =
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006590 cast_or_null<CXXRecordDecl>(Importer.Import(E->getNamingClass()));
6591 if (E->getNamingClass() && !NamingClass)
6592 return nullptr;
6593
6594 DeclarationName Name = Importer.Import(E->getName());
6595 if (E->getName() && !Name)
6596 return nullptr;
6597
6598 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6599 // Import additional name location/type info.
6600 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6601
6602 UnresolvedSet<8> ToDecls;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006603 for (auto *D : E->decls()) {
6604 if (auto *To = cast_or_null<NamedDecl>(Importer.Import(D)))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006605 ToDecls.addDecl(To);
6606 else
6607 return nullptr;
6608 }
6609
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006610 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006611 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006612 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6613 E->template_arguments(), ToTAInfo))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006614 return nullptr;
6615 ResInfo = &ToTAInfo;
6616 }
6617
6618 if (ResInfo || E->getTemplateKeywordLoc().isValid())
6619 return UnresolvedLookupExpr::Create(
6620 Importer.getToContext(), NamingClass,
6621 Importer.Import(E->getQualifierLoc()),
6622 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, E->requiresADL(),
6623 ResInfo, ToDecls.begin(), ToDecls.end());
6624
6625 return UnresolvedLookupExpr::Create(
6626 Importer.getToContext(), NamingClass,
6627 Importer.Import(E->getQualifierLoc()), NameInfo, E->requiresADL(),
6628 E->isOverloaded(), ToDecls.begin(), ToDecls.end());
6629}
6630
Peter Szecsice7f3182018-05-07 12:08:27 +00006631Expr *ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
6632 DeclarationName Name = Importer.Import(E->getName());
6633 if (!E->getName().isEmpty() && Name.isEmpty())
6634 return nullptr;
6635 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6636 // Import additional name location/type info.
6637 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6638
6639 QualType BaseType = Importer.Import(E->getType());
6640 if (!E->getType().isNull() && BaseType.isNull())
6641 return nullptr;
6642
6643 UnresolvedSet<8> ToDecls;
6644 for (Decl *D : E->decls()) {
6645 if (NamedDecl *To = cast_or_null<NamedDecl>(Importer.Import(D)))
6646 ToDecls.addDecl(To);
6647 else
6648 return nullptr;
6649 }
6650
6651 TemplateArgumentListInfo ToTAInfo;
6652 TemplateArgumentListInfo *ResInfo = nullptr;
6653 if (E->hasExplicitTemplateArgs()) {
6654 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6655 return nullptr;
6656 ResInfo = &ToTAInfo;
6657 }
6658
6659 Expr *BaseE = E->isImplicitAccess() ? nullptr : Importer.Import(E->getBase());
6660 if (!BaseE && !E->isImplicitAccess() && E->getBase()) {
6661 return nullptr;
6662 }
6663
6664 return UnresolvedMemberExpr::Create(
6665 Importer.getToContext(), E->hasUnresolvedUsing(), BaseE, BaseType,
6666 E->isArrow(), Importer.Import(E->getOperatorLoc()),
6667 Importer.Import(E->getQualifierLoc()),
6668 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo,
6669 ToDecls.begin(), ToDecls.end());
6670}
6671
Sean Callanan59721b32015-04-28 18:41:46 +00006672Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
6673 QualType T = Importer.Import(E->getType());
6674 if (T.isNull())
6675 return nullptr;
6676
6677 Expr *ToCallee = Importer.Import(E->getCallee());
6678 if (!ToCallee && E->getCallee())
6679 return nullptr;
6680
6681 unsigned NumArgs = E->getNumArgs();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006682 SmallVector<Expr *, 2> ToArgs(NumArgs);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006683 if (ImportContainerChecked(E->arguments(), ToArgs))
6684 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006685
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006686 auto **ToArgs_Copied = new (Importer.getToContext()) Expr*[NumArgs];
Sean Callanan59721b32015-04-28 18:41:46 +00006687
6688 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
6689 ToArgs_Copied[ai] = ToArgs[ai];
6690
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006691 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
6692 return new (Importer.getToContext()) CXXOperatorCallExpr(
6693 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, T,
6694 OCE->getValueKind(), Importer.Import(OCE->getRParenLoc()),
6695 OCE->getFPFeatures());
6696 }
6697
Sean Callanan59721b32015-04-28 18:41:46 +00006698 return new (Importer.getToContext())
Fangrui Song6907ce22018-07-30 19:24:48 +00006699 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00006700 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00006701 Importer.Import(E->getRParenLoc()));
6702}
6703
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006704Optional<LambdaCapture>
6705ASTNodeImporter::ImportLambdaCapture(const LambdaCapture &From) {
6706 VarDecl *Var = nullptr;
6707 if (From.capturesVariable()) {
6708 Var = cast_or_null<VarDecl>(Importer.Import(From.getCapturedVar()));
6709 if (!Var)
6710 return None;
6711 }
6712
6713 return LambdaCapture(Importer.Import(From.getLocation()), From.isImplicit(),
6714 From.getCaptureKind(), Var,
6715 From.isPackExpansion()
6716 ? Importer.Import(From.getEllipsisLoc())
6717 : SourceLocation());
6718}
6719
6720Expr *ASTNodeImporter::VisitLambdaExpr(LambdaExpr *LE) {
6721 CXXRecordDecl *FromClass = LE->getLambdaClass();
6722 auto *ToClass = dyn_cast_or_null<CXXRecordDecl>(Importer.Import(FromClass));
6723 if (!ToClass)
6724 return nullptr;
6725
6726 // NOTE: lambda classes are created with BeingDefined flag set up.
6727 // It means that ImportDefinition doesn't work for them and we should fill it
6728 // manually.
6729 if (ToClass->isBeingDefined()) {
6730 for (auto FromField : FromClass->fields()) {
6731 auto *ToField = cast_or_null<FieldDecl>(Importer.Import(FromField));
6732 if (!ToField)
6733 return nullptr;
6734 }
6735 }
6736
6737 auto *ToCallOp = dyn_cast_or_null<CXXMethodDecl>(
6738 Importer.Import(LE->getCallOperator()));
6739 if (!ToCallOp)
6740 return nullptr;
6741
6742 ToClass->completeDefinition();
6743
6744 unsigned NumCaptures = LE->capture_size();
6745 SmallVector<LambdaCapture, 8> Captures;
6746 Captures.reserve(NumCaptures);
6747 for (const auto &FromCapture : LE->captures()) {
6748 if (auto ToCapture = ImportLambdaCapture(FromCapture))
6749 Captures.push_back(*ToCapture);
6750 else
6751 return nullptr;
6752 }
6753
6754 SmallVector<Expr *, 8> InitCaptures(NumCaptures);
6755 if (ImportContainerChecked(LE->capture_inits(), InitCaptures))
6756 return nullptr;
6757
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006758 return LambdaExpr::Create(
6759 Importer.getToContext(), ToClass,
6760 Importer.Import(LE->getIntroducerRange()), LE->getCaptureDefault(),
6761 Importer.Import(LE->getCaptureDefaultLoc()), Captures,
6762 LE->hasExplicitParameters(), LE->hasExplicitResultType(), InitCaptures,
6763 Importer.Import(LE->getEndLoc()), LE->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006764}
6765
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006766Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
6767 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00006768 if (T.isNull())
6769 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006770
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006771 SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006772 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006773 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006774
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006775 ASTContext &ToCtx = Importer.getToContext();
6776 InitListExpr *To = new (ToCtx) InitListExpr(
6777 ToCtx, Importer.Import(ILE->getLBraceLoc()),
6778 Exprs, Importer.Import(ILE->getLBraceLoc()));
6779 To->setType(T);
6780
6781 if (ILE->hasArrayFiller()) {
6782 Expr *Filler = Importer.Import(ILE->getArrayFiller());
6783 if (!Filler)
6784 return nullptr;
6785 To->setArrayFiller(Filler);
6786 }
6787
6788 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006789 auto *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006790 if (!ToFD)
6791 return nullptr;
6792 To->setInitializedFieldInUnion(ToFD);
6793 }
6794
6795 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006796 auto *ToSyntForm = cast_or_null<InitListExpr>(Importer.Import(SyntForm));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006797 if (!ToSyntForm)
6798 return nullptr;
6799 To->setSyntacticForm(ToSyntForm);
6800 }
6801
6802 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
6803 To->setValueDependent(ILE->isValueDependent());
6804 To->setInstantiationDependent(ILE->isInstantiationDependent());
6805
6806 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00006807}
6808
Gabor Marton07b01ff2018-06-29 12:17:34 +00006809Expr *ASTNodeImporter::VisitCXXStdInitializerListExpr(
6810 CXXStdInitializerListExpr *E) {
6811 QualType T = Importer.Import(E->getType());
6812 if (T.isNull())
6813 return nullptr;
6814
6815 Expr *SE = Importer.Import(E->getSubExpr());
6816 if (!SE)
6817 return nullptr;
6818
6819 return new (Importer.getToContext()) CXXStdInitializerListExpr(T, SE);
6820}
6821
Balazs Keri95baa842018-07-25 10:21:06 +00006822Expr *ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
6823 CXXInheritedCtorInitExpr *E) {
6824 QualType T = Importer.Import(E->getType());
6825 if (T.isNull())
6826 return nullptr;
6827
6828 auto *Ctor = cast_or_null<CXXConstructorDecl>(Importer.Import(
6829 E->getConstructor()));
6830 if (!Ctor)
6831 return nullptr;
6832
6833 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
6834 Importer.Import(E->getLocation()), T, Ctor,
6835 E->constructsVBase(), E->inheritedFromVBase());
6836}
6837
Richard Smith30e304e2016-12-14 00:03:17 +00006838Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
6839 QualType ToType = Importer.Import(E->getType());
6840 if (ToType.isNull())
6841 return nullptr;
6842
6843 Expr *ToCommon = Importer.Import(E->getCommonExpr());
6844 if (!ToCommon && E->getCommonExpr())
6845 return nullptr;
6846
6847 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
6848 if (!ToSubExpr && E->getSubExpr())
6849 return nullptr;
6850
6851 return new (Importer.getToContext())
6852 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
6853}
6854
6855Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
6856 QualType ToType = Importer.Import(E->getType());
6857 if (ToType.isNull())
6858 return nullptr;
6859 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
6860}
6861
Sean Callanandd2c1742016-05-16 20:48:03 +00006862Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006863 auto *ToField = dyn_cast_or_null<FieldDecl>(Importer.Import(DIE->getField()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006864 if (!ToField && DIE->getField())
6865 return nullptr;
6866
6867 return CXXDefaultInitExpr::Create(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006868 Importer.getToContext(), Importer.Import(DIE->getBeginLoc()), ToField);
Sean Callanandd2c1742016-05-16 20:48:03 +00006869}
6870
6871Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
6872 QualType ToType = Importer.Import(E->getType());
6873 if (ToType.isNull() && !E->getType().isNull())
6874 return nullptr;
6875 ExprValueKind VK = E->getValueKind();
6876 CastKind CK = E->getCastKind();
6877 Expr *ToOp = Importer.Import(E->getSubExpr());
6878 if (!ToOp && E->getSubExpr())
6879 return nullptr;
6880 CXXCastPath BasePath;
6881 if (ImportCastPath(E, BasePath))
6882 return nullptr;
6883 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6884 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6885 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6886 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
Fangrui Song6907ce22018-07-30 19:24:48 +00006887
Sean Callanandd2c1742016-05-16 20:48:03 +00006888 if (isa<CXXStaticCastExpr>(E)) {
6889 return CXXStaticCastExpr::Create(
Fangrui Song6907ce22018-07-30 19:24:48 +00006890 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
Sean Callanandd2c1742016-05-16 20:48:03 +00006891 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6892 } else if (isa<CXXDynamicCastExpr>(E)) {
6893 return CXXDynamicCastExpr::Create(
Fangrui Song6907ce22018-07-30 19:24:48 +00006894 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
Sean Callanandd2c1742016-05-16 20:48:03 +00006895 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6896 } else if (isa<CXXReinterpretCastExpr>(E)) {
6897 return CXXReinterpretCastExpr::Create(
Fangrui Song6907ce22018-07-30 19:24:48 +00006898 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
Sean Callanandd2c1742016-05-16 20:48:03 +00006899 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00006900 } else if (isa<CXXConstCastExpr>(E)) {
6901 return CXXConstCastExpr::Create(Importer.getToContext(), ToType, VK, ToOp,
6902 ToWritten, ToOperatorLoc, ToRParenLoc,
6903 ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00006904 } else {
6905 return nullptr;
6906 }
6907}
6908
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006909Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
6910 SubstNonTypeTemplateParmExpr *E) {
6911 QualType T = Importer.Import(E->getType());
6912 if (T.isNull())
6913 return nullptr;
6914
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006915 auto *Param = cast_or_null<NonTypeTemplateParmDecl>(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006916 Importer.Import(E->getParameter()));
6917 if (!Param)
6918 return nullptr;
6919
6920 Expr *Replacement = Importer.Import(E->getReplacement());
6921 if (!Replacement)
6922 return nullptr;
6923
6924 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
6925 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
6926 Replacement);
6927}
6928
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00006929Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
6930 QualType ToType = Importer.Import(E->getType());
6931 if (ToType.isNull())
6932 return nullptr;
6933
6934 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
6935 if (ImportContainerChecked(E->getArgs(), ToArgs))
6936 return nullptr;
6937
6938 // According to Sema::BuildTypeTrait(), if E is value-dependent,
6939 // Value is always false.
6940 bool ToValue = false;
6941 if (!E->isValueDependent())
6942 ToValue = E->getValue();
6943
6944 return TypeTraitExpr::Create(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006945 Importer.getToContext(), ToType, Importer.Import(E->getBeginLoc()),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006946 E->getTrait(), ToArgs, Importer.Import(E->getEndLoc()), ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00006947}
6948
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006949Expr *ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
6950 QualType ToType = Importer.Import(E->getType());
6951 if (ToType.isNull())
6952 return nullptr;
6953
6954 if (E->isTypeOperand()) {
6955 TypeSourceInfo *TSI = Importer.Import(E->getTypeOperandSourceInfo());
6956 if (!TSI)
6957 return nullptr;
6958
6959 return new (Importer.getToContext())
6960 CXXTypeidExpr(ToType, TSI, Importer.Import(E->getSourceRange()));
6961 }
6962
6963 Expr *Op = Importer.Import(E->getExprOperand());
6964 if (!Op)
6965 return nullptr;
6966
6967 return new (Importer.getToContext())
6968 CXXTypeidExpr(ToType, Op, Importer.Import(E->getSourceRange()));
6969}
6970
Lang Hames19e07e12017-06-20 21:06:00 +00006971void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
6972 CXXMethodDecl *FromMethod) {
6973 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
6974 ToMethod->addOverriddenMethod(
6975 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
6976 FromOverriddenMethod))));
6977}
6978
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006979ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006980 ASTContext &FromContext, FileManager &FromFileManager,
6981 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006982 : ToContext(ToContext), FromContext(FromContext),
6983 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
6984 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00006985 ImportedDecls[FromContext.getTranslationUnitDecl()]
6986 = ToContext.getTranslationUnitDecl();
6987}
6988
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006989ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006990
6991QualType ASTImporter::Import(QualType FromT) {
6992 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006993 return {};
John McCall424cec92011-01-19 06:33:43 +00006994
6995 const Type *fromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00006996
6997 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006998 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6999 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007000 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00007001 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00007002
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007003 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00007004 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00007005 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00007006 if (ToT.isNull())
7007 return ToT;
Fangrui Song6907ce22018-07-30 19:24:48 +00007008
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007009 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00007010 ImportedTypes[fromTy] = ToT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007011
John McCall424cec92011-01-19 06:33:43 +00007012 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00007013}
7014
Douglas Gregor62d311f2010-02-09 19:21:46 +00007015TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007016 if (!FromTSI)
7017 return FromTSI;
7018
7019 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00007020 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007021 QualType T = Import(FromTSI->getType());
7022 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00007023 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007024
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007025 return ToContext.getTrivialTypeSourceInfo(
7026 T, Import(FromTSI->getTypeLoc().getBeginLoc()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00007027}
7028
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007029Attr *ASTImporter::Import(const Attr *FromAttr) {
7030 Attr *ToAttr = FromAttr->clone(ToContext);
7031 ToAttr->setRange(Import(FromAttr->getRange()));
7032 return ToAttr;
7033}
7034
Sean Callanan59721b32015-04-28 18:41:46 +00007035Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
7036 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
7037 if (Pos != ImportedDecls.end()) {
7038 Decl *ToD = Pos->second;
Gabor Marton26f72a92018-07-12 09:42:05 +00007039 // FIXME: move this call to ImportDeclParts().
Sean Callanan59721b32015-04-28 18:41:46 +00007040 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
7041 return ToD;
7042 } else {
7043 return nullptr;
7044 }
7045}
7046
Douglas Gregor62d311f2010-02-09 19:21:46 +00007047Decl *ASTImporter::Import(Decl *FromD) {
7048 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007049 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007050
Douglas Gregord451ea92011-07-29 23:31:30 +00007051 ASTNodeImporter Importer(*this);
7052
Gabor Marton26f72a92018-07-12 09:42:05 +00007053 // Check whether we've already imported this declaration.
7054 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7055 if (ToD) {
7056 // If FromD has some updated flags after last import, apply it
7057 updateFlags(FromD, ToD);
Douglas Gregord451ea92011-07-29 23:31:30 +00007058 return ToD;
7059 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007060
7061 // Import the type.
7062 ToD = Importer.Visit(FromD);
Douglas Gregor62d311f2010-02-09 19:21:46 +00007063 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00007064 return nullptr;
7065
Gabor Marton26f72a92018-07-12 09:42:05 +00007066 // Notify subclasses.
7067 Imported(FromD, ToD);
7068
Douglas Gregor62d311f2010-02-09 19:21:46 +00007069 return ToD;
7070}
7071
7072DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
7073 if (!FromDC)
7074 return FromDC;
7075
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007076 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00007077 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00007078 return nullptr;
7079
Fangrui Song6907ce22018-07-30 19:24:48 +00007080 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00007081 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007082 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
7083 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007084 if (ToRecord->isCompleteDefinition()) {
7085 // Do nothing.
7086 } else if (FromRecord->isCompleteDefinition()) {
7087 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
7088 ASTNodeImporter::IDK_Basic);
7089 } else {
7090 CompleteDecl(ToRecord);
7091 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007092 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
7093 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007094 if (ToEnum->isCompleteDefinition()) {
7095 // Do nothing.
7096 } else if (FromEnum->isCompleteDefinition()) {
7097 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
7098 ASTNodeImporter::IDK_Basic);
7099 } else {
7100 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00007101 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007102 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
7103 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007104 if (ToClass->getDefinition()) {
7105 // Do nothing.
7106 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
7107 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
7108 ASTNodeImporter::IDK_Basic);
7109 } else {
7110 CompleteDecl(ToClass);
7111 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007112 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
7113 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007114 if (ToProto->getDefinition()) {
7115 // Do nothing.
7116 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
7117 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
7118 ASTNodeImporter::IDK_Basic);
7119 } else {
7120 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00007121 }
Douglas Gregor95d82832012-01-24 18:36:04 +00007122 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007123
Douglas Gregor95d82832012-01-24 18:36:04 +00007124 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007125}
7126
7127Expr *ASTImporter::Import(Expr *FromE) {
7128 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00007129 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007130
7131 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
7132}
7133
7134Stmt *ASTImporter::Import(Stmt *FromS) {
7135 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007136 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007137
Fangrui Song6907ce22018-07-30 19:24:48 +00007138 // Check whether we've already imported this declaration.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007139 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7140 if (Pos != ImportedStmts.end())
7141 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00007142
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007143 // Import the type
7144 ASTNodeImporter Importer(*this);
7145 Stmt *ToS = Importer.Visit(FromS);
7146 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00007147 return nullptr;
7148
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007149 // Record the imported declaration.
7150 ImportedStmts[FromS] = ToS;
7151 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007152}
7153
7154NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
7155 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00007156 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007157
Douglas Gregor90ebf252011-04-27 16:48:40 +00007158 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
7159
7160 switch (FromNNS->getKind()) {
7161 case NestedNameSpecifier::Identifier:
7162 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
7163 return NestedNameSpecifier::Create(ToContext, prefix, II);
7164 }
Craig Topper36250ad2014-05-12 05:36:57 +00007165 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007166
7167 case NestedNameSpecifier::Namespace:
Fangrui Song6907ce22018-07-30 19:24:48 +00007168 if (auto *NS =
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007169 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007170 return NestedNameSpecifier::Create(ToContext, prefix, NS);
7171 }
Craig Topper36250ad2014-05-12 05:36:57 +00007172 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007173
7174 case NestedNameSpecifier::NamespaceAlias:
Fangrui Song6907ce22018-07-30 19:24:48 +00007175 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007176 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00007177 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
7178 }
Craig Topper36250ad2014-05-12 05:36:57 +00007179 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007180
7181 case NestedNameSpecifier::Global:
7182 return NestedNameSpecifier::GlobalSpecifier(ToContext);
7183
Nikola Smiljanic67860242014-09-26 00:28:20 +00007184 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007185 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007186 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00007187 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
7188 }
7189 return nullptr;
7190
Douglas Gregor90ebf252011-04-27 16:48:40 +00007191 case NestedNameSpecifier::TypeSpec:
7192 case NestedNameSpecifier::TypeSpecWithTemplate: {
7193 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
7194 if (!T.isNull()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007195 bool bTemplate = FromNNS->getKind() ==
Douglas Gregor90ebf252011-04-27 16:48:40 +00007196 NestedNameSpecifier::TypeSpecWithTemplate;
Fangrui Song6907ce22018-07-30 19:24:48 +00007197 return NestedNameSpecifier::Create(ToContext, prefix,
Douglas Gregor90ebf252011-04-27 16:48:40 +00007198 bTemplate, T.getTypePtr());
7199 }
7200 }
Craig Topper36250ad2014-05-12 05:36:57 +00007201 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00007202 }
7203
7204 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00007205}
7206
Douglas Gregor14454802011-02-25 02:25:35 +00007207NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007208 // Copied from NestedNameSpecifier mostly.
7209 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
7210 NestedNameSpecifierLoc NNS = FromNNS;
7211
7212 // Push each of the nested-name-specifiers's onto a stack for
7213 // serialization in reverse order.
7214 while (NNS) {
7215 NestedNames.push_back(NNS);
7216 NNS = NNS.getPrefix();
7217 }
7218
7219 NestedNameSpecifierLocBuilder Builder;
7220
7221 while (!NestedNames.empty()) {
7222 NNS = NestedNames.pop_back_val();
7223 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
7224 if (!Spec)
7225 return NestedNameSpecifierLoc();
7226
7227 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
7228 switch (Kind) {
7229 case NestedNameSpecifier::Identifier:
7230 Builder.Extend(getToContext(),
7231 Spec->getAsIdentifier(),
7232 Import(NNS.getLocalBeginLoc()),
7233 Import(NNS.getLocalEndLoc()));
7234 break;
7235
7236 case NestedNameSpecifier::Namespace:
7237 Builder.Extend(getToContext(),
7238 Spec->getAsNamespace(),
7239 Import(NNS.getLocalBeginLoc()),
7240 Import(NNS.getLocalEndLoc()));
7241 break;
7242
7243 case NestedNameSpecifier::NamespaceAlias:
7244 Builder.Extend(getToContext(),
7245 Spec->getAsNamespaceAlias(),
7246 Import(NNS.getLocalBeginLoc()),
7247 Import(NNS.getLocalEndLoc()));
7248 break;
7249
7250 case NestedNameSpecifier::TypeSpec:
7251 case NestedNameSpecifier::TypeSpecWithTemplate: {
7252 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
7253 QualType(Spec->getAsType(), 0));
7254 Builder.Extend(getToContext(),
7255 Import(NNS.getLocalBeginLoc()),
7256 TSI->getTypeLoc(),
7257 Import(NNS.getLocalEndLoc()));
7258 break;
7259 }
7260
7261 case NestedNameSpecifier::Global:
7262 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
7263 break;
7264
7265 case NestedNameSpecifier::Super: {
7266 SourceRange ToRange = Import(NNS.getSourceRange());
7267 Builder.MakeSuper(getToContext(),
7268 Spec->getAsRecordDecl(),
7269 ToRange.getBegin(),
7270 ToRange.getEnd());
7271 }
7272 }
7273 }
7274
7275 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00007276}
7277
Douglas Gregore2e50d332010-12-01 01:36:18 +00007278TemplateName ASTImporter::Import(TemplateName From) {
7279 switch (From.getKind()) {
7280 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007281 if (auto *ToTemplate =
7282 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007283 return TemplateName(ToTemplate);
Fangrui Song6907ce22018-07-30 19:24:48 +00007284
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007285 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007286
Douglas Gregore2e50d332010-12-01 01:36:18 +00007287 case TemplateName::OverloadedTemplate: {
7288 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
7289 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007290 for (auto *I : *FromStorage) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007291 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00007292 ToTemplates.addDecl(To);
7293 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007294 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007295 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007296 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00007297 ToTemplates.end());
7298 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007299
Douglas Gregore2e50d332010-12-01 01:36:18 +00007300 case TemplateName::QualifiedTemplate: {
7301 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
7302 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
7303 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007304 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007305
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007306 if (auto *ToTemplate =
7307 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Fangrui Song6907ce22018-07-30 19:24:48 +00007308 return ToContext.getQualifiedTemplateName(Qualifier,
7309 QTN->hasTemplateKeyword(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00007310 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007311
7312 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007313 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007314
Douglas Gregore2e50d332010-12-01 01:36:18 +00007315 case TemplateName::DependentTemplate: {
7316 DependentTemplateName *DTN = From.getAsDependentTemplateName();
7317 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
7318 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007319 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007320
Douglas Gregore2e50d332010-12-01 01:36:18 +00007321 if (DTN->isIdentifier()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007322 return ToContext.getDependentTemplateName(Qualifier,
Douglas Gregore2e50d332010-12-01 01:36:18 +00007323 Import(DTN->getIdentifier()));
7324 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007325
Douglas Gregore2e50d332010-12-01 01:36:18 +00007326 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
7327 }
John McCalld9dfe3a2011-06-30 08:33:18 +00007328
7329 case TemplateName::SubstTemplateTemplateParm: {
7330 SubstTemplateTemplateParmStorage *subst
7331 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007332 auto *param =
7333 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00007334 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007335 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00007336
7337 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007338 if (replacement.isNull())
7339 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007340
John McCalld9dfe3a2011-06-30 08:33:18 +00007341 return ToContext.getSubstTemplateTemplateParm(param, replacement);
7342 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007343
Douglas Gregor5590be02011-01-15 06:45:20 +00007344 case TemplateName::SubstTemplateTemplateParmPack: {
7345 SubstTemplateTemplateParmPackStorage *SubstPack
7346 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007347 auto *Param =
7348 cast_or_null<TemplateTemplateParmDecl>(
7349 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00007350 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007351 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007352
Douglas Gregor5590be02011-01-15 06:45:20 +00007353 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00007354 TemplateArgument ArgPack
Douglas Gregor5590be02011-01-15 06:45:20 +00007355 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
7356 if (ArgPack.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007357 return {};
Fangrui Song6907ce22018-07-30 19:24:48 +00007358
Douglas Gregor5590be02011-01-15 06:45:20 +00007359 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
7360 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00007361 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007362
Douglas Gregore2e50d332010-12-01 01:36:18 +00007363 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00007364}
7365
Douglas Gregor62d311f2010-02-09 19:21:46 +00007366SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
7367 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007368 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00007369
Douglas Gregor811663e2010-02-10 00:15:17 +00007370 SourceManager &FromSM = FromContext.getSourceManager();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007371
Douglas Gregor811663e2010-02-10 00:15:17 +00007372 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Sean Callanan238d8972014-12-10 01:26:39 +00007373 FileID ToFileID = Import(Decomposed.first);
7374 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007375 return {};
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007376 SourceManager &ToSM = ToContext.getSourceManager();
7377 return ToSM.getComposedLoc(ToFileID, Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00007378}
7379
7380SourceRange ASTImporter::Import(SourceRange FromRange) {
7381 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
7382}
7383
Douglas Gregor811663e2010-02-10 00:15:17 +00007384FileID ASTImporter::Import(FileID FromID) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007385 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00007386 if (Pos != ImportedFileIDs.end())
7387 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007388
Douglas Gregor811663e2010-02-10 00:15:17 +00007389 SourceManager &FromSM = FromContext.getSourceManager();
7390 SourceManager &ToSM = ToContext.getSourceManager();
7391 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007392
7393 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00007394 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007395 if (FromSLoc.isExpansion()) {
7396 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
7397 SourceLocation ToSpLoc = Import(FromEx.getSpellingLoc());
7398 SourceLocation ToExLocS = Import(FromEx.getExpansionLocStart());
7399 unsigned TokenLen = FromSM.getFileIDSize(FromID);
7400 SourceLocation MLoc;
7401 if (FromEx.isMacroArgExpansion()) {
7402 MLoc = ToSM.createMacroArgExpansionLoc(ToSpLoc, ToExLocS, TokenLen);
7403 } else {
7404 SourceLocation ToExLocE = Import(FromEx.getExpansionLocEnd());
7405 MLoc = ToSM.createExpansionLoc(ToSpLoc, ToExLocS, ToExLocE, TokenLen,
7406 FromEx.isExpansionTokenRange());
7407 }
7408 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00007409 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00007410 // Include location of this file.
7411 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
7412
7413 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
7414 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
7415 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
7416 // disk again
7417 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
7418 // than mmap the files several times.
7419 const FileEntry *Entry =
7420 ToFileManager.getFile(Cache->OrigEntry->getName());
7421 if (!Entry)
7422 return {};
7423 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
7424 FromSLoc.getFile().getFileCharacteristic());
7425 } else {
7426 // FIXME: We want to re-use the existing MemoryBuffer!
7427 const llvm::MemoryBuffer *FromBuf =
7428 Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
7429 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
7430 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
7431 FromBuf->getBufferIdentifier());
7432 ToID = ToSM.createFileID(std::move(ToBuf),
7433 FromSLoc.getFile().getFileCharacteristic());
7434 }
Douglas Gregor811663e2010-02-10 00:15:17 +00007435 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007436
Sebastian Redl99219f12010-09-30 01:03:06 +00007437 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00007438 return ToID;
7439}
7440
Sean Callanandd2c1742016-05-16 20:48:03 +00007441CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
7442 Expr *ToExpr = Import(From->getInit());
7443 if (!ToExpr && From->getInit())
7444 return nullptr;
7445
7446 if (From->isBaseInitializer()) {
7447 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7448 if (!ToTInfo && From->getTypeSourceInfo())
7449 return nullptr;
7450
7451 return new (ToContext) CXXCtorInitializer(
7452 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
7453 ToExpr, Import(From->getRParenLoc()),
7454 From->isPackExpansion() ? Import(From->getEllipsisLoc())
7455 : SourceLocation());
7456 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007457 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007458 if (!ToField && From->getMember())
7459 return nullptr;
7460
7461 return new (ToContext) CXXCtorInitializer(
7462 ToContext, ToField, Import(From->getMemberLocation()),
7463 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7464 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007465 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00007466 Import(From->getIndirectMember()));
7467 if (!ToIField && From->getIndirectMember())
7468 return nullptr;
7469
7470 return new (ToContext) CXXCtorInitializer(
7471 ToContext, ToIField, Import(From->getMemberLocation()),
7472 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7473 } else if (From->isDelegatingInitializer()) {
7474 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7475 if (!ToTInfo && From->getTypeSourceInfo())
7476 return nullptr;
7477
7478 return new (ToContext)
7479 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
7480 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007481 } else {
7482 return nullptr;
7483 }
7484}
7485
Aleksei Sidorina693b372016-09-28 10:16:56 +00007486CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
7487 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
7488 if (Pos != ImportedCXXBaseSpecifiers.end())
7489 return Pos->second;
7490
7491 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
7492 Import(BaseSpec->getSourceRange()),
7493 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
7494 BaseSpec->getAccessSpecifierAsWritten(),
7495 Import(BaseSpec->getTypeSourceInfo()),
7496 Import(BaseSpec->getEllipsisLoc()));
7497 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
7498 return Imported;
7499}
7500
Douglas Gregor0a791672011-01-18 03:11:38 +00007501void ASTImporter::ImportDefinition(Decl *From) {
7502 Decl *To = Import(From);
7503 if (!To)
7504 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00007505
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007506 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00007507 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00007508
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007509 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00007510 if (!ToRecord->getDefinition()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007511 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00007512 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007513 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00007514 }
Sean Callanan53a6bff2011-07-19 22:38:25 +00007515 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007516
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007517 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00007518 if (!ToEnum->getDefinition()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00007519 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007520 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00007521 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00007522 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007523 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007524
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007525 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007526 if (!ToIFace->getDefinition()) {
7527 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007528 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007529 return;
7530 }
7531 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007532
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007533 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007534 if (!ToProto->getDefinition()) {
7535 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007536 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007537 return;
7538 }
7539 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007540
Douglas Gregor0a791672011-01-18 03:11:38 +00007541 Importer.ImportDeclContext(FromDC, true);
7542 }
7543}
7544
Douglas Gregor96e578d2010-02-05 17:54:41 +00007545DeclarationName ASTImporter::Import(DeclarationName FromName) {
7546 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007547 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007548
7549 switch (FromName.getNameKind()) {
7550 case DeclarationName::Identifier:
7551 return Import(FromName.getAsIdentifierInfo());
7552
7553 case DeclarationName::ObjCZeroArgSelector:
7554 case DeclarationName::ObjCOneArgSelector:
7555 case DeclarationName::ObjCMultiArgSelector:
7556 return Import(FromName.getObjCSelector());
7557
7558 case DeclarationName::CXXConstructorName: {
7559 QualType T = Import(FromName.getCXXNameType());
7560 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007561 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007562
7563 return ToContext.DeclarationNames.getCXXConstructorName(
7564 ToContext.getCanonicalType(T));
7565 }
7566
7567 case DeclarationName::CXXDestructorName: {
7568 QualType T = Import(FromName.getCXXNameType());
7569 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007570 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007571
7572 return ToContext.DeclarationNames.getCXXDestructorName(
7573 ToContext.getCanonicalType(T));
7574 }
7575
Richard Smith35845152017-02-07 01:37:30 +00007576 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007577 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00007578 Import(FromName.getCXXDeductionGuideTemplate()));
7579 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007580 return {};
Richard Smith35845152017-02-07 01:37:30 +00007581 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
7582 }
7583
Douglas Gregor96e578d2010-02-05 17:54:41 +00007584 case DeclarationName::CXXConversionFunctionName: {
7585 QualType T = Import(FromName.getCXXNameType());
7586 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007587 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007588
7589 return ToContext.DeclarationNames.getCXXConversionFunctionName(
7590 ToContext.getCanonicalType(T));
7591 }
7592
7593 case DeclarationName::CXXOperatorName:
7594 return ToContext.DeclarationNames.getCXXOperatorName(
7595 FromName.getCXXOverloadedOperator());
7596
7597 case DeclarationName::CXXLiteralOperatorName:
7598 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
7599 Import(FromName.getCXXLiteralIdentifier()));
7600
7601 case DeclarationName::CXXUsingDirective:
7602 // FIXME: STATICS!
7603 return DeclarationName::getUsingDirectiveName();
7604 }
7605
David Blaikiee4d798f2012-01-20 21:50:17 +00007606 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00007607}
7608
Douglas Gregore2e50d332010-12-01 01:36:18 +00007609IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007610 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00007611 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007612
Sean Callananf94ef1d2016-05-14 06:11:19 +00007613 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
7614
7615 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
7616 ToId->setBuiltinID(FromId->getBuiltinID());
7617
7618 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007619}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007620
Douglas Gregor43f54792010-02-17 02:12:47 +00007621Selector ASTImporter::Import(Selector FromSel) {
7622 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007623 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00007624
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007625 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00007626 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
7627 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
7628 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
7629 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
7630}
7631
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007632DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
7633 DeclContext *DC,
7634 unsigned IDNS,
7635 NamedDecl **Decls,
7636 unsigned NumDecls) {
7637 return Name;
7638}
7639
7640DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007641 if (LastDiagFromFrom)
7642 ToContext.getDiagnostics().notePriorDiagnosticFrom(
7643 FromContext.getDiagnostics());
7644 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007645 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007646}
7647
7648DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007649 if (!LastDiagFromFrom)
7650 FromContext.getDiagnostics().notePriorDiagnosticFrom(
7651 ToContext.getDiagnostics());
7652 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007653 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007654}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007655
Douglas Gregor2e15c842012-02-01 21:00:38 +00007656void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007657 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007658 if (!ID->getDefinition())
7659 ID->startDefinition();
7660 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007661 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007662 if (!PD->getDefinition())
7663 PD->startDefinition();
7664 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007665 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007666 if (!TD->getDefinition() && !TD->isBeingDefined()) {
7667 TD->startDefinition();
7668 TD->setCompleteDefinition(true);
7669 }
7670 }
7671 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007672 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00007673 }
7674}
7675
Gabor Marton26f72a92018-07-12 09:42:05 +00007676Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
7677 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
7678 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
7679 "Try to import an already imported Decl");
7680 if (Pos != ImportedDecls.end())
7681 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007682 ImportedDecls[From] = To;
7683 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00007684}
Douglas Gregorb4964f72010-02-15 23:54:17 +00007685
Douglas Gregordd6006f2012-07-17 21:16:27 +00007686bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
7687 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00007688 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00007689 = ImportedTypes.find(From.getTypePtr());
7690 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
7691 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00007692
Douglas Gregordd6006f2012-07-17 21:16:27 +00007693 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00007694 getStructuralEquivalenceKind(*this), false,
7695 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00007696 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00007697}