blob: b75a689ec275972cdd860ee3cf424518298af6c4 [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);
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001737 if (!ImportedOrErr) {
1738 if (AccumulateChildErrors)
1739 ChildErrors =
1740 joinErrors(std::move(ChildErrors), ImportedOrErr.takeError());
1741 else
1742 consumeError(ImportedOrErr.takeError());
1743 }
Douglas Gregor0a791672011-01-18 03:11:38 +00001744 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001745
Gabor Marton48b16e12019-07-25 09:07:17 +00001746 // We reorder declarations in RecordDecls because they may have another order
1747 // in the "to" context than they have in the "from" context. This may happen
1748 // e.g when we import a class like this:
1749 // struct declToImport {
1750 // int a = c + b;
1751 // int b = 1;
1752 // int c = 2;
1753 // };
1754 // During the import of `a` we import first the dependencies in sequence,
1755 // thus the order would be `c`, `b`, `a`. We will get the normal order by
1756 // first removing the already imported members and then adding them in the
1757 // order as they apper in the "from" context.
1758 //
1759 // Keeping field order is vital because it determines structure layout.
1760 //
1761 // Here and below, we cannot call field_begin() method and its callers on
1762 // ToDC if it has an external storage. Calling field_begin() will
1763 // automatically load all the fields by calling
1764 // LoadFieldsFromExternalStorage(). LoadFieldsFromExternalStorage() would
1765 // call ASTImporter::Import(). This is because the ExternalASTSource
1766 // interface in LLDB is implemented by the means of the ASTImporter. However,
1767 // calling an import at this point would result in an uncontrolled import, we
1768 // must avoid that.
1769 const auto *FromRD = dyn_cast<RecordDecl>(FromDC);
1770 if (!FromRD)
1771 return ChildErrors;
1772
1773 auto ToDCOrErr = Importer.ImportContext(FromDC);
1774 if (!ToDCOrErr) {
1775 consumeError(std::move(ChildErrors));
1776 return ToDCOrErr.takeError();
1777 }
1778
1779 DeclContext *ToDC = *ToDCOrErr;
1780 // Remove all declarations, which may be in wrong order in the
1781 // lexical DeclContext and then add them in the proper order.
1782 for (auto *D : FromRD->decls()) {
Balazs Keri6e086692019-09-02 07:17:01 +00001783 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<FriendDecl>(D)) {
Gabor Marton48b16e12019-07-25 09:07:17 +00001784 assert(D && "DC contains a null decl");
1785 Decl *ToD = Importer.GetAlreadyImportedOrNull(D);
1786 // Remove only the decls which we successfully imported.
1787 if (ToD) {
1788 assert(ToDC == ToD->getLexicalDeclContext() && ToDC->containsDecl(ToD));
1789 // Remove the decl from its wrong place in the linked list.
1790 ToDC->removeDecl(ToD);
1791 // Add the decl to the end of the linked list.
1792 // This time it will be at the proper place because the enclosing for
1793 // loop iterates in the original (good) order of the decls.
1794 ToDC->addDeclInternal(ToD);
1795 }
1796 }
1797 }
1798
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001799 return ChildErrors;
Douglas Gregor968d6332010-02-21 18:24:45 +00001800}
1801
Balazs Keri3b30d652018-10-19 13:32:20 +00001802Error ASTNodeImporter::ImportDeclContext(
1803 Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) {
1804 auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext());
1805 if (!ToDCOrErr)
1806 return ToDCOrErr.takeError();
1807 ToDC = *ToDCOrErr;
1808
1809 if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) {
1810 auto ToLexicalDCOrErr = Importer.ImportContext(
1811 FromD->getLexicalDeclContext());
1812 if (!ToLexicalDCOrErr)
1813 return ToLexicalDCOrErr.takeError();
1814 ToLexicalDC = *ToLexicalDCOrErr;
1815 } else
1816 ToLexicalDC = ToDC;
1817
1818 return Error::success();
1819}
1820
1821Error ASTNodeImporter::ImportImplicitMethods(
Balazs Keri1d20cc22018-07-16 12:16:39 +00001822 const CXXRecordDecl *From, CXXRecordDecl *To) {
1823 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1824 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001825
Balazs Keri1d20cc22018-07-16 12:16:39 +00001826 for (CXXMethodDecl *FromM : From->methods())
Balazs Keri3b30d652018-10-19 13:32:20 +00001827 if (FromM->isImplicit()) {
1828 Expected<CXXMethodDecl *> ToMOrErr = import(FromM);
1829 if (!ToMOrErr)
1830 return ToMOrErr.takeError();
1831 }
1832
1833 return Error::success();
Balazs Keri1d20cc22018-07-16 12:16:39 +00001834}
1835
Balazs Keri3b30d652018-10-19 13:32:20 +00001836static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1837 ASTImporter &Importer) {
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001838 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00001839 if (ExpectedDecl ToTypedefOrErr = Importer.Import(FromTypedef))
Balazs Keri57949eb2019-03-25 09:16:39 +00001840 To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr));
1841 else
1842 return ToTypedefOrErr.takeError();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001843 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001844 return Error::success();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001845}
1846
Balazs Keri3b30d652018-10-19 13:32:20 +00001847Error ASTNodeImporter::ImportDefinition(
1848 RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) {
Gabor Martonaefcf512019-07-17 13:47:46 +00001849 auto DefinitionCompleter = [To]() {
1850 // There are cases in LLDB when we first import a class without its
1851 // members. The class will have DefinitionData, but no members. Then,
1852 // importDefinition is called from LLDB, which tries to get the members, so
1853 // when we get here, the class already has the DefinitionData set, so we
1854 // must unset the CompleteDefinition here to be able to complete again the
1855 // definition.
1856 To->setCompleteDefinition(false);
1857 To->completeDefinition();
1858 };
1859
Douglas Gregor95d82832012-01-24 18:36:04 +00001860 if (To->getDefinition() || To->isBeingDefined()) {
Gabor Martone73805f2019-07-08 12:49:13 +00001861 if (Kind == IDK_Everything ||
1862 // In case of lambdas, the class already has a definition ptr set, but
1863 // the contained decls are not imported yet. Also, isBeingDefined was
1864 // set in CXXRecordDecl::CreateLambda. We must import the contained
1865 // decls here and finish the definition.
1866 (To->isLambda() && shouldForceImportDeclContext(Kind))) {
1867 Error Result = ImportDeclContext(From, /*ForceImport=*/true);
1868 // Finish the definition of the lambda, set isBeingDefined to false.
1869 if (To->isLambda())
Gabor Martonaefcf512019-07-17 13:47:46 +00001870 DefinitionCompleter();
Gabor Martone73805f2019-07-08 12:49:13 +00001871 return Result;
1872 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001873
Balazs Keri3b30d652018-10-19 13:32:20 +00001874 return Error::success();
Douglas Gregor95d82832012-01-24 18:36:04 +00001875 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001876
Douglas Gregore2e50d332010-12-01 01:36:18 +00001877 To->startDefinition();
Gabor Marton17c3eaf2019-07-01 12:44:39 +00001878 // Complete the definition even if error is returned.
1879 // The RecordDecl may be already part of the AST so it is better to
1880 // have it in complete state even if something is wrong with it.
Gabor Martonaefcf512019-07-17 13:47:46 +00001881 auto DefinitionCompleterScopeExit =
1882 llvm::make_scope_exit(DefinitionCompleter);
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001883
Balazs Keri3b30d652018-10-19 13:32:20 +00001884 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1885 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001886
Douglas Gregore2e50d332010-12-01 01:36:18 +00001887 // Add base classes.
Gabor Marton17d39672018-11-26 15:54:08 +00001888 auto *ToCXX = dyn_cast<CXXRecordDecl>(To);
1889 auto *FromCXX = dyn_cast<CXXRecordDecl>(From);
1890 if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) {
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001891
1892 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1893 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
Richard Smith91aeacc2019-10-11 00:29:04 +00001894
1895 #define FIELD(Name, Width, Merge) \
1896 ToData.Name = FromData.Name;
1897 #include "clang/AST/CXXRecordDeclDefinitionBits.def"
Richard Smith561fb152012-02-25 07:33:38 +00001898
Shafik Yaghmour16b90732019-04-26 18:51:28 +00001899 // Copy over the data stored in RecordDeclBits
1900 ToCXX->setArgPassingRestrictions(FromCXX->getArgPassingRestrictions());
1901
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001902 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001903 for (const auto &Base1 : FromCXX->bases()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001904 ExpectedType TyOrErr = import(Base1.getType());
1905 if (!TyOrErr)
1906 return TyOrErr.takeError();
Douglas Gregor752a5952011-01-03 22:36:02 +00001907
1908 SourceLocation EllipsisLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00001909 if (Base1.isPackExpansion()) {
1910 if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc()))
1911 EllipsisLoc = *LocOrErr;
1912 else
1913 return LocOrErr.takeError();
1914 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001915
1916 // Ensure that we have a definition for the base.
Balazs Keri3b30d652018-10-19 13:32:20 +00001917 if (Error Err =
1918 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()))
1919 return Err;
1920
1921 auto RangeOrErr = import(Base1.getSourceRange());
1922 if (!RangeOrErr)
1923 return RangeOrErr.takeError();
1924
1925 auto TSIOrErr = import(Base1.getTypeSourceInfo());
1926 if (!TSIOrErr)
1927 return TSIOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001928
Douglas Gregore2e50d332010-12-01 01:36:18 +00001929 Bases.push_back(
Balazs Keri3b30d652018-10-19 13:32:20 +00001930 new (Importer.getToContext()) CXXBaseSpecifier(
1931 *RangeOrErr,
1932 Base1.isVirtual(),
1933 Base1.isBaseOfClass(),
1934 Base1.getAccessSpecifierAsWritten(),
1935 *TSIOrErr,
1936 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001937 }
1938 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001939 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001940 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001941
Douglas Gregor2e15c842012-02-01 21:00:38 +00001942 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001943 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1944 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001945
Balazs Keri3b30d652018-10-19 13:32:20 +00001946 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001947}
1948
Balazs Keri3b30d652018-10-19 13:32:20 +00001949Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) {
Sean Callanan59721b32015-04-28 18:41:46 +00001950 if (To->getAnyInitializer())
Balazs Keri3b30d652018-10-19 13:32:20 +00001951 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001952
Gabor Martonac3a5d62018-09-17 12:04:52 +00001953 Expr *FromInit = From->getInit();
1954 if (!FromInit)
Balazs Keri3b30d652018-10-19 13:32:20 +00001955 return Error::success();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001956
Balazs Keri3b30d652018-10-19 13:32:20 +00001957 ExpectedExpr ToInitOrErr = import(FromInit);
1958 if (!ToInitOrErr)
1959 return ToInitOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001960
Balazs Keri3b30d652018-10-19 13:32:20 +00001961 To->setInit(*ToInitOrErr);
Gabor Martonac3a5d62018-09-17 12:04:52 +00001962 if (From->isInitKnownICE()) {
1963 EvaluatedStmt *Eval = To->ensureEvaluatedStmt();
1964 Eval->CheckedICE = true;
1965 Eval->IsICE = From->isInitICE();
1966 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001967
1968 // FIXME: Other bits to merge?
Balazs Keri3b30d652018-10-19 13:32:20 +00001969 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001970}
1971
Balazs Keri3b30d652018-10-19 13:32:20 +00001972Error ASTNodeImporter::ImportDefinition(
1973 EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00001974 if (To->getDefinition() || To->isBeingDefined()) {
1975 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001976 return ImportDeclContext(From, /*ForceImport=*/true);
1977 return Error::success();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001978 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001979
Douglas Gregord451ea92011-07-29 23:31:30 +00001980 To->startDefinition();
1981
Balazs Keri3b30d652018-10-19 13:32:20 +00001982 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1983 return Err;
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001984
Balazs Keri3b30d652018-10-19 13:32:20 +00001985 ExpectedType ToTypeOrErr =
1986 import(Importer.getFromContext().getTypeDeclType(From));
1987 if (!ToTypeOrErr)
1988 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001989
Balazs Keri3b30d652018-10-19 13:32:20 +00001990 ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType());
1991 if (!ToPromotionTypeOrErr)
1992 return ToPromotionTypeOrErr.takeError();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001993
1994 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001995 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1996 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001997
Douglas Gregord451ea92011-07-29 23:31:30 +00001998 // FIXME: we might need to merge the number of positive or negative bits
1999 // if the enumerator lists don't match.
Balazs Keri3b30d652018-10-19 13:32:20 +00002000 To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr,
Douglas Gregord451ea92011-07-29 23:31:30 +00002001 From->getNumPositiveBits(),
2002 From->getNumNegativeBits());
Balazs Keri3b30d652018-10-19 13:32:20 +00002003 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00002004}
2005
Balazs Keri3b30d652018-10-19 13:32:20 +00002006Error ASTNodeImporter::ImportTemplateArguments(
2007 const TemplateArgument *FromArgs, unsigned NumFromArgs,
2008 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002009 for (unsigned I = 0; I != NumFromArgs; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002010 if (auto ToOrErr = import(FromArgs[I]))
2011 ToArgs.push_back(*ToOrErr);
2012 else
2013 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00002014 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002015
Balazs Keri3b30d652018-10-19 13:32:20 +00002016 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00002017}
2018
Balazs Keri3b30d652018-10-19 13:32:20 +00002019// FIXME: Do not forget to remove this and use only 'import'.
2020Expected<TemplateArgument>
2021ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2022 return import(From);
2023}
2024
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002025template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +00002026Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002027 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
2028 for (const auto &FromLoc : Container) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002029 if (auto ToLocOrErr = import(FromLoc))
2030 ToTAInfo.addArgument(*ToLocOrErr);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002031 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002032 return ToLocOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002033 }
Balazs Keri3b30d652018-10-19 13:32:20 +00002034 return Error::success();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002035}
2036
Gabor Marton26f72a92018-07-12 09:42:05 +00002037static StructuralEquivalenceKind
2038getStructuralEquivalenceKind(const ASTImporter &Importer) {
2039 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
2040 : StructuralEquivalenceKind::Default;
2041}
2042
Gabor Marton950fb572018-07-17 12:39:27 +00002043bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
2044 StructuralEquivalenceContext Ctx(
2045 Importer.getFromContext(), Importer.getToContext(),
2046 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2047 false, Complain);
2048 return Ctx.IsEquivalent(From, To);
2049}
2050
2051bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002052 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002053 // Eliminate a potential failure point where we attempt to re-import
2054 // something we're trying to import while completing ToRecord.
2055 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2056 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002057 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00002058 if (ToOriginRecord)
2059 ToRecord = ToOriginRecord;
2060 }
2061
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002062 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002063 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002064 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002065 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002066 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00002067 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002068}
2069
Larisse Voufo39a1e502013-08-06 01:03:05 +00002070bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2071 bool Complain) {
2072 StructuralEquivalenceContext Ctx(
2073 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002074 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2075 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00002076 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002077}
2078
Douglas Gregor98c10182010-02-12 22:17:39 +00002079bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00002080 // Eliminate a potential failure point where we attempt to re-import
Raphael Isemannfa26c202019-04-09 14:18:23 +00002081 // something we're trying to import while completing ToEnum.
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00002082 if (Decl *ToOrigin = Importer.GetOriginalDecl(ToEnum))
2083 if (auto *ToOriginEnum = dyn_cast<EnumDecl>(ToOrigin))
2084 ToEnum = ToOriginEnum;
2085
Gabor Marton26f72a92018-07-12 09:42:05 +00002086 StructuralEquivalenceContext Ctx(
2087 Importer.getFromContext(), Importer.getToContext(),
2088 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002089 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002090}
2091
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002092bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
2093 FunctionTemplateDecl *To) {
2094 StructuralEquivalenceContext Ctx(
2095 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002096 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2097 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002098 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00002099}
2100
Balazs Keric7797c42018-07-11 09:37:24 +00002101bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
2102 StructuralEquivalenceContext Ctx(
2103 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002104 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
2105 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00002106 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00002107}
2108
Douglas Gregor91155082012-11-14 22:29:20 +00002109bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002110 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00002111 const llvm::APSInt &FromVal = FromEC->getInitVal();
2112 const llvm::APSInt &ToVal = ToEC->getInitVal();
2113
2114 return FromVal.isSigned() == ToVal.isSigned() &&
2115 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2116 FromVal == ToVal;
2117}
2118
2119bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002120 ClassTemplateDecl *To) {
2121 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2122 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002123 Importer.getNonEquivalentDecls(),
2124 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002125 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00002126}
2127
Larisse Voufo39a1e502013-08-06 01:03:05 +00002128bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2129 VarTemplateDecl *To) {
2130 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2131 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002132 Importer.getNonEquivalentDecls(),
2133 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002134 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002135}
2136
Balazs Keri3b30d652018-10-19 13:32:20 +00002137ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002138 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002139 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00002140 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +00002141}
2142
Balazs Keri3b30d652018-10-19 13:32:20 +00002143ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) {
2144 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2145 << D->getDeclKindName();
2146 return make_error<ImportError>(ImportError::UnsupportedConstruct);
2147}
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002148
Balazs Keri3b30d652018-10-19 13:32:20 +00002149ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
2150 // Import the context of this declaration.
2151 DeclContext *DC, *LexicalDC;
2152 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
2153 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002154
2155 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002156 ExpectedSLoc LocOrErr = import(D->getLocation());
2157 if (!LocOrErr)
2158 return LocOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002159
Gabor Marton26f72a92018-07-12 09:42:05 +00002160 EmptyDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002161 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002162 return ToD;
2163
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002164 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002165 LexicalDC->addDeclInternal(ToD);
2166 return ToD;
2167}
2168
Balazs Keri3b30d652018-10-19 13:32:20 +00002169ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002170 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00002171 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00002172
Gabor Marton26f72a92018-07-12 09:42:05 +00002173 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002174
Sean Callanan65198272011-11-17 23:20:56 +00002175 return ToD;
2176}
2177
Balazs Keri3b30d652018-10-19 13:32:20 +00002178ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2179 ExpectedSLoc LocOrErr = import(D->getLocation());
2180 if (!LocOrErr)
2181 return LocOrErr.takeError();
2182 auto ColonLocOrErr = import(D->getColonLoc());
2183 if (!ColonLocOrErr)
2184 return ColonLocOrErr.takeError();
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002185
2186 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002187 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2188 if (!DCOrErr)
2189 return DCOrErr.takeError();
2190 DeclContext *DC = *DCOrErr;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002191
Gabor Marton26f72a92018-07-12 09:42:05 +00002192 AccessSpecDecl *ToD;
2193 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002194 DC, *LocOrErr, *ColonLocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002195 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002196
2197 // Lexical DeclContext and Semantic DeclContext
2198 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00002199 ToD->setLexicalDeclContext(DC);
2200 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002201
Gabor Marton26f72a92018-07-12 09:42:05 +00002202 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002203}
2204
Balazs Keri3b30d652018-10-19 13:32:20 +00002205ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2206 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2207 if (!DCOrErr)
2208 return DCOrErr.takeError();
2209 DeclContext *DC = *DCOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002210 DeclContext *LexicalDC = DC;
2211
Balazs Keri3b30d652018-10-19 13:32:20 +00002212 SourceLocation ToLocation, ToRParenLoc;
2213 Expr *ToAssertExpr;
2214 StringLiteral *ToMessage;
2215 if (auto Imp = importSeq(
2216 D->getLocation(), D->getAssertExpr(), D->getMessage(), D->getRParenLoc()))
2217 std::tie(ToLocation, ToAssertExpr, ToMessage, ToRParenLoc) = *Imp;
2218 else
2219 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002220
Gabor Marton26f72a92018-07-12 09:42:05 +00002221 StaticAssertDecl *ToD;
2222 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002223 ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage,
2224 ToRParenLoc, D->isFailed()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002225 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002226
2227 ToD->setLexicalDeclContext(LexicalDC);
2228 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002229 return ToD;
2230}
2231
Balazs Keri3b30d652018-10-19 13:32:20 +00002232ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002233 // Import the major distinguishing characteristics of this namespace.
2234 DeclContext *DC, *LexicalDC;
2235 DeclarationName Name;
2236 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002237 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002238 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2239 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002240 if (ToD)
2241 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002242
2243 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002244 if (!Name) {
2245 // This is an anonymous namespace. Adopt an existing anonymous
2246 // namespace if we can.
2247 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002248 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002249 MergeWithNamespace = TU->getAnonymousNamespace();
2250 else
2251 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2252 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002253 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002254 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002255 for (auto *FoundDecl : FoundDecls) {
2256 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002257 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002258
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002259 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002260 MergeWithNamespace = FoundNS;
2261 ConflictingDecls.clear();
2262 break;
2263 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002264
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002265 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002266 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002267
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002268 if (!ConflictingDecls.empty()) {
Gabor Martonf035b752019-08-27 11:36:10 +00002269 ExpectedName NameOrErr = Importer.HandleNameConflict(
2270 Name, DC, Decl::IDNS_Namespace, ConflictingDecls.data(),
2271 ConflictingDecls.size());
2272 if (NameOrErr)
2273 Name = NameOrErr.get();
2274 else
2275 return NameOrErr.takeError();
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002276 }
2277 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002278
Balazs Keri3b30d652018-10-19 13:32:20 +00002279 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2280 if (!BeginLocOrErr)
2281 return BeginLocOrErr.takeError();
Balázs Kéria9f10eb2019-12-05 16:21:21 +01002282 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
2283 if (!RBraceLocOrErr)
2284 return RBraceLocOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +00002285
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002286 // Create the "to" namespace, if needed.
2287 NamespaceDecl *ToNamespace = MergeWithNamespace;
2288 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002289 if (GetImportedOrCreateDecl(
2290 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002291 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002292 /*PrevDecl=*/nullptr))
2293 return ToNamespace;
Balázs Kéria9f10eb2019-12-05 16:21:21 +01002294 ToNamespace->setRBraceLoc(*RBraceLocOrErr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002295 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002296 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002297
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002298 // If this is an anonymous namespace, register it as the anonymous
2299 // namespace within its context.
2300 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002301 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002302 TU->setAnonymousNamespace(ToNamespace);
2303 else
2304 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2305 }
2306 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002307 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002308
Balazs Keri3b30d652018-10-19 13:32:20 +00002309 if (Error Err = ImportDeclContext(D))
2310 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00002311
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002312 return ToNamespace;
2313}
2314
Balazs Keri3b30d652018-10-19 13:32:20 +00002315ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002316 // Import the major distinguishing characteristics of this namespace.
2317 DeclContext *DC, *LexicalDC;
2318 DeclarationName Name;
2319 SourceLocation Loc;
2320 NamedDecl *LookupD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002321 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
2322 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002323 if (LookupD)
2324 return LookupD;
2325
2326 // NOTE: No conflict resolution is done for namespace aliases now.
2327
Balazs Keri3b30d652018-10-19 13:32:20 +00002328 SourceLocation ToNamespaceLoc, ToAliasLoc, ToTargetNameLoc;
2329 NestedNameSpecifierLoc ToQualifierLoc;
2330 NamespaceDecl *ToNamespace;
2331 if (auto Imp = importSeq(
2332 D->getNamespaceLoc(), D->getAliasLoc(), D->getQualifierLoc(),
2333 D->getTargetNameLoc(), D->getNamespace()))
2334 std::tie(
2335 ToNamespaceLoc, ToAliasLoc, ToQualifierLoc, ToTargetNameLoc,
2336 ToNamespace) = *Imp;
2337 else
2338 return Imp.takeError();
2339 IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002340
Gabor Marton26f72a92018-07-12 09:42:05 +00002341 NamespaceAliasDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002342 if (GetImportedOrCreateDecl(
2343 ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc,
2344 ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace))
Gabor Marton26f72a92018-07-12 09:42:05 +00002345 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002346
2347 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002348 LexicalDC->addDeclInternal(ToD);
2349
2350 return ToD;
2351}
2352
Balazs Keri3b30d652018-10-19 13:32:20 +00002353ExpectedDecl
2354ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002355 // Import the major distinguishing characteristics of this typedef.
2356 DeclContext *DC, *LexicalDC;
2357 DeclarationName Name;
2358 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002359 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002360 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2361 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002362 if (ToD)
2363 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002364
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002365 // If this typedef is not in block scope, determine whether we've
2366 // seen a typedef with the same name (that we can merge with) or any
2367 // other entity by that name (which name lookup could conflict with).
Balazs Keric86d47b2019-09-04 14:12:18 +00002368 // Note: Repeated typedefs are not valid in C99:
2369 // 'typedef int T; typedef int T;' is invalid
2370 // We do not care about this now.
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002371 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002372 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002373 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002374 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002375 for (auto *FoundDecl : FoundDecls) {
2376 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002377 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002378 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Balazs Keric86d47b2019-09-04 14:12:18 +00002379 if (!hasSameVisibilityContext(FoundTypedef, D))
2380 continue;
2381
Gabor Martonb93baf62018-11-27 09:51:36 +00002382 QualType FromUT = D->getUnderlyingType();
2383 QualType FoundUT = FoundTypedef->getUnderlyingType();
2384 if (Importer.IsStructurallyEquivalent(FromUT, FoundUT)) {
2385 // If the "From" context has a complete underlying type but we
2386 // already have a complete underlying type then return with that.
2387 if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType())
Balazs Keri3b30d652018-10-19 13:32:20 +00002388 return Importer.MapImported(D, FoundTypedef);
Gabor Martonf035b752019-08-27 11:36:10 +00002389 // FIXME Handle redecl chain. When you do that make consistent changes
2390 // in ASTImporterLookupTable too.
2391 } else {
2392 ConflictingDecls.push_back(FoundDecl);
Gabor Martonb93baf62018-11-27 09:51:36 +00002393 }
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002394 }
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002395 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002396
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002397 if (!ConflictingDecls.empty()) {
Gabor Martonf035b752019-08-27 11:36:10 +00002398 ExpectedName NameOrErr = Importer.HandleNameConflict(
2399 Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size());
2400 if (NameOrErr)
2401 Name = NameOrErr.get();
2402 else
2403 return NameOrErr.takeError();
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002404 }
2405 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002406
Balazs Keri3b30d652018-10-19 13:32:20 +00002407 QualType ToUnderlyingType;
2408 TypeSourceInfo *ToTypeSourceInfo;
2409 SourceLocation ToBeginLoc;
2410 if (auto Imp = importSeq(
2411 D->getUnderlyingType(), D->getTypeSourceInfo(), D->getBeginLoc()))
2412 std::tie(ToUnderlyingType, ToTypeSourceInfo, ToBeginLoc) = *Imp;
2413 else
2414 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002415
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002416 // Create the new typedef node.
Balazs Keri3b30d652018-10-19 13:32:20 +00002417 // FIXME: ToUnderlyingType is not used.
Richard Smithdda56e42011-04-15 14:24:37 +00002418 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00002419 if (IsAlias) {
2420 if (GetImportedOrCreateDecl<TypeAliasDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002421 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2422 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002423 return ToTypedef;
2424 } else if (GetImportedOrCreateDecl<TypedefDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002425 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2426 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002427 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002428
Douglas Gregordd483172010-02-22 17:42:47 +00002429 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002430 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002431
2432 // Templated declarations should not appear in DeclContext.
2433 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
2434 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
2435 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002436
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002437 return ToTypedef;
2438}
2439
Balazs Keri3b30d652018-10-19 13:32:20 +00002440ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002441 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2442}
2443
Balazs Keri3b30d652018-10-19 13:32:20 +00002444ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002445 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2446}
2447
Balazs Keri3b30d652018-10-19 13:32:20 +00002448ExpectedDecl
2449ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
Gabor Horvath7a91c082017-11-14 11:30:38 +00002450 // Import the major distinguishing characteristics of this typedef.
2451 DeclContext *DC, *LexicalDC;
2452 DeclarationName Name;
2453 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002454 NamedDecl *FoundD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002455 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
2456 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002457 if (FoundD)
2458 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002459
2460 // If this typedef is not in block scope, determine whether we've
2461 // seen a typedef with the same name (that we can merge with) or any
2462 // other entity by that name (which name lookup could conflict with).
2463 if (!DC->isFunctionOrMethod()) {
2464 SmallVector<NamedDecl *, 4> ConflictingDecls;
2465 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002466 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002467 for (auto *FoundDecl : FoundDecls) {
2468 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002469 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002470 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002471 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002472 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002473 }
2474
2475 if (!ConflictingDecls.empty()) {
Gabor Martonf035b752019-08-27 11:36:10 +00002476 ExpectedName NameOrErr = Importer.HandleNameConflict(
2477 Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size());
2478 if (NameOrErr)
2479 Name = NameOrErr.get();
2480 else
2481 return NameOrErr.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002482 }
2483 }
2484
Balazs Keri3b30d652018-10-19 13:32:20 +00002485 TemplateParameterList *ToTemplateParameters;
2486 TypeAliasDecl *ToTemplatedDecl;
2487 if (auto Imp = importSeq(D->getTemplateParameters(), D->getTemplatedDecl()))
2488 std::tie(ToTemplateParameters, ToTemplatedDecl) = *Imp;
2489 else
2490 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002491
Gabor Marton26f72a92018-07-12 09:42:05 +00002492 TypeAliasTemplateDecl *ToAlias;
2493 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002494 Name, ToTemplateParameters, ToTemplatedDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002495 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002496
Balazs Keri3b30d652018-10-19 13:32:20 +00002497 ToTemplatedDecl->setDescribedAliasTemplate(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002498
Gabor Horvath7a91c082017-11-14 11:30:38 +00002499 ToAlias->setAccess(D->getAccess());
2500 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002501 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002502 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002503}
2504
Balazs Keri3b30d652018-10-19 13:32:20 +00002505ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002506 // Import the major distinguishing characteristics of this label.
2507 DeclContext *DC, *LexicalDC;
2508 DeclarationName Name;
2509 SourceLocation Loc;
2510 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002511 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2512 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002513 if (ToD)
2514 return ToD;
2515
2516 assert(LexicalDC->isFunctionOrMethod());
2517
Gabor Marton26f72a92018-07-12 09:42:05 +00002518 LabelDecl *ToLabel;
Balazs Keri3b30d652018-10-19 13:32:20 +00002519 if (D->isGnuLocal()) {
2520 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2521 if (!BeginLocOrErr)
2522 return BeginLocOrErr.takeError();
2523 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2524 Name.getAsIdentifierInfo(), *BeginLocOrErr))
2525 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002526
Balazs Keri3b30d652018-10-19 13:32:20 +00002527 } else {
2528 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2529 Name.getAsIdentifierInfo()))
2530 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002531
Balazs Keri3b30d652018-10-19 13:32:20 +00002532 }
2533
2534 Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt());
2535 if (!ToStmtOrErr)
2536 return ToStmtOrErr.takeError();
2537
2538 ToLabel->setStmt(*ToStmtOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002539 ToLabel->setLexicalDeclContext(LexicalDC);
2540 LexicalDC->addDeclInternal(ToLabel);
2541 return ToLabel;
2542}
2543
Balazs Keri3b30d652018-10-19 13:32:20 +00002544ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002545 // Import the major distinguishing characteristics of this enum.
2546 DeclContext *DC, *LexicalDC;
2547 DeclarationName Name;
2548 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002549 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002550 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2551 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002552 if (ToD)
2553 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002554
Douglas Gregor98c10182010-02-12 22:17:39 +00002555 // Figure out what enum name we're looking for.
2556 unsigned IDNS = Decl::IDNS_Tag;
2557 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002558 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002559 if (Error Err = importInto(
2560 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2561 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002562 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002563 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002564 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002565
Douglas Gregor98c10182010-02-12 22:17:39 +00002566 // We may already have an enum of the same name; try to find and match it.
2567 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002568 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002569 auto FoundDecls =
2570 Importer.findDeclsInToCtx(DC, SearchName);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002571 for (auto *FoundDecl : FoundDecls) {
2572 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002573 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002574
Balazs Keri3b30d652018-10-19 13:32:20 +00002575 if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002576 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Balazs Keri3b30d652018-10-19 13:32:20 +00002577 FoundDecl = Tag->getDecl();
Douglas Gregor98c10182010-02-12 22:17:39 +00002578 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002579
Balazs Keri3b30d652018-10-19 13:32:20 +00002580 if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) {
Balazs Kerieb79b252019-07-09 11:08:18 +00002581 if (!hasSameVisibilityContext(FoundEnum, D))
2582 continue;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002583 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002584 return Importer.MapImported(D, FoundEnum);
Gabor Martonf035b752019-08-27 11:36:10 +00002585 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002586 }
Douglas Gregor98c10182010-02-12 22:17:39 +00002587 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002588
Douglas Gregor98c10182010-02-12 22:17:39 +00002589 if (!ConflictingDecls.empty()) {
Gabor Martonf035b752019-08-27 11:36:10 +00002590 ExpectedName NameOrErr = Importer.HandleNameConflict(
2591 SearchName, DC, IDNS, ConflictingDecls.data(),
2592 ConflictingDecls.size());
2593 if (NameOrErr)
2594 Name = NameOrErr.get();
2595 else
2596 return NameOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00002597 }
2598 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002599
Balazs Keri3b30d652018-10-19 13:32:20 +00002600 SourceLocation ToBeginLoc;
2601 NestedNameSpecifierLoc ToQualifierLoc;
2602 QualType ToIntegerType;
Balázs Kéria9f10eb2019-12-05 16:21:21 +01002603 SourceRange ToBraceRange;
2604 if (auto Imp = importSeq(D->getBeginLoc(), D->getQualifierLoc(),
2605 D->getIntegerType(), D->getBraceRange()))
2606 std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType, ToBraceRange) = *Imp;
Balazs Keri3b30d652018-10-19 13:32:20 +00002607 else
2608 return Imp.takeError();
2609
Douglas Gregor98c10182010-02-12 22:17:39 +00002610 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002611 EnumDecl *D2;
2612 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002613 D2, D, Importer.getToContext(), DC, ToBeginLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002614 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2615 D->isScopedUsingClassTag(), D->isFixed()))
2616 return D2;
2617
Balazs Keri3b30d652018-10-19 13:32:20 +00002618 D2->setQualifierInfo(ToQualifierLoc);
2619 D2->setIntegerType(ToIntegerType);
Balázs Kéria9f10eb2019-12-05 16:21:21 +01002620 D2->setBraceRange(ToBraceRange);
Douglas Gregordd483172010-02-22 17:42:47 +00002621 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002622 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002623 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002624
Douglas Gregor98c10182010-02-12 22:17:39 +00002625 // Import the definition
Balazs Keri3b30d652018-10-19 13:32:20 +00002626 if (D->isCompleteDefinition())
2627 if (Error Err = ImportDefinition(D, D2))
2628 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002629
Douglas Gregor3996e242010-02-15 22:01:00 +00002630 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002631}
2632
Balazs Keri3b30d652018-10-19 13:32:20 +00002633ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002634 bool IsFriendTemplate = false;
2635 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2636 IsFriendTemplate =
2637 DCXX->getDescribedClassTemplate() &&
2638 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
2639 Decl::FOK_None;
2640 }
2641
Douglas Gregor5c73e912010-02-11 00:48:18 +00002642 // Import the major distinguishing characteristics of this record.
Simon Pilgrim4706f3b2019-10-15 10:23:05 +00002643 DeclContext *DC = nullptr, *LexicalDC = nullptr;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002644 DeclarationName Name;
2645 SourceLocation Loc;
Simon Pilgrim4706f3b2019-10-15 10:23:05 +00002646 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00002647 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2648 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002649 if (ToD)
2650 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002651
Douglas Gregor5c73e912010-02-11 00:48:18 +00002652 // Figure out what structure name we're looking for.
2653 unsigned IDNS = Decl::IDNS_Tag;
2654 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002655 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002656 if (Error Err = importInto(
2657 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2658 return std::move(Err);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002659 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002660 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Gabor Marton7df342a2018-12-17 12:42:12 +00002661 IDNS |= Decl::IDNS_Ordinary | Decl::IDNS_TagFriend;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002662
2663 // We may already have a record of the same name; try to find and match it.
Sean Callanan9092d472017-05-13 00:46:33 +00002664 RecordDecl *PrevDecl = nullptr;
Gabor Martone3e83d72019-08-30 10:55:41 +00002665 if (!DC->isFunctionOrMethod() && !D->isLambda()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002666 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002667 auto FoundDecls =
2668 Importer.findDeclsInToCtx(DC, SearchName);
Sean Callanan9092d472017-05-13 00:46:33 +00002669 if (!FoundDecls.empty()) {
Gabor Marton41e38922019-03-05 11:23:24 +00002670 // We're going to have to compare D against potentially conflicting Decls,
2671 // so complete it.
Sean Callanan9092d472017-05-13 00:46:33 +00002672 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2673 D->getASTContext().getExternalSource()->CompleteType(D);
2674 }
2675
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002676 for (auto *FoundDecl : FoundDecls) {
2677 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002678 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002679
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002680 Decl *Found = FoundDecl;
2681 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2682 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002683 Found = Tag->getDecl();
2684 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002685
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002686 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Gabor Marton7df342a2018-12-17 12:42:12 +00002687 // Do not emit false positive diagnostic in case of unnamed
2688 // struct/union and in case of anonymous structs. Would be false
2689 // because there may be several anonymous/unnamed structs in a class.
2690 // E.g. these are both valid:
2691 // struct A { // unnamed structs
2692 // struct { struct A *next; } entry0;
2693 // struct { struct A *next; } entry1;
2694 // };
2695 // struct X { struct { int a; }; struct { int b; }; }; // anon structs
2696 if (!SearchName)
Gabor Marton0bebf952018-07-05 09:51:13 +00002697 if (!IsStructuralMatch(D, FoundRecord, false))
2698 continue;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002699
Balazs Keric8272192019-05-27 09:36:00 +00002700 if (!hasSameVisibilityContext(FoundRecord, D))
2701 continue;
2702
Gabor Marton7df342a2018-12-17 12:42:12 +00002703 if (IsStructuralMatch(D, FoundRecord)) {
2704 RecordDecl *FoundDef = FoundRecord->getDefinition();
2705 if (D->isThisDeclarationADefinition() && FoundDef) {
Balazs Keri1d20cc22018-07-16 12:16:39 +00002706 // FIXME: Structural equivalence check should check for same
2707 // user-defined methods.
2708 Importer.MapImported(D, FoundDef);
2709 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2710 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2711 assert(FoundCXX && "Record type mismatch");
2712
Gabor Marton7df342a2018-12-17 12:42:12 +00002713 if (!Importer.isMinimalImport())
Balazs Keri1d20cc22018-07-16 12:16:39 +00002714 // FoundDef may not have every implicit method that D has
2715 // because implicit methods are created only if they are used.
Balazs Keri3b30d652018-10-19 13:32:20 +00002716 if (Error Err = ImportImplicitMethods(DCXX, FoundCXX))
2717 return std::move(Err);
Balazs Keri1d20cc22018-07-16 12:16:39 +00002718 }
Douglas Gregor25791052010-02-12 00:09:27 +00002719 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002720 PrevDecl = FoundRecord->getMostRecentDecl();
2721 break;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002722 }
Gabor Martonf035b752019-08-27 11:36:10 +00002723 ConflictingDecls.push_back(FoundDecl);
2724 } // kind is RecordDecl
Gabor Marton7df342a2018-12-17 12:42:12 +00002725 } // for
Fangrui Song6907ce22018-07-30 19:24:48 +00002726
Douglas Gregordd6006f2012-07-17 21:16:27 +00002727 if (!ConflictingDecls.empty() && SearchName) {
Gabor Martonf035b752019-08-27 11:36:10 +00002728 ExpectedName NameOrErr = Importer.HandleNameConflict(
2729 SearchName, DC, IDNS, ConflictingDecls.data(),
2730 ConflictingDecls.size());
2731 if (NameOrErr)
2732 Name = NameOrErr.get();
2733 else
2734 return NameOrErr.takeError();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002735 }
2736 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002737
Balazs Keri3b30d652018-10-19 13:32:20 +00002738 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2739 if (!BeginLocOrErr)
2740 return BeginLocOrErr.takeError();
2741
Douglas Gregor5c73e912010-02-11 00:48:18 +00002742 // Create the record declaration.
Gabor Marton7df342a2018-12-17 12:42:12 +00002743 RecordDecl *D2 = nullptr;
2744 CXXRecordDecl *D2CXX = nullptr;
2745 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2746 if (DCXX->isLambda()) {
2747 auto TInfoOrErr = import(DCXX->getLambdaTypeInfo());
2748 if (!TInfoOrErr)
2749 return TInfoOrErr.takeError();
2750 if (GetImportedOrCreateSpecialDecl(
2751 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
2752 DC, *TInfoOrErr, Loc, DCXX->isDependentLambda(),
2753 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2754 return D2CXX;
2755 ExpectedDecl CDeclOrErr = import(DCXX->getLambdaContextDecl());
2756 if (!CDeclOrErr)
2757 return CDeclOrErr.takeError();
Michael Liao243ebfb2019-10-19 00:15:19 +00002758 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), *CDeclOrErr,
2759 DCXX->hasKnownLambdaInternalLinkage());
Gabor Marton7df342a2018-12-17 12:42:12 +00002760 } else if (DCXX->isInjectedClassName()) {
2761 // We have to be careful to do a similar dance to the one in
2762 // Sema::ActOnStartCXXMemberDeclarations
2763 const bool DelayTypeCreation = true;
2764 if (GetImportedOrCreateDecl(
2765 D2CXX, D, Importer.getToContext(), D->getTagKind(), DC,
2766 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
2767 cast_or_null<CXXRecordDecl>(PrevDecl), DelayTypeCreation))
2768 return D2CXX;
2769 Importer.getToContext().getTypeDeclType(
2770 D2CXX, dyn_cast<CXXRecordDecl>(DC));
2771 } else {
2772 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2773 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2774 Name.getAsIdentifierInfo(),
2775 cast_or_null<CXXRecordDecl>(PrevDecl)))
2776 return D2CXX;
2777 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002778
Gabor Marton7df342a2018-12-17 12:42:12 +00002779 D2 = D2CXX;
2780 D2->setAccess(D->getAccess());
2781 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonbc5b7e22019-12-04 17:12:08 +01002782 addDeclToContexts(D, D2);
Gabor Marton7df342a2018-12-17 12:42:12 +00002783
2784 if (ClassTemplateDecl *FromDescribed =
2785 DCXX->getDescribedClassTemplate()) {
2786 ClassTemplateDecl *ToDescribed;
2787 if (Error Err = importInto(ToDescribed, FromDescribed))
2788 return std::move(Err);
2789 D2CXX->setDescribedClassTemplate(ToDescribed);
2790 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
2791 // In a record describing a template the type should be an
2792 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2793 // previously set type to the correct value here (ToDescribed is not
2794 // available at record create).
2795 // FIXME: The previous type is cleared but not removed from
2796 // ASTContext's internal storage.
2797 CXXRecordDecl *Injected = nullptr;
2798 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2799 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2800 if (Record && Record->isInjectedClassName()) {
2801 Injected = Record;
2802 break;
Gabor Marton5915777e2018-06-26 13:44:24 +00002803 }
2804 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002805 // Create an injected type for the whole redecl chain.
2806 SmallVector<Decl *, 2> Redecls =
2807 getCanonicalForwardRedeclChain(D2CXX);
2808 for (auto *R : Redecls) {
2809 auto *RI = cast<CXXRecordDecl>(R);
2810 RI->setTypeForDecl(nullptr);
2811 // Below we create a new injected type and assign that to the
2812 // canonical decl, subsequent declarations in the chain will reuse
2813 // that type.
2814 Importer.getToContext().getInjectedClassNameType(
2815 RI, ToDescribed->getInjectedClassNameSpecialization());
2816 }
2817 // Set the new type for the previous injected decl too.
2818 if (Injected) {
2819 Injected->setTypeForDecl(nullptr);
2820 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2821 }
2822 }
2823 } else if (MemberSpecializationInfo *MemberInfo =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002824 DCXX->getMemberSpecializationInfo()) {
2825 TemplateSpecializationKind SK =
2826 MemberInfo->getTemplateSpecializationKind();
2827 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Balazs Keri3b30d652018-10-19 13:32:20 +00002828
2829 if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst))
2830 D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK);
2831 else
2832 return ToInstOrErr.takeError();
2833
2834 if (ExpectedSLoc POIOrErr =
2835 import(MemberInfo->getPointOfInstantiation()))
2836 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2837 *POIOrErr);
2838 else
2839 return POIOrErr.takeError();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002840 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002841
Gabor Marton7df342a2018-12-17 12:42:12 +00002842 } else {
2843 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
2844 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2845 Name.getAsIdentifierInfo(), PrevDecl))
2846 return D2;
2847 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonbc5b7e22019-12-04 17:12:08 +01002848 addDeclToContexts(D, D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002849 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002850
Balázs Kéria9f10eb2019-12-05 16:21:21 +01002851 if (auto BraceRangeOrErr = import(D->getBraceRange()))
2852 D2->setBraceRange(*BraceRangeOrErr);
2853 else
2854 return BraceRangeOrErr.takeError();
Gabor Marton7df342a2018-12-17 12:42:12 +00002855 if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
2856 D2->setQualifierInfo(*QualifierLocOrErr);
2857 else
2858 return QualifierLocOrErr.takeError();
2859
2860 if (D->isAnonymousStructOrUnion())
2861 D2->setAnonymousStructOrUnion(true);
Douglas Gregor25791052010-02-12 00:09:27 +00002862
Balazs Keri3b30d652018-10-19 13:32:20 +00002863 if (D->isCompleteDefinition())
2864 if (Error Err = ImportDefinition(D, D2, IDK_Default))
2865 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00002866
Douglas Gregor3996e242010-02-15 22:01:00 +00002867 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002868}
2869
Balazs Keri3b30d652018-10-19 13:32:20 +00002870ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002871 // Import the major distinguishing characteristics of this enumerator.
2872 DeclContext *DC, *LexicalDC;
2873 DeclarationName Name;
2874 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002875 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002876 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2877 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002878 if (ToD)
2879 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002880
Fangrui Song6907ce22018-07-30 19:24:48 +00002881 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002882 // in the same context.
2883 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002884 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002885 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002886 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002887 for (auto *FoundDecl : FoundDecls) {
2888 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002889 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002890
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002891 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002892 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002893 return Importer.MapImported(D, FoundEnumConstant);
Gabor Martonf035b752019-08-27 11:36:10 +00002894 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor91155082012-11-14 22:29:20 +00002895 }
Douglas Gregor98c10182010-02-12 22:17:39 +00002896 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002897
Douglas Gregor98c10182010-02-12 22:17:39 +00002898 if (!ConflictingDecls.empty()) {
Gabor Martonf035b752019-08-27 11:36:10 +00002899 ExpectedName NameOrErr = Importer.HandleNameConflict(
2900 Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size());
2901 if (NameOrErr)
2902 Name = NameOrErr.get();
2903 else
2904 return NameOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00002905 }
2906 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002907
Balazs Keri3b30d652018-10-19 13:32:20 +00002908 ExpectedType TypeOrErr = import(D->getType());
2909 if (!TypeOrErr)
2910 return TypeOrErr.takeError();
2911
2912 ExpectedExpr InitOrErr = import(D->getInitExpr());
2913 if (!InitOrErr)
2914 return InitOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002915
Gabor Marton26f72a92018-07-12 09:42:05 +00002916 EnumConstantDecl *ToEnumerator;
2917 if (GetImportedOrCreateDecl(
2918 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002919 Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002920 return ToEnumerator;
2921
Douglas Gregordd483172010-02-22 17:42:47 +00002922 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002923 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002924 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002925 return ToEnumerator;
2926}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002927
Balazs Keri1efc9742019-05-07 10:55:11 +00002928Error ASTNodeImporter::ImportTemplateParameterLists(const DeclaratorDecl *FromD,
2929 DeclaratorDecl *ToD) {
2930 unsigned int Num = FromD->getNumTemplateParameterLists();
2931 if (Num == 0)
2932 return Error::success();
2933 SmallVector<TemplateParameterList *, 2> ToTPLists(Num);
2934 for (unsigned int I = 0; I < Num; ++I)
2935 if (Expected<TemplateParameterList *> ToTPListOrErr =
2936 import(FromD->getTemplateParameterList(I)))
2937 ToTPLists[I] = *ToTPListOrErr;
2938 else
2939 return ToTPListOrErr.takeError();
2940 ToD->setTemplateParameterListsInfo(Importer.ToContext, ToTPLists);
2941 return Error::success();
2942}
2943
Balazs Keri3b30d652018-10-19 13:32:20 +00002944Error ASTNodeImporter::ImportTemplateInformation(
2945 FunctionDecl *FromFD, FunctionDecl *ToFD) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002946 switch (FromFD->getTemplatedKind()) {
2947 case FunctionDecl::TK_NonTemplate:
2948 case FunctionDecl::TK_FunctionTemplate:
Balazs Keri3b30d652018-10-19 13:32:20 +00002949 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002950
2951 case FunctionDecl::TK_MemberSpecialization: {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002952 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00002953
2954 if (Expected<FunctionDecl *> InstFDOrErr =
2955 import(FromFD->getInstantiatedFromMemberFunction()))
2956 ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK);
2957 else
2958 return InstFDOrErr.takeError();
2959
2960 if (ExpectedSLoc POIOrErr = import(
2961 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()))
2962 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
2963 else
2964 return POIOrErr.takeError();
2965
2966 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002967 }
2968
2969 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Balazs Keri3b30d652018-10-19 13:32:20 +00002970 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002971 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002972 if (!FunctionAndArgsOrErr)
2973 return FunctionAndArgsOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002974
2975 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Balazs Keri3b30d652018-10-19 13:32:20 +00002976 Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002977
Gabor Marton5254e642018-06-27 13:32:50 +00002978 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002979 TemplateArgumentListInfo ToTAInfo;
2980 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002981 if (FromTAArgsAsWritten)
Balazs Keri3b30d652018-10-19 13:32:20 +00002982 if (Error Err = ImportTemplateArgumentListInfo(
2983 *FromTAArgsAsWritten, ToTAInfo))
2984 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002985
Balazs Keri3b30d652018-10-19 13:32:20 +00002986 ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation());
2987 if (!POIOrErr)
2988 return POIOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002989
Balazs Keri1efc9742019-05-07 10:55:11 +00002990 if (Error Err = ImportTemplateParameterLists(FromFD, ToFD))
2991 return Err;
2992
Gabor Marton5254e642018-06-27 13:32:50 +00002993 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002994 ToFD->setFunctionTemplateSpecialization(
Balazs Keri3b30d652018-10-19 13:32:20 +00002995 std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr,
2996 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr);
2997 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002998 }
2999
3000 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
3001 auto *FromInfo = FromFD->getDependentSpecializationInfo();
3002 UnresolvedSet<8> TemplDecls;
3003 unsigned NumTemplates = FromInfo->getNumTemplates();
3004 for (unsigned I = 0; I < NumTemplates; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003005 if (Expected<FunctionTemplateDecl *> ToFTDOrErr =
3006 import(FromInfo->getTemplate(I)))
3007 TemplDecls.addDecl(*ToFTDOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003008 else
Balazs Keri3b30d652018-10-19 13:32:20 +00003009 return ToFTDOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003010 }
3011
3012 // Import TemplateArgumentListInfo.
3013 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00003014 if (Error Err = ImportTemplateArgumentListInfo(
3015 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
3016 llvm::makeArrayRef(
3017 FromInfo->getTemplateArgs(), FromInfo->getNumTemplateArgs()),
3018 ToTAInfo))
3019 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003020
3021 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
3022 TemplDecls, ToTAInfo);
Balazs Keri3b30d652018-10-19 13:32:20 +00003023 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003024 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003025 }
Sam McCallfdc32072018-01-26 12:06:44 +00003026 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003027}
3028
Balazs Keri3b30d652018-10-19 13:32:20 +00003029Expected<FunctionDecl *>
Gabor Marton5254e642018-06-27 13:32:50 +00003030ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003031 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00003032 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00003033 if (!FunctionAndArgsOrErr)
3034 return FunctionAndArgsOrErr.takeError();
Gabor Marton5254e642018-06-27 13:32:50 +00003035
Balazs Keri3b30d652018-10-19 13:32:20 +00003036 FunctionTemplateDecl *Template;
3037 TemplateArgsTy ToTemplArgs;
3038 std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr;
Gabor Marton5254e642018-06-27 13:32:50 +00003039 void *InsertPos = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003040 auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos);
Gabor Marton5254e642018-06-27 13:32:50 +00003041 return FoundSpec;
3042}
3043
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003044Error ASTNodeImporter::ImportFunctionDeclBody(FunctionDecl *FromFD,
3045 FunctionDecl *ToFD) {
3046 if (Stmt *FromBody = FromFD->getBody()) {
3047 if (ExpectedStmt ToBodyOrErr = import(FromBody))
3048 ToFD->setBody(*ToBodyOrErr);
3049 else
3050 return ToBodyOrErr.takeError();
3051 }
3052 return Error::success();
3053}
3054
Gabor Marton25234fd2019-12-12 17:13:35 +01003055// Returns true if the given D has a DeclContext up to the TranslationUnitDecl
3056// which is equal to the given DC.
3057bool isAncestorDeclContextOf(const DeclContext *DC, const Decl *D) {
3058 const DeclContext *DCi = D->getDeclContext();
3059 while (DCi != D->getTranslationUnitDecl()) {
3060 if (DCi == DC)
3061 return true;
3062 DCi = DCi->getParent();
3063 }
3064 return false;
3065}
3066
3067bool ASTNodeImporter::hasAutoReturnTypeDeclaredInside(FunctionDecl *D) {
3068 QualType FromTy = D->getType();
3069 const FunctionProtoType *FromFPT = FromTy->getAs<FunctionProtoType>();
3070 assert(FromFPT && "Must be called on FunctionProtoType");
3071 if (AutoType *AutoT = FromFPT->getReturnType()->getContainedAutoType()) {
3072 QualType DeducedT = AutoT->getDeducedType();
3073 if (const RecordType *RecordT =
3074 DeducedT.isNull() ? nullptr : dyn_cast<RecordType>(DeducedT)) {
3075 RecordDecl *RD = RecordT->getDecl();
3076 assert(RD);
3077 if (isAncestorDeclContextOf(D, RD)) {
3078 assert(RD->getLexicalDeclContext() == RD->getDeclContext());
3079 return true;
3080 }
3081 }
3082 }
3083 if (const TypedefType *TypedefT =
3084 dyn_cast<TypedefType>(FromFPT->getReturnType())) {
3085 TypedefNameDecl *TD = TypedefT->getDecl();
3086 assert(TD);
3087 if (isAncestorDeclContextOf(D, TD)) {
3088 assert(TD->getLexicalDeclContext() == TD->getDeclContext());
3089 return true;
3090 }
3091 }
3092 return false;
3093}
3094
Balazs Keri3b30d652018-10-19 13:32:20 +00003095ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00003096
Balazs Keri3b30d652018-10-19 13:32:20 +00003097 SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D);
Gabor Marton5254e642018-06-27 13:32:50 +00003098 auto RedeclIt = Redecls.begin();
3099 // Import the first part of the decl chain. I.e. import all previous
3100 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003101 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3102 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3103 if (!ToRedeclOrErr)
3104 return ToRedeclOrErr.takeError();
3105 }
Gabor Marton5254e642018-06-27 13:32:50 +00003106 assert(*RedeclIt == D);
3107
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003108 // Import the major distinguishing characteristics of this function.
3109 DeclContext *DC, *LexicalDC;
3110 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003111 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003112 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003113 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3114 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003115 if (ToD)
3116 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003117
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003118 FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00003119 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00003120
Gabor Marton5254e642018-06-27 13:32:50 +00003121 // If this is a function template specialization, then try to find the same
Gabor Marton54058b52018-12-17 13:53:12 +00003122 // existing specialization in the "to" context. The lookup below will not
3123 // find any specialization, but would find the primary template; thus, we
3124 // have to skip normal lookup in case of specializations.
Gabor Marton5254e642018-06-27 13:32:50 +00003125 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
3126 if (D->getTemplatedKind() ==
3127 FunctionDecl::TK_FunctionTemplateSpecialization) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003128 auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D);
3129 if (!FoundFunctionOrErr)
3130 return FoundFunctionOrErr.takeError();
3131 if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) {
Gabor Martondd59d272019-03-19 14:04:50 +00003132 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
3133 return Def;
Gabor Marton5254e642018-06-27 13:32:50 +00003134 FoundByLookup = FoundFunction;
3135 }
3136 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003137 // Try to find a function in our own ("to") context with the same name, same
3138 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00003139 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003140 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00003141 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00003142 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003143 for (auto *FoundDecl : FoundDecls) {
3144 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003145 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003146
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003147 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00003148 if (!hasSameVisibilityContext(FoundFunction, D))
3149 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003150
Gabor Marton458d1452019-02-14 13:07:03 +00003151 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Martondd59d272019-03-19 14:04:50 +00003152 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
3153 return Def;
Gabor Marton458d1452019-02-14 13:07:03 +00003154 FoundByLookup = FoundFunction;
3155 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003156 }
Gabor Marton458d1452019-02-14 13:07:03 +00003157 // FIXME: Check for overloading more carefully, e.g., by boosting
3158 // Sema::IsOverload out to the AST library.
3159
3160 // Function overloading is okay in C++.
3161 if (Importer.getToContext().getLangOpts().CPlusPlus)
3162 continue;
3163
3164 // Complain about inconsistent function types.
Gabor Marton410f32c2019-04-01 15:29:55 +00003165 Importer.ToDiag(Loc, diag::warn_odr_function_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00003166 << Name << D->getType() << FoundFunction->getType();
3167 Importer.ToDiag(FoundFunction->getLocation(), diag::note_odr_value_here)
3168 << FoundFunction->getType();
Gabor Martonf035b752019-08-27 11:36:10 +00003169 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003170 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003171 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003172
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003173 if (!ConflictingDecls.empty()) {
Gabor Martonf035b752019-08-27 11:36:10 +00003174 ExpectedName NameOrErr = Importer.HandleNameConflict(
3175 Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size());
3176 if (NameOrErr)
3177 Name = NameOrErr.get();
3178 else
3179 return NameOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00003180 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003181 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00003182
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003183 // We do not allow more than one in-class declaration of a function. This is
3184 // because AST clients like VTableBuilder asserts on this. VTableBuilder
3185 // assumes there is only one in-class declaration. Building a redecl
3186 // chain would result in more than one in-class declaration for
3187 // overrides (even if they are part of the same redecl chain inside the
3188 // derived class.)
3189 if (FoundByLookup) {
Mikael Holmenc1c97aa2019-01-29 06:53:31 +00003190 if (isa<CXXMethodDecl>(FoundByLookup)) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003191 if (D->getLexicalDeclContext() == D->getDeclContext()) {
Balazs Kerie9719f92019-08-07 12:40:17 +00003192 if (!D->doesThisDeclarationHaveABody()) {
3193 if (FunctionTemplateDecl *DescribedD =
3194 D->getDescribedFunctionTemplate()) {
3195 // Handle a "templated" function together with its described
3196 // template. This avoids need for a similar check at import of the
3197 // described template.
3198 assert(FoundByLookup->getDescribedFunctionTemplate() &&
3199 "Templated function mapped to non-templated?");
3200 Importer.MapImported(DescribedD,
3201 FoundByLookup->getDescribedFunctionTemplate());
3202 }
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003203 return Importer.MapImported(D, FoundByLookup);
Balazs Kerie9719f92019-08-07 12:40:17 +00003204 } else {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003205 // Let's continue and build up the redecl chain in this case.
3206 // FIXME Merge the functions into one decl.
3207 }
3208 }
3209 }
3210 }
3211
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003212 DeclarationNameInfo NameInfo(Name, Loc);
3213 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00003214 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
3215 return std::move(Err);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003216
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003217 QualType FromTy = D->getType();
Gabor Marton25234fd2019-12-12 17:13:35 +01003218 // Set to true if we do not import the type of the function as is. There are
3219 // cases when the original type would result in an infinite recursion during
3220 // the import. To avoid an infinite recursion when importing, we create the
3221 // FunctionDecl with a simplified function type and update it only after the
3222 // relevant AST nodes are already imported.
3223 bool UsedDifferentProtoType = false;
3224 if (const auto *FromFPT = FromTy->getAs<FunctionProtoType>()) {
3225 QualType FromReturnTy = FromFPT->getReturnType();
3226 // Functions with auto return type may define a struct inside their body
3227 // and the return type could refer to that struct.
3228 // E.g.: auto foo() { struct X{}; return X(); }
3229 // To avoid an infinite recursion when importing, create the FunctionDecl
3230 // with a simplified return type.
3231 if (hasAutoReturnTypeDeclaredInside(D)) {
3232 FromReturnTy = Importer.getFromContext().VoidTy;
3233 UsedDifferentProtoType = true;
3234 }
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003235 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3236 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3237 // FunctionDecl that we are importing the FunctionProtoType for.
3238 // To avoid an infinite recursion when importing, create the FunctionDecl
Gabor Marton25234fd2019-12-12 17:13:35 +01003239 // with a simplified function type.
Richard Smith8acb4282014-07-31 21:57:55 +00003240 if (FromEPI.ExceptionSpec.SourceDecl ||
3241 FromEPI.ExceptionSpec.SourceTemplate ||
3242 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003243 FunctionProtoType::ExtProtoInfo DefaultEPI;
Gabor Marton25234fd2019-12-12 17:13:35 +01003244 FromEPI = DefaultEPI;
3245 UsedDifferentProtoType = true;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003246 }
Gabor Marton25234fd2019-12-12 17:13:35 +01003247 FromTy = Importer.getFromContext().getFunctionType(
3248 FromReturnTy, FromFPT->getParamTypes(), FromEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003249 }
3250
Balazs Keri3b30d652018-10-19 13:32:20 +00003251 QualType T;
3252 TypeSourceInfo *TInfo;
3253 SourceLocation ToInnerLocStart, ToEndLoc;
3254 NestedNameSpecifierLoc ToQualifierLoc;
3255 if (auto Imp = importSeq(
3256 FromTy, D->getTypeSourceInfo(), D->getInnerLocStart(),
3257 D->getQualifierLoc(), D->getEndLoc()))
3258 std::tie(T, TInfo, ToInnerLocStart, ToQualifierLoc, ToEndLoc) = *Imp;
3259 else
3260 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003261
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003262 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003263 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003264 for (auto P : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003265 if (Expected<ParmVarDecl *> ToPOrErr = import(P))
3266 Parameters.push_back(*ToPOrErr);
3267 else
3268 return ToPOrErr.takeError();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003269 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003270
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003271 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00003272 FunctionDecl *ToFunction = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003273 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith76b90272019-05-09 03:59:21 +00003274 Expr *ExplicitExpr = nullptr;
3275 if (FromConstructor->getExplicitSpecifier().getExpr()) {
3276 auto Imp = importSeq(FromConstructor->getExplicitSpecifier().getExpr());
3277 if (!Imp)
3278 return Imp.takeError();
3279 std::tie(ExplicitExpr) = *Imp;
3280 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003281 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
Richard Smith76b90272019-05-09 03:59:21 +00003282 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3283 ToInnerLocStart, NameInfo, T, TInfo,
3284 ExplicitSpecifier(
3285 ExplicitExpr,
3286 FromConstructor->getExplicitSpecifier().getKind()),
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003287 D->isInlineSpecified(), D->isImplicit(), D->getConstexprKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003288 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003289 } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(D)) {
3290
3291 auto Imp =
3292 importSeq(const_cast<FunctionDecl *>(FromDtor->getOperatorDelete()),
3293 FromDtor->getOperatorDeleteThisArg());
3294
3295 if (!Imp)
3296 return Imp.takeError();
3297
3298 FunctionDecl *ToOperatorDelete;
3299 Expr *ToThisArg;
3300 std::tie(ToOperatorDelete, ToThisArg) = *Imp;
3301
Gabor Marton26f72a92018-07-12 09:42:05 +00003302 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003303 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3304 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Richard Smith457226e2019-09-23 03:48:44 +00003305 D->isImplicit(), D->getConstexprKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003306 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003307
3308 CXXDestructorDecl *ToDtor = cast<CXXDestructorDecl>(ToFunction);
3309
3310 ToDtor->setOperatorDelete(ToOperatorDelete, ToThisArg);
Gabor Marton26f72a92018-07-12 09:42:05 +00003311 } else if (CXXConversionDecl *FromConversion =
3312 dyn_cast<CXXConversionDecl>(D)) {
Richard Smith76b90272019-05-09 03:59:21 +00003313 Expr *ExplicitExpr = nullptr;
3314 if (FromConversion->getExplicitSpecifier().getExpr()) {
3315 auto Imp = importSeq(FromConversion->getExplicitSpecifier().getExpr());
3316 if (!Imp)
3317 return Imp.takeError();
3318 std::tie(ExplicitExpr) = *Imp;
3319 }
Gabor Marton26f72a92018-07-12 09:42:05 +00003320 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3321 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003322 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Richard Smith76b90272019-05-09 03:59:21 +00003323 ExplicitSpecifier(ExplicitExpr,
3324 FromConversion->getExplicitSpecifier().getKind()),
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003325 D->getConstexprKind(), SourceLocation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003326 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003327 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003328 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3329 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003330 ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003331 Method->isInlineSpecified(), D->getConstexprKind(),
3332 SourceLocation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003333 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003334 } else {
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003335 if (GetImportedOrCreateDecl(
3336 ToFunction, D, Importer.getToContext(), DC, ToInnerLocStart,
3337 NameInfo, T, TInfo, D->getStorageClass(), D->isInlineSpecified(),
3338 D->hasWrittenPrototype(), D->getConstexprKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003339 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003340 }
John McCall3e11ebe2010-03-15 10:12:16 +00003341
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003342 // Connect the redecl chain.
3343 if (FoundByLookup) {
3344 auto *Recent = const_cast<FunctionDecl *>(
3345 FoundByLookup->getMostRecentDecl());
3346 ToFunction->setPreviousDecl(Recent);
Gabor Martonce6b7812019-05-08 15:23:48 +00003347 // FIXME Probably we should merge exception specifications. E.g. In the
3348 // "To" context the existing function may have exception specification with
3349 // noexcept-unevaluated, while the newly imported function may have an
3350 // evaluated noexcept. A call to adjustExceptionSpec() on the imported
3351 // decl and its redeclarations may be required.
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003352 }
3353
Balazs Keri3b30d652018-10-19 13:32:20 +00003354 ToFunction->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003355 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003356 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003357 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3358 ToFunction->setTrivial(D->isTrivial());
3359 ToFunction->setPure(D->isPure());
Balazs Kerib427c062019-08-13 08:04:06 +00003360 ToFunction->setDefaulted(D->isDefaulted());
3361 ToFunction->setExplicitlyDefaulted(D->isExplicitlyDefaulted());
3362 ToFunction->setDeletedAsWritten(D->isDeletedAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00003363 ToFunction->setRangeEnd(ToEndLoc);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003364
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003365 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003366 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003367 Param->setOwningFunction(ToFunction);
3368 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003369 }
David Blaikie9c70e042011-09-21 18:16:56 +00003370 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003371
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003372 // We need to complete creation of FunctionProtoTypeLoc manually with setting
3373 // params it refers to.
3374 if (TInfo) {
3375 if (auto ProtoLoc =
3376 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
3377 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
3378 ProtoLoc.setParam(I, Parameters[I]);
3379 }
3380 }
3381
Balazs Keria35798d2018-07-17 09:52:41 +00003382 // Import the describing template function, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00003383 if (FromFT) {
3384 auto ToFTOrErr = import(FromFT);
3385 if (!ToFTOrErr)
3386 return ToFTOrErr.takeError();
3387 }
Balazs Keria35798d2018-07-17 09:52:41 +00003388
Balazs Kerie13e8362019-08-16 12:10:03 +00003389 // Import Ctor initializers.
3390 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3391 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3392 SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers);
3393 // Import first, then allocate memory and copy if there was no error.
3394 if (Error Err = ImportContainerChecked(
3395 FromConstructor->inits(), CtorInitializers))
3396 return std::move(Err);
3397 auto **Memory =
3398 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3399 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3400 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
3401 ToCtor->setCtorInitializers(Memory);
3402 ToCtor->setNumCtorInitializers(NumInitializers);
3403 }
3404 }
3405
Gabor Marton5254e642018-06-27 13:32:50 +00003406 if (D->doesThisDeclarationHaveABody()) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003407 Error Err = ImportFunctionDeclBody(D, ToFunction);
3408
3409 if (Err)
3410 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003411 }
3412
Gabor Marton25234fd2019-12-12 17:13:35 +01003413 // Import and set the original type in case we used another type.
3414 if (UsedDifferentProtoType) {
3415 if (ExpectedType TyOrErr = import(D->getType()))
3416 ToFunction->setType(*TyOrErr);
3417 else
3418 return TyOrErr.takeError();
3419 }
3420
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003421 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003422
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003423 // If it is a template, import all related things.
Balazs Keri3b30d652018-10-19 13:32:20 +00003424 if (Error Err = ImportTemplateInformation(D, ToFunction))
3425 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003426
Gabor Martonbc5b7e22019-12-04 17:12:08 +01003427 addDeclToContexts(D, ToFunction);
Gabor Marton5254e642018-06-27 13:32:50 +00003428
Gabor Marton7a0841e2018-10-29 10:18:28 +00003429 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
Balazs Kerib4fd7d42019-08-30 10:12:14 +00003430 if (Error Err = ImportOverriddenMethods(cast<CXXMethodDecl>(ToFunction),
3431 FromCXXMethod))
3432 return std::move(Err);
Gabor Marton7a0841e2018-10-29 10:18:28 +00003433
Gabor Marton5254e642018-06-27 13:32:50 +00003434 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003435 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3436 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3437 if (!ToRedeclOrErr)
3438 return ToRedeclOrErr.takeError();
3439 }
Gabor Marton5254e642018-06-27 13:32:50 +00003440
Douglas Gregor43f54792010-02-17 02:12:47 +00003441 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003442}
3443
Balazs Keri3b30d652018-10-19 13:32:20 +00003444ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003445 return VisitFunctionDecl(D);
3446}
3447
Balazs Keri3b30d652018-10-19 13:32:20 +00003448ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003449 return VisitCXXMethodDecl(D);
3450}
3451
Balazs Keri3b30d652018-10-19 13:32:20 +00003452ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003453 return VisitCXXMethodDecl(D);
3454}
3455
Balazs Keri3b30d652018-10-19 13:32:20 +00003456ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003457 return VisitCXXMethodDecl(D);
3458}
3459
Balazs Keri3b30d652018-10-19 13:32:20 +00003460ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003461 // Import the major distinguishing characteristics of a variable.
3462 DeclContext *DC, *LexicalDC;
3463 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003464 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003465 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003466 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3467 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003468 if (ToD)
3469 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003470
Fangrui Song6907ce22018-07-30 19:24:48 +00003471 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003472 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003473 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003474 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003475 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003476 if (!Name &&
3477 ASTImporter::getFieldIndex(D) !=
3478 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003479 continue;
3480
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003481 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003482 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003483 Importer.MapImported(D, FoundField);
Gabor Marton42e15de2018-08-22 11:52:14 +00003484 // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the
3485 // initializer of a FieldDecl might not had been instantiated in the
3486 // "To" context. However, the "From" context might instantiated that,
3487 // thus we have to merge that.
3488 if (Expr *FromInitializer = D->getInClassInitializer()) {
3489 // We don't have yet the initializer set.
3490 if (FoundField->hasInClassInitializer() &&
3491 !FoundField->getInClassInitializer()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003492 if (ExpectedExpr ToInitializerOrErr = import(FromInitializer))
3493 FoundField->setInClassInitializer(*ToInitializerOrErr);
3494 else {
3495 // We can't return error here,
Gabor Marton42e15de2018-08-22 11:52:14 +00003496 // since we already mapped D as imported.
Balazs Keri3b30d652018-10-19 13:32:20 +00003497 // FIXME: warning message?
3498 consumeError(ToInitializerOrErr.takeError());
Gabor Marton42e15de2018-08-22 11:52:14 +00003499 return FoundField;
Balazs Keri3b30d652018-10-19 13:32:20 +00003500 }
Gabor Marton42e15de2018-08-22 11:52:14 +00003501 }
3502 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003503 return FoundField;
3504 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003505
Balazs Keri3b30d652018-10-19 13:32:20 +00003506 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003507 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003508 << Name << D->getType() << FoundField->getType();
3509 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3510 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003511
3512 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003513 }
3514 }
3515
Balazs Keri3b30d652018-10-19 13:32:20 +00003516 QualType ToType;
3517 TypeSourceInfo *ToTInfo;
3518 Expr *ToBitWidth;
3519 SourceLocation ToInnerLocStart;
3520 Expr *ToInitializer;
3521 if (auto Imp = importSeq(
3522 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(),
3523 D->getInnerLocStart(), D->getInClassInitializer()))
3524 std::tie(
3525 ToType, ToTInfo, ToBitWidth, ToInnerLocStart, ToInitializer) = *Imp;
3526 else
3527 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003528
Gabor Marton26f72a92018-07-12 09:42:05 +00003529 FieldDecl *ToField;
3530 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003531 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3532 ToType, ToTInfo, ToBitWidth, D->isMutable(),
3533 D->getInClassInitStyle()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003534 return ToField;
3535
Douglas Gregordd483172010-02-22 17:42:47 +00003536 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003537 ToField->setLexicalDeclContext(LexicalDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00003538 if (ToInitializer)
3539 ToField->setInClassInitializer(ToInitializer);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003540 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00003541 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003542 return ToField;
3543}
3544
Balazs Keri3b30d652018-10-19 13:32:20 +00003545ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003546 // Import the major distinguishing characteristics of a variable.
3547 DeclContext *DC, *LexicalDC;
3548 DeclarationName Name;
3549 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003550 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003551 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3552 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003553 if (ToD)
3554 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003555
Fangrui Song6907ce22018-07-30 19:24:48 +00003556 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003557 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003558 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003559 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003560 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003561 if (!Name &&
3562 ASTImporter::getFieldIndex(D) !=
3563 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003564 continue;
3565
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003566 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003567 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003568 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003569 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003570 return FoundField;
3571 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003572
3573 // If there are more anonymous fields to check, continue.
3574 if (!Name && I < N-1)
3575 continue;
3576
Balazs Keri3b30d652018-10-19 13:32:20 +00003577 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003578 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003579 << Name << D->getType() << FoundField->getType();
3580 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3581 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003582
3583 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003584 }
3585 }
3586
Francois Pichet783dd6e2010-11-21 06:08:52 +00003587 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003588 auto TypeOrErr = import(D->getType());
3589 if (!TypeOrErr)
3590 return TypeOrErr.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003591
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003592 auto **NamedChain =
3593 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00003594
3595 unsigned i = 0;
Balazs Keri3b30d652018-10-19 13:32:20 +00003596 for (auto *PI : D->chain())
3597 if (Expected<NamedDecl *> ToD = import(PI))
3598 NamedChain[i++] = *ToD;
3599 else
3600 return ToD.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003601
Gabor Marton26f72a92018-07-12 09:42:05 +00003602 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
3603 IndirectFieldDecl *ToIndirectField;
3604 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003605 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
Gabor Marton26f72a92018-07-12 09:42:05 +00003606 // FIXME here we leak `NamedChain` which is allocated before
3607 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00003608
Francois Pichet783dd6e2010-11-21 06:08:52 +00003609 ToIndirectField->setAccess(D->getAccess());
3610 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003611 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003612 return ToIndirectField;
3613}
3614
Balazs Keri3b30d652018-10-19 13:32:20 +00003615ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00003616 // Import the major distinguishing characteristics of a declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00003617 DeclContext *DC, *LexicalDC;
3618 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
3619 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003620
3621 // Determine whether we've already imported this decl.
Gabor Marton54058b52018-12-17 13:53:12 +00003622 // FriendDecl is not a NamedDecl so we cannot use lookup.
Aleksei Sidorina693b372016-09-28 10:16:56 +00003623 auto *RD = cast<CXXRecordDecl>(DC);
3624 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003625
3626 while (ImportedFriend) {
3627 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003628 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3629 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003630 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003631
3632 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3633 if (Importer.IsStructurallyEquivalent(
3634 D->getFriendType()->getType(),
3635 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003636 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003637 }
3638 ImportedFriend = ImportedFriend->getNextFriend();
3639 }
3640
3641 // Not found. Create it.
3642 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003643 if (NamedDecl *FriendD = D->getFriendDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003644 NamedDecl *ToFriendD;
3645 if (Error Err = importInto(ToFriendD, FriendD))
3646 return std::move(Err);
3647
3648 if (FriendD->getFriendObjectKind() != Decl::FOK_None &&
Peter Szecsib180eeb2018-04-25 17:28:03 +00003649 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3650 ToFriendD->setObjectOfFriendDecl(false);
3651
3652 ToFU = ToFriendD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003653 } else { // The friend is a type, not a decl.
3654 if (auto TSIOrErr = import(D->getFriendType()))
3655 ToFU = *TSIOrErr;
3656 else
3657 return TSIOrErr.takeError();
3658 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00003659
3660 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003661 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003662 for (unsigned I = 0; I < D->NumTPLists; I++) {
Balazs Keridec09162019-03-20 15:42:42 +00003663 if (auto ListOrErr = import(FromTPLists[I]))
Balazs Keri3b30d652018-10-19 13:32:20 +00003664 ToTPLists[I] = *ListOrErr;
3665 else
3666 return ListOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003667 }
3668
Balazs Keri3b30d652018-10-19 13:32:20 +00003669 auto LocationOrErr = import(D->getLocation());
3670 if (!LocationOrErr)
3671 return LocationOrErr.takeError();
3672 auto FriendLocOrErr = import(D->getFriendLoc());
3673 if (!FriendLocOrErr)
3674 return FriendLocOrErr.takeError();
3675
Gabor Marton26f72a92018-07-12 09:42:05 +00003676 FriendDecl *FrD;
3677 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003678 *LocationOrErr, ToFU,
3679 *FriendLocOrErr, ToTPLists))
Gabor Marton26f72a92018-07-12 09:42:05 +00003680 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003681
3682 FrD->setAccess(D->getAccess());
3683 FrD->setLexicalDeclContext(LexicalDC);
3684 LexicalDC->addDeclInternal(FrD);
3685 return FrD;
3686}
3687
Balazs Keri3b30d652018-10-19 13:32:20 +00003688ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003689 // Import the major distinguishing characteristics of an ivar.
3690 DeclContext *DC, *LexicalDC;
3691 DeclarationName Name;
3692 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003693 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003694 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3695 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003696 if (ToD)
3697 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003698
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003699 // Determine whether we've already imported this ivar
Gabor Marton54058b52018-12-17 13:53:12 +00003700 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003701 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003702 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003703 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003704 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003705 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003706 return FoundIvar;
3707 }
3708
Gabor Marton410f32c2019-04-01 15:29:55 +00003709 Importer.ToDiag(Loc, diag::warn_odr_ivar_type_inconsistent)
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003710 << Name << D->getType() << FoundIvar->getType();
3711 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3712 << FoundIvar->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003713
3714 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003715 }
3716 }
3717
Balazs Keri3b30d652018-10-19 13:32:20 +00003718 QualType ToType;
3719 TypeSourceInfo *ToTypeSourceInfo;
3720 Expr *ToBitWidth;
3721 SourceLocation ToInnerLocStart;
3722 if (auto Imp = importSeq(
3723 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(), D->getInnerLocStart()))
3724 std::tie(ToType, ToTypeSourceInfo, ToBitWidth, ToInnerLocStart) = *Imp;
3725 else
3726 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003727
Gabor Marton26f72a92018-07-12 09:42:05 +00003728 ObjCIvarDecl *ToIvar;
3729 if (GetImportedOrCreateDecl(
3730 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003731 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3732 ToType, ToTypeSourceInfo,
3733 D->getAccessControl(),ToBitWidth, D->getSynthesize()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003734 return ToIvar;
3735
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003736 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003737 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003738 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003739}
3740
Balazs Keri3b30d652018-10-19 13:32:20 +00003741ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003742
3743 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
3744 auto RedeclIt = Redecls.begin();
3745 // Import the first part of the decl chain. I.e. import all previous
3746 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003747 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3748 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3749 if (!RedeclOrErr)
3750 return RedeclOrErr.takeError();
3751 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003752 assert(*RedeclIt == D);
3753
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003754 // Import the major distinguishing characteristics of a variable.
3755 DeclContext *DC, *LexicalDC;
3756 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003757 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003758 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003759 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3760 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003761 if (ToD)
3762 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003763
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003764 // Try to find a variable in our own ("to") context with the same name and
3765 // in the same context as the variable we're importing.
Gabor Martonac3a5d62018-09-17 12:04:52 +00003766 VarDecl *FoundByLookup = nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003767 if (D->isFileVarDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003768 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003769 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00003770 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003771 for (auto *FoundDecl : FoundDecls) {
3772 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003773 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003774
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003775 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00003776 if (!hasSameVisibilityContext(FoundVar, D))
3777 continue;
3778 if (Importer.IsStructurallyEquivalent(D->getType(),
3779 FoundVar->getType())) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003780
Gabor Marton458d1452019-02-14 13:07:03 +00003781 // The VarDecl in the "From" context has a definition, but in the
3782 // "To" context we already have a definition.
3783 VarDecl *FoundDef = FoundVar->getDefinition();
3784 if (D->isThisDeclarationADefinition() && FoundDef)
3785 // FIXME Check for ODR error if the two definitions have
3786 // different initializers?
3787 return Importer.MapImported(D, FoundDef);
Gabor Martonac3a5d62018-09-17 12:04:52 +00003788
Gabor Marton458d1452019-02-14 13:07:03 +00003789 // The VarDecl in the "From" context has an initializer, but in the
3790 // "To" context we already have an initializer.
3791 const VarDecl *FoundDInit = nullptr;
3792 if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
3793 // FIXME Diagnose ODR error if the two initializers are different?
3794 return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit));
3795
3796 FoundByLookup = FoundVar;
3797 break;
3798 }
3799
3800 const ArrayType *FoundArray
3801 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3802 const ArrayType *TArray
3803 = Importer.getToContext().getAsArrayType(D->getType());
3804 if (FoundArray && TArray) {
3805 if (isa<IncompleteArrayType>(FoundArray) &&
3806 isa<ConstantArrayType>(TArray)) {
3807 // Import the type.
3808 if (auto TyOrErr = import(D->getType()))
3809 FoundVar->setType(*TyOrErr);
3810 else
3811 return TyOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00003812
3813 FoundByLookup = FoundVar;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003814 break;
Gabor Marton458d1452019-02-14 13:07:03 +00003815 } else if (isa<IncompleteArrayType>(TArray) &&
3816 isa<ConstantArrayType>(FoundArray)) {
3817 FoundByLookup = FoundVar;
3818 break;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003819 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003820 }
Gabor Marton458d1452019-02-14 13:07:03 +00003821
Gabor Marton410f32c2019-04-01 15:29:55 +00003822 Importer.ToDiag(Loc, diag::warn_odr_variable_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00003823 << Name << D->getType() << FoundVar->getType();
3824 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3825 << FoundVar->getType();
Gabor Martonf035b752019-08-27 11:36:10 +00003826 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003827 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003828 }
3829
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003830 if (!ConflictingDecls.empty()) {
Gabor Martonf035b752019-08-27 11:36:10 +00003831 ExpectedName NameOrErr = Importer.HandleNameConflict(
3832 Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size());
3833 if (NameOrErr)
3834 Name = NameOrErr.get();
3835 else
3836 return NameOrErr.takeError();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003837 }
3838 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003839
Balazs Keri3b30d652018-10-19 13:32:20 +00003840 QualType ToType;
3841 TypeSourceInfo *ToTypeSourceInfo;
3842 SourceLocation ToInnerLocStart;
3843 NestedNameSpecifierLoc ToQualifierLoc;
3844 if (auto Imp = importSeq(
3845 D->getType(), D->getTypeSourceInfo(), D->getInnerLocStart(),
3846 D->getQualifierLoc()))
3847 std::tie(ToType, ToTypeSourceInfo, ToInnerLocStart, ToQualifierLoc) = *Imp;
3848 else
3849 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003850
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003851 // Create the imported variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00003852 VarDecl *ToVar;
3853 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003854 ToInnerLocStart, Loc,
3855 Name.getAsIdentifierInfo(),
3856 ToType, ToTypeSourceInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003857 D->getStorageClass()))
3858 return ToVar;
3859
Balazs Keri3b30d652018-10-19 13:32:20 +00003860 ToVar->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003861 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003862 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003863
Gabor Martonac3a5d62018-09-17 12:04:52 +00003864 if (FoundByLookup) {
3865 auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
3866 ToVar->setPreviousDecl(Recent);
3867 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003868
Balazs Keri3b30d652018-10-19 13:32:20 +00003869 if (Error Err = ImportInitializer(D, ToVar))
3870 return std::move(Err);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003871
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003872 if (D->isConstexpr())
3873 ToVar->setConstexpr(true);
3874
Gabor Martonbc5b7e22019-12-04 17:12:08 +01003875 addDeclToContexts(D, ToVar);
Gabor Martonac3a5d62018-09-17 12:04:52 +00003876
3877 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003878 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3879 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3880 if (!RedeclOrErr)
3881 return RedeclOrErr.takeError();
3882 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003883
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003884 return ToVar;
3885}
3886
Balazs Keri3b30d652018-10-19 13:32:20 +00003887ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
Douglas Gregor8b228d72010-02-17 21:22:52 +00003888 // Parameters are created in the translation unit's context, then moved
3889 // into the function declaration's context afterward.
3890 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003891
Balazs Keri3b30d652018-10-19 13:32:20 +00003892 DeclarationName ToDeclName;
3893 SourceLocation ToLocation;
3894 QualType ToType;
3895 if (auto Imp = importSeq(D->getDeclName(), D->getLocation(), D->getType()))
3896 std::tie(ToDeclName, ToLocation, ToType) = *Imp;
3897 else
3898 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003899
Douglas Gregor8b228d72010-02-17 21:22:52 +00003900 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003901 ImplicitParamDecl *ToParm = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003902 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3903 ToLocation, ToDeclName.getAsIdentifierInfo(),
3904 ToType, D->getParameterKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003905 return ToParm;
3906 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003907}
3908
Balazs Keric5095942019-08-14 09:41:39 +00003909Error ASTNodeImporter::ImportDefaultArgOfParmVarDecl(
3910 const ParmVarDecl *FromParam, ParmVarDecl *ToParam) {
3911 ToParam->setHasInheritedDefaultArg(FromParam->hasInheritedDefaultArg());
3912 ToParam->setKNRPromoted(FromParam->isKNRPromoted());
3913
3914 if (FromParam->hasUninstantiatedDefaultArg()) {
3915 if (auto ToDefArgOrErr = import(FromParam->getUninstantiatedDefaultArg()))
3916 ToParam->setUninstantiatedDefaultArg(*ToDefArgOrErr);
3917 else
3918 return ToDefArgOrErr.takeError();
3919 } else if (FromParam->hasUnparsedDefaultArg()) {
3920 ToParam->setUnparsedDefaultArg();
3921 } else if (FromParam->hasDefaultArg()) {
3922 if (auto ToDefArgOrErr = import(FromParam->getDefaultArg()))
3923 ToParam->setDefaultArg(*ToDefArgOrErr);
3924 else
3925 return ToDefArgOrErr.takeError();
3926 }
3927
3928 return Error::success();
3929}
3930
Balazs Keri3b30d652018-10-19 13:32:20 +00003931ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003932 // Parameters are created in the translation unit's context, then moved
3933 // into the function declaration's context afterward.
3934 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003935
Balazs Keri3b30d652018-10-19 13:32:20 +00003936 DeclarationName ToDeclName;
3937 SourceLocation ToLocation, ToInnerLocStart;
3938 QualType ToType;
3939 TypeSourceInfo *ToTypeSourceInfo;
3940 if (auto Imp = importSeq(
3941 D->getDeclName(), D->getLocation(), D->getType(), D->getInnerLocStart(),
3942 D->getTypeSourceInfo()))
3943 std::tie(
3944 ToDeclName, ToLocation, ToType, ToInnerLocStart,
3945 ToTypeSourceInfo) = *Imp;
3946 else
3947 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003948
Gabor Marton26f72a92018-07-12 09:42:05 +00003949 ParmVarDecl *ToParm;
3950 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003951 ToInnerLocStart, ToLocation,
3952 ToDeclName.getAsIdentifierInfo(), ToType,
3953 ToTypeSourceInfo, D->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003954 /*DefaultArg*/ nullptr))
3955 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003956
Balazs Keric5095942019-08-14 09:41:39 +00003957 // Set the default argument. It should be no problem if it was already done.
3958 // Do not import the default expression before GetImportedOrCreateDecl call
3959 // to avoid possible infinite import loop because circular dependency.
3960 if (Error Err = ImportDefaultArgOfParmVarDecl(D, ToParm))
3961 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003962
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003963 if (D->isObjCMethodParameter()) {
3964 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3965 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3966 } else {
3967 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3968 D->getFunctionScopeIndex());
3969 }
3970
Gabor Marton26f72a92018-07-12 09:42:05 +00003971 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003972}
3973
Balazs Keri3b30d652018-10-19 13:32:20 +00003974ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003975 // Import the major distinguishing characteristics of a method.
3976 DeclContext *DC, *LexicalDC;
3977 DeclarationName Name;
3978 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003979 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003980 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3981 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003982 if (ToD)
3983 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003984
Gabor Marton54058b52018-12-17 13:53:12 +00003985 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003986 for (auto *FoundDecl : FoundDecls) {
3987 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003988 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3989 continue;
3990
3991 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003992 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3993 FoundMethod->getReturnType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003994 Importer.ToDiag(Loc, diag::warn_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003995 << D->isInstanceMethod() << Name << D->getReturnType()
3996 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003997 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003998 diag::note_odr_objc_method_here)
3999 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00004000
4001 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00004002 }
4003
4004 // Check the number of parameters.
4005 if (D->param_size() != FoundMethod->param_size()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00004006 Importer.ToDiag(Loc, diag::warn_odr_objc_method_num_params_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00004007 << D->isInstanceMethod() << Name
4008 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00004009 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00004010 diag::note_odr_objc_method_here)
4011 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00004012
4013 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00004014 }
4015
4016 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004017 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00004018 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
4019 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004020 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00004021 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004022 Importer.FromDiag((*P)->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004023 diag::warn_odr_objc_method_param_type_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00004024 << D->isInstanceMethod() << Name
4025 << (*P)->getType() << (*FoundP)->getType();
4026 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
4027 << (*FoundP)->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004028
4029 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00004030 }
4031 }
4032
4033 // Check variadic/non-variadic.
4034 // Check the number of parameters.
4035 if (D->isVariadic() != FoundMethod->isVariadic()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00004036 Importer.ToDiag(Loc, diag::warn_odr_objc_method_variadic_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00004037 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00004038 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00004039 diag::note_odr_objc_method_here)
4040 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00004041
4042 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00004043 }
4044
4045 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00004046 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00004047 }
4048 }
4049
Balazs Keri3b30d652018-10-19 13:32:20 +00004050 SourceLocation ToEndLoc;
4051 QualType ToReturnType;
4052 TypeSourceInfo *ToReturnTypeSourceInfo;
4053 if (auto Imp = importSeq(
4054 D->getEndLoc(), D->getReturnType(), D->getReturnTypeSourceInfo()))
4055 std::tie(ToEndLoc, ToReturnType, ToReturnTypeSourceInfo) = *Imp;
4056 else
4057 return Imp.takeError();
Douglas Gregor12852d92010-03-08 14:59:44 +00004058
Gabor Marton26f72a92018-07-12 09:42:05 +00004059 ObjCMethodDecl *ToMethod;
4060 if (GetImportedOrCreateDecl(
Adrian Prantl2073dd22019-11-04 14:28:14 -08004061 ToMethod, D, Importer.getToContext(), Loc, ToEndLoc,
4062 Name.getObjCSelector(), ToReturnType, ToReturnTypeSourceInfo, DC,
4063 D->isInstanceMethod(), D->isVariadic(), D->isPropertyAccessor(),
4064 D->isSynthesizedAccessorStub(), D->isImplicit(), D->isDefined(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004065 D->getImplementationControl(), D->hasRelatedResultType()))
4066 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00004067
4068 // FIXME: When we decide to merge method definitions, we'll need to
4069 // deal with implicit parameters.
4070
4071 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004072 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00004073 for (auto *FromP : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004074 if (Expected<ParmVarDecl *> ToPOrErr = import(FromP))
4075 ToParams.push_back(*ToPOrErr);
4076 else
4077 return ToPOrErr.takeError();
Douglas Gregor43f54792010-02-17 02:12:47 +00004078 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004079
Douglas Gregor43f54792010-02-17 02:12:47 +00004080 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004081 for (auto *ToParam : ToParams) {
4082 ToParam->setOwningFunction(ToMethod);
4083 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00004084 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00004085
Balazs Keri3b30d652018-10-19 13:32:20 +00004086 SmallVector<SourceLocation, 12> FromSelLocs;
4087 D->getSelectorLocs(FromSelLocs);
4088 SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size());
4089 if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs))
4090 return std::move(Err);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00004091
Balazs Keri3b30d652018-10-19 13:32:20 +00004092 ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00004093
4094 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004095 LexicalDC->addDeclInternal(ToMethod);
Raphael Isemann164e0fc2019-12-06 18:10:23 +01004096
4097 // Implicit params are declared when Sema encounters the definition but this
4098 // never happens when the method is imported. Manually declare the implicit
4099 // params now that the MethodDecl knows its class interface.
4100 if (D->getSelfDecl())
4101 ToMethod->createImplicitParams(Importer.getToContext(),
4102 ToMethod->getClassInterface());
4103
Douglas Gregor43f54792010-02-17 02:12:47 +00004104 return ToMethod;
4105}
4106
Balazs Keri3b30d652018-10-19 13:32:20 +00004107ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00004108 // Import the major distinguishing characteristics of a category.
4109 DeclContext *DC, *LexicalDC;
4110 DeclarationName Name;
4111 SourceLocation Loc;
4112 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004113 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4114 return std::move(Err);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004115 if (ToD)
4116 return ToD;
4117
Balazs Keri3b30d652018-10-19 13:32:20 +00004118 SourceLocation ToVarianceLoc, ToLocation, ToColonLoc;
4119 TypeSourceInfo *ToTypeSourceInfo;
4120 if (auto Imp = importSeq(
4121 D->getVarianceLoc(), D->getLocation(), D->getColonLoc(),
4122 D->getTypeSourceInfo()))
4123 std::tie(ToVarianceLoc, ToLocation, ToColonLoc, ToTypeSourceInfo) = *Imp;
4124 else
4125 return Imp.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004126
Gabor Marton26f72a92018-07-12 09:42:05 +00004127 ObjCTypeParamDecl *Result;
4128 if (GetImportedOrCreateDecl(
4129 Result, D, Importer.getToContext(), DC, D->getVariance(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004130 ToVarianceLoc, D->getIndex(),
4131 ToLocation, Name.getAsIdentifierInfo(),
4132 ToColonLoc, ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00004133 return Result;
4134
Douglas Gregor85f3f952015-07-07 03:57:15 +00004135 Result->setLexicalDeclContext(LexicalDC);
4136 return Result;
4137}
4138
Balazs Keri3b30d652018-10-19 13:32:20 +00004139ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00004140 // Import the major distinguishing characteristics of a category.
4141 DeclContext *DC, *LexicalDC;
4142 DeclarationName Name;
4143 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004144 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004145 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4146 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004147 if (ToD)
4148 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004149
Balazs Keri3b30d652018-10-19 13:32:20 +00004150 ObjCInterfaceDecl *ToInterface;
4151 if (Error Err = importInto(ToInterface, D->getClassInterface()))
4152 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004153
Douglas Gregor84c51c32010-02-18 01:47:50 +00004154 // Determine if we've already encountered this category.
4155 ObjCCategoryDecl *MergeWithCategory
4156 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
4157 ObjCCategoryDecl *ToCategory = MergeWithCategory;
4158 if (!ToCategory) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004159 SourceLocation ToAtStartLoc, ToCategoryNameLoc;
4160 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4161 if (auto Imp = importSeq(
4162 D->getAtStartLoc(), D->getCategoryNameLoc(),
4163 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4164 std::tie(
4165 ToAtStartLoc, ToCategoryNameLoc,
4166 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4167 else
4168 return Imp.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004169
4170 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004171 ToAtStartLoc, Loc,
4172 ToCategoryNameLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00004173 Name.getAsIdentifierInfo(), ToInterface,
4174 /*TypeParamList=*/nullptr,
Balazs Keri3b30d652018-10-19 13:32:20 +00004175 ToIvarLBraceLoc,
4176 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004177 return ToCategory;
4178
Douglas Gregor84c51c32010-02-18 01:47:50 +00004179 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004180 LexicalDC->addDeclInternal(ToCategory);
Balazs Keri3b30d652018-10-19 13:32:20 +00004181 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004182 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004183 if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList()))
4184 ToCategory->setTypeParamList(*PListOrErr);
4185 else
4186 return PListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004187
Douglas Gregor84c51c32010-02-18 01:47:50 +00004188 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004189 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4190 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00004191 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
4192 = D->protocol_loc_begin();
4193 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
4194 FromProtoEnd = D->protocol_end();
4195 FromProto != FromProtoEnd;
4196 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004197 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4198 Protocols.push_back(*ToProtoOrErr);
4199 else
4200 return ToProtoOrErr.takeError();
4201
4202 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4203 ProtocolLocs.push_back(*ToProtoLocOrErr);
4204 else
4205 return ToProtoLocOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004206 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004207
Douglas Gregor84c51c32010-02-18 01:47:50 +00004208 // FIXME: If we're merging, make sure that the protocol list is the same.
4209 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
4210 ProtocolLocs.data(), Importer.getToContext());
Balazs Keri3b30d652018-10-19 13:32:20 +00004211
Douglas Gregor84c51c32010-02-18 01:47:50 +00004212 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004213 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00004214 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004215
Douglas Gregor84c51c32010-02-18 01:47:50 +00004216 // Import all of the members of this category.
Balazs Keri3b30d652018-10-19 13:32:20 +00004217 if (Error Err = ImportDeclContext(D))
4218 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00004219
Douglas Gregor84c51c32010-02-18 01:47:50 +00004220 // If we have an implementation, import it as well.
4221 if (D->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004222 if (Expected<ObjCCategoryImplDecl *> ToImplOrErr =
4223 import(D->getImplementation()))
4224 ToCategory->setImplementation(*ToImplOrErr);
4225 else
4226 return ToImplOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004227 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004228
Douglas Gregor84c51c32010-02-18 01:47:50 +00004229 return ToCategory;
4230}
4231
Balazs Keri3b30d652018-10-19 13:32:20 +00004232Error ASTNodeImporter::ImportDefinition(
4233 ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004234 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00004235 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004236 if (Error Err = ImportDeclContext(From))
4237 return Err;
4238 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004239 }
4240
4241 // Start the protocol definition
4242 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004243
Douglas Gregor2aa53772012-01-24 17:42:07 +00004244 // Import protocols
4245 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4246 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004247 ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc =
4248 From->protocol_loc_begin();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004249 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
4250 FromProtoEnd = From->protocol_end();
4251 FromProto != FromProtoEnd;
4252 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004253 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4254 Protocols.push_back(*ToProtoOrErr);
4255 else
4256 return ToProtoOrErr.takeError();
4257
4258 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4259 ProtocolLocs.push_back(*ToProtoLocOrErr);
4260 else
4261 return ToProtoLocOrErr.takeError();
4262
Douglas Gregor2aa53772012-01-24 17:42:07 +00004263 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004264
Douglas Gregor2aa53772012-01-24 17:42:07 +00004265 // FIXME: If we're merging, make sure that the protocol list is the same.
4266 To->setProtocolList(Protocols.data(), Protocols.size(),
4267 ProtocolLocs.data(), Importer.getToContext());
4268
Douglas Gregor2e15c842012-02-01 21:00:38 +00004269 if (shouldForceImportDeclContext(Kind)) {
4270 // Import all of the members of this protocol.
Balazs Keri3b30d652018-10-19 13:32:20 +00004271 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4272 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004273 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004274 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004275}
4276
Balazs Keri3b30d652018-10-19 13:32:20 +00004277ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004278 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00004279 // from, but this particular declaration is not that definition, import the
4280 // definition and map to that.
4281 ObjCProtocolDecl *Definition = D->getDefinition();
4282 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004283 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4284 return Importer.MapImported(D, *ImportedDefOrErr);
4285 else
4286 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004287 }
4288
Douglas Gregor84c51c32010-02-18 01:47:50 +00004289 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004290 DeclContext *DC, *LexicalDC;
4291 DeclarationName Name;
4292 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004293 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004294 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4295 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004296 if (ToD)
4297 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004298
Craig Topper36250ad2014-05-12 05:36:57 +00004299 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004300 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004301 for (auto *FoundDecl : FoundDecls) {
4302 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004303 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004304
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004305 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004306 break;
4307 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004308
Douglas Gregor98d156a2010-02-17 16:12:00 +00004309 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004310 if (!ToProto) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004311 auto ToAtBeginLocOrErr = import(D->getAtStartLoc());
4312 if (!ToAtBeginLocOrErr)
4313 return ToAtBeginLocOrErr.takeError();
4314
Gabor Marton26f72a92018-07-12 09:42:05 +00004315 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
4316 Name.getAsIdentifierInfo(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004317 *ToAtBeginLocOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004318 /*PrevDecl=*/nullptr))
4319 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004320 ToProto->setLexicalDeclContext(LexicalDC);
4321 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004322 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004323
4324 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004325
Balazs Keri3b30d652018-10-19 13:32:20 +00004326 if (D->isThisDeclarationADefinition())
4327 if (Error Err = ImportDefinition(D, ToProto))
4328 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004329
Douglas Gregor98d156a2010-02-17 16:12:00 +00004330 return ToProto;
4331}
4332
Balazs Keri3b30d652018-10-19 13:32:20 +00004333ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4334 DeclContext *DC, *LexicalDC;
4335 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4336 return std::move(Err);
Sean Callanan0aae0412014-12-10 00:00:37 +00004337
Balazs Keri3b30d652018-10-19 13:32:20 +00004338 ExpectedSLoc ExternLocOrErr = import(D->getExternLoc());
4339 if (!ExternLocOrErr)
4340 return ExternLocOrErr.takeError();
4341
4342 ExpectedSLoc LangLocOrErr = import(D->getLocation());
4343 if (!LangLocOrErr)
4344 return LangLocOrErr.takeError();
Sean Callanan0aae0412014-12-10 00:00:37 +00004345
4346 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00004347
4348 LinkageSpecDecl *ToLinkageSpec;
4349 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004350 *ExternLocOrErr, *LangLocOrErr,
4351 D->getLanguage(), HasBraces))
Gabor Marton26f72a92018-07-12 09:42:05 +00004352 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00004353
4354 if (HasBraces) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004355 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
4356 if (!RBraceLocOrErr)
4357 return RBraceLocOrErr.takeError();
4358 ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr);
Sean Callanan0aae0412014-12-10 00:00:37 +00004359 }
4360
4361 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4362 LexicalDC->addDeclInternal(ToLinkageSpec);
4363
Sean Callanan0aae0412014-12-10 00:00:37 +00004364 return ToLinkageSpec;
4365}
4366
Balazs Keri3b30d652018-10-19 13:32:20 +00004367ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004368 DeclContext *DC, *LexicalDC;
4369 DeclarationName Name;
4370 SourceLocation Loc;
4371 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004372 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4373 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004374 if (ToD)
4375 return ToD;
4376
Balazs Keri3b30d652018-10-19 13:32:20 +00004377 SourceLocation ToLoc, ToUsingLoc;
4378 NestedNameSpecifierLoc ToQualifierLoc;
4379 if (auto Imp = importSeq(
4380 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc()))
4381 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc) = *Imp;
4382 else
4383 return Imp.takeError();
4384
4385 DeclarationNameInfo NameInfo(Name, ToLoc);
4386 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4387 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004388
Gabor Marton26f72a92018-07-12 09:42:05 +00004389 UsingDecl *ToUsing;
4390 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004391 ToUsingLoc, ToQualifierLoc, NameInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00004392 D->hasTypename()))
4393 return ToUsing;
4394
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004395 ToUsing->setLexicalDeclContext(LexicalDC);
4396 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004397
4398 if (NamedDecl *FromPattern =
4399 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004400 if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern))
4401 Importer.getToContext().setInstantiatedFromUsingDecl(
4402 ToUsing, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004403 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004404 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004405 }
4406
Balazs Keri3b30d652018-10-19 13:32:20 +00004407 for (UsingShadowDecl *FromShadow : D->shadows()) {
4408 if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow))
4409 ToUsing->addShadowDecl(*ToShadowOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004410 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004411 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004412 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004413 return ToShadowOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004414 }
4415 return ToUsing;
4416}
4417
Balazs Keri3b30d652018-10-19 13:32:20 +00004418ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004419 DeclContext *DC, *LexicalDC;
4420 DeclarationName Name;
4421 SourceLocation Loc;
4422 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004423 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4424 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004425 if (ToD)
4426 return ToD;
4427
Balazs Keri3b30d652018-10-19 13:32:20 +00004428 Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl());
4429 if (!ToUsingOrErr)
4430 return ToUsingOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004431
Balazs Keri3b30d652018-10-19 13:32:20 +00004432 Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl());
4433 if (!ToTargetOrErr)
4434 return ToTargetOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004435
Gabor Marton26f72a92018-07-12 09:42:05 +00004436 UsingShadowDecl *ToShadow;
4437 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004438 *ToUsingOrErr, *ToTargetOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004439 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004440
4441 ToShadow->setLexicalDeclContext(LexicalDC);
4442 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004443
4444 if (UsingShadowDecl *FromPattern =
4445 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004446 if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern))
4447 Importer.getToContext().setInstantiatedFromUsingShadowDecl(
4448 ToShadow, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004449 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004450 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004451 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004452 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004453 }
4454
4455 LexicalDC->addDeclInternal(ToShadow);
4456
4457 return ToShadow;
4458}
4459
Balazs Keri3b30d652018-10-19 13:32:20 +00004460ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004461 DeclContext *DC, *LexicalDC;
4462 DeclarationName Name;
4463 SourceLocation Loc;
4464 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004465 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4466 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004467 if (ToD)
4468 return ToD;
4469
Balazs Keri3b30d652018-10-19 13:32:20 +00004470 auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor());
4471 if (!ToComAncestorOrErr)
4472 return ToComAncestorOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004473
Balazs Keri3b30d652018-10-19 13:32:20 +00004474 NamespaceDecl *ToNominatedNamespace;
4475 SourceLocation ToUsingLoc, ToNamespaceKeyLocation, ToIdentLocation;
4476 NestedNameSpecifierLoc ToQualifierLoc;
4477 if (auto Imp = importSeq(
4478 D->getNominatedNamespace(), D->getUsingLoc(),
4479 D->getNamespaceKeyLocation(), D->getQualifierLoc(),
4480 D->getIdentLocation()))
4481 std::tie(
4482 ToNominatedNamespace, ToUsingLoc, ToNamespaceKeyLocation,
4483 ToQualifierLoc, ToIdentLocation) = *Imp;
4484 else
4485 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004486
Gabor Marton26f72a92018-07-12 09:42:05 +00004487 UsingDirectiveDecl *ToUsingDir;
4488 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004489 ToUsingLoc,
4490 ToNamespaceKeyLocation,
4491 ToQualifierLoc,
4492 ToIdentLocation,
4493 ToNominatedNamespace, *ToComAncestorOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004494 return ToUsingDir;
4495
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004496 ToUsingDir->setLexicalDeclContext(LexicalDC);
4497 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004498
4499 return ToUsingDir;
4500}
4501
Balazs Keri3b30d652018-10-19 13:32:20 +00004502ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004503 UnresolvedUsingValueDecl *D) {
4504 DeclContext *DC, *LexicalDC;
4505 DeclarationName Name;
4506 SourceLocation Loc;
4507 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004508 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4509 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004510 if (ToD)
4511 return ToD;
4512
Balazs Keri3b30d652018-10-19 13:32:20 +00004513 SourceLocation ToLoc, ToUsingLoc, ToEllipsisLoc;
4514 NestedNameSpecifierLoc ToQualifierLoc;
4515 if (auto Imp = importSeq(
4516 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc(),
4517 D->getEllipsisLoc()))
4518 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4519 else
4520 return Imp.takeError();
4521
4522 DeclarationNameInfo NameInfo(Name, ToLoc);
4523 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4524 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004525
Gabor Marton26f72a92018-07-12 09:42:05 +00004526 UnresolvedUsingValueDecl *ToUsingValue;
4527 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004528 ToUsingLoc, ToQualifierLoc, NameInfo,
4529 ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004530 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004531
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004532 ToUsingValue->setAccess(D->getAccess());
4533 ToUsingValue->setLexicalDeclContext(LexicalDC);
4534 LexicalDC->addDeclInternal(ToUsingValue);
4535
4536 return ToUsingValue;
4537}
4538
Balazs Keri3b30d652018-10-19 13:32:20 +00004539ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004540 UnresolvedUsingTypenameDecl *D) {
4541 DeclContext *DC, *LexicalDC;
4542 DeclarationName Name;
4543 SourceLocation Loc;
4544 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004545 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4546 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004547 if (ToD)
4548 return ToD;
4549
Balazs Keri3b30d652018-10-19 13:32:20 +00004550 SourceLocation ToUsingLoc, ToTypenameLoc, ToEllipsisLoc;
4551 NestedNameSpecifierLoc ToQualifierLoc;
4552 if (auto Imp = importSeq(
4553 D->getUsingLoc(), D->getTypenameLoc(), D->getQualifierLoc(),
4554 D->getEllipsisLoc()))
4555 std::tie(ToUsingLoc, ToTypenameLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4556 else
4557 return Imp.takeError();
4558
Gabor Marton26f72a92018-07-12 09:42:05 +00004559 UnresolvedUsingTypenameDecl *ToUsing;
4560 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004561 ToUsingLoc, ToTypenameLoc,
4562 ToQualifierLoc, Loc, Name, ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004563 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004564
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004565 ToUsing->setAccess(D->getAccess());
4566 ToUsing->setLexicalDeclContext(LexicalDC);
4567 LexicalDC->addDeclInternal(ToUsing);
4568
4569 return ToUsing;
4570}
4571
Raphael Isemannba7bde62019-10-30 14:50:35 +01004572ExpectedDecl ASTNodeImporter::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
4573 Decl* ToD = nullptr;
4574 switch (D->getBuiltinTemplateKind()) {
4575 case BuiltinTemplateKind::BTK__make_integer_seq:
4576 ToD = Importer.getToContext().getMakeIntegerSeqDecl();
4577 break;
4578 case BuiltinTemplateKind::BTK__type_pack_element:
4579 ToD = Importer.getToContext().getTypePackElementDecl();
4580 break;
4581 }
4582 assert(ToD && "BuiltinTemplateDecl of unsupported kind!");
4583 Importer.MapImported(D, ToD);
4584 return ToD;
4585}
Balazs Keri3b30d652018-10-19 13:32:20 +00004586
4587Error ASTNodeImporter::ImportDefinition(
4588 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004589 if (To->getDefinition()) {
4590 // Check consistency of superclass.
4591 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4592 if (FromSuper) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004593 if (auto FromSuperOrErr = import(FromSuper))
4594 FromSuper = *FromSuperOrErr;
4595 else
4596 return FromSuperOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004597 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004598
4599 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004600 if ((bool)FromSuper != (bool)ToSuper ||
4601 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004602 Importer.ToDiag(To->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004603 diag::warn_odr_objc_superclass_inconsistent)
Douglas Gregor2aa53772012-01-24 17:42:07 +00004604 << To->getDeclName();
4605 if (ToSuper)
4606 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4607 << To->getSuperClass()->getDeclName();
4608 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004609 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004610 diag::note_odr_objc_missing_superclass);
4611 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00004612 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004613 diag::note_odr_objc_superclass)
4614 << From->getSuperClass()->getDeclName();
4615 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004616 Importer.FromDiag(From->getLocation(),
4617 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004618 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004619
Douglas Gregor2e15c842012-02-01 21:00:38 +00004620 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004621 if (Error Err = ImportDeclContext(From))
4622 return Err;
4623 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004624 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004625
Douglas Gregor2aa53772012-01-24 17:42:07 +00004626 // Start the definition.
4627 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004628
Douglas Gregor2aa53772012-01-24 17:42:07 +00004629 // If this class has a superclass, import it.
4630 if (From->getSuperClass()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004631 if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo()))
4632 To->setSuperClass(*SuperTInfoOrErr);
4633 else
4634 return SuperTInfoOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004635 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004636
Douglas Gregor2aa53772012-01-24 17:42:07 +00004637 // Import protocols
4638 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4639 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004640 ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc =
4641 From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00004642
Douglas Gregor2aa53772012-01-24 17:42:07 +00004643 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4644 FromProtoEnd = From->protocol_end();
4645 FromProto != FromProtoEnd;
4646 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004647 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4648 Protocols.push_back(*ToProtoOrErr);
4649 else
4650 return ToProtoOrErr.takeError();
4651
4652 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4653 ProtocolLocs.push_back(*ToProtoLocOrErr);
4654 else
4655 return ToProtoLocOrErr.takeError();
4656
Douglas Gregor2aa53772012-01-24 17:42:07 +00004657 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004658
Douglas Gregor2aa53772012-01-24 17:42:07 +00004659 // FIXME: If we're merging, make sure that the protocol list is the same.
4660 To->setProtocolList(Protocols.data(), Protocols.size(),
4661 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00004662
Douglas Gregor2aa53772012-01-24 17:42:07 +00004663 // Import categories. When the categories themselves are imported, they'll
4664 // hook themselves into this interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004665 for (auto *Cat : From->known_categories()) {
4666 auto ToCatOrErr = import(Cat);
4667 if (!ToCatOrErr)
4668 return ToCatOrErr.takeError();
4669 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004670
Douglas Gregor2aa53772012-01-24 17:42:07 +00004671 // If we have an @implementation, import it as well.
4672 if (From->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004673 if (Expected<ObjCImplementationDecl *> ToImplOrErr =
4674 import(From->getImplementation()))
4675 To->setImplementation(*ToImplOrErr);
4676 else
4677 return ToImplOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004678 }
4679
Douglas Gregor2e15c842012-02-01 21:00:38 +00004680 if (shouldForceImportDeclContext(Kind)) {
4681 // Import all of the members of this class.
Balazs Keri3b30d652018-10-19 13:32:20 +00004682 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4683 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004684 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004685 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004686}
4687
Balazs Keri3b30d652018-10-19 13:32:20 +00004688Expected<ObjCTypeParamList *>
Douglas Gregor85f3f952015-07-07 03:57:15 +00004689ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4690 if (!list)
4691 return nullptr;
4692
4693 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
Balazs Keri3b30d652018-10-19 13:32:20 +00004694 for (auto *fromTypeParam : *list) {
4695 if (auto toTypeParamOrErr = import(fromTypeParam))
4696 toTypeParams.push_back(*toTypeParamOrErr);
4697 else
4698 return toTypeParamOrErr.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004699 }
4700
Balazs Keri3b30d652018-10-19 13:32:20 +00004701 auto LAngleLocOrErr = import(list->getLAngleLoc());
4702 if (!LAngleLocOrErr)
4703 return LAngleLocOrErr.takeError();
4704
4705 auto RAngleLocOrErr = import(list->getRAngleLoc());
4706 if (!RAngleLocOrErr)
4707 return RAngleLocOrErr.takeError();
4708
Douglas Gregor85f3f952015-07-07 03:57:15 +00004709 return ObjCTypeParamList::create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004710 *LAngleLocOrErr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00004711 toTypeParams,
Balazs Keri3b30d652018-10-19 13:32:20 +00004712 *RAngleLocOrErr);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004713}
4714
Balazs Keri3b30d652018-10-19 13:32:20 +00004715ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004716 // If this class has a definition in the translation unit we're coming from,
4717 // but this particular declaration is not that definition, import the
4718 // definition and map to that.
4719 ObjCInterfaceDecl *Definition = D->getDefinition();
4720 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004721 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4722 return Importer.MapImported(D, *ImportedDefOrErr);
4723 else
4724 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004725 }
4726
Douglas Gregor45635322010-02-16 01:20:57 +00004727 // Import the major distinguishing characteristics of an @interface.
4728 DeclContext *DC, *LexicalDC;
4729 DeclarationName Name;
4730 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004731 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004732 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4733 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004734 if (ToD)
4735 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004736
Douglas Gregor2aa53772012-01-24 17:42:07 +00004737 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004738 ObjCInterfaceDecl *MergeWithIface = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004739 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004740 for (auto *FoundDecl : FoundDecls) {
4741 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004742 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004743
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004744 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004745 break;
4746 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004747
Douglas Gregor2aa53772012-01-24 17:42:07 +00004748 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004749 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004750 if (!ToIface) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004751 ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc());
4752 if (!AtBeginLocOrErr)
4753 return AtBeginLocOrErr.takeError();
4754
Gabor Marton26f72a92018-07-12 09:42:05 +00004755 if (GetImportedOrCreateDecl(
4756 ToIface, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004757 *AtBeginLocOrErr, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004758 /*TypeParamList=*/nullptr,
4759 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4760 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004761 ToIface->setLexicalDeclContext(LexicalDC);
4762 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004763 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004764 Importer.MapImported(D, ToIface);
Balazs Keri3b30d652018-10-19 13:32:20 +00004765 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004766 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004767 if (auto ToPListOrErr =
4768 ImportObjCTypeParamList(D->getTypeParamListAsWritten()))
4769 ToIface->setTypeParamList(*ToPListOrErr);
4770 else
4771 return ToPListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004772
Balazs Keri3b30d652018-10-19 13:32:20 +00004773 if (D->isThisDeclarationADefinition())
4774 if (Error Err = ImportDefinition(D, ToIface))
4775 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004776
Douglas Gregor98d156a2010-02-17 16:12:00 +00004777 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004778}
4779
Balazs Keri3b30d652018-10-19 13:32:20 +00004780ExpectedDecl
4781ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4782 ObjCCategoryDecl *Category;
4783 if (Error Err = importInto(Category, D->getCategoryDecl()))
4784 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004785
Douglas Gregor4da9d682010-12-07 15:32:12 +00004786 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4787 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004788 DeclContext *DC, *LexicalDC;
4789 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4790 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004791
Balazs Keri3b30d652018-10-19 13:32:20 +00004792 SourceLocation ToLocation, ToAtStartLoc, ToCategoryNameLoc;
4793 if (auto Imp = importSeq(
4794 D->getLocation(), D->getAtStartLoc(), D->getCategoryNameLoc()))
4795 std::tie(ToLocation, ToAtStartLoc, ToCategoryNameLoc) = *Imp;
4796 else
4797 return Imp.takeError();
4798
Gabor Marton26f72a92018-07-12 09:42:05 +00004799 if (GetImportedOrCreateDecl(
4800 ToImpl, D, Importer.getToContext(), DC,
4801 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004802 ToLocation, ToAtStartLoc, ToCategoryNameLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004803 return ToImpl;
4804
Balazs Keri3b30d652018-10-19 13:32:20 +00004805 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004806 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004807 Category->setImplementation(ToImpl);
4808 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004809
Gabor Marton26f72a92018-07-12 09:42:05 +00004810 Importer.MapImported(D, ToImpl);
Balazs Keri3b30d652018-10-19 13:32:20 +00004811 if (Error Err = ImportDeclContext(D))
4812 return std::move(Err);
4813
Douglas Gregor4da9d682010-12-07 15:32:12 +00004814 return ToImpl;
4815}
4816
Balazs Keri3b30d652018-10-19 13:32:20 +00004817ExpectedDecl
4818ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00004819 // Find the corresponding interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004820 ObjCInterfaceDecl *Iface;
4821 if (Error Err = importInto(Iface, D->getClassInterface()))
4822 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004823
4824 // Import the superclass, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00004825 ObjCInterfaceDecl *Super;
4826 if (Error Err = importInto(Super, D->getSuperClass()))
4827 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004828
4829 ObjCImplementationDecl *Impl = Iface->getImplementation();
4830 if (!Impl) {
4831 // We haven't imported an implementation yet. Create a new @implementation
4832 // now.
Balazs Keri3b30d652018-10-19 13:32:20 +00004833 DeclContext *DC, *LexicalDC;
4834 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4835 return std::move(Err);
4836
4837 SourceLocation ToLocation, ToAtStartLoc, ToSuperClassLoc;
4838 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4839 if (auto Imp = importSeq(
4840 D->getLocation(), D->getAtStartLoc(), D->getSuperClassLoc(),
4841 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4842 std::tie(
4843 ToLocation, ToAtStartLoc, ToSuperClassLoc,
4844 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4845 else
4846 return Imp.takeError();
4847
Gabor Marton26f72a92018-07-12 09:42:05 +00004848 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004849 DC, Iface, Super,
4850 ToLocation,
4851 ToAtStartLoc,
4852 ToSuperClassLoc,
4853 ToIvarLBraceLoc,
4854 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004855 return Impl;
4856
Balazs Keri3b30d652018-10-19 13:32:20 +00004857 Impl->setLexicalDeclContext(LexicalDC);
Gabor Marton26f72a92018-07-12 09:42:05 +00004858
Douglas Gregorda8025c2010-12-07 01:26:03 +00004859 // Associate the implementation with the class it implements.
4860 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004861 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004862 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004863 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004864
4865 // Verify that the existing @implementation has the same superclass.
4866 if ((Super && !Impl->getSuperClass()) ||
4867 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004868 (Super && Impl->getSuperClass() &&
4869 !declaresSameEntity(Super->getCanonicalDecl(),
4870 Impl->getSuperClass()))) {
4871 Importer.ToDiag(Impl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004872 diag::warn_odr_objc_superclass_inconsistent)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004873 << Iface->getDeclName();
4874 // FIXME: It would be nice to have the location of the superclass
4875 // below.
4876 if (Impl->getSuperClass())
4877 Importer.ToDiag(Impl->getLocation(),
4878 diag::note_odr_objc_superclass)
4879 << Impl->getSuperClass()->getDeclName();
4880 else
4881 Importer.ToDiag(Impl->getLocation(),
4882 diag::note_odr_objc_missing_superclass);
4883 if (D->getSuperClass())
4884 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004885 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004886 << D->getSuperClass()->getDeclName();
4887 else
4888 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004889 diag::note_odr_objc_missing_superclass);
Balazs Keri3b30d652018-10-19 13:32:20 +00004890
4891 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004892 }
4893 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004894
Douglas Gregorda8025c2010-12-07 01:26:03 +00004895 // Import all of the members of this @implementation.
Balazs Keri3b30d652018-10-19 13:32:20 +00004896 if (Error Err = ImportDeclContext(D))
4897 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004898
4899 return Impl;
4900}
4901
Balazs Keri3b30d652018-10-19 13:32:20 +00004902ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004903 // Import the major distinguishing characteristics of an @property.
4904 DeclContext *DC, *LexicalDC;
4905 DeclarationName Name;
4906 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004907 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004908 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4909 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004910 if (ToD)
4911 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004912
4913 // Check whether we have already imported this property.
Gabor Marton54058b52018-12-17 13:53:12 +00004914 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004915 for (auto *FoundDecl : FoundDecls) {
4916 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004917 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004918 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004919 FoundProp->getType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00004920 Importer.ToDiag(Loc, diag::warn_odr_objc_property_type_inconsistent)
Douglas Gregora11c4582010-02-17 18:02:10 +00004921 << Name << D->getType() << FoundProp->getType();
4922 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4923 << FoundProp->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004924
4925 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora11c4582010-02-17 18:02:10 +00004926 }
4927
4928 // FIXME: Check property attributes, getters, setters, etc.?
4929
4930 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004931 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004932 return FoundProp;
4933 }
4934 }
4935
Balazs Keri3b30d652018-10-19 13:32:20 +00004936 QualType ToType;
4937 TypeSourceInfo *ToTypeSourceInfo;
4938 SourceLocation ToAtLoc, ToLParenLoc;
4939 if (auto Imp = importSeq(
4940 D->getType(), D->getTypeSourceInfo(), D->getAtLoc(), D->getLParenLoc()))
4941 std::tie(ToType, ToTypeSourceInfo, ToAtLoc, ToLParenLoc) = *Imp;
4942 else
4943 return Imp.takeError();
Douglas Gregora11c4582010-02-17 18:02:10 +00004944
4945 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004946 ObjCPropertyDecl *ToProperty;
4947 if (GetImportedOrCreateDecl(
4948 ToProperty, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004949 Name.getAsIdentifierInfo(), ToAtLoc,
4950 ToLParenLoc, ToType,
4951 ToTypeSourceInfo, D->getPropertyImplementation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00004952 return ToProperty;
4953
Balazs Keri3b30d652018-10-19 13:32:20 +00004954 Selector ToGetterName, ToSetterName;
4955 SourceLocation ToGetterNameLoc, ToSetterNameLoc;
4956 ObjCMethodDecl *ToGetterMethodDecl, *ToSetterMethodDecl;
4957 ObjCIvarDecl *ToPropertyIvarDecl;
4958 if (auto Imp = importSeq(
4959 D->getGetterName(), D->getSetterName(),
4960 D->getGetterNameLoc(), D->getSetterNameLoc(),
4961 D->getGetterMethodDecl(), D->getSetterMethodDecl(),
4962 D->getPropertyIvarDecl()))
4963 std::tie(
4964 ToGetterName, ToSetterName,
4965 ToGetterNameLoc, ToSetterNameLoc,
4966 ToGetterMethodDecl, ToSetterMethodDecl,
4967 ToPropertyIvarDecl) = *Imp;
4968 else
4969 return Imp.takeError();
4970
Douglas Gregora11c4582010-02-17 18:02:10 +00004971 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004972 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004973
4974 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004975 ToProperty->setPropertyAttributesAsWritten(
4976 D->getPropertyAttributesAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00004977 ToProperty->setGetterName(ToGetterName, ToGetterNameLoc);
4978 ToProperty->setSetterName(ToSetterName, ToSetterNameLoc);
4979 ToProperty->setGetterMethodDecl(ToGetterMethodDecl);
4980 ToProperty->setSetterMethodDecl(ToSetterMethodDecl);
4981 ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl);
Douglas Gregora11c4582010-02-17 18:02:10 +00004982 return ToProperty;
4983}
4984
Balazs Keri3b30d652018-10-19 13:32:20 +00004985ExpectedDecl
4986ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4987 ObjCPropertyDecl *Property;
4988 if (Error Err = importInto(Property, D->getPropertyDecl()))
4989 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004990
Balazs Keri3b30d652018-10-19 13:32:20 +00004991 DeclContext *DC, *LexicalDC;
4992 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4993 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004994
Balazs Keri3b30d652018-10-19 13:32:20 +00004995 auto *InImpl = cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004996
4997 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004998 ObjCIvarDecl *Ivar = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004999 if (Error Err = importInto(Ivar, D->getPropertyIvarDecl()))
5000 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00005001
5002 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00005003 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
5004 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00005005 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005006 SourceLocation ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc;
5007 if (auto Imp = importSeq(
5008 D->getBeginLoc(), D->getLocation(), D->getPropertyIvarDeclLoc()))
5009 std::tie(ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc) = *Imp;
5010 else
5011 return Imp.takeError();
5012
Gabor Marton26f72a92018-07-12 09:42:05 +00005013 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005014 ToBeginLoc,
5015 ToLocation, Property,
Gabor Marton26f72a92018-07-12 09:42:05 +00005016 D->getPropertyImplementation(), Ivar,
Balazs Keri3b30d652018-10-19 13:32:20 +00005017 ToPropertyIvarDeclLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00005018 return ToImpl;
5019
Douglas Gregor14a49e22010-12-07 18:32:03 +00005020 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00005021 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00005022 } else {
5023 // Check that we have the same kind of property implementation (@synthesize
5024 // vs. @dynamic).
5025 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00005026 Importer.ToDiag(ToImpl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00005027 diag::warn_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00005028 << Property->getDeclName()
5029 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00005030 == ObjCPropertyImplDecl::Dynamic);
5031 Importer.FromDiag(D->getLocation(),
5032 diag::note_odr_objc_property_impl_kind)
5033 << D->getPropertyDecl()->getDeclName()
5034 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Balazs Keri3b30d652018-10-19 13:32:20 +00005035
5036 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00005037 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005038
5039 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00005040 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
5041 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00005042 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Gabor Marton410f32c2019-04-01 15:29:55 +00005043 diag::warn_odr_objc_synthesize_ivar_inconsistent)
Douglas Gregor14a49e22010-12-07 18:32:03 +00005044 << Property->getDeclName()
5045 << ToImpl->getPropertyIvarDecl()->getDeclName()
5046 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00005047 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00005048 diag::note_odr_objc_synthesize_ivar_here)
5049 << D->getPropertyIvarDecl()->getDeclName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005050
5051 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00005052 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005053
Douglas Gregor14a49e22010-12-07 18:32:03 +00005054 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00005055 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00005056 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005057
Douglas Gregor14a49e22010-12-07 18:32:03 +00005058 return ToImpl;
5059}
5060
Balazs Keri3b30d652018-10-19 13:32:20 +00005061ExpectedDecl
5062ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregora082a492010-11-30 19:14:50 +00005063 // For template arguments, we adopt the translation unit as our declaration
5064 // context. This context will be fixed when the actual template declaration
5065 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00005066
Douglas Gregora082a492010-11-30 19:14:50 +00005067 // FIXME: Import default argument.
Balazs Keri3b30d652018-10-19 13:32:20 +00005068
5069 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5070 if (!BeginLocOrErr)
5071 return BeginLocOrErr.takeError();
5072
5073 ExpectedSLoc LocationOrErr = import(D->getLocation());
5074 if (!LocationOrErr)
5075 return LocationOrErr.takeError();
5076
Gabor Marton26f72a92018-07-12 09:42:05 +00005077 TemplateTypeParmDecl *ToD = nullptr;
5078 (void)GetImportedOrCreateDecl(
5079 ToD, D, Importer.getToContext(),
5080 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005081 *BeginLocOrErr, *LocationOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005082 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
5083 D->wasDeclaredWithTypename(), D->isParameterPack());
5084 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00005085}
5086
Balazs Keri3b30d652018-10-19 13:32:20 +00005087ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00005088ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005089 DeclarationName ToDeclName;
5090 SourceLocation ToLocation, ToInnerLocStart;
5091 QualType ToType;
5092 TypeSourceInfo *ToTypeSourceInfo;
5093 if (auto Imp = importSeq(
5094 D->getDeclName(), D->getLocation(), D->getType(), D->getTypeSourceInfo(),
5095 D->getInnerLocStart()))
5096 std::tie(
5097 ToDeclName, ToLocation, ToType, ToTypeSourceInfo,
5098 ToInnerLocStart) = *Imp;
5099 else
5100 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005101
Douglas Gregora082a492010-11-30 19:14:50 +00005102 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00005103
5104 NonTypeTemplateParmDecl *ToD = nullptr;
5105 (void)GetImportedOrCreateDecl(
5106 ToD, D, Importer.getToContext(),
5107 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005108 ToInnerLocStart, ToLocation, D->getDepth(),
5109 D->getPosition(), ToDeclName.getAsIdentifierInfo(), ToType,
5110 D->isParameterPack(), ToTypeSourceInfo);
Gabor Marton26f72a92018-07-12 09:42:05 +00005111 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00005112}
5113
Balazs Keri3b30d652018-10-19 13:32:20 +00005114ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00005115ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
5116 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005117 auto NameOrErr = import(D->getDeclName());
5118 if (!NameOrErr)
5119 return NameOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005120
Douglas Gregora082a492010-11-30 19:14:50 +00005121 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005122 ExpectedSLoc LocationOrErr = import(D->getLocation());
5123 if (!LocationOrErr)
5124 return LocationOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00005125
Douglas Gregora082a492010-11-30 19:14:50 +00005126 // Import template parameters.
Balazs Keridec09162019-03-20 15:42:42 +00005127 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005128 if (!TemplateParamsOrErr)
5129 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005130
Douglas Gregora082a492010-11-30 19:14:50 +00005131 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00005132
5133 TemplateTemplateParmDecl *ToD = nullptr;
5134 (void)GetImportedOrCreateDecl(
5135 ToD, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005136 Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr,
5137 D->getDepth(), D->getPosition(), D->isParameterPack(),
5138 (*NameOrErr).getAsIdentifierInfo(),
5139 *TemplateParamsOrErr);
Gabor Marton26f72a92018-07-12 09:42:05 +00005140 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00005141}
5142
Gabor Marton16d98c22019-03-07 13:01:51 +00005143// Returns the definition for a (forward) declaration of a TemplateDecl, if
Gabor Marton9581c332018-05-23 13:53:36 +00005144// it has any definition in the redecl chain.
Gabor Marton16d98c22019-03-07 13:01:51 +00005145template <typename T> static auto getTemplateDefinition(T *D) -> T * {
5146 assert(D->getTemplatedDecl() && "Should be called on templates only");
5147 auto *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
Gabor Marton9581c332018-05-23 13:53:36 +00005148 if (!ToTemplatedDef)
5149 return nullptr;
Gabor Marton16d98c22019-03-07 13:01:51 +00005150 auto *TemplateWithDef = ToTemplatedDef->getDescribedTemplate();
5151 return cast_or_null<T>(TemplateWithDef);
Gabor Marton9581c332018-05-23 13:53:36 +00005152}
5153
Balazs Keri3b30d652018-10-19 13:32:20 +00005154ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00005155
Douglas Gregora082a492010-11-30 19:14:50 +00005156 // Import the major distinguishing characteristics of this class template.
5157 DeclContext *DC, *LexicalDC;
5158 DeclarationName Name;
5159 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005160 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005161 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5162 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005163 if (ToD)
5164 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00005165
Gabor Marton7df342a2018-12-17 12:42:12 +00005166 ClassTemplateDecl *FoundByLookup = nullptr;
5167
Douglas Gregora082a492010-11-30 19:14:50 +00005168 // We may already have a template of the same name; try to find and match it.
5169 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005170 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00005171 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005172 for (auto *FoundDecl : FoundDecls) {
Gabor Marton7df342a2018-12-17 12:42:12 +00005173 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary |
5174 Decl::IDNS_TagFriend))
Douglas Gregora082a492010-11-30 19:14:50 +00005175 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005176
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005177 Decl *Found = FoundDecl;
Gabor Marton7df342a2018-12-17 12:42:12 +00005178 auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found);
5179 if (FoundTemplate) {
Balázs Kéric2f6efc2019-11-15 15:05:20 +01005180 if (!hasSameVisibilityContext(FoundTemplate, D))
5181 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005182
Douglas Gregora082a492010-11-30 19:14:50 +00005183 if (IsStructuralMatch(D, FoundTemplate)) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005184 ClassTemplateDecl *TemplateWithDef =
5185 getTemplateDefinition(FoundTemplate);
Balazs Keri2e160602019-08-12 10:07:38 +00005186 if (D->isThisDeclarationADefinition() && TemplateWithDef)
Gabor Marton7df342a2018-12-17 12:42:12 +00005187 return Importer.MapImported(D, TemplateWithDef);
Balazs Keri2e160602019-08-12 10:07:38 +00005188 if (!FoundByLookup)
5189 FoundByLookup = FoundTemplate;
5190 // Search in all matches because there may be multiple decl chains,
5191 // see ASTTests test ImportExistingFriendClassTemplateDef.
5192 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00005193 }
Gabor Martonf035b752019-08-27 11:36:10 +00005194 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00005195 }
Douglas Gregora082a492010-11-30 19:14:50 +00005196 }
Gabor Marton9581c332018-05-23 13:53:36 +00005197
Douglas Gregora082a492010-11-30 19:14:50 +00005198 if (!ConflictingDecls.empty()) {
Gabor Martonf035b752019-08-27 11:36:10 +00005199 ExpectedName NameOrErr = Importer.HandleNameConflict(
5200 Name, DC, Decl::IDNS_Ordinary, ConflictingDecls.data(),
5201 ConflictingDecls.size());
5202 if (NameOrErr)
5203 Name = NameOrErr.get();
5204 else
5205 return NameOrErr.takeError();
Douglas Gregora082a492010-11-30 19:14:50 +00005206 }
Douglas Gregora082a492010-11-30 19:14:50 +00005207 }
5208
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005209 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
5210
Douglas Gregora082a492010-11-30 19:14:50 +00005211 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005212 CXXRecordDecl *ToTemplated;
5213 if (Error Err = importInto(ToTemplated, FromTemplated))
5214 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005215
Douglas Gregora082a492010-11-30 19:14:50 +00005216 // Create the class template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005217 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005218 if (!TemplateParamsOrErr)
5219 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00005220
Gabor Marton26f72a92018-07-12 09:42:05 +00005221 ClassTemplateDecl *D2;
5222 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005223 *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005224 return D2;
5225
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005226 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005227
Douglas Gregora082a492010-11-30 19:14:50 +00005228 D2->setAccess(D->getAccess());
5229 D2->setLexicalDeclContext(LexicalDC);
Gabor Marton7df342a2018-12-17 12:42:12 +00005230
Gabor Martonbc5b7e22019-12-04 17:12:08 +01005231 addDeclToContexts(D, D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00005232
Gabor Marton7df342a2018-12-17 12:42:12 +00005233 if (FoundByLookup) {
5234 auto *Recent =
5235 const_cast<ClassTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5236
5237 // It is possible that during the import of the class template definition
5238 // we start the import of a fwd friend decl of the very same class template
5239 // and we add the fwd friend decl to the lookup table. But the ToTemplated
5240 // had been created earlier and by that time the lookup could not find
5241 // anything existing, so it has no previous decl. Later, (still during the
5242 // import of the fwd friend decl) we start to import the definition again
5243 // and this time the lookup finds the previous fwd friend class template.
5244 // In this case we must set up the previous decl for the templated decl.
5245 if (!ToTemplated->getPreviousDecl()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005246 assert(FoundByLookup->getTemplatedDecl() &&
5247 "Found decl must have its templated decl set");
Gabor Marton7df342a2018-12-17 12:42:12 +00005248 CXXRecordDecl *PrevTemplated =
5249 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5250 if (ToTemplated != PrevTemplated)
5251 ToTemplated->setPreviousDecl(PrevTemplated);
5252 }
5253
5254 D2->setPreviousDecl(Recent);
5255 }
5256
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005257 if (FromTemplated->isCompleteDefinition() &&
5258 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00005259 // FIXME: Import definition!
5260 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005261
Douglas Gregora082a492010-11-30 19:14:50 +00005262 return D2;
5263}
5264
Balazs Keri3b30d652018-10-19 13:32:20 +00005265ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +00005266 ClassTemplateSpecializationDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005267 ClassTemplateDecl *ClassTemplate;
5268 if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate()))
5269 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005270
Douglas Gregore2e50d332010-12-01 01:36:18 +00005271 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005272 DeclContext *DC, *LexicalDC;
5273 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5274 return std::move(Err);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005275
5276 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005277 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005278 if (Error Err = ImportTemplateArguments(
5279 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5280 return std::move(Err);
Vedant Kumar5094e6d2019-12-18 14:52:51 -08005281
5282 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005283 void *InsertPos = nullptr;
Gabor Marton7f8c4002019-03-19 13:34:10 +00005284 ClassTemplateSpecializationDecl *PrevDecl = nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005285 ClassTemplatePartialSpecializationDecl *PartialSpec =
5286 dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
Vedant Kumar5094e6d2019-12-18 14:52:51 -08005287 if (PartialSpec)
5288 PrevDecl =
5289 ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos);
5290 else
Gabor Marton7f8c4002019-03-19 13:34:10 +00005291 PrevDecl = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005292
Gabor Marton7f8c4002019-03-19 13:34:10 +00005293 if (PrevDecl) {
5294 if (IsStructuralMatch(D, PrevDecl)) {
5295 if (D->isThisDeclarationADefinition() && PrevDecl->getDefinition()) {
5296 Importer.MapImported(D, PrevDecl->getDefinition());
5297 // Import those default field initializers which have been
5298 // instantiated in the "From" context, but not in the "To" context.
Gabor Marton5ac6d492019-05-15 10:29:48 +00005299 for (auto *FromField : D->fields()) {
5300 auto ToOrErr = import(FromField);
5301 if (!ToOrErr)
5302 return ToOrErr.takeError();
5303 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005304
Gabor Marton7f8c4002019-03-19 13:34:10 +00005305 // Import those methods which have been instantiated in the
5306 // "From" context, but not in the "To" context.
Gabor Marton5ac6d492019-05-15 10:29:48 +00005307 for (CXXMethodDecl *FromM : D->methods()) {
5308 auto ToOrErr = import(FromM);
5309 if (!ToOrErr)
5310 return ToOrErr.takeError();
5311 }
Gabor Marton42e15de2018-08-22 11:52:14 +00005312
Gabor Marton7f8c4002019-03-19 13:34:10 +00005313 // TODO Import instantiated default arguments.
5314 // TODO Import instantiated exception specifications.
5315 //
5316 // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint
5317 // what else could be fused during an AST merge.
5318 return PrevDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005319 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005320 } else { // ODR violation.
5321 // FIXME HandleNameConflict
Gabor Marton303c98612019-06-25 08:00:51 +00005322 return make_error<ImportError>(ImportError::NameConflict);
Gabor Marton42e15de2018-08-22 11:52:14 +00005323 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005324 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005325
Gabor Marton7f8c4002019-03-19 13:34:10 +00005326 // Import the location of this declaration.
5327 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5328 if (!BeginLocOrErr)
5329 return BeginLocOrErr.takeError();
5330 ExpectedSLoc IdLocOrErr = import(D->getLocation());
5331 if (!IdLocOrErr)
5332 return IdLocOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +00005333
Gabor Marton7f8c4002019-03-19 13:34:10 +00005334 // Create the specialization.
5335 ClassTemplateSpecializationDecl *D2 = nullptr;
5336 if (PartialSpec) {
5337 // Import TemplateArgumentListInfo.
5338 TemplateArgumentListInfo ToTAInfo;
5339 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
5340 if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
5341 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005342
Gabor Marton7f8c4002019-03-19 13:34:10 +00005343 QualType CanonInjType;
5344 if (Error Err = importInto(
5345 CanonInjType, PartialSpec->getInjectedSpecializationType()))
5346 return std::move(Err);
5347 CanonInjType = CanonInjType.getCanonicalType();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005348
Vedant Kumar5094e6d2019-12-18 14:52:51 -08005349 auto ToTPListOrErr = import(PartialSpec->getTemplateParameters());
5350 if (!ToTPListOrErr)
5351 return ToTPListOrErr.takeError();
5352
Gabor Marton7f8c4002019-03-19 13:34:10 +00005353 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
5354 D2, D, Importer.getToContext(), D->getTagKind(), DC,
Vedant Kumar5094e6d2019-12-18 14:52:51 -08005355 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, ClassTemplate,
Gabor Marton7f8c4002019-03-19 13:34:10 +00005356 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
5357 ToTAInfo, CanonInjType,
5358 cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl)))
5359 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005360
Gabor Marton7f8c4002019-03-19 13:34:10 +00005361 // Update InsertPos, because preceding import calls may have invalidated
5362 // it by adding new specializations.
Vedant Kumar5094e6d2019-12-18 14:52:51 -08005363 if (!ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos))
Gabor Marton7f8c4002019-03-19 13:34:10 +00005364 // Add this partial specialization to the class template.
Vedant Kumar5094e6d2019-12-18 14:52:51 -08005365 ClassTemplate->AddPartialSpecialization(
5366 cast<ClassTemplatePartialSpecializationDecl>(D2), InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005367
Gabor Marton7f8c4002019-03-19 13:34:10 +00005368 } else { // Not a partial specialization.
5369 if (GetImportedOrCreateDecl(
5370 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5371 *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs,
5372 PrevDecl))
5373 return D2;
Gabor Marton42e15de2018-08-22 11:52:14 +00005374
Gabor Marton7f8c4002019-03-19 13:34:10 +00005375 // Update InsertPos, because preceding import calls may have invalidated
5376 // it by adding new specializations.
5377 if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos))
5378 // Add this specialization to the class template.
5379 ClassTemplate->AddSpecialization(D2, InsertPos);
5380 }
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005381
Gabor Marton7f8c4002019-03-19 13:34:10 +00005382 D2->setSpecializationKind(D->getSpecializationKind());
Douglas Gregore2e50d332010-12-01 01:36:18 +00005383
Gabor Marton7f8c4002019-03-19 13:34:10 +00005384 // Set the context of this specialization/instantiation.
5385 D2->setLexicalDeclContext(LexicalDC);
5386
5387 // Add to the DC only if it was an explicit specialization/instantiation.
5388 if (D2->isExplicitInstantiationOrSpecialization()) {
5389 LexicalDC->addDeclInternal(D2);
5390 }
5391
Balázs Kéria9f10eb2019-12-05 16:21:21 +01005392 if (auto BraceRangeOrErr = import(D->getBraceRange()))
5393 D2->setBraceRange(*BraceRangeOrErr);
5394 else
5395 return BraceRangeOrErr.takeError();
5396
Gabor Marton7f8c4002019-03-19 13:34:10 +00005397 // Import the qualifier, if any.
5398 if (auto LocOrErr = import(D->getQualifierLoc()))
5399 D2->setQualifierInfo(*LocOrErr);
5400 else
5401 return LocOrErr.takeError();
5402
5403 if (auto *TSI = D->getTypeAsWritten()) {
5404 if (auto TInfoOrErr = import(TSI))
5405 D2->setTypeAsWritten(*TInfoOrErr);
5406 else
5407 return TInfoOrErr.takeError();
5408
5409 if (auto LocOrErr = import(D->getTemplateKeywordLoc()))
5410 D2->setTemplateKeywordLoc(*LocOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +00005411 else
5412 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005413
Gabor Marton7f8c4002019-03-19 13:34:10 +00005414 if (auto LocOrErr = import(D->getExternLoc()))
5415 D2->setExternLoc(*LocOrErr);
5416 else
5417 return LocOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00005418 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005419
5420 if (D->getPointOfInstantiation().isValid()) {
5421 if (auto POIOrErr = import(D->getPointOfInstantiation()))
5422 D2->setPointOfInstantiation(*POIOrErr);
5423 else
5424 return POIOrErr.takeError();
5425 }
5426
5427 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
5428
Balazs Keri3b30d652018-10-19 13:32:20 +00005429 if (D->isCompleteDefinition())
5430 if (Error Err = ImportDefinition(D, D2))
5431 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005432
Douglas Gregore2e50d332010-12-01 01:36:18 +00005433 return D2;
5434}
5435
Balazs Keri3b30d652018-10-19 13:32:20 +00005436ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005437 // If this variable has a definition in the translation unit we're coming
5438 // from,
5439 // but this particular declaration is not that definition, import the
5440 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005441 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00005442 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
5443 if (Definition && Definition != D->getTemplatedDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005444 if (ExpectedDecl ImportedDefOrErr = import(
5445 Definition->getDescribedVarTemplate()))
5446 return Importer.MapImported(D, *ImportedDefOrErr);
5447 else
5448 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005449 }
5450
5451 // Import the major distinguishing characteristics of this variable template.
5452 DeclContext *DC, *LexicalDC;
5453 DeclarationName Name;
5454 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005455 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005456 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5457 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005458 if (ToD)
5459 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005460
5461 // We may already have a template of the same name; try to find and match it.
5462 assert(!DC->isFunctionOrMethod() &&
5463 "Variable templates cannot be declared at function scope");
5464 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00005465 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005466 for (auto *FoundDecl : FoundDecls) {
5467 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00005468 continue;
5469
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005470 Decl *Found = FoundDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005471 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005472 if (IsStructuralMatch(D, FoundTemplate)) {
5473 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00005474 Importer.MapImported(D->getTemplatedDecl(),
5475 FoundTemplate->getTemplatedDecl());
5476 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005477 }
Gabor Martonf035b752019-08-27 11:36:10 +00005478 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005479 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00005480 }
5481
5482 if (!ConflictingDecls.empty()) {
Gabor Martonf035b752019-08-27 11:36:10 +00005483 ExpectedName NameOrErr = Importer.HandleNameConflict(
5484 Name, DC, Decl::IDNS_Ordinary, ConflictingDecls.data(),
5485 ConflictingDecls.size());
5486 if (NameOrErr)
5487 Name = NameOrErr.get();
5488 else
5489 return NameOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005490 }
5491
Larisse Voufo39a1e502013-08-06 01:03:05 +00005492 VarDecl *DTemplated = D->getTemplatedDecl();
5493
5494 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005495 // FIXME: Value not used?
5496 ExpectedType TypeOrErr = import(DTemplated->getType());
5497 if (!TypeOrErr)
5498 return TypeOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005499
5500 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005501 VarDecl *ToTemplated;
5502 if (Error Err = importInto(ToTemplated, DTemplated))
5503 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005504
5505 // Create the variable template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005506 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005507 if (!TemplateParamsOrErr)
5508 return TemplateParamsOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005509
Gabor Marton26f72a92018-07-12 09:42:05 +00005510 VarTemplateDecl *ToVarTD;
5511 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00005512 Name, *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005513 return ToVarTD;
5514
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005515 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005516
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005517 ToVarTD->setAccess(D->getAccess());
5518 ToVarTD->setLexicalDeclContext(LexicalDC);
5519 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005520
Larisse Voufo39a1e502013-08-06 01:03:05 +00005521 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005522 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005523 // FIXME: Import definition!
5524 }
5525
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005526 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005527}
5528
Balazs Keri3b30d652018-10-19 13:32:20 +00005529ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00005530 VarTemplateSpecializationDecl *D) {
5531 // If this record has a definition in the translation unit we're coming from,
5532 // but this particular declaration is not that definition, import the
5533 // definition and map to that.
5534 VarDecl *Definition = D->getDefinition();
5535 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005536 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5537 return Importer.MapImported(D, *ImportedDefOrErr);
5538 else
5539 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005540 }
5541
Simon Pilgrim4c146ab2019-05-18 11:33:27 +00005542 VarTemplateDecl *VarTemplate = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00005543 if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate()))
5544 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005545
5546 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005547 DeclContext *DC, *LexicalDC;
5548 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5549 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005550
5551 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005552 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5553 if (!BeginLocOrErr)
5554 return BeginLocOrErr.takeError();
5555
5556 auto IdLocOrErr = import(D->getLocation());
5557 if (!IdLocOrErr)
5558 return IdLocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005559
5560 // Import template arguments.
5561 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005562 if (Error Err = ImportTemplateArguments(
5563 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5564 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005565
5566 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005567 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005568 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005569 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005570 if (D2) {
5571 // We already have a variable template specialization with these template
5572 // arguments.
5573
5574 // FIXME: Check for specialization vs. instantiation errors.
5575
5576 if (VarDecl *FoundDef = D2->getDefinition()) {
5577 if (!D->isThisDeclarationADefinition() ||
5578 IsStructuralMatch(D, FoundDef)) {
5579 // The record types structurally match, or the "from" translation
5580 // unit only had a forward declaration anyway; call it the same
5581 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00005582 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005583 }
5584 }
5585 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005586 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005587 QualType T;
5588 if (Error Err = importInto(T, D->getType()))
5589 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005590
Balazs Keri3b30d652018-10-19 13:32:20 +00005591 auto TInfoOrErr = import(D->getTypeSourceInfo());
5592 if (!TInfoOrErr)
5593 return TInfoOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005594
5595 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00005596 if (Error Err = ImportTemplateArgumentListInfo(
5597 D->getTemplateArgsInfo(), ToTAInfo))
5598 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005599
5600 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005601 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005602 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
5603 // Import TemplateArgumentListInfo
5604 TemplateArgumentListInfo ArgInfos;
5605 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
5606 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
Balazs Keri3b30d652018-10-19 13:32:20 +00005607 if (Error Err = ImportTemplateArgumentListInfo(
5608 *FromTAArgsAsWritten, ArgInfos))
5609 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005610
Balazs Keridec09162019-03-20 15:42:42 +00005611 auto ToTPListOrErr = import(FromPartial->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005612 if (!ToTPListOrErr)
5613 return ToTPListOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005614
Gabor Marton26f72a92018-07-12 09:42:05 +00005615 PartVarSpecDecl *ToPartial;
5616 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005617 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
5618 VarTemplate, T, *TInfoOrErr,
5619 D->getStorageClass(), TemplateArgs, ArgInfos))
Gabor Marton26f72a92018-07-12 09:42:05 +00005620 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005621
Balazs Keri3b30d652018-10-19 13:32:20 +00005622 if (Expected<PartVarSpecDecl *> ToInstOrErr = import(
5623 FromPartial->getInstantiatedFromMember()))
5624 ToPartial->setInstantiatedFromMember(*ToInstOrErr);
5625 else
5626 return ToInstOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005627
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005628 if (FromPartial->isMemberSpecialization())
5629 ToPartial->setMemberSpecialization();
5630
5631 D2 = ToPartial;
Balazs Keri3b30d652018-10-19 13:32:20 +00005632
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005633 } else { // Full specialization
Balazs Keri3b30d652018-10-19 13:32:20 +00005634 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
5635 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
5636 T, *TInfoOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005637 D->getStorageClass(), TemplateArgs))
5638 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005639 }
5640
Balazs Keri3b30d652018-10-19 13:32:20 +00005641 if (D->getPointOfInstantiation().isValid()) {
5642 if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation()))
5643 D2->setPointOfInstantiation(*POIOrErr);
5644 else
5645 return POIOrErr.takeError();
5646 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005647
Larisse Voufo39a1e502013-08-06 01:03:05 +00005648 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005649 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005650
5651 // Add this specialization to the class template.
5652 VarTemplate->AddSpecialization(D2, InsertPos);
5653
5654 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005655 if (auto LocOrErr = import(D->getQualifierLoc()))
5656 D2->setQualifierInfo(*LocOrErr);
5657 else
5658 return LocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005659
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005660 if (D->isConstexpr())
5661 D2->setConstexpr(true);
5662
Larisse Voufo39a1e502013-08-06 01:03:05 +00005663 // Add the specialization to this context.
5664 D2->setLexicalDeclContext(LexicalDC);
5665 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005666
5667 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00005668 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005669
Balazs Keri3b30d652018-10-19 13:32:20 +00005670 if (Error Err = ImportInitializer(D, D2))
5671 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005672
5673 return D2;
5674}
5675
Balazs Keri3b30d652018-10-19 13:32:20 +00005676ExpectedDecl
5677ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005678 DeclContext *DC, *LexicalDC;
5679 DeclarationName Name;
5680 SourceLocation Loc;
5681 NamedDecl *ToD;
5682
Balazs Keri3b30d652018-10-19 13:32:20 +00005683 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5684 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005685
5686 if (ToD)
5687 return ToD;
5688
Gabor Marton16d98c22019-03-07 13:01:51 +00005689 const FunctionTemplateDecl *FoundByLookup = nullptr;
5690
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005691 // Try to find a function in our own ("to") context with the same name, same
5692 // type, and in the same context as the function we're importing.
Gabor Marton16d98c22019-03-07 13:01:51 +00005693 // FIXME Split this into a separate function.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005694 if (!LexicalDC->isFunctionOrMethod()) {
Gabor Martone331e632019-02-18 13:09:27 +00005695 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00005696 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005697 for (auto *FoundDecl : FoundDecls) {
5698 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005699 continue;
5700
Gabor Marton16d98c22019-03-07 13:01:51 +00005701 if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Balazs Kerif8a89c82019-09-13 08:03:49 +00005702 if (!hasSameVisibilityContext(FoundTemplate, D))
5703 continue;
5704 if (IsStructuralMatch(D, FoundTemplate)) {
5705 FunctionTemplateDecl *TemplateWithDef =
5706 getTemplateDefinition(FoundTemplate);
5707 if (D->isThisDeclarationADefinition() && TemplateWithDef)
5708 return Importer.MapImported(D, TemplateWithDef);
5709
5710 FoundByLookup = FoundTemplate;
5711 break;
Gabor Marton16d98c22019-03-07 13:01:51 +00005712 // TODO: handle conflicting names
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005713 }
5714 }
5715 }
5716 }
5717
Balazs Keridec09162019-03-20 15:42:42 +00005718 auto ParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005719 if (!ParamsOrErr)
5720 return ParamsOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005721
Balazs Keri3b30d652018-10-19 13:32:20 +00005722 FunctionDecl *TemplatedFD;
5723 if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl()))
5724 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005725
Gabor Marton26f72a92018-07-12 09:42:05 +00005726 FunctionTemplateDecl *ToFunc;
5727 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005728 *ParamsOrErr, TemplatedFD))
Gabor Marton26f72a92018-07-12 09:42:05 +00005729 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005730
5731 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005732
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005733 ToFunc->setAccess(D->getAccess());
5734 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005735 LexicalDC->addDeclInternal(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005736
5737 if (FoundByLookup) {
5738 auto *Recent =
5739 const_cast<FunctionTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5740 if (!TemplatedFD->getPreviousDecl()) {
5741 assert(FoundByLookup->getTemplatedDecl() &&
5742 "Found decl must have its templated decl set");
5743 auto *PrevTemplated =
5744 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5745 if (TemplatedFD != PrevTemplated)
5746 TemplatedFD->setPreviousDecl(PrevTemplated);
5747 }
5748 ToFunc->setPreviousDecl(Recent);
5749 }
5750
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005751 return ToFunc;
5752}
5753
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005754//----------------------------------------------------------------------------
5755// Import Statements
5756//----------------------------------------------------------------------------
5757
Balazs Keri3b30d652018-10-19 13:32:20 +00005758ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005759 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
5760 << S->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005761 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005762}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005763
Balazs Keri3b30d652018-10-19 13:32:20 +00005764
5765ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
Gabor Marton303c98612019-06-25 08:00:51 +00005766 if (Importer.returnWithErrorInTest())
5767 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005768 SmallVector<IdentifierInfo *, 4> Names;
5769 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5770 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005771 // ToII is nullptr when no symbolic name is given for output operand
5772 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005773 Names.push_back(ToII);
5774 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005775
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005776 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5777 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005778 // ToII is nullptr when no symbolic name is given for input operand
5779 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005780 Names.push_back(ToII);
5781 }
5782
5783 SmallVector<StringLiteral *, 4> Clobbers;
5784 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005785 if (auto ClobberOrErr = import(S->getClobberStringLiteral(I)))
5786 Clobbers.push_back(*ClobberOrErr);
5787 else
5788 return ClobberOrErr.takeError();
5789
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005790 }
5791
5792 SmallVector<StringLiteral *, 4> Constraints;
5793 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005794 if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I)))
5795 Constraints.push_back(*OutputOrErr);
5796 else
5797 return OutputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005798 }
5799
5800 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005801 if (auto InputOrErr = import(S->getInputConstraintLiteral(I)))
5802 Constraints.push_back(*InputOrErr);
5803 else
5804 return InputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005805 }
5806
Jennifer Yub8fee672019-06-03 15:57:25 +00005807 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs() +
5808 S->getNumLabels());
Balazs Keri3b30d652018-10-19 13:32:20 +00005809 if (Error Err = ImportContainerChecked(S->outputs(), Exprs))
5810 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005811
Jennifer Yub8fee672019-06-03 15:57:25 +00005812 if (Error Err =
5813 ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
5814 return std::move(Err);
5815
Balazs Keri3b30d652018-10-19 13:32:20 +00005816 if (Error Err = ImportArrayChecked(
Jennifer Yub8fee672019-06-03 15:57:25 +00005817 S->labels(), Exprs.begin() + S->getNumOutputs() + S->getNumInputs()))
Balazs Keri3b30d652018-10-19 13:32:20 +00005818 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005819
Balazs Keri3b30d652018-10-19 13:32:20 +00005820 ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc());
5821 if (!AsmLocOrErr)
5822 return AsmLocOrErr.takeError();
5823 auto AsmStrOrErr = import(S->getAsmString());
5824 if (!AsmStrOrErr)
5825 return AsmStrOrErr.takeError();
5826 ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc());
5827 if (!RParenLocOrErr)
5828 return RParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005829
5830 return new (Importer.getToContext()) GCCAsmStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005831 Importer.getToContext(),
5832 *AsmLocOrErr,
5833 S->isSimple(),
5834 S->isVolatile(),
5835 S->getNumOutputs(),
5836 S->getNumInputs(),
5837 Names.data(),
5838 Constraints.data(),
5839 Exprs.data(),
5840 *AsmStrOrErr,
5841 S->getNumClobbers(),
5842 Clobbers.data(),
Jennifer Yub8fee672019-06-03 15:57:25 +00005843 S->getNumLabels(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005844 *RParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005845}
5846
Balazs Keri3b30d652018-10-19 13:32:20 +00005847ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5848 auto Imp = importSeq(S->getDeclGroup(), S->getBeginLoc(), S->getEndLoc());
5849 if (!Imp)
5850 return Imp.takeError();
5851
5852 DeclGroupRef ToDG;
5853 SourceLocation ToBeginLoc, ToEndLoc;
5854 std::tie(ToDG, ToBeginLoc, ToEndLoc) = *Imp;
5855
5856 return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005857}
5858
Balazs Keri3b30d652018-10-19 13:32:20 +00005859ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5860 ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc());
5861 if (!ToSemiLocOrErr)
5862 return ToSemiLocOrErr.takeError();
5863 return new (Importer.getToContext()) NullStmt(
5864 *ToSemiLocOrErr, S->hasLeadingEmptyMacro());
Sean Callanan59721b32015-04-28 18:41:46 +00005865}
5866
Balazs Keri3b30d652018-10-19 13:32:20 +00005867ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005868 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005869
Balazs Keri3b30d652018-10-19 13:32:20 +00005870 if (Error Err = ImportContainerChecked(S->body(), ToStmts))
5871 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00005872
Balazs Keri3b30d652018-10-19 13:32:20 +00005873 ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc());
5874 if (!ToLBracLocOrErr)
5875 return ToLBracLocOrErr.takeError();
5876
5877 ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc());
5878 if (!ToRBracLocOrErr)
5879 return ToRBracLocOrErr.takeError();
5880
5881 return CompoundStmt::Create(
5882 Importer.getToContext(), ToStmts,
5883 *ToLBracLocOrErr, *ToRBracLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005884}
5885
Balazs Keri3b30d652018-10-19 13:32:20 +00005886ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5887 auto Imp = importSeq(
5888 S->getLHS(), S->getRHS(), S->getSubStmt(), S->getCaseLoc(),
5889 S->getEllipsisLoc(), S->getColonLoc());
5890 if (!Imp)
5891 return Imp.takeError();
5892
5893 Expr *ToLHS, *ToRHS;
5894 Stmt *ToSubStmt;
5895 SourceLocation ToCaseLoc, ToEllipsisLoc, ToColonLoc;
5896 std::tie(ToLHS, ToRHS, ToSubStmt, ToCaseLoc, ToEllipsisLoc, ToColonLoc) =
5897 *Imp;
5898
Bruno Ricci5b30571752018-10-28 12:30:53 +00005899 auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS,
5900 ToCaseLoc, ToEllipsisLoc, ToColonLoc);
Gabor Horvath480892b2017-10-18 09:25:18 +00005901 ToStmt->setSubStmt(ToSubStmt);
Balazs Keri3b30d652018-10-19 13:32:20 +00005902
Gabor Horvath480892b2017-10-18 09:25:18 +00005903 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005904}
5905
Balazs Keri3b30d652018-10-19 13:32:20 +00005906ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5907 auto Imp = importSeq(S->getDefaultLoc(), S->getColonLoc(), S->getSubStmt());
5908 if (!Imp)
5909 return Imp.takeError();
5910
5911 SourceLocation ToDefaultLoc, ToColonLoc;
5912 Stmt *ToSubStmt;
5913 std::tie(ToDefaultLoc, ToColonLoc, ToSubStmt) = *Imp;
5914
5915 return new (Importer.getToContext()) DefaultStmt(
5916 ToDefaultLoc, ToColonLoc, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005917}
5918
Balazs Keri3b30d652018-10-19 13:32:20 +00005919ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5920 auto Imp = importSeq(S->getIdentLoc(), S->getDecl(), S->getSubStmt());
5921 if (!Imp)
5922 return Imp.takeError();
5923
5924 SourceLocation ToIdentLoc;
5925 LabelDecl *ToLabelDecl;
5926 Stmt *ToSubStmt;
5927 std::tie(ToIdentLoc, ToLabelDecl, ToSubStmt) = *Imp;
5928
5929 return new (Importer.getToContext()) LabelStmt(
5930 ToIdentLoc, ToLabelDecl, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005931}
5932
Balazs Keri3b30d652018-10-19 13:32:20 +00005933ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5934 ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc());
5935 if (!ToAttrLocOrErr)
5936 return ToAttrLocOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005937 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5938 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00005939 if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs))
5940 return std::move(Err);
5941 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
5942 if (!ToSubStmtOrErr)
5943 return ToSubStmtOrErr.takeError();
5944
5945 return AttributedStmt::Create(
5946 Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005947}
5948
Balazs Keri3b30d652018-10-19 13:32:20 +00005949ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5950 auto Imp = importSeq(
5951 S->getIfLoc(), S->getInit(), S->getConditionVariable(), S->getCond(),
5952 S->getThen(), S->getElseLoc(), S->getElse());
5953 if (!Imp)
5954 return Imp.takeError();
5955
5956 SourceLocation ToIfLoc, ToElseLoc;
5957 Stmt *ToInit, *ToThen, *ToElse;
5958 VarDecl *ToConditionVariable;
5959 Expr *ToCond;
5960 std::tie(
5961 ToIfLoc, ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc, ToElse) =
5962 *Imp;
5963
Bruno Riccib1cc94b2018-10-27 21:12:20 +00005964 return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(),
5965 ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc,
5966 ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00005967}
5968
Balazs Keri3b30d652018-10-19 13:32:20 +00005969ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
5970 auto Imp = importSeq(
5971 S->getInit(), S->getConditionVariable(), S->getCond(),
5972 S->getBody(), S->getSwitchLoc());
5973 if (!Imp)
5974 return Imp.takeError();
5975
5976 Stmt *ToInit, *ToBody;
5977 VarDecl *ToConditionVariable;
5978 Expr *ToCond;
5979 SourceLocation ToSwitchLoc;
5980 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
5981
Bruno Riccie2806f82018-10-29 16:12:37 +00005982 auto *ToStmt = SwitchStmt::Create(Importer.getToContext(), ToInit,
5983 ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00005984 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00005985 ToStmt->setSwitchLoc(ToSwitchLoc);
5986
Sean Callanan59721b32015-04-28 18:41:46 +00005987 // Now we have to re-chain the cases.
5988 SwitchCase *LastChainedSwitchCase = nullptr;
5989 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5990 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005991 Expected<SwitchCase *> ToSCOrErr = import(SC);
5992 if (!ToSCOrErr)
5993 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005994 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00005995 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005996 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005997 ToStmt->setSwitchCaseList(*ToSCOrErr);
5998 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005999 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006000
Sean Callanan59721b32015-04-28 18:41:46 +00006001 return ToStmt;
6002}
6003
Balazs Keri3b30d652018-10-19 13:32:20 +00006004ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
6005 auto Imp = importSeq(
6006 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
6007 if (!Imp)
6008 return Imp.takeError();
6009
6010 VarDecl *ToConditionVariable;
6011 Expr *ToCond;
6012 Stmt *ToBody;
6013 SourceLocation ToWhileLoc;
6014 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
6015
Bruno Riccibacf7512018-10-30 13:42:41 +00006016 return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond,
6017 ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00006018}
6019
Balazs Keri3b30d652018-10-19 13:32:20 +00006020ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
6021 auto Imp = importSeq(
6022 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
6023 S->getRParenLoc());
6024 if (!Imp)
6025 return Imp.takeError();
6026
6027 Stmt *ToBody;
6028 Expr *ToCond;
6029 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
6030 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
6031
6032 return new (Importer.getToContext()) DoStmt(
6033 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00006034}
6035
Balazs Keri3b30d652018-10-19 13:32:20 +00006036ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
6037 auto Imp = importSeq(
6038 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
6039 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
6040 if (!Imp)
6041 return Imp.takeError();
6042
6043 Stmt *ToInit;
6044 Expr *ToCond, *ToInc;
6045 VarDecl *ToConditionVariable;
6046 Stmt *ToBody;
6047 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
6048 std::tie(
6049 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
6050 ToLParenLoc, ToRParenLoc) = *Imp;
6051
6052 return new (Importer.getToContext()) ForStmt(
6053 Importer.getToContext(),
6054 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
6055 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00006056}
6057
Balazs Keri3b30d652018-10-19 13:32:20 +00006058ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
6059 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
6060 if (!Imp)
6061 return Imp.takeError();
6062
6063 LabelDecl *ToLabel;
6064 SourceLocation ToGotoLoc, ToLabelLoc;
6065 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
6066
6067 return new (Importer.getToContext()) GotoStmt(
6068 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00006069}
6070
Balazs Keri3b30d652018-10-19 13:32:20 +00006071ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
6072 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
6073 if (!Imp)
6074 return Imp.takeError();
6075
6076 SourceLocation ToGotoLoc, ToStarLoc;
6077 Expr *ToTarget;
6078 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
6079
6080 return new (Importer.getToContext()) IndirectGotoStmt(
6081 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00006082}
6083
Balazs Keri3b30d652018-10-19 13:32:20 +00006084ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
6085 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
6086 if (!ToContinueLocOrErr)
6087 return ToContinueLocOrErr.takeError();
6088 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006089}
6090
Balazs Keri3b30d652018-10-19 13:32:20 +00006091ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
6092 auto ToBreakLocOrErr = import(S->getBreakLoc());
6093 if (!ToBreakLocOrErr)
6094 return ToBreakLocOrErr.takeError();
6095 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006096}
6097
Balazs Keri3b30d652018-10-19 13:32:20 +00006098ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
6099 auto Imp = importSeq(
6100 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
6101 if (!Imp)
6102 return Imp.takeError();
6103
6104 SourceLocation ToReturnLoc;
6105 Expr *ToRetValue;
6106 const VarDecl *ToNRVOCandidate;
6107 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
6108
Bruno Ricci023b1d12018-10-30 14:40:49 +00006109 return ReturnStmt::Create(Importer.getToContext(), ToReturnLoc, ToRetValue,
6110 ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00006111}
6112
Balazs Keri3b30d652018-10-19 13:32:20 +00006113ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
6114 auto Imp = importSeq(
6115 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
6116 if (!Imp)
6117 return Imp.takeError();
6118
6119 SourceLocation ToCatchLoc;
6120 VarDecl *ToExceptionDecl;
6121 Stmt *ToHandlerBlock;
6122 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
6123
6124 return new (Importer.getToContext()) CXXCatchStmt (
6125 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00006126}
6127
Balazs Keri3b30d652018-10-19 13:32:20 +00006128ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
6129 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
6130 if (!ToTryLocOrErr)
6131 return ToTryLocOrErr.takeError();
6132
6133 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
6134 if (!ToTryBlockOrErr)
6135 return ToTryBlockOrErr.takeError();
6136
Sean Callanan59721b32015-04-28 18:41:46 +00006137 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
6138 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
6139 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00006140 if (auto ToHandlerOrErr = import(FromHandler))
6141 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00006142 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006143 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006144 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006145
6146 return CXXTryStmt::Create(
6147 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00006148}
6149
Balazs Keri3b30d652018-10-19 13:32:20 +00006150ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
6151 auto Imp1 = importSeq(
6152 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
6153 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
6154 if (!Imp1)
6155 return Imp1.takeError();
6156 auto Imp2 = importSeq(
6157 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
6158 if (!Imp2)
6159 return Imp2.takeError();
6160
6161 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
6162 Expr *ToCond, *ToInc;
6163 Stmt *ToInit, *ToBody;
6164 std::tie(
6165 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
6166 ToBody) = *Imp1;
6167 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
6168 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
6169
6170 return new (Importer.getToContext()) CXXForRangeStmt(
6171 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
6172 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00006173}
6174
Balazs Keri3b30d652018-10-19 13:32:20 +00006175ExpectedStmt
6176ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
6177 auto Imp = importSeq(
6178 S->getElement(), S->getCollection(), S->getBody(),
6179 S->getForLoc(), S->getRParenLoc());
6180 if (!Imp)
6181 return Imp.takeError();
6182
6183 Stmt *ToElement, *ToBody;
6184 Expr *ToCollection;
6185 SourceLocation ToForLoc, ToRParenLoc;
6186 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
6187
6188 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
6189 ToCollection,
6190 ToBody,
6191 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00006192 ToRParenLoc);
6193}
6194
Balazs Keri3b30d652018-10-19 13:32:20 +00006195ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
6196 auto Imp = importSeq(
6197 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
6198 S->getCatchBody());
6199 if (!Imp)
6200 return Imp.takeError();
6201
6202 SourceLocation ToAtCatchLoc, ToRParenLoc;
6203 VarDecl *ToCatchParamDecl;
6204 Stmt *ToCatchBody;
6205 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
6206
6207 return new (Importer.getToContext()) ObjCAtCatchStmt (
6208 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00006209}
6210
Balazs Keri3b30d652018-10-19 13:32:20 +00006211ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
6212 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
6213 if (!ToAtFinallyLocOrErr)
6214 return ToAtFinallyLocOrErr.takeError();
6215 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
6216 if (!ToAtFinallyStmtOrErr)
6217 return ToAtFinallyStmtOrErr.takeError();
6218 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
6219 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006220}
6221
Balazs Keri3b30d652018-10-19 13:32:20 +00006222ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
6223 auto Imp = importSeq(
6224 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
6225 if (!Imp)
6226 return Imp.takeError();
6227
6228 SourceLocation ToAtTryLoc;
6229 Stmt *ToTryBody, *ToFinallyStmt;
6230 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
6231
Sean Callanan59721b32015-04-28 18:41:46 +00006232 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
6233 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
6234 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00006235 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
6236 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00006237 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006238 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006239 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006240
Sean Callanan59721b32015-04-28 18:41:46 +00006241 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006242 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00006243 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00006244 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00006245}
6246
Balazs Keri3b30d652018-10-19 13:32:20 +00006247ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00006248 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006249 auto Imp = importSeq(
6250 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
6251 if (!Imp)
6252 return Imp.takeError();
6253
6254 SourceLocation ToAtSynchronizedLoc;
6255 Expr *ToSynchExpr;
6256 Stmt *ToSynchBody;
6257 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
6258
Sean Callanan59721b32015-04-28 18:41:46 +00006259 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
6260 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
6261}
6262
Balazs Keri3b30d652018-10-19 13:32:20 +00006263ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
6264 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
6265 if (!ToThrowLocOrErr)
6266 return ToThrowLocOrErr.takeError();
6267 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
6268 if (!ToThrowExprOrErr)
6269 return ToThrowExprOrErr.takeError();
6270 return new (Importer.getToContext()) ObjCAtThrowStmt(
6271 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006272}
6273
Balazs Keri3b30d652018-10-19 13:32:20 +00006274ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
6275 ObjCAutoreleasePoolStmt *S) {
6276 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
6277 if (!ToAtLocOrErr)
6278 return ToAtLocOrErr.takeError();
6279 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6280 if (!ToSubStmtOrErr)
6281 return ToSubStmtOrErr.takeError();
6282 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6283 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006284}
6285
6286//----------------------------------------------------------------------------
6287// Import Expressions
6288//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006289ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006290 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6291 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006292 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006293}
6294
Balazs Keri3b30d652018-10-19 13:32:20 +00006295ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6296 auto Imp = importSeq(
6297 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6298 E->getRParenLoc(), E->getType());
6299 if (!Imp)
6300 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006301
Balazs Keri3b30d652018-10-19 13:32:20 +00006302 SourceLocation ToBuiltinLoc, ToRParenLoc;
6303 Expr *ToSubExpr;
6304 TypeSourceInfo *ToWrittenTypeInfo;
6305 QualType ToType;
6306 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6307 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006308
6309 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006310 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6311 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006312}
6313
Tom Roeder521f0042019-02-26 19:26:41 +00006314ExpectedStmt ASTNodeImporter::VisitChooseExpr(ChooseExpr *E) {
6315 auto Imp = importSeq(E->getCond(), E->getLHS(), E->getRHS(),
6316 E->getBuiltinLoc(), E->getRParenLoc(), E->getType());
6317 if (!Imp)
6318 return Imp.takeError();
6319
6320 Expr *ToCond;
6321 Expr *ToLHS;
6322 Expr *ToRHS;
6323 SourceLocation ToBuiltinLoc, ToRParenLoc;
6324 QualType ToType;
6325 std::tie(ToCond, ToLHS, ToRHS, ToBuiltinLoc, ToRParenLoc, ToType) = *Imp;
6326
6327 ExprValueKind VK = E->getValueKind();
6328 ExprObjectKind OK = E->getObjectKind();
6329
6330 bool TypeDependent = ToCond->isTypeDependent();
6331 bool ValueDependent = ToCond->isValueDependent();
6332
6333 // The value of CondIsTrue only matters if the value is not
6334 // condition-dependent.
6335 bool CondIsTrue = !E->isConditionDependent() && E->isConditionTrue();
6336
6337 return new (Importer.getToContext())
6338 ChooseExpr(ToBuiltinLoc, ToCond, ToLHS, ToRHS, ToType, VK, OK,
6339 ToRParenLoc, CondIsTrue, TypeDependent, ValueDependent);
6340}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006341
Balazs Keri3b30d652018-10-19 13:32:20 +00006342ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6343 ExpectedType TypeOrErr = import(E->getType());
6344 if (!TypeOrErr)
6345 return TypeOrErr.takeError();
6346
6347 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6348 if (!BeginLocOrErr)
6349 return BeginLocOrErr.takeError();
6350
6351 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006352}
6353
Balazs Keri3b30d652018-10-19 13:32:20 +00006354ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6355 auto Imp = importSeq(
6356 E->getBeginLoc(), E->getType(), E->getFunctionName());
6357 if (!Imp)
6358 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006359
Balazs Keri3b30d652018-10-19 13:32:20 +00006360 SourceLocation ToBeginLoc;
6361 QualType ToType;
6362 StringLiteral *ToFunctionName;
6363 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006364
Bruno Ricci17ff0262018-10-27 19:21:19 +00006365 return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType,
6366 E->getIdentKind(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006367}
6368
Balazs Keri3b30d652018-10-19 13:32:20 +00006369ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6370 auto Imp = importSeq(
6371 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6372 E->getLocation(), E->getType());
6373 if (!Imp)
6374 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006375
Balazs Keri3b30d652018-10-19 13:32:20 +00006376 NestedNameSpecifierLoc ToQualifierLoc;
6377 SourceLocation ToTemplateKeywordLoc, ToLocation;
6378 ValueDecl *ToDecl;
6379 QualType ToType;
6380 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6381 *Imp;
6382
6383 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006384 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006385 auto FoundDOrErr = import(E->getFoundDecl());
6386 if (!FoundDOrErr)
6387 return FoundDOrErr.takeError();
6388 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006389 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006390
Aleksei Sidorina693b372016-09-28 10:16:56 +00006391 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006392 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006393 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006394 if (Error Err =
Balázs Kéria9f10eb2019-12-05 16:21:21 +01006395 ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6396 E->template_arguments(), ToTAInfo))
Balazs Keri3b30d652018-10-19 13:32:20 +00006397 return std::move(Err);
6398 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006399 }
6400
Balazs Keri3b30d652018-10-19 13:32:20 +00006401 auto *ToE = DeclRefExpr::Create(
6402 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6403 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
Richard Smith715f7a12019-06-11 17:50:32 +00006404 E->getValueKind(), ToFoundD, ToResInfo, E->isNonOdrUse());
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006405 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006406 ToE->setHadMultipleCandidates(true);
6407 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006408}
6409
Balazs Keri3b30d652018-10-19 13:32:20 +00006410ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6411 ExpectedType TypeOrErr = import(E->getType());
6412 if (!TypeOrErr)
6413 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006414
Balazs Keri3b30d652018-10-19 13:32:20 +00006415 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006416}
6417
Balazs Keri3b30d652018-10-19 13:32:20 +00006418ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6419 ExpectedExpr ToInitOrErr = import(E->getInit());
6420 if (!ToInitOrErr)
6421 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006422
Balazs Keri3b30d652018-10-19 13:32:20 +00006423 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6424 if (!ToEqualOrColonLocOrErr)
6425 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006426
Balazs Keri3b30d652018-10-19 13:32:20 +00006427 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006428 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006429 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6430 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6431 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006432 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006433 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006434 }
6435
Balazs Keri3b30d652018-10-19 13:32:20 +00006436 SmallVector<Designator, 4> ToDesignators(E->size());
6437 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6438 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006439
6440 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006441 Importer.getToContext(), ToDesignators,
6442 ToIndexExprs, *ToEqualOrColonLocOrErr,
6443 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006444}
6445
Balazs Keri3b30d652018-10-19 13:32:20 +00006446ExpectedStmt
6447ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6448 ExpectedType ToTypeOrErr = import(E->getType());
6449 if (!ToTypeOrErr)
6450 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006451
Balazs Keri3b30d652018-10-19 13:32:20 +00006452 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6453 if (!ToLocationOrErr)
6454 return ToLocationOrErr.takeError();
6455
6456 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6457 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006458}
6459
Balazs Keri3b30d652018-10-19 13:32:20 +00006460ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6461 ExpectedType ToTypeOrErr = import(E->getType());
6462 if (!ToTypeOrErr)
6463 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006464
Balazs Keri3b30d652018-10-19 13:32:20 +00006465 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6466 if (!ToLocationOrErr)
6467 return ToLocationOrErr.takeError();
6468
6469 return IntegerLiteral::Create(
6470 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006471}
6472
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006473
Balazs Keri3b30d652018-10-19 13:32:20 +00006474ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6475 ExpectedType ToTypeOrErr = import(E->getType());
6476 if (!ToTypeOrErr)
6477 return ToTypeOrErr.takeError();
6478
6479 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6480 if (!ToLocationOrErr)
6481 return ToLocationOrErr.takeError();
6482
6483 return FloatingLiteral::Create(
6484 Importer.getToContext(), E->getValue(), E->isExact(),
6485 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006486}
6487
Balazs Keri3b30d652018-10-19 13:32:20 +00006488ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6489 auto ToTypeOrErr = import(E->getType());
6490 if (!ToTypeOrErr)
6491 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006492
Balazs Keri3b30d652018-10-19 13:32:20 +00006493 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6494 if (!ToSubExprOrErr)
6495 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006496
Balazs Keri3b30d652018-10-19 13:32:20 +00006497 return new (Importer.getToContext()) ImaginaryLiteral(
6498 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006499}
6500
Balazs Keri3b30d652018-10-19 13:32:20 +00006501ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6502 ExpectedType ToTypeOrErr = import(E->getType());
6503 if (!ToTypeOrErr)
6504 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006505
Balazs Keri3b30d652018-10-19 13:32:20 +00006506 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6507 if (!ToLocationOrErr)
6508 return ToLocationOrErr.takeError();
6509
6510 return new (Importer.getToContext()) CharacterLiteral(
6511 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006512}
6513
Balazs Keri3b30d652018-10-19 13:32:20 +00006514ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6515 ExpectedType ToTypeOrErr = import(E->getType());
6516 if (!ToTypeOrErr)
6517 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006518
Balazs Keri3b30d652018-10-19 13:32:20 +00006519 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6520 if (Error Err = ImportArrayChecked(
6521 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6522 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006523
Balazs Keri3b30d652018-10-19 13:32:20 +00006524 return StringLiteral::Create(
6525 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6526 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006527}
6528
Balazs Keri3b30d652018-10-19 13:32:20 +00006529ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6530 auto Imp = importSeq(
6531 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6532 E->getInitializer());
6533 if (!Imp)
6534 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006535
Balazs Keri3b30d652018-10-19 13:32:20 +00006536 SourceLocation ToLParenLoc;
6537 TypeSourceInfo *ToTypeSourceInfo;
6538 QualType ToType;
6539 Expr *ToInitializer;
6540 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006541
6542 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006543 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6544 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006545}
6546
Balazs Keri3b30d652018-10-19 13:32:20 +00006547ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6548 auto Imp = importSeq(
6549 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6550 if (!Imp)
6551 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006552
Balazs Keri3b30d652018-10-19 13:32:20 +00006553 SourceLocation ToBuiltinLoc, ToRParenLoc;
6554 QualType ToType;
6555 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6556
6557 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6558 if (Error Err = ImportArrayChecked(
6559 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6560 ToExprs.begin()))
6561 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006562
6563 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006564 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006565}
6566
Balazs Keri3b30d652018-10-19 13:32:20 +00006567ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6568 auto Imp = importSeq(
6569 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6570 if (!Imp)
6571 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006572
Balazs Keri3b30d652018-10-19 13:32:20 +00006573 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6574 LabelDecl *ToLabel;
6575 QualType ToType;
6576 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006577
6578 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006579 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006580}
6581
Bill Wendling8003edc2018-11-09 00:41:36 +00006582ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) {
6583 auto Imp = importSeq(E->getSubExpr());
6584 if (!Imp)
6585 return Imp.takeError();
6586
6587 Expr *ToSubExpr;
6588 std::tie(ToSubExpr) = *Imp;
6589
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00006590 // TODO : Handle APValue::ValueKind that require importing.
6591 APValue::ValueKind Kind = E->getResultAPValueKind();
6592 if (Kind == APValue::Int || Kind == APValue::Float ||
6593 Kind == APValue::FixedPoint || Kind == APValue::ComplexFloat ||
6594 Kind == APValue::ComplexInt)
6595 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr,
6596 E->getAPValueResult());
Fangrui Song407659a2018-11-30 23:41:18 +00006597 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr);
Bill Wendling8003edc2018-11-09 00:41:36 +00006598}
6599
Balazs Keri3b30d652018-10-19 13:32:20 +00006600ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6601 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6602 if (!Imp)
6603 return Imp.takeError();
6604
6605 SourceLocation ToLParen, ToRParen;
6606 Expr *ToSubExpr;
6607 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006608
Fangrui Song6907ce22018-07-30 19:24:48 +00006609 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006610 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006611}
6612
Balazs Keri3b30d652018-10-19 13:32:20 +00006613ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6614 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6615 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6616 return std::move(Err);
6617
6618 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6619 if (!ToLParenLocOrErr)
6620 return ToLParenLocOrErr.takeError();
6621
6622 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6623 if (!ToRParenLocOrErr)
6624 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006625
Bruno Riccif49e1ca2018-11-20 16:20:40 +00006626 return ParenListExpr::Create(Importer.getToContext(), *ToLParenLocOrErr,
6627 ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006628}
6629
Balazs Keri3b30d652018-10-19 13:32:20 +00006630ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6631 auto Imp = importSeq(
6632 E->getSubStmt(), E->getType(), E->getLParenLoc(), E->getRParenLoc());
6633 if (!Imp)
6634 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006635
Balazs Keri3b30d652018-10-19 13:32:20 +00006636 CompoundStmt *ToSubStmt;
6637 QualType ToType;
6638 SourceLocation ToLParenLoc, ToRParenLoc;
6639 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006640
Balazs Keri3b30d652018-10-19 13:32:20 +00006641 return new (Importer.getToContext()) StmtExpr(
6642 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006643}
6644
Balazs Keri3b30d652018-10-19 13:32:20 +00006645ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6646 auto Imp = importSeq(
6647 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6648 if (!Imp)
6649 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006650
Balazs Keri3b30d652018-10-19 13:32:20 +00006651 Expr *ToSubExpr;
6652 QualType ToType;
6653 SourceLocation ToOperatorLoc;
6654 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006655
Aaron Ballmana5038552018-01-09 13:07:03 +00006656 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006657 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6658 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006659}
6660
Balazs Keri3b30d652018-10-19 13:32:20 +00006661ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006662ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006663 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6664 if (!Imp)
6665 return Imp.takeError();
6666
6667 QualType ToType;
6668 SourceLocation ToOperatorLoc, ToRParenLoc;
6669 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006670
Douglas Gregord8552cd2010-02-19 01:24:23 +00006671 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006672 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6673 import(E->getArgumentTypeInfo());
6674 if (!ToArgumentTypeInfoOrErr)
6675 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006676
Balazs Keri3b30d652018-10-19 13:32:20 +00006677 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6678 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6679 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006680 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006681
Balazs Keri3b30d652018-10-19 13:32:20 +00006682 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6683 if (!ToArgumentExprOrErr)
6684 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006685
Balazs Keri3b30d652018-10-19 13:32:20 +00006686 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6687 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006688}
6689
Balazs Keri3b30d652018-10-19 13:32:20 +00006690ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6691 auto Imp = importSeq(
6692 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6693 if (!Imp)
6694 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006695
Balazs Keri3b30d652018-10-19 13:32:20 +00006696 Expr *ToLHS, *ToRHS;
6697 QualType ToType;
6698 SourceLocation ToOperatorLoc;
6699 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006700
Balazs Keri3b30d652018-10-19 13:32:20 +00006701 return new (Importer.getToContext()) BinaryOperator(
6702 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6703 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006704}
6705
Balazs Keri3b30d652018-10-19 13:32:20 +00006706ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6707 auto Imp = importSeq(
6708 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6709 E->getRHS(), E->getType());
6710 if (!Imp)
6711 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006712
Balazs Keri3b30d652018-10-19 13:32:20 +00006713 Expr *ToCond, *ToLHS, *ToRHS;
6714 SourceLocation ToQuestionLoc, ToColonLoc;
6715 QualType ToType;
6716 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006717
6718 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006719 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6720 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006721}
6722
Balazs Keri3b30d652018-10-19 13:32:20 +00006723ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006724 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006725 auto Imp = importSeq(
6726 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6727 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6728 if (!Imp)
6729 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006730
Balazs Keri3b30d652018-10-19 13:32:20 +00006731 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6732 OpaqueValueExpr *ToOpaqueValue;
6733 SourceLocation ToQuestionLoc, ToColonLoc;
6734 QualType ToType;
6735 std::tie(
6736 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6737 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006738
6739 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006740 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6741 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6742 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006743}
6744
Balazs Keri3b30d652018-10-19 13:32:20 +00006745ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6746 auto Imp = importSeq(
6747 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6748 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6749 if (!Imp)
6750 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006751
Balazs Keri3b30d652018-10-19 13:32:20 +00006752 SourceLocation ToBeginLoc, ToEndLoc;
6753 TypeSourceInfo *ToQueriedTypeSourceInfo;
6754 Expr *ToDimensionExpression;
6755 QualType ToType;
6756 std::tie(
6757 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6758 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006759
6760 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006761 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6762 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006763}
6764
Balazs Keri3b30d652018-10-19 13:32:20 +00006765ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6766 auto Imp = importSeq(
6767 E->getBeginLoc(), E->getQueriedExpression(), E->getEndLoc(), E->getType());
6768 if (!Imp)
6769 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006770
Balazs Keri3b30d652018-10-19 13:32:20 +00006771 SourceLocation ToBeginLoc, ToEndLoc;
6772 Expr *ToQueriedExpression;
6773 QualType ToType;
6774 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006775
6776 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006777 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6778 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006779}
6780
Balazs Keri3b30d652018-10-19 13:32:20 +00006781ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6782 auto Imp = importSeq(
6783 E->getLocation(), E->getType(), E->getSourceExpr());
6784 if (!Imp)
6785 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006786
Balazs Keri3b30d652018-10-19 13:32:20 +00006787 SourceLocation ToLocation;
6788 QualType ToType;
6789 Expr *ToSourceExpr;
6790 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006791
6792 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006793 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006794}
6795
Balazs Keri3b30d652018-10-19 13:32:20 +00006796ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6797 auto Imp = importSeq(
6798 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6799 if (!Imp)
6800 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006801
Balazs Keri3b30d652018-10-19 13:32:20 +00006802 Expr *ToLHS, *ToRHS;
6803 SourceLocation ToRBracketLoc;
6804 QualType ToType;
6805 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006806
6807 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006808 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6809 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006810}
6811
Balazs Keri3b30d652018-10-19 13:32:20 +00006812ExpectedStmt
6813ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6814 auto Imp = importSeq(
6815 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6816 E->getComputationResultType(), E->getOperatorLoc());
6817 if (!Imp)
6818 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006819
Balazs Keri3b30d652018-10-19 13:32:20 +00006820 Expr *ToLHS, *ToRHS;
6821 QualType ToType, ToComputationLHSType, ToComputationResultType;
6822 SourceLocation ToOperatorLoc;
6823 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6824 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006825
Balazs Keri3b30d652018-10-19 13:32:20 +00006826 return new (Importer.getToContext()) CompoundAssignOperator(
6827 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6828 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6829 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006830}
6831
Balazs Keri3b30d652018-10-19 13:32:20 +00006832Expected<CXXCastPath>
6833ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6834 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006835 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006836 if (auto SpecOrErr = import(*I))
6837 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006838 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006839 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006840 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006841 return Path;
John McCallcf142162010-08-07 06:22:56 +00006842}
6843
Balazs Keri3b30d652018-10-19 13:32:20 +00006844ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6845 ExpectedType ToTypeOrErr = import(E->getType());
6846 if (!ToTypeOrErr)
6847 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006848
Balazs Keri3b30d652018-10-19 13:32:20 +00006849 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6850 if (!ToSubExprOrErr)
6851 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006852
Balazs Keri3b30d652018-10-19 13:32:20 +00006853 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6854 if (!ToBasePathOrErr)
6855 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006856
Balazs Keri3b30d652018-10-19 13:32:20 +00006857 return ImplicitCastExpr::Create(
6858 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6859 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006860}
6861
Balazs Keri3b30d652018-10-19 13:32:20 +00006862ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6863 auto Imp1 = importSeq(
6864 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6865 if (!Imp1)
6866 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006867
Balazs Keri3b30d652018-10-19 13:32:20 +00006868 QualType ToType;
6869 Expr *ToSubExpr;
6870 TypeSourceInfo *ToTypeInfoAsWritten;
6871 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006872
Balazs Keri3b30d652018-10-19 13:32:20 +00006873 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6874 if (!ToBasePathOrErr)
6875 return ToBasePathOrErr.takeError();
6876 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006877
Aleksei Sidorina693b372016-09-28 10:16:56 +00006878 switch (E->getStmtClass()) {
6879 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006880 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006881 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6882 if (!ToLParenLocOrErr)
6883 return ToLParenLocOrErr.takeError();
6884 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6885 if (!ToRParenLocOrErr)
6886 return ToRParenLocOrErr.takeError();
6887 return CStyleCastExpr::Create(
6888 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6889 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6890 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006891 }
6892
6893 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006894 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006895 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6896 if (!ToLParenLocOrErr)
6897 return ToLParenLocOrErr.takeError();
6898 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6899 if (!ToRParenLocOrErr)
6900 return ToRParenLocOrErr.takeError();
6901 return CXXFunctionalCastExpr::Create(
6902 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6903 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6904 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006905 }
6906
6907 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006908 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6909 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6910 if (!ToLParenLocOrErr)
6911 return ToLParenLocOrErr.takeError();
6912 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6913 if (!ToBridgeKeywordLocOrErr)
6914 return ToBridgeKeywordLocOrErr.takeError();
6915 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6916 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6917 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006918 }
6919 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006920 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006921 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006922 }
6923}
6924
Balazs Keri3b30d652018-10-19 13:32:20 +00006925ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
6926 SmallVector<OffsetOfNode, 4> ToNodes;
6927 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
6928 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006929
Balazs Keri3b30d652018-10-19 13:32:20 +00006930 SourceLocation ToBeginLoc, ToEndLoc;
6931 if (FromNode.getKind() != OffsetOfNode::Base) {
6932 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
6933 if (!Imp)
6934 return Imp.takeError();
6935 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
6936 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00006937
Balazs Keri3b30d652018-10-19 13:32:20 +00006938 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00006939 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00006940 ToNodes.push_back(
6941 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006942 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006943 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006944 auto ToBSOrErr = import(FromNode.getBase());
6945 if (!ToBSOrErr)
6946 return ToBSOrErr.takeError();
6947 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006948 break;
6949 }
6950 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006951 auto ToFieldOrErr = import(FromNode.getField());
6952 if (!ToFieldOrErr)
6953 return ToFieldOrErr.takeError();
6954 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006955 break;
6956 }
6957 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006958 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
6959 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006960 break;
6961 }
6962 }
6963 }
6964
Balazs Keri3b30d652018-10-19 13:32:20 +00006965 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
6966 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
6967 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
6968 if (!ToIndexExprOrErr)
6969 return ToIndexExprOrErr.takeError();
6970 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006971 }
6972
Balazs Keri3b30d652018-10-19 13:32:20 +00006973 auto Imp = importSeq(
6974 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
6975 E->getRParenLoc());
6976 if (!Imp)
6977 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006978
Balazs Keri3b30d652018-10-19 13:32:20 +00006979 QualType ToType;
6980 TypeSourceInfo *ToTypeSourceInfo;
6981 SourceLocation ToOperatorLoc, ToRParenLoc;
6982 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
6983
6984 return OffsetOfExpr::Create(
6985 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
6986 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006987}
6988
Balazs Keri3b30d652018-10-19 13:32:20 +00006989ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6990 auto Imp = importSeq(
6991 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
6992 if (!Imp)
6993 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006994
Balazs Keri3b30d652018-10-19 13:32:20 +00006995 QualType ToType;
6996 Expr *ToOperand;
6997 SourceLocation ToBeginLoc, ToEndLoc;
6998 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006999
Balazs Keri3b30d652018-10-19 13:32:20 +00007000 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007001 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00007002 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007003 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007004 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007005
Balazs Keri3b30d652018-10-19 13:32:20 +00007006 return new (Importer.getToContext()) CXXNoexceptExpr(
7007 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007008}
7009
Balazs Keri3b30d652018-10-19 13:32:20 +00007010ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
7011 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
7012 if (!Imp)
7013 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007014
Balazs Keri3b30d652018-10-19 13:32:20 +00007015 Expr *ToSubExpr;
7016 QualType ToType;
7017 SourceLocation ToThrowLoc;
7018 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007019
7020 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007021 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00007022}
7023
Balazs Keri3b30d652018-10-19 13:32:20 +00007024ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
7025 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
7026 if (!ToUsedLocOrErr)
7027 return ToUsedLocOrErr.takeError();
7028
7029 auto ToParamOrErr = import(E->getParam());
7030 if (!ToParamOrErr)
7031 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007032
Eric Fiselier708afb52019-05-16 21:04:15 +00007033 auto UsedContextOrErr = Importer.ImportContext(E->getUsedContext());
7034 if (!UsedContextOrErr)
7035 return UsedContextOrErr.takeError();
7036
Balazs Keric5095942019-08-14 09:41:39 +00007037 // Import the default arg if it was not imported yet.
7038 // This is needed because it can happen that during the import of the
7039 // default expression (from VisitParmVarDecl) the same ParmVarDecl is
7040 // encountered here. The default argument for a ParmVarDecl is set in the
7041 // ParmVarDecl only after it is imported (set in VisitParmVarDecl if not here,
7042 // see VisitParmVarDecl).
7043 ParmVarDecl *ToParam = *ToParamOrErr;
7044 if (!ToParam->getDefaultArg()) {
7045 Optional<ParmVarDecl *> FromParam = Importer.getImportedFromDecl(ToParam);
7046 assert(FromParam && "ParmVarDecl was not imported?");
7047
7048 if (Error Err = ImportDefaultArgOfParmVarDecl(*FromParam, ToParam))
7049 return std::move(Err);
7050 }
7051
7052 return CXXDefaultArgExpr::Create(Importer.getToContext(), *ToUsedLocOrErr,
7053 *ToParamOrErr, *UsedContextOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007054}
7055
Balazs Keri3b30d652018-10-19 13:32:20 +00007056ExpectedStmt
7057ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
7058 auto Imp = importSeq(
7059 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
7060 if (!Imp)
7061 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007062
Balazs Keri3b30d652018-10-19 13:32:20 +00007063 QualType ToType;
7064 TypeSourceInfo *ToTypeSourceInfo;
7065 SourceLocation ToRParenLoc;
7066 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007067
7068 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007069 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007070}
7071
Balazs Keri3b30d652018-10-19 13:32:20 +00007072ExpectedStmt
7073ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
7074 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7075 if (!ToSubExprOrErr)
7076 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007077
Balazs Keri3b30d652018-10-19 13:32:20 +00007078 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
7079 if (!ToDtorOrErr)
7080 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007081
7082 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00007083 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
7084 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007085}
7086
Balazs Keri3b30d652018-10-19 13:32:20 +00007087ExpectedStmt
7088ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
7089 auto Imp = importSeq(
7090 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
7091 E->getParenOrBraceRange());
7092 if (!Imp)
7093 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007094
Balazs Keri3b30d652018-10-19 13:32:20 +00007095 CXXConstructorDecl *ToConstructor;
7096 QualType ToType;
7097 TypeSourceInfo *ToTypeSourceInfo;
7098 SourceRange ToParenOrBraceRange;
7099 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007100
Balazs Keri3b30d652018-10-19 13:32:20 +00007101 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
7102 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7103 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007104
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00007105 return CXXTemporaryObjectExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007106 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
7107 ToParenOrBraceRange, E->hadMultipleCandidates(),
7108 E->isListInitialization(), E->isStdInitListInitialization(),
7109 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00007110}
7111
Tykerb0561b32019-11-17 11:41:55 +01007112ExpectedDecl ASTNodeImporter::VisitLifetimeExtendedTemporaryDecl(
7113 LifetimeExtendedTemporaryDecl *D) {
7114 DeclContext *DC, *LexicalDC;
7115 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
7116 return std::move(Err);
7117
7118 auto Imp = importSeq(D->getTemporaryExpr(), D->getExtendingDecl());
7119 // FIXME: the APValue should be imported as well if present.
7120 if (!Imp)
7121 return Imp.takeError();
7122
7123 Expr *Temporary;
7124 ValueDecl *ExtendingDecl;
7125 std::tie(Temporary, ExtendingDecl) = *Imp;
7126 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
7127
7128 LifetimeExtendedTemporaryDecl *To;
7129 if (GetImportedOrCreateDecl(To, D, Temporary, ExtendingDecl,
7130 D->getManglingNumber()))
7131 return To;
7132
7133 To->setLexicalDeclContext(LexicalDC);
7134 LexicalDC->addDeclInternal(To);
7135 return To;
7136}
7137
Balazs Keri3b30d652018-10-19 13:32:20 +00007138ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00007139ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Tykerb0561b32019-11-17 11:41:55 +01007140 auto Imp = importSeq(E->getType(),
7141 E->getLifetimeExtendedTemporaryDecl() ? nullptr
7142 : E->getSubExpr(),
7143 E->getLifetimeExtendedTemporaryDecl());
Balazs Keri3b30d652018-10-19 13:32:20 +00007144 if (!Imp)
7145 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007146
Balazs Keri3b30d652018-10-19 13:32:20 +00007147 QualType ToType;
7148 Expr *ToTemporaryExpr;
Tykerb0561b32019-11-17 11:41:55 +01007149 LifetimeExtendedTemporaryDecl *ToMaterializedDecl;
7150 std::tie(ToType, ToTemporaryExpr, ToMaterializedDecl) = *Imp;
7151 if (!ToTemporaryExpr)
7152 ToTemporaryExpr = cast<Expr>(ToMaterializedDecl->getTemporaryExpr());
Aleksei Sidorina693b372016-09-28 10:16:56 +00007153
Tykerb0561b32019-11-17 11:41:55 +01007154 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
7155 ToType, ToTemporaryExpr, E->isBoundToLvalueReference(),
7156 ToMaterializedDecl);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007157
Aleksei Sidorina693b372016-09-28 10:16:56 +00007158 return ToMTE;
7159}
7160
Balazs Keri3b30d652018-10-19 13:32:20 +00007161ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
7162 auto Imp = importSeq(
7163 E->getType(), E->getPattern(), E->getEllipsisLoc());
7164 if (!Imp)
7165 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00007166
Balazs Keri3b30d652018-10-19 13:32:20 +00007167 QualType ToType;
7168 Expr *ToPattern;
7169 SourceLocation ToEllipsisLoc;
7170 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00007171
7172 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007173 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00007174}
7175
Balazs Keri3b30d652018-10-19 13:32:20 +00007176ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
7177 auto Imp = importSeq(
7178 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
7179 if (!Imp)
7180 return Imp.takeError();
7181
7182 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
7183 NamedDecl *ToPack;
7184 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007185
7186 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007187 if (!E->isValueDependent())
7188 Length = E->getPackLength();
7189
Balazs Keri3b30d652018-10-19 13:32:20 +00007190 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007191 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007192 if (Error Err = ImportTemplateArguments(
7193 E->getPartialArguments().data(),
7194 E->getPartialArguments().size(),
7195 ToPartialArguments))
7196 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007197 }
7198
7199 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007200 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
7201 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007202}
7203
Aleksei Sidorina693b372016-09-28 10:16:56 +00007204
Balazs Keri3b30d652018-10-19 13:32:20 +00007205ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
7206 auto Imp = importSeq(
7207 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
7208 E->getArraySize(), E->getInitializer(), E->getType(),
7209 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
7210 E->getDirectInitRange());
7211 if (!Imp)
7212 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007213
Balazs Keri3b30d652018-10-19 13:32:20 +00007214 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
7215 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
Richard Smithb9fb1212019-05-06 03:47:15 +00007216 Optional<Expr *> ToArraySize;
7217 Expr *ToInitializer;
Balazs Keri3b30d652018-10-19 13:32:20 +00007218 QualType ToType;
7219 TypeSourceInfo *ToAllocatedTypeSourceInfo;
7220 std::tie(
7221 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
7222 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007223
Balazs Keri3b30d652018-10-19 13:32:20 +00007224 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
7225 if (Error Err =
7226 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
7227 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007228
Bruno Ricci9b6dfac2019-01-07 15:04:45 +00007229 return CXXNewExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007230 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
7231 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
7232 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
7233 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
7234 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007235}
7236
Balazs Keri3b30d652018-10-19 13:32:20 +00007237ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
7238 auto Imp = importSeq(
7239 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
7240 if (!Imp)
7241 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007242
Balazs Keri3b30d652018-10-19 13:32:20 +00007243 QualType ToType;
7244 FunctionDecl *ToOperatorDelete;
7245 Expr *ToArgument;
7246 SourceLocation ToBeginLoc;
7247 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00007248
7249 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007250 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
7251 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
7252 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00007253}
7254
Balazs Keri3b30d652018-10-19 13:32:20 +00007255ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
7256 auto Imp = importSeq(
7257 E->getType(), E->getLocation(), E->getConstructor(),
7258 E->getParenOrBraceRange());
7259 if (!Imp)
7260 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007261
Balazs Keri3b30d652018-10-19 13:32:20 +00007262 QualType ToType;
7263 SourceLocation ToLocation;
7264 CXXConstructorDecl *ToConstructor;
7265 SourceRange ToParenOrBraceRange;
7266 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007267
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007268 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007269 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7270 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007271
Balazs Keri3b30d652018-10-19 13:32:20 +00007272 return CXXConstructExpr::Create(
7273 Importer.getToContext(), ToType, ToLocation, ToConstructor,
7274 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
7275 E->isListInitialization(), E->isStdInitListInitialization(),
7276 E->requiresZeroInitialization(), E->getConstructionKind(),
7277 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00007278}
7279
Balazs Keri3b30d652018-10-19 13:32:20 +00007280ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
7281 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7282 if (!ToSubExprOrErr)
7283 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00007284
Balazs Keri3b30d652018-10-19 13:32:20 +00007285 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
7286 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
7287 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007288
Balazs Keri3b30d652018-10-19 13:32:20 +00007289 return ExprWithCleanups::Create(
7290 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
7291 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00007292}
7293
Balazs Keri3b30d652018-10-19 13:32:20 +00007294ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
7295 auto Imp = importSeq(
7296 E->getCallee(), E->getType(), E->getRParenLoc());
7297 if (!Imp)
7298 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007299
Balazs Keri3b30d652018-10-19 13:32:20 +00007300 Expr *ToCallee;
7301 QualType ToType;
7302 SourceLocation ToRParenLoc;
7303 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00007304
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007305 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007306 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7307 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007308
Bruno Riccic5885cf2018-12-21 15:20:32 +00007309 return CXXMemberCallExpr::Create(Importer.getToContext(), ToCallee, ToArgs,
7310 ToType, E->getValueKind(), ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00007311}
7312
Balazs Keri3b30d652018-10-19 13:32:20 +00007313ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
7314 ExpectedType ToTypeOrErr = import(E->getType());
7315 if (!ToTypeOrErr)
7316 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007317
Balazs Keri3b30d652018-10-19 13:32:20 +00007318 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7319 if (!ToLocationOrErr)
7320 return ToLocationOrErr.takeError();
7321
7322 return new (Importer.getToContext()) CXXThisExpr(
7323 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00007324}
7325
Balazs Keri3b30d652018-10-19 13:32:20 +00007326ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
7327 ExpectedType ToTypeOrErr = import(E->getType());
7328 if (!ToTypeOrErr)
7329 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007330
Balazs Keri3b30d652018-10-19 13:32:20 +00007331 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7332 if (!ToLocationOrErr)
7333 return ToLocationOrErr.takeError();
7334
7335 return new (Importer.getToContext()) CXXBoolLiteralExpr(
7336 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00007337}
7338
Balazs Keri3b30d652018-10-19 13:32:20 +00007339ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7340 auto Imp1 = importSeq(
7341 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7342 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7343 if (!Imp1)
7344 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007345
Balazs Keri3b30d652018-10-19 13:32:20 +00007346 Expr *ToBase;
7347 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7348 NestedNameSpecifierLoc ToQualifierLoc;
7349 ValueDecl *ToMemberDecl;
7350 QualType ToType;
7351 std::tie(
7352 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7353 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007354
Balazs Keri3b30d652018-10-19 13:32:20 +00007355 auto Imp2 = importSeq(
7356 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7357 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7358 if (!Imp2)
7359 return Imp2.takeError();
7360 NamedDecl *ToDecl;
7361 DeclarationName ToName;
7362 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7363 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007364
7365 DeclAccessPair ToFoundDecl =
7366 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007367
Balazs Keri3b30d652018-10-19 13:32:20 +00007368 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007369
Gabor Marton5caba302019-03-07 13:38:20 +00007370 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007371 if (E->hasExplicitTemplateArgs()) {
Gabor Marton5caba302019-03-07 13:38:20 +00007372 if (Error Err =
7373 ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
7374 E->template_arguments(), ToTAInfo))
7375 return std::move(Err);
7376 ResInfo = &ToTAInfo;
Sean Callanan59721b32015-04-28 18:41:46 +00007377 }
7378
Richard Smith1bbad592019-06-11 17:50:36 +00007379 return MemberExpr::Create(Importer.getToContext(), ToBase, E->isArrow(),
7380 ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7381 ToMemberDecl, ToFoundDecl, ToMemberNameInfo,
7382 ResInfo, ToType, E->getValueKind(),
7383 E->getObjectKind(), E->isNonOdrUse());
Sean Callanan59721b32015-04-28 18:41:46 +00007384}
7385
Balazs Keri3b30d652018-10-19 13:32:20 +00007386ExpectedStmt
7387ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7388 auto Imp = importSeq(
7389 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7390 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7391 if (!Imp)
7392 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007393
Balazs Keri3b30d652018-10-19 13:32:20 +00007394 Expr *ToBase;
7395 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7396 NestedNameSpecifierLoc ToQualifierLoc;
7397 TypeSourceInfo *ToScopeTypeInfo;
7398 std::tie(
7399 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7400 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007401
7402 PseudoDestructorTypeStorage Storage;
7403 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7404 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007405 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7406 if (!ToDestroyedTypeLocOrErr)
7407 return ToDestroyedTypeLocOrErr.takeError();
7408 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007409 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007410 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7411 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7412 else
7413 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007414 }
7415
7416 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007417 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7418 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007419}
7420
Balazs Keri3b30d652018-10-19 13:32:20 +00007421ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007422 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007423 auto Imp = importSeq(
7424 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7425 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7426 if (!Imp)
7427 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007428
Balazs Keri3b30d652018-10-19 13:32:20 +00007429 QualType ToType;
7430 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7431 NestedNameSpecifierLoc ToQualifierLoc;
7432 NamedDecl *ToFirstQualifierFoundInScope;
7433 std::tie(
7434 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7435 ToFirstQualifierFoundInScope) = *Imp;
7436
7437 Expr *ToBase = nullptr;
7438 if (!E->isImplicitAccess()) {
7439 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7440 ToBase = *ToBaseOrErr;
7441 else
7442 return ToBaseOrErr.takeError();
7443 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007444
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007445 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007446 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007447 if (Error Err = ImportTemplateArgumentListInfo(
7448 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7449 ToTAInfo))
7450 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007451 ResInfo = &ToTAInfo;
7452 }
7453
Balazs Keri3b30d652018-10-19 13:32:20 +00007454 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7455 if (!ToMemberNameInfoOrErr)
7456 return ToMemberNameInfoOrErr.takeError();
7457 DeclarationNameInfo ToMemberNameInfo(
7458 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007459 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007460 if (Error Err = ImportDeclarationNameLoc(
7461 E->getMemberNameInfo(), ToMemberNameInfo))
7462 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007463
7464 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007465 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7466 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7467 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007468}
7469
Balazs Keri3b30d652018-10-19 13:32:20 +00007470ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007471ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balázs Kéria9f10eb2019-12-05 16:21:21 +01007472 auto Imp = importSeq(E->getQualifierLoc(), E->getTemplateKeywordLoc(),
7473 E->getDeclName(), E->getNameInfo().getLoc(),
7474 E->getLAngleLoc(), E->getRAngleLoc());
Balazs Keri3b30d652018-10-19 13:32:20 +00007475 if (!Imp)
7476 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007477
Balazs Keri3b30d652018-10-19 13:32:20 +00007478 NestedNameSpecifierLoc ToQualifierLoc;
Balázs Kéria9f10eb2019-12-05 16:21:21 +01007479 SourceLocation ToTemplateKeywordLoc, ToNameLoc, ToLAngleLoc, ToRAngleLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00007480 DeclarationName ToDeclName;
Balázs Kéria9f10eb2019-12-05 16:21:21 +01007481 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToNameLoc,
7482 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007483
Balázs Kéria9f10eb2019-12-05 16:21:21 +01007484 DeclarationNameInfo ToNameInfo(ToDeclName, ToNameLoc);
Balazs Keri3b30d652018-10-19 13:32:20 +00007485 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7486 return std::move(Err);
7487
7488 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007489 TemplateArgumentListInfo *ResInfo = nullptr;
7490 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007491 if (Error Err =
7492 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7493 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007494 ResInfo = &ToTAInfo;
7495 }
7496
7497 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007498 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7499 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007500}
7501
Balazs Keri3b30d652018-10-19 13:32:20 +00007502ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7503 CXXUnresolvedConstructExpr *E) {
7504 auto Imp = importSeq(
7505 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7506 if (!Imp)
7507 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007508
Balazs Keri3b30d652018-10-19 13:32:20 +00007509 SourceLocation ToLParenLoc, ToRParenLoc;
7510 TypeSourceInfo *ToTypeSourceInfo;
7511 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7512
7513 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7514 if (Error Err =
7515 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7516 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007517
7518 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007519 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7520 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007521}
7522
Balazs Keri3b30d652018-10-19 13:32:20 +00007523ExpectedStmt
7524ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7525 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7526 if (!ToNamingClassOrErr)
7527 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007528
Balazs Keri3b30d652018-10-19 13:32:20 +00007529 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7530 if (!ToQualifierLocOrErr)
7531 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007532
Balazs Keri3b30d652018-10-19 13:32:20 +00007533 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7534 if (!ToNameInfoOrErr)
7535 return ToNameInfoOrErr.takeError();
7536 DeclarationNameInfo ToNameInfo(
7537 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007538 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007539 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7540 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007541
7542 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007543 for (auto *D : E->decls())
7544 if (auto ToDOrErr = import(D))
7545 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007546 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007547 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007548
Balázs Kéria9f10eb2019-12-05 16:21:21 +01007549 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007550 TemplateArgumentListInfo ToTAInfo;
7551 if (Error Err = ImportTemplateArgumentListInfo(
7552 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7553 ToTAInfo))
7554 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007555
Balazs Keri3b30d652018-10-19 13:32:20 +00007556 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7557 if (!ToTemplateKeywordLocOrErr)
7558 return ToTemplateKeywordLocOrErr.takeError();
7559
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007560 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007561 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7562 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7563 ToDecls.begin(), ToDecls.end());
7564 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007565
7566 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007567 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7568 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7569 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007570}
7571
Balazs Keri3b30d652018-10-19 13:32:20 +00007572ExpectedStmt
7573ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7574 auto Imp1 = importSeq(
7575 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7576 E->getTemplateKeywordLoc());
7577 if (!Imp1)
7578 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007579
Balazs Keri3b30d652018-10-19 13:32:20 +00007580 QualType ToType;
7581 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7582 NestedNameSpecifierLoc ToQualifierLoc;
7583 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7584
7585 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7586 if (!Imp2)
7587 return Imp2.takeError();
7588 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7589 // Import additional name location/type info.
7590 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7591 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007592
7593 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007594 for (Decl *D : E->decls())
7595 if (auto ToDOrErr = import(D))
7596 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007597 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007598 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007599
7600 TemplateArgumentListInfo ToTAInfo;
7601 TemplateArgumentListInfo *ResInfo = nullptr;
7602 if (E->hasExplicitTemplateArgs()) {
Balázs Kéria9f10eb2019-12-05 16:21:21 +01007603 TemplateArgumentListInfo FromTAInfo;
7604 E->copyTemplateArgumentsInto(FromTAInfo);
7605 if (Error Err = ImportTemplateArgumentListInfo(FromTAInfo, ToTAInfo))
Balazs Keri3b30d652018-10-19 13:32:20 +00007606 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007607 ResInfo = &ToTAInfo;
7608 }
7609
Balazs Keri3b30d652018-10-19 13:32:20 +00007610 Expr *ToBase = nullptr;
7611 if (!E->isImplicitAccess()) {
7612 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7613 ToBase = *ToBaseOrErr;
7614 else
7615 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007616 }
7617
7618 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007619 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7620 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7621 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007622}
7623
Balazs Keri3b30d652018-10-19 13:32:20 +00007624ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7625 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7626 if (!Imp)
7627 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007628
Balazs Keri3b30d652018-10-19 13:32:20 +00007629 Expr *ToCallee;
7630 QualType ToType;
7631 SourceLocation ToRParenLoc;
7632 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007633
7634 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007635 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7636 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7637 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007638
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007639 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
Bruno Riccic5885cf2018-12-21 15:20:32 +00007640 return CXXOperatorCallExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007641 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
Eric Fiselier5cdc2cd2018-12-12 21:50:55 +00007642 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures(),
7643 OCE->getADLCallKind());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007644 }
7645
Bruno Riccic5885cf2018-12-21 15:20:32 +00007646 return CallExpr::Create(Importer.getToContext(), ToCallee, ToArgs, ToType,
7647 E->getValueKind(), ToRParenLoc, /*MinNumArgs=*/0,
7648 E->getADLCallKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007649}
7650
Balazs Keri3b30d652018-10-19 13:32:20 +00007651ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7652 CXXRecordDecl *FromClass = E->getLambdaClass();
7653 auto ToClassOrErr = import(FromClass);
7654 if (!ToClassOrErr)
7655 return ToClassOrErr.takeError();
7656 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007657
Balazs Keri3b30d652018-10-19 13:32:20 +00007658 auto ToCallOpOrErr = import(E->getCallOperator());
7659 if (!ToCallOpOrErr)
7660 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007661
Balazs Keri3b30d652018-10-19 13:32:20 +00007662 SmallVector<LambdaCapture, 8> ToCaptures;
7663 ToCaptures.reserve(E->capture_size());
7664 for (const auto &FromCapture : E->captures()) {
7665 if (auto ToCaptureOrErr = import(FromCapture))
7666 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007667 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007668 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007669 }
7670
Balazs Keri3b30d652018-10-19 13:32:20 +00007671 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7672 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7673 return std::move(Err);
7674
7675 auto Imp = importSeq(
7676 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7677 if (!Imp)
7678 return Imp.takeError();
7679
7680 SourceRange ToIntroducerRange;
7681 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7682 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007683
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007684 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007685 Importer.getToContext(), ToClass, ToIntroducerRange,
7686 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7687 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7688 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007689}
7690
Sean Callanan8bca9962016-03-28 21:43:01 +00007691
Balazs Keri3b30d652018-10-19 13:32:20 +00007692ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7693 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7694 if (!Imp)
7695 return Imp.takeError();
7696
7697 SourceLocation ToLBraceLoc, ToRBraceLoc;
7698 QualType ToType;
7699 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7700
7701 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7702 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7703 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007704
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007705 ASTContext &ToCtx = Importer.getToContext();
7706 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007707 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7708 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007709
Balazs Keri3b30d652018-10-19 13:32:20 +00007710 if (E->hasArrayFiller()) {
7711 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7712 To->setArrayFiller(*ToFillerOrErr);
7713 else
7714 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007715 }
7716
Balazs Keri3b30d652018-10-19 13:32:20 +00007717 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7718 if (auto ToFDOrErr = import(FromFD))
7719 To->setInitializedFieldInUnion(*ToFDOrErr);
7720 else
7721 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007722 }
7723
Balazs Keri3b30d652018-10-19 13:32:20 +00007724 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7725 if (auto ToSyntFormOrErr = import(SyntForm))
7726 To->setSyntacticForm(*ToSyntFormOrErr);
7727 else
7728 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007729 }
7730
Gabor Martona20ce602018-09-03 13:10:53 +00007731 // Copy InitListExprBitfields, which are not handled in the ctor of
7732 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007733 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007734
7735 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007736}
7737
Balazs Keri3b30d652018-10-19 13:32:20 +00007738ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007739 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007740 ExpectedType ToTypeOrErr = import(E->getType());
7741 if (!ToTypeOrErr)
7742 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007743
Balazs Keri3b30d652018-10-19 13:32:20 +00007744 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7745 if (!ToSubExprOrErr)
7746 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007747
Balazs Keri3b30d652018-10-19 13:32:20 +00007748 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7749 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007750}
7751
Balazs Keri3b30d652018-10-19 13:32:20 +00007752ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007753 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007754 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7755 if (!Imp)
7756 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007757
Balazs Keri3b30d652018-10-19 13:32:20 +00007758 SourceLocation ToLocation;
7759 QualType ToType;
7760 CXXConstructorDecl *ToConstructor;
7761 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007762
7763 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007764 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7765 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007766}
7767
Balazs Keri3b30d652018-10-19 13:32:20 +00007768ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7769 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7770 if (!Imp)
7771 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007772
Balazs Keri3b30d652018-10-19 13:32:20 +00007773 QualType ToType;
7774 Expr *ToCommonExpr, *ToSubExpr;
7775 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007776
Balazs Keri3b30d652018-10-19 13:32:20 +00007777 return new (Importer.getToContext()) ArrayInitLoopExpr(
7778 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007779}
7780
Balazs Keri3b30d652018-10-19 13:32:20 +00007781ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7782 ExpectedType ToTypeOrErr = import(E->getType());
7783 if (!ToTypeOrErr)
7784 return ToTypeOrErr.takeError();
7785 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007786}
7787
Balazs Keri3b30d652018-10-19 13:32:20 +00007788ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7789 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7790 if (!ToBeginLocOrErr)
7791 return ToBeginLocOrErr.takeError();
7792
7793 auto ToFieldOrErr = import(E->getField());
7794 if (!ToFieldOrErr)
7795 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007796
Eric Fiselier708afb52019-05-16 21:04:15 +00007797 auto UsedContextOrErr = Importer.ImportContext(E->getUsedContext());
7798 if (!UsedContextOrErr)
7799 return UsedContextOrErr.takeError();
7800
Sean Callanandd2c1742016-05-16 20:48:03 +00007801 return CXXDefaultInitExpr::Create(
Eric Fiselier708afb52019-05-16 21:04:15 +00007802 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr, *UsedContextOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007803}
7804
Balazs Keri3b30d652018-10-19 13:32:20 +00007805ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7806 auto Imp = importSeq(
7807 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7808 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7809 if (!Imp)
7810 return Imp.takeError();
7811
7812 QualType ToType;
7813 Expr *ToSubExpr;
7814 TypeSourceInfo *ToTypeInfoAsWritten;
7815 SourceLocation ToOperatorLoc, ToRParenLoc;
7816 SourceRange ToAngleBrackets;
7817 std::tie(
7818 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7819 ToAngleBrackets) = *Imp;
7820
Sean Callanandd2c1742016-05-16 20:48:03 +00007821 ExprValueKind VK = E->getValueKind();
7822 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007823 auto ToBasePathOrErr = ImportCastPath(E);
7824 if (!ToBasePathOrErr)
7825 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007826
Sean Callanandd2c1742016-05-16 20:48:03 +00007827 if (isa<CXXStaticCastExpr>(E)) {
7828 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007829 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7830 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007831 } else if (isa<CXXDynamicCastExpr>(E)) {
7832 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007833 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7834 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007835 } else if (isa<CXXReinterpretCastExpr>(E)) {
7836 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007837 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7838 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007839 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007840 return CXXConstCastExpr::Create(
7841 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7842 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007843 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007844 llvm_unreachable("Unknown cast type");
7845 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007846 }
7847}
7848
Balazs Keri3b30d652018-10-19 13:32:20 +00007849ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007850 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007851 auto Imp = importSeq(
7852 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7853 if (!Imp)
7854 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007855
Balazs Keri3b30d652018-10-19 13:32:20 +00007856 QualType ToType;
7857 SourceLocation ToExprLoc;
7858 NonTypeTemplateParmDecl *ToParameter;
7859 Expr *ToReplacement;
7860 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007861
7862 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007863 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007864}
7865
Balazs Keri3b30d652018-10-19 13:32:20 +00007866ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7867 auto Imp = importSeq(
7868 E->getType(), E->getBeginLoc(), E->getEndLoc());
7869 if (!Imp)
7870 return Imp.takeError();
7871
7872 QualType ToType;
7873 SourceLocation ToBeginLoc, ToEndLoc;
7874 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007875
7876 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007877 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7878 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007879
7880 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7881 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007882 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007883
7884 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007885 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7886 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007887}
7888
Balazs Keri3b30d652018-10-19 13:32:20 +00007889ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7890 ExpectedType ToTypeOrErr = import(E->getType());
7891 if (!ToTypeOrErr)
7892 return ToTypeOrErr.takeError();
7893
7894 auto ToSourceRangeOrErr = import(E->getSourceRange());
7895 if (!ToSourceRangeOrErr)
7896 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007897
7898 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007899 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7900 return new (Importer.getToContext()) CXXTypeidExpr(
7901 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7902 else
7903 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007904 }
7905
Balazs Keri3b30d652018-10-19 13:32:20 +00007906 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7907 if (!ToExprOperandOrErr)
7908 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007909
Balazs Keri3b30d652018-10-19 13:32:20 +00007910 return new (Importer.getToContext()) CXXTypeidExpr(
7911 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007912}
7913
Balazs Kerib4fd7d42019-08-30 10:12:14 +00007914Error ASTNodeImporter::ImportOverriddenMethods(CXXMethodDecl *ToMethod,
7915 CXXMethodDecl *FromMethod) {
7916 Error ImportErrors = Error::success();
Balazs Keri3b30d652018-10-19 13:32:20 +00007917 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7918 if (auto ImportedOrErr = import(FromOverriddenMethod))
7919 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7920 (*ImportedOrErr)->getCanonicalDecl()));
7921 else
Balazs Kerib4fd7d42019-08-30 10:12:14 +00007922 ImportErrors =
7923 joinErrors(std::move(ImportErrors), ImportedOrErr.takeError());
Balazs Keri3b30d652018-10-19 13:32:20 +00007924 }
Balazs Kerib4fd7d42019-08-30 10:12:14 +00007925 return ImportErrors;
Lang Hames19e07e12017-06-20 21:06:00 +00007926}
7927
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007928ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00007929 ASTContext &FromContext, FileManager &FromFileManager,
Gabor Marton54058b52018-12-17 13:53:12 +00007930 bool MinimalImport,
Gabor Marton2afbfb62019-07-01 15:37:07 +00007931 std::shared_ptr<ASTImporterSharedState> SharedState)
7932 : SharedState(SharedState), ToContext(ToContext), FromContext(FromContext),
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007933 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Gabor Martonf035b752019-08-27 11:36:10 +00007934 Minimal(MinimalImport), ODRHandling(ODRHandlingType::Conservative) {
Gabor Marton54058b52018-12-17 13:53:12 +00007935
Gabor Marton2afbfb62019-07-01 15:37:07 +00007936 // Create a default state without the lookup table: LLDB case.
7937 if (!SharedState) {
7938 this->SharedState = std::make_shared<ASTImporterSharedState>();
7939 }
7940
Gabor Marton54058b52018-12-17 13:53:12 +00007941 ImportedDecls[FromContext.getTranslationUnitDecl()] =
7942 ToContext.getTranslationUnitDecl();
Douglas Gregor62d311f2010-02-09 19:21:46 +00007943}
7944
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007945ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007946
Gabor Marton54058b52018-12-17 13:53:12 +00007947Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) {
7948 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
7949 "Try to get field index for non-field.");
7950
7951 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
7952 if (!Owner)
7953 return None;
7954
7955 unsigned Index = 0;
7956 for (const auto *D : Owner->decls()) {
7957 if (D == F)
7958 return Index;
7959
7960 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
7961 ++Index;
7962 }
7963
7964 llvm_unreachable("Field was not found in its parent context.");
7965
7966 return None;
7967}
7968
7969ASTImporter::FoundDeclsTy
7970ASTImporter::findDeclsInToCtx(DeclContext *DC, DeclarationName Name) {
7971 // We search in the redecl context because of transparent contexts.
7972 // E.g. a simple C language enum is a transparent context:
7973 // enum E { A, B };
7974 // Now if we had a global variable in the TU
7975 // int A;
7976 // then the enum constant 'A' and the variable 'A' violates ODR.
7977 // We can diagnose this only if we search in the redecl context.
7978 DeclContext *ReDC = DC->getRedeclContext();
Gabor Marton2afbfb62019-07-01 15:37:07 +00007979 if (SharedState->getLookupTable()) {
Gabor Marton54058b52018-12-17 13:53:12 +00007980 ASTImporterLookupTable::LookupResult LookupResult =
Gabor Marton2afbfb62019-07-01 15:37:07 +00007981 SharedState->getLookupTable()->lookup(ReDC, Name);
Gabor Marton54058b52018-12-17 13:53:12 +00007982 return FoundDeclsTy(LookupResult.begin(), LookupResult.end());
7983 } else {
Gabor Martonaefcf512019-07-17 13:47:46 +00007984 DeclContext::lookup_result NoloadLookupResult = ReDC->noload_lookup(Name);
7985 FoundDeclsTy Result(NoloadLookupResult.begin(), NoloadLookupResult.end());
7986 // We must search by the slow case of localUncachedLookup because that is
7987 // working even if there is no LookupPtr for the DC. We could use
7988 // DC::buildLookup() to create the LookupPtr, but that would load external
7989 // decls again, we must avoid that case.
7990 // Also, even if we had the LookupPtr, we must find Decls which are not
7991 // in the LookupPtr, so we need the slow case.
7992 // These cases are handled in ASTImporterLookupTable, but we cannot use
7993 // that with LLDB since that traverses through the AST which initiates the
7994 // load of external decls again via DC::decls(). And again, we must avoid
7995 // loading external decls during the import.
7996 if (Result.empty())
7997 ReDC->localUncachedLookup(Name, Result);
Gabor Marton54058b52018-12-17 13:53:12 +00007998 return Result;
7999 }
8000}
8001
8002void ASTImporter::AddToLookupTable(Decl *ToD) {
Gabor Marton2afbfb62019-07-01 15:37:07 +00008003 SharedState->addDeclToLookup(ToD);
Gabor Marton54058b52018-12-17 13:53:12 +00008004}
8005
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00008006Expected<Decl *> ASTImporter::ImportImpl(Decl *FromD) {
8007 // Import the decl using ASTNodeImporter.
8008 ASTNodeImporter Importer(*this);
8009 return Importer.Visit(FromD);
8010}
8011
8012void ASTImporter::RegisterImportedDecl(Decl *FromD, Decl *ToD) {
8013 MapImported(FromD, ToD);
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00008014}
8015
Gabor Marton5ac6d492019-05-15 10:29:48 +00008016Expected<QualType> ASTImporter::Import(QualType FromT) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008017 if (FromT.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008018 return QualType{};
John McCall424cec92011-01-19 06:33:43 +00008019
Balazs Keri3b30d652018-10-19 13:32:20 +00008020 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00008021
8022 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00008023 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00008024 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00008025 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00008026 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00008027
Douglas Gregorf65bbb32010-02-08 15:18:58 +00008028 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00008029 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00008030 ExpectedType ToTOrErr = Importer.Visit(FromTy);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008031 if (!ToTOrErr)
8032 return ToTOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008033
Douglas Gregorf65bbb32010-02-08 15:18:58 +00008034 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00008035 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00008036
Balazs Keri3b30d652018-10-19 13:32:20 +00008037 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00008038}
8039
Gabor Marton5ac6d492019-05-15 10:29:48 +00008040Expected<TypeSourceInfo *> ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00008041 if (!FromTSI)
8042 return FromTSI;
8043
8044 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00008045 // on the type and a single location. Implement a real version of this.
Gabor Marton5ac6d492019-05-15 10:29:48 +00008046 ExpectedType TOrErr = Import(FromTSI->getType());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008047 if (!TOrErr)
8048 return TOrErr.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008049 ExpectedSLoc BeginLocOrErr = Import(FromTSI->getTypeLoc().getBeginLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008050 if (!BeginLocOrErr)
8051 return BeginLocOrErr.takeError();
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00008052
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008053 return ToContext.getTrivialTypeSourceInfo(*TOrErr, *BeginLocOrErr);
8054}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008055
Gabor Marton5ac6d492019-05-15 10:29:48 +00008056Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) {
Davide Italianofaee83d2018-11-28 19:15:23 +00008057 Attr *ToAttr = FromAttr->clone(ToContext);
Gabor Marton5ac6d492019-05-15 10:29:48 +00008058 if (auto ToRangeOrErr = Import(FromAttr->getRange()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008059 ToAttr->setRange(*ToRangeOrErr);
8060 else
8061 return ToRangeOrErr.takeError();
8062
Davide Italianofaee83d2018-11-28 19:15:23 +00008063 return ToAttr;
Balazs Kerideaf7ab2018-11-28 13:21:26 +00008064}
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00008065
Gabor Martonbe77a982018-12-12 11:22:55 +00008066Decl *ASTImporter::GetAlreadyImportedOrNull(const Decl *FromD) const {
8067 auto Pos = ImportedDecls.find(FromD);
8068 if (Pos != ImportedDecls.end())
8069 return Pos->second;
8070 else
Sean Callanan59721b32015-04-28 18:41:46 +00008071 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00008072}
8073
Gabor Marton458d1452019-02-14 13:07:03 +00008074TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) {
8075 auto FromDPos = ImportedFromDecls.find(ToD);
8076 if (FromDPos == ImportedFromDecls.end())
8077 return nullptr;
8078 return FromDPos->second->getTranslationUnitDecl();
8079}
8080
Gabor Marton5ac6d492019-05-15 10:29:48 +00008081Expected<Decl *> ASTImporter::Import(Decl *FromD) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008082 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00008083 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00008084
Gabor Marton1ad4b992019-07-01 14:19:53 +00008085 // Push FromD to the stack, and remove that when we return.
8086 ImportPath.push(FromD);
8087 auto ImportPathBuilder =
8088 llvm::make_scope_exit([this]() { ImportPath.pop(); });
Douglas Gregord451ea92011-07-29 23:31:30 +00008089
Gabor Marton303c98612019-06-25 08:00:51 +00008090 // Check whether there was a previous failed import.
8091 // If yes return the existing error.
8092 if (auto Error = getImportDeclErrorIfAny(FromD))
8093 return make_error<ImportError>(*Error);
8094
Gabor Marton26f72a92018-07-12 09:42:05 +00008095 // Check whether we've already imported this declaration.
8096 Decl *ToD = GetAlreadyImportedOrNull(FromD);
8097 if (ToD) {
Gabor Marton2afbfb62019-07-01 15:37:07 +00008098 // Already imported (possibly from another TU) and with an error.
8099 if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) {
8100 setImportDeclError(FromD, *Error);
8101 return make_error<ImportError>(*Error);
8102 }
8103
Gabor Marton26f72a92018-07-12 09:42:05 +00008104 // If FromD has some updated flags after last import, apply it
8105 updateFlags(FromD, ToD);
Gabor Marton1ad4b992019-07-01 14:19:53 +00008106 // If we encounter a cycle during an import then we save the relevant part
8107 // of the import path associated to the Decl.
8108 if (ImportPath.hasCycleAtBack())
8109 SavedImportPaths[FromD].push_back(ImportPath.copyCycleAtBack());
Douglas Gregord451ea92011-07-29 23:31:30 +00008110 return ToD;
8111 }
Gabor Marton26f72a92018-07-12 09:42:05 +00008112
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008113 // Import the declaration.
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00008114 ExpectedDecl ToDOrErr = ImportImpl(FromD);
Gabor Marton303c98612019-06-25 08:00:51 +00008115 if (!ToDOrErr) {
8116 // Failed to import.
8117
8118 auto Pos = ImportedDecls.find(FromD);
8119 if (Pos != ImportedDecls.end()) {
8120 // Import failed after the object was created.
8121 // Remove all references to it.
8122 auto *ToD = Pos->second;
8123 ImportedDecls.erase(Pos);
8124
8125 // ImportedDecls and ImportedFromDecls are not symmetric. It may happen
8126 // (e.g. with namespaces) that several decls from the 'from' context are
8127 // mapped to the same decl in the 'to' context. If we removed entries
8128 // from the LookupTable here then we may end up removing them multiple
8129 // times.
8130
8131 // The Lookuptable contains decls only which are in the 'to' context.
8132 // Remove from the Lookuptable only if it is *imported* into the 'to'
8133 // context (and do not remove it if it was added during the initial
8134 // traverse of the 'to' context).
8135 auto PosF = ImportedFromDecls.find(ToD);
8136 if (PosF != ImportedFromDecls.end()) {
Gabor Marton2afbfb62019-07-01 15:37:07 +00008137 SharedState->removeDeclFromLookup(ToD);
Gabor Marton303c98612019-06-25 08:00:51 +00008138 ImportedFromDecls.erase(PosF);
8139 }
8140
8141 // FIXME: AST may contain remaining references to the failed object.
Gabor Marton2afbfb62019-07-01 15:37:07 +00008142 // However, the ImportDeclErrors in the shared state contains all the
8143 // failed objects together with their error.
Gabor Marton303c98612019-06-25 08:00:51 +00008144 }
8145
Gabor Marton2afbfb62019-07-01 15:37:07 +00008146 // Error encountered for the first time.
8147 // After takeError the error is not usable any more in ToDOrErr.
Gabor Marton303c98612019-06-25 08:00:51 +00008148 // Get a copy of the error object (any more simple solution for this?).
8149 ImportError ErrOut;
8150 handleAllErrors(ToDOrErr.takeError(),
8151 [&ErrOut](const ImportError &E) { ErrOut = E; });
8152 setImportDeclError(FromD, ErrOut);
Gabor Marton2afbfb62019-07-01 15:37:07 +00008153 // Set the error for the mapped to Decl, which is in the "to" context.
8154 if (Pos != ImportedDecls.end())
8155 SharedState->setImportDeclError(Pos->second, ErrOut);
Gabor Marton1ad4b992019-07-01 14:19:53 +00008156
8157 // Set the error for all nodes which have been created before we
8158 // recognized the error.
8159 for (const auto &Path : SavedImportPaths[FromD])
Gabor Marton2afbfb62019-07-01 15:37:07 +00008160 for (Decl *FromDi : Path) {
8161 setImportDeclError(FromDi, ErrOut);
8162 //FIXME Should we remove these Decls from ImportedDecls?
8163 // Set the error for the mapped to Decl, which is in the "to" context.
8164 auto Ii = ImportedDecls.find(FromDi);
8165 if (Ii != ImportedDecls.end())
8166 SharedState->setImportDeclError(Ii->second, ErrOut);
8167 // FIXME Should we remove these Decls from the LookupTable,
8168 // and from ImportedFromDecls?
8169 }
Gabor Marton1ad4b992019-07-01 14:19:53 +00008170 SavedImportPaths[FromD].clear();
8171
Gabor Marton303c98612019-06-25 08:00:51 +00008172 // Do not return ToDOrErr, error was taken out of it.
8173 return make_error<ImportError>(ErrOut);
8174 }
8175
Balazs Keri3b30d652018-10-19 13:32:20 +00008176 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00008177
Gabor Marton303c98612019-06-25 08:00:51 +00008178 // FIXME: Handle the "already imported with error" case. We can get here
8179 // nullptr only if GetImportedOrCreateDecl returned nullptr (after a
8180 // previously failed create was requested).
8181 // Later GetImportedOrCreateDecl can be updated to return the error.
Gabor Marton7f8c4002019-03-19 13:34:10 +00008182 if (!ToD) {
Gabor Marton303c98612019-06-25 08:00:51 +00008183 auto Err = getImportDeclErrorIfAny(FromD);
8184 assert(Err);
8185 return make_error<ImportError>(*Err);
Gabor Marton7f8c4002019-03-19 13:34:10 +00008186 }
8187
Gabor Marton2afbfb62019-07-01 15:37:07 +00008188 // We could import from the current TU without error. But previously we
8189 // already had imported a Decl as `ToD` from another TU (with another
8190 // ASTImporter object) and with an error.
8191 if (auto Error = SharedState->getImportDeclErrorIfAny(ToD)) {
8192 setImportDeclError(FromD, *Error);
8193 return make_error<ImportError>(*Error);
8194 }
8195
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00008196 // Make sure that ImportImpl registered the imported decl.
8197 assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?");
Gabor Marton2afbfb62019-07-01 15:37:07 +00008198
Gabor Marton26f72a92018-07-12 09:42:05 +00008199 // Notify subclasses.
8200 Imported(FromD, ToD);
8201
Gabor Martonac3a5d62018-09-17 12:04:52 +00008202 updateFlags(FromD, ToD);
Gabor Marton1ad4b992019-07-01 14:19:53 +00008203 SavedImportPaths[FromD].clear();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008204 return ToDOrErr;
8205}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008206
Balazs Keri3b30d652018-10-19 13:32:20 +00008207Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008208 if (!FromDC)
8209 return FromDC;
8210
Gabor Marton5ac6d492019-05-15 10:29:48 +00008211 ExpectedDecl ToDCOrErr = Import(cast<Decl>(FromDC));
Balazs Keria1f6b102019-04-08 13:59:15 +00008212 if (!ToDCOrErr)
8213 return ToDCOrErr.takeError();
8214 auto *ToDC = cast<DeclContext>(*ToDCOrErr);
Craig Topper36250ad2014-05-12 05:36:57 +00008215
Fangrui Song6907ce22018-07-30 19:24:48 +00008216 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00008217 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008218 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
8219 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008220 if (ToRecord->isCompleteDefinition()) {
8221 // Do nothing.
8222 } else if (FromRecord->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008223 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
8224 FromRecord, ToRecord, ASTNodeImporter::IDK_Basic))
8225 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008226 } else {
8227 CompleteDecl(ToRecord);
8228 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008229 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
8230 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008231 if (ToEnum->isCompleteDefinition()) {
8232 // Do nothing.
8233 } else if (FromEnum->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008234 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
8235 FromEnum, ToEnum, ASTNodeImporter::IDK_Basic))
8236 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008237 } else {
8238 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00008239 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008240 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
8241 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008242 if (ToClass->getDefinition()) {
8243 // Do nothing.
8244 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008245 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
8246 FromDef, ToClass, ASTNodeImporter::IDK_Basic))
8247 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008248 } else {
8249 CompleteDecl(ToClass);
8250 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008251 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
8252 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008253 if (ToProto->getDefinition()) {
8254 // Do nothing.
8255 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00008256 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
8257 FromDef, ToProto, ASTNodeImporter::IDK_Basic))
8258 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00008259 } else {
8260 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00008261 }
Douglas Gregor95d82832012-01-24 18:36:04 +00008262 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008263
Douglas Gregor95d82832012-01-24 18:36:04 +00008264 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00008265}
8266
Gabor Marton5ac6d492019-05-15 10:29:48 +00008267Expected<Expr *> ASTImporter::Import(Expr *FromE) {
8268 if (ExpectedStmt ToSOrErr = Import(cast_or_null<Stmt>(FromE)))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008269 return cast_or_null<Expr>(*ToSOrErr);
8270 else
8271 return ToSOrErr.takeError();
Balazs Keri4a3d7582018-11-27 18:36:31 +00008272}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008273
Gabor Marton5ac6d492019-05-15 10:29:48 +00008274Expected<Stmt *> ASTImporter::Import(Stmt *FromS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008275 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00008276 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00008277
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008278 // Check whether we've already imported this statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00008279 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
8280 if (Pos != ImportedStmts.end())
8281 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00008282
Balazs Keri3b30d652018-10-19 13:32:20 +00008283 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00008284 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00008285 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008286 if (!ToSOrErr)
8287 return ToSOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00008288
Balazs Keri3b30d652018-10-19 13:32:20 +00008289 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00008290 auto *FromE = cast<Expr>(FromS);
8291 // Copy ExprBitfields, which may not be handled in Expr subclasses
8292 // constructors.
8293 ToE->setValueKind(FromE->getValueKind());
8294 ToE->setObjectKind(FromE->getObjectKind());
8295 ToE->setTypeDependent(FromE->isTypeDependent());
8296 ToE->setValueDependent(FromE->isValueDependent());
8297 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
8298 ToE->setContainsUnexpandedParameterPack(
8299 FromE->containsUnexpandedParameterPack());
8300 }
8301
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008302 // Record the imported statement object.
Balazs Keri3b30d652018-10-19 13:32:20 +00008303 ImportedStmts[FromS] = *ToSOrErr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008304 return ToSOrErr;
8305}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008306
Balazs Keri4a3d7582018-11-27 18:36:31 +00008307Expected<NestedNameSpecifier *>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008308ASTImporter::Import(NestedNameSpecifier *FromNNS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008309 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00008310 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00008311
Simon Pilgrim130df2c2019-07-15 13:00:43 +00008312 NestedNameSpecifier *Prefix = nullptr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008313 if (Error Err = importInto(Prefix, FromNNS->getPrefix()))
8314 return std::move(Err);
Douglas Gregor90ebf252011-04-27 16:48:40 +00008315
8316 switch (FromNNS->getKind()) {
8317 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008318 assert(FromNNS->getAsIdentifier() && "NNS should contain identifier.");
8319 return NestedNameSpecifier::Create(ToContext, Prefix,
8320 Import(FromNNS->getAsIdentifier()));
Douglas Gregor90ebf252011-04-27 16:48:40 +00008321
8322 case NestedNameSpecifier::Namespace:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008323 if (ExpectedDecl NSOrErr = Import(FromNNS->getAsNamespace())) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008324 return NestedNameSpecifier::Create(ToContext, Prefix,
8325 cast<NamespaceDecl>(*NSOrErr));
8326 } else
8327 return NSOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008328
8329 case NestedNameSpecifier::NamespaceAlias:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008330 if (ExpectedDecl NSADOrErr = Import(FromNNS->getAsNamespaceAlias()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008331 return NestedNameSpecifier::Create(ToContext, Prefix,
8332 cast<NamespaceAliasDecl>(*NSADOrErr));
8333 else
8334 return NSADOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008335
8336 case NestedNameSpecifier::Global:
8337 return NestedNameSpecifier::GlobalSpecifier(ToContext);
8338
Nikola Smiljanic67860242014-09-26 00:28:20 +00008339 case NestedNameSpecifier::Super:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008340 if (ExpectedDecl RDOrErr = Import(FromNNS->getAsRecordDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008341 return NestedNameSpecifier::SuperSpecifier(ToContext,
8342 cast<CXXRecordDecl>(*RDOrErr));
8343 else
8344 return RDOrErr.takeError();
Nikola Smiljanic67860242014-09-26 00:28:20 +00008345
Douglas Gregor90ebf252011-04-27 16:48:40 +00008346 case NestedNameSpecifier::TypeSpec:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008347 case NestedNameSpecifier::TypeSpecWithTemplate:
8348 if (Expected<QualType> TyOrErr =
Gabor Marton5ac6d492019-05-15 10:29:48 +00008349 Import(QualType(FromNNS->getAsType(), 0u))) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008350 bool TSTemplate =
8351 FromNNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate;
8352 return NestedNameSpecifier::Create(ToContext, Prefix, TSTemplate,
8353 TyOrErr->getTypePtr());
8354 } else {
8355 return TyOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008356 }
Douglas Gregor90ebf252011-04-27 16:48:40 +00008357 }
8358
8359 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00008360}
8361
Balazs Keri4a3d7582018-11-27 18:36:31 +00008362Expected<NestedNameSpecifierLoc>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008363ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008364 // Copied from NestedNameSpecifier mostly.
8365 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
8366 NestedNameSpecifierLoc NNS = FromNNS;
8367
8368 // Push each of the nested-name-specifiers's onto a stack for
8369 // serialization in reverse order.
8370 while (NNS) {
8371 NestedNames.push_back(NNS);
8372 NNS = NNS.getPrefix();
8373 }
8374
8375 NestedNameSpecifierLocBuilder Builder;
8376
8377 while (!NestedNames.empty()) {
8378 NNS = NestedNames.pop_back_val();
Simon Pilgrim4c146ab2019-05-18 11:33:27 +00008379 NestedNameSpecifier *Spec = nullptr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008380 if (Error Err = importInto(Spec, NNS.getNestedNameSpecifier()))
8381 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008382
8383 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008384
8385 SourceLocation ToLocalBeginLoc, ToLocalEndLoc;
8386 if (Kind != NestedNameSpecifier::Super) {
8387 if (Error Err = importInto(ToLocalBeginLoc, NNS.getLocalBeginLoc()))
8388 return std::move(Err);
8389
8390 if (Kind != NestedNameSpecifier::Global)
8391 if (Error Err = importInto(ToLocalEndLoc, NNS.getLocalEndLoc()))
8392 return std::move(Err);
8393 }
8394
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008395 switch (Kind) {
8396 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008397 Builder.Extend(getToContext(), Spec->getAsIdentifier(), ToLocalBeginLoc,
8398 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008399 break;
8400
8401 case NestedNameSpecifier::Namespace:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008402 Builder.Extend(getToContext(), Spec->getAsNamespace(), ToLocalBeginLoc,
8403 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008404 break;
8405
8406 case NestedNameSpecifier::NamespaceAlias:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008407 Builder.Extend(getToContext(), Spec->getAsNamespaceAlias(),
8408 ToLocalBeginLoc, ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008409 break;
8410
8411 case NestedNameSpecifier::TypeSpec:
8412 case NestedNameSpecifier::TypeSpecWithTemplate: {
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008413 SourceLocation ToTLoc;
8414 if (Error Err = importInto(ToTLoc, NNS.getTypeLoc().getBeginLoc()))
8415 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008416 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008417 QualType(Spec->getAsType(), 0), ToTLoc);
Balázs Kéria9f10eb2019-12-05 16:21:21 +01008418 if (Kind == NestedNameSpecifier::TypeSpecWithTemplate)
8419 // ToLocalBeginLoc is here the location of the 'template' keyword.
8420 Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(),
8421 ToLocalEndLoc);
8422 else
8423 // No location for 'template' keyword here.
8424 Builder.Extend(getToContext(), SourceLocation{}, TSI->getTypeLoc(),
8425 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008426 break;
8427 }
8428
8429 case NestedNameSpecifier::Global:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008430 Builder.MakeGlobal(getToContext(), ToLocalBeginLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008431 break;
8432
8433 case NestedNameSpecifier::Super: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008434 auto ToSourceRangeOrErr = Import(NNS.getSourceRange());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008435 if (!ToSourceRangeOrErr)
8436 return ToSourceRangeOrErr.takeError();
8437
8438 Builder.MakeSuper(getToContext(), Spec->getAsRecordDecl(),
8439 ToSourceRangeOrErr->getBegin(),
8440 ToSourceRangeOrErr->getEnd());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008441 }
8442 }
8443 }
8444
8445 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00008446}
8447
Gabor Marton5ac6d492019-05-15 10:29:48 +00008448Expected<TemplateName> ASTImporter::Import(TemplateName From) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00008449 switch (From.getKind()) {
8450 case TemplateName::Template:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008451 if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008452 return TemplateName(cast<TemplateDecl>(*ToTemplateOrErr));
8453 else
8454 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008455
Douglas Gregore2e50d332010-12-01 01:36:18 +00008456 case TemplateName::OverloadedTemplate: {
8457 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
8458 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008459 for (auto *I : *FromStorage) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008460 if (auto ToOrErr = Import(I))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008461 ToTemplates.addDecl(cast<NamedDecl>(*ToOrErr));
Douglas Gregore2e50d332010-12-01 01:36:18 +00008462 else
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008463 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008464 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008465 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008466 ToTemplates.end());
8467 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008468
Richard Smithb23c5e82019-05-09 03:31:27 +00008469 case TemplateName::AssumedTemplate: {
8470 AssumedTemplateStorage *FromStorage = From.getAsAssumedTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008471 auto DeclNameOrErr = Import(FromStorage->getDeclName());
Richard Smithb23c5e82019-05-09 03:31:27 +00008472 if (!DeclNameOrErr)
8473 return DeclNameOrErr.takeError();
8474 return ToContext.getAssumedTemplateName(*DeclNameOrErr);
8475 }
8476
Douglas Gregore2e50d332010-12-01 01:36:18 +00008477 case TemplateName::QualifiedTemplate: {
8478 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008479 auto QualifierOrErr = Import(QTN->getQualifier());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008480 if (!QualifierOrErr)
8481 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008482
Gabor Marton5ac6d492019-05-15 10:29:48 +00008483 if (ExpectedDecl ToTemplateOrErr = Import(From.getAsTemplateDecl()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008484 return ToContext.getQualifiedTemplateName(
8485 *QualifierOrErr, QTN->hasTemplateKeyword(),
8486 cast<TemplateDecl>(*ToTemplateOrErr));
8487 else
8488 return ToTemplateOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008489 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008490
Douglas Gregore2e50d332010-12-01 01:36:18 +00008491 case TemplateName::DependentTemplate: {
8492 DependentTemplateName *DTN = From.getAsDependentTemplateName();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008493 auto QualifierOrErr = Import(DTN->getQualifier());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008494 if (!QualifierOrErr)
8495 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008496
Douglas Gregore2e50d332010-12-01 01:36:18 +00008497 if (DTN->isIdentifier()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008498 return ToContext.getDependentTemplateName(*QualifierOrErr,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008499 Import(DTN->getIdentifier()));
8500 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008501
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008502 return ToContext.getDependentTemplateName(*QualifierOrErr,
8503 DTN->getOperator());
Douglas Gregore2e50d332010-12-01 01:36:18 +00008504 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008505
8506 case TemplateName::SubstTemplateTemplateParm: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008507 SubstTemplateTemplateParmStorage *Subst =
8508 From.getAsSubstTemplateTemplateParm();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008509 ExpectedDecl ParamOrErr = Import(Subst->getParameter());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008510 if (!ParamOrErr)
8511 return ParamOrErr.takeError();
John McCalld9dfe3a2011-06-30 08:33:18 +00008512
Gabor Marton5ac6d492019-05-15 10:29:48 +00008513 auto ReplacementOrErr = Import(Subst->getReplacement());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008514 if (!ReplacementOrErr)
8515 return ReplacementOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008516
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008517 return ToContext.getSubstTemplateTemplateParm(
8518 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ReplacementOrErr);
John McCalld9dfe3a2011-06-30 08:33:18 +00008519 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008520
Douglas Gregor5590be02011-01-15 06:45:20 +00008521 case TemplateName::SubstTemplateTemplateParmPack: {
8522 SubstTemplateTemplateParmPackStorage *SubstPack
8523 = From.getAsSubstTemplateTemplateParmPack();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008524 ExpectedDecl ParamOrErr = Import(SubstPack->getParameterPack());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008525 if (!ParamOrErr)
8526 return ParamOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008527
Douglas Gregor5590be02011-01-15 06:45:20 +00008528 ASTNodeImporter Importer(*this);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008529 auto ArgPackOrErr =
8530 Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
8531 if (!ArgPackOrErr)
8532 return ArgPackOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008533
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008534 return ToContext.getSubstTemplateTemplateParmPack(
8535 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ArgPackOrErr);
Douglas Gregor5590be02011-01-15 06:45:20 +00008536 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008537 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008538
Douglas Gregore2e50d332010-12-01 01:36:18 +00008539 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008540}
8541
Gabor Marton5ac6d492019-05-15 10:29:48 +00008542Expected<SourceLocation> ASTImporter::Import(SourceLocation FromLoc) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008543 if (FromLoc.isInvalid())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008544 return SourceLocation{};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008545
Douglas Gregor811663e2010-02-10 00:15:17 +00008546 SourceManager &FromSM = FromContext.getSourceManager();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008547 bool IsBuiltin = FromSM.isWrittenInBuiltinFile(FromLoc);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008548
Douglas Gregor811663e2010-02-10 00:15:17 +00008549 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Gabor Marton5ac6d492019-05-15 10:29:48 +00008550 Expected<FileID> ToFileIDOrErr = Import(Decomposed.first, IsBuiltin);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008551 if (!ToFileIDOrErr)
8552 return ToFileIDOrErr.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008553 SourceManager &ToSM = ToContext.getSourceManager();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008554 return ToSM.getComposedLoc(*ToFileIDOrErr, Decomposed.second);
8555}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008556
Gabor Marton5ac6d492019-05-15 10:29:48 +00008557Expected<SourceRange> ASTImporter::Import(SourceRange FromRange) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008558 SourceLocation ToBegin, ToEnd;
8559 if (Error Err = importInto(ToBegin, FromRange.getBegin()))
8560 return std::move(Err);
8561 if (Error Err = importInto(ToEnd, FromRange.getEnd()))
8562 return std::move(Err);
8563
8564 return SourceRange(ToBegin, ToEnd);
Balazs Keri4a3d7582018-11-27 18:36:31 +00008565}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008566
Gabor Marton5ac6d492019-05-15 10:29:48 +00008567Expected<FileID> ASTImporter::Import(FileID FromID, bool IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008568 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008569 if (Pos != ImportedFileIDs.end())
8570 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008571
Douglas Gregor811663e2010-02-10 00:15:17 +00008572 SourceManager &FromSM = FromContext.getSourceManager();
8573 SourceManager &ToSM = ToContext.getSourceManager();
8574 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008575
8576 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008577 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008578 if (FromSLoc.isExpansion()) {
8579 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008580 ExpectedSLoc ToSpLoc = Import(FromEx.getSpellingLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008581 if (!ToSpLoc)
8582 return ToSpLoc.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008583 ExpectedSLoc ToExLocS = Import(FromEx.getExpansionLocStart());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008584 if (!ToExLocS)
8585 return ToExLocS.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008586 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8587 SourceLocation MLoc;
8588 if (FromEx.isMacroArgExpansion()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008589 MLoc = ToSM.createMacroArgExpansionLoc(*ToSpLoc, *ToExLocS, TokenLen);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008590 } else {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008591 if (ExpectedSLoc ToExLocE = Import(FromEx.getExpansionLocEnd()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008592 MLoc = ToSM.createExpansionLoc(*ToSpLoc, *ToExLocS, *ToExLocE, TokenLen,
8593 FromEx.isExpansionTokenRange());
8594 else
8595 return ToExLocE.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008596 }
8597 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008598 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008599 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008600
8601 if (!IsBuiltin) {
8602 // Include location of this file.
Gabor Marton5ac6d492019-05-15 10:29:48 +00008603 ExpectedSLoc ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008604 if (!ToIncludeLoc)
8605 return ToIncludeLoc.takeError();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008606
8607 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8608 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8609 // disk again
8610 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8611 // than mmap the files several times.
Harlan Haskins8d323d12019-08-01 21:31:56 +00008612 auto Entry =
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008613 ToFileManager.getFile(Cache->OrigEntry->getName());
8614 // FIXME: The filename may be a virtual name that does probably not
8615 // point to a valid file and we get no Entry here. In this case try with
8616 // the memory buffer below.
8617 if (Entry)
Harlan Haskins8d323d12019-08-01 21:31:56 +00008618 ToID = ToSM.createFileID(*Entry, *ToIncludeLoc,
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008619 FromSLoc.getFile().getFileCharacteristic());
8620 }
Balazs Keri9cf39df2019-02-27 16:31:48 +00008621 }
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008622
8623 if (ToID.isInvalid() || IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008624 // FIXME: We want to re-use the existing MemoryBuffer!
Balazs Keri9cf39df2019-02-27 16:31:48 +00008625 bool Invalid = true;
Duncan P. N. Exon Smithf5848192019-08-26 20:32:05 +00008626 const llvm::MemoryBuffer *FromBuf =
8627 Cache->getBuffer(FromContext.getDiagnostics(),
8628 FromSM.getFileManager(), SourceLocation{}, &Invalid);
Balazs Keri9cf39df2019-02-27 16:31:48 +00008629 if (!FromBuf || Invalid)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008630 // FIXME: Use a new error kind?
8631 return llvm::make_error<ImportError>(ImportError::Unknown);
Balazs Keri9cf39df2019-02-27 16:31:48 +00008632
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008633 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8634 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8635 FromBuf->getBufferIdentifier());
8636 ToID = ToSM.createFileID(std::move(ToBuf),
8637 FromSLoc.getFile().getFileCharacteristic());
8638 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008639 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008640
Balazs Keri9cf39df2019-02-27 16:31:48 +00008641 assert(ToID.isValid() && "Unexpected invalid fileID was created.");
8642
Sebastian Redl99219f12010-09-30 01:03:06 +00008643 ImportedFileIDs[FromID] = ToID;
Balazs Kerid22f8772019-07-24 10:16:37 +00008644
8645 if (FileIDImportHandler)
8646 FileIDImportHandler(ToID, FromID);
8647
Douglas Gregor811663e2010-02-10 00:15:17 +00008648 return ToID;
8649}
8650
Gabor Marton5ac6d492019-05-15 10:29:48 +00008651Expected<CXXCtorInitializer *> ASTImporter::Import(CXXCtorInitializer *From) {
8652 ExpectedExpr ToExprOrErr = Import(From->getInit());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008653 if (!ToExprOrErr)
8654 return ToExprOrErr.takeError();
8655
Gabor Marton5ac6d492019-05-15 10:29:48 +00008656 auto LParenLocOrErr = Import(From->getLParenLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008657 if (!LParenLocOrErr)
8658 return LParenLocOrErr.takeError();
8659
Gabor Marton5ac6d492019-05-15 10:29:48 +00008660 auto RParenLocOrErr = Import(From->getRParenLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008661 if (!RParenLocOrErr)
8662 return RParenLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008663
8664 if (From->isBaseInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008665 auto ToTInfoOrErr = Import(From->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008666 if (!ToTInfoOrErr)
8667 return ToTInfoOrErr.takeError();
8668
8669 SourceLocation EllipsisLoc;
8670 if (From->isPackExpansion())
8671 if (Error Err = importInto(EllipsisLoc, From->getEllipsisLoc()))
8672 return std::move(Err);
Davide Italianofaee83d2018-11-28 19:15:23 +00008673
8674 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008675 ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr,
8676 *ToExprOrErr, *RParenLocOrErr, EllipsisLoc);
Davide Italianofaee83d2018-11-28 19:15:23 +00008677 } else if (From->isMemberInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008678 ExpectedDecl ToFieldOrErr = Import(From->getMember());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008679 if (!ToFieldOrErr)
8680 return ToFieldOrErr.takeError();
8681
Gabor Marton5ac6d492019-05-15 10:29:48 +00008682 auto MemberLocOrErr = Import(From->getMemberLocation());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008683 if (!MemberLocOrErr)
8684 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008685
8686 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008687 ToContext, cast_or_null<FieldDecl>(*ToFieldOrErr), *MemberLocOrErr,
8688 *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008689 } else if (From->isIndirectMemberInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008690 ExpectedDecl ToIFieldOrErr = Import(From->getIndirectMember());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008691 if (!ToIFieldOrErr)
8692 return ToIFieldOrErr.takeError();
8693
Gabor Marton5ac6d492019-05-15 10:29:48 +00008694 auto MemberLocOrErr = Import(From->getMemberLocation());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008695 if (!MemberLocOrErr)
8696 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008697
8698 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008699 ToContext, cast_or_null<IndirectFieldDecl>(*ToIFieldOrErr),
8700 *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008701 } else if (From->isDelegatingInitializer()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008702 auto ToTInfoOrErr = Import(From->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008703 if (!ToTInfoOrErr)
8704 return ToTInfoOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008705
8706 return new (ToContext)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008707 CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr,
8708 *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008709 } else {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008710 // FIXME: assert?
8711 return make_error<ImportError>();
Davide Italianofaee83d2018-11-28 19:15:23 +00008712 }
Balazs Kerideaf7ab2018-11-28 13:21:26 +00008713}
Sean Callanandd2c1742016-05-16 20:48:03 +00008714
Balazs Keri4a3d7582018-11-27 18:36:31 +00008715Expected<CXXBaseSpecifier *>
Gabor Marton5ac6d492019-05-15 10:29:48 +00008716ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00008717 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8718 if (Pos != ImportedCXXBaseSpecifiers.end())
8719 return Pos->second;
8720
Gabor Marton5ac6d492019-05-15 10:29:48 +00008721 Expected<SourceRange> ToSourceRange = Import(BaseSpec->getSourceRange());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008722 if (!ToSourceRange)
8723 return ToSourceRange.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008724 Expected<TypeSourceInfo *> ToTSI = Import(BaseSpec->getTypeSourceInfo());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008725 if (!ToTSI)
8726 return ToTSI.takeError();
Gabor Marton5ac6d492019-05-15 10:29:48 +00008727 ExpectedSLoc ToEllipsisLoc = Import(BaseSpec->getEllipsisLoc());
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008728 if (!ToEllipsisLoc)
8729 return ToEllipsisLoc.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00008730 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008731 *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8732 BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00008733 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8734 return Imported;
8735}
8736
Gabor Marton5ac6d492019-05-15 10:29:48 +00008737Error ASTImporter::ImportDefinition(Decl *From) {
8738 ExpectedDecl ToOrErr = Import(From);
8739 if (!ToOrErr)
8740 return ToOrErr.takeError();
8741 Decl *To = *ToOrErr;
Fangrui Song6907ce22018-07-30 19:24:48 +00008742
Don Hintonf170dff2019-03-19 06:14:14 +00008743 auto *FromDC = cast<DeclContext>(From);
8744 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008745
Don Hintonf170dff2019-03-19 06:14:14 +00008746 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
8747 if (!ToRecord->getDefinition()) {
8748 return Importer.ImportDefinition(
8749 cast<RecordDecl>(FromDC), ToRecord,
8750 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00008751 }
Douglas Gregor0a791672011-01-18 03:11:38 +00008752 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008753
Don Hintonf170dff2019-03-19 06:14:14 +00008754 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
8755 if (!ToEnum->getDefinition()) {
8756 return Importer.ImportDefinition(
8757 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
8758 }
8759 }
8760
8761 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
8762 if (!ToIFace->getDefinition()) {
8763 return Importer.ImportDefinition(
8764 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8765 ASTNodeImporter::IDK_Everything);
8766 }
8767 }
8768
8769 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
8770 if (!ToProto->getDefinition()) {
8771 return Importer.ImportDefinition(
8772 cast<ObjCProtocolDecl>(FromDC), ToProto,
8773 ASTNodeImporter::IDK_Everything);
8774 }
8775 }
8776
8777 return Importer.ImportDeclContext(FromDC, true);
Balazs Keri3b30d652018-10-19 13:32:20 +00008778}
8779
Gabor Marton5ac6d492019-05-15 10:29:48 +00008780Expected<DeclarationName> ASTImporter::Import(DeclarationName FromName) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008781 if (!FromName)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008782 return DeclarationName{};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008783
8784 switch (FromName.getNameKind()) {
8785 case DeclarationName::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008786 return DeclarationName(Import(FromName.getAsIdentifierInfo()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008787
8788 case DeclarationName::ObjCZeroArgSelector:
8789 case DeclarationName::ObjCOneArgSelector:
8790 case DeclarationName::ObjCMultiArgSelector:
Gabor Marton5ac6d492019-05-15 10:29:48 +00008791 if (auto ToSelOrErr = Import(FromName.getObjCSelector()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008792 return DeclarationName(*ToSelOrErr);
8793 else
8794 return ToSelOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008795
8796 case DeclarationName::CXXConstructorName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008797 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008798 return ToContext.DeclarationNames.getCXXConstructorName(
8799 ToContext.getCanonicalType(*ToTyOrErr));
8800 else
8801 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008802 }
8803
8804 case DeclarationName::CXXDestructorName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008805 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008806 return ToContext.DeclarationNames.getCXXDestructorName(
8807 ToContext.getCanonicalType(*ToTyOrErr));
8808 else
8809 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008810 }
8811
Richard Smith35845152017-02-07 01:37:30 +00008812 case DeclarationName::CXXDeductionGuideName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008813 if (auto ToTemplateOrErr = Import(FromName.getCXXDeductionGuideTemplate()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008814 return ToContext.DeclarationNames.getCXXDeductionGuideName(
8815 cast<TemplateDecl>(*ToTemplateOrErr));
8816 else
8817 return ToTemplateOrErr.takeError();
Richard Smith35845152017-02-07 01:37:30 +00008818 }
8819
Douglas Gregor96e578d2010-02-05 17:54:41 +00008820 case DeclarationName::CXXConversionFunctionName: {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008821 if (auto ToTyOrErr = Import(FromName.getCXXNameType()))
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008822 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8823 ToContext.getCanonicalType(*ToTyOrErr));
8824 else
8825 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008826 }
8827
8828 case DeclarationName::CXXOperatorName:
8829 return ToContext.DeclarationNames.getCXXOperatorName(
8830 FromName.getCXXOverloadedOperator());
8831
8832 case DeclarationName::CXXLiteralOperatorName:
8833 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008834 Import(FromName.getCXXLiteralIdentifier()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008835
8836 case DeclarationName::CXXUsingDirective:
8837 // FIXME: STATICS!
8838 return DeclarationName::getUsingDirectiveName();
8839 }
8840
David Blaikiee4d798f2012-01-20 21:50:17 +00008841 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008842}
8843
Douglas Gregore2e50d332010-12-01 01:36:18 +00008844IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008845 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008846 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008847
Sean Callananf94ef1d2016-05-14 06:11:19 +00008848 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8849
8850 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8851 ToId->setBuiltinID(FromId->getBuiltinID());
8852
8853 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008854}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008855
Gabor Marton5ac6d492019-05-15 10:29:48 +00008856Expected<Selector> ASTImporter::Import(Selector FromSel) {
Douglas Gregor43f54792010-02-17 02:12:47 +00008857 if (FromSel.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008858 return Selector{};
Douglas Gregor43f54792010-02-17 02:12:47 +00008859
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008860 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008861 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8862 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8863 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8864 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8865}
8866
Gabor Martonf035b752019-08-27 11:36:10 +00008867Expected<DeclarationName> ASTImporter::HandleNameConflict(DeclarationName Name,
8868 DeclContext *DC,
8869 unsigned IDNS,
8870 NamedDecl **Decls,
8871 unsigned NumDecls) {
8872 if (ODRHandling == ODRHandlingType::Conservative)
8873 // Report error at any name conflict.
8874 return make_error<ImportError>(ImportError::NameConflict);
8875 else
8876 // Allow to create the new Decl with the same name.
8877 return Name;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008878}
8879
8880DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008881 if (LastDiagFromFrom)
8882 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8883 FromContext.getDiagnostics());
8884 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008885 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008886}
8887
8888DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008889 if (!LastDiagFromFrom)
8890 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8891 ToContext.getDiagnostics());
8892 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008893 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008894}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008895
Douglas Gregor2e15c842012-02-01 21:00:38 +00008896void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008897 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008898 if (!ID->getDefinition())
8899 ID->startDefinition();
8900 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008901 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008902 if (!PD->getDefinition())
8903 PD->startDefinition();
8904 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008905 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008906 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8907 TD->startDefinition();
8908 TD->setCompleteDefinition(true);
8909 }
8910 }
8911 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008912 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008913 }
8914}
8915
Gabor Marton26f72a92018-07-12 09:42:05 +00008916Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8917 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8918 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8919 "Try to import an already imported Decl");
8920 if (Pos != ImportedDecls.end())
8921 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008922 ImportedDecls[From] = To;
Gabor Marton458d1452019-02-14 13:07:03 +00008923 // This mapping should be maintained only in this function. Therefore do not
8924 // check for additional consistency.
8925 ImportedFromDecls[To] = From;
Gabor Marton303c98612019-06-25 08:00:51 +00008926 AddToLookupTable(To);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008927 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00008928}
Douglas Gregorb4964f72010-02-15 23:54:17 +00008929
Gabor Marton303c98612019-06-25 08:00:51 +00008930llvm::Optional<ImportError>
8931ASTImporter::getImportDeclErrorIfAny(Decl *FromD) const {
8932 auto Pos = ImportDeclErrors.find(FromD);
8933 if (Pos != ImportDeclErrors.end())
8934 return Pos->second;
8935 else
8936 return Optional<ImportError>();
8937}
8938
8939void ASTImporter::setImportDeclError(Decl *From, ImportError Error) {
Gabor Marton1ad4b992019-07-01 14:19:53 +00008940 auto InsertRes = ImportDeclErrors.insert({From, Error});
Benjamin Kramer4f769362019-07-01 14:33:26 +00008941 (void)InsertRes;
Gabor Marton1ad4b992019-07-01 14:19:53 +00008942 // Either we set the error for the first time, or we already had set one and
8943 // now we want to set the same error.
8944 assert(InsertRes.second || InsertRes.first->second.Error == Error.Error);
Gabor Marton303c98612019-06-25 08:00:51 +00008945}
8946
Douglas Gregordd6006f2012-07-17 21:16:27 +00008947bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
8948 bool Complain) {
Balazs Keria1f6b102019-04-08 13:59:15 +00008949 llvm::DenseMap<const Type *, const Type *>::iterator Pos =
8950 ImportedTypes.find(From.getTypePtr());
8951 if (Pos != ImportedTypes.end()) {
Gabor Marton5ac6d492019-05-15 10:29:48 +00008952 if (ExpectedType ToFromOrErr = Import(From)) {
Balazs Keria1f6b102019-04-08 13:59:15 +00008953 if (ToContext.hasSameType(*ToFromOrErr, To))
8954 return true;
8955 } else {
8956 llvm::consumeError(ToFromOrErr.takeError());
8957 }
8958 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00008959
Douglas Gregordd6006f2012-07-17 21:16:27 +00008960 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00008961 getStructuralEquivalenceKind(*this), false,
8962 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00008963 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00008964}