blob: b4c04284055cfd30d935ed527dabc4e030d2a349 [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"
Gabor Marton54058b52018-12-17 13:53:12 +000015#include "clang/AST/ASTImporterLookupTable.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000016#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +000018#include "clang/AST/ASTStructuralEquivalence.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000019#include "clang/AST/Attr.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclAccessPair.h"
22#include "clang/AST/DeclBase.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000023#include "clang/AST/DeclCXX.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000024#include "clang/AST/DeclFriend.h"
25#include "clang/AST/DeclGroup.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000026#include "clang/AST/DeclObjC.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000027#include "clang/AST/DeclTemplate.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000028#include "clang/AST/DeclVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000029#include "clang/AST/DeclarationName.h"
30#include "clang/AST/Expr.h"
31#include "clang/AST/ExprCXX.h"
32#include "clang/AST/ExprObjC.h"
33#include "clang/AST/ExternalASTSource.h"
34#include "clang/AST/LambdaCapture.h"
35#include "clang/AST/NestedNameSpecifier.h"
36#include "clang/AST/OperationKinds.h"
37#include "clang/AST/Stmt.h"
38#include "clang/AST/StmtCXX.h"
39#include "clang/AST/StmtObjC.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000040#include "clang/AST/StmtVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000041#include "clang/AST/TemplateBase.h"
42#include "clang/AST/TemplateName.h"
43#include "clang/AST/Type.h"
44#include "clang/AST/TypeLoc.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000045#include "clang/AST/TypeVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000046#include "clang/AST/UnresolvedSet.h"
47#include "clang/Basic/ExceptionSpecificationType.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000048#include "clang/Basic/FileManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000049#include "clang/Basic/IdentifierTable.h"
50#include "clang/Basic/LLVM.h"
51#include "clang/Basic/LangOptions.h"
52#include "clang/Basic/SourceLocation.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000053#include "clang/Basic/SourceManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000054#include "clang/Basic/Specifiers.h"
55#include "llvm/ADT/APSInt.h"
56#include "llvm/ADT/ArrayRef.h"
57#include "llvm/ADT/DenseMap.h"
58#include "llvm/ADT/None.h"
59#include "llvm/ADT/Optional.h"
60#include "llvm/ADT/STLExtras.h"
61#include "llvm/ADT/SmallVector.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/ErrorHandling.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000064#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000065#include <algorithm>
66#include <cassert>
67#include <cstddef>
68#include <memory>
69#include <type_traits>
70#include <utility>
Douglas Gregor96e578d2010-02-05 17:54:41 +000071
Douglas Gregor3c2404b2011-11-03 18:07:07 +000072namespace clang {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000073
Balazs Keri3b30d652018-10-19 13:32:20 +000074 using llvm::make_error;
75 using llvm::Error;
76 using llvm::Expected;
77 using ExpectedType = llvm::Expected<QualType>;
78 using ExpectedStmt = llvm::Expected<Stmt *>;
79 using ExpectedExpr = llvm::Expected<Expr *>;
80 using ExpectedDecl = llvm::Expected<Decl *>;
81 using ExpectedSLoc = llvm::Expected<SourceLocation>;
Balazs Keri2544b4b2018-08-08 09:40:57 +000082
Balazs Keri3b30d652018-10-19 13:32:20 +000083 std::string ImportError::toString() const {
84 // FIXME: Improve error texts.
85 switch (Error) {
86 case NameConflict:
87 return "NameConflict";
88 case UnsupportedConstruct:
89 return "UnsupportedConstruct";
90 case Unknown:
91 return "Unknown error";
Balazs Keri2544b4b2018-08-08 09:40:57 +000092 }
Balazs Keri2a13d662018-10-19 15:16:51 +000093 llvm_unreachable("Invalid error code.");
94 return "Invalid error code.";
Balazs Keri2544b4b2018-08-08 09:40:57 +000095 }
96
Balazs Keri3b30d652018-10-19 13:32:20 +000097 void ImportError::log(raw_ostream &OS) const {
98 OS << toString();
99 }
100
101 std::error_code ImportError::convertToErrorCode() const {
102 llvm_unreachable("Function not implemented.");
103 }
104
105 char ImportError::ID;
106
Gabor Marton5254e642018-06-27 13:32:50 +0000107 template <class T>
Balazs Keri3b30d652018-10-19 13:32:20 +0000108 SmallVector<Decl *, 2>
Gabor Marton5254e642018-06-27 13:32:50 +0000109 getCanonicalForwardRedeclChain(Redeclarable<T>* D) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000110 SmallVector<Decl *, 2> Redecls;
Gabor Marton5254e642018-06-27 13:32:50 +0000111 for (auto *R : D->getFirstDecl()->redecls()) {
112 if (R != D->getFirstDecl())
113 Redecls.push_back(R);
114 }
115 Redecls.push_back(D->getFirstDecl());
116 std::reverse(Redecls.begin(), Redecls.end());
117 return Redecls;
118 }
119
120 SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +0000121 if (auto *FD = dyn_cast<FunctionDecl>(D))
122 return getCanonicalForwardRedeclChain<FunctionDecl>(FD);
123 if (auto *VD = dyn_cast<VarDecl>(D))
124 return getCanonicalForwardRedeclChain<VarDecl>(VD);
Gabor Marton7df342a2018-12-17 12:42:12 +0000125 if (auto *TD = dyn_cast<TagDecl>(D))
126 return getCanonicalForwardRedeclChain<TagDecl>(TD);
Gabor Martonac3a5d62018-09-17 12:04:52 +0000127 llvm_unreachable("Bad declaration kind");
Gabor Marton5254e642018-06-27 13:32:50 +0000128 }
129
Gabor Marton26f72a92018-07-12 09:42:05 +0000130 void updateFlags(const Decl *From, Decl *To) {
131 // Check if some flags or attrs are new in 'From' and copy into 'To'.
132 // FIXME: Other flags or attrs?
133 if (From->isUsed(false) && !To->isUsed(false))
134 To->setIsUsed();
135 }
136
Balazs Keri3b30d652018-10-19 13:32:20 +0000137 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>,
138 public DeclVisitor<ASTNodeImporter, ExpectedDecl>,
139 public StmtVisitor<ASTNodeImporter, ExpectedStmt> {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000140 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000141
Balazs Keri3b30d652018-10-19 13:32:20 +0000142 // Use this instead of Importer.importInto .
143 template <typename ImportT>
144 LLVM_NODISCARD Error importInto(ImportT &To, const ImportT &From) {
145 return Importer.importInto(To, From);
146 }
147
148 // Use this to import pointers of specific type.
149 template <typename ImportT>
150 LLVM_NODISCARD Error importInto(ImportT *&To, ImportT *From) {
Balazs Keri57949eb2019-03-25 09:16:39 +0000151 auto ToOrErr = Importer.Import_New(From);
152 if (ToOrErr)
153 To = cast_or_null<ImportT>(*ToOrErr);
154 return ToOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +0000155 }
156
157 // Call the import function of ASTImporter for a baseclass of type `T` and
158 // cast the return value to `T`.
159 template <typename T>
160 Expected<T *> import(T *From) {
Balazs Keri57949eb2019-03-25 09:16:39 +0000161 auto ToOrErr = Importer.Import_New(From);
162 if (!ToOrErr)
163 return ToOrErr.takeError();
164 return cast_or_null<T>(*ToOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +0000165 }
166
167 template <typename T>
168 Expected<T *> import(const T *From) {
169 return import(const_cast<T *>(From));
170 }
171
172 // Call the import function of ASTImporter for type `T`.
173 template <typename T>
174 Expected<T> import(const T &From) {
Balazs Keri57949eb2019-03-25 09:16:39 +0000175 return Importer.Import_New(From);
Balazs Keri3b30d652018-10-19 13:32:20 +0000176 }
177
178 template <class T>
179 Expected<std::tuple<T>>
180 importSeq(const T &From) {
181 Expected<T> ToOrErr = import(From);
182 if (!ToOrErr)
183 return ToOrErr.takeError();
184 return std::make_tuple<T>(std::move(*ToOrErr));
185 }
186
187 // Import multiple objects with a single function call.
188 // This should work for every type for which a variant of `import` exists.
189 // The arguments are processed from left to right and import is stopped on
190 // first error.
191 template <class THead, class... TTail>
192 Expected<std::tuple<THead, TTail...>>
193 importSeq(const THead &FromHead, const TTail &...FromTail) {
194 Expected<std::tuple<THead>> ToHeadOrErr = importSeq(FromHead);
195 if (!ToHeadOrErr)
196 return ToHeadOrErr.takeError();
197 Expected<std::tuple<TTail...>> ToTailOrErr = importSeq(FromTail...);
198 if (!ToTailOrErr)
199 return ToTailOrErr.takeError();
200 return std::tuple_cat(*ToHeadOrErr, *ToTailOrErr);
201 }
202
203// Wrapper for an overload set.
Gabor Marton26f72a92018-07-12 09:42:05 +0000204 template <typename ToDeclT> struct CallOverloadedCreateFun {
205 template <typename... Args>
206 auto operator()(Args &&... args)
207 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) {
208 return ToDeclT::Create(std::forward<Args>(args)...);
209 }
210 };
211
212 // Always use these functions to create a Decl during import. There are
213 // certain tasks which must be done after the Decl was created, e.g. we
214 // must immediately register that as an imported Decl. The parameter `ToD`
215 // will be set to the newly created Decl or if had been imported before
216 // then to the already imported Decl. Returns a bool value set to true if
217 // the `FromD` had been imported before.
218 template <typename ToDeclT, typename FromDeclT, typename... Args>
219 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
220 Args &&... args) {
221 // There may be several overloads of ToDeclT::Create. We must make sure
222 // to call the one which would be chosen by the arguments, thus we use a
223 // wrapper for the overload set.
224 CallOverloadedCreateFun<ToDeclT> OC;
225 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
226 std::forward<Args>(args)...);
227 }
228 // Use this overload if a special Type is needed to be created. E.g if we
229 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl`
230 // then:
231 // TypedefNameDecl *ToTypedef;
232 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...);
233 template <typename NewDeclT, typename ToDeclT, typename FromDeclT,
234 typename... Args>
235 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
236 Args &&... args) {
237 CallOverloadedCreateFun<NewDeclT> OC;
238 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
239 std::forward<Args>(args)...);
240 }
241 // Use this version if a special create function must be
242 // used, e.g. CXXRecordDecl::CreateLambda .
243 template <typename ToDeclT, typename CreateFunT, typename FromDeclT,
244 typename... Args>
245 LLVM_NODISCARD bool
246 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
247 FromDeclT *FromD, Args &&... args) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000248 // FIXME: This code is needed later.
249 //if (Importer.getImportDeclErrorIfAny(FromD)) {
250 // ToD = nullptr;
251 // return true; // Already imported but with error.
252 //}
Gabor Marton26f72a92018-07-12 09:42:05 +0000253 ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD));
254 if (ToD)
255 return true; // Already imported.
256 ToD = CreateFun(std::forward<Args>(args)...);
Gabor Marton54058b52018-12-17 13:53:12 +0000257 // Keep track of imported Decls.
258 Importer.MapImported(FromD, ToD);
259 Importer.AddToLookupTable(ToD);
Gabor Marton26f72a92018-07-12 09:42:05 +0000260 InitializeImportedDecl(FromD, ToD);
261 return false; // A new Decl is created.
262 }
263
264 void InitializeImportedDecl(Decl *FromD, Decl *ToD) {
Gabor Marton26f72a92018-07-12 09:42:05 +0000265 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
266 if (FromD->hasAttrs())
Balazs Keri57949eb2019-03-25 09:16:39 +0000267 for (const Attr *FromAttr : FromD->getAttrs()) {
268 // FIXME: Return of the error here is not possible until store of
269 // import errors is implemented.
270 auto ToAttrOrErr = import(FromAttr);
271 if (ToAttrOrErr)
272 ToD->addAttr(*ToAttrOrErr);
273 else
274 llvm::consumeError(ToAttrOrErr.takeError());
275 }
Gabor Marton26f72a92018-07-12 09:42:05 +0000276 if (FromD->isUsed())
277 ToD->setIsUsed();
278 if (FromD->isImplicit())
279 ToD->setImplicit();
280 }
281
Gabor Martondd59d272019-03-19 14:04:50 +0000282 // Check if we have found an existing definition. Returns with that
283 // definition if yes, otherwise returns null.
284 Decl *FindAndMapDefinition(FunctionDecl *D, FunctionDecl *FoundFunction) {
285 const FunctionDecl *Definition = nullptr;
286 if (D->doesThisDeclarationHaveABody() &&
287 FoundFunction->hasBody(Definition))
288 return Importer.MapImported(D, const_cast<FunctionDecl *>(Definition));
289 return nullptr;
290 }
291
Douglas Gregor96e578d2010-02-05 17:54:41 +0000292 public:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000293 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
Gabor Marton344b0992018-05-16 11:48:11 +0000294
Balazs Keri3b30d652018-10-19 13:32:20 +0000295 using TypeVisitor<ASTNodeImporter, ExpectedType>::Visit;
296 using DeclVisitor<ASTNodeImporter, ExpectedDecl>::Visit;
297 using StmtVisitor<ASTNodeImporter, ExpectedStmt>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000298
299 // Importing types
Balazs Keri3b30d652018-10-19 13:32:20 +0000300 ExpectedType VisitType(const Type *T);
301 ExpectedType VisitAtomicType(const AtomicType *T);
302 ExpectedType VisitBuiltinType(const BuiltinType *T);
303 ExpectedType VisitDecayedType(const DecayedType *T);
304 ExpectedType VisitComplexType(const ComplexType *T);
305 ExpectedType VisitPointerType(const PointerType *T);
306 ExpectedType VisitBlockPointerType(const BlockPointerType *T);
307 ExpectedType VisitLValueReferenceType(const LValueReferenceType *T);
308 ExpectedType VisitRValueReferenceType(const RValueReferenceType *T);
309 ExpectedType VisitMemberPointerType(const MemberPointerType *T);
310 ExpectedType VisitConstantArrayType(const ConstantArrayType *T);
311 ExpectedType VisitIncompleteArrayType(const IncompleteArrayType *T);
312 ExpectedType VisitVariableArrayType(const VariableArrayType *T);
313 ExpectedType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000314 // FIXME: DependentSizedExtVectorType
Balazs Keri3b30d652018-10-19 13:32:20 +0000315 ExpectedType VisitVectorType(const VectorType *T);
316 ExpectedType VisitExtVectorType(const ExtVectorType *T);
317 ExpectedType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
318 ExpectedType VisitFunctionProtoType(const FunctionProtoType *T);
319 ExpectedType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
320 ExpectedType VisitParenType(const ParenType *T);
321 ExpectedType VisitTypedefType(const TypedefType *T);
322 ExpectedType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000323 // FIXME: DependentTypeOfExprType
Balazs Keri3b30d652018-10-19 13:32:20 +0000324 ExpectedType VisitTypeOfType(const TypeOfType *T);
325 ExpectedType VisitDecltypeType(const DecltypeType *T);
326 ExpectedType VisitUnaryTransformType(const UnaryTransformType *T);
327 ExpectedType VisitAutoType(const AutoType *T);
328 ExpectedType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000329 // FIXME: DependentDecltypeType
Balazs Keri3b30d652018-10-19 13:32:20 +0000330 ExpectedType VisitRecordType(const RecordType *T);
331 ExpectedType VisitEnumType(const EnumType *T);
332 ExpectedType VisitAttributedType(const AttributedType *T);
333 ExpectedType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
334 ExpectedType VisitSubstTemplateTypeParmType(
335 const SubstTemplateTypeParmType *T);
336 ExpectedType VisitTemplateSpecializationType(
337 const TemplateSpecializationType *T);
338 ExpectedType VisitElaboratedType(const ElaboratedType *T);
339 ExpectedType VisitDependentNameType(const DependentNameType *T);
340 ExpectedType VisitPackExpansionType(const PackExpansionType *T);
341 ExpectedType VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000342 const DependentTemplateSpecializationType *T);
Balazs Keri3b30d652018-10-19 13:32:20 +0000343 ExpectedType VisitObjCInterfaceType(const ObjCInterfaceType *T);
344 ExpectedType VisitObjCObjectType(const ObjCObjectType *T);
345 ExpectedType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Rafael Stahldf556202018-05-29 08:12:15 +0000346
347 // Importing declarations
Balazs Keri3b30d652018-10-19 13:32:20 +0000348 Error ImportDeclParts(
349 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
350 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc);
351 Error ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
352 Error ImportDeclarationNameLoc(
353 const DeclarationNameInfo &From, DeclarationNameInfo &To);
354 Error ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
355 Error ImportDeclContext(
356 Decl *From, DeclContext *&ToDC, DeclContext *&ToLexicalDC);
357 Error ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000358
Balazs Keri3b30d652018-10-19 13:32:20 +0000359 Expected<CXXCastPath> ImportCastPath(CastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000360
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000361 using Designator = DesignatedInitExpr::Designator;
362
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000363 /// What we should import from the definition.
Fangrui Song6907ce22018-07-30 19:24:48 +0000364 enum ImportDefinitionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000365 /// Import the default subset of the definition, which might be
Douglas Gregor95d82832012-01-24 18:36:04 +0000366 /// nothing (if minimal import is set) or might be everything (if minimal
367 /// import is not set).
368 IDK_Default,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000369 /// Import everything.
Douglas Gregor95d82832012-01-24 18:36:04 +0000370 IDK_Everything,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000371 /// Import only the bare bones needed to establish a valid
Douglas Gregor95d82832012-01-24 18:36:04 +0000372 /// DeclContext.
373 IDK_Basic
374 };
375
Douglas Gregor2e15c842012-02-01 21:00:38 +0000376 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
377 return IDK == IDK_Everything ||
378 (IDK == IDK_Default && !Importer.isMinimalImport());
379 }
380
Balazs Keri3b30d652018-10-19 13:32:20 +0000381 Error ImportInitializer(VarDecl *From, VarDecl *To);
382 Error ImportDefinition(
383 RecordDecl *From, RecordDecl *To,
384 ImportDefinitionKind Kind = IDK_Default);
385 Error ImportDefinition(
386 EnumDecl *From, EnumDecl *To,
387 ImportDefinitionKind Kind = IDK_Default);
388 Error ImportDefinition(
389 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
390 ImportDefinitionKind Kind = IDK_Default);
391 Error ImportDefinition(
392 ObjCProtocolDecl *From, ObjCProtocolDecl *To,
393 ImportDefinitionKind Kind = IDK_Default);
Balazs Keri3b30d652018-10-19 13:32:20 +0000394 Error ImportTemplateArguments(
395 const TemplateArgument *FromArgs, unsigned NumFromArgs,
396 SmallVectorImpl<TemplateArgument> &ToArgs);
397 Expected<TemplateArgument>
398 ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000399
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000400 template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000401 Error ImportTemplateArgumentListInfo(
402 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000403
404 template<typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000405 Error ImportTemplateArgumentListInfo(
406 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
407 const InContainerTy &Container, TemplateArgumentListInfo &Result);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000408
Gabor Marton5254e642018-06-27 13:32:50 +0000409 using TemplateArgsTy = SmallVector<TemplateArgument, 8>;
Balazs Keri3b30d652018-10-19 13:32:20 +0000410 using FunctionTemplateAndArgsTy =
411 std::tuple<FunctionTemplateDecl *, TemplateArgsTy>;
412 Expected<FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000413 ImportFunctionTemplateWithTemplateArgsFromSpecialization(
414 FunctionDecl *FromFD);
415
Balazs Keri3b30d652018-10-19 13:32:20 +0000416 Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000417
Shafik Yaghmour96b3d202019-01-28 21:55:33 +0000418 Error ImportFunctionDeclBody(FunctionDecl *FromFD, FunctionDecl *ToFD);
419
Gabor Marton458d1452019-02-14 13:07:03 +0000420 template <typename T>
421 bool hasSameVisibilityContext(T *Found, T *From);
422
Gabor Marton950fb572018-07-17 12:39:27 +0000423 bool IsStructuralMatch(Decl *From, Decl *To, bool Complain);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000424 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
425 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000426 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
427 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000428 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000429 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000430 bool IsStructuralMatch(FunctionTemplateDecl *From,
431 FunctionTemplateDecl *To);
Balazs Keric7797c42018-07-11 09:37:24 +0000432 bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000433 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000434 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Balazs Keri3b30d652018-10-19 13:32:20 +0000435 ExpectedDecl VisitDecl(Decl *D);
436 ExpectedDecl VisitImportDecl(ImportDecl *D);
437 ExpectedDecl VisitEmptyDecl(EmptyDecl *D);
438 ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D);
439 ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D);
440 ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D);
441 ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D);
442 ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
443 ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
444 ExpectedDecl VisitTypedefDecl(TypedefDecl *D);
445 ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D);
446 ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
447 ExpectedDecl VisitLabelDecl(LabelDecl *D);
448 ExpectedDecl VisitEnumDecl(EnumDecl *D);
449 ExpectedDecl VisitRecordDecl(RecordDecl *D);
450 ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D);
451 ExpectedDecl VisitFunctionDecl(FunctionDecl *D);
452 ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D);
453 ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D);
454 ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D);
455 ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D);
456 ExpectedDecl VisitFieldDecl(FieldDecl *D);
457 ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D);
458 ExpectedDecl VisitFriendDecl(FriendDecl *D);
459 ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D);
460 ExpectedDecl VisitVarDecl(VarDecl *D);
461 ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D);
462 ExpectedDecl VisitParmVarDecl(ParmVarDecl *D);
463 ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D);
464 ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
465 ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D);
466 ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D);
467 ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D);
468 ExpectedDecl VisitUsingDecl(UsingDecl *D);
469 ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D);
470 ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
471 ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
472 ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000473
Balazs Keri3b30d652018-10-19 13:32:20 +0000474 Expected<ObjCTypeParamList *>
475 ImportObjCTypeParamList(ObjCTypeParamList *list);
476
477 ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
478 ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
479 ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D);
480 ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D);
481 ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
482 ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
483 ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
484 ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
485 ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D);
486 ExpectedDecl VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +0000487 ClassTemplateSpecializationDecl *D);
Balazs Keri3b30d652018-10-19 13:32:20 +0000488 ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D);
489 ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
490 ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000491
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000492 // Importing statements
Balazs Keri3b30d652018-10-19 13:32:20 +0000493 ExpectedStmt VisitStmt(Stmt *S);
494 ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S);
495 ExpectedStmt VisitDeclStmt(DeclStmt *S);
496 ExpectedStmt VisitNullStmt(NullStmt *S);
497 ExpectedStmt VisitCompoundStmt(CompoundStmt *S);
498 ExpectedStmt VisitCaseStmt(CaseStmt *S);
499 ExpectedStmt VisitDefaultStmt(DefaultStmt *S);
500 ExpectedStmt VisitLabelStmt(LabelStmt *S);
501 ExpectedStmt VisitAttributedStmt(AttributedStmt *S);
502 ExpectedStmt VisitIfStmt(IfStmt *S);
503 ExpectedStmt VisitSwitchStmt(SwitchStmt *S);
504 ExpectedStmt VisitWhileStmt(WhileStmt *S);
505 ExpectedStmt VisitDoStmt(DoStmt *S);
506 ExpectedStmt VisitForStmt(ForStmt *S);
507 ExpectedStmt VisitGotoStmt(GotoStmt *S);
508 ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S);
509 ExpectedStmt VisitContinueStmt(ContinueStmt *S);
510 ExpectedStmt VisitBreakStmt(BreakStmt *S);
511 ExpectedStmt VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000512 // FIXME: MSAsmStmt
513 // FIXME: SEHExceptStmt
514 // FIXME: SEHFinallyStmt
515 // FIXME: SEHTryStmt
516 // FIXME: SEHLeaveStmt
517 // FIXME: CapturedStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000518 ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S);
519 ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S);
520 ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000521 // FIXME: MSDependentExistsStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000522 ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
523 ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
524 ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
525 ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S);
526 ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
527 ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
528 ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000529
530 // Importing expressions
Balazs Keri3b30d652018-10-19 13:32:20 +0000531 ExpectedStmt VisitExpr(Expr *E);
532 ExpectedStmt VisitVAArgExpr(VAArgExpr *E);
Tom Roeder521f0042019-02-26 19:26:41 +0000533 ExpectedStmt VisitChooseExpr(ChooseExpr *E);
Balazs Keri3b30d652018-10-19 13:32:20 +0000534 ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E);
535 ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E);
536 ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E);
537 ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
538 ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E);
539 ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
540 ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E);
541 ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E);
542 ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E);
543 ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E);
544 ExpectedStmt VisitStringLiteral(StringLiteral *E);
545 ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
546 ExpectedStmt VisitAtomicExpr(AtomicExpr *E);
547 ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E);
Bill Wendling8003edc2018-11-09 00:41:36 +0000548 ExpectedStmt VisitConstantExpr(ConstantExpr *E);
Balazs Keri3b30d652018-10-19 13:32:20 +0000549 ExpectedStmt VisitParenExpr(ParenExpr *E);
550 ExpectedStmt VisitParenListExpr(ParenListExpr *E);
551 ExpectedStmt VisitStmtExpr(StmtExpr *E);
552 ExpectedStmt VisitUnaryOperator(UnaryOperator *E);
553 ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
554 ExpectedStmt VisitBinaryOperator(BinaryOperator *E);
555 ExpectedStmt VisitConditionalOperator(ConditionalOperator *E);
556 ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
557 ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E);
558 ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
559 ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E);
560 ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E);
561 ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E);
562 ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E);
563 ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E);
564 ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE);
565 ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E);
566 ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
567 ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
568 ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
569 ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
570 ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
571 ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
572 ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E);
573 ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E);
574 ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E);
575 ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E);
576 ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E);
577 ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
578 ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
579 ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
580 ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
581 ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
582 ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
583 ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E);
584 ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E);
585 ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
586 ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
587 ExpectedStmt VisitMemberExpr(MemberExpr *E);
588 ExpectedStmt VisitCallExpr(CallExpr *E);
589 ExpectedStmt VisitLambdaExpr(LambdaExpr *LE);
590 ExpectedStmt VisitInitListExpr(InitListExpr *E);
591 ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
592 ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E);
593 ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
594 ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
595 ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
596 ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
597 ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
598 ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E);
599 ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000600
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000601 template<typename IIter, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000602 Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000603 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
Balazs Keri3b30d652018-10-19 13:32:20 +0000604 for (; Ibegin != Iend; ++Ibegin, ++Obegin) {
605 Expected<ItemT> ToOrErr = import(*Ibegin);
606 if (!ToOrErr)
607 return ToOrErr.takeError();
608 *Obegin = *ToOrErr;
609 }
610 return Error::success();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000611 }
612
Balazs Keri3b30d652018-10-19 13:32:20 +0000613 // Import every item from a container structure into an output container.
614 // If error occurs, stops at first error and returns the error.
615 // The output container should have space for all needed elements (it is not
616 // expanded, new items are put into from the beginning).
Aleksei Sidorina693b372016-09-28 10:16:56 +0000617 template<typename InContainerTy, typename OutContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000618 Error ImportContainerChecked(
619 const InContainerTy &InContainer, OutContainerTy &OutContainer) {
620 return ImportArrayChecked(
621 InContainer.begin(), InContainer.end(), OutContainer.begin());
Aleksei Sidorina693b372016-09-28 10:16:56 +0000622 }
623
624 template<typename InContainerTy, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000625 Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000626 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
627 }
Lang Hames19e07e12017-06-20 21:06:00 +0000628
Lang Hames19e07e12017-06-20 21:06:00 +0000629 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000630
Balazs Keri3b30d652018-10-19 13:32:20 +0000631 Expected<FunctionDecl *> FindFunctionTemplateSpecialization(
632 FunctionDecl *FromFD);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000633 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000634
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000635template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000636Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000637 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
638 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000639 auto ToLAngleLocOrErr = import(FromLAngleLoc);
640 if (!ToLAngleLocOrErr)
641 return ToLAngleLocOrErr.takeError();
642 auto ToRAngleLocOrErr = import(FromRAngleLoc);
643 if (!ToRAngleLocOrErr)
644 return ToRAngleLocOrErr.takeError();
645
646 TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr);
647 if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo))
648 return Err;
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000649 Result = ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +0000650 return Error::success();
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000651}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000652
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000653template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000654Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000655 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
656 return ImportTemplateArgumentListInfo(
657 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
658}
659
660template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000661Error ASTNodeImporter::ImportTemplateArgumentListInfo<
662 ASTTemplateArgumentListInfo>(
663 const ASTTemplateArgumentListInfo &From,
664 TemplateArgumentListInfo &Result) {
665 return ImportTemplateArgumentListInfo(
666 From.LAngleLoc, From.RAngleLoc, From.arguments(), Result);
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000667}
668
Balazs Keri3b30d652018-10-19 13:32:20 +0000669Expected<ASTNodeImporter::FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000670ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
671 FunctionDecl *FromFD) {
672 assert(FromFD->getTemplatedKind() ==
Balazs Keri3b30d652018-10-19 13:32:20 +0000673 FunctionDecl::TK_FunctionTemplateSpecialization);
674
675 FunctionTemplateAndArgsTy Result;
676
Gabor Marton5254e642018-06-27 13:32:50 +0000677 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Balazs Keri3b30d652018-10-19 13:32:20 +0000678 if (Error Err = importInto(std::get<0>(Result), FTSInfo->getTemplate()))
679 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000680
681 // Import template arguments.
682 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
Balazs Keri3b30d652018-10-19 13:32:20 +0000683 if (Error Err = ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
684 std::get<1>(Result)))
685 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000686
Balazs Keri3b30d652018-10-19 13:32:20 +0000687 return Result;
688}
689
690template <>
691Expected<TemplateParameterList *>
692ASTNodeImporter::import(TemplateParameterList *From) {
693 SmallVector<NamedDecl *, 4> To(From->size());
694 if (Error Err = ImportContainerChecked(*From, To))
695 return std::move(Err);
696
697 ExpectedExpr ToRequiresClause = import(From->getRequiresClause());
698 if (!ToRequiresClause)
699 return ToRequiresClause.takeError();
700
701 auto ToTemplateLocOrErr = import(From->getTemplateLoc());
702 if (!ToTemplateLocOrErr)
703 return ToTemplateLocOrErr.takeError();
704 auto ToLAngleLocOrErr = import(From->getLAngleLoc());
705 if (!ToLAngleLocOrErr)
706 return ToLAngleLocOrErr.takeError();
707 auto ToRAngleLocOrErr = import(From->getRAngleLoc());
708 if (!ToRAngleLocOrErr)
709 return ToRAngleLocOrErr.takeError();
710
711 return TemplateParameterList::Create(
712 Importer.getToContext(),
713 *ToTemplateLocOrErr,
714 *ToLAngleLocOrErr,
715 To,
716 *ToRAngleLocOrErr,
717 *ToRequiresClause);
718}
719
720template <>
721Expected<TemplateArgument>
722ASTNodeImporter::import(const TemplateArgument &From) {
723 switch (From.getKind()) {
724 case TemplateArgument::Null:
725 return TemplateArgument();
726
727 case TemplateArgument::Type: {
728 ExpectedType ToTypeOrErr = import(From.getAsType());
729 if (!ToTypeOrErr)
730 return ToTypeOrErr.takeError();
731 return TemplateArgument(*ToTypeOrErr);
732 }
733
734 case TemplateArgument::Integral: {
735 ExpectedType ToTypeOrErr = import(From.getIntegralType());
736 if (!ToTypeOrErr)
737 return ToTypeOrErr.takeError();
738 return TemplateArgument(From, *ToTypeOrErr);
739 }
740
741 case TemplateArgument::Declaration: {
742 Expected<ValueDecl *> ToOrErr = import(From.getAsDecl());
743 if (!ToOrErr)
744 return ToOrErr.takeError();
745 ExpectedType ToTypeOrErr = import(From.getParamTypeForDecl());
746 if (!ToTypeOrErr)
747 return ToTypeOrErr.takeError();
748 return TemplateArgument(*ToOrErr, *ToTypeOrErr);
749 }
750
751 case TemplateArgument::NullPtr: {
752 ExpectedType ToTypeOrErr = import(From.getNullPtrType());
753 if (!ToTypeOrErr)
754 return ToTypeOrErr.takeError();
755 return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/true);
756 }
757
758 case TemplateArgument::Template: {
759 Expected<TemplateName> ToTemplateOrErr = import(From.getAsTemplate());
760 if (!ToTemplateOrErr)
761 return ToTemplateOrErr.takeError();
762
763 return TemplateArgument(*ToTemplateOrErr);
764 }
765
766 case TemplateArgument::TemplateExpansion: {
767 Expected<TemplateName> ToTemplateOrErr =
768 import(From.getAsTemplateOrTemplatePattern());
769 if (!ToTemplateOrErr)
770 return ToTemplateOrErr.takeError();
771
772 return TemplateArgument(
773 *ToTemplateOrErr, From.getNumTemplateExpansions());
774 }
775
776 case TemplateArgument::Expression:
777 if (ExpectedExpr ToExpr = import(From.getAsExpr()))
778 return TemplateArgument(*ToExpr);
779 else
780 return ToExpr.takeError();
781
782 case TemplateArgument::Pack: {
783 SmallVector<TemplateArgument, 2> ToPack;
784 ToPack.reserve(From.pack_size());
785 if (Error Err = ImportTemplateArguments(
786 From.pack_begin(), From.pack_size(), ToPack))
787 return std::move(Err);
788
789 return TemplateArgument(
790 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
791 }
792 }
793
794 llvm_unreachable("Invalid template argument kind");
795}
796
797template <>
798Expected<TemplateArgumentLoc>
799ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) {
800 Expected<TemplateArgument> ArgOrErr = import(TALoc.getArgument());
801 if (!ArgOrErr)
802 return ArgOrErr.takeError();
803 TemplateArgument Arg = *ArgOrErr;
804
805 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
806
807 TemplateArgumentLocInfo ToInfo;
808 if (Arg.getKind() == TemplateArgument::Expression) {
809 ExpectedExpr E = import(FromInfo.getAsExpr());
810 if (!E)
811 return E.takeError();
812 ToInfo = TemplateArgumentLocInfo(*E);
813 } else if (Arg.getKind() == TemplateArgument::Type) {
814 if (auto TSIOrErr = import(FromInfo.getAsTypeSourceInfo()))
815 ToInfo = TemplateArgumentLocInfo(*TSIOrErr);
816 else
817 return TSIOrErr.takeError();
818 } else {
819 auto ToTemplateQualifierLocOrErr =
820 import(FromInfo.getTemplateQualifierLoc());
821 if (!ToTemplateQualifierLocOrErr)
822 return ToTemplateQualifierLocOrErr.takeError();
823 auto ToTemplateNameLocOrErr = import(FromInfo.getTemplateNameLoc());
824 if (!ToTemplateNameLocOrErr)
825 return ToTemplateNameLocOrErr.takeError();
826 auto ToTemplateEllipsisLocOrErr =
827 import(FromInfo.getTemplateEllipsisLoc());
828 if (!ToTemplateEllipsisLocOrErr)
829 return ToTemplateEllipsisLocOrErr.takeError();
830
831 ToInfo = TemplateArgumentLocInfo(
832 *ToTemplateQualifierLocOrErr,
833 *ToTemplateNameLocOrErr,
834 *ToTemplateEllipsisLocOrErr);
835 }
836
837 return TemplateArgumentLoc(Arg, ToInfo);
838}
839
840template <>
841Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) {
842 if (DG.isNull())
843 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
844 size_t NumDecls = DG.end() - DG.begin();
845 SmallVector<Decl *, 1> ToDecls;
846 ToDecls.reserve(NumDecls);
847 for (Decl *FromD : DG) {
848 if (auto ToDOrErr = import(FromD))
849 ToDecls.push_back(*ToDOrErr);
850 else
851 return ToDOrErr.takeError();
852 }
853 return DeclGroupRef::Create(Importer.getToContext(),
854 ToDecls.begin(),
855 NumDecls);
856}
857
858template <>
859Expected<ASTNodeImporter::Designator>
860ASTNodeImporter::import(const Designator &D) {
861 if (D.isFieldDesignator()) {
862 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
863
864 ExpectedSLoc ToDotLocOrErr = import(D.getDotLoc());
865 if (!ToDotLocOrErr)
866 return ToDotLocOrErr.takeError();
867
868 ExpectedSLoc ToFieldLocOrErr = import(D.getFieldLoc());
869 if (!ToFieldLocOrErr)
870 return ToFieldLocOrErr.takeError();
871
872 return Designator(ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr);
873 }
874
875 ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc());
876 if (!ToLBracketLocOrErr)
877 return ToLBracketLocOrErr.takeError();
878
879 ExpectedSLoc ToRBracketLocOrErr = import(D.getRBracketLoc());
880 if (!ToRBracketLocOrErr)
881 return ToRBracketLocOrErr.takeError();
882
883 if (D.isArrayDesignator())
884 return Designator(D.getFirstExprIndex(),
885 *ToLBracketLocOrErr, *ToRBracketLocOrErr);
886
887 ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc());
888 if (!ToEllipsisLocOrErr)
889 return ToEllipsisLocOrErr.takeError();
890
891 assert(D.isArrayRangeDesignator());
892 return Designator(
893 D.getFirstExprIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr,
894 *ToRBracketLocOrErr);
895}
896
897template <>
898Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) {
899 VarDecl *Var = nullptr;
900 if (From.capturesVariable()) {
901 if (auto VarOrErr = import(From.getCapturedVar()))
902 Var = *VarOrErr;
903 else
904 return VarOrErr.takeError();
905 }
906
907 auto LocationOrErr = import(From.getLocation());
908 if (!LocationOrErr)
909 return LocationOrErr.takeError();
910
911 SourceLocation EllipsisLoc;
912 if (From.isPackExpansion())
913 if (Error Err = importInto(EllipsisLoc, From.getEllipsisLoc()))
914 return std::move(Err);
915
916 return LambdaCapture(
917 *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var,
918 EllipsisLoc);
Gabor Marton5254e642018-06-27 13:32:50 +0000919}
920
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000921} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000922
Douglas Gregor3996e242010-02-15 22:01:00 +0000923//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000924// Import Types
925//----------------------------------------------------------------------------
926
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000927using namespace clang;
928
Balazs Keri3b30d652018-10-19 13:32:20 +0000929ExpectedType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000930 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
931 << T->getTypeClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +0000932 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000933}
934
Balazs Keri3b30d652018-10-19 13:32:20 +0000935ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
936 ExpectedType UnderlyingTypeOrErr = import(T->getValueType());
937 if (!UnderlyingTypeOrErr)
938 return UnderlyingTypeOrErr.takeError();
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000939
Balazs Keri3b30d652018-10-19 13:32:20 +0000940 return Importer.getToContext().getAtomicType(*UnderlyingTypeOrErr);
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000941}
942
Balazs Keri3b30d652018-10-19 13:32:20 +0000943ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000944 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000945#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
946 case BuiltinType::Id: \
947 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000948#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +0000949#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
950 case BuiltinType::Id: \
951 return Importer.getToContext().Id##Ty;
952#include "clang/Basic/OpenCLExtensionTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000953#define SHARED_SINGLETON_TYPE(Expansion)
954#define BUILTIN_TYPE(Id, SingletonId) \
955 case BuiltinType::Id: return Importer.getToContext().SingletonId;
956#include "clang/AST/BuiltinTypes.def"
957
958 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
959 // context supports C++.
960
961 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
962 // context supports ObjC.
963
Douglas Gregor96e578d2010-02-05 17:54:41 +0000964 case BuiltinType::Char_U:
Fangrui Song6907ce22018-07-30 19:24:48 +0000965 // The context we're importing from has an unsigned 'char'. If we're
966 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +0000967 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000968 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000969 return Importer.getToContext().UnsignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +0000970
Douglas Gregor96e578d2010-02-05 17:54:41 +0000971 return Importer.getToContext().CharTy;
972
Douglas Gregor96e578d2010-02-05 17:54:41 +0000973 case BuiltinType::Char_S:
Fangrui Song6907ce22018-07-30 19:24:48 +0000974 // The context we're importing from has an unsigned 'char'. If we're
975 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +0000976 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000977 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000978 return Importer.getToContext().SignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +0000979
Douglas Gregor96e578d2010-02-05 17:54:41 +0000980 return Importer.getToContext().CharTy;
981
Chris Lattnerad3467e2010-12-25 23:25:43 +0000982 case BuiltinType::WChar_S:
983 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000984 // FIXME: If not in C++, shall we translate to the C equivalent of
985 // wchar_t?
986 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000987 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000988
989 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000990}
991
Balazs Keri3b30d652018-10-19 13:32:20 +0000992ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
993 ExpectedType ToOriginalTypeOrErr = import(T->getOriginalType());
994 if (!ToOriginalTypeOrErr)
995 return ToOriginalTypeOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +0000996
Balazs Keri3b30d652018-10-19 13:32:20 +0000997 return Importer.getToContext().getDecayedType(*ToOriginalTypeOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000998}
999
Balazs Keri3b30d652018-10-19 13:32:20 +00001000ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
1001 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1002 if (!ToElementTypeOrErr)
1003 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001004
Balazs Keri3b30d652018-10-19 13:32:20 +00001005 return Importer.getToContext().getComplexType(*ToElementTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001006}
1007
Balazs Keri3b30d652018-10-19 13:32:20 +00001008ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) {
1009 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1010 if (!ToPointeeTypeOrErr)
1011 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001012
Balazs Keri3b30d652018-10-19 13:32:20 +00001013 return Importer.getToContext().getPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001014}
1015
Balazs Keri3b30d652018-10-19 13:32:20 +00001016ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001017 // FIXME: Check for blocks support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001018 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1019 if (!ToPointeeTypeOrErr)
1020 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001021
Balazs Keri3b30d652018-10-19 13:32:20 +00001022 return Importer.getToContext().getBlockPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001023}
1024
Balazs Keri3b30d652018-10-19 13:32:20 +00001025ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001026ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001027 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001028 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1029 if (!ToPointeeTypeOrErr)
1030 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001031
Balazs Keri3b30d652018-10-19 13:32:20 +00001032 return Importer.getToContext().getLValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001033}
1034
Balazs Keri3b30d652018-10-19 13:32:20 +00001035ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001036ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001037 // FIXME: Check for C++0x support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001038 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1039 if (!ToPointeeTypeOrErr)
1040 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001041
Balazs Keri3b30d652018-10-19 13:32:20 +00001042 return Importer.getToContext().getRValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001043}
1044
Balazs Keri3b30d652018-10-19 13:32:20 +00001045ExpectedType
1046ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001047 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001048 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1049 if (!ToPointeeTypeOrErr)
1050 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001051
Balazs Keri3b30d652018-10-19 13:32:20 +00001052 ExpectedType ClassTypeOrErr = import(QualType(T->getClass(), 0));
1053 if (!ClassTypeOrErr)
1054 return ClassTypeOrErr.takeError();
1055
1056 return Importer.getToContext().getMemberPointerType(
1057 *ToPointeeTypeOrErr, (*ClassTypeOrErr).getTypePtr());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001058}
1059
Balazs Keri3b30d652018-10-19 13:32:20 +00001060ExpectedType
1061ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
1062 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1063 if (!ToElementTypeOrErr)
1064 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001065
Balazs Keri3b30d652018-10-19 13:32:20 +00001066 return Importer.getToContext().getConstantArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001067 T->getSize(),
1068 T->getSizeModifier(),
1069 T->getIndexTypeCVRQualifiers());
1070}
1071
Balazs Keri3b30d652018-10-19 13:32:20 +00001072ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001073ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001074 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1075 if (!ToElementTypeOrErr)
1076 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001077
Balazs Keri3b30d652018-10-19 13:32:20 +00001078 return Importer.getToContext().getIncompleteArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001079 T->getSizeModifier(),
1080 T->getIndexTypeCVRQualifiers());
1081}
1082
Balazs Keri3b30d652018-10-19 13:32:20 +00001083ExpectedType
1084ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
1085 QualType ToElementType;
1086 Expr *ToSizeExpr;
1087 SourceRange ToBracketsRange;
1088 if (auto Imp = importSeq(
1089 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1090 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1091 else
1092 return Imp.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001093
Balazs Keri3b30d652018-10-19 13:32:20 +00001094 return Importer.getToContext().getVariableArrayType(
1095 ToElementType, ToSizeExpr, T->getSizeModifier(),
1096 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001097}
1098
Balazs Keri3b30d652018-10-19 13:32:20 +00001099ExpectedType ASTNodeImporter::VisitDependentSizedArrayType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001100 const DependentSizedArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001101 QualType ToElementType;
1102 Expr *ToSizeExpr;
1103 SourceRange ToBracketsRange;
1104 if (auto Imp = importSeq(
1105 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1106 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1107 else
1108 return Imp.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001109 // SizeExpr may be null if size is not specified directly.
1110 // For example, 'int a[]'.
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001111
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001112 return Importer.getToContext().getDependentSizedArrayType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001113 ToElementType, ToSizeExpr, T->getSizeModifier(),
1114 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001115}
1116
Balazs Keri3b30d652018-10-19 13:32:20 +00001117ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) {
1118 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1119 if (!ToElementTypeOrErr)
1120 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001121
Balazs Keri3b30d652018-10-19 13:32:20 +00001122 return Importer.getToContext().getVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001123 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001124 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001125}
1126
Balazs Keri3b30d652018-10-19 13:32:20 +00001127ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
1128 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1129 if (!ToElementTypeOrErr)
1130 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001131
Balazs Keri3b30d652018-10-19 13:32:20 +00001132 return Importer.getToContext().getExtVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001133 T->getNumElements());
1134}
1135
Balazs Keri3b30d652018-10-19 13:32:20 +00001136ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001137ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001138 // FIXME: What happens if we're importing a function without a prototype
Douglas Gregor96e578d2010-02-05 17:54:41 +00001139 // into C++? Should we make it variadic?
Balazs Keri3b30d652018-10-19 13:32:20 +00001140 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1141 if (!ToReturnTypeOrErr)
1142 return ToReturnTypeOrErr.takeError();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001143
Balazs Keri3b30d652018-10-19 13:32:20 +00001144 return Importer.getToContext().getFunctionNoProtoType(*ToReturnTypeOrErr,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001145 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001146}
1147
Balazs Keri3b30d652018-10-19 13:32:20 +00001148ExpectedType
1149ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
1150 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1151 if (!ToReturnTypeOrErr)
1152 return ToReturnTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001153
Douglas Gregor96e578d2010-02-05 17:54:41 +00001154 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001155 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001156 for (const auto &A : T->param_types()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001157 ExpectedType TyOrErr = import(A);
1158 if (!TyOrErr)
1159 return TyOrErr.takeError();
1160 ArgTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001161 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001162
Douglas Gregor96e578d2010-02-05 17:54:41 +00001163 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001164 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001165 for (const auto &E : T->exceptions()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001166 ExpectedType TyOrErr = import(E);
1167 if (!TyOrErr)
1168 return TyOrErr.takeError();
1169 ExceptionTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001170 }
John McCalldb40c7f2010-12-14 08:05:40 +00001171
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001172 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1173 FunctionProtoType::ExtProtoInfo ToEPI;
1174
Balazs Keri3b30d652018-10-19 13:32:20 +00001175 auto Imp = importSeq(
1176 FromEPI.ExceptionSpec.NoexceptExpr,
1177 FromEPI.ExceptionSpec.SourceDecl,
1178 FromEPI.ExceptionSpec.SourceTemplate);
1179 if (!Imp)
1180 return Imp.takeError();
1181
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001182 ToEPI.ExtInfo = FromEPI.ExtInfo;
1183 ToEPI.Variadic = FromEPI.Variadic;
1184 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1185 ToEPI.TypeQuals = FromEPI.TypeQuals;
1186 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001187 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1188 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
Balazs Keri3b30d652018-10-19 13:32:20 +00001189 std::tie(
1190 ToEPI.ExceptionSpec.NoexceptExpr,
1191 ToEPI.ExceptionSpec.SourceDecl,
1192 ToEPI.ExceptionSpec.SourceTemplate) = *Imp;
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001193
Balazs Keri3b30d652018-10-19 13:32:20 +00001194 return Importer.getToContext().getFunctionType(
1195 *ToReturnTypeOrErr, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001196}
1197
Balazs Keri3b30d652018-10-19 13:32:20 +00001198ExpectedType ASTNodeImporter::VisitUnresolvedUsingType(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001199 const UnresolvedUsingType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001200 UnresolvedUsingTypenameDecl *ToD;
1201 Decl *ToPrevD;
1202 if (auto Imp = importSeq(T->getDecl(), T->getDecl()->getPreviousDecl()))
1203 std::tie(ToD, ToPrevD) = *Imp;
1204 else
1205 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001206
Balazs Keri3b30d652018-10-19 13:32:20 +00001207 return Importer.getToContext().getTypeDeclType(
1208 ToD, cast_or_null<TypeDecl>(ToPrevD));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001209}
1210
Balazs Keri3b30d652018-10-19 13:32:20 +00001211ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) {
1212 ExpectedType ToInnerTypeOrErr = import(T->getInnerType());
1213 if (!ToInnerTypeOrErr)
1214 return ToInnerTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001215
Balazs Keri3b30d652018-10-19 13:32:20 +00001216 return Importer.getToContext().getParenType(*ToInnerTypeOrErr);
Sean Callananda6df8a2011-08-11 16:56:07 +00001217}
1218
Balazs Keri3b30d652018-10-19 13:32:20 +00001219ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
1220 Expected<TypedefNameDecl *> ToDeclOrErr = import(T->getDecl());
1221 if (!ToDeclOrErr)
1222 return ToDeclOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001223
Balazs Keri3b30d652018-10-19 13:32:20 +00001224 return Importer.getToContext().getTypeDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001225}
1226
Balazs Keri3b30d652018-10-19 13:32:20 +00001227ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
1228 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1229 if (!ToExprOrErr)
1230 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001231
Balazs Keri3b30d652018-10-19 13:32:20 +00001232 return Importer.getToContext().getTypeOfExprType(*ToExprOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001233}
1234
Balazs Keri3b30d652018-10-19 13:32:20 +00001235ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
1236 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1237 if (!ToUnderlyingTypeOrErr)
1238 return ToUnderlyingTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001239
Balazs Keri3b30d652018-10-19 13:32:20 +00001240 return Importer.getToContext().getTypeOfType(*ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001241}
1242
Balazs Keri3b30d652018-10-19 13:32:20 +00001243ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001244 // FIXME: Make sure that the "to" context supports C++0x!
Balazs Keri3b30d652018-10-19 13:32:20 +00001245 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1246 if (!ToExprOrErr)
1247 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001248
Balazs Keri3b30d652018-10-19 13:32:20 +00001249 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1250 if (!ToUnderlyingTypeOrErr)
1251 return ToUnderlyingTypeOrErr.takeError();
Douglas Gregor81495f32012-02-12 18:42:33 +00001252
Balazs Keri3b30d652018-10-19 13:32:20 +00001253 return Importer.getToContext().getDecltypeType(
1254 *ToExprOrErr, *ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001255}
1256
Balazs Keri3b30d652018-10-19 13:32:20 +00001257ExpectedType
1258ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1259 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1260 if (!ToBaseTypeOrErr)
1261 return ToBaseTypeOrErr.takeError();
Alexis Hunte852b102011-05-24 22:41:36 +00001262
Balazs Keri3b30d652018-10-19 13:32:20 +00001263 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1264 if (!ToUnderlyingTypeOrErr)
1265 return ToUnderlyingTypeOrErr.takeError();
1266
1267 return Importer.getToContext().getUnaryTransformType(
1268 *ToBaseTypeOrErr, *ToUnderlyingTypeOrErr, T->getUTTKind());
Alexis Hunte852b102011-05-24 22:41:36 +00001269}
1270
Balazs Keri3b30d652018-10-19 13:32:20 +00001271ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001272 // FIXME: Make sure that the "to" context supports C++11!
Balazs Keri3b30d652018-10-19 13:32:20 +00001273 ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType());
1274 if (!ToDeducedTypeOrErr)
1275 return ToDeducedTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001276
Balazs Keri3b30d652018-10-19 13:32:20 +00001277 return Importer.getToContext().getAutoType(*ToDeducedTypeOrErr,
1278 T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001279 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001280}
1281
Balazs Keri3b30d652018-10-19 13:32:20 +00001282ExpectedType ASTNodeImporter::VisitInjectedClassNameType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001283 const InjectedClassNameType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001284 Expected<CXXRecordDecl *> ToDeclOrErr = import(T->getDecl());
1285 if (!ToDeclOrErr)
1286 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001287
Balazs Keri3b30d652018-10-19 13:32:20 +00001288 ExpectedType ToInjTypeOrErr = import(T->getInjectedSpecializationType());
1289 if (!ToInjTypeOrErr)
1290 return ToInjTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001291
1292 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1293 // See comments in InjectedClassNameType definition for details
1294 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1295 enum {
1296 TypeAlignmentInBits = 4,
1297 TypeAlignment = 1 << TypeAlignmentInBits
1298 };
1299
1300 return QualType(new (Importer.getToContext(), TypeAlignment)
Balazs Keri3b30d652018-10-19 13:32:20 +00001301 InjectedClassNameType(*ToDeclOrErr, *ToInjTypeOrErr), 0);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001302}
1303
Balazs Keri3b30d652018-10-19 13:32:20 +00001304ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) {
1305 Expected<RecordDecl *> ToDeclOrErr = import(T->getDecl());
1306 if (!ToDeclOrErr)
1307 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001308
Balazs Keri3b30d652018-10-19 13:32:20 +00001309 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001310}
1311
Balazs Keri3b30d652018-10-19 13:32:20 +00001312ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) {
1313 Expected<EnumDecl *> ToDeclOrErr = import(T->getDecl());
1314 if (!ToDeclOrErr)
1315 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001316
Balazs Keri3b30d652018-10-19 13:32:20 +00001317 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001318}
1319
Balazs Keri3b30d652018-10-19 13:32:20 +00001320ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1321 ExpectedType ToModifiedTypeOrErr = import(T->getModifiedType());
1322 if (!ToModifiedTypeOrErr)
1323 return ToModifiedTypeOrErr.takeError();
1324 ExpectedType ToEquivalentTypeOrErr = import(T->getEquivalentType());
1325 if (!ToEquivalentTypeOrErr)
1326 return ToEquivalentTypeOrErr.takeError();
Sean Callanan72fe0852015-04-02 23:50:08 +00001327
1328 return Importer.getToContext().getAttributedType(T->getAttrKind(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001329 *ToModifiedTypeOrErr, *ToEquivalentTypeOrErr);
Sean Callanan72fe0852015-04-02 23:50:08 +00001330}
1331
Balazs Keri3b30d652018-10-19 13:32:20 +00001332ExpectedType ASTNodeImporter::VisitTemplateTypeParmType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001333 const TemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001334 Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(T->getDecl());
1335 if (!ToDeclOrErr)
1336 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001337
1338 return Importer.getToContext().getTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001339 T->getDepth(), T->getIndex(), T->isParameterPack(), *ToDeclOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001340}
1341
Balazs Keri3b30d652018-10-19 13:32:20 +00001342ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001343 const SubstTemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001344 ExpectedType ReplacedOrErr = import(QualType(T->getReplacedParameter(), 0));
1345 if (!ReplacedOrErr)
1346 return ReplacedOrErr.takeError();
1347 const TemplateTypeParmType *Replaced =
1348 cast<TemplateTypeParmType>((*ReplacedOrErr).getTypePtr());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001349
Balazs Keri3b30d652018-10-19 13:32:20 +00001350 ExpectedType ToReplacementTypeOrErr = import(T->getReplacementType());
1351 if (!ToReplacementTypeOrErr)
1352 return ToReplacementTypeOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001353
1354 return Importer.getToContext().getSubstTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001355 Replaced, (*ToReplacementTypeOrErr).getCanonicalType());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001356}
1357
Balazs Keri3b30d652018-10-19 13:32:20 +00001358ExpectedType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001359 const TemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001360 auto ToTemplateOrErr = import(T->getTemplateName());
1361 if (!ToTemplateOrErr)
1362 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001363
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001364 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00001365 if (Error Err = ImportTemplateArguments(
1366 T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1367 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001368
Douglas Gregore2e50d332010-12-01 01:36:18 +00001369 QualType ToCanonType;
1370 if (!QualType(T, 0).isCanonical()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001371 QualType FromCanonType
Douglas Gregore2e50d332010-12-01 01:36:18 +00001372 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
Balazs Keri3b30d652018-10-19 13:32:20 +00001373 if (ExpectedType TyOrErr = import(FromCanonType))
1374 ToCanonType = *TyOrErr;
1375 else
1376 return TyOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001377 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001378 return Importer.getToContext().getTemplateSpecializationType(*ToTemplateOrErr,
David Majnemer6fbeee32016-07-07 04:43:07 +00001379 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001380 ToCanonType);
1381}
1382
Balazs Keri3b30d652018-10-19 13:32:20 +00001383ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001384 // Note: the qualifier in an ElaboratedType is optional.
Balazs Keri3b30d652018-10-19 13:32:20 +00001385 auto ToQualifierOrErr = import(T->getQualifier());
1386 if (!ToQualifierOrErr)
1387 return ToQualifierOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001388
Balazs Keri3b30d652018-10-19 13:32:20 +00001389 ExpectedType ToNamedTypeOrErr = import(T->getNamedType());
1390 if (!ToNamedTypeOrErr)
1391 return ToNamedTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001392
Balazs Keri3b30d652018-10-19 13:32:20 +00001393 Expected<TagDecl *> ToOwnedTagDeclOrErr = import(T->getOwnedTagDecl());
1394 if (!ToOwnedTagDeclOrErr)
1395 return ToOwnedTagDeclOrErr.takeError();
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001396
Abramo Bagnara6150c882010-05-11 21:36:43 +00001397 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001398 *ToQualifierOrErr,
1399 *ToNamedTypeOrErr,
1400 *ToOwnedTagDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001401}
1402
Balazs Keri3b30d652018-10-19 13:32:20 +00001403ExpectedType
1404ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
1405 ExpectedType ToPatternOrErr = import(T->getPattern());
1406 if (!ToPatternOrErr)
1407 return ToPatternOrErr.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00001408
Balazs Keri3b30d652018-10-19 13:32:20 +00001409 return Importer.getToContext().getPackExpansionType(*ToPatternOrErr,
Gabor Horvath7a91c082017-11-14 11:30:38 +00001410 T->getNumExpansions());
1411}
1412
Balazs Keri3b30d652018-10-19 13:32:20 +00001413ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001414 const DependentTemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001415 auto ToQualifierOrErr = import(T->getQualifier());
1416 if (!ToQualifierOrErr)
1417 return ToQualifierOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001418
Balazs Keri3b30d652018-10-19 13:32:20 +00001419 IdentifierInfo *ToName = Importer.Import(T->getIdentifier());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001420
1421 SmallVector<TemplateArgument, 2> ToPack;
1422 ToPack.reserve(T->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00001423 if (Error Err = ImportTemplateArguments(
1424 T->getArgs(), T->getNumArgs(), ToPack))
1425 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001426
1427 return Importer.getToContext().getDependentTemplateSpecializationType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001428 T->getKeyword(), *ToQualifierOrErr, ToName, ToPack);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001429}
1430
Balazs Keri3b30d652018-10-19 13:32:20 +00001431ExpectedType
1432ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
1433 auto ToQualifierOrErr = import(T->getQualifier());
1434 if (!ToQualifierOrErr)
1435 return ToQualifierOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00001436
1437 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
Peter Szecsice7f3182018-05-07 12:08:27 +00001438
Balazs Keri3b30d652018-10-19 13:32:20 +00001439 QualType Canon;
1440 if (T != T->getCanonicalTypeInternal().getTypePtr()) {
1441 if (ExpectedType TyOrErr = import(T->getCanonicalTypeInternal()))
1442 Canon = (*TyOrErr).getCanonicalType();
1443 else
1444 return TyOrErr.takeError();
1445 }
Peter Szecsice7f3182018-05-07 12:08:27 +00001446
Balazs Keri3b30d652018-10-19 13:32:20 +00001447 return Importer.getToContext().getDependentNameType(T->getKeyword(),
1448 *ToQualifierOrErr,
Peter Szecsice7f3182018-05-07 12:08:27 +00001449 Name, Canon);
1450}
1451
Balazs Keri3b30d652018-10-19 13:32:20 +00001452ExpectedType
1453ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
1454 Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(T->getDecl());
1455 if (!ToDeclOrErr)
1456 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001457
Balazs Keri3b30d652018-10-19 13:32:20 +00001458 return Importer.getToContext().getObjCInterfaceType(*ToDeclOrErr);
John McCall8b07ec22010-05-15 11:32:37 +00001459}
1460
Balazs Keri3b30d652018-10-19 13:32:20 +00001461ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1462 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1463 if (!ToBaseTypeOrErr)
1464 return ToBaseTypeOrErr.takeError();
John McCall8b07ec22010-05-15 11:32:37 +00001465
Douglas Gregore9d95f12015-07-07 03:57:35 +00001466 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001467 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001468 if (ExpectedType TyOrErr = import(TypeArg))
1469 TypeArgs.push_back(*TyOrErr);
1470 else
1471 return TyOrErr.takeError();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001472 }
1473
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001474 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001475 for (auto *P : T->quals()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001476 if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(P))
1477 Protocols.push_back(*ProtocolOrErr);
1478 else
1479 return ProtocolOrErr.takeError();
1480
Douglas Gregor96e578d2010-02-05 17:54:41 +00001481 }
1482
Balazs Keri3b30d652018-10-19 13:32:20 +00001483 return Importer.getToContext().getObjCObjectType(*ToBaseTypeOrErr, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001484 Protocols,
1485 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001486}
1487
Balazs Keri3b30d652018-10-19 13:32:20 +00001488ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001489ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001490 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1491 if (!ToPointeeTypeOrErr)
1492 return ToPointeeTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001493
Balazs Keri3b30d652018-10-19 13:32:20 +00001494 return Importer.getToContext().getObjCObjectPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001495}
1496
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001497//----------------------------------------------------------------------------
1498// Import Declarations
1499//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00001500Error ASTNodeImporter::ImportDeclParts(
1501 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
1502 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) {
Gabor Marton6e1510c2018-07-12 11:50:21 +00001503 // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop.
1504 // example: int struct_in_proto(struct data_t{int a;int b;} *d);
1505 DeclContext *OrigDC = D->getDeclContext();
1506 FunctionDecl *FunDecl;
1507 if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) &&
1508 FunDecl->hasBody()) {
Gabor Martonfe68e292018-08-06 14:38:37 +00001509 auto getLeafPointeeType = [](const Type *T) {
1510 while (T->isPointerType() || T->isArrayType()) {
1511 T = T->getPointeeOrArrayElementType();
1512 }
1513 return T;
1514 };
1515 for (const ParmVarDecl *P : FunDecl->parameters()) {
1516 const Type *LeafT =
1517 getLeafPointeeType(P->getType().getCanonicalType().getTypePtr());
1518 auto *RT = dyn_cast<RecordType>(LeafT);
1519 if (RT && RT->getDecl() == D) {
1520 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1521 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00001522 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Gabor Martonfe68e292018-08-06 14:38:37 +00001523 }
Gabor Marton6e1510c2018-07-12 11:50:21 +00001524 }
1525 }
1526
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001527 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001528 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
1529 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001530
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001531 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001532 if (Error Err = importInto(Name, D->getDeclName()))
1533 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001534
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001535 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001536 if (Error Err = importInto(Loc, D->getLocation()))
1537 return Err;
1538
Sean Callanan59721b32015-04-28 18:41:46 +00001539 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Gabor Martonbe77a982018-12-12 11:22:55 +00001540 if (ToD)
1541 if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(D, ToD))
1542 return Err;
Balazs Keri3b30d652018-10-19 13:32:20 +00001543
1544 return Error::success();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001545}
1546
Balazs Keri3b30d652018-10-19 13:32:20 +00001547Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001548 if (!FromD)
Balazs Keri3b30d652018-10-19 13:32:20 +00001549 return Error::success();
Fangrui Song6907ce22018-07-30 19:24:48 +00001550
Balazs Keri3b30d652018-10-19 13:32:20 +00001551 if (!ToD)
1552 if (Error Err = importInto(ToD, FromD))
1553 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001554
Balazs Keri3b30d652018-10-19 13:32:20 +00001555 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1556 if (RecordDecl *ToRecord = cast<RecordDecl>(ToD)) {
1557 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() &&
1558 !ToRecord->getDefinition()) {
1559 if (Error Err = ImportDefinition(FromRecord, ToRecord))
1560 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001561 }
1562 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001563 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001564 }
1565
Balazs Keri3b30d652018-10-19 13:32:20 +00001566 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1567 if (EnumDecl *ToEnum = cast<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001568 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001569 if (Error Err = ImportDefinition(FromEnum, ToEnum))
1570 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001571 }
1572 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001573 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001574 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001575
1576 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001577}
1578
Balazs Keri3b30d652018-10-19 13:32:20 +00001579Error
1580ASTNodeImporter::ImportDeclarationNameLoc(
1581 const DeclarationNameInfo &From, DeclarationNameInfo& To) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001582 // NOTE: To.Name and To.Loc are already imported.
1583 // We only have to import To.LocInfo.
1584 switch (To.getName().getNameKind()) {
1585 case DeclarationName::Identifier:
1586 case DeclarationName::ObjCZeroArgSelector:
1587 case DeclarationName::ObjCOneArgSelector:
1588 case DeclarationName::ObjCMultiArgSelector:
1589 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001590 case DeclarationName::CXXDeductionGuideName:
Balazs Keri3b30d652018-10-19 13:32:20 +00001591 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001592
1593 case DeclarationName::CXXOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001594 if (auto ToRangeOrErr = import(From.getCXXOperatorNameRange()))
1595 To.setCXXOperatorNameRange(*ToRangeOrErr);
1596 else
1597 return ToRangeOrErr.takeError();
1598 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001599 }
1600 case DeclarationName::CXXLiteralOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001601 if (ExpectedSLoc LocOrErr = import(From.getCXXLiteralOperatorNameLoc()))
1602 To.setCXXLiteralOperatorNameLoc(*LocOrErr);
1603 else
1604 return LocOrErr.takeError();
1605 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001606 }
1607 case DeclarationName::CXXConstructorName:
1608 case DeclarationName::CXXDestructorName:
1609 case DeclarationName::CXXConversionFunctionName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001610 if (auto ToTInfoOrErr = import(From.getNamedTypeInfo()))
1611 To.setNamedTypeInfo(*ToTInfoOrErr);
1612 else
1613 return ToTInfoOrErr.takeError();
1614 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001615 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001616 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001617 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001618}
1619
Balazs Keri3b30d652018-10-19 13:32:20 +00001620Error
1621ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001622 if (Importer.isMinimalImport() && !ForceImport) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001623 auto ToDCOrErr = Importer.ImportContext(FromDC);
1624 return ToDCOrErr.takeError();
1625 }
Davide Italiano93a64ef2018-10-30 20:46:29 +00001626 llvm::SmallVector<Decl *, 8> ImportedDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00001627 for (auto *From : FromDC->decls()) {
1628 ExpectedDecl ImportedOrErr = import(From);
Davide Italiano93a64ef2018-10-30 20:46:29 +00001629 if (!ImportedOrErr)
Balazs Keri3b30d652018-10-19 13:32:20 +00001630 // Ignore the error, continue with next Decl.
1631 // FIXME: Handle this case somehow better.
Davide Italiano93a64ef2018-10-30 20:46:29 +00001632 consumeError(ImportedOrErr.takeError());
Douglas Gregor0a791672011-01-18 03:11:38 +00001633 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001634
Balazs Keri3b30d652018-10-19 13:32:20 +00001635 return Error::success();
Douglas Gregor968d6332010-02-21 18:24:45 +00001636}
1637
Balazs Keri3b30d652018-10-19 13:32:20 +00001638Error ASTNodeImporter::ImportDeclContext(
1639 Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) {
1640 auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext());
1641 if (!ToDCOrErr)
1642 return ToDCOrErr.takeError();
1643 ToDC = *ToDCOrErr;
1644
1645 if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) {
1646 auto ToLexicalDCOrErr = Importer.ImportContext(
1647 FromD->getLexicalDeclContext());
1648 if (!ToLexicalDCOrErr)
1649 return ToLexicalDCOrErr.takeError();
1650 ToLexicalDC = *ToLexicalDCOrErr;
1651 } else
1652 ToLexicalDC = ToDC;
1653
1654 return Error::success();
1655}
1656
1657Error ASTNodeImporter::ImportImplicitMethods(
Balazs Keri1d20cc22018-07-16 12:16:39 +00001658 const CXXRecordDecl *From, CXXRecordDecl *To) {
1659 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1660 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001661
Balazs Keri1d20cc22018-07-16 12:16:39 +00001662 for (CXXMethodDecl *FromM : From->methods())
Balazs Keri3b30d652018-10-19 13:32:20 +00001663 if (FromM->isImplicit()) {
1664 Expected<CXXMethodDecl *> ToMOrErr = import(FromM);
1665 if (!ToMOrErr)
1666 return ToMOrErr.takeError();
1667 }
1668
1669 return Error::success();
Balazs Keri1d20cc22018-07-16 12:16:39 +00001670}
1671
Balazs Keri3b30d652018-10-19 13:32:20 +00001672static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1673 ASTImporter &Importer) {
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001674 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
Balazs Keri57949eb2019-03-25 09:16:39 +00001675 if (ExpectedDecl ToTypedefOrErr = Importer.Import_New(FromTypedef))
1676 To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr));
1677 else
1678 return ToTypedefOrErr.takeError();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001679 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001680 return Error::success();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001681}
1682
Balazs Keri3b30d652018-10-19 13:32:20 +00001683Error ASTNodeImporter::ImportDefinition(
1684 RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor95d82832012-01-24 18:36:04 +00001685 if (To->getDefinition() || To->isBeingDefined()) {
1686 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001687 return ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001688
Balazs Keri3b30d652018-10-19 13:32:20 +00001689 return Error::success();
Douglas Gregor95d82832012-01-24 18:36:04 +00001690 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001691
Douglas Gregore2e50d332010-12-01 01:36:18 +00001692 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001693
Balazs Keri3b30d652018-10-19 13:32:20 +00001694 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1695 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001696
Douglas Gregore2e50d332010-12-01 01:36:18 +00001697 // Add base classes.
Gabor Marton17d39672018-11-26 15:54:08 +00001698 auto *ToCXX = dyn_cast<CXXRecordDecl>(To);
1699 auto *FromCXX = dyn_cast<CXXRecordDecl>(From);
1700 if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) {
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001701
1702 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1703 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1704 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001705 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001706 ToData.Aggregate = FromData.Aggregate;
1707 ToData.PlainOldData = FromData.PlainOldData;
1708 ToData.Empty = FromData.Empty;
1709 ToData.Polymorphic = FromData.Polymorphic;
1710 ToData.Abstract = FromData.Abstract;
1711 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001712 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1713 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1714 ToData.HasBasesWithNonStaticDataMembers =
1715 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001716 ToData.HasPrivateFields = FromData.HasPrivateFields;
1717 ToData.HasProtectedFields = FromData.HasProtectedFields;
1718 ToData.HasPublicFields = FromData.HasPublicFields;
1719 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001720 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001721 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001722 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001723 ToData.HasUninitializedReferenceMember
1724 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001725 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001726 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1727 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001728 ToData.NeedOverloadResolutionForCopyConstructor
1729 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001730 ToData.NeedOverloadResolutionForMoveConstructor
1731 = FromData.NeedOverloadResolutionForMoveConstructor;
1732 ToData.NeedOverloadResolutionForMoveAssignment
1733 = FromData.NeedOverloadResolutionForMoveAssignment;
1734 ToData.NeedOverloadResolutionForDestructor
1735 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001736 ToData.DefaultedCopyConstructorIsDeleted
1737 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001738 ToData.DefaultedMoveConstructorIsDeleted
1739 = FromData.DefaultedMoveConstructorIsDeleted;
1740 ToData.DefaultedMoveAssignmentIsDeleted
1741 = FromData.DefaultedMoveAssignmentIsDeleted;
1742 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001743 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1744 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001745 ToData.HasConstexprNonCopyMoveConstructor
1746 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001747 ToData.HasDefaultedDefaultConstructor
1748 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001749 ToData.DefaultedDefaultConstructorIsConstexpr
1750 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001751 ToData.HasConstexprDefaultConstructor
1752 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001753 ToData.HasNonLiteralTypeFieldsOrBases
1754 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001755 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001756 ToData.UserProvidedDefaultConstructor
1757 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001758 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001759 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1760 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1761 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1762 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001763 ToData.ImplicitCopyAssignmentHasConstParam
1764 = FromData.ImplicitCopyAssignmentHasConstParam;
1765 ToData.HasDeclaredCopyConstructorWithConstParam
1766 = FromData.HasDeclaredCopyConstructorWithConstParam;
1767 ToData.HasDeclaredCopyAssignmentWithConstParam
1768 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001769
Shafik Yaghmour16b90732019-04-26 18:51:28 +00001770 // Copy over the data stored in RecordDeclBits
1771 ToCXX->setArgPassingRestrictions(FromCXX->getArgPassingRestrictions());
1772
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001773 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001774 for (const auto &Base1 : FromCXX->bases()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001775 ExpectedType TyOrErr = import(Base1.getType());
1776 if (!TyOrErr)
1777 return TyOrErr.takeError();
Douglas Gregor752a5952011-01-03 22:36:02 +00001778
1779 SourceLocation EllipsisLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00001780 if (Base1.isPackExpansion()) {
1781 if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc()))
1782 EllipsisLoc = *LocOrErr;
1783 else
1784 return LocOrErr.takeError();
1785 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001786
1787 // Ensure that we have a definition for the base.
Balazs Keri3b30d652018-10-19 13:32:20 +00001788 if (Error Err =
1789 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()))
1790 return Err;
1791
1792 auto RangeOrErr = import(Base1.getSourceRange());
1793 if (!RangeOrErr)
1794 return RangeOrErr.takeError();
1795
1796 auto TSIOrErr = import(Base1.getTypeSourceInfo());
1797 if (!TSIOrErr)
1798 return TSIOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001799
Douglas Gregore2e50d332010-12-01 01:36:18 +00001800 Bases.push_back(
Balazs Keri3b30d652018-10-19 13:32:20 +00001801 new (Importer.getToContext()) CXXBaseSpecifier(
1802 *RangeOrErr,
1803 Base1.isVirtual(),
1804 Base1.isBaseOfClass(),
1805 Base1.getAccessSpecifierAsWritten(),
1806 *TSIOrErr,
1807 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001808 }
1809 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001810 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001811 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001812
Douglas Gregor2e15c842012-02-01 21:00:38 +00001813 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001814 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1815 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001816
Douglas Gregore2e50d332010-12-01 01:36:18 +00001817 To->completeDefinition();
Balazs Keri3b30d652018-10-19 13:32:20 +00001818 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001819}
1820
Balazs Keri3b30d652018-10-19 13:32:20 +00001821Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) {
Sean Callanan59721b32015-04-28 18:41:46 +00001822 if (To->getAnyInitializer())
Balazs Keri3b30d652018-10-19 13:32:20 +00001823 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001824
Gabor Martonac3a5d62018-09-17 12:04:52 +00001825 Expr *FromInit = From->getInit();
1826 if (!FromInit)
Balazs Keri3b30d652018-10-19 13:32:20 +00001827 return Error::success();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001828
Balazs Keri3b30d652018-10-19 13:32:20 +00001829 ExpectedExpr ToInitOrErr = import(FromInit);
1830 if (!ToInitOrErr)
1831 return ToInitOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001832
Balazs Keri3b30d652018-10-19 13:32:20 +00001833 To->setInit(*ToInitOrErr);
Gabor Martonac3a5d62018-09-17 12:04:52 +00001834 if (From->isInitKnownICE()) {
1835 EvaluatedStmt *Eval = To->ensureEvaluatedStmt();
1836 Eval->CheckedICE = true;
1837 Eval->IsICE = From->isInitICE();
1838 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001839
1840 // FIXME: Other bits to merge?
Balazs Keri3b30d652018-10-19 13:32:20 +00001841 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001842}
1843
Balazs Keri3b30d652018-10-19 13:32:20 +00001844Error ASTNodeImporter::ImportDefinition(
1845 EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00001846 if (To->getDefinition() || To->isBeingDefined()) {
1847 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001848 return ImportDeclContext(From, /*ForceImport=*/true);
1849 return Error::success();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001850 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001851
Douglas Gregord451ea92011-07-29 23:31:30 +00001852 To->startDefinition();
1853
Balazs Keri3b30d652018-10-19 13:32:20 +00001854 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1855 return Err;
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001856
Balazs Keri3b30d652018-10-19 13:32:20 +00001857 ExpectedType ToTypeOrErr =
1858 import(Importer.getFromContext().getTypeDeclType(From));
1859 if (!ToTypeOrErr)
1860 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001861
Balazs Keri3b30d652018-10-19 13:32:20 +00001862 ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType());
1863 if (!ToPromotionTypeOrErr)
1864 return ToPromotionTypeOrErr.takeError();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001865
1866 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001867 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1868 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001869
Douglas Gregord451ea92011-07-29 23:31:30 +00001870 // FIXME: we might need to merge the number of positive or negative bits
1871 // if the enumerator lists don't match.
Balazs Keri3b30d652018-10-19 13:32:20 +00001872 To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr,
Douglas Gregord451ea92011-07-29 23:31:30 +00001873 From->getNumPositiveBits(),
1874 From->getNumNegativeBits());
Balazs Keri3b30d652018-10-19 13:32:20 +00001875 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001876}
1877
Balazs Keri3b30d652018-10-19 13:32:20 +00001878Error ASTNodeImporter::ImportTemplateArguments(
1879 const TemplateArgument *FromArgs, unsigned NumFromArgs,
1880 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001881 for (unsigned I = 0; I != NumFromArgs; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001882 if (auto ToOrErr = import(FromArgs[I]))
1883 ToArgs.push_back(*ToOrErr);
1884 else
1885 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001886 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001887
Balazs Keri3b30d652018-10-19 13:32:20 +00001888 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001889}
1890
Balazs Keri3b30d652018-10-19 13:32:20 +00001891// FIXME: Do not forget to remove this and use only 'import'.
1892Expected<TemplateArgument>
1893ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1894 return import(From);
1895}
1896
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001897template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +00001898Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001899 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1900 for (const auto &FromLoc : Container) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001901 if (auto ToLocOrErr = import(FromLoc))
1902 ToTAInfo.addArgument(*ToLocOrErr);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001903 else
Balazs Keri3b30d652018-10-19 13:32:20 +00001904 return ToLocOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001905 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001906 return Error::success();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001907}
1908
Gabor Marton26f72a92018-07-12 09:42:05 +00001909static StructuralEquivalenceKind
1910getStructuralEquivalenceKind(const ASTImporter &Importer) {
1911 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
1912 : StructuralEquivalenceKind::Default;
1913}
1914
Gabor Marton950fb572018-07-17 12:39:27 +00001915bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
1916 StructuralEquivalenceContext Ctx(
1917 Importer.getFromContext(), Importer.getToContext(),
1918 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1919 false, Complain);
1920 return Ctx.IsEquivalent(From, To);
1921}
1922
1923bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001924 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001925 // Eliminate a potential failure point where we attempt to re-import
1926 // something we're trying to import while completing ToRecord.
1927 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1928 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001929 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00001930 if (ToOriginRecord)
1931 ToRecord = ToOriginRecord;
1932 }
1933
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001934 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001935 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001936 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001937 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001938 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001939 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001940}
1941
Larisse Voufo39a1e502013-08-06 01:03:05 +00001942bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1943 bool Complain) {
1944 StructuralEquivalenceContext Ctx(
1945 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001946 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1947 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001948 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001949}
1950
Douglas Gregor98c10182010-02-12 22:17:39 +00001951bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00001952 // Eliminate a potential failure point where we attempt to re-import
Raphael Isemannfa26c202019-04-09 14:18:23 +00001953 // something we're trying to import while completing ToEnum.
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00001954 if (Decl *ToOrigin = Importer.GetOriginalDecl(ToEnum))
1955 if (auto *ToOriginEnum = dyn_cast<EnumDecl>(ToOrigin))
1956 ToEnum = ToOriginEnum;
1957
Gabor Marton26f72a92018-07-12 09:42:05 +00001958 StructuralEquivalenceContext Ctx(
1959 Importer.getFromContext(), Importer.getToContext(),
1960 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00001961 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001962}
1963
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001964bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1965 FunctionTemplateDecl *To) {
1966 StructuralEquivalenceContext Ctx(
1967 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001968 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1969 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00001970 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001971}
1972
Balazs Keric7797c42018-07-11 09:37:24 +00001973bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
1974 StructuralEquivalenceContext Ctx(
1975 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001976 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1977 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00001978 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00001979}
1980
Douglas Gregor91155082012-11-14 22:29:20 +00001981bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001982 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00001983 const llvm::APSInt &FromVal = FromEC->getInitVal();
1984 const llvm::APSInt &ToVal = ToEC->getInitVal();
1985
1986 return FromVal.isSigned() == ToVal.isSigned() &&
1987 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1988 FromVal == ToVal;
1989}
1990
1991bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001992 ClassTemplateDecl *To) {
1993 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1994 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001995 Importer.getNonEquivalentDecls(),
1996 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00001997 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00001998}
1999
Larisse Voufo39a1e502013-08-06 01:03:05 +00002000bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2001 VarTemplateDecl *To) {
2002 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2003 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002004 Importer.getNonEquivalentDecls(),
2005 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002006 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002007}
2008
Balazs Keri3b30d652018-10-19 13:32:20 +00002009ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002010 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002011 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00002012 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +00002013}
2014
Balazs Keri3b30d652018-10-19 13:32:20 +00002015ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) {
2016 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2017 << D->getDeclKindName();
2018 return make_error<ImportError>(ImportError::UnsupportedConstruct);
2019}
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002020
Balazs Keri3b30d652018-10-19 13:32:20 +00002021ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
2022 // Import the context of this declaration.
2023 DeclContext *DC, *LexicalDC;
2024 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
2025 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002026
2027 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002028 ExpectedSLoc LocOrErr = import(D->getLocation());
2029 if (!LocOrErr)
2030 return LocOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002031
Gabor Marton26f72a92018-07-12 09:42:05 +00002032 EmptyDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002033 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002034 return ToD;
2035
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002036 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002037 LexicalDC->addDeclInternal(ToD);
2038 return ToD;
2039}
2040
Balazs Keri3b30d652018-10-19 13:32:20 +00002041ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002042 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00002043 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00002044
Gabor Marton26f72a92018-07-12 09:42:05 +00002045 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002046
Sean Callanan65198272011-11-17 23:20:56 +00002047 return ToD;
2048}
2049
Balazs Keri3b30d652018-10-19 13:32:20 +00002050ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2051 ExpectedSLoc LocOrErr = import(D->getLocation());
2052 if (!LocOrErr)
2053 return LocOrErr.takeError();
2054 auto ColonLocOrErr = import(D->getColonLoc());
2055 if (!ColonLocOrErr)
2056 return ColonLocOrErr.takeError();
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002057
2058 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002059 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2060 if (!DCOrErr)
2061 return DCOrErr.takeError();
2062 DeclContext *DC = *DCOrErr;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002063
Gabor Marton26f72a92018-07-12 09:42:05 +00002064 AccessSpecDecl *ToD;
2065 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002066 DC, *LocOrErr, *ColonLocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002067 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002068
2069 // Lexical DeclContext and Semantic DeclContext
2070 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00002071 ToD->setLexicalDeclContext(DC);
2072 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002073
Gabor Marton26f72a92018-07-12 09:42:05 +00002074 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002075}
2076
Balazs Keri3b30d652018-10-19 13:32:20 +00002077ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2078 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2079 if (!DCOrErr)
2080 return DCOrErr.takeError();
2081 DeclContext *DC = *DCOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002082 DeclContext *LexicalDC = DC;
2083
Balazs Keri3b30d652018-10-19 13:32:20 +00002084 SourceLocation ToLocation, ToRParenLoc;
2085 Expr *ToAssertExpr;
2086 StringLiteral *ToMessage;
2087 if (auto Imp = importSeq(
2088 D->getLocation(), D->getAssertExpr(), D->getMessage(), D->getRParenLoc()))
2089 std::tie(ToLocation, ToAssertExpr, ToMessage, ToRParenLoc) = *Imp;
2090 else
2091 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002092
Gabor Marton26f72a92018-07-12 09:42:05 +00002093 StaticAssertDecl *ToD;
2094 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002095 ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage,
2096 ToRParenLoc, D->isFailed()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002097 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002098
2099 ToD->setLexicalDeclContext(LexicalDC);
2100 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002101 return ToD;
2102}
2103
Balazs Keri3b30d652018-10-19 13:32:20 +00002104ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002105 // Import the major distinguishing characteristics of this namespace.
2106 DeclContext *DC, *LexicalDC;
2107 DeclarationName Name;
2108 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002109 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002110 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2111 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002112 if (ToD)
2113 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002114
2115 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002116 if (!Name) {
2117 // This is an anonymous namespace. Adopt an existing anonymous
2118 // namespace if we can.
2119 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002120 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002121 MergeWithNamespace = TU->getAnonymousNamespace();
2122 else
2123 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2124 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002125 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002126 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002127 for (auto *FoundDecl : FoundDecls) {
2128 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002129 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002130
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002131 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002132 MergeWithNamespace = FoundNS;
2133 ConflictingDecls.clear();
2134 break;
2135 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002136
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002137 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002138 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002139
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002140 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002141 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +00002142 ConflictingDecls.data(),
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002143 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002144 if (!Name)
2145 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002146 }
2147 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002148
Balazs Keri3b30d652018-10-19 13:32:20 +00002149 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2150 if (!BeginLocOrErr)
2151 return BeginLocOrErr.takeError();
2152
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002153 // Create the "to" namespace, if needed.
2154 NamespaceDecl *ToNamespace = MergeWithNamespace;
2155 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002156 if (GetImportedOrCreateDecl(
2157 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002158 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002159 /*PrevDecl=*/nullptr))
2160 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002161 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002162 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002163
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002164 // If this is an anonymous namespace, register it as the anonymous
2165 // namespace within its context.
2166 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002167 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002168 TU->setAnonymousNamespace(ToNamespace);
2169 else
2170 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2171 }
2172 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002173 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002174
Balazs Keri3b30d652018-10-19 13:32:20 +00002175 if (Error Err = ImportDeclContext(D))
2176 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00002177
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002178 return ToNamespace;
2179}
2180
Balazs Keri3b30d652018-10-19 13:32:20 +00002181ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002182 // Import the major distinguishing characteristics of this namespace.
2183 DeclContext *DC, *LexicalDC;
2184 DeclarationName Name;
2185 SourceLocation Loc;
2186 NamedDecl *LookupD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002187 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
2188 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002189 if (LookupD)
2190 return LookupD;
2191
2192 // NOTE: No conflict resolution is done for namespace aliases now.
2193
Balazs Keri3b30d652018-10-19 13:32:20 +00002194 SourceLocation ToNamespaceLoc, ToAliasLoc, ToTargetNameLoc;
2195 NestedNameSpecifierLoc ToQualifierLoc;
2196 NamespaceDecl *ToNamespace;
2197 if (auto Imp = importSeq(
2198 D->getNamespaceLoc(), D->getAliasLoc(), D->getQualifierLoc(),
2199 D->getTargetNameLoc(), D->getNamespace()))
2200 std::tie(
2201 ToNamespaceLoc, ToAliasLoc, ToQualifierLoc, ToTargetNameLoc,
2202 ToNamespace) = *Imp;
2203 else
2204 return Imp.takeError();
2205 IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002206
Gabor Marton26f72a92018-07-12 09:42:05 +00002207 NamespaceAliasDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002208 if (GetImportedOrCreateDecl(
2209 ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc,
2210 ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace))
Gabor Marton26f72a92018-07-12 09:42:05 +00002211 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002212
2213 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002214 LexicalDC->addDeclInternal(ToD);
2215
2216 return ToD;
2217}
2218
Balazs Keri3b30d652018-10-19 13:32:20 +00002219ExpectedDecl
2220ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002221 // Import the major distinguishing characteristics of this typedef.
2222 DeclContext *DC, *LexicalDC;
2223 DeclarationName Name;
2224 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002225 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002226 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2227 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002228 if (ToD)
2229 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002230
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002231 // If this typedef is not in block scope, determine whether we've
2232 // seen a typedef with the same name (that we can merge with) or any
2233 // other entity by that name (which name lookup could conflict with).
2234 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002235 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002236 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002237 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002238 for (auto *FoundDecl : FoundDecls) {
2239 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002240 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002241 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Gabor Martonb93baf62018-11-27 09:51:36 +00002242 QualType FromUT = D->getUnderlyingType();
2243 QualType FoundUT = FoundTypedef->getUnderlyingType();
2244 if (Importer.IsStructurallyEquivalent(FromUT, FoundUT)) {
2245 // If the "From" context has a complete underlying type but we
2246 // already have a complete underlying type then return with that.
2247 if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType())
Balazs Keri3b30d652018-10-19 13:32:20 +00002248 return Importer.MapImported(D, FoundTypedef);
Gabor Martonb93baf62018-11-27 09:51:36 +00002249 }
2250 // FIXME Handle redecl chain.
2251 break;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002252 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002253
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002254 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002255 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002256
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002257 if (!ConflictingDecls.empty()) {
2258 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002259 ConflictingDecls.data(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002260 ConflictingDecls.size());
2261 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002262 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002263 }
2264 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002265
Balazs Keri3b30d652018-10-19 13:32:20 +00002266 QualType ToUnderlyingType;
2267 TypeSourceInfo *ToTypeSourceInfo;
2268 SourceLocation ToBeginLoc;
2269 if (auto Imp = importSeq(
2270 D->getUnderlyingType(), D->getTypeSourceInfo(), D->getBeginLoc()))
2271 std::tie(ToUnderlyingType, ToTypeSourceInfo, ToBeginLoc) = *Imp;
2272 else
2273 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002274
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002275 // Create the new typedef node.
Balazs Keri3b30d652018-10-19 13:32:20 +00002276 // FIXME: ToUnderlyingType is not used.
Richard Smithdda56e42011-04-15 14:24:37 +00002277 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00002278 if (IsAlias) {
2279 if (GetImportedOrCreateDecl<TypeAliasDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002280 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2281 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002282 return ToTypedef;
2283 } else if (GetImportedOrCreateDecl<TypedefDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002284 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2285 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002286 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002287
Douglas Gregordd483172010-02-22 17:42:47 +00002288 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002289 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002290
2291 // Templated declarations should not appear in DeclContext.
2292 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
2293 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
2294 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002295
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002296 return ToTypedef;
2297}
2298
Balazs Keri3b30d652018-10-19 13:32:20 +00002299ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002300 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2301}
2302
Balazs Keri3b30d652018-10-19 13:32:20 +00002303ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002304 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2305}
2306
Balazs Keri3b30d652018-10-19 13:32:20 +00002307ExpectedDecl
2308ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
Gabor Horvath7a91c082017-11-14 11:30:38 +00002309 // Import the major distinguishing characteristics of this typedef.
2310 DeclContext *DC, *LexicalDC;
2311 DeclarationName Name;
2312 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002313 NamedDecl *FoundD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002314 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
2315 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002316 if (FoundD)
2317 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002318
2319 // If this typedef is not in block scope, determine whether we've
2320 // seen a typedef with the same name (that we can merge with) or any
2321 // other entity by that name (which name lookup could conflict with).
2322 if (!DC->isFunctionOrMethod()) {
2323 SmallVector<NamedDecl *, 4> ConflictingDecls;
2324 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002325 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002326 for (auto *FoundDecl : FoundDecls) {
2327 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002328 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002329 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002330 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002331 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002332 }
2333
2334 if (!ConflictingDecls.empty()) {
2335 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2336 ConflictingDecls.data(),
2337 ConflictingDecls.size());
2338 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002339 return make_error<ImportError>(ImportError::NameConflict);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002340 }
2341 }
2342
Balazs Keri3b30d652018-10-19 13:32:20 +00002343 TemplateParameterList *ToTemplateParameters;
2344 TypeAliasDecl *ToTemplatedDecl;
2345 if (auto Imp = importSeq(D->getTemplateParameters(), D->getTemplatedDecl()))
2346 std::tie(ToTemplateParameters, ToTemplatedDecl) = *Imp;
2347 else
2348 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002349
Gabor Marton26f72a92018-07-12 09:42:05 +00002350 TypeAliasTemplateDecl *ToAlias;
2351 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002352 Name, ToTemplateParameters, ToTemplatedDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002353 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002354
Balazs Keri3b30d652018-10-19 13:32:20 +00002355 ToTemplatedDecl->setDescribedAliasTemplate(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002356
Gabor Horvath7a91c082017-11-14 11:30:38 +00002357 ToAlias->setAccess(D->getAccess());
2358 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002359 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002360 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002361}
2362
Balazs Keri3b30d652018-10-19 13:32:20 +00002363ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002364 // Import the major distinguishing characteristics of this label.
2365 DeclContext *DC, *LexicalDC;
2366 DeclarationName Name;
2367 SourceLocation Loc;
2368 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002369 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2370 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002371 if (ToD)
2372 return ToD;
2373
2374 assert(LexicalDC->isFunctionOrMethod());
2375
Gabor Marton26f72a92018-07-12 09:42:05 +00002376 LabelDecl *ToLabel;
Balazs Keri3b30d652018-10-19 13:32:20 +00002377 if (D->isGnuLocal()) {
2378 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2379 if (!BeginLocOrErr)
2380 return BeginLocOrErr.takeError();
2381 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2382 Name.getAsIdentifierInfo(), *BeginLocOrErr))
2383 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002384
Balazs Keri3b30d652018-10-19 13:32:20 +00002385 } else {
2386 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2387 Name.getAsIdentifierInfo()))
2388 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002389
Balazs Keri3b30d652018-10-19 13:32:20 +00002390 }
2391
2392 Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt());
2393 if (!ToStmtOrErr)
2394 return ToStmtOrErr.takeError();
2395
2396 ToLabel->setStmt(*ToStmtOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002397 ToLabel->setLexicalDeclContext(LexicalDC);
2398 LexicalDC->addDeclInternal(ToLabel);
2399 return ToLabel;
2400}
2401
Balazs Keri3b30d652018-10-19 13:32:20 +00002402ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002403 // Import the major distinguishing characteristics of this enum.
2404 DeclContext *DC, *LexicalDC;
2405 DeclarationName Name;
2406 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002407 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002408 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2409 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002410 if (ToD)
2411 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002412
Douglas Gregor98c10182010-02-12 22:17:39 +00002413 // Figure out what enum name we're looking for.
2414 unsigned IDNS = Decl::IDNS_Tag;
2415 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002416 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002417 if (Error Err = importInto(
2418 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2419 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002420 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002421 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002422 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002423
Douglas Gregor98c10182010-02-12 22:17:39 +00002424 // We may already have an enum of the same name; try to find and match it.
2425 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002426 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002427 auto FoundDecls =
2428 Importer.findDeclsInToCtx(DC, SearchName);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002429 for (auto *FoundDecl : FoundDecls) {
2430 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002431 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002432
Balazs Keri3b30d652018-10-19 13:32:20 +00002433 if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002434 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Balazs Keri3b30d652018-10-19 13:32:20 +00002435 FoundDecl = Tag->getDecl();
Douglas Gregor98c10182010-02-12 22:17:39 +00002436 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002437
Balazs Keri3b30d652018-10-19 13:32:20 +00002438 if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002439 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002440 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002441 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002442
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002443 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002444 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002445
Douglas Gregor98c10182010-02-12 22:17:39 +00002446 if (!ConflictingDecls.empty()) {
Shafik Yaghmourd4263122019-04-08 20:50:21 +00002447 Name = Importer.HandleNameConflict(SearchName, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002448 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002449 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002450 if (!Name)
2451 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002452 }
2453 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002454
Balazs Keri3b30d652018-10-19 13:32:20 +00002455 SourceLocation ToBeginLoc;
2456 NestedNameSpecifierLoc ToQualifierLoc;
2457 QualType ToIntegerType;
2458 if (auto Imp = importSeq(
2459 D->getBeginLoc(), D->getQualifierLoc(), D->getIntegerType()))
2460 std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType) = *Imp;
2461 else
2462 return Imp.takeError();
2463
Douglas Gregor98c10182010-02-12 22:17:39 +00002464 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002465 EnumDecl *D2;
2466 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002467 D2, D, Importer.getToContext(), DC, ToBeginLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002468 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2469 D->isScopedUsingClassTag(), D->isFixed()))
2470 return D2;
2471
Balazs Keri3b30d652018-10-19 13:32:20 +00002472 D2->setQualifierInfo(ToQualifierLoc);
2473 D2->setIntegerType(ToIntegerType);
Douglas Gregordd483172010-02-22 17:42:47 +00002474 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002475 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002476 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002477
Douglas Gregor98c10182010-02-12 22:17:39 +00002478 // Import the definition
Balazs Keri3b30d652018-10-19 13:32:20 +00002479 if (D->isCompleteDefinition())
2480 if (Error Err = ImportDefinition(D, D2))
2481 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002482
Douglas Gregor3996e242010-02-15 22:01:00 +00002483 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002484}
2485
Balazs Keri3b30d652018-10-19 13:32:20 +00002486ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002487 bool IsFriendTemplate = false;
2488 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2489 IsFriendTemplate =
2490 DCXX->getDescribedClassTemplate() &&
2491 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
2492 Decl::FOK_None;
2493 }
2494
Douglas Gregor5c73e912010-02-11 00:48:18 +00002495 // Import the major distinguishing characteristics of this record.
2496 DeclContext *DC, *LexicalDC;
2497 DeclarationName Name;
2498 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002499 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002500 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2501 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002502 if (ToD)
2503 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002504
Douglas Gregor5c73e912010-02-11 00:48:18 +00002505 // Figure out what structure name we're looking for.
2506 unsigned IDNS = Decl::IDNS_Tag;
2507 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002508 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002509 if (Error Err = importInto(
2510 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2511 return std::move(Err);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002512 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002513 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Gabor Marton7df342a2018-12-17 12:42:12 +00002514 IDNS |= Decl::IDNS_Ordinary | Decl::IDNS_TagFriend;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002515
2516 // We may already have a record of the same name; try to find and match it.
Sean Callanan9092d472017-05-13 00:46:33 +00002517 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002518 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002519 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002520 auto FoundDecls =
2521 Importer.findDeclsInToCtx(DC, SearchName);
Sean Callanan9092d472017-05-13 00:46:33 +00002522 if (!FoundDecls.empty()) {
Gabor Marton41e38922019-03-05 11:23:24 +00002523 // We're going to have to compare D against potentially conflicting Decls,
2524 // so complete it.
Sean Callanan9092d472017-05-13 00:46:33 +00002525 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2526 D->getASTContext().getExternalSource()->CompleteType(D);
2527 }
2528
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002529 for (auto *FoundDecl : FoundDecls) {
2530 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002531 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002532
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002533 Decl *Found = FoundDecl;
2534 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2535 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002536 Found = Tag->getDecl();
2537 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002538
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002539 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Gabor Marton7df342a2018-12-17 12:42:12 +00002540 // Do not emit false positive diagnostic in case of unnamed
2541 // struct/union and in case of anonymous structs. Would be false
2542 // because there may be several anonymous/unnamed structs in a class.
2543 // E.g. these are both valid:
2544 // struct A { // unnamed structs
2545 // struct { struct A *next; } entry0;
2546 // struct { struct A *next; } entry1;
2547 // };
2548 // struct X { struct { int a; }; struct { int b; }; }; // anon structs
2549 if (!SearchName)
Gabor Marton0bebf952018-07-05 09:51:13 +00002550 if (!IsStructuralMatch(D, FoundRecord, false))
2551 continue;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002552
Gabor Marton7df342a2018-12-17 12:42:12 +00002553 if (IsStructuralMatch(D, FoundRecord)) {
2554 RecordDecl *FoundDef = FoundRecord->getDefinition();
2555 if (D->isThisDeclarationADefinition() && FoundDef) {
Balazs Keri1d20cc22018-07-16 12:16:39 +00002556 // FIXME: Structural equivalence check should check for same
2557 // user-defined methods.
2558 Importer.MapImported(D, FoundDef);
2559 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2560 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2561 assert(FoundCXX && "Record type mismatch");
2562
Gabor Marton7df342a2018-12-17 12:42:12 +00002563 if (!Importer.isMinimalImport())
Balazs Keri1d20cc22018-07-16 12:16:39 +00002564 // FoundDef may not have every implicit method that D has
2565 // because implicit methods are created only if they are used.
Balazs Keri3b30d652018-10-19 13:32:20 +00002566 if (Error Err = ImportImplicitMethods(DCXX, FoundCXX))
2567 return std::move(Err);
Balazs Keri1d20cc22018-07-16 12:16:39 +00002568 }
Douglas Gregor25791052010-02-12 00:09:27 +00002569 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002570 PrevDecl = FoundRecord->getMostRecentDecl();
2571 break;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002572 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002573 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002574
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002575 ConflictingDecls.push_back(FoundDecl);
Gabor Marton7df342a2018-12-17 12:42:12 +00002576 } // for
Fangrui Song6907ce22018-07-30 19:24:48 +00002577
Douglas Gregordd6006f2012-07-17 21:16:27 +00002578 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002579 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002580 ConflictingDecls.data(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002581 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002582 if (!Name)
2583 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002584 }
2585 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002586
Balazs Keri3b30d652018-10-19 13:32:20 +00002587 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2588 if (!BeginLocOrErr)
2589 return BeginLocOrErr.takeError();
2590
Douglas Gregor5c73e912010-02-11 00:48:18 +00002591 // Create the record declaration.
Gabor Marton7df342a2018-12-17 12:42:12 +00002592 RecordDecl *D2 = nullptr;
2593 CXXRecordDecl *D2CXX = nullptr;
2594 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2595 if (DCXX->isLambda()) {
2596 auto TInfoOrErr = import(DCXX->getLambdaTypeInfo());
2597 if (!TInfoOrErr)
2598 return TInfoOrErr.takeError();
2599 if (GetImportedOrCreateSpecialDecl(
2600 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
2601 DC, *TInfoOrErr, Loc, DCXX->isDependentLambda(),
2602 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2603 return D2CXX;
2604 ExpectedDecl CDeclOrErr = import(DCXX->getLambdaContextDecl());
2605 if (!CDeclOrErr)
2606 return CDeclOrErr.takeError();
2607 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), *CDeclOrErr);
2608 } else if (DCXX->isInjectedClassName()) {
2609 // We have to be careful to do a similar dance to the one in
2610 // Sema::ActOnStartCXXMemberDeclarations
2611 const bool DelayTypeCreation = true;
2612 if (GetImportedOrCreateDecl(
2613 D2CXX, D, Importer.getToContext(), D->getTagKind(), DC,
2614 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
2615 cast_or_null<CXXRecordDecl>(PrevDecl), DelayTypeCreation))
2616 return D2CXX;
2617 Importer.getToContext().getTypeDeclType(
2618 D2CXX, dyn_cast<CXXRecordDecl>(DC));
2619 } else {
2620 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2621 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2622 Name.getAsIdentifierInfo(),
2623 cast_or_null<CXXRecordDecl>(PrevDecl)))
2624 return D2CXX;
2625 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002626
Gabor Marton7df342a2018-12-17 12:42:12 +00002627 D2 = D2CXX;
2628 D2->setAccess(D->getAccess());
2629 D2->setLexicalDeclContext(LexicalDC);
2630 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
2631 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002632
Gabor Marton7df342a2018-12-17 12:42:12 +00002633 if (LexicalDC != DC && D->isInIdentifierNamespace(Decl::IDNS_TagFriend))
2634 DC->makeDeclVisibleInContext(D2);
2635
2636 if (ClassTemplateDecl *FromDescribed =
2637 DCXX->getDescribedClassTemplate()) {
2638 ClassTemplateDecl *ToDescribed;
2639 if (Error Err = importInto(ToDescribed, FromDescribed))
2640 return std::move(Err);
2641 D2CXX->setDescribedClassTemplate(ToDescribed);
2642 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
2643 // In a record describing a template the type should be an
2644 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2645 // previously set type to the correct value here (ToDescribed is not
2646 // available at record create).
2647 // FIXME: The previous type is cleared but not removed from
2648 // ASTContext's internal storage.
2649 CXXRecordDecl *Injected = nullptr;
2650 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2651 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2652 if (Record && Record->isInjectedClassName()) {
2653 Injected = Record;
2654 break;
Gabor Marton5915777e2018-06-26 13:44:24 +00002655 }
2656 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002657 // Create an injected type for the whole redecl chain.
2658 SmallVector<Decl *, 2> Redecls =
2659 getCanonicalForwardRedeclChain(D2CXX);
2660 for (auto *R : Redecls) {
2661 auto *RI = cast<CXXRecordDecl>(R);
2662 RI->setTypeForDecl(nullptr);
2663 // Below we create a new injected type and assign that to the
2664 // canonical decl, subsequent declarations in the chain will reuse
2665 // that type.
2666 Importer.getToContext().getInjectedClassNameType(
2667 RI, ToDescribed->getInjectedClassNameSpecialization());
2668 }
2669 // Set the new type for the previous injected decl too.
2670 if (Injected) {
2671 Injected->setTypeForDecl(nullptr);
2672 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2673 }
2674 }
2675 } else if (MemberSpecializationInfo *MemberInfo =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002676 DCXX->getMemberSpecializationInfo()) {
2677 TemplateSpecializationKind SK =
2678 MemberInfo->getTemplateSpecializationKind();
2679 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Balazs Keri3b30d652018-10-19 13:32:20 +00002680
2681 if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst))
2682 D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK);
2683 else
2684 return ToInstOrErr.takeError();
2685
2686 if (ExpectedSLoc POIOrErr =
2687 import(MemberInfo->getPointOfInstantiation()))
2688 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2689 *POIOrErr);
2690 else
2691 return POIOrErr.takeError();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002692 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002693
Gabor Marton7df342a2018-12-17 12:42:12 +00002694 } else {
2695 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
2696 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2697 Name.getAsIdentifierInfo(), PrevDecl))
2698 return D2;
2699 D2->setLexicalDeclContext(LexicalDC);
2700 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002701 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002702
Gabor Marton7df342a2018-12-17 12:42:12 +00002703 if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
2704 D2->setQualifierInfo(*QualifierLocOrErr);
2705 else
2706 return QualifierLocOrErr.takeError();
2707
2708 if (D->isAnonymousStructOrUnion())
2709 D2->setAnonymousStructOrUnion(true);
Douglas Gregor25791052010-02-12 00:09:27 +00002710
Balazs Keri3b30d652018-10-19 13:32:20 +00002711 if (D->isCompleteDefinition())
2712 if (Error Err = ImportDefinition(D, D2, IDK_Default))
2713 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00002714
Douglas Gregor3996e242010-02-15 22:01:00 +00002715 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002716}
2717
Balazs Keri3b30d652018-10-19 13:32:20 +00002718ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002719 // Import the major distinguishing characteristics of this enumerator.
2720 DeclContext *DC, *LexicalDC;
2721 DeclarationName Name;
2722 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002723 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002724 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2725 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002726 if (ToD)
2727 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002728
Fangrui Song6907ce22018-07-30 19:24:48 +00002729 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002730 // in the same context.
2731 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002732 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002733 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002734 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002735 for (auto *FoundDecl : FoundDecls) {
2736 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002737 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002738
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002739 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002740 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002741 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002742 }
2743
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002744 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002745 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002746
Douglas Gregor98c10182010-02-12 22:17:39 +00002747 if (!ConflictingDecls.empty()) {
2748 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002749 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002750 ConflictingDecls.size());
2751 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002752 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002753 }
2754 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002755
Balazs Keri3b30d652018-10-19 13:32:20 +00002756 ExpectedType TypeOrErr = import(D->getType());
2757 if (!TypeOrErr)
2758 return TypeOrErr.takeError();
2759
2760 ExpectedExpr InitOrErr = import(D->getInitExpr());
2761 if (!InitOrErr)
2762 return InitOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002763
Gabor Marton26f72a92018-07-12 09:42:05 +00002764 EnumConstantDecl *ToEnumerator;
2765 if (GetImportedOrCreateDecl(
2766 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002767 Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002768 return ToEnumerator;
2769
Douglas Gregordd483172010-02-22 17:42:47 +00002770 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002771 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002772 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002773 return ToEnumerator;
2774}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002775
Balazs Keri3b30d652018-10-19 13:32:20 +00002776Error ASTNodeImporter::ImportTemplateInformation(
2777 FunctionDecl *FromFD, FunctionDecl *ToFD) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002778 switch (FromFD->getTemplatedKind()) {
2779 case FunctionDecl::TK_NonTemplate:
2780 case FunctionDecl::TK_FunctionTemplate:
Balazs Keri3b30d652018-10-19 13:32:20 +00002781 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002782
2783 case FunctionDecl::TK_MemberSpecialization: {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002784 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00002785
2786 if (Expected<FunctionDecl *> InstFDOrErr =
2787 import(FromFD->getInstantiatedFromMemberFunction()))
2788 ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK);
2789 else
2790 return InstFDOrErr.takeError();
2791
2792 if (ExpectedSLoc POIOrErr = import(
2793 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()))
2794 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
2795 else
2796 return POIOrErr.takeError();
2797
2798 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002799 }
2800
2801 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Balazs Keri3b30d652018-10-19 13:32:20 +00002802 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002803 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002804 if (!FunctionAndArgsOrErr)
2805 return FunctionAndArgsOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002806
2807 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Balazs Keri3b30d652018-10-19 13:32:20 +00002808 Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002809
Gabor Marton5254e642018-06-27 13:32:50 +00002810 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002811 TemplateArgumentListInfo ToTAInfo;
2812 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002813 if (FromTAArgsAsWritten)
Balazs Keri3b30d652018-10-19 13:32:20 +00002814 if (Error Err = ImportTemplateArgumentListInfo(
2815 *FromTAArgsAsWritten, ToTAInfo))
2816 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002817
Balazs Keri3b30d652018-10-19 13:32:20 +00002818 ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation());
2819 if (!POIOrErr)
2820 return POIOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002821
Gabor Marton5254e642018-06-27 13:32:50 +00002822 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002823 ToFD->setFunctionTemplateSpecialization(
Balazs Keri3b30d652018-10-19 13:32:20 +00002824 std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr,
2825 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr);
2826 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002827 }
2828
2829 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2830 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2831 UnresolvedSet<8> TemplDecls;
2832 unsigned NumTemplates = FromInfo->getNumTemplates();
2833 for (unsigned I = 0; I < NumTemplates; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002834 if (Expected<FunctionTemplateDecl *> ToFTDOrErr =
2835 import(FromInfo->getTemplate(I)))
2836 TemplDecls.addDecl(*ToFTDOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002837 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002838 return ToFTDOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002839 }
2840
2841 // Import TemplateArgumentListInfo.
2842 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00002843 if (Error Err = ImportTemplateArgumentListInfo(
2844 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2845 llvm::makeArrayRef(
2846 FromInfo->getTemplateArgs(), FromInfo->getNumTemplateArgs()),
2847 ToTAInfo))
2848 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002849
2850 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2851 TemplDecls, ToTAInfo);
Balazs Keri3b30d652018-10-19 13:32:20 +00002852 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002853 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002854 }
Sam McCallfdc32072018-01-26 12:06:44 +00002855 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002856}
2857
Balazs Keri3b30d652018-10-19 13:32:20 +00002858Expected<FunctionDecl *>
Gabor Marton5254e642018-06-27 13:32:50 +00002859ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002860 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002861 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002862 if (!FunctionAndArgsOrErr)
2863 return FunctionAndArgsOrErr.takeError();
Gabor Marton5254e642018-06-27 13:32:50 +00002864
Balazs Keri3b30d652018-10-19 13:32:20 +00002865 FunctionTemplateDecl *Template;
2866 TemplateArgsTy ToTemplArgs;
2867 std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr;
Gabor Marton5254e642018-06-27 13:32:50 +00002868 void *InsertPos = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00002869 auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos);
Gabor Marton5254e642018-06-27 13:32:50 +00002870 return FoundSpec;
2871}
2872
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002873Error ASTNodeImporter::ImportFunctionDeclBody(FunctionDecl *FromFD,
2874 FunctionDecl *ToFD) {
2875 if (Stmt *FromBody = FromFD->getBody()) {
2876 if (ExpectedStmt ToBodyOrErr = import(FromBody))
2877 ToFD->setBody(*ToBodyOrErr);
2878 else
2879 return ToBodyOrErr.takeError();
2880 }
2881 return Error::success();
2882}
2883
Gabor Marton458d1452019-02-14 13:07:03 +00002884template <typename T>
2885bool ASTNodeImporter::hasSameVisibilityContext(T *Found, T *From) {
2886 if (From->hasExternalFormalLinkage())
2887 return Found->hasExternalFormalLinkage();
2888 if (Importer.GetFromTU(Found) != From->getTranslationUnitDecl())
2889 return false;
2890 if (From->isInAnonymousNamespace())
2891 return Found->isInAnonymousNamespace();
2892 else
2893 return !Found->isInAnonymousNamespace() &&
2894 !Found->hasExternalFormalLinkage();
2895}
2896
Balazs Keri3b30d652018-10-19 13:32:20 +00002897ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00002898
Balazs Keri3b30d652018-10-19 13:32:20 +00002899 SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D);
Gabor Marton5254e642018-06-27 13:32:50 +00002900 auto RedeclIt = Redecls.begin();
2901 // Import the first part of the decl chain. I.e. import all previous
2902 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00002903 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
2904 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
2905 if (!ToRedeclOrErr)
2906 return ToRedeclOrErr.takeError();
2907 }
Gabor Marton5254e642018-06-27 13:32:50 +00002908 assert(*RedeclIt == D);
2909
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002910 // Import the major distinguishing characteristics of this function.
2911 DeclContext *DC, *LexicalDC;
2912 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002913 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002914 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002915 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2916 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002917 if (ToD)
2918 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002919
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002920 FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00002921 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002922
Gabor Marton5254e642018-06-27 13:32:50 +00002923 // If this is a function template specialization, then try to find the same
Gabor Marton54058b52018-12-17 13:53:12 +00002924 // existing specialization in the "to" context. The lookup below will not
2925 // find any specialization, but would find the primary template; thus, we
2926 // have to skip normal lookup in case of specializations.
Gabor Marton5254e642018-06-27 13:32:50 +00002927 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
2928 if (D->getTemplatedKind() ==
2929 FunctionDecl::TK_FunctionTemplateSpecialization) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002930 auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D);
2931 if (!FoundFunctionOrErr)
2932 return FoundFunctionOrErr.takeError();
2933 if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) {
Gabor Martondd59d272019-03-19 14:04:50 +00002934 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
2935 return Def;
Gabor Marton5254e642018-06-27 13:32:50 +00002936 FoundByLookup = FoundFunction;
2937 }
2938 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002939 // Try to find a function in our own ("to") context with the same name, same
2940 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00002941 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002942 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00002943 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00002944 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002945 for (auto *FoundDecl : FoundDecls) {
2946 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002947 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002948
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002949 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00002950 if (!hasSameVisibilityContext(FoundFunction, D))
2951 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002952
Gabor Marton458d1452019-02-14 13:07:03 +00002953 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Martondd59d272019-03-19 14:04:50 +00002954 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
2955 return Def;
Gabor Marton458d1452019-02-14 13:07:03 +00002956 FoundByLookup = FoundFunction;
2957 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002958 }
Gabor Marton458d1452019-02-14 13:07:03 +00002959 // FIXME: Check for overloading more carefully, e.g., by boosting
2960 // Sema::IsOverload out to the AST library.
2961
2962 // Function overloading is okay in C++.
2963 if (Importer.getToContext().getLangOpts().CPlusPlus)
2964 continue;
2965
2966 // Complain about inconsistent function types.
Gabor Marton410f32c2019-04-01 15:29:55 +00002967 Importer.ToDiag(Loc, diag::warn_odr_function_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00002968 << Name << D->getType() << FoundFunction->getType();
2969 Importer.ToDiag(FoundFunction->getLocation(), diag::note_odr_value_here)
2970 << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002971 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002972
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002973 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002974 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002975
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002976 if (!ConflictingDecls.empty()) {
2977 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002978 ConflictingDecls.data(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002979 ConflictingDecls.size());
2980 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002981 return make_error<ImportError>(ImportError::NameConflict);
Fangrui Song6907ce22018-07-30 19:24:48 +00002982 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002983 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002984
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002985 // We do not allow more than one in-class declaration of a function. This is
2986 // because AST clients like VTableBuilder asserts on this. VTableBuilder
2987 // assumes there is only one in-class declaration. Building a redecl
2988 // chain would result in more than one in-class declaration for
2989 // overrides (even if they are part of the same redecl chain inside the
2990 // derived class.)
2991 if (FoundByLookup) {
Mikael Holmenc1c97aa2019-01-29 06:53:31 +00002992 if (isa<CXXMethodDecl>(FoundByLookup)) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002993 if (D->getLexicalDeclContext() == D->getDeclContext()) {
2994 if (!D->doesThisDeclarationHaveABody())
2995 return Importer.MapImported(D, FoundByLookup);
2996 else {
2997 // Let's continue and build up the redecl chain in this case.
2998 // FIXME Merge the functions into one decl.
2999 }
3000 }
3001 }
3002 }
3003
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003004 DeclarationNameInfo NameInfo(Name, Loc);
3005 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00003006 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
3007 return std::move(Err);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003008
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003009 QualType FromTy = D->getType();
3010 bool usedDifferentExceptionSpec = false;
3011
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003012 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003013 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3014 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3015 // FunctionDecl that we are importing the FunctionProtoType for.
3016 // To avoid an infinite recursion when importing, create the FunctionDecl
3017 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00003018 if (FromEPI.ExceptionSpec.SourceDecl ||
3019 FromEPI.ExceptionSpec.SourceTemplate ||
3020 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003021 FunctionProtoType::ExtProtoInfo DefaultEPI;
3022 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003023 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003024 usedDifferentExceptionSpec = true;
3025 }
3026 }
3027
Balazs Keri3b30d652018-10-19 13:32:20 +00003028 QualType T;
3029 TypeSourceInfo *TInfo;
3030 SourceLocation ToInnerLocStart, ToEndLoc;
3031 NestedNameSpecifierLoc ToQualifierLoc;
3032 if (auto Imp = importSeq(
3033 FromTy, D->getTypeSourceInfo(), D->getInnerLocStart(),
3034 D->getQualifierLoc(), D->getEndLoc()))
3035 std::tie(T, TInfo, ToInnerLocStart, ToQualifierLoc, ToEndLoc) = *Imp;
3036 else
3037 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003038
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003039 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003040 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003041 for (auto P : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003042 if (Expected<ParmVarDecl *> ToPOrErr = import(P))
3043 Parameters.push_back(*ToPOrErr);
3044 else
3045 return ToPOrErr.takeError();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003046 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003047
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003048 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00003049 FunctionDecl *ToFunction = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003050 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003051 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003052 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3053 ToInnerLocStart, NameInfo, T, TInfo,
3054 FromConstructor->isExplicit(),
3055 D->isInlineSpecified(), D->isImplicit(), D->isConstexpr()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003056 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003057 } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(D)) {
3058
3059 auto Imp =
3060 importSeq(const_cast<FunctionDecl *>(FromDtor->getOperatorDelete()),
3061 FromDtor->getOperatorDeleteThisArg());
3062
3063 if (!Imp)
3064 return Imp.takeError();
3065
3066 FunctionDecl *ToOperatorDelete;
3067 Expr *ToThisArg;
3068 std::tie(ToOperatorDelete, ToThisArg) = *Imp;
3069
Gabor Marton26f72a92018-07-12 09:42:05 +00003070 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003071 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3072 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
3073 D->isImplicit()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003074 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003075
3076 CXXDestructorDecl *ToDtor = cast<CXXDestructorDecl>(ToFunction);
3077
3078 ToDtor->setOperatorDelete(ToOperatorDelete, ToThisArg);
Gabor Marton26f72a92018-07-12 09:42:05 +00003079 } else if (CXXConversionDecl *FromConversion =
3080 dyn_cast<CXXConversionDecl>(D)) {
3081 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3082 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003083 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003084 FromConversion->isExplicit(), D->isConstexpr(), SourceLocation()))
3085 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003086 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003087 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3088 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003089 ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003090 Method->isInlineSpecified(), D->isConstexpr(), SourceLocation()))
3091 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003092 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003093 if (GetImportedOrCreateDecl(ToFunction, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003094 ToInnerLocStart, NameInfo, T, TInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003095 D->getStorageClass(), D->isInlineSpecified(),
3096 D->hasWrittenPrototype(), D->isConstexpr()))
3097 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003098 }
John McCall3e11ebe2010-03-15 10:12:16 +00003099
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003100 // Connect the redecl chain.
3101 if (FoundByLookup) {
3102 auto *Recent = const_cast<FunctionDecl *>(
3103 FoundByLookup->getMostRecentDecl());
3104 ToFunction->setPreviousDecl(Recent);
3105 }
3106
3107 // Import Ctor initializers.
3108 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3109 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3110 SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers);
3111 // Import first, then allocate memory and copy if there was no error.
3112 if (Error Err = ImportContainerChecked(
3113 FromConstructor->inits(), CtorInitializers))
3114 return std::move(Err);
3115 auto **Memory =
3116 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3117 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3118 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
3119 ToCtor->setCtorInitializers(Memory);
3120 ToCtor->setNumCtorInitializers(NumInitializers);
3121 }
3122 }
3123
Balazs Keri3b30d652018-10-19 13:32:20 +00003124 ToFunction->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003125 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003126 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003127 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3128 ToFunction->setTrivial(D->isTrivial());
3129 ToFunction->setPure(D->isPure());
Balazs Keri3b30d652018-10-19 13:32:20 +00003130 ToFunction->setRangeEnd(ToEndLoc);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003131
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003132 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003133 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003134 Param->setOwningFunction(ToFunction);
3135 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003136 }
David Blaikie9c70e042011-09-21 18:16:56 +00003137 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003138
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003139 // We need to complete creation of FunctionProtoTypeLoc manually with setting
3140 // params it refers to.
3141 if (TInfo) {
3142 if (auto ProtoLoc =
3143 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
3144 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
3145 ProtoLoc.setParam(I, Parameters[I]);
3146 }
3147 }
3148
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003149 if (usedDifferentExceptionSpec) {
3150 // Update FunctionProtoType::ExtProtoInfo.
Balazs Keri3b30d652018-10-19 13:32:20 +00003151 if (ExpectedType TyOrErr = import(D->getType()))
3152 ToFunction->setType(*TyOrErr);
3153 else
3154 return TyOrErr.takeError();
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003155 }
3156
Balazs Keria35798d2018-07-17 09:52:41 +00003157 // Import the describing template function, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00003158 if (FromFT) {
3159 auto ToFTOrErr = import(FromFT);
3160 if (!ToFTOrErr)
3161 return ToFTOrErr.takeError();
3162 }
Balazs Keria35798d2018-07-17 09:52:41 +00003163
Gabor Marton5254e642018-06-27 13:32:50 +00003164 if (D->doesThisDeclarationHaveABody()) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003165 Error Err = ImportFunctionDeclBody(D, ToFunction);
3166
3167 if (Err)
3168 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003169 }
3170
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003171 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003172
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003173 // If it is a template, import all related things.
Balazs Keri3b30d652018-10-19 13:32:20 +00003174 if (Error Err = ImportTemplateInformation(D, ToFunction))
3175 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003176
Gabor Marton5254e642018-06-27 13:32:50 +00003177 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
3178
3179 // TODO Can we generalize this approach to other AST nodes as well?
3180 if (D->getDeclContext()->containsDeclAndLoad(D))
3181 DC->addDeclInternal(ToFunction);
3182 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003183 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003184
Gabor Marton5254e642018-06-27 13:32:50 +00003185 // Friend declaration's lexical context is the befriending class, but the
3186 // semantic context is the enclosing scope of the befriending class.
3187 // We want the friend functions to be found in the semantic context by lookup.
3188 // FIXME should we handle this generically in VisitFriendDecl?
3189 // In Other cases when LexicalDC != DC we don't want it to be added,
3190 // e.g out-of-class definitions like void B::f() {} .
3191 if (LexicalDC != DC && IsFriend) {
3192 DC->makeDeclVisibleInContext(ToFunction);
3193 }
3194
Gabor Marton7a0841e2018-10-29 10:18:28 +00003195 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
3196 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
3197
Gabor Marton5254e642018-06-27 13:32:50 +00003198 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003199 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3200 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3201 if (!ToRedeclOrErr)
3202 return ToRedeclOrErr.takeError();
3203 }
Gabor Marton5254e642018-06-27 13:32:50 +00003204
Douglas Gregor43f54792010-02-17 02:12:47 +00003205 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003206}
3207
Balazs Keri3b30d652018-10-19 13:32:20 +00003208ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003209 return VisitFunctionDecl(D);
3210}
3211
Balazs Keri3b30d652018-10-19 13:32:20 +00003212ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003213 return VisitCXXMethodDecl(D);
3214}
3215
Balazs Keri3b30d652018-10-19 13:32:20 +00003216ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003217 return VisitCXXMethodDecl(D);
3218}
3219
Balazs Keri3b30d652018-10-19 13:32:20 +00003220ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003221 return VisitCXXMethodDecl(D);
3222}
3223
Balazs Keri3b30d652018-10-19 13:32:20 +00003224ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003225 // Import the major distinguishing characteristics of a variable.
3226 DeclContext *DC, *LexicalDC;
3227 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003228 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003229 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003230 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3231 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003232 if (ToD)
3233 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003234
Fangrui Song6907ce22018-07-30 19:24:48 +00003235 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003236 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003237 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003238 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003239 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003240 if (!Name &&
3241 ASTImporter::getFieldIndex(D) !=
3242 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003243 continue;
3244
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003245 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003246 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003247 Importer.MapImported(D, FoundField);
Gabor Marton42e15de2018-08-22 11:52:14 +00003248 // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the
3249 // initializer of a FieldDecl might not had been instantiated in the
3250 // "To" context. However, the "From" context might instantiated that,
3251 // thus we have to merge that.
3252 if (Expr *FromInitializer = D->getInClassInitializer()) {
3253 // We don't have yet the initializer set.
3254 if (FoundField->hasInClassInitializer() &&
3255 !FoundField->getInClassInitializer()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003256 if (ExpectedExpr ToInitializerOrErr = import(FromInitializer))
3257 FoundField->setInClassInitializer(*ToInitializerOrErr);
3258 else {
3259 // We can't return error here,
Gabor Marton42e15de2018-08-22 11:52:14 +00003260 // since we already mapped D as imported.
Balazs Keri3b30d652018-10-19 13:32:20 +00003261 // FIXME: warning message?
3262 consumeError(ToInitializerOrErr.takeError());
Gabor Marton42e15de2018-08-22 11:52:14 +00003263 return FoundField;
Balazs Keri3b30d652018-10-19 13:32:20 +00003264 }
Gabor Marton42e15de2018-08-22 11:52:14 +00003265 }
3266 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003267 return FoundField;
3268 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003269
Balazs Keri3b30d652018-10-19 13:32:20 +00003270 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003271 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003272 << Name << D->getType() << FoundField->getType();
3273 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3274 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003275
3276 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003277 }
3278 }
3279
Balazs Keri3b30d652018-10-19 13:32:20 +00003280 QualType ToType;
3281 TypeSourceInfo *ToTInfo;
3282 Expr *ToBitWidth;
3283 SourceLocation ToInnerLocStart;
3284 Expr *ToInitializer;
3285 if (auto Imp = importSeq(
3286 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(),
3287 D->getInnerLocStart(), D->getInClassInitializer()))
3288 std::tie(
3289 ToType, ToTInfo, ToBitWidth, ToInnerLocStart, ToInitializer) = *Imp;
3290 else
3291 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003292
Gabor Marton26f72a92018-07-12 09:42:05 +00003293 FieldDecl *ToField;
3294 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003295 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3296 ToType, ToTInfo, ToBitWidth, D->isMutable(),
3297 D->getInClassInitStyle()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003298 return ToField;
3299
Douglas Gregordd483172010-02-22 17:42:47 +00003300 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003301 ToField->setLexicalDeclContext(LexicalDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00003302 if (ToInitializer)
3303 ToField->setInClassInitializer(ToInitializer);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003304 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00003305 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003306 return ToField;
3307}
3308
Balazs Keri3b30d652018-10-19 13:32:20 +00003309ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003310 // Import the major distinguishing characteristics of a variable.
3311 DeclContext *DC, *LexicalDC;
3312 DeclarationName Name;
3313 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003314 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003315 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3316 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003317 if (ToD)
3318 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003319
Fangrui Song6907ce22018-07-30 19:24:48 +00003320 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003321 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003322 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003323 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003324 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003325 if (!Name &&
3326 ASTImporter::getFieldIndex(D) !=
3327 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003328 continue;
3329
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003330 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003331 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003332 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003333 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003334 return FoundField;
3335 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003336
3337 // If there are more anonymous fields to check, continue.
3338 if (!Name && I < N-1)
3339 continue;
3340
Balazs Keri3b30d652018-10-19 13:32:20 +00003341 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003342 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003343 << Name << D->getType() << FoundField->getType();
3344 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3345 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003346
3347 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003348 }
3349 }
3350
Francois Pichet783dd6e2010-11-21 06:08:52 +00003351 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003352 auto TypeOrErr = import(D->getType());
3353 if (!TypeOrErr)
3354 return TypeOrErr.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003355
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003356 auto **NamedChain =
3357 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00003358
3359 unsigned i = 0;
Balazs Keri3b30d652018-10-19 13:32:20 +00003360 for (auto *PI : D->chain())
3361 if (Expected<NamedDecl *> ToD = import(PI))
3362 NamedChain[i++] = *ToD;
3363 else
3364 return ToD.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003365
Gabor Marton26f72a92018-07-12 09:42:05 +00003366 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
3367 IndirectFieldDecl *ToIndirectField;
3368 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003369 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
Gabor Marton26f72a92018-07-12 09:42:05 +00003370 // FIXME here we leak `NamedChain` which is allocated before
3371 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00003372
Francois Pichet783dd6e2010-11-21 06:08:52 +00003373 ToIndirectField->setAccess(D->getAccess());
3374 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003375 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003376 return ToIndirectField;
3377}
3378
Balazs Keri3b30d652018-10-19 13:32:20 +00003379ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00003380 // Import the major distinguishing characteristics of a declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00003381 DeclContext *DC, *LexicalDC;
3382 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
3383 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003384
3385 // Determine whether we've already imported this decl.
Gabor Marton54058b52018-12-17 13:53:12 +00003386 // FriendDecl is not a NamedDecl so we cannot use lookup.
Aleksei Sidorina693b372016-09-28 10:16:56 +00003387 auto *RD = cast<CXXRecordDecl>(DC);
3388 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003389
3390 while (ImportedFriend) {
3391 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003392 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3393 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003394 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003395
3396 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3397 if (Importer.IsStructurallyEquivalent(
3398 D->getFriendType()->getType(),
3399 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003400 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003401 }
3402 ImportedFriend = ImportedFriend->getNextFriend();
3403 }
3404
3405 // Not found. Create it.
3406 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003407 if (NamedDecl *FriendD = D->getFriendDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003408 NamedDecl *ToFriendD;
3409 if (Error Err = importInto(ToFriendD, FriendD))
3410 return std::move(Err);
3411
3412 if (FriendD->getFriendObjectKind() != Decl::FOK_None &&
Peter Szecsib180eeb2018-04-25 17:28:03 +00003413 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3414 ToFriendD->setObjectOfFriendDecl(false);
3415
3416 ToFU = ToFriendD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003417 } else { // The friend is a type, not a decl.
3418 if (auto TSIOrErr = import(D->getFriendType()))
3419 ToFU = *TSIOrErr;
3420 else
3421 return TSIOrErr.takeError();
3422 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00003423
3424 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003425 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003426 for (unsigned I = 0; I < D->NumTPLists; I++) {
Balazs Keridec09162019-03-20 15:42:42 +00003427 if (auto ListOrErr = import(FromTPLists[I]))
Balazs Keri3b30d652018-10-19 13:32:20 +00003428 ToTPLists[I] = *ListOrErr;
3429 else
3430 return ListOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003431 }
3432
Balazs Keri3b30d652018-10-19 13:32:20 +00003433 auto LocationOrErr = import(D->getLocation());
3434 if (!LocationOrErr)
3435 return LocationOrErr.takeError();
3436 auto FriendLocOrErr = import(D->getFriendLoc());
3437 if (!FriendLocOrErr)
3438 return FriendLocOrErr.takeError();
3439
Gabor Marton26f72a92018-07-12 09:42:05 +00003440 FriendDecl *FrD;
3441 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003442 *LocationOrErr, ToFU,
3443 *FriendLocOrErr, ToTPLists))
Gabor Marton26f72a92018-07-12 09:42:05 +00003444 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003445
3446 FrD->setAccess(D->getAccess());
3447 FrD->setLexicalDeclContext(LexicalDC);
3448 LexicalDC->addDeclInternal(FrD);
3449 return FrD;
3450}
3451
Balazs Keri3b30d652018-10-19 13:32:20 +00003452ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003453 // Import the major distinguishing characteristics of an ivar.
3454 DeclContext *DC, *LexicalDC;
3455 DeclarationName Name;
3456 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003457 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003458 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3459 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003460 if (ToD)
3461 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003462
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003463 // Determine whether we've already imported this ivar
Gabor Marton54058b52018-12-17 13:53:12 +00003464 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003465 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003466 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003467 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003468 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003469 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003470 return FoundIvar;
3471 }
3472
Gabor Marton410f32c2019-04-01 15:29:55 +00003473 Importer.ToDiag(Loc, diag::warn_odr_ivar_type_inconsistent)
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003474 << Name << D->getType() << FoundIvar->getType();
3475 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3476 << FoundIvar->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003477
3478 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003479 }
3480 }
3481
Balazs Keri3b30d652018-10-19 13:32:20 +00003482 QualType ToType;
3483 TypeSourceInfo *ToTypeSourceInfo;
3484 Expr *ToBitWidth;
3485 SourceLocation ToInnerLocStart;
3486 if (auto Imp = importSeq(
3487 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(), D->getInnerLocStart()))
3488 std::tie(ToType, ToTypeSourceInfo, ToBitWidth, ToInnerLocStart) = *Imp;
3489 else
3490 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003491
Gabor Marton26f72a92018-07-12 09:42:05 +00003492 ObjCIvarDecl *ToIvar;
3493 if (GetImportedOrCreateDecl(
3494 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003495 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3496 ToType, ToTypeSourceInfo,
3497 D->getAccessControl(),ToBitWidth, D->getSynthesize()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003498 return ToIvar;
3499
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003500 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003501 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003502 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003503}
3504
Balazs Keri3b30d652018-10-19 13:32:20 +00003505ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003506
3507 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
3508 auto RedeclIt = Redecls.begin();
3509 // Import the first part of the decl chain. I.e. import all previous
3510 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003511 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3512 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3513 if (!RedeclOrErr)
3514 return RedeclOrErr.takeError();
3515 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003516 assert(*RedeclIt == D);
3517
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003518 // Import the major distinguishing characteristics of a variable.
3519 DeclContext *DC, *LexicalDC;
3520 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003521 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003522 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003523 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3524 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003525 if (ToD)
3526 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003527
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003528 // Try to find a variable in our own ("to") context with the same name and
3529 // in the same context as the variable we're importing.
Gabor Martonac3a5d62018-09-17 12:04:52 +00003530 VarDecl *FoundByLookup = nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003531 if (D->isFileVarDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003532 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003533 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00003534 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003535 for (auto *FoundDecl : FoundDecls) {
3536 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003537 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003538
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003539 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00003540 if (!hasSameVisibilityContext(FoundVar, D))
3541 continue;
3542 if (Importer.IsStructurallyEquivalent(D->getType(),
3543 FoundVar->getType())) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003544
Gabor Marton458d1452019-02-14 13:07:03 +00003545 // The VarDecl in the "From" context has a definition, but in the
3546 // "To" context we already have a definition.
3547 VarDecl *FoundDef = FoundVar->getDefinition();
3548 if (D->isThisDeclarationADefinition() && FoundDef)
3549 // FIXME Check for ODR error if the two definitions have
3550 // different initializers?
3551 return Importer.MapImported(D, FoundDef);
Gabor Martonac3a5d62018-09-17 12:04:52 +00003552
Gabor Marton458d1452019-02-14 13:07:03 +00003553 // The VarDecl in the "From" context has an initializer, but in the
3554 // "To" context we already have an initializer.
3555 const VarDecl *FoundDInit = nullptr;
3556 if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
3557 // FIXME Diagnose ODR error if the two initializers are different?
3558 return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit));
3559
3560 FoundByLookup = FoundVar;
3561 break;
3562 }
3563
3564 const ArrayType *FoundArray
3565 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3566 const ArrayType *TArray
3567 = Importer.getToContext().getAsArrayType(D->getType());
3568 if (FoundArray && TArray) {
3569 if (isa<IncompleteArrayType>(FoundArray) &&
3570 isa<ConstantArrayType>(TArray)) {
3571 // Import the type.
3572 if (auto TyOrErr = import(D->getType()))
3573 FoundVar->setType(*TyOrErr);
3574 else
3575 return TyOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00003576
3577 FoundByLookup = FoundVar;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003578 break;
Gabor Marton458d1452019-02-14 13:07:03 +00003579 } else if (isa<IncompleteArrayType>(TArray) &&
3580 isa<ConstantArrayType>(FoundArray)) {
3581 FoundByLookup = FoundVar;
3582 break;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003583 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003584 }
Gabor Marton458d1452019-02-14 13:07:03 +00003585
Gabor Marton410f32c2019-04-01 15:29:55 +00003586 Importer.ToDiag(Loc, diag::warn_odr_variable_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00003587 << Name << D->getType() << FoundVar->getType();
3588 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3589 << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003590 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003591
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003592 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003593 }
3594
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003595 if (!ConflictingDecls.empty()) {
3596 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003597 ConflictingDecls.data(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003598 ConflictingDecls.size());
3599 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003600 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003601 }
3602 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003603
Balazs Keri3b30d652018-10-19 13:32:20 +00003604 QualType ToType;
3605 TypeSourceInfo *ToTypeSourceInfo;
3606 SourceLocation ToInnerLocStart;
3607 NestedNameSpecifierLoc ToQualifierLoc;
3608 if (auto Imp = importSeq(
3609 D->getType(), D->getTypeSourceInfo(), D->getInnerLocStart(),
3610 D->getQualifierLoc()))
3611 std::tie(ToType, ToTypeSourceInfo, ToInnerLocStart, ToQualifierLoc) = *Imp;
3612 else
3613 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003614
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003615 // Create the imported variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00003616 VarDecl *ToVar;
3617 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003618 ToInnerLocStart, Loc,
3619 Name.getAsIdentifierInfo(),
3620 ToType, ToTypeSourceInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003621 D->getStorageClass()))
3622 return ToVar;
3623
Balazs Keri3b30d652018-10-19 13:32:20 +00003624 ToVar->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003625 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003626 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003627
Gabor Martonac3a5d62018-09-17 12:04:52 +00003628 if (FoundByLookup) {
3629 auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
3630 ToVar->setPreviousDecl(Recent);
3631 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003632
Balazs Keri3b30d652018-10-19 13:32:20 +00003633 if (Error Err = ImportInitializer(D, ToVar))
3634 return std::move(Err);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003635
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003636 if (D->isConstexpr())
3637 ToVar->setConstexpr(true);
3638
Gabor Martonac3a5d62018-09-17 12:04:52 +00003639 if (D->getDeclContext()->containsDeclAndLoad(D))
3640 DC->addDeclInternal(ToVar);
3641 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
3642 LexicalDC->addDeclInternal(ToVar);
3643
3644 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003645 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3646 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3647 if (!RedeclOrErr)
3648 return RedeclOrErr.takeError();
3649 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003650
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003651 return ToVar;
3652}
3653
Balazs Keri3b30d652018-10-19 13:32:20 +00003654ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
Douglas Gregor8b228d72010-02-17 21:22:52 +00003655 // Parameters are created in the translation unit's context, then moved
3656 // into the function declaration's context afterward.
3657 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003658
Balazs Keri3b30d652018-10-19 13:32:20 +00003659 DeclarationName ToDeclName;
3660 SourceLocation ToLocation;
3661 QualType ToType;
3662 if (auto Imp = importSeq(D->getDeclName(), D->getLocation(), D->getType()))
3663 std::tie(ToDeclName, ToLocation, ToType) = *Imp;
3664 else
3665 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003666
Douglas Gregor8b228d72010-02-17 21:22:52 +00003667 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003668 ImplicitParamDecl *ToParm = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003669 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3670 ToLocation, ToDeclName.getAsIdentifierInfo(),
3671 ToType, D->getParameterKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003672 return ToParm;
3673 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003674}
3675
Balazs Keri3b30d652018-10-19 13:32:20 +00003676ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003677 // Parameters are created in the translation unit's context, then moved
3678 // into the function declaration's context afterward.
3679 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003680
Balazs Keri3b30d652018-10-19 13:32:20 +00003681 DeclarationName ToDeclName;
3682 SourceLocation ToLocation, ToInnerLocStart;
3683 QualType ToType;
3684 TypeSourceInfo *ToTypeSourceInfo;
3685 if (auto Imp = importSeq(
3686 D->getDeclName(), D->getLocation(), D->getType(), D->getInnerLocStart(),
3687 D->getTypeSourceInfo()))
3688 std::tie(
3689 ToDeclName, ToLocation, ToType, ToInnerLocStart,
3690 ToTypeSourceInfo) = *Imp;
3691 else
3692 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003693
Gabor Marton26f72a92018-07-12 09:42:05 +00003694 ParmVarDecl *ToParm;
3695 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003696 ToInnerLocStart, ToLocation,
3697 ToDeclName.getAsIdentifierInfo(), ToType,
3698 ToTypeSourceInfo, D->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003699 /*DefaultArg*/ nullptr))
3700 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003701
3702 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003703 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003704 ToParm->setKNRPromoted(D->isKNRPromoted());
3705
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003706 if (D->hasUninstantiatedDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003707 if (auto ToDefArgOrErr = import(D->getUninstantiatedDefaultArg()))
3708 ToParm->setUninstantiatedDefaultArg(*ToDefArgOrErr);
3709 else
3710 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003711 } else if (D->hasUnparsedDefaultArg()) {
3712 ToParm->setUnparsedDefaultArg();
3713 } else if (D->hasDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003714 if (auto ToDefArgOrErr = import(D->getDefaultArg()))
3715 ToParm->setDefaultArg(*ToDefArgOrErr);
3716 else
3717 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003718 }
Sean Callanan59721b32015-04-28 18:41:46 +00003719
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003720 if (D->isObjCMethodParameter()) {
3721 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3722 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3723 } else {
3724 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3725 D->getFunctionScopeIndex());
3726 }
3727
Gabor Marton26f72a92018-07-12 09:42:05 +00003728 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003729}
3730
Balazs Keri3b30d652018-10-19 13:32:20 +00003731ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003732 // Import the major distinguishing characteristics of a method.
3733 DeclContext *DC, *LexicalDC;
3734 DeclarationName Name;
3735 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003736 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003737 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3738 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003739 if (ToD)
3740 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003741
Gabor Marton54058b52018-12-17 13:53:12 +00003742 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003743 for (auto *FoundDecl : FoundDecls) {
3744 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003745 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3746 continue;
3747
3748 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003749 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3750 FoundMethod->getReturnType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003751 Importer.ToDiag(Loc, diag::warn_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003752 << D->isInstanceMethod() << Name << D->getReturnType()
3753 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003754 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003755 diag::note_odr_objc_method_here)
3756 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003757
3758 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003759 }
3760
3761 // Check the number of parameters.
3762 if (D->param_size() != FoundMethod->param_size()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003763 Importer.ToDiag(Loc, diag::warn_odr_objc_method_num_params_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003764 << D->isInstanceMethod() << Name
3765 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00003766 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003767 diag::note_odr_objc_method_here)
3768 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003769
3770 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003771 }
3772
3773 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003774 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003775 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3776 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003777 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003778 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003779 Importer.FromDiag((*P)->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00003780 diag::warn_odr_objc_method_param_type_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003781 << D->isInstanceMethod() << Name
3782 << (*P)->getType() << (*FoundP)->getType();
3783 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3784 << (*FoundP)->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003785
3786 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003787 }
3788 }
3789
3790 // Check variadic/non-variadic.
3791 // Check the number of parameters.
3792 if (D->isVariadic() != FoundMethod->isVariadic()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003793 Importer.ToDiag(Loc, diag::warn_odr_objc_method_variadic_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003794 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00003795 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003796 diag::note_odr_objc_method_here)
3797 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003798
3799 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003800 }
3801
3802 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003803 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003804 }
3805 }
3806
Balazs Keri3b30d652018-10-19 13:32:20 +00003807 SourceLocation ToEndLoc;
3808 QualType ToReturnType;
3809 TypeSourceInfo *ToReturnTypeSourceInfo;
3810 if (auto Imp = importSeq(
3811 D->getEndLoc(), D->getReturnType(), D->getReturnTypeSourceInfo()))
3812 std::tie(ToEndLoc, ToReturnType, ToReturnTypeSourceInfo) = *Imp;
3813 else
3814 return Imp.takeError();
Douglas Gregor12852d92010-03-08 14:59:44 +00003815
Gabor Marton26f72a92018-07-12 09:42:05 +00003816 ObjCMethodDecl *ToMethod;
3817 if (GetImportedOrCreateDecl(
3818 ToMethod, D, Importer.getToContext(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00003819 ToEndLoc, Name.getObjCSelector(), ToReturnType,
3820 ToReturnTypeSourceInfo, DC, D->isInstanceMethod(), D->isVariadic(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003821 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3822 D->getImplementationControl(), D->hasRelatedResultType()))
3823 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003824
3825 // FIXME: When we decide to merge method definitions, we'll need to
3826 // deal with implicit parameters.
3827
3828 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003829 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003830 for (auto *FromP : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003831 if (Expected<ParmVarDecl *> ToPOrErr = import(FromP))
3832 ToParams.push_back(*ToPOrErr);
3833 else
3834 return ToPOrErr.takeError();
Douglas Gregor43f54792010-02-17 02:12:47 +00003835 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003836
Douglas Gregor43f54792010-02-17 02:12:47 +00003837 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003838 for (auto *ToParam : ToParams) {
3839 ToParam->setOwningFunction(ToMethod);
3840 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003841 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003842
Balazs Keri3b30d652018-10-19 13:32:20 +00003843 SmallVector<SourceLocation, 12> FromSelLocs;
3844 D->getSelectorLocs(FromSelLocs);
3845 SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size());
3846 if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs))
3847 return std::move(Err);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003848
Balazs Keri3b30d652018-10-19 13:32:20 +00003849 ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003850
3851 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003852 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003853 return ToMethod;
3854}
3855
Balazs Keri3b30d652018-10-19 13:32:20 +00003856ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00003857 // Import the major distinguishing characteristics of a category.
3858 DeclContext *DC, *LexicalDC;
3859 DeclarationName Name;
3860 SourceLocation Loc;
3861 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003862 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3863 return std::move(Err);
Douglas Gregor85f3f952015-07-07 03:57:15 +00003864 if (ToD)
3865 return ToD;
3866
Balazs Keri3b30d652018-10-19 13:32:20 +00003867 SourceLocation ToVarianceLoc, ToLocation, ToColonLoc;
3868 TypeSourceInfo *ToTypeSourceInfo;
3869 if (auto Imp = importSeq(
3870 D->getVarianceLoc(), D->getLocation(), D->getColonLoc(),
3871 D->getTypeSourceInfo()))
3872 std::tie(ToVarianceLoc, ToLocation, ToColonLoc, ToTypeSourceInfo) = *Imp;
3873 else
3874 return Imp.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00003875
Gabor Marton26f72a92018-07-12 09:42:05 +00003876 ObjCTypeParamDecl *Result;
3877 if (GetImportedOrCreateDecl(
3878 Result, D, Importer.getToContext(), DC, D->getVariance(),
Balazs Keri3b30d652018-10-19 13:32:20 +00003879 ToVarianceLoc, D->getIndex(),
3880 ToLocation, Name.getAsIdentifierInfo(),
3881 ToColonLoc, ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00003882 return Result;
3883
Douglas Gregor85f3f952015-07-07 03:57:15 +00003884 Result->setLexicalDeclContext(LexicalDC);
3885 return Result;
3886}
3887
Balazs Keri3b30d652018-10-19 13:32:20 +00003888ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00003889 // Import the major distinguishing characteristics of a category.
3890 DeclContext *DC, *LexicalDC;
3891 DeclarationName Name;
3892 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003893 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003894 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3895 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003896 if (ToD)
3897 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003898
Balazs Keri3b30d652018-10-19 13:32:20 +00003899 ObjCInterfaceDecl *ToInterface;
3900 if (Error Err = importInto(ToInterface, D->getClassInterface()))
3901 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00003902
Douglas Gregor84c51c32010-02-18 01:47:50 +00003903 // Determine if we've already encountered this category.
3904 ObjCCategoryDecl *MergeWithCategory
3905 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3906 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3907 if (!ToCategory) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003908 SourceLocation ToAtStartLoc, ToCategoryNameLoc;
3909 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
3910 if (auto Imp = importSeq(
3911 D->getAtStartLoc(), D->getCategoryNameLoc(),
3912 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
3913 std::tie(
3914 ToAtStartLoc, ToCategoryNameLoc,
3915 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
3916 else
3917 return Imp.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00003918
3919 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003920 ToAtStartLoc, Loc,
3921 ToCategoryNameLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00003922 Name.getAsIdentifierInfo(), ToInterface,
3923 /*TypeParamList=*/nullptr,
Balazs Keri3b30d652018-10-19 13:32:20 +00003924 ToIvarLBraceLoc,
3925 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00003926 return ToCategory;
3927
Douglas Gregor84c51c32010-02-18 01:47:50 +00003928 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003929 LexicalDC->addDeclInternal(ToCategory);
Balazs Keri3b30d652018-10-19 13:32:20 +00003930 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003931 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00003932 if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList()))
3933 ToCategory->setTypeParamList(*PListOrErr);
3934 else
3935 return PListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00003936
Douglas Gregor84c51c32010-02-18 01:47:50 +00003937 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003938 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3939 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003940 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3941 = D->protocol_loc_begin();
3942 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3943 FromProtoEnd = D->protocol_end();
3944 FromProto != FromProtoEnd;
3945 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003946 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
3947 Protocols.push_back(*ToProtoOrErr);
3948 else
3949 return ToProtoOrErr.takeError();
3950
3951 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
3952 ProtocolLocs.push_back(*ToProtoLocOrErr);
3953 else
3954 return ToProtoLocOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00003955 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003956
Douglas Gregor84c51c32010-02-18 01:47:50 +00003957 // FIXME: If we're merging, make sure that the protocol list is the same.
3958 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3959 ProtocolLocs.data(), Importer.getToContext());
Balazs Keri3b30d652018-10-19 13:32:20 +00003960
Douglas Gregor84c51c32010-02-18 01:47:50 +00003961 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003962 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003963 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003964
Douglas Gregor84c51c32010-02-18 01:47:50 +00003965 // Import all of the members of this category.
Balazs Keri3b30d652018-10-19 13:32:20 +00003966 if (Error Err = ImportDeclContext(D))
3967 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00003968
Douglas Gregor84c51c32010-02-18 01:47:50 +00003969 // If we have an implementation, import it as well.
3970 if (D->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003971 if (Expected<ObjCCategoryImplDecl *> ToImplOrErr =
3972 import(D->getImplementation()))
3973 ToCategory->setImplementation(*ToImplOrErr);
3974 else
3975 return ToImplOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00003976 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003977
Douglas Gregor84c51c32010-02-18 01:47:50 +00003978 return ToCategory;
3979}
3980
Balazs Keri3b30d652018-10-19 13:32:20 +00003981Error ASTNodeImporter::ImportDefinition(
3982 ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003983 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003984 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00003985 if (Error Err = ImportDeclContext(From))
3986 return Err;
3987 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00003988 }
3989
3990 // Start the protocol definition
3991 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00003992
Douglas Gregor2aa53772012-01-24 17:42:07 +00003993 // Import protocols
3994 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3995 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00003996 ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc =
3997 From->protocol_loc_begin();
Douglas Gregor2aa53772012-01-24 17:42:07 +00003998 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3999 FromProtoEnd = From->protocol_end();
4000 FromProto != FromProtoEnd;
4001 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004002 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4003 Protocols.push_back(*ToProtoOrErr);
4004 else
4005 return ToProtoOrErr.takeError();
4006
4007 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4008 ProtocolLocs.push_back(*ToProtoLocOrErr);
4009 else
4010 return ToProtoLocOrErr.takeError();
4011
Douglas Gregor2aa53772012-01-24 17:42:07 +00004012 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004013
Douglas Gregor2aa53772012-01-24 17:42:07 +00004014 // FIXME: If we're merging, make sure that the protocol list is the same.
4015 To->setProtocolList(Protocols.data(), Protocols.size(),
4016 ProtocolLocs.data(), Importer.getToContext());
4017
Douglas Gregor2e15c842012-02-01 21:00:38 +00004018 if (shouldForceImportDeclContext(Kind)) {
4019 // Import all of the members of this protocol.
Balazs Keri3b30d652018-10-19 13:32:20 +00004020 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4021 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004022 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004023 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004024}
4025
Balazs Keri3b30d652018-10-19 13:32:20 +00004026ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004027 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00004028 // from, but this particular declaration is not that definition, import the
4029 // definition and map to that.
4030 ObjCProtocolDecl *Definition = D->getDefinition();
4031 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004032 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4033 return Importer.MapImported(D, *ImportedDefOrErr);
4034 else
4035 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004036 }
4037
Douglas Gregor84c51c32010-02-18 01:47:50 +00004038 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004039 DeclContext *DC, *LexicalDC;
4040 DeclarationName Name;
4041 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004042 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004043 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4044 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004045 if (ToD)
4046 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004047
Craig Topper36250ad2014-05-12 05:36:57 +00004048 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004049 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004050 for (auto *FoundDecl : FoundDecls) {
4051 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004052 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004053
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004054 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004055 break;
4056 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004057
Douglas Gregor98d156a2010-02-17 16:12:00 +00004058 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004059 if (!ToProto) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004060 auto ToAtBeginLocOrErr = import(D->getAtStartLoc());
4061 if (!ToAtBeginLocOrErr)
4062 return ToAtBeginLocOrErr.takeError();
4063
Gabor Marton26f72a92018-07-12 09:42:05 +00004064 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
4065 Name.getAsIdentifierInfo(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004066 *ToAtBeginLocOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004067 /*PrevDecl=*/nullptr))
4068 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004069 ToProto->setLexicalDeclContext(LexicalDC);
4070 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004071 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004072
4073 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004074
Balazs Keri3b30d652018-10-19 13:32:20 +00004075 if (D->isThisDeclarationADefinition())
4076 if (Error Err = ImportDefinition(D, ToProto))
4077 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004078
Douglas Gregor98d156a2010-02-17 16:12:00 +00004079 return ToProto;
4080}
4081
Balazs Keri3b30d652018-10-19 13:32:20 +00004082ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4083 DeclContext *DC, *LexicalDC;
4084 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4085 return std::move(Err);
Sean Callanan0aae0412014-12-10 00:00:37 +00004086
Balazs Keri3b30d652018-10-19 13:32:20 +00004087 ExpectedSLoc ExternLocOrErr = import(D->getExternLoc());
4088 if (!ExternLocOrErr)
4089 return ExternLocOrErr.takeError();
4090
4091 ExpectedSLoc LangLocOrErr = import(D->getLocation());
4092 if (!LangLocOrErr)
4093 return LangLocOrErr.takeError();
Sean Callanan0aae0412014-12-10 00:00:37 +00004094
4095 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00004096
4097 LinkageSpecDecl *ToLinkageSpec;
4098 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004099 *ExternLocOrErr, *LangLocOrErr,
4100 D->getLanguage(), HasBraces))
Gabor Marton26f72a92018-07-12 09:42:05 +00004101 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00004102
4103 if (HasBraces) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004104 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
4105 if (!RBraceLocOrErr)
4106 return RBraceLocOrErr.takeError();
4107 ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr);
Sean Callanan0aae0412014-12-10 00:00:37 +00004108 }
4109
4110 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4111 LexicalDC->addDeclInternal(ToLinkageSpec);
4112
Sean Callanan0aae0412014-12-10 00:00:37 +00004113 return ToLinkageSpec;
4114}
4115
Balazs Keri3b30d652018-10-19 13:32:20 +00004116ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004117 DeclContext *DC, *LexicalDC;
4118 DeclarationName Name;
4119 SourceLocation Loc;
4120 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004121 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4122 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004123 if (ToD)
4124 return ToD;
4125
Balazs Keri3b30d652018-10-19 13:32:20 +00004126 SourceLocation ToLoc, ToUsingLoc;
4127 NestedNameSpecifierLoc ToQualifierLoc;
4128 if (auto Imp = importSeq(
4129 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc()))
4130 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc) = *Imp;
4131 else
4132 return Imp.takeError();
4133
4134 DeclarationNameInfo NameInfo(Name, ToLoc);
4135 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4136 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004137
Gabor Marton26f72a92018-07-12 09:42:05 +00004138 UsingDecl *ToUsing;
4139 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004140 ToUsingLoc, ToQualifierLoc, NameInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00004141 D->hasTypename()))
4142 return ToUsing;
4143
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004144 ToUsing->setLexicalDeclContext(LexicalDC);
4145 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004146
4147 if (NamedDecl *FromPattern =
4148 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004149 if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern))
4150 Importer.getToContext().setInstantiatedFromUsingDecl(
4151 ToUsing, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004152 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004153 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004154 }
4155
Balazs Keri3b30d652018-10-19 13:32:20 +00004156 for (UsingShadowDecl *FromShadow : D->shadows()) {
4157 if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow))
4158 ToUsing->addShadowDecl(*ToShadowOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004159 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004160 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004161 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004162 return ToShadowOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004163 }
4164 return ToUsing;
4165}
4166
Balazs Keri3b30d652018-10-19 13:32:20 +00004167ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004168 DeclContext *DC, *LexicalDC;
4169 DeclarationName Name;
4170 SourceLocation Loc;
4171 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004172 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4173 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004174 if (ToD)
4175 return ToD;
4176
Balazs Keri3b30d652018-10-19 13:32:20 +00004177 Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl());
4178 if (!ToUsingOrErr)
4179 return ToUsingOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004180
Balazs Keri3b30d652018-10-19 13:32:20 +00004181 Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl());
4182 if (!ToTargetOrErr)
4183 return ToTargetOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004184
Gabor Marton26f72a92018-07-12 09:42:05 +00004185 UsingShadowDecl *ToShadow;
4186 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004187 *ToUsingOrErr, *ToTargetOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004188 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004189
4190 ToShadow->setLexicalDeclContext(LexicalDC);
4191 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004192
4193 if (UsingShadowDecl *FromPattern =
4194 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004195 if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern))
4196 Importer.getToContext().setInstantiatedFromUsingShadowDecl(
4197 ToShadow, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004198 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004199 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004200 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004201 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004202 }
4203
4204 LexicalDC->addDeclInternal(ToShadow);
4205
4206 return ToShadow;
4207}
4208
Balazs Keri3b30d652018-10-19 13:32:20 +00004209ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004210 DeclContext *DC, *LexicalDC;
4211 DeclarationName Name;
4212 SourceLocation Loc;
4213 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004214 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4215 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004216 if (ToD)
4217 return ToD;
4218
Balazs Keri3b30d652018-10-19 13:32:20 +00004219 auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor());
4220 if (!ToComAncestorOrErr)
4221 return ToComAncestorOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004222
Balazs Keri3b30d652018-10-19 13:32:20 +00004223 NamespaceDecl *ToNominatedNamespace;
4224 SourceLocation ToUsingLoc, ToNamespaceKeyLocation, ToIdentLocation;
4225 NestedNameSpecifierLoc ToQualifierLoc;
4226 if (auto Imp = importSeq(
4227 D->getNominatedNamespace(), D->getUsingLoc(),
4228 D->getNamespaceKeyLocation(), D->getQualifierLoc(),
4229 D->getIdentLocation()))
4230 std::tie(
4231 ToNominatedNamespace, ToUsingLoc, ToNamespaceKeyLocation,
4232 ToQualifierLoc, ToIdentLocation) = *Imp;
4233 else
4234 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004235
Gabor Marton26f72a92018-07-12 09:42:05 +00004236 UsingDirectiveDecl *ToUsingDir;
4237 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004238 ToUsingLoc,
4239 ToNamespaceKeyLocation,
4240 ToQualifierLoc,
4241 ToIdentLocation,
4242 ToNominatedNamespace, *ToComAncestorOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004243 return ToUsingDir;
4244
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004245 ToUsingDir->setLexicalDeclContext(LexicalDC);
4246 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004247
4248 return ToUsingDir;
4249}
4250
Balazs Keri3b30d652018-10-19 13:32:20 +00004251ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004252 UnresolvedUsingValueDecl *D) {
4253 DeclContext *DC, *LexicalDC;
4254 DeclarationName Name;
4255 SourceLocation Loc;
4256 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004257 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4258 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004259 if (ToD)
4260 return ToD;
4261
Balazs Keri3b30d652018-10-19 13:32:20 +00004262 SourceLocation ToLoc, ToUsingLoc, ToEllipsisLoc;
4263 NestedNameSpecifierLoc ToQualifierLoc;
4264 if (auto Imp = importSeq(
4265 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc(),
4266 D->getEllipsisLoc()))
4267 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4268 else
4269 return Imp.takeError();
4270
4271 DeclarationNameInfo NameInfo(Name, ToLoc);
4272 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4273 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004274
Gabor Marton26f72a92018-07-12 09:42:05 +00004275 UnresolvedUsingValueDecl *ToUsingValue;
4276 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004277 ToUsingLoc, ToQualifierLoc, NameInfo,
4278 ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004279 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004280
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004281 ToUsingValue->setAccess(D->getAccess());
4282 ToUsingValue->setLexicalDeclContext(LexicalDC);
4283 LexicalDC->addDeclInternal(ToUsingValue);
4284
4285 return ToUsingValue;
4286}
4287
Balazs Keri3b30d652018-10-19 13:32:20 +00004288ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004289 UnresolvedUsingTypenameDecl *D) {
4290 DeclContext *DC, *LexicalDC;
4291 DeclarationName Name;
4292 SourceLocation Loc;
4293 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004294 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4295 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004296 if (ToD)
4297 return ToD;
4298
Balazs Keri3b30d652018-10-19 13:32:20 +00004299 SourceLocation ToUsingLoc, ToTypenameLoc, ToEllipsisLoc;
4300 NestedNameSpecifierLoc ToQualifierLoc;
4301 if (auto Imp = importSeq(
4302 D->getUsingLoc(), D->getTypenameLoc(), D->getQualifierLoc(),
4303 D->getEllipsisLoc()))
4304 std::tie(ToUsingLoc, ToTypenameLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4305 else
4306 return Imp.takeError();
4307
Gabor Marton26f72a92018-07-12 09:42:05 +00004308 UnresolvedUsingTypenameDecl *ToUsing;
4309 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004310 ToUsingLoc, ToTypenameLoc,
4311 ToQualifierLoc, Loc, Name, ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004312 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004313
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004314 ToUsing->setAccess(D->getAccess());
4315 ToUsing->setLexicalDeclContext(LexicalDC);
4316 LexicalDC->addDeclInternal(ToUsing);
4317
4318 return ToUsing;
4319}
4320
Balazs Keri3b30d652018-10-19 13:32:20 +00004321
4322Error ASTNodeImporter::ImportDefinition(
4323 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004324 if (To->getDefinition()) {
4325 // Check consistency of superclass.
4326 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4327 if (FromSuper) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004328 if (auto FromSuperOrErr = import(FromSuper))
4329 FromSuper = *FromSuperOrErr;
4330 else
4331 return FromSuperOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004332 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004333
4334 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004335 if ((bool)FromSuper != (bool)ToSuper ||
4336 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004337 Importer.ToDiag(To->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004338 diag::warn_odr_objc_superclass_inconsistent)
Douglas Gregor2aa53772012-01-24 17:42:07 +00004339 << To->getDeclName();
4340 if (ToSuper)
4341 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4342 << To->getSuperClass()->getDeclName();
4343 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004344 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004345 diag::note_odr_objc_missing_superclass);
4346 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00004347 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004348 diag::note_odr_objc_superclass)
4349 << From->getSuperClass()->getDeclName();
4350 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004351 Importer.FromDiag(From->getLocation(),
4352 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004353 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004354
Douglas Gregor2e15c842012-02-01 21:00:38 +00004355 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004356 if (Error Err = ImportDeclContext(From))
4357 return Err;
4358 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004359 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004360
Douglas Gregor2aa53772012-01-24 17:42:07 +00004361 // Start the definition.
4362 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004363
Douglas Gregor2aa53772012-01-24 17:42:07 +00004364 // If this class has a superclass, import it.
4365 if (From->getSuperClass()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004366 if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo()))
4367 To->setSuperClass(*SuperTInfoOrErr);
4368 else
4369 return SuperTInfoOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004370 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004371
Douglas Gregor2aa53772012-01-24 17:42:07 +00004372 // Import protocols
4373 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4374 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004375 ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc =
4376 From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00004377
Douglas Gregor2aa53772012-01-24 17:42:07 +00004378 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4379 FromProtoEnd = From->protocol_end();
4380 FromProto != FromProtoEnd;
4381 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004382 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4383 Protocols.push_back(*ToProtoOrErr);
4384 else
4385 return ToProtoOrErr.takeError();
4386
4387 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4388 ProtocolLocs.push_back(*ToProtoLocOrErr);
4389 else
4390 return ToProtoLocOrErr.takeError();
4391
Douglas Gregor2aa53772012-01-24 17:42:07 +00004392 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004393
Douglas Gregor2aa53772012-01-24 17:42:07 +00004394 // FIXME: If we're merging, make sure that the protocol list is the same.
4395 To->setProtocolList(Protocols.data(), Protocols.size(),
4396 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00004397
Douglas Gregor2aa53772012-01-24 17:42:07 +00004398 // Import categories. When the categories themselves are imported, they'll
4399 // hook themselves into this interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004400 for (auto *Cat : From->known_categories()) {
4401 auto ToCatOrErr = import(Cat);
4402 if (!ToCatOrErr)
4403 return ToCatOrErr.takeError();
4404 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004405
Douglas Gregor2aa53772012-01-24 17:42:07 +00004406 // If we have an @implementation, import it as well.
4407 if (From->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004408 if (Expected<ObjCImplementationDecl *> ToImplOrErr =
4409 import(From->getImplementation()))
4410 To->setImplementation(*ToImplOrErr);
4411 else
4412 return ToImplOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004413 }
4414
Douglas Gregor2e15c842012-02-01 21:00:38 +00004415 if (shouldForceImportDeclContext(Kind)) {
4416 // Import all of the members of this class.
Balazs Keri3b30d652018-10-19 13:32:20 +00004417 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4418 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004419 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004420 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004421}
4422
Balazs Keri3b30d652018-10-19 13:32:20 +00004423Expected<ObjCTypeParamList *>
Douglas Gregor85f3f952015-07-07 03:57:15 +00004424ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4425 if (!list)
4426 return nullptr;
4427
4428 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
Balazs Keri3b30d652018-10-19 13:32:20 +00004429 for (auto *fromTypeParam : *list) {
4430 if (auto toTypeParamOrErr = import(fromTypeParam))
4431 toTypeParams.push_back(*toTypeParamOrErr);
4432 else
4433 return toTypeParamOrErr.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004434 }
4435
Balazs Keri3b30d652018-10-19 13:32:20 +00004436 auto LAngleLocOrErr = import(list->getLAngleLoc());
4437 if (!LAngleLocOrErr)
4438 return LAngleLocOrErr.takeError();
4439
4440 auto RAngleLocOrErr = import(list->getRAngleLoc());
4441 if (!RAngleLocOrErr)
4442 return RAngleLocOrErr.takeError();
4443
Douglas Gregor85f3f952015-07-07 03:57:15 +00004444 return ObjCTypeParamList::create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004445 *LAngleLocOrErr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00004446 toTypeParams,
Balazs Keri3b30d652018-10-19 13:32:20 +00004447 *RAngleLocOrErr);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004448}
4449
Balazs Keri3b30d652018-10-19 13:32:20 +00004450ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004451 // If this class has a definition in the translation unit we're coming from,
4452 // but this particular declaration is not that definition, import the
4453 // definition and map to that.
4454 ObjCInterfaceDecl *Definition = D->getDefinition();
4455 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004456 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4457 return Importer.MapImported(D, *ImportedDefOrErr);
4458 else
4459 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004460 }
4461
Douglas Gregor45635322010-02-16 01:20:57 +00004462 // Import the major distinguishing characteristics of an @interface.
4463 DeclContext *DC, *LexicalDC;
4464 DeclarationName Name;
4465 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004466 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004467 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4468 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004469 if (ToD)
4470 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004471
Douglas Gregor2aa53772012-01-24 17:42:07 +00004472 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004473 ObjCInterfaceDecl *MergeWithIface = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004474 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004475 for (auto *FoundDecl : FoundDecls) {
4476 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004477 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004478
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004479 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004480 break;
4481 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004482
Douglas Gregor2aa53772012-01-24 17:42:07 +00004483 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004484 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004485 if (!ToIface) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004486 ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc());
4487 if (!AtBeginLocOrErr)
4488 return AtBeginLocOrErr.takeError();
4489
Gabor Marton26f72a92018-07-12 09:42:05 +00004490 if (GetImportedOrCreateDecl(
4491 ToIface, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004492 *AtBeginLocOrErr, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004493 /*TypeParamList=*/nullptr,
4494 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4495 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004496 ToIface->setLexicalDeclContext(LexicalDC);
4497 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004498 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004499 Importer.MapImported(D, ToIface);
Balazs Keri3b30d652018-10-19 13:32:20 +00004500 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004501 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004502 if (auto ToPListOrErr =
4503 ImportObjCTypeParamList(D->getTypeParamListAsWritten()))
4504 ToIface->setTypeParamList(*ToPListOrErr);
4505 else
4506 return ToPListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004507
Balazs Keri3b30d652018-10-19 13:32:20 +00004508 if (D->isThisDeclarationADefinition())
4509 if (Error Err = ImportDefinition(D, ToIface))
4510 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004511
Douglas Gregor98d156a2010-02-17 16:12:00 +00004512 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004513}
4514
Balazs Keri3b30d652018-10-19 13:32:20 +00004515ExpectedDecl
4516ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4517 ObjCCategoryDecl *Category;
4518 if (Error Err = importInto(Category, D->getCategoryDecl()))
4519 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004520
Douglas Gregor4da9d682010-12-07 15:32:12 +00004521 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4522 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004523 DeclContext *DC, *LexicalDC;
4524 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4525 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004526
Balazs Keri3b30d652018-10-19 13:32:20 +00004527 SourceLocation ToLocation, ToAtStartLoc, ToCategoryNameLoc;
4528 if (auto Imp = importSeq(
4529 D->getLocation(), D->getAtStartLoc(), D->getCategoryNameLoc()))
4530 std::tie(ToLocation, ToAtStartLoc, ToCategoryNameLoc) = *Imp;
4531 else
4532 return Imp.takeError();
4533
Gabor Marton26f72a92018-07-12 09:42:05 +00004534 if (GetImportedOrCreateDecl(
4535 ToImpl, D, Importer.getToContext(), DC,
4536 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004537 ToLocation, ToAtStartLoc, ToCategoryNameLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004538 return ToImpl;
4539
Balazs Keri3b30d652018-10-19 13:32:20 +00004540 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004541 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004542 Category->setImplementation(ToImpl);
4543 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004544
Gabor Marton26f72a92018-07-12 09:42:05 +00004545 Importer.MapImported(D, ToImpl);
Balazs Keri3b30d652018-10-19 13:32:20 +00004546 if (Error Err = ImportDeclContext(D))
4547 return std::move(Err);
4548
Douglas Gregor4da9d682010-12-07 15:32:12 +00004549 return ToImpl;
4550}
4551
Balazs Keri3b30d652018-10-19 13:32:20 +00004552ExpectedDecl
4553ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00004554 // Find the corresponding interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004555 ObjCInterfaceDecl *Iface;
4556 if (Error Err = importInto(Iface, D->getClassInterface()))
4557 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004558
4559 // Import the superclass, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00004560 ObjCInterfaceDecl *Super;
4561 if (Error Err = importInto(Super, D->getSuperClass()))
4562 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004563
4564 ObjCImplementationDecl *Impl = Iface->getImplementation();
4565 if (!Impl) {
4566 // We haven't imported an implementation yet. Create a new @implementation
4567 // now.
Balazs Keri3b30d652018-10-19 13:32:20 +00004568 DeclContext *DC, *LexicalDC;
4569 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4570 return std::move(Err);
4571
4572 SourceLocation ToLocation, ToAtStartLoc, ToSuperClassLoc;
4573 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4574 if (auto Imp = importSeq(
4575 D->getLocation(), D->getAtStartLoc(), D->getSuperClassLoc(),
4576 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4577 std::tie(
4578 ToLocation, ToAtStartLoc, ToSuperClassLoc,
4579 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4580 else
4581 return Imp.takeError();
4582
Gabor Marton26f72a92018-07-12 09:42:05 +00004583 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004584 DC, Iface, Super,
4585 ToLocation,
4586 ToAtStartLoc,
4587 ToSuperClassLoc,
4588 ToIvarLBraceLoc,
4589 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004590 return Impl;
4591
Balazs Keri3b30d652018-10-19 13:32:20 +00004592 Impl->setLexicalDeclContext(LexicalDC);
Gabor Marton26f72a92018-07-12 09:42:05 +00004593
Douglas Gregorda8025c2010-12-07 01:26:03 +00004594 // Associate the implementation with the class it implements.
4595 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004596 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004597 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004598 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004599
4600 // Verify that the existing @implementation has the same superclass.
4601 if ((Super && !Impl->getSuperClass()) ||
4602 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004603 (Super && Impl->getSuperClass() &&
4604 !declaresSameEntity(Super->getCanonicalDecl(),
4605 Impl->getSuperClass()))) {
4606 Importer.ToDiag(Impl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004607 diag::warn_odr_objc_superclass_inconsistent)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004608 << Iface->getDeclName();
4609 // FIXME: It would be nice to have the location of the superclass
4610 // below.
4611 if (Impl->getSuperClass())
4612 Importer.ToDiag(Impl->getLocation(),
4613 diag::note_odr_objc_superclass)
4614 << Impl->getSuperClass()->getDeclName();
4615 else
4616 Importer.ToDiag(Impl->getLocation(),
4617 diag::note_odr_objc_missing_superclass);
4618 if (D->getSuperClass())
4619 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004620 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004621 << D->getSuperClass()->getDeclName();
4622 else
4623 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004624 diag::note_odr_objc_missing_superclass);
Balazs Keri3b30d652018-10-19 13:32:20 +00004625
4626 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004627 }
4628 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004629
Douglas Gregorda8025c2010-12-07 01:26:03 +00004630 // Import all of the members of this @implementation.
Balazs Keri3b30d652018-10-19 13:32:20 +00004631 if (Error Err = ImportDeclContext(D))
4632 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004633
4634 return Impl;
4635}
4636
Balazs Keri3b30d652018-10-19 13:32:20 +00004637ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004638 // Import the major distinguishing characteristics of an @property.
4639 DeclContext *DC, *LexicalDC;
4640 DeclarationName Name;
4641 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004642 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004643 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4644 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004645 if (ToD)
4646 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004647
4648 // Check whether we have already imported this property.
Gabor Marton54058b52018-12-17 13:53:12 +00004649 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004650 for (auto *FoundDecl : FoundDecls) {
4651 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004652 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004653 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004654 FoundProp->getType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00004655 Importer.ToDiag(Loc, diag::warn_odr_objc_property_type_inconsistent)
Douglas Gregora11c4582010-02-17 18:02:10 +00004656 << Name << D->getType() << FoundProp->getType();
4657 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4658 << FoundProp->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004659
4660 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora11c4582010-02-17 18:02:10 +00004661 }
4662
4663 // FIXME: Check property attributes, getters, setters, etc.?
4664
4665 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004666 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004667 return FoundProp;
4668 }
4669 }
4670
Balazs Keri3b30d652018-10-19 13:32:20 +00004671 QualType ToType;
4672 TypeSourceInfo *ToTypeSourceInfo;
4673 SourceLocation ToAtLoc, ToLParenLoc;
4674 if (auto Imp = importSeq(
4675 D->getType(), D->getTypeSourceInfo(), D->getAtLoc(), D->getLParenLoc()))
4676 std::tie(ToType, ToTypeSourceInfo, ToAtLoc, ToLParenLoc) = *Imp;
4677 else
4678 return Imp.takeError();
Douglas Gregora11c4582010-02-17 18:02:10 +00004679
4680 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004681 ObjCPropertyDecl *ToProperty;
4682 if (GetImportedOrCreateDecl(
4683 ToProperty, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004684 Name.getAsIdentifierInfo(), ToAtLoc,
4685 ToLParenLoc, ToType,
4686 ToTypeSourceInfo, D->getPropertyImplementation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00004687 return ToProperty;
4688
Balazs Keri3b30d652018-10-19 13:32:20 +00004689 Selector ToGetterName, ToSetterName;
4690 SourceLocation ToGetterNameLoc, ToSetterNameLoc;
4691 ObjCMethodDecl *ToGetterMethodDecl, *ToSetterMethodDecl;
4692 ObjCIvarDecl *ToPropertyIvarDecl;
4693 if (auto Imp = importSeq(
4694 D->getGetterName(), D->getSetterName(),
4695 D->getGetterNameLoc(), D->getSetterNameLoc(),
4696 D->getGetterMethodDecl(), D->getSetterMethodDecl(),
4697 D->getPropertyIvarDecl()))
4698 std::tie(
4699 ToGetterName, ToSetterName,
4700 ToGetterNameLoc, ToSetterNameLoc,
4701 ToGetterMethodDecl, ToSetterMethodDecl,
4702 ToPropertyIvarDecl) = *Imp;
4703 else
4704 return Imp.takeError();
4705
Douglas Gregora11c4582010-02-17 18:02:10 +00004706 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004707 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004708
4709 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004710 ToProperty->setPropertyAttributesAsWritten(
4711 D->getPropertyAttributesAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00004712 ToProperty->setGetterName(ToGetterName, ToGetterNameLoc);
4713 ToProperty->setSetterName(ToSetterName, ToSetterNameLoc);
4714 ToProperty->setGetterMethodDecl(ToGetterMethodDecl);
4715 ToProperty->setSetterMethodDecl(ToSetterMethodDecl);
4716 ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl);
Douglas Gregora11c4582010-02-17 18:02:10 +00004717 return ToProperty;
4718}
4719
Balazs Keri3b30d652018-10-19 13:32:20 +00004720ExpectedDecl
4721ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4722 ObjCPropertyDecl *Property;
4723 if (Error Err = importInto(Property, D->getPropertyDecl()))
4724 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004725
Balazs Keri3b30d652018-10-19 13:32:20 +00004726 DeclContext *DC, *LexicalDC;
4727 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4728 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004729
Balazs Keri3b30d652018-10-19 13:32:20 +00004730 auto *InImpl = cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004731
4732 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004733 ObjCIvarDecl *Ivar = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004734 if (Error Err = importInto(Ivar, D->getPropertyIvarDecl()))
4735 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004736
4737 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004738 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4739 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004740 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004741 SourceLocation ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc;
4742 if (auto Imp = importSeq(
4743 D->getBeginLoc(), D->getLocation(), D->getPropertyIvarDeclLoc()))
4744 std::tie(ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc) = *Imp;
4745 else
4746 return Imp.takeError();
4747
Gabor Marton26f72a92018-07-12 09:42:05 +00004748 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004749 ToBeginLoc,
4750 ToLocation, Property,
Gabor Marton26f72a92018-07-12 09:42:05 +00004751 D->getPropertyImplementation(), Ivar,
Balazs Keri3b30d652018-10-19 13:32:20 +00004752 ToPropertyIvarDeclLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004753 return ToImpl;
4754
Douglas Gregor14a49e22010-12-07 18:32:03 +00004755 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004756 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004757 } else {
4758 // Check that we have the same kind of property implementation (@synthesize
4759 // vs. @dynamic).
4760 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004761 Importer.ToDiag(ToImpl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004762 diag::warn_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00004763 << Property->getDeclName()
4764 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00004765 == ObjCPropertyImplDecl::Dynamic);
4766 Importer.FromDiag(D->getLocation(),
4767 diag::note_odr_objc_property_impl_kind)
4768 << D->getPropertyDecl()->getDeclName()
4769 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Balazs Keri3b30d652018-10-19 13:32:20 +00004770
4771 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004772 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004773
4774 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00004775 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4776 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004777 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004778 diag::warn_odr_objc_synthesize_ivar_inconsistent)
Douglas Gregor14a49e22010-12-07 18:32:03 +00004779 << Property->getDeclName()
4780 << ToImpl->getPropertyIvarDecl()->getDeclName()
4781 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00004782 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004783 diag::note_odr_objc_synthesize_ivar_here)
4784 << D->getPropertyIvarDecl()->getDeclName();
Balazs Keri3b30d652018-10-19 13:32:20 +00004785
4786 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004787 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004788
Douglas Gregor14a49e22010-12-07 18:32:03 +00004789 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004790 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004791 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004792
Douglas Gregor14a49e22010-12-07 18:32:03 +00004793 return ToImpl;
4794}
4795
Balazs Keri3b30d652018-10-19 13:32:20 +00004796ExpectedDecl
4797ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregora082a492010-11-30 19:14:50 +00004798 // For template arguments, we adopt the translation unit as our declaration
4799 // context. This context will be fixed when the actual template declaration
4800 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00004801
Douglas Gregora082a492010-11-30 19:14:50 +00004802 // FIXME: Import default argument.
Balazs Keri3b30d652018-10-19 13:32:20 +00004803
4804 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
4805 if (!BeginLocOrErr)
4806 return BeginLocOrErr.takeError();
4807
4808 ExpectedSLoc LocationOrErr = import(D->getLocation());
4809 if (!LocationOrErr)
4810 return LocationOrErr.takeError();
4811
Gabor Marton26f72a92018-07-12 09:42:05 +00004812 TemplateTypeParmDecl *ToD = nullptr;
4813 (void)GetImportedOrCreateDecl(
4814 ToD, D, Importer.getToContext(),
4815 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004816 *BeginLocOrErr, *LocationOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004817 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4818 D->wasDeclaredWithTypename(), D->isParameterPack());
4819 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004820}
4821
Balazs Keri3b30d652018-10-19 13:32:20 +00004822ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004823ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004824 DeclarationName ToDeclName;
4825 SourceLocation ToLocation, ToInnerLocStart;
4826 QualType ToType;
4827 TypeSourceInfo *ToTypeSourceInfo;
4828 if (auto Imp = importSeq(
4829 D->getDeclName(), D->getLocation(), D->getType(), D->getTypeSourceInfo(),
4830 D->getInnerLocStart()))
4831 std::tie(
4832 ToDeclName, ToLocation, ToType, ToTypeSourceInfo,
4833 ToInnerLocStart) = *Imp;
4834 else
4835 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004836
Douglas Gregora082a492010-11-30 19:14:50 +00004837 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004838
4839 NonTypeTemplateParmDecl *ToD = nullptr;
4840 (void)GetImportedOrCreateDecl(
4841 ToD, D, Importer.getToContext(),
4842 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004843 ToInnerLocStart, ToLocation, D->getDepth(),
4844 D->getPosition(), ToDeclName.getAsIdentifierInfo(), ToType,
4845 D->isParameterPack(), ToTypeSourceInfo);
Gabor Marton26f72a92018-07-12 09:42:05 +00004846 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004847}
4848
Balazs Keri3b30d652018-10-19 13:32:20 +00004849ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004850ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4851 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004852 auto NameOrErr = import(D->getDeclName());
4853 if (!NameOrErr)
4854 return NameOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004855
Douglas Gregora082a492010-11-30 19:14:50 +00004856 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004857 ExpectedSLoc LocationOrErr = import(D->getLocation());
4858 if (!LocationOrErr)
4859 return LocationOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004860
Douglas Gregora082a492010-11-30 19:14:50 +00004861 // Import template parameters.
Balazs Keridec09162019-03-20 15:42:42 +00004862 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00004863 if (!TemplateParamsOrErr)
4864 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004865
Douglas Gregora082a492010-11-30 19:14:50 +00004866 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004867
4868 TemplateTemplateParmDecl *ToD = nullptr;
4869 (void)GetImportedOrCreateDecl(
4870 ToD, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004871 Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr,
4872 D->getDepth(), D->getPosition(), D->isParameterPack(),
4873 (*NameOrErr).getAsIdentifierInfo(),
4874 *TemplateParamsOrErr);
Gabor Marton26f72a92018-07-12 09:42:05 +00004875 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004876}
4877
Gabor Marton16d98c22019-03-07 13:01:51 +00004878// Returns the definition for a (forward) declaration of a TemplateDecl, if
Gabor Marton9581c332018-05-23 13:53:36 +00004879// it has any definition in the redecl chain.
Gabor Marton16d98c22019-03-07 13:01:51 +00004880template <typename T> static auto getTemplateDefinition(T *D) -> T * {
4881 assert(D->getTemplatedDecl() && "Should be called on templates only");
4882 auto *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
Gabor Marton9581c332018-05-23 13:53:36 +00004883 if (!ToTemplatedDef)
4884 return nullptr;
Gabor Marton16d98c22019-03-07 13:01:51 +00004885 auto *TemplateWithDef = ToTemplatedDef->getDescribedTemplate();
4886 return cast_or_null<T>(TemplateWithDef);
Gabor Marton9581c332018-05-23 13:53:36 +00004887}
4888
Balazs Keri3b30d652018-10-19 13:32:20 +00004889ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00004890 bool IsFriend = D->getFriendObjectKind() != Decl::FOK_None;
4891
Douglas Gregora082a492010-11-30 19:14:50 +00004892 // Import the major distinguishing characteristics of this class template.
4893 DeclContext *DC, *LexicalDC;
4894 DeclarationName Name;
4895 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004896 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004897 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4898 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004899 if (ToD)
4900 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004901
Gabor Marton7df342a2018-12-17 12:42:12 +00004902 ClassTemplateDecl *FoundByLookup = nullptr;
4903
Douglas Gregora082a492010-11-30 19:14:50 +00004904 // We may already have a template of the same name; try to find and match it.
4905 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004906 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00004907 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004908 for (auto *FoundDecl : FoundDecls) {
Gabor Marton7df342a2018-12-17 12:42:12 +00004909 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary |
4910 Decl::IDNS_TagFriend))
Douglas Gregora082a492010-11-30 19:14:50 +00004911 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00004912
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004913 Decl *Found = FoundDecl;
Gabor Marton7df342a2018-12-17 12:42:12 +00004914 auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found);
4915 if (FoundTemplate) {
Gabor Marton9581c332018-05-23 13:53:36 +00004916
Douglas Gregora082a492010-11-30 19:14:50 +00004917 if (IsStructuralMatch(D, FoundTemplate)) {
Gabor Marton16d98c22019-03-07 13:01:51 +00004918 ClassTemplateDecl *TemplateWithDef =
4919 getTemplateDefinition(FoundTemplate);
Gabor Marton7df342a2018-12-17 12:42:12 +00004920 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
4921 return Importer.MapImported(D, TemplateWithDef);
Balazs Keri0c23dc52018-08-13 13:08:37 +00004922 }
Gabor Marton7df342a2018-12-17 12:42:12 +00004923 FoundByLookup = FoundTemplate;
4924 break;
Gabor Marton9581c332018-05-23 13:53:36 +00004925 }
Douglas Gregora082a492010-11-30 19:14:50 +00004926 }
Gabor Marton9581c332018-05-23 13:53:36 +00004927
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004928 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00004929 }
Gabor Marton9581c332018-05-23 13:53:36 +00004930
Douglas Gregora082a492010-11-30 19:14:50 +00004931 if (!ConflictingDecls.empty()) {
4932 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00004933 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00004934 ConflictingDecls.size());
4935 }
Gabor Marton9581c332018-05-23 13:53:36 +00004936
Douglas Gregora082a492010-11-30 19:14:50 +00004937 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00004938 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora082a492010-11-30 19:14:50 +00004939 }
4940
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004941 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4942
Douglas Gregora082a492010-11-30 19:14:50 +00004943 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00004944 CXXRecordDecl *ToTemplated;
4945 if (Error Err = importInto(ToTemplated, FromTemplated))
4946 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004947
Douglas Gregora082a492010-11-30 19:14:50 +00004948 // Create the class template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00004949 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00004950 if (!TemplateParamsOrErr)
4951 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004952
Gabor Marton26f72a92018-07-12 09:42:05 +00004953 ClassTemplateDecl *D2;
4954 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00004955 *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00004956 return D2;
4957
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004958 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00004959
Douglas Gregora082a492010-11-30 19:14:50 +00004960 D2->setAccess(D->getAccess());
4961 D2->setLexicalDeclContext(LexicalDC);
Gabor Marton7df342a2018-12-17 12:42:12 +00004962
4963 if (D->getDeclContext()->containsDeclAndLoad(D))
4964 DC->addDeclInternal(D2);
4965 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Balazs Keri0c23dc52018-08-13 13:08:37 +00004966 LexicalDC->addDeclInternal(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00004967
Gabor Marton7df342a2018-12-17 12:42:12 +00004968 if (FoundByLookup) {
4969 auto *Recent =
4970 const_cast<ClassTemplateDecl *>(FoundByLookup->getMostRecentDecl());
4971
4972 // It is possible that during the import of the class template definition
4973 // we start the import of a fwd friend decl of the very same class template
4974 // and we add the fwd friend decl to the lookup table. But the ToTemplated
4975 // had been created earlier and by that time the lookup could not find
4976 // anything existing, so it has no previous decl. Later, (still during the
4977 // import of the fwd friend decl) we start to import the definition again
4978 // and this time the lookup finds the previous fwd friend class template.
4979 // In this case we must set up the previous decl for the templated decl.
4980 if (!ToTemplated->getPreviousDecl()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00004981 assert(FoundByLookup->getTemplatedDecl() &&
4982 "Found decl must have its templated decl set");
Gabor Marton7df342a2018-12-17 12:42:12 +00004983 CXXRecordDecl *PrevTemplated =
4984 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
4985 if (ToTemplated != PrevTemplated)
4986 ToTemplated->setPreviousDecl(PrevTemplated);
4987 }
4988
4989 D2->setPreviousDecl(Recent);
4990 }
4991
4992 if (LexicalDC != DC && IsFriend)
4993 DC->makeDeclVisibleInContext(D2);
4994
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004995 if (FromTemplated->isCompleteDefinition() &&
4996 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004997 // FIXME: Import definition!
4998 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004999
Douglas Gregora082a492010-11-30 19:14:50 +00005000 return D2;
5001}
5002
Balazs Keri3b30d652018-10-19 13:32:20 +00005003ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +00005004 ClassTemplateSpecializationDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005005 ClassTemplateDecl *ClassTemplate;
5006 if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate()))
5007 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005008
Douglas Gregore2e50d332010-12-01 01:36:18 +00005009 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005010 DeclContext *DC, *LexicalDC;
5011 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5012 return std::move(Err);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005013
5014 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005015 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005016 if (Error Err = ImportTemplateArguments(
5017 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5018 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005019
Douglas Gregore2e50d332010-12-01 01:36:18 +00005020 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005021 void *InsertPos = nullptr;
Gabor Marton7f8c4002019-03-19 13:34:10 +00005022 ClassTemplateSpecializationDecl *PrevDecl = nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005023 ClassTemplatePartialSpecializationDecl *PartialSpec =
5024 dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
5025 if (PartialSpec)
Gabor Marton7f8c4002019-03-19 13:34:10 +00005026 PrevDecl =
5027 ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005028 else
Gabor Marton7f8c4002019-03-19 13:34:10 +00005029 PrevDecl = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005030
Gabor Marton7f8c4002019-03-19 13:34:10 +00005031 if (PrevDecl) {
5032 if (IsStructuralMatch(D, PrevDecl)) {
5033 if (D->isThisDeclarationADefinition() && PrevDecl->getDefinition()) {
5034 Importer.MapImported(D, PrevDecl->getDefinition());
5035 // Import those default field initializers which have been
5036 // instantiated in the "From" context, but not in the "To" context.
5037 for (auto *FromField : D->fields())
5038 Importer.Import(FromField);
Gabor Marton42e15de2018-08-22 11:52:14 +00005039
Gabor Marton7f8c4002019-03-19 13:34:10 +00005040 // Import those methods which have been instantiated in the
5041 // "From" context, but not in the "To" context.
5042 for (CXXMethodDecl *FromM : D->methods())
5043 Importer.Import(FromM);
Gabor Marton42e15de2018-08-22 11:52:14 +00005044
Gabor Marton7f8c4002019-03-19 13:34:10 +00005045 // TODO Import instantiated default arguments.
5046 // TODO Import instantiated exception specifications.
5047 //
5048 // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint
5049 // what else could be fused during an AST merge.
5050 return PrevDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005051 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005052 } else { // ODR violation.
5053 // FIXME HandleNameConflict
5054 return nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005055 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005056 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005057
Gabor Marton7f8c4002019-03-19 13:34:10 +00005058 // Import the location of this declaration.
5059 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5060 if (!BeginLocOrErr)
5061 return BeginLocOrErr.takeError();
5062 ExpectedSLoc IdLocOrErr = import(D->getLocation());
5063 if (!IdLocOrErr)
5064 return IdLocOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +00005065
Gabor Marton7f8c4002019-03-19 13:34:10 +00005066 // Create the specialization.
5067 ClassTemplateSpecializationDecl *D2 = nullptr;
5068 if (PartialSpec) {
5069 // Import TemplateArgumentListInfo.
5070 TemplateArgumentListInfo ToTAInfo;
5071 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
5072 if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
5073 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005074
Gabor Marton7f8c4002019-03-19 13:34:10 +00005075 QualType CanonInjType;
5076 if (Error Err = importInto(
5077 CanonInjType, PartialSpec->getInjectedSpecializationType()))
5078 return std::move(Err);
5079 CanonInjType = CanonInjType.getCanonicalType();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005080
Balazs Keridec09162019-03-20 15:42:42 +00005081 auto ToTPListOrErr = import(PartialSpec->getTemplateParameters());
Gabor Marton7f8c4002019-03-19 13:34:10 +00005082 if (!ToTPListOrErr)
5083 return ToTPListOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005084
Gabor Marton7f8c4002019-03-19 13:34:10 +00005085 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
5086 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5087 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, ClassTemplate,
5088 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
5089 ToTAInfo, CanonInjType,
5090 cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl)))
5091 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005092
Gabor Marton7f8c4002019-03-19 13:34:10 +00005093 // Update InsertPos, because preceding import calls may have invalidated
5094 // it by adding new specializations.
5095 if (!ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos))
5096 // Add this partial specialization to the class template.
5097 ClassTemplate->AddPartialSpecialization(
5098 cast<ClassTemplatePartialSpecializationDecl>(D2), InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005099
Gabor Marton7f8c4002019-03-19 13:34:10 +00005100 } else { // Not a partial specialization.
5101 if (GetImportedOrCreateDecl(
5102 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5103 *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs,
5104 PrevDecl))
5105 return D2;
Gabor Marton42e15de2018-08-22 11:52:14 +00005106
Gabor Marton7f8c4002019-03-19 13:34:10 +00005107 // Update InsertPos, because preceding import calls may have invalidated
5108 // it by adding new specializations.
5109 if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos))
5110 // Add this specialization to the class template.
5111 ClassTemplate->AddSpecialization(D2, InsertPos);
5112 }
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005113
Gabor Marton7f8c4002019-03-19 13:34:10 +00005114 D2->setSpecializationKind(D->getSpecializationKind());
Douglas Gregore2e50d332010-12-01 01:36:18 +00005115
Gabor Marton7f8c4002019-03-19 13:34:10 +00005116 // Set the context of this specialization/instantiation.
5117 D2->setLexicalDeclContext(LexicalDC);
5118
5119 // Add to the DC only if it was an explicit specialization/instantiation.
5120 if (D2->isExplicitInstantiationOrSpecialization()) {
5121 LexicalDC->addDeclInternal(D2);
5122 }
5123
5124 // Import the qualifier, if any.
5125 if (auto LocOrErr = import(D->getQualifierLoc()))
5126 D2->setQualifierInfo(*LocOrErr);
5127 else
5128 return LocOrErr.takeError();
5129
5130 if (auto *TSI = D->getTypeAsWritten()) {
5131 if (auto TInfoOrErr = import(TSI))
5132 D2->setTypeAsWritten(*TInfoOrErr);
5133 else
5134 return TInfoOrErr.takeError();
5135
5136 if (auto LocOrErr = import(D->getTemplateKeywordLoc()))
5137 D2->setTemplateKeywordLoc(*LocOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +00005138 else
5139 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005140
Gabor Marton7f8c4002019-03-19 13:34:10 +00005141 if (auto LocOrErr = import(D->getExternLoc()))
5142 D2->setExternLoc(*LocOrErr);
5143 else
5144 return LocOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00005145 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005146
5147 if (D->getPointOfInstantiation().isValid()) {
5148 if (auto POIOrErr = import(D->getPointOfInstantiation()))
5149 D2->setPointOfInstantiation(*POIOrErr);
5150 else
5151 return POIOrErr.takeError();
5152 }
5153
5154 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
5155
Balazs Keri3b30d652018-10-19 13:32:20 +00005156 if (D->isCompleteDefinition())
5157 if (Error Err = ImportDefinition(D, D2))
5158 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005159
Douglas Gregore2e50d332010-12-01 01:36:18 +00005160 return D2;
5161}
5162
Balazs Keri3b30d652018-10-19 13:32:20 +00005163ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005164 // If this variable has a definition in the translation unit we're coming
5165 // from,
5166 // but this particular declaration is not that definition, import the
5167 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005168 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00005169 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
5170 if (Definition && Definition != D->getTemplatedDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005171 if (ExpectedDecl ImportedDefOrErr = import(
5172 Definition->getDescribedVarTemplate()))
5173 return Importer.MapImported(D, *ImportedDefOrErr);
5174 else
5175 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005176 }
5177
5178 // Import the major distinguishing characteristics of this variable template.
5179 DeclContext *DC, *LexicalDC;
5180 DeclarationName Name;
5181 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005182 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005183 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5184 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005185 if (ToD)
5186 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005187
5188 // We may already have a template of the same name; try to find and match it.
5189 assert(!DC->isFunctionOrMethod() &&
5190 "Variable templates cannot be declared at function scope");
5191 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00005192 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005193 for (auto *FoundDecl : FoundDecls) {
5194 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00005195 continue;
5196
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005197 Decl *Found = FoundDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005198 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005199 if (IsStructuralMatch(D, FoundTemplate)) {
5200 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00005201 Importer.MapImported(D->getTemplatedDecl(),
5202 FoundTemplate->getTemplatedDecl());
5203 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005204 }
5205 }
5206
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005207 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005208 }
5209
5210 if (!ConflictingDecls.empty()) {
5211 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
5212 ConflictingDecls.data(),
5213 ConflictingDecls.size());
5214 }
5215
5216 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005217 // FIXME: Is it possible to get other error than name conflict?
5218 // (Put this `if` into the previous `if`?)
5219 return make_error<ImportError>(ImportError::NameConflict);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005220
5221 VarDecl *DTemplated = D->getTemplatedDecl();
5222
5223 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005224 // FIXME: Value not used?
5225 ExpectedType TypeOrErr = import(DTemplated->getType());
5226 if (!TypeOrErr)
5227 return TypeOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005228
5229 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005230 VarDecl *ToTemplated;
5231 if (Error Err = importInto(ToTemplated, DTemplated))
5232 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005233
5234 // Create the variable template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005235 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005236 if (!TemplateParamsOrErr)
5237 return TemplateParamsOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005238
Gabor Marton26f72a92018-07-12 09:42:05 +00005239 VarTemplateDecl *ToVarTD;
5240 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00005241 Name, *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005242 return ToVarTD;
5243
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005244 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005245
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005246 ToVarTD->setAccess(D->getAccess());
5247 ToVarTD->setLexicalDeclContext(LexicalDC);
5248 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005249
Larisse Voufo39a1e502013-08-06 01:03:05 +00005250 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005251 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005252 // FIXME: Import definition!
5253 }
5254
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005255 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005256}
5257
Balazs Keri3b30d652018-10-19 13:32:20 +00005258ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00005259 VarTemplateSpecializationDecl *D) {
5260 // If this record has a definition in the translation unit we're coming from,
5261 // but this particular declaration is not that definition, import the
5262 // definition and map to that.
5263 VarDecl *Definition = D->getDefinition();
5264 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005265 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5266 return Importer.MapImported(D, *ImportedDefOrErr);
5267 else
5268 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005269 }
5270
Balazs Keri3b30d652018-10-19 13:32:20 +00005271 VarTemplateDecl *VarTemplate;
5272 if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate()))
5273 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005274
5275 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005276 DeclContext *DC, *LexicalDC;
5277 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5278 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005279
5280 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005281 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5282 if (!BeginLocOrErr)
5283 return BeginLocOrErr.takeError();
5284
5285 auto IdLocOrErr = import(D->getLocation());
5286 if (!IdLocOrErr)
5287 return IdLocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005288
5289 // Import template arguments.
5290 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005291 if (Error Err = ImportTemplateArguments(
5292 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5293 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005294
5295 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005296 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005297 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005298 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005299 if (D2) {
5300 // We already have a variable template specialization with these template
5301 // arguments.
5302
5303 // FIXME: Check for specialization vs. instantiation errors.
5304
5305 if (VarDecl *FoundDef = D2->getDefinition()) {
5306 if (!D->isThisDeclarationADefinition() ||
5307 IsStructuralMatch(D, FoundDef)) {
5308 // The record types structurally match, or the "from" translation
5309 // unit only had a forward declaration anyway; call it the same
5310 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00005311 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005312 }
5313 }
5314 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005315 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005316 QualType T;
5317 if (Error Err = importInto(T, D->getType()))
5318 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005319
Balazs Keri3b30d652018-10-19 13:32:20 +00005320 auto TInfoOrErr = import(D->getTypeSourceInfo());
5321 if (!TInfoOrErr)
5322 return TInfoOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005323
5324 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00005325 if (Error Err = ImportTemplateArgumentListInfo(
5326 D->getTemplateArgsInfo(), ToTAInfo))
5327 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005328
5329 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005330 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005331 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
5332 // Import TemplateArgumentListInfo
5333 TemplateArgumentListInfo ArgInfos;
5334 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
5335 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
Balazs Keri3b30d652018-10-19 13:32:20 +00005336 if (Error Err = ImportTemplateArgumentListInfo(
5337 *FromTAArgsAsWritten, ArgInfos))
5338 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005339
Balazs Keridec09162019-03-20 15:42:42 +00005340 auto ToTPListOrErr = import(FromPartial->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005341 if (!ToTPListOrErr)
5342 return ToTPListOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005343
Gabor Marton26f72a92018-07-12 09:42:05 +00005344 PartVarSpecDecl *ToPartial;
5345 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005346 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
5347 VarTemplate, T, *TInfoOrErr,
5348 D->getStorageClass(), TemplateArgs, ArgInfos))
Gabor Marton26f72a92018-07-12 09:42:05 +00005349 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005350
Balazs Keri3b30d652018-10-19 13:32:20 +00005351 if (Expected<PartVarSpecDecl *> ToInstOrErr = import(
5352 FromPartial->getInstantiatedFromMember()))
5353 ToPartial->setInstantiatedFromMember(*ToInstOrErr);
5354 else
5355 return ToInstOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005356
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005357 if (FromPartial->isMemberSpecialization())
5358 ToPartial->setMemberSpecialization();
5359
5360 D2 = ToPartial;
Balazs Keri3b30d652018-10-19 13:32:20 +00005361
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005362 } else { // Full specialization
Balazs Keri3b30d652018-10-19 13:32:20 +00005363 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
5364 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
5365 T, *TInfoOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005366 D->getStorageClass(), TemplateArgs))
5367 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005368 }
5369
Balazs Keri3b30d652018-10-19 13:32:20 +00005370 if (D->getPointOfInstantiation().isValid()) {
5371 if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation()))
5372 D2->setPointOfInstantiation(*POIOrErr);
5373 else
5374 return POIOrErr.takeError();
5375 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005376
Larisse Voufo39a1e502013-08-06 01:03:05 +00005377 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005378 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005379
5380 // Add this specialization to the class template.
5381 VarTemplate->AddSpecialization(D2, InsertPos);
5382
5383 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005384 if (auto LocOrErr = import(D->getQualifierLoc()))
5385 D2->setQualifierInfo(*LocOrErr);
5386 else
5387 return LocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005388
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005389 if (D->isConstexpr())
5390 D2->setConstexpr(true);
5391
Larisse Voufo39a1e502013-08-06 01:03:05 +00005392 // Add the specialization to this context.
5393 D2->setLexicalDeclContext(LexicalDC);
5394 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005395
5396 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00005397 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005398
Balazs Keri3b30d652018-10-19 13:32:20 +00005399 if (Error Err = ImportInitializer(D, D2))
5400 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005401
5402 return D2;
5403}
5404
Balazs Keri3b30d652018-10-19 13:32:20 +00005405ExpectedDecl
5406ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005407 DeclContext *DC, *LexicalDC;
5408 DeclarationName Name;
5409 SourceLocation Loc;
5410 NamedDecl *ToD;
5411
Balazs Keri3b30d652018-10-19 13:32:20 +00005412 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5413 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005414
5415 if (ToD)
5416 return ToD;
5417
Gabor Marton16d98c22019-03-07 13:01:51 +00005418 const FunctionTemplateDecl *FoundByLookup = nullptr;
5419
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005420 // Try to find a function in our own ("to") context with the same name, same
5421 // type, and in the same context as the function we're importing.
Gabor Marton16d98c22019-03-07 13:01:51 +00005422 // FIXME Split this into a separate function.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005423 if (!LexicalDC->isFunctionOrMethod()) {
Gabor Martone331e632019-02-18 13:09:27 +00005424 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00005425 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005426 for (auto *FoundDecl : FoundDecls) {
5427 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005428 continue;
5429
Gabor Marton16d98c22019-03-07 13:01:51 +00005430 if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
5431 if (FoundTemplate->hasExternalFormalLinkage() &&
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005432 D->hasExternalFormalLinkage()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005433 if (IsStructuralMatch(D, FoundTemplate)) {
5434 FunctionTemplateDecl *TemplateWithDef =
5435 getTemplateDefinition(FoundTemplate);
5436 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
5437 return Importer.MapImported(D, TemplateWithDef);
5438 }
5439 FoundByLookup = FoundTemplate;
5440 break;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005441 }
Gabor Marton16d98c22019-03-07 13:01:51 +00005442 // TODO: handle conflicting names
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005443 }
5444 }
5445 }
5446 }
5447
Balazs Keridec09162019-03-20 15:42:42 +00005448 auto ParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005449 if (!ParamsOrErr)
5450 return ParamsOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005451
Balazs Keri3b30d652018-10-19 13:32:20 +00005452 FunctionDecl *TemplatedFD;
5453 if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl()))
5454 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005455
Gabor Marton26f72a92018-07-12 09:42:05 +00005456 FunctionTemplateDecl *ToFunc;
5457 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005458 *ParamsOrErr, TemplatedFD))
Gabor Marton26f72a92018-07-12 09:42:05 +00005459 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005460
5461 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005462
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005463 ToFunc->setAccess(D->getAccess());
5464 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005465 LexicalDC->addDeclInternal(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005466
5467 if (FoundByLookup) {
5468 auto *Recent =
5469 const_cast<FunctionTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5470 if (!TemplatedFD->getPreviousDecl()) {
5471 assert(FoundByLookup->getTemplatedDecl() &&
5472 "Found decl must have its templated decl set");
5473 auto *PrevTemplated =
5474 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5475 if (TemplatedFD != PrevTemplated)
5476 TemplatedFD->setPreviousDecl(PrevTemplated);
5477 }
5478 ToFunc->setPreviousDecl(Recent);
5479 }
5480
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005481 return ToFunc;
5482}
5483
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005484//----------------------------------------------------------------------------
5485// Import Statements
5486//----------------------------------------------------------------------------
5487
Balazs Keri3b30d652018-10-19 13:32:20 +00005488ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005489 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
5490 << S->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005491 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005492}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005493
Balazs Keri3b30d652018-10-19 13:32:20 +00005494
5495ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005496 SmallVector<IdentifierInfo *, 4> Names;
5497 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5498 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005499 // ToII is nullptr when no symbolic name is given for output operand
5500 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005501 Names.push_back(ToII);
5502 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005503
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005504 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5505 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005506 // ToII is nullptr when no symbolic name is given for input operand
5507 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005508 Names.push_back(ToII);
5509 }
5510
5511 SmallVector<StringLiteral *, 4> Clobbers;
5512 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005513 if (auto ClobberOrErr = import(S->getClobberStringLiteral(I)))
5514 Clobbers.push_back(*ClobberOrErr);
5515 else
5516 return ClobberOrErr.takeError();
5517
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005518 }
5519
5520 SmallVector<StringLiteral *, 4> Constraints;
5521 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005522 if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I)))
5523 Constraints.push_back(*OutputOrErr);
5524 else
5525 return OutputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005526 }
5527
5528 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005529 if (auto InputOrErr = import(S->getInputConstraintLiteral(I)))
5530 Constraints.push_back(*InputOrErr);
5531 else
5532 return InputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005533 }
5534
5535 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Balazs Keri3b30d652018-10-19 13:32:20 +00005536 if (Error Err = ImportContainerChecked(S->outputs(), Exprs))
5537 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005538
Balazs Keri3b30d652018-10-19 13:32:20 +00005539 if (Error Err = ImportArrayChecked(
5540 S->inputs(), Exprs.begin() + S->getNumOutputs()))
5541 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005542
Balazs Keri3b30d652018-10-19 13:32:20 +00005543 ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc());
5544 if (!AsmLocOrErr)
5545 return AsmLocOrErr.takeError();
5546 auto AsmStrOrErr = import(S->getAsmString());
5547 if (!AsmStrOrErr)
5548 return AsmStrOrErr.takeError();
5549 ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc());
5550 if (!RParenLocOrErr)
5551 return RParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005552
5553 return new (Importer.getToContext()) GCCAsmStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005554 Importer.getToContext(),
5555 *AsmLocOrErr,
5556 S->isSimple(),
5557 S->isVolatile(),
5558 S->getNumOutputs(),
5559 S->getNumInputs(),
5560 Names.data(),
5561 Constraints.data(),
5562 Exprs.data(),
5563 *AsmStrOrErr,
5564 S->getNumClobbers(),
5565 Clobbers.data(),
5566 *RParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005567}
5568
Balazs Keri3b30d652018-10-19 13:32:20 +00005569ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5570 auto Imp = importSeq(S->getDeclGroup(), S->getBeginLoc(), S->getEndLoc());
5571 if (!Imp)
5572 return Imp.takeError();
5573
5574 DeclGroupRef ToDG;
5575 SourceLocation ToBeginLoc, ToEndLoc;
5576 std::tie(ToDG, ToBeginLoc, ToEndLoc) = *Imp;
5577
5578 return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005579}
5580
Balazs Keri3b30d652018-10-19 13:32:20 +00005581ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5582 ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc());
5583 if (!ToSemiLocOrErr)
5584 return ToSemiLocOrErr.takeError();
5585 return new (Importer.getToContext()) NullStmt(
5586 *ToSemiLocOrErr, S->hasLeadingEmptyMacro());
Sean Callanan59721b32015-04-28 18:41:46 +00005587}
5588
Balazs Keri3b30d652018-10-19 13:32:20 +00005589ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005590 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005591
Balazs Keri3b30d652018-10-19 13:32:20 +00005592 if (Error Err = ImportContainerChecked(S->body(), ToStmts))
5593 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00005594
Balazs Keri3b30d652018-10-19 13:32:20 +00005595 ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc());
5596 if (!ToLBracLocOrErr)
5597 return ToLBracLocOrErr.takeError();
5598
5599 ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc());
5600 if (!ToRBracLocOrErr)
5601 return ToRBracLocOrErr.takeError();
5602
5603 return CompoundStmt::Create(
5604 Importer.getToContext(), ToStmts,
5605 *ToLBracLocOrErr, *ToRBracLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005606}
5607
Balazs Keri3b30d652018-10-19 13:32:20 +00005608ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5609 auto Imp = importSeq(
5610 S->getLHS(), S->getRHS(), S->getSubStmt(), S->getCaseLoc(),
5611 S->getEllipsisLoc(), S->getColonLoc());
5612 if (!Imp)
5613 return Imp.takeError();
5614
5615 Expr *ToLHS, *ToRHS;
5616 Stmt *ToSubStmt;
5617 SourceLocation ToCaseLoc, ToEllipsisLoc, ToColonLoc;
5618 std::tie(ToLHS, ToRHS, ToSubStmt, ToCaseLoc, ToEllipsisLoc, ToColonLoc) =
5619 *Imp;
5620
Bruno Ricci5b30571752018-10-28 12:30:53 +00005621 auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS,
5622 ToCaseLoc, ToEllipsisLoc, ToColonLoc);
Gabor Horvath480892b2017-10-18 09:25:18 +00005623 ToStmt->setSubStmt(ToSubStmt);
Balazs Keri3b30d652018-10-19 13:32:20 +00005624
Gabor Horvath480892b2017-10-18 09:25:18 +00005625 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005626}
5627
Balazs Keri3b30d652018-10-19 13:32:20 +00005628ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5629 auto Imp = importSeq(S->getDefaultLoc(), S->getColonLoc(), S->getSubStmt());
5630 if (!Imp)
5631 return Imp.takeError();
5632
5633 SourceLocation ToDefaultLoc, ToColonLoc;
5634 Stmt *ToSubStmt;
5635 std::tie(ToDefaultLoc, ToColonLoc, ToSubStmt) = *Imp;
5636
5637 return new (Importer.getToContext()) DefaultStmt(
5638 ToDefaultLoc, ToColonLoc, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005639}
5640
Balazs Keri3b30d652018-10-19 13:32:20 +00005641ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5642 auto Imp = importSeq(S->getIdentLoc(), S->getDecl(), S->getSubStmt());
5643 if (!Imp)
5644 return Imp.takeError();
5645
5646 SourceLocation ToIdentLoc;
5647 LabelDecl *ToLabelDecl;
5648 Stmt *ToSubStmt;
5649 std::tie(ToIdentLoc, ToLabelDecl, ToSubStmt) = *Imp;
5650
5651 return new (Importer.getToContext()) LabelStmt(
5652 ToIdentLoc, ToLabelDecl, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005653}
5654
Balazs Keri3b30d652018-10-19 13:32:20 +00005655ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5656 ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc());
5657 if (!ToAttrLocOrErr)
5658 return ToAttrLocOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005659 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5660 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00005661 if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs))
5662 return std::move(Err);
5663 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
5664 if (!ToSubStmtOrErr)
5665 return ToSubStmtOrErr.takeError();
5666
5667 return AttributedStmt::Create(
5668 Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005669}
5670
Balazs Keri3b30d652018-10-19 13:32:20 +00005671ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5672 auto Imp = importSeq(
5673 S->getIfLoc(), S->getInit(), S->getConditionVariable(), S->getCond(),
5674 S->getThen(), S->getElseLoc(), S->getElse());
5675 if (!Imp)
5676 return Imp.takeError();
5677
5678 SourceLocation ToIfLoc, ToElseLoc;
5679 Stmt *ToInit, *ToThen, *ToElse;
5680 VarDecl *ToConditionVariable;
5681 Expr *ToCond;
5682 std::tie(
5683 ToIfLoc, ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc, ToElse) =
5684 *Imp;
5685
Bruno Riccib1cc94b2018-10-27 21:12:20 +00005686 return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(),
5687 ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc,
5688 ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00005689}
5690
Balazs Keri3b30d652018-10-19 13:32:20 +00005691ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
5692 auto Imp = importSeq(
5693 S->getInit(), S->getConditionVariable(), S->getCond(),
5694 S->getBody(), S->getSwitchLoc());
5695 if (!Imp)
5696 return Imp.takeError();
5697
5698 Stmt *ToInit, *ToBody;
5699 VarDecl *ToConditionVariable;
5700 Expr *ToCond;
5701 SourceLocation ToSwitchLoc;
5702 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
5703
Bruno Riccie2806f82018-10-29 16:12:37 +00005704 auto *ToStmt = SwitchStmt::Create(Importer.getToContext(), ToInit,
5705 ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00005706 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00005707 ToStmt->setSwitchLoc(ToSwitchLoc);
5708
Sean Callanan59721b32015-04-28 18:41:46 +00005709 // Now we have to re-chain the cases.
5710 SwitchCase *LastChainedSwitchCase = nullptr;
5711 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5712 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005713 Expected<SwitchCase *> ToSCOrErr = import(SC);
5714 if (!ToSCOrErr)
5715 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005716 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00005717 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005718 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005719 ToStmt->setSwitchCaseList(*ToSCOrErr);
5720 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005721 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005722
Sean Callanan59721b32015-04-28 18:41:46 +00005723 return ToStmt;
5724}
5725
Balazs Keri3b30d652018-10-19 13:32:20 +00005726ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5727 auto Imp = importSeq(
5728 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
5729 if (!Imp)
5730 return Imp.takeError();
5731
5732 VarDecl *ToConditionVariable;
5733 Expr *ToCond;
5734 Stmt *ToBody;
5735 SourceLocation ToWhileLoc;
5736 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
5737
Bruno Riccibacf7512018-10-30 13:42:41 +00005738 return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond,
5739 ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005740}
5741
Balazs Keri3b30d652018-10-19 13:32:20 +00005742ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5743 auto Imp = importSeq(
5744 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
5745 S->getRParenLoc());
5746 if (!Imp)
5747 return Imp.takeError();
5748
5749 Stmt *ToBody;
5750 Expr *ToCond;
5751 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
5752 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
5753
5754 return new (Importer.getToContext()) DoStmt(
5755 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005756}
5757
Balazs Keri3b30d652018-10-19 13:32:20 +00005758ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
5759 auto Imp = importSeq(
5760 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
5761 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
5762 if (!Imp)
5763 return Imp.takeError();
5764
5765 Stmt *ToInit;
5766 Expr *ToCond, *ToInc;
5767 VarDecl *ToConditionVariable;
5768 Stmt *ToBody;
5769 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
5770 std::tie(
5771 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
5772 ToLParenLoc, ToRParenLoc) = *Imp;
5773
5774 return new (Importer.getToContext()) ForStmt(
5775 Importer.getToContext(),
5776 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
5777 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005778}
5779
Balazs Keri3b30d652018-10-19 13:32:20 +00005780ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5781 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
5782 if (!Imp)
5783 return Imp.takeError();
5784
5785 LabelDecl *ToLabel;
5786 SourceLocation ToGotoLoc, ToLabelLoc;
5787 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
5788
5789 return new (Importer.getToContext()) GotoStmt(
5790 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005791}
5792
Balazs Keri3b30d652018-10-19 13:32:20 +00005793ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5794 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
5795 if (!Imp)
5796 return Imp.takeError();
5797
5798 SourceLocation ToGotoLoc, ToStarLoc;
5799 Expr *ToTarget;
5800 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
5801
5802 return new (Importer.getToContext()) IndirectGotoStmt(
5803 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00005804}
5805
Balazs Keri3b30d652018-10-19 13:32:20 +00005806ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5807 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
5808 if (!ToContinueLocOrErr)
5809 return ToContinueLocOrErr.takeError();
5810 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005811}
5812
Balazs Keri3b30d652018-10-19 13:32:20 +00005813ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5814 auto ToBreakLocOrErr = import(S->getBreakLoc());
5815 if (!ToBreakLocOrErr)
5816 return ToBreakLocOrErr.takeError();
5817 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005818}
5819
Balazs Keri3b30d652018-10-19 13:32:20 +00005820ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5821 auto Imp = importSeq(
5822 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
5823 if (!Imp)
5824 return Imp.takeError();
5825
5826 SourceLocation ToReturnLoc;
5827 Expr *ToRetValue;
5828 const VarDecl *ToNRVOCandidate;
5829 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
5830
Bruno Ricci023b1d12018-10-30 14:40:49 +00005831 return ReturnStmt::Create(Importer.getToContext(), ToReturnLoc, ToRetValue,
5832 ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00005833}
5834
Balazs Keri3b30d652018-10-19 13:32:20 +00005835ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5836 auto Imp = importSeq(
5837 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
5838 if (!Imp)
5839 return Imp.takeError();
5840
5841 SourceLocation ToCatchLoc;
5842 VarDecl *ToExceptionDecl;
5843 Stmt *ToHandlerBlock;
5844 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
5845
5846 return new (Importer.getToContext()) CXXCatchStmt (
5847 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00005848}
5849
Balazs Keri3b30d652018-10-19 13:32:20 +00005850ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5851 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
5852 if (!ToTryLocOrErr)
5853 return ToTryLocOrErr.takeError();
5854
5855 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
5856 if (!ToTryBlockOrErr)
5857 return ToTryBlockOrErr.takeError();
5858
Sean Callanan59721b32015-04-28 18:41:46 +00005859 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5860 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5861 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005862 if (auto ToHandlerOrErr = import(FromHandler))
5863 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005864 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005865 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005866 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005867
5868 return CXXTryStmt::Create(
5869 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00005870}
5871
Balazs Keri3b30d652018-10-19 13:32:20 +00005872ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5873 auto Imp1 = importSeq(
5874 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
5875 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
5876 if (!Imp1)
5877 return Imp1.takeError();
5878 auto Imp2 = importSeq(
5879 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
5880 if (!Imp2)
5881 return Imp2.takeError();
5882
5883 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
5884 Expr *ToCond, *ToInc;
5885 Stmt *ToInit, *ToBody;
5886 std::tie(
5887 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5888 ToBody) = *Imp1;
5889 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
5890 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
5891
5892 return new (Importer.getToContext()) CXXForRangeStmt(
5893 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5894 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005895}
5896
Balazs Keri3b30d652018-10-19 13:32:20 +00005897ExpectedStmt
5898ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5899 auto Imp = importSeq(
5900 S->getElement(), S->getCollection(), S->getBody(),
5901 S->getForLoc(), S->getRParenLoc());
5902 if (!Imp)
5903 return Imp.takeError();
5904
5905 Stmt *ToElement, *ToBody;
5906 Expr *ToCollection;
5907 SourceLocation ToForLoc, ToRParenLoc;
5908 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
5909
5910 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
5911 ToCollection,
5912 ToBody,
5913 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00005914 ToRParenLoc);
5915}
5916
Balazs Keri3b30d652018-10-19 13:32:20 +00005917ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5918 auto Imp = importSeq(
5919 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
5920 S->getCatchBody());
5921 if (!Imp)
5922 return Imp.takeError();
5923
5924 SourceLocation ToAtCatchLoc, ToRParenLoc;
5925 VarDecl *ToCatchParamDecl;
5926 Stmt *ToCatchBody;
5927 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
5928
5929 return new (Importer.getToContext()) ObjCAtCatchStmt (
5930 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00005931}
5932
Balazs Keri3b30d652018-10-19 13:32:20 +00005933ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5934 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
5935 if (!ToAtFinallyLocOrErr)
5936 return ToAtFinallyLocOrErr.takeError();
5937 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
5938 if (!ToAtFinallyStmtOrErr)
5939 return ToAtFinallyStmtOrErr.takeError();
5940 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
5941 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005942}
5943
Balazs Keri3b30d652018-10-19 13:32:20 +00005944ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5945 auto Imp = importSeq(
5946 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
5947 if (!Imp)
5948 return Imp.takeError();
5949
5950 SourceLocation ToAtTryLoc;
5951 Stmt *ToTryBody, *ToFinallyStmt;
5952 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
5953
Sean Callanan59721b32015-04-28 18:41:46 +00005954 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5955 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5956 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005957 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
5958 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005959 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005960 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005961 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005962
Sean Callanan59721b32015-04-28 18:41:46 +00005963 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005964 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00005965 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005966 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005967}
5968
Balazs Keri3b30d652018-10-19 13:32:20 +00005969ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00005970 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005971 auto Imp = importSeq(
5972 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
5973 if (!Imp)
5974 return Imp.takeError();
5975
5976 SourceLocation ToAtSynchronizedLoc;
5977 Expr *ToSynchExpr;
5978 Stmt *ToSynchBody;
5979 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
5980
Sean Callanan59721b32015-04-28 18:41:46 +00005981 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5982 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5983}
5984
Balazs Keri3b30d652018-10-19 13:32:20 +00005985ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5986 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
5987 if (!ToThrowLocOrErr)
5988 return ToThrowLocOrErr.takeError();
5989 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
5990 if (!ToThrowExprOrErr)
5991 return ToThrowExprOrErr.takeError();
5992 return new (Importer.getToContext()) ObjCAtThrowStmt(
5993 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005994}
5995
Balazs Keri3b30d652018-10-19 13:32:20 +00005996ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
5997 ObjCAutoreleasePoolStmt *S) {
5998 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
5999 if (!ToAtLocOrErr)
6000 return ToAtLocOrErr.takeError();
6001 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6002 if (!ToSubStmtOrErr)
6003 return ToSubStmtOrErr.takeError();
6004 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6005 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006006}
6007
6008//----------------------------------------------------------------------------
6009// Import Expressions
6010//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006011ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006012 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6013 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006014 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006015}
6016
Balazs Keri3b30d652018-10-19 13:32:20 +00006017ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6018 auto Imp = importSeq(
6019 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6020 E->getRParenLoc(), E->getType());
6021 if (!Imp)
6022 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006023
Balazs Keri3b30d652018-10-19 13:32:20 +00006024 SourceLocation ToBuiltinLoc, ToRParenLoc;
6025 Expr *ToSubExpr;
6026 TypeSourceInfo *ToWrittenTypeInfo;
6027 QualType ToType;
6028 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6029 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006030
6031 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006032 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6033 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006034}
6035
Tom Roeder521f0042019-02-26 19:26:41 +00006036ExpectedStmt ASTNodeImporter::VisitChooseExpr(ChooseExpr *E) {
6037 auto Imp = importSeq(E->getCond(), E->getLHS(), E->getRHS(),
6038 E->getBuiltinLoc(), E->getRParenLoc(), E->getType());
6039 if (!Imp)
6040 return Imp.takeError();
6041
6042 Expr *ToCond;
6043 Expr *ToLHS;
6044 Expr *ToRHS;
6045 SourceLocation ToBuiltinLoc, ToRParenLoc;
6046 QualType ToType;
6047 std::tie(ToCond, ToLHS, ToRHS, ToBuiltinLoc, ToRParenLoc, ToType) = *Imp;
6048
6049 ExprValueKind VK = E->getValueKind();
6050 ExprObjectKind OK = E->getObjectKind();
6051
6052 bool TypeDependent = ToCond->isTypeDependent();
6053 bool ValueDependent = ToCond->isValueDependent();
6054
6055 // The value of CondIsTrue only matters if the value is not
6056 // condition-dependent.
6057 bool CondIsTrue = !E->isConditionDependent() && E->isConditionTrue();
6058
6059 return new (Importer.getToContext())
6060 ChooseExpr(ToBuiltinLoc, ToCond, ToLHS, ToRHS, ToType, VK, OK,
6061 ToRParenLoc, CondIsTrue, TypeDependent, ValueDependent);
6062}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006063
Balazs Keri3b30d652018-10-19 13:32:20 +00006064ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6065 ExpectedType TypeOrErr = import(E->getType());
6066 if (!TypeOrErr)
6067 return TypeOrErr.takeError();
6068
6069 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6070 if (!BeginLocOrErr)
6071 return BeginLocOrErr.takeError();
6072
6073 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006074}
6075
Balazs Keri3b30d652018-10-19 13:32:20 +00006076ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6077 auto Imp = importSeq(
6078 E->getBeginLoc(), E->getType(), E->getFunctionName());
6079 if (!Imp)
6080 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006081
Balazs Keri3b30d652018-10-19 13:32:20 +00006082 SourceLocation ToBeginLoc;
6083 QualType ToType;
6084 StringLiteral *ToFunctionName;
6085 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006086
Bruno Ricci17ff0262018-10-27 19:21:19 +00006087 return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType,
6088 E->getIdentKind(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006089}
6090
Balazs Keri3b30d652018-10-19 13:32:20 +00006091ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6092 auto Imp = importSeq(
6093 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6094 E->getLocation(), E->getType());
6095 if (!Imp)
6096 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006097
Balazs Keri3b30d652018-10-19 13:32:20 +00006098 NestedNameSpecifierLoc ToQualifierLoc;
6099 SourceLocation ToTemplateKeywordLoc, ToLocation;
6100 ValueDecl *ToDecl;
6101 QualType ToType;
6102 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6103 *Imp;
6104
6105 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006106 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006107 auto FoundDOrErr = import(E->getFoundDecl());
6108 if (!FoundDOrErr)
6109 return FoundDOrErr.takeError();
6110 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006111 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006112
Aleksei Sidorina693b372016-09-28 10:16:56 +00006113 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006114 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006115 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006116 if (Error Err =
6117 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6118 return std::move(Err);
6119 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006120 }
6121
Balazs Keri3b30d652018-10-19 13:32:20 +00006122 auto *ToE = DeclRefExpr::Create(
6123 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6124 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
6125 E->getValueKind(), ToFoundD, ToResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006126 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006127 ToE->setHadMultipleCandidates(true);
6128 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006129}
6130
Balazs Keri3b30d652018-10-19 13:32:20 +00006131ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6132 ExpectedType TypeOrErr = import(E->getType());
6133 if (!TypeOrErr)
6134 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006135
Balazs Keri3b30d652018-10-19 13:32:20 +00006136 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006137}
6138
Balazs Keri3b30d652018-10-19 13:32:20 +00006139ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6140 ExpectedExpr ToInitOrErr = import(E->getInit());
6141 if (!ToInitOrErr)
6142 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006143
Balazs Keri3b30d652018-10-19 13:32:20 +00006144 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6145 if (!ToEqualOrColonLocOrErr)
6146 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006147
Balazs Keri3b30d652018-10-19 13:32:20 +00006148 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006149 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006150 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6151 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6152 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006153 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006154 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006155 }
6156
Balazs Keri3b30d652018-10-19 13:32:20 +00006157 SmallVector<Designator, 4> ToDesignators(E->size());
6158 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6159 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006160
6161 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006162 Importer.getToContext(), ToDesignators,
6163 ToIndexExprs, *ToEqualOrColonLocOrErr,
6164 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006165}
6166
Balazs Keri3b30d652018-10-19 13:32:20 +00006167ExpectedStmt
6168ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6169 ExpectedType ToTypeOrErr = import(E->getType());
6170 if (!ToTypeOrErr)
6171 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006172
Balazs Keri3b30d652018-10-19 13:32:20 +00006173 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6174 if (!ToLocationOrErr)
6175 return ToLocationOrErr.takeError();
6176
6177 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6178 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006179}
6180
Balazs Keri3b30d652018-10-19 13:32:20 +00006181ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6182 ExpectedType ToTypeOrErr = import(E->getType());
6183 if (!ToTypeOrErr)
6184 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006185
Balazs Keri3b30d652018-10-19 13:32:20 +00006186 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6187 if (!ToLocationOrErr)
6188 return ToLocationOrErr.takeError();
6189
6190 return IntegerLiteral::Create(
6191 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006192}
6193
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006194
Balazs Keri3b30d652018-10-19 13:32:20 +00006195ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6196 ExpectedType ToTypeOrErr = import(E->getType());
6197 if (!ToTypeOrErr)
6198 return ToTypeOrErr.takeError();
6199
6200 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6201 if (!ToLocationOrErr)
6202 return ToLocationOrErr.takeError();
6203
6204 return FloatingLiteral::Create(
6205 Importer.getToContext(), E->getValue(), E->isExact(),
6206 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006207}
6208
Balazs Keri3b30d652018-10-19 13:32:20 +00006209ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6210 auto ToTypeOrErr = import(E->getType());
6211 if (!ToTypeOrErr)
6212 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006213
Balazs Keri3b30d652018-10-19 13:32:20 +00006214 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6215 if (!ToSubExprOrErr)
6216 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006217
Balazs Keri3b30d652018-10-19 13:32:20 +00006218 return new (Importer.getToContext()) ImaginaryLiteral(
6219 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006220}
6221
Balazs Keri3b30d652018-10-19 13:32:20 +00006222ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6223 ExpectedType ToTypeOrErr = import(E->getType());
6224 if (!ToTypeOrErr)
6225 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006226
Balazs Keri3b30d652018-10-19 13:32:20 +00006227 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6228 if (!ToLocationOrErr)
6229 return ToLocationOrErr.takeError();
6230
6231 return new (Importer.getToContext()) CharacterLiteral(
6232 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006233}
6234
Balazs Keri3b30d652018-10-19 13:32:20 +00006235ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6236 ExpectedType ToTypeOrErr = import(E->getType());
6237 if (!ToTypeOrErr)
6238 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006239
Balazs Keri3b30d652018-10-19 13:32:20 +00006240 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6241 if (Error Err = ImportArrayChecked(
6242 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6243 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006244
Balazs Keri3b30d652018-10-19 13:32:20 +00006245 return StringLiteral::Create(
6246 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6247 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006248}
6249
Balazs Keri3b30d652018-10-19 13:32:20 +00006250ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6251 auto Imp = importSeq(
6252 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6253 E->getInitializer());
6254 if (!Imp)
6255 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006256
Balazs Keri3b30d652018-10-19 13:32:20 +00006257 SourceLocation ToLParenLoc;
6258 TypeSourceInfo *ToTypeSourceInfo;
6259 QualType ToType;
6260 Expr *ToInitializer;
6261 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006262
6263 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006264 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6265 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006266}
6267
Balazs Keri3b30d652018-10-19 13:32:20 +00006268ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6269 auto Imp = importSeq(
6270 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6271 if (!Imp)
6272 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006273
Balazs Keri3b30d652018-10-19 13:32:20 +00006274 SourceLocation ToBuiltinLoc, ToRParenLoc;
6275 QualType ToType;
6276 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6277
6278 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6279 if (Error Err = ImportArrayChecked(
6280 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6281 ToExprs.begin()))
6282 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006283
6284 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006285 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006286}
6287
Balazs Keri3b30d652018-10-19 13:32:20 +00006288ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6289 auto Imp = importSeq(
6290 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6291 if (!Imp)
6292 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006293
Balazs Keri3b30d652018-10-19 13:32:20 +00006294 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6295 LabelDecl *ToLabel;
6296 QualType ToType;
6297 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006298
6299 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006300 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006301}
6302
Bill Wendling8003edc2018-11-09 00:41:36 +00006303ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) {
6304 auto Imp = importSeq(E->getSubExpr());
6305 if (!Imp)
6306 return Imp.takeError();
6307
6308 Expr *ToSubExpr;
6309 std::tie(ToSubExpr) = *Imp;
6310
Fangrui Song407659a2018-11-30 23:41:18 +00006311 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr);
Bill Wendling8003edc2018-11-09 00:41:36 +00006312}
6313
Balazs Keri3b30d652018-10-19 13:32:20 +00006314ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6315 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6316 if (!Imp)
6317 return Imp.takeError();
6318
6319 SourceLocation ToLParen, ToRParen;
6320 Expr *ToSubExpr;
6321 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006322
Fangrui Song6907ce22018-07-30 19:24:48 +00006323 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006324 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006325}
6326
Balazs Keri3b30d652018-10-19 13:32:20 +00006327ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6328 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6329 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6330 return std::move(Err);
6331
6332 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6333 if (!ToLParenLocOrErr)
6334 return ToLParenLocOrErr.takeError();
6335
6336 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6337 if (!ToRParenLocOrErr)
6338 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006339
Bruno Riccif49e1ca2018-11-20 16:20:40 +00006340 return ParenListExpr::Create(Importer.getToContext(), *ToLParenLocOrErr,
6341 ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006342}
6343
Balazs Keri3b30d652018-10-19 13:32:20 +00006344ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6345 auto Imp = importSeq(
6346 E->getSubStmt(), E->getType(), E->getLParenLoc(), E->getRParenLoc());
6347 if (!Imp)
6348 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006349
Balazs Keri3b30d652018-10-19 13:32:20 +00006350 CompoundStmt *ToSubStmt;
6351 QualType ToType;
6352 SourceLocation ToLParenLoc, ToRParenLoc;
6353 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006354
Balazs Keri3b30d652018-10-19 13:32:20 +00006355 return new (Importer.getToContext()) StmtExpr(
6356 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006357}
6358
Balazs Keri3b30d652018-10-19 13:32:20 +00006359ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6360 auto Imp = importSeq(
6361 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6362 if (!Imp)
6363 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006364
Balazs Keri3b30d652018-10-19 13:32:20 +00006365 Expr *ToSubExpr;
6366 QualType ToType;
6367 SourceLocation ToOperatorLoc;
6368 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006369
Aaron Ballmana5038552018-01-09 13:07:03 +00006370 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006371 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6372 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006373}
6374
Balazs Keri3b30d652018-10-19 13:32:20 +00006375ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006376ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006377 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6378 if (!Imp)
6379 return Imp.takeError();
6380
6381 QualType ToType;
6382 SourceLocation ToOperatorLoc, ToRParenLoc;
6383 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006384
Douglas Gregord8552cd2010-02-19 01:24:23 +00006385 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006386 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6387 import(E->getArgumentTypeInfo());
6388 if (!ToArgumentTypeInfoOrErr)
6389 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006390
Balazs Keri3b30d652018-10-19 13:32:20 +00006391 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6392 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6393 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006394 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006395
Balazs Keri3b30d652018-10-19 13:32:20 +00006396 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6397 if (!ToArgumentExprOrErr)
6398 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006399
Balazs Keri3b30d652018-10-19 13:32:20 +00006400 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6401 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006402}
6403
Balazs Keri3b30d652018-10-19 13:32:20 +00006404ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6405 auto Imp = importSeq(
6406 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6407 if (!Imp)
6408 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006409
Balazs Keri3b30d652018-10-19 13:32:20 +00006410 Expr *ToLHS, *ToRHS;
6411 QualType ToType;
6412 SourceLocation ToOperatorLoc;
6413 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006414
Balazs Keri3b30d652018-10-19 13:32:20 +00006415 return new (Importer.getToContext()) BinaryOperator(
6416 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6417 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006418}
6419
Balazs Keri3b30d652018-10-19 13:32:20 +00006420ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6421 auto Imp = importSeq(
6422 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6423 E->getRHS(), E->getType());
6424 if (!Imp)
6425 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006426
Balazs Keri3b30d652018-10-19 13:32:20 +00006427 Expr *ToCond, *ToLHS, *ToRHS;
6428 SourceLocation ToQuestionLoc, ToColonLoc;
6429 QualType ToType;
6430 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006431
6432 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006433 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6434 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006435}
6436
Balazs Keri3b30d652018-10-19 13:32:20 +00006437ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006438 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006439 auto Imp = importSeq(
6440 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6441 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6442 if (!Imp)
6443 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006444
Balazs Keri3b30d652018-10-19 13:32:20 +00006445 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6446 OpaqueValueExpr *ToOpaqueValue;
6447 SourceLocation ToQuestionLoc, ToColonLoc;
6448 QualType ToType;
6449 std::tie(
6450 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6451 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006452
6453 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006454 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6455 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6456 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006457}
6458
Balazs Keri3b30d652018-10-19 13:32:20 +00006459ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6460 auto Imp = importSeq(
6461 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6462 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6463 if (!Imp)
6464 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006465
Balazs Keri3b30d652018-10-19 13:32:20 +00006466 SourceLocation ToBeginLoc, ToEndLoc;
6467 TypeSourceInfo *ToQueriedTypeSourceInfo;
6468 Expr *ToDimensionExpression;
6469 QualType ToType;
6470 std::tie(
6471 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6472 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006473
6474 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006475 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6476 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006477}
6478
Balazs Keri3b30d652018-10-19 13:32:20 +00006479ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6480 auto Imp = importSeq(
6481 E->getBeginLoc(), E->getQueriedExpression(), E->getEndLoc(), E->getType());
6482 if (!Imp)
6483 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006484
Balazs Keri3b30d652018-10-19 13:32:20 +00006485 SourceLocation ToBeginLoc, ToEndLoc;
6486 Expr *ToQueriedExpression;
6487 QualType ToType;
6488 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006489
6490 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006491 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6492 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006493}
6494
Balazs Keri3b30d652018-10-19 13:32:20 +00006495ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6496 auto Imp = importSeq(
6497 E->getLocation(), E->getType(), E->getSourceExpr());
6498 if (!Imp)
6499 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006500
Balazs Keri3b30d652018-10-19 13:32:20 +00006501 SourceLocation ToLocation;
6502 QualType ToType;
6503 Expr *ToSourceExpr;
6504 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006505
6506 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006507 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006508}
6509
Balazs Keri3b30d652018-10-19 13:32:20 +00006510ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6511 auto Imp = importSeq(
6512 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6513 if (!Imp)
6514 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006515
Balazs Keri3b30d652018-10-19 13:32:20 +00006516 Expr *ToLHS, *ToRHS;
6517 SourceLocation ToRBracketLoc;
6518 QualType ToType;
6519 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006520
6521 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006522 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6523 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006524}
6525
Balazs Keri3b30d652018-10-19 13:32:20 +00006526ExpectedStmt
6527ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6528 auto Imp = importSeq(
6529 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6530 E->getComputationResultType(), E->getOperatorLoc());
6531 if (!Imp)
6532 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006533
Balazs Keri3b30d652018-10-19 13:32:20 +00006534 Expr *ToLHS, *ToRHS;
6535 QualType ToType, ToComputationLHSType, ToComputationResultType;
6536 SourceLocation ToOperatorLoc;
6537 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6538 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006539
Balazs Keri3b30d652018-10-19 13:32:20 +00006540 return new (Importer.getToContext()) CompoundAssignOperator(
6541 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6542 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6543 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006544}
6545
Balazs Keri3b30d652018-10-19 13:32:20 +00006546Expected<CXXCastPath>
6547ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6548 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006549 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006550 if (auto SpecOrErr = import(*I))
6551 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006552 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006553 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006554 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006555 return Path;
John McCallcf142162010-08-07 06:22:56 +00006556}
6557
Balazs Keri3b30d652018-10-19 13:32:20 +00006558ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6559 ExpectedType ToTypeOrErr = import(E->getType());
6560 if (!ToTypeOrErr)
6561 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006562
Balazs Keri3b30d652018-10-19 13:32:20 +00006563 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6564 if (!ToSubExprOrErr)
6565 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006566
Balazs Keri3b30d652018-10-19 13:32:20 +00006567 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6568 if (!ToBasePathOrErr)
6569 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006570
Balazs Keri3b30d652018-10-19 13:32:20 +00006571 return ImplicitCastExpr::Create(
6572 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6573 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006574}
6575
Balazs Keri3b30d652018-10-19 13:32:20 +00006576ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6577 auto Imp1 = importSeq(
6578 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6579 if (!Imp1)
6580 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006581
Balazs Keri3b30d652018-10-19 13:32:20 +00006582 QualType ToType;
6583 Expr *ToSubExpr;
6584 TypeSourceInfo *ToTypeInfoAsWritten;
6585 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006586
Balazs Keri3b30d652018-10-19 13:32:20 +00006587 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6588 if (!ToBasePathOrErr)
6589 return ToBasePathOrErr.takeError();
6590 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006591
Aleksei Sidorina693b372016-09-28 10:16:56 +00006592 switch (E->getStmtClass()) {
6593 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006594 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006595 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6596 if (!ToLParenLocOrErr)
6597 return ToLParenLocOrErr.takeError();
6598 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6599 if (!ToRParenLocOrErr)
6600 return ToRParenLocOrErr.takeError();
6601 return CStyleCastExpr::Create(
6602 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6603 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6604 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006605 }
6606
6607 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006608 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006609 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6610 if (!ToLParenLocOrErr)
6611 return ToLParenLocOrErr.takeError();
6612 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6613 if (!ToRParenLocOrErr)
6614 return ToRParenLocOrErr.takeError();
6615 return CXXFunctionalCastExpr::Create(
6616 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6617 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6618 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006619 }
6620
6621 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006622 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6623 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6624 if (!ToLParenLocOrErr)
6625 return ToLParenLocOrErr.takeError();
6626 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6627 if (!ToBridgeKeywordLocOrErr)
6628 return ToBridgeKeywordLocOrErr.takeError();
6629 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6630 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6631 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006632 }
6633 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006634 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006635 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006636 }
6637}
6638
Balazs Keri3b30d652018-10-19 13:32:20 +00006639ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
6640 SmallVector<OffsetOfNode, 4> ToNodes;
6641 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
6642 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006643
Balazs Keri3b30d652018-10-19 13:32:20 +00006644 SourceLocation ToBeginLoc, ToEndLoc;
6645 if (FromNode.getKind() != OffsetOfNode::Base) {
6646 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
6647 if (!Imp)
6648 return Imp.takeError();
6649 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
6650 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00006651
Balazs Keri3b30d652018-10-19 13:32:20 +00006652 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00006653 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00006654 ToNodes.push_back(
6655 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006656 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006657 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006658 auto ToBSOrErr = import(FromNode.getBase());
6659 if (!ToBSOrErr)
6660 return ToBSOrErr.takeError();
6661 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006662 break;
6663 }
6664 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006665 auto ToFieldOrErr = import(FromNode.getField());
6666 if (!ToFieldOrErr)
6667 return ToFieldOrErr.takeError();
6668 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006669 break;
6670 }
6671 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006672 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
6673 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006674 break;
6675 }
6676 }
6677 }
6678
Balazs Keri3b30d652018-10-19 13:32:20 +00006679 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
6680 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
6681 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
6682 if (!ToIndexExprOrErr)
6683 return ToIndexExprOrErr.takeError();
6684 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006685 }
6686
Balazs Keri3b30d652018-10-19 13:32:20 +00006687 auto Imp = importSeq(
6688 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
6689 E->getRParenLoc());
6690 if (!Imp)
6691 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006692
Balazs Keri3b30d652018-10-19 13:32:20 +00006693 QualType ToType;
6694 TypeSourceInfo *ToTypeSourceInfo;
6695 SourceLocation ToOperatorLoc, ToRParenLoc;
6696 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
6697
6698 return OffsetOfExpr::Create(
6699 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
6700 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006701}
6702
Balazs Keri3b30d652018-10-19 13:32:20 +00006703ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6704 auto Imp = importSeq(
6705 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
6706 if (!Imp)
6707 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006708
Balazs Keri3b30d652018-10-19 13:32:20 +00006709 QualType ToType;
6710 Expr *ToOperand;
6711 SourceLocation ToBeginLoc, ToEndLoc;
6712 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006713
Balazs Keri3b30d652018-10-19 13:32:20 +00006714 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006715 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00006716 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006717 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006718 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006719
Balazs Keri3b30d652018-10-19 13:32:20 +00006720 return new (Importer.getToContext()) CXXNoexceptExpr(
6721 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006722}
6723
Balazs Keri3b30d652018-10-19 13:32:20 +00006724ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6725 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
6726 if (!Imp)
6727 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006728
Balazs Keri3b30d652018-10-19 13:32:20 +00006729 Expr *ToSubExpr;
6730 QualType ToType;
6731 SourceLocation ToThrowLoc;
6732 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006733
6734 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006735 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006736}
6737
Balazs Keri3b30d652018-10-19 13:32:20 +00006738ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6739 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
6740 if (!ToUsedLocOrErr)
6741 return ToUsedLocOrErr.takeError();
6742
6743 auto ToParamOrErr = import(E->getParam());
6744 if (!ToParamOrErr)
6745 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006746
6747 return CXXDefaultArgExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006748 Importer.getToContext(), *ToUsedLocOrErr, *ToParamOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006749}
6750
Balazs Keri3b30d652018-10-19 13:32:20 +00006751ExpectedStmt
6752ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6753 auto Imp = importSeq(
6754 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
6755 if (!Imp)
6756 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006757
Balazs Keri3b30d652018-10-19 13:32:20 +00006758 QualType ToType;
6759 TypeSourceInfo *ToTypeSourceInfo;
6760 SourceLocation ToRParenLoc;
6761 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006762
6763 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006764 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006765}
6766
Balazs Keri3b30d652018-10-19 13:32:20 +00006767ExpectedStmt
6768ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6769 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6770 if (!ToSubExprOrErr)
6771 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006772
Balazs Keri3b30d652018-10-19 13:32:20 +00006773 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
6774 if (!ToDtorOrErr)
6775 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006776
6777 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00006778 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
6779 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006780}
6781
Balazs Keri3b30d652018-10-19 13:32:20 +00006782ExpectedStmt
6783ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
6784 auto Imp = importSeq(
6785 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
6786 E->getParenOrBraceRange());
6787 if (!Imp)
6788 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006789
Balazs Keri3b30d652018-10-19 13:32:20 +00006790 CXXConstructorDecl *ToConstructor;
6791 QualType ToType;
6792 TypeSourceInfo *ToTypeSourceInfo;
6793 SourceRange ToParenOrBraceRange;
6794 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006795
Balazs Keri3b30d652018-10-19 13:32:20 +00006796 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
6797 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6798 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006799
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00006800 return CXXTemporaryObjectExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006801 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
6802 ToParenOrBraceRange, E->hadMultipleCandidates(),
6803 E->isListInitialization(), E->isStdInitListInitialization(),
6804 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006805}
6806
Balazs Keri3b30d652018-10-19 13:32:20 +00006807ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00006808ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006809 auto Imp = importSeq(
6810 E->getType(), E->GetTemporaryExpr(), E->getExtendingDecl());
6811 if (!Imp)
6812 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006813
Balazs Keri3b30d652018-10-19 13:32:20 +00006814 QualType ToType;
6815 Expr *ToTemporaryExpr;
6816 const ValueDecl *ToExtendingDecl;
6817 std::tie(ToType, ToTemporaryExpr, ToExtendingDecl) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006818
6819 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006820 ToType, ToTemporaryExpr, E->isBoundToLvalueReference());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006821
6822 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
Balazs Keri3b30d652018-10-19 13:32:20 +00006823 ToMTE->setExtendingDecl(ToExtendingDecl, E->getManglingNumber());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006824 return ToMTE;
6825}
6826
Balazs Keri3b30d652018-10-19 13:32:20 +00006827ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6828 auto Imp = importSeq(
6829 E->getType(), E->getPattern(), E->getEllipsisLoc());
6830 if (!Imp)
6831 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00006832
Balazs Keri3b30d652018-10-19 13:32:20 +00006833 QualType ToType;
6834 Expr *ToPattern;
6835 SourceLocation ToEllipsisLoc;
6836 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00006837
6838 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006839 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00006840}
6841
Balazs Keri3b30d652018-10-19 13:32:20 +00006842ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6843 auto Imp = importSeq(
6844 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
6845 if (!Imp)
6846 return Imp.takeError();
6847
6848 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
6849 NamedDecl *ToPack;
6850 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006851
6852 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006853 if (!E->isValueDependent())
6854 Length = E->getPackLength();
6855
Balazs Keri3b30d652018-10-19 13:32:20 +00006856 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006857 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006858 if (Error Err = ImportTemplateArguments(
6859 E->getPartialArguments().data(),
6860 E->getPartialArguments().size(),
6861 ToPartialArguments))
6862 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006863 }
6864
6865 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006866 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
6867 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006868}
6869
Aleksei Sidorina693b372016-09-28 10:16:56 +00006870
Balazs Keri3b30d652018-10-19 13:32:20 +00006871ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
6872 auto Imp = importSeq(
6873 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
6874 E->getArraySize(), E->getInitializer(), E->getType(),
6875 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
6876 E->getDirectInitRange());
6877 if (!Imp)
6878 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006879
Balazs Keri3b30d652018-10-19 13:32:20 +00006880 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
6881 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
6882 Expr *ToArraySize, *ToInitializer;
6883 QualType ToType;
6884 TypeSourceInfo *ToAllocatedTypeSourceInfo;
6885 std::tie(
6886 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
6887 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006888
Balazs Keri3b30d652018-10-19 13:32:20 +00006889 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
6890 if (Error Err =
6891 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
6892 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006893
Bruno Ricci9b6dfac2019-01-07 15:04:45 +00006894 return CXXNewExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006895 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
6896 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
6897 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
6898 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
6899 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006900}
6901
Balazs Keri3b30d652018-10-19 13:32:20 +00006902ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6903 auto Imp = importSeq(
6904 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
6905 if (!Imp)
6906 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006907
Balazs Keri3b30d652018-10-19 13:32:20 +00006908 QualType ToType;
6909 FunctionDecl *ToOperatorDelete;
6910 Expr *ToArgument;
6911 SourceLocation ToBeginLoc;
6912 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006913
6914 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006915 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
6916 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
6917 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00006918}
6919
Balazs Keri3b30d652018-10-19 13:32:20 +00006920ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6921 auto Imp = importSeq(
6922 E->getType(), E->getLocation(), E->getConstructor(),
6923 E->getParenOrBraceRange());
6924 if (!Imp)
6925 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006926
Balazs Keri3b30d652018-10-19 13:32:20 +00006927 QualType ToType;
6928 SourceLocation ToLocation;
6929 CXXConstructorDecl *ToConstructor;
6930 SourceRange ToParenOrBraceRange;
6931 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00006932
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006933 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00006934 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6935 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00006936
Balazs Keri3b30d652018-10-19 13:32:20 +00006937 return CXXConstructExpr::Create(
6938 Importer.getToContext(), ToType, ToLocation, ToConstructor,
6939 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
6940 E->isListInitialization(), E->isStdInitListInitialization(),
6941 E->requiresZeroInitialization(), E->getConstructionKind(),
6942 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00006943}
6944
Balazs Keri3b30d652018-10-19 13:32:20 +00006945ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
6946 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6947 if (!ToSubExprOrErr)
6948 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006949
Balazs Keri3b30d652018-10-19 13:32:20 +00006950 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
6951 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
6952 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006953
Balazs Keri3b30d652018-10-19 13:32:20 +00006954 return ExprWithCleanups::Create(
6955 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
6956 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006957}
6958
Balazs Keri3b30d652018-10-19 13:32:20 +00006959ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6960 auto Imp = importSeq(
6961 E->getCallee(), E->getType(), E->getRParenLoc());
6962 if (!Imp)
6963 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00006964
Balazs Keri3b30d652018-10-19 13:32:20 +00006965 Expr *ToCallee;
6966 QualType ToType;
6967 SourceLocation ToRParenLoc;
6968 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006969
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006970 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00006971 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6972 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00006973
Bruno Riccic5885cf2018-12-21 15:20:32 +00006974 return CXXMemberCallExpr::Create(Importer.getToContext(), ToCallee, ToArgs,
6975 ToType, E->getValueKind(), ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00006976}
6977
Balazs Keri3b30d652018-10-19 13:32:20 +00006978ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6979 ExpectedType ToTypeOrErr = import(E->getType());
6980 if (!ToTypeOrErr)
6981 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00006982
Balazs Keri3b30d652018-10-19 13:32:20 +00006983 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6984 if (!ToLocationOrErr)
6985 return ToLocationOrErr.takeError();
6986
6987 return new (Importer.getToContext()) CXXThisExpr(
6988 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00006989}
6990
Balazs Keri3b30d652018-10-19 13:32:20 +00006991ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
6992 ExpectedType ToTypeOrErr = import(E->getType());
6993 if (!ToTypeOrErr)
6994 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00006995
Balazs Keri3b30d652018-10-19 13:32:20 +00006996 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6997 if (!ToLocationOrErr)
6998 return ToLocationOrErr.takeError();
6999
7000 return new (Importer.getToContext()) CXXBoolLiteralExpr(
7001 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00007002}
7003
Balazs Keri3b30d652018-10-19 13:32:20 +00007004ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7005 auto Imp1 = importSeq(
7006 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7007 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7008 if (!Imp1)
7009 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007010
Balazs Keri3b30d652018-10-19 13:32:20 +00007011 Expr *ToBase;
7012 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7013 NestedNameSpecifierLoc ToQualifierLoc;
7014 ValueDecl *ToMemberDecl;
7015 QualType ToType;
7016 std::tie(
7017 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7018 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007019
Balazs Keri3b30d652018-10-19 13:32:20 +00007020 auto Imp2 = importSeq(
7021 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7022 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7023 if (!Imp2)
7024 return Imp2.takeError();
7025 NamedDecl *ToDecl;
7026 DeclarationName ToName;
7027 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7028 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007029
7030 DeclAccessPair ToFoundDecl =
7031 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007032
Balazs Keri3b30d652018-10-19 13:32:20 +00007033 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007034
Gabor Marton5caba302019-03-07 13:38:20 +00007035 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007036 if (E->hasExplicitTemplateArgs()) {
Gabor Marton5caba302019-03-07 13:38:20 +00007037 if (Error Err =
7038 ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
7039 E->template_arguments(), ToTAInfo))
7040 return std::move(Err);
7041 ResInfo = &ToTAInfo;
Sean Callanan59721b32015-04-28 18:41:46 +00007042 }
7043
Balazs Keri3b30d652018-10-19 13:32:20 +00007044 return MemberExpr::Create(
7045 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7046 ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl, ToFoundDecl,
Gabor Marton5caba302019-03-07 13:38:20 +00007047 ToMemberNameInfo, ResInfo, ToType, E->getValueKind(), E->getObjectKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007048}
7049
Balazs Keri3b30d652018-10-19 13:32:20 +00007050ExpectedStmt
7051ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7052 auto Imp = importSeq(
7053 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7054 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7055 if (!Imp)
7056 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007057
Balazs Keri3b30d652018-10-19 13:32:20 +00007058 Expr *ToBase;
7059 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7060 NestedNameSpecifierLoc ToQualifierLoc;
7061 TypeSourceInfo *ToScopeTypeInfo;
7062 std::tie(
7063 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7064 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007065
7066 PseudoDestructorTypeStorage Storage;
7067 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7068 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007069 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7070 if (!ToDestroyedTypeLocOrErr)
7071 return ToDestroyedTypeLocOrErr.takeError();
7072 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007073 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007074 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7075 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7076 else
7077 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007078 }
7079
7080 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007081 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7082 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007083}
7084
Balazs Keri3b30d652018-10-19 13:32:20 +00007085ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007086 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007087 auto Imp = importSeq(
7088 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7089 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7090 if (!Imp)
7091 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007092
Balazs Keri3b30d652018-10-19 13:32:20 +00007093 QualType ToType;
7094 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7095 NestedNameSpecifierLoc ToQualifierLoc;
7096 NamedDecl *ToFirstQualifierFoundInScope;
7097 std::tie(
7098 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7099 ToFirstQualifierFoundInScope) = *Imp;
7100
7101 Expr *ToBase = nullptr;
7102 if (!E->isImplicitAccess()) {
7103 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7104 ToBase = *ToBaseOrErr;
7105 else
7106 return ToBaseOrErr.takeError();
7107 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007108
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007109 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007110 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007111 if (Error Err = ImportTemplateArgumentListInfo(
7112 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7113 ToTAInfo))
7114 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007115 ResInfo = &ToTAInfo;
7116 }
7117
Balazs Keri3b30d652018-10-19 13:32:20 +00007118 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7119 if (!ToMemberNameInfoOrErr)
7120 return ToMemberNameInfoOrErr.takeError();
7121 DeclarationNameInfo ToMemberNameInfo(
7122 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007123 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007124 if (Error Err = ImportDeclarationNameLoc(
7125 E->getMemberNameInfo(), ToMemberNameInfo))
7126 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007127
7128 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007129 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7130 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7131 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007132}
7133
Balazs Keri3b30d652018-10-19 13:32:20 +00007134ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007135ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007136 auto Imp = importSeq(
7137 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
7138 E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7139 if (!Imp)
7140 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007141
Balazs Keri3b30d652018-10-19 13:32:20 +00007142 NestedNameSpecifierLoc ToQualifierLoc;
7143 SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
7144 DeclarationName ToDeclName;
7145 std::tie(
7146 ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
7147 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007148
Balazs Keri3b30d652018-10-19 13:32:20 +00007149 DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
7150 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7151 return std::move(Err);
7152
7153 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007154 TemplateArgumentListInfo *ResInfo = nullptr;
7155 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007156 if (Error Err =
7157 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7158 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007159 ResInfo = &ToTAInfo;
7160 }
7161
7162 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007163 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7164 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007165}
7166
Balazs Keri3b30d652018-10-19 13:32:20 +00007167ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7168 CXXUnresolvedConstructExpr *E) {
7169 auto Imp = importSeq(
7170 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7171 if (!Imp)
7172 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007173
Balazs Keri3b30d652018-10-19 13:32:20 +00007174 SourceLocation ToLParenLoc, ToRParenLoc;
7175 TypeSourceInfo *ToTypeSourceInfo;
7176 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7177
7178 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7179 if (Error Err =
7180 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7181 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007182
7183 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007184 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7185 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007186}
7187
Balazs Keri3b30d652018-10-19 13:32:20 +00007188ExpectedStmt
7189ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7190 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7191 if (!ToNamingClassOrErr)
7192 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007193
Balazs Keri3b30d652018-10-19 13:32:20 +00007194 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7195 if (!ToQualifierLocOrErr)
7196 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007197
Balazs Keri3b30d652018-10-19 13:32:20 +00007198 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7199 if (!ToNameInfoOrErr)
7200 return ToNameInfoOrErr.takeError();
7201 DeclarationNameInfo ToNameInfo(
7202 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007203 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007204 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7205 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007206
7207 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007208 for (auto *D : E->decls())
7209 if (auto ToDOrErr = import(D))
7210 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007211 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007212 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007213
Balazs Keri3b30d652018-10-19 13:32:20 +00007214 if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
7215 TemplateArgumentListInfo ToTAInfo;
7216 if (Error Err = ImportTemplateArgumentListInfo(
7217 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7218 ToTAInfo))
7219 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007220
Balazs Keri3b30d652018-10-19 13:32:20 +00007221 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7222 if (!ToTemplateKeywordLocOrErr)
7223 return ToTemplateKeywordLocOrErr.takeError();
7224
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007225 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007226 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7227 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7228 ToDecls.begin(), ToDecls.end());
7229 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007230
7231 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007232 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7233 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7234 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007235}
7236
Balazs Keri3b30d652018-10-19 13:32:20 +00007237ExpectedStmt
7238ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7239 auto Imp1 = importSeq(
7240 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7241 E->getTemplateKeywordLoc());
7242 if (!Imp1)
7243 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007244
Balazs Keri3b30d652018-10-19 13:32:20 +00007245 QualType ToType;
7246 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7247 NestedNameSpecifierLoc ToQualifierLoc;
7248 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7249
7250 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7251 if (!Imp2)
7252 return Imp2.takeError();
7253 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7254 // Import additional name location/type info.
7255 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7256 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007257
7258 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007259 for (Decl *D : E->decls())
7260 if (auto ToDOrErr = import(D))
7261 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007262 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007263 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007264
7265 TemplateArgumentListInfo ToTAInfo;
7266 TemplateArgumentListInfo *ResInfo = nullptr;
7267 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007268 if (Error Err =
7269 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7270 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007271 ResInfo = &ToTAInfo;
7272 }
7273
Balazs Keri3b30d652018-10-19 13:32:20 +00007274 Expr *ToBase = nullptr;
7275 if (!E->isImplicitAccess()) {
7276 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7277 ToBase = *ToBaseOrErr;
7278 else
7279 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007280 }
7281
7282 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007283 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7284 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7285 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007286}
7287
Balazs Keri3b30d652018-10-19 13:32:20 +00007288ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7289 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7290 if (!Imp)
7291 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007292
Balazs Keri3b30d652018-10-19 13:32:20 +00007293 Expr *ToCallee;
7294 QualType ToType;
7295 SourceLocation ToRParenLoc;
7296 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007297
7298 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007299 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7300 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7301 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007302
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007303 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
Bruno Riccic5885cf2018-12-21 15:20:32 +00007304 return CXXOperatorCallExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007305 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
Eric Fiselier5cdc2cd2018-12-12 21:50:55 +00007306 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures(),
7307 OCE->getADLCallKind());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007308 }
7309
Bruno Riccic5885cf2018-12-21 15:20:32 +00007310 return CallExpr::Create(Importer.getToContext(), ToCallee, ToArgs, ToType,
7311 E->getValueKind(), ToRParenLoc, /*MinNumArgs=*/0,
7312 E->getADLCallKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007313}
7314
Balazs Keri3b30d652018-10-19 13:32:20 +00007315ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7316 CXXRecordDecl *FromClass = E->getLambdaClass();
7317 auto ToClassOrErr = import(FromClass);
7318 if (!ToClassOrErr)
7319 return ToClassOrErr.takeError();
7320 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007321
7322 // NOTE: lambda classes are created with BeingDefined flag set up.
7323 // It means that ImportDefinition doesn't work for them and we should fill it
7324 // manually.
Gabor Marton302f3002019-02-15 12:04:05 +00007325 if (ToClass->isBeingDefined())
7326 if (Error Err = ImportDeclContext(FromClass, /*ForceImport = */ true))
7327 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007328
Balazs Keri3b30d652018-10-19 13:32:20 +00007329 auto ToCallOpOrErr = import(E->getCallOperator());
7330 if (!ToCallOpOrErr)
7331 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007332
7333 ToClass->completeDefinition();
7334
Balazs Keri3b30d652018-10-19 13:32:20 +00007335 SmallVector<LambdaCapture, 8> ToCaptures;
7336 ToCaptures.reserve(E->capture_size());
7337 for (const auto &FromCapture : E->captures()) {
7338 if (auto ToCaptureOrErr = import(FromCapture))
7339 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007340 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007341 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007342 }
7343
Balazs Keri3b30d652018-10-19 13:32:20 +00007344 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7345 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7346 return std::move(Err);
7347
7348 auto Imp = importSeq(
7349 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7350 if (!Imp)
7351 return Imp.takeError();
7352
7353 SourceRange ToIntroducerRange;
7354 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7355 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007356
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007357 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007358 Importer.getToContext(), ToClass, ToIntroducerRange,
7359 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7360 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7361 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007362}
7363
Sean Callanan8bca9962016-03-28 21:43:01 +00007364
Balazs Keri3b30d652018-10-19 13:32:20 +00007365ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7366 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7367 if (!Imp)
7368 return Imp.takeError();
7369
7370 SourceLocation ToLBraceLoc, ToRBraceLoc;
7371 QualType ToType;
7372 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7373
7374 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7375 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7376 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007377
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007378 ASTContext &ToCtx = Importer.getToContext();
7379 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007380 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7381 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007382
Balazs Keri3b30d652018-10-19 13:32:20 +00007383 if (E->hasArrayFiller()) {
7384 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7385 To->setArrayFiller(*ToFillerOrErr);
7386 else
7387 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007388 }
7389
Balazs Keri3b30d652018-10-19 13:32:20 +00007390 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7391 if (auto ToFDOrErr = import(FromFD))
7392 To->setInitializedFieldInUnion(*ToFDOrErr);
7393 else
7394 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007395 }
7396
Balazs Keri3b30d652018-10-19 13:32:20 +00007397 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7398 if (auto ToSyntFormOrErr = import(SyntForm))
7399 To->setSyntacticForm(*ToSyntFormOrErr);
7400 else
7401 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007402 }
7403
Gabor Martona20ce602018-09-03 13:10:53 +00007404 // Copy InitListExprBitfields, which are not handled in the ctor of
7405 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007406 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007407
7408 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007409}
7410
Balazs Keri3b30d652018-10-19 13:32:20 +00007411ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007412 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007413 ExpectedType ToTypeOrErr = import(E->getType());
7414 if (!ToTypeOrErr)
7415 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007416
Balazs Keri3b30d652018-10-19 13:32:20 +00007417 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7418 if (!ToSubExprOrErr)
7419 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007420
Balazs Keri3b30d652018-10-19 13:32:20 +00007421 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7422 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007423}
7424
Balazs Keri3b30d652018-10-19 13:32:20 +00007425ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007426 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007427 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7428 if (!Imp)
7429 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007430
Balazs Keri3b30d652018-10-19 13:32:20 +00007431 SourceLocation ToLocation;
7432 QualType ToType;
7433 CXXConstructorDecl *ToConstructor;
7434 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007435
7436 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007437 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7438 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007439}
7440
Balazs Keri3b30d652018-10-19 13:32:20 +00007441ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7442 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7443 if (!Imp)
7444 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007445
Balazs Keri3b30d652018-10-19 13:32:20 +00007446 QualType ToType;
7447 Expr *ToCommonExpr, *ToSubExpr;
7448 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007449
Balazs Keri3b30d652018-10-19 13:32:20 +00007450 return new (Importer.getToContext()) ArrayInitLoopExpr(
7451 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007452}
7453
Balazs Keri3b30d652018-10-19 13:32:20 +00007454ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7455 ExpectedType ToTypeOrErr = import(E->getType());
7456 if (!ToTypeOrErr)
7457 return ToTypeOrErr.takeError();
7458 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007459}
7460
Balazs Keri3b30d652018-10-19 13:32:20 +00007461ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7462 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7463 if (!ToBeginLocOrErr)
7464 return ToBeginLocOrErr.takeError();
7465
7466 auto ToFieldOrErr = import(E->getField());
7467 if (!ToFieldOrErr)
7468 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007469
7470 return CXXDefaultInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007471 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007472}
7473
Balazs Keri3b30d652018-10-19 13:32:20 +00007474ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7475 auto Imp = importSeq(
7476 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7477 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7478 if (!Imp)
7479 return Imp.takeError();
7480
7481 QualType ToType;
7482 Expr *ToSubExpr;
7483 TypeSourceInfo *ToTypeInfoAsWritten;
7484 SourceLocation ToOperatorLoc, ToRParenLoc;
7485 SourceRange ToAngleBrackets;
7486 std::tie(
7487 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7488 ToAngleBrackets) = *Imp;
7489
Sean Callanandd2c1742016-05-16 20:48:03 +00007490 ExprValueKind VK = E->getValueKind();
7491 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007492 auto ToBasePathOrErr = ImportCastPath(E);
7493 if (!ToBasePathOrErr)
7494 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007495
Sean Callanandd2c1742016-05-16 20:48:03 +00007496 if (isa<CXXStaticCastExpr>(E)) {
7497 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007498 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7499 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007500 } else if (isa<CXXDynamicCastExpr>(E)) {
7501 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007502 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7503 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007504 } else if (isa<CXXReinterpretCastExpr>(E)) {
7505 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007506 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7507 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007508 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007509 return CXXConstCastExpr::Create(
7510 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7511 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007512 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007513 llvm_unreachable("Unknown cast type");
7514 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007515 }
7516}
7517
Balazs Keri3b30d652018-10-19 13:32:20 +00007518ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007519 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007520 auto Imp = importSeq(
7521 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7522 if (!Imp)
7523 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007524
Balazs Keri3b30d652018-10-19 13:32:20 +00007525 QualType ToType;
7526 SourceLocation ToExprLoc;
7527 NonTypeTemplateParmDecl *ToParameter;
7528 Expr *ToReplacement;
7529 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007530
7531 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007532 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007533}
7534
Balazs Keri3b30d652018-10-19 13:32:20 +00007535ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7536 auto Imp = importSeq(
7537 E->getType(), E->getBeginLoc(), E->getEndLoc());
7538 if (!Imp)
7539 return Imp.takeError();
7540
7541 QualType ToType;
7542 SourceLocation ToBeginLoc, ToEndLoc;
7543 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007544
7545 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007546 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7547 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007548
7549 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7550 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007551 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007552
7553 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007554 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7555 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007556}
7557
Balazs Keri3b30d652018-10-19 13:32:20 +00007558ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7559 ExpectedType ToTypeOrErr = import(E->getType());
7560 if (!ToTypeOrErr)
7561 return ToTypeOrErr.takeError();
7562
7563 auto ToSourceRangeOrErr = import(E->getSourceRange());
7564 if (!ToSourceRangeOrErr)
7565 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007566
7567 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007568 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7569 return new (Importer.getToContext()) CXXTypeidExpr(
7570 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7571 else
7572 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007573 }
7574
Balazs Keri3b30d652018-10-19 13:32:20 +00007575 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7576 if (!ToExprOperandOrErr)
7577 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007578
Balazs Keri3b30d652018-10-19 13:32:20 +00007579 return new (Importer.getToContext()) CXXTypeidExpr(
7580 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007581}
7582
Lang Hames19e07e12017-06-20 21:06:00 +00007583void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
7584 CXXMethodDecl *FromMethod) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007585 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7586 if (auto ImportedOrErr = import(FromOverriddenMethod))
7587 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7588 (*ImportedOrErr)->getCanonicalDecl()));
7589 else
7590 consumeError(ImportedOrErr.takeError());
7591 }
Lang Hames19e07e12017-06-20 21:06:00 +00007592}
7593
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007594ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00007595 ASTContext &FromContext, FileManager &FromFileManager,
Gabor Marton54058b52018-12-17 13:53:12 +00007596 bool MinimalImport,
7597 ASTImporterLookupTable *LookupTable)
7598 : LookupTable(LookupTable), ToContext(ToContext), FromContext(FromContext),
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007599 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
7600 Minimal(MinimalImport) {
Gabor Marton54058b52018-12-17 13:53:12 +00007601
7602 ImportedDecls[FromContext.getTranslationUnitDecl()] =
7603 ToContext.getTranslationUnitDecl();
Douglas Gregor62d311f2010-02-09 19:21:46 +00007604}
7605
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007606ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007607
Gabor Marton54058b52018-12-17 13:53:12 +00007608Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) {
7609 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
7610 "Try to get field index for non-field.");
7611
7612 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
7613 if (!Owner)
7614 return None;
7615
7616 unsigned Index = 0;
7617 for (const auto *D : Owner->decls()) {
7618 if (D == F)
7619 return Index;
7620
7621 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
7622 ++Index;
7623 }
7624
7625 llvm_unreachable("Field was not found in its parent context.");
7626
7627 return None;
7628}
7629
7630ASTImporter::FoundDeclsTy
7631ASTImporter::findDeclsInToCtx(DeclContext *DC, DeclarationName Name) {
7632 // We search in the redecl context because of transparent contexts.
7633 // E.g. a simple C language enum is a transparent context:
7634 // enum E { A, B };
7635 // Now if we had a global variable in the TU
7636 // int A;
7637 // then the enum constant 'A' and the variable 'A' violates ODR.
7638 // We can diagnose this only if we search in the redecl context.
7639 DeclContext *ReDC = DC->getRedeclContext();
7640 if (LookupTable) {
7641 ASTImporterLookupTable::LookupResult LookupResult =
7642 LookupTable->lookup(ReDC, Name);
7643 return FoundDeclsTy(LookupResult.begin(), LookupResult.end());
7644 } else {
7645 // FIXME Can we remove this kind of lookup?
7646 // Or lldb really needs this C/C++ lookup?
7647 FoundDeclsTy Result;
7648 ReDC->localUncachedLookup(Name, Result);
7649 return Result;
7650 }
7651}
7652
7653void ASTImporter::AddToLookupTable(Decl *ToD) {
7654 if (LookupTable)
7655 if (auto *ToND = dyn_cast<NamedDecl>(ToD))
7656 LookupTable->add(ToND);
7657}
7658
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007659Expected<QualType> ASTImporter::Import_New(QualType FromT) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007660 if (FromT.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007661 return QualType{};
John McCall424cec92011-01-19 06:33:43 +00007662
Balazs Keri3b30d652018-10-19 13:32:20 +00007663 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007664
7665 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00007666 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00007667 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007668 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00007669 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00007670
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007671 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00007672 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007673 ExpectedType ToTOrErr = Importer.Visit(FromTy);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007674 if (!ToTOrErr)
7675 return ToTOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007676
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007677 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007678 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007679
Balazs Keri3b30d652018-10-19 13:32:20 +00007680 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00007681}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007682QualType ASTImporter::Import(QualType From) {
7683 llvm::Expected<QualType> To = Import_New(From);
7684 if (To)
7685 return *To;
7686 else
7687 llvm::consumeError(To.takeError());
7688 return {};
7689}
Douglas Gregor96e578d2010-02-05 17:54:41 +00007690
Balazs Keri4a3d7582018-11-27 18:36:31 +00007691Expected<TypeSourceInfo *> ASTImporter::Import_New(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007692 if (!FromTSI)
7693 return FromTSI;
7694
7695 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00007696 // on the type and a single location. Implement a real version of this.
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007697 ExpectedType TOrErr = Import_New(FromTSI->getType());
7698 if (!TOrErr)
7699 return TOrErr.takeError();
7700 ExpectedSLoc BeginLocOrErr = Import_New(FromTSI->getTypeLoc().getBeginLoc());
7701 if (!BeginLocOrErr)
7702 return BeginLocOrErr.takeError();
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007703
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007704 return ToContext.getTrivialTypeSourceInfo(*TOrErr, *BeginLocOrErr);
7705}
7706TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *From) {
7707 llvm::Expected<TypeSourceInfo *> To = Import_New(From);
7708 if (To)
7709 return *To;
7710 else
7711 llvm::consumeError(To.takeError());
7712 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007713}
7714
Balazs Keri4a3d7582018-11-27 18:36:31 +00007715Expected<Attr *> ASTImporter::Import_New(const Attr *FromAttr) {
Davide Italianofaee83d2018-11-28 19:15:23 +00007716 Attr *ToAttr = FromAttr->clone(ToContext);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007717 if (auto ToRangeOrErr = Import_New(FromAttr->getRange()))
7718 ToAttr->setRange(*ToRangeOrErr);
7719 else
7720 return ToRangeOrErr.takeError();
7721
Davide Italianofaee83d2018-11-28 19:15:23 +00007722 return ToAttr;
Balazs Kerideaf7ab2018-11-28 13:21:26 +00007723}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007724Attr *ASTImporter::Import(const Attr *From) {
7725 llvm::Expected<Attr *> To = Import_New(From);
7726 if (To)
7727 return *To;
7728 else
7729 llvm::consumeError(To.takeError());
7730 return nullptr;
7731}
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007732
Gabor Martonbe77a982018-12-12 11:22:55 +00007733Decl *ASTImporter::GetAlreadyImportedOrNull(const Decl *FromD) const {
7734 auto Pos = ImportedDecls.find(FromD);
7735 if (Pos != ImportedDecls.end())
7736 return Pos->second;
7737 else
Sean Callanan59721b32015-04-28 18:41:46 +00007738 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007739}
7740
Gabor Marton458d1452019-02-14 13:07:03 +00007741TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) {
7742 auto FromDPos = ImportedFromDecls.find(ToD);
7743 if (FromDPos == ImportedFromDecls.end())
7744 return nullptr;
7745 return FromDPos->second->getTranslationUnitDecl();
7746}
7747
Balazs Keri4a3d7582018-11-27 18:36:31 +00007748Expected<Decl *> ASTImporter::Import_New(Decl *FromD) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007749 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007750 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007751
Douglas Gregord451ea92011-07-29 23:31:30 +00007752 ASTNodeImporter Importer(*this);
7753
Gabor Marton26f72a92018-07-12 09:42:05 +00007754 // Check whether we've already imported this declaration.
7755 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7756 if (ToD) {
7757 // If FromD has some updated flags after last import, apply it
7758 updateFlags(FromD, ToD);
Douglas Gregord451ea92011-07-29 23:31:30 +00007759 return ToD;
7760 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007761
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007762 // Import the declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00007763 ExpectedDecl ToDOrErr = Importer.Visit(FromD);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007764 if (!ToDOrErr)
7765 return ToDOrErr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007766 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007767
Gabor Marton7f8c4002019-03-19 13:34:10 +00007768 // FIXME Use getImportDeclErrorIfAny() here (and return with the error) once
7769 // the error handling is finished in GetImportedOrCreateSpecialDecl().
7770 if (!ToD) {
7771 return nullptr;
7772 }
7773
Gabor Marton54058b52018-12-17 13:53:12 +00007774 // Once the decl is connected to the existing declarations, i.e. when the
7775 // redecl chain is properly set then we populate the lookup again.
7776 // This way the primary context will be able to find all decls.
7777 AddToLookupTable(ToD);
7778
Gabor Marton26f72a92018-07-12 09:42:05 +00007779 // Notify subclasses.
7780 Imported(FromD, ToD);
7781
Gabor Martonac3a5d62018-09-17 12:04:52 +00007782 updateFlags(FromD, ToD);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007783 return ToDOrErr;
7784}
7785Decl *ASTImporter::Import(Decl *From) {
7786 llvm::Expected<Decl *> To = Import_New(From);
7787 if (To)
7788 return *To;
7789 else
7790 llvm::consumeError(To.takeError());
7791 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007792}
7793
Balazs Keri3b30d652018-10-19 13:32:20 +00007794Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007795 if (!FromDC)
7796 return FromDC;
7797
Balazs Keria1f6b102019-04-08 13:59:15 +00007798 ExpectedDecl ToDCOrErr = Import_New(cast<Decl>(FromDC));
7799 if (!ToDCOrErr)
7800 return ToDCOrErr.takeError();
7801 auto *ToDC = cast<DeclContext>(*ToDCOrErr);
Craig Topper36250ad2014-05-12 05:36:57 +00007802
Fangrui Song6907ce22018-07-30 19:24:48 +00007803 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00007804 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007805 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
7806 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007807 if (ToRecord->isCompleteDefinition()) {
7808 // Do nothing.
7809 } else if (FromRecord->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007810 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7811 FromRecord, ToRecord, ASTNodeImporter::IDK_Basic))
7812 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007813 } else {
7814 CompleteDecl(ToRecord);
7815 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007816 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
7817 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007818 if (ToEnum->isCompleteDefinition()) {
7819 // Do nothing.
7820 } else if (FromEnum->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007821 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7822 FromEnum, ToEnum, ASTNodeImporter::IDK_Basic))
7823 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007824 } else {
7825 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00007826 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007827 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
7828 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007829 if (ToClass->getDefinition()) {
7830 // Do nothing.
7831 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007832 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7833 FromDef, ToClass, ASTNodeImporter::IDK_Basic))
7834 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007835 } else {
7836 CompleteDecl(ToClass);
7837 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007838 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
7839 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007840 if (ToProto->getDefinition()) {
7841 // Do nothing.
7842 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007843 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7844 FromDef, ToProto, ASTNodeImporter::IDK_Basic))
7845 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007846 } else {
7847 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00007848 }
Douglas Gregor95d82832012-01-24 18:36:04 +00007849 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007850
Douglas Gregor95d82832012-01-24 18:36:04 +00007851 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007852}
7853
Balazs Keri4a3d7582018-11-27 18:36:31 +00007854Expected<Expr *> ASTImporter::Import_New(Expr *FromE) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007855 if (ExpectedStmt ToSOrErr = Import_New(cast_or_null<Stmt>(FromE)))
7856 return cast_or_null<Expr>(*ToSOrErr);
7857 else
7858 return ToSOrErr.takeError();
Balazs Keri4a3d7582018-11-27 18:36:31 +00007859}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007860Expr *ASTImporter::Import(Expr *From) {
7861 llvm::Expected<Expr *> To = Import_New(From);
7862 if (To)
7863 return *To;
7864 else
7865 llvm::consumeError(To.takeError());
7866 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007867}
7868
Balazs Keri4a3d7582018-11-27 18:36:31 +00007869Expected<Stmt *> ASTImporter::Import_New(Stmt *FromS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007870 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007871 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007872
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007873 // Check whether we've already imported this statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007874 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7875 if (Pos != ImportedStmts.end())
7876 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00007877
Balazs Keri3b30d652018-10-19 13:32:20 +00007878 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007879 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007880 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007881 if (!ToSOrErr)
7882 return ToSOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007883
Balazs Keri3b30d652018-10-19 13:32:20 +00007884 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00007885 auto *FromE = cast<Expr>(FromS);
7886 // Copy ExprBitfields, which may not be handled in Expr subclasses
7887 // constructors.
7888 ToE->setValueKind(FromE->getValueKind());
7889 ToE->setObjectKind(FromE->getObjectKind());
7890 ToE->setTypeDependent(FromE->isTypeDependent());
7891 ToE->setValueDependent(FromE->isValueDependent());
7892 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
7893 ToE->setContainsUnexpandedParameterPack(
7894 FromE->containsUnexpandedParameterPack());
7895 }
7896
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007897 // Record the imported statement object.
Balazs Keri3b30d652018-10-19 13:32:20 +00007898 ImportedStmts[FromS] = *ToSOrErr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007899 return ToSOrErr;
7900}
7901Stmt *ASTImporter::Import(Stmt *From) {
7902 llvm::Expected<Stmt *> To = Import_New(From);
7903 if (To)
7904 return *To;
7905 else
7906 llvm::consumeError(To.takeError());
7907 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007908}
7909
Balazs Keri4a3d7582018-11-27 18:36:31 +00007910Expected<NestedNameSpecifier *>
7911ASTImporter::Import_New(NestedNameSpecifier *FromNNS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007912 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00007913 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007914
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007915 NestedNameSpecifier *Prefix;
7916 if (Error Err = importInto(Prefix, FromNNS->getPrefix()))
7917 return std::move(Err);
Douglas Gregor90ebf252011-04-27 16:48:40 +00007918
7919 switch (FromNNS->getKind()) {
7920 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007921 assert(FromNNS->getAsIdentifier() && "NNS should contain identifier.");
7922 return NestedNameSpecifier::Create(ToContext, Prefix,
7923 Import(FromNNS->getAsIdentifier()));
Douglas Gregor90ebf252011-04-27 16:48:40 +00007924
7925 case NestedNameSpecifier::Namespace:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007926 if (ExpectedDecl NSOrErr = Import_New(FromNNS->getAsNamespace())) {
7927 return NestedNameSpecifier::Create(ToContext, Prefix,
7928 cast<NamespaceDecl>(*NSOrErr));
7929 } else
7930 return NSOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00007931
7932 case NestedNameSpecifier::NamespaceAlias:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007933 if (ExpectedDecl NSADOrErr = Import_New(FromNNS->getAsNamespaceAlias()))
7934 return NestedNameSpecifier::Create(ToContext, Prefix,
7935 cast<NamespaceAliasDecl>(*NSADOrErr));
7936 else
7937 return NSADOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00007938
7939 case NestedNameSpecifier::Global:
7940 return NestedNameSpecifier::GlobalSpecifier(ToContext);
7941
Nikola Smiljanic67860242014-09-26 00:28:20 +00007942 case NestedNameSpecifier::Super:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007943 if (ExpectedDecl RDOrErr = Import_New(FromNNS->getAsRecordDecl()))
7944 return NestedNameSpecifier::SuperSpecifier(ToContext,
7945 cast<CXXRecordDecl>(*RDOrErr));
7946 else
7947 return RDOrErr.takeError();
Nikola Smiljanic67860242014-09-26 00:28:20 +00007948
Douglas Gregor90ebf252011-04-27 16:48:40 +00007949 case NestedNameSpecifier::TypeSpec:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007950 case NestedNameSpecifier::TypeSpecWithTemplate:
7951 if (Expected<QualType> TyOrErr =
7952 Import_New(QualType(FromNNS->getAsType(), 0u))) {
7953 bool TSTemplate =
7954 FromNNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate;
7955 return NestedNameSpecifier::Create(ToContext, Prefix, TSTemplate,
7956 TyOrErr->getTypePtr());
7957 } else {
7958 return TyOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00007959 }
Douglas Gregor90ebf252011-04-27 16:48:40 +00007960 }
7961
7962 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00007963}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007964NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *From) {
7965 llvm::Expected<NestedNameSpecifier *> To = Import_New(From);
7966 if (To)
7967 return *To;
7968 else
7969 llvm::consumeError(To.takeError());
7970 return nullptr;
7971}
Douglas Gregor62d311f2010-02-09 19:21:46 +00007972
Balazs Keri4a3d7582018-11-27 18:36:31 +00007973Expected<NestedNameSpecifierLoc>
7974ASTImporter::Import_New(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007975 // Copied from NestedNameSpecifier mostly.
7976 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
7977 NestedNameSpecifierLoc NNS = FromNNS;
7978
7979 // Push each of the nested-name-specifiers's onto a stack for
7980 // serialization in reverse order.
7981 while (NNS) {
7982 NestedNames.push_back(NNS);
7983 NNS = NNS.getPrefix();
7984 }
7985
7986 NestedNameSpecifierLocBuilder Builder;
7987
7988 while (!NestedNames.empty()) {
7989 NNS = NestedNames.pop_back_val();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007990 NestedNameSpecifier *Spec;
7991 if (Error Err = importInto(Spec, NNS.getNestedNameSpecifier()))
7992 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007993
7994 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007995
7996 SourceLocation ToLocalBeginLoc, ToLocalEndLoc;
7997 if (Kind != NestedNameSpecifier::Super) {
7998 if (Error Err = importInto(ToLocalBeginLoc, NNS.getLocalBeginLoc()))
7999 return std::move(Err);
8000
8001 if (Kind != NestedNameSpecifier::Global)
8002 if (Error Err = importInto(ToLocalEndLoc, NNS.getLocalEndLoc()))
8003 return std::move(Err);
8004 }
8005
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008006 switch (Kind) {
8007 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008008 Builder.Extend(getToContext(), Spec->getAsIdentifier(), ToLocalBeginLoc,
8009 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008010 break;
8011
8012 case NestedNameSpecifier::Namespace:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008013 Builder.Extend(getToContext(), Spec->getAsNamespace(), ToLocalBeginLoc,
8014 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008015 break;
8016
8017 case NestedNameSpecifier::NamespaceAlias:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008018 Builder.Extend(getToContext(), Spec->getAsNamespaceAlias(),
8019 ToLocalBeginLoc, ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008020 break;
8021
8022 case NestedNameSpecifier::TypeSpec:
8023 case NestedNameSpecifier::TypeSpecWithTemplate: {
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008024 SourceLocation ToTLoc;
8025 if (Error Err = importInto(ToTLoc, NNS.getTypeLoc().getBeginLoc()))
8026 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008027 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008028 QualType(Spec->getAsType(), 0), ToTLoc);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008029 Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(),
8030 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008031 break;
8032 }
8033
8034 case NestedNameSpecifier::Global:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008035 Builder.MakeGlobal(getToContext(), ToLocalBeginLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008036 break;
8037
8038 case NestedNameSpecifier::Super: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008039 auto ToSourceRangeOrErr = Import_New(NNS.getSourceRange());
8040 if (!ToSourceRangeOrErr)
8041 return ToSourceRangeOrErr.takeError();
8042
8043 Builder.MakeSuper(getToContext(), Spec->getAsRecordDecl(),
8044 ToSourceRangeOrErr->getBegin(),
8045 ToSourceRangeOrErr->getEnd());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008046 }
8047 }
8048 }
8049
8050 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00008051}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008052NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc From) {
8053 llvm::Expected<NestedNameSpecifierLoc> To = Import_New(From);
8054 if (To)
8055 return *To;
8056 else
8057 llvm::consumeError(To.takeError());
8058 return {};
8059}
Douglas Gregor14454802011-02-25 02:25:35 +00008060
Balazs Keri4a3d7582018-11-27 18:36:31 +00008061Expected<TemplateName> ASTImporter::Import_New(TemplateName From) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00008062 switch (From.getKind()) {
8063 case TemplateName::Template:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008064 if (ExpectedDecl ToTemplateOrErr = Import_New(From.getAsTemplateDecl()))
8065 return TemplateName(cast<TemplateDecl>(*ToTemplateOrErr));
8066 else
8067 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008068
Douglas Gregore2e50d332010-12-01 01:36:18 +00008069 case TemplateName::OverloadedTemplate: {
8070 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
8071 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008072 for (auto *I : *FromStorage) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008073 if (auto ToOrErr = Import_New(I))
8074 ToTemplates.addDecl(cast<NamedDecl>(*ToOrErr));
Douglas Gregore2e50d332010-12-01 01:36:18 +00008075 else
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008076 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008077 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008078 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008079 ToTemplates.end());
8080 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008081
Douglas Gregore2e50d332010-12-01 01:36:18 +00008082 case TemplateName::QualifiedTemplate: {
8083 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008084 auto QualifierOrErr = Import_New(QTN->getQualifier());
8085 if (!QualifierOrErr)
8086 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008087
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008088 if (ExpectedDecl ToTemplateOrErr = Import_New(From.getAsTemplateDecl()))
8089 return ToContext.getQualifiedTemplateName(
8090 *QualifierOrErr, QTN->hasTemplateKeyword(),
8091 cast<TemplateDecl>(*ToTemplateOrErr));
8092 else
8093 return ToTemplateOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008094 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008095
Douglas Gregore2e50d332010-12-01 01:36:18 +00008096 case TemplateName::DependentTemplate: {
8097 DependentTemplateName *DTN = From.getAsDependentTemplateName();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008098 auto QualifierOrErr = Import_New(DTN->getQualifier());
8099 if (!QualifierOrErr)
8100 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008101
Douglas Gregore2e50d332010-12-01 01:36:18 +00008102 if (DTN->isIdentifier()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008103 return ToContext.getDependentTemplateName(*QualifierOrErr,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008104 Import(DTN->getIdentifier()));
8105 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008106
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008107 return ToContext.getDependentTemplateName(*QualifierOrErr,
8108 DTN->getOperator());
Douglas Gregore2e50d332010-12-01 01:36:18 +00008109 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008110
8111 case TemplateName::SubstTemplateTemplateParm: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008112 SubstTemplateTemplateParmStorage *Subst =
8113 From.getAsSubstTemplateTemplateParm();
8114 ExpectedDecl ParamOrErr = Import_New(Subst->getParameter());
8115 if (!ParamOrErr)
8116 return ParamOrErr.takeError();
John McCalld9dfe3a2011-06-30 08:33:18 +00008117
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008118 auto ReplacementOrErr = Import_New(Subst->getReplacement());
8119 if (!ReplacementOrErr)
8120 return ReplacementOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008121
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008122 return ToContext.getSubstTemplateTemplateParm(
8123 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ReplacementOrErr);
John McCalld9dfe3a2011-06-30 08:33:18 +00008124 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008125
Douglas Gregor5590be02011-01-15 06:45:20 +00008126 case TemplateName::SubstTemplateTemplateParmPack: {
8127 SubstTemplateTemplateParmPackStorage *SubstPack
8128 = From.getAsSubstTemplateTemplateParmPack();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008129 ExpectedDecl ParamOrErr = Import_New(SubstPack->getParameterPack());
8130 if (!ParamOrErr)
8131 return ParamOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008132
Douglas Gregor5590be02011-01-15 06:45:20 +00008133 ASTNodeImporter Importer(*this);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008134 auto ArgPackOrErr =
8135 Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
8136 if (!ArgPackOrErr)
8137 return ArgPackOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008138
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008139 return ToContext.getSubstTemplateTemplateParmPack(
8140 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ArgPackOrErr);
Douglas Gregor5590be02011-01-15 06:45:20 +00008141 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008142 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008143
Douglas Gregore2e50d332010-12-01 01:36:18 +00008144 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008145}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008146TemplateName ASTImporter::Import(TemplateName From) {
8147 llvm::Expected<TemplateName> To = Import_New(From);
8148 if (To)
8149 return *To;
8150 else
8151 llvm::consumeError(To.takeError());
8152 return {};
8153}
Douglas Gregore2e50d332010-12-01 01:36:18 +00008154
Balazs Keri4a3d7582018-11-27 18:36:31 +00008155Expected<SourceLocation> ASTImporter::Import_New(SourceLocation FromLoc) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008156 if (FromLoc.isInvalid())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008157 return SourceLocation{};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008158
Douglas Gregor811663e2010-02-10 00:15:17 +00008159 SourceManager &FromSM = FromContext.getSourceManager();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008160 bool IsBuiltin = FromSM.isWrittenInBuiltinFile(FromLoc);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008161
Douglas Gregor811663e2010-02-10 00:15:17 +00008162 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008163 Expected<FileID> ToFileIDOrErr = Import_New(Decomposed.first, IsBuiltin);
8164 if (!ToFileIDOrErr)
8165 return ToFileIDOrErr.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008166 SourceManager &ToSM = ToContext.getSourceManager();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008167 return ToSM.getComposedLoc(*ToFileIDOrErr, Decomposed.second);
8168}
8169SourceLocation ASTImporter::Import(SourceLocation From) {
8170 llvm::Expected<SourceLocation> To = Import_New(From);
8171 if (To)
8172 return *To;
8173 else
8174 llvm::consumeError(To.takeError());
8175 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008176}
8177
Balazs Keri4a3d7582018-11-27 18:36:31 +00008178Expected<SourceRange> ASTImporter::Import_New(SourceRange FromRange) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008179 SourceLocation ToBegin, ToEnd;
8180 if (Error Err = importInto(ToBegin, FromRange.getBegin()))
8181 return std::move(Err);
8182 if (Error Err = importInto(ToEnd, FromRange.getEnd()))
8183 return std::move(Err);
8184
8185 return SourceRange(ToBegin, ToEnd);
Balazs Keri4a3d7582018-11-27 18:36:31 +00008186}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008187SourceRange ASTImporter::Import(SourceRange From) {
8188 llvm::Expected<SourceRange> To = Import_New(From);
8189 if (To)
8190 return *To;
8191 else
8192 llvm::consumeError(To.takeError());
8193 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008194}
8195
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008196Expected<FileID> ASTImporter::Import_New(FileID FromID, bool IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008197 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008198 if (Pos != ImportedFileIDs.end())
8199 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008200
Douglas Gregor811663e2010-02-10 00:15:17 +00008201 SourceManager &FromSM = FromContext.getSourceManager();
8202 SourceManager &ToSM = ToContext.getSourceManager();
8203 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008204
8205 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008206 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008207 if (FromSLoc.isExpansion()) {
8208 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008209 ExpectedSLoc ToSpLoc = Import_New(FromEx.getSpellingLoc());
8210 if (!ToSpLoc)
8211 return ToSpLoc.takeError();
8212 ExpectedSLoc ToExLocS = Import_New(FromEx.getExpansionLocStart());
8213 if (!ToExLocS)
8214 return ToExLocS.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008215 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8216 SourceLocation MLoc;
8217 if (FromEx.isMacroArgExpansion()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008218 MLoc = ToSM.createMacroArgExpansionLoc(*ToSpLoc, *ToExLocS, TokenLen);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008219 } else {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008220 if (ExpectedSLoc ToExLocE = Import_New(FromEx.getExpansionLocEnd()))
8221 MLoc = ToSM.createExpansionLoc(*ToSpLoc, *ToExLocS, *ToExLocE, TokenLen,
8222 FromEx.isExpansionTokenRange());
8223 else
8224 return ToExLocE.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008225 }
8226 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008227 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008228 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008229
8230 if (!IsBuiltin) {
8231 // Include location of this file.
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008232 ExpectedSLoc ToIncludeLoc =
8233 Import_New(FromSLoc.getFile().getIncludeLoc());
8234 if (!ToIncludeLoc)
8235 return ToIncludeLoc.takeError();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008236
8237 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8238 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8239 // disk again
8240 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8241 // than mmap the files several times.
8242 const FileEntry *Entry =
8243 ToFileManager.getFile(Cache->OrigEntry->getName());
8244 // FIXME: The filename may be a virtual name that does probably not
8245 // point to a valid file and we get no Entry here. In this case try with
8246 // the memory buffer below.
8247 if (Entry)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008248 ToID = ToSM.createFileID(Entry, *ToIncludeLoc,
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008249 FromSLoc.getFile().getFileCharacteristic());
8250 }
Balazs Keri9cf39df2019-02-27 16:31:48 +00008251 }
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008252
8253 if (ToID.isInvalid() || IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008254 // FIXME: We want to re-use the existing MemoryBuffer!
Balazs Keri9cf39df2019-02-27 16:31:48 +00008255 bool Invalid = true;
8256 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(
8257 FromContext.getDiagnostics(), FromSM, SourceLocation{}, &Invalid);
8258 if (!FromBuf || Invalid)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008259 // FIXME: Use a new error kind?
8260 return llvm::make_error<ImportError>(ImportError::Unknown);
Balazs Keri9cf39df2019-02-27 16:31:48 +00008261
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008262 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8263 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8264 FromBuf->getBufferIdentifier());
8265 ToID = ToSM.createFileID(std::move(ToBuf),
8266 FromSLoc.getFile().getFileCharacteristic());
8267 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008268 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008269
Balazs Keri9cf39df2019-02-27 16:31:48 +00008270 assert(ToID.isValid() && "Unexpected invalid fileID was created.");
8271
Sebastian Redl99219f12010-09-30 01:03:06 +00008272 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00008273 return ToID;
8274}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008275FileID ASTImporter::Import(FileID From, bool IsBuiltin) {
8276 llvm::Expected<FileID> To = Import_New(From, IsBuiltin);
8277 if (To)
8278 return *To;
8279 else
8280 llvm::consumeError(To.takeError());
8281 return {};
8282}
Douglas Gregor811663e2010-02-10 00:15:17 +00008283
Balazs Keri4a3d7582018-11-27 18:36:31 +00008284Expected<CXXCtorInitializer *>
8285ASTImporter::Import_New(CXXCtorInitializer *From) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008286 ExpectedExpr ToExprOrErr = Import_New(From->getInit());
8287 if (!ToExprOrErr)
8288 return ToExprOrErr.takeError();
8289
8290 auto LParenLocOrErr = Import_New(From->getLParenLoc());
8291 if (!LParenLocOrErr)
8292 return LParenLocOrErr.takeError();
8293
8294 auto RParenLocOrErr = Import_New(From->getRParenLoc());
8295 if (!RParenLocOrErr)
8296 return RParenLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008297
8298 if (From->isBaseInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008299 auto ToTInfoOrErr = Import_New(From->getTypeSourceInfo());
8300 if (!ToTInfoOrErr)
8301 return ToTInfoOrErr.takeError();
8302
8303 SourceLocation EllipsisLoc;
8304 if (From->isPackExpansion())
8305 if (Error Err = importInto(EllipsisLoc, From->getEllipsisLoc()))
8306 return std::move(Err);
Davide Italianofaee83d2018-11-28 19:15:23 +00008307
8308 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008309 ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr,
8310 *ToExprOrErr, *RParenLocOrErr, EllipsisLoc);
Davide Italianofaee83d2018-11-28 19:15:23 +00008311 } else if (From->isMemberInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008312 ExpectedDecl ToFieldOrErr = Import_New(From->getMember());
8313 if (!ToFieldOrErr)
8314 return ToFieldOrErr.takeError();
8315
8316 auto MemberLocOrErr = Import_New(From->getMemberLocation());
8317 if (!MemberLocOrErr)
8318 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008319
8320 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008321 ToContext, cast_or_null<FieldDecl>(*ToFieldOrErr), *MemberLocOrErr,
8322 *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008323 } else if (From->isIndirectMemberInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008324 ExpectedDecl ToIFieldOrErr = Import_New(From->getIndirectMember());
8325 if (!ToIFieldOrErr)
8326 return ToIFieldOrErr.takeError();
8327
8328 auto MemberLocOrErr = Import_New(From->getMemberLocation());
8329 if (!MemberLocOrErr)
8330 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008331
8332 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008333 ToContext, cast_or_null<IndirectFieldDecl>(*ToIFieldOrErr),
8334 *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008335 } else if (From->isDelegatingInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008336 auto ToTInfoOrErr = Import_New(From->getTypeSourceInfo());
8337 if (!ToTInfoOrErr)
8338 return ToTInfoOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008339
8340 return new (ToContext)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008341 CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr,
8342 *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008343 } else {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008344 // FIXME: assert?
8345 return make_error<ImportError>();
Davide Italianofaee83d2018-11-28 19:15:23 +00008346 }
Balazs Kerideaf7ab2018-11-28 13:21:26 +00008347}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008348CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
8349 llvm::Expected<CXXCtorInitializer *> To = Import_New(From);
8350 if (To)
8351 return *To;
8352 else
8353 llvm::consumeError(To.takeError());
8354 return nullptr;
8355}
Sean Callanandd2c1742016-05-16 20:48:03 +00008356
Balazs Keri4a3d7582018-11-27 18:36:31 +00008357Expected<CXXBaseSpecifier *>
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008358ASTImporter::Import_New(const CXXBaseSpecifier *BaseSpec) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00008359 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8360 if (Pos != ImportedCXXBaseSpecifiers.end())
8361 return Pos->second;
8362
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008363 Expected<SourceRange> ToSourceRange = Import_New(BaseSpec->getSourceRange());
8364 if (!ToSourceRange)
8365 return ToSourceRange.takeError();
8366 Expected<TypeSourceInfo *> ToTSI = Import_New(BaseSpec->getTypeSourceInfo());
8367 if (!ToTSI)
8368 return ToTSI.takeError();
8369 ExpectedSLoc ToEllipsisLoc = Import_New(BaseSpec->getEllipsisLoc());
8370 if (!ToEllipsisLoc)
8371 return ToEllipsisLoc.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00008372 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008373 *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8374 BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00008375 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8376 return Imported;
8377}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008378CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *From) {
8379 llvm::Expected<CXXBaseSpecifier *> To = Import_New(From);
8380 if (To)
8381 return *To;
8382 else
8383 llvm::consumeError(To.takeError());
8384 return nullptr;
8385}
Aleksei Sidorina693b372016-09-28 10:16:56 +00008386
Balazs Keri3b30d652018-10-19 13:32:20 +00008387Error ASTImporter::ImportDefinition_New(Decl *From) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008388 Decl *To = Import(From);
8389 if (!To)
Balazs Keri3b30d652018-10-19 13:32:20 +00008390 return llvm::make_error<ImportError>();
Fangrui Song6907ce22018-07-30 19:24:48 +00008391
Don Hintonf170dff2019-03-19 06:14:14 +00008392 auto *FromDC = cast<DeclContext>(From);
8393 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008394
Don Hintonf170dff2019-03-19 06:14:14 +00008395 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
8396 if (!ToRecord->getDefinition()) {
8397 return Importer.ImportDefinition(
8398 cast<RecordDecl>(FromDC), ToRecord,
8399 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00008400 }
Douglas Gregor0a791672011-01-18 03:11:38 +00008401 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008402
Don Hintonf170dff2019-03-19 06:14:14 +00008403 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
8404 if (!ToEnum->getDefinition()) {
8405 return Importer.ImportDefinition(
8406 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
8407 }
8408 }
8409
8410 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
8411 if (!ToIFace->getDefinition()) {
8412 return Importer.ImportDefinition(
8413 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8414 ASTNodeImporter::IDK_Everything);
8415 }
8416 }
8417
8418 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
8419 if (!ToProto->getDefinition()) {
8420 return Importer.ImportDefinition(
8421 cast<ObjCProtocolDecl>(FromDC), ToProto,
8422 ASTNodeImporter::IDK_Everything);
8423 }
8424 }
8425
8426 return Importer.ImportDeclContext(FromDC, true);
Balazs Keri3b30d652018-10-19 13:32:20 +00008427}
8428
8429void ASTImporter::ImportDefinition(Decl *From) {
8430 Error Err = ImportDefinition_New(From);
8431 llvm::consumeError(std::move(Err));
Douglas Gregor0a791672011-01-18 03:11:38 +00008432}
8433
Balazs Keri4a3d7582018-11-27 18:36:31 +00008434Expected<DeclarationName> ASTImporter::Import_New(DeclarationName FromName) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008435 if (!FromName)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008436 return DeclarationName{};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008437
8438 switch (FromName.getNameKind()) {
8439 case DeclarationName::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008440 return DeclarationName(Import(FromName.getAsIdentifierInfo()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008441
8442 case DeclarationName::ObjCZeroArgSelector:
8443 case DeclarationName::ObjCOneArgSelector:
8444 case DeclarationName::ObjCMultiArgSelector:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008445 if (auto ToSelOrErr = Import_New(FromName.getObjCSelector()))
8446 return DeclarationName(*ToSelOrErr);
8447 else
8448 return ToSelOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008449
8450 case DeclarationName::CXXConstructorName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008451 if (auto ToTyOrErr = Import_New(FromName.getCXXNameType()))
8452 return ToContext.DeclarationNames.getCXXConstructorName(
8453 ToContext.getCanonicalType(*ToTyOrErr));
8454 else
8455 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008456 }
8457
8458 case DeclarationName::CXXDestructorName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008459 if (auto ToTyOrErr = Import_New(FromName.getCXXNameType()))
8460 return ToContext.DeclarationNames.getCXXDestructorName(
8461 ToContext.getCanonicalType(*ToTyOrErr));
8462 else
8463 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008464 }
8465
Richard Smith35845152017-02-07 01:37:30 +00008466 case DeclarationName::CXXDeductionGuideName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008467 if (auto ToTemplateOrErr =
8468 Import_New(FromName.getCXXDeductionGuideTemplate()))
8469 return ToContext.DeclarationNames.getCXXDeductionGuideName(
8470 cast<TemplateDecl>(*ToTemplateOrErr));
8471 else
8472 return ToTemplateOrErr.takeError();
Richard Smith35845152017-02-07 01:37:30 +00008473 }
8474
Douglas Gregor96e578d2010-02-05 17:54:41 +00008475 case DeclarationName::CXXConversionFunctionName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008476 if (auto ToTyOrErr = Import_New(FromName.getCXXNameType()))
8477 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8478 ToContext.getCanonicalType(*ToTyOrErr));
8479 else
8480 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008481 }
8482
8483 case DeclarationName::CXXOperatorName:
8484 return ToContext.DeclarationNames.getCXXOperatorName(
8485 FromName.getCXXOverloadedOperator());
8486
8487 case DeclarationName::CXXLiteralOperatorName:
8488 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008489 Import(FromName.getCXXLiteralIdentifier()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008490
8491 case DeclarationName::CXXUsingDirective:
8492 // FIXME: STATICS!
8493 return DeclarationName::getUsingDirectiveName();
8494 }
8495
David Blaikiee4d798f2012-01-20 21:50:17 +00008496 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008497}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008498DeclarationName ASTImporter::Import(DeclarationName From) {
8499 llvm::Expected<DeclarationName> To = Import_New(From);
8500 if (To)
8501 return *To;
8502 else
8503 llvm::consumeError(To.takeError());
8504 return {};
8505}
Douglas Gregor96e578d2010-02-05 17:54:41 +00008506
Douglas Gregore2e50d332010-12-01 01:36:18 +00008507IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008508 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008509 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008510
Sean Callananf94ef1d2016-05-14 06:11:19 +00008511 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8512
8513 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8514 ToId->setBuiltinID(FromId->getBuiltinID());
8515
8516 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008517}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008518
Balazs Keri4a3d7582018-11-27 18:36:31 +00008519Expected<Selector> ASTImporter::Import_New(Selector FromSel) {
Douglas Gregor43f54792010-02-17 02:12:47 +00008520 if (FromSel.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008521 return Selector{};
Douglas Gregor43f54792010-02-17 02:12:47 +00008522
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008523 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008524 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8525 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8526 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8527 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8528}
8529
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008530DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
8531 DeclContext *DC,
8532 unsigned IDNS,
8533 NamedDecl **Decls,
8534 unsigned NumDecls) {
8535 return Name;
8536}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008537Selector ASTImporter::Import(Selector From) {
8538 llvm::Expected<Selector> To = Import_New(From);
8539 if (To)
8540 return *To;
8541 else
8542 llvm::consumeError(To.takeError());
8543 return {};
8544}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008545
8546DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008547 if (LastDiagFromFrom)
8548 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8549 FromContext.getDiagnostics());
8550 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008551 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008552}
8553
8554DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008555 if (!LastDiagFromFrom)
8556 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8557 ToContext.getDiagnostics());
8558 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008559 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008560}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008561
Douglas Gregor2e15c842012-02-01 21:00:38 +00008562void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008563 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008564 if (!ID->getDefinition())
8565 ID->startDefinition();
8566 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008567 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008568 if (!PD->getDefinition())
8569 PD->startDefinition();
8570 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008571 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008572 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8573 TD->startDefinition();
8574 TD->setCompleteDefinition(true);
8575 }
8576 }
8577 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008578 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008579 }
8580}
8581
Gabor Marton26f72a92018-07-12 09:42:05 +00008582Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8583 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8584 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8585 "Try to import an already imported Decl");
8586 if (Pos != ImportedDecls.end())
8587 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008588 ImportedDecls[From] = To;
Gabor Marton458d1452019-02-14 13:07:03 +00008589 // This mapping should be maintained only in this function. Therefore do not
8590 // check for additional consistency.
8591 ImportedFromDecls[To] = From;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008592 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00008593}
Douglas Gregorb4964f72010-02-15 23:54:17 +00008594
Douglas Gregordd6006f2012-07-17 21:16:27 +00008595bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
8596 bool Complain) {
Balazs Keria1f6b102019-04-08 13:59:15 +00008597 llvm::DenseMap<const Type *, const Type *>::iterator Pos =
8598 ImportedTypes.find(From.getTypePtr());
8599 if (Pos != ImportedTypes.end()) {
8600 if (ExpectedType ToFromOrErr = Import_New(From)) {
8601 if (ToContext.hasSameType(*ToFromOrErr, To))
8602 return true;
8603 } else {
8604 llvm::consumeError(ToFromOrErr.takeError());
8605 }
8606 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00008607
Douglas Gregordd6006f2012-07-17 21:16:27 +00008608 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00008609 getStructuralEquivalenceKind(*this), false,
8610 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00008611 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00008612}