blob: 22fb67478c9692139dde8020f9b683d20dffe903 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Douglas Gregor96e578d2010-02-05 17:54:41 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ASTImporter class which imports AST nodes from one
10// context into another context.
11//
12//===----------------------------------------------------------------------===//
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000013
Douglas Gregor96e578d2010-02-05 17:54:41 +000014#include "clang/AST/ASTImporter.h"
Ilya Biryukovabc744d2019-07-18 15:43:26 +000015#include "clang/AST/ASTImporterSharedState.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"
Raphael Isemannba7bde62019-10-30 14:50:35 +010047#include "clang/Basic/Builtins.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000048#include "clang/Basic/ExceptionSpecificationType.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000049#include "clang/Basic/FileManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000050#include "clang/Basic/IdentifierTable.h"
51#include "clang/Basic/LLVM.h"
52#include "clang/Basic/LangOptions.h"
53#include "clang/Basic/SourceLocation.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000054#include "clang/Basic/SourceManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000055#include "clang/Basic/Specifiers.h"
56#include "llvm/ADT/APSInt.h"
57#include "llvm/ADT/ArrayRef.h"
58#include "llvm/ADT/DenseMap.h"
59#include "llvm/ADT/None.h"
60#include "llvm/ADT/Optional.h"
Balazs Kerid2c57612019-07-18 15:23:10 +000061#include "llvm/ADT/ScopeExit.h"
Ilya Biryukovabc744d2019-07-18 15:43:26 +000062#include "llvm/ADT/STLExtras.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000063#include "llvm/ADT/SmallVector.h"
64#include "llvm/Support/Casting.h"
65#include "llvm/Support/ErrorHandling.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000066#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000067#include <algorithm>
68#include <cassert>
69#include <cstddef>
70#include <memory>
71#include <type_traits>
72#include <utility>
Douglas Gregor96e578d2010-02-05 17:54:41 +000073
Douglas Gregor3c2404b2011-11-03 18:07:07 +000074namespace clang {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000075
Balazs Keri3b30d652018-10-19 13:32:20 +000076 using llvm::make_error;
77 using llvm::Error;
78 using llvm::Expected;
79 using ExpectedType = llvm::Expected<QualType>;
80 using ExpectedStmt = llvm::Expected<Stmt *>;
81 using ExpectedExpr = llvm::Expected<Expr *>;
82 using ExpectedDecl = llvm::Expected<Decl *>;
83 using ExpectedSLoc = llvm::Expected<SourceLocation>;
Gabor Martonf035b752019-08-27 11:36:10 +000084 using ExpectedName = llvm::Expected<DeclarationName>;
Balazs Keri2544b4b2018-08-08 09:40:57 +000085
Balazs Keri3b30d652018-10-19 13:32:20 +000086 std::string ImportError::toString() const {
87 // FIXME: Improve error texts.
88 switch (Error) {
89 case NameConflict:
90 return "NameConflict";
91 case UnsupportedConstruct:
92 return "UnsupportedConstruct";
93 case Unknown:
94 return "Unknown error";
Balazs Keri2544b4b2018-08-08 09:40:57 +000095 }
Balazs Keri2a13d662018-10-19 15:16:51 +000096 llvm_unreachable("Invalid error code.");
97 return "Invalid error code.";
Balazs Keri2544b4b2018-08-08 09:40:57 +000098 }
99
Balazs Keri3b30d652018-10-19 13:32:20 +0000100 void ImportError::log(raw_ostream &OS) const {
101 OS << toString();
102 }
103
104 std::error_code ImportError::convertToErrorCode() const {
105 llvm_unreachable("Function not implemented.");
106 }
107
108 char ImportError::ID;
109
Gabor Marton5254e642018-06-27 13:32:50 +0000110 template <class T>
Balazs Keri3b30d652018-10-19 13:32:20 +0000111 SmallVector<Decl *, 2>
Gabor Marton5254e642018-06-27 13:32:50 +0000112 getCanonicalForwardRedeclChain(Redeclarable<T>* D) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000113 SmallVector<Decl *, 2> Redecls;
Gabor Marton5254e642018-06-27 13:32:50 +0000114 for (auto *R : D->getFirstDecl()->redecls()) {
115 if (R != D->getFirstDecl())
116 Redecls.push_back(R);
117 }
118 Redecls.push_back(D->getFirstDecl());
119 std::reverse(Redecls.begin(), Redecls.end());
120 return Redecls;
121 }
122
123 SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +0000124 if (auto *FD = dyn_cast<FunctionDecl>(D))
125 return getCanonicalForwardRedeclChain<FunctionDecl>(FD);
126 if (auto *VD = dyn_cast<VarDecl>(D))
127 return getCanonicalForwardRedeclChain<VarDecl>(VD);
Gabor Marton7df342a2018-12-17 12:42:12 +0000128 if (auto *TD = dyn_cast<TagDecl>(D))
129 return getCanonicalForwardRedeclChain<TagDecl>(TD);
Gabor Martonac3a5d62018-09-17 12:04:52 +0000130 llvm_unreachable("Bad declaration kind");
Gabor Marton5254e642018-06-27 13:32:50 +0000131 }
132
Gabor Marton26f72a92018-07-12 09:42:05 +0000133 void updateFlags(const Decl *From, Decl *To) {
134 // Check if some flags or attrs are new in 'From' and copy into 'To'.
135 // FIXME: Other flags or attrs?
136 if (From->isUsed(false) && !To->isUsed(false))
137 To->setIsUsed();
138 }
139
Balazs Keri3b30d652018-10-19 13:32:20 +0000140 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>,
141 public DeclVisitor<ASTNodeImporter, ExpectedDecl>,
142 public StmtVisitor<ASTNodeImporter, ExpectedStmt> {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000143 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000144
Balazs Keri3b30d652018-10-19 13:32:20 +0000145 // Use this instead of Importer.importInto .
146 template <typename ImportT>
147 LLVM_NODISCARD Error importInto(ImportT &To, const ImportT &From) {
148 return Importer.importInto(To, From);
149 }
150
151 // Use this to import pointers of specific type.
152 template <typename ImportT>
153 LLVM_NODISCARD Error importInto(ImportT *&To, ImportT *From) {
Gabor Marton5ac6d492019-05-15 10:29:48 +0000154 auto ToOrErr = Importer.Import(From);
Balazs Keri57949eb2019-03-25 09:16:39 +0000155 if (ToOrErr)
156 To = cast_or_null<ImportT>(*ToOrErr);
157 return ToOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +0000158 }
159
160 // Call the import function of ASTImporter for a baseclass of type `T` and
161 // cast the return value to `T`.
162 template <typename T>
163 Expected<T *> import(T *From) {
Gabor Marton5ac6d492019-05-15 10:29:48 +0000164 auto ToOrErr = Importer.Import(From);
Balazs Keri57949eb2019-03-25 09:16:39 +0000165 if (!ToOrErr)
166 return ToOrErr.takeError();
167 return cast_or_null<T>(*ToOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +0000168 }
169
170 template <typename T>
171 Expected<T *> import(const T *From) {
172 return import(const_cast<T *>(From));
173 }
174
175 // Call the import function of ASTImporter for type `T`.
176 template <typename T>
177 Expected<T> import(const T &From) {
Gabor Marton5ac6d492019-05-15 10:29:48 +0000178 return Importer.Import(From);
Balazs Keri3b30d652018-10-19 13:32:20 +0000179 }
180
Richard Smithb9fb1212019-05-06 03:47:15 +0000181 // Import an Optional<T> by importing the contained T, if any.
182 template<typename T>
183 Expected<Optional<T>> import(Optional<T> From) {
184 if (!From)
185 return Optional<T>();
186 return import(*From);
187 }
188
Balazs Keri3b30d652018-10-19 13:32:20 +0000189 template <class T>
190 Expected<std::tuple<T>>
191 importSeq(const T &From) {
192 Expected<T> ToOrErr = import(From);
193 if (!ToOrErr)
194 return ToOrErr.takeError();
195 return std::make_tuple<T>(std::move(*ToOrErr));
196 }
197
198 // Import multiple objects with a single function call.
199 // This should work for every type for which a variant of `import` exists.
200 // The arguments are processed from left to right and import is stopped on
201 // first error.
202 template <class THead, class... TTail>
203 Expected<std::tuple<THead, TTail...>>
204 importSeq(const THead &FromHead, const TTail &...FromTail) {
205 Expected<std::tuple<THead>> ToHeadOrErr = importSeq(FromHead);
206 if (!ToHeadOrErr)
207 return ToHeadOrErr.takeError();
208 Expected<std::tuple<TTail...>> ToTailOrErr = importSeq(FromTail...);
209 if (!ToTailOrErr)
210 return ToTailOrErr.takeError();
211 return std::tuple_cat(*ToHeadOrErr, *ToTailOrErr);
212 }
213
214// Wrapper for an overload set.
Gabor Marton26f72a92018-07-12 09:42:05 +0000215 template <typename ToDeclT> struct CallOverloadedCreateFun {
216 template <typename... Args>
217 auto operator()(Args &&... args)
218 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) {
219 return ToDeclT::Create(std::forward<Args>(args)...);
220 }
221 };
222
223 // Always use these functions to create a Decl during import. There are
224 // certain tasks which must be done after the Decl was created, e.g. we
225 // must immediately register that as an imported Decl. The parameter `ToD`
226 // will be set to the newly created Decl or if had been imported before
227 // then to the already imported Decl. Returns a bool value set to true if
228 // the `FromD` had been imported before.
229 template <typename ToDeclT, typename FromDeclT, typename... Args>
230 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
231 Args &&... args) {
232 // There may be several overloads of ToDeclT::Create. We must make sure
233 // to call the one which would be chosen by the arguments, thus we use a
234 // wrapper for the overload set.
235 CallOverloadedCreateFun<ToDeclT> OC;
236 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
237 std::forward<Args>(args)...);
238 }
239 // Use this overload if a special Type is needed to be created. E.g if we
240 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl`
241 // then:
242 // TypedefNameDecl *ToTypedef;
243 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...);
244 template <typename NewDeclT, typename ToDeclT, typename FromDeclT,
245 typename... Args>
246 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
247 Args &&... args) {
248 CallOverloadedCreateFun<NewDeclT> OC;
249 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
250 std::forward<Args>(args)...);
251 }
252 // Use this version if a special create function must be
253 // used, e.g. CXXRecordDecl::CreateLambda .
254 template <typename ToDeclT, typename CreateFunT, typename FromDeclT,
255 typename... Args>
256 LLVM_NODISCARD bool
257 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
258 FromDeclT *FromD, Args &&... args) {
Gabor Marton303c98612019-06-25 08:00:51 +0000259 if (Importer.getImportDeclErrorIfAny(FromD)) {
260 ToD = nullptr;
261 return true; // Already imported but with error.
262 }
Gabor Marton26f72a92018-07-12 09:42:05 +0000263 ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD));
264 if (ToD)
265 return true; // Already imported.
266 ToD = CreateFun(std::forward<Args>(args)...);
Gabor Marton54058b52018-12-17 13:53:12 +0000267 // Keep track of imported Decls.
Raphael Isemanne9bc35f2019-04-29 21:02:35 +0000268 Importer.RegisterImportedDecl(FromD, ToD);
Gabor Marton26f72a92018-07-12 09:42:05 +0000269 InitializeImportedDecl(FromD, ToD);
270 return false; // A new Decl is created.
271 }
272
273 void InitializeImportedDecl(Decl *FromD, Decl *ToD) {
Gabor Marton26f72a92018-07-12 09:42:05 +0000274 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
275 if (FromD->hasAttrs())
Balazs Keri57949eb2019-03-25 09:16:39 +0000276 for (const Attr *FromAttr : FromD->getAttrs()) {
277 // FIXME: Return of the error here is not possible until store of
278 // import errors is implemented.
279 auto ToAttrOrErr = import(FromAttr);
280 if (ToAttrOrErr)
281 ToD->addAttr(*ToAttrOrErr);
282 else
283 llvm::consumeError(ToAttrOrErr.takeError());
284 }
Gabor Marton26f72a92018-07-12 09:42:05 +0000285 if (FromD->isUsed())
286 ToD->setIsUsed();
287 if (FromD->isImplicit())
288 ToD->setImplicit();
289 }
290
Gabor Martondd59d272019-03-19 14:04:50 +0000291 // Check if we have found an existing definition. Returns with that
292 // definition if yes, otherwise returns null.
293 Decl *FindAndMapDefinition(FunctionDecl *D, FunctionDecl *FoundFunction) {
294 const FunctionDecl *Definition = nullptr;
295 if (D->doesThisDeclarationHaveABody() &&
296 FoundFunction->hasBody(Definition))
297 return Importer.MapImported(D, const_cast<FunctionDecl *>(Definition));
298 return nullptr;
299 }
300
Gabor Martonbc5b7e22019-12-04 17:12:08 +0100301 void addDeclToContexts(Decl *FromD, Decl *ToD) {
302 if (Importer.isMinimalImport()) {
303 // In minimal import case the decl must be added even if it is not
304 // contained in original context, for LLDB compatibility.
305 // FIXME: Check if a better solution is possible.
306 if (!FromD->getDescribedTemplate() &&
307 FromD->getFriendObjectKind() == Decl::FOK_None)
308 ToD->getLexicalDeclContext()->addDeclInternal(ToD);
309 return;
310 }
311
312 DeclContext *FromDC = FromD->getDeclContext();
313 DeclContext *FromLexicalDC = FromD->getLexicalDeclContext();
314 DeclContext *ToDC = ToD->getDeclContext();
315 DeclContext *ToLexicalDC = ToD->getLexicalDeclContext();
316
317 bool Visible = false;
318 if (FromDC->containsDeclAndLoad(FromD)) {
319 ToDC->addDeclInternal(ToD);
320 Visible = true;
321 }
322 if (ToDC != ToLexicalDC && FromLexicalDC->containsDeclAndLoad(FromD)) {
323 ToLexicalDC->addDeclInternal(ToD);
324 Visible = true;
325 }
326
327 // If the Decl was added to any context, it was made already visible.
328 // Otherwise it is still possible that it should be visible.
329 if (!Visible) {
330 if (auto *FromNamed = dyn_cast<NamedDecl>(FromD)) {
331 auto *ToNamed = cast<NamedDecl>(ToD);
332 DeclContextLookupResult FromLookup =
333 FromDC->lookup(FromNamed->getDeclName());
334 for (NamedDecl *ND : FromLookup)
335 if (ND == FromNamed) {
336 ToDC->makeDeclVisibleInContext(ToNamed);
337 break;
338 }
339 }
340 }
341 }
342
Douglas Gregor96e578d2010-02-05 17:54:41 +0000343 public:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000344 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
Gabor Marton344b0992018-05-16 11:48:11 +0000345
Balazs Keri3b30d652018-10-19 13:32:20 +0000346 using TypeVisitor<ASTNodeImporter, ExpectedType>::Visit;
347 using DeclVisitor<ASTNodeImporter, ExpectedDecl>::Visit;
348 using StmtVisitor<ASTNodeImporter, ExpectedStmt>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000349
350 // Importing types
Balazs Keri3b30d652018-10-19 13:32:20 +0000351 ExpectedType VisitType(const Type *T);
352 ExpectedType VisitAtomicType(const AtomicType *T);
353 ExpectedType VisitBuiltinType(const BuiltinType *T);
354 ExpectedType VisitDecayedType(const DecayedType *T);
355 ExpectedType VisitComplexType(const ComplexType *T);
356 ExpectedType VisitPointerType(const PointerType *T);
357 ExpectedType VisitBlockPointerType(const BlockPointerType *T);
358 ExpectedType VisitLValueReferenceType(const LValueReferenceType *T);
359 ExpectedType VisitRValueReferenceType(const RValueReferenceType *T);
360 ExpectedType VisitMemberPointerType(const MemberPointerType *T);
361 ExpectedType VisitConstantArrayType(const ConstantArrayType *T);
362 ExpectedType VisitIncompleteArrayType(const IncompleteArrayType *T);
363 ExpectedType VisitVariableArrayType(const VariableArrayType *T);
364 ExpectedType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000365 // FIXME: DependentSizedExtVectorType
Balazs Keri3b30d652018-10-19 13:32:20 +0000366 ExpectedType VisitVectorType(const VectorType *T);
367 ExpectedType VisitExtVectorType(const ExtVectorType *T);
368 ExpectedType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
369 ExpectedType VisitFunctionProtoType(const FunctionProtoType *T);
370 ExpectedType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
371 ExpectedType VisitParenType(const ParenType *T);
372 ExpectedType VisitTypedefType(const TypedefType *T);
373 ExpectedType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000374 // FIXME: DependentTypeOfExprType
Balazs Keri3b30d652018-10-19 13:32:20 +0000375 ExpectedType VisitTypeOfType(const TypeOfType *T);
376 ExpectedType VisitDecltypeType(const DecltypeType *T);
377 ExpectedType VisitUnaryTransformType(const UnaryTransformType *T);
378 ExpectedType VisitAutoType(const AutoType *T);
379 ExpectedType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000380 // FIXME: DependentDecltypeType
Balazs Keri3b30d652018-10-19 13:32:20 +0000381 ExpectedType VisitRecordType(const RecordType *T);
382 ExpectedType VisitEnumType(const EnumType *T);
383 ExpectedType VisitAttributedType(const AttributedType *T);
384 ExpectedType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
385 ExpectedType VisitSubstTemplateTypeParmType(
386 const SubstTemplateTypeParmType *T);
387 ExpectedType VisitTemplateSpecializationType(
388 const TemplateSpecializationType *T);
389 ExpectedType VisitElaboratedType(const ElaboratedType *T);
390 ExpectedType VisitDependentNameType(const DependentNameType *T);
391 ExpectedType VisitPackExpansionType(const PackExpansionType *T);
392 ExpectedType VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000393 const DependentTemplateSpecializationType *T);
Balazs Keri3b30d652018-10-19 13:32:20 +0000394 ExpectedType VisitObjCInterfaceType(const ObjCInterfaceType *T);
395 ExpectedType VisitObjCObjectType(const ObjCObjectType *T);
396 ExpectedType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Rafael Stahldf556202018-05-29 08:12:15 +0000397
398 // Importing declarations
Balazs Keri3b30d652018-10-19 13:32:20 +0000399 Error ImportDeclParts(
400 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
401 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc);
402 Error ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
403 Error ImportDeclarationNameLoc(
404 const DeclarationNameInfo &From, DeclarationNameInfo &To);
405 Error ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
406 Error ImportDeclContext(
407 Decl *From, DeclContext *&ToDC, DeclContext *&ToLexicalDC);
408 Error ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000409
Balazs Keri3b30d652018-10-19 13:32:20 +0000410 Expected<CXXCastPath> ImportCastPath(CastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000411
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000412 using Designator = DesignatedInitExpr::Designator;
413
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000414 /// What we should import from the definition.
Fangrui Song6907ce22018-07-30 19:24:48 +0000415 enum ImportDefinitionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000416 /// Import the default subset of the definition, which might be
Douglas Gregor95d82832012-01-24 18:36:04 +0000417 /// nothing (if minimal import is set) or might be everything (if minimal
418 /// import is not set).
419 IDK_Default,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000420 /// Import everything.
Douglas Gregor95d82832012-01-24 18:36:04 +0000421 IDK_Everything,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000422 /// Import only the bare bones needed to establish a valid
Douglas Gregor95d82832012-01-24 18:36:04 +0000423 /// DeclContext.
424 IDK_Basic
425 };
426
Douglas Gregor2e15c842012-02-01 21:00:38 +0000427 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
428 return IDK == IDK_Everything ||
429 (IDK == IDK_Default && !Importer.isMinimalImport());
430 }
431
Balazs Keri3b30d652018-10-19 13:32:20 +0000432 Error ImportInitializer(VarDecl *From, VarDecl *To);
433 Error ImportDefinition(
434 RecordDecl *From, RecordDecl *To,
435 ImportDefinitionKind Kind = IDK_Default);
436 Error ImportDefinition(
437 EnumDecl *From, EnumDecl *To,
438 ImportDefinitionKind Kind = IDK_Default);
439 Error ImportDefinition(
440 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
441 ImportDefinitionKind Kind = IDK_Default);
442 Error ImportDefinition(
443 ObjCProtocolDecl *From, ObjCProtocolDecl *To,
444 ImportDefinitionKind Kind = IDK_Default);
Balazs Keri3b30d652018-10-19 13:32:20 +0000445 Error ImportTemplateArguments(
446 const TemplateArgument *FromArgs, unsigned NumFromArgs,
447 SmallVectorImpl<TemplateArgument> &ToArgs);
448 Expected<TemplateArgument>
449 ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000450
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000451 template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000452 Error ImportTemplateArgumentListInfo(
453 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000454
455 template<typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000456 Error ImportTemplateArgumentListInfo(
457 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
458 const InContainerTy &Container, TemplateArgumentListInfo &Result);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000459
Gabor Marton5254e642018-06-27 13:32:50 +0000460 using TemplateArgsTy = SmallVector<TemplateArgument, 8>;
Balazs Keri3b30d652018-10-19 13:32:20 +0000461 using FunctionTemplateAndArgsTy =
462 std::tuple<FunctionTemplateDecl *, TemplateArgsTy>;
463 Expected<FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000464 ImportFunctionTemplateWithTemplateArgsFromSpecialization(
465 FunctionDecl *FromFD);
Balazs Keri1efc9742019-05-07 10:55:11 +0000466 Error ImportTemplateParameterLists(const DeclaratorDecl *FromD,
467 DeclaratorDecl *ToD);
Gabor Marton5254e642018-06-27 13:32:50 +0000468
Balazs Keri3b30d652018-10-19 13:32:20 +0000469 Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000470
Shafik Yaghmour96b3d202019-01-28 21:55:33 +0000471 Error ImportFunctionDeclBody(FunctionDecl *FromFD, FunctionDecl *ToFD);
472
Balazs Keric5095942019-08-14 09:41:39 +0000473 Error ImportDefaultArgOfParmVarDecl(const ParmVarDecl *FromParam,
474 ParmVarDecl *ToParam);
475
Gabor Marton458d1452019-02-14 13:07:03 +0000476 template <typename T>
477 bool hasSameVisibilityContext(T *Found, T *From);
478
Gabor Marton950fb572018-07-17 12:39:27 +0000479 bool IsStructuralMatch(Decl *From, Decl *To, bool Complain);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000480 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
481 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000482 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
483 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000484 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000485 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000486 bool IsStructuralMatch(FunctionTemplateDecl *From,
487 FunctionTemplateDecl *To);
Balazs Keric7797c42018-07-11 09:37:24 +0000488 bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000489 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000490 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Balazs Keri3b30d652018-10-19 13:32:20 +0000491 ExpectedDecl VisitDecl(Decl *D);
492 ExpectedDecl VisitImportDecl(ImportDecl *D);
493 ExpectedDecl VisitEmptyDecl(EmptyDecl *D);
494 ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D);
495 ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D);
496 ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D);
497 ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D);
498 ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
499 ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
500 ExpectedDecl VisitTypedefDecl(TypedefDecl *D);
501 ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D);
502 ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
503 ExpectedDecl VisitLabelDecl(LabelDecl *D);
504 ExpectedDecl VisitEnumDecl(EnumDecl *D);
505 ExpectedDecl VisitRecordDecl(RecordDecl *D);
506 ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D);
507 ExpectedDecl VisitFunctionDecl(FunctionDecl *D);
508 ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D);
509 ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D);
510 ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D);
511 ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D);
512 ExpectedDecl VisitFieldDecl(FieldDecl *D);
513 ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D);
514 ExpectedDecl VisitFriendDecl(FriendDecl *D);
515 ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D);
516 ExpectedDecl VisitVarDecl(VarDecl *D);
517 ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D);
518 ExpectedDecl VisitParmVarDecl(ParmVarDecl *D);
519 ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D);
520 ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
521 ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D);
522 ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D);
523 ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D);
524 ExpectedDecl VisitUsingDecl(UsingDecl *D);
525 ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D);
526 ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
527 ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
528 ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Raphael Isemannba7bde62019-10-30 14:50:35 +0100529 ExpectedDecl VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D);
Tykerb0561b32019-11-17 11:41:55 +0100530 ExpectedDecl
531 VisitLifetimeExtendedTemporaryDecl(LifetimeExtendedTemporaryDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000532
Balazs Keri3b30d652018-10-19 13:32:20 +0000533 Expected<ObjCTypeParamList *>
534 ImportObjCTypeParamList(ObjCTypeParamList *list);
535
536 ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
537 ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
538 ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D);
539 ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D);
540 ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
541 ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
542 ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
543 ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
544 ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D);
545 ExpectedDecl VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +0000546 ClassTemplateSpecializationDecl *D);
Balazs Keri3b30d652018-10-19 13:32:20 +0000547 ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D);
548 ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
549 ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000550
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000551 // Importing statements
Balazs Keri3b30d652018-10-19 13:32:20 +0000552 ExpectedStmt VisitStmt(Stmt *S);
553 ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S);
554 ExpectedStmt VisitDeclStmt(DeclStmt *S);
555 ExpectedStmt VisitNullStmt(NullStmt *S);
556 ExpectedStmt VisitCompoundStmt(CompoundStmt *S);
557 ExpectedStmt VisitCaseStmt(CaseStmt *S);
558 ExpectedStmt VisitDefaultStmt(DefaultStmt *S);
559 ExpectedStmt VisitLabelStmt(LabelStmt *S);
560 ExpectedStmt VisitAttributedStmt(AttributedStmt *S);
561 ExpectedStmt VisitIfStmt(IfStmt *S);
562 ExpectedStmt VisitSwitchStmt(SwitchStmt *S);
563 ExpectedStmt VisitWhileStmt(WhileStmt *S);
564 ExpectedStmt VisitDoStmt(DoStmt *S);
565 ExpectedStmt VisitForStmt(ForStmt *S);
566 ExpectedStmt VisitGotoStmt(GotoStmt *S);
567 ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S);
568 ExpectedStmt VisitContinueStmt(ContinueStmt *S);
569 ExpectedStmt VisitBreakStmt(BreakStmt *S);
570 ExpectedStmt VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000571 // FIXME: MSAsmStmt
572 // FIXME: SEHExceptStmt
573 // FIXME: SEHFinallyStmt
574 // FIXME: SEHTryStmt
575 // FIXME: SEHLeaveStmt
576 // FIXME: CapturedStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000577 ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S);
578 ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S);
579 ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000580 // FIXME: MSDependentExistsStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000581 ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
582 ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
583 ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
584 ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S);
585 ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
586 ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
587 ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000588
589 // Importing expressions
Balazs Keri3b30d652018-10-19 13:32:20 +0000590 ExpectedStmt VisitExpr(Expr *E);
591 ExpectedStmt VisitVAArgExpr(VAArgExpr *E);
Tom Roeder521f0042019-02-26 19:26:41 +0000592 ExpectedStmt VisitChooseExpr(ChooseExpr *E);
Balazs Keri3b30d652018-10-19 13:32:20 +0000593 ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E);
594 ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E);
595 ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E);
596 ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
597 ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E);
598 ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
599 ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E);
600 ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E);
601 ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E);
602 ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E);
603 ExpectedStmt VisitStringLiteral(StringLiteral *E);
604 ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
605 ExpectedStmt VisitAtomicExpr(AtomicExpr *E);
606 ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E);
Bill Wendling8003edc2018-11-09 00:41:36 +0000607 ExpectedStmt VisitConstantExpr(ConstantExpr *E);
Balazs Keri3b30d652018-10-19 13:32:20 +0000608 ExpectedStmt VisitParenExpr(ParenExpr *E);
609 ExpectedStmt VisitParenListExpr(ParenListExpr *E);
610 ExpectedStmt VisitStmtExpr(StmtExpr *E);
611 ExpectedStmt VisitUnaryOperator(UnaryOperator *E);
612 ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
613 ExpectedStmt VisitBinaryOperator(BinaryOperator *E);
614 ExpectedStmt VisitConditionalOperator(ConditionalOperator *E);
615 ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
616 ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E);
617 ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
618 ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E);
619 ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E);
620 ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E);
621 ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E);
622 ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E);
623 ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE);
624 ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E);
625 ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
626 ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
627 ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
628 ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
629 ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
630 ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
631 ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E);
632 ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E);
633 ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E);
634 ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E);
635 ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E);
636 ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
637 ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
638 ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
639 ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
640 ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
641 ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
642 ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E);
643 ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E);
644 ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
645 ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
646 ExpectedStmt VisitMemberExpr(MemberExpr *E);
647 ExpectedStmt VisitCallExpr(CallExpr *E);
648 ExpectedStmt VisitLambdaExpr(LambdaExpr *LE);
649 ExpectedStmt VisitInitListExpr(InitListExpr *E);
650 ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
651 ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E);
652 ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
653 ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
654 ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
655 ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
656 ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
657 ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E);
658 ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000659
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000660 template<typename IIter, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000661 Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000662 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
Balazs Keri3b30d652018-10-19 13:32:20 +0000663 for (; Ibegin != Iend; ++Ibegin, ++Obegin) {
664 Expected<ItemT> ToOrErr = import(*Ibegin);
665 if (!ToOrErr)
666 return ToOrErr.takeError();
667 *Obegin = *ToOrErr;
668 }
669 return Error::success();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000670 }
671
Balazs Keri3b30d652018-10-19 13:32:20 +0000672 // Import every item from a container structure into an output container.
673 // If error occurs, stops at first error and returns the error.
674 // The output container should have space for all needed elements (it is not
675 // expanded, new items are put into from the beginning).
Aleksei Sidorina693b372016-09-28 10:16:56 +0000676 template<typename InContainerTy, typename OutContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000677 Error ImportContainerChecked(
678 const InContainerTy &InContainer, OutContainerTy &OutContainer) {
679 return ImportArrayChecked(
680 InContainer.begin(), InContainer.end(), OutContainer.begin());
Aleksei Sidorina693b372016-09-28 10:16:56 +0000681 }
682
683 template<typename InContainerTy, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000684 Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000685 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
686 }
Lang Hames19e07e12017-06-20 21:06:00 +0000687
Balazs Kerib4fd7d42019-08-30 10:12:14 +0000688 Error ImportOverriddenMethods(CXXMethodDecl *ToMethod,
689 CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000690
Balazs Keri3b30d652018-10-19 13:32:20 +0000691 Expected<FunctionDecl *> FindFunctionTemplateSpecialization(
692 FunctionDecl *FromFD);
Gabor Marton25234fd2019-12-12 17:13:35 +0100693
694 // Returns true if the given function has a placeholder return type and
695 // that type is declared inside the body of the function.
696 // E.g. auto f() { struct X{}; return X(); }
697 bool hasAutoReturnTypeDeclaredInside(FunctionDecl *D);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000698 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000699
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000700template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000701Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000702 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
703 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000704 auto ToLAngleLocOrErr = import(FromLAngleLoc);
705 if (!ToLAngleLocOrErr)
706 return ToLAngleLocOrErr.takeError();
707 auto ToRAngleLocOrErr = import(FromRAngleLoc);
708 if (!ToRAngleLocOrErr)
709 return ToRAngleLocOrErr.takeError();
710
711 TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr);
712 if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo))
713 return Err;
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000714 Result = ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +0000715 return Error::success();
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000716}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000717
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000718template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000719Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000720 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
721 return ImportTemplateArgumentListInfo(
722 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
723}
724
725template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000726Error ASTNodeImporter::ImportTemplateArgumentListInfo<
727 ASTTemplateArgumentListInfo>(
728 const ASTTemplateArgumentListInfo &From,
729 TemplateArgumentListInfo &Result) {
730 return ImportTemplateArgumentListInfo(
731 From.LAngleLoc, From.RAngleLoc, From.arguments(), Result);
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000732}
733
Balazs Keri3b30d652018-10-19 13:32:20 +0000734Expected<ASTNodeImporter::FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000735ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
736 FunctionDecl *FromFD) {
737 assert(FromFD->getTemplatedKind() ==
Balazs Keri3b30d652018-10-19 13:32:20 +0000738 FunctionDecl::TK_FunctionTemplateSpecialization);
739
740 FunctionTemplateAndArgsTy Result;
741
Gabor Marton5254e642018-06-27 13:32:50 +0000742 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Balazs Keri3b30d652018-10-19 13:32:20 +0000743 if (Error Err = importInto(std::get<0>(Result), FTSInfo->getTemplate()))
744 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000745
746 // Import template arguments.
747 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
Balazs Keri3b30d652018-10-19 13:32:20 +0000748 if (Error Err = ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
749 std::get<1>(Result)))
750 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000751
Balazs Keri3b30d652018-10-19 13:32:20 +0000752 return Result;
753}
754
755template <>
756Expected<TemplateParameterList *>
757ASTNodeImporter::import(TemplateParameterList *From) {
758 SmallVector<NamedDecl *, 4> To(From->size());
759 if (Error Err = ImportContainerChecked(*From, To))
760 return std::move(Err);
761
762 ExpectedExpr ToRequiresClause = import(From->getRequiresClause());
763 if (!ToRequiresClause)
764 return ToRequiresClause.takeError();
765
766 auto ToTemplateLocOrErr = import(From->getTemplateLoc());
767 if (!ToTemplateLocOrErr)
768 return ToTemplateLocOrErr.takeError();
769 auto ToLAngleLocOrErr = import(From->getLAngleLoc());
770 if (!ToLAngleLocOrErr)
771 return ToLAngleLocOrErr.takeError();
772 auto ToRAngleLocOrErr = import(From->getRAngleLoc());
773 if (!ToRAngleLocOrErr)
774 return ToRAngleLocOrErr.takeError();
775
776 return TemplateParameterList::Create(
777 Importer.getToContext(),
778 *ToTemplateLocOrErr,
779 *ToLAngleLocOrErr,
780 To,
781 *ToRAngleLocOrErr,
782 *ToRequiresClause);
783}
784
785template <>
786Expected<TemplateArgument>
787ASTNodeImporter::import(const TemplateArgument &From) {
788 switch (From.getKind()) {
789 case TemplateArgument::Null:
790 return TemplateArgument();
791
792 case TemplateArgument::Type: {
793 ExpectedType ToTypeOrErr = import(From.getAsType());
794 if (!ToTypeOrErr)
795 return ToTypeOrErr.takeError();
796 return TemplateArgument(*ToTypeOrErr);
797 }
798
799 case TemplateArgument::Integral: {
800 ExpectedType ToTypeOrErr = import(From.getIntegralType());
801 if (!ToTypeOrErr)
802 return ToTypeOrErr.takeError();
803 return TemplateArgument(From, *ToTypeOrErr);
804 }
805
806 case TemplateArgument::Declaration: {
807 Expected<ValueDecl *> ToOrErr = import(From.getAsDecl());
808 if (!ToOrErr)
809 return ToOrErr.takeError();
810 ExpectedType ToTypeOrErr = import(From.getParamTypeForDecl());
811 if (!ToTypeOrErr)
812 return ToTypeOrErr.takeError();
813 return TemplateArgument(*ToOrErr, *ToTypeOrErr);
814 }
815
816 case TemplateArgument::NullPtr: {
817 ExpectedType ToTypeOrErr = import(From.getNullPtrType());
818 if (!ToTypeOrErr)
819 return ToTypeOrErr.takeError();
820 return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/true);
821 }
822
823 case TemplateArgument::Template: {
824 Expected<TemplateName> ToTemplateOrErr = import(From.getAsTemplate());
825 if (!ToTemplateOrErr)
826 return ToTemplateOrErr.takeError();
827
828 return TemplateArgument(*ToTemplateOrErr);
829 }
830
831 case TemplateArgument::TemplateExpansion: {
832 Expected<TemplateName> ToTemplateOrErr =
833 import(From.getAsTemplateOrTemplatePattern());
834 if (!ToTemplateOrErr)
835 return ToTemplateOrErr.takeError();
836
837 return TemplateArgument(
838 *ToTemplateOrErr, From.getNumTemplateExpansions());
839 }
840
841 case TemplateArgument::Expression:
842 if (ExpectedExpr ToExpr = import(From.getAsExpr()))
843 return TemplateArgument(*ToExpr);
844 else
845 return ToExpr.takeError();
846
847 case TemplateArgument::Pack: {
848 SmallVector<TemplateArgument, 2> ToPack;
849 ToPack.reserve(From.pack_size());
850 if (Error Err = ImportTemplateArguments(
851 From.pack_begin(), From.pack_size(), ToPack))
852 return std::move(Err);
853
854 return TemplateArgument(
855 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
856 }
857 }
858
859 llvm_unreachable("Invalid template argument kind");
860}
861
862template <>
863Expected<TemplateArgumentLoc>
864ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) {
865 Expected<TemplateArgument> ArgOrErr = import(TALoc.getArgument());
866 if (!ArgOrErr)
867 return ArgOrErr.takeError();
868 TemplateArgument Arg = *ArgOrErr;
869
870 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
871
872 TemplateArgumentLocInfo ToInfo;
873 if (Arg.getKind() == TemplateArgument::Expression) {
874 ExpectedExpr E = import(FromInfo.getAsExpr());
875 if (!E)
876 return E.takeError();
877 ToInfo = TemplateArgumentLocInfo(*E);
878 } else if (Arg.getKind() == TemplateArgument::Type) {
879 if (auto TSIOrErr = import(FromInfo.getAsTypeSourceInfo()))
880 ToInfo = TemplateArgumentLocInfo(*TSIOrErr);
881 else
882 return TSIOrErr.takeError();
883 } else {
884 auto ToTemplateQualifierLocOrErr =
885 import(FromInfo.getTemplateQualifierLoc());
886 if (!ToTemplateQualifierLocOrErr)
887 return ToTemplateQualifierLocOrErr.takeError();
888 auto ToTemplateNameLocOrErr = import(FromInfo.getTemplateNameLoc());
889 if (!ToTemplateNameLocOrErr)
890 return ToTemplateNameLocOrErr.takeError();
891 auto ToTemplateEllipsisLocOrErr =
892 import(FromInfo.getTemplateEllipsisLoc());
893 if (!ToTemplateEllipsisLocOrErr)
894 return ToTemplateEllipsisLocOrErr.takeError();
895
896 ToInfo = TemplateArgumentLocInfo(
897 *ToTemplateQualifierLocOrErr,
898 *ToTemplateNameLocOrErr,
899 *ToTemplateEllipsisLocOrErr);
900 }
901
902 return TemplateArgumentLoc(Arg, ToInfo);
903}
904
905template <>
906Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) {
907 if (DG.isNull())
908 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
909 size_t NumDecls = DG.end() - DG.begin();
910 SmallVector<Decl *, 1> ToDecls;
911 ToDecls.reserve(NumDecls);
912 for (Decl *FromD : DG) {
913 if (auto ToDOrErr = import(FromD))
914 ToDecls.push_back(*ToDOrErr);
915 else
916 return ToDOrErr.takeError();
917 }
918 return DeclGroupRef::Create(Importer.getToContext(),
919 ToDecls.begin(),
920 NumDecls);
921}
922
923template <>
924Expected<ASTNodeImporter::Designator>
925ASTNodeImporter::import(const Designator &D) {
926 if (D.isFieldDesignator()) {
927 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
928
929 ExpectedSLoc ToDotLocOrErr = import(D.getDotLoc());
930 if (!ToDotLocOrErr)
931 return ToDotLocOrErr.takeError();
932
933 ExpectedSLoc ToFieldLocOrErr = import(D.getFieldLoc());
934 if (!ToFieldLocOrErr)
935 return ToFieldLocOrErr.takeError();
936
937 return Designator(ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr);
938 }
939
940 ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc());
941 if (!ToLBracketLocOrErr)
942 return ToLBracketLocOrErr.takeError();
943
944 ExpectedSLoc ToRBracketLocOrErr = import(D.getRBracketLoc());
945 if (!ToRBracketLocOrErr)
946 return ToRBracketLocOrErr.takeError();
947
948 if (D.isArrayDesignator())
949 return Designator(D.getFirstExprIndex(),
950 *ToLBracketLocOrErr, *ToRBracketLocOrErr);
951
952 ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc());
953 if (!ToEllipsisLocOrErr)
954 return ToEllipsisLocOrErr.takeError();
955
956 assert(D.isArrayRangeDesignator());
957 return Designator(
958 D.getFirstExprIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr,
959 *ToRBracketLocOrErr);
960}
961
962template <>
963Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) {
964 VarDecl *Var = nullptr;
965 if (From.capturesVariable()) {
966 if (auto VarOrErr = import(From.getCapturedVar()))
967 Var = *VarOrErr;
968 else
969 return VarOrErr.takeError();
970 }
971
972 auto LocationOrErr = import(From.getLocation());
973 if (!LocationOrErr)
974 return LocationOrErr.takeError();
975
976 SourceLocation EllipsisLoc;
977 if (From.isPackExpansion())
978 if (Error Err = importInto(EllipsisLoc, From.getEllipsisLoc()))
979 return std::move(Err);
980
981 return LambdaCapture(
982 *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var,
983 EllipsisLoc);
Gabor Marton5254e642018-06-27 13:32:50 +0000984}
985
Balazs Keric86d47b2019-09-04 14:12:18 +0000986template <typename T>
987bool ASTNodeImporter::hasSameVisibilityContext(T *Found, T *From) {
988 if (From->hasExternalFormalLinkage())
989 return Found->hasExternalFormalLinkage();
990 if (Importer.GetFromTU(Found) != From->getTranslationUnitDecl())
991 return false;
992 if (From->isInAnonymousNamespace())
993 return Found->isInAnonymousNamespace();
994 else
995 return !Found->isInAnonymousNamespace() &&
996 !Found->hasExternalFormalLinkage();
997}
998
999template <>
1000bool ASTNodeImporter::hasSameVisibilityContext(TypedefNameDecl *Found,
1001 TypedefNameDecl *From) {
1002 if (From->isInAnonymousNamespace() && Found->isInAnonymousNamespace())
1003 return Importer.GetFromTU(Found) == From->getTranslationUnitDecl();
1004 return From->isInAnonymousNamespace() == Found->isInAnonymousNamespace();
1005}
1006
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001007} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +00001008
Douglas Gregor3996e242010-02-15 22:01:00 +00001009//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001010// Import Types
1011//----------------------------------------------------------------------------
1012
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001013using namespace clang;
1014
Balazs Keri3b30d652018-10-19 13:32:20 +00001015ExpectedType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001016 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1017 << T->getTypeClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00001018 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +00001019}
1020
Balazs Keri3b30d652018-10-19 13:32:20 +00001021ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
1022 ExpectedType UnderlyingTypeOrErr = import(T->getValueType());
1023 if (!UnderlyingTypeOrErr)
1024 return UnderlyingTypeOrErr.takeError();
Gabor Horvath0866c2f2016-11-23 15:24:23 +00001025
Balazs Keri3b30d652018-10-19 13:32:20 +00001026 return Importer.getToContext().getAtomicType(*UnderlyingTypeOrErr);
Gabor Horvath0866c2f2016-11-23 15:24:23 +00001027}
1028
Balazs Keri3b30d652018-10-19 13:32:20 +00001029ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001030 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +00001031#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
1032 case BuiltinType::Id: \
1033 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +00001034#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +00001035#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
1036 case BuiltinType::Id: \
1037 return Importer.getToContext().Id##Ty;
1038#include "clang/Basic/OpenCLExtensionTypes.def"
Richard Sandifordeb485fb2019-08-09 08:52:54 +00001039#define SVE_TYPE(Name, Id, SingletonId) \
1040 case BuiltinType::Id: \
1041 return Importer.getToContext().SingletonId;
1042#include "clang/Basic/AArch64SVEACLETypes.def"
John McCalle314e272011-10-18 21:02:43 +00001043#define SHARED_SINGLETON_TYPE(Expansion)
1044#define BUILTIN_TYPE(Id, SingletonId) \
1045 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1046#include "clang/AST/BuiltinTypes.def"
1047
1048 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1049 // context supports C++.
1050
1051 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1052 // context supports ObjC.
1053
Douglas Gregor96e578d2010-02-05 17:54:41 +00001054 case BuiltinType::Char_U:
Fangrui Song6907ce22018-07-30 19:24:48 +00001055 // The context we're importing from has an unsigned 'char'. If we're
1056 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +00001057 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001058 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001059 return Importer.getToContext().UnsignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +00001060
Douglas Gregor96e578d2010-02-05 17:54:41 +00001061 return Importer.getToContext().CharTy;
1062
Douglas Gregor96e578d2010-02-05 17:54:41 +00001063 case BuiltinType::Char_S:
Fangrui Song6907ce22018-07-30 19:24:48 +00001064 // The context we're importing from has an unsigned 'char'. If we're
1065 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +00001066 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001067 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001068 return Importer.getToContext().SignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +00001069
Douglas Gregor96e578d2010-02-05 17:54:41 +00001070 return Importer.getToContext().CharTy;
1071
Chris Lattnerad3467e2010-12-25 23:25:43 +00001072 case BuiltinType::WChar_S:
1073 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001074 // FIXME: If not in C++, shall we translate to the C equivalent of
1075 // wchar_t?
1076 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001077 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001078
1079 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001080}
1081
Balazs Keri3b30d652018-10-19 13:32:20 +00001082ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
1083 ExpectedType ToOriginalTypeOrErr = import(T->getOriginalType());
1084 if (!ToOriginalTypeOrErr)
1085 return ToOriginalTypeOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00001086
Balazs Keri3b30d652018-10-19 13:32:20 +00001087 return Importer.getToContext().getDecayedType(*ToOriginalTypeOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00001088}
1089
Balazs Keri3b30d652018-10-19 13:32:20 +00001090ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
1091 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1092 if (!ToElementTypeOrErr)
1093 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001094
Balazs Keri3b30d652018-10-19 13:32:20 +00001095 return Importer.getToContext().getComplexType(*ToElementTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001096}
1097
Balazs Keri3b30d652018-10-19 13:32:20 +00001098ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) {
1099 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1100 if (!ToPointeeTypeOrErr)
1101 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001102
Balazs Keri3b30d652018-10-19 13:32:20 +00001103 return Importer.getToContext().getPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001104}
1105
Balazs Keri3b30d652018-10-19 13:32:20 +00001106ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001107 // FIXME: Check for blocks support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001108 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1109 if (!ToPointeeTypeOrErr)
1110 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001111
Balazs Keri3b30d652018-10-19 13:32:20 +00001112 return Importer.getToContext().getBlockPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001113}
1114
Balazs Keri3b30d652018-10-19 13:32:20 +00001115ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001116ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001117 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001118 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1119 if (!ToPointeeTypeOrErr)
1120 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001121
Balazs Keri3b30d652018-10-19 13:32:20 +00001122 return Importer.getToContext().getLValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001123}
1124
Balazs Keri3b30d652018-10-19 13:32:20 +00001125ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001126ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001127 // FIXME: Check for C++0x support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001128 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1129 if (!ToPointeeTypeOrErr)
1130 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001131
Balazs Keri3b30d652018-10-19 13:32:20 +00001132 return Importer.getToContext().getRValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001133}
1134
Balazs Keri3b30d652018-10-19 13:32:20 +00001135ExpectedType
1136ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001137 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001138 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1139 if (!ToPointeeTypeOrErr)
1140 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001141
Balazs Keri3b30d652018-10-19 13:32:20 +00001142 ExpectedType ClassTypeOrErr = import(QualType(T->getClass(), 0));
1143 if (!ClassTypeOrErr)
1144 return ClassTypeOrErr.takeError();
1145
1146 return Importer.getToContext().getMemberPointerType(
1147 *ToPointeeTypeOrErr, (*ClassTypeOrErr).getTypePtr());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001148}
1149
Balazs Keri3b30d652018-10-19 13:32:20 +00001150ExpectedType
1151ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Richard Smith772e2662019-10-04 01:25:59 +00001152 QualType ToElementType;
1153 const Expr *ToSizeExpr;
1154 if (auto Imp = importSeq(T->getElementType(), T->getSizeExpr()))
1155 std::tie(ToElementType, ToSizeExpr) = *Imp;
1156 else
1157 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001158
Richard Smith772e2662019-10-04 01:25:59 +00001159 return Importer.getToContext().getConstantArrayType(
1160 ToElementType, T->getSize(), ToSizeExpr, T->getSizeModifier(),
1161 T->getIndexTypeCVRQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001162}
1163
Balazs Keri3b30d652018-10-19 13:32:20 +00001164ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001165ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001166 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1167 if (!ToElementTypeOrErr)
1168 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001169
Balazs Keri3b30d652018-10-19 13:32:20 +00001170 return Importer.getToContext().getIncompleteArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001171 T->getSizeModifier(),
1172 T->getIndexTypeCVRQualifiers());
1173}
1174
Balazs Keri3b30d652018-10-19 13:32:20 +00001175ExpectedType
1176ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
1177 QualType ToElementType;
1178 Expr *ToSizeExpr;
1179 SourceRange ToBracketsRange;
1180 if (auto Imp = importSeq(
1181 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1182 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1183 else
1184 return Imp.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001185
Balazs Keri3b30d652018-10-19 13:32:20 +00001186 return Importer.getToContext().getVariableArrayType(
1187 ToElementType, ToSizeExpr, T->getSizeModifier(),
1188 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001189}
1190
Balazs Keri3b30d652018-10-19 13:32:20 +00001191ExpectedType ASTNodeImporter::VisitDependentSizedArrayType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001192 const DependentSizedArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001193 QualType ToElementType;
1194 Expr *ToSizeExpr;
1195 SourceRange ToBracketsRange;
1196 if (auto Imp = importSeq(
1197 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1198 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1199 else
1200 return Imp.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001201 // SizeExpr may be null if size is not specified directly.
1202 // For example, 'int a[]'.
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001203
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001204 return Importer.getToContext().getDependentSizedArrayType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001205 ToElementType, ToSizeExpr, T->getSizeModifier(),
1206 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001207}
1208
Balazs Keri3b30d652018-10-19 13:32:20 +00001209ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) {
1210 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1211 if (!ToElementTypeOrErr)
1212 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001213
Balazs Keri3b30d652018-10-19 13:32:20 +00001214 return Importer.getToContext().getVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001215 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001216 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001217}
1218
Balazs Keri3b30d652018-10-19 13:32:20 +00001219ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
1220 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1221 if (!ToElementTypeOrErr)
1222 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001223
Balazs Keri3b30d652018-10-19 13:32:20 +00001224 return Importer.getToContext().getExtVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001225 T->getNumElements());
1226}
1227
Balazs Keri3b30d652018-10-19 13:32:20 +00001228ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001229ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001230 // FIXME: What happens if we're importing a function without a prototype
Douglas Gregor96e578d2010-02-05 17:54:41 +00001231 // into C++? Should we make it variadic?
Balazs Keri3b30d652018-10-19 13:32:20 +00001232 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1233 if (!ToReturnTypeOrErr)
1234 return ToReturnTypeOrErr.takeError();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001235
Balazs Keri3b30d652018-10-19 13:32:20 +00001236 return Importer.getToContext().getFunctionNoProtoType(*ToReturnTypeOrErr,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001237 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001238}
1239
Balazs Keri3b30d652018-10-19 13:32:20 +00001240ExpectedType
1241ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
1242 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1243 if (!ToReturnTypeOrErr)
1244 return ToReturnTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001245
Douglas Gregor96e578d2010-02-05 17:54:41 +00001246 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001247 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001248 for (const auto &A : T->param_types()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001249 ExpectedType TyOrErr = import(A);
1250 if (!TyOrErr)
1251 return TyOrErr.takeError();
1252 ArgTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001253 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001254
Douglas Gregor96e578d2010-02-05 17:54:41 +00001255 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001256 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001257 for (const auto &E : T->exceptions()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001258 ExpectedType TyOrErr = import(E);
1259 if (!TyOrErr)
1260 return TyOrErr.takeError();
1261 ExceptionTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001262 }
John McCalldb40c7f2010-12-14 08:05:40 +00001263
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001264 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1265 FunctionProtoType::ExtProtoInfo ToEPI;
1266
Balazs Keri3b30d652018-10-19 13:32:20 +00001267 auto Imp = importSeq(
1268 FromEPI.ExceptionSpec.NoexceptExpr,
1269 FromEPI.ExceptionSpec.SourceDecl,
1270 FromEPI.ExceptionSpec.SourceTemplate);
1271 if (!Imp)
1272 return Imp.takeError();
1273
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001274 ToEPI.ExtInfo = FromEPI.ExtInfo;
1275 ToEPI.Variadic = FromEPI.Variadic;
1276 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1277 ToEPI.TypeQuals = FromEPI.TypeQuals;
1278 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001279 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1280 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
Balazs Keri3b30d652018-10-19 13:32:20 +00001281 std::tie(
1282 ToEPI.ExceptionSpec.NoexceptExpr,
1283 ToEPI.ExceptionSpec.SourceDecl,
1284 ToEPI.ExceptionSpec.SourceTemplate) = *Imp;
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001285
Balazs Keri3b30d652018-10-19 13:32:20 +00001286 return Importer.getToContext().getFunctionType(
1287 *ToReturnTypeOrErr, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001288}
1289
Balazs Keri3b30d652018-10-19 13:32:20 +00001290ExpectedType ASTNodeImporter::VisitUnresolvedUsingType(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001291 const UnresolvedUsingType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001292 UnresolvedUsingTypenameDecl *ToD;
1293 Decl *ToPrevD;
1294 if (auto Imp = importSeq(T->getDecl(), T->getDecl()->getPreviousDecl()))
1295 std::tie(ToD, ToPrevD) = *Imp;
1296 else
1297 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001298
Balazs Keri3b30d652018-10-19 13:32:20 +00001299 return Importer.getToContext().getTypeDeclType(
1300 ToD, cast_or_null<TypeDecl>(ToPrevD));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001301}
1302
Balazs Keri3b30d652018-10-19 13:32:20 +00001303ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) {
1304 ExpectedType ToInnerTypeOrErr = import(T->getInnerType());
1305 if (!ToInnerTypeOrErr)
1306 return ToInnerTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001307
Balazs Keri3b30d652018-10-19 13:32:20 +00001308 return Importer.getToContext().getParenType(*ToInnerTypeOrErr);
Sean Callananda6df8a2011-08-11 16:56:07 +00001309}
1310
Balazs Keri3b30d652018-10-19 13:32:20 +00001311ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
1312 Expected<TypedefNameDecl *> ToDeclOrErr = import(T->getDecl());
1313 if (!ToDeclOrErr)
1314 return ToDeclOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001315
Balazs Keri3b30d652018-10-19 13:32:20 +00001316 return Importer.getToContext().getTypeDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001317}
1318
Balazs Keri3b30d652018-10-19 13:32:20 +00001319ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
1320 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1321 if (!ToExprOrErr)
1322 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001323
Balazs Keri3b30d652018-10-19 13:32:20 +00001324 return Importer.getToContext().getTypeOfExprType(*ToExprOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001325}
1326
Balazs Keri3b30d652018-10-19 13:32:20 +00001327ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
1328 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1329 if (!ToUnderlyingTypeOrErr)
1330 return ToUnderlyingTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001331
Balazs Keri3b30d652018-10-19 13:32:20 +00001332 return Importer.getToContext().getTypeOfType(*ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001333}
1334
Balazs Keri3b30d652018-10-19 13:32:20 +00001335ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001336 // FIXME: Make sure that the "to" context supports C++0x!
Balazs Keri3b30d652018-10-19 13:32:20 +00001337 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1338 if (!ToExprOrErr)
1339 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001340
Balazs Keri3b30d652018-10-19 13:32:20 +00001341 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1342 if (!ToUnderlyingTypeOrErr)
1343 return ToUnderlyingTypeOrErr.takeError();
Douglas Gregor81495f32012-02-12 18:42:33 +00001344
Balazs Keri3b30d652018-10-19 13:32:20 +00001345 return Importer.getToContext().getDecltypeType(
1346 *ToExprOrErr, *ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001347}
1348
Balazs Keri3b30d652018-10-19 13:32:20 +00001349ExpectedType
1350ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1351 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1352 if (!ToBaseTypeOrErr)
1353 return ToBaseTypeOrErr.takeError();
Alexis Hunte852b102011-05-24 22:41:36 +00001354
Balazs Keri3b30d652018-10-19 13:32:20 +00001355 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1356 if (!ToUnderlyingTypeOrErr)
1357 return ToUnderlyingTypeOrErr.takeError();
1358
1359 return Importer.getToContext().getUnaryTransformType(
1360 *ToBaseTypeOrErr, *ToUnderlyingTypeOrErr, T->getUTTKind());
Alexis Hunte852b102011-05-24 22:41:36 +00001361}
1362
Balazs Keri3b30d652018-10-19 13:32:20 +00001363ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001364 // FIXME: Make sure that the "to" context supports C++11!
Balazs Keri3b30d652018-10-19 13:32:20 +00001365 ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType());
1366 if (!ToDeducedTypeOrErr)
1367 return ToDeducedTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001368
Balazs Keri3b30d652018-10-19 13:32:20 +00001369 return Importer.getToContext().getAutoType(*ToDeducedTypeOrErr,
1370 T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001371 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001372}
1373
Balazs Keri3b30d652018-10-19 13:32:20 +00001374ExpectedType ASTNodeImporter::VisitInjectedClassNameType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001375 const InjectedClassNameType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001376 Expected<CXXRecordDecl *> ToDeclOrErr = import(T->getDecl());
1377 if (!ToDeclOrErr)
1378 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001379
Balazs Keri3b30d652018-10-19 13:32:20 +00001380 ExpectedType ToInjTypeOrErr = import(T->getInjectedSpecializationType());
1381 if (!ToInjTypeOrErr)
1382 return ToInjTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001383
1384 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1385 // See comments in InjectedClassNameType definition for details
1386 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1387 enum {
1388 TypeAlignmentInBits = 4,
1389 TypeAlignment = 1 << TypeAlignmentInBits
1390 };
1391
1392 return QualType(new (Importer.getToContext(), TypeAlignment)
Balazs Keri3b30d652018-10-19 13:32:20 +00001393 InjectedClassNameType(*ToDeclOrErr, *ToInjTypeOrErr), 0);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001394}
1395
Balazs Keri3b30d652018-10-19 13:32:20 +00001396ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) {
1397 Expected<RecordDecl *> ToDeclOrErr = import(T->getDecl());
1398 if (!ToDeclOrErr)
1399 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001400
Balazs Keri3b30d652018-10-19 13:32:20 +00001401 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001402}
1403
Balazs Keri3b30d652018-10-19 13:32:20 +00001404ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) {
1405 Expected<EnumDecl *> ToDeclOrErr = import(T->getDecl());
1406 if (!ToDeclOrErr)
1407 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001408
Balazs Keri3b30d652018-10-19 13:32:20 +00001409 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001410}
1411
Balazs Keri3b30d652018-10-19 13:32:20 +00001412ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1413 ExpectedType ToModifiedTypeOrErr = import(T->getModifiedType());
1414 if (!ToModifiedTypeOrErr)
1415 return ToModifiedTypeOrErr.takeError();
1416 ExpectedType ToEquivalentTypeOrErr = import(T->getEquivalentType());
1417 if (!ToEquivalentTypeOrErr)
1418 return ToEquivalentTypeOrErr.takeError();
Sean Callanan72fe0852015-04-02 23:50:08 +00001419
1420 return Importer.getToContext().getAttributedType(T->getAttrKind(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001421 *ToModifiedTypeOrErr, *ToEquivalentTypeOrErr);
Sean Callanan72fe0852015-04-02 23:50:08 +00001422}
1423
Balazs Keri3b30d652018-10-19 13:32:20 +00001424ExpectedType ASTNodeImporter::VisitTemplateTypeParmType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001425 const TemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001426 Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(T->getDecl());
1427 if (!ToDeclOrErr)
1428 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001429
1430 return Importer.getToContext().getTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001431 T->getDepth(), T->getIndex(), T->isParameterPack(), *ToDeclOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001432}
1433
Balazs Keri3b30d652018-10-19 13:32:20 +00001434ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001435 const SubstTemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001436 ExpectedType ReplacedOrErr = import(QualType(T->getReplacedParameter(), 0));
1437 if (!ReplacedOrErr)
1438 return ReplacedOrErr.takeError();
1439 const TemplateTypeParmType *Replaced =
1440 cast<TemplateTypeParmType>((*ReplacedOrErr).getTypePtr());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001441
Balazs Keri3b30d652018-10-19 13:32:20 +00001442 ExpectedType ToReplacementTypeOrErr = import(T->getReplacementType());
1443 if (!ToReplacementTypeOrErr)
1444 return ToReplacementTypeOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001445
1446 return Importer.getToContext().getSubstTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001447 Replaced, (*ToReplacementTypeOrErr).getCanonicalType());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001448}
1449
Balazs Keri3b30d652018-10-19 13:32:20 +00001450ExpectedType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001451 const TemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001452 auto ToTemplateOrErr = import(T->getTemplateName());
1453 if (!ToTemplateOrErr)
1454 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001455
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001456 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00001457 if (Error Err = ImportTemplateArguments(
1458 T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1459 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001460
Douglas Gregore2e50d332010-12-01 01:36:18 +00001461 QualType ToCanonType;
1462 if (!QualType(T, 0).isCanonical()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001463 QualType FromCanonType
Douglas Gregore2e50d332010-12-01 01:36:18 +00001464 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
Balazs Keri3b30d652018-10-19 13:32:20 +00001465 if (ExpectedType TyOrErr = import(FromCanonType))
1466 ToCanonType = *TyOrErr;
1467 else
1468 return TyOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001469 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001470 return Importer.getToContext().getTemplateSpecializationType(*ToTemplateOrErr,
David Majnemer6fbeee32016-07-07 04:43:07 +00001471 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001472 ToCanonType);
1473}
1474
Balazs Keri3b30d652018-10-19 13:32:20 +00001475ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001476 // Note: the qualifier in an ElaboratedType is optional.
Balazs Keri3b30d652018-10-19 13:32:20 +00001477 auto ToQualifierOrErr = import(T->getQualifier());
1478 if (!ToQualifierOrErr)
1479 return ToQualifierOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001480
Balazs Keri3b30d652018-10-19 13:32:20 +00001481 ExpectedType ToNamedTypeOrErr = import(T->getNamedType());
1482 if (!ToNamedTypeOrErr)
1483 return ToNamedTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001484
Balazs Keri3b30d652018-10-19 13:32:20 +00001485 Expected<TagDecl *> ToOwnedTagDeclOrErr = import(T->getOwnedTagDecl());
1486 if (!ToOwnedTagDeclOrErr)
1487 return ToOwnedTagDeclOrErr.takeError();
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001488
Abramo Bagnara6150c882010-05-11 21:36:43 +00001489 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001490 *ToQualifierOrErr,
1491 *ToNamedTypeOrErr,
1492 *ToOwnedTagDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001493}
1494
Balazs Keri3b30d652018-10-19 13:32:20 +00001495ExpectedType
1496ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
1497 ExpectedType ToPatternOrErr = import(T->getPattern());
1498 if (!ToPatternOrErr)
1499 return ToPatternOrErr.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00001500
Balazs Keri3b30d652018-10-19 13:32:20 +00001501 return Importer.getToContext().getPackExpansionType(*ToPatternOrErr,
Gabor Horvath7a91c082017-11-14 11:30:38 +00001502 T->getNumExpansions());
1503}
1504
Balazs Keri3b30d652018-10-19 13:32:20 +00001505ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001506 const DependentTemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001507 auto ToQualifierOrErr = import(T->getQualifier());
1508 if (!ToQualifierOrErr)
1509 return ToQualifierOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001510
Balazs Keri3b30d652018-10-19 13:32:20 +00001511 IdentifierInfo *ToName = Importer.Import(T->getIdentifier());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001512
1513 SmallVector<TemplateArgument, 2> ToPack;
1514 ToPack.reserve(T->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00001515 if (Error Err = ImportTemplateArguments(
1516 T->getArgs(), T->getNumArgs(), ToPack))
1517 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001518
1519 return Importer.getToContext().getDependentTemplateSpecializationType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001520 T->getKeyword(), *ToQualifierOrErr, ToName, ToPack);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001521}
1522
Balazs Keri3b30d652018-10-19 13:32:20 +00001523ExpectedType
1524ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
1525 auto ToQualifierOrErr = import(T->getQualifier());
1526 if (!ToQualifierOrErr)
1527 return ToQualifierOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00001528
1529 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
Peter Szecsice7f3182018-05-07 12:08:27 +00001530
Balazs Keri3b30d652018-10-19 13:32:20 +00001531 QualType Canon;
1532 if (T != T->getCanonicalTypeInternal().getTypePtr()) {
1533 if (ExpectedType TyOrErr = import(T->getCanonicalTypeInternal()))
1534 Canon = (*TyOrErr).getCanonicalType();
1535 else
1536 return TyOrErr.takeError();
1537 }
Peter Szecsice7f3182018-05-07 12:08:27 +00001538
Balazs Keri3b30d652018-10-19 13:32:20 +00001539 return Importer.getToContext().getDependentNameType(T->getKeyword(),
1540 *ToQualifierOrErr,
Peter Szecsice7f3182018-05-07 12:08:27 +00001541 Name, Canon);
1542}
1543
Balazs Keri3b30d652018-10-19 13:32:20 +00001544ExpectedType
1545ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
1546 Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(T->getDecl());
1547 if (!ToDeclOrErr)
1548 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001549
Balazs Keri3b30d652018-10-19 13:32:20 +00001550 return Importer.getToContext().getObjCInterfaceType(*ToDeclOrErr);
John McCall8b07ec22010-05-15 11:32:37 +00001551}
1552
Balazs Keri3b30d652018-10-19 13:32:20 +00001553ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1554 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1555 if (!ToBaseTypeOrErr)
1556 return ToBaseTypeOrErr.takeError();
John McCall8b07ec22010-05-15 11:32:37 +00001557
Douglas Gregore9d95f12015-07-07 03:57:35 +00001558 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001559 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001560 if (ExpectedType TyOrErr = import(TypeArg))
1561 TypeArgs.push_back(*TyOrErr);
1562 else
1563 return TyOrErr.takeError();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001564 }
1565
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001566 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001567 for (auto *P : T->quals()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001568 if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(P))
1569 Protocols.push_back(*ProtocolOrErr);
1570 else
1571 return ProtocolOrErr.takeError();
1572
Douglas Gregor96e578d2010-02-05 17:54:41 +00001573 }
1574
Balazs Keri3b30d652018-10-19 13:32:20 +00001575 return Importer.getToContext().getObjCObjectType(*ToBaseTypeOrErr, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001576 Protocols,
1577 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001578}
1579
Balazs Keri3b30d652018-10-19 13:32:20 +00001580ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001581ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001582 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1583 if (!ToPointeeTypeOrErr)
1584 return ToPointeeTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001585
Balazs Keri3b30d652018-10-19 13:32:20 +00001586 return Importer.getToContext().getObjCObjectPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001587}
1588
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001589//----------------------------------------------------------------------------
1590// Import Declarations
1591//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00001592Error ASTNodeImporter::ImportDeclParts(
1593 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
1594 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) {
Gabor Marton6e1510c2018-07-12 11:50:21 +00001595 // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop.
1596 // example: int struct_in_proto(struct data_t{int a;int b;} *d);
Gabor Marton25234fd2019-12-12 17:13:35 +01001597 // FIXME: We could support these constructs by importing a different type of
1598 // this parameter and by importing the original type of the parameter only
1599 // after the FunctionDecl is created. See
1600 // VisitFunctionDecl::UsedDifferentProtoType.
Gabor Marton6e1510c2018-07-12 11:50:21 +00001601 DeclContext *OrigDC = D->getDeclContext();
1602 FunctionDecl *FunDecl;
1603 if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) &&
1604 FunDecl->hasBody()) {
Gabor Martonfe68e292018-08-06 14:38:37 +00001605 auto getLeafPointeeType = [](const Type *T) {
1606 while (T->isPointerType() || T->isArrayType()) {
1607 T = T->getPointeeOrArrayElementType();
1608 }
1609 return T;
1610 };
1611 for (const ParmVarDecl *P : FunDecl->parameters()) {
1612 const Type *LeafT =
1613 getLeafPointeeType(P->getType().getCanonicalType().getTypePtr());
1614 auto *RT = dyn_cast<RecordType>(LeafT);
1615 if (RT && RT->getDecl() == D) {
1616 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1617 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00001618 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Gabor Martonfe68e292018-08-06 14:38:37 +00001619 }
Gabor Marton6e1510c2018-07-12 11:50:21 +00001620 }
1621 }
1622
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001623 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001624 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
1625 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001626
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001627 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001628 if (Error Err = importInto(Name, D->getDeclName()))
1629 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001630
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001631 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001632 if (Error Err = importInto(Loc, D->getLocation()))
1633 return Err;
1634
Sean Callanan59721b32015-04-28 18:41:46 +00001635 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Gabor Martonbe77a982018-12-12 11:22:55 +00001636 if (ToD)
1637 if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(D, ToD))
1638 return Err;
Balazs Keri3b30d652018-10-19 13:32:20 +00001639
1640 return Error::success();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001641}
1642
Balazs Keri3b30d652018-10-19 13:32:20 +00001643Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001644 if (!FromD)
Balazs Keri3b30d652018-10-19 13:32:20 +00001645 return Error::success();
Fangrui Song6907ce22018-07-30 19:24:48 +00001646
Balazs Keri3b30d652018-10-19 13:32:20 +00001647 if (!ToD)
1648 if (Error Err = importInto(ToD, FromD))
1649 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001650
Balazs Keri3b30d652018-10-19 13:32:20 +00001651 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1652 if (RecordDecl *ToRecord = cast<RecordDecl>(ToD)) {
1653 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() &&
1654 !ToRecord->getDefinition()) {
1655 if (Error Err = ImportDefinition(FromRecord, ToRecord))
1656 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001657 }
1658 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001659 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001660 }
1661
Balazs Keri3b30d652018-10-19 13:32:20 +00001662 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1663 if (EnumDecl *ToEnum = cast<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001664 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001665 if (Error Err = ImportDefinition(FromEnum, ToEnum))
1666 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001667 }
1668 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001669 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001670 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001671
1672 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001673}
1674
Balazs Keri3b30d652018-10-19 13:32:20 +00001675Error
1676ASTNodeImporter::ImportDeclarationNameLoc(
1677 const DeclarationNameInfo &From, DeclarationNameInfo& To) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001678 // NOTE: To.Name and To.Loc are already imported.
1679 // We only have to import To.LocInfo.
1680 switch (To.getName().getNameKind()) {
1681 case DeclarationName::Identifier:
1682 case DeclarationName::ObjCZeroArgSelector:
1683 case DeclarationName::ObjCOneArgSelector:
1684 case DeclarationName::ObjCMultiArgSelector:
1685 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001686 case DeclarationName::CXXDeductionGuideName:
Balazs Keri3b30d652018-10-19 13:32:20 +00001687 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001688
1689 case DeclarationName::CXXOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001690 if (auto ToRangeOrErr = import(From.getCXXOperatorNameRange()))
1691 To.setCXXOperatorNameRange(*ToRangeOrErr);
1692 else
1693 return ToRangeOrErr.takeError();
1694 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001695 }
1696 case DeclarationName::CXXLiteralOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001697 if (ExpectedSLoc LocOrErr = import(From.getCXXLiteralOperatorNameLoc()))
1698 To.setCXXLiteralOperatorNameLoc(*LocOrErr);
1699 else
1700 return LocOrErr.takeError();
1701 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001702 }
1703 case DeclarationName::CXXConstructorName:
1704 case DeclarationName::CXXDestructorName:
1705 case DeclarationName::CXXConversionFunctionName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001706 if (auto ToTInfoOrErr = import(From.getNamedTypeInfo()))
1707 To.setNamedTypeInfo(*ToTInfoOrErr);
1708 else
1709 return ToTInfoOrErr.takeError();
1710 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001711 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001712 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001713 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001714}
1715
Balazs Keri3b30d652018-10-19 13:32:20 +00001716Error
1717ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001718 if (Importer.isMinimalImport() && !ForceImport) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001719 auto ToDCOrErr = Importer.ImportContext(FromDC);
1720 return ToDCOrErr.takeError();
1721 }
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001722
1723 // We use strict error handling in case of records and enums, but not
1724 // with e.g. namespaces.
1725 //
1726 // FIXME Clients of the ASTImporter should be able to choose an
1727 // appropriate error handling strategy for their needs. For instance,
1728 // they may not want to mark an entire namespace as erroneous merely
1729 // because there is an ODR error with two typedefs. As another example,
1730 // the client may allow EnumConstantDecls with same names but with
1731 // different values in two distinct translation units.
1732 bool AccumulateChildErrors = isa<TagDecl>(FromDC);
1733
1734 Error ChildErrors = Error::success();
Balazs Keri3b30d652018-10-19 13:32:20 +00001735 for (auto *From : FromDC->decls()) {
1736 ExpectedDecl ImportedOrErr = import(From);
shafik6a7df3a2019-12-19 11:14:39 -08001737
1738 // If we are in the process of ImportDefinition(...) for a RecordDecl we
1739 // want to make sure that we are also completing each FieldDecl. There
1740 // are currently cases where this does not happen and this is correctness
1741 // fix since operations such as code generation will expect this to be so.
1742 if (ImportedOrErr) {
1743 FieldDecl *FieldFrom = dyn_cast_or_null<FieldDecl>(From);
1744 Decl *ImportedDecl = (Decl*)*ImportedOrErr;
1745 FieldDecl *FieldTo = dyn_cast_or_null<FieldDecl>(ImportedDecl);
1746 if (FieldFrom && FieldTo) {
1747 const RecordType *RecordFrom = FieldFrom->getType()->getAs<RecordType>();
1748 const RecordType *RecordTo = FieldTo->getType()->getAs<RecordType>();
1749 if (RecordFrom && RecordTo) {
1750 RecordDecl *FromRecordDecl = RecordFrom->getDecl();
1751 RecordDecl *ToRecordDecl = RecordTo->getDecl();
1752
1753 if (FromRecordDecl->isCompleteDefinition() &&
1754 !ToRecordDecl->isCompleteDefinition()) {
1755 Error Err = ImportDefinition(FromRecordDecl, ToRecordDecl);
1756
1757 if (Err && AccumulateChildErrors)
1758 ChildErrors = joinErrors(std::move(ChildErrors), std::move(Err));
1759 else
1760 consumeError(std::move(Err));
1761 }
1762 }
1763 }
1764 } else {
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001765 if (AccumulateChildErrors)
1766 ChildErrors =
1767 joinErrors(std::move(ChildErrors), ImportedOrErr.takeError());
1768 else
1769 consumeError(ImportedOrErr.takeError());
1770 }
Douglas Gregor0a791672011-01-18 03:11:38 +00001771 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001772
Gabor Marton48b16e12019-07-25 09:07:17 +00001773 // We reorder declarations in RecordDecls because they may have another order
1774 // in the "to" context than they have in the "from" context. This may happen
1775 // e.g when we import a class like this:
1776 // struct declToImport {
1777 // int a = c + b;
1778 // int b = 1;
1779 // int c = 2;
1780 // };
1781 // During the import of `a` we import first the dependencies in sequence,
1782 // thus the order would be `c`, `b`, `a`. We will get the normal order by
1783 // first removing the already imported members and then adding them in the
1784 // order as they apper in the "from" context.
1785 //
1786 // Keeping field order is vital because it determines structure layout.
1787 //
1788 // Here and below, we cannot call field_begin() method and its callers on
1789 // ToDC if it has an external storage. Calling field_begin() will
1790 // automatically load all the fields by calling
1791 // LoadFieldsFromExternalStorage(). LoadFieldsFromExternalStorage() would
1792 // call ASTImporter::Import(). This is because the ExternalASTSource
1793 // interface in LLDB is implemented by the means of the ASTImporter. However,
1794 // calling an import at this point would result in an uncontrolled import, we
1795 // must avoid that.
1796 const auto *FromRD = dyn_cast<RecordDecl>(FromDC);
1797 if (!FromRD)
1798 return ChildErrors;
1799
1800 auto ToDCOrErr = Importer.ImportContext(FromDC);
1801 if (!ToDCOrErr) {
1802 consumeError(std::move(ChildErrors));
1803 return ToDCOrErr.takeError();
1804 }
1805
1806 DeclContext *ToDC = *ToDCOrErr;
1807 // Remove all declarations, which may be in wrong order in the
1808 // lexical DeclContext and then add them in the proper order.
1809 for (auto *D : FromRD->decls()) {
Balazs Keri6e086692019-09-02 07:17:01 +00001810 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<FriendDecl>(D)) {
Gabor Marton48b16e12019-07-25 09:07:17 +00001811 assert(D && "DC contains a null decl");
1812 Decl *ToD = Importer.GetAlreadyImportedOrNull(D);
1813 // Remove only the decls which we successfully imported.
1814 if (ToD) {
1815 assert(ToDC == ToD->getLexicalDeclContext() && ToDC->containsDecl(ToD));
1816 // Remove the decl from its wrong place in the linked list.
1817 ToDC->removeDecl(ToD);
1818 // Add the decl to the end of the linked list.
1819 // This time it will be at the proper place because the enclosing for
1820 // loop iterates in the original (good) order of the decls.
1821 ToDC->addDeclInternal(ToD);
1822 }
1823 }
1824 }
1825
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001826 return ChildErrors;
Douglas Gregor968d6332010-02-21 18:24:45 +00001827}
1828
Balazs Keri3b30d652018-10-19 13:32:20 +00001829Error ASTNodeImporter::ImportDeclContext(
1830 Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) {
1831 auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext());
1832 if (!ToDCOrErr)
1833 return ToDCOrErr.takeError();
1834 ToDC = *ToDCOrErr;
1835
1836 if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) {
1837 auto ToLexicalDCOrErr = Importer.ImportContext(
1838 FromD->getLexicalDeclContext());
1839 if (!ToLexicalDCOrErr)
1840 return ToLexicalDCOrErr.takeError();
1841 ToLexicalDC = *ToLexicalDCOrErr;
1842 } else
1843 ToLexicalDC = ToDC;
1844
1845 return Error::success();
1846}
1847
1848Error ASTNodeImporter::ImportImplicitMethods(
Balazs Keri1d20cc22018-07-16 12:16:39 +00001849 const CXXRecordDecl *From, CXXRecordDecl *To) {
1850 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1851 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001852
Balazs Keri1d20cc22018-07-16 12:16:39 +00001853 for (CXXMethodDecl *FromM : From->methods())
Balazs Keri3b30d652018-10-19 13:32:20 +00001854 if (FromM->isImplicit()) {
1855 Expected<CXXMethodDecl *> ToMOrErr = import(FromM);
1856 if (!ToMOrErr)
1857 return ToMOrErr.takeError();
1858 }
1859
1860 return Error::success();
Balazs Keri1d20cc22018-07-16 12:16:39 +00001861}
1862
Balazs Keri3b30d652018-10-19 13:32:20 +00001863static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1864 ASTImporter &Importer) {
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001865 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00001866 if (ExpectedDecl ToTypedefOrErr = Importer.Import(FromTypedef))
Balazs Keri57949eb2019-03-25 09:16:39 +00001867 To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr));
1868 else
1869 return ToTypedefOrErr.takeError();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001870 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001871 return Error::success();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001872}
1873
Balazs Keri3b30d652018-10-19 13:32:20 +00001874Error ASTNodeImporter::ImportDefinition(
1875 RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) {
Gabor Martonaefcf512019-07-17 13:47:46 +00001876 auto DefinitionCompleter = [To]() {
1877 // There are cases in LLDB when we first import a class without its
1878 // members. The class will have DefinitionData, but no members. Then,
1879 // importDefinition is called from LLDB, which tries to get the members, so
1880 // when we get here, the class already has the DefinitionData set, so we
1881 // must unset the CompleteDefinition here to be able to complete again the
1882 // definition.
1883 To->setCompleteDefinition(false);
1884 To->completeDefinition();
1885 };
1886
Douglas Gregor95d82832012-01-24 18:36:04 +00001887 if (To->getDefinition() || To->isBeingDefined()) {
Gabor Martone73805f2019-07-08 12:49:13 +00001888 if (Kind == IDK_Everything ||
1889 // In case of lambdas, the class already has a definition ptr set, but
1890 // the contained decls are not imported yet. Also, isBeingDefined was
1891 // set in CXXRecordDecl::CreateLambda. We must import the contained
1892 // decls here and finish the definition.
1893 (To->isLambda() && shouldForceImportDeclContext(Kind))) {
1894 Error Result = ImportDeclContext(From, /*ForceImport=*/true);
1895 // Finish the definition of the lambda, set isBeingDefined to false.
1896 if (To->isLambda())
Gabor Martonaefcf512019-07-17 13:47:46 +00001897 DefinitionCompleter();
Gabor Martone73805f2019-07-08 12:49:13 +00001898 return Result;
1899 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001900
Balazs Keri3b30d652018-10-19 13:32:20 +00001901 return Error::success();
Douglas Gregor95d82832012-01-24 18:36:04 +00001902 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001903
Douglas Gregore2e50d332010-12-01 01:36:18 +00001904 To->startDefinition();
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001905 // Complete the definition even if error is returned.
1906 // The RecordDecl may be already part of the AST so it is better to
1907 // have it in complete state even if something is wrong with it.
Gabor Martonaefcf512019-07-17 13:47:46 +00001908 auto DefinitionCompleterScopeExit =
1909 llvm::make_scope_exit(DefinitionCompleter);
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001910
Balazs Keri3b30d652018-10-19 13:32:20 +00001911 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1912 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001913
Douglas Gregore2e50d332010-12-01 01:36:18 +00001914 // Add base classes.
Gabor Marton17d39672018-11-26 15:54:08 +00001915 auto *ToCXX = dyn_cast<CXXRecordDecl>(To);
1916 auto *FromCXX = dyn_cast<CXXRecordDecl>(From);
1917 if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) {
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001918
1919 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1920 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
Richard Smith91aeacc2019-10-11 00:29:04 +00001921
1922 #define FIELD(Name, Width, Merge) \
1923 ToData.Name = FromData.Name;
1924 #include "clang/AST/CXXRecordDeclDefinitionBits.def"
Richard Smith561fb152012-02-25 07:33:38 +00001925
Shafik Yaghmour16b90732019-04-26 18:51:28 +00001926 // Copy over the data stored in RecordDeclBits
1927 ToCXX->setArgPassingRestrictions(FromCXX->getArgPassingRestrictions());
1928
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001929 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001930 for (const auto &Base1 : FromCXX->bases()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001931 ExpectedType TyOrErr = import(Base1.getType());
1932 if (!TyOrErr)
1933 return TyOrErr.takeError();
Douglas Gregor752a5952011-01-03 22:36:02 +00001934
1935 SourceLocation EllipsisLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00001936 if (Base1.isPackExpansion()) {
1937 if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc()))
1938 EllipsisLoc = *LocOrErr;
1939 else
1940 return LocOrErr.takeError();
1941 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001942
1943 // Ensure that we have a definition for the base.
Balazs Keri3b30d652018-10-19 13:32:20 +00001944 if (Error Err =
1945 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()))
1946 return Err;
1947
1948 auto RangeOrErr = import(Base1.getSourceRange());
1949 if (!RangeOrErr)
1950 return RangeOrErr.takeError();
1951
1952 auto TSIOrErr = import(Base1.getTypeSourceInfo());
1953 if (!TSIOrErr)
1954 return TSIOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001955
Douglas Gregore2e50d332010-12-01 01:36:18 +00001956 Bases.push_back(
Balazs Keri3b30d652018-10-19 13:32:20 +00001957 new (Importer.getToContext()) CXXBaseSpecifier(
1958 *RangeOrErr,
1959 Base1.isVirtual(),
1960 Base1.isBaseOfClass(),
1961 Base1.getAccessSpecifierAsWritten(),
1962 *TSIOrErr,
1963 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001964 }
1965 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001966 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001967 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001968
Douglas Gregor2e15c842012-02-01 21:00:38 +00001969 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001970 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1971 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001972
Balazs Keri3b30d652018-10-19 13:32:20 +00001973 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001974}
1975
Balazs Keri3b30d652018-10-19 13:32:20 +00001976Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) {
Sean Callanan59721b32015-04-28 18:41:46 +00001977 if (To->getAnyInitializer())
Balazs Keri3b30d652018-10-19 13:32:20 +00001978 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001979
Gabor Martonac3a5d62018-09-17 12:04:52 +00001980 Expr *FromInit = From->getInit();
1981 if (!FromInit)
Balazs Keri3b30d652018-10-19 13:32:20 +00001982 return Error::success();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001983
Balazs Keri3b30d652018-10-19 13:32:20 +00001984 ExpectedExpr ToInitOrErr = import(FromInit);
1985 if (!ToInitOrErr)
1986 return ToInitOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001987
Balazs Keri3b30d652018-10-19 13:32:20 +00001988 To->setInit(*ToInitOrErr);
Gabor Martonac3a5d62018-09-17 12:04:52 +00001989 if (From->isInitKnownICE()) {
1990 EvaluatedStmt *Eval = To->ensureEvaluatedStmt();
1991 Eval->CheckedICE = true;
1992 Eval->IsICE = From->isInitICE();
1993 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001994
1995 // FIXME: Other bits to merge?
Balazs Keri3b30d652018-10-19 13:32:20 +00001996 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001997}
1998
Balazs Keri3b30d652018-10-19 13:32:20 +00001999Error ASTNodeImporter::ImportDefinition(
2000 EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00002001 if (To->getDefinition() || To->isBeingDefined()) {
2002 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00002003 return ImportDeclContext(From, /*ForceImport=*/true);
2004 return Error::success();
Douglas Gregor2e15c842012-02-01 21:00:38 +00002005 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002006
Douglas Gregord451ea92011-07-29 23:31:30 +00002007 To->startDefinition();
2008
Balazs Keri3b30d652018-10-19 13:32:20 +00002009 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
2010 return Err;
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00002011
Balazs Keri3b30d652018-10-19 13:32:20 +00002012 ExpectedType ToTypeOrErr =
2013 import(Importer.getFromContext().getTypeDeclType(From));
2014 if (!ToTypeOrErr)
2015 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00002016
Balazs Keri3b30d652018-10-19 13:32:20 +00002017 ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType());
2018 if (!ToPromotionTypeOrErr)
2019 return ToPromotionTypeOrErr.takeError();
Douglas Gregor2e15c842012-02-01 21:00:38 +00002020
2021 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00002022 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
2023 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00002024
Douglas Gregord451ea92011-07-29 23:31:30 +00002025 // FIXME: we might need to merge the number of positive or negative bits
2026 // if the enumerator lists don't match.
Balazs Keri3b30d652018-10-19 13:32:20 +00002027 To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr,
Douglas Gregord451ea92011-07-29 23:31:30 +00002028 From->getNumPositiveBits(),
2029 From->getNumNegativeBits());
Balazs Keri3b30d652018-10-19 13:32:20 +00002030 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00002031}
2032
Balazs Keri3b30d652018-10-19 13:32:20 +00002033Error ASTNodeImporter::ImportTemplateArguments(
2034 const TemplateArgument *FromArgs, unsigned NumFromArgs,
2035 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002036 for (unsigned I = 0; I != NumFromArgs; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002037 if (auto ToOrErr = import(FromArgs[I]))
2038 ToArgs.push_back(*ToOrErr);
2039 else
2040 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00002041 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002042
Balazs Keri3b30d652018-10-19 13:32:20 +00002043 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00002044}
2045
Balazs Keri3b30d652018-10-19 13:32:20 +00002046// FIXME: Do not forget to remove this and use only 'import'.
2047Expected<TemplateArgument>
2048ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2049 return import(From);
2050}
2051
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002052template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +00002053Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002054 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
2055 for (const auto &FromLoc : Container) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002056 if (auto ToLocOrErr = import(FromLoc))
2057 ToTAInfo.addArgument(*ToLocOrErr);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002058 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002059 return ToLocOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002060 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002061 return Error::success();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002062}
2063
Gabor Marton26f72a92018-07-12 09:42:05 +00002064static StructuralEquivalenceKind
2065getStructuralEquivalenceKind(const ASTImporter &Importer) {
2066 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
2067 : StructuralEquivalenceKind::Default;
2068}
2069
Gabor Marton950fb572018-07-17 12:39:27 +00002070bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
2071 StructuralEquivalenceContext Ctx(
2072 Importer.getFromContext(), Importer.getToContext(),
2073 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2074 false, Complain);
2075 return Ctx.IsEquivalent(From, To);
2076}
2077
2078bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002079 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002080 // Eliminate a potential failure point where we attempt to re-import
2081 // something we're trying to import while completing ToRecord.
2082 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2083 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002084 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00002085 if (ToOriginRecord)
2086 ToRecord = ToOriginRecord;
2087 }
2088
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002089 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002090 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002091 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002092 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002093 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00002094 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002095}
2096
Larisse Voufo39a1e502013-08-06 01:03:05 +00002097bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2098 bool Complain) {
2099 StructuralEquivalenceContext Ctx(
2100 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002101 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2102 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00002103 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002104}
2105
Douglas Gregor98c10182010-02-12 22:17:39 +00002106bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00002107 // Eliminate a potential failure point where we attempt to re-import
Raphael Isemannfa26c202019-04-09 14:18:23 +00002108 // something we're trying to import while completing ToEnum.
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00002109 if (Decl *ToOrigin = Importer.GetOriginalDecl(ToEnum))
2110 if (auto *ToOriginEnum = dyn_cast<EnumDecl>(ToOrigin))
2111 ToEnum = ToOriginEnum;
2112
Gabor Marton26f72a92018-07-12 09:42:05 +00002113 StructuralEquivalenceContext Ctx(
2114 Importer.getFromContext(), Importer.getToContext(),
2115 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002116 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002117}
2118
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002119bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
2120 FunctionTemplateDecl *To) {
2121 StructuralEquivalenceContext Ctx(
2122 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002123 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2124 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002125 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002126}
2127
Balazs Keric7797c42018-07-11 09:37:24 +00002128bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
2129 StructuralEquivalenceContext Ctx(
2130 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002131 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2132 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002133 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00002134}
2135
Douglas Gregor91155082012-11-14 22:29:20 +00002136bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002137 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00002138 const llvm::APSInt &FromVal = FromEC->getInitVal();
2139 const llvm::APSInt &ToVal = ToEC->getInitVal();
2140
2141 return FromVal.isSigned() == ToVal.isSigned() &&
2142 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2143 FromVal == ToVal;
2144}
2145
2146bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002147 ClassTemplateDecl *To) {
2148 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2149 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002150 Importer.getNonEquivalentDecls(),
2151 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002152 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00002153}
2154
Larisse Voufo39a1e502013-08-06 01:03:05 +00002155bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2156 VarTemplateDecl *To) {
2157 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2158 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002159 Importer.getNonEquivalentDecls(),
2160 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002161 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002162}
2163
Balazs Keri3b30d652018-10-19 13:32:20 +00002164ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002165 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002166 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00002167 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +00002168}
2169
Balazs Keri3b30d652018-10-19 13:32:20 +00002170ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) {
2171 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2172 << D->getDeclKindName();
2173 return make_error<ImportError>(ImportError::UnsupportedConstruct);
2174}
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002175
Balazs Keri3b30d652018-10-19 13:32:20 +00002176ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
2177 // Import the context of this declaration.
2178 DeclContext *DC, *LexicalDC;
2179 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
2180 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002181
2182 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002183 ExpectedSLoc LocOrErr = import(D->getLocation());
2184 if (!LocOrErr)
2185 return LocOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002186
Gabor Marton26f72a92018-07-12 09:42:05 +00002187 EmptyDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002188 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002189 return ToD;
2190
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002191 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002192 LexicalDC->addDeclInternal(ToD);
2193 return ToD;
2194}
2195
Balazs Keri3b30d652018-10-19 13:32:20 +00002196ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002197 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00002198 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00002199
Gabor Marton26f72a92018-07-12 09:42:05 +00002200 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002201
Sean Callanan65198272011-11-17 23:20:56 +00002202 return ToD;
2203}
2204
Balazs Keri3b30d652018-10-19 13:32:20 +00002205ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2206 ExpectedSLoc LocOrErr = import(D->getLocation());
2207 if (!LocOrErr)
2208 return LocOrErr.takeError();
2209 auto ColonLocOrErr = import(D->getColonLoc());
2210 if (!ColonLocOrErr)
2211 return ColonLocOrErr.takeError();
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002212
2213 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002214 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2215 if (!DCOrErr)
2216 return DCOrErr.takeError();
2217 DeclContext *DC = *DCOrErr;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002218
Gabor Marton26f72a92018-07-12 09:42:05 +00002219 AccessSpecDecl *ToD;
2220 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002221 DC, *LocOrErr, *ColonLocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002222 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002223
2224 // Lexical DeclContext and Semantic DeclContext
2225 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00002226 ToD->setLexicalDeclContext(DC);
2227 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002228
Gabor Marton26f72a92018-07-12 09:42:05 +00002229 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002230}
2231
Balazs Keri3b30d652018-10-19 13:32:20 +00002232ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2233 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2234 if (!DCOrErr)
2235 return DCOrErr.takeError();
2236 DeclContext *DC = *DCOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002237 DeclContext *LexicalDC = DC;
2238
Balazs Keri3b30d652018-10-19 13:32:20 +00002239 SourceLocation ToLocation, ToRParenLoc;
2240 Expr *ToAssertExpr;
2241 StringLiteral *ToMessage;
2242 if (auto Imp = importSeq(
2243 D->getLocation(), D->getAssertExpr(), D->getMessage(), D->getRParenLoc()))
2244 std::tie(ToLocation, ToAssertExpr, ToMessage, ToRParenLoc) = *Imp;
2245 else
2246 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002247
Gabor Marton26f72a92018-07-12 09:42:05 +00002248 StaticAssertDecl *ToD;
2249 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002250 ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage,
2251 ToRParenLoc, D->isFailed()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002252 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002253
2254 ToD->setLexicalDeclContext(LexicalDC);
2255 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002256 return ToD;
2257}
2258
Balazs Keri3b30d652018-10-19 13:32:20 +00002259ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002260 // Import the major distinguishing characteristics of this namespace.
2261 DeclContext *DC, *LexicalDC;
2262 DeclarationName Name;
2263 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002264 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002265 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2266 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002267 if (ToD)
2268 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002269
2270 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002271 if (!Name) {
2272 // This is an anonymous namespace. Adopt an existing anonymous
2273 // namespace if we can.
2274 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002275 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002276 MergeWithNamespace = TU->getAnonymousNamespace();
2277 else
2278 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2279 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002280 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002281 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002282 for (auto *FoundDecl : FoundDecls) {
2283 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002284 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002285
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002286 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002287 MergeWithNamespace = FoundNS;
2288 ConflictingDecls.clear();
2289 break;
2290 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002291
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002292 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002293 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002294
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002295 if (!ConflictingDecls.empty()) {
Gabor Martonf035b752019-08-27 11:36:10 +00002296 ExpectedName NameOrErr = Importer.HandleNameConflict(
2297 Name, DC, Decl::IDNS_Namespace, ConflictingDecls.data(),
2298 ConflictingDecls.size());
2299 if (NameOrErr)
2300 Name = NameOrErr.get();
2301 else
2302 return NameOrErr.takeError();
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002303 }
2304 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002305
Balazs Keri3b30d652018-10-19 13:32:20 +00002306 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2307 if (!BeginLocOrErr)
2308 return BeginLocOrErr.takeError();
Balázs Kéria9f10eb2019-12-05 16:21:21 +01002309 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
2310 if (!RBraceLocOrErr)
2311 return RBraceLocOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +00002312
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002313 // Create the "to" namespace, if needed.
2314 NamespaceDecl *ToNamespace = MergeWithNamespace;
2315 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002316 if (GetImportedOrCreateDecl(
2317 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002318 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002319 /*PrevDecl=*/nullptr))
2320 return ToNamespace;
Balázs Kéria9f10eb2019-12-05 16:21:21 +01002321 ToNamespace->setRBraceLoc(*RBraceLocOrErr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002322 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002323 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002324
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002325 // If this is an anonymous namespace, register it as the anonymous
2326 // namespace within its context.
2327 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002328 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002329 TU->setAnonymousNamespace(ToNamespace);
2330 else
2331 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2332 }
2333 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002334 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002335
Balazs Keri3b30d652018-10-19 13:32:20 +00002336 if (Error Err = ImportDeclContext(D))
2337 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00002338
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002339 return ToNamespace;
2340}
2341
Balazs Keri3b30d652018-10-19 13:32:20 +00002342ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002343 // Import the major distinguishing characteristics of this namespace.
2344 DeclContext *DC, *LexicalDC;
2345 DeclarationName Name;
2346 SourceLocation Loc;
2347 NamedDecl *LookupD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002348 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
2349 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002350 if (LookupD)
2351 return LookupD;
2352
2353 // NOTE: No conflict resolution is done for namespace aliases now.
2354
Balazs Keri3b30d652018-10-19 13:32:20 +00002355 SourceLocation ToNamespaceLoc, ToAliasLoc, ToTargetNameLoc;
2356 NestedNameSpecifierLoc ToQualifierLoc;
2357 NamespaceDecl *ToNamespace;
2358 if (auto Imp = importSeq(
2359 D->getNamespaceLoc(), D->getAliasLoc(), D->getQualifierLoc(),
2360 D->getTargetNameLoc(), D->getNamespace()))
2361 std::tie(
2362 ToNamespaceLoc, ToAliasLoc, ToQualifierLoc, ToTargetNameLoc,
2363 ToNamespace) = *Imp;
2364 else
2365 return Imp.takeError();
2366 IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002367
Gabor Marton26f72a92018-07-12 09:42:05 +00002368 NamespaceAliasDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002369 if (GetImportedOrCreateDecl(
2370 ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc,
2371 ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace))
Gabor Marton26f72a92018-07-12 09:42:05 +00002372 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002373
2374 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002375 LexicalDC->addDeclInternal(ToD);
2376
2377 return ToD;
2378}
2379
Balazs Keri3b30d652018-10-19 13:32:20 +00002380ExpectedDecl
2381ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002382 // Import the major distinguishing characteristics of this typedef.
2383 DeclContext *DC, *LexicalDC;
2384 DeclarationName Name;
2385 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002386 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002387 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2388 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002389 if (ToD)
2390 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002391
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002392 // If this typedef is not in block scope, determine whether we've
2393 // seen a typedef with the same name (that we can merge with) or any
2394 // other entity by that name (which name lookup could conflict with).
Balazs Keric86d47b2019-09-04 14:12:18 +00002395 // Note: Repeated typedefs are not valid in C99:
2396 // 'typedef int T; typedef int T;' is invalid
2397 // We do not care about this now.
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002398 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002399 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002400 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002401 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002402 for (auto *FoundDecl : FoundDecls) {
2403 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002404 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002405 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Balazs Keric86d47b2019-09-04 14:12:18 +00002406 if (!hasSameVisibilityContext(FoundTypedef, D))
2407 continue;
2408
Gabor Martonb93baf62018-11-27 09:51:36 +00002409 QualType FromUT = D->getUnderlyingType();
2410 QualType FoundUT = FoundTypedef->getUnderlyingType();
2411 if (Importer.IsStructurallyEquivalent(FromUT, FoundUT)) {
2412 // If the "From" context has a complete underlying type but we
2413 // already have a complete underlying type then return with that.
2414 if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType())
Balazs Keri3b30d652018-10-19 13:32:20 +00002415 return Importer.MapImported(D, FoundTypedef);
Gabor Martonf035b752019-08-27 11:36:10 +00002416 // FIXME Handle redecl chain. When you do that make consistent changes
2417 // in ASTImporterLookupTable too.
2418 } else {
2419 ConflictingDecls.push_back(FoundDecl);
Gabor Martonb93baf62018-11-27 09:51:36 +00002420 }
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002421 }
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002422 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002423
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002424 if (!ConflictingDecls.empty()) {
Gabor Martonf035b752019-08-27 11:36:10 +00002425 ExpectedName NameOrErr = Importer.HandleNameConflict(
2426 Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size());
2427 if (NameOrErr)
2428 Name = NameOrErr.get();
2429 else
2430 return NameOrErr.takeError();
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002431 }
2432 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002433
Balazs Keri3b30d652018-10-19 13:32:20 +00002434 QualType ToUnderlyingType;
2435 TypeSourceInfo *ToTypeSourceInfo;
2436 SourceLocation ToBeginLoc;
2437 if (auto Imp = importSeq(
2438 D->getUnderlyingType(), D->getTypeSourceInfo(), D->getBeginLoc()))
2439 std::tie(ToUnderlyingType, ToTypeSourceInfo, ToBeginLoc) = *Imp;
2440 else
2441 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002442
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002443 // Create the new typedef node.
Balazs Keri3b30d652018-10-19 13:32:20 +00002444 // FIXME: ToUnderlyingType is not used.
Richard Smithdda56e42011-04-15 14:24:37 +00002445 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00002446 if (IsAlias) {
2447 if (GetImportedOrCreateDecl<TypeAliasDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002448 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2449 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002450 return ToTypedef;
2451 } else if (GetImportedOrCreateDecl<TypedefDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002452 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2453 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002454 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002455
Douglas Gregordd483172010-02-22 17:42:47 +00002456 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002457 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002458
2459 // Templated declarations should not appear in DeclContext.
2460 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
2461 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
2462 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002463
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002464 return ToTypedef;
2465}
2466
Balazs Keri3b30d652018-10-19 13:32:20 +00002467ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002468 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2469}
2470
Balazs Keri3b30d652018-10-19 13:32:20 +00002471ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002472 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2473}
2474
Balazs Keri3b30d652018-10-19 13:32:20 +00002475ExpectedDecl
2476ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
Gabor Horvath7a91c082017-11-14 11:30:38 +00002477 // Import the major distinguishing characteristics of this typedef.
2478 DeclContext *DC, *LexicalDC;
2479 DeclarationName Name;
2480 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002481 NamedDecl *FoundD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002482 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
2483 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002484 if (FoundD)
2485 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002486
2487 // If this typedef is not in block scope, determine whether we've
2488 // seen a typedef with the same name (that we can merge with) or any
2489 // other entity by that name (which name lookup could conflict with).
2490 if (!DC->isFunctionOrMethod()) {
2491 SmallVector<NamedDecl *, 4> ConflictingDecls;
2492 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002493 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002494 for (auto *FoundDecl : FoundDecls) {
2495 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002496 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002497 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002498 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002499 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002500 }
2501
2502 if (!ConflictingDecls.empty()) {
Gabor Martonf035b752019-08-27 11:36:10 +00002503 ExpectedName NameOrErr = Importer.HandleNameConflict(
2504 Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size());
2505 if (NameOrErr)
2506 Name = NameOrErr.get();
2507 else
2508 return NameOrErr.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002509 }
2510 }
2511
Balazs Keri3b30d652018-10-19 13:32:20 +00002512 TemplateParameterList *ToTemplateParameters;
2513 TypeAliasDecl *ToTemplatedDecl;
2514 if (auto Imp = importSeq(D->getTemplateParameters(), D->getTemplatedDecl()))
2515 std::tie(ToTemplateParameters, ToTemplatedDecl) = *Imp;
2516 else
2517 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002518
Gabor Marton26f72a92018-07-12 09:42:05 +00002519 TypeAliasTemplateDecl *ToAlias;
2520 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002521 Name, ToTemplateParameters, ToTemplatedDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002522 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002523
Balazs Keri3b30d652018-10-19 13:32:20 +00002524 ToTemplatedDecl->setDescribedAliasTemplate(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002525
Gabor Horvath7a91c082017-11-14 11:30:38 +00002526 ToAlias->setAccess(D->getAccess());
2527 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002528 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002529 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002530}
2531
Balazs Keri3b30d652018-10-19 13:32:20 +00002532ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002533 // Import the major distinguishing characteristics of this label.
2534 DeclContext *DC, *LexicalDC;
2535 DeclarationName Name;
2536 SourceLocation Loc;
2537 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002538 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2539 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002540 if (ToD)
2541 return ToD;
2542
2543 assert(LexicalDC->isFunctionOrMethod());
2544
Gabor Marton26f72a92018-07-12 09:42:05 +00002545 LabelDecl *ToLabel;
Balazs Keri3b30d652018-10-19 13:32:20 +00002546 if (D->isGnuLocal()) {
2547 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2548 if (!BeginLocOrErr)
2549 return BeginLocOrErr.takeError();
2550 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2551 Name.getAsIdentifierInfo(), *BeginLocOrErr))
2552 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002553
Balazs Keri3b30d652018-10-19 13:32:20 +00002554 } else {
2555 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2556 Name.getAsIdentifierInfo()))
2557 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002558
Balazs Keri3b30d652018-10-19 13:32:20 +00002559 }
2560
2561 Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt());
2562 if (!ToStmtOrErr)
2563 return ToStmtOrErr.takeError();
2564
2565 ToLabel->setStmt(*ToStmtOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002566 ToLabel->setLexicalDeclContext(LexicalDC);
2567 LexicalDC->addDeclInternal(ToLabel);
2568 return ToLabel;
2569}
2570
Balazs Keri3b30d652018-10-19 13:32:20 +00002571ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002572 // Import the major distinguishing characteristics of this enum.
2573 DeclContext *DC, *LexicalDC;
2574 DeclarationName Name;
2575 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002576 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002577 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2578 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002579 if (ToD)
2580 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002581
Douglas Gregor98c10182010-02-12 22:17:39 +00002582 // Figure out what enum name we're looking for.
2583 unsigned IDNS = Decl::IDNS_Tag;
2584 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002585 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002586 if (Error Err = importInto(
2587 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2588 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002589 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002590 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002591 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002592
Douglas Gregor98c10182010-02-12 22:17:39 +00002593 // We may already have an enum of the same name; try to find and match it.
2594 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002595 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002596 auto FoundDecls =
2597 Importer.findDeclsInToCtx(DC, SearchName);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002598 for (auto *FoundDecl : FoundDecls) {
2599 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002600 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002601
Balazs Keri3b30d652018-10-19 13:32:20 +00002602 if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002603 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Balazs Keri3b30d652018-10-19 13:32:20 +00002604 FoundDecl = Tag->getDecl();
Douglas Gregor98c10182010-02-12 22:17:39 +00002605 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002606
Balazs Keri3b30d652018-10-19 13:32:20 +00002607 if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) {
Balazs Kerieb79b252019-07-09 11:08:18 +00002608 if (!hasSameVisibilityContext(FoundEnum, D))
2609 continue;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002610 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002611 return Importer.MapImported(D, FoundEnum);
Gabor Martonf035b752019-08-27 11:36:10 +00002612 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002613 }
Douglas Gregor98c10182010-02-12 22:17:39 +00002614 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002615
Douglas Gregor98c10182010-02-12 22:17:39 +00002616 if (!ConflictingDecls.empty()) {
Gabor Martonf035b752019-08-27 11:36:10 +00002617 ExpectedName NameOrErr = Importer.HandleNameConflict(
2618 SearchName, DC, IDNS, ConflictingDecls.data(),
2619 ConflictingDecls.size());
2620 if (NameOrErr)
2621 Name = NameOrErr.get();
2622 else
2623 return NameOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00002624 }
2625 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002626
Balazs Keri3b30d652018-10-19 13:32:20 +00002627 SourceLocation ToBeginLoc;
2628 NestedNameSpecifierLoc ToQualifierLoc;
2629 QualType ToIntegerType;
Balázs Kéria9f10eb2019-12-05 16:21:21 +01002630 SourceRange ToBraceRange;
2631 if (auto Imp = importSeq(D->getBeginLoc(), D->getQualifierLoc(),
2632 D->getIntegerType(), D->getBraceRange()))
2633 std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType, ToBraceRange) = *Imp;
Balazs Keri3b30d652018-10-19 13:32:20 +00002634 else
2635 return Imp.takeError();
2636
Douglas Gregor98c10182010-02-12 22:17:39 +00002637 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002638 EnumDecl *D2;
2639 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002640 D2, D, Importer.getToContext(), DC, ToBeginLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002641 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2642 D->isScopedUsingClassTag(), D->isFixed()))
2643 return D2;
2644
Balazs Keri3b30d652018-10-19 13:32:20 +00002645 D2->setQualifierInfo(ToQualifierLoc);
2646 D2->setIntegerType(ToIntegerType);
Balázs Kéria9f10eb2019-12-05 16:21:21 +01002647 D2->setBraceRange(ToBraceRange);
Douglas Gregordd483172010-02-22 17:42:47 +00002648 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002649 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002650 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002651
Douglas Gregor98c10182010-02-12 22:17:39 +00002652 // Import the definition
Balazs Keri3b30d652018-10-19 13:32:20 +00002653 if (D->isCompleteDefinition())
2654 if (Error Err = ImportDefinition(D, D2))
2655 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002656
Douglas Gregor3996e242010-02-15 22:01:00 +00002657 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002658}
2659
Balazs Keri3b30d652018-10-19 13:32:20 +00002660ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002661 bool IsFriendTemplate = false;
2662 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2663 IsFriendTemplate =
2664 DCXX->getDescribedClassTemplate() &&
2665 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
2666 Decl::FOK_None;
2667 }
2668
Douglas Gregor5c73e912010-02-11 00:48:18 +00002669 // Import the major distinguishing characteristics of this record.
Simon Pilgrim4706f3b2019-10-15 10:23:05 +00002670 DeclContext *DC = nullptr, *LexicalDC = nullptr;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002671 DeclarationName Name;
2672 SourceLocation Loc;
Simon Pilgrim4706f3b2019-10-15 10:23:05 +00002673 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00002674 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2675 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002676 if (ToD)
2677 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002678
Douglas Gregor5c73e912010-02-11 00:48:18 +00002679 // Figure out what structure name we're looking for.
2680 unsigned IDNS = Decl::IDNS_Tag;
2681 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002682 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002683 if (Error Err = importInto(
2684 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2685 return std::move(Err);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002686 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002687 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Gabor Marton7df342a2018-12-17 12:42:12 +00002688 IDNS |= Decl::IDNS_Ordinary | Decl::IDNS_TagFriend;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002689
2690 // We may already have a record of the same name; try to find and match it.
Sean Callanan9092d472017-05-13 00:46:33 +00002691 RecordDecl *PrevDecl = nullptr;
Gabor Martone3e83d72019-08-30 10:55:41 +00002692 if (!DC->isFunctionOrMethod() && !D->isLambda()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002693 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002694 auto FoundDecls =
2695 Importer.findDeclsInToCtx(DC, SearchName);
Sean Callanan9092d472017-05-13 00:46:33 +00002696 if (!FoundDecls.empty()) {
Gabor Marton41e38922019-03-05 11:23:24 +00002697 // We're going to have to compare D against potentially conflicting Decls,
2698 // so complete it.
Sean Callanan9092d472017-05-13 00:46:33 +00002699 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2700 D->getASTContext().getExternalSource()->CompleteType(D);
2701 }
2702
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002703 for (auto *FoundDecl : FoundDecls) {
2704 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002705 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002706
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002707 Decl *Found = FoundDecl;
2708 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2709 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002710 Found = Tag->getDecl();
2711 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002712
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002713 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Gabor Marton7df342a2018-12-17 12:42:12 +00002714 // Do not emit false positive diagnostic in case of unnamed
2715 // struct/union and in case of anonymous structs. Would be false
2716 // because there may be several anonymous/unnamed structs in a class.
2717 // E.g. these are both valid:
2718 // struct A { // unnamed structs
2719 // struct { struct A *next; } entry0;
2720 // struct { struct A *next; } entry1;
2721 // };
2722 // struct X { struct { int a; }; struct { int b; }; }; // anon structs
2723 if (!SearchName)
Gabor Marton0bebf952018-07-05 09:51:13 +00002724 if (!IsStructuralMatch(D, FoundRecord, false))
2725 continue;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002726
Balazs Keric8272192019-05-27 09:36:00 +00002727 if (!hasSameVisibilityContext(FoundRecord, D))
2728 continue;
2729
Gabor Marton7df342a2018-12-17 12:42:12 +00002730 if (IsStructuralMatch(D, FoundRecord)) {
2731 RecordDecl *FoundDef = FoundRecord->getDefinition();
2732 if (D->isThisDeclarationADefinition() && FoundDef) {
Balazs Keri1d20cc22018-07-16 12:16:39 +00002733 // FIXME: Structural equivalence check should check for same
2734 // user-defined methods.
2735 Importer.MapImported(D, FoundDef);
2736 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2737 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2738 assert(FoundCXX && "Record type mismatch");
2739
Gabor Marton7df342a2018-12-17 12:42:12 +00002740 if (!Importer.isMinimalImport())
Balazs Keri1d20cc22018-07-16 12:16:39 +00002741 // FoundDef may not have every implicit method that D has
2742 // because implicit methods are created only if they are used.
Balazs Keri3b30d652018-10-19 13:32:20 +00002743 if (Error Err = ImportImplicitMethods(DCXX, FoundCXX))
2744 return std::move(Err);
Balazs Keri1d20cc22018-07-16 12:16:39 +00002745 }
Douglas Gregor25791052010-02-12 00:09:27 +00002746 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002747 PrevDecl = FoundRecord->getMostRecentDecl();
2748 break;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002749 }
Gabor Martonf035b752019-08-27 11:36:10 +00002750 ConflictingDecls.push_back(FoundDecl);
2751 } // kind is RecordDecl
Gabor Marton7df342a2018-12-17 12:42:12 +00002752 } // for
Fangrui Song6907ce22018-07-30 19:24:48 +00002753
Douglas Gregordd6006f2012-07-17 21:16:27 +00002754 if (!ConflictingDecls.empty() && SearchName) {
Gabor Martonf035b752019-08-27 11:36:10 +00002755 ExpectedName NameOrErr = Importer.HandleNameConflict(
2756 SearchName, DC, IDNS, ConflictingDecls.data(),
2757 ConflictingDecls.size());
2758 if (NameOrErr)
2759 Name = NameOrErr.get();
2760 else
2761 return NameOrErr.takeError();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002762 }
2763 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002764
Balazs Keri3b30d652018-10-19 13:32:20 +00002765 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2766 if (!BeginLocOrErr)
2767 return BeginLocOrErr.takeError();
2768
Douglas Gregor5c73e912010-02-11 00:48:18 +00002769 // Create the record declaration.
Gabor Marton7df342a2018-12-17 12:42:12 +00002770 RecordDecl *D2 = nullptr;
2771 CXXRecordDecl *D2CXX = nullptr;
2772 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2773 if (DCXX->isLambda()) {
2774 auto TInfoOrErr = import(DCXX->getLambdaTypeInfo());
2775 if (!TInfoOrErr)
2776 return TInfoOrErr.takeError();
2777 if (GetImportedOrCreateSpecialDecl(
2778 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
2779 DC, *TInfoOrErr, Loc, DCXX->isDependentLambda(),
2780 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2781 return D2CXX;
2782 ExpectedDecl CDeclOrErr = import(DCXX->getLambdaContextDecl());
2783 if (!CDeclOrErr)
2784 return CDeclOrErr.takeError();
Michael Liao243ebfb2019-10-19 00:15:19 +00002785 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), *CDeclOrErr,
2786 DCXX->hasKnownLambdaInternalLinkage());
Gabor Marton7df342a2018-12-17 12:42:12 +00002787 } else if (DCXX->isInjectedClassName()) {
2788 // We have to be careful to do a similar dance to the one in
2789 // Sema::ActOnStartCXXMemberDeclarations
2790 const bool DelayTypeCreation = true;
2791 if (GetImportedOrCreateDecl(
2792 D2CXX, D, Importer.getToContext(), D->getTagKind(), DC,
2793 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
2794 cast_or_null<CXXRecordDecl>(PrevDecl), DelayTypeCreation))
2795 return D2CXX;
2796 Importer.getToContext().getTypeDeclType(
2797 D2CXX, dyn_cast<CXXRecordDecl>(DC));
2798 } else {
2799 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2800 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2801 Name.getAsIdentifierInfo(),
2802 cast_or_null<CXXRecordDecl>(PrevDecl)))
2803 return D2CXX;
2804 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002805
Gabor Marton7df342a2018-12-17 12:42:12 +00002806 D2 = D2CXX;
2807 D2->setAccess(D->getAccess());
2808 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonbc5b7e22019-12-04 17:12:08 +01002809 addDeclToContexts(D, D2);
Gabor Marton7df342a2018-12-17 12:42:12 +00002810
2811 if (ClassTemplateDecl *FromDescribed =
2812 DCXX->getDescribedClassTemplate()) {
2813 ClassTemplateDecl *ToDescribed;
2814 if (Error Err = importInto(ToDescribed, FromDescribed))
2815 return std::move(Err);
2816 D2CXX->setDescribedClassTemplate(ToDescribed);
2817 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
2818 // In a record describing a template the type should be an
2819 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2820 // previously set type to the correct value here (ToDescribed is not
2821 // available at record create).
2822 // FIXME: The previous type is cleared but not removed from
2823 // ASTContext's internal storage.
2824 CXXRecordDecl *Injected = nullptr;
2825 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2826 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2827 if (Record && Record->isInjectedClassName()) {
2828 Injected = Record;
2829 break;
Gabor Marton5915777e2018-06-26 13:44:24 +00002830 }
2831 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002832 // Create an injected type for the whole redecl chain.
2833 SmallVector<Decl *, 2> Redecls =
2834 getCanonicalForwardRedeclChain(D2CXX);
2835 for (auto *R : Redecls) {
2836 auto *RI = cast<CXXRecordDecl>(R);
2837 RI->setTypeForDecl(nullptr);
2838 // Below we create a new injected type and assign that to the
2839 // canonical decl, subsequent declarations in the chain will reuse
2840 // that type.
2841 Importer.getToContext().getInjectedClassNameType(
2842 RI, ToDescribed->getInjectedClassNameSpecialization());
2843 }
2844 // Set the new type for the previous injected decl too.
2845 if (Injected) {
2846 Injected->setTypeForDecl(nullptr);
2847 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2848 }
2849 }
2850 } else if (MemberSpecializationInfo *MemberInfo =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002851 DCXX->getMemberSpecializationInfo()) {
2852 TemplateSpecializationKind SK =
2853 MemberInfo->getTemplateSpecializationKind();
2854 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Balazs Keri3b30d652018-10-19 13:32:20 +00002855
2856 if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst))
2857 D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK);
2858 else
2859 return ToInstOrErr.takeError();
2860
2861 if (ExpectedSLoc POIOrErr =
2862 import(MemberInfo->getPointOfInstantiation()))
2863 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2864 *POIOrErr);
2865 else
2866 return POIOrErr.takeError();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002867 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002868
Gabor Marton7df342a2018-12-17 12:42:12 +00002869 } else {
2870 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
2871 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2872 Name.getAsIdentifierInfo(), PrevDecl))
2873 return D2;
2874 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonbc5b7e22019-12-04 17:12:08 +01002875 addDeclToContexts(D, D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002876 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002877
Balázs Kéria9f10eb2019-12-05 16:21:21 +01002878 if (auto BraceRangeOrErr = import(D->getBraceRange()))
2879 D2->setBraceRange(*BraceRangeOrErr);
2880 else
2881 return BraceRangeOrErr.takeError();
Gabor Marton7df342a2018-12-17 12:42:12 +00002882 if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
2883 D2->setQualifierInfo(*QualifierLocOrErr);
2884 else
2885 return QualifierLocOrErr.takeError();
2886
2887 if (D->isAnonymousStructOrUnion())
2888 D2->setAnonymousStructOrUnion(true);
Douglas Gregor25791052010-02-12 00:09:27 +00002889
Balazs Keri3b30d652018-10-19 13:32:20 +00002890 if (D->isCompleteDefinition())
2891 if (Error Err = ImportDefinition(D, D2, IDK_Default))
2892 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00002893
Douglas Gregor3996e242010-02-15 22:01:00 +00002894 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002895}
2896
Balazs Keri3b30d652018-10-19 13:32:20 +00002897ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002898 // Import the major distinguishing characteristics of this enumerator.
2899 DeclContext *DC, *LexicalDC;
2900 DeclarationName Name;
2901 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002902 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002903 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2904 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002905 if (ToD)
2906 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002907
Fangrui Song6907ce22018-07-30 19:24:48 +00002908 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002909 // in the same context.
2910 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002911 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002912 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002913 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002914 for (auto *FoundDecl : FoundDecls) {
2915 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002916 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002917
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002918 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002919 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002920 return Importer.MapImported(D, FoundEnumConstant);
Gabor Martonf035b752019-08-27 11:36:10 +00002921 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor91155082012-11-14 22:29:20 +00002922 }
Douglas Gregor98c10182010-02-12 22:17:39 +00002923 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002924
Douglas Gregor98c10182010-02-12 22:17:39 +00002925 if (!ConflictingDecls.empty()) {
Gabor Martonf035b752019-08-27 11:36:10 +00002926 ExpectedName NameOrErr = Importer.HandleNameConflict(
2927 Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size());
2928 if (NameOrErr)
2929 Name = NameOrErr.get();
2930 else
2931 return NameOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00002932 }
2933 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002934
Balazs Keri3b30d652018-10-19 13:32:20 +00002935 ExpectedType TypeOrErr = import(D->getType());
2936 if (!TypeOrErr)
2937 return TypeOrErr.takeError();
2938
2939 ExpectedExpr InitOrErr = import(D->getInitExpr());
2940 if (!InitOrErr)
2941 return InitOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002942
Gabor Marton26f72a92018-07-12 09:42:05 +00002943 EnumConstantDecl *ToEnumerator;
2944 if (GetImportedOrCreateDecl(
2945 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002946 Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002947 return ToEnumerator;
2948
Douglas Gregordd483172010-02-22 17:42:47 +00002949 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002950 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002951 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002952 return ToEnumerator;
2953}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002954
Balazs Keri1efc9742019-05-07 10:55:11 +00002955Error ASTNodeImporter::ImportTemplateParameterLists(const DeclaratorDecl *FromD,
2956 DeclaratorDecl *ToD) {
2957 unsigned int Num = FromD->getNumTemplateParameterLists();
2958 if (Num == 0)
2959 return Error::success();
2960 SmallVector<TemplateParameterList *, 2> ToTPLists(Num);
2961 for (unsigned int I = 0; I < Num; ++I)
2962 if (Expected<TemplateParameterList *> ToTPListOrErr =
2963 import(FromD->getTemplateParameterList(I)))
2964 ToTPLists[I] = *ToTPListOrErr;
2965 else
2966 return ToTPListOrErr.takeError();
2967 ToD->setTemplateParameterListsInfo(Importer.ToContext, ToTPLists);
2968 return Error::success();
2969}
2970
Balazs Keri3b30d652018-10-19 13:32:20 +00002971Error ASTNodeImporter::ImportTemplateInformation(
2972 FunctionDecl *FromFD, FunctionDecl *ToFD) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002973 switch (FromFD->getTemplatedKind()) {
2974 case FunctionDecl::TK_NonTemplate:
2975 case FunctionDecl::TK_FunctionTemplate:
Balazs Keri3b30d652018-10-19 13:32:20 +00002976 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002977
2978 case FunctionDecl::TK_MemberSpecialization: {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002979 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00002980
2981 if (Expected<FunctionDecl *> InstFDOrErr =
2982 import(FromFD->getInstantiatedFromMemberFunction()))
2983 ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK);
2984 else
2985 return InstFDOrErr.takeError();
2986
2987 if (ExpectedSLoc POIOrErr = import(
2988 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()))
2989 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
2990 else
2991 return POIOrErr.takeError();
2992
2993 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002994 }
2995
2996 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Balazs Keri3b30d652018-10-19 13:32:20 +00002997 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002998 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002999 if (!FunctionAndArgsOrErr)
3000 return FunctionAndArgsOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003001
3002 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Balazs Keri3b30d652018-10-19 13:32:20 +00003003 Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003004
Gabor Marton5254e642018-06-27 13:32:50 +00003005 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003006 TemplateArgumentListInfo ToTAInfo;
3007 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003008 if (FromTAArgsAsWritten)
Balazs Keri3b30d652018-10-19 13:32:20 +00003009 if (Error Err = ImportTemplateArgumentListInfo(
3010 *FromTAArgsAsWritten, ToTAInfo))
3011 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003012
Balazs Keri3b30d652018-10-19 13:32:20 +00003013 ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation());
3014 if (!POIOrErr)
3015 return POIOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003016
Balazs Keri1efc9742019-05-07 10:55:11 +00003017 if (Error Err = ImportTemplateParameterLists(FromFD, ToFD))
3018 return Err;
3019
Gabor Marton5254e642018-06-27 13:32:50 +00003020 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003021 ToFD->setFunctionTemplateSpecialization(
Balazs Keri3b30d652018-10-19 13:32:20 +00003022 std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr,
3023 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr);
3024 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003025 }
3026
3027 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
3028 auto *FromInfo = FromFD->getDependentSpecializationInfo();
3029 UnresolvedSet<8> TemplDecls;
3030 unsigned NumTemplates = FromInfo->getNumTemplates();
3031 for (unsigned I = 0; I < NumTemplates; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003032 if (Expected<FunctionTemplateDecl *> ToFTDOrErr =
3033 import(FromInfo->getTemplate(I)))
3034 TemplDecls.addDecl(*ToFTDOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003035 else
Balazs Keri3b30d652018-10-19 13:32:20 +00003036 return ToFTDOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003037 }
3038
3039 // Import TemplateArgumentListInfo.
3040 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00003041 if (Error Err = ImportTemplateArgumentListInfo(
3042 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
3043 llvm::makeArrayRef(
3044 FromInfo->getTemplateArgs(), FromInfo->getNumTemplateArgs()),
3045 ToTAInfo))
3046 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003047
3048 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
3049 TemplDecls, ToTAInfo);
Balazs Keri3b30d652018-10-19 13:32:20 +00003050 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003051 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003052 }
Sam McCallfdc32072018-01-26 12:06:44 +00003053 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003054}
3055
Balazs Keri3b30d652018-10-19 13:32:20 +00003056Expected<FunctionDecl *>
Gabor Marton5254e642018-06-27 13:32:50 +00003057ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003058 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00003059 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00003060 if (!FunctionAndArgsOrErr)
3061 return FunctionAndArgsOrErr.takeError();
Gabor Marton5254e642018-06-27 13:32:50 +00003062
Balazs Keri3b30d652018-10-19 13:32:20 +00003063 FunctionTemplateDecl *Template;
3064 TemplateArgsTy ToTemplArgs;
3065 std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr;
Gabor Marton5254e642018-06-27 13:32:50 +00003066 void *InsertPos = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003067 auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos);
Gabor Marton5254e642018-06-27 13:32:50 +00003068 return FoundSpec;
3069}
3070
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003071Error ASTNodeImporter::ImportFunctionDeclBody(FunctionDecl *FromFD,
3072 FunctionDecl *ToFD) {
3073 if (Stmt *FromBody = FromFD->getBody()) {
3074 if (ExpectedStmt ToBodyOrErr = import(FromBody))
3075 ToFD->setBody(*ToBodyOrErr);
3076 else
3077 return ToBodyOrErr.takeError();
3078 }
3079 return Error::success();
3080}
3081
Gabor Marton25234fd2019-12-12 17:13:35 +01003082// Returns true if the given D has a DeclContext up to the TranslationUnitDecl
3083// which is equal to the given DC.
Benjamin Kramerdf186502020-01-14 14:06:12 +01003084static bool isAncestorDeclContextOf(const DeclContext *DC, const Decl *D) {
Gabor Marton25234fd2019-12-12 17:13:35 +01003085 const DeclContext *DCi = D->getDeclContext();
3086 while (DCi != D->getTranslationUnitDecl()) {
3087 if (DCi == DC)
3088 return true;
3089 DCi = DCi->getParent();
3090 }
3091 return false;
3092}
3093
3094bool ASTNodeImporter::hasAutoReturnTypeDeclaredInside(FunctionDecl *D) {
3095 QualType FromTy = D->getType();
3096 const FunctionProtoType *FromFPT = FromTy->getAs<FunctionProtoType>();
3097 assert(FromFPT && "Must be called on FunctionProtoType");
3098 if (AutoType *AutoT = FromFPT->getReturnType()->getContainedAutoType()) {
3099 QualType DeducedT = AutoT->getDeducedType();
3100 if (const RecordType *RecordT =
3101 DeducedT.isNull() ? nullptr : dyn_cast<RecordType>(DeducedT)) {
3102 RecordDecl *RD = RecordT->getDecl();
3103 assert(RD);
3104 if (isAncestorDeclContextOf(D, RD)) {
3105 assert(RD->getLexicalDeclContext() == RD->getDeclContext());
3106 return true;
3107 }
3108 }
3109 }
3110 if (const TypedefType *TypedefT =
3111 dyn_cast<TypedefType>(FromFPT->getReturnType())) {
3112 TypedefNameDecl *TD = TypedefT->getDecl();
3113 assert(TD);
3114 if (isAncestorDeclContextOf(D, TD)) {
3115 assert(TD->getLexicalDeclContext() == TD->getDeclContext());
3116 return true;
3117 }
3118 }
3119 return false;
3120}
3121
Balazs Keri3b30d652018-10-19 13:32:20 +00003122ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00003123
Balazs Keri3b30d652018-10-19 13:32:20 +00003124 SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D);
Gabor Marton5254e642018-06-27 13:32:50 +00003125 auto RedeclIt = Redecls.begin();
3126 // Import the first part of the decl chain. I.e. import all previous
3127 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003128 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3129 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3130 if (!ToRedeclOrErr)
3131 return ToRedeclOrErr.takeError();
3132 }
Gabor Marton5254e642018-06-27 13:32:50 +00003133 assert(*RedeclIt == D);
3134
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003135 // Import the major distinguishing characteristics of this function.
3136 DeclContext *DC, *LexicalDC;
3137 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003138 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003139 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003140 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3141 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003142 if (ToD)
3143 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003144
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003145 FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00003146 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00003147
Gabor Marton5254e642018-06-27 13:32:50 +00003148 // If this is a function template specialization, then try to find the same
Gabor Marton54058b52018-12-17 13:53:12 +00003149 // existing specialization in the "to" context. The lookup below will not
3150 // find any specialization, but would find the primary template; thus, we
3151 // have to skip normal lookup in case of specializations.
Gabor Marton5254e642018-06-27 13:32:50 +00003152 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
3153 if (D->getTemplatedKind() ==
3154 FunctionDecl::TK_FunctionTemplateSpecialization) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003155 auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D);
3156 if (!FoundFunctionOrErr)
3157 return FoundFunctionOrErr.takeError();
3158 if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) {
Gabor Martondd59d272019-03-19 14:04:50 +00003159 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
3160 return Def;
Gabor Marton5254e642018-06-27 13:32:50 +00003161 FoundByLookup = FoundFunction;
3162 }
3163 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003164 // Try to find a function in our own ("to") context with the same name, same
3165 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00003166 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003167 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00003168 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00003169 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003170 for (auto *FoundDecl : FoundDecls) {
3171 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003172 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003173
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003174 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00003175 if (!hasSameVisibilityContext(FoundFunction, D))
3176 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003177
Gabor Marton458d1452019-02-14 13:07:03 +00003178 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Martondd59d272019-03-19 14:04:50 +00003179 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
3180 return Def;
Gabor Marton458d1452019-02-14 13:07:03 +00003181 FoundByLookup = FoundFunction;
3182 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003183 }
Gabor Marton458d1452019-02-14 13:07:03 +00003184 // FIXME: Check for overloading more carefully, e.g., by boosting
3185 // Sema::IsOverload out to the AST library.
3186
3187 // Function overloading is okay in C++.
3188 if (Importer.getToContext().getLangOpts().CPlusPlus)
3189 continue;
3190
3191 // Complain about inconsistent function types.
Gabor Marton410f32c2019-04-01 15:29:55 +00003192 Importer.ToDiag(Loc, diag::warn_odr_function_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00003193 << Name << D->getType() << FoundFunction->getType();
3194 Importer.ToDiag(FoundFunction->getLocation(), diag::note_odr_value_here)
3195 << FoundFunction->getType();
Gabor Martonf035b752019-08-27 11:36:10 +00003196 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003197 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003198 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003199
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003200 if (!ConflictingDecls.empty()) {
Gabor Martonf035b752019-08-27 11:36:10 +00003201 ExpectedName NameOrErr = Importer.HandleNameConflict(
3202 Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size());
3203 if (NameOrErr)
3204 Name = NameOrErr.get();
3205 else
3206 return NameOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00003207 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003208 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00003209
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003210 // We do not allow more than one in-class declaration of a function. This is
3211 // because AST clients like VTableBuilder asserts on this. VTableBuilder
3212 // assumes there is only one in-class declaration. Building a redecl
3213 // chain would result in more than one in-class declaration for
3214 // overrides (even if they are part of the same redecl chain inside the
3215 // derived class.)
3216 if (FoundByLookup) {
Mikael Holmenc1c97aa2019-01-29 06:53:31 +00003217 if (isa<CXXMethodDecl>(FoundByLookup)) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003218 if (D->getLexicalDeclContext() == D->getDeclContext()) {
Balazs Kerie9719f92019-08-07 12:40:17 +00003219 if (!D->doesThisDeclarationHaveABody()) {
3220 if (FunctionTemplateDecl *DescribedD =
3221 D->getDescribedFunctionTemplate()) {
3222 // Handle a "templated" function together with its described
3223 // template. This avoids need for a similar check at import of the
3224 // described template.
3225 assert(FoundByLookup->getDescribedFunctionTemplate() &&
3226 "Templated function mapped to non-templated?");
3227 Importer.MapImported(DescribedD,
3228 FoundByLookup->getDescribedFunctionTemplate());
3229 }
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003230 return Importer.MapImported(D, FoundByLookup);
Balazs Kerie9719f92019-08-07 12:40:17 +00003231 } else {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003232 // Let's continue and build up the redecl chain in this case.
3233 // FIXME Merge the functions into one decl.
3234 }
3235 }
3236 }
3237 }
3238
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003239 DeclarationNameInfo NameInfo(Name, Loc);
3240 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00003241 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
3242 return std::move(Err);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003243
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003244 QualType FromTy = D->getType();
Gabor Marton25234fd2019-12-12 17:13:35 +01003245 // Set to true if we do not import the type of the function as is. There are
3246 // cases when the original type would result in an infinite recursion during
3247 // the import. To avoid an infinite recursion when importing, we create the
3248 // FunctionDecl with a simplified function type and update it only after the
3249 // relevant AST nodes are already imported.
3250 bool UsedDifferentProtoType = false;
3251 if (const auto *FromFPT = FromTy->getAs<FunctionProtoType>()) {
3252 QualType FromReturnTy = FromFPT->getReturnType();
3253 // Functions with auto return type may define a struct inside their body
3254 // and the return type could refer to that struct.
3255 // E.g.: auto foo() { struct X{}; return X(); }
3256 // To avoid an infinite recursion when importing, create the FunctionDecl
3257 // with a simplified return type.
3258 if (hasAutoReturnTypeDeclaredInside(D)) {
3259 FromReturnTy = Importer.getFromContext().VoidTy;
3260 UsedDifferentProtoType = true;
3261 }
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003262 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3263 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3264 // FunctionDecl that we are importing the FunctionProtoType for.
3265 // To avoid an infinite recursion when importing, create the FunctionDecl
Gabor Marton25234fd2019-12-12 17:13:35 +01003266 // with a simplified function type.
Richard Smith8acb4282014-07-31 21:57:55 +00003267 if (FromEPI.ExceptionSpec.SourceDecl ||
3268 FromEPI.ExceptionSpec.SourceTemplate ||
3269 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003270 FunctionProtoType::ExtProtoInfo DefaultEPI;
Gabor Marton25234fd2019-12-12 17:13:35 +01003271 FromEPI = DefaultEPI;
3272 UsedDifferentProtoType = true;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003273 }
Gabor Marton25234fd2019-12-12 17:13:35 +01003274 FromTy = Importer.getFromContext().getFunctionType(
3275 FromReturnTy, FromFPT->getParamTypes(), FromEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003276 }
3277
Balazs Keri3b30d652018-10-19 13:32:20 +00003278 QualType T;
3279 TypeSourceInfo *TInfo;
3280 SourceLocation ToInnerLocStart, ToEndLoc;
3281 NestedNameSpecifierLoc ToQualifierLoc;
Saar Razb65b1f32020-01-09 15:07:51 +02003282 Expr *TrailingRequiresClause;
Balazs Keri3b30d652018-10-19 13:32:20 +00003283 if (auto Imp = importSeq(
3284 FromTy, D->getTypeSourceInfo(), D->getInnerLocStart(),
Saar Razb65b1f32020-01-09 15:07:51 +02003285 D->getQualifierLoc(), D->getEndLoc(), D->getTrailingRequiresClause()))
3286 std::tie(T, TInfo, ToInnerLocStart, ToQualifierLoc, ToEndLoc,
3287 TrailingRequiresClause) = *Imp;
Balazs Keri3b30d652018-10-19 13:32:20 +00003288 else
3289 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003290
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003291 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003292 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003293 for (auto P : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003294 if (Expected<ParmVarDecl *> ToPOrErr = import(P))
3295 Parameters.push_back(*ToPOrErr);
3296 else
3297 return ToPOrErr.takeError();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003298 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003299
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003300 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00003301 FunctionDecl *ToFunction = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003302 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith76b90272019-05-09 03:59:21 +00003303 Expr *ExplicitExpr = nullptr;
3304 if (FromConstructor->getExplicitSpecifier().getExpr()) {
3305 auto Imp = importSeq(FromConstructor->getExplicitSpecifier().getExpr());
3306 if (!Imp)
3307 return Imp.takeError();
3308 std::tie(ExplicitExpr) = *Imp;
3309 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003310 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
Richard Smith76b90272019-05-09 03:59:21 +00003311 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3312 ToInnerLocStart, NameInfo, T, TInfo,
3313 ExplicitSpecifier(
3314 ExplicitExpr,
3315 FromConstructor->getExplicitSpecifier().getKind()),
Saar Razb65b1f32020-01-09 15:07:51 +02003316 D->isInlineSpecified(), D->isImplicit(), D->getConstexprKind(),
3317 InheritedConstructor(), // FIXME: Properly import inherited
3318 // constructor info
3319 TrailingRequiresClause))
Gabor Marton26f72a92018-07-12 09:42:05 +00003320 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003321 } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(D)) {
3322
3323 auto Imp =
3324 importSeq(const_cast<FunctionDecl *>(FromDtor->getOperatorDelete()),
3325 FromDtor->getOperatorDeleteThisArg());
3326
3327 if (!Imp)
3328 return Imp.takeError();
3329
3330 FunctionDecl *ToOperatorDelete;
3331 Expr *ToThisArg;
3332 std::tie(ToOperatorDelete, ToThisArg) = *Imp;
3333
Gabor Marton26f72a92018-07-12 09:42:05 +00003334 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003335 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3336 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Saar Razb65b1f32020-01-09 15:07:51 +02003337 D->isImplicit(), D->getConstexprKind(), TrailingRequiresClause))
Gabor Marton26f72a92018-07-12 09:42:05 +00003338 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003339
3340 CXXDestructorDecl *ToDtor = cast<CXXDestructorDecl>(ToFunction);
3341
3342 ToDtor->setOperatorDelete(ToOperatorDelete, ToThisArg);
Gabor Marton26f72a92018-07-12 09:42:05 +00003343 } else if (CXXConversionDecl *FromConversion =
3344 dyn_cast<CXXConversionDecl>(D)) {
Richard Smith76b90272019-05-09 03:59:21 +00003345 Expr *ExplicitExpr = nullptr;
3346 if (FromConversion->getExplicitSpecifier().getExpr()) {
3347 auto Imp = importSeq(FromConversion->getExplicitSpecifier().getExpr());
3348 if (!Imp)
3349 return Imp.takeError();
3350 std::tie(ExplicitExpr) = *Imp;
3351 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003352 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3353 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003354 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Richard Smith76b90272019-05-09 03:59:21 +00003355 ExplicitSpecifier(ExplicitExpr,
3356 FromConversion->getExplicitSpecifier().getKind()),
Saar Razb65b1f32020-01-09 15:07:51 +02003357 D->getConstexprKind(), SourceLocation(), TrailingRequiresClause))
Gabor Marton26f72a92018-07-12 09:42:05 +00003358 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003359 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003360 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3361 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003362 ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003363 Method->isInlineSpecified(), D->getConstexprKind(),
Saar Razb65b1f32020-01-09 15:07:51 +02003364 SourceLocation(), TrailingRequiresClause))
Gabor Marton26f72a92018-07-12 09:42:05 +00003365 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003366 } else {
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003367 if (GetImportedOrCreateDecl(
3368 ToFunction, D, Importer.getToContext(), DC, ToInnerLocStart,
3369 NameInfo, T, TInfo, D->getStorageClass(), D->isInlineSpecified(),
Saar Razb65b1f32020-01-09 15:07:51 +02003370 D->hasWrittenPrototype(), D->getConstexprKind(),
3371 TrailingRequiresClause))
Gabor Marton26f72a92018-07-12 09:42:05 +00003372 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003373 }
John McCall3e11ebe2010-03-15 10:12:16 +00003374
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003375 // Connect the redecl chain.
3376 if (FoundByLookup) {
3377 auto *Recent = const_cast<FunctionDecl *>(
3378 FoundByLookup->getMostRecentDecl());
3379 ToFunction->setPreviousDecl(Recent);
Gabor Martonce6b7812019-05-08 15:23:48 +00003380 // FIXME Probably we should merge exception specifications. E.g. In the
3381 // "To" context the existing function may have exception specification with
3382 // noexcept-unevaluated, while the newly imported function may have an
3383 // evaluated noexcept. A call to adjustExceptionSpec() on the imported
3384 // decl and its redeclarations may be required.
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003385 }
3386
Balazs Keri3b30d652018-10-19 13:32:20 +00003387 ToFunction->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003388 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003389 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003390 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3391 ToFunction->setTrivial(D->isTrivial());
3392 ToFunction->setPure(D->isPure());
Balazs Kerib427c062019-08-13 08:04:06 +00003393 ToFunction->setDefaulted(D->isDefaulted());
3394 ToFunction->setExplicitlyDefaulted(D->isExplicitlyDefaulted());
3395 ToFunction->setDeletedAsWritten(D->isDeletedAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00003396 ToFunction->setRangeEnd(ToEndLoc);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003397
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003398 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003399 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003400 Param->setOwningFunction(ToFunction);
3401 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003402 }
David Blaikie9c70e042011-09-21 18:16:56 +00003403 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003404
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003405 // We need to complete creation of FunctionProtoTypeLoc manually with setting
3406 // params it refers to.
3407 if (TInfo) {
3408 if (auto ProtoLoc =
3409 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
3410 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
3411 ProtoLoc.setParam(I, Parameters[I]);
3412 }
3413 }
3414
Balazs Keria35798d2018-07-17 09:52:41 +00003415 // Import the describing template function, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00003416 if (FromFT) {
3417 auto ToFTOrErr = import(FromFT);
3418 if (!ToFTOrErr)
3419 return ToFTOrErr.takeError();
3420 }
Balazs Keria35798d2018-07-17 09:52:41 +00003421
Balazs Kerie13e8362019-08-16 12:10:03 +00003422 // Import Ctor initializers.
3423 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3424 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3425 SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers);
3426 // Import first, then allocate memory and copy if there was no error.
3427 if (Error Err = ImportContainerChecked(
3428 FromConstructor->inits(), CtorInitializers))
3429 return std::move(Err);
3430 auto **Memory =
3431 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3432 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3433 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
3434 ToCtor->setCtorInitializers(Memory);
3435 ToCtor->setNumCtorInitializers(NumInitializers);
3436 }
3437 }
3438
Gabor Marton5254e642018-06-27 13:32:50 +00003439 if (D->doesThisDeclarationHaveABody()) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003440 Error Err = ImportFunctionDeclBody(D, ToFunction);
3441
3442 if (Err)
3443 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003444 }
3445
Gabor Marton25234fd2019-12-12 17:13:35 +01003446 // Import and set the original type in case we used another type.
3447 if (UsedDifferentProtoType) {
3448 if (ExpectedType TyOrErr = import(D->getType()))
3449 ToFunction->setType(*TyOrErr);
3450 else
3451 return TyOrErr.takeError();
3452 }
3453
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003454 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003455
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003456 // If it is a template, import all related things.
Balazs Keri3b30d652018-10-19 13:32:20 +00003457 if (Error Err = ImportTemplateInformation(D, ToFunction))
3458 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003459
Gabor Martonbc5b7e22019-12-04 17:12:08 +01003460 addDeclToContexts(D, ToFunction);
Gabor Marton5254e642018-06-27 13:32:50 +00003461
Gabor Marton7a0841e2018-10-29 10:18:28 +00003462 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
Balazs Kerib4fd7d42019-08-30 10:12:14 +00003463 if (Error Err = ImportOverriddenMethods(cast<CXXMethodDecl>(ToFunction),
3464 FromCXXMethod))
3465 return std::move(Err);
Gabor Marton7a0841e2018-10-29 10:18:28 +00003466
Gabor Marton5254e642018-06-27 13:32:50 +00003467 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003468 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3469 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3470 if (!ToRedeclOrErr)
3471 return ToRedeclOrErr.takeError();
3472 }
Gabor Marton5254e642018-06-27 13:32:50 +00003473
Douglas Gregor43f54792010-02-17 02:12:47 +00003474 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003475}
3476
Balazs Keri3b30d652018-10-19 13:32:20 +00003477ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003478 return VisitFunctionDecl(D);
3479}
3480
Balazs Keri3b30d652018-10-19 13:32:20 +00003481ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003482 return VisitCXXMethodDecl(D);
3483}
3484
Balazs Keri3b30d652018-10-19 13:32:20 +00003485ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003486 return VisitCXXMethodDecl(D);
3487}
3488
Balazs Keri3b30d652018-10-19 13:32:20 +00003489ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003490 return VisitCXXMethodDecl(D);
3491}
3492
Balazs Keri3b30d652018-10-19 13:32:20 +00003493ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003494 // Import the major distinguishing characteristics of a variable.
3495 DeclContext *DC, *LexicalDC;
3496 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003497 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003498 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003499 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3500 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003501 if (ToD)
3502 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003503
Fangrui Song6907ce22018-07-30 19:24:48 +00003504 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003505 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003506 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003507 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003508 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003509 if (!Name &&
3510 ASTImporter::getFieldIndex(D) !=
3511 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003512 continue;
3513
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003514 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003515 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003516 Importer.MapImported(D, FoundField);
Gabor Marton42e15de2018-08-22 11:52:14 +00003517 // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the
3518 // initializer of a FieldDecl might not had been instantiated in the
3519 // "To" context. However, the "From" context might instantiated that,
3520 // thus we have to merge that.
3521 if (Expr *FromInitializer = D->getInClassInitializer()) {
3522 // We don't have yet the initializer set.
3523 if (FoundField->hasInClassInitializer() &&
3524 !FoundField->getInClassInitializer()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003525 if (ExpectedExpr ToInitializerOrErr = import(FromInitializer))
3526 FoundField->setInClassInitializer(*ToInitializerOrErr);
3527 else {
3528 // We can't return error here,
Gabor Marton42e15de2018-08-22 11:52:14 +00003529 // since we already mapped D as imported.
Balazs Keri3b30d652018-10-19 13:32:20 +00003530 // FIXME: warning message?
3531 consumeError(ToInitializerOrErr.takeError());
Gabor Marton42e15de2018-08-22 11:52:14 +00003532 return FoundField;
Balazs Keri3b30d652018-10-19 13:32:20 +00003533 }
Gabor Marton42e15de2018-08-22 11:52:14 +00003534 }
3535 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003536 return FoundField;
3537 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003538
Balazs Keri3b30d652018-10-19 13:32:20 +00003539 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003540 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003541 << Name << D->getType() << FoundField->getType();
3542 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3543 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003544
3545 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003546 }
3547 }
3548
Balazs Keri3b30d652018-10-19 13:32:20 +00003549 QualType ToType;
3550 TypeSourceInfo *ToTInfo;
3551 Expr *ToBitWidth;
3552 SourceLocation ToInnerLocStart;
3553 Expr *ToInitializer;
3554 if (auto Imp = importSeq(
3555 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(),
3556 D->getInnerLocStart(), D->getInClassInitializer()))
3557 std::tie(
3558 ToType, ToTInfo, ToBitWidth, ToInnerLocStart, ToInitializer) = *Imp;
3559 else
3560 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003561
Gabor Marton26f72a92018-07-12 09:42:05 +00003562 FieldDecl *ToField;
3563 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003564 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3565 ToType, ToTInfo, ToBitWidth, D->isMutable(),
3566 D->getInClassInitStyle()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003567 return ToField;
3568
Douglas Gregordd483172010-02-22 17:42:47 +00003569 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003570 ToField->setLexicalDeclContext(LexicalDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00003571 if (ToInitializer)
3572 ToField->setInClassInitializer(ToInitializer);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003573 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00003574 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003575 return ToField;
3576}
3577
Balazs Keri3b30d652018-10-19 13:32:20 +00003578ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003579 // Import the major distinguishing characteristics of a variable.
3580 DeclContext *DC, *LexicalDC;
3581 DeclarationName Name;
3582 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003583 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003584 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3585 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003586 if (ToD)
3587 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003588
Fangrui Song6907ce22018-07-30 19:24:48 +00003589 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003590 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003591 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003592 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003593 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003594 if (!Name &&
3595 ASTImporter::getFieldIndex(D) !=
3596 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003597 continue;
3598
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003599 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003600 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003601 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003602 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003603 return FoundField;
3604 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003605
3606 // If there are more anonymous fields to check, continue.
3607 if (!Name && I < N-1)
3608 continue;
3609
Balazs Keri3b30d652018-10-19 13:32:20 +00003610 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003611 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003612 << Name << D->getType() << FoundField->getType();
3613 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3614 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003615
3616 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003617 }
3618 }
3619
Francois Pichet783dd6e2010-11-21 06:08:52 +00003620 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003621 auto TypeOrErr = import(D->getType());
3622 if (!TypeOrErr)
3623 return TypeOrErr.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003624
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003625 auto **NamedChain =
3626 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00003627
3628 unsigned i = 0;
Balazs Keri3b30d652018-10-19 13:32:20 +00003629 for (auto *PI : D->chain())
3630 if (Expected<NamedDecl *> ToD = import(PI))
3631 NamedChain[i++] = *ToD;
3632 else
3633 return ToD.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003634
Gabor Marton26f72a92018-07-12 09:42:05 +00003635 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
3636 IndirectFieldDecl *ToIndirectField;
3637 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003638 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
Gabor Marton26f72a92018-07-12 09:42:05 +00003639 // FIXME here we leak `NamedChain` which is allocated before
3640 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00003641
Francois Pichet783dd6e2010-11-21 06:08:52 +00003642 ToIndirectField->setAccess(D->getAccess());
3643 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003644 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003645 return ToIndirectField;
3646}
3647
Balazs Keri3b30d652018-10-19 13:32:20 +00003648ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00003649 // Import the major distinguishing characteristics of a declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00003650 DeclContext *DC, *LexicalDC;
3651 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
3652 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003653
3654 // Determine whether we've already imported this decl.
Gabor Marton54058b52018-12-17 13:53:12 +00003655 // FriendDecl is not a NamedDecl so we cannot use lookup.
Aleksei Sidorina693b372016-09-28 10:16:56 +00003656 auto *RD = cast<CXXRecordDecl>(DC);
3657 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003658
3659 while (ImportedFriend) {
3660 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003661 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3662 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003663 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003664
3665 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3666 if (Importer.IsStructurallyEquivalent(
3667 D->getFriendType()->getType(),
3668 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003669 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003670 }
3671 ImportedFriend = ImportedFriend->getNextFriend();
3672 }
3673
3674 // Not found. Create it.
3675 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003676 if (NamedDecl *FriendD = D->getFriendDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003677 NamedDecl *ToFriendD;
3678 if (Error Err = importInto(ToFriendD, FriendD))
3679 return std::move(Err);
3680
3681 if (FriendD->getFriendObjectKind() != Decl::FOK_None &&
Peter Szecsib180eeb2018-04-25 17:28:03 +00003682 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3683 ToFriendD->setObjectOfFriendDecl(false);
3684
3685 ToFU = ToFriendD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003686 } else { // The friend is a type, not a decl.
3687 if (auto TSIOrErr = import(D->getFriendType()))
3688 ToFU = *TSIOrErr;
3689 else
3690 return TSIOrErr.takeError();
3691 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00003692
3693 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003694 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003695 for (unsigned I = 0; I < D->NumTPLists; I++) {
Balazs Keridec09162019-03-20 15:42:42 +00003696 if (auto ListOrErr = import(FromTPLists[I]))
Balazs Keri3b30d652018-10-19 13:32:20 +00003697 ToTPLists[I] = *ListOrErr;
3698 else
3699 return ListOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003700 }
3701
Balazs Keri3b30d652018-10-19 13:32:20 +00003702 auto LocationOrErr = import(D->getLocation());
3703 if (!LocationOrErr)
3704 return LocationOrErr.takeError();
3705 auto FriendLocOrErr = import(D->getFriendLoc());
3706 if (!FriendLocOrErr)
3707 return FriendLocOrErr.takeError();
3708
Gabor Marton26f72a92018-07-12 09:42:05 +00003709 FriendDecl *FrD;
3710 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003711 *LocationOrErr, ToFU,
3712 *FriendLocOrErr, ToTPLists))
Gabor Marton26f72a92018-07-12 09:42:05 +00003713 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003714
3715 FrD->setAccess(D->getAccess());
3716 FrD->setLexicalDeclContext(LexicalDC);
3717 LexicalDC->addDeclInternal(FrD);
3718 return FrD;
3719}
3720
Balazs Keri3b30d652018-10-19 13:32:20 +00003721ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003722 // Import the major distinguishing characteristics of an ivar.
3723 DeclContext *DC, *LexicalDC;
3724 DeclarationName Name;
3725 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003726 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003727 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3728 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003729 if (ToD)
3730 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003731
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003732 // Determine whether we've already imported this ivar
Gabor Marton54058b52018-12-17 13:53:12 +00003733 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003734 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003735 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003736 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003737 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003738 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003739 return FoundIvar;
3740 }
3741
Gabor Marton410f32c2019-04-01 15:29:55 +00003742 Importer.ToDiag(Loc, diag::warn_odr_ivar_type_inconsistent)
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003743 << Name << D->getType() << FoundIvar->getType();
3744 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3745 << FoundIvar->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003746
3747 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003748 }
3749 }
3750
Balazs Keri3b30d652018-10-19 13:32:20 +00003751 QualType ToType;
3752 TypeSourceInfo *ToTypeSourceInfo;
3753 Expr *ToBitWidth;
3754 SourceLocation ToInnerLocStart;
3755 if (auto Imp = importSeq(
3756 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(), D->getInnerLocStart()))
3757 std::tie(ToType, ToTypeSourceInfo, ToBitWidth, ToInnerLocStart) = *Imp;
3758 else
3759 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003760
Gabor Marton26f72a92018-07-12 09:42:05 +00003761 ObjCIvarDecl *ToIvar;
3762 if (GetImportedOrCreateDecl(
3763 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003764 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3765 ToType, ToTypeSourceInfo,
3766 D->getAccessControl(),ToBitWidth, D->getSynthesize()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003767 return ToIvar;
3768
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003769 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003770 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003771 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003772}
3773
Balazs Keri3b30d652018-10-19 13:32:20 +00003774ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003775
3776 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
3777 auto RedeclIt = Redecls.begin();
3778 // Import the first part of the decl chain. I.e. import all previous
3779 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003780 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3781 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3782 if (!RedeclOrErr)
3783 return RedeclOrErr.takeError();
3784 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003785 assert(*RedeclIt == D);
3786
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003787 // Import the major distinguishing characteristics of a variable.
3788 DeclContext *DC, *LexicalDC;
3789 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003790 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003791 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003792 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3793 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003794 if (ToD)
3795 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003796
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003797 // Try to find a variable in our own ("to") context with the same name and
3798 // in the same context as the variable we're importing.
Gabor Martonac3a5d62018-09-17 12:04:52 +00003799 VarDecl *FoundByLookup = nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003800 if (D->isFileVarDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003801 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003802 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00003803 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003804 for (auto *FoundDecl : FoundDecls) {
3805 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003806 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003807
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003808 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00003809 if (!hasSameVisibilityContext(FoundVar, D))
3810 continue;
3811 if (Importer.IsStructurallyEquivalent(D->getType(),
3812 FoundVar->getType())) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003813
Gabor Marton458d1452019-02-14 13:07:03 +00003814 // The VarDecl in the "From" context has a definition, but in the
3815 // "To" context we already have a definition.
3816 VarDecl *FoundDef = FoundVar->getDefinition();
3817 if (D->isThisDeclarationADefinition() && FoundDef)
3818 // FIXME Check for ODR error if the two definitions have
3819 // different initializers?
3820 return Importer.MapImported(D, FoundDef);
Gabor Martonac3a5d62018-09-17 12:04:52 +00003821
Gabor Marton458d1452019-02-14 13:07:03 +00003822 // The VarDecl in the "From" context has an initializer, but in the
3823 // "To" context we already have an initializer.
3824 const VarDecl *FoundDInit = nullptr;
3825 if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
3826 // FIXME Diagnose ODR error if the two initializers are different?
3827 return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit));
3828
3829 FoundByLookup = FoundVar;
3830 break;
3831 }
3832
3833 const ArrayType *FoundArray
3834 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3835 const ArrayType *TArray
3836 = Importer.getToContext().getAsArrayType(D->getType());
3837 if (FoundArray && TArray) {
3838 if (isa<IncompleteArrayType>(FoundArray) &&
3839 isa<ConstantArrayType>(TArray)) {
3840 // Import the type.
3841 if (auto TyOrErr = import(D->getType()))
3842 FoundVar->setType(*TyOrErr);
3843 else
3844 return TyOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00003845
3846 FoundByLookup = FoundVar;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003847 break;
Gabor Marton458d1452019-02-14 13:07:03 +00003848 } else if (isa<IncompleteArrayType>(TArray) &&
3849 isa<ConstantArrayType>(FoundArray)) {
3850 FoundByLookup = FoundVar;
3851 break;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003852 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003853 }
Gabor Marton458d1452019-02-14 13:07:03 +00003854
Gabor Marton410f32c2019-04-01 15:29:55 +00003855 Importer.ToDiag(Loc, diag::warn_odr_variable_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00003856 << Name << D->getType() << FoundVar->getType();
3857 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3858 << FoundVar->getType();
Gabor Martonf035b752019-08-27 11:36:10 +00003859 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003860 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003861 }
3862
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003863 if (!ConflictingDecls.empty()) {
Gabor Martonf035b752019-08-27 11:36:10 +00003864 ExpectedName NameOrErr = Importer.HandleNameConflict(
3865 Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size());
3866 if (NameOrErr)
3867 Name = NameOrErr.get();
3868 else
3869 return NameOrErr.takeError();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003870 }
3871 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003872
Balazs Keri3b30d652018-10-19 13:32:20 +00003873 QualType ToType;
3874 TypeSourceInfo *ToTypeSourceInfo;
3875 SourceLocation ToInnerLocStart;
3876 NestedNameSpecifierLoc ToQualifierLoc;
3877 if (auto Imp = importSeq(
3878 D->getType(), D->getTypeSourceInfo(), D->getInnerLocStart(),
3879 D->getQualifierLoc()))
3880 std::tie(ToType, ToTypeSourceInfo, ToInnerLocStart, ToQualifierLoc) = *Imp;
3881 else
3882 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003883
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003884 // Create the imported variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00003885 VarDecl *ToVar;
3886 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003887 ToInnerLocStart, Loc,
3888 Name.getAsIdentifierInfo(),
3889 ToType, ToTypeSourceInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003890 D->getStorageClass()))
3891 return ToVar;
3892
Balazs Keri3b30d652018-10-19 13:32:20 +00003893 ToVar->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003894 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003895 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003896
Gabor Martonac3a5d62018-09-17 12:04:52 +00003897 if (FoundByLookup) {
3898 auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
3899 ToVar->setPreviousDecl(Recent);
3900 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003901
Balazs Keri3b30d652018-10-19 13:32:20 +00003902 if (Error Err = ImportInitializer(D, ToVar))
3903 return std::move(Err);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003904
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003905 if (D->isConstexpr())
3906 ToVar->setConstexpr(true);
3907
Gabor Martonbc5b7e22019-12-04 17:12:08 +01003908 addDeclToContexts(D, ToVar);
Gabor Martonac3a5d62018-09-17 12:04:52 +00003909
3910 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003911 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3912 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3913 if (!RedeclOrErr)
3914 return RedeclOrErr.takeError();
3915 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003916
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003917 return ToVar;
3918}
3919
Balazs Keri3b30d652018-10-19 13:32:20 +00003920ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
Douglas Gregor8b228d72010-02-17 21:22:52 +00003921 // Parameters are created in the translation unit's context, then moved
3922 // into the function declaration's context afterward.
3923 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003924
Balazs Keri3b30d652018-10-19 13:32:20 +00003925 DeclarationName ToDeclName;
3926 SourceLocation ToLocation;
3927 QualType ToType;
3928 if (auto Imp = importSeq(D->getDeclName(), D->getLocation(), D->getType()))
3929 std::tie(ToDeclName, ToLocation, ToType) = *Imp;
3930 else
3931 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003932
Douglas Gregor8b228d72010-02-17 21:22:52 +00003933 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003934 ImplicitParamDecl *ToParm = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003935 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3936 ToLocation, ToDeclName.getAsIdentifierInfo(),
3937 ToType, D->getParameterKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003938 return ToParm;
3939 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003940}
3941
Balazs Keric5095942019-08-14 09:41:39 +00003942Error ASTNodeImporter::ImportDefaultArgOfParmVarDecl(
3943 const ParmVarDecl *FromParam, ParmVarDecl *ToParam) {
3944 ToParam->setHasInheritedDefaultArg(FromParam->hasInheritedDefaultArg());
3945 ToParam->setKNRPromoted(FromParam->isKNRPromoted());
3946
3947 if (FromParam->hasUninstantiatedDefaultArg()) {
3948 if (auto ToDefArgOrErr = import(FromParam->getUninstantiatedDefaultArg()))
3949 ToParam->setUninstantiatedDefaultArg(*ToDefArgOrErr);
3950 else
3951 return ToDefArgOrErr.takeError();
3952 } else if (FromParam->hasUnparsedDefaultArg()) {
3953 ToParam->setUnparsedDefaultArg();
3954 } else if (FromParam->hasDefaultArg()) {
3955 if (auto ToDefArgOrErr = import(FromParam->getDefaultArg()))
3956 ToParam->setDefaultArg(*ToDefArgOrErr);
3957 else
3958 return ToDefArgOrErr.takeError();
3959 }
3960
3961 return Error::success();
3962}
3963
Balazs Keri3b30d652018-10-19 13:32:20 +00003964ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003965 // Parameters are created in the translation unit's context, then moved
3966 // into the function declaration's context afterward.
3967 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003968
Balazs Keri3b30d652018-10-19 13:32:20 +00003969 DeclarationName ToDeclName;
3970 SourceLocation ToLocation, ToInnerLocStart;
3971 QualType ToType;
3972 TypeSourceInfo *ToTypeSourceInfo;
3973 if (auto Imp = importSeq(
3974 D->getDeclName(), D->getLocation(), D->getType(), D->getInnerLocStart(),
3975 D->getTypeSourceInfo()))
3976 std::tie(
3977 ToDeclName, ToLocation, ToType, ToInnerLocStart,
3978 ToTypeSourceInfo) = *Imp;
3979 else
3980 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003981
Gabor Marton26f72a92018-07-12 09:42:05 +00003982 ParmVarDecl *ToParm;
3983 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003984 ToInnerLocStart, ToLocation,
3985 ToDeclName.getAsIdentifierInfo(), ToType,
3986 ToTypeSourceInfo, D->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003987 /*DefaultArg*/ nullptr))
3988 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003989
Balazs Keric5095942019-08-14 09:41:39 +00003990 // Set the default argument. It should be no problem if it was already done.
3991 // Do not import the default expression before GetImportedOrCreateDecl call
3992 // to avoid possible infinite import loop because circular dependency.
3993 if (Error Err = ImportDefaultArgOfParmVarDecl(D, ToParm))
3994 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003995
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003996 if (D->isObjCMethodParameter()) {
3997 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3998 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3999 } else {
4000 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
4001 D->getFunctionScopeIndex());
4002 }
4003
Gabor Marton26f72a92018-07-12 09:42:05 +00004004 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00004005}
4006
Balazs Keri3b30d652018-10-19 13:32:20 +00004007ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Douglas Gregor43f54792010-02-17 02:12:47 +00004008 // Import the major distinguishing characteristics of a method.
4009 DeclContext *DC, *LexicalDC;
4010 DeclarationName Name;
4011 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004012 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004013 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4014 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004015 if (ToD)
4016 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004017
Gabor Marton54058b52018-12-17 13:53:12 +00004018 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004019 for (auto *FoundDecl : FoundDecls) {
4020 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00004021 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
4022 continue;
4023
4024 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00004025 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
4026 FoundMethod->getReturnType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00004027 Importer.ToDiag(Loc, diag::warn_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00004028 << D->isInstanceMethod() << Name << D->getReturnType()
4029 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00004030 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00004031 diag::note_odr_objc_method_here)
4032 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00004033
4034 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00004035 }
4036
4037 // Check the number of parameters.
4038 if (D->param_size() != FoundMethod->param_size()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00004039 Importer.ToDiag(Loc, diag::warn_odr_objc_method_num_params_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00004040 << D->isInstanceMethod() << Name
4041 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00004042 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00004043 diag::note_odr_objc_method_here)
4044 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00004045
4046 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00004047 }
4048
4049 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004050 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00004051 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
4052 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004053 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00004054 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004055 Importer.FromDiag((*P)->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004056 diag::warn_odr_objc_method_param_type_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00004057 << D->isInstanceMethod() << Name
4058 << (*P)->getType() << (*FoundP)->getType();
4059 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
4060 << (*FoundP)->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004061
4062 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00004063 }
4064 }
4065
4066 // Check variadic/non-variadic.
4067 // Check the number of parameters.
4068 if (D->isVariadic() != FoundMethod->isVariadic()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00004069 Importer.ToDiag(Loc, diag::warn_odr_objc_method_variadic_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00004070 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00004071 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00004072 diag::note_odr_objc_method_here)
4073 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00004074
4075 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00004076 }
4077
4078 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00004079 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00004080 }
4081 }
4082
Balazs Keri3b30d652018-10-19 13:32:20 +00004083 SourceLocation ToEndLoc;
4084 QualType ToReturnType;
4085 TypeSourceInfo *ToReturnTypeSourceInfo;
4086 if (auto Imp = importSeq(
4087 D->getEndLoc(), D->getReturnType(), D->getReturnTypeSourceInfo()))
4088 std::tie(ToEndLoc, ToReturnType, ToReturnTypeSourceInfo) = *Imp;
4089 else
4090 return Imp.takeError();
Douglas Gregor12852d92010-03-08 14:59:44 +00004091
Gabor Marton26f72a92018-07-12 09:42:05 +00004092 ObjCMethodDecl *ToMethod;
4093 if (GetImportedOrCreateDecl(
Adrian Prantl2073dd22019-11-04 14:28:14 -08004094 ToMethod, D, Importer.getToContext(), Loc, ToEndLoc,
4095 Name.getObjCSelector(), ToReturnType, ToReturnTypeSourceInfo, DC,
4096 D->isInstanceMethod(), D->isVariadic(), D->isPropertyAccessor(),
4097 D->isSynthesizedAccessorStub(), D->isImplicit(), D->isDefined(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004098 D->getImplementationControl(), D->hasRelatedResultType()))
4099 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00004100
4101 // FIXME: When we decide to merge method definitions, we'll need to
4102 // deal with implicit parameters.
4103
4104 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004105 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00004106 for (auto *FromP : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004107 if (Expected<ParmVarDecl *> ToPOrErr = import(FromP))
4108 ToParams.push_back(*ToPOrErr);
4109 else
4110 return ToPOrErr.takeError();
Douglas Gregor43f54792010-02-17 02:12:47 +00004111 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004112
Douglas Gregor43f54792010-02-17 02:12:47 +00004113 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004114 for (auto *ToParam : ToParams) {
4115 ToParam->setOwningFunction(ToMethod);
4116 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00004117 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00004118
Balazs Keri3b30d652018-10-19 13:32:20 +00004119 SmallVector<SourceLocation, 12> FromSelLocs;
4120 D->getSelectorLocs(FromSelLocs);
4121 SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size());
4122 if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs))
4123 return std::move(Err);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00004124
Balazs Keri3b30d652018-10-19 13:32:20 +00004125 ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00004126
4127 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004128 LexicalDC->addDeclInternal(ToMethod);
Raphael Isemann164e0fc2019-12-06 18:10:23 +01004129
4130 // Implicit params are declared when Sema encounters the definition but this
4131 // never happens when the method is imported. Manually declare the implicit
4132 // params now that the MethodDecl knows its class interface.
4133 if (D->getSelfDecl())
4134 ToMethod->createImplicitParams(Importer.getToContext(),
4135 ToMethod->getClassInterface());
4136
Douglas Gregor43f54792010-02-17 02:12:47 +00004137 return ToMethod;
4138}
4139
Balazs Keri3b30d652018-10-19 13:32:20 +00004140ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00004141 // Import the major distinguishing characteristics of a category.
4142 DeclContext *DC, *LexicalDC;
4143 DeclarationName Name;
4144 SourceLocation Loc;
4145 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004146 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4147 return std::move(Err);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004148 if (ToD)
4149 return ToD;
4150
Balazs Keri3b30d652018-10-19 13:32:20 +00004151 SourceLocation ToVarianceLoc, ToLocation, ToColonLoc;
4152 TypeSourceInfo *ToTypeSourceInfo;
4153 if (auto Imp = importSeq(
4154 D->getVarianceLoc(), D->getLocation(), D->getColonLoc(),
4155 D->getTypeSourceInfo()))
4156 std::tie(ToVarianceLoc, ToLocation, ToColonLoc, ToTypeSourceInfo) = *Imp;
4157 else
4158 return Imp.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004159
Gabor Marton26f72a92018-07-12 09:42:05 +00004160 ObjCTypeParamDecl *Result;
4161 if (GetImportedOrCreateDecl(
4162 Result, D, Importer.getToContext(), DC, D->getVariance(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004163 ToVarianceLoc, D->getIndex(),
4164 ToLocation, Name.getAsIdentifierInfo(),
4165 ToColonLoc, ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00004166 return Result;
4167
Douglas Gregor85f3f952015-07-07 03:57:15 +00004168 Result->setLexicalDeclContext(LexicalDC);
4169 return Result;
4170}
4171
Balazs Keri3b30d652018-10-19 13:32:20 +00004172ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00004173 // Import the major distinguishing characteristics of a category.
4174 DeclContext *DC, *LexicalDC;
4175 DeclarationName Name;
4176 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004177 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004178 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4179 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004180 if (ToD)
4181 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004182
Balazs Keri3b30d652018-10-19 13:32:20 +00004183 ObjCInterfaceDecl *ToInterface;
4184 if (Error Err = importInto(ToInterface, D->getClassInterface()))
4185 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004186
Douglas Gregor84c51c32010-02-18 01:47:50 +00004187 // Determine if we've already encountered this category.
4188 ObjCCategoryDecl *MergeWithCategory
4189 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
4190 ObjCCategoryDecl *ToCategory = MergeWithCategory;
4191 if (!ToCategory) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004192 SourceLocation ToAtStartLoc, ToCategoryNameLoc;
4193 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4194 if (auto Imp = importSeq(
4195 D->getAtStartLoc(), D->getCategoryNameLoc(),
4196 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4197 std::tie(
4198 ToAtStartLoc, ToCategoryNameLoc,
4199 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4200 else
4201 return Imp.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004202
4203 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004204 ToAtStartLoc, Loc,
4205 ToCategoryNameLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00004206 Name.getAsIdentifierInfo(), ToInterface,
4207 /*TypeParamList=*/nullptr,
Balazs Keri3b30d652018-10-19 13:32:20 +00004208 ToIvarLBraceLoc,
4209 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004210 return ToCategory;
4211
Douglas Gregor84c51c32010-02-18 01:47:50 +00004212 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004213 LexicalDC->addDeclInternal(ToCategory);
Balazs Keri3b30d652018-10-19 13:32:20 +00004214 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004215 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004216 if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList()))
4217 ToCategory->setTypeParamList(*PListOrErr);
4218 else
4219 return PListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004220
Douglas Gregor84c51c32010-02-18 01:47:50 +00004221 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004222 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4223 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00004224 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
4225 = D->protocol_loc_begin();
4226 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
4227 FromProtoEnd = D->protocol_end();
4228 FromProto != FromProtoEnd;
4229 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004230 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4231 Protocols.push_back(*ToProtoOrErr);
4232 else
4233 return ToProtoOrErr.takeError();
4234
4235 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4236 ProtocolLocs.push_back(*ToProtoLocOrErr);
4237 else
4238 return ToProtoLocOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004239 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004240
Douglas Gregor84c51c32010-02-18 01:47:50 +00004241 // FIXME: If we're merging, make sure that the protocol list is the same.
4242 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
4243 ProtocolLocs.data(), Importer.getToContext());
Balazs Keri3b30d652018-10-19 13:32:20 +00004244
Douglas Gregor84c51c32010-02-18 01:47:50 +00004245 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004246 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00004247 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004248
Douglas Gregor84c51c32010-02-18 01:47:50 +00004249 // Import all of the members of this category.
Balazs Keri3b30d652018-10-19 13:32:20 +00004250 if (Error Err = ImportDeclContext(D))
4251 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00004252
Douglas Gregor84c51c32010-02-18 01:47:50 +00004253 // If we have an implementation, import it as well.
4254 if (D->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004255 if (Expected<ObjCCategoryImplDecl *> ToImplOrErr =
4256 import(D->getImplementation()))
4257 ToCategory->setImplementation(*ToImplOrErr);
4258 else
4259 return ToImplOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004260 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004261
Douglas Gregor84c51c32010-02-18 01:47:50 +00004262 return ToCategory;
4263}
4264
Balazs Keri3b30d652018-10-19 13:32:20 +00004265Error ASTNodeImporter::ImportDefinition(
4266 ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004267 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00004268 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004269 if (Error Err = ImportDeclContext(From))
4270 return Err;
4271 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004272 }
4273
4274 // Start the protocol definition
4275 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004276
Douglas Gregor2aa53772012-01-24 17:42:07 +00004277 // Import protocols
4278 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4279 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004280 ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc =
4281 From->protocol_loc_begin();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004282 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
4283 FromProtoEnd = From->protocol_end();
4284 FromProto != FromProtoEnd;
4285 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004286 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4287 Protocols.push_back(*ToProtoOrErr);
4288 else
4289 return ToProtoOrErr.takeError();
4290
4291 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4292 ProtocolLocs.push_back(*ToProtoLocOrErr);
4293 else
4294 return ToProtoLocOrErr.takeError();
4295
Douglas Gregor2aa53772012-01-24 17:42:07 +00004296 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004297
Douglas Gregor2aa53772012-01-24 17:42:07 +00004298 // FIXME: If we're merging, make sure that the protocol list is the same.
4299 To->setProtocolList(Protocols.data(), Protocols.size(),
4300 ProtocolLocs.data(), Importer.getToContext());
4301
Douglas Gregor2e15c842012-02-01 21:00:38 +00004302 if (shouldForceImportDeclContext(Kind)) {
4303 // Import all of the members of this protocol.
Balazs Keri3b30d652018-10-19 13:32:20 +00004304 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4305 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004306 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004307 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004308}
4309
Balazs Keri3b30d652018-10-19 13:32:20 +00004310ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004311 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00004312 // from, but this particular declaration is not that definition, import the
4313 // definition and map to that.
4314 ObjCProtocolDecl *Definition = D->getDefinition();
4315 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004316 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4317 return Importer.MapImported(D, *ImportedDefOrErr);
4318 else
4319 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004320 }
4321
Douglas Gregor84c51c32010-02-18 01:47:50 +00004322 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004323 DeclContext *DC, *LexicalDC;
4324 DeclarationName Name;
4325 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004326 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004327 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4328 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004329 if (ToD)
4330 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004331
Craig Topper36250ad2014-05-12 05:36:57 +00004332 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004333 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004334 for (auto *FoundDecl : FoundDecls) {
4335 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004336 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004337
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004338 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004339 break;
4340 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004341
Douglas Gregor98d156a2010-02-17 16:12:00 +00004342 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004343 if (!ToProto) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004344 auto ToAtBeginLocOrErr = import(D->getAtStartLoc());
4345 if (!ToAtBeginLocOrErr)
4346 return ToAtBeginLocOrErr.takeError();
4347
Gabor Marton26f72a92018-07-12 09:42:05 +00004348 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
4349 Name.getAsIdentifierInfo(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004350 *ToAtBeginLocOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004351 /*PrevDecl=*/nullptr))
4352 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004353 ToProto->setLexicalDeclContext(LexicalDC);
4354 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004355 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004356
4357 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004358
Balazs Keri3b30d652018-10-19 13:32:20 +00004359 if (D->isThisDeclarationADefinition())
4360 if (Error Err = ImportDefinition(D, ToProto))
4361 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004362
Douglas Gregor98d156a2010-02-17 16:12:00 +00004363 return ToProto;
4364}
4365
Balazs Keri3b30d652018-10-19 13:32:20 +00004366ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4367 DeclContext *DC, *LexicalDC;
4368 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4369 return std::move(Err);
Sean Callanan0aae0412014-12-10 00:00:37 +00004370
Balazs Keri3b30d652018-10-19 13:32:20 +00004371 ExpectedSLoc ExternLocOrErr = import(D->getExternLoc());
4372 if (!ExternLocOrErr)
4373 return ExternLocOrErr.takeError();
4374
4375 ExpectedSLoc LangLocOrErr = import(D->getLocation());
4376 if (!LangLocOrErr)
4377 return LangLocOrErr.takeError();
Sean Callanan0aae0412014-12-10 00:00:37 +00004378
4379 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00004380
4381 LinkageSpecDecl *ToLinkageSpec;
4382 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004383 *ExternLocOrErr, *LangLocOrErr,
4384 D->getLanguage(), HasBraces))
Gabor Marton26f72a92018-07-12 09:42:05 +00004385 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00004386
4387 if (HasBraces) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004388 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
4389 if (!RBraceLocOrErr)
4390 return RBraceLocOrErr.takeError();
4391 ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr);
Sean Callanan0aae0412014-12-10 00:00:37 +00004392 }
4393
4394 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4395 LexicalDC->addDeclInternal(ToLinkageSpec);
4396
Sean Callanan0aae0412014-12-10 00:00:37 +00004397 return ToLinkageSpec;
4398}
4399
Balazs Keri3b30d652018-10-19 13:32:20 +00004400ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004401 DeclContext *DC, *LexicalDC;
4402 DeclarationName Name;
4403 SourceLocation Loc;
4404 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004405 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4406 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004407 if (ToD)
4408 return ToD;
4409
Balazs Keri3b30d652018-10-19 13:32:20 +00004410 SourceLocation ToLoc, ToUsingLoc;
4411 NestedNameSpecifierLoc ToQualifierLoc;
4412 if (auto Imp = importSeq(
4413 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc()))
4414 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc) = *Imp;
4415 else
4416 return Imp.takeError();
4417
4418 DeclarationNameInfo NameInfo(Name, ToLoc);
4419 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4420 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004421
Gabor Marton26f72a92018-07-12 09:42:05 +00004422 UsingDecl *ToUsing;
4423 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004424 ToUsingLoc, ToQualifierLoc, NameInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00004425 D->hasTypename()))
4426 return ToUsing;
4427
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004428 ToUsing->setLexicalDeclContext(LexicalDC);
4429 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004430
4431 if (NamedDecl *FromPattern =
4432 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004433 if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern))
4434 Importer.getToContext().setInstantiatedFromUsingDecl(
4435 ToUsing, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004436 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004437 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004438 }
4439
Balazs Keri3b30d652018-10-19 13:32:20 +00004440 for (UsingShadowDecl *FromShadow : D->shadows()) {
4441 if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow))
4442 ToUsing->addShadowDecl(*ToShadowOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004443 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004444 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004445 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004446 return ToShadowOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004447 }
4448 return ToUsing;
4449}
4450
Balazs Keri3b30d652018-10-19 13:32:20 +00004451ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004452 DeclContext *DC, *LexicalDC;
4453 DeclarationName Name;
4454 SourceLocation Loc;
4455 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004456 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4457 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004458 if (ToD)
4459 return ToD;
4460
Balazs Keri3b30d652018-10-19 13:32:20 +00004461 Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl());
4462 if (!ToUsingOrErr)
4463 return ToUsingOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004464
Balazs Keri3b30d652018-10-19 13:32:20 +00004465 Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl());
4466 if (!ToTargetOrErr)
4467 return ToTargetOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004468
Gabor Marton26f72a92018-07-12 09:42:05 +00004469 UsingShadowDecl *ToShadow;
4470 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004471 *ToUsingOrErr, *ToTargetOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004472 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004473
4474 ToShadow->setLexicalDeclContext(LexicalDC);
4475 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004476
4477 if (UsingShadowDecl *FromPattern =
4478 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004479 if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern))
4480 Importer.getToContext().setInstantiatedFromUsingShadowDecl(
4481 ToShadow, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004482 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004483 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004484 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004485 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004486 }
4487
4488 LexicalDC->addDeclInternal(ToShadow);
4489
4490 return ToShadow;
4491}
4492
Balazs Keri3b30d652018-10-19 13:32:20 +00004493ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004494 DeclContext *DC, *LexicalDC;
4495 DeclarationName Name;
4496 SourceLocation Loc;
4497 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004498 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4499 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004500 if (ToD)
4501 return ToD;
4502
Balazs Keri3b30d652018-10-19 13:32:20 +00004503 auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor());
4504 if (!ToComAncestorOrErr)
4505 return ToComAncestorOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004506
Balazs Keri3b30d652018-10-19 13:32:20 +00004507 NamespaceDecl *ToNominatedNamespace;
4508 SourceLocation ToUsingLoc, ToNamespaceKeyLocation, ToIdentLocation;
4509 NestedNameSpecifierLoc ToQualifierLoc;
4510 if (auto Imp = importSeq(
4511 D->getNominatedNamespace(), D->getUsingLoc(),
4512 D->getNamespaceKeyLocation(), D->getQualifierLoc(),
4513 D->getIdentLocation()))
4514 std::tie(
4515 ToNominatedNamespace, ToUsingLoc, ToNamespaceKeyLocation,
4516 ToQualifierLoc, ToIdentLocation) = *Imp;
4517 else
4518 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004519
Gabor Marton26f72a92018-07-12 09:42:05 +00004520 UsingDirectiveDecl *ToUsingDir;
4521 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004522 ToUsingLoc,
4523 ToNamespaceKeyLocation,
4524 ToQualifierLoc,
4525 ToIdentLocation,
4526 ToNominatedNamespace, *ToComAncestorOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004527 return ToUsingDir;
4528
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004529 ToUsingDir->setLexicalDeclContext(LexicalDC);
4530 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004531
4532 return ToUsingDir;
4533}
4534
Balazs Keri3b30d652018-10-19 13:32:20 +00004535ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004536 UnresolvedUsingValueDecl *D) {
4537 DeclContext *DC, *LexicalDC;
4538 DeclarationName Name;
4539 SourceLocation Loc;
4540 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004541 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4542 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004543 if (ToD)
4544 return ToD;
4545
Balazs Keri3b30d652018-10-19 13:32:20 +00004546 SourceLocation ToLoc, ToUsingLoc, ToEllipsisLoc;
4547 NestedNameSpecifierLoc ToQualifierLoc;
4548 if (auto Imp = importSeq(
4549 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc(),
4550 D->getEllipsisLoc()))
4551 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4552 else
4553 return Imp.takeError();
4554
4555 DeclarationNameInfo NameInfo(Name, ToLoc);
4556 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4557 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004558
Gabor Marton26f72a92018-07-12 09:42:05 +00004559 UnresolvedUsingValueDecl *ToUsingValue;
4560 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004561 ToUsingLoc, ToQualifierLoc, NameInfo,
4562 ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004563 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004564
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004565 ToUsingValue->setAccess(D->getAccess());
4566 ToUsingValue->setLexicalDeclContext(LexicalDC);
4567 LexicalDC->addDeclInternal(ToUsingValue);
4568
4569 return ToUsingValue;
4570}
4571
Balazs Keri3b30d652018-10-19 13:32:20 +00004572ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004573 UnresolvedUsingTypenameDecl *D) {
4574 DeclContext *DC, *LexicalDC;
4575 DeclarationName Name;
4576 SourceLocation Loc;
4577 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004578 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4579 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004580 if (ToD)
4581 return ToD;
4582
Balazs Keri3b30d652018-10-19 13:32:20 +00004583 SourceLocation ToUsingLoc, ToTypenameLoc, ToEllipsisLoc;
4584 NestedNameSpecifierLoc ToQualifierLoc;
4585 if (auto Imp = importSeq(
4586 D->getUsingLoc(), D->getTypenameLoc(), D->getQualifierLoc(),
4587 D->getEllipsisLoc()))
4588 std::tie(ToUsingLoc, ToTypenameLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4589 else
4590 return Imp.takeError();
4591
Gabor Marton26f72a92018-07-12 09:42:05 +00004592 UnresolvedUsingTypenameDecl *ToUsing;
4593 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004594 ToUsingLoc, ToTypenameLoc,
4595 ToQualifierLoc, Loc, Name, ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004596 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004597
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004598 ToUsing->setAccess(D->getAccess());
4599 ToUsing->setLexicalDeclContext(LexicalDC);
4600 LexicalDC->addDeclInternal(ToUsing);
4601
4602 return ToUsing;
4603}
4604
Raphael Isemannba7bde62019-10-30 14:50:35 +01004605ExpectedDecl ASTNodeImporter::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
4606 Decl* ToD = nullptr;
4607 switch (D->getBuiltinTemplateKind()) {
4608 case BuiltinTemplateKind::BTK__make_integer_seq:
4609 ToD = Importer.getToContext().getMakeIntegerSeqDecl();
4610 break;
4611 case BuiltinTemplateKind::BTK__type_pack_element:
4612 ToD = Importer.getToContext().getTypePackElementDecl();
4613 break;
4614 }
4615 assert(ToD && "BuiltinTemplateDecl of unsupported kind!");
4616 Importer.MapImported(D, ToD);
4617 return ToD;
4618}
Balazs Keri3b30d652018-10-19 13:32:20 +00004619
4620Error ASTNodeImporter::ImportDefinition(
4621 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004622 if (To->getDefinition()) {
4623 // Check consistency of superclass.
4624 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4625 if (FromSuper) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004626 if (auto FromSuperOrErr = import(FromSuper))
4627 FromSuper = *FromSuperOrErr;
4628 else
4629 return FromSuperOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004630 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004631
4632 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004633 if ((bool)FromSuper != (bool)ToSuper ||
4634 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004635 Importer.ToDiag(To->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004636 diag::warn_odr_objc_superclass_inconsistent)
Douglas Gregor2aa53772012-01-24 17:42:07 +00004637 << To->getDeclName();
4638 if (ToSuper)
4639 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4640 << To->getSuperClass()->getDeclName();
4641 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004642 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004643 diag::note_odr_objc_missing_superclass);
4644 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00004645 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004646 diag::note_odr_objc_superclass)
4647 << From->getSuperClass()->getDeclName();
4648 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004649 Importer.FromDiag(From->getLocation(),
4650 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004651 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004652
Douglas Gregor2e15c842012-02-01 21:00:38 +00004653 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004654 if (Error Err = ImportDeclContext(From))
4655 return Err;
4656 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004657 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004658
Douglas Gregor2aa53772012-01-24 17:42:07 +00004659 // Start the definition.
4660 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004661
Douglas Gregor2aa53772012-01-24 17:42:07 +00004662 // If this class has a superclass, import it.
4663 if (From->getSuperClass()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004664 if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo()))
4665 To->setSuperClass(*SuperTInfoOrErr);
4666 else
4667 return SuperTInfoOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004668 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004669
Douglas Gregor2aa53772012-01-24 17:42:07 +00004670 // Import protocols
4671 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4672 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004673 ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc =
4674 From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00004675
Douglas Gregor2aa53772012-01-24 17:42:07 +00004676 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4677 FromProtoEnd = From->protocol_end();
4678 FromProto != FromProtoEnd;
4679 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004680 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4681 Protocols.push_back(*ToProtoOrErr);
4682 else
4683 return ToProtoOrErr.takeError();
4684
4685 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4686 ProtocolLocs.push_back(*ToProtoLocOrErr);
4687 else
4688 return ToProtoLocOrErr.takeError();
4689
Douglas Gregor2aa53772012-01-24 17:42:07 +00004690 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004691
Douglas Gregor2aa53772012-01-24 17:42:07 +00004692 // FIXME: If we're merging, make sure that the protocol list is the same.
4693 To->setProtocolList(Protocols.data(), Protocols.size(),
4694 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00004695
Douglas Gregor2aa53772012-01-24 17:42:07 +00004696 // Import categories. When the categories themselves are imported, they'll
4697 // hook themselves into this interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004698 for (auto *Cat : From->known_categories()) {
4699 auto ToCatOrErr = import(Cat);
4700 if (!ToCatOrErr)
4701 return ToCatOrErr.takeError();
4702 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004703
Douglas Gregor2aa53772012-01-24 17:42:07 +00004704 // If we have an @implementation, import it as well.
4705 if (From->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004706 if (Expected<ObjCImplementationDecl *> ToImplOrErr =
4707 import(From->getImplementation()))
4708 To->setImplementation(*ToImplOrErr);
4709 else
4710 return ToImplOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004711 }
4712
Douglas Gregor2e15c842012-02-01 21:00:38 +00004713 if (shouldForceImportDeclContext(Kind)) {
4714 // Import all of the members of this class.
Balazs Keri3b30d652018-10-19 13:32:20 +00004715 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4716 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004717 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004718 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004719}
4720
Balazs Keri3b30d652018-10-19 13:32:20 +00004721Expected<ObjCTypeParamList *>
Douglas Gregor85f3f952015-07-07 03:57:15 +00004722ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4723 if (!list)
4724 return nullptr;
4725
4726 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
Balazs Keri3b30d652018-10-19 13:32:20 +00004727 for (auto *fromTypeParam : *list) {
4728 if (auto toTypeParamOrErr = import(fromTypeParam))
4729 toTypeParams.push_back(*toTypeParamOrErr);
4730 else
4731 return toTypeParamOrErr.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004732 }
4733
Balazs Keri3b30d652018-10-19 13:32:20 +00004734 auto LAngleLocOrErr = import(list->getLAngleLoc());
4735 if (!LAngleLocOrErr)
4736 return LAngleLocOrErr.takeError();
4737
4738 auto RAngleLocOrErr = import(list->getRAngleLoc());
4739 if (!RAngleLocOrErr)
4740 return RAngleLocOrErr.takeError();
4741
Douglas Gregor85f3f952015-07-07 03:57:15 +00004742 return ObjCTypeParamList::create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004743 *LAngleLocOrErr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00004744 toTypeParams,
Balazs Keri3b30d652018-10-19 13:32:20 +00004745 *RAngleLocOrErr);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004746}
4747
Balazs Keri3b30d652018-10-19 13:32:20 +00004748ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004749 // If this class has a definition in the translation unit we're coming from,
4750 // but this particular declaration is not that definition, import the
4751 // definition and map to that.
4752 ObjCInterfaceDecl *Definition = D->getDefinition();
4753 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004754 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4755 return Importer.MapImported(D, *ImportedDefOrErr);
4756 else
4757 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004758 }
4759
Douglas Gregor45635322010-02-16 01:20:57 +00004760 // Import the major distinguishing characteristics of an @interface.
4761 DeclContext *DC, *LexicalDC;
4762 DeclarationName Name;
4763 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004764 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004765 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4766 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004767 if (ToD)
4768 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004769
Douglas Gregor2aa53772012-01-24 17:42:07 +00004770 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004771 ObjCInterfaceDecl *MergeWithIface = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004772 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004773 for (auto *FoundDecl : FoundDecls) {
4774 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004775 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004776
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004777 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004778 break;
4779 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004780
Douglas Gregor2aa53772012-01-24 17:42:07 +00004781 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004782 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004783 if (!ToIface) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004784 ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc());
4785 if (!AtBeginLocOrErr)
4786 return AtBeginLocOrErr.takeError();
4787
Gabor Marton26f72a92018-07-12 09:42:05 +00004788 if (GetImportedOrCreateDecl(
4789 ToIface, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004790 *AtBeginLocOrErr, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004791 /*TypeParamList=*/nullptr,
4792 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4793 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004794 ToIface->setLexicalDeclContext(LexicalDC);
4795 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004796 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004797 Importer.MapImported(D, ToIface);
Balazs Keri3b30d652018-10-19 13:32:20 +00004798 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004799 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004800 if (auto ToPListOrErr =
4801 ImportObjCTypeParamList(D->getTypeParamListAsWritten()))
4802 ToIface->setTypeParamList(*ToPListOrErr);
4803 else
4804 return ToPListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004805
Balazs Keri3b30d652018-10-19 13:32:20 +00004806 if (D->isThisDeclarationADefinition())
4807 if (Error Err = ImportDefinition(D, ToIface))
4808 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004809
Douglas Gregor98d156a2010-02-17 16:12:00 +00004810 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004811}
4812
Balazs Keri3b30d652018-10-19 13:32:20 +00004813ExpectedDecl
4814ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4815 ObjCCategoryDecl *Category;
4816 if (Error Err = importInto(Category, D->getCategoryDecl()))
4817 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004818
Douglas Gregor4da9d682010-12-07 15:32:12 +00004819 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4820 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004821 DeclContext *DC, *LexicalDC;
4822 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4823 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004824
Balazs Keri3b30d652018-10-19 13:32:20 +00004825 SourceLocation ToLocation, ToAtStartLoc, ToCategoryNameLoc;
4826 if (auto Imp = importSeq(
4827 D->getLocation(), D->getAtStartLoc(), D->getCategoryNameLoc()))
4828 std::tie(ToLocation, ToAtStartLoc, ToCategoryNameLoc) = *Imp;
4829 else
4830 return Imp.takeError();
4831
Gabor Marton26f72a92018-07-12 09:42:05 +00004832 if (GetImportedOrCreateDecl(
4833 ToImpl, D, Importer.getToContext(), DC,
4834 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004835 ToLocation, ToAtStartLoc, ToCategoryNameLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004836 return ToImpl;
4837
Balazs Keri3b30d652018-10-19 13:32:20 +00004838 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004839 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004840 Category->setImplementation(ToImpl);
4841 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004842
Gabor Marton26f72a92018-07-12 09:42:05 +00004843 Importer.MapImported(D, ToImpl);
Balazs Keri3b30d652018-10-19 13:32:20 +00004844 if (Error Err = ImportDeclContext(D))
4845 return std::move(Err);
4846
Douglas Gregor4da9d682010-12-07 15:32:12 +00004847 return ToImpl;
4848}
4849
Balazs Keri3b30d652018-10-19 13:32:20 +00004850ExpectedDecl
4851ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00004852 // Find the corresponding interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004853 ObjCInterfaceDecl *Iface;
4854 if (Error Err = importInto(Iface, D->getClassInterface()))
4855 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004856
4857 // Import the superclass, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00004858 ObjCInterfaceDecl *Super;
4859 if (Error Err = importInto(Super, D->getSuperClass()))
4860 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004861
4862 ObjCImplementationDecl *Impl = Iface->getImplementation();
4863 if (!Impl) {
4864 // We haven't imported an implementation yet. Create a new @implementation
4865 // now.
Balazs Keri3b30d652018-10-19 13:32:20 +00004866 DeclContext *DC, *LexicalDC;
4867 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4868 return std::move(Err);
4869
4870 SourceLocation ToLocation, ToAtStartLoc, ToSuperClassLoc;
4871 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4872 if (auto Imp = importSeq(
4873 D->getLocation(), D->getAtStartLoc(), D->getSuperClassLoc(),
4874 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4875 std::tie(
4876 ToLocation, ToAtStartLoc, ToSuperClassLoc,
4877 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4878 else
4879 return Imp.takeError();
4880
Gabor Marton26f72a92018-07-12 09:42:05 +00004881 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004882 DC, Iface, Super,
4883 ToLocation,
4884 ToAtStartLoc,
4885 ToSuperClassLoc,
4886 ToIvarLBraceLoc,
4887 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004888 return Impl;
4889
Balazs Keri3b30d652018-10-19 13:32:20 +00004890 Impl->setLexicalDeclContext(LexicalDC);
Gabor Marton26f72a92018-07-12 09:42:05 +00004891
Douglas Gregorda8025c2010-12-07 01:26:03 +00004892 // Associate the implementation with the class it implements.
4893 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004894 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004895 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004896 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004897
4898 // Verify that the existing @implementation has the same superclass.
4899 if ((Super && !Impl->getSuperClass()) ||
4900 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004901 (Super && Impl->getSuperClass() &&
4902 !declaresSameEntity(Super->getCanonicalDecl(),
4903 Impl->getSuperClass()))) {
4904 Importer.ToDiag(Impl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004905 diag::warn_odr_objc_superclass_inconsistent)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004906 << Iface->getDeclName();
4907 // FIXME: It would be nice to have the location of the superclass
4908 // below.
4909 if (Impl->getSuperClass())
4910 Importer.ToDiag(Impl->getLocation(),
4911 diag::note_odr_objc_superclass)
4912 << Impl->getSuperClass()->getDeclName();
4913 else
4914 Importer.ToDiag(Impl->getLocation(),
4915 diag::note_odr_objc_missing_superclass);
4916 if (D->getSuperClass())
4917 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004918 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004919 << D->getSuperClass()->getDeclName();
4920 else
4921 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004922 diag::note_odr_objc_missing_superclass);
Balazs Keri3b30d652018-10-19 13:32:20 +00004923
4924 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004925 }
4926 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004927
Douglas Gregorda8025c2010-12-07 01:26:03 +00004928 // Import all of the members of this @implementation.
Balazs Keri3b30d652018-10-19 13:32:20 +00004929 if (Error Err = ImportDeclContext(D))
4930 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004931
4932 return Impl;
4933}
4934
Balazs Keri3b30d652018-10-19 13:32:20 +00004935ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004936 // Import the major distinguishing characteristics of an @property.
4937 DeclContext *DC, *LexicalDC;
4938 DeclarationName Name;
4939 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004940 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004941 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4942 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004943 if (ToD)
4944 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004945
4946 // Check whether we have already imported this property.
Gabor Marton54058b52018-12-17 13:53:12 +00004947 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004948 for (auto *FoundDecl : FoundDecls) {
4949 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004950 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004951 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004952 FoundProp->getType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00004953 Importer.ToDiag(Loc, diag::warn_odr_objc_property_type_inconsistent)
Douglas Gregora11c4582010-02-17 18:02:10 +00004954 << Name << D->getType() << FoundProp->getType();
4955 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4956 << FoundProp->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004957
4958 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora11c4582010-02-17 18:02:10 +00004959 }
4960
4961 // FIXME: Check property attributes, getters, setters, etc.?
4962
4963 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004964 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004965 return FoundProp;
4966 }
4967 }
4968
Balazs Keri3b30d652018-10-19 13:32:20 +00004969 QualType ToType;
4970 TypeSourceInfo *ToTypeSourceInfo;
4971 SourceLocation ToAtLoc, ToLParenLoc;
4972 if (auto Imp = importSeq(
4973 D->getType(), D->getTypeSourceInfo(), D->getAtLoc(), D->getLParenLoc()))
4974 std::tie(ToType, ToTypeSourceInfo, ToAtLoc, ToLParenLoc) = *Imp;
4975 else
4976 return Imp.takeError();
Douglas Gregora11c4582010-02-17 18:02:10 +00004977
4978 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004979 ObjCPropertyDecl *ToProperty;
4980 if (GetImportedOrCreateDecl(
4981 ToProperty, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004982 Name.getAsIdentifierInfo(), ToAtLoc,
4983 ToLParenLoc, ToType,
4984 ToTypeSourceInfo, D->getPropertyImplementation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00004985 return ToProperty;
4986
Balazs Keri3b30d652018-10-19 13:32:20 +00004987 Selector ToGetterName, ToSetterName;
4988 SourceLocation ToGetterNameLoc, ToSetterNameLoc;
4989 ObjCMethodDecl *ToGetterMethodDecl, *ToSetterMethodDecl;
4990 ObjCIvarDecl *ToPropertyIvarDecl;
4991 if (auto Imp = importSeq(
4992 D->getGetterName(), D->getSetterName(),
4993 D->getGetterNameLoc(), D->getSetterNameLoc(),
4994 D->getGetterMethodDecl(), D->getSetterMethodDecl(),
4995 D->getPropertyIvarDecl()))
4996 std::tie(
4997 ToGetterName, ToSetterName,
4998 ToGetterNameLoc, ToSetterNameLoc,
4999 ToGetterMethodDecl, ToSetterMethodDecl,
5000 ToPropertyIvarDecl) = *Imp;
5001 else
5002 return Imp.takeError();
5003
Douglas Gregora11c4582010-02-17 18:02:10 +00005004 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00005005 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00005006
5007 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00005008 ToProperty->setPropertyAttributesAsWritten(
5009 D->getPropertyAttributesAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00005010 ToProperty->setGetterName(ToGetterName, ToGetterNameLoc);
5011 ToProperty->setSetterName(ToSetterName, ToSetterNameLoc);
5012 ToProperty->setGetterMethodDecl(ToGetterMethodDecl);
5013 ToProperty->setSetterMethodDecl(ToSetterMethodDecl);
5014 ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl);
Douglas Gregora11c4582010-02-17 18:02:10 +00005015 return ToProperty;
5016}
5017
Balazs Keri3b30d652018-10-19 13:32:20 +00005018ExpectedDecl
5019ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
5020 ObjCPropertyDecl *Property;
5021 if (Error Err = importInto(Property, D->getPropertyDecl()))
5022 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00005023
Balazs Keri3b30d652018-10-19 13:32:20 +00005024 DeclContext *DC, *LexicalDC;
5025 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5026 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005027
Balazs Keri3b30d652018-10-19 13:32:20 +00005028 auto *InImpl = cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00005029
5030 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00005031 ObjCIvarDecl *Ivar = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00005032 if (Error Err = importInto(Ivar, D->getPropertyIvarDecl()))
5033 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00005034
5035 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00005036 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
5037 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00005038 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005039 SourceLocation ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc;
5040 if (auto Imp = importSeq(
5041 D->getBeginLoc(), D->getLocation(), D->getPropertyIvarDeclLoc()))
5042 std::tie(ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc) = *Imp;
5043 else
5044 return Imp.takeError();
5045
Gabor Marton26f72a92018-07-12 09:42:05 +00005046 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005047 ToBeginLoc,
5048 ToLocation, Property,
Gabor Marton26f72a92018-07-12 09:42:05 +00005049 D->getPropertyImplementation(), Ivar,
Balazs Keri3b30d652018-10-19 13:32:20 +00005050 ToPropertyIvarDeclLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00005051 return ToImpl;
5052
Douglas Gregor14a49e22010-12-07 18:32:03 +00005053 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00005054 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00005055 } else {
5056 // Check that we have the same kind of property implementation (@synthesize
5057 // vs. @dynamic).
5058 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00005059 Importer.ToDiag(ToImpl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00005060 diag::warn_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00005061 << Property->getDeclName()
5062 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00005063 == ObjCPropertyImplDecl::Dynamic);
5064 Importer.FromDiag(D->getLocation(),
5065 diag::note_odr_objc_property_impl_kind)
5066 << D->getPropertyDecl()->getDeclName()
5067 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Balazs Keri3b30d652018-10-19 13:32:20 +00005068
5069 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00005070 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005071
5072 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00005073 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
5074 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00005075 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Gabor Marton410f32c2019-04-01 15:29:55 +00005076 diag::warn_odr_objc_synthesize_ivar_inconsistent)
Douglas Gregor14a49e22010-12-07 18:32:03 +00005077 << Property->getDeclName()
5078 << ToImpl->getPropertyIvarDecl()->getDeclName()
5079 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00005080 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00005081 diag::note_odr_objc_synthesize_ivar_here)
5082 << D->getPropertyIvarDecl()->getDeclName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005083
5084 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00005085 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005086
Douglas Gregor14a49e22010-12-07 18:32:03 +00005087 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00005088 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00005089 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005090
Douglas Gregor14a49e22010-12-07 18:32:03 +00005091 return ToImpl;
5092}
5093
Balazs Keri3b30d652018-10-19 13:32:20 +00005094ExpectedDecl
5095ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregora082a492010-11-30 19:14:50 +00005096 // For template arguments, we adopt the translation unit as our declaration
5097 // context. This context will be fixed when the actual template declaration
5098 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00005099
Saar Razff1e0fc2020-01-15 02:48:42 +02005100 // FIXME: Import default argument and constraint expression.
Balazs Keri3b30d652018-10-19 13:32:20 +00005101
5102 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5103 if (!BeginLocOrErr)
5104 return BeginLocOrErr.takeError();
5105
5106 ExpectedSLoc LocationOrErr = import(D->getLocation());
5107 if (!LocationOrErr)
5108 return LocationOrErr.takeError();
5109
Gabor Marton26f72a92018-07-12 09:42:05 +00005110 TemplateTypeParmDecl *ToD = nullptr;
Saar Razff1e0fc2020-01-15 02:48:42 +02005111 if (GetImportedOrCreateDecl(
Gabor Marton26f72a92018-07-12 09:42:05 +00005112 ToD, D, Importer.getToContext(),
5113 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005114 *BeginLocOrErr, *LocationOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005115 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
Saar Razff1e0fc2020-01-15 02:48:42 +02005116 D->wasDeclaredWithTypename(), D->isParameterPack(),
5117 D->hasTypeConstraint()))
5118 return ToD;
5119
5120 // Import the type-constraint
5121 if (const TypeConstraint *TC = D->getTypeConstraint()) {
5122 NestedNameSpecifierLoc ToNNS;
5123 DeclarationName ToName;
5124 SourceLocation ToNameLoc;
5125 NamedDecl *ToFoundDecl;
5126 ConceptDecl *ToNamedConcept;
5127 Expr *ToIDC;
5128 if (auto Imp = importSeq(TC->getNestedNameSpecifierLoc(),
5129 TC->getConceptNameInfo().getName(), TC->getConceptNameInfo().getLoc(),
5130 TC->getFoundDecl(), TC->getNamedConcept(),
5131 TC->getImmediatelyDeclaredConstraint()))
5132 std::tie(ToNNS, ToName, ToNameLoc, ToFoundDecl, ToNamedConcept,
5133 ToIDC) = *Imp;
5134 else
5135 return Imp.takeError();
5136
5137 TemplateArgumentListInfo ToTAInfo;
5138 const auto *ASTTemplateArgs = TC->getTemplateArgsAsWritten();
5139 if (ASTTemplateArgs)
5140 if (Error Err = ImportTemplateArgumentListInfo(*ASTTemplateArgs,
5141 ToTAInfo))
5142 return std::move(Err);
5143
5144 ToD->setTypeConstraint(ToNNS, DeclarationNameInfo(ToName, ToNameLoc),
5145 ToFoundDecl, ToNamedConcept,
5146 ASTTemplateArgs ?
5147 ASTTemplateArgumentListInfo::Create(Importer.getToContext(),
5148 ToTAInfo) : nullptr,
5149 ToIDC);
5150 }
5151
Gabor Marton26f72a92018-07-12 09:42:05 +00005152 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00005153}
5154
Balazs Keri3b30d652018-10-19 13:32:20 +00005155ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00005156ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005157 DeclarationName ToDeclName;
5158 SourceLocation ToLocation, ToInnerLocStart;
5159 QualType ToType;
5160 TypeSourceInfo *ToTypeSourceInfo;
5161 if (auto Imp = importSeq(
5162 D->getDeclName(), D->getLocation(), D->getType(), D->getTypeSourceInfo(),
5163 D->getInnerLocStart()))
5164 std::tie(
5165 ToDeclName, ToLocation, ToType, ToTypeSourceInfo,
5166 ToInnerLocStart) = *Imp;
5167 else
5168 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005169
Douglas Gregora082a492010-11-30 19:14:50 +00005170 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00005171
5172 NonTypeTemplateParmDecl *ToD = nullptr;
5173 (void)GetImportedOrCreateDecl(
5174 ToD, D, Importer.getToContext(),
5175 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005176 ToInnerLocStart, ToLocation, D->getDepth(),
5177 D->getPosition(), ToDeclName.getAsIdentifierInfo(), ToType,
5178 D->isParameterPack(), ToTypeSourceInfo);
Gabor Marton26f72a92018-07-12 09:42:05 +00005179 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00005180}
5181
Balazs Keri3b30d652018-10-19 13:32:20 +00005182ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00005183ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
5184 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005185 auto NameOrErr = import(D->getDeclName());
5186 if (!NameOrErr)
5187 return NameOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005188
Douglas Gregora082a492010-11-30 19:14:50 +00005189 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005190 ExpectedSLoc LocationOrErr = import(D->getLocation());
5191 if (!LocationOrErr)
5192 return LocationOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00005193
Douglas Gregora082a492010-11-30 19:14:50 +00005194 // Import template parameters.
Balazs Keridec09162019-03-20 15:42:42 +00005195 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005196 if (!TemplateParamsOrErr)
5197 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005198
Douglas Gregora082a492010-11-30 19:14:50 +00005199 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00005200
5201 TemplateTemplateParmDecl *ToD = nullptr;
5202 (void)GetImportedOrCreateDecl(
5203 ToD, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005204 Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr,
5205 D->getDepth(), D->getPosition(), D->isParameterPack(),
5206 (*NameOrErr).getAsIdentifierInfo(),
5207 *TemplateParamsOrErr);
Gabor Marton26f72a92018-07-12 09:42:05 +00005208 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00005209}
5210
Gabor Marton16d98c22019-03-07 13:01:51 +00005211// Returns the definition for a (forward) declaration of a TemplateDecl, if
Gabor Marton9581c332018-05-23 13:53:36 +00005212// it has any definition in the redecl chain.
Gabor Marton16d98c22019-03-07 13:01:51 +00005213template <typename T> static auto getTemplateDefinition(T *D) -> T * {
5214 assert(D->getTemplatedDecl() && "Should be called on templates only");
5215 auto *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
Gabor Marton9581c332018-05-23 13:53:36 +00005216 if (!ToTemplatedDef)
5217 return nullptr;
Gabor Marton16d98c22019-03-07 13:01:51 +00005218 auto *TemplateWithDef = ToTemplatedDef->getDescribedTemplate();
5219 return cast_or_null<T>(TemplateWithDef);
Gabor Marton9581c332018-05-23 13:53:36 +00005220}
5221
Balazs Keri3b30d652018-10-19 13:32:20 +00005222ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00005223
Douglas Gregora082a492010-11-30 19:14:50 +00005224 // Import the major distinguishing characteristics of this class template.
5225 DeclContext *DC, *LexicalDC;
5226 DeclarationName Name;
5227 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005228 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005229 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5230 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005231 if (ToD)
5232 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00005233
Gabor Marton7df342a2018-12-17 12:42:12 +00005234 ClassTemplateDecl *FoundByLookup = nullptr;
5235
Douglas Gregora082a492010-11-30 19:14:50 +00005236 // We may already have a template of the same name; try to find and match it.
5237 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005238 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00005239 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005240 for (auto *FoundDecl : FoundDecls) {
Gabor Marton7df342a2018-12-17 12:42:12 +00005241 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary |
5242 Decl::IDNS_TagFriend))
Douglas Gregora082a492010-11-30 19:14:50 +00005243 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005244
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005245 Decl *Found = FoundDecl;
Gabor Marton7df342a2018-12-17 12:42:12 +00005246 auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found);
5247 if (FoundTemplate) {
Balázs Kéric2f6efc2019-11-15 15:05:20 +01005248 if (!hasSameVisibilityContext(FoundTemplate, D))
5249 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005250
Douglas Gregora082a492010-11-30 19:14:50 +00005251 if (IsStructuralMatch(D, FoundTemplate)) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005252 ClassTemplateDecl *TemplateWithDef =
5253 getTemplateDefinition(FoundTemplate);
Balazs Keri2e160602019-08-12 10:07:38 +00005254 if (D->isThisDeclarationADefinition() && TemplateWithDef)
Gabor Marton7df342a2018-12-17 12:42:12 +00005255 return Importer.MapImported(D, TemplateWithDef);
Balazs Keri2e160602019-08-12 10:07:38 +00005256 if (!FoundByLookup)
5257 FoundByLookup = FoundTemplate;
5258 // Search in all matches because there may be multiple decl chains,
5259 // see ASTTests test ImportExistingFriendClassTemplateDef.
5260 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005261 }
Gabor Martonf035b752019-08-27 11:36:10 +00005262 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00005263 }
Douglas Gregora082a492010-11-30 19:14:50 +00005264 }
Gabor Marton9581c332018-05-23 13:53:36 +00005265
Douglas Gregora082a492010-11-30 19:14:50 +00005266 if (!ConflictingDecls.empty()) {
Gabor Martonf035b752019-08-27 11:36:10 +00005267 ExpectedName NameOrErr = Importer.HandleNameConflict(
5268 Name, DC, Decl::IDNS_Ordinary, ConflictingDecls.data(),
5269 ConflictingDecls.size());
5270 if (NameOrErr)
5271 Name = NameOrErr.get();
5272 else
5273 return NameOrErr.takeError();
Douglas Gregora082a492010-11-30 19:14:50 +00005274 }
Douglas Gregora082a492010-11-30 19:14:50 +00005275 }
5276
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005277 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
5278
Douglas Gregora082a492010-11-30 19:14:50 +00005279 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005280 CXXRecordDecl *ToTemplated;
5281 if (Error Err = importInto(ToTemplated, FromTemplated))
5282 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005283
Douglas Gregora082a492010-11-30 19:14:50 +00005284 // Create the class template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005285 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005286 if (!TemplateParamsOrErr)
5287 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005288
Gabor Marton26f72a92018-07-12 09:42:05 +00005289 ClassTemplateDecl *D2;
5290 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005291 *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005292 return D2;
5293
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005294 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005295
Douglas Gregora082a492010-11-30 19:14:50 +00005296 D2->setAccess(D->getAccess());
5297 D2->setLexicalDeclContext(LexicalDC);
Gabor Marton7df342a2018-12-17 12:42:12 +00005298
Gabor Martonbc5b7e22019-12-04 17:12:08 +01005299 addDeclToContexts(D, D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005300
Gabor Marton7df342a2018-12-17 12:42:12 +00005301 if (FoundByLookup) {
5302 auto *Recent =
5303 const_cast<ClassTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5304
5305 // It is possible that during the import of the class template definition
5306 // we start the import of a fwd friend decl of the very same class template
5307 // and we add the fwd friend decl to the lookup table. But the ToTemplated
5308 // had been created earlier and by that time the lookup could not find
5309 // anything existing, so it has no previous decl. Later, (still during the
5310 // import of the fwd friend decl) we start to import the definition again
5311 // and this time the lookup finds the previous fwd friend class template.
5312 // In this case we must set up the previous decl for the templated decl.
5313 if (!ToTemplated->getPreviousDecl()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005314 assert(FoundByLookup->getTemplatedDecl() &&
5315 "Found decl must have its templated decl set");
Gabor Marton7df342a2018-12-17 12:42:12 +00005316 CXXRecordDecl *PrevTemplated =
5317 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5318 if (ToTemplated != PrevTemplated)
5319 ToTemplated->setPreviousDecl(PrevTemplated);
5320 }
5321
5322 D2->setPreviousDecl(Recent);
5323 }
5324
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005325 if (FromTemplated->isCompleteDefinition() &&
5326 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00005327 // FIXME: Import definition!
5328 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005329
Douglas Gregora082a492010-11-30 19:14:50 +00005330 return D2;
5331}
5332
Balazs Keri3b30d652018-10-19 13:32:20 +00005333ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +00005334 ClassTemplateSpecializationDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005335 ClassTemplateDecl *ClassTemplate;
5336 if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate()))
5337 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005338
Douglas Gregore2e50d332010-12-01 01:36:18 +00005339 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005340 DeclContext *DC, *LexicalDC;
5341 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5342 return std::move(Err);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005343
5344 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005345 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005346 if (Error Err = ImportTemplateArguments(
5347 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5348 return std::move(Err);
Saar Razdf061c32019-12-23 08:37:35 +02005349 // Try to find an existing specialization with these template arguments and
5350 // template parameter list.
Craig Topper36250ad2014-05-12 05:36:57 +00005351 void *InsertPos = nullptr;
Gabor Marton7f8c4002019-03-19 13:34:10 +00005352 ClassTemplateSpecializationDecl *PrevDecl = nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005353 ClassTemplatePartialSpecializationDecl *PartialSpec =
5354 dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
Saar Razdf061c32019-12-23 08:37:35 +02005355
5356 // Import template parameters.
5357 TemplateParameterList *ToTPList = nullptr;
5358
5359 if (PartialSpec) {
5360 auto ToTPListOrErr = import(PartialSpec->getTemplateParameters());
5361 if (!ToTPListOrErr)
5362 return ToTPListOrErr.takeError();
5363 ToTPList = *ToTPListOrErr;
5364 PrevDecl = ClassTemplate->findPartialSpecialization(TemplateArgs,
5365 *ToTPListOrErr,
5366 InsertPos);
5367 } else
Gabor Marton7f8c4002019-03-19 13:34:10 +00005368 PrevDecl = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005369
Gabor Marton7f8c4002019-03-19 13:34:10 +00005370 if (PrevDecl) {
5371 if (IsStructuralMatch(D, PrevDecl)) {
5372 if (D->isThisDeclarationADefinition() && PrevDecl->getDefinition()) {
5373 Importer.MapImported(D, PrevDecl->getDefinition());
5374 // Import those default field initializers which have been
5375 // instantiated in the "From" context, but not in the "To" context.
Gabor Marton5ac6d492019-05-15 10:29:48 +00005376 for (auto *FromField : D->fields()) {
5377 auto ToOrErr = import(FromField);
5378 if (!ToOrErr)
5379 return ToOrErr.takeError();
5380 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005381
Gabor Marton7f8c4002019-03-19 13:34:10 +00005382 // Import those methods which have been instantiated in the
5383 // "From" context, but not in the "To" context.
Gabor Marton5ac6d492019-05-15 10:29:48 +00005384 for (CXXMethodDecl *FromM : D->methods()) {
5385 auto ToOrErr = import(FromM);
5386 if (!ToOrErr)
5387 return ToOrErr.takeError();
5388 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005389
Gabor Marton7f8c4002019-03-19 13:34:10 +00005390 // TODO Import instantiated default arguments.
5391 // TODO Import instantiated exception specifications.
5392 //
5393 // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint
5394 // what else could be fused during an AST merge.
5395 return PrevDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005396 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005397 } else { // ODR violation.
5398 // FIXME HandleNameConflict
Gabor Marton303c98612019-06-25 08:00:51 +00005399 return make_error<ImportError>(ImportError::NameConflict);
Gabor Marton42e15de2018-08-22 11:52:14 +00005400 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005401 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005402
Gabor Marton7f8c4002019-03-19 13:34:10 +00005403 // Import the location of this declaration.
5404 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5405 if (!BeginLocOrErr)
5406 return BeginLocOrErr.takeError();
5407 ExpectedSLoc IdLocOrErr = import(D->getLocation());
5408 if (!IdLocOrErr)
5409 return IdLocOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +00005410
Gabor Marton7f8c4002019-03-19 13:34:10 +00005411 // Create the specialization.
5412 ClassTemplateSpecializationDecl *D2 = nullptr;
5413 if (PartialSpec) {
5414 // Import TemplateArgumentListInfo.
5415 TemplateArgumentListInfo ToTAInfo;
5416 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
5417 if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
5418 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005419
Gabor Marton7f8c4002019-03-19 13:34:10 +00005420 QualType CanonInjType;
5421 if (Error Err = importInto(
5422 CanonInjType, PartialSpec->getInjectedSpecializationType()))
5423 return std::move(Err);
5424 CanonInjType = CanonInjType.getCanonicalType();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005425
Gabor Marton7f8c4002019-03-19 13:34:10 +00005426 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
5427 D2, D, Importer.getToContext(), D->getTagKind(), DC,
Saar Razdf061c32019-12-23 08:37:35 +02005428 *BeginLocOrErr, *IdLocOrErr, ToTPList, ClassTemplate,
Gabor Marton7f8c4002019-03-19 13:34:10 +00005429 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
5430 ToTAInfo, CanonInjType,
5431 cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl)))
5432 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005433
Gabor Marton7f8c4002019-03-19 13:34:10 +00005434 // Update InsertPos, because preceding import calls may have invalidated
5435 // it by adding new specializations.
Saar Razdf061c32019-12-23 08:37:35 +02005436 auto *PartSpec2 = cast<ClassTemplatePartialSpecializationDecl>(D2);
5437 if (!ClassTemplate->findPartialSpecialization(TemplateArgs, ToTPList,
5438 InsertPos))
Gabor Marton7f8c4002019-03-19 13:34:10 +00005439 // Add this partial specialization to the class template.
Saar Razdf061c32019-12-23 08:37:35 +02005440 ClassTemplate->AddPartialSpecialization(PartSpec2, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005441
Gabor Marton7f8c4002019-03-19 13:34:10 +00005442 } else { // Not a partial specialization.
5443 if (GetImportedOrCreateDecl(
5444 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5445 *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs,
5446 PrevDecl))
5447 return D2;
Gabor Marton42e15de2018-08-22 11:52:14 +00005448
Gabor Marton7f8c4002019-03-19 13:34:10 +00005449 // Update InsertPos, because preceding import calls may have invalidated
5450 // it by adding new specializations.
5451 if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos))
5452 // Add this specialization to the class template.
5453 ClassTemplate->AddSpecialization(D2, InsertPos);
5454 }
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005455
Gabor Marton7f8c4002019-03-19 13:34:10 +00005456 D2->setSpecializationKind(D->getSpecializationKind());
Douglas Gregore2e50d332010-12-01 01:36:18 +00005457
Gabor Marton7f8c4002019-03-19 13:34:10 +00005458 // Set the context of this specialization/instantiation.
5459 D2->setLexicalDeclContext(LexicalDC);
5460
5461 // Add to the DC only if it was an explicit specialization/instantiation.
5462 if (D2->isExplicitInstantiationOrSpecialization()) {
5463 LexicalDC->addDeclInternal(D2);
5464 }
5465
Balázs Kéria9f10eb2019-12-05 16:21:21 +01005466 if (auto BraceRangeOrErr = import(D->getBraceRange()))
5467 D2->setBraceRange(*BraceRangeOrErr);
5468 else
5469 return BraceRangeOrErr.takeError();
5470
Gabor Marton7f8c4002019-03-19 13:34:10 +00005471 // Import the qualifier, if any.
5472 if (auto LocOrErr = import(D->getQualifierLoc()))
5473 D2->setQualifierInfo(*LocOrErr);
5474 else
5475 return LocOrErr.takeError();
5476
5477 if (auto *TSI = D->getTypeAsWritten()) {
5478 if (auto TInfoOrErr = import(TSI))
5479 D2->setTypeAsWritten(*TInfoOrErr);
5480 else
5481 return TInfoOrErr.takeError();
5482
5483 if (auto LocOrErr = import(D->getTemplateKeywordLoc()))
5484 D2->setTemplateKeywordLoc(*LocOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +00005485 else
5486 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005487
Gabor Marton7f8c4002019-03-19 13:34:10 +00005488 if (auto LocOrErr = import(D->getExternLoc()))
5489 D2->setExternLoc(*LocOrErr);
5490 else
5491 return LocOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00005492 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005493
5494 if (D->getPointOfInstantiation().isValid()) {
5495 if (auto POIOrErr = import(D->getPointOfInstantiation()))
5496 D2->setPointOfInstantiation(*POIOrErr);
5497 else
5498 return POIOrErr.takeError();
5499 }
5500
5501 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
5502
Balazs Keri3b30d652018-10-19 13:32:20 +00005503 if (D->isCompleteDefinition())
5504 if (Error Err = ImportDefinition(D, D2))
5505 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005506
Douglas Gregore2e50d332010-12-01 01:36:18 +00005507 return D2;
5508}
5509
Balazs Keri3b30d652018-10-19 13:32:20 +00005510ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005511 // If this variable has a definition in the translation unit we're coming
5512 // from,
5513 // but this particular declaration is not that definition, import the
5514 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005515 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00005516 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
5517 if (Definition && Definition != D->getTemplatedDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005518 if (ExpectedDecl ImportedDefOrErr = import(
5519 Definition->getDescribedVarTemplate()))
5520 return Importer.MapImported(D, *ImportedDefOrErr);
5521 else
5522 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005523 }
5524
5525 // Import the major distinguishing characteristics of this variable template.
5526 DeclContext *DC, *LexicalDC;
5527 DeclarationName Name;
5528 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005529 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005530 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5531 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005532 if (ToD)
5533 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005534
5535 // We may already have a template of the same name; try to find and match it.
5536 assert(!DC->isFunctionOrMethod() &&
5537 "Variable templates cannot be declared at function scope");
5538 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00005539 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005540 for (auto *FoundDecl : FoundDecls) {
5541 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00005542 continue;
5543
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005544 Decl *Found = FoundDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005545 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005546 if (IsStructuralMatch(D, FoundTemplate)) {
5547 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00005548 Importer.MapImported(D->getTemplatedDecl(),
5549 FoundTemplate->getTemplatedDecl());
5550 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005551 }
Gabor Martonf035b752019-08-27 11:36:10 +00005552 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005553 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00005554 }
5555
5556 if (!ConflictingDecls.empty()) {
Gabor Martonf035b752019-08-27 11:36:10 +00005557 ExpectedName NameOrErr = Importer.HandleNameConflict(
5558 Name, DC, Decl::IDNS_Ordinary, ConflictingDecls.data(),
5559 ConflictingDecls.size());
5560 if (NameOrErr)
5561 Name = NameOrErr.get();
5562 else
5563 return NameOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005564 }
5565
Larisse Voufo39a1e502013-08-06 01:03:05 +00005566 VarDecl *DTemplated = D->getTemplatedDecl();
5567
5568 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005569 // FIXME: Value not used?
5570 ExpectedType TypeOrErr = import(DTemplated->getType());
5571 if (!TypeOrErr)
5572 return TypeOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005573
5574 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005575 VarDecl *ToTemplated;
5576 if (Error Err = importInto(ToTemplated, DTemplated))
5577 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005578
5579 // Create the variable template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005580 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005581 if (!TemplateParamsOrErr)
5582 return TemplateParamsOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005583
Gabor Marton26f72a92018-07-12 09:42:05 +00005584 VarTemplateDecl *ToVarTD;
5585 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00005586 Name, *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005587 return ToVarTD;
5588
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005589 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005590
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005591 ToVarTD->setAccess(D->getAccess());
5592 ToVarTD->setLexicalDeclContext(LexicalDC);
5593 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005594
Larisse Voufo39a1e502013-08-06 01:03:05 +00005595 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005596 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005597 // FIXME: Import definition!
5598 }
5599
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005600 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005601}
5602
Balazs Keri3b30d652018-10-19 13:32:20 +00005603ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00005604 VarTemplateSpecializationDecl *D) {
5605 // If this record has a definition in the translation unit we're coming from,
5606 // but this particular declaration is not that definition, import the
5607 // definition and map to that.
5608 VarDecl *Definition = D->getDefinition();
5609 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005610 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5611 return Importer.MapImported(D, *ImportedDefOrErr);
5612 else
5613 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005614 }
5615
Simon Pilgrim4c146ab2019-05-18 11:33:27 +00005616 VarTemplateDecl *VarTemplate = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00005617 if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate()))
5618 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005619
5620 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005621 DeclContext *DC, *LexicalDC;
5622 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5623 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005624
5625 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005626 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5627 if (!BeginLocOrErr)
5628 return BeginLocOrErr.takeError();
5629
5630 auto IdLocOrErr = import(D->getLocation());
5631 if (!IdLocOrErr)
5632 return IdLocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005633
5634 // Import template arguments.
5635 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005636 if (Error Err = ImportTemplateArguments(
5637 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5638 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005639
5640 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005641 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005642 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005643 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005644 if (D2) {
5645 // We already have a variable template specialization with these template
5646 // arguments.
5647
5648 // FIXME: Check for specialization vs. instantiation errors.
5649
5650 if (VarDecl *FoundDef = D2->getDefinition()) {
5651 if (!D->isThisDeclarationADefinition() ||
5652 IsStructuralMatch(D, FoundDef)) {
5653 // The record types structurally match, or the "from" translation
5654 // unit only had a forward declaration anyway; call it the same
5655 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00005656 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005657 }
5658 }
5659 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005660 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005661 QualType T;
5662 if (Error Err = importInto(T, D->getType()))
5663 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005664
Balazs Keri3b30d652018-10-19 13:32:20 +00005665 auto TInfoOrErr = import(D->getTypeSourceInfo());
5666 if (!TInfoOrErr)
5667 return TInfoOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005668
5669 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00005670 if (Error Err = ImportTemplateArgumentListInfo(
5671 D->getTemplateArgsInfo(), ToTAInfo))
5672 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005673
5674 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005675 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005676 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
5677 // Import TemplateArgumentListInfo
5678 TemplateArgumentListInfo ArgInfos;
5679 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
5680 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
Balazs Keri3b30d652018-10-19 13:32:20 +00005681 if (Error Err = ImportTemplateArgumentListInfo(
5682 *FromTAArgsAsWritten, ArgInfos))
5683 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005684
Balazs Keridec09162019-03-20 15:42:42 +00005685 auto ToTPListOrErr = import(FromPartial->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005686 if (!ToTPListOrErr)
5687 return ToTPListOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005688
Gabor Marton26f72a92018-07-12 09:42:05 +00005689 PartVarSpecDecl *ToPartial;
5690 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005691 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
5692 VarTemplate, T, *TInfoOrErr,
5693 D->getStorageClass(), TemplateArgs, ArgInfos))
Gabor Marton26f72a92018-07-12 09:42:05 +00005694 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005695
Balazs Keri3b30d652018-10-19 13:32:20 +00005696 if (Expected<PartVarSpecDecl *> ToInstOrErr = import(
5697 FromPartial->getInstantiatedFromMember()))
5698 ToPartial->setInstantiatedFromMember(*ToInstOrErr);
5699 else
5700 return ToInstOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005701
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005702 if (FromPartial->isMemberSpecialization())
5703 ToPartial->setMemberSpecialization();
5704
5705 D2 = ToPartial;
Balazs Keri3b30d652018-10-19 13:32:20 +00005706
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005707 } else { // Full specialization
Balazs Keri3b30d652018-10-19 13:32:20 +00005708 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
5709 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
5710 T, *TInfoOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005711 D->getStorageClass(), TemplateArgs))
5712 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005713 }
5714
Balazs Keri3b30d652018-10-19 13:32:20 +00005715 if (D->getPointOfInstantiation().isValid()) {
5716 if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation()))
5717 D2->setPointOfInstantiation(*POIOrErr);
5718 else
5719 return POIOrErr.takeError();
5720 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005721
Larisse Voufo39a1e502013-08-06 01:03:05 +00005722 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005723 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005724
5725 // Add this specialization to the class template.
5726 VarTemplate->AddSpecialization(D2, InsertPos);
5727
5728 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005729 if (auto LocOrErr = import(D->getQualifierLoc()))
5730 D2->setQualifierInfo(*LocOrErr);
5731 else
5732 return LocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005733
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005734 if (D->isConstexpr())
5735 D2->setConstexpr(true);
5736
Larisse Voufo39a1e502013-08-06 01:03:05 +00005737 // Add the specialization to this context.
5738 D2->setLexicalDeclContext(LexicalDC);
5739 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005740
5741 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00005742 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005743
Balazs Keri3b30d652018-10-19 13:32:20 +00005744 if (Error Err = ImportInitializer(D, D2))
5745 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005746
5747 return D2;
5748}
5749
Balazs Keri3b30d652018-10-19 13:32:20 +00005750ExpectedDecl
5751ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005752 DeclContext *DC, *LexicalDC;
5753 DeclarationName Name;
5754 SourceLocation Loc;
5755 NamedDecl *ToD;
5756
Balazs Keri3b30d652018-10-19 13:32:20 +00005757 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5758 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005759
5760 if (ToD)
5761 return ToD;
5762
Gabor Marton16d98c22019-03-07 13:01:51 +00005763 const FunctionTemplateDecl *FoundByLookup = nullptr;
5764
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005765 // Try to find a function in our own ("to") context with the same name, same
5766 // type, and in the same context as the function we're importing.
Gabor Marton16d98c22019-03-07 13:01:51 +00005767 // FIXME Split this into a separate function.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005768 if (!LexicalDC->isFunctionOrMethod()) {
Gabor Martone331e632019-02-18 13:09:27 +00005769 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00005770 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005771 for (auto *FoundDecl : FoundDecls) {
5772 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005773 continue;
5774
Gabor Marton16d98c22019-03-07 13:01:51 +00005775 if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Balazs Kerif8a89c82019-09-13 08:03:49 +00005776 if (!hasSameVisibilityContext(FoundTemplate, D))
5777 continue;
5778 if (IsStructuralMatch(D, FoundTemplate)) {
5779 FunctionTemplateDecl *TemplateWithDef =
5780 getTemplateDefinition(FoundTemplate);
5781 if (D->isThisDeclarationADefinition() && TemplateWithDef)
5782 return Importer.MapImported(D, TemplateWithDef);
5783
5784 FoundByLookup = FoundTemplate;
5785 break;
Gabor Marton16d98c22019-03-07 13:01:51 +00005786 // TODO: handle conflicting names
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005787 }
5788 }
5789 }
5790 }
5791
Balazs Keridec09162019-03-20 15:42:42 +00005792 auto ParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005793 if (!ParamsOrErr)
5794 return ParamsOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005795
Balazs Keri3b30d652018-10-19 13:32:20 +00005796 FunctionDecl *TemplatedFD;
5797 if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl()))
5798 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005799
Gabor Marton26f72a92018-07-12 09:42:05 +00005800 FunctionTemplateDecl *ToFunc;
5801 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005802 *ParamsOrErr, TemplatedFD))
Gabor Marton26f72a92018-07-12 09:42:05 +00005803 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005804
5805 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005806
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005807 ToFunc->setAccess(D->getAccess());
5808 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005809 LexicalDC->addDeclInternal(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005810
5811 if (FoundByLookup) {
5812 auto *Recent =
5813 const_cast<FunctionTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5814 if (!TemplatedFD->getPreviousDecl()) {
5815 assert(FoundByLookup->getTemplatedDecl() &&
5816 "Found decl must have its templated decl set");
5817 auto *PrevTemplated =
5818 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5819 if (TemplatedFD != PrevTemplated)
5820 TemplatedFD->setPreviousDecl(PrevTemplated);
5821 }
5822 ToFunc->setPreviousDecl(Recent);
5823 }
5824
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005825 return ToFunc;
5826}
5827
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005828//----------------------------------------------------------------------------
5829// Import Statements
5830//----------------------------------------------------------------------------
5831
Balazs Keri3b30d652018-10-19 13:32:20 +00005832ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005833 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
5834 << S->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005835 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005836}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005837
Balazs Keri3b30d652018-10-19 13:32:20 +00005838
5839ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
Gabor Marton303c98612019-06-25 08:00:51 +00005840 if (Importer.returnWithErrorInTest())
5841 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005842 SmallVector<IdentifierInfo *, 4> Names;
5843 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5844 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005845 // ToII is nullptr when no symbolic name is given for output operand
5846 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005847 Names.push_back(ToII);
5848 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005849
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005850 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5851 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005852 // ToII is nullptr when no symbolic name is given for input operand
5853 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005854 Names.push_back(ToII);
5855 }
5856
5857 SmallVector<StringLiteral *, 4> Clobbers;
5858 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005859 if (auto ClobberOrErr = import(S->getClobberStringLiteral(I)))
5860 Clobbers.push_back(*ClobberOrErr);
5861 else
5862 return ClobberOrErr.takeError();
5863
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005864 }
5865
5866 SmallVector<StringLiteral *, 4> Constraints;
5867 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005868 if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I)))
5869 Constraints.push_back(*OutputOrErr);
5870 else
5871 return OutputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005872 }
5873
5874 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005875 if (auto InputOrErr = import(S->getInputConstraintLiteral(I)))
5876 Constraints.push_back(*InputOrErr);
5877 else
5878 return InputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005879 }
5880
Jennifer Yub8fee672019-06-03 15:57:25 +00005881 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs() +
5882 S->getNumLabels());
Balazs Keri3b30d652018-10-19 13:32:20 +00005883 if (Error Err = ImportContainerChecked(S->outputs(), Exprs))
5884 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005885
Jennifer Yub8fee672019-06-03 15:57:25 +00005886 if (Error Err =
5887 ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
5888 return std::move(Err);
5889
Balazs Keri3b30d652018-10-19 13:32:20 +00005890 if (Error Err = ImportArrayChecked(
Jennifer Yub8fee672019-06-03 15:57:25 +00005891 S->labels(), Exprs.begin() + S->getNumOutputs() + S->getNumInputs()))
Balazs Keri3b30d652018-10-19 13:32:20 +00005892 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005893
Balazs Keri3b30d652018-10-19 13:32:20 +00005894 ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc());
5895 if (!AsmLocOrErr)
5896 return AsmLocOrErr.takeError();
5897 auto AsmStrOrErr = import(S->getAsmString());
5898 if (!AsmStrOrErr)
5899 return AsmStrOrErr.takeError();
5900 ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc());
5901 if (!RParenLocOrErr)
5902 return RParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005903
5904 return new (Importer.getToContext()) GCCAsmStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005905 Importer.getToContext(),
5906 *AsmLocOrErr,
5907 S->isSimple(),
5908 S->isVolatile(),
5909 S->getNumOutputs(),
5910 S->getNumInputs(),
5911 Names.data(),
5912 Constraints.data(),
5913 Exprs.data(),
5914 *AsmStrOrErr,
5915 S->getNumClobbers(),
5916 Clobbers.data(),
Jennifer Yub8fee672019-06-03 15:57:25 +00005917 S->getNumLabels(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005918 *RParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005919}
5920
Balazs Keri3b30d652018-10-19 13:32:20 +00005921ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5922 auto Imp = importSeq(S->getDeclGroup(), S->getBeginLoc(), S->getEndLoc());
5923 if (!Imp)
5924 return Imp.takeError();
5925
5926 DeclGroupRef ToDG;
5927 SourceLocation ToBeginLoc, ToEndLoc;
5928 std::tie(ToDG, ToBeginLoc, ToEndLoc) = *Imp;
5929
5930 return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005931}
5932
Balazs Keri3b30d652018-10-19 13:32:20 +00005933ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5934 ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc());
5935 if (!ToSemiLocOrErr)
5936 return ToSemiLocOrErr.takeError();
5937 return new (Importer.getToContext()) NullStmt(
5938 *ToSemiLocOrErr, S->hasLeadingEmptyMacro());
Sean Callanan59721b32015-04-28 18:41:46 +00005939}
5940
Balazs Keri3b30d652018-10-19 13:32:20 +00005941ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005942 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005943
Balazs Keri3b30d652018-10-19 13:32:20 +00005944 if (Error Err = ImportContainerChecked(S->body(), ToStmts))
5945 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00005946
Balazs Keri3b30d652018-10-19 13:32:20 +00005947 ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc());
5948 if (!ToLBracLocOrErr)
5949 return ToLBracLocOrErr.takeError();
5950
5951 ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc());
5952 if (!ToRBracLocOrErr)
5953 return ToRBracLocOrErr.takeError();
5954
5955 return CompoundStmt::Create(
5956 Importer.getToContext(), ToStmts,
5957 *ToLBracLocOrErr, *ToRBracLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005958}
5959
Balazs Keri3b30d652018-10-19 13:32:20 +00005960ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5961 auto Imp = importSeq(
5962 S->getLHS(), S->getRHS(), S->getSubStmt(), S->getCaseLoc(),
5963 S->getEllipsisLoc(), S->getColonLoc());
5964 if (!Imp)
5965 return Imp.takeError();
5966
5967 Expr *ToLHS, *ToRHS;
5968 Stmt *ToSubStmt;
5969 SourceLocation ToCaseLoc, ToEllipsisLoc, ToColonLoc;
5970 std::tie(ToLHS, ToRHS, ToSubStmt, ToCaseLoc, ToEllipsisLoc, ToColonLoc) =
5971 *Imp;
5972
Bruno Ricci5b30571752018-10-28 12:30:53 +00005973 auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS,
5974 ToCaseLoc, ToEllipsisLoc, ToColonLoc);
Gabor Horvath480892b2017-10-18 09:25:18 +00005975 ToStmt->setSubStmt(ToSubStmt);
Balazs Keri3b30d652018-10-19 13:32:20 +00005976
Gabor Horvath480892b2017-10-18 09:25:18 +00005977 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005978}
5979
Balazs Keri3b30d652018-10-19 13:32:20 +00005980ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5981 auto Imp = importSeq(S->getDefaultLoc(), S->getColonLoc(), S->getSubStmt());
5982 if (!Imp)
5983 return Imp.takeError();
5984
5985 SourceLocation ToDefaultLoc, ToColonLoc;
5986 Stmt *ToSubStmt;
5987 std::tie(ToDefaultLoc, ToColonLoc, ToSubStmt) = *Imp;
5988
5989 return new (Importer.getToContext()) DefaultStmt(
5990 ToDefaultLoc, ToColonLoc, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005991}
5992
Balazs Keri3b30d652018-10-19 13:32:20 +00005993ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5994 auto Imp = importSeq(S->getIdentLoc(), S->getDecl(), S->getSubStmt());
5995 if (!Imp)
5996 return Imp.takeError();
5997
5998 SourceLocation ToIdentLoc;
5999 LabelDecl *ToLabelDecl;
6000 Stmt *ToSubStmt;
6001 std::tie(ToIdentLoc, ToLabelDecl, ToSubStmt) = *Imp;
6002
6003 return new (Importer.getToContext()) LabelStmt(
6004 ToIdentLoc, ToLabelDecl, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00006005}
6006
Balazs Keri3b30d652018-10-19 13:32:20 +00006007ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
6008 ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc());
6009 if (!ToAttrLocOrErr)
6010 return ToAttrLocOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006011 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
6012 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00006013 if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs))
6014 return std::move(Err);
6015 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6016 if (!ToSubStmtOrErr)
6017 return ToSubStmtOrErr.takeError();
6018
6019 return AttributedStmt::Create(
6020 Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006021}
6022
Balazs Keri3b30d652018-10-19 13:32:20 +00006023ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) {
6024 auto Imp = importSeq(
6025 S->getIfLoc(), S->getInit(), S->getConditionVariable(), S->getCond(),
6026 S->getThen(), S->getElseLoc(), S->getElse());
6027 if (!Imp)
6028 return Imp.takeError();
6029
6030 SourceLocation ToIfLoc, ToElseLoc;
6031 Stmt *ToInit, *ToThen, *ToElse;
6032 VarDecl *ToConditionVariable;
6033 Expr *ToCond;
6034 std::tie(
6035 ToIfLoc, ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc, ToElse) =
6036 *Imp;
6037
Bruno Riccib1cc94b2018-10-27 21:12:20 +00006038 return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(),
6039 ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc,
6040 ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00006041}
6042
Balazs Keri3b30d652018-10-19 13:32:20 +00006043ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
6044 auto Imp = importSeq(
6045 S->getInit(), S->getConditionVariable(), S->getCond(),
6046 S->getBody(), S->getSwitchLoc());
6047 if (!Imp)
6048 return Imp.takeError();
6049
6050 Stmt *ToInit, *ToBody;
6051 VarDecl *ToConditionVariable;
6052 Expr *ToCond;
6053 SourceLocation ToSwitchLoc;
6054 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
6055
Bruno Riccie2806f82018-10-29 16:12:37 +00006056 auto *ToStmt = SwitchStmt::Create(Importer.getToContext(), ToInit,
6057 ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00006058 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00006059 ToStmt->setSwitchLoc(ToSwitchLoc);
6060
Sean Callanan59721b32015-04-28 18:41:46 +00006061 // Now we have to re-chain the cases.
6062 SwitchCase *LastChainedSwitchCase = nullptr;
6063 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
6064 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006065 Expected<SwitchCase *> ToSCOrErr = import(SC);
6066 if (!ToSCOrErr)
6067 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006068 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00006069 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006070 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006071 ToStmt->setSwitchCaseList(*ToSCOrErr);
6072 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00006073 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006074
Sean Callanan59721b32015-04-28 18:41:46 +00006075 return ToStmt;
6076}
6077
Balazs Keri3b30d652018-10-19 13:32:20 +00006078ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
6079 auto Imp = importSeq(
6080 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
6081 if (!Imp)
6082 return Imp.takeError();
6083
6084 VarDecl *ToConditionVariable;
6085 Expr *ToCond;
6086 Stmt *ToBody;
6087 SourceLocation ToWhileLoc;
6088 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
6089
Bruno Riccibacf7512018-10-30 13:42:41 +00006090 return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond,
6091 ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00006092}
6093
Balazs Keri3b30d652018-10-19 13:32:20 +00006094ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
6095 auto Imp = importSeq(
6096 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
6097 S->getRParenLoc());
6098 if (!Imp)
6099 return Imp.takeError();
6100
6101 Stmt *ToBody;
6102 Expr *ToCond;
6103 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
6104 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
6105
6106 return new (Importer.getToContext()) DoStmt(
6107 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00006108}
6109
Balazs Keri3b30d652018-10-19 13:32:20 +00006110ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
6111 auto Imp = importSeq(
6112 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
6113 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
6114 if (!Imp)
6115 return Imp.takeError();
6116
6117 Stmt *ToInit;
6118 Expr *ToCond, *ToInc;
6119 VarDecl *ToConditionVariable;
6120 Stmt *ToBody;
6121 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
6122 std::tie(
6123 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
6124 ToLParenLoc, ToRParenLoc) = *Imp;
6125
6126 return new (Importer.getToContext()) ForStmt(
6127 Importer.getToContext(),
6128 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
6129 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00006130}
6131
Balazs Keri3b30d652018-10-19 13:32:20 +00006132ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
6133 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
6134 if (!Imp)
6135 return Imp.takeError();
6136
6137 LabelDecl *ToLabel;
6138 SourceLocation ToGotoLoc, ToLabelLoc;
6139 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
6140
6141 return new (Importer.getToContext()) GotoStmt(
6142 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00006143}
6144
Balazs Keri3b30d652018-10-19 13:32:20 +00006145ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
6146 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
6147 if (!Imp)
6148 return Imp.takeError();
6149
6150 SourceLocation ToGotoLoc, ToStarLoc;
6151 Expr *ToTarget;
6152 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
6153
6154 return new (Importer.getToContext()) IndirectGotoStmt(
6155 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00006156}
6157
Balazs Keri3b30d652018-10-19 13:32:20 +00006158ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
6159 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
6160 if (!ToContinueLocOrErr)
6161 return ToContinueLocOrErr.takeError();
6162 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006163}
6164
Balazs Keri3b30d652018-10-19 13:32:20 +00006165ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
6166 auto ToBreakLocOrErr = import(S->getBreakLoc());
6167 if (!ToBreakLocOrErr)
6168 return ToBreakLocOrErr.takeError();
6169 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006170}
6171
Balazs Keri3b30d652018-10-19 13:32:20 +00006172ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
6173 auto Imp = importSeq(
6174 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
6175 if (!Imp)
6176 return Imp.takeError();
6177
6178 SourceLocation ToReturnLoc;
6179 Expr *ToRetValue;
6180 const VarDecl *ToNRVOCandidate;
6181 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
6182
Bruno Ricci023b1d12018-10-30 14:40:49 +00006183 return ReturnStmt::Create(Importer.getToContext(), ToReturnLoc, ToRetValue,
6184 ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00006185}
6186
Balazs Keri3b30d652018-10-19 13:32:20 +00006187ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
6188 auto Imp = importSeq(
6189 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
6190 if (!Imp)
6191 return Imp.takeError();
6192
6193 SourceLocation ToCatchLoc;
6194 VarDecl *ToExceptionDecl;
6195 Stmt *ToHandlerBlock;
6196 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
6197
6198 return new (Importer.getToContext()) CXXCatchStmt (
6199 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00006200}
6201
Balazs Keri3b30d652018-10-19 13:32:20 +00006202ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
6203 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
6204 if (!ToTryLocOrErr)
6205 return ToTryLocOrErr.takeError();
6206
6207 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
6208 if (!ToTryBlockOrErr)
6209 return ToTryBlockOrErr.takeError();
6210
Sean Callanan59721b32015-04-28 18:41:46 +00006211 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
6212 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
6213 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00006214 if (auto ToHandlerOrErr = import(FromHandler))
6215 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00006216 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006217 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006218 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006219
6220 return CXXTryStmt::Create(
6221 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00006222}
6223
Balazs Keri3b30d652018-10-19 13:32:20 +00006224ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
6225 auto Imp1 = importSeq(
6226 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
6227 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
6228 if (!Imp1)
6229 return Imp1.takeError();
6230 auto Imp2 = importSeq(
6231 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
6232 if (!Imp2)
6233 return Imp2.takeError();
6234
6235 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
6236 Expr *ToCond, *ToInc;
6237 Stmt *ToInit, *ToBody;
6238 std::tie(
6239 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
6240 ToBody) = *Imp1;
6241 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
6242 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
6243
6244 return new (Importer.getToContext()) CXXForRangeStmt(
6245 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
6246 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00006247}
6248
Balazs Keri3b30d652018-10-19 13:32:20 +00006249ExpectedStmt
6250ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
6251 auto Imp = importSeq(
6252 S->getElement(), S->getCollection(), S->getBody(),
6253 S->getForLoc(), S->getRParenLoc());
6254 if (!Imp)
6255 return Imp.takeError();
6256
6257 Stmt *ToElement, *ToBody;
6258 Expr *ToCollection;
6259 SourceLocation ToForLoc, ToRParenLoc;
6260 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
6261
6262 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
6263 ToCollection,
6264 ToBody,
6265 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00006266 ToRParenLoc);
6267}
6268
Balazs Keri3b30d652018-10-19 13:32:20 +00006269ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
6270 auto Imp = importSeq(
6271 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
6272 S->getCatchBody());
6273 if (!Imp)
6274 return Imp.takeError();
6275
6276 SourceLocation ToAtCatchLoc, ToRParenLoc;
6277 VarDecl *ToCatchParamDecl;
6278 Stmt *ToCatchBody;
6279 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
6280
6281 return new (Importer.getToContext()) ObjCAtCatchStmt (
6282 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00006283}
6284
Balazs Keri3b30d652018-10-19 13:32:20 +00006285ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
6286 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
6287 if (!ToAtFinallyLocOrErr)
6288 return ToAtFinallyLocOrErr.takeError();
6289 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
6290 if (!ToAtFinallyStmtOrErr)
6291 return ToAtFinallyStmtOrErr.takeError();
6292 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
6293 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006294}
6295
Balazs Keri3b30d652018-10-19 13:32:20 +00006296ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
6297 auto Imp = importSeq(
6298 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
6299 if (!Imp)
6300 return Imp.takeError();
6301
6302 SourceLocation ToAtTryLoc;
6303 Stmt *ToTryBody, *ToFinallyStmt;
6304 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
6305
Sean Callanan59721b32015-04-28 18:41:46 +00006306 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
6307 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
6308 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00006309 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
6310 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00006311 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006312 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006313 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006314
Sean Callanan59721b32015-04-28 18:41:46 +00006315 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006316 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00006317 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006318 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00006319}
6320
Balazs Keri3b30d652018-10-19 13:32:20 +00006321ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00006322 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006323 auto Imp = importSeq(
6324 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
6325 if (!Imp)
6326 return Imp.takeError();
6327
6328 SourceLocation ToAtSynchronizedLoc;
6329 Expr *ToSynchExpr;
6330 Stmt *ToSynchBody;
6331 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
6332
Sean Callanan59721b32015-04-28 18:41:46 +00006333 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
6334 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
6335}
6336
Balazs Keri3b30d652018-10-19 13:32:20 +00006337ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
6338 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
6339 if (!ToThrowLocOrErr)
6340 return ToThrowLocOrErr.takeError();
6341 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
6342 if (!ToThrowExprOrErr)
6343 return ToThrowExprOrErr.takeError();
6344 return new (Importer.getToContext()) ObjCAtThrowStmt(
6345 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006346}
6347
Balazs Keri3b30d652018-10-19 13:32:20 +00006348ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
6349 ObjCAutoreleasePoolStmt *S) {
6350 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
6351 if (!ToAtLocOrErr)
6352 return ToAtLocOrErr.takeError();
6353 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6354 if (!ToSubStmtOrErr)
6355 return ToSubStmtOrErr.takeError();
6356 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6357 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006358}
6359
6360//----------------------------------------------------------------------------
6361// Import Expressions
6362//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006363ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006364 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6365 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006366 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006367}
6368
Balazs Keri3b30d652018-10-19 13:32:20 +00006369ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6370 auto Imp = importSeq(
6371 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6372 E->getRParenLoc(), E->getType());
6373 if (!Imp)
6374 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006375
Balazs Keri3b30d652018-10-19 13:32:20 +00006376 SourceLocation ToBuiltinLoc, ToRParenLoc;
6377 Expr *ToSubExpr;
6378 TypeSourceInfo *ToWrittenTypeInfo;
6379 QualType ToType;
6380 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6381 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006382
6383 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006384 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6385 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006386}
6387
Tom Roeder521f0042019-02-26 19:26:41 +00006388ExpectedStmt ASTNodeImporter::VisitChooseExpr(ChooseExpr *E) {
6389 auto Imp = importSeq(E->getCond(), E->getLHS(), E->getRHS(),
6390 E->getBuiltinLoc(), E->getRParenLoc(), E->getType());
6391 if (!Imp)
6392 return Imp.takeError();
6393
6394 Expr *ToCond;
6395 Expr *ToLHS;
6396 Expr *ToRHS;
6397 SourceLocation ToBuiltinLoc, ToRParenLoc;
6398 QualType ToType;
6399 std::tie(ToCond, ToLHS, ToRHS, ToBuiltinLoc, ToRParenLoc, ToType) = *Imp;
6400
6401 ExprValueKind VK = E->getValueKind();
6402 ExprObjectKind OK = E->getObjectKind();
6403
6404 bool TypeDependent = ToCond->isTypeDependent();
6405 bool ValueDependent = ToCond->isValueDependent();
6406
6407 // The value of CondIsTrue only matters if the value is not
6408 // condition-dependent.
6409 bool CondIsTrue = !E->isConditionDependent() && E->isConditionTrue();
6410
6411 return new (Importer.getToContext())
6412 ChooseExpr(ToBuiltinLoc, ToCond, ToLHS, ToRHS, ToType, VK, OK,
6413 ToRParenLoc, CondIsTrue, TypeDependent, ValueDependent);
6414}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006415
Balazs Keri3b30d652018-10-19 13:32:20 +00006416ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6417 ExpectedType TypeOrErr = import(E->getType());
6418 if (!TypeOrErr)
6419 return TypeOrErr.takeError();
6420
6421 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6422 if (!BeginLocOrErr)
6423 return BeginLocOrErr.takeError();
6424
6425 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006426}
6427
Balazs Keri3b30d652018-10-19 13:32:20 +00006428ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6429 auto Imp = importSeq(
6430 E->getBeginLoc(), E->getType(), E->getFunctionName());
6431 if (!Imp)
6432 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006433
Balazs Keri3b30d652018-10-19 13:32:20 +00006434 SourceLocation ToBeginLoc;
6435 QualType ToType;
6436 StringLiteral *ToFunctionName;
6437 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006438
Bruno Ricci17ff0262018-10-27 19:21:19 +00006439 return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType,
6440 E->getIdentKind(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006441}
6442
Balazs Keri3b30d652018-10-19 13:32:20 +00006443ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6444 auto Imp = importSeq(
6445 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6446 E->getLocation(), E->getType());
6447 if (!Imp)
6448 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006449
Balazs Keri3b30d652018-10-19 13:32:20 +00006450 NestedNameSpecifierLoc ToQualifierLoc;
6451 SourceLocation ToTemplateKeywordLoc, ToLocation;
6452 ValueDecl *ToDecl;
6453 QualType ToType;
6454 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6455 *Imp;
6456
6457 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006458 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006459 auto FoundDOrErr = import(E->getFoundDecl());
6460 if (!FoundDOrErr)
6461 return FoundDOrErr.takeError();
6462 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006463 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006464
Aleksei Sidorina693b372016-09-28 10:16:56 +00006465 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006466 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006467 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006468 if (Error Err =
Balázs Kéria9f10eb2019-12-05 16:21:21 +01006469 ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6470 E->template_arguments(), ToTAInfo))
Balazs Keri3b30d652018-10-19 13:32:20 +00006471 return std::move(Err);
6472 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006473 }
6474
Balazs Keri3b30d652018-10-19 13:32:20 +00006475 auto *ToE = DeclRefExpr::Create(
6476 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6477 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
Richard Smith715f7a12019-06-11 17:50:32 +00006478 E->getValueKind(), ToFoundD, ToResInfo, E->isNonOdrUse());
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006479 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006480 ToE->setHadMultipleCandidates(true);
6481 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006482}
6483
Balazs Keri3b30d652018-10-19 13:32:20 +00006484ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6485 ExpectedType TypeOrErr = import(E->getType());
6486 if (!TypeOrErr)
6487 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006488
Balazs Keri3b30d652018-10-19 13:32:20 +00006489 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006490}
6491
Balazs Keri3b30d652018-10-19 13:32:20 +00006492ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6493 ExpectedExpr ToInitOrErr = import(E->getInit());
6494 if (!ToInitOrErr)
6495 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006496
Balazs Keri3b30d652018-10-19 13:32:20 +00006497 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6498 if (!ToEqualOrColonLocOrErr)
6499 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006500
Balazs Keri3b30d652018-10-19 13:32:20 +00006501 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006502 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006503 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6504 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6505 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006506 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006507 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006508 }
6509
Balazs Keri3b30d652018-10-19 13:32:20 +00006510 SmallVector<Designator, 4> ToDesignators(E->size());
6511 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6512 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006513
6514 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006515 Importer.getToContext(), ToDesignators,
6516 ToIndexExprs, *ToEqualOrColonLocOrErr,
6517 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006518}
6519
Balazs Keri3b30d652018-10-19 13:32:20 +00006520ExpectedStmt
6521ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6522 ExpectedType ToTypeOrErr = import(E->getType());
6523 if (!ToTypeOrErr)
6524 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006525
Balazs Keri3b30d652018-10-19 13:32:20 +00006526 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6527 if (!ToLocationOrErr)
6528 return ToLocationOrErr.takeError();
6529
6530 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6531 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006532}
6533
Balazs Keri3b30d652018-10-19 13:32:20 +00006534ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6535 ExpectedType ToTypeOrErr = import(E->getType());
6536 if (!ToTypeOrErr)
6537 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006538
Balazs Keri3b30d652018-10-19 13:32:20 +00006539 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6540 if (!ToLocationOrErr)
6541 return ToLocationOrErr.takeError();
6542
6543 return IntegerLiteral::Create(
6544 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006545}
6546
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006547
Balazs Keri3b30d652018-10-19 13:32:20 +00006548ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6549 ExpectedType ToTypeOrErr = import(E->getType());
6550 if (!ToTypeOrErr)
6551 return ToTypeOrErr.takeError();
6552
6553 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6554 if (!ToLocationOrErr)
6555 return ToLocationOrErr.takeError();
6556
6557 return FloatingLiteral::Create(
6558 Importer.getToContext(), E->getValue(), E->isExact(),
6559 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006560}
6561
Balazs Keri3b30d652018-10-19 13:32:20 +00006562ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6563 auto ToTypeOrErr = import(E->getType());
6564 if (!ToTypeOrErr)
6565 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006566
Balazs Keri3b30d652018-10-19 13:32:20 +00006567 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6568 if (!ToSubExprOrErr)
6569 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006570
Balazs Keri3b30d652018-10-19 13:32:20 +00006571 return new (Importer.getToContext()) ImaginaryLiteral(
6572 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006573}
6574
Balazs Keri3b30d652018-10-19 13:32:20 +00006575ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6576 ExpectedType ToTypeOrErr = import(E->getType());
6577 if (!ToTypeOrErr)
6578 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006579
Balazs Keri3b30d652018-10-19 13:32:20 +00006580 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6581 if (!ToLocationOrErr)
6582 return ToLocationOrErr.takeError();
6583
6584 return new (Importer.getToContext()) CharacterLiteral(
6585 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006586}
6587
Balazs Keri3b30d652018-10-19 13:32:20 +00006588ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6589 ExpectedType ToTypeOrErr = import(E->getType());
6590 if (!ToTypeOrErr)
6591 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006592
Balazs Keri3b30d652018-10-19 13:32:20 +00006593 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6594 if (Error Err = ImportArrayChecked(
6595 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6596 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006597
Balazs Keri3b30d652018-10-19 13:32:20 +00006598 return StringLiteral::Create(
6599 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6600 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006601}
6602
Balazs Keri3b30d652018-10-19 13:32:20 +00006603ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6604 auto Imp = importSeq(
6605 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6606 E->getInitializer());
6607 if (!Imp)
6608 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006609
Balazs Keri3b30d652018-10-19 13:32:20 +00006610 SourceLocation ToLParenLoc;
6611 TypeSourceInfo *ToTypeSourceInfo;
6612 QualType ToType;
6613 Expr *ToInitializer;
6614 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006615
6616 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006617 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6618 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006619}
6620
Balazs Keri3b30d652018-10-19 13:32:20 +00006621ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6622 auto Imp = importSeq(
6623 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6624 if (!Imp)
6625 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006626
Balazs Keri3b30d652018-10-19 13:32:20 +00006627 SourceLocation ToBuiltinLoc, ToRParenLoc;
6628 QualType ToType;
6629 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6630
6631 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6632 if (Error Err = ImportArrayChecked(
6633 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6634 ToExprs.begin()))
6635 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006636
6637 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006638 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006639}
6640
Balazs Keri3b30d652018-10-19 13:32:20 +00006641ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6642 auto Imp = importSeq(
6643 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6644 if (!Imp)
6645 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006646
Balazs Keri3b30d652018-10-19 13:32:20 +00006647 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6648 LabelDecl *ToLabel;
6649 QualType ToType;
6650 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006651
6652 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006653 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006654}
6655
Bill Wendling8003edc2018-11-09 00:41:36 +00006656ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) {
6657 auto Imp = importSeq(E->getSubExpr());
6658 if (!Imp)
6659 return Imp.takeError();
6660
6661 Expr *ToSubExpr;
6662 std::tie(ToSubExpr) = *Imp;
6663
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00006664 // TODO : Handle APValue::ValueKind that require importing.
6665 APValue::ValueKind Kind = E->getResultAPValueKind();
6666 if (Kind == APValue::Int || Kind == APValue::Float ||
6667 Kind == APValue::FixedPoint || Kind == APValue::ComplexFloat ||
6668 Kind == APValue::ComplexInt)
6669 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr,
6670 E->getAPValueResult());
Fangrui Song407659a2018-11-30 23:41:18 +00006671 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr);
Bill Wendling8003edc2018-11-09 00:41:36 +00006672}
6673
Balazs Keri3b30d652018-10-19 13:32:20 +00006674ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6675 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6676 if (!Imp)
6677 return Imp.takeError();
6678
6679 SourceLocation ToLParen, ToRParen;
6680 Expr *ToSubExpr;
6681 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006682
Fangrui Song6907ce22018-07-30 19:24:48 +00006683 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006684 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006685}
6686
Balazs Keri3b30d652018-10-19 13:32:20 +00006687ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6688 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6689 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6690 return std::move(Err);
6691
6692 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6693 if (!ToLParenLocOrErr)
6694 return ToLParenLocOrErr.takeError();
6695
6696 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6697 if (!ToRParenLocOrErr)
6698 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006699
Bruno Riccif49e1ca2018-11-20 16:20:40 +00006700 return ParenListExpr::Create(Importer.getToContext(), *ToLParenLocOrErr,
6701 ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006702}
6703
Balazs Keri3b30d652018-10-19 13:32:20 +00006704ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6705 auto Imp = importSeq(
6706 E->getSubStmt(), E->getType(), E->getLParenLoc(), E->getRParenLoc());
6707 if (!Imp)
6708 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006709
Balazs Keri3b30d652018-10-19 13:32:20 +00006710 CompoundStmt *ToSubStmt;
6711 QualType ToType;
6712 SourceLocation ToLParenLoc, ToRParenLoc;
6713 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006714
Balazs Keri3b30d652018-10-19 13:32:20 +00006715 return new (Importer.getToContext()) StmtExpr(
6716 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006717}
6718
Balazs Keri3b30d652018-10-19 13:32:20 +00006719ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6720 auto Imp = importSeq(
6721 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6722 if (!Imp)
6723 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006724
Balazs Keri3b30d652018-10-19 13:32:20 +00006725 Expr *ToSubExpr;
6726 QualType ToType;
6727 SourceLocation ToOperatorLoc;
6728 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006729
Aaron Ballmana5038552018-01-09 13:07:03 +00006730 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006731 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6732 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006733}
6734
Balazs Keri3b30d652018-10-19 13:32:20 +00006735ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006736ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006737 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6738 if (!Imp)
6739 return Imp.takeError();
6740
6741 QualType ToType;
6742 SourceLocation ToOperatorLoc, ToRParenLoc;
6743 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006744
Douglas Gregord8552cd2010-02-19 01:24:23 +00006745 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006746 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6747 import(E->getArgumentTypeInfo());
6748 if (!ToArgumentTypeInfoOrErr)
6749 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006750
Balazs Keri3b30d652018-10-19 13:32:20 +00006751 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6752 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6753 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006754 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006755
Balazs Keri3b30d652018-10-19 13:32:20 +00006756 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6757 if (!ToArgumentExprOrErr)
6758 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006759
Balazs Keri3b30d652018-10-19 13:32:20 +00006760 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6761 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006762}
6763
Balazs Keri3b30d652018-10-19 13:32:20 +00006764ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6765 auto Imp = importSeq(
6766 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6767 if (!Imp)
6768 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006769
Balazs Keri3b30d652018-10-19 13:32:20 +00006770 Expr *ToLHS, *ToRHS;
6771 QualType ToType;
6772 SourceLocation ToOperatorLoc;
6773 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006774
Balazs Keri3b30d652018-10-19 13:32:20 +00006775 return new (Importer.getToContext()) BinaryOperator(
6776 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6777 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006778}
6779
Balazs Keri3b30d652018-10-19 13:32:20 +00006780ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6781 auto Imp = importSeq(
6782 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6783 E->getRHS(), E->getType());
6784 if (!Imp)
6785 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006786
Balazs Keri3b30d652018-10-19 13:32:20 +00006787 Expr *ToCond, *ToLHS, *ToRHS;
6788 SourceLocation ToQuestionLoc, ToColonLoc;
6789 QualType ToType;
6790 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006791
6792 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006793 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6794 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006795}
6796
Balazs Keri3b30d652018-10-19 13:32:20 +00006797ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006798 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006799 auto Imp = importSeq(
6800 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6801 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6802 if (!Imp)
6803 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006804
Balazs Keri3b30d652018-10-19 13:32:20 +00006805 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6806 OpaqueValueExpr *ToOpaqueValue;
6807 SourceLocation ToQuestionLoc, ToColonLoc;
6808 QualType ToType;
6809 std::tie(
6810 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6811 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006812
6813 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006814 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6815 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6816 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006817}
6818
Balazs Keri3b30d652018-10-19 13:32:20 +00006819ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6820 auto Imp = importSeq(
6821 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6822 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6823 if (!Imp)
6824 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006825
Balazs Keri3b30d652018-10-19 13:32:20 +00006826 SourceLocation ToBeginLoc, ToEndLoc;
6827 TypeSourceInfo *ToQueriedTypeSourceInfo;
6828 Expr *ToDimensionExpression;
6829 QualType ToType;
6830 std::tie(
6831 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6832 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006833
6834 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006835 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6836 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006837}
6838
Balazs Keri3b30d652018-10-19 13:32:20 +00006839ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6840 auto Imp = importSeq(
6841 E->getBeginLoc(), E->getQueriedExpression(), E->getEndLoc(), E->getType());
6842 if (!Imp)
6843 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006844
Balazs Keri3b30d652018-10-19 13:32:20 +00006845 SourceLocation ToBeginLoc, ToEndLoc;
6846 Expr *ToQueriedExpression;
6847 QualType ToType;
6848 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006849
6850 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006851 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6852 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006853}
6854
Balazs Keri3b30d652018-10-19 13:32:20 +00006855ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6856 auto Imp = importSeq(
6857 E->getLocation(), E->getType(), E->getSourceExpr());
6858 if (!Imp)
6859 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006860
Balazs Keri3b30d652018-10-19 13:32:20 +00006861 SourceLocation ToLocation;
6862 QualType ToType;
6863 Expr *ToSourceExpr;
6864 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006865
6866 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006867 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006868}
6869
Balazs Keri3b30d652018-10-19 13:32:20 +00006870ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6871 auto Imp = importSeq(
6872 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6873 if (!Imp)
6874 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006875
Balazs Keri3b30d652018-10-19 13:32:20 +00006876 Expr *ToLHS, *ToRHS;
6877 SourceLocation ToRBracketLoc;
6878 QualType ToType;
6879 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006880
6881 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006882 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6883 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006884}
6885
Balazs Keri3b30d652018-10-19 13:32:20 +00006886ExpectedStmt
6887ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6888 auto Imp = importSeq(
6889 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6890 E->getComputationResultType(), E->getOperatorLoc());
6891 if (!Imp)
6892 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006893
Balazs Keri3b30d652018-10-19 13:32:20 +00006894 Expr *ToLHS, *ToRHS;
6895 QualType ToType, ToComputationLHSType, ToComputationResultType;
6896 SourceLocation ToOperatorLoc;
6897 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6898 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006899
Balazs Keri3b30d652018-10-19 13:32:20 +00006900 return new (Importer.getToContext()) CompoundAssignOperator(
6901 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6902 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6903 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006904}
6905
Balazs Keri3b30d652018-10-19 13:32:20 +00006906Expected<CXXCastPath>
6907ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6908 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006909 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006910 if (auto SpecOrErr = import(*I))
6911 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006912 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006913 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006914 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006915 return Path;
John McCallcf142162010-08-07 06:22:56 +00006916}
6917
Balazs Keri3b30d652018-10-19 13:32:20 +00006918ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6919 ExpectedType ToTypeOrErr = import(E->getType());
6920 if (!ToTypeOrErr)
6921 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006922
Balazs Keri3b30d652018-10-19 13:32:20 +00006923 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6924 if (!ToSubExprOrErr)
6925 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006926
Balazs Keri3b30d652018-10-19 13:32:20 +00006927 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6928 if (!ToBasePathOrErr)
6929 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006930
Balazs Keri3b30d652018-10-19 13:32:20 +00006931 return ImplicitCastExpr::Create(
6932 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6933 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006934}
6935
Balazs Keri3b30d652018-10-19 13:32:20 +00006936ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6937 auto Imp1 = importSeq(
6938 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6939 if (!Imp1)
6940 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006941
Balazs Keri3b30d652018-10-19 13:32:20 +00006942 QualType ToType;
6943 Expr *ToSubExpr;
6944 TypeSourceInfo *ToTypeInfoAsWritten;
6945 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006946
Balazs Keri3b30d652018-10-19 13:32:20 +00006947 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6948 if (!ToBasePathOrErr)
6949 return ToBasePathOrErr.takeError();
6950 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006951
Aleksei Sidorina693b372016-09-28 10:16:56 +00006952 switch (E->getStmtClass()) {
6953 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006954 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006955 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6956 if (!ToLParenLocOrErr)
6957 return ToLParenLocOrErr.takeError();
6958 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6959 if (!ToRParenLocOrErr)
6960 return ToRParenLocOrErr.takeError();
6961 return CStyleCastExpr::Create(
6962 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6963 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6964 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006965 }
6966
6967 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006968 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006969 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6970 if (!ToLParenLocOrErr)
6971 return ToLParenLocOrErr.takeError();
6972 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6973 if (!ToRParenLocOrErr)
6974 return ToRParenLocOrErr.takeError();
6975 return CXXFunctionalCastExpr::Create(
6976 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6977 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6978 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006979 }
6980
6981 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006982 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6983 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6984 if (!ToLParenLocOrErr)
6985 return ToLParenLocOrErr.takeError();
6986 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6987 if (!ToBridgeKeywordLocOrErr)
6988 return ToBridgeKeywordLocOrErr.takeError();
6989 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6990 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6991 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006992 }
6993 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006994 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006995 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006996 }
6997}
6998
Balazs Keri3b30d652018-10-19 13:32:20 +00006999ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
7000 SmallVector<OffsetOfNode, 4> ToNodes;
7001 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
7002 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007003
Balazs Keri3b30d652018-10-19 13:32:20 +00007004 SourceLocation ToBeginLoc, ToEndLoc;
7005 if (FromNode.getKind() != OffsetOfNode::Base) {
7006 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
7007 if (!Imp)
7008 return Imp.takeError();
7009 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
7010 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00007011
Balazs Keri3b30d652018-10-19 13:32:20 +00007012 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00007013 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00007014 ToNodes.push_back(
7015 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00007016 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007017 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00007018 auto ToBSOrErr = import(FromNode.getBase());
7019 if (!ToBSOrErr)
7020 return ToBSOrErr.takeError();
7021 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00007022 break;
7023 }
7024 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00007025 auto ToFieldOrErr = import(FromNode.getField());
7026 if (!ToFieldOrErr)
7027 return ToFieldOrErr.takeError();
7028 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00007029 break;
7030 }
7031 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00007032 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
7033 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00007034 break;
7035 }
7036 }
7037 }
7038
Balazs Keri3b30d652018-10-19 13:32:20 +00007039 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
7040 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
7041 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
7042 if (!ToIndexExprOrErr)
7043 return ToIndexExprOrErr.takeError();
7044 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007045 }
7046
Balazs Keri3b30d652018-10-19 13:32:20 +00007047 auto Imp = importSeq(
7048 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
7049 E->getRParenLoc());
7050 if (!Imp)
7051 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007052
Balazs Keri3b30d652018-10-19 13:32:20 +00007053 QualType ToType;
7054 TypeSourceInfo *ToTypeSourceInfo;
7055 SourceLocation ToOperatorLoc, ToRParenLoc;
7056 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
7057
7058 return OffsetOfExpr::Create(
7059 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
7060 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007061}
7062
Balazs Keri3b30d652018-10-19 13:32:20 +00007063ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
7064 auto Imp = importSeq(
7065 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
7066 if (!Imp)
7067 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007068
Balazs Keri3b30d652018-10-19 13:32:20 +00007069 QualType ToType;
7070 Expr *ToOperand;
7071 SourceLocation ToBeginLoc, ToEndLoc;
7072 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007073
Balazs Keri3b30d652018-10-19 13:32:20 +00007074 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007075 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00007076 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007077 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007078 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007079
Balazs Keri3b30d652018-10-19 13:32:20 +00007080 return new (Importer.getToContext()) CXXNoexceptExpr(
7081 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007082}
7083
Balazs Keri3b30d652018-10-19 13:32:20 +00007084ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
7085 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
7086 if (!Imp)
7087 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007088
Balazs Keri3b30d652018-10-19 13:32:20 +00007089 Expr *ToSubExpr;
7090 QualType ToType;
7091 SourceLocation ToThrowLoc;
7092 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007093
7094 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007095 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00007096}
7097
Balazs Keri3b30d652018-10-19 13:32:20 +00007098ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
7099 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
7100 if (!ToUsedLocOrErr)
7101 return ToUsedLocOrErr.takeError();
7102
7103 auto ToParamOrErr = import(E->getParam());
7104 if (!ToParamOrErr)
7105 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007106
Eric Fiselier708afb52019-05-16 21:04:15 +00007107 auto UsedContextOrErr = Importer.ImportContext(E->getUsedContext());
7108 if (!UsedContextOrErr)
7109 return UsedContextOrErr.takeError();
7110
Balazs Keric5095942019-08-14 09:41:39 +00007111 // Import the default arg if it was not imported yet.
7112 // This is needed because it can happen that during the import of the
7113 // default expression (from VisitParmVarDecl) the same ParmVarDecl is
7114 // encountered here. The default argument for a ParmVarDecl is set in the
7115 // ParmVarDecl only after it is imported (set in VisitParmVarDecl if not here,
7116 // see VisitParmVarDecl).
7117 ParmVarDecl *ToParam = *ToParamOrErr;
7118 if (!ToParam->getDefaultArg()) {
7119 Optional<ParmVarDecl *> FromParam = Importer.getImportedFromDecl(ToParam);
7120 assert(FromParam && "ParmVarDecl was not imported?");
7121
7122 if (Error Err = ImportDefaultArgOfParmVarDecl(*FromParam, ToParam))
7123 return std::move(Err);
7124 }
7125
7126 return CXXDefaultArgExpr::Create(Importer.getToContext(), *ToUsedLocOrErr,
7127 *ToParamOrErr, *UsedContextOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007128}
7129
Balazs Keri3b30d652018-10-19 13:32:20 +00007130ExpectedStmt
7131ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
7132 auto Imp = importSeq(
7133 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
7134 if (!Imp)
7135 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007136
Balazs Keri3b30d652018-10-19 13:32:20 +00007137 QualType ToType;
7138 TypeSourceInfo *ToTypeSourceInfo;
7139 SourceLocation ToRParenLoc;
7140 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007141
7142 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007143 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007144}
7145
Balazs Keri3b30d652018-10-19 13:32:20 +00007146ExpectedStmt
7147ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
7148 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7149 if (!ToSubExprOrErr)
7150 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007151
Balazs Keri3b30d652018-10-19 13:32:20 +00007152 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
7153 if (!ToDtorOrErr)
7154 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007155
7156 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00007157 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
7158 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007159}
7160
Balazs Keri3b30d652018-10-19 13:32:20 +00007161ExpectedStmt
7162ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
7163 auto Imp = importSeq(
7164 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
7165 E->getParenOrBraceRange());
7166 if (!Imp)
7167 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007168
Balazs Keri3b30d652018-10-19 13:32:20 +00007169 CXXConstructorDecl *ToConstructor;
7170 QualType ToType;
7171 TypeSourceInfo *ToTypeSourceInfo;
7172 SourceRange ToParenOrBraceRange;
7173 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007174
Balazs Keri3b30d652018-10-19 13:32:20 +00007175 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
7176 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7177 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007178
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00007179 return CXXTemporaryObjectExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007180 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
7181 ToParenOrBraceRange, E->hadMultipleCandidates(),
7182 E->isListInitialization(), E->isStdInitListInitialization(),
7183 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00007184}
7185
Tykerb0561b32019-11-17 11:41:55 +01007186ExpectedDecl ASTNodeImporter::VisitLifetimeExtendedTemporaryDecl(
7187 LifetimeExtendedTemporaryDecl *D) {
7188 DeclContext *DC, *LexicalDC;
7189 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
7190 return std::move(Err);
7191
7192 auto Imp = importSeq(D->getTemporaryExpr(), D->getExtendingDecl());
7193 // FIXME: the APValue should be imported as well if present.
7194 if (!Imp)
7195 return Imp.takeError();
7196
7197 Expr *Temporary;
7198 ValueDecl *ExtendingDecl;
7199 std::tie(Temporary, ExtendingDecl) = *Imp;
7200 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
7201
7202 LifetimeExtendedTemporaryDecl *To;
7203 if (GetImportedOrCreateDecl(To, D, Temporary, ExtendingDecl,
7204 D->getManglingNumber()))
7205 return To;
7206
7207 To->setLexicalDeclContext(LexicalDC);
7208 LexicalDC->addDeclInternal(To);
7209 return To;
7210}
7211
Balazs Keri3b30d652018-10-19 13:32:20 +00007212ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00007213ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Tykerb0561b32019-11-17 11:41:55 +01007214 auto Imp = importSeq(E->getType(),
7215 E->getLifetimeExtendedTemporaryDecl() ? nullptr
7216 : E->getSubExpr(),
7217 E->getLifetimeExtendedTemporaryDecl());
Balazs Keri3b30d652018-10-19 13:32:20 +00007218 if (!Imp)
7219 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007220
Balazs Keri3b30d652018-10-19 13:32:20 +00007221 QualType ToType;
7222 Expr *ToTemporaryExpr;
Tykerb0561b32019-11-17 11:41:55 +01007223 LifetimeExtendedTemporaryDecl *ToMaterializedDecl;
7224 std::tie(ToType, ToTemporaryExpr, ToMaterializedDecl) = *Imp;
7225 if (!ToTemporaryExpr)
7226 ToTemporaryExpr = cast<Expr>(ToMaterializedDecl->getTemporaryExpr());
Aleksei Sidorina693b372016-09-28 10:16:56 +00007227
Tykerb0561b32019-11-17 11:41:55 +01007228 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
7229 ToType, ToTemporaryExpr, E->isBoundToLvalueReference(),
7230 ToMaterializedDecl);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007231
Aleksei Sidorina693b372016-09-28 10:16:56 +00007232 return ToMTE;
7233}
7234
Balazs Keri3b30d652018-10-19 13:32:20 +00007235ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
7236 auto Imp = importSeq(
7237 E->getType(), E->getPattern(), E->getEllipsisLoc());
7238 if (!Imp)
7239 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00007240
Balazs Keri3b30d652018-10-19 13:32:20 +00007241 QualType ToType;
7242 Expr *ToPattern;
7243 SourceLocation ToEllipsisLoc;
7244 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00007245
7246 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007247 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00007248}
7249
Balazs Keri3b30d652018-10-19 13:32:20 +00007250ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
7251 auto Imp = importSeq(
7252 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
7253 if (!Imp)
7254 return Imp.takeError();
7255
7256 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
7257 NamedDecl *ToPack;
7258 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007259
7260 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007261 if (!E->isValueDependent())
7262 Length = E->getPackLength();
7263
Balazs Keri3b30d652018-10-19 13:32:20 +00007264 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007265 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007266 if (Error Err = ImportTemplateArguments(
7267 E->getPartialArguments().data(),
7268 E->getPartialArguments().size(),
7269 ToPartialArguments))
7270 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007271 }
7272
7273 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007274 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
7275 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007276}
7277
Aleksei Sidorina693b372016-09-28 10:16:56 +00007278
Balazs Keri3b30d652018-10-19 13:32:20 +00007279ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
7280 auto Imp = importSeq(
7281 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
7282 E->getArraySize(), E->getInitializer(), E->getType(),
7283 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
7284 E->getDirectInitRange());
7285 if (!Imp)
7286 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007287
Balazs Keri3b30d652018-10-19 13:32:20 +00007288 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
7289 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
Richard Smithb9fb1212019-05-06 03:47:15 +00007290 Optional<Expr *> ToArraySize;
7291 Expr *ToInitializer;
Balazs Keri3b30d652018-10-19 13:32:20 +00007292 QualType ToType;
7293 TypeSourceInfo *ToAllocatedTypeSourceInfo;
7294 std::tie(
7295 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
7296 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007297
Balazs Keri3b30d652018-10-19 13:32:20 +00007298 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
7299 if (Error Err =
7300 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
7301 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007302
Bruno Ricci9b6dfac2019-01-07 15:04:45 +00007303 return CXXNewExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007304 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
7305 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
7306 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
7307 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
7308 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007309}
7310
Balazs Keri3b30d652018-10-19 13:32:20 +00007311ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
7312 auto Imp = importSeq(
7313 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
7314 if (!Imp)
7315 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007316
Balazs Keri3b30d652018-10-19 13:32:20 +00007317 QualType ToType;
7318 FunctionDecl *ToOperatorDelete;
7319 Expr *ToArgument;
7320 SourceLocation ToBeginLoc;
7321 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007322
7323 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007324 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
7325 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
7326 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00007327}
7328
Balazs Keri3b30d652018-10-19 13:32:20 +00007329ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
7330 auto Imp = importSeq(
7331 E->getType(), E->getLocation(), E->getConstructor(),
7332 E->getParenOrBraceRange());
7333 if (!Imp)
7334 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007335
Balazs Keri3b30d652018-10-19 13:32:20 +00007336 QualType ToType;
7337 SourceLocation ToLocation;
7338 CXXConstructorDecl *ToConstructor;
7339 SourceRange ToParenOrBraceRange;
7340 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007341
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007342 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007343 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7344 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007345
Balazs Keri3b30d652018-10-19 13:32:20 +00007346 return CXXConstructExpr::Create(
7347 Importer.getToContext(), ToType, ToLocation, ToConstructor,
7348 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
7349 E->isListInitialization(), E->isStdInitListInitialization(),
7350 E->requiresZeroInitialization(), E->getConstructionKind(),
7351 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00007352}
7353
Balazs Keri3b30d652018-10-19 13:32:20 +00007354ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
7355 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7356 if (!ToSubExprOrErr)
7357 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007358
Balazs Keri3b30d652018-10-19 13:32:20 +00007359 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
7360 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
7361 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007362
Balazs Keri3b30d652018-10-19 13:32:20 +00007363 return ExprWithCleanups::Create(
7364 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
7365 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007366}
7367
Balazs Keri3b30d652018-10-19 13:32:20 +00007368ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
7369 auto Imp = importSeq(
7370 E->getCallee(), E->getType(), E->getRParenLoc());
7371 if (!Imp)
7372 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007373
Balazs Keri3b30d652018-10-19 13:32:20 +00007374 Expr *ToCallee;
7375 QualType ToType;
7376 SourceLocation ToRParenLoc;
7377 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00007378
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007379 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007380 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7381 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007382
Bruno Riccic5885cf2018-12-21 15:20:32 +00007383 return CXXMemberCallExpr::Create(Importer.getToContext(), ToCallee, ToArgs,
7384 ToType, E->getValueKind(), ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00007385}
7386
Balazs Keri3b30d652018-10-19 13:32:20 +00007387ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
7388 ExpectedType ToTypeOrErr = import(E->getType());
7389 if (!ToTypeOrErr)
7390 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007391
Balazs Keri3b30d652018-10-19 13:32:20 +00007392 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7393 if (!ToLocationOrErr)
7394 return ToLocationOrErr.takeError();
7395
7396 return new (Importer.getToContext()) CXXThisExpr(
7397 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00007398}
7399
Balazs Keri3b30d652018-10-19 13:32:20 +00007400ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
7401 ExpectedType ToTypeOrErr = import(E->getType());
7402 if (!ToTypeOrErr)
7403 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007404
Balazs Keri3b30d652018-10-19 13:32:20 +00007405 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7406 if (!ToLocationOrErr)
7407 return ToLocationOrErr.takeError();
7408
7409 return new (Importer.getToContext()) CXXBoolLiteralExpr(
7410 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00007411}
7412
Balazs Keri3b30d652018-10-19 13:32:20 +00007413ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7414 auto Imp1 = importSeq(
7415 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7416 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7417 if (!Imp1)
7418 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007419
Balazs Keri3b30d652018-10-19 13:32:20 +00007420 Expr *ToBase;
7421 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7422 NestedNameSpecifierLoc ToQualifierLoc;
7423 ValueDecl *ToMemberDecl;
7424 QualType ToType;
7425 std::tie(
7426 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7427 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007428
Balazs Keri3b30d652018-10-19 13:32:20 +00007429 auto Imp2 = importSeq(
7430 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7431 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7432 if (!Imp2)
7433 return Imp2.takeError();
7434 NamedDecl *ToDecl;
7435 DeclarationName ToName;
7436 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7437 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007438
7439 DeclAccessPair ToFoundDecl =
7440 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007441
Balazs Keri3b30d652018-10-19 13:32:20 +00007442 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007443
Gabor Marton5caba302019-03-07 13:38:20 +00007444 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007445 if (E->hasExplicitTemplateArgs()) {
Gabor Marton5caba302019-03-07 13:38:20 +00007446 if (Error Err =
7447 ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
7448 E->template_arguments(), ToTAInfo))
7449 return std::move(Err);
7450 ResInfo = &ToTAInfo;
Sean Callanan59721b32015-04-28 18:41:46 +00007451 }
7452
Richard Smith1bbad592019-06-11 17:50:36 +00007453 return MemberExpr::Create(Importer.getToContext(), ToBase, E->isArrow(),
7454 ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7455 ToMemberDecl, ToFoundDecl, ToMemberNameInfo,
7456 ResInfo, ToType, E->getValueKind(),
7457 E->getObjectKind(), E->isNonOdrUse());
Sean Callanan59721b32015-04-28 18:41:46 +00007458}
7459
Balazs Keri3b30d652018-10-19 13:32:20 +00007460ExpectedStmt
7461ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7462 auto Imp = importSeq(
7463 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7464 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7465 if (!Imp)
7466 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007467
Balazs Keri3b30d652018-10-19 13:32:20 +00007468 Expr *ToBase;
7469 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7470 NestedNameSpecifierLoc ToQualifierLoc;
7471 TypeSourceInfo *ToScopeTypeInfo;
7472 std::tie(
7473 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7474 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007475
7476 PseudoDestructorTypeStorage Storage;
7477 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7478 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007479 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7480 if (!ToDestroyedTypeLocOrErr)
7481 return ToDestroyedTypeLocOrErr.takeError();
7482 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007483 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007484 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7485 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7486 else
7487 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007488 }
7489
7490 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007491 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7492 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007493}
7494
Balazs Keri3b30d652018-10-19 13:32:20 +00007495ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007496 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007497 auto Imp = importSeq(
7498 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7499 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7500 if (!Imp)
7501 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007502
Balazs Keri3b30d652018-10-19 13:32:20 +00007503 QualType ToType;
7504 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7505 NestedNameSpecifierLoc ToQualifierLoc;
7506 NamedDecl *ToFirstQualifierFoundInScope;
7507 std::tie(
7508 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7509 ToFirstQualifierFoundInScope) = *Imp;
7510
7511 Expr *ToBase = nullptr;
7512 if (!E->isImplicitAccess()) {
7513 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7514 ToBase = *ToBaseOrErr;
7515 else
7516 return ToBaseOrErr.takeError();
7517 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007518
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007519 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007520 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007521 if (Error Err = ImportTemplateArgumentListInfo(
7522 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7523 ToTAInfo))
7524 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007525 ResInfo = &ToTAInfo;
7526 }
7527
Balazs Keri3b30d652018-10-19 13:32:20 +00007528 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7529 if (!ToMemberNameInfoOrErr)
7530 return ToMemberNameInfoOrErr.takeError();
7531 DeclarationNameInfo ToMemberNameInfo(
7532 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007533 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007534 if (Error Err = ImportDeclarationNameLoc(
7535 E->getMemberNameInfo(), ToMemberNameInfo))
7536 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007537
7538 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007539 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7540 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7541 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007542}
7543
Balazs Keri3b30d652018-10-19 13:32:20 +00007544ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007545ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balázs Kéria9f10eb2019-12-05 16:21:21 +01007546 auto Imp = importSeq(E->getQualifierLoc(), E->getTemplateKeywordLoc(),
7547 E->getDeclName(), E->getNameInfo().getLoc(),
7548 E->getLAngleLoc(), E->getRAngleLoc());
Balazs Keri3b30d652018-10-19 13:32:20 +00007549 if (!Imp)
7550 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007551
Balazs Keri3b30d652018-10-19 13:32:20 +00007552 NestedNameSpecifierLoc ToQualifierLoc;
Balázs Kéria9f10eb2019-12-05 16:21:21 +01007553 SourceLocation ToTemplateKeywordLoc, ToNameLoc, ToLAngleLoc, ToRAngleLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00007554 DeclarationName ToDeclName;
Balázs Kéria9f10eb2019-12-05 16:21:21 +01007555 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToNameLoc,
7556 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007557
Balázs Kéria9f10eb2019-12-05 16:21:21 +01007558 DeclarationNameInfo ToNameInfo(ToDeclName, ToNameLoc);
Balazs Keri3b30d652018-10-19 13:32:20 +00007559 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7560 return std::move(Err);
7561
7562 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007563 TemplateArgumentListInfo *ResInfo = nullptr;
7564 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007565 if (Error Err =
7566 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7567 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007568 ResInfo = &ToTAInfo;
7569 }
7570
7571 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007572 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7573 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007574}
7575
Balazs Keri3b30d652018-10-19 13:32:20 +00007576ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7577 CXXUnresolvedConstructExpr *E) {
7578 auto Imp = importSeq(
7579 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7580 if (!Imp)
7581 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007582
Balazs Keri3b30d652018-10-19 13:32:20 +00007583 SourceLocation ToLParenLoc, ToRParenLoc;
7584 TypeSourceInfo *ToTypeSourceInfo;
7585 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7586
7587 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7588 if (Error Err =
7589 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7590 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007591
7592 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007593 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7594 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007595}
7596
Balazs Keri3b30d652018-10-19 13:32:20 +00007597ExpectedStmt
7598ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7599 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7600 if (!ToNamingClassOrErr)
7601 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007602
Balazs Keri3b30d652018-10-19 13:32:20 +00007603 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7604 if (!ToQualifierLocOrErr)
7605 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007606
Balazs Keri3b30d652018-10-19 13:32:20 +00007607 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7608 if (!ToNameInfoOrErr)
7609 return ToNameInfoOrErr.takeError();
7610 DeclarationNameInfo ToNameInfo(
7611 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007612 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007613 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7614 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007615
7616 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007617 for (auto *D : E->decls())
7618 if (auto ToDOrErr = import(D))
7619 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007620 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007621 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007622
Balázs Kéria9f10eb2019-12-05 16:21:21 +01007623 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007624 TemplateArgumentListInfo ToTAInfo;
7625 if (Error Err = ImportTemplateArgumentListInfo(
7626 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7627 ToTAInfo))
7628 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007629
Balazs Keri3b30d652018-10-19 13:32:20 +00007630 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7631 if (!ToTemplateKeywordLocOrErr)
7632 return ToTemplateKeywordLocOrErr.takeError();
7633
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007634 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007635 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7636 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7637 ToDecls.begin(), ToDecls.end());
7638 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007639
7640 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007641 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7642 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7643 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007644}
7645
Balazs Keri3b30d652018-10-19 13:32:20 +00007646ExpectedStmt
7647ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7648 auto Imp1 = importSeq(
7649 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7650 E->getTemplateKeywordLoc());
7651 if (!Imp1)
7652 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007653
Balazs Keri3b30d652018-10-19 13:32:20 +00007654 QualType ToType;
7655 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7656 NestedNameSpecifierLoc ToQualifierLoc;
7657 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7658
7659 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7660 if (!Imp2)
7661 return Imp2.takeError();
7662 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7663 // Import additional name location/type info.
7664 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7665 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007666
7667 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007668 for (Decl *D : E->decls())
7669 if (auto ToDOrErr = import(D))
7670 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007671 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007672 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007673
7674 TemplateArgumentListInfo ToTAInfo;
7675 TemplateArgumentListInfo *ResInfo = nullptr;
7676 if (E->hasExplicitTemplateArgs()) {
Balázs Kéria9f10eb2019-12-05 16:21:21 +01007677 TemplateArgumentListInfo FromTAInfo;
7678 E->copyTemplateArgumentsInto(FromTAInfo);
7679 if (Error Err = ImportTemplateArgumentListInfo(FromTAInfo, ToTAInfo))
Balazs Keri3b30d652018-10-19 13:32:20 +00007680 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007681 ResInfo = &ToTAInfo;
7682 }
7683
Balazs Keri3b30d652018-10-19 13:32:20 +00007684 Expr *ToBase = nullptr;
7685 if (!E->isImplicitAccess()) {
7686 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7687 ToBase = *ToBaseOrErr;
7688 else
7689 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007690 }
7691
7692 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007693 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7694 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7695 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007696}
7697
Balazs Keri3b30d652018-10-19 13:32:20 +00007698ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7699 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7700 if (!Imp)
7701 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007702
Balazs Keri3b30d652018-10-19 13:32:20 +00007703 Expr *ToCallee;
7704 QualType ToType;
7705 SourceLocation ToRParenLoc;
7706 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007707
7708 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007709 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7710 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7711 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007712
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007713 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
Bruno Riccic5885cf2018-12-21 15:20:32 +00007714 return CXXOperatorCallExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007715 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
Eric Fiselier5cdc2cd2018-12-12 21:50:55 +00007716 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures(),
7717 OCE->getADLCallKind());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007718 }
7719
Bruno Riccic5885cf2018-12-21 15:20:32 +00007720 return CallExpr::Create(Importer.getToContext(), ToCallee, ToArgs, ToType,
7721 E->getValueKind(), ToRParenLoc, /*MinNumArgs=*/0,
7722 E->getADLCallKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007723}
7724
Balazs Keri3b30d652018-10-19 13:32:20 +00007725ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7726 CXXRecordDecl *FromClass = E->getLambdaClass();
7727 auto ToClassOrErr = import(FromClass);
7728 if (!ToClassOrErr)
7729 return ToClassOrErr.takeError();
7730 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007731
Balazs Keri3b30d652018-10-19 13:32:20 +00007732 auto ToCallOpOrErr = import(E->getCallOperator());
7733 if (!ToCallOpOrErr)
7734 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007735
Balazs Keri3b30d652018-10-19 13:32:20 +00007736 SmallVector<LambdaCapture, 8> ToCaptures;
7737 ToCaptures.reserve(E->capture_size());
7738 for (const auto &FromCapture : E->captures()) {
7739 if (auto ToCaptureOrErr = import(FromCapture))
7740 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007741 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007742 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007743 }
7744
Balazs Keri3b30d652018-10-19 13:32:20 +00007745 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7746 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7747 return std::move(Err);
7748
7749 auto Imp = importSeq(
7750 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7751 if (!Imp)
7752 return Imp.takeError();
7753
7754 SourceRange ToIntroducerRange;
7755 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7756 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007757
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007758 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007759 Importer.getToContext(), ToClass, ToIntroducerRange,
7760 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7761 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7762 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007763}
7764
Sean Callanan8bca9962016-03-28 21:43:01 +00007765
Balazs Keri3b30d652018-10-19 13:32:20 +00007766ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7767 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7768 if (!Imp)
7769 return Imp.takeError();
7770
7771 SourceLocation ToLBraceLoc, ToRBraceLoc;
7772 QualType ToType;
7773 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7774
7775 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7776 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7777 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007778
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007779 ASTContext &ToCtx = Importer.getToContext();
7780 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007781 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7782 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007783
Balazs Keri3b30d652018-10-19 13:32:20 +00007784 if (E->hasArrayFiller()) {
7785 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7786 To->setArrayFiller(*ToFillerOrErr);
7787 else
7788 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007789 }
7790
Balazs Keri3b30d652018-10-19 13:32:20 +00007791 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7792 if (auto ToFDOrErr = import(FromFD))
7793 To->setInitializedFieldInUnion(*ToFDOrErr);
7794 else
7795 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007796 }
7797
Balazs Keri3b30d652018-10-19 13:32:20 +00007798 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7799 if (auto ToSyntFormOrErr = import(SyntForm))
7800 To->setSyntacticForm(*ToSyntFormOrErr);
7801 else
7802 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007803 }
7804
Gabor Martona20ce602018-09-03 13:10:53 +00007805 // Copy InitListExprBitfields, which are not handled in the ctor of
7806 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007807 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007808
7809 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007810}
7811
Balazs Keri3b30d652018-10-19 13:32:20 +00007812ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007813 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007814 ExpectedType ToTypeOrErr = import(E->getType());
7815 if (!ToTypeOrErr)
7816 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007817
Balazs Keri3b30d652018-10-19 13:32:20 +00007818 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7819 if (!ToSubExprOrErr)
7820 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007821
Balazs Keri3b30d652018-10-19 13:32:20 +00007822 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7823 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007824}
7825
Balazs Keri3b30d652018-10-19 13:32:20 +00007826ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007827 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007828 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7829 if (!Imp)
7830 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007831
Balazs Keri3b30d652018-10-19 13:32:20 +00007832 SourceLocation ToLocation;
7833 QualType ToType;
7834 CXXConstructorDecl *ToConstructor;
7835 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007836
7837 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007838 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7839 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007840}
7841
Balazs Keri3b30d652018-10-19 13:32:20 +00007842ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7843 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7844 if (!Imp)
7845 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007846
Balazs Keri3b30d652018-10-19 13:32:20 +00007847 QualType ToType;
7848 Expr *ToCommonExpr, *ToSubExpr;
7849 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007850
Balazs Keri3b30d652018-10-19 13:32:20 +00007851 return new (Importer.getToContext()) ArrayInitLoopExpr(
7852 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007853}
7854
Balazs Keri3b30d652018-10-19 13:32:20 +00007855ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7856 ExpectedType ToTypeOrErr = import(E->getType());
7857 if (!ToTypeOrErr)
7858 return ToTypeOrErr.takeError();
7859 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007860}
7861
Balazs Keri3b30d652018-10-19 13:32:20 +00007862ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7863 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7864 if (!ToBeginLocOrErr)
7865 return ToBeginLocOrErr.takeError();
7866
7867 auto ToFieldOrErr = import(E->getField());
7868 if (!ToFieldOrErr)
7869 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007870
Eric Fiselier708afb52019-05-16 21:04:15 +00007871 auto UsedContextOrErr = Importer.ImportContext(E->getUsedContext());
7872 if (!UsedContextOrErr)
7873 return UsedContextOrErr.takeError();
7874
Sean Callanandd2c1742016-05-16 20:48:03 +00007875 return CXXDefaultInitExpr::Create(
Eric Fiselier708afb52019-05-16 21:04:15 +00007876 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr, *UsedContextOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007877}
7878
Balazs Keri3b30d652018-10-19 13:32:20 +00007879ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7880 auto Imp = importSeq(
7881 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7882 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7883 if (!Imp)
7884 return Imp.takeError();
7885
7886 QualType ToType;
7887 Expr *ToSubExpr;
7888 TypeSourceInfo *ToTypeInfoAsWritten;
7889 SourceLocation ToOperatorLoc, ToRParenLoc;
7890 SourceRange ToAngleBrackets;
7891 std::tie(
7892 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7893 ToAngleBrackets) = *Imp;
7894
Sean Callanandd2c1742016-05-16 20:48:03 +00007895 ExprValueKind VK = E->getValueKind();
7896 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007897 auto ToBasePathOrErr = ImportCastPath(E);
7898 if (!ToBasePathOrErr)
7899 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007900
Sean Callanandd2c1742016-05-16 20:48:03 +00007901 if (isa<CXXStaticCastExpr>(E)) {
7902 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007903 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7904 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007905 } else if (isa<CXXDynamicCastExpr>(E)) {
7906 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007907 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7908 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007909 } else if (isa<CXXReinterpretCastExpr>(E)) {
7910 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007911 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7912 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007913 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007914 return CXXConstCastExpr::Create(
7915 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7916 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007917 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007918 llvm_unreachable("Unknown cast type");
7919 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007920 }
7921}
7922
Balazs Keri3b30d652018-10-19 13:32:20 +00007923ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007924 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007925 auto Imp = importSeq(
7926 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7927 if (!Imp)
7928 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007929
Balazs Keri3b30d652018-10-19 13:32:20 +00007930 QualType ToType;
7931 SourceLocation ToExprLoc;
7932 NonTypeTemplateParmDecl *ToParameter;
7933 Expr *ToReplacement;
7934 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007935
7936 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007937 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007938}
7939
Balazs Keri3b30d652018-10-19 13:32:20 +00007940ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7941 auto Imp = importSeq(
7942 E->getType(), E->getBeginLoc(), E->getEndLoc());
7943 if (!Imp)
7944 return Imp.takeError();
7945
7946 QualType ToType;
7947 SourceLocation ToBeginLoc, ToEndLoc;
7948 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007949
7950 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007951 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7952 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007953
7954 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7955 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007956 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007957
7958 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007959 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7960 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007961}
7962
Balazs Keri3b30d652018-10-19 13:32:20 +00007963ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7964 ExpectedType ToTypeOrErr = import(E->getType());
7965 if (!ToTypeOrErr)
7966 return ToTypeOrErr.takeError();
7967
7968 auto ToSourceRangeOrErr = import(E->getSourceRange());
7969 if (!ToSourceRangeOrErr)
7970 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007971
7972 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007973 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7974 return new (Importer.getToContext()) CXXTypeidExpr(
7975 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7976 else
7977 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007978 }
7979
Balazs Keri3b30d652018-10-19 13:32:20 +00007980 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7981 if (!ToExprOperandOrErr)
7982 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007983
Balazs Keri3b30d652018-10-19 13:32:20 +00007984 return new (Importer.getToContext()) CXXTypeidExpr(
7985 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007986}
7987
Balazs Kerib4fd7d42019-08-30 10:12:14 +00007988Error ASTNodeImporter::ImportOverriddenMethods(CXXMethodDecl *ToMethod,
7989 CXXMethodDecl *FromMethod) {
7990 Error ImportErrors = Error::success();
Balazs Keri3b30d652018-10-19 13:32:20 +00007991 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7992 if (auto ImportedOrErr = import(FromOverriddenMethod))
7993 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7994 (*ImportedOrErr)->getCanonicalDecl()));
7995 else
Balazs Kerib4fd7d42019-08-30 10:12:14 +00007996 ImportErrors =
7997 joinErrors(std::move(ImportErrors), ImportedOrErr.takeError());
Balazs Keri3b30d652018-10-19 13:32:20 +00007998 }
Balazs Kerib4fd7d42019-08-30 10:12:14 +00007999 return ImportErrors;
Lang Hames19e07e12017-06-20 21:06:00 +00008000}
8001
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008002ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00008003 ASTContext &FromContext, FileManager &FromFileManager,
Gabor Marton54058b52018-12-17 13:53:12 +00008004 bool MinimalImport,
Gabor Marton2afbfb62019-07-01 15:37:07 +00008005 std::shared_ptr<ASTImporterSharedState> SharedState)
8006 : SharedState(SharedState), ToContext(ToContext), FromContext(FromContext),
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008007 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Gabor Martonf035b752019-08-27 11:36:10 +00008008 Minimal(MinimalImport), ODRHandling(ODRHandlingType::Conservative) {
Gabor Marton54058b52018-12-17 13:53:12 +00008009
Gabor Marton2afbfb62019-07-01 15:37:07 +00008010 // Create a default state without the lookup table: LLDB case.
8011 if (!SharedState) {
8012 this->SharedState = std::make_shared<ASTImporterSharedState>();
8013 }
8014
Gabor Marton54058b52018-12-17 13:53:12 +00008015 ImportedDecls[FromContext.getTranslationUnitDecl()] =
8016 ToContext.getTranslationUnitDecl();
Douglas Gregor62d311f2010-02-09 19:21:46 +00008017}
8018
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008019ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008020
Gabor Marton54058b52018-12-17 13:53:12 +00008021Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) {
8022 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
8023 "Try to get field index for non-field.");
8024
8025 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
8026 if (!Owner)
8027 return None;
8028
8029 unsigned Index = 0;
8030 for (const auto *D : Owner->decls()) {
8031 if (D == F)
8032 return Index;
8033
8034 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
8035 ++Index;
8036 }
8037
8038 llvm_unreachable("Field was not found in its parent context.");
8039
8040 return None;
8041}
8042
8043ASTImporter::FoundDeclsTy
8044ASTImporter::findDeclsInToCtx(DeclContext *DC, DeclarationName Name) {
8045 // We search in the redecl context because of transparent contexts.
8046 // E.g. a simple C language enum is a transparent context:
8047 // enum E { A, B };
8048 // Now if we had a global variable in the TU
8049 // int A;
8050 // then the enum constant 'A' and the variable 'A' violates ODR.
8051 // We can diagnose this only if we search in the redecl context.
8052 DeclContext *ReDC = DC->getRedeclContext();
Gabor Marton2afbfb62019-07-01 15:37:07 +00008053 if (SharedState->getLookupTable()) {
Gabor Marton54058b52018-12-17 13:53:12 +00008054 ASTImporterLookupTable::LookupResult LookupResult =
Gabor Marton2afbfb62019-07-01 15:37:07 +00008055 SharedState->getLookupTable()->lookup(ReDC, Name);
Gabor Marton54058b52018-12-17 13:53:12 +00008056 return FoundDeclsTy(LookupResult.begin(), LookupResult.end());
8057 } else {
Gabor Martonaefcf512019-07-17 13:47:46 +00008058 DeclContext::lookup_result NoloadLookupResult = ReDC->noload_lookup(Name);
8059 FoundDeclsTy Result(NoloadLookupResult.begin(), NoloadLookupResult.end());
8060 // We must search by the slow case of localUncachedLookup because that is
8061 // working even if there is no LookupPtr for the DC. We could use
8062 // DC::buildLookup() to create the LookupPtr, but that would load external
8063 // decls again, we must avoid that case.
8064 // Also, even if we had the LookupPtr, we must find Decls which are not
8065 // in the LookupPtr, so we need the slow case.
8066 // These cases are handled in ASTImporterLookupTable, but we cannot use
8067 // that with LLDB since that traverses through the AST which initiates the
8068 // load of external decls again via DC::decls(). And again, we must avoid
8069 // loading external decls during the import.
8070 if (Result.empty())
8071 ReDC->localUncachedLookup(Name, Result);
Gabor Marton54058b52018-12-17 13:53:12 +00008072 return Result;
8073 }
8074}
8075
8076void ASTImporter::AddToLookupTable(Decl *ToD) {
Gabor Marton2afbfb62019-07-01 15:37:07 +00008077 SharedState->addDeclToLookup(ToD);
Gabor Marton54058b52018-12-17 13:53:12 +00008078}
8079
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00008080Expected<Decl *> ASTImporter::ImportImpl(Decl *FromD) {
8081 // Import the decl using ASTNodeImporter.
8082 ASTNodeImporter Importer(*this);
8083 return Importer.Visit(FromD);
8084}
8085
8086void ASTImporter::RegisterImportedDecl(Decl *FromD, Decl *ToD) {
8087 MapImported(FromD, ToD);
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00008088}
8089
Gabor Marton5ac6d492019-05-15 10:29:48 +00008090Expected<QualType> ASTImporter::Import(QualType FromT) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008091 if (FromT.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008092 return QualType{};
John McCall424cec92011-01-19 06:33:43 +00008093
Balazs Keri3b30d652018-10-19 13:32:20 +00008094 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00008095
8096 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00008097 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00008098 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00008099 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00008100 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00008101
Douglas Gregorf65bbb32010-02-08 15:18:58 +00008102 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00008103 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00008104 ExpectedType ToTOrErr = Importer.Visit(FromTy);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008105 if (!ToTOrErr)
8106 return ToTOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008107
Douglas Gregorf65bbb32010-02-08 15:18:58 +00008108 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00008109 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00008110
Balazs Keri3b30d652018-10-19 13:32:20 +00008111 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00008112}
8113
Gabor Marton5ac6d492019-05-15 10:29:48 +00008114Expected<TypeSourceInfo *> ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00008115 if (!FromTSI)
8116 return FromTSI;
8117
8118 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00008119 // on the type and a single location. Implement a real version of this.
Gabor Marton5ac6d492019-05-15 10:29:48 +00008120 ExpectedType TOrErr = Import(FromTSI->getType());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008121 if (!TOrErr)
8122 return TOrErr.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008123 ExpectedSLoc BeginLocOrErr = Import(FromTSI->getTypeLoc().getBeginLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008124 if (!BeginLocOrErr)
8125 return BeginLocOrErr.takeError();
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00008126
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008127 return ToContext.getTrivialTypeSourceInfo(*TOrErr, *BeginLocOrErr);
8128}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008129
Gabor Marton5ac6d492019-05-15 10:29:48 +00008130Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) {
Davide Italianofaee83d2018-11-28 19:15:23 +00008131 Attr *ToAttr = FromAttr->clone(ToContext);
Gabor Marton5ac6d492019-05-15 10:29:48 +00008132 if (auto ToRangeOrErr = Import(FromAttr->getRange()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008133 ToAttr->setRange(*ToRangeOrErr);
8134 else
8135 return ToRangeOrErr.takeError();
8136
Davide Italianofaee83d2018-11-28 19:15:23 +00008137 return ToAttr;
Balazs Kerideaf7ab2018-11-28 13:21:26 +00008138}
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00008139
Gabor Martonbe77a982018-12-12 11:22:55 +00008140Decl *ASTImporter::GetAlreadyImportedOrNull(const Decl *FromD) const {
8141 auto Pos = ImportedDecls.find(FromD);
8142 if (Pos != ImportedDecls.end())
8143 return Pos->second;
8144 else
Sean Callanan59721b32015-04-28 18:41:46 +00008145 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00008146}
8147
Gabor Marton458d1452019-02-14 13:07:03 +00008148TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) {
8149 auto FromDPos = ImportedFromDecls.find(ToD);
8150 if (FromDPos == ImportedFromDecls.end())
8151 return nullptr;
8152 return FromDPos->second->getTranslationUnitDecl();
8153}
8154
Gabor Marton5ac6d492019-05-15 10:29:48 +00008155Expected<Decl *> ASTImporter::Import(Decl *FromD) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008156 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00008157 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00008158
Gabor Marton1ad4b992019-07-01 14:19:53 +00008159 // Push FromD to the stack, and remove that when we return.
8160 ImportPath.push(FromD);
8161 auto ImportPathBuilder =
8162 llvm::make_scope_exit([this]() { ImportPath.pop(); });
Douglas Gregord451ea92011-07-29 23:31:30 +00008163
Gabor Marton303c98612019-06-25 08:00:51 +00008164 // Check whether there was a previous failed import.
8165 // If yes return the existing error.
8166 if (auto Error = getImportDeclErrorIfAny(FromD))
8167 return make_error<ImportError>(*Error);
8168
Gabor Marton26f72a92018-07-12 09:42:05 +00008169 // Check whether we've already imported this declaration.
8170 Decl *ToD = GetAlreadyImportedOrNull(FromD);
8171 if (ToD) {
Gabor Marton2afbfb62019-07-01 15:37:07 +00008172 // Already imported (possibly from another TU) and with an error.
8173 if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) {
8174 setImportDeclError(FromD, *Error);
8175 return make_error<ImportError>(*Error);
8176 }
8177
Gabor Marton26f72a92018-07-12 09:42:05 +00008178 // If FromD has some updated flags after last import, apply it
8179 updateFlags(FromD, ToD);
Gabor Marton1ad4b992019-07-01 14:19:53 +00008180 // If we encounter a cycle during an import then we save the relevant part
8181 // of the import path associated to the Decl.
8182 if (ImportPath.hasCycleAtBack())
8183 SavedImportPaths[FromD].push_back(ImportPath.copyCycleAtBack());
Douglas Gregord451ea92011-07-29 23:31:30 +00008184 return ToD;
8185 }
Gabor Marton26f72a92018-07-12 09:42:05 +00008186
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008187 // Import the declaration.
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00008188 ExpectedDecl ToDOrErr = ImportImpl(FromD);
Gabor Marton303c98612019-06-25 08:00:51 +00008189 if (!ToDOrErr) {
8190 // Failed to import.
8191
8192 auto Pos = ImportedDecls.find(FromD);
8193 if (Pos != ImportedDecls.end()) {
8194 // Import failed after the object was created.
8195 // Remove all references to it.
8196 auto *ToD = Pos->second;
8197 ImportedDecls.erase(Pos);
8198
8199 // ImportedDecls and ImportedFromDecls are not symmetric. It may happen
8200 // (e.g. with namespaces) that several decls from the 'from' context are
8201 // mapped to the same decl in the 'to' context. If we removed entries
8202 // from the LookupTable here then we may end up removing them multiple
8203 // times.
8204
8205 // The Lookuptable contains decls only which are in the 'to' context.
8206 // Remove from the Lookuptable only if it is *imported* into the 'to'
8207 // context (and do not remove it if it was added during the initial
8208 // traverse of the 'to' context).
8209 auto PosF = ImportedFromDecls.find(ToD);
8210 if (PosF != ImportedFromDecls.end()) {
Gabor Marton2afbfb62019-07-01 15:37:07 +00008211 SharedState->removeDeclFromLookup(ToD);
Gabor Marton303c98612019-06-25 08:00:51 +00008212 ImportedFromDecls.erase(PosF);
8213 }
8214
8215 // FIXME: AST may contain remaining references to the failed object.
Gabor Marton2afbfb62019-07-01 15:37:07 +00008216 // However, the ImportDeclErrors in the shared state contains all the
8217 // failed objects together with their error.
Gabor Marton303c98612019-06-25 08:00:51 +00008218 }
8219
Gabor Marton2afbfb62019-07-01 15:37:07 +00008220 // Error encountered for the first time.
8221 // After takeError the error is not usable any more in ToDOrErr.
Gabor Marton303c98612019-06-25 08:00:51 +00008222 // Get a copy of the error object (any more simple solution for this?).
8223 ImportError ErrOut;
8224 handleAllErrors(ToDOrErr.takeError(),
8225 [&ErrOut](const ImportError &E) { ErrOut = E; });
8226 setImportDeclError(FromD, ErrOut);
Gabor Marton2afbfb62019-07-01 15:37:07 +00008227 // Set the error for the mapped to Decl, which is in the "to" context.
8228 if (Pos != ImportedDecls.end())
8229 SharedState->setImportDeclError(Pos->second, ErrOut);
Gabor Marton1ad4b992019-07-01 14:19:53 +00008230
8231 // Set the error for all nodes which have been created before we
8232 // recognized the error.
8233 for (const auto &Path : SavedImportPaths[FromD])
Gabor Marton2afbfb62019-07-01 15:37:07 +00008234 for (Decl *FromDi : Path) {
8235 setImportDeclError(FromDi, ErrOut);
8236 //FIXME Should we remove these Decls from ImportedDecls?
8237 // Set the error for the mapped to Decl, which is in the "to" context.
8238 auto Ii = ImportedDecls.find(FromDi);
8239 if (Ii != ImportedDecls.end())
8240 SharedState->setImportDeclError(Ii->second, ErrOut);
8241 // FIXME Should we remove these Decls from the LookupTable,
8242 // and from ImportedFromDecls?
8243 }
Gabor Marton1ad4b992019-07-01 14:19:53 +00008244 SavedImportPaths[FromD].clear();
8245
Gabor Marton303c98612019-06-25 08:00:51 +00008246 // Do not return ToDOrErr, error was taken out of it.
8247 return make_error<ImportError>(ErrOut);
8248 }
8249
Balazs Keri3b30d652018-10-19 13:32:20 +00008250 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00008251
Gabor Marton303c98612019-06-25 08:00:51 +00008252 // FIXME: Handle the "already imported with error" case. We can get here
8253 // nullptr only if GetImportedOrCreateDecl returned nullptr (after a
8254 // previously failed create was requested).
8255 // Later GetImportedOrCreateDecl can be updated to return the error.
Gabor Marton7f8c4002019-03-19 13:34:10 +00008256 if (!ToD) {
Gabor Marton303c98612019-06-25 08:00:51 +00008257 auto Err = getImportDeclErrorIfAny(FromD);
8258 assert(Err);
8259 return make_error<ImportError>(*Err);
Gabor Marton7f8c4002019-03-19 13:34:10 +00008260 }
8261
Gabor Marton2afbfb62019-07-01 15:37:07 +00008262 // We could import from the current TU without error. But previously we
8263 // already had imported a Decl as `ToD` from another TU (with another
8264 // ASTImporter object) and with an error.
8265 if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) {
8266 setImportDeclError(FromD, *Error);
8267 return make_error<ImportError>(*Error);
8268 }
8269
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00008270 // Make sure that ImportImpl registered the imported decl.
8271 assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?");
Gabor Marton2afbfb62019-07-01 15:37:07 +00008272
Gabor Marton26f72a92018-07-12 09:42:05 +00008273 // Notify subclasses.
8274 Imported(FromD, ToD);
8275
Gabor Martonac3a5d62018-09-17 12:04:52 +00008276 updateFlags(FromD, ToD);
Gabor Marton1ad4b992019-07-01 14:19:53 +00008277 SavedImportPaths[FromD].clear();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008278 return ToDOrErr;
8279}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008280
Balazs Keri3b30d652018-10-19 13:32:20 +00008281Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008282 if (!FromDC)
8283 return FromDC;
8284
Gabor Marton5ac6d492019-05-15 10:29:48 +00008285 ExpectedDecl ToDCOrErr = Import(cast<Decl>(FromDC));
Balazs Keria1f6b102019-04-08 13:59:15 +00008286 if (!ToDCOrErr)
8287 return ToDCOrErr.takeError();
8288 auto *ToDC = cast<DeclContext>(*ToDCOrErr);
Craig Topper36250ad2014-05-12 05:36:57 +00008289
Fangrui Song6907ce22018-07-30 19:24:48 +00008290 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00008291 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008292 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
8293 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008294 if (ToRecord->isCompleteDefinition()) {
8295 // Do nothing.
8296 } else if (FromRecord->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008297 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
8298 FromRecord, ToRecord, ASTNodeImporter::IDK_Basic))
8299 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008300 } else {
8301 CompleteDecl(ToRecord);
8302 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008303 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
8304 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008305 if (ToEnum->isCompleteDefinition()) {
8306 // Do nothing.
8307 } else if (FromEnum->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008308 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
8309 FromEnum, ToEnum, ASTNodeImporter::IDK_Basic))
8310 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008311 } else {
8312 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00008313 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008314 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
8315 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008316 if (ToClass->getDefinition()) {
8317 // Do nothing.
8318 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008319 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
8320 FromDef, ToClass, ASTNodeImporter::IDK_Basic))
8321 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008322 } else {
8323 CompleteDecl(ToClass);
8324 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008325 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
8326 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008327 if (ToProto->getDefinition()) {
8328 // Do nothing.
8329 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008330 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
8331 FromDef, ToProto, ASTNodeImporter::IDK_Basic))
8332 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008333 } else {
8334 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00008335 }
Douglas Gregor95d82832012-01-24 18:36:04 +00008336 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008337
Douglas Gregor95d82832012-01-24 18:36:04 +00008338 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00008339}
8340
Gabor Marton5ac6d492019-05-15 10:29:48 +00008341Expected<Expr *> ASTImporter::Import(Expr *FromE) {
8342 if (ExpectedStmt ToSOrErr = Import(cast_or_null<Stmt>(FromE)))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008343 return cast_or_null<Expr>(*ToSOrErr);
8344 else
8345 return ToSOrErr.takeError();
Balazs Keri4a3d7582018-11-27 18:36:31 +00008346}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008347
Gabor Marton5ac6d492019-05-15 10:29:48 +00008348Expected<Stmt *> ASTImporter::Import(Stmt *FromS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008349 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00008350 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00008351
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008352 // Check whether we've already imported this statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00008353 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
8354 if (Pos != ImportedStmts.end())
8355 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00008356
Balazs Keri3b30d652018-10-19 13:32:20 +00008357 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00008358 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00008359 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008360 if (!ToSOrErr)
8361 return ToSOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00008362
Balazs Keri3b30d652018-10-19 13:32:20 +00008363 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00008364 auto *FromE = cast<Expr>(FromS);
8365 // Copy ExprBitfields, which may not be handled in Expr subclasses
8366 // constructors.
8367 ToE->setValueKind(FromE->getValueKind());
8368 ToE->setObjectKind(FromE->getObjectKind());
8369 ToE->setTypeDependent(FromE->isTypeDependent());
8370 ToE->setValueDependent(FromE->isValueDependent());
8371 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
8372 ToE->setContainsUnexpandedParameterPack(
8373 FromE->containsUnexpandedParameterPack());
8374 }
8375
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008376 // Record the imported statement object.
Balazs Keri3b30d652018-10-19 13:32:20 +00008377 ImportedStmts[FromS] = *ToSOrErr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008378 return ToSOrErr;
8379}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008380
Balazs Keri4a3d7582018-11-27 18:36:31 +00008381Expected<NestedNameSpecifier *>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008382ASTImporter::Import(NestedNameSpecifier *FromNNS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008383 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00008384 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00008385
Simon Pilgrim130df2c2019-07-15 13:00:43 +00008386 NestedNameSpecifier *Prefix = nullptr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008387 if (Error Err = importInto(Prefix, FromNNS->getPrefix()))
8388 return std::move(Err);
Douglas Gregor90ebf252011-04-27 16:48:40 +00008389
8390 switch (FromNNS->getKind()) {
8391 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008392 assert(FromNNS->getAsIdentifier() && "NNS should contain identifier.");
8393 return NestedNameSpecifier::Create(ToContext, Prefix,
8394 Import(FromNNS->getAsIdentifier()));
Douglas Gregor90ebf252011-04-27 16:48:40 +00008395
8396 case NestedNameSpecifier::Namespace:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008397 if (ExpectedDecl NSOrErr = Import(FromNNS->getAsNamespace())) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008398 return NestedNameSpecifier::Create(ToContext, Prefix,
8399 cast<NamespaceDecl>(*NSOrErr));
8400 } else
8401 return NSOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008402
8403 case NestedNameSpecifier::NamespaceAlias:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008404 if (ExpectedDecl NSADOrErr = Import(FromNNS->getAsNamespaceAlias()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008405 return NestedNameSpecifier::Create(ToContext, Prefix,
8406 cast<NamespaceAliasDecl>(*NSADOrErr));
8407 else
8408 return NSADOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008409
8410 case NestedNameSpecifier::Global:
8411 return NestedNameSpecifier::GlobalSpecifier(ToContext);
8412
Nikola Smiljanic67860242014-09-26 00:28:20 +00008413 case NestedNameSpecifier::Super:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008414 if (ExpectedDecl RDOrErr = Import(FromNNS->getAsRecordDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008415 return NestedNameSpecifier::SuperSpecifier(ToContext,
8416 cast<CXXRecordDecl>(*RDOrErr));
8417 else
8418 return RDOrErr.takeError();
Nikola Smiljanic67860242014-09-26 00:28:20 +00008419
Douglas Gregor90ebf252011-04-27 16:48:40 +00008420 case NestedNameSpecifier::TypeSpec:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008421 case NestedNameSpecifier::TypeSpecWithTemplate:
8422 if (Expected<QualType> TyOrErr =
Gabor Marton5ac6d492019-05-15 10:29:48 +00008423 Import(QualType(FromNNS->getAsType(), 0u))) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008424 bool TSTemplate =
8425 FromNNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate;
8426 return NestedNameSpecifier::Create(ToContext, Prefix, TSTemplate,
8427 TyOrErr->getTypePtr());
8428 } else {
8429 return TyOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008430 }
Douglas Gregor90ebf252011-04-27 16:48:40 +00008431 }
8432
8433 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00008434}
8435
Balazs Keri4a3d7582018-11-27 18:36:31 +00008436Expected<NestedNameSpecifierLoc>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008437ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008438 // Copied from NestedNameSpecifier mostly.
8439 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
8440 NestedNameSpecifierLoc NNS = FromNNS;
8441
8442 // Push each of the nested-name-specifiers's onto a stack for
8443 // serialization in reverse order.
8444 while (NNS) {
8445 NestedNames.push_back(NNS);
8446 NNS = NNS.getPrefix();
8447 }
8448
8449 NestedNameSpecifierLocBuilder Builder;
8450
8451 while (!NestedNames.empty()) {
8452 NNS = NestedNames.pop_back_val();
Simon Pilgrim4c146ab2019-05-18 11:33:27 +00008453 NestedNameSpecifier *Spec = nullptr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008454 if (Error Err = importInto(Spec, NNS.getNestedNameSpecifier()))
8455 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008456
8457 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008458
8459 SourceLocation ToLocalBeginLoc, ToLocalEndLoc;
8460 if (Kind != NestedNameSpecifier::Super) {
8461 if (Error Err = importInto(ToLocalBeginLoc, NNS.getLocalBeginLoc()))
8462 return std::move(Err);
8463
8464 if (Kind != NestedNameSpecifier::Global)
8465 if (Error Err = importInto(ToLocalEndLoc, NNS.getLocalEndLoc()))
8466 return std::move(Err);
8467 }
8468
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008469 switch (Kind) {
8470 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008471 Builder.Extend(getToContext(), Spec->getAsIdentifier(), ToLocalBeginLoc,
8472 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008473 break;
8474
8475 case NestedNameSpecifier::Namespace:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008476 Builder.Extend(getToContext(), Spec->getAsNamespace(), ToLocalBeginLoc,
8477 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008478 break;
8479
8480 case NestedNameSpecifier::NamespaceAlias:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008481 Builder.Extend(getToContext(), Spec->getAsNamespaceAlias(),
8482 ToLocalBeginLoc, ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008483 break;
8484
8485 case NestedNameSpecifier::TypeSpec:
8486 case NestedNameSpecifier::TypeSpecWithTemplate: {
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008487 SourceLocation ToTLoc;
8488 if (Error Err = importInto(ToTLoc, NNS.getTypeLoc().getBeginLoc()))
8489 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008490 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008491 QualType(Spec->getAsType(), 0), ToTLoc);
Balázs Kéria9f10eb2019-12-05 16:21:21 +01008492 if (Kind == NestedNameSpecifier::TypeSpecWithTemplate)
8493 // ToLocalBeginLoc is here the location of the 'template' keyword.
8494 Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(),
8495 ToLocalEndLoc);
8496 else
8497 // No location for 'template' keyword here.
8498 Builder.Extend(getToContext(), SourceLocation{}, TSI->getTypeLoc(),
8499 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008500 break;
8501 }
8502
8503 case NestedNameSpecifier::Global:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008504 Builder.MakeGlobal(getToContext(), ToLocalBeginLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008505 break;
8506
8507 case NestedNameSpecifier::Super: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008508 auto ToSourceRangeOrErr = Import(NNS.getSourceRange());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008509 if (!ToSourceRangeOrErr)
8510 return ToSourceRangeOrErr.takeError();
8511
8512 Builder.MakeSuper(getToContext(), Spec->getAsRecordDecl(),
8513 ToSourceRangeOrErr->getBegin(),
8514 ToSourceRangeOrErr->getEnd());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008515 }
8516 }
8517 }
8518
8519 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00008520}
8521
Gabor Marton5ac6d492019-05-15 10:29:48 +00008522Expected<TemplateName> ASTImporter::Import(TemplateName From) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00008523 switch (From.getKind()) {
8524 case TemplateName::Template:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008525 if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008526 return TemplateName(cast<TemplateDecl>(*ToTemplateOrErr));
8527 else
8528 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008529
Douglas Gregore2e50d332010-12-01 01:36:18 +00008530 case TemplateName::OverloadedTemplate: {
8531 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
8532 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008533 for (auto *I : *FromStorage) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008534 if (auto ToOrErr = Import(I))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008535 ToTemplates.addDecl(cast<NamedDecl>(*ToOrErr));
Douglas Gregore2e50d332010-12-01 01:36:18 +00008536 else
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008537 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008538 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008539 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008540 ToTemplates.end());
8541 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008542
Richard Smithb23c5e82019-05-09 03:31:27 +00008543 case TemplateName::AssumedTemplate: {
8544 AssumedTemplateStorage *FromStorage = From.getAsAssumedTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008545 auto DeclNameOrErr = Import(FromStorage->getDeclName());
Richard Smithb23c5e82019-05-09 03:31:27 +00008546 if (!DeclNameOrErr)
8547 return DeclNameOrErr.takeError();
8548 return ToContext.getAssumedTemplateName(*DeclNameOrErr);
8549 }
8550
Douglas Gregore2e50d332010-12-01 01:36:18 +00008551 case TemplateName::QualifiedTemplate: {
8552 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008553 auto QualifierOrErr = Import(QTN->getQualifier());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008554 if (!QualifierOrErr)
8555 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008556
Gabor Marton5ac6d492019-05-15 10:29:48 +00008557 if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008558 return ToContext.getQualifiedTemplateName(
8559 *QualifierOrErr, QTN->hasTemplateKeyword(),
8560 cast<TemplateDecl>(*ToTemplateOrErr));
8561 else
8562 return ToTemplateOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008563 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008564
Douglas Gregore2e50d332010-12-01 01:36:18 +00008565 case TemplateName::DependentTemplate: {
8566 DependentTemplateName *DTN = From.getAsDependentTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008567 auto QualifierOrErr = Import(DTN->getQualifier());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008568 if (!QualifierOrErr)
8569 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008570
Douglas Gregore2e50d332010-12-01 01:36:18 +00008571 if (DTN->isIdentifier()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008572 return ToContext.getDependentTemplateName(*QualifierOrErr,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008573 Import(DTN->getIdentifier()));
8574 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008575
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008576 return ToContext.getDependentTemplateName(*QualifierOrErr,
8577 DTN->getOperator());
Douglas Gregore2e50d332010-12-01 01:36:18 +00008578 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008579
8580 case TemplateName::SubstTemplateTemplateParm: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008581 SubstTemplateTemplateParmStorage *Subst =
8582 From.getAsSubstTemplateTemplateParm();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008583 ExpectedDecl ParamOrErr = Import(Subst->getParameter());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008584 if (!ParamOrErr)
8585 return ParamOrErr.takeError();
John McCalld9dfe3a2011-06-30 08:33:18 +00008586
Gabor Marton5ac6d492019-05-15 10:29:48 +00008587 auto ReplacementOrErr = Import(Subst->getReplacement());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008588 if (!ReplacementOrErr)
8589 return ReplacementOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008590
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008591 return ToContext.getSubstTemplateTemplateParm(
8592 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ReplacementOrErr);
John McCalld9dfe3a2011-06-30 08:33:18 +00008593 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008594
Douglas Gregor5590be02011-01-15 06:45:20 +00008595 case TemplateName::SubstTemplateTemplateParmPack: {
8596 SubstTemplateTemplateParmPackStorage *SubstPack
8597 = From.getAsSubstTemplateTemplateParmPack();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008598 ExpectedDecl ParamOrErr = Import(SubstPack->getParameterPack());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008599 if (!ParamOrErr)
8600 return ParamOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008601
Douglas Gregor5590be02011-01-15 06:45:20 +00008602 ASTNodeImporter Importer(*this);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008603 auto ArgPackOrErr =
8604 Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
8605 if (!ArgPackOrErr)
8606 return ArgPackOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008607
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008608 return ToContext.getSubstTemplateTemplateParmPack(
8609 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ArgPackOrErr);
Douglas Gregor5590be02011-01-15 06:45:20 +00008610 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008611 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008612
Douglas Gregore2e50d332010-12-01 01:36:18 +00008613 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008614}
8615
Gabor Marton5ac6d492019-05-15 10:29:48 +00008616Expected<SourceLocation> ASTImporter::Import(SourceLocation FromLoc) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008617 if (FromLoc.isInvalid())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008618 return SourceLocation{};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008619
Douglas Gregor811663e2010-02-10 00:15:17 +00008620 SourceManager &FromSM = FromContext.getSourceManager();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008621 bool IsBuiltin = FromSM.isWrittenInBuiltinFile(FromLoc);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008622
Douglas Gregor811663e2010-02-10 00:15:17 +00008623 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Gabor Marton5ac6d492019-05-15 10:29:48 +00008624 Expected<FileID> ToFileIDOrErr = Import(Decomposed.first, IsBuiltin);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008625 if (!ToFileIDOrErr)
8626 return ToFileIDOrErr.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008627 SourceManager &ToSM = ToContext.getSourceManager();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008628 return ToSM.getComposedLoc(*ToFileIDOrErr, Decomposed.second);
8629}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008630
Gabor Marton5ac6d492019-05-15 10:29:48 +00008631Expected<SourceRange> ASTImporter::Import(SourceRange FromRange) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008632 SourceLocation ToBegin, ToEnd;
8633 if (Error Err = importInto(ToBegin, FromRange.getBegin()))
8634 return std::move(Err);
8635 if (Error Err = importInto(ToEnd, FromRange.getEnd()))
8636 return std::move(Err);
8637
8638 return SourceRange(ToBegin, ToEnd);
Balazs Keri4a3d7582018-11-27 18:36:31 +00008639}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008640
Gabor Marton5ac6d492019-05-15 10:29:48 +00008641Expected<FileID> ASTImporter::Import(FileID FromID, bool IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008642 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008643 if (Pos != ImportedFileIDs.end())
8644 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008645
Douglas Gregor811663e2010-02-10 00:15:17 +00008646 SourceManager &FromSM = FromContext.getSourceManager();
8647 SourceManager &ToSM = ToContext.getSourceManager();
8648 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008649
8650 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008651 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008652 if (FromSLoc.isExpansion()) {
8653 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008654 ExpectedSLoc ToSpLoc = Import(FromEx.getSpellingLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008655 if (!ToSpLoc)
8656 return ToSpLoc.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008657 ExpectedSLoc ToExLocS = Import(FromEx.getExpansionLocStart());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008658 if (!ToExLocS)
8659 return ToExLocS.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008660 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8661 SourceLocation MLoc;
8662 if (FromEx.isMacroArgExpansion()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008663 MLoc = ToSM.createMacroArgExpansionLoc(*ToSpLoc, *ToExLocS, TokenLen);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008664 } else {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008665 if (ExpectedSLoc ToExLocE = Import(FromEx.getExpansionLocEnd()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008666 MLoc = ToSM.createExpansionLoc(*ToSpLoc, *ToExLocS, *ToExLocE, TokenLen,
8667 FromEx.isExpansionTokenRange());
8668 else
8669 return ToExLocE.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008670 }
8671 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008672 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008673 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008674
8675 if (!IsBuiltin) {
8676 // Include location of this file.
Gabor Marton5ac6d492019-05-15 10:29:48 +00008677 ExpectedSLoc ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008678 if (!ToIncludeLoc)
8679 return ToIncludeLoc.takeError();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008680
8681 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8682 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8683 // disk again
8684 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8685 // than mmap the files several times.
Harlan Haskins8d323d12019-08-01 21:31:56 +00008686 auto Entry =
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008687 ToFileManager.getFile(Cache->OrigEntry->getName());
8688 // FIXME: The filename may be a virtual name that does probably not
8689 // point to a valid file and we get no Entry here. In this case try with
8690 // the memory buffer below.
8691 if (Entry)
Harlan Haskins8d323d12019-08-01 21:31:56 +00008692 ToID = ToSM.createFileID(*Entry, *ToIncludeLoc,
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008693 FromSLoc.getFile().getFileCharacteristic());
8694 }
Balazs Keri9cf39df2019-02-27 16:31:48 +00008695 }
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008696
8697 if (ToID.isInvalid() || IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008698 // FIXME: We want to re-use the existing MemoryBuffer!
Balazs Keri9cf39df2019-02-27 16:31:48 +00008699 bool Invalid = true;
Duncan P. N. Exon Smithf5848192019-08-26 20:32:05 +00008700 const llvm::MemoryBuffer *FromBuf =
8701 Cache->getBuffer(FromContext.getDiagnostics(),
8702 FromSM.getFileManager(), SourceLocation{}, &Invalid);
Balazs Keri9cf39df2019-02-27 16:31:48 +00008703 if (!FromBuf || Invalid)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008704 // FIXME: Use a new error kind?
8705 return llvm::make_error<ImportError>(ImportError::Unknown);
Balazs Keri9cf39df2019-02-27 16:31:48 +00008706
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008707 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8708 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8709 FromBuf->getBufferIdentifier());
8710 ToID = ToSM.createFileID(std::move(ToBuf),
8711 FromSLoc.getFile().getFileCharacteristic());
8712 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008713 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008714
Balazs Keri9cf39df2019-02-27 16:31:48 +00008715 assert(ToID.isValid() && "Unexpected invalid fileID was created.");
8716
Sebastian Redl99219f12010-09-30 01:03:06 +00008717 ImportedFileIDs[FromID] = ToID;
Balazs Kerid22f8772019-07-24 10:16:37 +00008718
8719 if (FileIDImportHandler)
8720 FileIDImportHandler(ToID, FromID);
8721
Douglas Gregor811663e2010-02-10 00:15:17 +00008722 return ToID;
8723}
8724
Gabor Marton5ac6d492019-05-15 10:29:48 +00008725Expected<CXXCtorInitializer *> ASTImporter::Import(CXXCtorInitializer *From) {
8726 ExpectedExpr ToExprOrErr = Import(From->getInit());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008727 if (!ToExprOrErr)
8728 return ToExprOrErr.takeError();
8729
Gabor Marton5ac6d492019-05-15 10:29:48 +00008730 auto LParenLocOrErr = Import(From->getLParenLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008731 if (!LParenLocOrErr)
8732 return LParenLocOrErr.takeError();
8733
Gabor Marton5ac6d492019-05-15 10:29:48 +00008734 auto RParenLocOrErr = Import(From->getRParenLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008735 if (!RParenLocOrErr)
8736 return RParenLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008737
8738 if (From->isBaseInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008739 auto ToTInfoOrErr = Import(From->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008740 if (!ToTInfoOrErr)
8741 return ToTInfoOrErr.takeError();
8742
8743 SourceLocation EllipsisLoc;
8744 if (From->isPackExpansion())
8745 if (Error Err = importInto(EllipsisLoc, From->getEllipsisLoc()))
8746 return std::move(Err);
Davide Italianofaee83d2018-11-28 19:15:23 +00008747
8748 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008749 ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr,
8750 *ToExprOrErr, *RParenLocOrErr, EllipsisLoc);
Davide Italianofaee83d2018-11-28 19:15:23 +00008751 } else if (From->isMemberInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008752 ExpectedDecl ToFieldOrErr = Import(From->getMember());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008753 if (!ToFieldOrErr)
8754 return ToFieldOrErr.takeError();
8755
Gabor Marton5ac6d492019-05-15 10:29:48 +00008756 auto MemberLocOrErr = Import(From->getMemberLocation());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008757 if (!MemberLocOrErr)
8758 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008759
8760 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008761 ToContext, cast_or_null<FieldDecl>(*ToFieldOrErr), *MemberLocOrErr,
8762 *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008763 } else if (From->isIndirectMemberInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008764 ExpectedDecl ToIFieldOrErr = Import(From->getIndirectMember());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008765 if (!ToIFieldOrErr)
8766 return ToIFieldOrErr.takeError();
8767
Gabor Marton5ac6d492019-05-15 10:29:48 +00008768 auto MemberLocOrErr = Import(From->getMemberLocation());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008769 if (!MemberLocOrErr)
8770 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008771
8772 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008773 ToContext, cast_or_null<IndirectFieldDecl>(*ToIFieldOrErr),
8774 *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008775 } else if (From->isDelegatingInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008776 auto ToTInfoOrErr = Import(From->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008777 if (!ToTInfoOrErr)
8778 return ToTInfoOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008779
8780 return new (ToContext)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008781 CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr,
8782 *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008783 } else {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008784 // FIXME: assert?
8785 return make_error<ImportError>();
Davide Italianofaee83d2018-11-28 19:15:23 +00008786 }
Balazs Kerideaf7ab2018-11-28 13:21:26 +00008787}
Sean Callanandd2c1742016-05-16 20:48:03 +00008788
Balazs Keri4a3d7582018-11-27 18:36:31 +00008789Expected<CXXBaseSpecifier *>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008790ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00008791 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8792 if (Pos != ImportedCXXBaseSpecifiers.end())
8793 return Pos->second;
8794
Gabor Marton5ac6d492019-05-15 10:29:48 +00008795 Expected<SourceRange> ToSourceRange = Import(BaseSpec->getSourceRange());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008796 if (!ToSourceRange)
8797 return ToSourceRange.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008798 Expected<TypeSourceInfo *> ToTSI = Import(BaseSpec->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008799 if (!ToTSI)
8800 return ToTSI.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008801 ExpectedSLoc ToEllipsisLoc = Import(BaseSpec->getEllipsisLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008802 if (!ToEllipsisLoc)
8803 return ToEllipsisLoc.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00008804 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008805 *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8806 BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00008807 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8808 return Imported;
8809}
8810
Gabor Marton5ac6d492019-05-15 10:29:48 +00008811Error ASTImporter::ImportDefinition(Decl *From) {
8812 ExpectedDecl ToOrErr = Import(From);
8813 if (!ToOrErr)
8814 return ToOrErr.takeError();
8815 Decl *To = *ToOrErr;
Fangrui Song6907ce22018-07-30 19:24:48 +00008816
Don Hintonf170dff2019-03-19 06:14:14 +00008817 auto *FromDC = cast<DeclContext>(From);
8818 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008819
Don Hintonf170dff2019-03-19 06:14:14 +00008820 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
8821 if (!ToRecord->getDefinition()) {
8822 return Importer.ImportDefinition(
8823 cast<RecordDecl>(FromDC), ToRecord,
8824 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00008825 }
Douglas Gregor0a791672011-01-18 03:11:38 +00008826 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008827
Don Hintonf170dff2019-03-19 06:14:14 +00008828 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
8829 if (!ToEnum->getDefinition()) {
8830 return Importer.ImportDefinition(
8831 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
8832 }
8833 }
8834
8835 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
8836 if (!ToIFace->getDefinition()) {
8837 return Importer.ImportDefinition(
8838 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8839 ASTNodeImporter::IDK_Everything);
8840 }
8841 }
8842
8843 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
8844 if (!ToProto->getDefinition()) {
8845 return Importer.ImportDefinition(
8846 cast<ObjCProtocolDecl>(FromDC), ToProto,
8847 ASTNodeImporter::IDK_Everything);
8848 }
8849 }
8850
8851 return Importer.ImportDeclContext(FromDC, true);
Balazs Keri3b30d652018-10-19 13:32:20 +00008852}
8853
Gabor Marton5ac6d492019-05-15 10:29:48 +00008854Expected<DeclarationName> ASTImporter::Import(DeclarationName FromName) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008855 if (!FromName)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008856 return DeclarationName{};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008857
8858 switch (FromName.getNameKind()) {
8859 case DeclarationName::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008860 return DeclarationName(Import(FromName.getAsIdentifierInfo()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008861
8862 case DeclarationName::ObjCZeroArgSelector:
8863 case DeclarationName::ObjCOneArgSelector:
8864 case DeclarationName::ObjCMultiArgSelector:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008865 if (auto ToSelOrErr = Import(FromName.getObjCSelector()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008866 return DeclarationName(*ToSelOrErr);
8867 else
8868 return ToSelOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008869
8870 case DeclarationName::CXXConstructorName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008871 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008872 return ToContext.DeclarationNames.getCXXConstructorName(
8873 ToContext.getCanonicalType(*ToTyOrErr));
8874 else
8875 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008876 }
8877
8878 case DeclarationName::CXXDestructorName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008879 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008880 return ToContext.DeclarationNames.getCXXDestructorName(
8881 ToContext.getCanonicalType(*ToTyOrErr));
8882 else
8883 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008884 }
8885
Richard Smith35845152017-02-07 01:37:30 +00008886 case DeclarationName::CXXDeductionGuideName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008887 if (auto ToTemplateOrErr = Import(FromName.getCXXDeductionGuideTemplate()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008888 return ToContext.DeclarationNames.getCXXDeductionGuideName(
8889 cast<TemplateDecl>(*ToTemplateOrErr));
8890 else
8891 return ToTemplateOrErr.takeError();
Richard Smith35845152017-02-07 01:37:30 +00008892 }
8893
Douglas Gregor96e578d2010-02-05 17:54:41 +00008894 case DeclarationName::CXXConversionFunctionName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008895 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008896 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8897 ToContext.getCanonicalType(*ToTyOrErr));
8898 else
8899 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008900 }
8901
8902 case DeclarationName::CXXOperatorName:
8903 return ToContext.DeclarationNames.getCXXOperatorName(
8904 FromName.getCXXOverloadedOperator());
8905
8906 case DeclarationName::CXXLiteralOperatorName:
8907 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008908 Import(FromName.getCXXLiteralIdentifier()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008909
8910 case DeclarationName::CXXUsingDirective:
8911 // FIXME: STATICS!
8912 return DeclarationName::getUsingDirectiveName();
8913 }
8914
David Blaikiee4d798f2012-01-20 21:50:17 +00008915 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008916}
8917
Douglas Gregore2e50d332010-12-01 01:36:18 +00008918IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008919 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008920 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008921
Sean Callananf94ef1d2016-05-14 06:11:19 +00008922 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8923
8924 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8925 ToId->setBuiltinID(FromId->getBuiltinID());
8926
8927 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008928}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008929
Gabor Marton5ac6d492019-05-15 10:29:48 +00008930Expected<Selector> ASTImporter::Import(Selector FromSel) {
Douglas Gregor43f54792010-02-17 02:12:47 +00008931 if (FromSel.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008932 return Selector{};
Douglas Gregor43f54792010-02-17 02:12:47 +00008933
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008934 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008935 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8936 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8937 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8938 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8939}
8940
Gabor Martonf035b752019-08-27 11:36:10 +00008941Expected<DeclarationName> ASTImporter::HandleNameConflict(DeclarationName Name,
8942 DeclContext *DC,
8943 unsigned IDNS,
8944 NamedDecl **Decls,
8945 unsigned NumDecls) {
8946 if (ODRHandling == ODRHandlingType::Conservative)
8947 // Report error at any name conflict.
8948 return make_error<ImportError>(ImportError::NameConflict);
8949 else
8950 // Allow to create the new Decl with the same name.
8951 return Name;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008952}
8953
8954DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008955 if (LastDiagFromFrom)
8956 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8957 FromContext.getDiagnostics());
8958 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008959 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008960}
8961
8962DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008963 if (!LastDiagFromFrom)
8964 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8965 ToContext.getDiagnostics());
8966 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008967 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008968}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008969
Douglas Gregor2e15c842012-02-01 21:00:38 +00008970void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008971 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008972 if (!ID->getDefinition())
8973 ID->startDefinition();
8974 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008975 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008976 if (!PD->getDefinition())
8977 PD->startDefinition();
8978 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008979 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008980 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8981 TD->startDefinition();
8982 TD->setCompleteDefinition(true);
8983 }
8984 }
8985 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008986 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008987 }
8988}
8989
Gabor Marton26f72a92018-07-12 09:42:05 +00008990Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8991 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8992 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8993 "Try to import an already imported Decl");
8994 if (Pos != ImportedDecls.end())
8995 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008996 ImportedDecls[From] = To;
Gabor Marton458d1452019-02-14 13:07:03 +00008997 // This mapping should be maintained only in this function. Therefore do not
8998 // check for additional consistency.
8999 ImportedFromDecls[To] = From;
Gabor Marton303c98612019-06-25 08:00:51 +00009000 AddToLookupTable(To);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00009001 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00009002}
Douglas Gregorb4964f72010-02-15 23:54:17 +00009003
Gabor Marton303c98612019-06-25 08:00:51 +00009004llvm::Optional<ImportError>
9005ASTImporter::getImportDeclErrorIfAny(Decl *FromD) const {
9006 auto Pos = ImportDeclErrors.find(FromD);
9007 if (Pos != ImportDeclErrors.end())
9008 return Pos->second;
9009 else
9010 return Optional<ImportError>();
9011}
9012
9013void ASTImporter::setImportDeclError(Decl *From, ImportError Error) {
Gabor Marton1ad4b992019-07-01 14:19:53 +00009014 auto InsertRes = ImportDeclErrors.insert({From, Error});
Benjamin Kramer4f769362019-07-01 14:33:26 +00009015 (void)InsertRes;
Gabor Marton1ad4b992019-07-01 14:19:53 +00009016 // Either we set the error for the first time, or we already had set one and
9017 // now we want to set the same error.
9018 assert(InsertRes.second || InsertRes.first->second.Error == Error.Error);
Gabor Marton303c98612019-06-25 08:00:51 +00009019}
9020
Douglas Gregordd6006f2012-07-17 21:16:27 +00009021bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
9022 bool Complain) {
Balazs Keria1f6b102019-04-08 13:59:15 +00009023 llvm::DenseMap<const Type *, const Type *>::iterator Pos =
9024 ImportedTypes.find(From.getTypePtr());
9025 if (Pos != ImportedTypes.end()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00009026 if (ExpectedType ToFromOrErr = Import(From)) {
Balazs Keria1f6b102019-04-08 13:59:15 +00009027 if (ToContext.hasSameType(*ToFromOrErr, To))
9028 return true;
9029 } else {
9030 llvm::consumeError(ToFromOrErr.takeError());
9031 }
9032 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00009033
Douglas Gregordd6006f2012-07-17 21:16:27 +00009034 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00009035 getStructuralEquivalenceKind(*this), false,
9036 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00009037 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00009038}