blob: 1ac4870cabea8ffe66b8b29733dbc4c1b82feab9 [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
Richard Smithb9fb1212019-05-06 03:47:15 +0000178 // Import an Optional<T> by importing the contained T, if any.
179 template<typename T>
180 Expected<Optional<T>> import(Optional<T> From) {
181 if (!From)
182 return Optional<T>();
183 return import(*From);
184 }
185
Balazs Keri3b30d652018-10-19 13:32:20 +0000186 template <class T>
187 Expected<std::tuple<T>>
188 importSeq(const T &From) {
189 Expected<T> ToOrErr = import(From);
190 if (!ToOrErr)
191 return ToOrErr.takeError();
192 return std::make_tuple<T>(std::move(*ToOrErr));
193 }
194
195 // Import multiple objects with a single function call.
196 // This should work for every type for which a variant of `import` exists.
197 // The arguments are processed from left to right and import is stopped on
198 // first error.
199 template <class THead, class... TTail>
200 Expected<std::tuple<THead, TTail...>>
201 importSeq(const THead &FromHead, const TTail &...FromTail) {
202 Expected<std::tuple<THead>> ToHeadOrErr = importSeq(FromHead);
203 if (!ToHeadOrErr)
204 return ToHeadOrErr.takeError();
205 Expected<std::tuple<TTail...>> ToTailOrErr = importSeq(FromTail...);
206 if (!ToTailOrErr)
207 return ToTailOrErr.takeError();
208 return std::tuple_cat(*ToHeadOrErr, *ToTailOrErr);
209 }
210
211// Wrapper for an overload set.
Gabor Marton26f72a92018-07-12 09:42:05 +0000212 template <typename ToDeclT> struct CallOverloadedCreateFun {
213 template <typename... Args>
214 auto operator()(Args &&... args)
215 -> decltype(ToDeclT::Create(std::forward<Args>(args)...)) {
216 return ToDeclT::Create(std::forward<Args>(args)...);
217 }
218 };
219
220 // Always use these functions to create a Decl during import. There are
221 // certain tasks which must be done after the Decl was created, e.g. we
222 // must immediately register that as an imported Decl. The parameter `ToD`
223 // will be set to the newly created Decl or if had been imported before
224 // then to the already imported Decl. Returns a bool value set to true if
225 // the `FromD` had been imported before.
226 template <typename ToDeclT, typename FromDeclT, typename... Args>
227 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
228 Args &&... args) {
229 // There may be several overloads of ToDeclT::Create. We must make sure
230 // to call the one which would be chosen by the arguments, thus we use a
231 // wrapper for the overload set.
232 CallOverloadedCreateFun<ToDeclT> OC;
233 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
234 std::forward<Args>(args)...);
235 }
236 // Use this overload if a special Type is needed to be created. E.g if we
237 // want to create a `TypeAliasDecl` and assign that to a `TypedefNameDecl`
238 // then:
239 // TypedefNameDecl *ToTypedef;
240 // GetImportedOrCreateDecl<TypeAliasDecl>(ToTypedef, FromD, ...);
241 template <typename NewDeclT, typename ToDeclT, typename FromDeclT,
242 typename... Args>
243 LLVM_NODISCARD bool GetImportedOrCreateDecl(ToDeclT *&ToD, FromDeclT *FromD,
244 Args &&... args) {
245 CallOverloadedCreateFun<NewDeclT> OC;
246 return GetImportedOrCreateSpecialDecl(ToD, OC, FromD,
247 std::forward<Args>(args)...);
248 }
249 // Use this version if a special create function must be
250 // used, e.g. CXXRecordDecl::CreateLambda .
251 template <typename ToDeclT, typename CreateFunT, typename FromDeclT,
252 typename... Args>
253 LLVM_NODISCARD bool
254 GetImportedOrCreateSpecialDecl(ToDeclT *&ToD, CreateFunT CreateFun,
255 FromDeclT *FromD, Args &&... args) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000256 // FIXME: This code is needed later.
257 //if (Importer.getImportDeclErrorIfAny(FromD)) {
258 // ToD = nullptr;
259 // return true; // Already imported but with error.
260 //}
Gabor Marton26f72a92018-07-12 09:42:05 +0000261 ToD = cast_or_null<ToDeclT>(Importer.GetAlreadyImportedOrNull(FromD));
262 if (ToD)
263 return true; // Already imported.
264 ToD = CreateFun(std::forward<Args>(args)...);
Gabor Marton54058b52018-12-17 13:53:12 +0000265 // Keep track of imported Decls.
Raphael Isemanne9bc35f2019-04-29 21:02:35 +0000266 Importer.RegisterImportedDecl(FromD, ToD);
Gabor Marton26f72a92018-07-12 09:42:05 +0000267 InitializeImportedDecl(FromD, ToD);
268 return false; // A new Decl is created.
269 }
270
271 void InitializeImportedDecl(Decl *FromD, Decl *ToD) {
Gabor Marton26f72a92018-07-12 09:42:05 +0000272 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
273 if (FromD->hasAttrs())
Balazs Keri57949eb2019-03-25 09:16:39 +0000274 for (const Attr *FromAttr : FromD->getAttrs()) {
275 // FIXME: Return of the error here is not possible until store of
276 // import errors is implemented.
277 auto ToAttrOrErr = import(FromAttr);
278 if (ToAttrOrErr)
279 ToD->addAttr(*ToAttrOrErr);
280 else
281 llvm::consumeError(ToAttrOrErr.takeError());
282 }
Gabor Marton26f72a92018-07-12 09:42:05 +0000283 if (FromD->isUsed())
284 ToD->setIsUsed();
285 if (FromD->isImplicit())
286 ToD->setImplicit();
287 }
288
Gabor Martondd59d272019-03-19 14:04:50 +0000289 // Check if we have found an existing definition. Returns with that
290 // definition if yes, otherwise returns null.
291 Decl *FindAndMapDefinition(FunctionDecl *D, FunctionDecl *FoundFunction) {
292 const FunctionDecl *Definition = nullptr;
293 if (D->doesThisDeclarationHaveABody() &&
294 FoundFunction->hasBody(Definition))
295 return Importer.MapImported(D, const_cast<FunctionDecl *>(Definition));
296 return nullptr;
297 }
298
Douglas Gregor96e578d2010-02-05 17:54:41 +0000299 public:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000300 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
Gabor Marton344b0992018-05-16 11:48:11 +0000301
Balazs Keri3b30d652018-10-19 13:32:20 +0000302 using TypeVisitor<ASTNodeImporter, ExpectedType>::Visit;
303 using DeclVisitor<ASTNodeImporter, ExpectedDecl>::Visit;
304 using StmtVisitor<ASTNodeImporter, ExpectedStmt>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000305
306 // Importing types
Balazs Keri3b30d652018-10-19 13:32:20 +0000307 ExpectedType VisitType(const Type *T);
308 ExpectedType VisitAtomicType(const AtomicType *T);
309 ExpectedType VisitBuiltinType(const BuiltinType *T);
310 ExpectedType VisitDecayedType(const DecayedType *T);
311 ExpectedType VisitComplexType(const ComplexType *T);
312 ExpectedType VisitPointerType(const PointerType *T);
313 ExpectedType VisitBlockPointerType(const BlockPointerType *T);
314 ExpectedType VisitLValueReferenceType(const LValueReferenceType *T);
315 ExpectedType VisitRValueReferenceType(const RValueReferenceType *T);
316 ExpectedType VisitMemberPointerType(const MemberPointerType *T);
317 ExpectedType VisitConstantArrayType(const ConstantArrayType *T);
318 ExpectedType VisitIncompleteArrayType(const IncompleteArrayType *T);
319 ExpectedType VisitVariableArrayType(const VariableArrayType *T);
320 ExpectedType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000321 // FIXME: DependentSizedExtVectorType
Balazs Keri3b30d652018-10-19 13:32:20 +0000322 ExpectedType VisitVectorType(const VectorType *T);
323 ExpectedType VisitExtVectorType(const ExtVectorType *T);
324 ExpectedType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
325 ExpectedType VisitFunctionProtoType(const FunctionProtoType *T);
326 ExpectedType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
327 ExpectedType VisitParenType(const ParenType *T);
328 ExpectedType VisitTypedefType(const TypedefType *T);
329 ExpectedType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000330 // FIXME: DependentTypeOfExprType
Balazs Keri3b30d652018-10-19 13:32:20 +0000331 ExpectedType VisitTypeOfType(const TypeOfType *T);
332 ExpectedType VisitDecltypeType(const DecltypeType *T);
333 ExpectedType VisitUnaryTransformType(const UnaryTransformType *T);
334 ExpectedType VisitAutoType(const AutoType *T);
335 ExpectedType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000336 // FIXME: DependentDecltypeType
Balazs Keri3b30d652018-10-19 13:32:20 +0000337 ExpectedType VisitRecordType(const RecordType *T);
338 ExpectedType VisitEnumType(const EnumType *T);
339 ExpectedType VisitAttributedType(const AttributedType *T);
340 ExpectedType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
341 ExpectedType VisitSubstTemplateTypeParmType(
342 const SubstTemplateTypeParmType *T);
343 ExpectedType VisitTemplateSpecializationType(
344 const TemplateSpecializationType *T);
345 ExpectedType VisitElaboratedType(const ElaboratedType *T);
346 ExpectedType VisitDependentNameType(const DependentNameType *T);
347 ExpectedType VisitPackExpansionType(const PackExpansionType *T);
348 ExpectedType VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000349 const DependentTemplateSpecializationType *T);
Balazs Keri3b30d652018-10-19 13:32:20 +0000350 ExpectedType VisitObjCInterfaceType(const ObjCInterfaceType *T);
351 ExpectedType VisitObjCObjectType(const ObjCObjectType *T);
352 ExpectedType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Rafael Stahldf556202018-05-29 08:12:15 +0000353
354 // Importing declarations
Balazs Keri3b30d652018-10-19 13:32:20 +0000355 Error ImportDeclParts(
356 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
357 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc);
358 Error ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
359 Error ImportDeclarationNameLoc(
360 const DeclarationNameInfo &From, DeclarationNameInfo &To);
361 Error ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
362 Error ImportDeclContext(
363 Decl *From, DeclContext *&ToDC, DeclContext *&ToLexicalDC);
364 Error ImportImplicitMethods(const CXXRecordDecl *From, CXXRecordDecl *To);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000365
Balazs Keri3b30d652018-10-19 13:32:20 +0000366 Expected<CXXCastPath> ImportCastPath(CastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000367
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000368 using Designator = DesignatedInitExpr::Designator;
369
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000370 /// What we should import from the definition.
Fangrui Song6907ce22018-07-30 19:24:48 +0000371 enum ImportDefinitionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000372 /// Import the default subset of the definition, which might be
Douglas Gregor95d82832012-01-24 18:36:04 +0000373 /// nothing (if minimal import is set) or might be everything (if minimal
374 /// import is not set).
375 IDK_Default,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000376 /// Import everything.
Douglas Gregor95d82832012-01-24 18:36:04 +0000377 IDK_Everything,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000378 /// Import only the bare bones needed to establish a valid
Douglas Gregor95d82832012-01-24 18:36:04 +0000379 /// DeclContext.
380 IDK_Basic
381 };
382
Douglas Gregor2e15c842012-02-01 21:00:38 +0000383 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
384 return IDK == IDK_Everything ||
385 (IDK == IDK_Default && !Importer.isMinimalImport());
386 }
387
Balazs Keri3b30d652018-10-19 13:32:20 +0000388 Error ImportInitializer(VarDecl *From, VarDecl *To);
389 Error ImportDefinition(
390 RecordDecl *From, RecordDecl *To,
391 ImportDefinitionKind Kind = IDK_Default);
392 Error ImportDefinition(
393 EnumDecl *From, EnumDecl *To,
394 ImportDefinitionKind Kind = IDK_Default);
395 Error ImportDefinition(
396 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
397 ImportDefinitionKind Kind = IDK_Default);
398 Error ImportDefinition(
399 ObjCProtocolDecl *From, ObjCProtocolDecl *To,
400 ImportDefinitionKind Kind = IDK_Default);
Balazs Keri3b30d652018-10-19 13:32:20 +0000401 Error ImportTemplateArguments(
402 const TemplateArgument *FromArgs, unsigned NumFromArgs,
403 SmallVectorImpl<TemplateArgument> &ToArgs);
404 Expected<TemplateArgument>
405 ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000406
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000407 template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000408 Error ImportTemplateArgumentListInfo(
409 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000410
411 template<typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000412 Error ImportTemplateArgumentListInfo(
413 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
414 const InContainerTy &Container, TemplateArgumentListInfo &Result);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000415
Gabor Marton5254e642018-06-27 13:32:50 +0000416 using TemplateArgsTy = SmallVector<TemplateArgument, 8>;
Balazs Keri3b30d652018-10-19 13:32:20 +0000417 using FunctionTemplateAndArgsTy =
418 std::tuple<FunctionTemplateDecl *, TemplateArgsTy>;
419 Expected<FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000420 ImportFunctionTemplateWithTemplateArgsFromSpecialization(
421 FunctionDecl *FromFD);
422
Balazs Keri3b30d652018-10-19 13:32:20 +0000423 Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000424
Shafik Yaghmour96b3d202019-01-28 21:55:33 +0000425 Error ImportFunctionDeclBody(FunctionDecl *FromFD, FunctionDecl *ToFD);
426
Gabor Marton458d1452019-02-14 13:07:03 +0000427 template <typename T>
428 bool hasSameVisibilityContext(T *Found, T *From);
429
Gabor Marton950fb572018-07-17 12:39:27 +0000430 bool IsStructuralMatch(Decl *From, Decl *To, bool Complain);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000431 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
432 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000433 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
434 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000435 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000436 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000437 bool IsStructuralMatch(FunctionTemplateDecl *From,
438 FunctionTemplateDecl *To);
Balazs Keric7797c42018-07-11 09:37:24 +0000439 bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000440 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000441 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Balazs Keri3b30d652018-10-19 13:32:20 +0000442 ExpectedDecl VisitDecl(Decl *D);
443 ExpectedDecl VisitImportDecl(ImportDecl *D);
444 ExpectedDecl VisitEmptyDecl(EmptyDecl *D);
445 ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D);
446 ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D);
447 ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D);
448 ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D);
449 ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
450 ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
451 ExpectedDecl VisitTypedefDecl(TypedefDecl *D);
452 ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D);
453 ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
454 ExpectedDecl VisitLabelDecl(LabelDecl *D);
455 ExpectedDecl VisitEnumDecl(EnumDecl *D);
456 ExpectedDecl VisitRecordDecl(RecordDecl *D);
457 ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D);
458 ExpectedDecl VisitFunctionDecl(FunctionDecl *D);
459 ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D);
460 ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D);
461 ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D);
462 ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D);
463 ExpectedDecl VisitFieldDecl(FieldDecl *D);
464 ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D);
465 ExpectedDecl VisitFriendDecl(FriendDecl *D);
466 ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D);
467 ExpectedDecl VisitVarDecl(VarDecl *D);
468 ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D);
469 ExpectedDecl VisitParmVarDecl(ParmVarDecl *D);
470 ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D);
471 ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
472 ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D);
473 ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D);
474 ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D);
475 ExpectedDecl VisitUsingDecl(UsingDecl *D);
476 ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D);
477 ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
478 ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
479 ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000480
Balazs Keri3b30d652018-10-19 13:32:20 +0000481 Expected<ObjCTypeParamList *>
482 ImportObjCTypeParamList(ObjCTypeParamList *list);
483
484 ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
485 ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
486 ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D);
487 ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D);
488 ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
489 ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
490 ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
491 ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
492 ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D);
493 ExpectedDecl VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +0000494 ClassTemplateSpecializationDecl *D);
Balazs Keri3b30d652018-10-19 13:32:20 +0000495 ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D);
496 ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
497 ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000498
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000499 // Importing statements
Balazs Keri3b30d652018-10-19 13:32:20 +0000500 ExpectedStmt VisitStmt(Stmt *S);
501 ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S);
502 ExpectedStmt VisitDeclStmt(DeclStmt *S);
503 ExpectedStmt VisitNullStmt(NullStmt *S);
504 ExpectedStmt VisitCompoundStmt(CompoundStmt *S);
505 ExpectedStmt VisitCaseStmt(CaseStmt *S);
506 ExpectedStmt VisitDefaultStmt(DefaultStmt *S);
507 ExpectedStmt VisitLabelStmt(LabelStmt *S);
508 ExpectedStmt VisitAttributedStmt(AttributedStmt *S);
509 ExpectedStmt VisitIfStmt(IfStmt *S);
510 ExpectedStmt VisitSwitchStmt(SwitchStmt *S);
511 ExpectedStmt VisitWhileStmt(WhileStmt *S);
512 ExpectedStmt VisitDoStmt(DoStmt *S);
513 ExpectedStmt VisitForStmt(ForStmt *S);
514 ExpectedStmt VisitGotoStmt(GotoStmt *S);
515 ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S);
516 ExpectedStmt VisitContinueStmt(ContinueStmt *S);
517 ExpectedStmt VisitBreakStmt(BreakStmt *S);
518 ExpectedStmt VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000519 // FIXME: MSAsmStmt
520 // FIXME: SEHExceptStmt
521 // FIXME: SEHFinallyStmt
522 // FIXME: SEHTryStmt
523 // FIXME: SEHLeaveStmt
524 // FIXME: CapturedStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000525 ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S);
526 ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S);
527 ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000528 // FIXME: MSDependentExistsStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000529 ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
530 ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
531 ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
532 ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S);
533 ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
534 ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
535 ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000536
537 // Importing expressions
Balazs Keri3b30d652018-10-19 13:32:20 +0000538 ExpectedStmt VisitExpr(Expr *E);
539 ExpectedStmt VisitVAArgExpr(VAArgExpr *E);
Tom Roeder521f0042019-02-26 19:26:41 +0000540 ExpectedStmt VisitChooseExpr(ChooseExpr *E);
Balazs Keri3b30d652018-10-19 13:32:20 +0000541 ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E);
542 ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E);
543 ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E);
544 ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
545 ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E);
546 ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
547 ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E);
548 ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E);
549 ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E);
550 ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E);
551 ExpectedStmt VisitStringLiteral(StringLiteral *E);
552 ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
553 ExpectedStmt VisitAtomicExpr(AtomicExpr *E);
554 ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E);
Bill Wendling8003edc2018-11-09 00:41:36 +0000555 ExpectedStmt VisitConstantExpr(ConstantExpr *E);
Balazs Keri3b30d652018-10-19 13:32:20 +0000556 ExpectedStmt VisitParenExpr(ParenExpr *E);
557 ExpectedStmt VisitParenListExpr(ParenListExpr *E);
558 ExpectedStmt VisitStmtExpr(StmtExpr *E);
559 ExpectedStmt VisitUnaryOperator(UnaryOperator *E);
560 ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
561 ExpectedStmt VisitBinaryOperator(BinaryOperator *E);
562 ExpectedStmt VisitConditionalOperator(ConditionalOperator *E);
563 ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
564 ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E);
565 ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
566 ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E);
567 ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E);
568 ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E);
569 ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E);
570 ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E);
571 ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE);
572 ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E);
573 ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
574 ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
575 ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
576 ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
577 ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
578 ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
579 ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E);
580 ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E);
581 ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E);
582 ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E);
583 ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E);
584 ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
585 ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
586 ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
587 ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
588 ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
589 ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
590 ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E);
591 ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E);
592 ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
593 ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
594 ExpectedStmt VisitMemberExpr(MemberExpr *E);
595 ExpectedStmt VisitCallExpr(CallExpr *E);
596 ExpectedStmt VisitLambdaExpr(LambdaExpr *LE);
597 ExpectedStmt VisitInitListExpr(InitListExpr *E);
598 ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
599 ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E);
600 ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
601 ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
602 ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
603 ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
604 ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
605 ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E);
606 ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000607
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000608 template<typename IIter, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000609 Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000610 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
Balazs Keri3b30d652018-10-19 13:32:20 +0000611 for (; Ibegin != Iend; ++Ibegin, ++Obegin) {
612 Expected<ItemT> ToOrErr = import(*Ibegin);
613 if (!ToOrErr)
614 return ToOrErr.takeError();
615 *Obegin = *ToOrErr;
616 }
617 return Error::success();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000618 }
619
Balazs Keri3b30d652018-10-19 13:32:20 +0000620 // Import every item from a container structure into an output container.
621 // If error occurs, stops at first error and returns the error.
622 // The output container should have space for all needed elements (it is not
623 // expanded, new items are put into from the beginning).
Aleksei Sidorina693b372016-09-28 10:16:56 +0000624 template<typename InContainerTy, typename OutContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000625 Error ImportContainerChecked(
626 const InContainerTy &InContainer, OutContainerTy &OutContainer) {
627 return ImportArrayChecked(
628 InContainer.begin(), InContainer.end(), OutContainer.begin());
Aleksei Sidorina693b372016-09-28 10:16:56 +0000629 }
630
631 template<typename InContainerTy, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000632 Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000633 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
634 }
Lang Hames19e07e12017-06-20 21:06:00 +0000635
Lang Hames19e07e12017-06-20 21:06:00 +0000636 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000637
Balazs Keri3b30d652018-10-19 13:32:20 +0000638 Expected<FunctionDecl *> FindFunctionTemplateSpecialization(
639 FunctionDecl *FromFD);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000640 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000641
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000642template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000643Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000644 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
645 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000646 auto ToLAngleLocOrErr = import(FromLAngleLoc);
647 if (!ToLAngleLocOrErr)
648 return ToLAngleLocOrErr.takeError();
649 auto ToRAngleLocOrErr = import(FromRAngleLoc);
650 if (!ToRAngleLocOrErr)
651 return ToRAngleLocOrErr.takeError();
652
653 TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr);
654 if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo))
655 return Err;
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000656 Result = ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +0000657 return Error::success();
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000658}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000659
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000660template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000661Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000662 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
663 return ImportTemplateArgumentListInfo(
664 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
665}
666
667template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000668Error ASTNodeImporter::ImportTemplateArgumentListInfo<
669 ASTTemplateArgumentListInfo>(
670 const ASTTemplateArgumentListInfo &From,
671 TemplateArgumentListInfo &Result) {
672 return ImportTemplateArgumentListInfo(
673 From.LAngleLoc, From.RAngleLoc, From.arguments(), Result);
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000674}
675
Balazs Keri3b30d652018-10-19 13:32:20 +0000676Expected<ASTNodeImporter::FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000677ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
678 FunctionDecl *FromFD) {
679 assert(FromFD->getTemplatedKind() ==
Balazs Keri3b30d652018-10-19 13:32:20 +0000680 FunctionDecl::TK_FunctionTemplateSpecialization);
681
682 FunctionTemplateAndArgsTy Result;
683
Gabor Marton5254e642018-06-27 13:32:50 +0000684 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Balazs Keri3b30d652018-10-19 13:32:20 +0000685 if (Error Err = importInto(std::get<0>(Result), FTSInfo->getTemplate()))
686 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000687
688 // Import template arguments.
689 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
Balazs Keri3b30d652018-10-19 13:32:20 +0000690 if (Error Err = ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
691 std::get<1>(Result)))
692 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000693
Balazs Keri3b30d652018-10-19 13:32:20 +0000694 return Result;
695}
696
697template <>
698Expected<TemplateParameterList *>
699ASTNodeImporter::import(TemplateParameterList *From) {
700 SmallVector<NamedDecl *, 4> To(From->size());
701 if (Error Err = ImportContainerChecked(*From, To))
702 return std::move(Err);
703
704 ExpectedExpr ToRequiresClause = import(From->getRequiresClause());
705 if (!ToRequiresClause)
706 return ToRequiresClause.takeError();
707
708 auto ToTemplateLocOrErr = import(From->getTemplateLoc());
709 if (!ToTemplateLocOrErr)
710 return ToTemplateLocOrErr.takeError();
711 auto ToLAngleLocOrErr = import(From->getLAngleLoc());
712 if (!ToLAngleLocOrErr)
713 return ToLAngleLocOrErr.takeError();
714 auto ToRAngleLocOrErr = import(From->getRAngleLoc());
715 if (!ToRAngleLocOrErr)
716 return ToRAngleLocOrErr.takeError();
717
718 return TemplateParameterList::Create(
719 Importer.getToContext(),
720 *ToTemplateLocOrErr,
721 *ToLAngleLocOrErr,
722 To,
723 *ToRAngleLocOrErr,
724 *ToRequiresClause);
725}
726
727template <>
728Expected<TemplateArgument>
729ASTNodeImporter::import(const TemplateArgument &From) {
730 switch (From.getKind()) {
731 case TemplateArgument::Null:
732 return TemplateArgument();
733
734 case TemplateArgument::Type: {
735 ExpectedType ToTypeOrErr = import(From.getAsType());
736 if (!ToTypeOrErr)
737 return ToTypeOrErr.takeError();
738 return TemplateArgument(*ToTypeOrErr);
739 }
740
741 case TemplateArgument::Integral: {
742 ExpectedType ToTypeOrErr = import(From.getIntegralType());
743 if (!ToTypeOrErr)
744 return ToTypeOrErr.takeError();
745 return TemplateArgument(From, *ToTypeOrErr);
746 }
747
748 case TemplateArgument::Declaration: {
749 Expected<ValueDecl *> ToOrErr = import(From.getAsDecl());
750 if (!ToOrErr)
751 return ToOrErr.takeError();
752 ExpectedType ToTypeOrErr = import(From.getParamTypeForDecl());
753 if (!ToTypeOrErr)
754 return ToTypeOrErr.takeError();
755 return TemplateArgument(*ToOrErr, *ToTypeOrErr);
756 }
757
758 case TemplateArgument::NullPtr: {
759 ExpectedType ToTypeOrErr = import(From.getNullPtrType());
760 if (!ToTypeOrErr)
761 return ToTypeOrErr.takeError();
762 return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/true);
763 }
764
765 case TemplateArgument::Template: {
766 Expected<TemplateName> ToTemplateOrErr = import(From.getAsTemplate());
767 if (!ToTemplateOrErr)
768 return ToTemplateOrErr.takeError();
769
770 return TemplateArgument(*ToTemplateOrErr);
771 }
772
773 case TemplateArgument::TemplateExpansion: {
774 Expected<TemplateName> ToTemplateOrErr =
775 import(From.getAsTemplateOrTemplatePattern());
776 if (!ToTemplateOrErr)
777 return ToTemplateOrErr.takeError();
778
779 return TemplateArgument(
780 *ToTemplateOrErr, From.getNumTemplateExpansions());
781 }
782
783 case TemplateArgument::Expression:
784 if (ExpectedExpr ToExpr = import(From.getAsExpr()))
785 return TemplateArgument(*ToExpr);
786 else
787 return ToExpr.takeError();
788
789 case TemplateArgument::Pack: {
790 SmallVector<TemplateArgument, 2> ToPack;
791 ToPack.reserve(From.pack_size());
792 if (Error Err = ImportTemplateArguments(
793 From.pack_begin(), From.pack_size(), ToPack))
794 return std::move(Err);
795
796 return TemplateArgument(
797 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
798 }
799 }
800
801 llvm_unreachable("Invalid template argument kind");
802}
803
804template <>
805Expected<TemplateArgumentLoc>
806ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) {
807 Expected<TemplateArgument> ArgOrErr = import(TALoc.getArgument());
808 if (!ArgOrErr)
809 return ArgOrErr.takeError();
810 TemplateArgument Arg = *ArgOrErr;
811
812 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
813
814 TemplateArgumentLocInfo ToInfo;
815 if (Arg.getKind() == TemplateArgument::Expression) {
816 ExpectedExpr E = import(FromInfo.getAsExpr());
817 if (!E)
818 return E.takeError();
819 ToInfo = TemplateArgumentLocInfo(*E);
820 } else if (Arg.getKind() == TemplateArgument::Type) {
821 if (auto TSIOrErr = import(FromInfo.getAsTypeSourceInfo()))
822 ToInfo = TemplateArgumentLocInfo(*TSIOrErr);
823 else
824 return TSIOrErr.takeError();
825 } else {
826 auto ToTemplateQualifierLocOrErr =
827 import(FromInfo.getTemplateQualifierLoc());
828 if (!ToTemplateQualifierLocOrErr)
829 return ToTemplateQualifierLocOrErr.takeError();
830 auto ToTemplateNameLocOrErr = import(FromInfo.getTemplateNameLoc());
831 if (!ToTemplateNameLocOrErr)
832 return ToTemplateNameLocOrErr.takeError();
833 auto ToTemplateEllipsisLocOrErr =
834 import(FromInfo.getTemplateEllipsisLoc());
835 if (!ToTemplateEllipsisLocOrErr)
836 return ToTemplateEllipsisLocOrErr.takeError();
837
838 ToInfo = TemplateArgumentLocInfo(
839 *ToTemplateQualifierLocOrErr,
840 *ToTemplateNameLocOrErr,
841 *ToTemplateEllipsisLocOrErr);
842 }
843
844 return TemplateArgumentLoc(Arg, ToInfo);
845}
846
847template <>
848Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) {
849 if (DG.isNull())
850 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
851 size_t NumDecls = DG.end() - DG.begin();
852 SmallVector<Decl *, 1> ToDecls;
853 ToDecls.reserve(NumDecls);
854 for (Decl *FromD : DG) {
855 if (auto ToDOrErr = import(FromD))
856 ToDecls.push_back(*ToDOrErr);
857 else
858 return ToDOrErr.takeError();
859 }
860 return DeclGroupRef::Create(Importer.getToContext(),
861 ToDecls.begin(),
862 NumDecls);
863}
864
865template <>
866Expected<ASTNodeImporter::Designator>
867ASTNodeImporter::import(const Designator &D) {
868 if (D.isFieldDesignator()) {
869 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
870
871 ExpectedSLoc ToDotLocOrErr = import(D.getDotLoc());
872 if (!ToDotLocOrErr)
873 return ToDotLocOrErr.takeError();
874
875 ExpectedSLoc ToFieldLocOrErr = import(D.getFieldLoc());
876 if (!ToFieldLocOrErr)
877 return ToFieldLocOrErr.takeError();
878
879 return Designator(ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr);
880 }
881
882 ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc());
883 if (!ToLBracketLocOrErr)
884 return ToLBracketLocOrErr.takeError();
885
886 ExpectedSLoc ToRBracketLocOrErr = import(D.getRBracketLoc());
887 if (!ToRBracketLocOrErr)
888 return ToRBracketLocOrErr.takeError();
889
890 if (D.isArrayDesignator())
891 return Designator(D.getFirstExprIndex(),
892 *ToLBracketLocOrErr, *ToRBracketLocOrErr);
893
894 ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc());
895 if (!ToEllipsisLocOrErr)
896 return ToEllipsisLocOrErr.takeError();
897
898 assert(D.isArrayRangeDesignator());
899 return Designator(
900 D.getFirstExprIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr,
901 *ToRBracketLocOrErr);
902}
903
904template <>
905Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) {
906 VarDecl *Var = nullptr;
907 if (From.capturesVariable()) {
908 if (auto VarOrErr = import(From.getCapturedVar()))
909 Var = *VarOrErr;
910 else
911 return VarOrErr.takeError();
912 }
913
914 auto LocationOrErr = import(From.getLocation());
915 if (!LocationOrErr)
916 return LocationOrErr.takeError();
917
918 SourceLocation EllipsisLoc;
919 if (From.isPackExpansion())
920 if (Error Err = importInto(EllipsisLoc, From.getEllipsisLoc()))
921 return std::move(Err);
922
923 return LambdaCapture(
924 *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var,
925 EllipsisLoc);
Gabor Marton5254e642018-06-27 13:32:50 +0000926}
927
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000928} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000929
Douglas Gregor3996e242010-02-15 22:01:00 +0000930//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000931// Import Types
932//----------------------------------------------------------------------------
933
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000934using namespace clang;
935
Balazs Keri3b30d652018-10-19 13:32:20 +0000936ExpectedType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000937 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
938 << T->getTypeClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +0000939 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000940}
941
Balazs Keri3b30d652018-10-19 13:32:20 +0000942ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
943 ExpectedType UnderlyingTypeOrErr = import(T->getValueType());
944 if (!UnderlyingTypeOrErr)
945 return UnderlyingTypeOrErr.takeError();
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000946
Balazs Keri3b30d652018-10-19 13:32:20 +0000947 return Importer.getToContext().getAtomicType(*UnderlyingTypeOrErr);
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000948}
949
Balazs Keri3b30d652018-10-19 13:32:20 +0000950ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000951 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000952#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
953 case BuiltinType::Id: \
954 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000955#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +0000956#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
957 case BuiltinType::Id: \
958 return Importer.getToContext().Id##Ty;
959#include "clang/Basic/OpenCLExtensionTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000960#define SHARED_SINGLETON_TYPE(Expansion)
961#define BUILTIN_TYPE(Id, SingletonId) \
962 case BuiltinType::Id: return Importer.getToContext().SingletonId;
963#include "clang/AST/BuiltinTypes.def"
964
965 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
966 // context supports C++.
967
968 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
969 // context supports ObjC.
970
Douglas Gregor96e578d2010-02-05 17:54:41 +0000971 case BuiltinType::Char_U:
Fangrui Song6907ce22018-07-30 19:24:48 +0000972 // The context we're importing from has an unsigned 'char'. If we're
973 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +0000974 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000975 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000976 return Importer.getToContext().UnsignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +0000977
Douglas Gregor96e578d2010-02-05 17:54:41 +0000978 return Importer.getToContext().CharTy;
979
Douglas Gregor96e578d2010-02-05 17:54:41 +0000980 case BuiltinType::Char_S:
Fangrui Song6907ce22018-07-30 19:24:48 +0000981 // The context we're importing from has an unsigned 'char'. If we're
982 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +0000983 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000984 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000985 return Importer.getToContext().SignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +0000986
Douglas Gregor96e578d2010-02-05 17:54:41 +0000987 return Importer.getToContext().CharTy;
988
Chris Lattnerad3467e2010-12-25 23:25:43 +0000989 case BuiltinType::WChar_S:
990 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000991 // FIXME: If not in C++, shall we translate to the C equivalent of
992 // wchar_t?
993 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000994 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000995
996 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000997}
998
Balazs Keri3b30d652018-10-19 13:32:20 +0000999ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
1000 ExpectedType ToOriginalTypeOrErr = import(T->getOriginalType());
1001 if (!ToOriginalTypeOrErr)
1002 return ToOriginalTypeOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00001003
Balazs Keri3b30d652018-10-19 13:32:20 +00001004 return Importer.getToContext().getDecayedType(*ToOriginalTypeOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00001005}
1006
Balazs Keri3b30d652018-10-19 13:32:20 +00001007ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
1008 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1009 if (!ToElementTypeOrErr)
1010 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001011
Balazs Keri3b30d652018-10-19 13:32:20 +00001012 return Importer.getToContext().getComplexType(*ToElementTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001013}
1014
Balazs Keri3b30d652018-10-19 13:32:20 +00001015ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) {
1016 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1017 if (!ToPointeeTypeOrErr)
1018 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001019
Balazs Keri3b30d652018-10-19 13:32:20 +00001020 return Importer.getToContext().getPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001021}
1022
Balazs Keri3b30d652018-10-19 13:32:20 +00001023ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001024 // FIXME: Check for blocks support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001025 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1026 if (!ToPointeeTypeOrErr)
1027 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001028
Balazs Keri3b30d652018-10-19 13:32:20 +00001029 return Importer.getToContext().getBlockPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001030}
1031
Balazs Keri3b30d652018-10-19 13:32:20 +00001032ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001033ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001034 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001035 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1036 if (!ToPointeeTypeOrErr)
1037 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001038
Balazs Keri3b30d652018-10-19 13:32:20 +00001039 return Importer.getToContext().getLValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001040}
1041
Balazs Keri3b30d652018-10-19 13:32:20 +00001042ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001043ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001044 // FIXME: Check for C++0x support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001045 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1046 if (!ToPointeeTypeOrErr)
1047 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001048
Balazs Keri3b30d652018-10-19 13:32:20 +00001049 return Importer.getToContext().getRValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001050}
1051
Balazs Keri3b30d652018-10-19 13:32:20 +00001052ExpectedType
1053ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001054 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001055 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1056 if (!ToPointeeTypeOrErr)
1057 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001058
Balazs Keri3b30d652018-10-19 13:32:20 +00001059 ExpectedType ClassTypeOrErr = import(QualType(T->getClass(), 0));
1060 if (!ClassTypeOrErr)
1061 return ClassTypeOrErr.takeError();
1062
1063 return Importer.getToContext().getMemberPointerType(
1064 *ToPointeeTypeOrErr, (*ClassTypeOrErr).getTypePtr());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001065}
1066
Balazs Keri3b30d652018-10-19 13:32:20 +00001067ExpectedType
1068ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
1069 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1070 if (!ToElementTypeOrErr)
1071 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001072
Balazs Keri3b30d652018-10-19 13:32:20 +00001073 return Importer.getToContext().getConstantArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001074 T->getSize(),
1075 T->getSizeModifier(),
1076 T->getIndexTypeCVRQualifiers());
1077}
1078
Balazs Keri3b30d652018-10-19 13:32:20 +00001079ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001080ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001081 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1082 if (!ToElementTypeOrErr)
1083 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001084
Balazs Keri3b30d652018-10-19 13:32:20 +00001085 return Importer.getToContext().getIncompleteArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001086 T->getSizeModifier(),
1087 T->getIndexTypeCVRQualifiers());
1088}
1089
Balazs Keri3b30d652018-10-19 13:32:20 +00001090ExpectedType
1091ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
1092 QualType ToElementType;
1093 Expr *ToSizeExpr;
1094 SourceRange ToBracketsRange;
1095 if (auto Imp = importSeq(
1096 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1097 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1098 else
1099 return Imp.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001100
Balazs Keri3b30d652018-10-19 13:32:20 +00001101 return Importer.getToContext().getVariableArrayType(
1102 ToElementType, ToSizeExpr, T->getSizeModifier(),
1103 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001104}
1105
Balazs Keri3b30d652018-10-19 13:32:20 +00001106ExpectedType ASTNodeImporter::VisitDependentSizedArrayType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001107 const DependentSizedArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001108 QualType ToElementType;
1109 Expr *ToSizeExpr;
1110 SourceRange ToBracketsRange;
1111 if (auto Imp = importSeq(
1112 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1113 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1114 else
1115 return Imp.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001116 // SizeExpr may be null if size is not specified directly.
1117 // For example, 'int a[]'.
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001118
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001119 return Importer.getToContext().getDependentSizedArrayType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001120 ToElementType, ToSizeExpr, T->getSizeModifier(),
1121 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001122}
1123
Balazs Keri3b30d652018-10-19 13:32:20 +00001124ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) {
1125 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1126 if (!ToElementTypeOrErr)
1127 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001128
Balazs Keri3b30d652018-10-19 13:32:20 +00001129 return Importer.getToContext().getVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001130 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001131 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001132}
1133
Balazs Keri3b30d652018-10-19 13:32:20 +00001134ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
1135 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1136 if (!ToElementTypeOrErr)
1137 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001138
Balazs Keri3b30d652018-10-19 13:32:20 +00001139 return Importer.getToContext().getExtVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001140 T->getNumElements());
1141}
1142
Balazs Keri3b30d652018-10-19 13:32:20 +00001143ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001144ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001145 // FIXME: What happens if we're importing a function without a prototype
Douglas Gregor96e578d2010-02-05 17:54:41 +00001146 // into C++? Should we make it variadic?
Balazs Keri3b30d652018-10-19 13:32:20 +00001147 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1148 if (!ToReturnTypeOrErr)
1149 return ToReturnTypeOrErr.takeError();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001150
Balazs Keri3b30d652018-10-19 13:32:20 +00001151 return Importer.getToContext().getFunctionNoProtoType(*ToReturnTypeOrErr,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001152 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001153}
1154
Balazs Keri3b30d652018-10-19 13:32:20 +00001155ExpectedType
1156ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
1157 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1158 if (!ToReturnTypeOrErr)
1159 return ToReturnTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001160
Douglas Gregor96e578d2010-02-05 17:54:41 +00001161 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001162 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001163 for (const auto &A : T->param_types()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001164 ExpectedType TyOrErr = import(A);
1165 if (!TyOrErr)
1166 return TyOrErr.takeError();
1167 ArgTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001168 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001169
Douglas Gregor96e578d2010-02-05 17:54:41 +00001170 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001171 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001172 for (const auto &E : T->exceptions()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001173 ExpectedType TyOrErr = import(E);
1174 if (!TyOrErr)
1175 return TyOrErr.takeError();
1176 ExceptionTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001177 }
John McCalldb40c7f2010-12-14 08:05:40 +00001178
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001179 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1180 FunctionProtoType::ExtProtoInfo ToEPI;
1181
Balazs Keri3b30d652018-10-19 13:32:20 +00001182 auto Imp = importSeq(
1183 FromEPI.ExceptionSpec.NoexceptExpr,
1184 FromEPI.ExceptionSpec.SourceDecl,
1185 FromEPI.ExceptionSpec.SourceTemplate);
1186 if (!Imp)
1187 return Imp.takeError();
1188
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001189 ToEPI.ExtInfo = FromEPI.ExtInfo;
1190 ToEPI.Variadic = FromEPI.Variadic;
1191 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1192 ToEPI.TypeQuals = FromEPI.TypeQuals;
1193 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001194 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1195 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
Balazs Keri3b30d652018-10-19 13:32:20 +00001196 std::tie(
1197 ToEPI.ExceptionSpec.NoexceptExpr,
1198 ToEPI.ExceptionSpec.SourceDecl,
1199 ToEPI.ExceptionSpec.SourceTemplate) = *Imp;
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001200
Balazs Keri3b30d652018-10-19 13:32:20 +00001201 return Importer.getToContext().getFunctionType(
1202 *ToReturnTypeOrErr, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001203}
1204
Balazs Keri3b30d652018-10-19 13:32:20 +00001205ExpectedType ASTNodeImporter::VisitUnresolvedUsingType(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001206 const UnresolvedUsingType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001207 UnresolvedUsingTypenameDecl *ToD;
1208 Decl *ToPrevD;
1209 if (auto Imp = importSeq(T->getDecl(), T->getDecl()->getPreviousDecl()))
1210 std::tie(ToD, ToPrevD) = *Imp;
1211 else
1212 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001213
Balazs Keri3b30d652018-10-19 13:32:20 +00001214 return Importer.getToContext().getTypeDeclType(
1215 ToD, cast_or_null<TypeDecl>(ToPrevD));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001216}
1217
Balazs Keri3b30d652018-10-19 13:32:20 +00001218ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) {
1219 ExpectedType ToInnerTypeOrErr = import(T->getInnerType());
1220 if (!ToInnerTypeOrErr)
1221 return ToInnerTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001222
Balazs Keri3b30d652018-10-19 13:32:20 +00001223 return Importer.getToContext().getParenType(*ToInnerTypeOrErr);
Sean Callananda6df8a2011-08-11 16:56:07 +00001224}
1225
Balazs Keri3b30d652018-10-19 13:32:20 +00001226ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
1227 Expected<TypedefNameDecl *> ToDeclOrErr = import(T->getDecl());
1228 if (!ToDeclOrErr)
1229 return ToDeclOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001230
Balazs Keri3b30d652018-10-19 13:32:20 +00001231 return Importer.getToContext().getTypeDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001232}
1233
Balazs Keri3b30d652018-10-19 13:32:20 +00001234ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
1235 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1236 if (!ToExprOrErr)
1237 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001238
Balazs Keri3b30d652018-10-19 13:32:20 +00001239 return Importer.getToContext().getTypeOfExprType(*ToExprOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001240}
1241
Balazs Keri3b30d652018-10-19 13:32:20 +00001242ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
1243 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1244 if (!ToUnderlyingTypeOrErr)
1245 return ToUnderlyingTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001246
Balazs Keri3b30d652018-10-19 13:32:20 +00001247 return Importer.getToContext().getTypeOfType(*ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001248}
1249
Balazs Keri3b30d652018-10-19 13:32:20 +00001250ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001251 // FIXME: Make sure that the "to" context supports C++0x!
Balazs Keri3b30d652018-10-19 13:32:20 +00001252 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1253 if (!ToExprOrErr)
1254 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001255
Balazs Keri3b30d652018-10-19 13:32:20 +00001256 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1257 if (!ToUnderlyingTypeOrErr)
1258 return ToUnderlyingTypeOrErr.takeError();
Douglas Gregor81495f32012-02-12 18:42:33 +00001259
Balazs Keri3b30d652018-10-19 13:32:20 +00001260 return Importer.getToContext().getDecltypeType(
1261 *ToExprOrErr, *ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001262}
1263
Balazs Keri3b30d652018-10-19 13:32:20 +00001264ExpectedType
1265ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1266 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1267 if (!ToBaseTypeOrErr)
1268 return ToBaseTypeOrErr.takeError();
Alexis Hunte852b102011-05-24 22:41:36 +00001269
Balazs Keri3b30d652018-10-19 13:32:20 +00001270 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1271 if (!ToUnderlyingTypeOrErr)
1272 return ToUnderlyingTypeOrErr.takeError();
1273
1274 return Importer.getToContext().getUnaryTransformType(
1275 *ToBaseTypeOrErr, *ToUnderlyingTypeOrErr, T->getUTTKind());
Alexis Hunte852b102011-05-24 22:41:36 +00001276}
1277
Balazs Keri3b30d652018-10-19 13:32:20 +00001278ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001279 // FIXME: Make sure that the "to" context supports C++11!
Balazs Keri3b30d652018-10-19 13:32:20 +00001280 ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType());
1281 if (!ToDeducedTypeOrErr)
1282 return ToDeducedTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001283
Balazs Keri3b30d652018-10-19 13:32:20 +00001284 return Importer.getToContext().getAutoType(*ToDeducedTypeOrErr,
1285 T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001286 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001287}
1288
Balazs Keri3b30d652018-10-19 13:32:20 +00001289ExpectedType ASTNodeImporter::VisitInjectedClassNameType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001290 const InjectedClassNameType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001291 Expected<CXXRecordDecl *> ToDeclOrErr = import(T->getDecl());
1292 if (!ToDeclOrErr)
1293 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001294
Balazs Keri3b30d652018-10-19 13:32:20 +00001295 ExpectedType ToInjTypeOrErr = import(T->getInjectedSpecializationType());
1296 if (!ToInjTypeOrErr)
1297 return ToInjTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001298
1299 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1300 // See comments in InjectedClassNameType definition for details
1301 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1302 enum {
1303 TypeAlignmentInBits = 4,
1304 TypeAlignment = 1 << TypeAlignmentInBits
1305 };
1306
1307 return QualType(new (Importer.getToContext(), TypeAlignment)
Balazs Keri3b30d652018-10-19 13:32:20 +00001308 InjectedClassNameType(*ToDeclOrErr, *ToInjTypeOrErr), 0);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001309}
1310
Balazs Keri3b30d652018-10-19 13:32:20 +00001311ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) {
1312 Expected<RecordDecl *> ToDeclOrErr = import(T->getDecl());
1313 if (!ToDeclOrErr)
1314 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001315
Balazs Keri3b30d652018-10-19 13:32:20 +00001316 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001317}
1318
Balazs Keri3b30d652018-10-19 13:32:20 +00001319ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) {
1320 Expected<EnumDecl *> ToDeclOrErr = import(T->getDecl());
1321 if (!ToDeclOrErr)
1322 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001323
Balazs Keri3b30d652018-10-19 13:32:20 +00001324 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001325}
1326
Balazs Keri3b30d652018-10-19 13:32:20 +00001327ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1328 ExpectedType ToModifiedTypeOrErr = import(T->getModifiedType());
1329 if (!ToModifiedTypeOrErr)
1330 return ToModifiedTypeOrErr.takeError();
1331 ExpectedType ToEquivalentTypeOrErr = import(T->getEquivalentType());
1332 if (!ToEquivalentTypeOrErr)
1333 return ToEquivalentTypeOrErr.takeError();
Sean Callanan72fe0852015-04-02 23:50:08 +00001334
1335 return Importer.getToContext().getAttributedType(T->getAttrKind(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001336 *ToModifiedTypeOrErr, *ToEquivalentTypeOrErr);
Sean Callanan72fe0852015-04-02 23:50:08 +00001337}
1338
Balazs Keri3b30d652018-10-19 13:32:20 +00001339ExpectedType ASTNodeImporter::VisitTemplateTypeParmType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001340 const TemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001341 Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(T->getDecl());
1342 if (!ToDeclOrErr)
1343 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001344
1345 return Importer.getToContext().getTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001346 T->getDepth(), T->getIndex(), T->isParameterPack(), *ToDeclOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001347}
1348
Balazs Keri3b30d652018-10-19 13:32:20 +00001349ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001350 const SubstTemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001351 ExpectedType ReplacedOrErr = import(QualType(T->getReplacedParameter(), 0));
1352 if (!ReplacedOrErr)
1353 return ReplacedOrErr.takeError();
1354 const TemplateTypeParmType *Replaced =
1355 cast<TemplateTypeParmType>((*ReplacedOrErr).getTypePtr());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001356
Balazs Keri3b30d652018-10-19 13:32:20 +00001357 ExpectedType ToReplacementTypeOrErr = import(T->getReplacementType());
1358 if (!ToReplacementTypeOrErr)
1359 return ToReplacementTypeOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001360
1361 return Importer.getToContext().getSubstTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001362 Replaced, (*ToReplacementTypeOrErr).getCanonicalType());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001363}
1364
Balazs Keri3b30d652018-10-19 13:32:20 +00001365ExpectedType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001366 const TemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001367 auto ToTemplateOrErr = import(T->getTemplateName());
1368 if (!ToTemplateOrErr)
1369 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001370
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001371 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00001372 if (Error Err = ImportTemplateArguments(
1373 T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1374 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001375
Douglas Gregore2e50d332010-12-01 01:36:18 +00001376 QualType ToCanonType;
1377 if (!QualType(T, 0).isCanonical()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001378 QualType FromCanonType
Douglas Gregore2e50d332010-12-01 01:36:18 +00001379 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
Balazs Keri3b30d652018-10-19 13:32:20 +00001380 if (ExpectedType TyOrErr = import(FromCanonType))
1381 ToCanonType = *TyOrErr;
1382 else
1383 return TyOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001384 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001385 return Importer.getToContext().getTemplateSpecializationType(*ToTemplateOrErr,
David Majnemer6fbeee32016-07-07 04:43:07 +00001386 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001387 ToCanonType);
1388}
1389
Balazs Keri3b30d652018-10-19 13:32:20 +00001390ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001391 // Note: the qualifier in an ElaboratedType is optional.
Balazs Keri3b30d652018-10-19 13:32:20 +00001392 auto ToQualifierOrErr = import(T->getQualifier());
1393 if (!ToQualifierOrErr)
1394 return ToQualifierOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001395
Balazs Keri3b30d652018-10-19 13:32:20 +00001396 ExpectedType ToNamedTypeOrErr = import(T->getNamedType());
1397 if (!ToNamedTypeOrErr)
1398 return ToNamedTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001399
Balazs Keri3b30d652018-10-19 13:32:20 +00001400 Expected<TagDecl *> ToOwnedTagDeclOrErr = import(T->getOwnedTagDecl());
1401 if (!ToOwnedTagDeclOrErr)
1402 return ToOwnedTagDeclOrErr.takeError();
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001403
Abramo Bagnara6150c882010-05-11 21:36:43 +00001404 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001405 *ToQualifierOrErr,
1406 *ToNamedTypeOrErr,
1407 *ToOwnedTagDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001408}
1409
Balazs Keri3b30d652018-10-19 13:32:20 +00001410ExpectedType
1411ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
1412 ExpectedType ToPatternOrErr = import(T->getPattern());
1413 if (!ToPatternOrErr)
1414 return ToPatternOrErr.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00001415
Balazs Keri3b30d652018-10-19 13:32:20 +00001416 return Importer.getToContext().getPackExpansionType(*ToPatternOrErr,
Gabor Horvath7a91c082017-11-14 11:30:38 +00001417 T->getNumExpansions());
1418}
1419
Balazs Keri3b30d652018-10-19 13:32:20 +00001420ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001421 const DependentTemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001422 auto ToQualifierOrErr = import(T->getQualifier());
1423 if (!ToQualifierOrErr)
1424 return ToQualifierOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001425
Balazs Keri3b30d652018-10-19 13:32:20 +00001426 IdentifierInfo *ToName = Importer.Import(T->getIdentifier());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001427
1428 SmallVector<TemplateArgument, 2> ToPack;
1429 ToPack.reserve(T->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00001430 if (Error Err = ImportTemplateArguments(
1431 T->getArgs(), T->getNumArgs(), ToPack))
1432 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001433
1434 return Importer.getToContext().getDependentTemplateSpecializationType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001435 T->getKeyword(), *ToQualifierOrErr, ToName, ToPack);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001436}
1437
Balazs Keri3b30d652018-10-19 13:32:20 +00001438ExpectedType
1439ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
1440 auto ToQualifierOrErr = import(T->getQualifier());
1441 if (!ToQualifierOrErr)
1442 return ToQualifierOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00001443
1444 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
Peter Szecsice7f3182018-05-07 12:08:27 +00001445
Balazs Keri3b30d652018-10-19 13:32:20 +00001446 QualType Canon;
1447 if (T != T->getCanonicalTypeInternal().getTypePtr()) {
1448 if (ExpectedType TyOrErr = import(T->getCanonicalTypeInternal()))
1449 Canon = (*TyOrErr).getCanonicalType();
1450 else
1451 return TyOrErr.takeError();
1452 }
Peter Szecsice7f3182018-05-07 12:08:27 +00001453
Balazs Keri3b30d652018-10-19 13:32:20 +00001454 return Importer.getToContext().getDependentNameType(T->getKeyword(),
1455 *ToQualifierOrErr,
Peter Szecsice7f3182018-05-07 12:08:27 +00001456 Name, Canon);
1457}
1458
Balazs Keri3b30d652018-10-19 13:32:20 +00001459ExpectedType
1460ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
1461 Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(T->getDecl());
1462 if (!ToDeclOrErr)
1463 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001464
Balazs Keri3b30d652018-10-19 13:32:20 +00001465 return Importer.getToContext().getObjCInterfaceType(*ToDeclOrErr);
John McCall8b07ec22010-05-15 11:32:37 +00001466}
1467
Balazs Keri3b30d652018-10-19 13:32:20 +00001468ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1469 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1470 if (!ToBaseTypeOrErr)
1471 return ToBaseTypeOrErr.takeError();
John McCall8b07ec22010-05-15 11:32:37 +00001472
Douglas Gregore9d95f12015-07-07 03:57:35 +00001473 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001474 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001475 if (ExpectedType TyOrErr = import(TypeArg))
1476 TypeArgs.push_back(*TyOrErr);
1477 else
1478 return TyOrErr.takeError();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001479 }
1480
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001481 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001482 for (auto *P : T->quals()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001483 if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(P))
1484 Protocols.push_back(*ProtocolOrErr);
1485 else
1486 return ProtocolOrErr.takeError();
1487
Douglas Gregor96e578d2010-02-05 17:54:41 +00001488 }
1489
Balazs Keri3b30d652018-10-19 13:32:20 +00001490 return Importer.getToContext().getObjCObjectType(*ToBaseTypeOrErr, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001491 Protocols,
1492 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001493}
1494
Balazs Keri3b30d652018-10-19 13:32:20 +00001495ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001496ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001497 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1498 if (!ToPointeeTypeOrErr)
1499 return ToPointeeTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001500
Balazs Keri3b30d652018-10-19 13:32:20 +00001501 return Importer.getToContext().getObjCObjectPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001502}
1503
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001504//----------------------------------------------------------------------------
1505// Import Declarations
1506//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00001507Error ASTNodeImporter::ImportDeclParts(
1508 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
1509 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) {
Gabor Marton6e1510c2018-07-12 11:50:21 +00001510 // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop.
1511 // example: int struct_in_proto(struct data_t{int a;int b;} *d);
1512 DeclContext *OrigDC = D->getDeclContext();
1513 FunctionDecl *FunDecl;
1514 if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) &&
1515 FunDecl->hasBody()) {
Gabor Martonfe68e292018-08-06 14:38:37 +00001516 auto getLeafPointeeType = [](const Type *T) {
1517 while (T->isPointerType() || T->isArrayType()) {
1518 T = T->getPointeeOrArrayElementType();
1519 }
1520 return T;
1521 };
1522 for (const ParmVarDecl *P : FunDecl->parameters()) {
1523 const Type *LeafT =
1524 getLeafPointeeType(P->getType().getCanonicalType().getTypePtr());
1525 auto *RT = dyn_cast<RecordType>(LeafT);
1526 if (RT && RT->getDecl() == D) {
1527 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1528 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00001529 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Gabor Martonfe68e292018-08-06 14:38:37 +00001530 }
Gabor Marton6e1510c2018-07-12 11:50:21 +00001531 }
1532 }
1533
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001534 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001535 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
1536 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001537
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001538 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001539 if (Error Err = importInto(Name, D->getDeclName()))
1540 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001541
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001542 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001543 if (Error Err = importInto(Loc, D->getLocation()))
1544 return Err;
1545
Sean Callanan59721b32015-04-28 18:41:46 +00001546 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Gabor Martonbe77a982018-12-12 11:22:55 +00001547 if (ToD)
1548 if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(D, ToD))
1549 return Err;
Balazs Keri3b30d652018-10-19 13:32:20 +00001550
1551 return Error::success();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001552}
1553
Balazs Keri3b30d652018-10-19 13:32:20 +00001554Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001555 if (!FromD)
Balazs Keri3b30d652018-10-19 13:32:20 +00001556 return Error::success();
Fangrui Song6907ce22018-07-30 19:24:48 +00001557
Balazs Keri3b30d652018-10-19 13:32:20 +00001558 if (!ToD)
1559 if (Error Err = importInto(ToD, FromD))
1560 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001561
Balazs Keri3b30d652018-10-19 13:32:20 +00001562 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1563 if (RecordDecl *ToRecord = cast<RecordDecl>(ToD)) {
1564 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() &&
1565 !ToRecord->getDefinition()) {
1566 if (Error Err = ImportDefinition(FromRecord, ToRecord))
1567 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001568 }
1569 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001570 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001571 }
1572
Balazs Keri3b30d652018-10-19 13:32:20 +00001573 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1574 if (EnumDecl *ToEnum = cast<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001575 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001576 if (Error Err = ImportDefinition(FromEnum, ToEnum))
1577 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001578 }
1579 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001580 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001581 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001582
1583 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001584}
1585
Balazs Keri3b30d652018-10-19 13:32:20 +00001586Error
1587ASTNodeImporter::ImportDeclarationNameLoc(
1588 const DeclarationNameInfo &From, DeclarationNameInfo& To) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001589 // NOTE: To.Name and To.Loc are already imported.
1590 // We only have to import To.LocInfo.
1591 switch (To.getName().getNameKind()) {
1592 case DeclarationName::Identifier:
1593 case DeclarationName::ObjCZeroArgSelector:
1594 case DeclarationName::ObjCOneArgSelector:
1595 case DeclarationName::ObjCMultiArgSelector:
1596 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001597 case DeclarationName::CXXDeductionGuideName:
Balazs Keri3b30d652018-10-19 13:32:20 +00001598 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001599
1600 case DeclarationName::CXXOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001601 if (auto ToRangeOrErr = import(From.getCXXOperatorNameRange()))
1602 To.setCXXOperatorNameRange(*ToRangeOrErr);
1603 else
1604 return ToRangeOrErr.takeError();
1605 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001606 }
1607 case DeclarationName::CXXLiteralOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001608 if (ExpectedSLoc LocOrErr = import(From.getCXXLiteralOperatorNameLoc()))
1609 To.setCXXLiteralOperatorNameLoc(*LocOrErr);
1610 else
1611 return LocOrErr.takeError();
1612 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001613 }
1614 case DeclarationName::CXXConstructorName:
1615 case DeclarationName::CXXDestructorName:
1616 case DeclarationName::CXXConversionFunctionName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001617 if (auto ToTInfoOrErr = import(From.getNamedTypeInfo()))
1618 To.setNamedTypeInfo(*ToTInfoOrErr);
1619 else
1620 return ToTInfoOrErr.takeError();
1621 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001622 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001623 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001624 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001625}
1626
Balazs Keri3b30d652018-10-19 13:32:20 +00001627Error
1628ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001629 if (Importer.isMinimalImport() && !ForceImport) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001630 auto ToDCOrErr = Importer.ImportContext(FromDC);
1631 return ToDCOrErr.takeError();
1632 }
Davide Italiano93a64ef2018-10-30 20:46:29 +00001633 llvm::SmallVector<Decl *, 8> ImportedDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00001634 for (auto *From : FromDC->decls()) {
1635 ExpectedDecl ImportedOrErr = import(From);
Davide Italiano93a64ef2018-10-30 20:46:29 +00001636 if (!ImportedOrErr)
Balazs Keri3b30d652018-10-19 13:32:20 +00001637 // Ignore the error, continue with next Decl.
1638 // FIXME: Handle this case somehow better.
Davide Italiano93a64ef2018-10-30 20:46:29 +00001639 consumeError(ImportedOrErr.takeError());
Douglas Gregor0a791672011-01-18 03:11:38 +00001640 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001641
Balazs Keri3b30d652018-10-19 13:32:20 +00001642 return Error::success();
Douglas Gregor968d6332010-02-21 18:24:45 +00001643}
1644
Balazs Keri3b30d652018-10-19 13:32:20 +00001645Error ASTNodeImporter::ImportDeclContext(
1646 Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) {
1647 auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext());
1648 if (!ToDCOrErr)
1649 return ToDCOrErr.takeError();
1650 ToDC = *ToDCOrErr;
1651
1652 if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) {
1653 auto ToLexicalDCOrErr = Importer.ImportContext(
1654 FromD->getLexicalDeclContext());
1655 if (!ToLexicalDCOrErr)
1656 return ToLexicalDCOrErr.takeError();
1657 ToLexicalDC = *ToLexicalDCOrErr;
1658 } else
1659 ToLexicalDC = ToDC;
1660
1661 return Error::success();
1662}
1663
1664Error ASTNodeImporter::ImportImplicitMethods(
Balazs Keri1d20cc22018-07-16 12:16:39 +00001665 const CXXRecordDecl *From, CXXRecordDecl *To) {
1666 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1667 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001668
Balazs Keri1d20cc22018-07-16 12:16:39 +00001669 for (CXXMethodDecl *FromM : From->methods())
Balazs Keri3b30d652018-10-19 13:32:20 +00001670 if (FromM->isImplicit()) {
1671 Expected<CXXMethodDecl *> ToMOrErr = import(FromM);
1672 if (!ToMOrErr)
1673 return ToMOrErr.takeError();
1674 }
1675
1676 return Error::success();
Balazs Keri1d20cc22018-07-16 12:16:39 +00001677}
1678
Balazs Keri3b30d652018-10-19 13:32:20 +00001679static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1680 ASTImporter &Importer) {
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001681 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
Balazs Keri57949eb2019-03-25 09:16:39 +00001682 if (ExpectedDecl ToTypedefOrErr = Importer.Import_New(FromTypedef))
1683 To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr));
1684 else
1685 return ToTypedefOrErr.takeError();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001686 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001687 return Error::success();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001688}
1689
Balazs Keri3b30d652018-10-19 13:32:20 +00001690Error ASTNodeImporter::ImportDefinition(
1691 RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor95d82832012-01-24 18:36:04 +00001692 if (To->getDefinition() || To->isBeingDefined()) {
1693 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001694 return ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001695
Balazs Keri3b30d652018-10-19 13:32:20 +00001696 return Error::success();
Douglas Gregor95d82832012-01-24 18:36:04 +00001697 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001698
Douglas Gregore2e50d332010-12-01 01:36:18 +00001699 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001700
Balazs Keri3b30d652018-10-19 13:32:20 +00001701 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1702 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001703
Douglas Gregore2e50d332010-12-01 01:36:18 +00001704 // Add base classes.
Gabor Marton17d39672018-11-26 15:54:08 +00001705 auto *ToCXX = dyn_cast<CXXRecordDecl>(To);
1706 auto *FromCXX = dyn_cast<CXXRecordDecl>(From);
1707 if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) {
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001708
1709 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1710 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1711 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001712 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001713 ToData.Aggregate = FromData.Aggregate;
1714 ToData.PlainOldData = FromData.PlainOldData;
1715 ToData.Empty = FromData.Empty;
1716 ToData.Polymorphic = FromData.Polymorphic;
1717 ToData.Abstract = FromData.Abstract;
1718 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001719 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1720 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1721 ToData.HasBasesWithNonStaticDataMembers =
1722 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001723 ToData.HasPrivateFields = FromData.HasPrivateFields;
1724 ToData.HasProtectedFields = FromData.HasProtectedFields;
1725 ToData.HasPublicFields = FromData.HasPublicFields;
1726 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001727 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001728 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001729 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001730 ToData.HasUninitializedReferenceMember
1731 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001732 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001733 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1734 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001735 ToData.NeedOverloadResolutionForCopyConstructor
1736 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001737 ToData.NeedOverloadResolutionForMoveConstructor
1738 = FromData.NeedOverloadResolutionForMoveConstructor;
1739 ToData.NeedOverloadResolutionForMoveAssignment
1740 = FromData.NeedOverloadResolutionForMoveAssignment;
1741 ToData.NeedOverloadResolutionForDestructor
1742 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001743 ToData.DefaultedCopyConstructorIsDeleted
1744 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001745 ToData.DefaultedMoveConstructorIsDeleted
1746 = FromData.DefaultedMoveConstructorIsDeleted;
1747 ToData.DefaultedMoveAssignmentIsDeleted
1748 = FromData.DefaultedMoveAssignmentIsDeleted;
1749 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001750 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1751 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001752 ToData.HasConstexprNonCopyMoveConstructor
1753 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001754 ToData.HasDefaultedDefaultConstructor
1755 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001756 ToData.DefaultedDefaultConstructorIsConstexpr
1757 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001758 ToData.HasConstexprDefaultConstructor
1759 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001760 ToData.HasNonLiteralTypeFieldsOrBases
1761 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001762 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001763 ToData.UserProvidedDefaultConstructor
1764 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001765 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001766 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1767 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1768 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1769 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001770 ToData.ImplicitCopyAssignmentHasConstParam
1771 = FromData.ImplicitCopyAssignmentHasConstParam;
1772 ToData.HasDeclaredCopyConstructorWithConstParam
1773 = FromData.HasDeclaredCopyConstructorWithConstParam;
1774 ToData.HasDeclaredCopyAssignmentWithConstParam
1775 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001776
Shafik Yaghmour16b90732019-04-26 18:51:28 +00001777 // Copy over the data stored in RecordDeclBits
1778 ToCXX->setArgPassingRestrictions(FromCXX->getArgPassingRestrictions());
1779
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001780 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001781 for (const auto &Base1 : FromCXX->bases()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001782 ExpectedType TyOrErr = import(Base1.getType());
1783 if (!TyOrErr)
1784 return TyOrErr.takeError();
Douglas Gregor752a5952011-01-03 22:36:02 +00001785
1786 SourceLocation EllipsisLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00001787 if (Base1.isPackExpansion()) {
1788 if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc()))
1789 EllipsisLoc = *LocOrErr;
1790 else
1791 return LocOrErr.takeError();
1792 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001793
1794 // Ensure that we have a definition for the base.
Balazs Keri3b30d652018-10-19 13:32:20 +00001795 if (Error Err =
1796 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()))
1797 return Err;
1798
1799 auto RangeOrErr = import(Base1.getSourceRange());
1800 if (!RangeOrErr)
1801 return RangeOrErr.takeError();
1802
1803 auto TSIOrErr = import(Base1.getTypeSourceInfo());
1804 if (!TSIOrErr)
1805 return TSIOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001806
Douglas Gregore2e50d332010-12-01 01:36:18 +00001807 Bases.push_back(
Balazs Keri3b30d652018-10-19 13:32:20 +00001808 new (Importer.getToContext()) CXXBaseSpecifier(
1809 *RangeOrErr,
1810 Base1.isVirtual(),
1811 Base1.isBaseOfClass(),
1812 Base1.getAccessSpecifierAsWritten(),
1813 *TSIOrErr,
1814 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001815 }
1816 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001817 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001818 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001819
Douglas Gregor2e15c842012-02-01 21:00:38 +00001820 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001821 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1822 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001823
Douglas Gregore2e50d332010-12-01 01:36:18 +00001824 To->completeDefinition();
Balazs Keri3b30d652018-10-19 13:32:20 +00001825 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001826}
1827
Balazs Keri3b30d652018-10-19 13:32:20 +00001828Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) {
Sean Callanan59721b32015-04-28 18:41:46 +00001829 if (To->getAnyInitializer())
Balazs Keri3b30d652018-10-19 13:32:20 +00001830 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001831
Gabor Martonac3a5d62018-09-17 12:04:52 +00001832 Expr *FromInit = From->getInit();
1833 if (!FromInit)
Balazs Keri3b30d652018-10-19 13:32:20 +00001834 return Error::success();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001835
Balazs Keri3b30d652018-10-19 13:32:20 +00001836 ExpectedExpr ToInitOrErr = import(FromInit);
1837 if (!ToInitOrErr)
1838 return ToInitOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001839
Balazs Keri3b30d652018-10-19 13:32:20 +00001840 To->setInit(*ToInitOrErr);
Gabor Martonac3a5d62018-09-17 12:04:52 +00001841 if (From->isInitKnownICE()) {
1842 EvaluatedStmt *Eval = To->ensureEvaluatedStmt();
1843 Eval->CheckedICE = true;
1844 Eval->IsICE = From->isInitICE();
1845 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001846
1847 // FIXME: Other bits to merge?
Balazs Keri3b30d652018-10-19 13:32:20 +00001848 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001849}
1850
Balazs Keri3b30d652018-10-19 13:32:20 +00001851Error ASTNodeImporter::ImportDefinition(
1852 EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00001853 if (To->getDefinition() || To->isBeingDefined()) {
1854 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001855 return ImportDeclContext(From, /*ForceImport=*/true);
1856 return Error::success();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001857 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001858
Douglas Gregord451ea92011-07-29 23:31:30 +00001859 To->startDefinition();
1860
Balazs Keri3b30d652018-10-19 13:32:20 +00001861 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1862 return Err;
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001863
Balazs Keri3b30d652018-10-19 13:32:20 +00001864 ExpectedType ToTypeOrErr =
1865 import(Importer.getFromContext().getTypeDeclType(From));
1866 if (!ToTypeOrErr)
1867 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001868
Balazs Keri3b30d652018-10-19 13:32:20 +00001869 ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType());
1870 if (!ToPromotionTypeOrErr)
1871 return ToPromotionTypeOrErr.takeError();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001872
1873 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001874 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1875 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001876
Douglas Gregord451ea92011-07-29 23:31:30 +00001877 // FIXME: we might need to merge the number of positive or negative bits
1878 // if the enumerator lists don't match.
Balazs Keri3b30d652018-10-19 13:32:20 +00001879 To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr,
Douglas Gregord451ea92011-07-29 23:31:30 +00001880 From->getNumPositiveBits(),
1881 From->getNumNegativeBits());
Balazs Keri3b30d652018-10-19 13:32:20 +00001882 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001883}
1884
Balazs Keri3b30d652018-10-19 13:32:20 +00001885Error ASTNodeImporter::ImportTemplateArguments(
1886 const TemplateArgument *FromArgs, unsigned NumFromArgs,
1887 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001888 for (unsigned I = 0; I != NumFromArgs; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001889 if (auto ToOrErr = import(FromArgs[I]))
1890 ToArgs.push_back(*ToOrErr);
1891 else
1892 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001893 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001894
Balazs Keri3b30d652018-10-19 13:32:20 +00001895 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001896}
1897
Balazs Keri3b30d652018-10-19 13:32:20 +00001898// FIXME: Do not forget to remove this and use only 'import'.
1899Expected<TemplateArgument>
1900ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1901 return import(From);
1902}
1903
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001904template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +00001905Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001906 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1907 for (const auto &FromLoc : Container) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001908 if (auto ToLocOrErr = import(FromLoc))
1909 ToTAInfo.addArgument(*ToLocOrErr);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001910 else
Balazs Keri3b30d652018-10-19 13:32:20 +00001911 return ToLocOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001912 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001913 return Error::success();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001914}
1915
Gabor Marton26f72a92018-07-12 09:42:05 +00001916static StructuralEquivalenceKind
1917getStructuralEquivalenceKind(const ASTImporter &Importer) {
1918 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
1919 : StructuralEquivalenceKind::Default;
1920}
1921
Gabor Marton950fb572018-07-17 12:39:27 +00001922bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
1923 StructuralEquivalenceContext Ctx(
1924 Importer.getFromContext(), Importer.getToContext(),
1925 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1926 false, Complain);
1927 return Ctx.IsEquivalent(From, To);
1928}
1929
1930bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001931 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001932 // Eliminate a potential failure point where we attempt to re-import
1933 // something we're trying to import while completing ToRecord.
1934 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1935 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001936 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00001937 if (ToOriginRecord)
1938 ToRecord = ToOriginRecord;
1939 }
1940
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001941 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001942 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001943 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001944 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001945 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001946 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001947}
1948
Larisse Voufo39a1e502013-08-06 01:03:05 +00001949bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1950 bool Complain) {
1951 StructuralEquivalenceContext Ctx(
1952 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001953 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1954 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001955 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001956}
1957
Douglas Gregor98c10182010-02-12 22:17:39 +00001958bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00001959 // Eliminate a potential failure point where we attempt to re-import
Raphael Isemannfa26c202019-04-09 14:18:23 +00001960 // something we're trying to import while completing ToEnum.
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00001961 if (Decl *ToOrigin = Importer.GetOriginalDecl(ToEnum))
1962 if (auto *ToOriginEnum = dyn_cast<EnumDecl>(ToOrigin))
1963 ToEnum = ToOriginEnum;
1964
Gabor Marton26f72a92018-07-12 09:42:05 +00001965 StructuralEquivalenceContext Ctx(
1966 Importer.getFromContext(), Importer.getToContext(),
1967 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00001968 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001969}
1970
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001971bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1972 FunctionTemplateDecl *To) {
1973 StructuralEquivalenceContext Ctx(
1974 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001975 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1976 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00001977 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001978}
1979
Balazs Keric7797c42018-07-11 09:37:24 +00001980bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
1981 StructuralEquivalenceContext Ctx(
1982 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001983 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1984 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00001985 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00001986}
1987
Douglas Gregor91155082012-11-14 22:29:20 +00001988bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001989 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00001990 const llvm::APSInt &FromVal = FromEC->getInitVal();
1991 const llvm::APSInt &ToVal = ToEC->getInitVal();
1992
1993 return FromVal.isSigned() == ToVal.isSigned() &&
1994 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1995 FromVal == ToVal;
1996}
1997
1998bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001999 ClassTemplateDecl *To) {
2000 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2001 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002002 Importer.getNonEquivalentDecls(),
2003 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002004 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00002005}
2006
Larisse Voufo39a1e502013-08-06 01:03:05 +00002007bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2008 VarTemplateDecl *To) {
2009 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2010 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002011 Importer.getNonEquivalentDecls(),
2012 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002013 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002014}
2015
Balazs Keri3b30d652018-10-19 13:32:20 +00002016ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002017 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002018 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00002019 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +00002020}
2021
Balazs Keri3b30d652018-10-19 13:32:20 +00002022ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) {
2023 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2024 << D->getDeclKindName();
2025 return make_error<ImportError>(ImportError::UnsupportedConstruct);
2026}
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002027
Balazs Keri3b30d652018-10-19 13:32:20 +00002028ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
2029 // Import the context of this declaration.
2030 DeclContext *DC, *LexicalDC;
2031 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
2032 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002033
2034 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002035 ExpectedSLoc LocOrErr = import(D->getLocation());
2036 if (!LocOrErr)
2037 return LocOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002038
Gabor Marton26f72a92018-07-12 09:42:05 +00002039 EmptyDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002040 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002041 return ToD;
2042
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002043 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002044 LexicalDC->addDeclInternal(ToD);
2045 return ToD;
2046}
2047
Balazs Keri3b30d652018-10-19 13:32:20 +00002048ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002049 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00002050 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00002051
Gabor Marton26f72a92018-07-12 09:42:05 +00002052 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002053
Sean Callanan65198272011-11-17 23:20:56 +00002054 return ToD;
2055}
2056
Balazs Keri3b30d652018-10-19 13:32:20 +00002057ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2058 ExpectedSLoc LocOrErr = import(D->getLocation());
2059 if (!LocOrErr)
2060 return LocOrErr.takeError();
2061 auto ColonLocOrErr = import(D->getColonLoc());
2062 if (!ColonLocOrErr)
2063 return ColonLocOrErr.takeError();
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002064
2065 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002066 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2067 if (!DCOrErr)
2068 return DCOrErr.takeError();
2069 DeclContext *DC = *DCOrErr;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002070
Gabor Marton26f72a92018-07-12 09:42:05 +00002071 AccessSpecDecl *ToD;
2072 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002073 DC, *LocOrErr, *ColonLocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002074 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002075
2076 // Lexical DeclContext and Semantic DeclContext
2077 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00002078 ToD->setLexicalDeclContext(DC);
2079 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002080
Gabor Marton26f72a92018-07-12 09:42:05 +00002081 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002082}
2083
Balazs Keri3b30d652018-10-19 13:32:20 +00002084ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2085 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2086 if (!DCOrErr)
2087 return DCOrErr.takeError();
2088 DeclContext *DC = *DCOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002089 DeclContext *LexicalDC = DC;
2090
Balazs Keri3b30d652018-10-19 13:32:20 +00002091 SourceLocation ToLocation, ToRParenLoc;
2092 Expr *ToAssertExpr;
2093 StringLiteral *ToMessage;
2094 if (auto Imp = importSeq(
2095 D->getLocation(), D->getAssertExpr(), D->getMessage(), D->getRParenLoc()))
2096 std::tie(ToLocation, ToAssertExpr, ToMessage, ToRParenLoc) = *Imp;
2097 else
2098 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002099
Gabor Marton26f72a92018-07-12 09:42:05 +00002100 StaticAssertDecl *ToD;
2101 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002102 ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage,
2103 ToRParenLoc, D->isFailed()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002104 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002105
2106 ToD->setLexicalDeclContext(LexicalDC);
2107 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002108 return ToD;
2109}
2110
Balazs Keri3b30d652018-10-19 13:32:20 +00002111ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002112 // Import the major distinguishing characteristics of this namespace.
2113 DeclContext *DC, *LexicalDC;
2114 DeclarationName Name;
2115 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002116 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002117 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2118 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002119 if (ToD)
2120 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002121
2122 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002123 if (!Name) {
2124 // This is an anonymous namespace. Adopt an existing anonymous
2125 // namespace if we can.
2126 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002127 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002128 MergeWithNamespace = TU->getAnonymousNamespace();
2129 else
2130 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2131 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002132 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002133 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002134 for (auto *FoundDecl : FoundDecls) {
2135 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002136 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002137
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002138 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002139 MergeWithNamespace = FoundNS;
2140 ConflictingDecls.clear();
2141 break;
2142 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002143
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002144 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002145 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002146
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002147 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002148 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +00002149 ConflictingDecls.data(),
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002150 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002151 if (!Name)
2152 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002153 }
2154 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002155
Balazs Keri3b30d652018-10-19 13:32:20 +00002156 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2157 if (!BeginLocOrErr)
2158 return BeginLocOrErr.takeError();
2159
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002160 // Create the "to" namespace, if needed.
2161 NamespaceDecl *ToNamespace = MergeWithNamespace;
2162 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002163 if (GetImportedOrCreateDecl(
2164 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002165 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002166 /*PrevDecl=*/nullptr))
2167 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002168 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002169 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002170
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002171 // If this is an anonymous namespace, register it as the anonymous
2172 // namespace within its context.
2173 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002174 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002175 TU->setAnonymousNamespace(ToNamespace);
2176 else
2177 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2178 }
2179 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002180 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002181
Balazs Keri3b30d652018-10-19 13:32:20 +00002182 if (Error Err = ImportDeclContext(D))
2183 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00002184
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002185 return ToNamespace;
2186}
2187
Balazs Keri3b30d652018-10-19 13:32:20 +00002188ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002189 // Import the major distinguishing characteristics of this namespace.
2190 DeclContext *DC, *LexicalDC;
2191 DeclarationName Name;
2192 SourceLocation Loc;
2193 NamedDecl *LookupD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002194 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
2195 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002196 if (LookupD)
2197 return LookupD;
2198
2199 // NOTE: No conflict resolution is done for namespace aliases now.
2200
Balazs Keri3b30d652018-10-19 13:32:20 +00002201 SourceLocation ToNamespaceLoc, ToAliasLoc, ToTargetNameLoc;
2202 NestedNameSpecifierLoc ToQualifierLoc;
2203 NamespaceDecl *ToNamespace;
2204 if (auto Imp = importSeq(
2205 D->getNamespaceLoc(), D->getAliasLoc(), D->getQualifierLoc(),
2206 D->getTargetNameLoc(), D->getNamespace()))
2207 std::tie(
2208 ToNamespaceLoc, ToAliasLoc, ToQualifierLoc, ToTargetNameLoc,
2209 ToNamespace) = *Imp;
2210 else
2211 return Imp.takeError();
2212 IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002213
Gabor Marton26f72a92018-07-12 09:42:05 +00002214 NamespaceAliasDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002215 if (GetImportedOrCreateDecl(
2216 ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc,
2217 ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace))
Gabor Marton26f72a92018-07-12 09:42:05 +00002218 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002219
2220 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002221 LexicalDC->addDeclInternal(ToD);
2222
2223 return ToD;
2224}
2225
Balazs Keri3b30d652018-10-19 13:32:20 +00002226ExpectedDecl
2227ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002228 // Import the major distinguishing characteristics of this typedef.
2229 DeclContext *DC, *LexicalDC;
2230 DeclarationName Name;
2231 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002232 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002233 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2234 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002235 if (ToD)
2236 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002237
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002238 // If this typedef is not in block scope, determine whether we've
2239 // seen a typedef with the same name (that we can merge with) or any
2240 // other entity by that name (which name lookup could conflict with).
2241 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002242 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002243 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002244 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002245 for (auto *FoundDecl : FoundDecls) {
2246 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002247 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002248 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Gabor Martonb93baf62018-11-27 09:51:36 +00002249 QualType FromUT = D->getUnderlyingType();
2250 QualType FoundUT = FoundTypedef->getUnderlyingType();
2251 if (Importer.IsStructurallyEquivalent(FromUT, FoundUT)) {
2252 // If the "From" context has a complete underlying type but we
2253 // already have a complete underlying type then return with that.
2254 if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType())
Balazs Keri3b30d652018-10-19 13:32:20 +00002255 return Importer.MapImported(D, FoundTypedef);
Gabor Martonb93baf62018-11-27 09:51:36 +00002256 }
2257 // FIXME Handle redecl chain.
2258 break;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002259 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002260
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002261 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002262 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002263
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002264 if (!ConflictingDecls.empty()) {
2265 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002266 ConflictingDecls.data(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002267 ConflictingDecls.size());
2268 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002269 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002270 }
2271 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002272
Balazs Keri3b30d652018-10-19 13:32:20 +00002273 QualType ToUnderlyingType;
2274 TypeSourceInfo *ToTypeSourceInfo;
2275 SourceLocation ToBeginLoc;
2276 if (auto Imp = importSeq(
2277 D->getUnderlyingType(), D->getTypeSourceInfo(), D->getBeginLoc()))
2278 std::tie(ToUnderlyingType, ToTypeSourceInfo, ToBeginLoc) = *Imp;
2279 else
2280 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002281
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002282 // Create the new typedef node.
Balazs Keri3b30d652018-10-19 13:32:20 +00002283 // FIXME: ToUnderlyingType is not used.
Richard Smithdda56e42011-04-15 14:24:37 +00002284 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00002285 if (IsAlias) {
2286 if (GetImportedOrCreateDecl<TypeAliasDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002287 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2288 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002289 return ToTypedef;
2290 } else if (GetImportedOrCreateDecl<TypedefDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002291 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2292 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002293 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002294
Douglas Gregordd483172010-02-22 17:42:47 +00002295 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002296 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002297
2298 // Templated declarations should not appear in DeclContext.
2299 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
2300 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
2301 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002302
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002303 return ToTypedef;
2304}
2305
Balazs Keri3b30d652018-10-19 13:32:20 +00002306ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002307 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2308}
2309
Balazs Keri3b30d652018-10-19 13:32:20 +00002310ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002311 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2312}
2313
Balazs Keri3b30d652018-10-19 13:32:20 +00002314ExpectedDecl
2315ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
Gabor Horvath7a91c082017-11-14 11:30:38 +00002316 // Import the major distinguishing characteristics of this typedef.
2317 DeclContext *DC, *LexicalDC;
2318 DeclarationName Name;
2319 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002320 NamedDecl *FoundD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002321 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
2322 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002323 if (FoundD)
2324 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002325
2326 // If this typedef is not in block scope, determine whether we've
2327 // seen a typedef with the same name (that we can merge with) or any
2328 // other entity by that name (which name lookup could conflict with).
2329 if (!DC->isFunctionOrMethod()) {
2330 SmallVector<NamedDecl *, 4> ConflictingDecls;
2331 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002332 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002333 for (auto *FoundDecl : FoundDecls) {
2334 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002335 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002336 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002337 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002338 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002339 }
2340
2341 if (!ConflictingDecls.empty()) {
2342 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2343 ConflictingDecls.data(),
2344 ConflictingDecls.size());
2345 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002346 return make_error<ImportError>(ImportError::NameConflict);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002347 }
2348 }
2349
Balazs Keri3b30d652018-10-19 13:32:20 +00002350 TemplateParameterList *ToTemplateParameters;
2351 TypeAliasDecl *ToTemplatedDecl;
2352 if (auto Imp = importSeq(D->getTemplateParameters(), D->getTemplatedDecl()))
2353 std::tie(ToTemplateParameters, ToTemplatedDecl) = *Imp;
2354 else
2355 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002356
Gabor Marton26f72a92018-07-12 09:42:05 +00002357 TypeAliasTemplateDecl *ToAlias;
2358 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002359 Name, ToTemplateParameters, ToTemplatedDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002360 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002361
Balazs Keri3b30d652018-10-19 13:32:20 +00002362 ToTemplatedDecl->setDescribedAliasTemplate(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002363
Gabor Horvath7a91c082017-11-14 11:30:38 +00002364 ToAlias->setAccess(D->getAccess());
2365 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002366 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002367 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002368}
2369
Balazs Keri3b30d652018-10-19 13:32:20 +00002370ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002371 // Import the major distinguishing characteristics of this label.
2372 DeclContext *DC, *LexicalDC;
2373 DeclarationName Name;
2374 SourceLocation Loc;
2375 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002376 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2377 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002378 if (ToD)
2379 return ToD;
2380
2381 assert(LexicalDC->isFunctionOrMethod());
2382
Gabor Marton26f72a92018-07-12 09:42:05 +00002383 LabelDecl *ToLabel;
Balazs Keri3b30d652018-10-19 13:32:20 +00002384 if (D->isGnuLocal()) {
2385 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2386 if (!BeginLocOrErr)
2387 return BeginLocOrErr.takeError();
2388 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2389 Name.getAsIdentifierInfo(), *BeginLocOrErr))
2390 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002391
Balazs Keri3b30d652018-10-19 13:32:20 +00002392 } else {
2393 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2394 Name.getAsIdentifierInfo()))
2395 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002396
Balazs Keri3b30d652018-10-19 13:32:20 +00002397 }
2398
2399 Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt());
2400 if (!ToStmtOrErr)
2401 return ToStmtOrErr.takeError();
2402
2403 ToLabel->setStmt(*ToStmtOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002404 ToLabel->setLexicalDeclContext(LexicalDC);
2405 LexicalDC->addDeclInternal(ToLabel);
2406 return ToLabel;
2407}
2408
Balazs Keri3b30d652018-10-19 13:32:20 +00002409ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002410 // Import the major distinguishing characteristics of this enum.
2411 DeclContext *DC, *LexicalDC;
2412 DeclarationName Name;
2413 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002414 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002415 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2416 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002417 if (ToD)
2418 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002419
Douglas Gregor98c10182010-02-12 22:17:39 +00002420 // Figure out what enum name we're looking for.
2421 unsigned IDNS = Decl::IDNS_Tag;
2422 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002423 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002424 if (Error Err = importInto(
2425 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2426 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002427 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002428 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002429 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002430
Douglas Gregor98c10182010-02-12 22:17:39 +00002431 // We may already have an enum of the same name; try to find and match it.
2432 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002433 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002434 auto FoundDecls =
2435 Importer.findDeclsInToCtx(DC, SearchName);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002436 for (auto *FoundDecl : FoundDecls) {
2437 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002438 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002439
Balazs Keri3b30d652018-10-19 13:32:20 +00002440 if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002441 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Balazs Keri3b30d652018-10-19 13:32:20 +00002442 FoundDecl = Tag->getDecl();
Douglas Gregor98c10182010-02-12 22:17:39 +00002443 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002444
Balazs Keri3b30d652018-10-19 13:32:20 +00002445 if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002446 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002447 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002448 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002449
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002450 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002451 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002452
Douglas Gregor98c10182010-02-12 22:17:39 +00002453 if (!ConflictingDecls.empty()) {
Shafik Yaghmourd4263122019-04-08 20:50:21 +00002454 Name = Importer.HandleNameConflict(SearchName, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002455 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002456 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002457 if (!Name)
2458 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002459 }
2460 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002461
Balazs Keri3b30d652018-10-19 13:32:20 +00002462 SourceLocation ToBeginLoc;
2463 NestedNameSpecifierLoc ToQualifierLoc;
2464 QualType ToIntegerType;
2465 if (auto Imp = importSeq(
2466 D->getBeginLoc(), D->getQualifierLoc(), D->getIntegerType()))
2467 std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType) = *Imp;
2468 else
2469 return Imp.takeError();
2470
Douglas Gregor98c10182010-02-12 22:17:39 +00002471 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002472 EnumDecl *D2;
2473 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002474 D2, D, Importer.getToContext(), DC, ToBeginLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002475 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2476 D->isScopedUsingClassTag(), D->isFixed()))
2477 return D2;
2478
Balazs Keri3b30d652018-10-19 13:32:20 +00002479 D2->setQualifierInfo(ToQualifierLoc);
2480 D2->setIntegerType(ToIntegerType);
Douglas Gregordd483172010-02-22 17:42:47 +00002481 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002482 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002483 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002484
Douglas Gregor98c10182010-02-12 22:17:39 +00002485 // Import the definition
Balazs Keri3b30d652018-10-19 13:32:20 +00002486 if (D->isCompleteDefinition())
2487 if (Error Err = ImportDefinition(D, D2))
2488 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002489
Douglas Gregor3996e242010-02-15 22:01:00 +00002490 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002491}
2492
Balazs Keri3b30d652018-10-19 13:32:20 +00002493ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002494 bool IsFriendTemplate = false;
2495 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2496 IsFriendTemplate =
2497 DCXX->getDescribedClassTemplate() &&
2498 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
2499 Decl::FOK_None;
2500 }
2501
Douglas Gregor5c73e912010-02-11 00:48:18 +00002502 // Import the major distinguishing characteristics of this record.
2503 DeclContext *DC, *LexicalDC;
2504 DeclarationName Name;
2505 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002506 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002507 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2508 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002509 if (ToD)
2510 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002511
Douglas Gregor5c73e912010-02-11 00:48:18 +00002512 // Figure out what structure name we're looking for.
2513 unsigned IDNS = Decl::IDNS_Tag;
2514 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002515 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002516 if (Error Err = importInto(
2517 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2518 return std::move(Err);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002519 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002520 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Gabor Marton7df342a2018-12-17 12:42:12 +00002521 IDNS |= Decl::IDNS_Ordinary | Decl::IDNS_TagFriend;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002522
2523 // We may already have a record of the same name; try to find and match it.
Sean Callanan9092d472017-05-13 00:46:33 +00002524 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002525 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002526 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002527 auto FoundDecls =
2528 Importer.findDeclsInToCtx(DC, SearchName);
Sean Callanan9092d472017-05-13 00:46:33 +00002529 if (!FoundDecls.empty()) {
Gabor Marton41e38922019-03-05 11:23:24 +00002530 // We're going to have to compare D against potentially conflicting Decls,
2531 // so complete it.
Sean Callanan9092d472017-05-13 00:46:33 +00002532 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2533 D->getASTContext().getExternalSource()->CompleteType(D);
2534 }
2535
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002536 for (auto *FoundDecl : FoundDecls) {
2537 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002538 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002539
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002540 Decl *Found = FoundDecl;
2541 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2542 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002543 Found = Tag->getDecl();
2544 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002545
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002546 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Gabor Marton7df342a2018-12-17 12:42:12 +00002547 // Do not emit false positive diagnostic in case of unnamed
2548 // struct/union and in case of anonymous structs. Would be false
2549 // because there may be several anonymous/unnamed structs in a class.
2550 // E.g. these are both valid:
2551 // struct A { // unnamed structs
2552 // struct { struct A *next; } entry0;
2553 // struct { struct A *next; } entry1;
2554 // };
2555 // struct X { struct { int a; }; struct { int b; }; }; // anon structs
2556 if (!SearchName)
Gabor Marton0bebf952018-07-05 09:51:13 +00002557 if (!IsStructuralMatch(D, FoundRecord, false))
2558 continue;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002559
Gabor Marton7df342a2018-12-17 12:42:12 +00002560 if (IsStructuralMatch(D, FoundRecord)) {
2561 RecordDecl *FoundDef = FoundRecord->getDefinition();
2562 if (D->isThisDeclarationADefinition() && FoundDef) {
Balazs Keri1d20cc22018-07-16 12:16:39 +00002563 // FIXME: Structural equivalence check should check for same
2564 // user-defined methods.
2565 Importer.MapImported(D, FoundDef);
2566 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2567 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2568 assert(FoundCXX && "Record type mismatch");
2569
Gabor Marton7df342a2018-12-17 12:42:12 +00002570 if (!Importer.isMinimalImport())
Balazs Keri1d20cc22018-07-16 12:16:39 +00002571 // FoundDef may not have every implicit method that D has
2572 // because implicit methods are created only if they are used.
Balazs Keri3b30d652018-10-19 13:32:20 +00002573 if (Error Err = ImportImplicitMethods(DCXX, FoundCXX))
2574 return std::move(Err);
Balazs Keri1d20cc22018-07-16 12:16:39 +00002575 }
Douglas Gregor25791052010-02-12 00:09:27 +00002576 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002577 PrevDecl = FoundRecord->getMostRecentDecl();
2578 break;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002579 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002580 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002581
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002582 ConflictingDecls.push_back(FoundDecl);
Gabor Marton7df342a2018-12-17 12:42:12 +00002583 } // for
Fangrui Song6907ce22018-07-30 19:24:48 +00002584
Douglas Gregordd6006f2012-07-17 21:16:27 +00002585 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002586 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002587 ConflictingDecls.data(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002588 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002589 if (!Name)
2590 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002591 }
2592 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002593
Balazs Keri3b30d652018-10-19 13:32:20 +00002594 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2595 if (!BeginLocOrErr)
2596 return BeginLocOrErr.takeError();
2597
Douglas Gregor5c73e912010-02-11 00:48:18 +00002598 // Create the record declaration.
Gabor Marton7df342a2018-12-17 12:42:12 +00002599 RecordDecl *D2 = nullptr;
2600 CXXRecordDecl *D2CXX = nullptr;
2601 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2602 if (DCXX->isLambda()) {
2603 auto TInfoOrErr = import(DCXX->getLambdaTypeInfo());
2604 if (!TInfoOrErr)
2605 return TInfoOrErr.takeError();
2606 if (GetImportedOrCreateSpecialDecl(
2607 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
2608 DC, *TInfoOrErr, Loc, DCXX->isDependentLambda(),
2609 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2610 return D2CXX;
2611 ExpectedDecl CDeclOrErr = import(DCXX->getLambdaContextDecl());
2612 if (!CDeclOrErr)
2613 return CDeclOrErr.takeError();
2614 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), *CDeclOrErr);
2615 } else if (DCXX->isInjectedClassName()) {
2616 // We have to be careful to do a similar dance to the one in
2617 // Sema::ActOnStartCXXMemberDeclarations
2618 const bool DelayTypeCreation = true;
2619 if (GetImportedOrCreateDecl(
2620 D2CXX, D, Importer.getToContext(), D->getTagKind(), DC,
2621 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
2622 cast_or_null<CXXRecordDecl>(PrevDecl), DelayTypeCreation))
2623 return D2CXX;
2624 Importer.getToContext().getTypeDeclType(
2625 D2CXX, dyn_cast<CXXRecordDecl>(DC));
2626 } else {
2627 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2628 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2629 Name.getAsIdentifierInfo(),
2630 cast_or_null<CXXRecordDecl>(PrevDecl)))
2631 return D2CXX;
2632 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002633
Gabor Marton7df342a2018-12-17 12:42:12 +00002634 D2 = D2CXX;
2635 D2->setAccess(D->getAccess());
2636 D2->setLexicalDeclContext(LexicalDC);
2637 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
2638 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002639
Gabor Marton7df342a2018-12-17 12:42:12 +00002640 if (LexicalDC != DC && D->isInIdentifierNamespace(Decl::IDNS_TagFriend))
2641 DC->makeDeclVisibleInContext(D2);
2642
2643 if (ClassTemplateDecl *FromDescribed =
2644 DCXX->getDescribedClassTemplate()) {
2645 ClassTemplateDecl *ToDescribed;
2646 if (Error Err = importInto(ToDescribed, FromDescribed))
2647 return std::move(Err);
2648 D2CXX->setDescribedClassTemplate(ToDescribed);
2649 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
2650 // In a record describing a template the type should be an
2651 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2652 // previously set type to the correct value here (ToDescribed is not
2653 // available at record create).
2654 // FIXME: The previous type is cleared but not removed from
2655 // ASTContext's internal storage.
2656 CXXRecordDecl *Injected = nullptr;
2657 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2658 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2659 if (Record && Record->isInjectedClassName()) {
2660 Injected = Record;
2661 break;
Gabor Marton5915777e2018-06-26 13:44:24 +00002662 }
2663 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002664 // Create an injected type for the whole redecl chain.
2665 SmallVector<Decl *, 2> Redecls =
2666 getCanonicalForwardRedeclChain(D2CXX);
2667 for (auto *R : Redecls) {
2668 auto *RI = cast<CXXRecordDecl>(R);
2669 RI->setTypeForDecl(nullptr);
2670 // Below we create a new injected type and assign that to the
2671 // canonical decl, subsequent declarations in the chain will reuse
2672 // that type.
2673 Importer.getToContext().getInjectedClassNameType(
2674 RI, ToDescribed->getInjectedClassNameSpecialization());
2675 }
2676 // Set the new type for the previous injected decl too.
2677 if (Injected) {
2678 Injected->setTypeForDecl(nullptr);
2679 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2680 }
2681 }
2682 } else if (MemberSpecializationInfo *MemberInfo =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002683 DCXX->getMemberSpecializationInfo()) {
2684 TemplateSpecializationKind SK =
2685 MemberInfo->getTemplateSpecializationKind();
2686 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Balazs Keri3b30d652018-10-19 13:32:20 +00002687
2688 if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst))
2689 D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK);
2690 else
2691 return ToInstOrErr.takeError();
2692
2693 if (ExpectedSLoc POIOrErr =
2694 import(MemberInfo->getPointOfInstantiation()))
2695 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2696 *POIOrErr);
2697 else
2698 return POIOrErr.takeError();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002699 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002700
Gabor Marton7df342a2018-12-17 12:42:12 +00002701 } else {
2702 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
2703 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2704 Name.getAsIdentifierInfo(), PrevDecl))
2705 return D2;
2706 D2->setLexicalDeclContext(LexicalDC);
2707 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002708 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002709
Gabor Marton7df342a2018-12-17 12:42:12 +00002710 if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
2711 D2->setQualifierInfo(*QualifierLocOrErr);
2712 else
2713 return QualifierLocOrErr.takeError();
2714
2715 if (D->isAnonymousStructOrUnion())
2716 D2->setAnonymousStructOrUnion(true);
Douglas Gregor25791052010-02-12 00:09:27 +00002717
Balazs Keri3b30d652018-10-19 13:32:20 +00002718 if (D->isCompleteDefinition())
2719 if (Error Err = ImportDefinition(D, D2, IDK_Default))
2720 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00002721
Douglas Gregor3996e242010-02-15 22:01:00 +00002722 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002723}
2724
Balazs Keri3b30d652018-10-19 13:32:20 +00002725ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002726 // Import the major distinguishing characteristics of this enumerator.
2727 DeclContext *DC, *LexicalDC;
2728 DeclarationName Name;
2729 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002730 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002731 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2732 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002733 if (ToD)
2734 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002735
Fangrui Song6907ce22018-07-30 19:24:48 +00002736 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002737 // in the same context.
2738 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002739 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002740 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002741 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002742 for (auto *FoundDecl : FoundDecls) {
2743 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002744 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002745
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002746 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002747 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002748 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002749 }
2750
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002751 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002752 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002753
Douglas Gregor98c10182010-02-12 22:17:39 +00002754 if (!ConflictingDecls.empty()) {
2755 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002756 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002757 ConflictingDecls.size());
2758 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002759 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002760 }
2761 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002762
Balazs Keri3b30d652018-10-19 13:32:20 +00002763 ExpectedType TypeOrErr = import(D->getType());
2764 if (!TypeOrErr)
2765 return TypeOrErr.takeError();
2766
2767 ExpectedExpr InitOrErr = import(D->getInitExpr());
2768 if (!InitOrErr)
2769 return InitOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002770
Gabor Marton26f72a92018-07-12 09:42:05 +00002771 EnumConstantDecl *ToEnumerator;
2772 if (GetImportedOrCreateDecl(
2773 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002774 Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002775 return ToEnumerator;
2776
Douglas Gregordd483172010-02-22 17:42:47 +00002777 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002778 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002779 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002780 return ToEnumerator;
2781}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002782
Balazs Keri3b30d652018-10-19 13:32:20 +00002783Error ASTNodeImporter::ImportTemplateInformation(
2784 FunctionDecl *FromFD, FunctionDecl *ToFD) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002785 switch (FromFD->getTemplatedKind()) {
2786 case FunctionDecl::TK_NonTemplate:
2787 case FunctionDecl::TK_FunctionTemplate:
Balazs Keri3b30d652018-10-19 13:32:20 +00002788 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002789
2790 case FunctionDecl::TK_MemberSpecialization: {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002791 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00002792
2793 if (Expected<FunctionDecl *> InstFDOrErr =
2794 import(FromFD->getInstantiatedFromMemberFunction()))
2795 ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK);
2796 else
2797 return InstFDOrErr.takeError();
2798
2799 if (ExpectedSLoc POIOrErr = import(
2800 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()))
2801 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
2802 else
2803 return POIOrErr.takeError();
2804
2805 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002806 }
2807
2808 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Balazs Keri3b30d652018-10-19 13:32:20 +00002809 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002810 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002811 if (!FunctionAndArgsOrErr)
2812 return FunctionAndArgsOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002813
2814 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Balazs Keri3b30d652018-10-19 13:32:20 +00002815 Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002816
Gabor Marton5254e642018-06-27 13:32:50 +00002817 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002818 TemplateArgumentListInfo ToTAInfo;
2819 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002820 if (FromTAArgsAsWritten)
Balazs Keri3b30d652018-10-19 13:32:20 +00002821 if (Error Err = ImportTemplateArgumentListInfo(
2822 *FromTAArgsAsWritten, ToTAInfo))
2823 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002824
Balazs Keri3b30d652018-10-19 13:32:20 +00002825 ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation());
2826 if (!POIOrErr)
2827 return POIOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002828
Gabor Marton5254e642018-06-27 13:32:50 +00002829 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002830 ToFD->setFunctionTemplateSpecialization(
Balazs Keri3b30d652018-10-19 13:32:20 +00002831 std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr,
2832 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr);
2833 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002834 }
2835
2836 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2837 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2838 UnresolvedSet<8> TemplDecls;
2839 unsigned NumTemplates = FromInfo->getNumTemplates();
2840 for (unsigned I = 0; I < NumTemplates; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002841 if (Expected<FunctionTemplateDecl *> ToFTDOrErr =
2842 import(FromInfo->getTemplate(I)))
2843 TemplDecls.addDecl(*ToFTDOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002844 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002845 return ToFTDOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002846 }
2847
2848 // Import TemplateArgumentListInfo.
2849 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00002850 if (Error Err = ImportTemplateArgumentListInfo(
2851 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2852 llvm::makeArrayRef(
2853 FromInfo->getTemplateArgs(), FromInfo->getNumTemplateArgs()),
2854 ToTAInfo))
2855 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002856
2857 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2858 TemplDecls, ToTAInfo);
Balazs Keri3b30d652018-10-19 13:32:20 +00002859 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002860 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002861 }
Sam McCallfdc32072018-01-26 12:06:44 +00002862 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002863}
2864
Balazs Keri3b30d652018-10-19 13:32:20 +00002865Expected<FunctionDecl *>
Gabor Marton5254e642018-06-27 13:32:50 +00002866ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002867 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002868 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002869 if (!FunctionAndArgsOrErr)
2870 return FunctionAndArgsOrErr.takeError();
Gabor Marton5254e642018-06-27 13:32:50 +00002871
Balazs Keri3b30d652018-10-19 13:32:20 +00002872 FunctionTemplateDecl *Template;
2873 TemplateArgsTy ToTemplArgs;
2874 std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr;
Gabor Marton5254e642018-06-27 13:32:50 +00002875 void *InsertPos = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00002876 auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos);
Gabor Marton5254e642018-06-27 13:32:50 +00002877 return FoundSpec;
2878}
2879
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002880Error ASTNodeImporter::ImportFunctionDeclBody(FunctionDecl *FromFD,
2881 FunctionDecl *ToFD) {
2882 if (Stmt *FromBody = FromFD->getBody()) {
2883 if (ExpectedStmt ToBodyOrErr = import(FromBody))
2884 ToFD->setBody(*ToBodyOrErr);
2885 else
2886 return ToBodyOrErr.takeError();
2887 }
2888 return Error::success();
2889}
2890
Gabor Marton458d1452019-02-14 13:07:03 +00002891template <typename T>
2892bool ASTNodeImporter::hasSameVisibilityContext(T *Found, T *From) {
2893 if (From->hasExternalFormalLinkage())
2894 return Found->hasExternalFormalLinkage();
2895 if (Importer.GetFromTU(Found) != From->getTranslationUnitDecl())
2896 return false;
2897 if (From->isInAnonymousNamespace())
2898 return Found->isInAnonymousNamespace();
2899 else
2900 return !Found->isInAnonymousNamespace() &&
2901 !Found->hasExternalFormalLinkage();
2902}
2903
Balazs Keri3b30d652018-10-19 13:32:20 +00002904ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00002905
Balazs Keri3b30d652018-10-19 13:32:20 +00002906 SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D);
Gabor Marton5254e642018-06-27 13:32:50 +00002907 auto RedeclIt = Redecls.begin();
2908 // Import the first part of the decl chain. I.e. import all previous
2909 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00002910 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
2911 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
2912 if (!ToRedeclOrErr)
2913 return ToRedeclOrErr.takeError();
2914 }
Gabor Marton5254e642018-06-27 13:32:50 +00002915 assert(*RedeclIt == D);
2916
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002917 // Import the major distinguishing characteristics of this function.
2918 DeclContext *DC, *LexicalDC;
2919 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002920 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002921 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002922 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2923 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002924 if (ToD)
2925 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002926
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002927 FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00002928 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002929
Gabor Marton5254e642018-06-27 13:32:50 +00002930 // If this is a function template specialization, then try to find the same
Gabor Marton54058b52018-12-17 13:53:12 +00002931 // existing specialization in the "to" context. The lookup below will not
2932 // find any specialization, but would find the primary template; thus, we
2933 // have to skip normal lookup in case of specializations.
Gabor Marton5254e642018-06-27 13:32:50 +00002934 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
2935 if (D->getTemplatedKind() ==
2936 FunctionDecl::TK_FunctionTemplateSpecialization) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002937 auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D);
2938 if (!FoundFunctionOrErr)
2939 return FoundFunctionOrErr.takeError();
2940 if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) {
Gabor Martondd59d272019-03-19 14:04:50 +00002941 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
2942 return Def;
Gabor Marton5254e642018-06-27 13:32:50 +00002943 FoundByLookup = FoundFunction;
2944 }
2945 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002946 // Try to find a function in our own ("to") context with the same name, same
2947 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00002948 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002949 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00002950 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00002951 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002952 for (auto *FoundDecl : FoundDecls) {
2953 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002954 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002955
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002956 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00002957 if (!hasSameVisibilityContext(FoundFunction, D))
2958 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002959
Gabor Marton458d1452019-02-14 13:07:03 +00002960 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Martondd59d272019-03-19 14:04:50 +00002961 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
2962 return Def;
Gabor Marton458d1452019-02-14 13:07:03 +00002963 FoundByLookup = FoundFunction;
2964 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002965 }
Gabor Marton458d1452019-02-14 13:07:03 +00002966 // FIXME: Check for overloading more carefully, e.g., by boosting
2967 // Sema::IsOverload out to the AST library.
2968
2969 // Function overloading is okay in C++.
2970 if (Importer.getToContext().getLangOpts().CPlusPlus)
2971 continue;
2972
2973 // Complain about inconsistent function types.
Gabor Marton410f32c2019-04-01 15:29:55 +00002974 Importer.ToDiag(Loc, diag::warn_odr_function_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00002975 << Name << D->getType() << FoundFunction->getType();
2976 Importer.ToDiag(FoundFunction->getLocation(), diag::note_odr_value_here)
2977 << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002978 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002979
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002980 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002981 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002982
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002983 if (!ConflictingDecls.empty()) {
2984 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002985 ConflictingDecls.data(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002986 ConflictingDecls.size());
2987 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002988 return make_error<ImportError>(ImportError::NameConflict);
Fangrui Song6907ce22018-07-30 19:24:48 +00002989 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002990 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002991
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002992 // We do not allow more than one in-class declaration of a function. This is
2993 // because AST clients like VTableBuilder asserts on this. VTableBuilder
2994 // assumes there is only one in-class declaration. Building a redecl
2995 // chain would result in more than one in-class declaration for
2996 // overrides (even if they are part of the same redecl chain inside the
2997 // derived class.)
2998 if (FoundByLookup) {
Mikael Holmenc1c97aa2019-01-29 06:53:31 +00002999 if (isa<CXXMethodDecl>(FoundByLookup)) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003000 if (D->getLexicalDeclContext() == D->getDeclContext()) {
3001 if (!D->doesThisDeclarationHaveABody())
3002 return Importer.MapImported(D, FoundByLookup);
3003 else {
3004 // Let's continue and build up the redecl chain in this case.
3005 // FIXME Merge the functions into one decl.
3006 }
3007 }
3008 }
3009 }
3010
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003011 DeclarationNameInfo NameInfo(Name, Loc);
3012 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00003013 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
3014 return std::move(Err);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003015
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003016 QualType FromTy = D->getType();
3017 bool usedDifferentExceptionSpec = false;
3018
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003019 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003020 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3021 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3022 // FunctionDecl that we are importing the FunctionProtoType for.
3023 // To avoid an infinite recursion when importing, create the FunctionDecl
3024 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00003025 if (FromEPI.ExceptionSpec.SourceDecl ||
3026 FromEPI.ExceptionSpec.SourceTemplate ||
3027 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003028 FunctionProtoType::ExtProtoInfo DefaultEPI;
3029 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003030 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003031 usedDifferentExceptionSpec = true;
3032 }
3033 }
3034
Balazs Keri3b30d652018-10-19 13:32:20 +00003035 QualType T;
3036 TypeSourceInfo *TInfo;
3037 SourceLocation ToInnerLocStart, ToEndLoc;
3038 NestedNameSpecifierLoc ToQualifierLoc;
3039 if (auto Imp = importSeq(
3040 FromTy, D->getTypeSourceInfo(), D->getInnerLocStart(),
3041 D->getQualifierLoc(), D->getEndLoc()))
3042 std::tie(T, TInfo, ToInnerLocStart, ToQualifierLoc, ToEndLoc) = *Imp;
3043 else
3044 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003045
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003046 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003047 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003048 for (auto P : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003049 if (Expected<ParmVarDecl *> ToPOrErr = import(P))
3050 Parameters.push_back(*ToPOrErr);
3051 else
3052 return ToPOrErr.takeError();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003053 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003054
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003055 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00003056 FunctionDecl *ToFunction = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003057 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003058 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
Hans Wennborgd2b9fc82019-05-06 09:51:10 +00003059 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3060 ToInnerLocStart, NameInfo, T, TInfo,
3061 FromConstructor->isExplicit(),
3062 D->isInlineSpecified(), D->isImplicit(), D->isConstexpr()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003063 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003064 } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(D)) {
3065
3066 auto Imp =
3067 importSeq(const_cast<FunctionDecl *>(FromDtor->getOperatorDelete()),
3068 FromDtor->getOperatorDeleteThisArg());
3069
3070 if (!Imp)
3071 return Imp.takeError();
3072
3073 FunctionDecl *ToOperatorDelete;
3074 Expr *ToThisArg;
3075 std::tie(ToOperatorDelete, ToThisArg) = *Imp;
3076
Gabor Marton26f72a92018-07-12 09:42:05 +00003077 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003078 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3079 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
3080 D->isImplicit()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003081 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003082
3083 CXXDestructorDecl *ToDtor = cast<CXXDestructorDecl>(ToFunction);
3084
3085 ToDtor->setOperatorDelete(ToOperatorDelete, ToThisArg);
Gabor Marton26f72a92018-07-12 09:42:05 +00003086 } else if (CXXConversionDecl *FromConversion =
3087 dyn_cast<CXXConversionDecl>(D)) {
3088 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3089 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003090 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Hans Wennborgd2b9fc82019-05-06 09:51:10 +00003091 FromConversion->isExplicit(), D->isConstexpr(), SourceLocation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003092 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003093 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003094 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3095 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003096 ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003097 Method->isInlineSpecified(), D->isConstexpr(), SourceLocation()))
3098 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003099 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003100 if (GetImportedOrCreateDecl(ToFunction, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003101 ToInnerLocStart, NameInfo, T, TInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003102 D->getStorageClass(), D->isInlineSpecified(),
3103 D->hasWrittenPrototype(), D->isConstexpr()))
3104 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003105 }
John McCall3e11ebe2010-03-15 10:12:16 +00003106
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003107 // Connect the redecl chain.
3108 if (FoundByLookup) {
3109 auto *Recent = const_cast<FunctionDecl *>(
3110 FoundByLookup->getMostRecentDecl());
3111 ToFunction->setPreviousDecl(Recent);
3112 }
3113
3114 // Import Ctor initializers.
3115 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3116 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3117 SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers);
3118 // Import first, then allocate memory and copy if there was no error.
3119 if (Error Err = ImportContainerChecked(
3120 FromConstructor->inits(), CtorInitializers))
3121 return std::move(Err);
3122 auto **Memory =
3123 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3124 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3125 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
3126 ToCtor->setCtorInitializers(Memory);
3127 ToCtor->setNumCtorInitializers(NumInitializers);
3128 }
3129 }
3130
Balazs Keri3b30d652018-10-19 13:32:20 +00003131 ToFunction->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003132 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003133 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003134 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3135 ToFunction->setTrivial(D->isTrivial());
3136 ToFunction->setPure(D->isPure());
Balazs Keri3b30d652018-10-19 13:32:20 +00003137 ToFunction->setRangeEnd(ToEndLoc);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003138
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003139 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003140 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003141 Param->setOwningFunction(ToFunction);
3142 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003143 }
David Blaikie9c70e042011-09-21 18:16:56 +00003144 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003145
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003146 // We need to complete creation of FunctionProtoTypeLoc manually with setting
3147 // params it refers to.
3148 if (TInfo) {
3149 if (auto ProtoLoc =
3150 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
3151 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
3152 ProtoLoc.setParam(I, Parameters[I]);
3153 }
3154 }
3155
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003156 if (usedDifferentExceptionSpec) {
3157 // Update FunctionProtoType::ExtProtoInfo.
Balazs Keri3b30d652018-10-19 13:32:20 +00003158 if (ExpectedType TyOrErr = import(D->getType()))
3159 ToFunction->setType(*TyOrErr);
3160 else
3161 return TyOrErr.takeError();
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003162 }
3163
Balazs Keria35798d2018-07-17 09:52:41 +00003164 // Import the describing template function, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00003165 if (FromFT) {
3166 auto ToFTOrErr = import(FromFT);
3167 if (!ToFTOrErr)
3168 return ToFTOrErr.takeError();
3169 }
Balazs Keria35798d2018-07-17 09:52:41 +00003170
Gabor Marton5254e642018-06-27 13:32:50 +00003171 if (D->doesThisDeclarationHaveABody()) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003172 Error Err = ImportFunctionDeclBody(D, ToFunction);
3173
3174 if (Err)
3175 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003176 }
3177
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003178 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003179
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003180 // If it is a template, import all related things.
Balazs Keri3b30d652018-10-19 13:32:20 +00003181 if (Error Err = ImportTemplateInformation(D, ToFunction))
3182 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003183
Gabor Marton5254e642018-06-27 13:32:50 +00003184 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
3185
3186 // TODO Can we generalize this approach to other AST nodes as well?
3187 if (D->getDeclContext()->containsDeclAndLoad(D))
3188 DC->addDeclInternal(ToFunction);
3189 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003190 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003191
Gabor Marton5254e642018-06-27 13:32:50 +00003192 // Friend declaration's lexical context is the befriending class, but the
3193 // semantic context is the enclosing scope of the befriending class.
3194 // We want the friend functions to be found in the semantic context by lookup.
3195 // FIXME should we handle this generically in VisitFriendDecl?
3196 // In Other cases when LexicalDC != DC we don't want it to be added,
3197 // e.g out-of-class definitions like void B::f() {} .
3198 if (LexicalDC != DC && IsFriend) {
3199 DC->makeDeclVisibleInContext(ToFunction);
3200 }
3201
Gabor Marton7a0841e2018-10-29 10:18:28 +00003202 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
3203 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
3204
Gabor Marton5254e642018-06-27 13:32:50 +00003205 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003206 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3207 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3208 if (!ToRedeclOrErr)
3209 return ToRedeclOrErr.takeError();
3210 }
Gabor Marton5254e642018-06-27 13:32:50 +00003211
Douglas Gregor43f54792010-02-17 02:12:47 +00003212 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003213}
3214
Balazs Keri3b30d652018-10-19 13:32:20 +00003215ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003216 return VisitFunctionDecl(D);
3217}
3218
Balazs Keri3b30d652018-10-19 13:32:20 +00003219ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003220 return VisitCXXMethodDecl(D);
3221}
3222
Balazs Keri3b30d652018-10-19 13:32:20 +00003223ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003224 return VisitCXXMethodDecl(D);
3225}
3226
Balazs Keri3b30d652018-10-19 13:32:20 +00003227ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003228 return VisitCXXMethodDecl(D);
3229}
3230
Balazs Keri3b30d652018-10-19 13:32:20 +00003231ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003232 // Import the major distinguishing characteristics of a variable.
3233 DeclContext *DC, *LexicalDC;
3234 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003235 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003236 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003237 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3238 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003239 if (ToD)
3240 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003241
Fangrui Song6907ce22018-07-30 19:24:48 +00003242 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003243 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003244 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003245 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003246 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003247 if (!Name &&
3248 ASTImporter::getFieldIndex(D) !=
3249 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003250 continue;
3251
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003252 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003253 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003254 Importer.MapImported(D, FoundField);
Gabor Marton42e15de2018-08-22 11:52:14 +00003255 // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the
3256 // initializer of a FieldDecl might not had been instantiated in the
3257 // "To" context. However, the "From" context might instantiated that,
3258 // thus we have to merge that.
3259 if (Expr *FromInitializer = D->getInClassInitializer()) {
3260 // We don't have yet the initializer set.
3261 if (FoundField->hasInClassInitializer() &&
3262 !FoundField->getInClassInitializer()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003263 if (ExpectedExpr ToInitializerOrErr = import(FromInitializer))
3264 FoundField->setInClassInitializer(*ToInitializerOrErr);
3265 else {
3266 // We can't return error here,
Gabor Marton42e15de2018-08-22 11:52:14 +00003267 // since we already mapped D as imported.
Balazs Keri3b30d652018-10-19 13:32:20 +00003268 // FIXME: warning message?
3269 consumeError(ToInitializerOrErr.takeError());
Gabor Marton42e15de2018-08-22 11:52:14 +00003270 return FoundField;
Balazs Keri3b30d652018-10-19 13:32:20 +00003271 }
Gabor Marton42e15de2018-08-22 11:52:14 +00003272 }
3273 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003274 return FoundField;
3275 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003276
Balazs Keri3b30d652018-10-19 13:32:20 +00003277 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003278 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003279 << Name << D->getType() << FoundField->getType();
3280 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3281 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003282
3283 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003284 }
3285 }
3286
Balazs Keri3b30d652018-10-19 13:32:20 +00003287 QualType ToType;
3288 TypeSourceInfo *ToTInfo;
3289 Expr *ToBitWidth;
3290 SourceLocation ToInnerLocStart;
3291 Expr *ToInitializer;
3292 if (auto Imp = importSeq(
3293 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(),
3294 D->getInnerLocStart(), D->getInClassInitializer()))
3295 std::tie(
3296 ToType, ToTInfo, ToBitWidth, ToInnerLocStart, ToInitializer) = *Imp;
3297 else
3298 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003299
Gabor Marton26f72a92018-07-12 09:42:05 +00003300 FieldDecl *ToField;
3301 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003302 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3303 ToType, ToTInfo, ToBitWidth, D->isMutable(),
3304 D->getInClassInitStyle()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003305 return ToField;
3306
Douglas Gregordd483172010-02-22 17:42:47 +00003307 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003308 ToField->setLexicalDeclContext(LexicalDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00003309 if (ToInitializer)
3310 ToField->setInClassInitializer(ToInitializer);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003311 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00003312 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003313 return ToField;
3314}
3315
Balazs Keri3b30d652018-10-19 13:32:20 +00003316ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003317 // Import the major distinguishing characteristics of a variable.
3318 DeclContext *DC, *LexicalDC;
3319 DeclarationName Name;
3320 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003321 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003322 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3323 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003324 if (ToD)
3325 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003326
Fangrui Song6907ce22018-07-30 19:24:48 +00003327 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003328 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003329 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003330 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003331 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003332 if (!Name &&
3333 ASTImporter::getFieldIndex(D) !=
3334 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003335 continue;
3336
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003337 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003338 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003339 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003340 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003341 return FoundField;
3342 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003343
3344 // If there are more anonymous fields to check, continue.
3345 if (!Name && I < N-1)
3346 continue;
3347
Balazs Keri3b30d652018-10-19 13:32:20 +00003348 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003349 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003350 << Name << D->getType() << FoundField->getType();
3351 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3352 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003353
3354 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003355 }
3356 }
3357
Francois Pichet783dd6e2010-11-21 06:08:52 +00003358 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003359 auto TypeOrErr = import(D->getType());
3360 if (!TypeOrErr)
3361 return TypeOrErr.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003362
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003363 auto **NamedChain =
3364 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00003365
3366 unsigned i = 0;
Balazs Keri3b30d652018-10-19 13:32:20 +00003367 for (auto *PI : D->chain())
3368 if (Expected<NamedDecl *> ToD = import(PI))
3369 NamedChain[i++] = *ToD;
3370 else
3371 return ToD.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003372
Gabor Marton26f72a92018-07-12 09:42:05 +00003373 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
3374 IndirectFieldDecl *ToIndirectField;
3375 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003376 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
Gabor Marton26f72a92018-07-12 09:42:05 +00003377 // FIXME here we leak `NamedChain` which is allocated before
3378 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00003379
Francois Pichet783dd6e2010-11-21 06:08:52 +00003380 ToIndirectField->setAccess(D->getAccess());
3381 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003382 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003383 return ToIndirectField;
3384}
3385
Balazs Keri3b30d652018-10-19 13:32:20 +00003386ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00003387 // Import the major distinguishing characteristics of a declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00003388 DeclContext *DC, *LexicalDC;
3389 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
3390 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003391
3392 // Determine whether we've already imported this decl.
Gabor Marton54058b52018-12-17 13:53:12 +00003393 // FriendDecl is not a NamedDecl so we cannot use lookup.
Aleksei Sidorina693b372016-09-28 10:16:56 +00003394 auto *RD = cast<CXXRecordDecl>(DC);
3395 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003396
3397 while (ImportedFriend) {
3398 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003399 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3400 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003401 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003402
3403 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3404 if (Importer.IsStructurallyEquivalent(
3405 D->getFriendType()->getType(),
3406 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003407 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003408 }
3409 ImportedFriend = ImportedFriend->getNextFriend();
3410 }
3411
3412 // Not found. Create it.
3413 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003414 if (NamedDecl *FriendD = D->getFriendDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003415 NamedDecl *ToFriendD;
3416 if (Error Err = importInto(ToFriendD, FriendD))
3417 return std::move(Err);
3418
3419 if (FriendD->getFriendObjectKind() != Decl::FOK_None &&
Peter Szecsib180eeb2018-04-25 17:28:03 +00003420 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3421 ToFriendD->setObjectOfFriendDecl(false);
3422
3423 ToFU = ToFriendD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003424 } else { // The friend is a type, not a decl.
3425 if (auto TSIOrErr = import(D->getFriendType()))
3426 ToFU = *TSIOrErr;
3427 else
3428 return TSIOrErr.takeError();
3429 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00003430
3431 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003432 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003433 for (unsigned I = 0; I < D->NumTPLists; I++) {
Balazs Keridec09162019-03-20 15:42:42 +00003434 if (auto ListOrErr = import(FromTPLists[I]))
Balazs Keri3b30d652018-10-19 13:32:20 +00003435 ToTPLists[I] = *ListOrErr;
3436 else
3437 return ListOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003438 }
3439
Balazs Keri3b30d652018-10-19 13:32:20 +00003440 auto LocationOrErr = import(D->getLocation());
3441 if (!LocationOrErr)
3442 return LocationOrErr.takeError();
3443 auto FriendLocOrErr = import(D->getFriendLoc());
3444 if (!FriendLocOrErr)
3445 return FriendLocOrErr.takeError();
3446
Gabor Marton26f72a92018-07-12 09:42:05 +00003447 FriendDecl *FrD;
3448 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003449 *LocationOrErr, ToFU,
3450 *FriendLocOrErr, ToTPLists))
Gabor Marton26f72a92018-07-12 09:42:05 +00003451 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003452
3453 FrD->setAccess(D->getAccess());
3454 FrD->setLexicalDeclContext(LexicalDC);
3455 LexicalDC->addDeclInternal(FrD);
3456 return FrD;
3457}
3458
Balazs Keri3b30d652018-10-19 13:32:20 +00003459ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003460 // Import the major distinguishing characteristics of an ivar.
3461 DeclContext *DC, *LexicalDC;
3462 DeclarationName Name;
3463 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003464 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003465 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3466 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003467 if (ToD)
3468 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003469
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003470 // Determine whether we've already imported this ivar
Gabor Marton54058b52018-12-17 13:53:12 +00003471 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003472 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003473 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003474 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003475 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003476 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003477 return FoundIvar;
3478 }
3479
Gabor Marton410f32c2019-04-01 15:29:55 +00003480 Importer.ToDiag(Loc, diag::warn_odr_ivar_type_inconsistent)
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003481 << Name << D->getType() << FoundIvar->getType();
3482 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3483 << FoundIvar->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003484
3485 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003486 }
3487 }
3488
Balazs Keri3b30d652018-10-19 13:32:20 +00003489 QualType ToType;
3490 TypeSourceInfo *ToTypeSourceInfo;
3491 Expr *ToBitWidth;
3492 SourceLocation ToInnerLocStart;
3493 if (auto Imp = importSeq(
3494 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(), D->getInnerLocStart()))
3495 std::tie(ToType, ToTypeSourceInfo, ToBitWidth, ToInnerLocStart) = *Imp;
3496 else
3497 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003498
Gabor Marton26f72a92018-07-12 09:42:05 +00003499 ObjCIvarDecl *ToIvar;
3500 if (GetImportedOrCreateDecl(
3501 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003502 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3503 ToType, ToTypeSourceInfo,
3504 D->getAccessControl(),ToBitWidth, D->getSynthesize()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003505 return ToIvar;
3506
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003507 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003508 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003509 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003510}
3511
Balazs Keri3b30d652018-10-19 13:32:20 +00003512ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003513
3514 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
3515 auto RedeclIt = Redecls.begin();
3516 // Import the first part of the decl chain. I.e. import all previous
3517 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003518 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3519 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3520 if (!RedeclOrErr)
3521 return RedeclOrErr.takeError();
3522 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003523 assert(*RedeclIt == D);
3524
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003525 // Import the major distinguishing characteristics of a variable.
3526 DeclContext *DC, *LexicalDC;
3527 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003528 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003529 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003530 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3531 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003532 if (ToD)
3533 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003534
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003535 // Try to find a variable in our own ("to") context with the same name and
3536 // in the same context as the variable we're importing.
Gabor Martonac3a5d62018-09-17 12:04:52 +00003537 VarDecl *FoundByLookup = nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003538 if (D->isFileVarDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003539 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003540 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00003541 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003542 for (auto *FoundDecl : FoundDecls) {
3543 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003544 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003545
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003546 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00003547 if (!hasSameVisibilityContext(FoundVar, D))
3548 continue;
3549 if (Importer.IsStructurallyEquivalent(D->getType(),
3550 FoundVar->getType())) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003551
Gabor Marton458d1452019-02-14 13:07:03 +00003552 // The VarDecl in the "From" context has a definition, but in the
3553 // "To" context we already have a definition.
3554 VarDecl *FoundDef = FoundVar->getDefinition();
3555 if (D->isThisDeclarationADefinition() && FoundDef)
3556 // FIXME Check for ODR error if the two definitions have
3557 // different initializers?
3558 return Importer.MapImported(D, FoundDef);
Gabor Martonac3a5d62018-09-17 12:04:52 +00003559
Gabor Marton458d1452019-02-14 13:07:03 +00003560 // The VarDecl in the "From" context has an initializer, but in the
3561 // "To" context we already have an initializer.
3562 const VarDecl *FoundDInit = nullptr;
3563 if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
3564 // FIXME Diagnose ODR error if the two initializers are different?
3565 return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit));
3566
3567 FoundByLookup = FoundVar;
3568 break;
3569 }
3570
3571 const ArrayType *FoundArray
3572 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3573 const ArrayType *TArray
3574 = Importer.getToContext().getAsArrayType(D->getType());
3575 if (FoundArray && TArray) {
3576 if (isa<IncompleteArrayType>(FoundArray) &&
3577 isa<ConstantArrayType>(TArray)) {
3578 // Import the type.
3579 if (auto TyOrErr = import(D->getType()))
3580 FoundVar->setType(*TyOrErr);
3581 else
3582 return TyOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00003583
3584 FoundByLookup = FoundVar;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003585 break;
Gabor Marton458d1452019-02-14 13:07:03 +00003586 } else if (isa<IncompleteArrayType>(TArray) &&
3587 isa<ConstantArrayType>(FoundArray)) {
3588 FoundByLookup = FoundVar;
3589 break;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003590 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003591 }
Gabor Marton458d1452019-02-14 13:07:03 +00003592
Gabor Marton410f32c2019-04-01 15:29:55 +00003593 Importer.ToDiag(Loc, diag::warn_odr_variable_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00003594 << Name << D->getType() << FoundVar->getType();
3595 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3596 << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003597 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003598
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003599 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003600 }
3601
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003602 if (!ConflictingDecls.empty()) {
3603 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003604 ConflictingDecls.data(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003605 ConflictingDecls.size());
3606 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003607 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003608 }
3609 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003610
Balazs Keri3b30d652018-10-19 13:32:20 +00003611 QualType ToType;
3612 TypeSourceInfo *ToTypeSourceInfo;
3613 SourceLocation ToInnerLocStart;
3614 NestedNameSpecifierLoc ToQualifierLoc;
3615 if (auto Imp = importSeq(
3616 D->getType(), D->getTypeSourceInfo(), D->getInnerLocStart(),
3617 D->getQualifierLoc()))
3618 std::tie(ToType, ToTypeSourceInfo, ToInnerLocStart, ToQualifierLoc) = *Imp;
3619 else
3620 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003621
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003622 // Create the imported variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00003623 VarDecl *ToVar;
3624 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003625 ToInnerLocStart, Loc,
3626 Name.getAsIdentifierInfo(),
3627 ToType, ToTypeSourceInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003628 D->getStorageClass()))
3629 return ToVar;
3630
Balazs Keri3b30d652018-10-19 13:32:20 +00003631 ToVar->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003632 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003633 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003634
Gabor Martonac3a5d62018-09-17 12:04:52 +00003635 if (FoundByLookup) {
3636 auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
3637 ToVar->setPreviousDecl(Recent);
3638 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003639
Balazs Keri3b30d652018-10-19 13:32:20 +00003640 if (Error Err = ImportInitializer(D, ToVar))
3641 return std::move(Err);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003642
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003643 if (D->isConstexpr())
3644 ToVar->setConstexpr(true);
3645
Gabor Martonac3a5d62018-09-17 12:04:52 +00003646 if (D->getDeclContext()->containsDeclAndLoad(D))
3647 DC->addDeclInternal(ToVar);
3648 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
3649 LexicalDC->addDeclInternal(ToVar);
3650
3651 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003652 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3653 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3654 if (!RedeclOrErr)
3655 return RedeclOrErr.takeError();
3656 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003657
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003658 return ToVar;
3659}
3660
Balazs Keri3b30d652018-10-19 13:32:20 +00003661ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
Douglas Gregor8b228d72010-02-17 21:22:52 +00003662 // Parameters are created in the translation unit's context, then moved
3663 // into the function declaration's context afterward.
3664 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003665
Balazs Keri3b30d652018-10-19 13:32:20 +00003666 DeclarationName ToDeclName;
3667 SourceLocation ToLocation;
3668 QualType ToType;
3669 if (auto Imp = importSeq(D->getDeclName(), D->getLocation(), D->getType()))
3670 std::tie(ToDeclName, ToLocation, ToType) = *Imp;
3671 else
3672 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003673
Douglas Gregor8b228d72010-02-17 21:22:52 +00003674 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003675 ImplicitParamDecl *ToParm = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003676 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3677 ToLocation, ToDeclName.getAsIdentifierInfo(),
3678 ToType, D->getParameterKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003679 return ToParm;
3680 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003681}
3682
Balazs Keri3b30d652018-10-19 13:32:20 +00003683ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003684 // Parameters are created in the translation unit's context, then moved
3685 // into the function declaration's context afterward.
3686 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003687
Balazs Keri3b30d652018-10-19 13:32:20 +00003688 DeclarationName ToDeclName;
3689 SourceLocation ToLocation, ToInnerLocStart;
3690 QualType ToType;
3691 TypeSourceInfo *ToTypeSourceInfo;
3692 if (auto Imp = importSeq(
3693 D->getDeclName(), D->getLocation(), D->getType(), D->getInnerLocStart(),
3694 D->getTypeSourceInfo()))
3695 std::tie(
3696 ToDeclName, ToLocation, ToType, ToInnerLocStart,
3697 ToTypeSourceInfo) = *Imp;
3698 else
3699 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003700
Gabor Marton26f72a92018-07-12 09:42:05 +00003701 ParmVarDecl *ToParm;
3702 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003703 ToInnerLocStart, ToLocation,
3704 ToDeclName.getAsIdentifierInfo(), ToType,
3705 ToTypeSourceInfo, D->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003706 /*DefaultArg*/ nullptr))
3707 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003708
3709 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003710 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003711 ToParm->setKNRPromoted(D->isKNRPromoted());
3712
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003713 if (D->hasUninstantiatedDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003714 if (auto ToDefArgOrErr = import(D->getUninstantiatedDefaultArg()))
3715 ToParm->setUninstantiatedDefaultArg(*ToDefArgOrErr);
3716 else
3717 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003718 } else if (D->hasUnparsedDefaultArg()) {
3719 ToParm->setUnparsedDefaultArg();
3720 } else if (D->hasDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003721 if (auto ToDefArgOrErr = import(D->getDefaultArg()))
3722 ToParm->setDefaultArg(*ToDefArgOrErr);
3723 else
3724 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003725 }
Sean Callanan59721b32015-04-28 18:41:46 +00003726
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003727 if (D->isObjCMethodParameter()) {
3728 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3729 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3730 } else {
3731 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3732 D->getFunctionScopeIndex());
3733 }
3734
Gabor Marton26f72a92018-07-12 09:42:05 +00003735 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003736}
3737
Balazs Keri3b30d652018-10-19 13:32:20 +00003738ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003739 // Import the major distinguishing characteristics of a method.
3740 DeclContext *DC, *LexicalDC;
3741 DeclarationName Name;
3742 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003743 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003744 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3745 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003746 if (ToD)
3747 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003748
Gabor Marton54058b52018-12-17 13:53:12 +00003749 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003750 for (auto *FoundDecl : FoundDecls) {
3751 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003752 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3753 continue;
3754
3755 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003756 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3757 FoundMethod->getReturnType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003758 Importer.ToDiag(Loc, diag::warn_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003759 << D->isInstanceMethod() << Name << D->getReturnType()
3760 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003761 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003762 diag::note_odr_objc_method_here)
3763 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003764
3765 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003766 }
3767
3768 // Check the number of parameters.
3769 if (D->param_size() != FoundMethod->param_size()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003770 Importer.ToDiag(Loc, diag::warn_odr_objc_method_num_params_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003771 << D->isInstanceMethod() << Name
3772 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00003773 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003774 diag::note_odr_objc_method_here)
3775 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003776
3777 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003778 }
3779
3780 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003781 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003782 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3783 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003784 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003785 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003786 Importer.FromDiag((*P)->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00003787 diag::warn_odr_objc_method_param_type_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003788 << D->isInstanceMethod() << Name
3789 << (*P)->getType() << (*FoundP)->getType();
3790 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3791 << (*FoundP)->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003792
3793 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003794 }
3795 }
3796
3797 // Check variadic/non-variadic.
3798 // Check the number of parameters.
3799 if (D->isVariadic() != FoundMethod->isVariadic()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003800 Importer.ToDiag(Loc, diag::warn_odr_objc_method_variadic_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003801 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00003802 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003803 diag::note_odr_objc_method_here)
3804 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003805
3806 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003807 }
3808
3809 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003810 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003811 }
3812 }
3813
Balazs Keri3b30d652018-10-19 13:32:20 +00003814 SourceLocation ToEndLoc;
3815 QualType ToReturnType;
3816 TypeSourceInfo *ToReturnTypeSourceInfo;
3817 if (auto Imp = importSeq(
3818 D->getEndLoc(), D->getReturnType(), D->getReturnTypeSourceInfo()))
3819 std::tie(ToEndLoc, ToReturnType, ToReturnTypeSourceInfo) = *Imp;
3820 else
3821 return Imp.takeError();
Douglas Gregor12852d92010-03-08 14:59:44 +00003822
Gabor Marton26f72a92018-07-12 09:42:05 +00003823 ObjCMethodDecl *ToMethod;
3824 if (GetImportedOrCreateDecl(
3825 ToMethod, D, Importer.getToContext(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00003826 ToEndLoc, Name.getObjCSelector(), ToReturnType,
3827 ToReturnTypeSourceInfo, DC, D->isInstanceMethod(), D->isVariadic(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003828 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3829 D->getImplementationControl(), D->hasRelatedResultType()))
3830 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003831
3832 // FIXME: When we decide to merge method definitions, we'll need to
3833 // deal with implicit parameters.
3834
3835 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003836 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003837 for (auto *FromP : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003838 if (Expected<ParmVarDecl *> ToPOrErr = import(FromP))
3839 ToParams.push_back(*ToPOrErr);
3840 else
3841 return ToPOrErr.takeError();
Douglas Gregor43f54792010-02-17 02:12:47 +00003842 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003843
Douglas Gregor43f54792010-02-17 02:12:47 +00003844 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003845 for (auto *ToParam : ToParams) {
3846 ToParam->setOwningFunction(ToMethod);
3847 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003848 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003849
Balazs Keri3b30d652018-10-19 13:32:20 +00003850 SmallVector<SourceLocation, 12> FromSelLocs;
3851 D->getSelectorLocs(FromSelLocs);
3852 SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size());
3853 if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs))
3854 return std::move(Err);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003855
Balazs Keri3b30d652018-10-19 13:32:20 +00003856 ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003857
3858 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003859 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003860 return ToMethod;
3861}
3862
Balazs Keri3b30d652018-10-19 13:32:20 +00003863ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00003864 // Import the major distinguishing characteristics of a category.
3865 DeclContext *DC, *LexicalDC;
3866 DeclarationName Name;
3867 SourceLocation Loc;
3868 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003869 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3870 return std::move(Err);
Douglas Gregor85f3f952015-07-07 03:57:15 +00003871 if (ToD)
3872 return ToD;
3873
Balazs Keri3b30d652018-10-19 13:32:20 +00003874 SourceLocation ToVarianceLoc, ToLocation, ToColonLoc;
3875 TypeSourceInfo *ToTypeSourceInfo;
3876 if (auto Imp = importSeq(
3877 D->getVarianceLoc(), D->getLocation(), D->getColonLoc(),
3878 D->getTypeSourceInfo()))
3879 std::tie(ToVarianceLoc, ToLocation, ToColonLoc, ToTypeSourceInfo) = *Imp;
3880 else
3881 return Imp.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00003882
Gabor Marton26f72a92018-07-12 09:42:05 +00003883 ObjCTypeParamDecl *Result;
3884 if (GetImportedOrCreateDecl(
3885 Result, D, Importer.getToContext(), DC, D->getVariance(),
Balazs Keri3b30d652018-10-19 13:32:20 +00003886 ToVarianceLoc, D->getIndex(),
3887 ToLocation, Name.getAsIdentifierInfo(),
3888 ToColonLoc, ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00003889 return Result;
3890
Douglas Gregor85f3f952015-07-07 03:57:15 +00003891 Result->setLexicalDeclContext(LexicalDC);
3892 return Result;
3893}
3894
Balazs Keri3b30d652018-10-19 13:32:20 +00003895ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00003896 // Import the major distinguishing characteristics of a category.
3897 DeclContext *DC, *LexicalDC;
3898 DeclarationName Name;
3899 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003900 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003901 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3902 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003903 if (ToD)
3904 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003905
Balazs Keri3b30d652018-10-19 13:32:20 +00003906 ObjCInterfaceDecl *ToInterface;
3907 if (Error Err = importInto(ToInterface, D->getClassInterface()))
3908 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00003909
Douglas Gregor84c51c32010-02-18 01:47:50 +00003910 // Determine if we've already encountered this category.
3911 ObjCCategoryDecl *MergeWithCategory
3912 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3913 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3914 if (!ToCategory) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003915 SourceLocation ToAtStartLoc, ToCategoryNameLoc;
3916 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
3917 if (auto Imp = importSeq(
3918 D->getAtStartLoc(), D->getCategoryNameLoc(),
3919 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
3920 std::tie(
3921 ToAtStartLoc, ToCategoryNameLoc,
3922 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
3923 else
3924 return Imp.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00003925
3926 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003927 ToAtStartLoc, Loc,
3928 ToCategoryNameLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00003929 Name.getAsIdentifierInfo(), ToInterface,
3930 /*TypeParamList=*/nullptr,
Balazs Keri3b30d652018-10-19 13:32:20 +00003931 ToIvarLBraceLoc,
3932 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00003933 return ToCategory;
3934
Douglas Gregor84c51c32010-02-18 01:47:50 +00003935 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003936 LexicalDC->addDeclInternal(ToCategory);
Balazs Keri3b30d652018-10-19 13:32:20 +00003937 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003938 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00003939 if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList()))
3940 ToCategory->setTypeParamList(*PListOrErr);
3941 else
3942 return PListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00003943
Douglas Gregor84c51c32010-02-18 01:47:50 +00003944 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003945 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3946 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003947 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3948 = D->protocol_loc_begin();
3949 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3950 FromProtoEnd = D->protocol_end();
3951 FromProto != FromProtoEnd;
3952 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003953 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
3954 Protocols.push_back(*ToProtoOrErr);
3955 else
3956 return ToProtoOrErr.takeError();
3957
3958 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
3959 ProtocolLocs.push_back(*ToProtoLocOrErr);
3960 else
3961 return ToProtoLocOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00003962 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003963
Douglas Gregor84c51c32010-02-18 01:47:50 +00003964 // FIXME: If we're merging, make sure that the protocol list is the same.
3965 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3966 ProtocolLocs.data(), Importer.getToContext());
Balazs Keri3b30d652018-10-19 13:32:20 +00003967
Douglas Gregor84c51c32010-02-18 01:47:50 +00003968 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003969 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003970 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003971
Douglas Gregor84c51c32010-02-18 01:47:50 +00003972 // Import all of the members of this category.
Balazs Keri3b30d652018-10-19 13:32:20 +00003973 if (Error Err = ImportDeclContext(D))
3974 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00003975
Douglas Gregor84c51c32010-02-18 01:47:50 +00003976 // If we have an implementation, import it as well.
3977 if (D->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003978 if (Expected<ObjCCategoryImplDecl *> ToImplOrErr =
3979 import(D->getImplementation()))
3980 ToCategory->setImplementation(*ToImplOrErr);
3981 else
3982 return ToImplOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00003983 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003984
Douglas Gregor84c51c32010-02-18 01:47:50 +00003985 return ToCategory;
3986}
3987
Balazs Keri3b30d652018-10-19 13:32:20 +00003988Error ASTNodeImporter::ImportDefinition(
3989 ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003990 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003991 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00003992 if (Error Err = ImportDeclContext(From))
3993 return Err;
3994 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00003995 }
3996
3997 // Start the protocol definition
3998 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00003999
Douglas Gregor2aa53772012-01-24 17:42:07 +00004000 // Import protocols
4001 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4002 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004003 ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc =
4004 From->protocol_loc_begin();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004005 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
4006 FromProtoEnd = From->protocol_end();
4007 FromProto != FromProtoEnd;
4008 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004009 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4010 Protocols.push_back(*ToProtoOrErr);
4011 else
4012 return ToProtoOrErr.takeError();
4013
4014 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4015 ProtocolLocs.push_back(*ToProtoLocOrErr);
4016 else
4017 return ToProtoLocOrErr.takeError();
4018
Douglas Gregor2aa53772012-01-24 17:42:07 +00004019 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004020
Douglas Gregor2aa53772012-01-24 17:42:07 +00004021 // FIXME: If we're merging, make sure that the protocol list is the same.
4022 To->setProtocolList(Protocols.data(), Protocols.size(),
4023 ProtocolLocs.data(), Importer.getToContext());
4024
Douglas Gregor2e15c842012-02-01 21:00:38 +00004025 if (shouldForceImportDeclContext(Kind)) {
4026 // Import all of the members of this protocol.
Balazs Keri3b30d652018-10-19 13:32:20 +00004027 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4028 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004029 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004030 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004031}
4032
Balazs Keri3b30d652018-10-19 13:32:20 +00004033ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004034 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00004035 // from, but this particular declaration is not that definition, import the
4036 // definition and map to that.
4037 ObjCProtocolDecl *Definition = D->getDefinition();
4038 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004039 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4040 return Importer.MapImported(D, *ImportedDefOrErr);
4041 else
4042 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004043 }
4044
Douglas Gregor84c51c32010-02-18 01:47:50 +00004045 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004046 DeclContext *DC, *LexicalDC;
4047 DeclarationName Name;
4048 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004049 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004050 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4051 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004052 if (ToD)
4053 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004054
Craig Topper36250ad2014-05-12 05:36:57 +00004055 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004056 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004057 for (auto *FoundDecl : FoundDecls) {
4058 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004059 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004060
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004061 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004062 break;
4063 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004064
Douglas Gregor98d156a2010-02-17 16:12:00 +00004065 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004066 if (!ToProto) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004067 auto ToAtBeginLocOrErr = import(D->getAtStartLoc());
4068 if (!ToAtBeginLocOrErr)
4069 return ToAtBeginLocOrErr.takeError();
4070
Gabor Marton26f72a92018-07-12 09:42:05 +00004071 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
4072 Name.getAsIdentifierInfo(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004073 *ToAtBeginLocOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004074 /*PrevDecl=*/nullptr))
4075 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004076 ToProto->setLexicalDeclContext(LexicalDC);
4077 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004078 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004079
4080 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004081
Balazs Keri3b30d652018-10-19 13:32:20 +00004082 if (D->isThisDeclarationADefinition())
4083 if (Error Err = ImportDefinition(D, ToProto))
4084 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004085
Douglas Gregor98d156a2010-02-17 16:12:00 +00004086 return ToProto;
4087}
4088
Balazs Keri3b30d652018-10-19 13:32:20 +00004089ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4090 DeclContext *DC, *LexicalDC;
4091 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4092 return std::move(Err);
Sean Callanan0aae0412014-12-10 00:00:37 +00004093
Balazs Keri3b30d652018-10-19 13:32:20 +00004094 ExpectedSLoc ExternLocOrErr = import(D->getExternLoc());
4095 if (!ExternLocOrErr)
4096 return ExternLocOrErr.takeError();
4097
4098 ExpectedSLoc LangLocOrErr = import(D->getLocation());
4099 if (!LangLocOrErr)
4100 return LangLocOrErr.takeError();
Sean Callanan0aae0412014-12-10 00:00:37 +00004101
4102 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00004103
4104 LinkageSpecDecl *ToLinkageSpec;
4105 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004106 *ExternLocOrErr, *LangLocOrErr,
4107 D->getLanguage(), HasBraces))
Gabor Marton26f72a92018-07-12 09:42:05 +00004108 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00004109
4110 if (HasBraces) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004111 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
4112 if (!RBraceLocOrErr)
4113 return RBraceLocOrErr.takeError();
4114 ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr);
Sean Callanan0aae0412014-12-10 00:00:37 +00004115 }
4116
4117 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4118 LexicalDC->addDeclInternal(ToLinkageSpec);
4119
Sean Callanan0aae0412014-12-10 00:00:37 +00004120 return ToLinkageSpec;
4121}
4122
Balazs Keri3b30d652018-10-19 13:32:20 +00004123ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004124 DeclContext *DC, *LexicalDC;
4125 DeclarationName Name;
4126 SourceLocation Loc;
4127 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004128 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4129 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004130 if (ToD)
4131 return ToD;
4132
Balazs Keri3b30d652018-10-19 13:32:20 +00004133 SourceLocation ToLoc, ToUsingLoc;
4134 NestedNameSpecifierLoc ToQualifierLoc;
4135 if (auto Imp = importSeq(
4136 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc()))
4137 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc) = *Imp;
4138 else
4139 return Imp.takeError();
4140
4141 DeclarationNameInfo NameInfo(Name, ToLoc);
4142 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4143 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004144
Gabor Marton26f72a92018-07-12 09:42:05 +00004145 UsingDecl *ToUsing;
4146 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004147 ToUsingLoc, ToQualifierLoc, NameInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00004148 D->hasTypename()))
4149 return ToUsing;
4150
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004151 ToUsing->setLexicalDeclContext(LexicalDC);
4152 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004153
4154 if (NamedDecl *FromPattern =
4155 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004156 if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern))
4157 Importer.getToContext().setInstantiatedFromUsingDecl(
4158 ToUsing, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004159 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004160 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004161 }
4162
Balazs Keri3b30d652018-10-19 13:32:20 +00004163 for (UsingShadowDecl *FromShadow : D->shadows()) {
4164 if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow))
4165 ToUsing->addShadowDecl(*ToShadowOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004166 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004167 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004168 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004169 return ToShadowOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004170 }
4171 return ToUsing;
4172}
4173
Balazs Keri3b30d652018-10-19 13:32:20 +00004174ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004175 DeclContext *DC, *LexicalDC;
4176 DeclarationName Name;
4177 SourceLocation Loc;
4178 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004179 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4180 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004181 if (ToD)
4182 return ToD;
4183
Balazs Keri3b30d652018-10-19 13:32:20 +00004184 Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl());
4185 if (!ToUsingOrErr)
4186 return ToUsingOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004187
Balazs Keri3b30d652018-10-19 13:32:20 +00004188 Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl());
4189 if (!ToTargetOrErr)
4190 return ToTargetOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004191
Gabor Marton26f72a92018-07-12 09:42:05 +00004192 UsingShadowDecl *ToShadow;
4193 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004194 *ToUsingOrErr, *ToTargetOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004195 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004196
4197 ToShadow->setLexicalDeclContext(LexicalDC);
4198 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004199
4200 if (UsingShadowDecl *FromPattern =
4201 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004202 if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern))
4203 Importer.getToContext().setInstantiatedFromUsingShadowDecl(
4204 ToShadow, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004205 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004206 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004207 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004208 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004209 }
4210
4211 LexicalDC->addDeclInternal(ToShadow);
4212
4213 return ToShadow;
4214}
4215
Balazs Keri3b30d652018-10-19 13:32:20 +00004216ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004217 DeclContext *DC, *LexicalDC;
4218 DeclarationName Name;
4219 SourceLocation Loc;
4220 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004221 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4222 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004223 if (ToD)
4224 return ToD;
4225
Balazs Keri3b30d652018-10-19 13:32:20 +00004226 auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor());
4227 if (!ToComAncestorOrErr)
4228 return ToComAncestorOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004229
Balazs Keri3b30d652018-10-19 13:32:20 +00004230 NamespaceDecl *ToNominatedNamespace;
4231 SourceLocation ToUsingLoc, ToNamespaceKeyLocation, ToIdentLocation;
4232 NestedNameSpecifierLoc ToQualifierLoc;
4233 if (auto Imp = importSeq(
4234 D->getNominatedNamespace(), D->getUsingLoc(),
4235 D->getNamespaceKeyLocation(), D->getQualifierLoc(),
4236 D->getIdentLocation()))
4237 std::tie(
4238 ToNominatedNamespace, ToUsingLoc, ToNamespaceKeyLocation,
4239 ToQualifierLoc, ToIdentLocation) = *Imp;
4240 else
4241 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004242
Gabor Marton26f72a92018-07-12 09:42:05 +00004243 UsingDirectiveDecl *ToUsingDir;
4244 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004245 ToUsingLoc,
4246 ToNamespaceKeyLocation,
4247 ToQualifierLoc,
4248 ToIdentLocation,
4249 ToNominatedNamespace, *ToComAncestorOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004250 return ToUsingDir;
4251
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004252 ToUsingDir->setLexicalDeclContext(LexicalDC);
4253 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004254
4255 return ToUsingDir;
4256}
4257
Balazs Keri3b30d652018-10-19 13:32:20 +00004258ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004259 UnresolvedUsingValueDecl *D) {
4260 DeclContext *DC, *LexicalDC;
4261 DeclarationName Name;
4262 SourceLocation Loc;
4263 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004264 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4265 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004266 if (ToD)
4267 return ToD;
4268
Balazs Keri3b30d652018-10-19 13:32:20 +00004269 SourceLocation ToLoc, ToUsingLoc, ToEllipsisLoc;
4270 NestedNameSpecifierLoc ToQualifierLoc;
4271 if (auto Imp = importSeq(
4272 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc(),
4273 D->getEllipsisLoc()))
4274 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4275 else
4276 return Imp.takeError();
4277
4278 DeclarationNameInfo NameInfo(Name, ToLoc);
4279 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4280 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004281
Gabor Marton26f72a92018-07-12 09:42:05 +00004282 UnresolvedUsingValueDecl *ToUsingValue;
4283 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004284 ToUsingLoc, ToQualifierLoc, NameInfo,
4285 ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004286 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004287
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004288 ToUsingValue->setAccess(D->getAccess());
4289 ToUsingValue->setLexicalDeclContext(LexicalDC);
4290 LexicalDC->addDeclInternal(ToUsingValue);
4291
4292 return ToUsingValue;
4293}
4294
Balazs Keri3b30d652018-10-19 13:32:20 +00004295ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004296 UnresolvedUsingTypenameDecl *D) {
4297 DeclContext *DC, *LexicalDC;
4298 DeclarationName Name;
4299 SourceLocation Loc;
4300 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004301 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4302 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004303 if (ToD)
4304 return ToD;
4305
Balazs Keri3b30d652018-10-19 13:32:20 +00004306 SourceLocation ToUsingLoc, ToTypenameLoc, ToEllipsisLoc;
4307 NestedNameSpecifierLoc ToQualifierLoc;
4308 if (auto Imp = importSeq(
4309 D->getUsingLoc(), D->getTypenameLoc(), D->getQualifierLoc(),
4310 D->getEllipsisLoc()))
4311 std::tie(ToUsingLoc, ToTypenameLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4312 else
4313 return Imp.takeError();
4314
Gabor Marton26f72a92018-07-12 09:42:05 +00004315 UnresolvedUsingTypenameDecl *ToUsing;
4316 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004317 ToUsingLoc, ToTypenameLoc,
4318 ToQualifierLoc, Loc, Name, ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004319 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004320
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004321 ToUsing->setAccess(D->getAccess());
4322 ToUsing->setLexicalDeclContext(LexicalDC);
4323 LexicalDC->addDeclInternal(ToUsing);
4324
4325 return ToUsing;
4326}
4327
Balazs Keri3b30d652018-10-19 13:32:20 +00004328
4329Error ASTNodeImporter::ImportDefinition(
4330 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004331 if (To->getDefinition()) {
4332 // Check consistency of superclass.
4333 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4334 if (FromSuper) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004335 if (auto FromSuperOrErr = import(FromSuper))
4336 FromSuper = *FromSuperOrErr;
4337 else
4338 return FromSuperOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004339 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004340
4341 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004342 if ((bool)FromSuper != (bool)ToSuper ||
4343 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004344 Importer.ToDiag(To->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004345 diag::warn_odr_objc_superclass_inconsistent)
Douglas Gregor2aa53772012-01-24 17:42:07 +00004346 << To->getDeclName();
4347 if (ToSuper)
4348 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4349 << To->getSuperClass()->getDeclName();
4350 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004351 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004352 diag::note_odr_objc_missing_superclass);
4353 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00004354 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004355 diag::note_odr_objc_superclass)
4356 << From->getSuperClass()->getDeclName();
4357 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004358 Importer.FromDiag(From->getLocation(),
4359 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004360 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004361
Douglas Gregor2e15c842012-02-01 21:00:38 +00004362 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004363 if (Error Err = ImportDeclContext(From))
4364 return Err;
4365 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004366 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004367
Douglas Gregor2aa53772012-01-24 17:42:07 +00004368 // Start the definition.
4369 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004370
Douglas Gregor2aa53772012-01-24 17:42:07 +00004371 // If this class has a superclass, import it.
4372 if (From->getSuperClass()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004373 if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo()))
4374 To->setSuperClass(*SuperTInfoOrErr);
4375 else
4376 return SuperTInfoOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004377 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004378
Douglas Gregor2aa53772012-01-24 17:42:07 +00004379 // Import protocols
4380 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4381 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004382 ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc =
4383 From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00004384
Douglas Gregor2aa53772012-01-24 17:42:07 +00004385 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4386 FromProtoEnd = From->protocol_end();
4387 FromProto != FromProtoEnd;
4388 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004389 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4390 Protocols.push_back(*ToProtoOrErr);
4391 else
4392 return ToProtoOrErr.takeError();
4393
4394 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4395 ProtocolLocs.push_back(*ToProtoLocOrErr);
4396 else
4397 return ToProtoLocOrErr.takeError();
4398
Douglas Gregor2aa53772012-01-24 17:42:07 +00004399 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004400
Douglas Gregor2aa53772012-01-24 17:42:07 +00004401 // FIXME: If we're merging, make sure that the protocol list is the same.
4402 To->setProtocolList(Protocols.data(), Protocols.size(),
4403 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00004404
Douglas Gregor2aa53772012-01-24 17:42:07 +00004405 // Import categories. When the categories themselves are imported, they'll
4406 // hook themselves into this interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004407 for (auto *Cat : From->known_categories()) {
4408 auto ToCatOrErr = import(Cat);
4409 if (!ToCatOrErr)
4410 return ToCatOrErr.takeError();
4411 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004412
Douglas Gregor2aa53772012-01-24 17:42:07 +00004413 // If we have an @implementation, import it as well.
4414 if (From->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004415 if (Expected<ObjCImplementationDecl *> ToImplOrErr =
4416 import(From->getImplementation()))
4417 To->setImplementation(*ToImplOrErr);
4418 else
4419 return ToImplOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004420 }
4421
Douglas Gregor2e15c842012-02-01 21:00:38 +00004422 if (shouldForceImportDeclContext(Kind)) {
4423 // Import all of the members of this class.
Balazs Keri3b30d652018-10-19 13:32:20 +00004424 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4425 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004426 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004427 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004428}
4429
Balazs Keri3b30d652018-10-19 13:32:20 +00004430Expected<ObjCTypeParamList *>
Douglas Gregor85f3f952015-07-07 03:57:15 +00004431ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4432 if (!list)
4433 return nullptr;
4434
4435 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
Balazs Keri3b30d652018-10-19 13:32:20 +00004436 for (auto *fromTypeParam : *list) {
4437 if (auto toTypeParamOrErr = import(fromTypeParam))
4438 toTypeParams.push_back(*toTypeParamOrErr);
4439 else
4440 return toTypeParamOrErr.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004441 }
4442
Balazs Keri3b30d652018-10-19 13:32:20 +00004443 auto LAngleLocOrErr = import(list->getLAngleLoc());
4444 if (!LAngleLocOrErr)
4445 return LAngleLocOrErr.takeError();
4446
4447 auto RAngleLocOrErr = import(list->getRAngleLoc());
4448 if (!RAngleLocOrErr)
4449 return RAngleLocOrErr.takeError();
4450
Douglas Gregor85f3f952015-07-07 03:57:15 +00004451 return ObjCTypeParamList::create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004452 *LAngleLocOrErr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00004453 toTypeParams,
Balazs Keri3b30d652018-10-19 13:32:20 +00004454 *RAngleLocOrErr);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004455}
4456
Balazs Keri3b30d652018-10-19 13:32:20 +00004457ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004458 // If this class has a definition in the translation unit we're coming from,
4459 // but this particular declaration is not that definition, import the
4460 // definition and map to that.
4461 ObjCInterfaceDecl *Definition = D->getDefinition();
4462 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004463 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4464 return Importer.MapImported(D, *ImportedDefOrErr);
4465 else
4466 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004467 }
4468
Douglas Gregor45635322010-02-16 01:20:57 +00004469 // Import the major distinguishing characteristics of an @interface.
4470 DeclContext *DC, *LexicalDC;
4471 DeclarationName Name;
4472 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004473 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004474 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4475 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004476 if (ToD)
4477 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004478
Douglas Gregor2aa53772012-01-24 17:42:07 +00004479 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004480 ObjCInterfaceDecl *MergeWithIface = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004481 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004482 for (auto *FoundDecl : FoundDecls) {
4483 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004484 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004485
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004486 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004487 break;
4488 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004489
Douglas Gregor2aa53772012-01-24 17:42:07 +00004490 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004491 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004492 if (!ToIface) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004493 ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc());
4494 if (!AtBeginLocOrErr)
4495 return AtBeginLocOrErr.takeError();
4496
Gabor Marton26f72a92018-07-12 09:42:05 +00004497 if (GetImportedOrCreateDecl(
4498 ToIface, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004499 *AtBeginLocOrErr, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004500 /*TypeParamList=*/nullptr,
4501 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4502 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004503 ToIface->setLexicalDeclContext(LexicalDC);
4504 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004505 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004506 Importer.MapImported(D, ToIface);
Balazs Keri3b30d652018-10-19 13:32:20 +00004507 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004508 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004509 if (auto ToPListOrErr =
4510 ImportObjCTypeParamList(D->getTypeParamListAsWritten()))
4511 ToIface->setTypeParamList(*ToPListOrErr);
4512 else
4513 return ToPListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004514
Balazs Keri3b30d652018-10-19 13:32:20 +00004515 if (D->isThisDeclarationADefinition())
4516 if (Error Err = ImportDefinition(D, ToIface))
4517 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004518
Douglas Gregor98d156a2010-02-17 16:12:00 +00004519 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004520}
4521
Balazs Keri3b30d652018-10-19 13:32:20 +00004522ExpectedDecl
4523ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4524 ObjCCategoryDecl *Category;
4525 if (Error Err = importInto(Category, D->getCategoryDecl()))
4526 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004527
Douglas Gregor4da9d682010-12-07 15:32:12 +00004528 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4529 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004530 DeclContext *DC, *LexicalDC;
4531 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4532 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004533
Balazs Keri3b30d652018-10-19 13:32:20 +00004534 SourceLocation ToLocation, ToAtStartLoc, ToCategoryNameLoc;
4535 if (auto Imp = importSeq(
4536 D->getLocation(), D->getAtStartLoc(), D->getCategoryNameLoc()))
4537 std::tie(ToLocation, ToAtStartLoc, ToCategoryNameLoc) = *Imp;
4538 else
4539 return Imp.takeError();
4540
Gabor Marton26f72a92018-07-12 09:42:05 +00004541 if (GetImportedOrCreateDecl(
4542 ToImpl, D, Importer.getToContext(), DC,
4543 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004544 ToLocation, ToAtStartLoc, ToCategoryNameLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004545 return ToImpl;
4546
Balazs Keri3b30d652018-10-19 13:32:20 +00004547 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004548 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004549 Category->setImplementation(ToImpl);
4550 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004551
Gabor Marton26f72a92018-07-12 09:42:05 +00004552 Importer.MapImported(D, ToImpl);
Balazs Keri3b30d652018-10-19 13:32:20 +00004553 if (Error Err = ImportDeclContext(D))
4554 return std::move(Err);
4555
Douglas Gregor4da9d682010-12-07 15:32:12 +00004556 return ToImpl;
4557}
4558
Balazs Keri3b30d652018-10-19 13:32:20 +00004559ExpectedDecl
4560ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00004561 // Find the corresponding interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004562 ObjCInterfaceDecl *Iface;
4563 if (Error Err = importInto(Iface, D->getClassInterface()))
4564 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004565
4566 // Import the superclass, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00004567 ObjCInterfaceDecl *Super;
4568 if (Error Err = importInto(Super, D->getSuperClass()))
4569 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004570
4571 ObjCImplementationDecl *Impl = Iface->getImplementation();
4572 if (!Impl) {
4573 // We haven't imported an implementation yet. Create a new @implementation
4574 // now.
Balazs Keri3b30d652018-10-19 13:32:20 +00004575 DeclContext *DC, *LexicalDC;
4576 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4577 return std::move(Err);
4578
4579 SourceLocation ToLocation, ToAtStartLoc, ToSuperClassLoc;
4580 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4581 if (auto Imp = importSeq(
4582 D->getLocation(), D->getAtStartLoc(), D->getSuperClassLoc(),
4583 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4584 std::tie(
4585 ToLocation, ToAtStartLoc, ToSuperClassLoc,
4586 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4587 else
4588 return Imp.takeError();
4589
Gabor Marton26f72a92018-07-12 09:42:05 +00004590 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004591 DC, Iface, Super,
4592 ToLocation,
4593 ToAtStartLoc,
4594 ToSuperClassLoc,
4595 ToIvarLBraceLoc,
4596 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004597 return Impl;
4598
Balazs Keri3b30d652018-10-19 13:32:20 +00004599 Impl->setLexicalDeclContext(LexicalDC);
Gabor Marton26f72a92018-07-12 09:42:05 +00004600
Douglas Gregorda8025c2010-12-07 01:26:03 +00004601 // Associate the implementation with the class it implements.
4602 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004603 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004604 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004605 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004606
4607 // Verify that the existing @implementation has the same superclass.
4608 if ((Super && !Impl->getSuperClass()) ||
4609 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004610 (Super && Impl->getSuperClass() &&
4611 !declaresSameEntity(Super->getCanonicalDecl(),
4612 Impl->getSuperClass()))) {
4613 Importer.ToDiag(Impl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004614 diag::warn_odr_objc_superclass_inconsistent)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004615 << Iface->getDeclName();
4616 // FIXME: It would be nice to have the location of the superclass
4617 // below.
4618 if (Impl->getSuperClass())
4619 Importer.ToDiag(Impl->getLocation(),
4620 diag::note_odr_objc_superclass)
4621 << Impl->getSuperClass()->getDeclName();
4622 else
4623 Importer.ToDiag(Impl->getLocation(),
4624 diag::note_odr_objc_missing_superclass);
4625 if (D->getSuperClass())
4626 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004627 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004628 << D->getSuperClass()->getDeclName();
4629 else
4630 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004631 diag::note_odr_objc_missing_superclass);
Balazs Keri3b30d652018-10-19 13:32:20 +00004632
4633 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004634 }
4635 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004636
Douglas Gregorda8025c2010-12-07 01:26:03 +00004637 // Import all of the members of this @implementation.
Balazs Keri3b30d652018-10-19 13:32:20 +00004638 if (Error Err = ImportDeclContext(D))
4639 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004640
4641 return Impl;
4642}
4643
Balazs Keri3b30d652018-10-19 13:32:20 +00004644ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004645 // Import the major distinguishing characteristics of an @property.
4646 DeclContext *DC, *LexicalDC;
4647 DeclarationName Name;
4648 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004649 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004650 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4651 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004652 if (ToD)
4653 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004654
4655 // Check whether we have already imported this property.
Gabor Marton54058b52018-12-17 13:53:12 +00004656 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004657 for (auto *FoundDecl : FoundDecls) {
4658 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004659 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004660 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004661 FoundProp->getType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00004662 Importer.ToDiag(Loc, diag::warn_odr_objc_property_type_inconsistent)
Douglas Gregora11c4582010-02-17 18:02:10 +00004663 << Name << D->getType() << FoundProp->getType();
4664 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4665 << FoundProp->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004666
4667 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora11c4582010-02-17 18:02:10 +00004668 }
4669
4670 // FIXME: Check property attributes, getters, setters, etc.?
4671
4672 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004673 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004674 return FoundProp;
4675 }
4676 }
4677
Balazs Keri3b30d652018-10-19 13:32:20 +00004678 QualType ToType;
4679 TypeSourceInfo *ToTypeSourceInfo;
4680 SourceLocation ToAtLoc, ToLParenLoc;
4681 if (auto Imp = importSeq(
4682 D->getType(), D->getTypeSourceInfo(), D->getAtLoc(), D->getLParenLoc()))
4683 std::tie(ToType, ToTypeSourceInfo, ToAtLoc, ToLParenLoc) = *Imp;
4684 else
4685 return Imp.takeError();
Douglas Gregora11c4582010-02-17 18:02:10 +00004686
4687 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004688 ObjCPropertyDecl *ToProperty;
4689 if (GetImportedOrCreateDecl(
4690 ToProperty, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004691 Name.getAsIdentifierInfo(), ToAtLoc,
4692 ToLParenLoc, ToType,
4693 ToTypeSourceInfo, D->getPropertyImplementation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00004694 return ToProperty;
4695
Balazs Keri3b30d652018-10-19 13:32:20 +00004696 Selector ToGetterName, ToSetterName;
4697 SourceLocation ToGetterNameLoc, ToSetterNameLoc;
4698 ObjCMethodDecl *ToGetterMethodDecl, *ToSetterMethodDecl;
4699 ObjCIvarDecl *ToPropertyIvarDecl;
4700 if (auto Imp = importSeq(
4701 D->getGetterName(), D->getSetterName(),
4702 D->getGetterNameLoc(), D->getSetterNameLoc(),
4703 D->getGetterMethodDecl(), D->getSetterMethodDecl(),
4704 D->getPropertyIvarDecl()))
4705 std::tie(
4706 ToGetterName, ToSetterName,
4707 ToGetterNameLoc, ToSetterNameLoc,
4708 ToGetterMethodDecl, ToSetterMethodDecl,
4709 ToPropertyIvarDecl) = *Imp;
4710 else
4711 return Imp.takeError();
4712
Douglas Gregora11c4582010-02-17 18:02:10 +00004713 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004714 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004715
4716 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004717 ToProperty->setPropertyAttributesAsWritten(
4718 D->getPropertyAttributesAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00004719 ToProperty->setGetterName(ToGetterName, ToGetterNameLoc);
4720 ToProperty->setSetterName(ToSetterName, ToSetterNameLoc);
4721 ToProperty->setGetterMethodDecl(ToGetterMethodDecl);
4722 ToProperty->setSetterMethodDecl(ToSetterMethodDecl);
4723 ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl);
Douglas Gregora11c4582010-02-17 18:02:10 +00004724 return ToProperty;
4725}
4726
Balazs Keri3b30d652018-10-19 13:32:20 +00004727ExpectedDecl
4728ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4729 ObjCPropertyDecl *Property;
4730 if (Error Err = importInto(Property, D->getPropertyDecl()))
4731 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004732
Balazs Keri3b30d652018-10-19 13:32:20 +00004733 DeclContext *DC, *LexicalDC;
4734 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4735 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004736
Balazs Keri3b30d652018-10-19 13:32:20 +00004737 auto *InImpl = cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004738
4739 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004740 ObjCIvarDecl *Ivar = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004741 if (Error Err = importInto(Ivar, D->getPropertyIvarDecl()))
4742 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004743
4744 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004745 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4746 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004747 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004748 SourceLocation ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc;
4749 if (auto Imp = importSeq(
4750 D->getBeginLoc(), D->getLocation(), D->getPropertyIvarDeclLoc()))
4751 std::tie(ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc) = *Imp;
4752 else
4753 return Imp.takeError();
4754
Gabor Marton26f72a92018-07-12 09:42:05 +00004755 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004756 ToBeginLoc,
4757 ToLocation, Property,
Gabor Marton26f72a92018-07-12 09:42:05 +00004758 D->getPropertyImplementation(), Ivar,
Balazs Keri3b30d652018-10-19 13:32:20 +00004759 ToPropertyIvarDeclLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004760 return ToImpl;
4761
Douglas Gregor14a49e22010-12-07 18:32:03 +00004762 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004763 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004764 } else {
4765 // Check that we have the same kind of property implementation (@synthesize
4766 // vs. @dynamic).
4767 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004768 Importer.ToDiag(ToImpl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004769 diag::warn_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00004770 << Property->getDeclName()
4771 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00004772 == ObjCPropertyImplDecl::Dynamic);
4773 Importer.FromDiag(D->getLocation(),
4774 diag::note_odr_objc_property_impl_kind)
4775 << D->getPropertyDecl()->getDeclName()
4776 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Balazs Keri3b30d652018-10-19 13:32:20 +00004777
4778 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004779 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004780
4781 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00004782 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4783 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004784 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004785 diag::warn_odr_objc_synthesize_ivar_inconsistent)
Douglas Gregor14a49e22010-12-07 18:32:03 +00004786 << Property->getDeclName()
4787 << ToImpl->getPropertyIvarDecl()->getDeclName()
4788 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00004789 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004790 diag::note_odr_objc_synthesize_ivar_here)
4791 << D->getPropertyIvarDecl()->getDeclName();
Balazs Keri3b30d652018-10-19 13:32:20 +00004792
4793 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004794 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004795
Douglas Gregor14a49e22010-12-07 18:32:03 +00004796 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004797 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004798 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004799
Douglas Gregor14a49e22010-12-07 18:32:03 +00004800 return ToImpl;
4801}
4802
Balazs Keri3b30d652018-10-19 13:32:20 +00004803ExpectedDecl
4804ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregora082a492010-11-30 19:14:50 +00004805 // For template arguments, we adopt the translation unit as our declaration
4806 // context. This context will be fixed when the actual template declaration
4807 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00004808
Douglas Gregora082a492010-11-30 19:14:50 +00004809 // FIXME: Import default argument.
Balazs Keri3b30d652018-10-19 13:32:20 +00004810
4811 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
4812 if (!BeginLocOrErr)
4813 return BeginLocOrErr.takeError();
4814
4815 ExpectedSLoc LocationOrErr = import(D->getLocation());
4816 if (!LocationOrErr)
4817 return LocationOrErr.takeError();
4818
Gabor Marton26f72a92018-07-12 09:42:05 +00004819 TemplateTypeParmDecl *ToD = nullptr;
4820 (void)GetImportedOrCreateDecl(
4821 ToD, D, Importer.getToContext(),
4822 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004823 *BeginLocOrErr, *LocationOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004824 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4825 D->wasDeclaredWithTypename(), D->isParameterPack());
4826 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004827}
4828
Balazs Keri3b30d652018-10-19 13:32:20 +00004829ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004830ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004831 DeclarationName ToDeclName;
4832 SourceLocation ToLocation, ToInnerLocStart;
4833 QualType ToType;
4834 TypeSourceInfo *ToTypeSourceInfo;
4835 if (auto Imp = importSeq(
4836 D->getDeclName(), D->getLocation(), D->getType(), D->getTypeSourceInfo(),
4837 D->getInnerLocStart()))
4838 std::tie(
4839 ToDeclName, ToLocation, ToType, ToTypeSourceInfo,
4840 ToInnerLocStart) = *Imp;
4841 else
4842 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004843
Douglas Gregora082a492010-11-30 19:14:50 +00004844 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004845
4846 NonTypeTemplateParmDecl *ToD = nullptr;
4847 (void)GetImportedOrCreateDecl(
4848 ToD, D, Importer.getToContext(),
4849 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004850 ToInnerLocStart, ToLocation, D->getDepth(),
4851 D->getPosition(), ToDeclName.getAsIdentifierInfo(), ToType,
4852 D->isParameterPack(), ToTypeSourceInfo);
Gabor Marton26f72a92018-07-12 09:42:05 +00004853 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004854}
4855
Balazs Keri3b30d652018-10-19 13:32:20 +00004856ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004857ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4858 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004859 auto NameOrErr = import(D->getDeclName());
4860 if (!NameOrErr)
4861 return NameOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004862
Douglas Gregora082a492010-11-30 19:14:50 +00004863 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004864 ExpectedSLoc LocationOrErr = import(D->getLocation());
4865 if (!LocationOrErr)
4866 return LocationOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004867
Douglas Gregora082a492010-11-30 19:14:50 +00004868 // Import template parameters.
Balazs Keridec09162019-03-20 15:42:42 +00004869 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00004870 if (!TemplateParamsOrErr)
4871 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004872
Douglas Gregora082a492010-11-30 19:14:50 +00004873 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004874
4875 TemplateTemplateParmDecl *ToD = nullptr;
4876 (void)GetImportedOrCreateDecl(
4877 ToD, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004878 Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr,
4879 D->getDepth(), D->getPosition(), D->isParameterPack(),
4880 (*NameOrErr).getAsIdentifierInfo(),
4881 *TemplateParamsOrErr);
Gabor Marton26f72a92018-07-12 09:42:05 +00004882 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004883}
4884
Gabor Marton16d98c22019-03-07 13:01:51 +00004885// Returns the definition for a (forward) declaration of a TemplateDecl, if
Gabor Marton9581c332018-05-23 13:53:36 +00004886// it has any definition in the redecl chain.
Gabor Marton16d98c22019-03-07 13:01:51 +00004887template <typename T> static auto getTemplateDefinition(T *D) -> T * {
4888 assert(D->getTemplatedDecl() && "Should be called on templates only");
4889 auto *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
Gabor Marton9581c332018-05-23 13:53:36 +00004890 if (!ToTemplatedDef)
4891 return nullptr;
Gabor Marton16d98c22019-03-07 13:01:51 +00004892 auto *TemplateWithDef = ToTemplatedDef->getDescribedTemplate();
4893 return cast_or_null<T>(TemplateWithDef);
Gabor Marton9581c332018-05-23 13:53:36 +00004894}
4895
Balazs Keri3b30d652018-10-19 13:32:20 +00004896ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00004897 bool IsFriend = D->getFriendObjectKind() != Decl::FOK_None;
4898
Douglas Gregora082a492010-11-30 19:14:50 +00004899 // Import the major distinguishing characteristics of this class template.
4900 DeclContext *DC, *LexicalDC;
4901 DeclarationName Name;
4902 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004903 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004904 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4905 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004906 if (ToD)
4907 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004908
Gabor Marton7df342a2018-12-17 12:42:12 +00004909 ClassTemplateDecl *FoundByLookup = nullptr;
4910
Douglas Gregora082a492010-11-30 19:14:50 +00004911 // We may already have a template of the same name; try to find and match it.
4912 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004913 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00004914 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004915 for (auto *FoundDecl : FoundDecls) {
Gabor Marton7df342a2018-12-17 12:42:12 +00004916 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary |
4917 Decl::IDNS_TagFriend))
Douglas Gregora082a492010-11-30 19:14:50 +00004918 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00004919
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004920 Decl *Found = FoundDecl;
Gabor Marton7df342a2018-12-17 12:42:12 +00004921 auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found);
4922 if (FoundTemplate) {
Gabor Marton9581c332018-05-23 13:53:36 +00004923
Douglas Gregora082a492010-11-30 19:14:50 +00004924 if (IsStructuralMatch(D, FoundTemplate)) {
Gabor Marton16d98c22019-03-07 13:01:51 +00004925 ClassTemplateDecl *TemplateWithDef =
4926 getTemplateDefinition(FoundTemplate);
Gabor Marton7df342a2018-12-17 12:42:12 +00004927 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
4928 return Importer.MapImported(D, TemplateWithDef);
Balazs Keri0c23dc52018-08-13 13:08:37 +00004929 }
Gabor Marton7df342a2018-12-17 12:42:12 +00004930 FoundByLookup = FoundTemplate;
4931 break;
Gabor Marton9581c332018-05-23 13:53:36 +00004932 }
Douglas Gregora082a492010-11-30 19:14:50 +00004933 }
Gabor Marton9581c332018-05-23 13:53:36 +00004934
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004935 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00004936 }
Gabor Marton9581c332018-05-23 13:53:36 +00004937
Douglas Gregora082a492010-11-30 19:14:50 +00004938 if (!ConflictingDecls.empty()) {
4939 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00004940 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00004941 ConflictingDecls.size());
4942 }
Gabor Marton9581c332018-05-23 13:53:36 +00004943
Douglas Gregora082a492010-11-30 19:14:50 +00004944 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00004945 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora082a492010-11-30 19:14:50 +00004946 }
4947
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004948 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4949
Douglas Gregora082a492010-11-30 19:14:50 +00004950 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00004951 CXXRecordDecl *ToTemplated;
4952 if (Error Err = importInto(ToTemplated, FromTemplated))
4953 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004954
Douglas Gregora082a492010-11-30 19:14:50 +00004955 // Create the class template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00004956 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00004957 if (!TemplateParamsOrErr)
4958 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004959
Gabor Marton26f72a92018-07-12 09:42:05 +00004960 ClassTemplateDecl *D2;
4961 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00004962 *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00004963 return D2;
4964
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004965 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00004966
Douglas Gregora082a492010-11-30 19:14:50 +00004967 D2->setAccess(D->getAccess());
4968 D2->setLexicalDeclContext(LexicalDC);
Gabor Marton7df342a2018-12-17 12:42:12 +00004969
4970 if (D->getDeclContext()->containsDeclAndLoad(D))
4971 DC->addDeclInternal(D2);
4972 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Balazs Keri0c23dc52018-08-13 13:08:37 +00004973 LexicalDC->addDeclInternal(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00004974
Gabor Marton7df342a2018-12-17 12:42:12 +00004975 if (FoundByLookup) {
4976 auto *Recent =
4977 const_cast<ClassTemplateDecl *>(FoundByLookup->getMostRecentDecl());
4978
4979 // It is possible that during the import of the class template definition
4980 // we start the import of a fwd friend decl of the very same class template
4981 // and we add the fwd friend decl to the lookup table. But the ToTemplated
4982 // had been created earlier and by that time the lookup could not find
4983 // anything existing, so it has no previous decl. Later, (still during the
4984 // import of the fwd friend decl) we start to import the definition again
4985 // and this time the lookup finds the previous fwd friend class template.
4986 // In this case we must set up the previous decl for the templated decl.
4987 if (!ToTemplated->getPreviousDecl()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00004988 assert(FoundByLookup->getTemplatedDecl() &&
4989 "Found decl must have its templated decl set");
Gabor Marton7df342a2018-12-17 12:42:12 +00004990 CXXRecordDecl *PrevTemplated =
4991 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
4992 if (ToTemplated != PrevTemplated)
4993 ToTemplated->setPreviousDecl(PrevTemplated);
4994 }
4995
4996 D2->setPreviousDecl(Recent);
4997 }
4998
4999 if (LexicalDC != DC && IsFriend)
5000 DC->makeDeclVisibleInContext(D2);
5001
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005002 if (FromTemplated->isCompleteDefinition() &&
5003 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00005004 // FIXME: Import definition!
5005 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005006
Douglas Gregora082a492010-11-30 19:14:50 +00005007 return D2;
5008}
5009
Balazs Keri3b30d652018-10-19 13:32:20 +00005010ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +00005011 ClassTemplateSpecializationDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005012 ClassTemplateDecl *ClassTemplate;
5013 if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate()))
5014 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005015
Douglas Gregore2e50d332010-12-01 01:36:18 +00005016 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005017 DeclContext *DC, *LexicalDC;
5018 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5019 return std::move(Err);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005020
5021 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005022 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005023 if (Error Err = ImportTemplateArguments(
5024 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5025 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005026
Douglas Gregore2e50d332010-12-01 01:36:18 +00005027 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005028 void *InsertPos = nullptr;
Gabor Marton7f8c4002019-03-19 13:34:10 +00005029 ClassTemplateSpecializationDecl *PrevDecl = nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005030 ClassTemplatePartialSpecializationDecl *PartialSpec =
5031 dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
5032 if (PartialSpec)
Gabor Marton7f8c4002019-03-19 13:34:10 +00005033 PrevDecl =
5034 ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005035 else
Gabor Marton7f8c4002019-03-19 13:34:10 +00005036 PrevDecl = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005037
Gabor Marton7f8c4002019-03-19 13:34:10 +00005038 if (PrevDecl) {
5039 if (IsStructuralMatch(D, PrevDecl)) {
5040 if (D->isThisDeclarationADefinition() && PrevDecl->getDefinition()) {
5041 Importer.MapImported(D, PrevDecl->getDefinition());
5042 // Import those default field initializers which have been
5043 // instantiated in the "From" context, but not in the "To" context.
5044 for (auto *FromField : D->fields())
5045 Importer.Import(FromField);
Gabor Marton42e15de2018-08-22 11:52:14 +00005046
Gabor Marton7f8c4002019-03-19 13:34:10 +00005047 // Import those methods which have been instantiated in the
5048 // "From" context, but not in the "To" context.
5049 for (CXXMethodDecl *FromM : D->methods())
5050 Importer.Import(FromM);
Gabor Marton42e15de2018-08-22 11:52:14 +00005051
Gabor Marton7f8c4002019-03-19 13:34:10 +00005052 // TODO Import instantiated default arguments.
5053 // TODO Import instantiated exception specifications.
5054 //
5055 // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint
5056 // what else could be fused during an AST merge.
5057 return PrevDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005058 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005059 } else { // ODR violation.
5060 // FIXME HandleNameConflict
5061 return nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005062 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005063 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005064
Gabor Marton7f8c4002019-03-19 13:34:10 +00005065 // Import the location of this declaration.
5066 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5067 if (!BeginLocOrErr)
5068 return BeginLocOrErr.takeError();
5069 ExpectedSLoc IdLocOrErr = import(D->getLocation());
5070 if (!IdLocOrErr)
5071 return IdLocOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +00005072
Gabor Marton7f8c4002019-03-19 13:34:10 +00005073 // Create the specialization.
5074 ClassTemplateSpecializationDecl *D2 = nullptr;
5075 if (PartialSpec) {
5076 // Import TemplateArgumentListInfo.
5077 TemplateArgumentListInfo ToTAInfo;
5078 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
5079 if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
5080 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005081
Gabor Marton7f8c4002019-03-19 13:34:10 +00005082 QualType CanonInjType;
5083 if (Error Err = importInto(
5084 CanonInjType, PartialSpec->getInjectedSpecializationType()))
5085 return std::move(Err);
5086 CanonInjType = CanonInjType.getCanonicalType();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005087
Balazs Keridec09162019-03-20 15:42:42 +00005088 auto ToTPListOrErr = import(PartialSpec->getTemplateParameters());
Gabor Marton7f8c4002019-03-19 13:34:10 +00005089 if (!ToTPListOrErr)
5090 return ToTPListOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005091
Gabor Marton7f8c4002019-03-19 13:34:10 +00005092 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
5093 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5094 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, ClassTemplate,
5095 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
5096 ToTAInfo, CanonInjType,
5097 cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl)))
5098 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005099
Gabor Marton7f8c4002019-03-19 13:34:10 +00005100 // Update InsertPos, because preceding import calls may have invalidated
5101 // it by adding new specializations.
5102 if (!ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos))
5103 // Add this partial specialization to the class template.
5104 ClassTemplate->AddPartialSpecialization(
5105 cast<ClassTemplatePartialSpecializationDecl>(D2), InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005106
Gabor Marton7f8c4002019-03-19 13:34:10 +00005107 } else { // Not a partial specialization.
5108 if (GetImportedOrCreateDecl(
5109 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5110 *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs,
5111 PrevDecl))
5112 return D2;
Gabor Marton42e15de2018-08-22 11:52:14 +00005113
Gabor Marton7f8c4002019-03-19 13:34:10 +00005114 // Update InsertPos, because preceding import calls may have invalidated
5115 // it by adding new specializations.
5116 if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos))
5117 // Add this specialization to the class template.
5118 ClassTemplate->AddSpecialization(D2, InsertPos);
5119 }
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005120
Gabor Marton7f8c4002019-03-19 13:34:10 +00005121 D2->setSpecializationKind(D->getSpecializationKind());
Douglas Gregore2e50d332010-12-01 01:36:18 +00005122
Gabor Marton7f8c4002019-03-19 13:34:10 +00005123 // Set the context of this specialization/instantiation.
5124 D2->setLexicalDeclContext(LexicalDC);
5125
5126 // Add to the DC only if it was an explicit specialization/instantiation.
5127 if (D2->isExplicitInstantiationOrSpecialization()) {
5128 LexicalDC->addDeclInternal(D2);
5129 }
5130
5131 // Import the qualifier, if any.
5132 if (auto LocOrErr = import(D->getQualifierLoc()))
5133 D2->setQualifierInfo(*LocOrErr);
5134 else
5135 return LocOrErr.takeError();
5136
5137 if (auto *TSI = D->getTypeAsWritten()) {
5138 if (auto TInfoOrErr = import(TSI))
5139 D2->setTypeAsWritten(*TInfoOrErr);
5140 else
5141 return TInfoOrErr.takeError();
5142
5143 if (auto LocOrErr = import(D->getTemplateKeywordLoc()))
5144 D2->setTemplateKeywordLoc(*LocOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +00005145 else
5146 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005147
Gabor Marton7f8c4002019-03-19 13:34:10 +00005148 if (auto LocOrErr = import(D->getExternLoc()))
5149 D2->setExternLoc(*LocOrErr);
5150 else
5151 return LocOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00005152 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005153
5154 if (D->getPointOfInstantiation().isValid()) {
5155 if (auto POIOrErr = import(D->getPointOfInstantiation()))
5156 D2->setPointOfInstantiation(*POIOrErr);
5157 else
5158 return POIOrErr.takeError();
5159 }
5160
5161 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
5162
Balazs Keri3b30d652018-10-19 13:32:20 +00005163 if (D->isCompleteDefinition())
5164 if (Error Err = ImportDefinition(D, D2))
5165 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005166
Douglas Gregore2e50d332010-12-01 01:36:18 +00005167 return D2;
5168}
5169
Balazs Keri3b30d652018-10-19 13:32:20 +00005170ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005171 // If this variable has a definition in the translation unit we're coming
5172 // from,
5173 // but this particular declaration is not that definition, import the
5174 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005175 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00005176 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
5177 if (Definition && Definition != D->getTemplatedDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005178 if (ExpectedDecl ImportedDefOrErr = import(
5179 Definition->getDescribedVarTemplate()))
5180 return Importer.MapImported(D, *ImportedDefOrErr);
5181 else
5182 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005183 }
5184
5185 // Import the major distinguishing characteristics of this variable template.
5186 DeclContext *DC, *LexicalDC;
5187 DeclarationName Name;
5188 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005189 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005190 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5191 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005192 if (ToD)
5193 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005194
5195 // We may already have a template of the same name; try to find and match it.
5196 assert(!DC->isFunctionOrMethod() &&
5197 "Variable templates cannot be declared at function scope");
5198 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00005199 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005200 for (auto *FoundDecl : FoundDecls) {
5201 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00005202 continue;
5203
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005204 Decl *Found = FoundDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005205 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005206 if (IsStructuralMatch(D, FoundTemplate)) {
5207 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00005208 Importer.MapImported(D->getTemplatedDecl(),
5209 FoundTemplate->getTemplatedDecl());
5210 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005211 }
5212 }
5213
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005214 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005215 }
5216
5217 if (!ConflictingDecls.empty()) {
5218 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
5219 ConflictingDecls.data(),
5220 ConflictingDecls.size());
5221 }
5222
5223 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005224 // FIXME: Is it possible to get other error than name conflict?
5225 // (Put this `if` into the previous `if`?)
5226 return make_error<ImportError>(ImportError::NameConflict);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005227
5228 VarDecl *DTemplated = D->getTemplatedDecl();
5229
5230 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005231 // FIXME: Value not used?
5232 ExpectedType TypeOrErr = import(DTemplated->getType());
5233 if (!TypeOrErr)
5234 return TypeOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005235
5236 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005237 VarDecl *ToTemplated;
5238 if (Error Err = importInto(ToTemplated, DTemplated))
5239 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005240
5241 // Create the variable template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005242 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005243 if (!TemplateParamsOrErr)
5244 return TemplateParamsOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005245
Gabor Marton26f72a92018-07-12 09:42:05 +00005246 VarTemplateDecl *ToVarTD;
5247 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00005248 Name, *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005249 return ToVarTD;
5250
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005251 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005252
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005253 ToVarTD->setAccess(D->getAccess());
5254 ToVarTD->setLexicalDeclContext(LexicalDC);
5255 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005256
Larisse Voufo39a1e502013-08-06 01:03:05 +00005257 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005258 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005259 // FIXME: Import definition!
5260 }
5261
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005262 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005263}
5264
Balazs Keri3b30d652018-10-19 13:32:20 +00005265ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00005266 VarTemplateSpecializationDecl *D) {
5267 // If this record has a definition in the translation unit we're coming from,
5268 // but this particular declaration is not that definition, import the
5269 // definition and map to that.
5270 VarDecl *Definition = D->getDefinition();
5271 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005272 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5273 return Importer.MapImported(D, *ImportedDefOrErr);
5274 else
5275 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005276 }
5277
Balazs Keri3b30d652018-10-19 13:32:20 +00005278 VarTemplateDecl *VarTemplate;
5279 if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate()))
5280 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005281
5282 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005283 DeclContext *DC, *LexicalDC;
5284 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5285 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005286
5287 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005288 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5289 if (!BeginLocOrErr)
5290 return BeginLocOrErr.takeError();
5291
5292 auto IdLocOrErr = import(D->getLocation());
5293 if (!IdLocOrErr)
5294 return IdLocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005295
5296 // Import template arguments.
5297 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005298 if (Error Err = ImportTemplateArguments(
5299 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5300 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005301
5302 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005303 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005304 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005305 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005306 if (D2) {
5307 // We already have a variable template specialization with these template
5308 // arguments.
5309
5310 // FIXME: Check for specialization vs. instantiation errors.
5311
5312 if (VarDecl *FoundDef = D2->getDefinition()) {
5313 if (!D->isThisDeclarationADefinition() ||
5314 IsStructuralMatch(D, FoundDef)) {
5315 // The record types structurally match, or the "from" translation
5316 // unit only had a forward declaration anyway; call it the same
5317 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00005318 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005319 }
5320 }
5321 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005322 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005323 QualType T;
5324 if (Error Err = importInto(T, D->getType()))
5325 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005326
Balazs Keri3b30d652018-10-19 13:32:20 +00005327 auto TInfoOrErr = import(D->getTypeSourceInfo());
5328 if (!TInfoOrErr)
5329 return TInfoOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005330
5331 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00005332 if (Error Err = ImportTemplateArgumentListInfo(
5333 D->getTemplateArgsInfo(), ToTAInfo))
5334 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005335
5336 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005337 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005338 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
5339 // Import TemplateArgumentListInfo
5340 TemplateArgumentListInfo ArgInfos;
5341 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
5342 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
Balazs Keri3b30d652018-10-19 13:32:20 +00005343 if (Error Err = ImportTemplateArgumentListInfo(
5344 *FromTAArgsAsWritten, ArgInfos))
5345 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005346
Balazs Keridec09162019-03-20 15:42:42 +00005347 auto ToTPListOrErr = import(FromPartial->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005348 if (!ToTPListOrErr)
5349 return ToTPListOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005350
Gabor Marton26f72a92018-07-12 09:42:05 +00005351 PartVarSpecDecl *ToPartial;
5352 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005353 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
5354 VarTemplate, T, *TInfoOrErr,
5355 D->getStorageClass(), TemplateArgs, ArgInfos))
Gabor Marton26f72a92018-07-12 09:42:05 +00005356 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005357
Balazs Keri3b30d652018-10-19 13:32:20 +00005358 if (Expected<PartVarSpecDecl *> ToInstOrErr = import(
5359 FromPartial->getInstantiatedFromMember()))
5360 ToPartial->setInstantiatedFromMember(*ToInstOrErr);
5361 else
5362 return ToInstOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005363
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005364 if (FromPartial->isMemberSpecialization())
5365 ToPartial->setMemberSpecialization();
5366
5367 D2 = ToPartial;
Balazs Keri3b30d652018-10-19 13:32:20 +00005368
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005369 } else { // Full specialization
Balazs Keri3b30d652018-10-19 13:32:20 +00005370 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
5371 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
5372 T, *TInfoOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005373 D->getStorageClass(), TemplateArgs))
5374 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005375 }
5376
Balazs Keri3b30d652018-10-19 13:32:20 +00005377 if (D->getPointOfInstantiation().isValid()) {
5378 if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation()))
5379 D2->setPointOfInstantiation(*POIOrErr);
5380 else
5381 return POIOrErr.takeError();
5382 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005383
Larisse Voufo39a1e502013-08-06 01:03:05 +00005384 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005385 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005386
5387 // Add this specialization to the class template.
5388 VarTemplate->AddSpecialization(D2, InsertPos);
5389
5390 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005391 if (auto LocOrErr = import(D->getQualifierLoc()))
5392 D2->setQualifierInfo(*LocOrErr);
5393 else
5394 return LocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005395
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005396 if (D->isConstexpr())
5397 D2->setConstexpr(true);
5398
Larisse Voufo39a1e502013-08-06 01:03:05 +00005399 // Add the specialization to this context.
5400 D2->setLexicalDeclContext(LexicalDC);
5401 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005402
5403 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00005404 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005405
Balazs Keri3b30d652018-10-19 13:32:20 +00005406 if (Error Err = ImportInitializer(D, D2))
5407 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005408
5409 return D2;
5410}
5411
Balazs Keri3b30d652018-10-19 13:32:20 +00005412ExpectedDecl
5413ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005414 DeclContext *DC, *LexicalDC;
5415 DeclarationName Name;
5416 SourceLocation Loc;
5417 NamedDecl *ToD;
5418
Balazs Keri3b30d652018-10-19 13:32:20 +00005419 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5420 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005421
5422 if (ToD)
5423 return ToD;
5424
Gabor Marton16d98c22019-03-07 13:01:51 +00005425 const FunctionTemplateDecl *FoundByLookup = nullptr;
5426
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005427 // Try to find a function in our own ("to") context with the same name, same
5428 // type, and in the same context as the function we're importing.
Gabor Marton16d98c22019-03-07 13:01:51 +00005429 // FIXME Split this into a separate function.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005430 if (!LexicalDC->isFunctionOrMethod()) {
Gabor Martone331e632019-02-18 13:09:27 +00005431 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00005432 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005433 for (auto *FoundDecl : FoundDecls) {
5434 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005435 continue;
5436
Gabor Marton16d98c22019-03-07 13:01:51 +00005437 if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
5438 if (FoundTemplate->hasExternalFormalLinkage() &&
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005439 D->hasExternalFormalLinkage()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005440 if (IsStructuralMatch(D, FoundTemplate)) {
5441 FunctionTemplateDecl *TemplateWithDef =
5442 getTemplateDefinition(FoundTemplate);
5443 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
5444 return Importer.MapImported(D, TemplateWithDef);
5445 }
5446 FoundByLookup = FoundTemplate;
5447 break;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005448 }
Gabor Marton16d98c22019-03-07 13:01:51 +00005449 // TODO: handle conflicting names
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005450 }
5451 }
5452 }
5453 }
5454
Balazs Keridec09162019-03-20 15:42:42 +00005455 auto ParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005456 if (!ParamsOrErr)
5457 return ParamsOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005458
Balazs Keri3b30d652018-10-19 13:32:20 +00005459 FunctionDecl *TemplatedFD;
5460 if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl()))
5461 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005462
Gabor Marton26f72a92018-07-12 09:42:05 +00005463 FunctionTemplateDecl *ToFunc;
5464 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005465 *ParamsOrErr, TemplatedFD))
Gabor Marton26f72a92018-07-12 09:42:05 +00005466 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005467
5468 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005469
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005470 ToFunc->setAccess(D->getAccess());
5471 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005472 LexicalDC->addDeclInternal(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005473
5474 if (FoundByLookup) {
5475 auto *Recent =
5476 const_cast<FunctionTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5477 if (!TemplatedFD->getPreviousDecl()) {
5478 assert(FoundByLookup->getTemplatedDecl() &&
5479 "Found decl must have its templated decl set");
5480 auto *PrevTemplated =
5481 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5482 if (TemplatedFD != PrevTemplated)
5483 TemplatedFD->setPreviousDecl(PrevTemplated);
5484 }
5485 ToFunc->setPreviousDecl(Recent);
5486 }
5487
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005488 return ToFunc;
5489}
5490
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005491//----------------------------------------------------------------------------
5492// Import Statements
5493//----------------------------------------------------------------------------
5494
Balazs Keri3b30d652018-10-19 13:32:20 +00005495ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005496 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
5497 << S->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005498 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005499}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005500
Balazs Keri3b30d652018-10-19 13:32:20 +00005501
5502ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005503 SmallVector<IdentifierInfo *, 4> Names;
5504 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5505 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005506 // ToII is nullptr when no symbolic name is given for output operand
5507 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005508 Names.push_back(ToII);
5509 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005510
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005511 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5512 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005513 // ToII is nullptr when no symbolic name is given for input operand
5514 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005515 Names.push_back(ToII);
5516 }
5517
5518 SmallVector<StringLiteral *, 4> Clobbers;
5519 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005520 if (auto ClobberOrErr = import(S->getClobberStringLiteral(I)))
5521 Clobbers.push_back(*ClobberOrErr);
5522 else
5523 return ClobberOrErr.takeError();
5524
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005525 }
5526
5527 SmallVector<StringLiteral *, 4> Constraints;
5528 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005529 if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I)))
5530 Constraints.push_back(*OutputOrErr);
5531 else
5532 return OutputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005533 }
5534
5535 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005536 if (auto InputOrErr = import(S->getInputConstraintLiteral(I)))
5537 Constraints.push_back(*InputOrErr);
5538 else
5539 return InputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005540 }
5541
5542 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Balazs Keri3b30d652018-10-19 13:32:20 +00005543 if (Error Err = ImportContainerChecked(S->outputs(), Exprs))
5544 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005545
Balazs Keri3b30d652018-10-19 13:32:20 +00005546 if (Error Err = ImportArrayChecked(
5547 S->inputs(), Exprs.begin() + S->getNumOutputs()))
5548 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005549
Balazs Keri3b30d652018-10-19 13:32:20 +00005550 ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc());
5551 if (!AsmLocOrErr)
5552 return AsmLocOrErr.takeError();
5553 auto AsmStrOrErr = import(S->getAsmString());
5554 if (!AsmStrOrErr)
5555 return AsmStrOrErr.takeError();
5556 ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc());
5557 if (!RParenLocOrErr)
5558 return RParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005559
5560 return new (Importer.getToContext()) GCCAsmStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005561 Importer.getToContext(),
5562 *AsmLocOrErr,
5563 S->isSimple(),
5564 S->isVolatile(),
5565 S->getNumOutputs(),
5566 S->getNumInputs(),
5567 Names.data(),
5568 Constraints.data(),
5569 Exprs.data(),
5570 *AsmStrOrErr,
5571 S->getNumClobbers(),
5572 Clobbers.data(),
5573 *RParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005574}
5575
Balazs Keri3b30d652018-10-19 13:32:20 +00005576ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5577 auto Imp = importSeq(S->getDeclGroup(), S->getBeginLoc(), S->getEndLoc());
5578 if (!Imp)
5579 return Imp.takeError();
5580
5581 DeclGroupRef ToDG;
5582 SourceLocation ToBeginLoc, ToEndLoc;
5583 std::tie(ToDG, ToBeginLoc, ToEndLoc) = *Imp;
5584
5585 return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005586}
5587
Balazs Keri3b30d652018-10-19 13:32:20 +00005588ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5589 ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc());
5590 if (!ToSemiLocOrErr)
5591 return ToSemiLocOrErr.takeError();
5592 return new (Importer.getToContext()) NullStmt(
5593 *ToSemiLocOrErr, S->hasLeadingEmptyMacro());
Sean Callanan59721b32015-04-28 18:41:46 +00005594}
5595
Balazs Keri3b30d652018-10-19 13:32:20 +00005596ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005597 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005598
Balazs Keri3b30d652018-10-19 13:32:20 +00005599 if (Error Err = ImportContainerChecked(S->body(), ToStmts))
5600 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00005601
Balazs Keri3b30d652018-10-19 13:32:20 +00005602 ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc());
5603 if (!ToLBracLocOrErr)
5604 return ToLBracLocOrErr.takeError();
5605
5606 ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc());
5607 if (!ToRBracLocOrErr)
5608 return ToRBracLocOrErr.takeError();
5609
5610 return CompoundStmt::Create(
5611 Importer.getToContext(), ToStmts,
5612 *ToLBracLocOrErr, *ToRBracLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005613}
5614
Balazs Keri3b30d652018-10-19 13:32:20 +00005615ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5616 auto Imp = importSeq(
5617 S->getLHS(), S->getRHS(), S->getSubStmt(), S->getCaseLoc(),
5618 S->getEllipsisLoc(), S->getColonLoc());
5619 if (!Imp)
5620 return Imp.takeError();
5621
5622 Expr *ToLHS, *ToRHS;
5623 Stmt *ToSubStmt;
5624 SourceLocation ToCaseLoc, ToEllipsisLoc, ToColonLoc;
5625 std::tie(ToLHS, ToRHS, ToSubStmt, ToCaseLoc, ToEllipsisLoc, ToColonLoc) =
5626 *Imp;
5627
Bruno Ricci5b30571752018-10-28 12:30:53 +00005628 auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS,
5629 ToCaseLoc, ToEllipsisLoc, ToColonLoc);
Gabor Horvath480892b2017-10-18 09:25:18 +00005630 ToStmt->setSubStmt(ToSubStmt);
Balazs Keri3b30d652018-10-19 13:32:20 +00005631
Gabor Horvath480892b2017-10-18 09:25:18 +00005632 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005633}
5634
Balazs Keri3b30d652018-10-19 13:32:20 +00005635ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5636 auto Imp = importSeq(S->getDefaultLoc(), S->getColonLoc(), S->getSubStmt());
5637 if (!Imp)
5638 return Imp.takeError();
5639
5640 SourceLocation ToDefaultLoc, ToColonLoc;
5641 Stmt *ToSubStmt;
5642 std::tie(ToDefaultLoc, ToColonLoc, ToSubStmt) = *Imp;
5643
5644 return new (Importer.getToContext()) DefaultStmt(
5645 ToDefaultLoc, ToColonLoc, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005646}
5647
Balazs Keri3b30d652018-10-19 13:32:20 +00005648ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5649 auto Imp = importSeq(S->getIdentLoc(), S->getDecl(), S->getSubStmt());
5650 if (!Imp)
5651 return Imp.takeError();
5652
5653 SourceLocation ToIdentLoc;
5654 LabelDecl *ToLabelDecl;
5655 Stmt *ToSubStmt;
5656 std::tie(ToIdentLoc, ToLabelDecl, ToSubStmt) = *Imp;
5657
5658 return new (Importer.getToContext()) LabelStmt(
5659 ToIdentLoc, ToLabelDecl, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005660}
5661
Balazs Keri3b30d652018-10-19 13:32:20 +00005662ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5663 ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc());
5664 if (!ToAttrLocOrErr)
5665 return ToAttrLocOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005666 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5667 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00005668 if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs))
5669 return std::move(Err);
5670 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
5671 if (!ToSubStmtOrErr)
5672 return ToSubStmtOrErr.takeError();
5673
5674 return AttributedStmt::Create(
5675 Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005676}
5677
Balazs Keri3b30d652018-10-19 13:32:20 +00005678ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5679 auto Imp = importSeq(
5680 S->getIfLoc(), S->getInit(), S->getConditionVariable(), S->getCond(),
5681 S->getThen(), S->getElseLoc(), S->getElse());
5682 if (!Imp)
5683 return Imp.takeError();
5684
5685 SourceLocation ToIfLoc, ToElseLoc;
5686 Stmt *ToInit, *ToThen, *ToElse;
5687 VarDecl *ToConditionVariable;
5688 Expr *ToCond;
5689 std::tie(
5690 ToIfLoc, ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc, ToElse) =
5691 *Imp;
5692
Bruno Riccib1cc94b2018-10-27 21:12:20 +00005693 return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(),
5694 ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc,
5695 ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00005696}
5697
Balazs Keri3b30d652018-10-19 13:32:20 +00005698ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
5699 auto Imp = importSeq(
5700 S->getInit(), S->getConditionVariable(), S->getCond(),
5701 S->getBody(), S->getSwitchLoc());
5702 if (!Imp)
5703 return Imp.takeError();
5704
5705 Stmt *ToInit, *ToBody;
5706 VarDecl *ToConditionVariable;
5707 Expr *ToCond;
5708 SourceLocation ToSwitchLoc;
5709 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
5710
Bruno Riccie2806f82018-10-29 16:12:37 +00005711 auto *ToStmt = SwitchStmt::Create(Importer.getToContext(), ToInit,
5712 ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00005713 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00005714 ToStmt->setSwitchLoc(ToSwitchLoc);
5715
Sean Callanan59721b32015-04-28 18:41:46 +00005716 // Now we have to re-chain the cases.
5717 SwitchCase *LastChainedSwitchCase = nullptr;
5718 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5719 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005720 Expected<SwitchCase *> ToSCOrErr = import(SC);
5721 if (!ToSCOrErr)
5722 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005723 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00005724 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005725 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005726 ToStmt->setSwitchCaseList(*ToSCOrErr);
5727 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005728 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005729
Sean Callanan59721b32015-04-28 18:41:46 +00005730 return ToStmt;
5731}
5732
Balazs Keri3b30d652018-10-19 13:32:20 +00005733ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5734 auto Imp = importSeq(
5735 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
5736 if (!Imp)
5737 return Imp.takeError();
5738
5739 VarDecl *ToConditionVariable;
5740 Expr *ToCond;
5741 Stmt *ToBody;
5742 SourceLocation ToWhileLoc;
5743 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
5744
Bruno Riccibacf7512018-10-30 13:42:41 +00005745 return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond,
5746 ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005747}
5748
Balazs Keri3b30d652018-10-19 13:32:20 +00005749ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5750 auto Imp = importSeq(
5751 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
5752 S->getRParenLoc());
5753 if (!Imp)
5754 return Imp.takeError();
5755
5756 Stmt *ToBody;
5757 Expr *ToCond;
5758 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
5759 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
5760
5761 return new (Importer.getToContext()) DoStmt(
5762 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005763}
5764
Balazs Keri3b30d652018-10-19 13:32:20 +00005765ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
5766 auto Imp = importSeq(
5767 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
5768 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
5769 if (!Imp)
5770 return Imp.takeError();
5771
5772 Stmt *ToInit;
5773 Expr *ToCond, *ToInc;
5774 VarDecl *ToConditionVariable;
5775 Stmt *ToBody;
5776 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
5777 std::tie(
5778 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
5779 ToLParenLoc, ToRParenLoc) = *Imp;
5780
5781 return new (Importer.getToContext()) ForStmt(
5782 Importer.getToContext(),
5783 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
5784 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005785}
5786
Balazs Keri3b30d652018-10-19 13:32:20 +00005787ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5788 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
5789 if (!Imp)
5790 return Imp.takeError();
5791
5792 LabelDecl *ToLabel;
5793 SourceLocation ToGotoLoc, ToLabelLoc;
5794 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
5795
5796 return new (Importer.getToContext()) GotoStmt(
5797 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005798}
5799
Balazs Keri3b30d652018-10-19 13:32:20 +00005800ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5801 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
5802 if (!Imp)
5803 return Imp.takeError();
5804
5805 SourceLocation ToGotoLoc, ToStarLoc;
5806 Expr *ToTarget;
5807 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
5808
5809 return new (Importer.getToContext()) IndirectGotoStmt(
5810 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00005811}
5812
Balazs Keri3b30d652018-10-19 13:32:20 +00005813ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5814 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
5815 if (!ToContinueLocOrErr)
5816 return ToContinueLocOrErr.takeError();
5817 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005818}
5819
Balazs Keri3b30d652018-10-19 13:32:20 +00005820ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5821 auto ToBreakLocOrErr = import(S->getBreakLoc());
5822 if (!ToBreakLocOrErr)
5823 return ToBreakLocOrErr.takeError();
5824 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005825}
5826
Balazs Keri3b30d652018-10-19 13:32:20 +00005827ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5828 auto Imp = importSeq(
5829 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
5830 if (!Imp)
5831 return Imp.takeError();
5832
5833 SourceLocation ToReturnLoc;
5834 Expr *ToRetValue;
5835 const VarDecl *ToNRVOCandidate;
5836 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
5837
Bruno Ricci023b1d12018-10-30 14:40:49 +00005838 return ReturnStmt::Create(Importer.getToContext(), ToReturnLoc, ToRetValue,
5839 ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00005840}
5841
Balazs Keri3b30d652018-10-19 13:32:20 +00005842ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5843 auto Imp = importSeq(
5844 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
5845 if (!Imp)
5846 return Imp.takeError();
5847
5848 SourceLocation ToCatchLoc;
5849 VarDecl *ToExceptionDecl;
5850 Stmt *ToHandlerBlock;
5851 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
5852
5853 return new (Importer.getToContext()) CXXCatchStmt (
5854 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00005855}
5856
Balazs Keri3b30d652018-10-19 13:32:20 +00005857ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5858 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
5859 if (!ToTryLocOrErr)
5860 return ToTryLocOrErr.takeError();
5861
5862 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
5863 if (!ToTryBlockOrErr)
5864 return ToTryBlockOrErr.takeError();
5865
Sean Callanan59721b32015-04-28 18:41:46 +00005866 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5867 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5868 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005869 if (auto ToHandlerOrErr = import(FromHandler))
5870 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005871 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005872 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005873 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005874
5875 return CXXTryStmt::Create(
5876 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00005877}
5878
Balazs Keri3b30d652018-10-19 13:32:20 +00005879ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5880 auto Imp1 = importSeq(
5881 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
5882 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
5883 if (!Imp1)
5884 return Imp1.takeError();
5885 auto Imp2 = importSeq(
5886 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
5887 if (!Imp2)
5888 return Imp2.takeError();
5889
5890 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
5891 Expr *ToCond, *ToInc;
5892 Stmt *ToInit, *ToBody;
5893 std::tie(
5894 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5895 ToBody) = *Imp1;
5896 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
5897 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
5898
5899 return new (Importer.getToContext()) CXXForRangeStmt(
5900 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5901 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005902}
5903
Balazs Keri3b30d652018-10-19 13:32:20 +00005904ExpectedStmt
5905ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5906 auto Imp = importSeq(
5907 S->getElement(), S->getCollection(), S->getBody(),
5908 S->getForLoc(), S->getRParenLoc());
5909 if (!Imp)
5910 return Imp.takeError();
5911
5912 Stmt *ToElement, *ToBody;
5913 Expr *ToCollection;
5914 SourceLocation ToForLoc, ToRParenLoc;
5915 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
5916
5917 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
5918 ToCollection,
5919 ToBody,
5920 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00005921 ToRParenLoc);
5922}
5923
Balazs Keri3b30d652018-10-19 13:32:20 +00005924ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5925 auto Imp = importSeq(
5926 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
5927 S->getCatchBody());
5928 if (!Imp)
5929 return Imp.takeError();
5930
5931 SourceLocation ToAtCatchLoc, ToRParenLoc;
5932 VarDecl *ToCatchParamDecl;
5933 Stmt *ToCatchBody;
5934 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
5935
5936 return new (Importer.getToContext()) ObjCAtCatchStmt (
5937 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00005938}
5939
Balazs Keri3b30d652018-10-19 13:32:20 +00005940ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5941 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
5942 if (!ToAtFinallyLocOrErr)
5943 return ToAtFinallyLocOrErr.takeError();
5944 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
5945 if (!ToAtFinallyStmtOrErr)
5946 return ToAtFinallyStmtOrErr.takeError();
5947 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
5948 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005949}
5950
Balazs Keri3b30d652018-10-19 13:32:20 +00005951ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5952 auto Imp = importSeq(
5953 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
5954 if (!Imp)
5955 return Imp.takeError();
5956
5957 SourceLocation ToAtTryLoc;
5958 Stmt *ToTryBody, *ToFinallyStmt;
5959 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
5960
Sean Callanan59721b32015-04-28 18:41:46 +00005961 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5962 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5963 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005964 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
5965 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005966 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005967 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005968 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005969
Sean Callanan59721b32015-04-28 18:41:46 +00005970 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005971 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00005972 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005973 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005974}
5975
Balazs Keri3b30d652018-10-19 13:32:20 +00005976ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00005977 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005978 auto Imp = importSeq(
5979 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
5980 if (!Imp)
5981 return Imp.takeError();
5982
5983 SourceLocation ToAtSynchronizedLoc;
5984 Expr *ToSynchExpr;
5985 Stmt *ToSynchBody;
5986 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
5987
Sean Callanan59721b32015-04-28 18:41:46 +00005988 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5989 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5990}
5991
Balazs Keri3b30d652018-10-19 13:32:20 +00005992ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5993 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
5994 if (!ToThrowLocOrErr)
5995 return ToThrowLocOrErr.takeError();
5996 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
5997 if (!ToThrowExprOrErr)
5998 return ToThrowExprOrErr.takeError();
5999 return new (Importer.getToContext()) ObjCAtThrowStmt(
6000 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006001}
6002
Balazs Keri3b30d652018-10-19 13:32:20 +00006003ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
6004 ObjCAutoreleasePoolStmt *S) {
6005 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
6006 if (!ToAtLocOrErr)
6007 return ToAtLocOrErr.takeError();
6008 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6009 if (!ToSubStmtOrErr)
6010 return ToSubStmtOrErr.takeError();
6011 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6012 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006013}
6014
6015//----------------------------------------------------------------------------
6016// Import Expressions
6017//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006018ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006019 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6020 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006021 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006022}
6023
Balazs Keri3b30d652018-10-19 13:32:20 +00006024ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6025 auto Imp = importSeq(
6026 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6027 E->getRParenLoc(), E->getType());
6028 if (!Imp)
6029 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006030
Balazs Keri3b30d652018-10-19 13:32:20 +00006031 SourceLocation ToBuiltinLoc, ToRParenLoc;
6032 Expr *ToSubExpr;
6033 TypeSourceInfo *ToWrittenTypeInfo;
6034 QualType ToType;
6035 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6036 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006037
6038 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006039 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6040 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006041}
6042
Tom Roeder521f0042019-02-26 19:26:41 +00006043ExpectedStmt ASTNodeImporter::VisitChooseExpr(ChooseExpr *E) {
6044 auto Imp = importSeq(E->getCond(), E->getLHS(), E->getRHS(),
6045 E->getBuiltinLoc(), E->getRParenLoc(), E->getType());
6046 if (!Imp)
6047 return Imp.takeError();
6048
6049 Expr *ToCond;
6050 Expr *ToLHS;
6051 Expr *ToRHS;
6052 SourceLocation ToBuiltinLoc, ToRParenLoc;
6053 QualType ToType;
6054 std::tie(ToCond, ToLHS, ToRHS, ToBuiltinLoc, ToRParenLoc, ToType) = *Imp;
6055
6056 ExprValueKind VK = E->getValueKind();
6057 ExprObjectKind OK = E->getObjectKind();
6058
6059 bool TypeDependent = ToCond->isTypeDependent();
6060 bool ValueDependent = ToCond->isValueDependent();
6061
6062 // The value of CondIsTrue only matters if the value is not
6063 // condition-dependent.
6064 bool CondIsTrue = !E->isConditionDependent() && E->isConditionTrue();
6065
6066 return new (Importer.getToContext())
6067 ChooseExpr(ToBuiltinLoc, ToCond, ToLHS, ToRHS, ToType, VK, OK,
6068 ToRParenLoc, CondIsTrue, TypeDependent, ValueDependent);
6069}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006070
Balazs Keri3b30d652018-10-19 13:32:20 +00006071ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6072 ExpectedType TypeOrErr = import(E->getType());
6073 if (!TypeOrErr)
6074 return TypeOrErr.takeError();
6075
6076 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6077 if (!BeginLocOrErr)
6078 return BeginLocOrErr.takeError();
6079
6080 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006081}
6082
Balazs Keri3b30d652018-10-19 13:32:20 +00006083ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6084 auto Imp = importSeq(
6085 E->getBeginLoc(), E->getType(), E->getFunctionName());
6086 if (!Imp)
6087 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006088
Balazs Keri3b30d652018-10-19 13:32:20 +00006089 SourceLocation ToBeginLoc;
6090 QualType ToType;
6091 StringLiteral *ToFunctionName;
6092 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006093
Bruno Ricci17ff0262018-10-27 19:21:19 +00006094 return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType,
6095 E->getIdentKind(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006096}
6097
Balazs Keri3b30d652018-10-19 13:32:20 +00006098ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6099 auto Imp = importSeq(
6100 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6101 E->getLocation(), E->getType());
6102 if (!Imp)
6103 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006104
Balazs Keri3b30d652018-10-19 13:32:20 +00006105 NestedNameSpecifierLoc ToQualifierLoc;
6106 SourceLocation ToTemplateKeywordLoc, ToLocation;
6107 ValueDecl *ToDecl;
6108 QualType ToType;
6109 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6110 *Imp;
6111
6112 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006113 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006114 auto FoundDOrErr = import(E->getFoundDecl());
6115 if (!FoundDOrErr)
6116 return FoundDOrErr.takeError();
6117 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006118 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006119
Aleksei Sidorina693b372016-09-28 10:16:56 +00006120 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006121 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006122 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006123 if (Error Err =
6124 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6125 return std::move(Err);
6126 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006127 }
6128
Balazs Keri3b30d652018-10-19 13:32:20 +00006129 auto *ToE = DeclRefExpr::Create(
6130 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6131 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
6132 E->getValueKind(), ToFoundD, ToResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006133 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006134 ToE->setHadMultipleCandidates(true);
6135 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006136}
6137
Balazs Keri3b30d652018-10-19 13:32:20 +00006138ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6139 ExpectedType TypeOrErr = import(E->getType());
6140 if (!TypeOrErr)
6141 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006142
Balazs Keri3b30d652018-10-19 13:32:20 +00006143 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006144}
6145
Balazs Keri3b30d652018-10-19 13:32:20 +00006146ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6147 ExpectedExpr ToInitOrErr = import(E->getInit());
6148 if (!ToInitOrErr)
6149 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006150
Balazs Keri3b30d652018-10-19 13:32:20 +00006151 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6152 if (!ToEqualOrColonLocOrErr)
6153 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006154
Balazs Keri3b30d652018-10-19 13:32:20 +00006155 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006156 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006157 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6158 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6159 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006160 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006161 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006162 }
6163
Balazs Keri3b30d652018-10-19 13:32:20 +00006164 SmallVector<Designator, 4> ToDesignators(E->size());
6165 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6166 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006167
6168 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006169 Importer.getToContext(), ToDesignators,
6170 ToIndexExprs, *ToEqualOrColonLocOrErr,
6171 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006172}
6173
Balazs Keri3b30d652018-10-19 13:32:20 +00006174ExpectedStmt
6175ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6176 ExpectedType ToTypeOrErr = import(E->getType());
6177 if (!ToTypeOrErr)
6178 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006179
Balazs Keri3b30d652018-10-19 13:32:20 +00006180 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6181 if (!ToLocationOrErr)
6182 return ToLocationOrErr.takeError();
6183
6184 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6185 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006186}
6187
Balazs Keri3b30d652018-10-19 13:32:20 +00006188ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6189 ExpectedType ToTypeOrErr = import(E->getType());
6190 if (!ToTypeOrErr)
6191 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006192
Balazs Keri3b30d652018-10-19 13:32:20 +00006193 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6194 if (!ToLocationOrErr)
6195 return ToLocationOrErr.takeError();
6196
6197 return IntegerLiteral::Create(
6198 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006199}
6200
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006201
Balazs Keri3b30d652018-10-19 13:32:20 +00006202ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6203 ExpectedType ToTypeOrErr = import(E->getType());
6204 if (!ToTypeOrErr)
6205 return ToTypeOrErr.takeError();
6206
6207 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6208 if (!ToLocationOrErr)
6209 return ToLocationOrErr.takeError();
6210
6211 return FloatingLiteral::Create(
6212 Importer.getToContext(), E->getValue(), E->isExact(),
6213 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006214}
6215
Balazs Keri3b30d652018-10-19 13:32:20 +00006216ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6217 auto ToTypeOrErr = import(E->getType());
6218 if (!ToTypeOrErr)
6219 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006220
Balazs Keri3b30d652018-10-19 13:32:20 +00006221 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6222 if (!ToSubExprOrErr)
6223 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006224
Balazs Keri3b30d652018-10-19 13:32:20 +00006225 return new (Importer.getToContext()) ImaginaryLiteral(
6226 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006227}
6228
Balazs Keri3b30d652018-10-19 13:32:20 +00006229ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6230 ExpectedType ToTypeOrErr = import(E->getType());
6231 if (!ToTypeOrErr)
6232 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006233
Balazs Keri3b30d652018-10-19 13:32:20 +00006234 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6235 if (!ToLocationOrErr)
6236 return ToLocationOrErr.takeError();
6237
6238 return new (Importer.getToContext()) CharacterLiteral(
6239 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006240}
6241
Balazs Keri3b30d652018-10-19 13:32:20 +00006242ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6243 ExpectedType ToTypeOrErr = import(E->getType());
6244 if (!ToTypeOrErr)
6245 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006246
Balazs Keri3b30d652018-10-19 13:32:20 +00006247 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6248 if (Error Err = ImportArrayChecked(
6249 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6250 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006251
Balazs Keri3b30d652018-10-19 13:32:20 +00006252 return StringLiteral::Create(
6253 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6254 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006255}
6256
Balazs Keri3b30d652018-10-19 13:32:20 +00006257ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6258 auto Imp = importSeq(
6259 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6260 E->getInitializer());
6261 if (!Imp)
6262 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006263
Balazs Keri3b30d652018-10-19 13:32:20 +00006264 SourceLocation ToLParenLoc;
6265 TypeSourceInfo *ToTypeSourceInfo;
6266 QualType ToType;
6267 Expr *ToInitializer;
6268 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006269
6270 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006271 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6272 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006273}
6274
Balazs Keri3b30d652018-10-19 13:32:20 +00006275ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6276 auto Imp = importSeq(
6277 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6278 if (!Imp)
6279 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006280
Balazs Keri3b30d652018-10-19 13:32:20 +00006281 SourceLocation ToBuiltinLoc, ToRParenLoc;
6282 QualType ToType;
6283 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6284
6285 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6286 if (Error Err = ImportArrayChecked(
6287 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6288 ToExprs.begin()))
6289 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006290
6291 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006292 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006293}
6294
Balazs Keri3b30d652018-10-19 13:32:20 +00006295ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6296 auto Imp = importSeq(
6297 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6298 if (!Imp)
6299 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006300
Balazs Keri3b30d652018-10-19 13:32:20 +00006301 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6302 LabelDecl *ToLabel;
6303 QualType ToType;
6304 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006305
6306 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006307 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006308}
6309
Bill Wendling8003edc2018-11-09 00:41:36 +00006310ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) {
6311 auto Imp = importSeq(E->getSubExpr());
6312 if (!Imp)
6313 return Imp.takeError();
6314
6315 Expr *ToSubExpr;
6316 std::tie(ToSubExpr) = *Imp;
6317
Fangrui Song407659a2018-11-30 23:41:18 +00006318 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr);
Bill Wendling8003edc2018-11-09 00:41:36 +00006319}
6320
Balazs Keri3b30d652018-10-19 13:32:20 +00006321ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6322 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6323 if (!Imp)
6324 return Imp.takeError();
6325
6326 SourceLocation ToLParen, ToRParen;
6327 Expr *ToSubExpr;
6328 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006329
Fangrui Song6907ce22018-07-30 19:24:48 +00006330 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006331 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006332}
6333
Balazs Keri3b30d652018-10-19 13:32:20 +00006334ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6335 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6336 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6337 return std::move(Err);
6338
6339 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6340 if (!ToLParenLocOrErr)
6341 return ToLParenLocOrErr.takeError();
6342
6343 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6344 if (!ToRParenLocOrErr)
6345 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006346
Bruno Riccif49e1ca2018-11-20 16:20:40 +00006347 return ParenListExpr::Create(Importer.getToContext(), *ToLParenLocOrErr,
6348 ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006349}
6350
Balazs Keri3b30d652018-10-19 13:32:20 +00006351ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6352 auto Imp = importSeq(
6353 E->getSubStmt(), E->getType(), E->getLParenLoc(), E->getRParenLoc());
6354 if (!Imp)
6355 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006356
Balazs Keri3b30d652018-10-19 13:32:20 +00006357 CompoundStmt *ToSubStmt;
6358 QualType ToType;
6359 SourceLocation ToLParenLoc, ToRParenLoc;
6360 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006361
Balazs Keri3b30d652018-10-19 13:32:20 +00006362 return new (Importer.getToContext()) StmtExpr(
6363 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006364}
6365
Balazs Keri3b30d652018-10-19 13:32:20 +00006366ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6367 auto Imp = importSeq(
6368 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6369 if (!Imp)
6370 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006371
Balazs Keri3b30d652018-10-19 13:32:20 +00006372 Expr *ToSubExpr;
6373 QualType ToType;
6374 SourceLocation ToOperatorLoc;
6375 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006376
Aaron Ballmana5038552018-01-09 13:07:03 +00006377 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006378 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6379 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006380}
6381
Balazs Keri3b30d652018-10-19 13:32:20 +00006382ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006383ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006384 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6385 if (!Imp)
6386 return Imp.takeError();
6387
6388 QualType ToType;
6389 SourceLocation ToOperatorLoc, ToRParenLoc;
6390 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006391
Douglas Gregord8552cd2010-02-19 01:24:23 +00006392 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006393 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6394 import(E->getArgumentTypeInfo());
6395 if (!ToArgumentTypeInfoOrErr)
6396 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006397
Balazs Keri3b30d652018-10-19 13:32:20 +00006398 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6399 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6400 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006401 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006402
Balazs Keri3b30d652018-10-19 13:32:20 +00006403 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6404 if (!ToArgumentExprOrErr)
6405 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006406
Balazs Keri3b30d652018-10-19 13:32:20 +00006407 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6408 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006409}
6410
Balazs Keri3b30d652018-10-19 13:32:20 +00006411ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6412 auto Imp = importSeq(
6413 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6414 if (!Imp)
6415 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006416
Balazs Keri3b30d652018-10-19 13:32:20 +00006417 Expr *ToLHS, *ToRHS;
6418 QualType ToType;
6419 SourceLocation ToOperatorLoc;
6420 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006421
Balazs Keri3b30d652018-10-19 13:32:20 +00006422 return new (Importer.getToContext()) BinaryOperator(
6423 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6424 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006425}
6426
Balazs Keri3b30d652018-10-19 13:32:20 +00006427ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6428 auto Imp = importSeq(
6429 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6430 E->getRHS(), E->getType());
6431 if (!Imp)
6432 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006433
Balazs Keri3b30d652018-10-19 13:32:20 +00006434 Expr *ToCond, *ToLHS, *ToRHS;
6435 SourceLocation ToQuestionLoc, ToColonLoc;
6436 QualType ToType;
6437 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006438
6439 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006440 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6441 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006442}
6443
Balazs Keri3b30d652018-10-19 13:32:20 +00006444ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006445 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006446 auto Imp = importSeq(
6447 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6448 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6449 if (!Imp)
6450 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006451
Balazs Keri3b30d652018-10-19 13:32:20 +00006452 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6453 OpaqueValueExpr *ToOpaqueValue;
6454 SourceLocation ToQuestionLoc, ToColonLoc;
6455 QualType ToType;
6456 std::tie(
6457 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6458 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006459
6460 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006461 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6462 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6463 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006464}
6465
Balazs Keri3b30d652018-10-19 13:32:20 +00006466ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6467 auto Imp = importSeq(
6468 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6469 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6470 if (!Imp)
6471 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006472
Balazs Keri3b30d652018-10-19 13:32:20 +00006473 SourceLocation ToBeginLoc, ToEndLoc;
6474 TypeSourceInfo *ToQueriedTypeSourceInfo;
6475 Expr *ToDimensionExpression;
6476 QualType ToType;
6477 std::tie(
6478 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6479 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006480
6481 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006482 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6483 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006484}
6485
Balazs Keri3b30d652018-10-19 13:32:20 +00006486ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6487 auto Imp = importSeq(
6488 E->getBeginLoc(), E->getQueriedExpression(), E->getEndLoc(), E->getType());
6489 if (!Imp)
6490 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006491
Balazs Keri3b30d652018-10-19 13:32:20 +00006492 SourceLocation ToBeginLoc, ToEndLoc;
6493 Expr *ToQueriedExpression;
6494 QualType ToType;
6495 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006496
6497 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006498 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6499 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006500}
6501
Balazs Keri3b30d652018-10-19 13:32:20 +00006502ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6503 auto Imp = importSeq(
6504 E->getLocation(), E->getType(), E->getSourceExpr());
6505 if (!Imp)
6506 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006507
Balazs Keri3b30d652018-10-19 13:32:20 +00006508 SourceLocation ToLocation;
6509 QualType ToType;
6510 Expr *ToSourceExpr;
6511 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006512
6513 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006514 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006515}
6516
Balazs Keri3b30d652018-10-19 13:32:20 +00006517ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6518 auto Imp = importSeq(
6519 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6520 if (!Imp)
6521 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006522
Balazs Keri3b30d652018-10-19 13:32:20 +00006523 Expr *ToLHS, *ToRHS;
6524 SourceLocation ToRBracketLoc;
6525 QualType ToType;
6526 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006527
6528 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006529 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6530 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006531}
6532
Balazs Keri3b30d652018-10-19 13:32:20 +00006533ExpectedStmt
6534ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6535 auto Imp = importSeq(
6536 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6537 E->getComputationResultType(), E->getOperatorLoc());
6538 if (!Imp)
6539 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006540
Balazs Keri3b30d652018-10-19 13:32:20 +00006541 Expr *ToLHS, *ToRHS;
6542 QualType ToType, ToComputationLHSType, ToComputationResultType;
6543 SourceLocation ToOperatorLoc;
6544 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6545 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006546
Balazs Keri3b30d652018-10-19 13:32:20 +00006547 return new (Importer.getToContext()) CompoundAssignOperator(
6548 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6549 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6550 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006551}
6552
Balazs Keri3b30d652018-10-19 13:32:20 +00006553Expected<CXXCastPath>
6554ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6555 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006556 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006557 if (auto SpecOrErr = import(*I))
6558 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006559 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006560 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006561 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006562 return Path;
John McCallcf142162010-08-07 06:22:56 +00006563}
6564
Balazs Keri3b30d652018-10-19 13:32:20 +00006565ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6566 ExpectedType ToTypeOrErr = import(E->getType());
6567 if (!ToTypeOrErr)
6568 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006569
Balazs Keri3b30d652018-10-19 13:32:20 +00006570 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6571 if (!ToSubExprOrErr)
6572 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006573
Balazs Keri3b30d652018-10-19 13:32:20 +00006574 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6575 if (!ToBasePathOrErr)
6576 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006577
Balazs Keri3b30d652018-10-19 13:32:20 +00006578 return ImplicitCastExpr::Create(
6579 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6580 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006581}
6582
Balazs Keri3b30d652018-10-19 13:32:20 +00006583ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6584 auto Imp1 = importSeq(
6585 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6586 if (!Imp1)
6587 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006588
Balazs Keri3b30d652018-10-19 13:32:20 +00006589 QualType ToType;
6590 Expr *ToSubExpr;
6591 TypeSourceInfo *ToTypeInfoAsWritten;
6592 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006593
Balazs Keri3b30d652018-10-19 13:32:20 +00006594 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6595 if (!ToBasePathOrErr)
6596 return ToBasePathOrErr.takeError();
6597 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006598
Aleksei Sidorina693b372016-09-28 10:16:56 +00006599 switch (E->getStmtClass()) {
6600 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006601 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006602 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6603 if (!ToLParenLocOrErr)
6604 return ToLParenLocOrErr.takeError();
6605 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6606 if (!ToRParenLocOrErr)
6607 return ToRParenLocOrErr.takeError();
6608 return CStyleCastExpr::Create(
6609 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6610 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6611 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006612 }
6613
6614 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006615 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006616 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6617 if (!ToLParenLocOrErr)
6618 return ToLParenLocOrErr.takeError();
6619 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6620 if (!ToRParenLocOrErr)
6621 return ToRParenLocOrErr.takeError();
6622 return CXXFunctionalCastExpr::Create(
6623 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6624 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6625 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006626 }
6627
6628 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006629 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6630 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6631 if (!ToLParenLocOrErr)
6632 return ToLParenLocOrErr.takeError();
6633 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6634 if (!ToBridgeKeywordLocOrErr)
6635 return ToBridgeKeywordLocOrErr.takeError();
6636 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6637 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6638 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006639 }
6640 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006641 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006642 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006643 }
6644}
6645
Balazs Keri3b30d652018-10-19 13:32:20 +00006646ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
6647 SmallVector<OffsetOfNode, 4> ToNodes;
6648 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
6649 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006650
Balazs Keri3b30d652018-10-19 13:32:20 +00006651 SourceLocation ToBeginLoc, ToEndLoc;
6652 if (FromNode.getKind() != OffsetOfNode::Base) {
6653 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
6654 if (!Imp)
6655 return Imp.takeError();
6656 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
6657 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00006658
Balazs Keri3b30d652018-10-19 13:32:20 +00006659 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00006660 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00006661 ToNodes.push_back(
6662 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006663 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006664 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006665 auto ToBSOrErr = import(FromNode.getBase());
6666 if (!ToBSOrErr)
6667 return ToBSOrErr.takeError();
6668 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006669 break;
6670 }
6671 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006672 auto ToFieldOrErr = import(FromNode.getField());
6673 if (!ToFieldOrErr)
6674 return ToFieldOrErr.takeError();
6675 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006676 break;
6677 }
6678 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006679 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
6680 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006681 break;
6682 }
6683 }
6684 }
6685
Balazs Keri3b30d652018-10-19 13:32:20 +00006686 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
6687 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
6688 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
6689 if (!ToIndexExprOrErr)
6690 return ToIndexExprOrErr.takeError();
6691 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006692 }
6693
Balazs Keri3b30d652018-10-19 13:32:20 +00006694 auto Imp = importSeq(
6695 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
6696 E->getRParenLoc());
6697 if (!Imp)
6698 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006699
Balazs Keri3b30d652018-10-19 13:32:20 +00006700 QualType ToType;
6701 TypeSourceInfo *ToTypeSourceInfo;
6702 SourceLocation ToOperatorLoc, ToRParenLoc;
6703 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
6704
6705 return OffsetOfExpr::Create(
6706 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
6707 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006708}
6709
Balazs Keri3b30d652018-10-19 13:32:20 +00006710ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6711 auto Imp = importSeq(
6712 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
6713 if (!Imp)
6714 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006715
Balazs Keri3b30d652018-10-19 13:32:20 +00006716 QualType ToType;
6717 Expr *ToOperand;
6718 SourceLocation ToBeginLoc, ToEndLoc;
6719 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006720
Balazs Keri3b30d652018-10-19 13:32:20 +00006721 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006722 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00006723 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006724 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006725 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006726
Balazs Keri3b30d652018-10-19 13:32:20 +00006727 return new (Importer.getToContext()) CXXNoexceptExpr(
6728 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006729}
6730
Balazs Keri3b30d652018-10-19 13:32:20 +00006731ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6732 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
6733 if (!Imp)
6734 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006735
Balazs Keri3b30d652018-10-19 13:32:20 +00006736 Expr *ToSubExpr;
6737 QualType ToType;
6738 SourceLocation ToThrowLoc;
6739 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006740
6741 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006742 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006743}
6744
Balazs Keri3b30d652018-10-19 13:32:20 +00006745ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6746 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
6747 if (!ToUsedLocOrErr)
6748 return ToUsedLocOrErr.takeError();
6749
6750 auto ToParamOrErr = import(E->getParam());
6751 if (!ToParamOrErr)
6752 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006753
6754 return CXXDefaultArgExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006755 Importer.getToContext(), *ToUsedLocOrErr, *ToParamOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006756}
6757
Balazs Keri3b30d652018-10-19 13:32:20 +00006758ExpectedStmt
6759ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6760 auto Imp = importSeq(
6761 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
6762 if (!Imp)
6763 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006764
Balazs Keri3b30d652018-10-19 13:32:20 +00006765 QualType ToType;
6766 TypeSourceInfo *ToTypeSourceInfo;
6767 SourceLocation ToRParenLoc;
6768 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006769
6770 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006771 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006772}
6773
Balazs Keri3b30d652018-10-19 13:32:20 +00006774ExpectedStmt
6775ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6776 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6777 if (!ToSubExprOrErr)
6778 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006779
Balazs Keri3b30d652018-10-19 13:32:20 +00006780 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
6781 if (!ToDtorOrErr)
6782 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006783
6784 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00006785 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
6786 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006787}
6788
Balazs Keri3b30d652018-10-19 13:32:20 +00006789ExpectedStmt
6790ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
6791 auto Imp = importSeq(
6792 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
6793 E->getParenOrBraceRange());
6794 if (!Imp)
6795 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006796
Balazs Keri3b30d652018-10-19 13:32:20 +00006797 CXXConstructorDecl *ToConstructor;
6798 QualType ToType;
6799 TypeSourceInfo *ToTypeSourceInfo;
6800 SourceRange ToParenOrBraceRange;
6801 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006802
Balazs Keri3b30d652018-10-19 13:32:20 +00006803 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
6804 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6805 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006806
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00006807 return CXXTemporaryObjectExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006808 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
6809 ToParenOrBraceRange, E->hadMultipleCandidates(),
6810 E->isListInitialization(), E->isStdInitListInitialization(),
6811 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006812}
6813
Balazs Keri3b30d652018-10-19 13:32:20 +00006814ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00006815ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006816 auto Imp = importSeq(
6817 E->getType(), E->GetTemporaryExpr(), E->getExtendingDecl());
6818 if (!Imp)
6819 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006820
Balazs Keri3b30d652018-10-19 13:32:20 +00006821 QualType ToType;
6822 Expr *ToTemporaryExpr;
6823 const ValueDecl *ToExtendingDecl;
6824 std::tie(ToType, ToTemporaryExpr, ToExtendingDecl) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006825
6826 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006827 ToType, ToTemporaryExpr, E->isBoundToLvalueReference());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006828
6829 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
Balazs Keri3b30d652018-10-19 13:32:20 +00006830 ToMTE->setExtendingDecl(ToExtendingDecl, E->getManglingNumber());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006831 return ToMTE;
6832}
6833
Balazs Keri3b30d652018-10-19 13:32:20 +00006834ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6835 auto Imp = importSeq(
6836 E->getType(), E->getPattern(), E->getEllipsisLoc());
6837 if (!Imp)
6838 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00006839
Balazs Keri3b30d652018-10-19 13:32:20 +00006840 QualType ToType;
6841 Expr *ToPattern;
6842 SourceLocation ToEllipsisLoc;
6843 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00006844
6845 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006846 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00006847}
6848
Balazs Keri3b30d652018-10-19 13:32:20 +00006849ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6850 auto Imp = importSeq(
6851 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
6852 if (!Imp)
6853 return Imp.takeError();
6854
6855 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
6856 NamedDecl *ToPack;
6857 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006858
6859 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006860 if (!E->isValueDependent())
6861 Length = E->getPackLength();
6862
Balazs Keri3b30d652018-10-19 13:32:20 +00006863 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006864 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006865 if (Error Err = ImportTemplateArguments(
6866 E->getPartialArguments().data(),
6867 E->getPartialArguments().size(),
6868 ToPartialArguments))
6869 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006870 }
6871
6872 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006873 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
6874 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006875}
6876
Aleksei Sidorina693b372016-09-28 10:16:56 +00006877
Balazs Keri3b30d652018-10-19 13:32:20 +00006878ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
6879 auto Imp = importSeq(
6880 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
6881 E->getArraySize(), E->getInitializer(), E->getType(),
6882 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
6883 E->getDirectInitRange());
6884 if (!Imp)
6885 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006886
Balazs Keri3b30d652018-10-19 13:32:20 +00006887 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
6888 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
Richard Smithb9fb1212019-05-06 03:47:15 +00006889 Optional<Expr *> ToArraySize;
6890 Expr *ToInitializer;
Balazs Keri3b30d652018-10-19 13:32:20 +00006891 QualType ToType;
6892 TypeSourceInfo *ToAllocatedTypeSourceInfo;
6893 std::tie(
6894 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
6895 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006896
Balazs Keri3b30d652018-10-19 13:32:20 +00006897 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
6898 if (Error Err =
6899 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
6900 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006901
Bruno Ricci9b6dfac2019-01-07 15:04:45 +00006902 return CXXNewExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006903 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
6904 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
6905 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
6906 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
6907 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006908}
6909
Balazs Keri3b30d652018-10-19 13:32:20 +00006910ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6911 auto Imp = importSeq(
6912 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
6913 if (!Imp)
6914 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006915
Balazs Keri3b30d652018-10-19 13:32:20 +00006916 QualType ToType;
6917 FunctionDecl *ToOperatorDelete;
6918 Expr *ToArgument;
6919 SourceLocation ToBeginLoc;
6920 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006921
6922 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006923 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
6924 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
6925 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00006926}
6927
Balazs Keri3b30d652018-10-19 13:32:20 +00006928ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6929 auto Imp = importSeq(
6930 E->getType(), E->getLocation(), E->getConstructor(),
6931 E->getParenOrBraceRange());
6932 if (!Imp)
6933 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006934
Balazs Keri3b30d652018-10-19 13:32:20 +00006935 QualType ToType;
6936 SourceLocation ToLocation;
6937 CXXConstructorDecl *ToConstructor;
6938 SourceRange ToParenOrBraceRange;
6939 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00006940
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006941 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00006942 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6943 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00006944
Balazs Keri3b30d652018-10-19 13:32:20 +00006945 return CXXConstructExpr::Create(
6946 Importer.getToContext(), ToType, ToLocation, ToConstructor,
6947 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
6948 E->isListInitialization(), E->isStdInitListInitialization(),
6949 E->requiresZeroInitialization(), E->getConstructionKind(),
6950 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00006951}
6952
Balazs Keri3b30d652018-10-19 13:32:20 +00006953ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
6954 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6955 if (!ToSubExprOrErr)
6956 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006957
Balazs Keri3b30d652018-10-19 13:32:20 +00006958 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
6959 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
6960 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006961
Balazs Keri3b30d652018-10-19 13:32:20 +00006962 return ExprWithCleanups::Create(
6963 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
6964 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006965}
6966
Balazs Keri3b30d652018-10-19 13:32:20 +00006967ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6968 auto Imp = importSeq(
6969 E->getCallee(), E->getType(), E->getRParenLoc());
6970 if (!Imp)
6971 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00006972
Balazs Keri3b30d652018-10-19 13:32:20 +00006973 Expr *ToCallee;
6974 QualType ToType;
6975 SourceLocation ToRParenLoc;
6976 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006977
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006978 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00006979 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6980 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00006981
Bruno Riccic5885cf2018-12-21 15:20:32 +00006982 return CXXMemberCallExpr::Create(Importer.getToContext(), ToCallee, ToArgs,
6983 ToType, E->getValueKind(), ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00006984}
6985
Balazs Keri3b30d652018-10-19 13:32:20 +00006986ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6987 ExpectedType ToTypeOrErr = import(E->getType());
6988 if (!ToTypeOrErr)
6989 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00006990
Balazs Keri3b30d652018-10-19 13:32:20 +00006991 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6992 if (!ToLocationOrErr)
6993 return ToLocationOrErr.takeError();
6994
6995 return new (Importer.getToContext()) CXXThisExpr(
6996 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00006997}
6998
Balazs Keri3b30d652018-10-19 13:32:20 +00006999ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
7000 ExpectedType ToTypeOrErr = import(E->getType());
7001 if (!ToTypeOrErr)
7002 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007003
Balazs Keri3b30d652018-10-19 13:32:20 +00007004 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7005 if (!ToLocationOrErr)
7006 return ToLocationOrErr.takeError();
7007
7008 return new (Importer.getToContext()) CXXBoolLiteralExpr(
7009 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00007010}
7011
Balazs Keri3b30d652018-10-19 13:32:20 +00007012ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7013 auto Imp1 = importSeq(
7014 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7015 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7016 if (!Imp1)
7017 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007018
Balazs Keri3b30d652018-10-19 13:32:20 +00007019 Expr *ToBase;
7020 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7021 NestedNameSpecifierLoc ToQualifierLoc;
7022 ValueDecl *ToMemberDecl;
7023 QualType ToType;
7024 std::tie(
7025 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7026 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007027
Balazs Keri3b30d652018-10-19 13:32:20 +00007028 auto Imp2 = importSeq(
7029 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7030 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7031 if (!Imp2)
7032 return Imp2.takeError();
7033 NamedDecl *ToDecl;
7034 DeclarationName ToName;
7035 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7036 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007037
7038 DeclAccessPair ToFoundDecl =
7039 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007040
Balazs Keri3b30d652018-10-19 13:32:20 +00007041 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007042
Gabor Marton5caba302019-03-07 13:38:20 +00007043 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007044 if (E->hasExplicitTemplateArgs()) {
Gabor Marton5caba302019-03-07 13:38:20 +00007045 if (Error Err =
7046 ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
7047 E->template_arguments(), ToTAInfo))
7048 return std::move(Err);
7049 ResInfo = &ToTAInfo;
Sean Callanan59721b32015-04-28 18:41:46 +00007050 }
7051
Balazs Keri3b30d652018-10-19 13:32:20 +00007052 return MemberExpr::Create(
7053 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7054 ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl, ToFoundDecl,
Gabor Marton5caba302019-03-07 13:38:20 +00007055 ToMemberNameInfo, ResInfo, ToType, E->getValueKind(), E->getObjectKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007056}
7057
Balazs Keri3b30d652018-10-19 13:32:20 +00007058ExpectedStmt
7059ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7060 auto Imp = importSeq(
7061 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7062 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7063 if (!Imp)
7064 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007065
Balazs Keri3b30d652018-10-19 13:32:20 +00007066 Expr *ToBase;
7067 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7068 NestedNameSpecifierLoc ToQualifierLoc;
7069 TypeSourceInfo *ToScopeTypeInfo;
7070 std::tie(
7071 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7072 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007073
7074 PseudoDestructorTypeStorage Storage;
7075 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7076 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007077 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7078 if (!ToDestroyedTypeLocOrErr)
7079 return ToDestroyedTypeLocOrErr.takeError();
7080 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007081 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007082 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7083 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7084 else
7085 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007086 }
7087
7088 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007089 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7090 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007091}
7092
Balazs Keri3b30d652018-10-19 13:32:20 +00007093ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007094 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007095 auto Imp = importSeq(
7096 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7097 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7098 if (!Imp)
7099 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007100
Balazs Keri3b30d652018-10-19 13:32:20 +00007101 QualType ToType;
7102 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7103 NestedNameSpecifierLoc ToQualifierLoc;
7104 NamedDecl *ToFirstQualifierFoundInScope;
7105 std::tie(
7106 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7107 ToFirstQualifierFoundInScope) = *Imp;
7108
7109 Expr *ToBase = nullptr;
7110 if (!E->isImplicitAccess()) {
7111 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7112 ToBase = *ToBaseOrErr;
7113 else
7114 return ToBaseOrErr.takeError();
7115 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007116
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007117 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007118 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007119 if (Error Err = ImportTemplateArgumentListInfo(
7120 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7121 ToTAInfo))
7122 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007123 ResInfo = &ToTAInfo;
7124 }
7125
Balazs Keri3b30d652018-10-19 13:32:20 +00007126 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7127 if (!ToMemberNameInfoOrErr)
7128 return ToMemberNameInfoOrErr.takeError();
7129 DeclarationNameInfo ToMemberNameInfo(
7130 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007131 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007132 if (Error Err = ImportDeclarationNameLoc(
7133 E->getMemberNameInfo(), ToMemberNameInfo))
7134 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007135
7136 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007137 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7138 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7139 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007140}
7141
Balazs Keri3b30d652018-10-19 13:32:20 +00007142ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007143ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007144 auto Imp = importSeq(
7145 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
7146 E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7147 if (!Imp)
7148 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007149
Balazs Keri3b30d652018-10-19 13:32:20 +00007150 NestedNameSpecifierLoc ToQualifierLoc;
7151 SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
7152 DeclarationName ToDeclName;
7153 std::tie(
7154 ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
7155 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007156
Balazs Keri3b30d652018-10-19 13:32:20 +00007157 DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
7158 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7159 return std::move(Err);
7160
7161 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007162 TemplateArgumentListInfo *ResInfo = nullptr;
7163 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007164 if (Error Err =
7165 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7166 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007167 ResInfo = &ToTAInfo;
7168 }
7169
7170 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007171 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7172 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007173}
7174
Balazs Keri3b30d652018-10-19 13:32:20 +00007175ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7176 CXXUnresolvedConstructExpr *E) {
7177 auto Imp = importSeq(
7178 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7179 if (!Imp)
7180 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007181
Balazs Keri3b30d652018-10-19 13:32:20 +00007182 SourceLocation ToLParenLoc, ToRParenLoc;
7183 TypeSourceInfo *ToTypeSourceInfo;
7184 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7185
7186 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7187 if (Error Err =
7188 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7189 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007190
7191 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007192 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7193 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007194}
7195
Balazs Keri3b30d652018-10-19 13:32:20 +00007196ExpectedStmt
7197ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7198 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7199 if (!ToNamingClassOrErr)
7200 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007201
Balazs Keri3b30d652018-10-19 13:32:20 +00007202 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7203 if (!ToQualifierLocOrErr)
7204 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007205
Balazs Keri3b30d652018-10-19 13:32:20 +00007206 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7207 if (!ToNameInfoOrErr)
7208 return ToNameInfoOrErr.takeError();
7209 DeclarationNameInfo ToNameInfo(
7210 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007211 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007212 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7213 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007214
7215 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007216 for (auto *D : E->decls())
7217 if (auto ToDOrErr = import(D))
7218 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007219 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007220 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007221
Balazs Keri3b30d652018-10-19 13:32:20 +00007222 if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
7223 TemplateArgumentListInfo ToTAInfo;
7224 if (Error Err = ImportTemplateArgumentListInfo(
7225 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7226 ToTAInfo))
7227 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007228
Balazs Keri3b30d652018-10-19 13:32:20 +00007229 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7230 if (!ToTemplateKeywordLocOrErr)
7231 return ToTemplateKeywordLocOrErr.takeError();
7232
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007233 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007234 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7235 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7236 ToDecls.begin(), ToDecls.end());
7237 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007238
7239 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007240 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7241 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7242 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007243}
7244
Balazs Keri3b30d652018-10-19 13:32:20 +00007245ExpectedStmt
7246ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7247 auto Imp1 = importSeq(
7248 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7249 E->getTemplateKeywordLoc());
7250 if (!Imp1)
7251 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007252
Balazs Keri3b30d652018-10-19 13:32:20 +00007253 QualType ToType;
7254 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7255 NestedNameSpecifierLoc ToQualifierLoc;
7256 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7257
7258 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7259 if (!Imp2)
7260 return Imp2.takeError();
7261 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7262 // Import additional name location/type info.
7263 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7264 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007265
7266 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007267 for (Decl *D : E->decls())
7268 if (auto ToDOrErr = import(D))
7269 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007270 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007271 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007272
7273 TemplateArgumentListInfo ToTAInfo;
7274 TemplateArgumentListInfo *ResInfo = nullptr;
7275 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007276 if (Error Err =
7277 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7278 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007279 ResInfo = &ToTAInfo;
7280 }
7281
Balazs Keri3b30d652018-10-19 13:32:20 +00007282 Expr *ToBase = nullptr;
7283 if (!E->isImplicitAccess()) {
7284 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7285 ToBase = *ToBaseOrErr;
7286 else
7287 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007288 }
7289
7290 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007291 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7292 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7293 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007294}
7295
Balazs Keri3b30d652018-10-19 13:32:20 +00007296ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7297 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7298 if (!Imp)
7299 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007300
Balazs Keri3b30d652018-10-19 13:32:20 +00007301 Expr *ToCallee;
7302 QualType ToType;
7303 SourceLocation ToRParenLoc;
7304 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007305
7306 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007307 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7308 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7309 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007310
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007311 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
Bruno Riccic5885cf2018-12-21 15:20:32 +00007312 return CXXOperatorCallExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007313 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
Eric Fiselier5cdc2cd2018-12-12 21:50:55 +00007314 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures(),
7315 OCE->getADLCallKind());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007316 }
7317
Bruno Riccic5885cf2018-12-21 15:20:32 +00007318 return CallExpr::Create(Importer.getToContext(), ToCallee, ToArgs, ToType,
7319 E->getValueKind(), ToRParenLoc, /*MinNumArgs=*/0,
7320 E->getADLCallKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007321}
7322
Balazs Keri3b30d652018-10-19 13:32:20 +00007323ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7324 CXXRecordDecl *FromClass = E->getLambdaClass();
7325 auto ToClassOrErr = import(FromClass);
7326 if (!ToClassOrErr)
7327 return ToClassOrErr.takeError();
7328 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007329
7330 // NOTE: lambda classes are created with BeingDefined flag set up.
7331 // It means that ImportDefinition doesn't work for them and we should fill it
7332 // manually.
Gabor Marton302f3002019-02-15 12:04:05 +00007333 if (ToClass->isBeingDefined())
7334 if (Error Err = ImportDeclContext(FromClass, /*ForceImport = */ true))
7335 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007336
Balazs Keri3b30d652018-10-19 13:32:20 +00007337 auto ToCallOpOrErr = import(E->getCallOperator());
7338 if (!ToCallOpOrErr)
7339 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007340
7341 ToClass->completeDefinition();
7342
Balazs Keri3b30d652018-10-19 13:32:20 +00007343 SmallVector<LambdaCapture, 8> ToCaptures;
7344 ToCaptures.reserve(E->capture_size());
7345 for (const auto &FromCapture : E->captures()) {
7346 if (auto ToCaptureOrErr = import(FromCapture))
7347 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007348 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007349 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007350 }
7351
Balazs Keri3b30d652018-10-19 13:32:20 +00007352 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7353 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7354 return std::move(Err);
7355
7356 auto Imp = importSeq(
7357 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7358 if (!Imp)
7359 return Imp.takeError();
7360
7361 SourceRange ToIntroducerRange;
7362 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7363 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007364
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007365 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007366 Importer.getToContext(), ToClass, ToIntroducerRange,
7367 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7368 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7369 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007370}
7371
Sean Callanan8bca9962016-03-28 21:43:01 +00007372
Balazs Keri3b30d652018-10-19 13:32:20 +00007373ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7374 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7375 if (!Imp)
7376 return Imp.takeError();
7377
7378 SourceLocation ToLBraceLoc, ToRBraceLoc;
7379 QualType ToType;
7380 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7381
7382 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7383 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7384 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007385
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007386 ASTContext &ToCtx = Importer.getToContext();
7387 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007388 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7389 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007390
Balazs Keri3b30d652018-10-19 13:32:20 +00007391 if (E->hasArrayFiller()) {
7392 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7393 To->setArrayFiller(*ToFillerOrErr);
7394 else
7395 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007396 }
7397
Balazs Keri3b30d652018-10-19 13:32:20 +00007398 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7399 if (auto ToFDOrErr = import(FromFD))
7400 To->setInitializedFieldInUnion(*ToFDOrErr);
7401 else
7402 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007403 }
7404
Balazs Keri3b30d652018-10-19 13:32:20 +00007405 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7406 if (auto ToSyntFormOrErr = import(SyntForm))
7407 To->setSyntacticForm(*ToSyntFormOrErr);
7408 else
7409 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007410 }
7411
Gabor Martona20ce602018-09-03 13:10:53 +00007412 // Copy InitListExprBitfields, which are not handled in the ctor of
7413 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007414 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007415
7416 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007417}
7418
Balazs Keri3b30d652018-10-19 13:32:20 +00007419ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007420 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007421 ExpectedType ToTypeOrErr = import(E->getType());
7422 if (!ToTypeOrErr)
7423 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007424
Balazs Keri3b30d652018-10-19 13:32:20 +00007425 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7426 if (!ToSubExprOrErr)
7427 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007428
Balazs Keri3b30d652018-10-19 13:32:20 +00007429 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7430 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007431}
7432
Balazs Keri3b30d652018-10-19 13:32:20 +00007433ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007434 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007435 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7436 if (!Imp)
7437 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007438
Balazs Keri3b30d652018-10-19 13:32:20 +00007439 SourceLocation ToLocation;
7440 QualType ToType;
7441 CXXConstructorDecl *ToConstructor;
7442 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007443
7444 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007445 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7446 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007447}
7448
Balazs Keri3b30d652018-10-19 13:32:20 +00007449ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7450 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7451 if (!Imp)
7452 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007453
Balazs Keri3b30d652018-10-19 13:32:20 +00007454 QualType ToType;
7455 Expr *ToCommonExpr, *ToSubExpr;
7456 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007457
Balazs Keri3b30d652018-10-19 13:32:20 +00007458 return new (Importer.getToContext()) ArrayInitLoopExpr(
7459 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007460}
7461
Balazs Keri3b30d652018-10-19 13:32:20 +00007462ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7463 ExpectedType ToTypeOrErr = import(E->getType());
7464 if (!ToTypeOrErr)
7465 return ToTypeOrErr.takeError();
7466 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007467}
7468
Balazs Keri3b30d652018-10-19 13:32:20 +00007469ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7470 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7471 if (!ToBeginLocOrErr)
7472 return ToBeginLocOrErr.takeError();
7473
7474 auto ToFieldOrErr = import(E->getField());
7475 if (!ToFieldOrErr)
7476 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007477
7478 return CXXDefaultInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007479 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007480}
7481
Balazs Keri3b30d652018-10-19 13:32:20 +00007482ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7483 auto Imp = importSeq(
7484 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7485 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7486 if (!Imp)
7487 return Imp.takeError();
7488
7489 QualType ToType;
7490 Expr *ToSubExpr;
7491 TypeSourceInfo *ToTypeInfoAsWritten;
7492 SourceLocation ToOperatorLoc, ToRParenLoc;
7493 SourceRange ToAngleBrackets;
7494 std::tie(
7495 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7496 ToAngleBrackets) = *Imp;
7497
Sean Callanandd2c1742016-05-16 20:48:03 +00007498 ExprValueKind VK = E->getValueKind();
7499 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007500 auto ToBasePathOrErr = ImportCastPath(E);
7501 if (!ToBasePathOrErr)
7502 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007503
Sean Callanandd2c1742016-05-16 20:48:03 +00007504 if (isa<CXXStaticCastExpr>(E)) {
7505 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007506 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7507 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007508 } else if (isa<CXXDynamicCastExpr>(E)) {
7509 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007510 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7511 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007512 } else if (isa<CXXReinterpretCastExpr>(E)) {
7513 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007514 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7515 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007516 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007517 return CXXConstCastExpr::Create(
7518 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7519 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007520 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007521 llvm_unreachable("Unknown cast type");
7522 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007523 }
7524}
7525
Balazs Keri3b30d652018-10-19 13:32:20 +00007526ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007527 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007528 auto Imp = importSeq(
7529 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7530 if (!Imp)
7531 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007532
Balazs Keri3b30d652018-10-19 13:32:20 +00007533 QualType ToType;
7534 SourceLocation ToExprLoc;
7535 NonTypeTemplateParmDecl *ToParameter;
7536 Expr *ToReplacement;
7537 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007538
7539 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007540 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007541}
7542
Balazs Keri3b30d652018-10-19 13:32:20 +00007543ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7544 auto Imp = importSeq(
7545 E->getType(), E->getBeginLoc(), E->getEndLoc());
7546 if (!Imp)
7547 return Imp.takeError();
7548
7549 QualType ToType;
7550 SourceLocation ToBeginLoc, ToEndLoc;
7551 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007552
7553 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007554 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7555 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007556
7557 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7558 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007559 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007560
7561 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007562 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7563 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007564}
7565
Balazs Keri3b30d652018-10-19 13:32:20 +00007566ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7567 ExpectedType ToTypeOrErr = import(E->getType());
7568 if (!ToTypeOrErr)
7569 return ToTypeOrErr.takeError();
7570
7571 auto ToSourceRangeOrErr = import(E->getSourceRange());
7572 if (!ToSourceRangeOrErr)
7573 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007574
7575 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007576 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7577 return new (Importer.getToContext()) CXXTypeidExpr(
7578 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7579 else
7580 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007581 }
7582
Balazs Keri3b30d652018-10-19 13:32:20 +00007583 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7584 if (!ToExprOperandOrErr)
7585 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007586
Balazs Keri3b30d652018-10-19 13:32:20 +00007587 return new (Importer.getToContext()) CXXTypeidExpr(
7588 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007589}
7590
Lang Hames19e07e12017-06-20 21:06:00 +00007591void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
7592 CXXMethodDecl *FromMethod) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007593 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7594 if (auto ImportedOrErr = import(FromOverriddenMethod))
7595 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7596 (*ImportedOrErr)->getCanonicalDecl()));
7597 else
7598 consumeError(ImportedOrErr.takeError());
7599 }
Lang Hames19e07e12017-06-20 21:06:00 +00007600}
7601
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007602ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00007603 ASTContext &FromContext, FileManager &FromFileManager,
Gabor Marton54058b52018-12-17 13:53:12 +00007604 bool MinimalImport,
7605 ASTImporterLookupTable *LookupTable)
7606 : LookupTable(LookupTable), ToContext(ToContext), FromContext(FromContext),
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007607 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
7608 Minimal(MinimalImport) {
Gabor Marton54058b52018-12-17 13:53:12 +00007609
7610 ImportedDecls[FromContext.getTranslationUnitDecl()] =
7611 ToContext.getTranslationUnitDecl();
Douglas Gregor62d311f2010-02-09 19:21:46 +00007612}
7613
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007614ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007615
Gabor Marton54058b52018-12-17 13:53:12 +00007616Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) {
7617 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
7618 "Try to get field index for non-field.");
7619
7620 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
7621 if (!Owner)
7622 return None;
7623
7624 unsigned Index = 0;
7625 for (const auto *D : Owner->decls()) {
7626 if (D == F)
7627 return Index;
7628
7629 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
7630 ++Index;
7631 }
7632
7633 llvm_unreachable("Field was not found in its parent context.");
7634
7635 return None;
7636}
7637
7638ASTImporter::FoundDeclsTy
7639ASTImporter::findDeclsInToCtx(DeclContext *DC, DeclarationName Name) {
7640 // We search in the redecl context because of transparent contexts.
7641 // E.g. a simple C language enum is a transparent context:
7642 // enum E { A, B };
7643 // Now if we had a global variable in the TU
7644 // int A;
7645 // then the enum constant 'A' and the variable 'A' violates ODR.
7646 // We can diagnose this only if we search in the redecl context.
7647 DeclContext *ReDC = DC->getRedeclContext();
7648 if (LookupTable) {
7649 ASTImporterLookupTable::LookupResult LookupResult =
7650 LookupTable->lookup(ReDC, Name);
7651 return FoundDeclsTy(LookupResult.begin(), LookupResult.end());
7652 } else {
7653 // FIXME Can we remove this kind of lookup?
7654 // Or lldb really needs this C/C++ lookup?
7655 FoundDeclsTy Result;
7656 ReDC->localUncachedLookup(Name, Result);
7657 return Result;
7658 }
7659}
7660
7661void ASTImporter::AddToLookupTable(Decl *ToD) {
7662 if (LookupTable)
7663 if (auto *ToND = dyn_cast<NamedDecl>(ToD))
7664 LookupTable->add(ToND);
7665}
7666
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007667Expected<Decl *> ASTImporter::ImportImpl(Decl *FromD) {
7668 // Import the decl using ASTNodeImporter.
7669 ASTNodeImporter Importer(*this);
7670 return Importer.Visit(FromD);
7671}
7672
7673void ASTImporter::RegisterImportedDecl(Decl *FromD, Decl *ToD) {
7674 MapImported(FromD, ToD);
7675 AddToLookupTable(ToD);
7676}
7677
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007678Expected<QualType> ASTImporter::Import_New(QualType FromT) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007679 if (FromT.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007680 return QualType{};
John McCall424cec92011-01-19 06:33:43 +00007681
Balazs Keri3b30d652018-10-19 13:32:20 +00007682 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007683
7684 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00007685 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00007686 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007687 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00007688 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00007689
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007690 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00007691 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007692 ExpectedType ToTOrErr = Importer.Visit(FromTy);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007693 if (!ToTOrErr)
7694 return ToTOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007695
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007696 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007697 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007698
Balazs Keri3b30d652018-10-19 13:32:20 +00007699 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00007700}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007701QualType ASTImporter::Import(QualType From) {
7702 llvm::Expected<QualType> To = Import_New(From);
7703 if (To)
7704 return *To;
7705 else
7706 llvm::consumeError(To.takeError());
7707 return {};
7708}
Douglas Gregor96e578d2010-02-05 17:54:41 +00007709
Balazs Keri4a3d7582018-11-27 18:36:31 +00007710Expected<TypeSourceInfo *> ASTImporter::Import_New(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007711 if (!FromTSI)
7712 return FromTSI;
7713
7714 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00007715 // on the type and a single location. Implement a real version of this.
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007716 ExpectedType TOrErr = Import_New(FromTSI->getType());
7717 if (!TOrErr)
7718 return TOrErr.takeError();
7719 ExpectedSLoc BeginLocOrErr = Import_New(FromTSI->getTypeLoc().getBeginLoc());
7720 if (!BeginLocOrErr)
7721 return BeginLocOrErr.takeError();
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007722
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007723 return ToContext.getTrivialTypeSourceInfo(*TOrErr, *BeginLocOrErr);
7724}
7725TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *From) {
7726 llvm::Expected<TypeSourceInfo *> To = Import_New(From);
7727 if (To)
7728 return *To;
7729 else
7730 llvm::consumeError(To.takeError());
7731 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007732}
7733
Balazs Keri4a3d7582018-11-27 18:36:31 +00007734Expected<Attr *> ASTImporter::Import_New(const Attr *FromAttr) {
Davide Italianofaee83d2018-11-28 19:15:23 +00007735 Attr *ToAttr = FromAttr->clone(ToContext);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007736 if (auto ToRangeOrErr = Import_New(FromAttr->getRange()))
7737 ToAttr->setRange(*ToRangeOrErr);
7738 else
7739 return ToRangeOrErr.takeError();
7740
Davide Italianofaee83d2018-11-28 19:15:23 +00007741 return ToAttr;
Balazs Kerideaf7ab2018-11-28 13:21:26 +00007742}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007743Attr *ASTImporter::Import(const Attr *From) {
7744 llvm::Expected<Attr *> To = Import_New(From);
7745 if (To)
7746 return *To;
7747 else
7748 llvm::consumeError(To.takeError());
7749 return nullptr;
7750}
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007751
Gabor Martonbe77a982018-12-12 11:22:55 +00007752Decl *ASTImporter::GetAlreadyImportedOrNull(const Decl *FromD) const {
7753 auto Pos = ImportedDecls.find(FromD);
7754 if (Pos != ImportedDecls.end())
7755 return Pos->second;
7756 else
Sean Callanan59721b32015-04-28 18:41:46 +00007757 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007758}
7759
Gabor Marton458d1452019-02-14 13:07:03 +00007760TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) {
7761 auto FromDPos = ImportedFromDecls.find(ToD);
7762 if (FromDPos == ImportedFromDecls.end())
7763 return nullptr;
7764 return FromDPos->second->getTranslationUnitDecl();
7765}
7766
Balazs Keri4a3d7582018-11-27 18:36:31 +00007767Expected<Decl *> ASTImporter::Import_New(Decl *FromD) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007768 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007769 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007770
Douglas Gregord451ea92011-07-29 23:31:30 +00007771
Gabor Marton26f72a92018-07-12 09:42:05 +00007772 // Check whether we've already imported this declaration.
7773 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7774 if (ToD) {
7775 // If FromD has some updated flags after last import, apply it
7776 updateFlags(FromD, ToD);
Douglas Gregord451ea92011-07-29 23:31:30 +00007777 return ToD;
7778 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007779
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007780 // Import the declaration.
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007781 ExpectedDecl ToDOrErr = ImportImpl(FromD);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007782 if (!ToDOrErr)
7783 return ToDOrErr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007784 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007785
Gabor Marton7f8c4002019-03-19 13:34:10 +00007786 // FIXME Use getImportDeclErrorIfAny() here (and return with the error) once
7787 // the error handling is finished in GetImportedOrCreateSpecialDecl().
7788 if (!ToD) {
7789 return nullptr;
7790 }
7791
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007792 // Make sure that ImportImpl registered the imported decl.
7793 assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?");
7794
Gabor Marton54058b52018-12-17 13:53:12 +00007795 // Once the decl is connected to the existing declarations, i.e. when the
7796 // redecl chain is properly set then we populate the lookup again.
7797 // This way the primary context will be able to find all decls.
7798 AddToLookupTable(ToD);
7799
Gabor Marton26f72a92018-07-12 09:42:05 +00007800 // Notify subclasses.
7801 Imported(FromD, ToD);
7802
Gabor Martonac3a5d62018-09-17 12:04:52 +00007803 updateFlags(FromD, ToD);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007804 return ToDOrErr;
7805}
7806Decl *ASTImporter::Import(Decl *From) {
7807 llvm::Expected<Decl *> To = Import_New(From);
7808 if (To)
7809 return *To;
7810 else
7811 llvm::consumeError(To.takeError());
7812 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007813}
7814
Balazs Keri3b30d652018-10-19 13:32:20 +00007815Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007816 if (!FromDC)
7817 return FromDC;
7818
Balazs Keria1f6b102019-04-08 13:59:15 +00007819 ExpectedDecl ToDCOrErr = Import_New(cast<Decl>(FromDC));
7820 if (!ToDCOrErr)
7821 return ToDCOrErr.takeError();
7822 auto *ToDC = cast<DeclContext>(*ToDCOrErr);
Craig Topper36250ad2014-05-12 05:36:57 +00007823
Fangrui Song6907ce22018-07-30 19:24:48 +00007824 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00007825 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007826 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
7827 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007828 if (ToRecord->isCompleteDefinition()) {
7829 // Do nothing.
7830 } else if (FromRecord->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007831 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7832 FromRecord, ToRecord, ASTNodeImporter::IDK_Basic))
7833 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007834 } else {
7835 CompleteDecl(ToRecord);
7836 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007837 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
7838 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007839 if (ToEnum->isCompleteDefinition()) {
7840 // Do nothing.
7841 } else if (FromEnum->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007842 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7843 FromEnum, ToEnum, ASTNodeImporter::IDK_Basic))
7844 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007845 } else {
7846 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00007847 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007848 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
7849 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007850 if (ToClass->getDefinition()) {
7851 // Do nothing.
7852 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007853 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7854 FromDef, ToClass, ASTNodeImporter::IDK_Basic))
7855 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007856 } else {
7857 CompleteDecl(ToClass);
7858 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007859 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
7860 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007861 if (ToProto->getDefinition()) {
7862 // Do nothing.
7863 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007864 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7865 FromDef, ToProto, ASTNodeImporter::IDK_Basic))
7866 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007867 } else {
7868 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00007869 }
Douglas Gregor95d82832012-01-24 18:36:04 +00007870 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007871
Douglas Gregor95d82832012-01-24 18:36:04 +00007872 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007873}
7874
Balazs Keri4a3d7582018-11-27 18:36:31 +00007875Expected<Expr *> ASTImporter::Import_New(Expr *FromE) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007876 if (ExpectedStmt ToSOrErr = Import_New(cast_or_null<Stmt>(FromE)))
7877 return cast_or_null<Expr>(*ToSOrErr);
7878 else
7879 return ToSOrErr.takeError();
Balazs Keri4a3d7582018-11-27 18:36:31 +00007880}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007881Expr *ASTImporter::Import(Expr *From) {
7882 llvm::Expected<Expr *> To = Import_New(From);
7883 if (To)
7884 return *To;
7885 else
7886 llvm::consumeError(To.takeError());
7887 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007888}
7889
Balazs Keri4a3d7582018-11-27 18:36:31 +00007890Expected<Stmt *> ASTImporter::Import_New(Stmt *FromS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007891 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007892 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007893
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007894 // Check whether we've already imported this statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007895 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7896 if (Pos != ImportedStmts.end())
7897 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00007898
Balazs Keri3b30d652018-10-19 13:32:20 +00007899 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007900 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007901 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007902 if (!ToSOrErr)
7903 return ToSOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007904
Balazs Keri3b30d652018-10-19 13:32:20 +00007905 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00007906 auto *FromE = cast<Expr>(FromS);
7907 // Copy ExprBitfields, which may not be handled in Expr subclasses
7908 // constructors.
7909 ToE->setValueKind(FromE->getValueKind());
7910 ToE->setObjectKind(FromE->getObjectKind());
7911 ToE->setTypeDependent(FromE->isTypeDependent());
7912 ToE->setValueDependent(FromE->isValueDependent());
7913 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
7914 ToE->setContainsUnexpandedParameterPack(
7915 FromE->containsUnexpandedParameterPack());
7916 }
7917
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007918 // Record the imported statement object.
Balazs Keri3b30d652018-10-19 13:32:20 +00007919 ImportedStmts[FromS] = *ToSOrErr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007920 return ToSOrErr;
7921}
7922Stmt *ASTImporter::Import(Stmt *From) {
7923 llvm::Expected<Stmt *> To = Import_New(From);
7924 if (To)
7925 return *To;
7926 else
7927 llvm::consumeError(To.takeError());
7928 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007929}
7930
Balazs Keri4a3d7582018-11-27 18:36:31 +00007931Expected<NestedNameSpecifier *>
7932ASTImporter::Import_New(NestedNameSpecifier *FromNNS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007933 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00007934 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007935
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007936 NestedNameSpecifier *Prefix;
7937 if (Error Err = importInto(Prefix, FromNNS->getPrefix()))
7938 return std::move(Err);
Douglas Gregor90ebf252011-04-27 16:48:40 +00007939
7940 switch (FromNNS->getKind()) {
7941 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007942 assert(FromNNS->getAsIdentifier() && "NNS should contain identifier.");
7943 return NestedNameSpecifier::Create(ToContext, Prefix,
7944 Import(FromNNS->getAsIdentifier()));
Douglas Gregor90ebf252011-04-27 16:48:40 +00007945
7946 case NestedNameSpecifier::Namespace:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007947 if (ExpectedDecl NSOrErr = Import_New(FromNNS->getAsNamespace())) {
7948 return NestedNameSpecifier::Create(ToContext, Prefix,
7949 cast<NamespaceDecl>(*NSOrErr));
7950 } else
7951 return NSOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00007952
7953 case NestedNameSpecifier::NamespaceAlias:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007954 if (ExpectedDecl NSADOrErr = Import_New(FromNNS->getAsNamespaceAlias()))
7955 return NestedNameSpecifier::Create(ToContext, Prefix,
7956 cast<NamespaceAliasDecl>(*NSADOrErr));
7957 else
7958 return NSADOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00007959
7960 case NestedNameSpecifier::Global:
7961 return NestedNameSpecifier::GlobalSpecifier(ToContext);
7962
Nikola Smiljanic67860242014-09-26 00:28:20 +00007963 case NestedNameSpecifier::Super:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007964 if (ExpectedDecl RDOrErr = Import_New(FromNNS->getAsRecordDecl()))
7965 return NestedNameSpecifier::SuperSpecifier(ToContext,
7966 cast<CXXRecordDecl>(*RDOrErr));
7967 else
7968 return RDOrErr.takeError();
Nikola Smiljanic67860242014-09-26 00:28:20 +00007969
Douglas Gregor90ebf252011-04-27 16:48:40 +00007970 case NestedNameSpecifier::TypeSpec:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007971 case NestedNameSpecifier::TypeSpecWithTemplate:
7972 if (Expected<QualType> TyOrErr =
7973 Import_New(QualType(FromNNS->getAsType(), 0u))) {
7974 bool TSTemplate =
7975 FromNNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate;
7976 return NestedNameSpecifier::Create(ToContext, Prefix, TSTemplate,
7977 TyOrErr->getTypePtr());
7978 } else {
7979 return TyOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00007980 }
Douglas Gregor90ebf252011-04-27 16:48:40 +00007981 }
7982
7983 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00007984}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007985NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *From) {
7986 llvm::Expected<NestedNameSpecifier *> To = Import_New(From);
7987 if (To)
7988 return *To;
7989 else
7990 llvm::consumeError(To.takeError());
7991 return nullptr;
7992}
Douglas Gregor62d311f2010-02-09 19:21:46 +00007993
Balazs Keri4a3d7582018-11-27 18:36:31 +00007994Expected<NestedNameSpecifierLoc>
7995ASTImporter::Import_New(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007996 // Copied from NestedNameSpecifier mostly.
7997 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
7998 NestedNameSpecifierLoc NNS = FromNNS;
7999
8000 // Push each of the nested-name-specifiers's onto a stack for
8001 // serialization in reverse order.
8002 while (NNS) {
8003 NestedNames.push_back(NNS);
8004 NNS = NNS.getPrefix();
8005 }
8006
8007 NestedNameSpecifierLocBuilder Builder;
8008
8009 while (!NestedNames.empty()) {
8010 NNS = NestedNames.pop_back_val();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008011 NestedNameSpecifier *Spec;
8012 if (Error Err = importInto(Spec, NNS.getNestedNameSpecifier()))
8013 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008014
8015 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008016
8017 SourceLocation ToLocalBeginLoc, ToLocalEndLoc;
8018 if (Kind != NestedNameSpecifier::Super) {
8019 if (Error Err = importInto(ToLocalBeginLoc, NNS.getLocalBeginLoc()))
8020 return std::move(Err);
8021
8022 if (Kind != NestedNameSpecifier::Global)
8023 if (Error Err = importInto(ToLocalEndLoc, NNS.getLocalEndLoc()))
8024 return std::move(Err);
8025 }
8026
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008027 switch (Kind) {
8028 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008029 Builder.Extend(getToContext(), Spec->getAsIdentifier(), ToLocalBeginLoc,
8030 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008031 break;
8032
8033 case NestedNameSpecifier::Namespace:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008034 Builder.Extend(getToContext(), Spec->getAsNamespace(), ToLocalBeginLoc,
8035 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008036 break;
8037
8038 case NestedNameSpecifier::NamespaceAlias:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008039 Builder.Extend(getToContext(), Spec->getAsNamespaceAlias(),
8040 ToLocalBeginLoc, ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008041 break;
8042
8043 case NestedNameSpecifier::TypeSpec:
8044 case NestedNameSpecifier::TypeSpecWithTemplate: {
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008045 SourceLocation ToTLoc;
8046 if (Error Err = importInto(ToTLoc, NNS.getTypeLoc().getBeginLoc()))
8047 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008048 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008049 QualType(Spec->getAsType(), 0), ToTLoc);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008050 Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(),
8051 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008052 break;
8053 }
8054
8055 case NestedNameSpecifier::Global:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008056 Builder.MakeGlobal(getToContext(), ToLocalBeginLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008057 break;
8058
8059 case NestedNameSpecifier::Super: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008060 auto ToSourceRangeOrErr = Import_New(NNS.getSourceRange());
8061 if (!ToSourceRangeOrErr)
8062 return ToSourceRangeOrErr.takeError();
8063
8064 Builder.MakeSuper(getToContext(), Spec->getAsRecordDecl(),
8065 ToSourceRangeOrErr->getBegin(),
8066 ToSourceRangeOrErr->getEnd());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008067 }
8068 }
8069 }
8070
8071 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00008072}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008073NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc From) {
8074 llvm::Expected<NestedNameSpecifierLoc> To = Import_New(From);
8075 if (To)
8076 return *To;
8077 else
8078 llvm::consumeError(To.takeError());
8079 return {};
8080}
Douglas Gregor14454802011-02-25 02:25:35 +00008081
Balazs Keri4a3d7582018-11-27 18:36:31 +00008082Expected<TemplateName> ASTImporter::Import_New(TemplateName From) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00008083 switch (From.getKind()) {
8084 case TemplateName::Template:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008085 if (ExpectedDecl ToTemplateOrErr = Import_New(From.getAsTemplateDecl()))
8086 return TemplateName(cast<TemplateDecl>(*ToTemplateOrErr));
8087 else
8088 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008089
Douglas Gregore2e50d332010-12-01 01:36:18 +00008090 case TemplateName::OverloadedTemplate: {
8091 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
8092 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008093 for (auto *I : *FromStorage) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008094 if (auto ToOrErr = Import_New(I))
8095 ToTemplates.addDecl(cast<NamedDecl>(*ToOrErr));
Douglas Gregore2e50d332010-12-01 01:36:18 +00008096 else
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008097 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008098 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008099 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008100 ToTemplates.end());
8101 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008102
Douglas Gregore2e50d332010-12-01 01:36:18 +00008103 case TemplateName::QualifiedTemplate: {
8104 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008105 auto QualifierOrErr = Import_New(QTN->getQualifier());
8106 if (!QualifierOrErr)
8107 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008108
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008109 if (ExpectedDecl ToTemplateOrErr = Import_New(From.getAsTemplateDecl()))
8110 return ToContext.getQualifiedTemplateName(
8111 *QualifierOrErr, QTN->hasTemplateKeyword(),
8112 cast<TemplateDecl>(*ToTemplateOrErr));
8113 else
8114 return ToTemplateOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008115 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008116
Douglas Gregore2e50d332010-12-01 01:36:18 +00008117 case TemplateName::DependentTemplate: {
8118 DependentTemplateName *DTN = From.getAsDependentTemplateName();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008119 auto QualifierOrErr = Import_New(DTN->getQualifier());
8120 if (!QualifierOrErr)
8121 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008122
Douglas Gregore2e50d332010-12-01 01:36:18 +00008123 if (DTN->isIdentifier()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008124 return ToContext.getDependentTemplateName(*QualifierOrErr,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008125 Import(DTN->getIdentifier()));
8126 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008127
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008128 return ToContext.getDependentTemplateName(*QualifierOrErr,
8129 DTN->getOperator());
Douglas Gregore2e50d332010-12-01 01:36:18 +00008130 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008131
8132 case TemplateName::SubstTemplateTemplateParm: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008133 SubstTemplateTemplateParmStorage *Subst =
8134 From.getAsSubstTemplateTemplateParm();
8135 ExpectedDecl ParamOrErr = Import_New(Subst->getParameter());
8136 if (!ParamOrErr)
8137 return ParamOrErr.takeError();
John McCalld9dfe3a2011-06-30 08:33:18 +00008138
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008139 auto ReplacementOrErr = Import_New(Subst->getReplacement());
8140 if (!ReplacementOrErr)
8141 return ReplacementOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008142
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008143 return ToContext.getSubstTemplateTemplateParm(
8144 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ReplacementOrErr);
John McCalld9dfe3a2011-06-30 08:33:18 +00008145 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008146
Douglas Gregor5590be02011-01-15 06:45:20 +00008147 case TemplateName::SubstTemplateTemplateParmPack: {
8148 SubstTemplateTemplateParmPackStorage *SubstPack
8149 = From.getAsSubstTemplateTemplateParmPack();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008150 ExpectedDecl ParamOrErr = Import_New(SubstPack->getParameterPack());
8151 if (!ParamOrErr)
8152 return ParamOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008153
Douglas Gregor5590be02011-01-15 06:45:20 +00008154 ASTNodeImporter Importer(*this);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008155 auto ArgPackOrErr =
8156 Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
8157 if (!ArgPackOrErr)
8158 return ArgPackOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008159
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008160 return ToContext.getSubstTemplateTemplateParmPack(
8161 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ArgPackOrErr);
Douglas Gregor5590be02011-01-15 06:45:20 +00008162 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008163 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008164
Douglas Gregore2e50d332010-12-01 01:36:18 +00008165 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008166}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008167TemplateName ASTImporter::Import(TemplateName From) {
8168 llvm::Expected<TemplateName> To = Import_New(From);
8169 if (To)
8170 return *To;
8171 else
8172 llvm::consumeError(To.takeError());
8173 return {};
8174}
Douglas Gregore2e50d332010-12-01 01:36:18 +00008175
Balazs Keri4a3d7582018-11-27 18:36:31 +00008176Expected<SourceLocation> ASTImporter::Import_New(SourceLocation FromLoc) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008177 if (FromLoc.isInvalid())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008178 return SourceLocation{};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008179
Douglas Gregor811663e2010-02-10 00:15:17 +00008180 SourceManager &FromSM = FromContext.getSourceManager();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008181 bool IsBuiltin = FromSM.isWrittenInBuiltinFile(FromLoc);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008182
Douglas Gregor811663e2010-02-10 00:15:17 +00008183 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008184 Expected<FileID> ToFileIDOrErr = Import_New(Decomposed.first, IsBuiltin);
8185 if (!ToFileIDOrErr)
8186 return ToFileIDOrErr.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008187 SourceManager &ToSM = ToContext.getSourceManager();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008188 return ToSM.getComposedLoc(*ToFileIDOrErr, Decomposed.second);
8189}
8190SourceLocation ASTImporter::Import(SourceLocation From) {
8191 llvm::Expected<SourceLocation> To = Import_New(From);
8192 if (To)
8193 return *To;
8194 else
8195 llvm::consumeError(To.takeError());
8196 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008197}
8198
Balazs Keri4a3d7582018-11-27 18:36:31 +00008199Expected<SourceRange> ASTImporter::Import_New(SourceRange FromRange) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008200 SourceLocation ToBegin, ToEnd;
8201 if (Error Err = importInto(ToBegin, FromRange.getBegin()))
8202 return std::move(Err);
8203 if (Error Err = importInto(ToEnd, FromRange.getEnd()))
8204 return std::move(Err);
8205
8206 return SourceRange(ToBegin, ToEnd);
Balazs Keri4a3d7582018-11-27 18:36:31 +00008207}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008208SourceRange ASTImporter::Import(SourceRange From) {
8209 llvm::Expected<SourceRange> To = Import_New(From);
8210 if (To)
8211 return *To;
8212 else
8213 llvm::consumeError(To.takeError());
8214 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008215}
8216
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008217Expected<FileID> ASTImporter::Import_New(FileID FromID, bool IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008218 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008219 if (Pos != ImportedFileIDs.end())
8220 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008221
Douglas Gregor811663e2010-02-10 00:15:17 +00008222 SourceManager &FromSM = FromContext.getSourceManager();
8223 SourceManager &ToSM = ToContext.getSourceManager();
8224 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008225
8226 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008227 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008228 if (FromSLoc.isExpansion()) {
8229 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008230 ExpectedSLoc ToSpLoc = Import_New(FromEx.getSpellingLoc());
8231 if (!ToSpLoc)
8232 return ToSpLoc.takeError();
8233 ExpectedSLoc ToExLocS = Import_New(FromEx.getExpansionLocStart());
8234 if (!ToExLocS)
8235 return ToExLocS.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008236 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8237 SourceLocation MLoc;
8238 if (FromEx.isMacroArgExpansion()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008239 MLoc = ToSM.createMacroArgExpansionLoc(*ToSpLoc, *ToExLocS, TokenLen);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008240 } else {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008241 if (ExpectedSLoc ToExLocE = Import_New(FromEx.getExpansionLocEnd()))
8242 MLoc = ToSM.createExpansionLoc(*ToSpLoc, *ToExLocS, *ToExLocE, TokenLen,
8243 FromEx.isExpansionTokenRange());
8244 else
8245 return ToExLocE.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008246 }
8247 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008248 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008249 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008250
8251 if (!IsBuiltin) {
8252 // Include location of this file.
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008253 ExpectedSLoc ToIncludeLoc =
8254 Import_New(FromSLoc.getFile().getIncludeLoc());
8255 if (!ToIncludeLoc)
8256 return ToIncludeLoc.takeError();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008257
8258 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8259 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8260 // disk again
8261 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8262 // than mmap the files several times.
8263 const FileEntry *Entry =
8264 ToFileManager.getFile(Cache->OrigEntry->getName());
8265 // FIXME: The filename may be a virtual name that does probably not
8266 // point to a valid file and we get no Entry here. In this case try with
8267 // the memory buffer below.
8268 if (Entry)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008269 ToID = ToSM.createFileID(Entry, *ToIncludeLoc,
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008270 FromSLoc.getFile().getFileCharacteristic());
8271 }
Balazs Keri9cf39df2019-02-27 16:31:48 +00008272 }
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008273
8274 if (ToID.isInvalid() || IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008275 // FIXME: We want to re-use the existing MemoryBuffer!
Balazs Keri9cf39df2019-02-27 16:31:48 +00008276 bool Invalid = true;
8277 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(
8278 FromContext.getDiagnostics(), FromSM, SourceLocation{}, &Invalid);
8279 if (!FromBuf || Invalid)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008280 // FIXME: Use a new error kind?
8281 return llvm::make_error<ImportError>(ImportError::Unknown);
Balazs Keri9cf39df2019-02-27 16:31:48 +00008282
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008283 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8284 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8285 FromBuf->getBufferIdentifier());
8286 ToID = ToSM.createFileID(std::move(ToBuf),
8287 FromSLoc.getFile().getFileCharacteristic());
8288 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008289 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008290
Balazs Keri9cf39df2019-02-27 16:31:48 +00008291 assert(ToID.isValid() && "Unexpected invalid fileID was created.");
8292
Sebastian Redl99219f12010-09-30 01:03:06 +00008293 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00008294 return ToID;
8295}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008296FileID ASTImporter::Import(FileID From, bool IsBuiltin) {
8297 llvm::Expected<FileID> To = Import_New(From, IsBuiltin);
8298 if (To)
8299 return *To;
8300 else
8301 llvm::consumeError(To.takeError());
8302 return {};
8303}
Douglas Gregor811663e2010-02-10 00:15:17 +00008304
Balazs Keri4a3d7582018-11-27 18:36:31 +00008305Expected<CXXCtorInitializer *>
8306ASTImporter::Import_New(CXXCtorInitializer *From) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008307 ExpectedExpr ToExprOrErr = Import_New(From->getInit());
8308 if (!ToExprOrErr)
8309 return ToExprOrErr.takeError();
8310
8311 auto LParenLocOrErr = Import_New(From->getLParenLoc());
8312 if (!LParenLocOrErr)
8313 return LParenLocOrErr.takeError();
8314
8315 auto RParenLocOrErr = Import_New(From->getRParenLoc());
8316 if (!RParenLocOrErr)
8317 return RParenLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008318
8319 if (From->isBaseInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008320 auto ToTInfoOrErr = Import_New(From->getTypeSourceInfo());
8321 if (!ToTInfoOrErr)
8322 return ToTInfoOrErr.takeError();
8323
8324 SourceLocation EllipsisLoc;
8325 if (From->isPackExpansion())
8326 if (Error Err = importInto(EllipsisLoc, From->getEllipsisLoc()))
8327 return std::move(Err);
Davide Italianofaee83d2018-11-28 19:15:23 +00008328
8329 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008330 ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr,
8331 *ToExprOrErr, *RParenLocOrErr, EllipsisLoc);
Davide Italianofaee83d2018-11-28 19:15:23 +00008332 } else if (From->isMemberInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008333 ExpectedDecl ToFieldOrErr = Import_New(From->getMember());
8334 if (!ToFieldOrErr)
8335 return ToFieldOrErr.takeError();
8336
8337 auto MemberLocOrErr = Import_New(From->getMemberLocation());
8338 if (!MemberLocOrErr)
8339 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008340
8341 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008342 ToContext, cast_or_null<FieldDecl>(*ToFieldOrErr), *MemberLocOrErr,
8343 *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008344 } else if (From->isIndirectMemberInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008345 ExpectedDecl ToIFieldOrErr = Import_New(From->getIndirectMember());
8346 if (!ToIFieldOrErr)
8347 return ToIFieldOrErr.takeError();
8348
8349 auto MemberLocOrErr = Import_New(From->getMemberLocation());
8350 if (!MemberLocOrErr)
8351 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008352
8353 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008354 ToContext, cast_or_null<IndirectFieldDecl>(*ToIFieldOrErr),
8355 *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008356 } else if (From->isDelegatingInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008357 auto ToTInfoOrErr = Import_New(From->getTypeSourceInfo());
8358 if (!ToTInfoOrErr)
8359 return ToTInfoOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008360
8361 return new (ToContext)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008362 CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr,
8363 *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008364 } else {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008365 // FIXME: assert?
8366 return make_error<ImportError>();
Davide Italianofaee83d2018-11-28 19:15:23 +00008367 }
Balazs Kerideaf7ab2018-11-28 13:21:26 +00008368}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008369CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
8370 llvm::Expected<CXXCtorInitializer *> To = Import_New(From);
8371 if (To)
8372 return *To;
8373 else
8374 llvm::consumeError(To.takeError());
8375 return nullptr;
8376}
Sean Callanandd2c1742016-05-16 20:48:03 +00008377
Balazs Keri4a3d7582018-11-27 18:36:31 +00008378Expected<CXXBaseSpecifier *>
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008379ASTImporter::Import_New(const CXXBaseSpecifier *BaseSpec) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00008380 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8381 if (Pos != ImportedCXXBaseSpecifiers.end())
8382 return Pos->second;
8383
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008384 Expected<SourceRange> ToSourceRange = Import_New(BaseSpec->getSourceRange());
8385 if (!ToSourceRange)
8386 return ToSourceRange.takeError();
8387 Expected<TypeSourceInfo *> ToTSI = Import_New(BaseSpec->getTypeSourceInfo());
8388 if (!ToTSI)
8389 return ToTSI.takeError();
8390 ExpectedSLoc ToEllipsisLoc = Import_New(BaseSpec->getEllipsisLoc());
8391 if (!ToEllipsisLoc)
8392 return ToEllipsisLoc.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00008393 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008394 *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8395 BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00008396 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8397 return Imported;
8398}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008399CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *From) {
8400 llvm::Expected<CXXBaseSpecifier *> To = Import_New(From);
8401 if (To)
8402 return *To;
8403 else
8404 llvm::consumeError(To.takeError());
8405 return nullptr;
8406}
Aleksei Sidorina693b372016-09-28 10:16:56 +00008407
Balazs Keri3b30d652018-10-19 13:32:20 +00008408Error ASTImporter::ImportDefinition_New(Decl *From) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008409 Decl *To = Import(From);
8410 if (!To)
Balazs Keri3b30d652018-10-19 13:32:20 +00008411 return llvm::make_error<ImportError>();
Fangrui Song6907ce22018-07-30 19:24:48 +00008412
Don Hintonf170dff2019-03-19 06:14:14 +00008413 auto *FromDC = cast<DeclContext>(From);
8414 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008415
Don Hintonf170dff2019-03-19 06:14:14 +00008416 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
8417 if (!ToRecord->getDefinition()) {
8418 return Importer.ImportDefinition(
8419 cast<RecordDecl>(FromDC), ToRecord,
8420 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00008421 }
Douglas Gregor0a791672011-01-18 03:11:38 +00008422 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008423
Don Hintonf170dff2019-03-19 06:14:14 +00008424 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
8425 if (!ToEnum->getDefinition()) {
8426 return Importer.ImportDefinition(
8427 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
8428 }
8429 }
8430
8431 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
8432 if (!ToIFace->getDefinition()) {
8433 return Importer.ImportDefinition(
8434 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8435 ASTNodeImporter::IDK_Everything);
8436 }
8437 }
8438
8439 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
8440 if (!ToProto->getDefinition()) {
8441 return Importer.ImportDefinition(
8442 cast<ObjCProtocolDecl>(FromDC), ToProto,
8443 ASTNodeImporter::IDK_Everything);
8444 }
8445 }
8446
8447 return Importer.ImportDeclContext(FromDC, true);
Balazs Keri3b30d652018-10-19 13:32:20 +00008448}
8449
8450void ASTImporter::ImportDefinition(Decl *From) {
8451 Error Err = ImportDefinition_New(From);
8452 llvm::consumeError(std::move(Err));
Douglas Gregor0a791672011-01-18 03:11:38 +00008453}
8454
Balazs Keri4a3d7582018-11-27 18:36:31 +00008455Expected<DeclarationName> ASTImporter::Import_New(DeclarationName FromName) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008456 if (!FromName)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008457 return DeclarationName{};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008458
8459 switch (FromName.getNameKind()) {
8460 case DeclarationName::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008461 return DeclarationName(Import(FromName.getAsIdentifierInfo()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008462
8463 case DeclarationName::ObjCZeroArgSelector:
8464 case DeclarationName::ObjCOneArgSelector:
8465 case DeclarationName::ObjCMultiArgSelector:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008466 if (auto ToSelOrErr = Import_New(FromName.getObjCSelector()))
8467 return DeclarationName(*ToSelOrErr);
8468 else
8469 return ToSelOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008470
8471 case DeclarationName::CXXConstructorName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008472 if (auto ToTyOrErr = Import_New(FromName.getCXXNameType()))
8473 return ToContext.DeclarationNames.getCXXConstructorName(
8474 ToContext.getCanonicalType(*ToTyOrErr));
8475 else
8476 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008477 }
8478
8479 case DeclarationName::CXXDestructorName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008480 if (auto ToTyOrErr = Import_New(FromName.getCXXNameType()))
8481 return ToContext.DeclarationNames.getCXXDestructorName(
8482 ToContext.getCanonicalType(*ToTyOrErr));
8483 else
8484 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008485 }
8486
Richard Smith35845152017-02-07 01:37:30 +00008487 case DeclarationName::CXXDeductionGuideName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008488 if (auto ToTemplateOrErr =
8489 Import_New(FromName.getCXXDeductionGuideTemplate()))
8490 return ToContext.DeclarationNames.getCXXDeductionGuideName(
8491 cast<TemplateDecl>(*ToTemplateOrErr));
8492 else
8493 return ToTemplateOrErr.takeError();
Richard Smith35845152017-02-07 01:37:30 +00008494 }
8495
Douglas Gregor96e578d2010-02-05 17:54:41 +00008496 case DeclarationName::CXXConversionFunctionName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008497 if (auto ToTyOrErr = Import_New(FromName.getCXXNameType()))
8498 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8499 ToContext.getCanonicalType(*ToTyOrErr));
8500 else
8501 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008502 }
8503
8504 case DeclarationName::CXXOperatorName:
8505 return ToContext.DeclarationNames.getCXXOperatorName(
8506 FromName.getCXXOverloadedOperator());
8507
8508 case DeclarationName::CXXLiteralOperatorName:
8509 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008510 Import(FromName.getCXXLiteralIdentifier()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008511
8512 case DeclarationName::CXXUsingDirective:
8513 // FIXME: STATICS!
8514 return DeclarationName::getUsingDirectiveName();
8515 }
8516
David Blaikiee4d798f2012-01-20 21:50:17 +00008517 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008518}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008519DeclarationName ASTImporter::Import(DeclarationName From) {
8520 llvm::Expected<DeclarationName> To = Import_New(From);
8521 if (To)
8522 return *To;
8523 else
8524 llvm::consumeError(To.takeError());
8525 return {};
8526}
Douglas Gregor96e578d2010-02-05 17:54:41 +00008527
Douglas Gregore2e50d332010-12-01 01:36:18 +00008528IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008529 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008530 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008531
Sean Callananf94ef1d2016-05-14 06:11:19 +00008532 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8533
8534 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8535 ToId->setBuiltinID(FromId->getBuiltinID());
8536
8537 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008538}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008539
Balazs Keri4a3d7582018-11-27 18:36:31 +00008540Expected<Selector> ASTImporter::Import_New(Selector FromSel) {
Douglas Gregor43f54792010-02-17 02:12:47 +00008541 if (FromSel.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008542 return Selector{};
Douglas Gregor43f54792010-02-17 02:12:47 +00008543
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008544 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008545 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8546 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8547 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8548 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8549}
8550
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008551DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
8552 DeclContext *DC,
8553 unsigned IDNS,
8554 NamedDecl **Decls,
8555 unsigned NumDecls) {
8556 return Name;
8557}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008558Selector ASTImporter::Import(Selector From) {
8559 llvm::Expected<Selector> To = Import_New(From);
8560 if (To)
8561 return *To;
8562 else
8563 llvm::consumeError(To.takeError());
8564 return {};
8565}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008566
8567DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008568 if (LastDiagFromFrom)
8569 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8570 FromContext.getDiagnostics());
8571 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008572 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008573}
8574
8575DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008576 if (!LastDiagFromFrom)
8577 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8578 ToContext.getDiagnostics());
8579 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008580 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008581}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008582
Douglas Gregor2e15c842012-02-01 21:00:38 +00008583void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008584 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008585 if (!ID->getDefinition())
8586 ID->startDefinition();
8587 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008588 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008589 if (!PD->getDefinition())
8590 PD->startDefinition();
8591 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008592 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008593 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8594 TD->startDefinition();
8595 TD->setCompleteDefinition(true);
8596 }
8597 }
8598 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008599 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008600 }
8601}
8602
Gabor Marton26f72a92018-07-12 09:42:05 +00008603Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8604 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8605 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8606 "Try to import an already imported Decl");
8607 if (Pos != ImportedDecls.end())
8608 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008609 ImportedDecls[From] = To;
Gabor Marton458d1452019-02-14 13:07:03 +00008610 // This mapping should be maintained only in this function. Therefore do not
8611 // check for additional consistency.
8612 ImportedFromDecls[To] = From;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008613 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00008614}
Douglas Gregorb4964f72010-02-15 23:54:17 +00008615
Douglas Gregordd6006f2012-07-17 21:16:27 +00008616bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
8617 bool Complain) {
Balazs Keria1f6b102019-04-08 13:59:15 +00008618 llvm::DenseMap<const Type *, const Type *>::iterator Pos =
8619 ImportedTypes.find(From.getTypePtr());
8620 if (Pos != ImportedTypes.end()) {
8621 if (ExpectedType ToFromOrErr = Import_New(From)) {
8622 if (ToContext.hasSameType(*ToFromOrErr, To))
8623 return true;
8624 } else {
8625 llvm::consumeError(ToFromOrErr.takeError());
8626 }
8627 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00008628
Douglas Gregordd6006f2012-07-17 21:16:27 +00008629 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00008630 getStructuralEquivalenceKind(*this), false,
8631 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00008632 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00008633}