blob: da0d68900afeec9603dd493bc1f9573d465f631d [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);
Balazs Keri1efc9742019-05-07 10:55:11 +0000422 Error ImportTemplateParameterLists(const DeclaratorDecl *FromD,
423 DeclaratorDecl *ToD);
Gabor Marton5254e642018-06-27 13:32:50 +0000424
Balazs Keri3b30d652018-10-19 13:32:20 +0000425 Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000426
Shafik Yaghmour96b3d202019-01-28 21:55:33 +0000427 Error ImportFunctionDeclBody(FunctionDecl *FromFD, FunctionDecl *ToFD);
428
Gabor Marton458d1452019-02-14 13:07:03 +0000429 template <typename T>
430 bool hasSameVisibilityContext(T *Found, T *From);
431
Gabor Marton950fb572018-07-17 12:39:27 +0000432 bool IsStructuralMatch(Decl *From, Decl *To, bool Complain);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000433 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
434 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000435 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
436 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000437 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000438 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000439 bool IsStructuralMatch(FunctionTemplateDecl *From,
440 FunctionTemplateDecl *To);
Balazs Keric7797c42018-07-11 09:37:24 +0000441 bool IsStructuralMatch(FunctionDecl *From, FunctionDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000442 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000443 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Balazs Keri3b30d652018-10-19 13:32:20 +0000444 ExpectedDecl VisitDecl(Decl *D);
445 ExpectedDecl VisitImportDecl(ImportDecl *D);
446 ExpectedDecl VisitEmptyDecl(EmptyDecl *D);
447 ExpectedDecl VisitAccessSpecDecl(AccessSpecDecl *D);
448 ExpectedDecl VisitStaticAssertDecl(StaticAssertDecl *D);
449 ExpectedDecl VisitTranslationUnitDecl(TranslationUnitDecl *D);
450 ExpectedDecl VisitNamespaceDecl(NamespaceDecl *D);
451 ExpectedDecl VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
452 ExpectedDecl VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
453 ExpectedDecl VisitTypedefDecl(TypedefDecl *D);
454 ExpectedDecl VisitTypeAliasDecl(TypeAliasDecl *D);
455 ExpectedDecl VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
456 ExpectedDecl VisitLabelDecl(LabelDecl *D);
457 ExpectedDecl VisitEnumDecl(EnumDecl *D);
458 ExpectedDecl VisitRecordDecl(RecordDecl *D);
459 ExpectedDecl VisitEnumConstantDecl(EnumConstantDecl *D);
460 ExpectedDecl VisitFunctionDecl(FunctionDecl *D);
461 ExpectedDecl VisitCXXMethodDecl(CXXMethodDecl *D);
462 ExpectedDecl VisitCXXConstructorDecl(CXXConstructorDecl *D);
463 ExpectedDecl VisitCXXDestructorDecl(CXXDestructorDecl *D);
464 ExpectedDecl VisitCXXConversionDecl(CXXConversionDecl *D);
465 ExpectedDecl VisitFieldDecl(FieldDecl *D);
466 ExpectedDecl VisitIndirectFieldDecl(IndirectFieldDecl *D);
467 ExpectedDecl VisitFriendDecl(FriendDecl *D);
468 ExpectedDecl VisitObjCIvarDecl(ObjCIvarDecl *D);
469 ExpectedDecl VisitVarDecl(VarDecl *D);
470 ExpectedDecl VisitImplicitParamDecl(ImplicitParamDecl *D);
471 ExpectedDecl VisitParmVarDecl(ParmVarDecl *D);
472 ExpectedDecl VisitObjCMethodDecl(ObjCMethodDecl *D);
473 ExpectedDecl VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
474 ExpectedDecl VisitObjCCategoryDecl(ObjCCategoryDecl *D);
475 ExpectedDecl VisitObjCProtocolDecl(ObjCProtocolDecl *D);
476 ExpectedDecl VisitLinkageSpecDecl(LinkageSpecDecl *D);
477 ExpectedDecl VisitUsingDecl(UsingDecl *D);
478 ExpectedDecl VisitUsingShadowDecl(UsingShadowDecl *D);
479 ExpectedDecl VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
480 ExpectedDecl VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
481 ExpectedDecl VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000482
Balazs Keri3b30d652018-10-19 13:32:20 +0000483 Expected<ObjCTypeParamList *>
484 ImportObjCTypeParamList(ObjCTypeParamList *list);
485
486 ExpectedDecl VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
487 ExpectedDecl VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
488 ExpectedDecl VisitObjCImplementationDecl(ObjCImplementationDecl *D);
489 ExpectedDecl VisitObjCPropertyDecl(ObjCPropertyDecl *D);
490 ExpectedDecl VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
491 ExpectedDecl VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
492 ExpectedDecl VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
493 ExpectedDecl VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
494 ExpectedDecl VisitClassTemplateDecl(ClassTemplateDecl *D);
495 ExpectedDecl VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +0000496 ClassTemplateSpecializationDecl *D);
Balazs Keri3b30d652018-10-19 13:32:20 +0000497 ExpectedDecl VisitVarTemplateDecl(VarTemplateDecl *D);
498 ExpectedDecl VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
499 ExpectedDecl VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000500
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000501 // Importing statements
Balazs Keri3b30d652018-10-19 13:32:20 +0000502 ExpectedStmt VisitStmt(Stmt *S);
503 ExpectedStmt VisitGCCAsmStmt(GCCAsmStmt *S);
504 ExpectedStmt VisitDeclStmt(DeclStmt *S);
505 ExpectedStmt VisitNullStmt(NullStmt *S);
506 ExpectedStmt VisitCompoundStmt(CompoundStmt *S);
507 ExpectedStmt VisitCaseStmt(CaseStmt *S);
508 ExpectedStmt VisitDefaultStmt(DefaultStmt *S);
509 ExpectedStmt VisitLabelStmt(LabelStmt *S);
510 ExpectedStmt VisitAttributedStmt(AttributedStmt *S);
511 ExpectedStmt VisitIfStmt(IfStmt *S);
512 ExpectedStmt VisitSwitchStmt(SwitchStmt *S);
513 ExpectedStmt VisitWhileStmt(WhileStmt *S);
514 ExpectedStmt VisitDoStmt(DoStmt *S);
515 ExpectedStmt VisitForStmt(ForStmt *S);
516 ExpectedStmt VisitGotoStmt(GotoStmt *S);
517 ExpectedStmt VisitIndirectGotoStmt(IndirectGotoStmt *S);
518 ExpectedStmt VisitContinueStmt(ContinueStmt *S);
519 ExpectedStmt VisitBreakStmt(BreakStmt *S);
520 ExpectedStmt VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000521 // FIXME: MSAsmStmt
522 // FIXME: SEHExceptStmt
523 // FIXME: SEHFinallyStmt
524 // FIXME: SEHTryStmt
525 // FIXME: SEHLeaveStmt
526 // FIXME: CapturedStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000527 ExpectedStmt VisitCXXCatchStmt(CXXCatchStmt *S);
528 ExpectedStmt VisitCXXTryStmt(CXXTryStmt *S);
529 ExpectedStmt VisitCXXForRangeStmt(CXXForRangeStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000530 // FIXME: MSDependentExistsStmt
Balazs Keri3b30d652018-10-19 13:32:20 +0000531 ExpectedStmt VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
532 ExpectedStmt VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
533 ExpectedStmt VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
534 ExpectedStmt VisitObjCAtTryStmt(ObjCAtTryStmt *S);
535 ExpectedStmt VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
536 ExpectedStmt VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
537 ExpectedStmt VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000538
539 // Importing expressions
Balazs Keri3b30d652018-10-19 13:32:20 +0000540 ExpectedStmt VisitExpr(Expr *E);
541 ExpectedStmt VisitVAArgExpr(VAArgExpr *E);
Tom Roeder521f0042019-02-26 19:26:41 +0000542 ExpectedStmt VisitChooseExpr(ChooseExpr *E);
Balazs Keri3b30d652018-10-19 13:32:20 +0000543 ExpectedStmt VisitGNUNullExpr(GNUNullExpr *E);
544 ExpectedStmt VisitPredefinedExpr(PredefinedExpr *E);
545 ExpectedStmt VisitDeclRefExpr(DeclRefExpr *E);
546 ExpectedStmt VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
547 ExpectedStmt VisitDesignatedInitExpr(DesignatedInitExpr *E);
548 ExpectedStmt VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
549 ExpectedStmt VisitIntegerLiteral(IntegerLiteral *E);
550 ExpectedStmt VisitFloatingLiteral(FloatingLiteral *E);
551 ExpectedStmt VisitImaginaryLiteral(ImaginaryLiteral *E);
552 ExpectedStmt VisitCharacterLiteral(CharacterLiteral *E);
553 ExpectedStmt VisitStringLiteral(StringLiteral *E);
554 ExpectedStmt VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
555 ExpectedStmt VisitAtomicExpr(AtomicExpr *E);
556 ExpectedStmt VisitAddrLabelExpr(AddrLabelExpr *E);
Bill Wendling8003edc2018-11-09 00:41:36 +0000557 ExpectedStmt VisitConstantExpr(ConstantExpr *E);
Balazs Keri3b30d652018-10-19 13:32:20 +0000558 ExpectedStmt VisitParenExpr(ParenExpr *E);
559 ExpectedStmt VisitParenListExpr(ParenListExpr *E);
560 ExpectedStmt VisitStmtExpr(StmtExpr *E);
561 ExpectedStmt VisitUnaryOperator(UnaryOperator *E);
562 ExpectedStmt VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
563 ExpectedStmt VisitBinaryOperator(BinaryOperator *E);
564 ExpectedStmt VisitConditionalOperator(ConditionalOperator *E);
565 ExpectedStmt VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
566 ExpectedStmt VisitOpaqueValueExpr(OpaqueValueExpr *E);
567 ExpectedStmt VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
568 ExpectedStmt VisitExpressionTraitExpr(ExpressionTraitExpr *E);
569 ExpectedStmt VisitArraySubscriptExpr(ArraySubscriptExpr *E);
570 ExpectedStmt VisitCompoundAssignOperator(CompoundAssignOperator *E);
571 ExpectedStmt VisitImplicitCastExpr(ImplicitCastExpr *E);
572 ExpectedStmt VisitExplicitCastExpr(ExplicitCastExpr *E);
573 ExpectedStmt VisitOffsetOfExpr(OffsetOfExpr *OE);
574 ExpectedStmt VisitCXXThrowExpr(CXXThrowExpr *E);
575 ExpectedStmt VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
576 ExpectedStmt VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
577 ExpectedStmt VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
578 ExpectedStmt VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
579 ExpectedStmt VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
580 ExpectedStmt VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
581 ExpectedStmt VisitPackExpansionExpr(PackExpansionExpr *E);
582 ExpectedStmt VisitSizeOfPackExpr(SizeOfPackExpr *E);
583 ExpectedStmt VisitCXXNewExpr(CXXNewExpr *E);
584 ExpectedStmt VisitCXXDeleteExpr(CXXDeleteExpr *E);
585 ExpectedStmt VisitCXXConstructExpr(CXXConstructExpr *E);
586 ExpectedStmt VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
587 ExpectedStmt VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
588 ExpectedStmt VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
589 ExpectedStmt VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
590 ExpectedStmt VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
591 ExpectedStmt VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
592 ExpectedStmt VisitExprWithCleanups(ExprWithCleanups *E);
593 ExpectedStmt VisitCXXThisExpr(CXXThisExpr *E);
594 ExpectedStmt VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
595 ExpectedStmt VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
596 ExpectedStmt VisitMemberExpr(MemberExpr *E);
597 ExpectedStmt VisitCallExpr(CallExpr *E);
598 ExpectedStmt VisitLambdaExpr(LambdaExpr *LE);
599 ExpectedStmt VisitInitListExpr(InitListExpr *E);
600 ExpectedStmt VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E);
601 ExpectedStmt VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E);
602 ExpectedStmt VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
603 ExpectedStmt VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
604 ExpectedStmt VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
605 ExpectedStmt VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
606 ExpectedStmt VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
607 ExpectedStmt VisitTypeTraitExpr(TypeTraitExpr *E);
608 ExpectedStmt VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000609
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000610 template<typename IIter, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000611 Error ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000612 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
Balazs Keri3b30d652018-10-19 13:32:20 +0000613 for (; Ibegin != Iend; ++Ibegin, ++Obegin) {
614 Expected<ItemT> ToOrErr = import(*Ibegin);
615 if (!ToOrErr)
616 return ToOrErr.takeError();
617 *Obegin = *ToOrErr;
618 }
619 return Error::success();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000620 }
621
Balazs Keri3b30d652018-10-19 13:32:20 +0000622 // Import every item from a container structure into an output container.
623 // If error occurs, stops at first error and returns the error.
624 // The output container should have space for all needed elements (it is not
625 // expanded, new items are put into from the beginning).
Aleksei Sidorina693b372016-09-28 10:16:56 +0000626 template<typename InContainerTy, typename OutContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000627 Error ImportContainerChecked(
628 const InContainerTy &InContainer, OutContainerTy &OutContainer) {
629 return ImportArrayChecked(
630 InContainer.begin(), InContainer.end(), OutContainer.begin());
Aleksei Sidorina693b372016-09-28 10:16:56 +0000631 }
632
633 template<typename InContainerTy, typename OIter>
Balazs Keri3b30d652018-10-19 13:32:20 +0000634 Error ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000635 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
636 }
Lang Hames19e07e12017-06-20 21:06:00 +0000637
Lang Hames19e07e12017-06-20 21:06:00 +0000638 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Gabor Marton5254e642018-06-27 13:32:50 +0000639
Balazs Keri3b30d652018-10-19 13:32:20 +0000640 Expected<FunctionDecl *> FindFunctionTemplateSpecialization(
641 FunctionDecl *FromFD);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000642 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000643
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000644template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +0000645Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000646 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
647 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
Balazs Keri3b30d652018-10-19 13:32:20 +0000648 auto ToLAngleLocOrErr = import(FromLAngleLoc);
649 if (!ToLAngleLocOrErr)
650 return ToLAngleLocOrErr.takeError();
651 auto ToRAngleLocOrErr = import(FromRAngleLoc);
652 if (!ToRAngleLocOrErr)
653 return ToRAngleLocOrErr.takeError();
654
655 TemplateArgumentListInfo ToTAInfo(*ToLAngleLocOrErr, *ToRAngleLocOrErr);
656 if (auto Err = ImportTemplateArgumentListInfo(Container, ToTAInfo))
657 return Err;
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000658 Result = ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +0000659 return Error::success();
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000660}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000661
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000662template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000663Error ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000664 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
665 return ImportTemplateArgumentListInfo(
666 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
667}
668
669template <>
Balazs Keri3b30d652018-10-19 13:32:20 +0000670Error ASTNodeImporter::ImportTemplateArgumentListInfo<
671 ASTTemplateArgumentListInfo>(
672 const ASTTemplateArgumentListInfo &From,
673 TemplateArgumentListInfo &Result) {
674 return ImportTemplateArgumentListInfo(
675 From.LAngleLoc, From.RAngleLoc, From.arguments(), Result);
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000676}
677
Balazs Keri3b30d652018-10-19 13:32:20 +0000678Expected<ASTNodeImporter::FunctionTemplateAndArgsTy>
Gabor Marton5254e642018-06-27 13:32:50 +0000679ASTNodeImporter::ImportFunctionTemplateWithTemplateArgsFromSpecialization(
680 FunctionDecl *FromFD) {
681 assert(FromFD->getTemplatedKind() ==
Balazs Keri3b30d652018-10-19 13:32:20 +0000682 FunctionDecl::TK_FunctionTemplateSpecialization);
683
684 FunctionTemplateAndArgsTy Result;
685
Gabor Marton5254e642018-06-27 13:32:50 +0000686 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Balazs Keri3b30d652018-10-19 13:32:20 +0000687 if (Error Err = importInto(std::get<0>(Result), FTSInfo->getTemplate()))
688 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000689
690 // Import template arguments.
691 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
Balazs Keri3b30d652018-10-19 13:32:20 +0000692 if (Error Err = ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
693 std::get<1>(Result)))
694 return std::move(Err);
Gabor Marton5254e642018-06-27 13:32:50 +0000695
Balazs Keri3b30d652018-10-19 13:32:20 +0000696 return Result;
697}
698
699template <>
700Expected<TemplateParameterList *>
701ASTNodeImporter::import(TemplateParameterList *From) {
702 SmallVector<NamedDecl *, 4> To(From->size());
703 if (Error Err = ImportContainerChecked(*From, To))
704 return std::move(Err);
705
706 ExpectedExpr ToRequiresClause = import(From->getRequiresClause());
707 if (!ToRequiresClause)
708 return ToRequiresClause.takeError();
709
710 auto ToTemplateLocOrErr = import(From->getTemplateLoc());
711 if (!ToTemplateLocOrErr)
712 return ToTemplateLocOrErr.takeError();
713 auto ToLAngleLocOrErr = import(From->getLAngleLoc());
714 if (!ToLAngleLocOrErr)
715 return ToLAngleLocOrErr.takeError();
716 auto ToRAngleLocOrErr = import(From->getRAngleLoc());
717 if (!ToRAngleLocOrErr)
718 return ToRAngleLocOrErr.takeError();
719
720 return TemplateParameterList::Create(
721 Importer.getToContext(),
722 *ToTemplateLocOrErr,
723 *ToLAngleLocOrErr,
724 To,
725 *ToRAngleLocOrErr,
726 *ToRequiresClause);
727}
728
729template <>
730Expected<TemplateArgument>
731ASTNodeImporter::import(const TemplateArgument &From) {
732 switch (From.getKind()) {
733 case TemplateArgument::Null:
734 return TemplateArgument();
735
736 case TemplateArgument::Type: {
737 ExpectedType ToTypeOrErr = import(From.getAsType());
738 if (!ToTypeOrErr)
739 return ToTypeOrErr.takeError();
740 return TemplateArgument(*ToTypeOrErr);
741 }
742
743 case TemplateArgument::Integral: {
744 ExpectedType ToTypeOrErr = import(From.getIntegralType());
745 if (!ToTypeOrErr)
746 return ToTypeOrErr.takeError();
747 return TemplateArgument(From, *ToTypeOrErr);
748 }
749
750 case TemplateArgument::Declaration: {
751 Expected<ValueDecl *> ToOrErr = import(From.getAsDecl());
752 if (!ToOrErr)
753 return ToOrErr.takeError();
754 ExpectedType ToTypeOrErr = import(From.getParamTypeForDecl());
755 if (!ToTypeOrErr)
756 return ToTypeOrErr.takeError();
757 return TemplateArgument(*ToOrErr, *ToTypeOrErr);
758 }
759
760 case TemplateArgument::NullPtr: {
761 ExpectedType ToTypeOrErr = import(From.getNullPtrType());
762 if (!ToTypeOrErr)
763 return ToTypeOrErr.takeError();
764 return TemplateArgument(*ToTypeOrErr, /*isNullPtr*/true);
765 }
766
767 case TemplateArgument::Template: {
768 Expected<TemplateName> ToTemplateOrErr = import(From.getAsTemplate());
769 if (!ToTemplateOrErr)
770 return ToTemplateOrErr.takeError();
771
772 return TemplateArgument(*ToTemplateOrErr);
773 }
774
775 case TemplateArgument::TemplateExpansion: {
776 Expected<TemplateName> ToTemplateOrErr =
777 import(From.getAsTemplateOrTemplatePattern());
778 if (!ToTemplateOrErr)
779 return ToTemplateOrErr.takeError();
780
781 return TemplateArgument(
782 *ToTemplateOrErr, From.getNumTemplateExpansions());
783 }
784
785 case TemplateArgument::Expression:
786 if (ExpectedExpr ToExpr = import(From.getAsExpr()))
787 return TemplateArgument(*ToExpr);
788 else
789 return ToExpr.takeError();
790
791 case TemplateArgument::Pack: {
792 SmallVector<TemplateArgument, 2> ToPack;
793 ToPack.reserve(From.pack_size());
794 if (Error Err = ImportTemplateArguments(
795 From.pack_begin(), From.pack_size(), ToPack))
796 return std::move(Err);
797
798 return TemplateArgument(
799 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
800 }
801 }
802
803 llvm_unreachable("Invalid template argument kind");
804}
805
806template <>
807Expected<TemplateArgumentLoc>
808ASTNodeImporter::import(const TemplateArgumentLoc &TALoc) {
809 Expected<TemplateArgument> ArgOrErr = import(TALoc.getArgument());
810 if (!ArgOrErr)
811 return ArgOrErr.takeError();
812 TemplateArgument Arg = *ArgOrErr;
813
814 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
815
816 TemplateArgumentLocInfo ToInfo;
817 if (Arg.getKind() == TemplateArgument::Expression) {
818 ExpectedExpr E = import(FromInfo.getAsExpr());
819 if (!E)
820 return E.takeError();
821 ToInfo = TemplateArgumentLocInfo(*E);
822 } else if (Arg.getKind() == TemplateArgument::Type) {
823 if (auto TSIOrErr = import(FromInfo.getAsTypeSourceInfo()))
824 ToInfo = TemplateArgumentLocInfo(*TSIOrErr);
825 else
826 return TSIOrErr.takeError();
827 } else {
828 auto ToTemplateQualifierLocOrErr =
829 import(FromInfo.getTemplateQualifierLoc());
830 if (!ToTemplateQualifierLocOrErr)
831 return ToTemplateQualifierLocOrErr.takeError();
832 auto ToTemplateNameLocOrErr = import(FromInfo.getTemplateNameLoc());
833 if (!ToTemplateNameLocOrErr)
834 return ToTemplateNameLocOrErr.takeError();
835 auto ToTemplateEllipsisLocOrErr =
836 import(FromInfo.getTemplateEllipsisLoc());
837 if (!ToTemplateEllipsisLocOrErr)
838 return ToTemplateEllipsisLocOrErr.takeError();
839
840 ToInfo = TemplateArgumentLocInfo(
841 *ToTemplateQualifierLocOrErr,
842 *ToTemplateNameLocOrErr,
843 *ToTemplateEllipsisLocOrErr);
844 }
845
846 return TemplateArgumentLoc(Arg, ToInfo);
847}
848
849template <>
850Expected<DeclGroupRef> ASTNodeImporter::import(const DeclGroupRef &DG) {
851 if (DG.isNull())
852 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
853 size_t NumDecls = DG.end() - DG.begin();
854 SmallVector<Decl *, 1> ToDecls;
855 ToDecls.reserve(NumDecls);
856 for (Decl *FromD : DG) {
857 if (auto ToDOrErr = import(FromD))
858 ToDecls.push_back(*ToDOrErr);
859 else
860 return ToDOrErr.takeError();
861 }
862 return DeclGroupRef::Create(Importer.getToContext(),
863 ToDecls.begin(),
864 NumDecls);
865}
866
867template <>
868Expected<ASTNodeImporter::Designator>
869ASTNodeImporter::import(const Designator &D) {
870 if (D.isFieldDesignator()) {
871 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
872
873 ExpectedSLoc ToDotLocOrErr = import(D.getDotLoc());
874 if (!ToDotLocOrErr)
875 return ToDotLocOrErr.takeError();
876
877 ExpectedSLoc ToFieldLocOrErr = import(D.getFieldLoc());
878 if (!ToFieldLocOrErr)
879 return ToFieldLocOrErr.takeError();
880
881 return Designator(ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr);
882 }
883
884 ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc());
885 if (!ToLBracketLocOrErr)
886 return ToLBracketLocOrErr.takeError();
887
888 ExpectedSLoc ToRBracketLocOrErr = import(D.getRBracketLoc());
889 if (!ToRBracketLocOrErr)
890 return ToRBracketLocOrErr.takeError();
891
892 if (D.isArrayDesignator())
893 return Designator(D.getFirstExprIndex(),
894 *ToLBracketLocOrErr, *ToRBracketLocOrErr);
895
896 ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc());
897 if (!ToEllipsisLocOrErr)
898 return ToEllipsisLocOrErr.takeError();
899
900 assert(D.isArrayRangeDesignator());
901 return Designator(
902 D.getFirstExprIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr,
903 *ToRBracketLocOrErr);
904}
905
906template <>
907Expected<LambdaCapture> ASTNodeImporter::import(const LambdaCapture &From) {
908 VarDecl *Var = nullptr;
909 if (From.capturesVariable()) {
910 if (auto VarOrErr = import(From.getCapturedVar()))
911 Var = *VarOrErr;
912 else
913 return VarOrErr.takeError();
914 }
915
916 auto LocationOrErr = import(From.getLocation());
917 if (!LocationOrErr)
918 return LocationOrErr.takeError();
919
920 SourceLocation EllipsisLoc;
921 if (From.isPackExpansion())
922 if (Error Err = importInto(EllipsisLoc, From.getEllipsisLoc()))
923 return std::move(Err);
924
925 return LambdaCapture(
926 *LocationOrErr, From.isImplicit(), From.getCaptureKind(), Var,
927 EllipsisLoc);
Gabor Marton5254e642018-06-27 13:32:50 +0000928}
929
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000930} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000931
Douglas Gregor3996e242010-02-15 22:01:00 +0000932//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000933// Import Types
934//----------------------------------------------------------------------------
935
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000936using namespace clang;
937
Balazs Keri3b30d652018-10-19 13:32:20 +0000938ExpectedType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000939 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
940 << T->getTypeClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +0000941 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000942}
943
Balazs Keri3b30d652018-10-19 13:32:20 +0000944ExpectedType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
945 ExpectedType UnderlyingTypeOrErr = import(T->getValueType());
946 if (!UnderlyingTypeOrErr)
947 return UnderlyingTypeOrErr.takeError();
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000948
Balazs Keri3b30d652018-10-19 13:32:20 +0000949 return Importer.getToContext().getAtomicType(*UnderlyingTypeOrErr);
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000950}
951
Balazs Keri3b30d652018-10-19 13:32:20 +0000952ExpectedType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000953 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000954#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
955 case BuiltinType::Id: \
956 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000957#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +0000958#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
959 case BuiltinType::Id: \
960 return Importer.getToContext().Id##Ty;
961#include "clang/Basic/OpenCLExtensionTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000962#define SHARED_SINGLETON_TYPE(Expansion)
963#define BUILTIN_TYPE(Id, SingletonId) \
964 case BuiltinType::Id: return Importer.getToContext().SingletonId;
965#include "clang/AST/BuiltinTypes.def"
966
967 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
968 // context supports C++.
969
970 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
971 // context supports ObjC.
972
Douglas Gregor96e578d2010-02-05 17:54:41 +0000973 case BuiltinType::Char_U:
Fangrui Song6907ce22018-07-30 19:24:48 +0000974 // The context we're importing from has an unsigned 'char'. If we're
975 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +0000976 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000977 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000978 return Importer.getToContext().UnsignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +0000979
Douglas Gregor96e578d2010-02-05 17:54:41 +0000980 return Importer.getToContext().CharTy;
981
Douglas Gregor96e578d2010-02-05 17:54:41 +0000982 case BuiltinType::Char_S:
Fangrui Song6907ce22018-07-30 19:24:48 +0000983 // The context we're importing from has an unsigned 'char'. If we're
984 // importing into a context with a signed 'char', translate to
Douglas Gregor96e578d2010-02-05 17:54:41 +0000985 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000986 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000987 return Importer.getToContext().SignedCharTy;
Fangrui Song6907ce22018-07-30 19:24:48 +0000988
Douglas Gregor96e578d2010-02-05 17:54:41 +0000989 return Importer.getToContext().CharTy;
990
Chris Lattnerad3467e2010-12-25 23:25:43 +0000991 case BuiltinType::WChar_S:
992 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000993 // FIXME: If not in C++, shall we translate to the C equivalent of
994 // wchar_t?
995 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000996 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000997
998 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000999}
1000
Balazs Keri3b30d652018-10-19 13:32:20 +00001001ExpectedType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
1002 ExpectedType ToOriginalTypeOrErr = import(T->getOriginalType());
1003 if (!ToOriginalTypeOrErr)
1004 return ToOriginalTypeOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00001005
Balazs Keri3b30d652018-10-19 13:32:20 +00001006 return Importer.getToContext().getDecayedType(*ToOriginalTypeOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00001007}
1008
Balazs Keri3b30d652018-10-19 13:32:20 +00001009ExpectedType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
1010 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1011 if (!ToElementTypeOrErr)
1012 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001013
Balazs Keri3b30d652018-10-19 13:32:20 +00001014 return Importer.getToContext().getComplexType(*ToElementTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001015}
1016
Balazs Keri3b30d652018-10-19 13:32:20 +00001017ExpectedType ASTNodeImporter::VisitPointerType(const PointerType *T) {
1018 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1019 if (!ToPointeeTypeOrErr)
1020 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001021
Balazs Keri3b30d652018-10-19 13:32:20 +00001022 return Importer.getToContext().getPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001023}
1024
Balazs Keri3b30d652018-10-19 13:32:20 +00001025ExpectedType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001026 // FIXME: Check for blocks support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001027 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1028 if (!ToPointeeTypeOrErr)
1029 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001030
Balazs Keri3b30d652018-10-19 13:32:20 +00001031 return Importer.getToContext().getBlockPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001032}
1033
Balazs Keri3b30d652018-10-19 13:32:20 +00001034ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001035ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001036 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001037 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1038 if (!ToPointeeTypeOrErr)
1039 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001040
Balazs Keri3b30d652018-10-19 13:32:20 +00001041 return Importer.getToContext().getLValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001042}
1043
Balazs Keri3b30d652018-10-19 13:32:20 +00001044ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001045ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001046 // FIXME: Check for C++0x support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001047 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeTypeAsWritten());
1048 if (!ToPointeeTypeOrErr)
1049 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001050
Balazs Keri3b30d652018-10-19 13:32:20 +00001051 return Importer.getToContext().getRValueReferenceType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001052}
1053
Balazs Keri3b30d652018-10-19 13:32:20 +00001054ExpectedType
1055ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001056 // FIXME: Check for C++ support in "to" context.
Balazs Keri3b30d652018-10-19 13:32:20 +00001057 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1058 if (!ToPointeeTypeOrErr)
1059 return ToPointeeTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001060
Balazs Keri3b30d652018-10-19 13:32:20 +00001061 ExpectedType ClassTypeOrErr = import(QualType(T->getClass(), 0));
1062 if (!ClassTypeOrErr)
1063 return ClassTypeOrErr.takeError();
1064
1065 return Importer.getToContext().getMemberPointerType(
1066 *ToPointeeTypeOrErr, (*ClassTypeOrErr).getTypePtr());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001067}
1068
Balazs Keri3b30d652018-10-19 13:32:20 +00001069ExpectedType
1070ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
1071 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1072 if (!ToElementTypeOrErr)
1073 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001074
Balazs Keri3b30d652018-10-19 13:32:20 +00001075 return Importer.getToContext().getConstantArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001076 T->getSize(),
1077 T->getSizeModifier(),
1078 T->getIndexTypeCVRQualifiers());
1079}
1080
Balazs Keri3b30d652018-10-19 13:32:20 +00001081ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001082ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001083 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1084 if (!ToElementTypeOrErr)
1085 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001086
Balazs Keri3b30d652018-10-19 13:32:20 +00001087 return Importer.getToContext().getIncompleteArrayType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001088 T->getSizeModifier(),
1089 T->getIndexTypeCVRQualifiers());
1090}
1091
Balazs Keri3b30d652018-10-19 13:32:20 +00001092ExpectedType
1093ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
1094 QualType ToElementType;
1095 Expr *ToSizeExpr;
1096 SourceRange ToBracketsRange;
1097 if (auto Imp = importSeq(
1098 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1099 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1100 else
1101 return Imp.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001102
Balazs Keri3b30d652018-10-19 13:32:20 +00001103 return Importer.getToContext().getVariableArrayType(
1104 ToElementType, ToSizeExpr, T->getSizeModifier(),
1105 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001106}
1107
Balazs Keri3b30d652018-10-19 13:32:20 +00001108ExpectedType ASTNodeImporter::VisitDependentSizedArrayType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001109 const DependentSizedArrayType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001110 QualType ToElementType;
1111 Expr *ToSizeExpr;
1112 SourceRange ToBracketsRange;
1113 if (auto Imp = importSeq(
1114 T->getElementType(), T->getSizeExpr(), T->getBracketsRange()))
1115 std::tie(ToElementType, ToSizeExpr, ToBracketsRange) = *Imp;
1116 else
1117 return Imp.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001118 // SizeExpr may be null if size is not specified directly.
1119 // For example, 'int a[]'.
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001120
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001121 return Importer.getToContext().getDependentSizedArrayType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001122 ToElementType, ToSizeExpr, T->getSizeModifier(),
1123 T->getIndexTypeCVRQualifiers(), ToBracketsRange);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001124}
1125
Balazs Keri3b30d652018-10-19 13:32:20 +00001126ExpectedType ASTNodeImporter::VisitVectorType(const VectorType *T) {
1127 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1128 if (!ToElementTypeOrErr)
1129 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001130
Balazs Keri3b30d652018-10-19 13:32:20 +00001131 return Importer.getToContext().getVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001132 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001133 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001134}
1135
Balazs Keri3b30d652018-10-19 13:32:20 +00001136ExpectedType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
1137 ExpectedType ToElementTypeOrErr = import(T->getElementType());
1138 if (!ToElementTypeOrErr)
1139 return ToElementTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001140
Balazs Keri3b30d652018-10-19 13:32:20 +00001141 return Importer.getToContext().getExtVectorType(*ToElementTypeOrErr,
Douglas Gregor96e578d2010-02-05 17:54:41 +00001142 T->getNumElements());
1143}
1144
Balazs Keri3b30d652018-10-19 13:32:20 +00001145ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001146ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001147 // FIXME: What happens if we're importing a function without a prototype
Douglas Gregor96e578d2010-02-05 17:54:41 +00001148 // into C++? Should we make it variadic?
Balazs Keri3b30d652018-10-19 13:32:20 +00001149 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1150 if (!ToReturnTypeOrErr)
1151 return ToReturnTypeOrErr.takeError();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001152
Balazs Keri3b30d652018-10-19 13:32:20 +00001153 return Importer.getToContext().getFunctionNoProtoType(*ToReturnTypeOrErr,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001154 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001155}
1156
Balazs Keri3b30d652018-10-19 13:32:20 +00001157ExpectedType
1158ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
1159 ExpectedType ToReturnTypeOrErr = import(T->getReturnType());
1160 if (!ToReturnTypeOrErr)
1161 return ToReturnTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001162
Douglas Gregor96e578d2010-02-05 17:54:41 +00001163 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001164 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001165 for (const auto &A : T->param_types()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001166 ExpectedType TyOrErr = import(A);
1167 if (!TyOrErr)
1168 return TyOrErr.takeError();
1169 ArgTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001170 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001171
Douglas Gregor96e578d2010-02-05 17:54:41 +00001172 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001173 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001174 for (const auto &E : T->exceptions()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001175 ExpectedType TyOrErr = import(E);
1176 if (!TyOrErr)
1177 return TyOrErr.takeError();
1178 ExceptionTypes.push_back(*TyOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001179 }
John McCalldb40c7f2010-12-14 08:05:40 +00001180
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001181 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1182 FunctionProtoType::ExtProtoInfo ToEPI;
1183
Balazs Keri3b30d652018-10-19 13:32:20 +00001184 auto Imp = importSeq(
1185 FromEPI.ExceptionSpec.NoexceptExpr,
1186 FromEPI.ExceptionSpec.SourceDecl,
1187 FromEPI.ExceptionSpec.SourceTemplate);
1188 if (!Imp)
1189 return Imp.takeError();
1190
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001191 ToEPI.ExtInfo = FromEPI.ExtInfo;
1192 ToEPI.Variadic = FromEPI.Variadic;
1193 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1194 ToEPI.TypeQuals = FromEPI.TypeQuals;
1195 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001196 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1197 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
Balazs Keri3b30d652018-10-19 13:32:20 +00001198 std::tie(
1199 ToEPI.ExceptionSpec.NoexceptExpr,
1200 ToEPI.ExceptionSpec.SourceDecl,
1201 ToEPI.ExceptionSpec.SourceTemplate) = *Imp;
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001202
Balazs Keri3b30d652018-10-19 13:32:20 +00001203 return Importer.getToContext().getFunctionType(
1204 *ToReturnTypeOrErr, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001205}
1206
Balazs Keri3b30d652018-10-19 13:32:20 +00001207ExpectedType ASTNodeImporter::VisitUnresolvedUsingType(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001208 const UnresolvedUsingType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001209 UnresolvedUsingTypenameDecl *ToD;
1210 Decl *ToPrevD;
1211 if (auto Imp = importSeq(T->getDecl(), T->getDecl()->getPreviousDecl()))
1212 std::tie(ToD, ToPrevD) = *Imp;
1213 else
1214 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001215
Balazs Keri3b30d652018-10-19 13:32:20 +00001216 return Importer.getToContext().getTypeDeclType(
1217 ToD, cast_or_null<TypeDecl>(ToPrevD));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001218}
1219
Balazs Keri3b30d652018-10-19 13:32:20 +00001220ExpectedType ASTNodeImporter::VisitParenType(const ParenType *T) {
1221 ExpectedType ToInnerTypeOrErr = import(T->getInnerType());
1222 if (!ToInnerTypeOrErr)
1223 return ToInnerTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001224
Balazs Keri3b30d652018-10-19 13:32:20 +00001225 return Importer.getToContext().getParenType(*ToInnerTypeOrErr);
Sean Callananda6df8a2011-08-11 16:56:07 +00001226}
1227
Balazs Keri3b30d652018-10-19 13:32:20 +00001228ExpectedType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
1229 Expected<TypedefNameDecl *> ToDeclOrErr = import(T->getDecl());
1230 if (!ToDeclOrErr)
1231 return ToDeclOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001232
Balazs Keri3b30d652018-10-19 13:32:20 +00001233 return Importer.getToContext().getTypeDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001234}
1235
Balazs Keri3b30d652018-10-19 13:32:20 +00001236ExpectedType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
1237 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1238 if (!ToExprOrErr)
1239 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001240
Balazs Keri3b30d652018-10-19 13:32:20 +00001241 return Importer.getToContext().getTypeOfExprType(*ToExprOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001242}
1243
Balazs Keri3b30d652018-10-19 13:32:20 +00001244ExpectedType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
1245 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1246 if (!ToUnderlyingTypeOrErr)
1247 return ToUnderlyingTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001248
Balazs Keri3b30d652018-10-19 13:32:20 +00001249 return Importer.getToContext().getTypeOfType(*ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001250}
1251
Balazs Keri3b30d652018-10-19 13:32:20 +00001252ExpectedType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001253 // FIXME: Make sure that the "to" context supports C++0x!
Balazs Keri3b30d652018-10-19 13:32:20 +00001254 ExpectedExpr ToExprOrErr = import(T->getUnderlyingExpr());
1255 if (!ToExprOrErr)
1256 return ToExprOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001257
Balazs Keri3b30d652018-10-19 13:32:20 +00001258 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1259 if (!ToUnderlyingTypeOrErr)
1260 return ToUnderlyingTypeOrErr.takeError();
Douglas Gregor81495f32012-02-12 18:42:33 +00001261
Balazs Keri3b30d652018-10-19 13:32:20 +00001262 return Importer.getToContext().getDecltypeType(
1263 *ToExprOrErr, *ToUnderlyingTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001264}
1265
Balazs Keri3b30d652018-10-19 13:32:20 +00001266ExpectedType
1267ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1268 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1269 if (!ToBaseTypeOrErr)
1270 return ToBaseTypeOrErr.takeError();
Alexis Hunte852b102011-05-24 22:41:36 +00001271
Balazs Keri3b30d652018-10-19 13:32:20 +00001272 ExpectedType ToUnderlyingTypeOrErr = import(T->getUnderlyingType());
1273 if (!ToUnderlyingTypeOrErr)
1274 return ToUnderlyingTypeOrErr.takeError();
1275
1276 return Importer.getToContext().getUnaryTransformType(
1277 *ToBaseTypeOrErr, *ToUnderlyingTypeOrErr, T->getUTTKind());
Alexis Hunte852b102011-05-24 22:41:36 +00001278}
1279
Balazs Keri3b30d652018-10-19 13:32:20 +00001280ExpectedType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001281 // FIXME: Make sure that the "to" context supports C++11!
Balazs Keri3b30d652018-10-19 13:32:20 +00001282 ExpectedType ToDeducedTypeOrErr = import(T->getDeducedType());
1283 if (!ToDeducedTypeOrErr)
1284 return ToDeducedTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001285
Balazs Keri3b30d652018-10-19 13:32:20 +00001286 return Importer.getToContext().getAutoType(*ToDeducedTypeOrErr,
1287 T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001288 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001289}
1290
Balazs Keri3b30d652018-10-19 13:32:20 +00001291ExpectedType ASTNodeImporter::VisitInjectedClassNameType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001292 const InjectedClassNameType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001293 Expected<CXXRecordDecl *> ToDeclOrErr = import(T->getDecl());
1294 if (!ToDeclOrErr)
1295 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001296
Balazs Keri3b30d652018-10-19 13:32:20 +00001297 ExpectedType ToInjTypeOrErr = import(T->getInjectedSpecializationType());
1298 if (!ToInjTypeOrErr)
1299 return ToInjTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001300
1301 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1302 // See comments in InjectedClassNameType definition for details
1303 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1304 enum {
1305 TypeAlignmentInBits = 4,
1306 TypeAlignment = 1 << TypeAlignmentInBits
1307 };
1308
1309 return QualType(new (Importer.getToContext(), TypeAlignment)
Balazs Keri3b30d652018-10-19 13:32:20 +00001310 InjectedClassNameType(*ToDeclOrErr, *ToInjTypeOrErr), 0);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001311}
1312
Balazs Keri3b30d652018-10-19 13:32:20 +00001313ExpectedType ASTNodeImporter::VisitRecordType(const RecordType *T) {
1314 Expected<RecordDecl *> ToDeclOrErr = import(T->getDecl());
1315 if (!ToDeclOrErr)
1316 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001317
Balazs Keri3b30d652018-10-19 13:32:20 +00001318 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001319}
1320
Balazs Keri3b30d652018-10-19 13:32:20 +00001321ExpectedType ASTNodeImporter::VisitEnumType(const EnumType *T) {
1322 Expected<EnumDecl *> ToDeclOrErr = import(T->getDecl());
1323 if (!ToDeclOrErr)
1324 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001325
Balazs Keri3b30d652018-10-19 13:32:20 +00001326 return Importer.getToContext().getTagDeclType(*ToDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001327}
1328
Balazs Keri3b30d652018-10-19 13:32:20 +00001329ExpectedType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1330 ExpectedType ToModifiedTypeOrErr = import(T->getModifiedType());
1331 if (!ToModifiedTypeOrErr)
1332 return ToModifiedTypeOrErr.takeError();
1333 ExpectedType ToEquivalentTypeOrErr = import(T->getEquivalentType());
1334 if (!ToEquivalentTypeOrErr)
1335 return ToEquivalentTypeOrErr.takeError();
Sean Callanan72fe0852015-04-02 23:50:08 +00001336
1337 return Importer.getToContext().getAttributedType(T->getAttrKind(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001338 *ToModifiedTypeOrErr, *ToEquivalentTypeOrErr);
Sean Callanan72fe0852015-04-02 23:50:08 +00001339}
1340
Balazs Keri3b30d652018-10-19 13:32:20 +00001341ExpectedType ASTNodeImporter::VisitTemplateTypeParmType(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001342 const TemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001343 Expected<TemplateTypeParmDecl *> ToDeclOrErr = import(T->getDecl());
1344 if (!ToDeclOrErr)
1345 return ToDeclOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001346
1347 return Importer.getToContext().getTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001348 T->getDepth(), T->getIndex(), T->isParameterPack(), *ToDeclOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001349}
1350
Balazs Keri3b30d652018-10-19 13:32:20 +00001351ExpectedType ASTNodeImporter::VisitSubstTemplateTypeParmType(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001352 const SubstTemplateTypeParmType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001353 ExpectedType ReplacedOrErr = import(QualType(T->getReplacedParameter(), 0));
1354 if (!ReplacedOrErr)
1355 return ReplacedOrErr.takeError();
1356 const TemplateTypeParmType *Replaced =
1357 cast<TemplateTypeParmType>((*ReplacedOrErr).getTypePtr());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001358
Balazs Keri3b30d652018-10-19 13:32:20 +00001359 ExpectedType ToReplacementTypeOrErr = import(T->getReplacementType());
1360 if (!ToReplacementTypeOrErr)
1361 return ToReplacementTypeOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001362
1363 return Importer.getToContext().getSubstTemplateTypeParmType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001364 Replaced, (*ToReplacementTypeOrErr).getCanonicalType());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00001365}
1366
Balazs Keri3b30d652018-10-19 13:32:20 +00001367ExpectedType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001368 const TemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001369 auto ToTemplateOrErr = import(T->getTemplateName());
1370 if (!ToTemplateOrErr)
1371 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001372
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001373 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00001374 if (Error Err = ImportTemplateArguments(
1375 T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1376 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00001377
Douglas Gregore2e50d332010-12-01 01:36:18 +00001378 QualType ToCanonType;
1379 if (!QualType(T, 0).isCanonical()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001380 QualType FromCanonType
Douglas Gregore2e50d332010-12-01 01:36:18 +00001381 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
Balazs Keri3b30d652018-10-19 13:32:20 +00001382 if (ExpectedType TyOrErr = import(FromCanonType))
1383 ToCanonType = *TyOrErr;
1384 else
1385 return TyOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001386 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001387 return Importer.getToContext().getTemplateSpecializationType(*ToTemplateOrErr,
David Majnemer6fbeee32016-07-07 04:43:07 +00001388 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +00001389 ToCanonType);
1390}
1391
Balazs Keri3b30d652018-10-19 13:32:20 +00001392ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001393 // Note: the qualifier in an ElaboratedType is optional.
Balazs Keri3b30d652018-10-19 13:32:20 +00001394 auto ToQualifierOrErr = import(T->getQualifier());
1395 if (!ToQualifierOrErr)
1396 return ToQualifierOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001397
Balazs Keri3b30d652018-10-19 13:32:20 +00001398 ExpectedType ToNamedTypeOrErr = import(T->getNamedType());
1399 if (!ToNamedTypeOrErr)
1400 return ToNamedTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001401
Balazs Keri3b30d652018-10-19 13:32:20 +00001402 Expected<TagDecl *> ToOwnedTagDeclOrErr = import(T->getOwnedTagDecl());
1403 if (!ToOwnedTagDeclOrErr)
1404 return ToOwnedTagDeclOrErr.takeError();
Joel E. Denny7509a2f2018-05-14 19:36:45 +00001405
Abramo Bagnara6150c882010-05-11 21:36:43 +00001406 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Balazs Keri3b30d652018-10-19 13:32:20 +00001407 *ToQualifierOrErr,
1408 *ToNamedTypeOrErr,
1409 *ToOwnedTagDeclOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001410}
1411
Balazs Keri3b30d652018-10-19 13:32:20 +00001412ExpectedType
1413ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
1414 ExpectedType ToPatternOrErr = import(T->getPattern());
1415 if (!ToPatternOrErr)
1416 return ToPatternOrErr.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00001417
Balazs Keri3b30d652018-10-19 13:32:20 +00001418 return Importer.getToContext().getPackExpansionType(*ToPatternOrErr,
Gabor Horvath7a91c082017-11-14 11:30:38 +00001419 T->getNumExpansions());
1420}
1421
Balazs Keri3b30d652018-10-19 13:32:20 +00001422ExpectedType ASTNodeImporter::VisitDependentTemplateSpecializationType(
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001423 const DependentTemplateSpecializationType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001424 auto ToQualifierOrErr = import(T->getQualifier());
1425 if (!ToQualifierOrErr)
1426 return ToQualifierOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001427
Balazs Keri3b30d652018-10-19 13:32:20 +00001428 IdentifierInfo *ToName = Importer.Import(T->getIdentifier());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001429
1430 SmallVector<TemplateArgument, 2> ToPack;
1431 ToPack.reserve(T->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00001432 if (Error Err = ImportTemplateArguments(
1433 T->getArgs(), T->getNumArgs(), ToPack))
1434 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001435
1436 return Importer.getToContext().getDependentTemplateSpecializationType(
Balazs Keri3b30d652018-10-19 13:32:20 +00001437 T->getKeyword(), *ToQualifierOrErr, ToName, ToPack);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00001438}
1439
Balazs Keri3b30d652018-10-19 13:32:20 +00001440ExpectedType
1441ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
1442 auto ToQualifierOrErr = import(T->getQualifier());
1443 if (!ToQualifierOrErr)
1444 return ToQualifierOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00001445
1446 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
Peter Szecsice7f3182018-05-07 12:08:27 +00001447
Balazs Keri3b30d652018-10-19 13:32:20 +00001448 QualType Canon;
1449 if (T != T->getCanonicalTypeInternal().getTypePtr()) {
1450 if (ExpectedType TyOrErr = import(T->getCanonicalTypeInternal()))
1451 Canon = (*TyOrErr).getCanonicalType();
1452 else
1453 return TyOrErr.takeError();
1454 }
Peter Szecsice7f3182018-05-07 12:08:27 +00001455
Balazs Keri3b30d652018-10-19 13:32:20 +00001456 return Importer.getToContext().getDependentNameType(T->getKeyword(),
1457 *ToQualifierOrErr,
Peter Szecsice7f3182018-05-07 12:08:27 +00001458 Name, Canon);
1459}
1460
Balazs Keri3b30d652018-10-19 13:32:20 +00001461ExpectedType
1462ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
1463 Expected<ObjCInterfaceDecl *> ToDeclOrErr = import(T->getDecl());
1464 if (!ToDeclOrErr)
1465 return ToDeclOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001466
Balazs Keri3b30d652018-10-19 13:32:20 +00001467 return Importer.getToContext().getObjCInterfaceType(*ToDeclOrErr);
John McCall8b07ec22010-05-15 11:32:37 +00001468}
1469
Balazs Keri3b30d652018-10-19 13:32:20 +00001470ExpectedType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
1471 ExpectedType ToBaseTypeOrErr = import(T->getBaseType());
1472 if (!ToBaseTypeOrErr)
1473 return ToBaseTypeOrErr.takeError();
John McCall8b07ec22010-05-15 11:32:37 +00001474
Douglas Gregore9d95f12015-07-07 03:57:35 +00001475 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001476 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001477 if (ExpectedType TyOrErr = import(TypeArg))
1478 TypeArgs.push_back(*TyOrErr);
1479 else
1480 return TyOrErr.takeError();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001481 }
1482
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001483 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001484 for (auto *P : T->quals()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001485 if (Expected<ObjCProtocolDecl *> ProtocolOrErr = import(P))
1486 Protocols.push_back(*ProtocolOrErr);
1487 else
1488 return ProtocolOrErr.takeError();
1489
Douglas Gregor96e578d2010-02-05 17:54:41 +00001490 }
1491
Balazs Keri3b30d652018-10-19 13:32:20 +00001492 return Importer.getToContext().getObjCObjectType(*ToBaseTypeOrErr, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001493 Protocols,
1494 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001495}
1496
Balazs Keri3b30d652018-10-19 13:32:20 +00001497ExpectedType
John McCall424cec92011-01-19 06:33:43 +00001498ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001499 ExpectedType ToPointeeTypeOrErr = import(T->getPointeeType());
1500 if (!ToPointeeTypeOrErr)
1501 return ToPointeeTypeOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001502
Balazs Keri3b30d652018-10-19 13:32:20 +00001503 return Importer.getToContext().getObjCObjectPointerType(*ToPointeeTypeOrErr);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001504}
1505
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001506//----------------------------------------------------------------------------
1507// Import Declarations
1508//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00001509Error ASTNodeImporter::ImportDeclParts(
1510 NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC,
1511 DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) {
Gabor Marton6e1510c2018-07-12 11:50:21 +00001512 // Check if RecordDecl is in FunctionDecl parameters to avoid infinite loop.
1513 // example: int struct_in_proto(struct data_t{int a;int b;} *d);
1514 DeclContext *OrigDC = D->getDeclContext();
1515 FunctionDecl *FunDecl;
1516 if (isa<RecordDecl>(D) && (FunDecl = dyn_cast<FunctionDecl>(OrigDC)) &&
1517 FunDecl->hasBody()) {
Gabor Martonfe68e292018-08-06 14:38:37 +00001518 auto getLeafPointeeType = [](const Type *T) {
1519 while (T->isPointerType() || T->isArrayType()) {
1520 T = T->getPointeeOrArrayElementType();
1521 }
1522 return T;
1523 };
1524 for (const ParmVarDecl *P : FunDecl->parameters()) {
1525 const Type *LeafT =
1526 getLeafPointeeType(P->getType().getCanonicalType().getTypePtr());
1527 auto *RT = dyn_cast<RecordType>(LeafT);
1528 if (RT && RT->getDecl() == D) {
1529 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
1530 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00001531 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Gabor Martonfe68e292018-08-06 14:38:37 +00001532 }
Gabor Marton6e1510c2018-07-12 11:50:21 +00001533 }
1534 }
1535
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001536 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001537 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
1538 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001539
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001540 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001541 if (Error Err = importInto(Name, D->getDeclName()))
1542 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001543
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001544 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00001545 if (Error Err = importInto(Loc, D->getLocation()))
1546 return Err;
1547
Sean Callanan59721b32015-04-28 18:41:46 +00001548 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Gabor Martonbe77a982018-12-12 11:22:55 +00001549 if (ToD)
1550 if (Error Err = ASTNodeImporter(*this).ImportDefinitionIfNeeded(D, ToD))
1551 return Err;
Balazs Keri3b30d652018-10-19 13:32:20 +00001552
1553 return Error::success();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001554}
1555
Balazs Keri3b30d652018-10-19 13:32:20 +00001556Error ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001557 if (!FromD)
Balazs Keri3b30d652018-10-19 13:32:20 +00001558 return Error::success();
Fangrui Song6907ce22018-07-30 19:24:48 +00001559
Balazs Keri3b30d652018-10-19 13:32:20 +00001560 if (!ToD)
1561 if (Error Err = importInto(ToD, FromD))
1562 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001563
Balazs Keri3b30d652018-10-19 13:32:20 +00001564 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1565 if (RecordDecl *ToRecord = cast<RecordDecl>(ToD)) {
1566 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() &&
1567 !ToRecord->getDefinition()) {
1568 if (Error Err = ImportDefinition(FromRecord, ToRecord))
1569 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001570 }
1571 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001572 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001573 }
1574
Balazs Keri3b30d652018-10-19 13:32:20 +00001575 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1576 if (EnumDecl *ToEnum = cast<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001577 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001578 if (Error Err = ImportDefinition(FromEnum, ToEnum))
1579 return Err;
Douglas Gregord451ea92011-07-29 23:31:30 +00001580 }
1581 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001582 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001583 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001584
1585 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001586}
1587
Balazs Keri3b30d652018-10-19 13:32:20 +00001588Error
1589ASTNodeImporter::ImportDeclarationNameLoc(
1590 const DeclarationNameInfo &From, DeclarationNameInfo& To) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001591 // NOTE: To.Name and To.Loc are already imported.
1592 // We only have to import To.LocInfo.
1593 switch (To.getName().getNameKind()) {
1594 case DeclarationName::Identifier:
1595 case DeclarationName::ObjCZeroArgSelector:
1596 case DeclarationName::ObjCOneArgSelector:
1597 case DeclarationName::ObjCMultiArgSelector:
1598 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001599 case DeclarationName::CXXDeductionGuideName:
Balazs Keri3b30d652018-10-19 13:32:20 +00001600 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001601
1602 case DeclarationName::CXXOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001603 if (auto ToRangeOrErr = import(From.getCXXOperatorNameRange()))
1604 To.setCXXOperatorNameRange(*ToRangeOrErr);
1605 else
1606 return ToRangeOrErr.takeError();
1607 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001608 }
1609 case DeclarationName::CXXLiteralOperatorName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001610 if (ExpectedSLoc LocOrErr = import(From.getCXXLiteralOperatorNameLoc()))
1611 To.setCXXLiteralOperatorNameLoc(*LocOrErr);
1612 else
1613 return LocOrErr.takeError();
1614 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001615 }
1616 case DeclarationName::CXXConstructorName:
1617 case DeclarationName::CXXDestructorName:
1618 case DeclarationName::CXXConversionFunctionName: {
Balazs Keri3b30d652018-10-19 13:32:20 +00001619 if (auto ToTInfoOrErr = import(From.getNamedTypeInfo()))
1620 To.setNamedTypeInfo(*ToTInfoOrErr);
1621 else
1622 return ToTInfoOrErr.takeError();
1623 return Error::success();
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001624 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001625 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001626 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001627}
1628
Balazs Keri3b30d652018-10-19 13:32:20 +00001629Error
1630ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001631 if (Importer.isMinimalImport() && !ForceImport) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001632 auto ToDCOrErr = Importer.ImportContext(FromDC);
1633 return ToDCOrErr.takeError();
1634 }
Davide Italiano93a64ef2018-10-30 20:46:29 +00001635 llvm::SmallVector<Decl *, 8> ImportedDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00001636 for (auto *From : FromDC->decls()) {
1637 ExpectedDecl ImportedOrErr = import(From);
Davide Italiano93a64ef2018-10-30 20:46:29 +00001638 if (!ImportedOrErr)
Balazs Keri3b30d652018-10-19 13:32:20 +00001639 // Ignore the error, continue with next Decl.
1640 // FIXME: Handle this case somehow better.
Davide Italiano93a64ef2018-10-30 20:46:29 +00001641 consumeError(ImportedOrErr.takeError());
Douglas Gregor0a791672011-01-18 03:11:38 +00001642 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001643
Balazs Keri3b30d652018-10-19 13:32:20 +00001644 return Error::success();
Douglas Gregor968d6332010-02-21 18:24:45 +00001645}
1646
Balazs Keri3b30d652018-10-19 13:32:20 +00001647Error ASTNodeImporter::ImportDeclContext(
1648 Decl *FromD, DeclContext *&ToDC, DeclContext *&ToLexicalDC) {
1649 auto ToDCOrErr = Importer.ImportContext(FromD->getDeclContext());
1650 if (!ToDCOrErr)
1651 return ToDCOrErr.takeError();
1652 ToDC = *ToDCOrErr;
1653
1654 if (FromD->getDeclContext() != FromD->getLexicalDeclContext()) {
1655 auto ToLexicalDCOrErr = Importer.ImportContext(
1656 FromD->getLexicalDeclContext());
1657 if (!ToLexicalDCOrErr)
1658 return ToLexicalDCOrErr.takeError();
1659 ToLexicalDC = *ToLexicalDCOrErr;
1660 } else
1661 ToLexicalDC = ToDC;
1662
1663 return Error::success();
1664}
1665
1666Error ASTNodeImporter::ImportImplicitMethods(
Balazs Keri1d20cc22018-07-16 12:16:39 +00001667 const CXXRecordDecl *From, CXXRecordDecl *To) {
1668 assert(From->isCompleteDefinition() && To->getDefinition() == To &&
1669 "Import implicit methods to or from non-definition");
Fangrui Song6907ce22018-07-30 19:24:48 +00001670
Balazs Keri1d20cc22018-07-16 12:16:39 +00001671 for (CXXMethodDecl *FromM : From->methods())
Balazs Keri3b30d652018-10-19 13:32:20 +00001672 if (FromM->isImplicit()) {
1673 Expected<CXXMethodDecl *> ToMOrErr = import(FromM);
1674 if (!ToMOrErr)
1675 return ToMOrErr.takeError();
1676 }
1677
1678 return Error::success();
Balazs Keri1d20cc22018-07-16 12:16:39 +00001679}
1680
Balazs Keri3b30d652018-10-19 13:32:20 +00001681static Error setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1682 ASTImporter &Importer) {
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001683 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
Balazs Keri57949eb2019-03-25 09:16:39 +00001684 if (ExpectedDecl ToTypedefOrErr = Importer.Import_New(FromTypedef))
1685 To->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(*ToTypedefOrErr));
1686 else
1687 return ToTypedefOrErr.takeError();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001688 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001689 return Error::success();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001690}
1691
Balazs Keri3b30d652018-10-19 13:32:20 +00001692Error ASTNodeImporter::ImportDefinition(
1693 RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor95d82832012-01-24 18:36:04 +00001694 if (To->getDefinition() || To->isBeingDefined()) {
1695 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001696 return ImportDeclContext(From, /*ForceImport=*/true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001697
Balazs Keri3b30d652018-10-19 13:32:20 +00001698 return Error::success();
Douglas Gregor95d82832012-01-24 18:36:04 +00001699 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001700
Douglas Gregore2e50d332010-12-01 01:36:18 +00001701 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001702
Balazs Keri3b30d652018-10-19 13:32:20 +00001703 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1704 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001705
Douglas Gregore2e50d332010-12-01 01:36:18 +00001706 // Add base classes.
Gabor Marton17d39672018-11-26 15:54:08 +00001707 auto *ToCXX = dyn_cast<CXXRecordDecl>(To);
1708 auto *FromCXX = dyn_cast<CXXRecordDecl>(From);
1709 if (ToCXX && FromCXX && ToCXX->dataPtr() && FromCXX->dataPtr()) {
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001710
1711 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1712 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1713 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001714 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001715 ToData.Aggregate = FromData.Aggregate;
1716 ToData.PlainOldData = FromData.PlainOldData;
1717 ToData.Empty = FromData.Empty;
1718 ToData.Polymorphic = FromData.Polymorphic;
1719 ToData.Abstract = FromData.Abstract;
1720 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001721 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1722 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1723 ToData.HasBasesWithNonStaticDataMembers =
1724 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001725 ToData.HasPrivateFields = FromData.HasPrivateFields;
1726 ToData.HasProtectedFields = FromData.HasProtectedFields;
1727 ToData.HasPublicFields = FromData.HasPublicFields;
1728 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001729 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001730 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001731 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001732 ToData.HasUninitializedReferenceMember
1733 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001734 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001735 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1736 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001737 ToData.NeedOverloadResolutionForCopyConstructor
1738 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001739 ToData.NeedOverloadResolutionForMoveConstructor
1740 = FromData.NeedOverloadResolutionForMoveConstructor;
1741 ToData.NeedOverloadResolutionForMoveAssignment
1742 = FromData.NeedOverloadResolutionForMoveAssignment;
1743 ToData.NeedOverloadResolutionForDestructor
1744 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001745 ToData.DefaultedCopyConstructorIsDeleted
1746 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001747 ToData.DefaultedMoveConstructorIsDeleted
1748 = FromData.DefaultedMoveConstructorIsDeleted;
1749 ToData.DefaultedMoveAssignmentIsDeleted
1750 = FromData.DefaultedMoveAssignmentIsDeleted;
1751 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001752 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1753 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001754 ToData.HasConstexprNonCopyMoveConstructor
1755 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001756 ToData.HasDefaultedDefaultConstructor
1757 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001758 ToData.DefaultedDefaultConstructorIsConstexpr
1759 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001760 ToData.HasConstexprDefaultConstructor
1761 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001762 ToData.HasNonLiteralTypeFieldsOrBases
1763 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001764 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001765 ToData.UserProvidedDefaultConstructor
1766 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001767 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001768 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1769 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1770 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1771 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001772 ToData.ImplicitCopyAssignmentHasConstParam
1773 = FromData.ImplicitCopyAssignmentHasConstParam;
1774 ToData.HasDeclaredCopyConstructorWithConstParam
1775 = FromData.HasDeclaredCopyConstructorWithConstParam;
1776 ToData.HasDeclaredCopyAssignmentWithConstParam
1777 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001778
Shafik Yaghmour16b90732019-04-26 18:51:28 +00001779 // Copy over the data stored in RecordDeclBits
1780 ToCXX->setArgPassingRestrictions(FromCXX->getArgPassingRestrictions());
1781
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001782 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001783 for (const auto &Base1 : FromCXX->bases()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001784 ExpectedType TyOrErr = import(Base1.getType());
1785 if (!TyOrErr)
1786 return TyOrErr.takeError();
Douglas Gregor752a5952011-01-03 22:36:02 +00001787
1788 SourceLocation EllipsisLoc;
Balazs Keri3b30d652018-10-19 13:32:20 +00001789 if (Base1.isPackExpansion()) {
1790 if (ExpectedSLoc LocOrErr = import(Base1.getEllipsisLoc()))
1791 EllipsisLoc = *LocOrErr;
1792 else
1793 return LocOrErr.takeError();
1794 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001795
1796 // Ensure that we have a definition for the base.
Balazs Keri3b30d652018-10-19 13:32:20 +00001797 if (Error Err =
1798 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()))
1799 return Err;
1800
1801 auto RangeOrErr = import(Base1.getSourceRange());
1802 if (!RangeOrErr)
1803 return RangeOrErr.takeError();
1804
1805 auto TSIOrErr = import(Base1.getTypeSourceInfo());
1806 if (!TSIOrErr)
1807 return TSIOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001808
Douglas Gregore2e50d332010-12-01 01:36:18 +00001809 Bases.push_back(
Balazs Keri3b30d652018-10-19 13:32:20 +00001810 new (Importer.getToContext()) CXXBaseSpecifier(
1811 *RangeOrErr,
1812 Base1.isVirtual(),
1813 Base1.isBaseOfClass(),
1814 Base1.getAccessSpecifierAsWritten(),
1815 *TSIOrErr,
1816 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001817 }
1818 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001819 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001820 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001821
Douglas Gregor2e15c842012-02-01 21:00:38 +00001822 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001823 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1824 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001825
Douglas Gregore2e50d332010-12-01 01:36:18 +00001826 To->completeDefinition();
Balazs Keri3b30d652018-10-19 13:32:20 +00001827 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001828}
1829
Balazs Keri3b30d652018-10-19 13:32:20 +00001830Error ASTNodeImporter::ImportInitializer(VarDecl *From, VarDecl *To) {
Sean Callanan59721b32015-04-28 18:41:46 +00001831 if (To->getAnyInitializer())
Balazs Keri3b30d652018-10-19 13:32:20 +00001832 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001833
Gabor Martonac3a5d62018-09-17 12:04:52 +00001834 Expr *FromInit = From->getInit();
1835 if (!FromInit)
Balazs Keri3b30d652018-10-19 13:32:20 +00001836 return Error::success();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001837
Balazs Keri3b30d652018-10-19 13:32:20 +00001838 ExpectedExpr ToInitOrErr = import(FromInit);
1839 if (!ToInitOrErr)
1840 return ToInitOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00001841
Balazs Keri3b30d652018-10-19 13:32:20 +00001842 To->setInit(*ToInitOrErr);
Gabor Martonac3a5d62018-09-17 12:04:52 +00001843 if (From->isInitKnownICE()) {
1844 EvaluatedStmt *Eval = To->ensureEvaluatedStmt();
1845 Eval->CheckedICE = true;
1846 Eval->IsICE = From->isInitICE();
1847 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001848
1849 // FIXME: Other bits to merge?
Balazs Keri3b30d652018-10-19 13:32:20 +00001850 return Error::success();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001851}
1852
Balazs Keri3b30d652018-10-19 13:32:20 +00001853Error ASTNodeImporter::ImportDefinition(
1854 EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00001855 if (To->getDefinition() || To->isBeingDefined()) {
1856 if (Kind == IDK_Everything)
Balazs Keri3b30d652018-10-19 13:32:20 +00001857 return ImportDeclContext(From, /*ForceImport=*/true);
1858 return Error::success();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001859 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001860
Douglas Gregord451ea92011-07-29 23:31:30 +00001861 To->startDefinition();
1862
Balazs Keri3b30d652018-10-19 13:32:20 +00001863 if (Error Err = setTypedefNameForAnonDecl(From, To, Importer))
1864 return Err;
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001865
Balazs Keri3b30d652018-10-19 13:32:20 +00001866 ExpectedType ToTypeOrErr =
1867 import(Importer.getFromContext().getTypeDeclType(From));
1868 if (!ToTypeOrErr)
1869 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00001870
Balazs Keri3b30d652018-10-19 13:32:20 +00001871 ExpectedType ToPromotionTypeOrErr = import(From->getPromotionType());
1872 if (!ToPromotionTypeOrErr)
1873 return ToPromotionTypeOrErr.takeError();
Douglas Gregor2e15c842012-02-01 21:00:38 +00001874
1875 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00001876 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
1877 return Err;
Fangrui Song6907ce22018-07-30 19:24:48 +00001878
Douglas Gregord451ea92011-07-29 23:31:30 +00001879 // FIXME: we might need to merge the number of positive or negative bits
1880 // if the enumerator lists don't match.
Balazs Keri3b30d652018-10-19 13:32:20 +00001881 To->completeDefinition(*ToTypeOrErr, *ToPromotionTypeOrErr,
Douglas Gregord451ea92011-07-29 23:31:30 +00001882 From->getNumPositiveBits(),
1883 From->getNumNegativeBits());
Balazs Keri3b30d652018-10-19 13:32:20 +00001884 return Error::success();
Douglas Gregord451ea92011-07-29 23:31:30 +00001885}
1886
Balazs Keri3b30d652018-10-19 13:32:20 +00001887Error ASTNodeImporter::ImportTemplateArguments(
1888 const TemplateArgument *FromArgs, unsigned NumFromArgs,
1889 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001890 for (unsigned I = 0; I != NumFromArgs; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001891 if (auto ToOrErr = import(FromArgs[I]))
1892 ToArgs.push_back(*ToOrErr);
1893 else
1894 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001895 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001896
Balazs Keri3b30d652018-10-19 13:32:20 +00001897 return Error::success();
Douglas Gregore2e50d332010-12-01 01:36:18 +00001898}
1899
Balazs Keri3b30d652018-10-19 13:32:20 +00001900// FIXME: Do not forget to remove this and use only 'import'.
1901Expected<TemplateArgument>
1902ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1903 return import(From);
1904}
1905
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001906template <typename InContainerTy>
Balazs Keri3b30d652018-10-19 13:32:20 +00001907Error ASTNodeImporter::ImportTemplateArgumentListInfo(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001908 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1909 for (const auto &FromLoc : Container) {
Balazs Keri3b30d652018-10-19 13:32:20 +00001910 if (auto ToLocOrErr = import(FromLoc))
1911 ToTAInfo.addArgument(*ToLocOrErr);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001912 else
Balazs Keri3b30d652018-10-19 13:32:20 +00001913 return ToLocOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001914 }
Balazs Keri3b30d652018-10-19 13:32:20 +00001915 return Error::success();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001916}
1917
Gabor Marton26f72a92018-07-12 09:42:05 +00001918static StructuralEquivalenceKind
1919getStructuralEquivalenceKind(const ASTImporter &Importer) {
1920 return Importer.isMinimalImport() ? StructuralEquivalenceKind::Minimal
1921 : StructuralEquivalenceKind::Default;
1922}
1923
Gabor Marton950fb572018-07-17 12:39:27 +00001924bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain) {
1925 StructuralEquivalenceContext Ctx(
1926 Importer.getFromContext(), Importer.getToContext(),
1927 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1928 false, Complain);
1929 return Ctx.IsEquivalent(From, To);
1930}
1931
1932bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001933 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001934 // Eliminate a potential failure point where we attempt to re-import
1935 // something we're trying to import while completing ToRecord.
1936 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1937 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001938 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00001939 if (ToOriginRecord)
1940 ToRecord = ToOriginRecord;
1941 }
1942
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001943 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001944 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001945 Importer.getNonEquivalentDecls(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001946 getStructuralEquivalenceKind(Importer),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001947 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001948 return Ctx.IsEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001949}
1950
Larisse Voufo39a1e502013-08-06 01:03:05 +00001951bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1952 bool Complain) {
1953 StructuralEquivalenceContext Ctx(
1954 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001955 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1956 false, Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00001957 return Ctx.IsEquivalent(FromVar, ToVar);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001958}
1959
Douglas Gregor98c10182010-02-12 22:17:39 +00001960bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00001961 // Eliminate a potential failure point where we attempt to re-import
Raphael Isemannfa26c202019-04-09 14:18:23 +00001962 // something we're trying to import while completing ToEnum.
Shafik Yaghmoure5094d62019-03-27 17:47:36 +00001963 if (Decl *ToOrigin = Importer.GetOriginalDecl(ToEnum))
1964 if (auto *ToOriginEnum = dyn_cast<EnumDecl>(ToOrigin))
1965 ToEnum = ToOriginEnum;
1966
Gabor Marton26f72a92018-07-12 09:42:05 +00001967 StructuralEquivalenceContext Ctx(
1968 Importer.getFromContext(), Importer.getToContext(),
1969 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00001970 return Ctx.IsEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001971}
1972
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001973bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1974 FunctionTemplateDecl *To) {
1975 StructuralEquivalenceContext Ctx(
1976 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001977 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1978 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00001979 return Ctx.IsEquivalent(From, To);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001980}
1981
Balazs Keric7797c42018-07-11 09:37:24 +00001982bool ASTNodeImporter::IsStructuralMatch(FunctionDecl *From, FunctionDecl *To) {
1983 StructuralEquivalenceContext Ctx(
1984 Importer.getFromContext(), Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00001985 Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
1986 false, false);
Gabor Marton950fb572018-07-17 12:39:27 +00001987 return Ctx.IsEquivalent(From, To);
Balazs Keric7797c42018-07-11 09:37:24 +00001988}
1989
Douglas Gregor91155082012-11-14 22:29:20 +00001990bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001991 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00001992 const llvm::APSInt &FromVal = FromEC->getInitVal();
1993 const llvm::APSInt &ToVal = ToEC->getInitVal();
1994
1995 return FromVal.isSigned() == ToVal.isSigned() &&
1996 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1997 FromVal == ToVal;
1998}
1999
2000bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002001 ClassTemplateDecl *To) {
2002 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2003 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002004 Importer.getNonEquivalentDecls(),
2005 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002006 return Ctx.IsEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00002007}
2008
Larisse Voufo39a1e502013-08-06 01:03:05 +00002009bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2010 VarTemplateDecl *To) {
2011 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2012 Importer.getToContext(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002013 Importer.getNonEquivalentDecls(),
2014 getStructuralEquivalenceKind(Importer));
Gabor Marton950fb572018-07-17 12:39:27 +00002015 return Ctx.IsEquivalent(From, To);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002016}
2017
Balazs Keri3b30d652018-10-19 13:32:20 +00002018ExpectedDecl ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002019 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002020 << D->getDeclKindName();
Balazs Keri3b30d652018-10-19 13:32:20 +00002021 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregore4c83e42010-02-09 22:48:33 +00002022}
2023
Balazs Keri3b30d652018-10-19 13:32:20 +00002024ExpectedDecl ASTNodeImporter::VisitImportDecl(ImportDecl *D) {
2025 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
2026 << D->getDeclKindName();
2027 return make_error<ImportError>(ImportError::UnsupportedConstruct);
2028}
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002029
Balazs Keri3b30d652018-10-19 13:32:20 +00002030ExpectedDecl ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
2031 // Import the context of this declaration.
2032 DeclContext *DC, *LexicalDC;
2033 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
2034 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002035
2036 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002037 ExpectedSLoc LocOrErr = import(D->getLocation());
2038 if (!LocOrErr)
2039 return LocOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002040
Gabor Marton26f72a92018-07-12 09:42:05 +00002041 EmptyDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002042 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), DC, *LocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002043 return ToD;
2044
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002045 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002046 LexicalDC->addDeclInternal(ToD);
2047 return ToD;
2048}
2049
Balazs Keri3b30d652018-10-19 13:32:20 +00002050ExpectedDecl ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002051 TranslationUnitDecl *ToD =
Sean Callanan65198272011-11-17 23:20:56 +00002052 Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00002053
Gabor Marton26f72a92018-07-12 09:42:05 +00002054 Importer.MapImported(D, ToD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002055
Sean Callanan65198272011-11-17 23:20:56 +00002056 return ToD;
2057}
2058
Balazs Keri3b30d652018-10-19 13:32:20 +00002059ExpectedDecl ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2060 ExpectedSLoc LocOrErr = import(D->getLocation());
2061 if (!LocOrErr)
2062 return LocOrErr.takeError();
2063 auto ColonLocOrErr = import(D->getColonLoc());
2064 if (!ColonLocOrErr)
2065 return ColonLocOrErr.takeError();
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002066
2067 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00002068 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2069 if (!DCOrErr)
2070 return DCOrErr.takeError();
2071 DeclContext *DC = *DCOrErr;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002072
Gabor Marton26f72a92018-07-12 09:42:05 +00002073 AccessSpecDecl *ToD;
2074 if (GetImportedOrCreateDecl(ToD, D, Importer.getToContext(), D->getAccess(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002075 DC, *LocOrErr, *ColonLocOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00002076 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002077
2078 // Lexical DeclContext and Semantic DeclContext
2079 // is always the same for the accessSpec.
Gabor Marton26f72a92018-07-12 09:42:05 +00002080 ToD->setLexicalDeclContext(DC);
2081 DC->addDeclInternal(ToD);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002082
Gabor Marton26f72a92018-07-12 09:42:05 +00002083 return ToD;
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002084}
2085
Balazs Keri3b30d652018-10-19 13:32:20 +00002086ExpectedDecl ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
2087 auto DCOrErr = Importer.ImportContext(D->getDeclContext());
2088 if (!DCOrErr)
2089 return DCOrErr.takeError();
2090 DeclContext *DC = *DCOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002091 DeclContext *LexicalDC = DC;
2092
Balazs Keri3b30d652018-10-19 13:32:20 +00002093 SourceLocation ToLocation, ToRParenLoc;
2094 Expr *ToAssertExpr;
2095 StringLiteral *ToMessage;
2096 if (auto Imp = importSeq(
2097 D->getLocation(), D->getAssertExpr(), D->getMessage(), D->getRParenLoc()))
2098 std::tie(ToLocation, ToAssertExpr, ToMessage, ToRParenLoc) = *Imp;
2099 else
2100 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002101
Gabor Marton26f72a92018-07-12 09:42:05 +00002102 StaticAssertDecl *ToD;
2103 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002104 ToD, D, Importer.getToContext(), DC, ToLocation, ToAssertExpr, ToMessage,
2105 ToRParenLoc, D->isFailed()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002106 return ToD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00002107
2108 ToD->setLexicalDeclContext(LexicalDC);
2109 LexicalDC->addDeclInternal(ToD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002110 return ToD;
2111}
2112
Balazs Keri3b30d652018-10-19 13:32:20 +00002113ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002114 // Import the major distinguishing characteristics of this namespace.
2115 DeclContext *DC, *LexicalDC;
2116 DeclarationName Name;
2117 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002118 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002119 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2120 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002121 if (ToD)
2122 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002123
2124 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002125 if (!Name) {
2126 // This is an anonymous namespace. Adopt an existing anonymous
2127 // namespace if we can.
2128 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002129 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002130 MergeWithNamespace = TU->getAnonymousNamespace();
2131 else
2132 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2133 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002134 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002135 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002136 for (auto *FoundDecl : FoundDecls) {
2137 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002138 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002139
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002140 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002141 MergeWithNamespace = FoundNS;
2142 ConflictingDecls.clear();
2143 break;
2144 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002145
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002146 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002147 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002148
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002149 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002150 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +00002151 ConflictingDecls.data(),
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002152 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002153 if (!Name)
2154 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002155 }
2156 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002157
Balazs Keri3b30d652018-10-19 13:32:20 +00002158 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2159 if (!BeginLocOrErr)
2160 return BeginLocOrErr.takeError();
2161
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002162 // Create the "to" namespace, if needed.
2163 NamespaceDecl *ToNamespace = MergeWithNamespace;
2164 if (!ToNamespace) {
Gabor Marton26f72a92018-07-12 09:42:05 +00002165 if (GetImportedOrCreateDecl(
2166 ToNamespace, D, Importer.getToContext(), DC, D->isInline(),
Balazs Keri3b30d652018-10-19 13:32:20 +00002167 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00002168 /*PrevDecl=*/nullptr))
2169 return ToNamespace;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002170 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002171 LexicalDC->addDeclInternal(ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002172
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002173 // If this is an anonymous namespace, register it as the anonymous
2174 // namespace within its context.
2175 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002176 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002177 TU->setAnonymousNamespace(ToNamespace);
2178 else
2179 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2180 }
2181 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002182 Importer.MapImported(D, ToNamespace);
Fangrui Song6907ce22018-07-30 19:24:48 +00002183
Balazs Keri3b30d652018-10-19 13:32:20 +00002184 if (Error Err = ImportDeclContext(D))
2185 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00002186
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002187 return ToNamespace;
2188}
2189
Balazs Keri3b30d652018-10-19 13:32:20 +00002190ExpectedDecl ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002191 // Import the major distinguishing characteristics of this namespace.
2192 DeclContext *DC, *LexicalDC;
2193 DeclarationName Name;
2194 SourceLocation Loc;
2195 NamedDecl *LookupD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002196 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
2197 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002198 if (LookupD)
2199 return LookupD;
2200
2201 // NOTE: No conflict resolution is done for namespace aliases now.
2202
Balazs Keri3b30d652018-10-19 13:32:20 +00002203 SourceLocation ToNamespaceLoc, ToAliasLoc, ToTargetNameLoc;
2204 NestedNameSpecifierLoc ToQualifierLoc;
2205 NamespaceDecl *ToNamespace;
2206 if (auto Imp = importSeq(
2207 D->getNamespaceLoc(), D->getAliasLoc(), D->getQualifierLoc(),
2208 D->getTargetNameLoc(), D->getNamespace()))
2209 std::tie(
2210 ToNamespaceLoc, ToAliasLoc, ToQualifierLoc, ToTargetNameLoc,
2211 ToNamespace) = *Imp;
2212 else
2213 return Imp.takeError();
2214 IdentifierInfo *ToIdentifier = Importer.Import(D->getIdentifier());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002215
Gabor Marton26f72a92018-07-12 09:42:05 +00002216 NamespaceAliasDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002217 if (GetImportedOrCreateDecl(
2218 ToD, D, Importer.getToContext(), DC, ToNamespaceLoc, ToAliasLoc,
2219 ToIdentifier, ToQualifierLoc, ToTargetNameLoc, ToNamespace))
Gabor Marton26f72a92018-07-12 09:42:05 +00002220 return ToD;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002221
2222 ToD->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002223 LexicalDC->addDeclInternal(ToD);
2224
2225 return ToD;
2226}
2227
Balazs Keri3b30d652018-10-19 13:32:20 +00002228ExpectedDecl
2229ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002230 // Import the major distinguishing characteristics of this typedef.
2231 DeclContext *DC, *LexicalDC;
2232 DeclarationName Name;
2233 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002234 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002235 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2236 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002237 if (ToD)
2238 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002239
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002240 // If this typedef is not in block scope, determine whether we've
2241 // seen a typedef with the same name (that we can merge with) or any
2242 // other entity by that name (which name lookup could conflict with).
2243 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002244 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002245 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002246 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002247 for (auto *FoundDecl : FoundDecls) {
2248 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002249 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002250 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Gabor Martonb93baf62018-11-27 09:51:36 +00002251 QualType FromUT = D->getUnderlyingType();
2252 QualType FoundUT = FoundTypedef->getUnderlyingType();
2253 if (Importer.IsStructurallyEquivalent(FromUT, FoundUT)) {
2254 // If the "From" context has a complete underlying type but we
2255 // already have a complete underlying type then return with that.
2256 if (!FromUT->isIncompleteType() && !FoundUT->isIncompleteType())
Balazs Keri3b30d652018-10-19 13:32:20 +00002257 return Importer.MapImported(D, FoundTypedef);
Gabor Martonb93baf62018-11-27 09:51:36 +00002258 }
2259 // FIXME Handle redecl chain.
2260 break;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002261 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002262
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002263 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002264 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002265
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002266 if (!ConflictingDecls.empty()) {
2267 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002268 ConflictingDecls.data(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002269 ConflictingDecls.size());
2270 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002271 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002272 }
2273 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002274
Balazs Keri3b30d652018-10-19 13:32:20 +00002275 QualType ToUnderlyingType;
2276 TypeSourceInfo *ToTypeSourceInfo;
2277 SourceLocation ToBeginLoc;
2278 if (auto Imp = importSeq(
2279 D->getUnderlyingType(), D->getTypeSourceInfo(), D->getBeginLoc()))
2280 std::tie(ToUnderlyingType, ToTypeSourceInfo, ToBeginLoc) = *Imp;
2281 else
2282 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002283
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002284 // Create the new typedef node.
Balazs Keri3b30d652018-10-19 13:32:20 +00002285 // FIXME: ToUnderlyingType is not used.
Richard Smithdda56e42011-04-15 14:24:37 +00002286 TypedefNameDecl *ToTypedef;
Gabor Marton26f72a92018-07-12 09:42:05 +00002287 if (IsAlias) {
2288 if (GetImportedOrCreateDecl<TypeAliasDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002289 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2290 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002291 return ToTypedef;
2292 } else if (GetImportedOrCreateDecl<TypedefDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00002293 ToTypedef, D, Importer.getToContext(), DC, ToBeginLoc, Loc,
2294 Name.getAsIdentifierInfo(), ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00002295 return ToTypedef;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002296
Douglas Gregordd483172010-02-22 17:42:47 +00002297 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002298 ToTypedef->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002299
2300 // Templated declarations should not appear in DeclContext.
2301 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
2302 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
2303 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002304
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002305 return ToTypedef;
2306}
2307
Balazs Keri3b30d652018-10-19 13:32:20 +00002308ExpectedDecl ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002309 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2310}
2311
Balazs Keri3b30d652018-10-19 13:32:20 +00002312ExpectedDecl ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
Richard Smithdda56e42011-04-15 14:24:37 +00002313 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2314}
2315
Balazs Keri3b30d652018-10-19 13:32:20 +00002316ExpectedDecl
2317ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
Gabor Horvath7a91c082017-11-14 11:30:38 +00002318 // Import the major distinguishing characteristics of this typedef.
2319 DeclContext *DC, *LexicalDC;
2320 DeclarationName Name;
2321 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002322 NamedDecl *FoundD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002323 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
2324 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002325 if (FoundD)
2326 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002327
2328 // If this typedef is not in block scope, determine whether we've
2329 // seen a typedef with the same name (that we can merge with) or any
2330 // other entity by that name (which name lookup could conflict with).
2331 if (!DC->isFunctionOrMethod()) {
2332 SmallVector<NamedDecl *, 4> ConflictingDecls;
2333 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002334 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002335 for (auto *FoundDecl : FoundDecls) {
2336 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00002337 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002338 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002339 return Importer.MapImported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002340 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002341 }
2342
2343 if (!ConflictingDecls.empty()) {
2344 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2345 ConflictingDecls.data(),
2346 ConflictingDecls.size());
2347 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002348 return make_error<ImportError>(ImportError::NameConflict);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002349 }
2350 }
2351
Balazs Keri3b30d652018-10-19 13:32:20 +00002352 TemplateParameterList *ToTemplateParameters;
2353 TypeAliasDecl *ToTemplatedDecl;
2354 if (auto Imp = importSeq(D->getTemplateParameters(), D->getTemplatedDecl()))
2355 std::tie(ToTemplateParameters, ToTemplatedDecl) = *Imp;
2356 else
2357 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00002358
Gabor Marton26f72a92018-07-12 09:42:05 +00002359 TypeAliasTemplateDecl *ToAlias;
2360 if (GetImportedOrCreateDecl(ToAlias, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002361 Name, ToTemplateParameters, ToTemplatedDecl))
Gabor Marton26f72a92018-07-12 09:42:05 +00002362 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002363
Balazs Keri3b30d652018-10-19 13:32:20 +00002364 ToTemplatedDecl->setDescribedAliasTemplate(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002365
Gabor Horvath7a91c082017-11-14 11:30:38 +00002366 ToAlias->setAccess(D->getAccess());
2367 ToAlias->setLexicalDeclContext(LexicalDC);
Gabor Horvath7a91c082017-11-14 11:30:38 +00002368 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002369 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00002370}
2371
Balazs Keri3b30d652018-10-19 13:32:20 +00002372ExpectedDecl ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002373 // Import the major distinguishing characteristics of this label.
2374 DeclContext *DC, *LexicalDC;
2375 DeclarationName Name;
2376 SourceLocation Loc;
2377 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002378 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2379 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002380 if (ToD)
2381 return ToD;
2382
2383 assert(LexicalDC->isFunctionOrMethod());
2384
Gabor Marton26f72a92018-07-12 09:42:05 +00002385 LabelDecl *ToLabel;
Balazs Keri3b30d652018-10-19 13:32:20 +00002386 if (D->isGnuLocal()) {
2387 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2388 if (!BeginLocOrErr)
2389 return BeginLocOrErr.takeError();
2390 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2391 Name.getAsIdentifierInfo(), *BeginLocOrErr))
2392 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002393
Balazs Keri3b30d652018-10-19 13:32:20 +00002394 } else {
2395 if (GetImportedOrCreateDecl(ToLabel, D, Importer.getToContext(), DC, Loc,
2396 Name.getAsIdentifierInfo()))
2397 return ToLabel;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002398
Balazs Keri3b30d652018-10-19 13:32:20 +00002399 }
2400
2401 Expected<LabelStmt *> ToStmtOrErr = import(D->getStmt());
2402 if (!ToStmtOrErr)
2403 return ToStmtOrErr.takeError();
2404
2405 ToLabel->setStmt(*ToStmtOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002406 ToLabel->setLexicalDeclContext(LexicalDC);
2407 LexicalDC->addDeclInternal(ToLabel);
2408 return ToLabel;
2409}
2410
Balazs Keri3b30d652018-10-19 13:32:20 +00002411ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002412 // Import the major distinguishing characteristics of this enum.
2413 DeclContext *DC, *LexicalDC;
2414 DeclarationName Name;
2415 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002416 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002417 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2418 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002419 if (ToD)
2420 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002421
Douglas Gregor98c10182010-02-12 22:17:39 +00002422 // Figure out what enum name we're looking for.
2423 unsigned IDNS = Decl::IDNS_Tag;
2424 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002425 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002426 if (Error Err = importInto(
2427 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2428 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002429 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002430 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002431 IDNS |= Decl::IDNS_Ordinary;
Fangrui Song6907ce22018-07-30 19:24:48 +00002432
Douglas Gregor98c10182010-02-12 22:17:39 +00002433 // We may already have an enum of the same name; try to find and match it.
2434 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002435 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002436 auto FoundDecls =
2437 Importer.findDeclsInToCtx(DC, SearchName);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002438 for (auto *FoundDecl : FoundDecls) {
2439 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002440 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002441
Balazs Keri3b30d652018-10-19 13:32:20 +00002442 if (auto *Typedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002443 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Balazs Keri3b30d652018-10-19 13:32:20 +00002444 FoundDecl = Tag->getDecl();
Douglas Gregor98c10182010-02-12 22:17:39 +00002445 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002446
Balazs Keri3b30d652018-10-19 13:32:20 +00002447 if (auto *FoundEnum = dyn_cast<EnumDecl>(FoundDecl)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002448 if (IsStructuralMatch(D, FoundEnum))
Gabor Marton26f72a92018-07-12 09:42:05 +00002449 return Importer.MapImported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002450 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002451
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002452 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002453 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002454
Douglas Gregor98c10182010-02-12 22:17:39 +00002455 if (!ConflictingDecls.empty()) {
Shafik Yaghmourd4263122019-04-08 20:50:21 +00002456 Name = Importer.HandleNameConflict(SearchName, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002457 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002458 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002459 if (!Name)
2460 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002461 }
2462 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002463
Balazs Keri3b30d652018-10-19 13:32:20 +00002464 SourceLocation ToBeginLoc;
2465 NestedNameSpecifierLoc ToQualifierLoc;
2466 QualType ToIntegerType;
2467 if (auto Imp = importSeq(
2468 D->getBeginLoc(), D->getQualifierLoc(), D->getIntegerType()))
2469 std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType) = *Imp;
2470 else
2471 return Imp.takeError();
2472
Douglas Gregor98c10182010-02-12 22:17:39 +00002473 // Create the enum declaration.
Gabor Marton26f72a92018-07-12 09:42:05 +00002474 EnumDecl *D2;
2475 if (GetImportedOrCreateDecl(
Balazs Keri3b30d652018-10-19 13:32:20 +00002476 D2, D, Importer.getToContext(), DC, ToBeginLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00002477 Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(),
2478 D->isScopedUsingClassTag(), D->isFixed()))
2479 return D2;
2480
Balazs Keri3b30d652018-10-19 13:32:20 +00002481 D2->setQualifierInfo(ToQualifierLoc);
2482 D2->setIntegerType(ToIntegerType);
Douglas Gregordd483172010-02-22 17:42:47 +00002483 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002484 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002485 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002486
Douglas Gregor98c10182010-02-12 22:17:39 +00002487 // Import the definition
Balazs Keri3b30d652018-10-19 13:32:20 +00002488 if (D->isCompleteDefinition())
2489 if (Error Err = ImportDefinition(D, D2))
2490 return std::move(Err);
Douglas Gregor98c10182010-02-12 22:17:39 +00002491
Douglas Gregor3996e242010-02-15 22:01:00 +00002492 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002493}
2494
Balazs Keri3b30d652018-10-19 13:32:20 +00002495ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00002496 bool IsFriendTemplate = false;
2497 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2498 IsFriendTemplate =
2499 DCXX->getDescribedClassTemplate() &&
2500 DCXX->getDescribedClassTemplate()->getFriendObjectKind() !=
2501 Decl::FOK_None;
2502 }
2503
Douglas Gregor5c73e912010-02-11 00:48:18 +00002504 // Import the major distinguishing characteristics of this record.
2505 DeclContext *DC, *LexicalDC;
2506 DeclarationName Name;
2507 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002508 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002509 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2510 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002511 if (ToD)
2512 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002513
Douglas Gregor5c73e912010-02-11 00:48:18 +00002514 // Figure out what structure name we're looking for.
2515 unsigned IDNS = Decl::IDNS_Tag;
2516 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002517 if (!SearchName && D->getTypedefNameForAnonDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002518 if (Error Err = importInto(
2519 SearchName, D->getTypedefNameForAnonDecl()->getDeclName()))
2520 return std::move(Err);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002521 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002522 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Gabor Marton7df342a2018-12-17 12:42:12 +00002523 IDNS |= Decl::IDNS_Ordinary | Decl::IDNS_TagFriend;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002524
2525 // We may already have a record of the same name; try to find and match it.
Sean Callanan9092d472017-05-13 00:46:33 +00002526 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002527 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002528 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00002529 auto FoundDecls =
2530 Importer.findDeclsInToCtx(DC, SearchName);
Sean Callanan9092d472017-05-13 00:46:33 +00002531 if (!FoundDecls.empty()) {
Gabor Marton41e38922019-03-05 11:23:24 +00002532 // We're going to have to compare D against potentially conflicting Decls,
2533 // so complete it.
Sean Callanan9092d472017-05-13 00:46:33 +00002534 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2535 D->getASTContext().getExternalSource()->CompleteType(D);
2536 }
2537
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002538 for (auto *FoundDecl : FoundDecls) {
2539 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002540 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002541
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002542 Decl *Found = FoundDecl;
2543 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2544 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002545 Found = Tag->getDecl();
2546 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002547
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002548 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Gabor Marton7df342a2018-12-17 12:42:12 +00002549 // Do not emit false positive diagnostic in case of unnamed
2550 // struct/union and in case of anonymous structs. Would be false
2551 // because there may be several anonymous/unnamed structs in a class.
2552 // E.g. these are both valid:
2553 // struct A { // unnamed structs
2554 // struct { struct A *next; } entry0;
2555 // struct { struct A *next; } entry1;
2556 // };
2557 // struct X { struct { int a; }; struct { int b; }; }; // anon structs
2558 if (!SearchName)
Gabor Marton0bebf952018-07-05 09:51:13 +00002559 if (!IsStructuralMatch(D, FoundRecord, false))
2560 continue;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002561
Gabor Marton7df342a2018-12-17 12:42:12 +00002562 if (IsStructuralMatch(D, FoundRecord)) {
2563 RecordDecl *FoundDef = FoundRecord->getDefinition();
2564 if (D->isThisDeclarationADefinition() && FoundDef) {
Balazs Keri1d20cc22018-07-16 12:16:39 +00002565 // FIXME: Structural equivalence check should check for same
2566 // user-defined methods.
2567 Importer.MapImported(D, FoundDef);
2568 if (const auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2569 auto *FoundCXX = dyn_cast<CXXRecordDecl>(FoundDef);
2570 assert(FoundCXX && "Record type mismatch");
2571
Gabor Marton7df342a2018-12-17 12:42:12 +00002572 if (!Importer.isMinimalImport())
Balazs Keri1d20cc22018-07-16 12:16:39 +00002573 // FoundDef may not have every implicit method that D has
2574 // because implicit methods are created only if they are used.
Balazs Keri3b30d652018-10-19 13:32:20 +00002575 if (Error Err = ImportImplicitMethods(DCXX, FoundCXX))
2576 return std::move(Err);
Balazs Keri1d20cc22018-07-16 12:16:39 +00002577 }
Douglas Gregor25791052010-02-12 00:09:27 +00002578 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002579 PrevDecl = FoundRecord->getMostRecentDecl();
2580 break;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002581 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002582 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002583
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002584 ConflictingDecls.push_back(FoundDecl);
Gabor Marton7df342a2018-12-17 12:42:12 +00002585 } // for
Fangrui Song6907ce22018-07-30 19:24:48 +00002586
Douglas Gregordd6006f2012-07-17 21:16:27 +00002587 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002588 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002589 ConflictingDecls.data(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002590 ConflictingDecls.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00002591 if (!Name)
2592 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002593 }
2594 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002595
Balazs Keri3b30d652018-10-19 13:32:20 +00002596 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
2597 if (!BeginLocOrErr)
2598 return BeginLocOrErr.takeError();
2599
Douglas Gregor5c73e912010-02-11 00:48:18 +00002600 // Create the record declaration.
Gabor Marton7df342a2018-12-17 12:42:12 +00002601 RecordDecl *D2 = nullptr;
2602 CXXRecordDecl *D2CXX = nullptr;
2603 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
2604 if (DCXX->isLambda()) {
2605 auto TInfoOrErr = import(DCXX->getLambdaTypeInfo());
2606 if (!TInfoOrErr)
2607 return TInfoOrErr.takeError();
2608 if (GetImportedOrCreateSpecialDecl(
2609 D2CXX, CXXRecordDecl::CreateLambda, D, Importer.getToContext(),
2610 DC, *TInfoOrErr, Loc, DCXX->isDependentLambda(),
2611 DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()))
2612 return D2CXX;
2613 ExpectedDecl CDeclOrErr = import(DCXX->getLambdaContextDecl());
2614 if (!CDeclOrErr)
2615 return CDeclOrErr.takeError();
2616 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), *CDeclOrErr);
2617 } else if (DCXX->isInjectedClassName()) {
2618 // We have to be careful to do a similar dance to the one in
2619 // Sema::ActOnStartCXXMemberDeclarations
2620 const bool DelayTypeCreation = true;
2621 if (GetImportedOrCreateDecl(
2622 D2CXX, D, Importer.getToContext(), D->getTagKind(), DC,
2623 *BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
2624 cast_or_null<CXXRecordDecl>(PrevDecl), DelayTypeCreation))
2625 return D2CXX;
2626 Importer.getToContext().getTypeDeclType(
2627 D2CXX, dyn_cast<CXXRecordDecl>(DC));
2628 } else {
2629 if (GetImportedOrCreateDecl(D2CXX, D, Importer.getToContext(),
2630 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2631 Name.getAsIdentifierInfo(),
2632 cast_or_null<CXXRecordDecl>(PrevDecl)))
2633 return D2CXX;
2634 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002635
Gabor Marton7df342a2018-12-17 12:42:12 +00002636 D2 = D2CXX;
2637 D2->setAccess(D->getAccess());
2638 D2->setLexicalDeclContext(LexicalDC);
2639 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
2640 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002641
Gabor Marton7df342a2018-12-17 12:42:12 +00002642 if (LexicalDC != DC && D->isInIdentifierNamespace(Decl::IDNS_TagFriend))
2643 DC->makeDeclVisibleInContext(D2);
2644
2645 if (ClassTemplateDecl *FromDescribed =
2646 DCXX->getDescribedClassTemplate()) {
2647 ClassTemplateDecl *ToDescribed;
2648 if (Error Err = importInto(ToDescribed, FromDescribed))
2649 return std::move(Err);
2650 D2CXX->setDescribedClassTemplate(ToDescribed);
2651 if (!DCXX->isInjectedClassName() && !IsFriendTemplate) {
2652 // In a record describing a template the type should be an
2653 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2654 // previously set type to the correct value here (ToDescribed is not
2655 // available at record create).
2656 // FIXME: The previous type is cleared but not removed from
2657 // ASTContext's internal storage.
2658 CXXRecordDecl *Injected = nullptr;
2659 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2660 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2661 if (Record && Record->isInjectedClassName()) {
2662 Injected = Record;
2663 break;
Gabor Marton5915777e2018-06-26 13:44:24 +00002664 }
2665 }
Gabor Marton7df342a2018-12-17 12:42:12 +00002666 // Create an injected type for the whole redecl chain.
2667 SmallVector<Decl *, 2> Redecls =
2668 getCanonicalForwardRedeclChain(D2CXX);
2669 for (auto *R : Redecls) {
2670 auto *RI = cast<CXXRecordDecl>(R);
2671 RI->setTypeForDecl(nullptr);
2672 // Below we create a new injected type and assign that to the
2673 // canonical decl, subsequent declarations in the chain will reuse
2674 // that type.
2675 Importer.getToContext().getInjectedClassNameType(
2676 RI, ToDescribed->getInjectedClassNameSpecialization());
2677 }
2678 // Set the new type for the previous injected decl too.
2679 if (Injected) {
2680 Injected->setTypeForDecl(nullptr);
2681 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2682 }
2683 }
2684 } else if (MemberSpecializationInfo *MemberInfo =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002685 DCXX->getMemberSpecializationInfo()) {
2686 TemplateSpecializationKind SK =
2687 MemberInfo->getTemplateSpecializationKind();
2688 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Balazs Keri3b30d652018-10-19 13:32:20 +00002689
2690 if (Expected<CXXRecordDecl *> ToInstOrErr = import(FromInst))
2691 D2CXX->setInstantiationOfMemberClass(*ToInstOrErr, SK);
2692 else
2693 return ToInstOrErr.takeError();
2694
2695 if (ExpectedSLoc POIOrErr =
2696 import(MemberInfo->getPointOfInstantiation()))
2697 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2698 *POIOrErr);
2699 else
2700 return POIOrErr.takeError();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002701 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002702
Gabor Marton7df342a2018-12-17 12:42:12 +00002703 } else {
2704 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(),
2705 D->getTagKind(), DC, *BeginLocOrErr, Loc,
2706 Name.getAsIdentifierInfo(), PrevDecl))
2707 return D2;
2708 D2->setLexicalDeclContext(LexicalDC);
2709 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002710 }
Gabor Marton26f72a92018-07-12 09:42:05 +00002711
Gabor Marton7df342a2018-12-17 12:42:12 +00002712 if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
2713 D2->setQualifierInfo(*QualifierLocOrErr);
2714 else
2715 return QualifierLocOrErr.takeError();
2716
2717 if (D->isAnonymousStructOrUnion())
2718 D2->setAnonymousStructOrUnion(true);
Douglas Gregor25791052010-02-12 00:09:27 +00002719
Balazs Keri3b30d652018-10-19 13:32:20 +00002720 if (D->isCompleteDefinition())
2721 if (Error Err = ImportDefinition(D, D2, IDK_Default))
2722 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00002723
Douglas Gregor3996e242010-02-15 22:01:00 +00002724 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002725}
2726
Balazs Keri3b30d652018-10-19 13:32:20 +00002727ExpectedDecl ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002728 // Import the major distinguishing characteristics of this enumerator.
2729 DeclContext *DC, *LexicalDC;
2730 DeclarationName Name;
2731 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002732 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002733 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2734 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002735 if (ToD)
2736 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002737
Fangrui Song6907ce22018-07-30 19:24:48 +00002738 // Determine whether there are any other declarations with the same name and
Douglas Gregor98c10182010-02-12 22:17:39 +00002739 // in the same context.
2740 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002741 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002742 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00002743 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002744 for (auto *FoundDecl : FoundDecls) {
2745 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002746 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002747
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002748 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002749 if (IsStructuralMatch(D, FoundEnumConstant))
Gabor Marton26f72a92018-07-12 09:42:05 +00002750 return Importer.MapImported(D, FoundEnumConstant);
Douglas Gregor91155082012-11-14 22:29:20 +00002751 }
2752
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002753 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002754 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002755
Douglas Gregor98c10182010-02-12 22:17:39 +00002756 if (!ConflictingDecls.empty()) {
2757 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002758 ConflictingDecls.data(),
Douglas Gregor98c10182010-02-12 22:17:39 +00002759 ConflictingDecls.size());
2760 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00002761 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor98c10182010-02-12 22:17:39 +00002762 }
2763 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002764
Balazs Keri3b30d652018-10-19 13:32:20 +00002765 ExpectedType TypeOrErr = import(D->getType());
2766 if (!TypeOrErr)
2767 return TypeOrErr.takeError();
2768
2769 ExpectedExpr InitOrErr = import(D->getInitExpr());
2770 if (!InitOrErr)
2771 return InitOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00002772
Gabor Marton26f72a92018-07-12 09:42:05 +00002773 EnumConstantDecl *ToEnumerator;
2774 if (GetImportedOrCreateDecl(
2775 ToEnumerator, D, Importer.getToContext(), cast<EnumDecl>(DC), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00002776 Name.getAsIdentifierInfo(), *TypeOrErr, *InitOrErr, D->getInitVal()))
Gabor Marton26f72a92018-07-12 09:42:05 +00002777 return ToEnumerator;
2778
Douglas Gregordd483172010-02-22 17:42:47 +00002779 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002780 ToEnumerator->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002781 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002782 return ToEnumerator;
2783}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002784
Balazs Keri1efc9742019-05-07 10:55:11 +00002785Error ASTNodeImporter::ImportTemplateParameterLists(const DeclaratorDecl *FromD,
2786 DeclaratorDecl *ToD) {
2787 unsigned int Num = FromD->getNumTemplateParameterLists();
2788 if (Num == 0)
2789 return Error::success();
2790 SmallVector<TemplateParameterList *, 2> ToTPLists(Num);
2791 for (unsigned int I = 0; I < Num; ++I)
2792 if (Expected<TemplateParameterList *> ToTPListOrErr =
2793 import(FromD->getTemplateParameterList(I)))
2794 ToTPLists[I] = *ToTPListOrErr;
2795 else
2796 return ToTPListOrErr.takeError();
2797 ToD->setTemplateParameterListsInfo(Importer.ToContext, ToTPLists);
2798 return Error::success();
2799}
2800
Balazs Keri3b30d652018-10-19 13:32:20 +00002801Error ASTNodeImporter::ImportTemplateInformation(
2802 FunctionDecl *FromFD, FunctionDecl *ToFD) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002803 switch (FromFD->getTemplatedKind()) {
2804 case FunctionDecl::TK_NonTemplate:
2805 case FunctionDecl::TK_FunctionTemplate:
Balazs Keri3b30d652018-10-19 13:32:20 +00002806 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002807
2808 case FunctionDecl::TK_MemberSpecialization: {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002809 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00002810
2811 if (Expected<FunctionDecl *> InstFDOrErr =
2812 import(FromFD->getInstantiatedFromMemberFunction()))
2813 ToFD->setInstantiationOfMemberFunction(*InstFDOrErr, TSK);
2814 else
2815 return InstFDOrErr.takeError();
2816
2817 if (ExpectedSLoc POIOrErr = import(
2818 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation()))
2819 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(*POIOrErr);
2820 else
2821 return POIOrErr.takeError();
2822
2823 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002824 }
2825
2826 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Balazs Keri3b30d652018-10-19 13:32:20 +00002827 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002828 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002829 if (!FunctionAndArgsOrErr)
2830 return FunctionAndArgsOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002831
2832 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Balazs Keri3b30d652018-10-19 13:32:20 +00002833 Importer.getToContext(), std::get<1>(*FunctionAndArgsOrErr));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002834
Gabor Marton5254e642018-06-27 13:32:50 +00002835 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002836 TemplateArgumentListInfo ToTAInfo;
2837 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002838 if (FromTAArgsAsWritten)
Balazs Keri3b30d652018-10-19 13:32:20 +00002839 if (Error Err = ImportTemplateArgumentListInfo(
2840 *FromTAArgsAsWritten, ToTAInfo))
2841 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002842
Balazs Keri3b30d652018-10-19 13:32:20 +00002843 ExpectedSLoc POIOrErr = import(FTSInfo->getPointOfInstantiation());
2844 if (!POIOrErr)
2845 return POIOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002846
Balazs Keri1efc9742019-05-07 10:55:11 +00002847 if (Error Err = ImportTemplateParameterLists(FromFD, ToFD))
2848 return Err;
2849
Gabor Marton5254e642018-06-27 13:32:50 +00002850 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002851 ToFD->setFunctionTemplateSpecialization(
Balazs Keri3b30d652018-10-19 13:32:20 +00002852 std::get<0>(*FunctionAndArgsOrErr), ToTAList, /* InsertPos= */ nullptr,
2853 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, *POIOrErr);
2854 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002855 }
2856
2857 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2858 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2859 UnresolvedSet<8> TemplDecls;
2860 unsigned NumTemplates = FromInfo->getNumTemplates();
2861 for (unsigned I = 0; I < NumTemplates; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002862 if (Expected<FunctionTemplateDecl *> ToFTDOrErr =
2863 import(FromInfo->getTemplate(I)))
2864 TemplDecls.addDecl(*ToFTDOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002865 else
Balazs Keri3b30d652018-10-19 13:32:20 +00002866 return ToFTDOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002867 }
2868
2869 // Import TemplateArgumentListInfo.
2870 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00002871 if (Error Err = ImportTemplateArgumentListInfo(
2872 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2873 llvm::makeArrayRef(
2874 FromInfo->getTemplateArgs(), FromInfo->getNumTemplateArgs()),
2875 ToTAInfo))
2876 return Err;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002877
2878 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2879 TemplDecls, ToTAInfo);
Balazs Keri3b30d652018-10-19 13:32:20 +00002880 return Error::success();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002881 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002882 }
Sam McCallfdc32072018-01-26 12:06:44 +00002883 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002884}
2885
Balazs Keri3b30d652018-10-19 13:32:20 +00002886Expected<FunctionDecl *>
Gabor Marton5254e642018-06-27 13:32:50 +00002887ASTNodeImporter::FindFunctionTemplateSpecialization(FunctionDecl *FromFD) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002888 auto FunctionAndArgsOrErr =
Gabor Marton5254e642018-06-27 13:32:50 +00002889 ImportFunctionTemplateWithTemplateArgsFromSpecialization(FromFD);
Balazs Keri3b30d652018-10-19 13:32:20 +00002890 if (!FunctionAndArgsOrErr)
2891 return FunctionAndArgsOrErr.takeError();
Gabor Marton5254e642018-06-27 13:32:50 +00002892
Balazs Keri3b30d652018-10-19 13:32:20 +00002893 FunctionTemplateDecl *Template;
2894 TemplateArgsTy ToTemplArgs;
2895 std::tie(Template, ToTemplArgs) = *FunctionAndArgsOrErr;
Gabor Marton5254e642018-06-27 13:32:50 +00002896 void *InsertPos = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00002897 auto *FoundSpec = Template->findSpecialization(ToTemplArgs, InsertPos);
Gabor Marton5254e642018-06-27 13:32:50 +00002898 return FoundSpec;
2899}
2900
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002901Error ASTNodeImporter::ImportFunctionDeclBody(FunctionDecl *FromFD,
2902 FunctionDecl *ToFD) {
2903 if (Stmt *FromBody = FromFD->getBody()) {
2904 if (ExpectedStmt ToBodyOrErr = import(FromBody))
2905 ToFD->setBody(*ToBodyOrErr);
2906 else
2907 return ToBodyOrErr.takeError();
2908 }
2909 return Error::success();
2910}
2911
Gabor Marton458d1452019-02-14 13:07:03 +00002912template <typename T>
2913bool ASTNodeImporter::hasSameVisibilityContext(T *Found, T *From) {
2914 if (From->hasExternalFormalLinkage())
2915 return Found->hasExternalFormalLinkage();
2916 if (Importer.GetFromTU(Found) != From->getTranslationUnitDecl())
2917 return false;
2918 if (From->isInAnonymousNamespace())
2919 return Found->isInAnonymousNamespace();
2920 else
2921 return !Found->isInAnonymousNamespace() &&
2922 !Found->hasExternalFormalLinkage();
2923}
2924
Balazs Keri3b30d652018-10-19 13:32:20 +00002925ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
Gabor Marton5254e642018-06-27 13:32:50 +00002926
Balazs Keri3b30d652018-10-19 13:32:20 +00002927 SmallVector<Decl *, 2> Redecls = getCanonicalForwardRedeclChain(D);
Gabor Marton5254e642018-06-27 13:32:50 +00002928 auto RedeclIt = Redecls.begin();
2929 // Import the first part of the decl chain. I.e. import all previous
2930 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00002931 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
2932 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
2933 if (!ToRedeclOrErr)
2934 return ToRedeclOrErr.takeError();
2935 }
Gabor Marton5254e642018-06-27 13:32:50 +00002936 assert(*RedeclIt == D);
2937
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002938 // Import the major distinguishing characteristics of this function.
2939 DeclContext *DC, *LexicalDC;
2940 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002941 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002942 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00002943 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2944 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00002945 if (ToD)
2946 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002947
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00002948 FunctionDecl *FoundByLookup = nullptr;
Balazs Keria35798d2018-07-17 09:52:41 +00002949 FunctionTemplateDecl *FromFT = D->getDescribedFunctionTemplate();
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002950
Gabor Marton5254e642018-06-27 13:32:50 +00002951 // If this is a function template specialization, then try to find the same
Gabor Marton54058b52018-12-17 13:53:12 +00002952 // existing specialization in the "to" context. The lookup below will not
2953 // find any specialization, but would find the primary template; thus, we
2954 // have to skip normal lookup in case of specializations.
Gabor Marton5254e642018-06-27 13:32:50 +00002955 // FIXME handle member function templates (TK_MemberSpecialization) similarly?
2956 if (D->getTemplatedKind() ==
2957 FunctionDecl::TK_FunctionTemplateSpecialization) {
Balazs Keri3b30d652018-10-19 13:32:20 +00002958 auto FoundFunctionOrErr = FindFunctionTemplateSpecialization(D);
2959 if (!FoundFunctionOrErr)
2960 return FoundFunctionOrErr.takeError();
2961 if (FunctionDecl *FoundFunction = *FoundFunctionOrErr) {
Gabor Martondd59d272019-03-19 14:04:50 +00002962 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
2963 return Def;
Gabor Marton5254e642018-06-27 13:32:50 +00002964 FoundByLookup = FoundFunction;
2965 }
2966 }
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002967 // Try to find a function in our own ("to") context with the same name, same
2968 // type, and in the same context as the function we're importing.
Gabor Marton5254e642018-06-27 13:32:50 +00002969 else if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002970 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton5254e642018-06-27 13:32:50 +00002971 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00002972 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002973 for (auto *FoundDecl : FoundDecls) {
2974 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002975 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002976
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002977 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00002978 if (!hasSameVisibilityContext(FoundFunction, D))
2979 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002980
Gabor Marton458d1452019-02-14 13:07:03 +00002981 if (IsStructuralMatch(D, FoundFunction)) {
Gabor Martondd59d272019-03-19 14:04:50 +00002982 if (Decl *Def = FindAndMapDefinition(D, FoundFunction))
2983 return Def;
Gabor Marton458d1452019-02-14 13:07:03 +00002984 FoundByLookup = FoundFunction;
2985 break;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002986 }
Gabor Marton458d1452019-02-14 13:07:03 +00002987 // FIXME: Check for overloading more carefully, e.g., by boosting
2988 // Sema::IsOverload out to the AST library.
2989
2990 // Function overloading is okay in C++.
2991 if (Importer.getToContext().getLangOpts().CPlusPlus)
2992 continue;
2993
2994 // Complain about inconsistent function types.
Gabor Marton410f32c2019-04-01 15:29:55 +00002995 Importer.ToDiag(Loc, diag::warn_odr_function_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00002996 << Name << D->getType() << FoundFunction->getType();
2997 Importer.ToDiag(FoundFunction->getLocation(), diag::note_odr_value_here)
2998 << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002999 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003000
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003001 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003002 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003003
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003004 if (!ConflictingDecls.empty()) {
3005 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003006 ConflictingDecls.data(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003007 ConflictingDecls.size());
3008 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003009 return make_error<ImportError>(ImportError::NameConflict);
Fangrui Song6907ce22018-07-30 19:24:48 +00003010 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003011 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00003012
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003013 // We do not allow more than one in-class declaration of a function. This is
3014 // because AST clients like VTableBuilder asserts on this. VTableBuilder
3015 // assumes there is only one in-class declaration. Building a redecl
3016 // chain would result in more than one in-class declaration for
3017 // overrides (even if they are part of the same redecl chain inside the
3018 // derived class.)
3019 if (FoundByLookup) {
Mikael Holmenc1c97aa2019-01-29 06:53:31 +00003020 if (isa<CXXMethodDecl>(FoundByLookup)) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003021 if (D->getLexicalDeclContext() == D->getDeclContext()) {
3022 if (!D->doesThisDeclarationHaveABody())
3023 return Importer.MapImported(D, FoundByLookup);
3024 else {
3025 // Let's continue and build up the redecl chain in this case.
3026 // FIXME Merge the functions into one decl.
3027 }
3028 }
3029 }
3030 }
3031
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003032 DeclarationNameInfo NameInfo(Name, Loc);
3033 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00003034 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
3035 return std::move(Err);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003036
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003037 QualType FromTy = D->getType();
3038 bool usedDifferentExceptionSpec = false;
3039
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003040 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003041 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
3042 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
3043 // FunctionDecl that we are importing the FunctionProtoType for.
3044 // To avoid an infinite recursion when importing, create the FunctionDecl
3045 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00003046 if (FromEPI.ExceptionSpec.SourceDecl ||
3047 FromEPI.ExceptionSpec.SourceTemplate ||
3048 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003049 FunctionProtoType::ExtProtoInfo DefaultEPI;
3050 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003051 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003052 usedDifferentExceptionSpec = true;
3053 }
3054 }
3055
Balazs Keri3b30d652018-10-19 13:32:20 +00003056 QualType T;
3057 TypeSourceInfo *TInfo;
3058 SourceLocation ToInnerLocStart, ToEndLoc;
3059 NestedNameSpecifierLoc ToQualifierLoc;
3060 if (auto Imp = importSeq(
3061 FromTy, D->getTypeSourceInfo(), D->getInnerLocStart(),
3062 D->getQualifierLoc(), D->getEndLoc()))
3063 std::tie(T, TInfo, ToInnerLocStart, ToQualifierLoc, ToEndLoc) = *Imp;
3064 else
3065 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003066
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003067 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003068 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00003069 for (auto P : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003070 if (Expected<ParmVarDecl *> ToPOrErr = import(P))
3071 Parameters.push_back(*ToPOrErr);
3072 else
3073 return ToPOrErr.takeError();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003074 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003075
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003076 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00003077 FunctionDecl *ToFunction = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003078 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003079 if (GetImportedOrCreateDecl<CXXConstructorDecl>(
Hans Wennborgd2b9fc82019-05-06 09:51:10 +00003080 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3081 ToInnerLocStart, NameInfo, T, TInfo,
3082 FromConstructor->isExplicit(),
3083 D->isInlineSpecified(), D->isImplicit(), D->isConstexpr()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003084 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003085 } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(D)) {
3086
3087 auto Imp =
3088 importSeq(const_cast<FunctionDecl *>(FromDtor->getOperatorDelete()),
3089 FromDtor->getOperatorDeleteThisArg());
3090
3091 if (!Imp)
3092 return Imp.takeError();
3093
3094 FunctionDecl *ToOperatorDelete;
3095 Expr *ToThisArg;
3096 std::tie(ToOperatorDelete, ToThisArg) = *Imp;
3097
Gabor Marton26f72a92018-07-12 09:42:05 +00003098 if (GetImportedOrCreateDecl<CXXDestructorDecl>(
Balazs Keri3b30d652018-10-19 13:32:20 +00003099 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
3100 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
3101 D->isImplicit()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003102 return ToFunction;
Raphael Isemann1c5d23f2019-01-22 17:59:45 +00003103
3104 CXXDestructorDecl *ToDtor = cast<CXXDestructorDecl>(ToFunction);
3105
3106 ToDtor->setOperatorDelete(ToOperatorDelete, ToThisArg);
Gabor Marton26f72a92018-07-12 09:42:05 +00003107 } else if (CXXConversionDecl *FromConversion =
3108 dyn_cast<CXXConversionDecl>(D)) {
3109 if (GetImportedOrCreateDecl<CXXConversionDecl>(
3110 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003111 ToInnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(),
Hans Wennborgd2b9fc82019-05-06 09:51:10 +00003112 FromConversion->isExplicit(), D->isConstexpr(), SourceLocation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003113 return ToFunction;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003114 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003115 if (GetImportedOrCreateDecl<CXXMethodDecl>(
3116 ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003117 ToInnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003118 Method->isInlineSpecified(), D->isConstexpr(), SourceLocation()))
3119 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003120 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003121 if (GetImportedOrCreateDecl(ToFunction, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003122 ToInnerLocStart, NameInfo, T, TInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003123 D->getStorageClass(), D->isInlineSpecified(),
3124 D->hasWrittenPrototype(), D->isConstexpr()))
3125 return ToFunction;
Douglas Gregor00eace12010-02-21 18:29:16 +00003126 }
John McCall3e11ebe2010-03-15 10:12:16 +00003127
Gabor Martonf5e4f0a2018-11-20 14:19:39 +00003128 // Connect the redecl chain.
3129 if (FoundByLookup) {
3130 auto *Recent = const_cast<FunctionDecl *>(
3131 FoundByLookup->getMostRecentDecl());
3132 ToFunction->setPreviousDecl(Recent);
3133 }
3134
3135 // Import Ctor initializers.
3136 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3137 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
3138 SmallVector<CXXCtorInitializer *, 4> CtorInitializers(NumInitializers);
3139 // Import first, then allocate memory and copy if there was no error.
3140 if (Error Err = ImportContainerChecked(
3141 FromConstructor->inits(), CtorInitializers))
3142 return std::move(Err);
3143 auto **Memory =
3144 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
3145 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
3146 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
3147 ToCtor->setCtorInitializers(Memory);
3148 ToCtor->setNumCtorInitializers(NumInitializers);
3149 }
3150 }
3151
Balazs Keri3b30d652018-10-19 13:32:20 +00003152 ToFunction->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003153 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003154 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003155 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3156 ToFunction->setTrivial(D->isTrivial());
3157 ToFunction->setPure(D->isPure());
Balazs Keri3b30d652018-10-19 13:32:20 +00003158 ToFunction->setRangeEnd(ToEndLoc);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003159
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003160 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003161 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003162 Param->setOwningFunction(ToFunction);
3163 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003164 }
David Blaikie9c70e042011-09-21 18:16:56 +00003165 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003166
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003167 // We need to complete creation of FunctionProtoTypeLoc manually with setting
3168 // params it refers to.
3169 if (TInfo) {
3170 if (auto ProtoLoc =
3171 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
3172 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
3173 ProtoLoc.setParam(I, Parameters[I]);
3174 }
3175 }
3176
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003177 if (usedDifferentExceptionSpec) {
3178 // Update FunctionProtoType::ExtProtoInfo.
Balazs Keri3b30d652018-10-19 13:32:20 +00003179 if (ExpectedType TyOrErr = import(D->getType()))
3180 ToFunction->setType(*TyOrErr);
3181 else
3182 return TyOrErr.takeError();
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003183 }
3184
Balazs Keria35798d2018-07-17 09:52:41 +00003185 // Import the describing template function, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00003186 if (FromFT) {
3187 auto ToFTOrErr = import(FromFT);
3188 if (!ToFTOrErr)
3189 return ToFTOrErr.takeError();
3190 }
Balazs Keria35798d2018-07-17 09:52:41 +00003191
Gabor Marton5254e642018-06-27 13:32:50 +00003192 if (D->doesThisDeclarationHaveABody()) {
Shafik Yaghmour96b3d202019-01-28 21:55:33 +00003193 Error Err = ImportFunctionDeclBody(D, ToFunction);
3194
3195 if (Err)
3196 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003197 }
3198
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003199 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003200
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003201 // If it is a template, import all related things.
Balazs Keri3b30d652018-10-19 13:32:20 +00003202 if (Error Err = ImportTemplateInformation(D, ToFunction))
3203 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003204
Gabor Marton5254e642018-06-27 13:32:50 +00003205 bool IsFriend = D->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend);
3206
3207 // TODO Can we generalize this approach to other AST nodes as well?
3208 if (D->getDeclContext()->containsDeclAndLoad(D))
3209 DC->addDeclInternal(ToFunction);
3210 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003211 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003212
Gabor Marton5254e642018-06-27 13:32:50 +00003213 // Friend declaration's lexical context is the befriending class, but the
3214 // semantic context is the enclosing scope of the befriending class.
3215 // We want the friend functions to be found in the semantic context by lookup.
3216 // FIXME should we handle this generically in VisitFriendDecl?
3217 // In Other cases when LexicalDC != DC we don't want it to be added,
3218 // e.g out-of-class definitions like void B::f() {} .
3219 if (LexicalDC != DC && IsFriend) {
3220 DC->makeDeclVisibleInContext(ToFunction);
3221 }
3222
Gabor Marton7a0841e2018-10-29 10:18:28 +00003223 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
3224 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
3225
Gabor Marton5254e642018-06-27 13:32:50 +00003226 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003227 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3228 ExpectedDecl ToRedeclOrErr = import(*RedeclIt);
3229 if (!ToRedeclOrErr)
3230 return ToRedeclOrErr.takeError();
3231 }
Gabor Marton5254e642018-06-27 13:32:50 +00003232
Douglas Gregor43f54792010-02-17 02:12:47 +00003233 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003234}
3235
Balazs Keri3b30d652018-10-19 13:32:20 +00003236ExpectedDecl ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003237 return VisitFunctionDecl(D);
3238}
3239
Balazs Keri3b30d652018-10-19 13:32:20 +00003240ExpectedDecl ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003241 return VisitCXXMethodDecl(D);
3242}
3243
Balazs Keri3b30d652018-10-19 13:32:20 +00003244ExpectedDecl ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003245 return VisitCXXMethodDecl(D);
3246}
3247
Balazs Keri3b30d652018-10-19 13:32:20 +00003248ExpectedDecl ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor00eace12010-02-21 18:29:16 +00003249 return VisitCXXMethodDecl(D);
3250}
3251
Balazs Keri3b30d652018-10-19 13:32:20 +00003252ExpectedDecl ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00003253 // Import the major distinguishing characteristics of a variable.
3254 DeclContext *DC, *LexicalDC;
3255 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003256 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003257 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003258 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3259 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003260 if (ToD)
3261 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003262
Fangrui Song6907ce22018-07-30 19:24:48 +00003263 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003264 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003265 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003266 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003267 // For anonymous fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003268 if (!Name &&
3269 ASTImporter::getFieldIndex(D) !=
3270 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003271 continue;
3272
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003273 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003274 FoundField->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003275 Importer.MapImported(D, FoundField);
Gabor Marton42e15de2018-08-22 11:52:14 +00003276 // In case of a FieldDecl of a ClassTemplateSpecializationDecl, the
3277 // initializer of a FieldDecl might not had been instantiated in the
3278 // "To" context. However, the "From" context might instantiated that,
3279 // thus we have to merge that.
3280 if (Expr *FromInitializer = D->getInClassInitializer()) {
3281 // We don't have yet the initializer set.
3282 if (FoundField->hasInClassInitializer() &&
3283 !FoundField->getInClassInitializer()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003284 if (ExpectedExpr ToInitializerOrErr = import(FromInitializer))
3285 FoundField->setInClassInitializer(*ToInitializerOrErr);
3286 else {
3287 // We can't return error here,
Gabor Marton42e15de2018-08-22 11:52:14 +00003288 // since we already mapped D as imported.
Balazs Keri3b30d652018-10-19 13:32:20 +00003289 // FIXME: warning message?
3290 consumeError(ToInitializerOrErr.takeError());
Gabor Marton42e15de2018-08-22 11:52:14 +00003291 return FoundField;
Balazs Keri3b30d652018-10-19 13:32:20 +00003292 }
Gabor Marton42e15de2018-08-22 11:52:14 +00003293 }
3294 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003295 return FoundField;
3296 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003297
Balazs Keri3b30d652018-10-19 13:32:20 +00003298 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003299 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003300 << Name << D->getType() << FoundField->getType();
3301 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3302 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003303
3304 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003305 }
3306 }
3307
Balazs Keri3b30d652018-10-19 13:32:20 +00003308 QualType ToType;
3309 TypeSourceInfo *ToTInfo;
3310 Expr *ToBitWidth;
3311 SourceLocation ToInnerLocStart;
3312 Expr *ToInitializer;
3313 if (auto Imp = importSeq(
3314 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(),
3315 D->getInnerLocStart(), D->getInClassInitializer()))
3316 std::tie(
3317 ToType, ToTInfo, ToBitWidth, ToInnerLocStart, ToInitializer) = *Imp;
3318 else
3319 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003320
Gabor Marton26f72a92018-07-12 09:42:05 +00003321 FieldDecl *ToField;
3322 if (GetImportedOrCreateDecl(ToField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003323 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3324 ToType, ToTInfo, ToBitWidth, D->isMutable(),
3325 D->getInClassInitStyle()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003326 return ToField;
3327
Douglas Gregordd483172010-02-22 17:42:47 +00003328 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003329 ToField->setLexicalDeclContext(LexicalDC);
Balazs Keri3b30d652018-10-19 13:32:20 +00003330 if (ToInitializer)
3331 ToField->setInClassInitializer(ToInitializer);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003332 ToField->setImplicit(D->isImplicit());
Sean Callanan95e74be2011-10-21 02:57:43 +00003333 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003334 return ToField;
3335}
3336
Balazs Keri3b30d652018-10-19 13:32:20 +00003337ExpectedDecl ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00003338 // Import the major distinguishing characteristics of a variable.
3339 DeclContext *DC, *LexicalDC;
3340 DeclarationName Name;
3341 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003342 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003343 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3344 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003345 if (ToD)
3346 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003347
Fangrui Song6907ce22018-07-30 19:24:48 +00003348 // Determine whether we've already imported this field.
Gabor Marton54058b52018-12-17 13:53:12 +00003349 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003350 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003351 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003352 // For anonymous indirect fields, match up by index.
Balazs Keri2544b4b2018-08-08 09:40:57 +00003353 if (!Name &&
3354 ASTImporter::getFieldIndex(D) !=
3355 ASTImporter::getFieldIndex(FoundField))
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003356 continue;
3357
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003358 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003359 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003360 !Name.isEmpty())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003361 Importer.MapImported(D, FoundField);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003362 return FoundField;
3363 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003364
3365 // If there are more anonymous fields to check, continue.
3366 if (!Name && I < N-1)
3367 continue;
3368
Balazs Keri3b30d652018-10-19 13:32:20 +00003369 // FIXME: Why is this case not handled with calling HandleNameConflict?
Gabor Marton410f32c2019-04-01 15:29:55 +00003370 Importer.ToDiag(Loc, diag::warn_odr_field_type_inconsistent)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003371 << Name << D->getType() << FoundField->getType();
3372 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3373 << FoundField->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003374
3375 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003376 }
3377 }
3378
Francois Pichet783dd6e2010-11-21 06:08:52 +00003379 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00003380 auto TypeOrErr = import(D->getType());
3381 if (!TypeOrErr)
3382 return TypeOrErr.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003383
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003384 auto **NamedChain =
3385 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00003386
3387 unsigned i = 0;
Balazs Keri3b30d652018-10-19 13:32:20 +00003388 for (auto *PI : D->chain())
3389 if (Expected<NamedDecl *> ToD = import(PI))
3390 NamedChain[i++] = *ToD;
3391 else
3392 return ToD.takeError();
Francois Pichet783dd6e2010-11-21 06:08:52 +00003393
Gabor Marton26f72a92018-07-12 09:42:05 +00003394 llvm::MutableArrayRef<NamedDecl *> CH = {NamedChain, D->getChainingSize()};
3395 IndirectFieldDecl *ToIndirectField;
3396 if (GetImportedOrCreateDecl(ToIndirectField, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003397 Loc, Name.getAsIdentifierInfo(), *TypeOrErr, CH))
Gabor Marton26f72a92018-07-12 09:42:05 +00003398 // FIXME here we leak `NamedChain` which is allocated before
3399 return ToIndirectField;
Aaron Ballman260995b2014-10-15 16:58:18 +00003400
Francois Pichet783dd6e2010-11-21 06:08:52 +00003401 ToIndirectField->setAccess(D->getAccess());
3402 ToIndirectField->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003403 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003404 return ToIndirectField;
3405}
3406
Balazs Keri3b30d652018-10-19 13:32:20 +00003407ExpectedDecl ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00003408 // Import the major distinguishing characteristics of a declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00003409 DeclContext *DC, *LexicalDC;
3410 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
3411 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003412
3413 // Determine whether we've already imported this decl.
Gabor Marton54058b52018-12-17 13:53:12 +00003414 // FriendDecl is not a NamedDecl so we cannot use lookup.
Aleksei Sidorina693b372016-09-28 10:16:56 +00003415 auto *RD = cast<CXXRecordDecl>(DC);
3416 FriendDecl *ImportedFriend = RD->getFirstFriend();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003417
3418 while (ImportedFriend) {
3419 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
Gabor Marton950fb572018-07-17 12:39:27 +00003420 if (IsStructuralMatch(D->getFriendDecl(), ImportedFriend->getFriendDecl(),
3421 /*Complain=*/false))
Gabor Marton26f72a92018-07-12 09:42:05 +00003422 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003423
3424 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
3425 if (Importer.IsStructurallyEquivalent(
3426 D->getFriendType()->getType(),
3427 ImportedFriend->getFriendType()->getType(), true))
Gabor Marton26f72a92018-07-12 09:42:05 +00003428 return Importer.MapImported(D, ImportedFriend);
Aleksei Sidorina693b372016-09-28 10:16:56 +00003429 }
3430 ImportedFriend = ImportedFriend->getNextFriend();
3431 }
3432
3433 // Not found. Create it.
3434 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00003435 if (NamedDecl *FriendD = D->getFriendDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003436 NamedDecl *ToFriendD;
3437 if (Error Err = importInto(ToFriendD, FriendD))
3438 return std::move(Err);
3439
3440 if (FriendD->getFriendObjectKind() != Decl::FOK_None &&
Peter Szecsib180eeb2018-04-25 17:28:03 +00003441 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
3442 ToFriendD->setObjectOfFriendDecl(false);
3443
3444 ToFU = ToFriendD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003445 } else { // The friend is a type, not a decl.
3446 if (auto TSIOrErr = import(D->getFriendType()))
3447 ToFU = *TSIOrErr;
3448 else
3449 return TSIOrErr.takeError();
3450 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00003451
3452 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003453 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003454 for (unsigned I = 0; I < D->NumTPLists; I++) {
Balazs Keridec09162019-03-20 15:42:42 +00003455 if (auto ListOrErr = import(FromTPLists[I]))
Balazs Keri3b30d652018-10-19 13:32:20 +00003456 ToTPLists[I] = *ListOrErr;
3457 else
3458 return ListOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00003459 }
3460
Balazs Keri3b30d652018-10-19 13:32:20 +00003461 auto LocationOrErr = import(D->getLocation());
3462 if (!LocationOrErr)
3463 return LocationOrErr.takeError();
3464 auto FriendLocOrErr = import(D->getFriendLoc());
3465 if (!FriendLocOrErr)
3466 return FriendLocOrErr.takeError();
3467
Gabor Marton26f72a92018-07-12 09:42:05 +00003468 FriendDecl *FrD;
3469 if (GetImportedOrCreateDecl(FrD, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003470 *LocationOrErr, ToFU,
3471 *FriendLocOrErr, ToTPLists))
Gabor Marton26f72a92018-07-12 09:42:05 +00003472 return FrD;
Aleksei Sidorina693b372016-09-28 10:16:56 +00003473
3474 FrD->setAccess(D->getAccess());
3475 FrD->setLexicalDeclContext(LexicalDC);
3476 LexicalDC->addDeclInternal(FrD);
3477 return FrD;
3478}
3479
Balazs Keri3b30d652018-10-19 13:32:20 +00003480ExpectedDecl ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003481 // Import the major distinguishing characteristics of an ivar.
3482 DeclContext *DC, *LexicalDC;
3483 DeclarationName Name;
3484 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003485 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003486 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3487 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003488 if (ToD)
3489 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003490
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003491 // Determine whether we've already imported this ivar
Gabor Marton54058b52018-12-17 13:53:12 +00003492 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003493 for (auto *FoundDecl : FoundDecls) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003494 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003495 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003496 FoundIvar->getType())) {
Gabor Marton26f72a92018-07-12 09:42:05 +00003497 Importer.MapImported(D, FoundIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003498 return FoundIvar;
3499 }
3500
Gabor Marton410f32c2019-04-01 15:29:55 +00003501 Importer.ToDiag(Loc, diag::warn_odr_ivar_type_inconsistent)
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003502 << Name << D->getType() << FoundIvar->getType();
3503 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3504 << FoundIvar->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003505
3506 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003507 }
3508 }
3509
Balazs Keri3b30d652018-10-19 13:32:20 +00003510 QualType ToType;
3511 TypeSourceInfo *ToTypeSourceInfo;
3512 Expr *ToBitWidth;
3513 SourceLocation ToInnerLocStart;
3514 if (auto Imp = importSeq(
3515 D->getType(), D->getTypeSourceInfo(), D->getBitWidth(), D->getInnerLocStart()))
3516 std::tie(ToType, ToTypeSourceInfo, ToBitWidth, ToInnerLocStart) = *Imp;
3517 else
3518 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003519
Gabor Marton26f72a92018-07-12 09:42:05 +00003520 ObjCIvarDecl *ToIvar;
3521 if (GetImportedOrCreateDecl(
3522 ToIvar, D, Importer.getToContext(), cast<ObjCContainerDecl>(DC),
Balazs Keri3b30d652018-10-19 13:32:20 +00003523 ToInnerLocStart, Loc, Name.getAsIdentifierInfo(),
3524 ToType, ToTypeSourceInfo,
3525 D->getAccessControl(),ToBitWidth, D->getSynthesize()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003526 return ToIvar;
3527
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003528 ToIvar->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003529 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003530 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003531}
3532
Balazs Keri3b30d652018-10-19 13:32:20 +00003533ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003534
3535 SmallVector<Decl*, 2> Redecls = getCanonicalForwardRedeclChain(D);
3536 auto RedeclIt = Redecls.begin();
3537 // Import the first part of the decl chain. I.e. import all previous
3538 // declarations starting from the canonical decl.
Balazs Keri3b30d652018-10-19 13:32:20 +00003539 for (; RedeclIt != Redecls.end() && *RedeclIt != D; ++RedeclIt) {
3540 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3541 if (!RedeclOrErr)
3542 return RedeclOrErr.takeError();
3543 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003544 assert(*RedeclIt == D);
3545
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003546 // Import the major distinguishing characteristics of a variable.
3547 DeclContext *DC, *LexicalDC;
3548 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003549 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003550 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003551 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3552 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003553 if (ToD)
3554 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003555
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003556 // Try to find a variable in our own ("to") context with the same name and
3557 // in the same context as the variable we're importing.
Gabor Martonac3a5d62018-09-17 12:04:52 +00003558 VarDecl *FoundByLookup = nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003559 if (D->isFileVarDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003560 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003561 unsigned IDNS = Decl::IDNS_Ordinary;
Gabor Marton54058b52018-12-17 13:53:12 +00003562 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003563 for (auto *FoundDecl : FoundDecls) {
3564 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003565 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003566
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003567 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Gabor Marton458d1452019-02-14 13:07:03 +00003568 if (!hasSameVisibilityContext(FoundVar, D))
3569 continue;
3570 if (Importer.IsStructurallyEquivalent(D->getType(),
3571 FoundVar->getType())) {
Gabor Martonac3a5d62018-09-17 12:04:52 +00003572
Gabor Marton458d1452019-02-14 13:07:03 +00003573 // The VarDecl in the "From" context has a definition, but in the
3574 // "To" context we already have a definition.
3575 VarDecl *FoundDef = FoundVar->getDefinition();
3576 if (D->isThisDeclarationADefinition() && FoundDef)
3577 // FIXME Check for ODR error if the two definitions have
3578 // different initializers?
3579 return Importer.MapImported(D, FoundDef);
Gabor Martonac3a5d62018-09-17 12:04:52 +00003580
Gabor Marton458d1452019-02-14 13:07:03 +00003581 // The VarDecl in the "From" context has an initializer, but in the
3582 // "To" context we already have an initializer.
3583 const VarDecl *FoundDInit = nullptr;
3584 if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit))
3585 // FIXME Diagnose ODR error if the two initializers are different?
3586 return Importer.MapImported(D, const_cast<VarDecl*>(FoundDInit));
3587
3588 FoundByLookup = FoundVar;
3589 break;
3590 }
3591
3592 const ArrayType *FoundArray
3593 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3594 const ArrayType *TArray
3595 = Importer.getToContext().getAsArrayType(D->getType());
3596 if (FoundArray && TArray) {
3597 if (isa<IncompleteArrayType>(FoundArray) &&
3598 isa<ConstantArrayType>(TArray)) {
3599 // Import the type.
3600 if (auto TyOrErr = import(D->getType()))
3601 FoundVar->setType(*TyOrErr);
3602 else
3603 return TyOrErr.takeError();
Gabor Martonac3a5d62018-09-17 12:04:52 +00003604
3605 FoundByLookup = FoundVar;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003606 break;
Gabor Marton458d1452019-02-14 13:07:03 +00003607 } else if (isa<IncompleteArrayType>(TArray) &&
3608 isa<ConstantArrayType>(FoundArray)) {
3609 FoundByLookup = FoundVar;
3610 break;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003611 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003612 }
Gabor Marton458d1452019-02-14 13:07:03 +00003613
Gabor Marton410f32c2019-04-01 15:29:55 +00003614 Importer.ToDiag(Loc, diag::warn_odr_variable_type_inconsistent)
Gabor Marton458d1452019-02-14 13:07:03 +00003615 << Name << D->getType() << FoundVar->getType();
3616 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3617 << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003618 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003619
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003620 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003621 }
3622
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003623 if (!ConflictingDecls.empty()) {
3624 Name = Importer.HandleNameConflict(Name, DC, IDNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00003625 ConflictingDecls.data(),
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003626 ConflictingDecls.size());
3627 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00003628 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003629 }
3630 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003631
Balazs Keri3b30d652018-10-19 13:32:20 +00003632 QualType ToType;
3633 TypeSourceInfo *ToTypeSourceInfo;
3634 SourceLocation ToInnerLocStart;
3635 NestedNameSpecifierLoc ToQualifierLoc;
3636 if (auto Imp = importSeq(
3637 D->getType(), D->getTypeSourceInfo(), D->getInnerLocStart(),
3638 D->getQualifierLoc()))
3639 std::tie(ToType, ToTypeSourceInfo, ToInnerLocStart, ToQualifierLoc) = *Imp;
3640 else
3641 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003642
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003643 // Create the imported variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00003644 VarDecl *ToVar;
3645 if (GetImportedOrCreateDecl(ToVar, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003646 ToInnerLocStart, Loc,
3647 Name.getAsIdentifierInfo(),
3648 ToType, ToTypeSourceInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00003649 D->getStorageClass()))
3650 return ToVar;
3651
Balazs Keri3b30d652018-10-19 13:32:20 +00003652 ToVar->setQualifierInfo(ToQualifierLoc);
Douglas Gregordd483172010-02-22 17:42:47 +00003653 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003654 ToVar->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00003655
Gabor Martonac3a5d62018-09-17 12:04:52 +00003656 if (FoundByLookup) {
3657 auto *Recent = const_cast<VarDecl *>(FoundByLookup->getMostRecentDecl());
3658 ToVar->setPreviousDecl(Recent);
3659 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00003660
Balazs Keri3b30d652018-10-19 13:32:20 +00003661 if (Error Err = ImportInitializer(D, ToVar))
3662 return std::move(Err);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003663
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003664 if (D->isConstexpr())
3665 ToVar->setConstexpr(true);
3666
Gabor Martonac3a5d62018-09-17 12:04:52 +00003667 if (D->getDeclContext()->containsDeclAndLoad(D))
3668 DC->addDeclInternal(ToVar);
3669 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
3670 LexicalDC->addDeclInternal(ToVar);
3671
3672 // Import the rest of the chain. I.e. import all subsequent declarations.
Balazs Keri3b30d652018-10-19 13:32:20 +00003673 for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) {
3674 ExpectedDecl RedeclOrErr = import(*RedeclIt);
3675 if (!RedeclOrErr)
3676 return RedeclOrErr.takeError();
3677 }
Gabor Martonac3a5d62018-09-17 12:04:52 +00003678
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003679 return ToVar;
3680}
3681
Balazs Keri3b30d652018-10-19 13:32:20 +00003682ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
Douglas Gregor8b228d72010-02-17 21:22:52 +00003683 // Parameters are created in the translation unit's context, then moved
3684 // into the function declaration's context afterward.
3685 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003686
Balazs Keri3b30d652018-10-19 13:32:20 +00003687 DeclarationName ToDeclName;
3688 SourceLocation ToLocation;
3689 QualType ToType;
3690 if (auto Imp = importSeq(D->getDeclName(), D->getLocation(), D->getType()))
3691 std::tie(ToDeclName, ToLocation, ToType) = *Imp;
3692 else
3693 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003694
Douglas Gregor8b228d72010-02-17 21:22:52 +00003695 // Create the imported parameter.
Gabor Marton26f72a92018-07-12 09:42:05 +00003696 ImplicitParamDecl *ToParm = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00003697 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
3698 ToLocation, ToDeclName.getAsIdentifierInfo(),
3699 ToType, D->getParameterKind()))
Gabor Marton26f72a92018-07-12 09:42:05 +00003700 return ToParm;
3701 return ToParm;
Douglas Gregor8b228d72010-02-17 21:22:52 +00003702}
3703
Balazs Keri3b30d652018-10-19 13:32:20 +00003704ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003705 // Parameters are created in the translation unit's context, then moved
3706 // into the function declaration's context afterward.
3707 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00003708
Balazs Keri3b30d652018-10-19 13:32:20 +00003709 DeclarationName ToDeclName;
3710 SourceLocation ToLocation, ToInnerLocStart;
3711 QualType ToType;
3712 TypeSourceInfo *ToTypeSourceInfo;
3713 if (auto Imp = importSeq(
3714 D->getDeclName(), D->getLocation(), D->getType(), D->getInnerLocStart(),
3715 D->getTypeSourceInfo()))
3716 std::tie(
3717 ToDeclName, ToLocation, ToType, ToInnerLocStart,
3718 ToTypeSourceInfo) = *Imp;
3719 else
3720 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00003721
Gabor Marton26f72a92018-07-12 09:42:05 +00003722 ParmVarDecl *ToParm;
3723 if (GetImportedOrCreateDecl(ToParm, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003724 ToInnerLocStart, ToLocation,
3725 ToDeclName.getAsIdentifierInfo(), ToType,
3726 ToTypeSourceInfo, D->getStorageClass(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003727 /*DefaultArg*/ nullptr))
3728 return ToParm;
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003729
3730 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003731 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003732 ToParm->setKNRPromoted(D->isKNRPromoted());
3733
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003734 if (D->hasUninstantiatedDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003735 if (auto ToDefArgOrErr = import(D->getUninstantiatedDefaultArg()))
3736 ToParm->setUninstantiatedDefaultArg(*ToDefArgOrErr);
3737 else
3738 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003739 } else if (D->hasUnparsedDefaultArg()) {
3740 ToParm->setUnparsedDefaultArg();
3741 } else if (D->hasDefaultArg()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003742 if (auto ToDefArgOrErr = import(D->getDefaultArg()))
3743 ToParm->setDefaultArg(*ToDefArgOrErr);
3744 else
3745 return ToDefArgOrErr.takeError();
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003746 }
Sean Callanan59721b32015-04-28 18:41:46 +00003747
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003748 if (D->isObjCMethodParameter()) {
3749 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3750 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3751 } else {
3752 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3753 D->getFunctionScopeIndex());
3754 }
3755
Gabor Marton26f72a92018-07-12 09:42:05 +00003756 return ToParm;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003757}
3758
Balazs Keri3b30d652018-10-19 13:32:20 +00003759ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003760 // Import the major distinguishing characteristics of a method.
3761 DeclContext *DC, *LexicalDC;
3762 DeclarationName Name;
3763 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003764 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003765 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3766 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003767 if (ToD)
3768 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003769
Gabor Marton54058b52018-12-17 13:53:12 +00003770 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003771 for (auto *FoundDecl : FoundDecls) {
3772 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003773 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3774 continue;
3775
3776 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003777 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3778 FoundMethod->getReturnType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003779 Importer.ToDiag(Loc, diag::warn_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003780 << D->isInstanceMethod() << Name << D->getReturnType()
3781 << FoundMethod->getReturnType();
Fangrui Song6907ce22018-07-30 19:24:48 +00003782 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003783 diag::note_odr_objc_method_here)
3784 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003785
3786 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003787 }
3788
3789 // Check the number of parameters.
3790 if (D->param_size() != FoundMethod->param_size()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003791 Importer.ToDiag(Loc, diag::warn_odr_objc_method_num_params_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003792 << D->isInstanceMethod() << Name
3793 << D->param_size() << FoundMethod->param_size();
Fangrui Song6907ce22018-07-30 19:24:48 +00003794 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003795 diag::note_odr_objc_method_here)
3796 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003797
3798 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003799 }
3800
3801 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003802 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003803 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3804 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003805 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003806 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003807 Importer.FromDiag((*P)->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00003808 diag::warn_odr_objc_method_param_type_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003809 << D->isInstanceMethod() << Name
3810 << (*P)->getType() << (*FoundP)->getType();
3811 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3812 << (*FoundP)->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00003813
3814 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003815 }
3816 }
3817
3818 // Check variadic/non-variadic.
3819 // Check the number of parameters.
3820 if (D->isVariadic() != FoundMethod->isVariadic()) {
Gabor Marton410f32c2019-04-01 15:29:55 +00003821 Importer.ToDiag(Loc, diag::warn_odr_objc_method_variadic_inconsistent)
Douglas Gregor43f54792010-02-17 02:12:47 +00003822 << D->isInstanceMethod() << Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00003823 Importer.ToDiag(FoundMethod->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003824 diag::note_odr_objc_method_here)
3825 << D->isInstanceMethod() << Name;
Balazs Keri3b30d652018-10-19 13:32:20 +00003826
3827 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor43f54792010-02-17 02:12:47 +00003828 }
3829
3830 // FIXME: Any other bits we need to merge?
Gabor Marton26f72a92018-07-12 09:42:05 +00003831 return Importer.MapImported(D, FoundMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003832 }
3833 }
3834
Balazs Keri3b30d652018-10-19 13:32:20 +00003835 SourceLocation ToEndLoc;
3836 QualType ToReturnType;
3837 TypeSourceInfo *ToReturnTypeSourceInfo;
3838 if (auto Imp = importSeq(
3839 D->getEndLoc(), D->getReturnType(), D->getReturnTypeSourceInfo()))
3840 std::tie(ToEndLoc, ToReturnType, ToReturnTypeSourceInfo) = *Imp;
3841 else
3842 return Imp.takeError();
Douglas Gregor12852d92010-03-08 14:59:44 +00003843
Gabor Marton26f72a92018-07-12 09:42:05 +00003844 ObjCMethodDecl *ToMethod;
3845 if (GetImportedOrCreateDecl(
3846 ToMethod, D, Importer.getToContext(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00003847 ToEndLoc, Name.getObjCSelector(), ToReturnType,
3848 ToReturnTypeSourceInfo, DC, D->isInstanceMethod(), D->isVariadic(),
Gabor Marton26f72a92018-07-12 09:42:05 +00003849 D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3850 D->getImplementationControl(), D->hasRelatedResultType()))
3851 return ToMethod;
Douglas Gregor43f54792010-02-17 02:12:47 +00003852
3853 // FIXME: When we decide to merge method definitions, we'll need to
3854 // deal with implicit parameters.
3855
3856 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003857 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003858 for (auto *FromP : D->parameters()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003859 if (Expected<ParmVarDecl *> ToPOrErr = import(FromP))
3860 ToParams.push_back(*ToPOrErr);
3861 else
3862 return ToPOrErr.takeError();
Douglas Gregor43f54792010-02-17 02:12:47 +00003863 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003864
Douglas Gregor43f54792010-02-17 02:12:47 +00003865 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003866 for (auto *ToParam : ToParams) {
3867 ToParam->setOwningFunction(ToMethod);
3868 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003869 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003870
Balazs Keri3b30d652018-10-19 13:32:20 +00003871 SmallVector<SourceLocation, 12> FromSelLocs;
3872 D->getSelectorLocs(FromSelLocs);
3873 SmallVector<SourceLocation, 12> ToSelLocs(FromSelLocs.size());
3874 if (Error Err = ImportContainerChecked(FromSelLocs, ToSelLocs))
3875 return std::move(Err);
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003876
Balazs Keri3b30d652018-10-19 13:32:20 +00003877 ToMethod->setMethodParams(Importer.getToContext(), ToParams, ToSelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003878
3879 ToMethod->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003880 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003881 return ToMethod;
3882}
3883
Balazs Keri3b30d652018-10-19 13:32:20 +00003884ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00003885 // Import the major distinguishing characteristics of a category.
3886 DeclContext *DC, *LexicalDC;
3887 DeclarationName Name;
3888 SourceLocation Loc;
3889 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003890 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3891 return std::move(Err);
Douglas Gregor85f3f952015-07-07 03:57:15 +00003892 if (ToD)
3893 return ToD;
3894
Balazs Keri3b30d652018-10-19 13:32:20 +00003895 SourceLocation ToVarianceLoc, ToLocation, ToColonLoc;
3896 TypeSourceInfo *ToTypeSourceInfo;
3897 if (auto Imp = importSeq(
3898 D->getVarianceLoc(), D->getLocation(), D->getColonLoc(),
3899 D->getTypeSourceInfo()))
3900 std::tie(ToVarianceLoc, ToLocation, ToColonLoc, ToTypeSourceInfo) = *Imp;
3901 else
3902 return Imp.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00003903
Gabor Marton26f72a92018-07-12 09:42:05 +00003904 ObjCTypeParamDecl *Result;
3905 if (GetImportedOrCreateDecl(
3906 Result, D, Importer.getToContext(), DC, D->getVariance(),
Balazs Keri3b30d652018-10-19 13:32:20 +00003907 ToVarianceLoc, D->getIndex(),
3908 ToLocation, Name.getAsIdentifierInfo(),
3909 ToColonLoc, ToTypeSourceInfo))
Gabor Marton26f72a92018-07-12 09:42:05 +00003910 return Result;
3911
Douglas Gregor85f3f952015-07-07 03:57:15 +00003912 Result->setLexicalDeclContext(LexicalDC);
3913 return Result;
3914}
3915
Balazs Keri3b30d652018-10-19 13:32:20 +00003916ExpectedDecl ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00003917 // Import the major distinguishing characteristics of a category.
3918 DeclContext *DC, *LexicalDC;
3919 DeclarationName Name;
3920 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003921 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00003922 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3923 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00003924 if (ToD)
3925 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003926
Balazs Keri3b30d652018-10-19 13:32:20 +00003927 ObjCInterfaceDecl *ToInterface;
3928 if (Error Err = importInto(ToInterface, D->getClassInterface()))
3929 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00003930
Douglas Gregor84c51c32010-02-18 01:47:50 +00003931 // Determine if we've already encountered this category.
3932 ObjCCategoryDecl *MergeWithCategory
3933 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3934 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3935 if (!ToCategory) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003936 SourceLocation ToAtStartLoc, ToCategoryNameLoc;
3937 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
3938 if (auto Imp = importSeq(
3939 D->getAtStartLoc(), D->getCategoryNameLoc(),
3940 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
3941 std::tie(
3942 ToAtStartLoc, ToCategoryNameLoc,
3943 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
3944 else
3945 return Imp.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00003946
3947 if (GetImportedOrCreateDecl(ToCategory, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00003948 ToAtStartLoc, Loc,
3949 ToCategoryNameLoc,
Gabor Marton26f72a92018-07-12 09:42:05 +00003950 Name.getAsIdentifierInfo(), ToInterface,
3951 /*TypeParamList=*/nullptr,
Balazs Keri3b30d652018-10-19 13:32:20 +00003952 ToIvarLBraceLoc,
3953 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00003954 return ToCategory;
3955
Douglas Gregor84c51c32010-02-18 01:47:50 +00003956 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003957 LexicalDC->addDeclInternal(ToCategory);
Balazs Keri3b30d652018-10-19 13:32:20 +00003958 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003959 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00003960 if (auto PListOrErr = ImportObjCTypeParamList(D->getTypeParamList()))
3961 ToCategory->setTypeParamList(*PListOrErr);
3962 else
3963 return PListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00003964
Douglas Gregor84c51c32010-02-18 01:47:50 +00003965 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003966 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3967 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003968 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3969 = D->protocol_loc_begin();
3970 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3971 FromProtoEnd = D->protocol_end();
3972 FromProto != FromProtoEnd;
3973 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003974 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
3975 Protocols.push_back(*ToProtoOrErr);
3976 else
3977 return ToProtoOrErr.takeError();
3978
3979 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
3980 ProtocolLocs.push_back(*ToProtoLocOrErr);
3981 else
3982 return ToProtoLocOrErr.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 // FIXME: If we're merging, make sure that the protocol list is the same.
3986 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3987 ProtocolLocs.data(), Importer.getToContext());
Balazs Keri3b30d652018-10-19 13:32:20 +00003988
Douglas Gregor84c51c32010-02-18 01:47:50 +00003989 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00003990 Importer.MapImported(D, ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003991 }
Fangrui Song6907ce22018-07-30 19:24:48 +00003992
Douglas Gregor84c51c32010-02-18 01:47:50 +00003993 // Import all of the members of this category.
Balazs Keri3b30d652018-10-19 13:32:20 +00003994 if (Error Err = ImportDeclContext(D))
3995 return std::move(Err);
Fangrui Song6907ce22018-07-30 19:24:48 +00003996
Douglas Gregor84c51c32010-02-18 01:47:50 +00003997 // If we have an implementation, import it as well.
3998 if (D->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00003999 if (Expected<ObjCCategoryImplDecl *> ToImplOrErr =
4000 import(D->getImplementation()))
4001 ToCategory->setImplementation(*ToImplOrErr);
4002 else
4003 return ToImplOrErr.takeError();
Douglas Gregor84c51c32010-02-18 01:47:50 +00004004 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004005
Douglas Gregor84c51c32010-02-18 01:47:50 +00004006 return ToCategory;
4007}
4008
Balazs Keri3b30d652018-10-19 13:32:20 +00004009Error ASTNodeImporter::ImportDefinition(
4010 ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004011 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00004012 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004013 if (Error Err = ImportDeclContext(From))
4014 return Err;
4015 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004016 }
4017
4018 // Start the protocol definition
4019 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004020
Douglas Gregor2aa53772012-01-24 17:42:07 +00004021 // Import protocols
4022 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4023 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004024 ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc =
4025 From->protocol_loc_begin();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004026 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
4027 FromProtoEnd = From->protocol_end();
4028 FromProto != FromProtoEnd;
4029 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004030 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4031 Protocols.push_back(*ToProtoOrErr);
4032 else
4033 return ToProtoOrErr.takeError();
4034
4035 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4036 ProtocolLocs.push_back(*ToProtoLocOrErr);
4037 else
4038 return ToProtoLocOrErr.takeError();
4039
Douglas Gregor2aa53772012-01-24 17:42:07 +00004040 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004041
Douglas Gregor2aa53772012-01-24 17:42:07 +00004042 // FIXME: If we're merging, make sure that the protocol list is the same.
4043 To->setProtocolList(Protocols.data(), Protocols.size(),
4044 ProtocolLocs.data(), Importer.getToContext());
4045
Douglas Gregor2e15c842012-02-01 21:00:38 +00004046 if (shouldForceImportDeclContext(Kind)) {
4047 // Import all of the members of this protocol.
Balazs Keri3b30d652018-10-19 13:32:20 +00004048 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4049 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004050 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004051 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004052}
4053
Balazs Keri3b30d652018-10-19 13:32:20 +00004054ExpectedDecl ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004055 // If this protocol has a definition in the translation unit we're coming
Douglas Gregor2aa53772012-01-24 17:42:07 +00004056 // from, but this particular declaration is not that definition, import the
4057 // definition and map to that.
4058 ObjCProtocolDecl *Definition = D->getDefinition();
4059 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004060 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4061 return Importer.MapImported(D, *ImportedDefOrErr);
4062 else
4063 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004064 }
4065
Douglas Gregor84c51c32010-02-18 01:47:50 +00004066 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00004067 DeclContext *DC, *LexicalDC;
4068 DeclarationName Name;
4069 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004070 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004071 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4072 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004073 if (ToD)
4074 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00004075
Craig Topper36250ad2014-05-12 05:36:57 +00004076 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004077 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004078 for (auto *FoundDecl : FoundDecls) {
4079 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004080 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004081
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004082 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00004083 break;
4084 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004085
Douglas Gregor98d156a2010-02-17 16:12:00 +00004086 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004087 if (!ToProto) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004088 auto ToAtBeginLocOrErr = import(D->getAtStartLoc());
4089 if (!ToAtBeginLocOrErr)
4090 return ToAtBeginLocOrErr.takeError();
4091
Gabor Marton26f72a92018-07-12 09:42:05 +00004092 if (GetImportedOrCreateDecl(ToProto, D, Importer.getToContext(), DC,
4093 Name.getAsIdentifierInfo(), Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004094 *ToAtBeginLocOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004095 /*PrevDecl=*/nullptr))
4096 return ToProto;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004097 ToProto->setLexicalDeclContext(LexicalDC);
4098 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004099 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004100
4101 Importer.MapImported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00004102
Balazs Keri3b30d652018-10-19 13:32:20 +00004103 if (D->isThisDeclarationADefinition())
4104 if (Error Err = ImportDefinition(D, ToProto))
4105 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004106
Douglas Gregor98d156a2010-02-17 16:12:00 +00004107 return ToProto;
4108}
4109
Balazs Keri3b30d652018-10-19 13:32:20 +00004110ExpectedDecl ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
4111 DeclContext *DC, *LexicalDC;
4112 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4113 return std::move(Err);
Sean Callanan0aae0412014-12-10 00:00:37 +00004114
Balazs Keri3b30d652018-10-19 13:32:20 +00004115 ExpectedSLoc ExternLocOrErr = import(D->getExternLoc());
4116 if (!ExternLocOrErr)
4117 return ExternLocOrErr.takeError();
4118
4119 ExpectedSLoc LangLocOrErr = import(D->getLocation());
4120 if (!LangLocOrErr)
4121 return LangLocOrErr.takeError();
Sean Callanan0aae0412014-12-10 00:00:37 +00004122
4123 bool HasBraces = D->hasBraces();
Gabor Marton26f72a92018-07-12 09:42:05 +00004124
4125 LinkageSpecDecl *ToLinkageSpec;
4126 if (GetImportedOrCreateDecl(ToLinkageSpec, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004127 *ExternLocOrErr, *LangLocOrErr,
4128 D->getLanguage(), HasBraces))
Gabor Marton26f72a92018-07-12 09:42:05 +00004129 return ToLinkageSpec;
Sean Callanan0aae0412014-12-10 00:00:37 +00004130
4131 if (HasBraces) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004132 ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
4133 if (!RBraceLocOrErr)
4134 return RBraceLocOrErr.takeError();
4135 ToLinkageSpec->setRBraceLoc(*RBraceLocOrErr);
Sean Callanan0aae0412014-12-10 00:00:37 +00004136 }
4137
4138 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
4139 LexicalDC->addDeclInternal(ToLinkageSpec);
4140
Sean Callanan0aae0412014-12-10 00:00:37 +00004141 return ToLinkageSpec;
4142}
4143
Balazs Keri3b30d652018-10-19 13:32:20 +00004144ExpectedDecl ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004145 DeclContext *DC, *LexicalDC;
4146 DeclarationName Name;
4147 SourceLocation Loc;
4148 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004149 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4150 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004151 if (ToD)
4152 return ToD;
4153
Balazs Keri3b30d652018-10-19 13:32:20 +00004154 SourceLocation ToLoc, ToUsingLoc;
4155 NestedNameSpecifierLoc ToQualifierLoc;
4156 if (auto Imp = importSeq(
4157 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc()))
4158 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc) = *Imp;
4159 else
4160 return Imp.takeError();
4161
4162 DeclarationNameInfo NameInfo(Name, ToLoc);
4163 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4164 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004165
Gabor Marton26f72a92018-07-12 09:42:05 +00004166 UsingDecl *ToUsing;
4167 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004168 ToUsingLoc, ToQualifierLoc, NameInfo,
Gabor Marton26f72a92018-07-12 09:42:05 +00004169 D->hasTypename()))
4170 return ToUsing;
4171
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004172 ToUsing->setLexicalDeclContext(LexicalDC);
4173 LexicalDC->addDeclInternal(ToUsing);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004174
4175 if (NamedDecl *FromPattern =
4176 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004177 if (Expected<NamedDecl *> ToPatternOrErr = import(FromPattern))
4178 Importer.getToContext().setInstantiatedFromUsingDecl(
4179 ToUsing, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004180 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004181 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004182 }
4183
Balazs Keri3b30d652018-10-19 13:32:20 +00004184 for (UsingShadowDecl *FromShadow : D->shadows()) {
4185 if (Expected<UsingShadowDecl *> ToShadowOrErr = import(FromShadow))
4186 ToUsing->addShadowDecl(*ToShadowOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004187 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004188 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004189 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004190 return ToShadowOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004191 }
4192 return ToUsing;
4193}
4194
Balazs Keri3b30d652018-10-19 13:32:20 +00004195ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004196 DeclContext *DC, *LexicalDC;
4197 DeclarationName Name;
4198 SourceLocation Loc;
4199 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004200 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4201 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004202 if (ToD)
4203 return ToD;
4204
Balazs Keri3b30d652018-10-19 13:32:20 +00004205 Expected<UsingDecl *> ToUsingOrErr = import(D->getUsingDecl());
4206 if (!ToUsingOrErr)
4207 return ToUsingOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004208
Balazs Keri3b30d652018-10-19 13:32:20 +00004209 Expected<NamedDecl *> ToTargetOrErr = import(D->getTargetDecl());
4210 if (!ToTargetOrErr)
4211 return ToTargetOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004212
Gabor Marton26f72a92018-07-12 09:42:05 +00004213 UsingShadowDecl *ToShadow;
4214 if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004215 *ToUsingOrErr, *ToTargetOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004216 return ToShadow;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004217
4218 ToShadow->setLexicalDeclContext(LexicalDC);
4219 ToShadow->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004220
4221 if (UsingShadowDecl *FromPattern =
4222 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004223 if (Expected<UsingShadowDecl *> ToPatternOrErr = import(FromPattern))
4224 Importer.getToContext().setInstantiatedFromUsingShadowDecl(
4225 ToShadow, *ToPatternOrErr);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004226 else
Balazs Keri3b30d652018-10-19 13:32:20 +00004227 // FIXME: We return error here but the definition is already created
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004228 // and available with lookups. How to fix this?..
Balazs Keri3b30d652018-10-19 13:32:20 +00004229 return ToPatternOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004230 }
4231
4232 LexicalDC->addDeclInternal(ToShadow);
4233
4234 return ToShadow;
4235}
4236
Balazs Keri3b30d652018-10-19 13:32:20 +00004237ExpectedDecl ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004238 DeclContext *DC, *LexicalDC;
4239 DeclarationName Name;
4240 SourceLocation Loc;
4241 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004242 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4243 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004244 if (ToD)
4245 return ToD;
4246
Balazs Keri3b30d652018-10-19 13:32:20 +00004247 auto ToComAncestorOrErr = Importer.ImportContext(D->getCommonAncestor());
4248 if (!ToComAncestorOrErr)
4249 return ToComAncestorOrErr.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004250
Balazs Keri3b30d652018-10-19 13:32:20 +00004251 NamespaceDecl *ToNominatedNamespace;
4252 SourceLocation ToUsingLoc, ToNamespaceKeyLocation, ToIdentLocation;
4253 NestedNameSpecifierLoc ToQualifierLoc;
4254 if (auto Imp = importSeq(
4255 D->getNominatedNamespace(), D->getUsingLoc(),
4256 D->getNamespaceKeyLocation(), D->getQualifierLoc(),
4257 D->getIdentLocation()))
4258 std::tie(
4259 ToNominatedNamespace, ToUsingLoc, ToNamespaceKeyLocation,
4260 ToQualifierLoc, ToIdentLocation) = *Imp;
4261 else
4262 return Imp.takeError();
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004263
Gabor Marton26f72a92018-07-12 09:42:05 +00004264 UsingDirectiveDecl *ToUsingDir;
4265 if (GetImportedOrCreateDecl(ToUsingDir, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004266 ToUsingLoc,
4267 ToNamespaceKeyLocation,
4268 ToQualifierLoc,
4269 ToIdentLocation,
4270 ToNominatedNamespace, *ToComAncestorOrErr))
Gabor Marton26f72a92018-07-12 09:42:05 +00004271 return ToUsingDir;
4272
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004273 ToUsingDir->setLexicalDeclContext(LexicalDC);
4274 LexicalDC->addDeclInternal(ToUsingDir);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004275
4276 return ToUsingDir;
4277}
4278
Balazs Keri3b30d652018-10-19 13:32:20 +00004279ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingValueDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004280 UnresolvedUsingValueDecl *D) {
4281 DeclContext *DC, *LexicalDC;
4282 DeclarationName Name;
4283 SourceLocation Loc;
4284 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004285 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4286 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004287 if (ToD)
4288 return ToD;
4289
Balazs Keri3b30d652018-10-19 13:32:20 +00004290 SourceLocation ToLoc, ToUsingLoc, ToEllipsisLoc;
4291 NestedNameSpecifierLoc ToQualifierLoc;
4292 if (auto Imp = importSeq(
4293 D->getNameInfo().getLoc(), D->getUsingLoc(), D->getQualifierLoc(),
4294 D->getEllipsisLoc()))
4295 std::tie(ToLoc, ToUsingLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4296 else
4297 return Imp.takeError();
4298
4299 DeclarationNameInfo NameInfo(Name, ToLoc);
4300 if (Error Err = ImportDeclarationNameLoc(D->getNameInfo(), NameInfo))
4301 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004302
Gabor Marton26f72a92018-07-12 09:42:05 +00004303 UnresolvedUsingValueDecl *ToUsingValue;
4304 if (GetImportedOrCreateDecl(ToUsingValue, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004305 ToUsingLoc, ToQualifierLoc, NameInfo,
4306 ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004307 return ToUsingValue;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004308
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004309 ToUsingValue->setAccess(D->getAccess());
4310 ToUsingValue->setLexicalDeclContext(LexicalDC);
4311 LexicalDC->addDeclInternal(ToUsingValue);
4312
4313 return ToUsingValue;
4314}
4315
Balazs Keri3b30d652018-10-19 13:32:20 +00004316ExpectedDecl ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004317 UnresolvedUsingTypenameDecl *D) {
4318 DeclContext *DC, *LexicalDC;
4319 DeclarationName Name;
4320 SourceLocation Loc;
4321 NamedDecl *ToD = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004322 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4323 return std::move(Err);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004324 if (ToD)
4325 return ToD;
4326
Balazs Keri3b30d652018-10-19 13:32:20 +00004327 SourceLocation ToUsingLoc, ToTypenameLoc, ToEllipsisLoc;
4328 NestedNameSpecifierLoc ToQualifierLoc;
4329 if (auto Imp = importSeq(
4330 D->getUsingLoc(), D->getTypenameLoc(), D->getQualifierLoc(),
4331 D->getEllipsisLoc()))
4332 std::tie(ToUsingLoc, ToTypenameLoc, ToQualifierLoc, ToEllipsisLoc) = *Imp;
4333 else
4334 return Imp.takeError();
4335
Gabor Marton26f72a92018-07-12 09:42:05 +00004336 UnresolvedUsingTypenameDecl *ToUsing;
4337 if (GetImportedOrCreateDecl(ToUsing, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004338 ToUsingLoc, ToTypenameLoc,
4339 ToQualifierLoc, Loc, Name, ToEllipsisLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004340 return ToUsing;
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004341
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004342 ToUsing->setAccess(D->getAccess());
4343 ToUsing->setLexicalDeclContext(LexicalDC);
4344 LexicalDC->addDeclInternal(ToUsing);
4345
4346 return ToUsing;
4347}
4348
Balazs Keri3b30d652018-10-19 13:32:20 +00004349
4350Error ASTNodeImporter::ImportDefinition(
4351 ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004352 if (To->getDefinition()) {
4353 // Check consistency of superclass.
4354 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
4355 if (FromSuper) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004356 if (auto FromSuperOrErr = import(FromSuper))
4357 FromSuper = *FromSuperOrErr;
4358 else
4359 return FromSuperOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004360 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004361
4362 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004363 if ((bool)FromSuper != (bool)ToSuper ||
4364 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004365 Importer.ToDiag(To->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004366 diag::warn_odr_objc_superclass_inconsistent)
Douglas Gregor2aa53772012-01-24 17:42:07 +00004367 << To->getDeclName();
4368 if (ToSuper)
4369 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
4370 << To->getSuperClass()->getDeclName();
4371 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004372 Importer.ToDiag(To->getLocation(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004373 diag::note_odr_objc_missing_superclass);
4374 if (From->getSuperClass())
Fangrui Song6907ce22018-07-30 19:24:48 +00004375 Importer.FromDiag(From->getSuperClassLoc(),
Douglas Gregor2aa53772012-01-24 17:42:07 +00004376 diag::note_odr_objc_superclass)
4377 << From->getSuperClass()->getDeclName();
4378 else
Fangrui Song6907ce22018-07-30 19:24:48 +00004379 Importer.FromDiag(From->getLocation(),
4380 diag::note_odr_objc_missing_superclass);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004381 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004382
Douglas Gregor2e15c842012-02-01 21:00:38 +00004383 if (shouldForceImportDeclContext(Kind))
Balazs Keri3b30d652018-10-19 13:32:20 +00004384 if (Error Err = ImportDeclContext(From))
4385 return Err;
4386 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004387 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004388
Douglas Gregor2aa53772012-01-24 17:42:07 +00004389 // Start the definition.
4390 To->startDefinition();
Fangrui Song6907ce22018-07-30 19:24:48 +00004391
Douglas Gregor2aa53772012-01-24 17:42:07 +00004392 // If this class has a superclass, import it.
4393 if (From->getSuperClass()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004394 if (auto SuperTInfoOrErr = import(From->getSuperClassTInfo()))
4395 To->setSuperClass(*SuperTInfoOrErr);
4396 else
4397 return SuperTInfoOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004398 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004399
Douglas Gregor2aa53772012-01-24 17:42:07 +00004400 // Import protocols
4401 SmallVector<ObjCProtocolDecl *, 4> Protocols;
4402 SmallVector<SourceLocation, 4> ProtocolLocs;
Balazs Keri3b30d652018-10-19 13:32:20 +00004403 ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc =
4404 From->protocol_loc_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00004405
Douglas Gregor2aa53772012-01-24 17:42:07 +00004406 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
4407 FromProtoEnd = From->protocol_end();
4408 FromProto != FromProtoEnd;
4409 ++FromProto, ++FromProtoLoc) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004410 if (Expected<ObjCProtocolDecl *> ToProtoOrErr = import(*FromProto))
4411 Protocols.push_back(*ToProtoOrErr);
4412 else
4413 return ToProtoOrErr.takeError();
4414
4415 if (ExpectedSLoc ToProtoLocOrErr = import(*FromProtoLoc))
4416 ProtocolLocs.push_back(*ToProtoLocOrErr);
4417 else
4418 return ToProtoLocOrErr.takeError();
4419
Douglas Gregor2aa53772012-01-24 17:42:07 +00004420 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004421
Douglas Gregor2aa53772012-01-24 17:42:07 +00004422 // FIXME: If we're merging, make sure that the protocol list is the same.
4423 To->setProtocolList(Protocols.data(), Protocols.size(),
4424 ProtocolLocs.data(), Importer.getToContext());
Fangrui Song6907ce22018-07-30 19:24:48 +00004425
Douglas Gregor2aa53772012-01-24 17:42:07 +00004426 // Import categories. When the categories themselves are imported, they'll
4427 // hook themselves into this interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004428 for (auto *Cat : From->known_categories()) {
4429 auto ToCatOrErr = import(Cat);
4430 if (!ToCatOrErr)
4431 return ToCatOrErr.takeError();
4432 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004433
Douglas Gregor2aa53772012-01-24 17:42:07 +00004434 // If we have an @implementation, import it as well.
4435 if (From->getImplementation()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004436 if (Expected<ObjCImplementationDecl *> ToImplOrErr =
4437 import(From->getImplementation()))
4438 To->setImplementation(*ToImplOrErr);
4439 else
4440 return ToImplOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004441 }
4442
Douglas Gregor2e15c842012-02-01 21:00:38 +00004443 if (shouldForceImportDeclContext(Kind)) {
4444 // Import all of the members of this class.
Balazs Keri3b30d652018-10-19 13:32:20 +00004445 if (Error Err = ImportDeclContext(From, /*ForceImport=*/true))
4446 return Err;
Douglas Gregor2e15c842012-02-01 21:00:38 +00004447 }
Balazs Keri3b30d652018-10-19 13:32:20 +00004448 return Error::success();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004449}
4450
Balazs Keri3b30d652018-10-19 13:32:20 +00004451Expected<ObjCTypeParamList *>
Douglas Gregor85f3f952015-07-07 03:57:15 +00004452ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
4453 if (!list)
4454 return nullptr;
4455
4456 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
Balazs Keri3b30d652018-10-19 13:32:20 +00004457 for (auto *fromTypeParam : *list) {
4458 if (auto toTypeParamOrErr = import(fromTypeParam))
4459 toTypeParams.push_back(*toTypeParamOrErr);
4460 else
4461 return toTypeParamOrErr.takeError();
Douglas Gregor85f3f952015-07-07 03:57:15 +00004462 }
4463
Balazs Keri3b30d652018-10-19 13:32:20 +00004464 auto LAngleLocOrErr = import(list->getLAngleLoc());
4465 if (!LAngleLocOrErr)
4466 return LAngleLocOrErr.takeError();
4467
4468 auto RAngleLocOrErr = import(list->getRAngleLoc());
4469 if (!RAngleLocOrErr)
4470 return RAngleLocOrErr.takeError();
4471
Douglas Gregor85f3f952015-07-07 03:57:15 +00004472 return ObjCTypeParamList::create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004473 *LAngleLocOrErr,
Douglas Gregor85f3f952015-07-07 03:57:15 +00004474 toTypeParams,
Balazs Keri3b30d652018-10-19 13:32:20 +00004475 *RAngleLocOrErr);
Douglas Gregor85f3f952015-07-07 03:57:15 +00004476}
4477
Balazs Keri3b30d652018-10-19 13:32:20 +00004478ExpectedDecl ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00004479 // If this class has a definition in the translation unit we're coming from,
4480 // but this particular declaration is not that definition, import the
4481 // definition and map to that.
4482 ObjCInterfaceDecl *Definition = D->getDefinition();
4483 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004484 if (ExpectedDecl ImportedDefOrErr = import(Definition))
4485 return Importer.MapImported(D, *ImportedDefOrErr);
4486 else
4487 return ImportedDefOrErr.takeError();
Douglas Gregor2aa53772012-01-24 17:42:07 +00004488 }
4489
Douglas Gregor45635322010-02-16 01:20:57 +00004490 // Import the major distinguishing characteristics of an @interface.
4491 DeclContext *DC, *LexicalDC;
4492 DeclarationName Name;
4493 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004494 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004495 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4496 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004497 if (ToD)
4498 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00004499
Douglas Gregor2aa53772012-01-24 17:42:07 +00004500 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00004501 ObjCInterfaceDecl *MergeWithIface = nullptr;
Gabor Marton54058b52018-12-17 13:53:12 +00004502 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004503 for (auto *FoundDecl : FoundDecls) {
4504 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00004505 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00004506
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004507 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00004508 break;
4509 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004510
Douglas Gregor2aa53772012-01-24 17:42:07 +00004511 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00004512 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004513 if (!ToIface) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004514 ExpectedSLoc AtBeginLocOrErr = import(D->getAtStartLoc());
4515 if (!AtBeginLocOrErr)
4516 return AtBeginLocOrErr.takeError();
4517
Gabor Marton26f72a92018-07-12 09:42:05 +00004518 if (GetImportedOrCreateDecl(
4519 ToIface, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004520 *AtBeginLocOrErr, Name.getAsIdentifierInfo(),
Gabor Marton26f72a92018-07-12 09:42:05 +00004521 /*TypeParamList=*/nullptr,
4522 /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()))
4523 return ToIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00004524 ToIface->setLexicalDeclContext(LexicalDC);
4525 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004526 }
Gabor Marton26f72a92018-07-12 09:42:05 +00004527 Importer.MapImported(D, ToIface);
Balazs Keri3b30d652018-10-19 13:32:20 +00004528 // Import the type parameter list after MapImported, to avoid
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004529 // loops when bringing in their DeclContext.
Balazs Keri3b30d652018-10-19 13:32:20 +00004530 if (auto ToPListOrErr =
4531 ImportObjCTypeParamList(D->getTypeParamListAsWritten()))
4532 ToIface->setTypeParamList(*ToPListOrErr);
4533 else
4534 return ToPListOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00004535
Balazs Keri3b30d652018-10-19 13:32:20 +00004536 if (D->isThisDeclarationADefinition())
4537 if (Error Err = ImportDefinition(D, ToIface))
4538 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004539
Douglas Gregor98d156a2010-02-17 16:12:00 +00004540 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004541}
4542
Balazs Keri3b30d652018-10-19 13:32:20 +00004543ExpectedDecl
4544ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4545 ObjCCategoryDecl *Category;
4546 if (Error Err = importInto(Category, D->getCategoryDecl()))
4547 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004548
Douglas Gregor4da9d682010-12-07 15:32:12 +00004549 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4550 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004551 DeclContext *DC, *LexicalDC;
4552 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4553 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004554
Balazs Keri3b30d652018-10-19 13:32:20 +00004555 SourceLocation ToLocation, ToAtStartLoc, ToCategoryNameLoc;
4556 if (auto Imp = importSeq(
4557 D->getLocation(), D->getAtStartLoc(), D->getCategoryNameLoc()))
4558 std::tie(ToLocation, ToAtStartLoc, ToCategoryNameLoc) = *Imp;
4559 else
4560 return Imp.takeError();
4561
Gabor Marton26f72a92018-07-12 09:42:05 +00004562 if (GetImportedOrCreateDecl(
4563 ToImpl, D, Importer.getToContext(), DC,
4564 Importer.Import(D->getIdentifier()), Category->getClassInterface(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004565 ToLocation, ToAtStartLoc, ToCategoryNameLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004566 return ToImpl;
4567
Balazs Keri3b30d652018-10-19 13:32:20 +00004568 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004569 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004570 Category->setImplementation(ToImpl);
4571 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004572
Gabor Marton26f72a92018-07-12 09:42:05 +00004573 Importer.MapImported(D, ToImpl);
Balazs Keri3b30d652018-10-19 13:32:20 +00004574 if (Error Err = ImportDeclContext(D))
4575 return std::move(Err);
4576
Douglas Gregor4da9d682010-12-07 15:32:12 +00004577 return ToImpl;
4578}
4579
Balazs Keri3b30d652018-10-19 13:32:20 +00004580ExpectedDecl
4581ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00004582 // Find the corresponding interface.
Balazs Keri3b30d652018-10-19 13:32:20 +00004583 ObjCInterfaceDecl *Iface;
4584 if (Error Err = importInto(Iface, D->getClassInterface()))
4585 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004586
4587 // Import the superclass, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00004588 ObjCInterfaceDecl *Super;
4589 if (Error Err = importInto(Super, D->getSuperClass()))
4590 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004591
4592 ObjCImplementationDecl *Impl = Iface->getImplementation();
4593 if (!Impl) {
4594 // We haven't imported an implementation yet. Create a new @implementation
4595 // now.
Balazs Keri3b30d652018-10-19 13:32:20 +00004596 DeclContext *DC, *LexicalDC;
4597 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4598 return std::move(Err);
4599
4600 SourceLocation ToLocation, ToAtStartLoc, ToSuperClassLoc;
4601 SourceLocation ToIvarLBraceLoc, ToIvarRBraceLoc;
4602 if (auto Imp = importSeq(
4603 D->getLocation(), D->getAtStartLoc(), D->getSuperClassLoc(),
4604 D->getIvarLBraceLoc(), D->getIvarRBraceLoc()))
4605 std::tie(
4606 ToLocation, ToAtStartLoc, ToSuperClassLoc,
4607 ToIvarLBraceLoc, ToIvarRBraceLoc) = *Imp;
4608 else
4609 return Imp.takeError();
4610
Gabor Marton26f72a92018-07-12 09:42:05 +00004611 if (GetImportedOrCreateDecl(Impl, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004612 DC, Iface, Super,
4613 ToLocation,
4614 ToAtStartLoc,
4615 ToSuperClassLoc,
4616 ToIvarLBraceLoc,
4617 ToIvarRBraceLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004618 return Impl;
4619
Balazs Keri3b30d652018-10-19 13:32:20 +00004620 Impl->setLexicalDeclContext(LexicalDC);
Gabor Marton26f72a92018-07-12 09:42:05 +00004621
Douglas Gregorda8025c2010-12-07 01:26:03 +00004622 // Associate the implementation with the class it implements.
4623 Iface->setImplementation(Impl);
Gabor Marton26f72a92018-07-12 09:42:05 +00004624 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004625 } else {
Gabor Marton26f72a92018-07-12 09:42:05 +00004626 Importer.MapImported(D, Iface->getImplementation());
Douglas Gregorda8025c2010-12-07 01:26:03 +00004627
4628 // Verify that the existing @implementation has the same superclass.
4629 if ((Super && !Impl->getSuperClass()) ||
4630 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004631 (Super && Impl->getSuperClass() &&
4632 !declaresSameEntity(Super->getCanonicalDecl(),
4633 Impl->getSuperClass()))) {
4634 Importer.ToDiag(Impl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004635 diag::warn_odr_objc_superclass_inconsistent)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004636 << Iface->getDeclName();
4637 // FIXME: It would be nice to have the location of the superclass
4638 // below.
4639 if (Impl->getSuperClass())
4640 Importer.ToDiag(Impl->getLocation(),
4641 diag::note_odr_objc_superclass)
4642 << Impl->getSuperClass()->getDeclName();
4643 else
4644 Importer.ToDiag(Impl->getLocation(),
4645 diag::note_odr_objc_missing_superclass);
4646 if (D->getSuperClass())
4647 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004648 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004649 << D->getSuperClass()->getDeclName();
4650 else
4651 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004652 diag::note_odr_objc_missing_superclass);
Balazs Keri3b30d652018-10-19 13:32:20 +00004653
4654 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004655 }
4656 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004657
Douglas Gregorda8025c2010-12-07 01:26:03 +00004658 // Import all of the members of this @implementation.
Balazs Keri3b30d652018-10-19 13:32:20 +00004659 if (Error Err = ImportDeclContext(D))
4660 return std::move(Err);
Douglas Gregorda8025c2010-12-07 01:26:03 +00004661
4662 return Impl;
4663}
4664
Balazs Keri3b30d652018-10-19 13:32:20 +00004665ExpectedDecl ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004666 // Import the major distinguishing characteristics of an @property.
4667 DeclContext *DC, *LexicalDC;
4668 DeclarationName Name;
4669 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004670 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004671 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4672 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004673 if (ToD)
4674 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004675
4676 // Check whether we have already imported this property.
Gabor Marton54058b52018-12-17 13:53:12 +00004677 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004678 for (auto *FoundDecl : FoundDecls) {
4679 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004680 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00004681 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00004682 FoundProp->getType())) {
Gabor Marton410f32c2019-04-01 15:29:55 +00004683 Importer.ToDiag(Loc, diag::warn_odr_objc_property_type_inconsistent)
Douglas Gregora11c4582010-02-17 18:02:10 +00004684 << Name << D->getType() << FoundProp->getType();
4685 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4686 << FoundProp->getType();
Balazs Keri3b30d652018-10-19 13:32:20 +00004687
4688 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora11c4582010-02-17 18:02:10 +00004689 }
4690
4691 // FIXME: Check property attributes, getters, setters, etc.?
4692
4693 // Consider these properties to be equivalent.
Gabor Marton26f72a92018-07-12 09:42:05 +00004694 Importer.MapImported(D, FoundProp);
Douglas Gregora11c4582010-02-17 18:02:10 +00004695 return FoundProp;
4696 }
4697 }
4698
Balazs Keri3b30d652018-10-19 13:32:20 +00004699 QualType ToType;
4700 TypeSourceInfo *ToTypeSourceInfo;
4701 SourceLocation ToAtLoc, ToLParenLoc;
4702 if (auto Imp = importSeq(
4703 D->getType(), D->getTypeSourceInfo(), D->getAtLoc(), D->getLParenLoc()))
4704 std::tie(ToType, ToTypeSourceInfo, ToAtLoc, ToLParenLoc) = *Imp;
4705 else
4706 return Imp.takeError();
Douglas Gregora11c4582010-02-17 18:02:10 +00004707
4708 // Create the new property.
Gabor Marton26f72a92018-07-12 09:42:05 +00004709 ObjCPropertyDecl *ToProperty;
4710 if (GetImportedOrCreateDecl(
4711 ToProperty, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00004712 Name.getAsIdentifierInfo(), ToAtLoc,
4713 ToLParenLoc, ToType,
4714 ToTypeSourceInfo, D->getPropertyImplementation()))
Gabor Marton26f72a92018-07-12 09:42:05 +00004715 return ToProperty;
4716
Balazs Keri3b30d652018-10-19 13:32:20 +00004717 Selector ToGetterName, ToSetterName;
4718 SourceLocation ToGetterNameLoc, ToSetterNameLoc;
4719 ObjCMethodDecl *ToGetterMethodDecl, *ToSetterMethodDecl;
4720 ObjCIvarDecl *ToPropertyIvarDecl;
4721 if (auto Imp = importSeq(
4722 D->getGetterName(), D->getSetterName(),
4723 D->getGetterNameLoc(), D->getSetterNameLoc(),
4724 D->getGetterMethodDecl(), D->getSetterMethodDecl(),
4725 D->getPropertyIvarDecl()))
4726 std::tie(
4727 ToGetterName, ToSetterName,
4728 ToGetterNameLoc, ToSetterNameLoc,
4729 ToGetterMethodDecl, ToSetterMethodDecl,
4730 ToPropertyIvarDecl) = *Imp;
4731 else
4732 return Imp.takeError();
4733
Douglas Gregora11c4582010-02-17 18:02:10 +00004734 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004735 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004736
4737 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004738 ToProperty->setPropertyAttributesAsWritten(
4739 D->getPropertyAttributesAsWritten());
Balazs Keri3b30d652018-10-19 13:32:20 +00004740 ToProperty->setGetterName(ToGetterName, ToGetterNameLoc);
4741 ToProperty->setSetterName(ToSetterName, ToSetterNameLoc);
4742 ToProperty->setGetterMethodDecl(ToGetterMethodDecl);
4743 ToProperty->setSetterMethodDecl(ToSetterMethodDecl);
4744 ToProperty->setPropertyIvarDecl(ToPropertyIvarDecl);
Douglas Gregora11c4582010-02-17 18:02:10 +00004745 return ToProperty;
4746}
4747
Balazs Keri3b30d652018-10-19 13:32:20 +00004748ExpectedDecl
4749ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4750 ObjCPropertyDecl *Property;
4751 if (Error Err = importInto(Property, D->getPropertyDecl()))
4752 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004753
Balazs Keri3b30d652018-10-19 13:32:20 +00004754 DeclContext *DC, *LexicalDC;
4755 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
4756 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00004757
Balazs Keri3b30d652018-10-19 13:32:20 +00004758 auto *InImpl = cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004759
4760 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004761 ObjCIvarDecl *Ivar = nullptr;
Balazs Keri3b30d652018-10-19 13:32:20 +00004762 if (Error Err = importInto(Ivar, D->getPropertyIvarDecl()))
4763 return std::move(Err);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004764
4765 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004766 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4767 Property->getQueryKind());
Gabor Marton26f72a92018-07-12 09:42:05 +00004768 if (!ToImpl) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004769 SourceLocation ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc;
4770 if (auto Imp = importSeq(
4771 D->getBeginLoc(), D->getLocation(), D->getPropertyIvarDeclLoc()))
4772 std::tie(ToBeginLoc, ToLocation, ToPropertyIvarDeclLoc) = *Imp;
4773 else
4774 return Imp.takeError();
4775
Gabor Marton26f72a92018-07-12 09:42:05 +00004776 if (GetImportedOrCreateDecl(ToImpl, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00004777 ToBeginLoc,
4778 ToLocation, Property,
Gabor Marton26f72a92018-07-12 09:42:05 +00004779 D->getPropertyImplementation(), Ivar,
Balazs Keri3b30d652018-10-19 13:32:20 +00004780 ToPropertyIvarDeclLoc))
Gabor Marton26f72a92018-07-12 09:42:05 +00004781 return ToImpl;
4782
Douglas Gregor14a49e22010-12-07 18:32:03 +00004783 ToImpl->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004784 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004785 } else {
4786 // Check that we have the same kind of property implementation (@synthesize
4787 // vs. @dynamic).
4788 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004789 Importer.ToDiag(ToImpl->getLocation(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004790 diag::warn_odr_objc_property_impl_kind_inconsistent)
Fangrui Song6907ce22018-07-30 19:24:48 +00004791 << Property->getDeclName()
4792 << (ToImpl->getPropertyImplementation()
Douglas Gregor14a49e22010-12-07 18:32:03 +00004793 == ObjCPropertyImplDecl::Dynamic);
4794 Importer.FromDiag(D->getLocation(),
4795 diag::note_odr_objc_property_impl_kind)
4796 << D->getPropertyDecl()->getDeclName()
4797 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Balazs Keri3b30d652018-10-19 13:32:20 +00004798
4799 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004800 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004801
4802 // For @synthesize, check that we have the same
Douglas Gregor14a49e22010-12-07 18:32:03 +00004803 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4804 Ivar != ToImpl->getPropertyIvarDecl()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00004805 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
Gabor Marton410f32c2019-04-01 15:29:55 +00004806 diag::warn_odr_objc_synthesize_ivar_inconsistent)
Douglas Gregor14a49e22010-12-07 18:32:03 +00004807 << Property->getDeclName()
4808 << ToImpl->getPropertyIvarDecl()->getDeclName()
4809 << Ivar->getDeclName();
Fangrui Song6907ce22018-07-30 19:24:48 +00004810 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
Douglas Gregor14a49e22010-12-07 18:32:03 +00004811 diag::note_odr_objc_synthesize_ivar_here)
4812 << D->getPropertyIvarDecl()->getDeclName();
Balazs Keri3b30d652018-10-19 13:32:20 +00004813
4814 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004815 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004816
Douglas Gregor14a49e22010-12-07 18:32:03 +00004817 // Merge the existing implementation with the new implementation.
Gabor Marton26f72a92018-07-12 09:42:05 +00004818 Importer.MapImported(D, ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004819 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004820
Douglas Gregor14a49e22010-12-07 18:32:03 +00004821 return ToImpl;
4822}
4823
Balazs Keri3b30d652018-10-19 13:32:20 +00004824ExpectedDecl
4825ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Douglas Gregora082a492010-11-30 19:14:50 +00004826 // For template arguments, we adopt the translation unit as our declaration
4827 // context. This context will be fixed when the actual template declaration
4828 // is created.
Fangrui Song6907ce22018-07-30 19:24:48 +00004829
Douglas Gregora082a492010-11-30 19:14:50 +00004830 // FIXME: Import default argument.
Balazs Keri3b30d652018-10-19 13:32:20 +00004831
4832 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
4833 if (!BeginLocOrErr)
4834 return BeginLocOrErr.takeError();
4835
4836 ExpectedSLoc LocationOrErr = import(D->getLocation());
4837 if (!LocationOrErr)
4838 return LocationOrErr.takeError();
4839
Gabor Marton26f72a92018-07-12 09:42:05 +00004840 TemplateTypeParmDecl *ToD = nullptr;
4841 (void)GetImportedOrCreateDecl(
4842 ToD, D, Importer.getToContext(),
4843 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004844 *BeginLocOrErr, *LocationOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00004845 D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()),
4846 D->wasDeclaredWithTypename(), D->isParameterPack());
4847 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004848}
4849
Balazs Keri3b30d652018-10-19 13:32:20 +00004850ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004851ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00004852 DeclarationName ToDeclName;
4853 SourceLocation ToLocation, ToInnerLocStart;
4854 QualType ToType;
4855 TypeSourceInfo *ToTypeSourceInfo;
4856 if (auto Imp = importSeq(
4857 D->getDeclName(), D->getLocation(), D->getType(), D->getTypeSourceInfo(),
4858 D->getInnerLocStart()))
4859 std::tie(
4860 ToDeclName, ToLocation, ToType, ToTypeSourceInfo,
4861 ToInnerLocStart) = *Imp;
4862 else
4863 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004864
Douglas Gregora082a492010-11-30 19:14:50 +00004865 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004866
4867 NonTypeTemplateParmDecl *ToD = nullptr;
4868 (void)GetImportedOrCreateDecl(
4869 ToD, D, Importer.getToContext(),
4870 Importer.getToContext().getTranslationUnitDecl(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004871 ToInnerLocStart, ToLocation, D->getDepth(),
4872 D->getPosition(), ToDeclName.getAsIdentifierInfo(), ToType,
4873 D->isParameterPack(), ToTypeSourceInfo);
Gabor Marton26f72a92018-07-12 09:42:05 +00004874 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004875}
4876
Balazs Keri3b30d652018-10-19 13:32:20 +00004877ExpectedDecl
Douglas Gregora082a492010-11-30 19:14:50 +00004878ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4879 // Import the name of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004880 auto NameOrErr = import(D->getDeclName());
4881 if (!NameOrErr)
4882 return NameOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004883
Douglas Gregora082a492010-11-30 19:14:50 +00004884 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00004885 ExpectedSLoc LocationOrErr = import(D->getLocation());
4886 if (!LocationOrErr)
4887 return LocationOrErr.takeError();
Gabor Marton26f72a92018-07-12 09:42:05 +00004888
Douglas Gregora082a492010-11-30 19:14:50 +00004889 // Import template parameters.
Balazs Keridec09162019-03-20 15:42:42 +00004890 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00004891 if (!TemplateParamsOrErr)
4892 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004893
Douglas Gregora082a492010-11-30 19:14:50 +00004894 // FIXME: Import default argument.
Gabor Marton26f72a92018-07-12 09:42:05 +00004895
4896 TemplateTemplateParmDecl *ToD = nullptr;
4897 (void)GetImportedOrCreateDecl(
4898 ToD, D, Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00004899 Importer.getToContext().getTranslationUnitDecl(), *LocationOrErr,
4900 D->getDepth(), D->getPosition(), D->isParameterPack(),
4901 (*NameOrErr).getAsIdentifierInfo(),
4902 *TemplateParamsOrErr);
Gabor Marton26f72a92018-07-12 09:42:05 +00004903 return ToD;
Douglas Gregora082a492010-11-30 19:14:50 +00004904}
4905
Gabor Marton16d98c22019-03-07 13:01:51 +00004906// Returns the definition for a (forward) declaration of a TemplateDecl, if
Gabor Marton9581c332018-05-23 13:53:36 +00004907// it has any definition in the redecl chain.
Gabor Marton16d98c22019-03-07 13:01:51 +00004908template <typename T> static auto getTemplateDefinition(T *D) -> T * {
4909 assert(D->getTemplatedDecl() && "Should be called on templates only");
4910 auto *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
Gabor Marton9581c332018-05-23 13:53:36 +00004911 if (!ToTemplatedDef)
4912 return nullptr;
Gabor Marton16d98c22019-03-07 13:01:51 +00004913 auto *TemplateWithDef = ToTemplatedDef->getDescribedTemplate();
4914 return cast_or_null<T>(TemplateWithDef);
Gabor Marton9581c332018-05-23 13:53:36 +00004915}
4916
Balazs Keri3b30d652018-10-19 13:32:20 +00004917ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Balazs Keri0c23dc52018-08-13 13:08:37 +00004918 bool IsFriend = D->getFriendObjectKind() != Decl::FOK_None;
4919
Douglas Gregora082a492010-11-30 19:14:50 +00004920 // Import the major distinguishing characteristics of this class template.
4921 DeclContext *DC, *LexicalDC;
4922 DeclarationName Name;
4923 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004924 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00004925 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4926 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00004927 if (ToD)
4928 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004929
Gabor Marton7df342a2018-12-17 12:42:12 +00004930 ClassTemplateDecl *FoundByLookup = nullptr;
4931
Douglas Gregora082a492010-11-30 19:14:50 +00004932 // We may already have a template of the same name; try to find and match it.
4933 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004934 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00004935 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004936 for (auto *FoundDecl : FoundDecls) {
Gabor Marton7df342a2018-12-17 12:42:12 +00004937 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary |
4938 Decl::IDNS_TagFriend))
Douglas Gregora082a492010-11-30 19:14:50 +00004939 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00004940
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004941 Decl *Found = FoundDecl;
Gabor Marton7df342a2018-12-17 12:42:12 +00004942 auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found);
4943 if (FoundTemplate) {
Gabor Marton9581c332018-05-23 13:53:36 +00004944
Douglas Gregora082a492010-11-30 19:14:50 +00004945 if (IsStructuralMatch(D, FoundTemplate)) {
Gabor Marton16d98c22019-03-07 13:01:51 +00004946 ClassTemplateDecl *TemplateWithDef =
4947 getTemplateDefinition(FoundTemplate);
Gabor Marton7df342a2018-12-17 12:42:12 +00004948 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
4949 return Importer.MapImported(D, TemplateWithDef);
Balazs Keri0c23dc52018-08-13 13:08:37 +00004950 }
Gabor Marton7df342a2018-12-17 12:42:12 +00004951 FoundByLookup = FoundTemplate;
4952 break;
Gabor Marton9581c332018-05-23 13:53:36 +00004953 }
Douglas Gregora082a492010-11-30 19:14:50 +00004954 }
Gabor Marton9581c332018-05-23 13:53:36 +00004955
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004956 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00004957 }
Gabor Marton9581c332018-05-23 13:53:36 +00004958
Douglas Gregora082a492010-11-30 19:14:50 +00004959 if (!ConflictingDecls.empty()) {
4960 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00004961 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00004962 ConflictingDecls.size());
4963 }
Gabor Marton9581c332018-05-23 13:53:36 +00004964
Douglas Gregora082a492010-11-30 19:14:50 +00004965 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00004966 return make_error<ImportError>(ImportError::NameConflict);
Douglas Gregora082a492010-11-30 19:14:50 +00004967 }
4968
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004969 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4970
Douglas Gregora082a492010-11-30 19:14:50 +00004971 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00004972 CXXRecordDecl *ToTemplated;
4973 if (Error Err = importInto(ToTemplated, FromTemplated))
4974 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004975
Douglas Gregora082a492010-11-30 19:14:50 +00004976 // Create the class template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00004977 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00004978 if (!TemplateParamsOrErr)
4979 return TemplateParamsOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00004980
Gabor Marton26f72a92018-07-12 09:42:05 +00004981 ClassTemplateDecl *D2;
4982 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00004983 *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00004984 return D2;
4985
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004986 ToTemplated->setDescribedClassTemplate(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00004987
Douglas Gregora082a492010-11-30 19:14:50 +00004988 D2->setAccess(D->getAccess());
4989 D2->setLexicalDeclContext(LexicalDC);
Gabor Marton7df342a2018-12-17 12:42:12 +00004990
4991 if (D->getDeclContext()->containsDeclAndLoad(D))
4992 DC->addDeclInternal(D2);
4993 if (DC != LexicalDC && D->getLexicalDeclContext()->containsDeclAndLoad(D))
Balazs Keri0c23dc52018-08-13 13:08:37 +00004994 LexicalDC->addDeclInternal(D2);
Fangrui Song6907ce22018-07-30 19:24:48 +00004995
Gabor Marton7df342a2018-12-17 12:42:12 +00004996 if (FoundByLookup) {
4997 auto *Recent =
4998 const_cast<ClassTemplateDecl *>(FoundByLookup->getMostRecentDecl());
4999
5000 // It is possible that during the import of the class template definition
5001 // we start the import of a fwd friend decl of the very same class template
5002 // and we add the fwd friend decl to the lookup table. But the ToTemplated
5003 // had been created earlier and by that time the lookup could not find
5004 // anything existing, so it has no previous decl. Later, (still during the
5005 // import of the fwd friend decl) we start to import the definition again
5006 // and this time the lookup finds the previous fwd friend class template.
5007 // In this case we must set up the previous decl for the templated decl.
5008 if (!ToTemplated->getPreviousDecl()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005009 assert(FoundByLookup->getTemplatedDecl() &&
5010 "Found decl must have its templated decl set");
Gabor Marton7df342a2018-12-17 12:42:12 +00005011 CXXRecordDecl *PrevTemplated =
5012 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5013 if (ToTemplated != PrevTemplated)
5014 ToTemplated->setPreviousDecl(PrevTemplated);
5015 }
5016
5017 D2->setPreviousDecl(Recent);
5018 }
5019
5020 if (LexicalDC != DC && IsFriend)
5021 DC->makeDeclVisibleInContext(D2);
5022
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005023 if (FromTemplated->isCompleteDefinition() &&
5024 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00005025 // FIXME: Import definition!
5026 }
Fangrui Song6907ce22018-07-30 19:24:48 +00005027
Douglas Gregora082a492010-11-30 19:14:50 +00005028 return D2;
5029}
5030
Balazs Keri3b30d652018-10-19 13:32:20 +00005031ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
Douglas Gregore2e50d332010-12-01 01:36:18 +00005032 ClassTemplateSpecializationDecl *D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005033 ClassTemplateDecl *ClassTemplate;
5034 if (Error Err = importInto(ClassTemplate, D->getSpecializedTemplate()))
5035 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005036
Douglas Gregore2e50d332010-12-01 01:36:18 +00005037 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005038 DeclContext *DC, *LexicalDC;
5039 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5040 return std::move(Err);
Douglas Gregore2e50d332010-12-01 01:36:18 +00005041
5042 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005043 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005044 if (Error Err = ImportTemplateArguments(
5045 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5046 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005047
Douglas Gregore2e50d332010-12-01 01:36:18 +00005048 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005049 void *InsertPos = nullptr;
Gabor Marton7f8c4002019-03-19 13:34:10 +00005050 ClassTemplateSpecializationDecl *PrevDecl = nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005051 ClassTemplatePartialSpecializationDecl *PartialSpec =
5052 dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
5053 if (PartialSpec)
Gabor Marton7f8c4002019-03-19 13:34:10 +00005054 PrevDecl =
5055 ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005056 else
Gabor Marton7f8c4002019-03-19 13:34:10 +00005057 PrevDecl = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005058
Gabor Marton7f8c4002019-03-19 13:34:10 +00005059 if (PrevDecl) {
5060 if (IsStructuralMatch(D, PrevDecl)) {
5061 if (D->isThisDeclarationADefinition() && PrevDecl->getDefinition()) {
5062 Importer.MapImported(D, PrevDecl->getDefinition());
5063 // Import those default field initializers which have been
5064 // instantiated in the "From" context, but not in the "To" context.
5065 for (auto *FromField : D->fields())
5066 Importer.Import(FromField);
Gabor Marton42e15de2018-08-22 11:52:14 +00005067
Gabor Marton7f8c4002019-03-19 13:34:10 +00005068 // Import those methods which have been instantiated in the
5069 // "From" context, but not in the "To" context.
5070 for (CXXMethodDecl *FromM : D->methods())
5071 Importer.Import(FromM);
Gabor Marton42e15de2018-08-22 11:52:14 +00005072
Gabor Marton7f8c4002019-03-19 13:34:10 +00005073 // TODO Import instantiated default arguments.
5074 // TODO Import instantiated exception specifications.
5075 //
5076 // Generally, ASTCommon.h/DeclUpdateKind enum gives a very good hint
5077 // what else could be fused during an AST merge.
5078 return PrevDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005079 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005080 } else { // ODR violation.
5081 // FIXME HandleNameConflict
5082 return nullptr;
Gabor Marton42e15de2018-08-22 11:52:14 +00005083 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005084 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005085
Gabor Marton7f8c4002019-03-19 13:34:10 +00005086 // Import the location of this declaration.
5087 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5088 if (!BeginLocOrErr)
5089 return BeginLocOrErr.takeError();
5090 ExpectedSLoc IdLocOrErr = import(D->getLocation());
5091 if (!IdLocOrErr)
5092 return IdLocOrErr.takeError();
Balazs Keri3b30d652018-10-19 13:32:20 +00005093
Gabor Marton7f8c4002019-03-19 13:34:10 +00005094 // Create the specialization.
5095 ClassTemplateSpecializationDecl *D2 = nullptr;
5096 if (PartialSpec) {
5097 // Import TemplateArgumentListInfo.
5098 TemplateArgumentListInfo ToTAInfo;
5099 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
5100 if (Error Err = ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
5101 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005102
Gabor Marton7f8c4002019-03-19 13:34:10 +00005103 QualType CanonInjType;
5104 if (Error Err = importInto(
5105 CanonInjType, PartialSpec->getInjectedSpecializationType()))
5106 return std::move(Err);
5107 CanonInjType = CanonInjType.getCanonicalType();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005108
Balazs Keridec09162019-03-20 15:42:42 +00005109 auto ToTPListOrErr = import(PartialSpec->getTemplateParameters());
Gabor Marton7f8c4002019-03-19 13:34:10 +00005110 if (!ToTPListOrErr)
5111 return ToTPListOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005112
Gabor Marton7f8c4002019-03-19 13:34:10 +00005113 if (GetImportedOrCreateDecl<ClassTemplatePartialSpecializationDecl>(
5114 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5115 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr, ClassTemplate,
5116 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
5117 ToTAInfo, CanonInjType,
5118 cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl)))
5119 return D2;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005120
Gabor Marton7f8c4002019-03-19 13:34:10 +00005121 // Update InsertPos, because preceding import calls may have invalidated
5122 // it by adding new specializations.
5123 if (!ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos))
5124 // Add this partial specialization to the class template.
5125 ClassTemplate->AddPartialSpecialization(
5126 cast<ClassTemplatePartialSpecializationDecl>(D2), InsertPos);
Gabor Marton42e15de2018-08-22 11:52:14 +00005127
Gabor Marton7f8c4002019-03-19 13:34:10 +00005128 } else { // Not a partial specialization.
5129 if (GetImportedOrCreateDecl(
5130 D2, D, Importer.getToContext(), D->getTagKind(), DC,
5131 *BeginLocOrErr, *IdLocOrErr, ClassTemplate, TemplateArgs,
5132 PrevDecl))
5133 return D2;
Gabor Marton42e15de2018-08-22 11:52:14 +00005134
Gabor Marton7f8c4002019-03-19 13:34:10 +00005135 // Update InsertPos, because preceding import calls may have invalidated
5136 // it by adding new specializations.
5137 if (!ClassTemplate->findSpecialization(TemplateArgs, InsertPos))
5138 // Add this specialization to the class template.
5139 ClassTemplate->AddSpecialization(D2, InsertPos);
5140 }
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005141
Gabor Marton7f8c4002019-03-19 13:34:10 +00005142 D2->setSpecializationKind(D->getSpecializationKind());
Douglas Gregore2e50d332010-12-01 01:36:18 +00005143
Gabor Marton7f8c4002019-03-19 13:34:10 +00005144 // Set the context of this specialization/instantiation.
5145 D2->setLexicalDeclContext(LexicalDC);
5146
5147 // Add to the DC only if it was an explicit specialization/instantiation.
5148 if (D2->isExplicitInstantiationOrSpecialization()) {
5149 LexicalDC->addDeclInternal(D2);
5150 }
5151
5152 // Import the qualifier, if any.
5153 if (auto LocOrErr = import(D->getQualifierLoc()))
5154 D2->setQualifierInfo(*LocOrErr);
5155 else
5156 return LocOrErr.takeError();
5157
5158 if (auto *TSI = D->getTypeAsWritten()) {
5159 if (auto TInfoOrErr = import(TSI))
5160 D2->setTypeAsWritten(*TInfoOrErr);
5161 else
5162 return TInfoOrErr.takeError();
5163
5164 if (auto LocOrErr = import(D->getTemplateKeywordLoc()))
5165 D2->setTemplateKeywordLoc(*LocOrErr);
Balazs Keri3b30d652018-10-19 13:32:20 +00005166 else
5167 return LocOrErr.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005168
Gabor Marton7f8c4002019-03-19 13:34:10 +00005169 if (auto LocOrErr = import(D->getExternLoc()))
5170 D2->setExternLoc(*LocOrErr);
5171 else
5172 return LocOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00005173 }
Gabor Marton7f8c4002019-03-19 13:34:10 +00005174
5175 if (D->getPointOfInstantiation().isValid()) {
5176 if (auto POIOrErr = import(D->getPointOfInstantiation()))
5177 D2->setPointOfInstantiation(*POIOrErr);
5178 else
5179 return POIOrErr.takeError();
5180 }
5181
5182 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
5183
Balazs Keri3b30d652018-10-19 13:32:20 +00005184 if (D->isCompleteDefinition())
5185 if (Error Err = ImportDefinition(D, D2))
5186 return std::move(Err);
Craig Topper36250ad2014-05-12 05:36:57 +00005187
Douglas Gregore2e50d332010-12-01 01:36:18 +00005188 return D2;
5189}
5190
Balazs Keri3b30d652018-10-19 13:32:20 +00005191ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005192 // If this variable has a definition in the translation unit we're coming
5193 // from,
5194 // but this particular declaration is not that definition, import the
5195 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005196 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00005197 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
5198 if (Definition && Definition != D->getTemplatedDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005199 if (ExpectedDecl ImportedDefOrErr = import(
5200 Definition->getDescribedVarTemplate()))
5201 return Importer.MapImported(D, *ImportedDefOrErr);
5202 else
5203 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005204 }
5205
5206 // Import the major distinguishing characteristics of this variable template.
5207 DeclContext *DC, *LexicalDC;
5208 DeclarationName Name;
5209 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00005210 NamedDecl *ToD;
Balazs Keri3b30d652018-10-19 13:32:20 +00005211 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5212 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00005213 if (ToD)
5214 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005215
5216 // We may already have a template of the same name; try to find and match it.
5217 assert(!DC->isFunctionOrMethod() &&
5218 "Variable templates cannot be declared at function scope");
5219 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Marton54058b52018-12-17 13:53:12 +00005220 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005221 for (auto *FoundDecl : FoundDecls) {
5222 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00005223 continue;
5224
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005225 Decl *Found = FoundDecl;
Balazs Keri3b30d652018-10-19 13:32:20 +00005226 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005227 if (IsStructuralMatch(D, FoundTemplate)) {
5228 // The variable templates structurally match; call it the same template.
Gabor Marton26f72a92018-07-12 09:42:05 +00005229 Importer.MapImported(D->getTemplatedDecl(),
5230 FoundTemplate->getTemplatedDecl());
5231 return Importer.MapImported(D, FoundTemplate);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005232 }
5233 }
5234
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005235 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005236 }
5237
5238 if (!ConflictingDecls.empty()) {
5239 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
5240 ConflictingDecls.data(),
5241 ConflictingDecls.size());
5242 }
5243
5244 if (!Name)
Balazs Keri3b30d652018-10-19 13:32:20 +00005245 // FIXME: Is it possible to get other error than name conflict?
5246 // (Put this `if` into the previous `if`?)
5247 return make_error<ImportError>(ImportError::NameConflict);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005248
5249 VarDecl *DTemplated = D->getTemplatedDecl();
5250
5251 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005252 // FIXME: Value not used?
5253 ExpectedType TypeOrErr = import(DTemplated->getType());
5254 if (!TypeOrErr)
5255 return TypeOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005256
5257 // Create the declaration that is being templated.
Balazs Keri3b30d652018-10-19 13:32:20 +00005258 VarDecl *ToTemplated;
5259 if (Error Err = importInto(ToTemplated, DTemplated))
5260 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005261
5262 // Create the variable template declaration itself.
Balazs Keridec09162019-03-20 15:42:42 +00005263 auto TemplateParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005264 if (!TemplateParamsOrErr)
5265 return TemplateParamsOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005266
Gabor Marton26f72a92018-07-12 09:42:05 +00005267 VarTemplateDecl *ToVarTD;
5268 if (GetImportedOrCreateDecl(ToVarTD, D, Importer.getToContext(), DC, Loc,
Balazs Keri3b30d652018-10-19 13:32:20 +00005269 Name, *TemplateParamsOrErr, ToTemplated))
Gabor Marton26f72a92018-07-12 09:42:05 +00005270 return ToVarTD;
5271
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005272 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005273
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005274 ToVarTD->setAccess(D->getAccess());
5275 ToVarTD->setLexicalDeclContext(LexicalDC);
5276 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005277
Larisse Voufo39a1e502013-08-06 01:03:05 +00005278 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005279 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005280 // FIXME: Import definition!
5281 }
5282
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005283 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005284}
5285
Balazs Keri3b30d652018-10-19 13:32:20 +00005286ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00005287 VarTemplateSpecializationDecl *D) {
5288 // If this record has a definition in the translation unit we're coming from,
5289 // but this particular declaration is not that definition, import the
5290 // definition and map to that.
5291 VarDecl *Definition = D->getDefinition();
5292 if (Definition && Definition != D) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005293 if (ExpectedDecl ImportedDefOrErr = import(Definition))
5294 return Importer.MapImported(D, *ImportedDefOrErr);
5295 else
5296 return ImportedDefOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005297 }
5298
Balazs Keri3b30d652018-10-19 13:32:20 +00005299 VarTemplateDecl *VarTemplate;
5300 if (Error Err = importInto(VarTemplate, D->getSpecializedTemplate()))
5301 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005302
5303 // Import the context of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005304 DeclContext *DC, *LexicalDC;
5305 if (Error Err = ImportDeclContext(D, DC, LexicalDC))
5306 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005307
5308 // Import the location of this declaration.
Balazs Keri3b30d652018-10-19 13:32:20 +00005309 ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
5310 if (!BeginLocOrErr)
5311 return BeginLocOrErr.takeError();
5312
5313 auto IdLocOrErr = import(D->getLocation());
5314 if (!IdLocOrErr)
5315 return IdLocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005316
5317 // Import template arguments.
5318 SmallVector<TemplateArgument, 2> TemplateArgs;
Balazs Keri3b30d652018-10-19 13:32:20 +00005319 if (Error Err = ImportTemplateArguments(
5320 D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
5321 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005322
5323 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00005324 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005325 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00005326 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005327 if (D2) {
5328 // We already have a variable template specialization with these template
5329 // arguments.
5330
5331 // FIXME: Check for specialization vs. instantiation errors.
5332
5333 if (VarDecl *FoundDef = D2->getDefinition()) {
5334 if (!D->isThisDeclarationADefinition() ||
5335 IsStructuralMatch(D, FoundDef)) {
5336 // The record types structurally match, or the "from" translation
5337 // unit only had a forward declaration anyway; call it the same
5338 // variable.
Gabor Marton26f72a92018-07-12 09:42:05 +00005339 return Importer.MapImported(D, FoundDef);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005340 }
5341 }
5342 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00005343 // Import the type.
Balazs Keri3b30d652018-10-19 13:32:20 +00005344 QualType T;
5345 if (Error Err = importInto(T, D->getType()))
5346 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005347
Balazs Keri3b30d652018-10-19 13:32:20 +00005348 auto TInfoOrErr = import(D->getTypeSourceInfo());
5349 if (!TInfoOrErr)
5350 return TInfoOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005351
5352 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00005353 if (Error Err = ImportTemplateArgumentListInfo(
5354 D->getTemplateArgsInfo(), ToTAInfo))
5355 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005356
5357 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00005358 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005359 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
5360 // Import TemplateArgumentListInfo
5361 TemplateArgumentListInfo ArgInfos;
5362 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
5363 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
Balazs Keri3b30d652018-10-19 13:32:20 +00005364 if (Error Err = ImportTemplateArgumentListInfo(
5365 *FromTAArgsAsWritten, ArgInfos))
5366 return std::move(Err);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005367
Balazs Keridec09162019-03-20 15:42:42 +00005368 auto ToTPListOrErr = import(FromPartial->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005369 if (!ToTPListOrErr)
5370 return ToTPListOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005371
Gabor Marton26f72a92018-07-12 09:42:05 +00005372 PartVarSpecDecl *ToPartial;
5373 if (GetImportedOrCreateDecl(ToPartial, D, Importer.getToContext(), DC,
Balazs Keri3b30d652018-10-19 13:32:20 +00005374 *BeginLocOrErr, *IdLocOrErr, *ToTPListOrErr,
5375 VarTemplate, T, *TInfoOrErr,
5376 D->getStorageClass(), TemplateArgs, ArgInfos))
Gabor Marton26f72a92018-07-12 09:42:05 +00005377 return ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005378
Balazs Keri3b30d652018-10-19 13:32:20 +00005379 if (Expected<PartVarSpecDecl *> ToInstOrErr = import(
5380 FromPartial->getInstantiatedFromMember()))
5381 ToPartial->setInstantiatedFromMember(*ToInstOrErr);
5382 else
5383 return ToInstOrErr.takeError();
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005384
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005385 if (FromPartial->isMemberSpecialization())
5386 ToPartial->setMemberSpecialization();
5387
5388 D2 = ToPartial;
Balazs Keri3b30d652018-10-19 13:32:20 +00005389
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005390 } else { // Full specialization
Balazs Keri3b30d652018-10-19 13:32:20 +00005391 if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC,
5392 *BeginLocOrErr, *IdLocOrErr, VarTemplate,
5393 T, *TInfoOrErr,
Gabor Marton26f72a92018-07-12 09:42:05 +00005394 D->getStorageClass(), TemplateArgs))
5395 return D2;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005396 }
5397
Balazs Keri3b30d652018-10-19 13:32:20 +00005398 if (D->getPointOfInstantiation().isValid()) {
5399 if (ExpectedSLoc POIOrErr = import(D->getPointOfInstantiation()))
5400 D2->setPointOfInstantiation(*POIOrErr);
5401 else
5402 return POIOrErr.takeError();
5403 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005404
Larisse Voufo39a1e502013-08-06 01:03:05 +00005405 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005406 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005407
5408 // Add this specialization to the class template.
5409 VarTemplate->AddSpecialization(D2, InsertPos);
5410
5411 // Import the qualifier, if any.
Balazs Keri3b30d652018-10-19 13:32:20 +00005412 if (auto LocOrErr = import(D->getQualifierLoc()))
5413 D2->setQualifierInfo(*LocOrErr);
5414 else
5415 return LocOrErr.takeError();
Larisse Voufo39a1e502013-08-06 01:03:05 +00005416
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005417 if (D->isConstexpr())
5418 D2->setConstexpr(true);
5419
Larisse Voufo39a1e502013-08-06 01:03:05 +00005420 // Add the specialization to this context.
5421 D2->setLexicalDeclContext(LexicalDC);
5422 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005423
5424 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00005425 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00005426
Balazs Keri3b30d652018-10-19 13:32:20 +00005427 if (Error Err = ImportInitializer(D, D2))
5428 return std::move(Err);
Larisse Voufo39a1e502013-08-06 01:03:05 +00005429
5430 return D2;
5431}
5432
Balazs Keri3b30d652018-10-19 13:32:20 +00005433ExpectedDecl
5434ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005435 DeclContext *DC, *LexicalDC;
5436 DeclarationName Name;
5437 SourceLocation Loc;
5438 NamedDecl *ToD;
5439
Balazs Keri3b30d652018-10-19 13:32:20 +00005440 if (Error Err = ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
5441 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005442
5443 if (ToD)
5444 return ToD;
5445
Gabor Marton16d98c22019-03-07 13:01:51 +00005446 const FunctionTemplateDecl *FoundByLookup = nullptr;
5447
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005448 // Try to find a function in our own ("to") context with the same name, same
5449 // type, and in the same context as the function we're importing.
Gabor Marton16d98c22019-03-07 13:01:51 +00005450 // FIXME Split this into a separate function.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005451 if (!LexicalDC->isFunctionOrMethod()) {
Gabor Martone331e632019-02-18 13:09:27 +00005452 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_OrdinaryFriend;
Gabor Marton54058b52018-12-17 13:53:12 +00005453 auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005454 for (auto *FoundDecl : FoundDecls) {
5455 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005456 continue;
5457
Gabor Marton16d98c22019-03-07 13:01:51 +00005458 if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
5459 if (FoundTemplate->hasExternalFormalLinkage() &&
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005460 D->hasExternalFormalLinkage()) {
Gabor Marton16d98c22019-03-07 13:01:51 +00005461 if (IsStructuralMatch(D, FoundTemplate)) {
5462 FunctionTemplateDecl *TemplateWithDef =
5463 getTemplateDefinition(FoundTemplate);
5464 if (D->isThisDeclarationADefinition() && TemplateWithDef) {
5465 return Importer.MapImported(D, TemplateWithDef);
5466 }
5467 FoundByLookup = FoundTemplate;
5468 break;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005469 }
Gabor Marton16d98c22019-03-07 13:01:51 +00005470 // TODO: handle conflicting names
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005471 }
5472 }
5473 }
5474 }
5475
Balazs Keridec09162019-03-20 15:42:42 +00005476 auto ParamsOrErr = import(D->getTemplateParameters());
Balazs Keri3b30d652018-10-19 13:32:20 +00005477 if (!ParamsOrErr)
5478 return ParamsOrErr.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005479
Balazs Keri3b30d652018-10-19 13:32:20 +00005480 FunctionDecl *TemplatedFD;
5481 if (Error Err = importInto(TemplatedFD, D->getTemplatedDecl()))
5482 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005483
Gabor Marton26f72a92018-07-12 09:42:05 +00005484 FunctionTemplateDecl *ToFunc;
5485 if (GetImportedOrCreateDecl(ToFunc, D, Importer.getToContext(), DC, Loc, Name,
Balazs Keri3b30d652018-10-19 13:32:20 +00005486 *ParamsOrErr, TemplatedFD))
Gabor Marton26f72a92018-07-12 09:42:05 +00005487 return ToFunc;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005488
5489 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005490
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005491 ToFunc->setAccess(D->getAccess());
5492 ToFunc->setLexicalDeclContext(LexicalDC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005493 LexicalDC->addDeclInternal(ToFunc);
Gabor Marton16d98c22019-03-07 13:01:51 +00005494
5495 if (FoundByLookup) {
5496 auto *Recent =
5497 const_cast<FunctionTemplateDecl *>(FoundByLookup->getMostRecentDecl());
5498 if (!TemplatedFD->getPreviousDecl()) {
5499 assert(FoundByLookup->getTemplatedDecl() &&
5500 "Found decl must have its templated decl set");
5501 auto *PrevTemplated =
5502 FoundByLookup->getTemplatedDecl()->getMostRecentDecl();
5503 if (TemplatedFD != PrevTemplated)
5504 TemplatedFD->setPreviousDecl(PrevTemplated);
5505 }
5506 ToFunc->setPreviousDecl(Recent);
5507 }
5508
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00005509 return ToFunc;
5510}
5511
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005512//----------------------------------------------------------------------------
5513// Import Statements
5514//----------------------------------------------------------------------------
5515
Balazs Keri3b30d652018-10-19 13:32:20 +00005516ExpectedStmt ASTNodeImporter::VisitStmt(Stmt *S) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005517 Importer.FromDiag(S->getBeginLoc(), diag::err_unsupported_ast_node)
5518 << S->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00005519 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005520}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005521
Balazs Keri3b30d652018-10-19 13:32:20 +00005522
5523ExpectedStmt ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005524 SmallVector<IdentifierInfo *, 4> Names;
5525 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
5526 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005527 // ToII is nullptr when no symbolic name is given for output operand
5528 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005529 Names.push_back(ToII);
5530 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005531
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005532 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
5533 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00005534 // ToII is nullptr when no symbolic name is given for input operand
5535 // see ParseStmtAsm::ParseAsmOperandsOpt
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005536 Names.push_back(ToII);
5537 }
5538
5539 SmallVector<StringLiteral *, 4> Clobbers;
5540 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005541 if (auto ClobberOrErr = import(S->getClobberStringLiteral(I)))
5542 Clobbers.push_back(*ClobberOrErr);
5543 else
5544 return ClobberOrErr.takeError();
5545
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005546 }
5547
5548 SmallVector<StringLiteral *, 4> Constraints;
5549 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005550 if (auto OutputOrErr = import(S->getOutputConstraintLiteral(I)))
5551 Constraints.push_back(*OutputOrErr);
5552 else
5553 return OutputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005554 }
5555
5556 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005557 if (auto InputOrErr = import(S->getInputConstraintLiteral(I)))
5558 Constraints.push_back(*InputOrErr);
5559 else
5560 return InputOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005561 }
5562
5563 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Balazs Keri3b30d652018-10-19 13:32:20 +00005564 if (Error Err = ImportContainerChecked(S->outputs(), Exprs))
5565 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005566
Balazs Keri3b30d652018-10-19 13:32:20 +00005567 if (Error Err = ImportArrayChecked(
5568 S->inputs(), Exprs.begin() + S->getNumOutputs()))
5569 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005570
Balazs Keri3b30d652018-10-19 13:32:20 +00005571 ExpectedSLoc AsmLocOrErr = import(S->getAsmLoc());
5572 if (!AsmLocOrErr)
5573 return AsmLocOrErr.takeError();
5574 auto AsmStrOrErr = import(S->getAsmString());
5575 if (!AsmStrOrErr)
5576 return AsmStrOrErr.takeError();
5577 ExpectedSLoc RParenLocOrErr = import(S->getRParenLoc());
5578 if (!RParenLocOrErr)
5579 return RParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005580
5581 return new (Importer.getToContext()) GCCAsmStmt(
Balazs Keri3b30d652018-10-19 13:32:20 +00005582 Importer.getToContext(),
5583 *AsmLocOrErr,
5584 S->isSimple(),
5585 S->isVolatile(),
5586 S->getNumOutputs(),
5587 S->getNumInputs(),
5588 Names.data(),
5589 Constraints.data(),
5590 Exprs.data(),
5591 *AsmStrOrErr,
5592 S->getNumClobbers(),
5593 Clobbers.data(),
5594 *RParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005595}
5596
Balazs Keri3b30d652018-10-19 13:32:20 +00005597ExpectedStmt ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
5598 auto Imp = importSeq(S->getDeclGroup(), S->getBeginLoc(), S->getEndLoc());
5599 if (!Imp)
5600 return Imp.takeError();
5601
5602 DeclGroupRef ToDG;
5603 SourceLocation ToBeginLoc, ToEndLoc;
5604 std::tie(ToDG, ToBeginLoc, ToEndLoc) = *Imp;
5605
5606 return new (Importer.getToContext()) DeclStmt(ToDG, ToBeginLoc, ToEndLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005607}
5608
Balazs Keri3b30d652018-10-19 13:32:20 +00005609ExpectedStmt ASTNodeImporter::VisitNullStmt(NullStmt *S) {
5610 ExpectedSLoc ToSemiLocOrErr = import(S->getSemiLoc());
5611 if (!ToSemiLocOrErr)
5612 return ToSemiLocOrErr.takeError();
5613 return new (Importer.getToContext()) NullStmt(
5614 *ToSemiLocOrErr, S->hasLeadingEmptyMacro());
Sean Callanan59721b32015-04-28 18:41:46 +00005615}
5616
Balazs Keri3b30d652018-10-19 13:32:20 +00005617ExpectedStmt ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005618 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005619
Balazs Keri3b30d652018-10-19 13:32:20 +00005620 if (Error Err = ImportContainerChecked(S->body(), ToStmts))
5621 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00005622
Balazs Keri3b30d652018-10-19 13:32:20 +00005623 ExpectedSLoc ToLBracLocOrErr = import(S->getLBracLoc());
5624 if (!ToLBracLocOrErr)
5625 return ToLBracLocOrErr.takeError();
5626
5627 ExpectedSLoc ToRBracLocOrErr = import(S->getRBracLoc());
5628 if (!ToRBracLocOrErr)
5629 return ToRBracLocOrErr.takeError();
5630
5631 return CompoundStmt::Create(
5632 Importer.getToContext(), ToStmts,
5633 *ToLBracLocOrErr, *ToRBracLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005634}
5635
Balazs Keri3b30d652018-10-19 13:32:20 +00005636ExpectedStmt ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
5637 auto Imp = importSeq(
5638 S->getLHS(), S->getRHS(), S->getSubStmt(), S->getCaseLoc(),
5639 S->getEllipsisLoc(), S->getColonLoc());
5640 if (!Imp)
5641 return Imp.takeError();
5642
5643 Expr *ToLHS, *ToRHS;
5644 Stmt *ToSubStmt;
5645 SourceLocation ToCaseLoc, ToEllipsisLoc, ToColonLoc;
5646 std::tie(ToLHS, ToRHS, ToSubStmt, ToCaseLoc, ToEllipsisLoc, ToColonLoc) =
5647 *Imp;
5648
Bruno Ricci5b30571752018-10-28 12:30:53 +00005649 auto *ToStmt = CaseStmt::Create(Importer.getToContext(), ToLHS, ToRHS,
5650 ToCaseLoc, ToEllipsisLoc, ToColonLoc);
Gabor Horvath480892b2017-10-18 09:25:18 +00005651 ToStmt->setSubStmt(ToSubStmt);
Balazs Keri3b30d652018-10-19 13:32:20 +00005652
Gabor Horvath480892b2017-10-18 09:25:18 +00005653 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00005654}
5655
Balazs Keri3b30d652018-10-19 13:32:20 +00005656ExpectedStmt ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
5657 auto Imp = importSeq(S->getDefaultLoc(), S->getColonLoc(), S->getSubStmt());
5658 if (!Imp)
5659 return Imp.takeError();
5660
5661 SourceLocation ToDefaultLoc, ToColonLoc;
5662 Stmt *ToSubStmt;
5663 std::tie(ToDefaultLoc, ToColonLoc, ToSubStmt) = *Imp;
5664
5665 return new (Importer.getToContext()) DefaultStmt(
5666 ToDefaultLoc, ToColonLoc, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005667}
5668
Balazs Keri3b30d652018-10-19 13:32:20 +00005669ExpectedStmt ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
5670 auto Imp = importSeq(S->getIdentLoc(), S->getDecl(), S->getSubStmt());
5671 if (!Imp)
5672 return Imp.takeError();
5673
5674 SourceLocation ToIdentLoc;
5675 LabelDecl *ToLabelDecl;
5676 Stmt *ToSubStmt;
5677 std::tie(ToIdentLoc, ToLabelDecl, ToSubStmt) = *Imp;
5678
5679 return new (Importer.getToContext()) LabelStmt(
5680 ToIdentLoc, ToLabelDecl, ToSubStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005681}
5682
Balazs Keri3b30d652018-10-19 13:32:20 +00005683ExpectedStmt ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
5684 ExpectedSLoc ToAttrLocOrErr = import(S->getAttrLoc());
5685 if (!ToAttrLocOrErr)
5686 return ToAttrLocOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005687 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
5688 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Balazs Keri3b30d652018-10-19 13:32:20 +00005689 if (Error Err = ImportContainerChecked(FromAttrs, ToAttrs))
5690 return std::move(Err);
5691 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
5692 if (!ToSubStmtOrErr)
5693 return ToSubStmtOrErr.takeError();
5694
5695 return AttributedStmt::Create(
5696 Importer.getToContext(), *ToAttrLocOrErr, ToAttrs, *ToSubStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005697}
5698
Balazs Keri3b30d652018-10-19 13:32:20 +00005699ExpectedStmt ASTNodeImporter::VisitIfStmt(IfStmt *S) {
5700 auto Imp = importSeq(
5701 S->getIfLoc(), S->getInit(), S->getConditionVariable(), S->getCond(),
5702 S->getThen(), S->getElseLoc(), S->getElse());
5703 if (!Imp)
5704 return Imp.takeError();
5705
5706 SourceLocation ToIfLoc, ToElseLoc;
5707 Stmt *ToInit, *ToThen, *ToElse;
5708 VarDecl *ToConditionVariable;
5709 Expr *ToCond;
5710 std::tie(
5711 ToIfLoc, ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc, ToElse) =
5712 *Imp;
5713
Bruno Riccib1cc94b2018-10-27 21:12:20 +00005714 return IfStmt::Create(Importer.getToContext(), ToIfLoc, S->isConstexpr(),
5715 ToInit, ToConditionVariable, ToCond, ToThen, ToElseLoc,
5716 ToElse);
Sean Callanan59721b32015-04-28 18:41:46 +00005717}
5718
Balazs Keri3b30d652018-10-19 13:32:20 +00005719ExpectedStmt ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
5720 auto Imp = importSeq(
5721 S->getInit(), S->getConditionVariable(), S->getCond(),
5722 S->getBody(), S->getSwitchLoc());
5723 if (!Imp)
5724 return Imp.takeError();
5725
5726 Stmt *ToInit, *ToBody;
5727 VarDecl *ToConditionVariable;
5728 Expr *ToCond;
5729 SourceLocation ToSwitchLoc;
5730 std::tie(ToInit, ToConditionVariable, ToCond, ToBody, ToSwitchLoc) = *Imp;
5731
Bruno Riccie2806f82018-10-29 16:12:37 +00005732 auto *ToStmt = SwitchStmt::Create(Importer.getToContext(), ToInit,
5733 ToConditionVariable, ToCond);
Sean Callanan59721b32015-04-28 18:41:46 +00005734 ToStmt->setBody(ToBody);
Balazs Keri3b30d652018-10-19 13:32:20 +00005735 ToStmt->setSwitchLoc(ToSwitchLoc);
5736
Sean Callanan59721b32015-04-28 18:41:46 +00005737 // Now we have to re-chain the cases.
5738 SwitchCase *LastChainedSwitchCase = nullptr;
5739 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
5740 SC = SC->getNextSwitchCase()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005741 Expected<SwitchCase *> ToSCOrErr = import(SC);
5742 if (!ToSCOrErr)
5743 return ToSCOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005744 if (LastChainedSwitchCase)
Balazs Keri3b30d652018-10-19 13:32:20 +00005745 LastChainedSwitchCase->setNextSwitchCase(*ToSCOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005746 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005747 ToStmt->setSwitchCaseList(*ToSCOrErr);
5748 LastChainedSwitchCase = *ToSCOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005749 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005750
Sean Callanan59721b32015-04-28 18:41:46 +00005751 return ToStmt;
5752}
5753
Balazs Keri3b30d652018-10-19 13:32:20 +00005754ExpectedStmt ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
5755 auto Imp = importSeq(
5756 S->getConditionVariable(), S->getCond(), S->getBody(), S->getWhileLoc());
5757 if (!Imp)
5758 return Imp.takeError();
5759
5760 VarDecl *ToConditionVariable;
5761 Expr *ToCond;
5762 Stmt *ToBody;
5763 SourceLocation ToWhileLoc;
5764 std::tie(ToConditionVariable, ToCond, ToBody, ToWhileLoc) = *Imp;
5765
Bruno Riccibacf7512018-10-30 13:42:41 +00005766 return WhileStmt::Create(Importer.getToContext(), ToConditionVariable, ToCond,
5767 ToBody, ToWhileLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005768}
5769
Balazs Keri3b30d652018-10-19 13:32:20 +00005770ExpectedStmt ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5771 auto Imp = importSeq(
5772 S->getBody(), S->getCond(), S->getDoLoc(), S->getWhileLoc(),
5773 S->getRParenLoc());
5774 if (!Imp)
5775 return Imp.takeError();
5776
5777 Stmt *ToBody;
5778 Expr *ToCond;
5779 SourceLocation ToDoLoc, ToWhileLoc, ToRParenLoc;
5780 std::tie(ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc) = *Imp;
5781
5782 return new (Importer.getToContext()) DoStmt(
5783 ToBody, ToCond, ToDoLoc, ToWhileLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005784}
5785
Balazs Keri3b30d652018-10-19 13:32:20 +00005786ExpectedStmt ASTNodeImporter::VisitForStmt(ForStmt *S) {
5787 auto Imp = importSeq(
5788 S->getInit(), S->getCond(), S->getConditionVariable(), S->getInc(),
5789 S->getBody(), S->getForLoc(), S->getLParenLoc(), S->getRParenLoc());
5790 if (!Imp)
5791 return Imp.takeError();
5792
5793 Stmt *ToInit;
5794 Expr *ToCond, *ToInc;
5795 VarDecl *ToConditionVariable;
5796 Stmt *ToBody;
5797 SourceLocation ToForLoc, ToLParenLoc, ToRParenLoc;
5798 std::tie(
5799 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc,
5800 ToLParenLoc, ToRParenLoc) = *Imp;
5801
5802 return new (Importer.getToContext()) ForStmt(
5803 Importer.getToContext(),
5804 ToInit, ToCond, ToConditionVariable, ToInc, ToBody, ToForLoc, ToLParenLoc,
5805 ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005806}
5807
Balazs Keri3b30d652018-10-19 13:32:20 +00005808ExpectedStmt ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5809 auto Imp = importSeq(S->getLabel(), S->getGotoLoc(), S->getLabelLoc());
5810 if (!Imp)
5811 return Imp.takeError();
5812
5813 LabelDecl *ToLabel;
5814 SourceLocation ToGotoLoc, ToLabelLoc;
5815 std::tie(ToLabel, ToGotoLoc, ToLabelLoc) = *Imp;
5816
5817 return new (Importer.getToContext()) GotoStmt(
5818 ToLabel, ToGotoLoc, ToLabelLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005819}
5820
Balazs Keri3b30d652018-10-19 13:32:20 +00005821ExpectedStmt ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5822 auto Imp = importSeq(S->getGotoLoc(), S->getStarLoc(), S->getTarget());
5823 if (!Imp)
5824 return Imp.takeError();
5825
5826 SourceLocation ToGotoLoc, ToStarLoc;
5827 Expr *ToTarget;
5828 std::tie(ToGotoLoc, ToStarLoc, ToTarget) = *Imp;
5829
5830 return new (Importer.getToContext()) IndirectGotoStmt(
5831 ToGotoLoc, ToStarLoc, ToTarget);
Sean Callanan59721b32015-04-28 18:41:46 +00005832}
5833
Balazs Keri3b30d652018-10-19 13:32:20 +00005834ExpectedStmt ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5835 ExpectedSLoc ToContinueLocOrErr = import(S->getContinueLoc());
5836 if (!ToContinueLocOrErr)
5837 return ToContinueLocOrErr.takeError();
5838 return new (Importer.getToContext()) ContinueStmt(*ToContinueLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005839}
5840
Balazs Keri3b30d652018-10-19 13:32:20 +00005841ExpectedStmt ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5842 auto ToBreakLocOrErr = import(S->getBreakLoc());
5843 if (!ToBreakLocOrErr)
5844 return ToBreakLocOrErr.takeError();
5845 return new (Importer.getToContext()) BreakStmt(*ToBreakLocOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005846}
5847
Balazs Keri3b30d652018-10-19 13:32:20 +00005848ExpectedStmt ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5849 auto Imp = importSeq(
5850 S->getReturnLoc(), S->getRetValue(), S->getNRVOCandidate());
5851 if (!Imp)
5852 return Imp.takeError();
5853
5854 SourceLocation ToReturnLoc;
5855 Expr *ToRetValue;
5856 const VarDecl *ToNRVOCandidate;
5857 std::tie(ToReturnLoc, ToRetValue, ToNRVOCandidate) = *Imp;
5858
Bruno Ricci023b1d12018-10-30 14:40:49 +00005859 return ReturnStmt::Create(Importer.getToContext(), ToReturnLoc, ToRetValue,
5860 ToNRVOCandidate);
Sean Callanan59721b32015-04-28 18:41:46 +00005861}
5862
Balazs Keri3b30d652018-10-19 13:32:20 +00005863ExpectedStmt ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5864 auto Imp = importSeq(
5865 S->getCatchLoc(), S->getExceptionDecl(), S->getHandlerBlock());
5866 if (!Imp)
5867 return Imp.takeError();
5868
5869 SourceLocation ToCatchLoc;
5870 VarDecl *ToExceptionDecl;
5871 Stmt *ToHandlerBlock;
5872 std::tie(ToCatchLoc, ToExceptionDecl, ToHandlerBlock) = *Imp;
5873
5874 return new (Importer.getToContext()) CXXCatchStmt (
5875 ToCatchLoc, ToExceptionDecl, ToHandlerBlock);
Sean Callanan59721b32015-04-28 18:41:46 +00005876}
5877
Balazs Keri3b30d652018-10-19 13:32:20 +00005878ExpectedStmt ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5879 ExpectedSLoc ToTryLocOrErr = import(S->getTryLoc());
5880 if (!ToTryLocOrErr)
5881 return ToTryLocOrErr.takeError();
5882
5883 ExpectedStmt ToTryBlockOrErr = import(S->getTryBlock());
5884 if (!ToTryBlockOrErr)
5885 return ToTryBlockOrErr.takeError();
5886
Sean Callanan59721b32015-04-28 18:41:46 +00005887 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5888 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5889 CXXCatchStmt *FromHandler = S->getHandler(HI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005890 if (auto ToHandlerOrErr = import(FromHandler))
5891 ToHandlers[HI] = *ToHandlerOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005892 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005893 return ToHandlerOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005894 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005895
5896 return CXXTryStmt::Create(
5897 Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers);
Sean Callanan59721b32015-04-28 18:41:46 +00005898}
5899
Balazs Keri3b30d652018-10-19 13:32:20 +00005900ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5901 auto Imp1 = importSeq(
5902 S->getInit(), S->getRangeStmt(), S->getBeginStmt(), S->getEndStmt(),
5903 S->getCond(), S->getInc(), S->getLoopVarStmt(), S->getBody());
5904 if (!Imp1)
5905 return Imp1.takeError();
5906 auto Imp2 = importSeq(
5907 S->getForLoc(), S->getCoawaitLoc(), S->getColonLoc(), S->getRParenLoc());
5908 if (!Imp2)
5909 return Imp2.takeError();
5910
5911 DeclStmt *ToRangeStmt, *ToBeginStmt, *ToEndStmt, *ToLoopVarStmt;
5912 Expr *ToCond, *ToInc;
5913 Stmt *ToInit, *ToBody;
5914 std::tie(
5915 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5916 ToBody) = *Imp1;
5917 SourceLocation ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc;
5918 std::tie(ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc) = *Imp2;
5919
5920 return new (Importer.getToContext()) CXXForRangeStmt(
5921 ToInit, ToRangeStmt, ToBeginStmt, ToEndStmt, ToCond, ToInc, ToLoopVarStmt,
5922 ToBody, ToForLoc, ToCoawaitLoc, ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005923}
5924
Balazs Keri3b30d652018-10-19 13:32:20 +00005925ExpectedStmt
5926ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5927 auto Imp = importSeq(
5928 S->getElement(), S->getCollection(), S->getBody(),
5929 S->getForLoc(), S->getRParenLoc());
5930 if (!Imp)
5931 return Imp.takeError();
5932
5933 Stmt *ToElement, *ToBody;
5934 Expr *ToCollection;
5935 SourceLocation ToForLoc, ToRParenLoc;
5936 std::tie(ToElement, ToCollection, ToBody, ToForLoc, ToRParenLoc) = *Imp;
5937
5938 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElement,
5939 ToCollection,
5940 ToBody,
5941 ToForLoc,
Sean Callanan59721b32015-04-28 18:41:46 +00005942 ToRParenLoc);
5943}
5944
Balazs Keri3b30d652018-10-19 13:32:20 +00005945ExpectedStmt ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5946 auto Imp = importSeq(
5947 S->getAtCatchLoc(), S->getRParenLoc(), S->getCatchParamDecl(),
5948 S->getCatchBody());
5949 if (!Imp)
5950 return Imp.takeError();
5951
5952 SourceLocation ToAtCatchLoc, ToRParenLoc;
5953 VarDecl *ToCatchParamDecl;
5954 Stmt *ToCatchBody;
5955 std::tie(ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody) = *Imp;
5956
5957 return new (Importer.getToContext()) ObjCAtCatchStmt (
5958 ToAtCatchLoc, ToRParenLoc, ToCatchParamDecl, ToCatchBody);
Sean Callanan59721b32015-04-28 18:41:46 +00005959}
5960
Balazs Keri3b30d652018-10-19 13:32:20 +00005961ExpectedStmt ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5962 ExpectedSLoc ToAtFinallyLocOrErr = import(S->getAtFinallyLoc());
5963 if (!ToAtFinallyLocOrErr)
5964 return ToAtFinallyLocOrErr.takeError();
5965 ExpectedStmt ToAtFinallyStmtOrErr = import(S->getFinallyBody());
5966 if (!ToAtFinallyStmtOrErr)
5967 return ToAtFinallyStmtOrErr.takeError();
5968 return new (Importer.getToContext()) ObjCAtFinallyStmt(*ToAtFinallyLocOrErr,
5969 *ToAtFinallyStmtOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00005970}
5971
Balazs Keri3b30d652018-10-19 13:32:20 +00005972ExpectedStmt ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5973 auto Imp = importSeq(
5974 S->getAtTryLoc(), S->getTryBody(), S->getFinallyStmt());
5975 if (!Imp)
5976 return Imp.takeError();
5977
5978 SourceLocation ToAtTryLoc;
5979 Stmt *ToTryBody, *ToFinallyStmt;
5980 std::tie(ToAtTryLoc, ToTryBody, ToFinallyStmt) = *Imp;
5981
Sean Callanan59721b32015-04-28 18:41:46 +00005982 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5983 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5984 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
Balazs Keri3b30d652018-10-19 13:32:20 +00005985 if (ExpectedStmt ToCatchStmtOrErr = import(FromCatchStmt))
5986 ToCatchStmts[CI] = *ToCatchStmtOrErr;
Sean Callanan59721b32015-04-28 18:41:46 +00005987 else
Balazs Keri3b30d652018-10-19 13:32:20 +00005988 return ToCatchStmtOrErr.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00005989 }
Balazs Keri3b30d652018-10-19 13:32:20 +00005990
Sean Callanan59721b32015-04-28 18:41:46 +00005991 return ObjCAtTryStmt::Create(Importer.getToContext(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005992 ToAtTryLoc, ToTryBody,
Sean Callanan59721b32015-04-28 18:41:46 +00005993 ToCatchStmts.begin(), ToCatchStmts.size(),
Balazs Keri3b30d652018-10-19 13:32:20 +00005994 ToFinallyStmt);
Sean Callanan59721b32015-04-28 18:41:46 +00005995}
5996
Balazs Keri3b30d652018-10-19 13:32:20 +00005997ExpectedStmt ASTNodeImporter::VisitObjCAtSynchronizedStmt
Sean Callanan59721b32015-04-28 18:41:46 +00005998 (ObjCAtSynchronizedStmt *S) {
Balazs Keri3b30d652018-10-19 13:32:20 +00005999 auto Imp = importSeq(
6000 S->getAtSynchronizedLoc(), S->getSynchExpr(), S->getSynchBody());
6001 if (!Imp)
6002 return Imp.takeError();
6003
6004 SourceLocation ToAtSynchronizedLoc;
6005 Expr *ToSynchExpr;
6006 Stmt *ToSynchBody;
6007 std::tie(ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody) = *Imp;
6008
Sean Callanan59721b32015-04-28 18:41:46 +00006009 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
6010 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
6011}
6012
Balazs Keri3b30d652018-10-19 13:32:20 +00006013ExpectedStmt ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
6014 ExpectedSLoc ToThrowLocOrErr = import(S->getThrowLoc());
6015 if (!ToThrowLocOrErr)
6016 return ToThrowLocOrErr.takeError();
6017 ExpectedExpr ToThrowExprOrErr = import(S->getThrowExpr());
6018 if (!ToThrowExprOrErr)
6019 return ToThrowExprOrErr.takeError();
6020 return new (Importer.getToContext()) ObjCAtThrowStmt(
6021 *ToThrowLocOrErr, *ToThrowExprOrErr);
Sean Callanan59721b32015-04-28 18:41:46 +00006022}
6023
Balazs Keri3b30d652018-10-19 13:32:20 +00006024ExpectedStmt ASTNodeImporter::VisitObjCAutoreleasePoolStmt(
6025 ObjCAutoreleasePoolStmt *S) {
6026 ExpectedSLoc ToAtLocOrErr = import(S->getAtLoc());
6027 if (!ToAtLocOrErr)
6028 return ToAtLocOrErr.takeError();
6029 ExpectedStmt ToSubStmtOrErr = import(S->getSubStmt());
6030 if (!ToSubStmtOrErr)
6031 return ToSubStmtOrErr.takeError();
6032 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(*ToAtLocOrErr,
6033 *ToSubStmtOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006034}
6035
6036//----------------------------------------------------------------------------
6037// Import Expressions
6038//----------------------------------------------------------------------------
Balazs Keri3b30d652018-10-19 13:32:20 +00006039ExpectedStmt ASTNodeImporter::VisitExpr(Expr *E) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006040 Importer.FromDiag(E->getBeginLoc(), diag::err_unsupported_ast_node)
6041 << E->getStmtClassName();
Balazs Keri3b30d652018-10-19 13:32:20 +00006042 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006043}
6044
Balazs Keri3b30d652018-10-19 13:32:20 +00006045ExpectedStmt ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
6046 auto Imp = importSeq(
6047 E->getBuiltinLoc(), E->getSubExpr(), E->getWrittenTypeInfo(),
6048 E->getRParenLoc(), E->getType());
6049 if (!Imp)
6050 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006051
Balazs Keri3b30d652018-10-19 13:32:20 +00006052 SourceLocation ToBuiltinLoc, ToRParenLoc;
6053 Expr *ToSubExpr;
6054 TypeSourceInfo *ToWrittenTypeInfo;
6055 QualType ToType;
6056 std::tie(ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType) =
6057 *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006058
6059 return new (Importer.getToContext()) VAArgExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006060 ToBuiltinLoc, ToSubExpr, ToWrittenTypeInfo, ToRParenLoc, ToType,
6061 E->isMicrosoftABI());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006062}
6063
Tom Roeder521f0042019-02-26 19:26:41 +00006064ExpectedStmt ASTNodeImporter::VisitChooseExpr(ChooseExpr *E) {
6065 auto Imp = importSeq(E->getCond(), E->getLHS(), E->getRHS(),
6066 E->getBuiltinLoc(), E->getRParenLoc(), E->getType());
6067 if (!Imp)
6068 return Imp.takeError();
6069
6070 Expr *ToCond;
6071 Expr *ToLHS;
6072 Expr *ToRHS;
6073 SourceLocation ToBuiltinLoc, ToRParenLoc;
6074 QualType ToType;
6075 std::tie(ToCond, ToLHS, ToRHS, ToBuiltinLoc, ToRParenLoc, ToType) = *Imp;
6076
6077 ExprValueKind VK = E->getValueKind();
6078 ExprObjectKind OK = E->getObjectKind();
6079
6080 bool TypeDependent = ToCond->isTypeDependent();
6081 bool ValueDependent = ToCond->isValueDependent();
6082
6083 // The value of CondIsTrue only matters if the value is not
6084 // condition-dependent.
6085 bool CondIsTrue = !E->isConditionDependent() && E->isConditionTrue();
6086
6087 return new (Importer.getToContext())
6088 ChooseExpr(ToBuiltinLoc, ToCond, ToLHS, ToRHS, ToType, VK, OK,
6089 ToRParenLoc, CondIsTrue, TypeDependent, ValueDependent);
6090}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006091
Balazs Keri3b30d652018-10-19 13:32:20 +00006092ExpectedStmt ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
6093 ExpectedType TypeOrErr = import(E->getType());
6094 if (!TypeOrErr)
6095 return TypeOrErr.takeError();
6096
6097 ExpectedSLoc BeginLocOrErr = import(E->getBeginLoc());
6098 if (!BeginLocOrErr)
6099 return BeginLocOrErr.takeError();
6100
6101 return new (Importer.getToContext()) GNUNullExpr(*TypeOrErr, *BeginLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006102}
6103
Balazs Keri3b30d652018-10-19 13:32:20 +00006104ExpectedStmt ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
6105 auto Imp = importSeq(
6106 E->getBeginLoc(), E->getType(), E->getFunctionName());
6107 if (!Imp)
6108 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006109
Balazs Keri3b30d652018-10-19 13:32:20 +00006110 SourceLocation ToBeginLoc;
6111 QualType ToType;
6112 StringLiteral *ToFunctionName;
6113 std::tie(ToBeginLoc, ToType, ToFunctionName) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006114
Bruno Ricci17ff0262018-10-27 19:21:19 +00006115 return PredefinedExpr::Create(Importer.getToContext(), ToBeginLoc, ToType,
6116 E->getIdentKind(), ToFunctionName);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006117}
6118
Balazs Keri3b30d652018-10-19 13:32:20 +00006119ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
6120 auto Imp = importSeq(
6121 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDecl(),
6122 E->getLocation(), E->getType());
6123 if (!Imp)
6124 return Imp.takeError();
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006125
Balazs Keri3b30d652018-10-19 13:32:20 +00006126 NestedNameSpecifierLoc ToQualifierLoc;
6127 SourceLocation ToTemplateKeywordLoc, ToLocation;
6128 ValueDecl *ToDecl;
6129 QualType ToType;
6130 std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDecl, ToLocation, ToType) =
6131 *Imp;
6132
6133 NamedDecl *ToFoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006134 if (E->getDecl() != E->getFoundDecl()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006135 auto FoundDOrErr = import(E->getFoundDecl());
6136 if (!FoundDOrErr)
6137 return FoundDOrErr.takeError();
6138 ToFoundD = *FoundDOrErr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00006139 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006140
Aleksei Sidorina693b372016-09-28 10:16:56 +00006141 TemplateArgumentListInfo ToTAInfo;
Balazs Keri3b30d652018-10-19 13:32:20 +00006142 TemplateArgumentListInfo *ToResInfo = nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006143 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006144 if (Error Err =
6145 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6146 return std::move(Err);
6147 ToResInfo = &ToTAInfo;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006148 }
6149
Balazs Keri3b30d652018-10-19 13:32:20 +00006150 auto *ToE = DeclRefExpr::Create(
6151 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
6152 E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
6153 E->getValueKind(), ToFoundD, ToResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00006154 if (E->hadMultipleCandidates())
Balazs Keri3b30d652018-10-19 13:32:20 +00006155 ToE->setHadMultipleCandidates(true);
6156 return ToE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00006157}
6158
Balazs Keri3b30d652018-10-19 13:32:20 +00006159ExpectedStmt ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
6160 ExpectedType TypeOrErr = import(E->getType());
6161 if (!TypeOrErr)
6162 return TypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006163
Balazs Keri3b30d652018-10-19 13:32:20 +00006164 return new (Importer.getToContext()) ImplicitValueInitExpr(*TypeOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006165}
6166
Balazs Keri3b30d652018-10-19 13:32:20 +00006167ExpectedStmt ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
6168 ExpectedExpr ToInitOrErr = import(E->getInit());
6169 if (!ToInitOrErr)
6170 return ToInitOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006171
Balazs Keri3b30d652018-10-19 13:32:20 +00006172 ExpectedSLoc ToEqualOrColonLocOrErr = import(E->getEqualOrColonLoc());
6173 if (!ToEqualOrColonLocOrErr)
6174 return ToEqualOrColonLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006175
Balazs Keri3b30d652018-10-19 13:32:20 +00006176 SmallVector<Expr *, 4> ToIndexExprs(E->getNumSubExprs() - 1);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006177 // List elements from the second, the first is Init itself
Balazs Keri3b30d652018-10-19 13:32:20 +00006178 for (unsigned I = 1, N = E->getNumSubExprs(); I < N; I++) {
6179 if (ExpectedExpr ToArgOrErr = import(E->getSubExpr(I)))
6180 ToIndexExprs[I - 1] = *ToArgOrErr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006181 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006182 return ToArgOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006183 }
6184
Balazs Keri3b30d652018-10-19 13:32:20 +00006185 SmallVector<Designator, 4> ToDesignators(E->size());
6186 if (Error Err = ImportContainerChecked(E->designators(), ToDesignators))
6187 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006188
6189 return DesignatedInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006190 Importer.getToContext(), ToDesignators,
6191 ToIndexExprs, *ToEqualOrColonLocOrErr,
6192 E->usesGNUSyntax(), *ToInitOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006193}
6194
Balazs Keri3b30d652018-10-19 13:32:20 +00006195ExpectedStmt
6196ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
6197 ExpectedType ToTypeOrErr = import(E->getType());
6198 if (!ToTypeOrErr)
6199 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006200
Balazs Keri3b30d652018-10-19 13:32:20 +00006201 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6202 if (!ToLocationOrErr)
6203 return ToLocationOrErr.takeError();
6204
6205 return new (Importer.getToContext()) CXXNullPtrLiteralExpr(
6206 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006207}
6208
Balazs Keri3b30d652018-10-19 13:32:20 +00006209ExpectedStmt ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
6210 ExpectedType ToTypeOrErr = import(E->getType());
6211 if (!ToTypeOrErr)
6212 return ToTypeOrErr.takeError();
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006213
Balazs Keri3b30d652018-10-19 13:32:20 +00006214 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6215 if (!ToLocationOrErr)
6216 return ToLocationOrErr.takeError();
6217
6218 return IntegerLiteral::Create(
6219 Importer.getToContext(), E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006220}
6221
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006222
Balazs Keri3b30d652018-10-19 13:32:20 +00006223ExpectedStmt ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
6224 ExpectedType ToTypeOrErr = import(E->getType());
6225 if (!ToTypeOrErr)
6226 return ToTypeOrErr.takeError();
6227
6228 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6229 if (!ToLocationOrErr)
6230 return ToLocationOrErr.takeError();
6231
6232 return FloatingLiteral::Create(
6233 Importer.getToContext(), E->getValue(), E->isExact(),
6234 *ToTypeOrErr, *ToLocationOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006235}
6236
Balazs Keri3b30d652018-10-19 13:32:20 +00006237ExpectedStmt ASTNodeImporter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
6238 auto ToTypeOrErr = import(E->getType());
6239 if (!ToTypeOrErr)
6240 return ToTypeOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006241
Balazs Keri3b30d652018-10-19 13:32:20 +00006242 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6243 if (!ToSubExprOrErr)
6244 return ToSubExprOrErr.takeError();
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006245
Balazs Keri3b30d652018-10-19 13:32:20 +00006246 return new (Importer.getToContext()) ImaginaryLiteral(
6247 *ToSubExprOrErr, *ToTypeOrErr);
Gabor Martonbf7f18b2018-08-09 12:18:07 +00006248}
6249
Balazs Keri3b30d652018-10-19 13:32:20 +00006250ExpectedStmt ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
6251 ExpectedType ToTypeOrErr = import(E->getType());
6252 if (!ToTypeOrErr)
6253 return ToTypeOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006254
Balazs Keri3b30d652018-10-19 13:32:20 +00006255 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
6256 if (!ToLocationOrErr)
6257 return ToLocationOrErr.takeError();
6258
6259 return new (Importer.getToContext()) CharacterLiteral(
6260 E->getValue(), E->getKind(), *ToTypeOrErr, *ToLocationOrErr);
Douglas Gregor623421d2010-02-18 02:21:22 +00006261}
6262
Balazs Keri3b30d652018-10-19 13:32:20 +00006263ExpectedStmt ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
6264 ExpectedType ToTypeOrErr = import(E->getType());
6265 if (!ToTypeOrErr)
6266 return ToTypeOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006267
Balazs Keri3b30d652018-10-19 13:32:20 +00006268 SmallVector<SourceLocation, 4> ToLocations(E->getNumConcatenated());
6269 if (Error Err = ImportArrayChecked(
6270 E->tokloc_begin(), E->tokloc_end(), ToLocations.begin()))
6271 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006272
Balazs Keri3b30d652018-10-19 13:32:20 +00006273 return StringLiteral::Create(
6274 Importer.getToContext(), E->getBytes(), E->getKind(), E->isPascal(),
6275 *ToTypeOrErr, ToLocations.data(), ToLocations.size());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006276}
6277
Balazs Keri3b30d652018-10-19 13:32:20 +00006278ExpectedStmt ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
6279 auto Imp = importSeq(
6280 E->getLParenLoc(), E->getTypeSourceInfo(), E->getType(),
6281 E->getInitializer());
6282 if (!Imp)
6283 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006284
Balazs Keri3b30d652018-10-19 13:32:20 +00006285 SourceLocation ToLParenLoc;
6286 TypeSourceInfo *ToTypeSourceInfo;
6287 QualType ToType;
6288 Expr *ToInitializer;
6289 std::tie(ToLParenLoc, ToTypeSourceInfo, ToType, ToInitializer) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006290
6291 return new (Importer.getToContext()) CompoundLiteralExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006292 ToLParenLoc, ToTypeSourceInfo, ToType, E->getValueKind(),
6293 ToInitializer, E->isFileScope());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006294}
6295
Balazs Keri3b30d652018-10-19 13:32:20 +00006296ExpectedStmt ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
6297 auto Imp = importSeq(
6298 E->getBuiltinLoc(), E->getType(), E->getRParenLoc());
6299 if (!Imp)
6300 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006301
Balazs Keri3b30d652018-10-19 13:32:20 +00006302 SourceLocation ToBuiltinLoc, ToRParenLoc;
6303 QualType ToType;
6304 std::tie(ToBuiltinLoc, ToType, ToRParenLoc) = *Imp;
6305
6306 SmallVector<Expr *, 6> ToExprs(E->getNumSubExprs());
6307 if (Error Err = ImportArrayChecked(
6308 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
6309 ToExprs.begin()))
6310 return std::move(Err);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006311
6312 return new (Importer.getToContext()) AtomicExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006313 ToBuiltinLoc, ToExprs, ToType, E->getOp(), ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006314}
6315
Balazs Keri3b30d652018-10-19 13:32:20 +00006316ExpectedStmt ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
6317 auto Imp = importSeq(
6318 E->getAmpAmpLoc(), E->getLabelLoc(), E->getLabel(), E->getType());
6319 if (!Imp)
6320 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006321
Balazs Keri3b30d652018-10-19 13:32:20 +00006322 SourceLocation ToAmpAmpLoc, ToLabelLoc;
6323 LabelDecl *ToLabel;
6324 QualType ToType;
6325 std::tie(ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006326
6327 return new (Importer.getToContext()) AddrLabelExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006328 ToAmpAmpLoc, ToLabelLoc, ToLabel, ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006329}
6330
Bill Wendling8003edc2018-11-09 00:41:36 +00006331ExpectedStmt ASTNodeImporter::VisitConstantExpr(ConstantExpr *E) {
6332 auto Imp = importSeq(E->getSubExpr());
6333 if (!Imp)
6334 return Imp.takeError();
6335
6336 Expr *ToSubExpr;
6337 std::tie(ToSubExpr) = *Imp;
6338
Fangrui Song407659a2018-11-30 23:41:18 +00006339 return ConstantExpr::Create(Importer.getToContext(), ToSubExpr);
Bill Wendling8003edc2018-11-09 00:41:36 +00006340}
6341
Balazs Keri3b30d652018-10-19 13:32:20 +00006342ExpectedStmt ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
6343 auto Imp = importSeq(E->getLParen(), E->getRParen(), E->getSubExpr());
6344 if (!Imp)
6345 return Imp.takeError();
6346
6347 SourceLocation ToLParen, ToRParen;
6348 Expr *ToSubExpr;
6349 std::tie(ToLParen, ToRParen, ToSubExpr) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006350
Fangrui Song6907ce22018-07-30 19:24:48 +00006351 return new (Importer.getToContext())
Balazs Keri3b30d652018-10-19 13:32:20 +00006352 ParenExpr(ToLParen, ToRParen, ToSubExpr);
Douglas Gregorc74247e2010-02-19 01:07:06 +00006353}
6354
Balazs Keri3b30d652018-10-19 13:32:20 +00006355ExpectedStmt ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
6356 SmallVector<Expr *, 4> ToExprs(E->getNumExprs());
6357 if (Error Err = ImportContainerChecked(E->exprs(), ToExprs))
6358 return std::move(Err);
6359
6360 ExpectedSLoc ToLParenLocOrErr = import(E->getLParenLoc());
6361 if (!ToLParenLocOrErr)
6362 return ToLParenLocOrErr.takeError();
6363
6364 ExpectedSLoc ToRParenLocOrErr = import(E->getRParenLoc());
6365 if (!ToRParenLocOrErr)
6366 return ToRParenLocOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006367
Bruno Riccif49e1ca2018-11-20 16:20:40 +00006368 return ParenListExpr::Create(Importer.getToContext(), *ToLParenLocOrErr,
6369 ToExprs, *ToRParenLocOrErr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006370}
6371
Balazs Keri3b30d652018-10-19 13:32:20 +00006372ExpectedStmt ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
6373 auto Imp = importSeq(
6374 E->getSubStmt(), E->getType(), E->getLParenLoc(), E->getRParenLoc());
6375 if (!Imp)
6376 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006377
Balazs Keri3b30d652018-10-19 13:32:20 +00006378 CompoundStmt *ToSubStmt;
6379 QualType ToType;
6380 SourceLocation ToLParenLoc, ToRParenLoc;
6381 std::tie(ToSubStmt, ToType, ToLParenLoc, ToRParenLoc) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006382
Balazs Keri3b30d652018-10-19 13:32:20 +00006383 return new (Importer.getToContext()) StmtExpr(
6384 ToSubStmt, ToType, ToLParenLoc, ToRParenLoc);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006385}
6386
Balazs Keri3b30d652018-10-19 13:32:20 +00006387ExpectedStmt ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
6388 auto Imp = importSeq(
6389 E->getSubExpr(), E->getType(), E->getOperatorLoc());
6390 if (!Imp)
6391 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006392
Balazs Keri3b30d652018-10-19 13:32:20 +00006393 Expr *ToSubExpr;
6394 QualType ToType;
6395 SourceLocation ToOperatorLoc;
6396 std::tie(ToSubExpr, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006397
Aaron Ballmana5038552018-01-09 13:07:03 +00006398 return new (Importer.getToContext()) UnaryOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006399 ToSubExpr, E->getOpcode(), ToType, E->getValueKind(), E->getObjectKind(),
6400 ToOperatorLoc, E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006401}
6402
Balazs Keri3b30d652018-10-19 13:32:20 +00006403ExpectedStmt
Aaron Ballmana5038552018-01-09 13:07:03 +00006404ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006405 auto Imp = importSeq(E->getType(), E->getOperatorLoc(), E->getRParenLoc());
6406 if (!Imp)
6407 return Imp.takeError();
6408
6409 QualType ToType;
6410 SourceLocation ToOperatorLoc, ToRParenLoc;
6411 std::tie(ToType, ToOperatorLoc, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006412
Douglas Gregord8552cd2010-02-19 01:24:23 +00006413 if (E->isArgumentType()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006414 Expected<TypeSourceInfo *> ToArgumentTypeInfoOrErr =
6415 import(E->getArgumentTypeInfo());
6416 if (!ToArgumentTypeInfoOrErr)
6417 return ToArgumentTypeInfoOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006418
Balazs Keri3b30d652018-10-19 13:32:20 +00006419 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6420 E->getKind(), *ToArgumentTypeInfoOrErr, ToType, ToOperatorLoc,
6421 ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006422 }
Fangrui Song6907ce22018-07-30 19:24:48 +00006423
Balazs Keri3b30d652018-10-19 13:32:20 +00006424 ExpectedExpr ToArgumentExprOrErr = import(E->getArgumentExpr());
6425 if (!ToArgumentExprOrErr)
6426 return ToArgumentExprOrErr.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006427
Balazs Keri3b30d652018-10-19 13:32:20 +00006428 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(
6429 E->getKind(), *ToArgumentExprOrErr, ToType, ToOperatorLoc, ToRParenLoc);
Douglas Gregord8552cd2010-02-19 01:24:23 +00006430}
6431
Balazs Keri3b30d652018-10-19 13:32:20 +00006432ExpectedStmt ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
6433 auto Imp = importSeq(
6434 E->getLHS(), E->getRHS(), E->getType(), E->getOperatorLoc());
6435 if (!Imp)
6436 return Imp.takeError();
Douglas Gregorc74247e2010-02-19 01:07:06 +00006437
Balazs Keri3b30d652018-10-19 13:32:20 +00006438 Expr *ToLHS, *ToRHS;
6439 QualType ToType;
6440 SourceLocation ToOperatorLoc;
6441 std::tie(ToLHS, ToRHS, ToType, ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006442
Balazs Keri3b30d652018-10-19 13:32:20 +00006443 return new (Importer.getToContext()) BinaryOperator(
6444 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6445 E->getObjectKind(), ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006446}
6447
Balazs Keri3b30d652018-10-19 13:32:20 +00006448ExpectedStmt ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
6449 auto Imp = importSeq(
6450 E->getCond(), E->getQuestionLoc(), E->getLHS(), E->getColonLoc(),
6451 E->getRHS(), E->getType());
6452 if (!Imp)
6453 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006454
Balazs Keri3b30d652018-10-19 13:32:20 +00006455 Expr *ToCond, *ToLHS, *ToRHS;
6456 SourceLocation ToQuestionLoc, ToColonLoc;
6457 QualType ToType;
6458 std::tie(ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006459
6460 return new (Importer.getToContext()) ConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006461 ToCond, ToQuestionLoc, ToLHS, ToColonLoc, ToRHS, ToType,
6462 E->getValueKind(), E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006463}
6464
Balazs Keri3b30d652018-10-19 13:32:20 +00006465ExpectedStmt ASTNodeImporter::VisitBinaryConditionalOperator(
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006466 BinaryConditionalOperator *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006467 auto Imp = importSeq(
6468 E->getCommon(), E->getOpaqueValue(), E->getCond(), E->getTrueExpr(),
6469 E->getFalseExpr(), E->getQuestionLoc(), E->getColonLoc(), E->getType());
6470 if (!Imp)
6471 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006472
Balazs Keri3b30d652018-10-19 13:32:20 +00006473 Expr *ToCommon, *ToCond, *ToTrueExpr, *ToFalseExpr;
6474 OpaqueValueExpr *ToOpaqueValue;
6475 SourceLocation ToQuestionLoc, ToColonLoc;
6476 QualType ToType;
6477 std::tie(
6478 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr, ToQuestionLoc,
6479 ToColonLoc, ToType) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006480
6481 return new (Importer.getToContext()) BinaryConditionalOperator(
Balazs Keri3b30d652018-10-19 13:32:20 +00006482 ToCommon, ToOpaqueValue, ToCond, ToTrueExpr, ToFalseExpr,
6483 ToQuestionLoc, ToColonLoc, ToType, E->getValueKind(),
6484 E->getObjectKind());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006485}
6486
Balazs Keri3b30d652018-10-19 13:32:20 +00006487ExpectedStmt ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
6488 auto Imp = importSeq(
6489 E->getBeginLoc(), E->getQueriedTypeSourceInfo(),
6490 E->getDimensionExpression(), E->getEndLoc(), E->getType());
6491 if (!Imp)
6492 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006493
Balazs Keri3b30d652018-10-19 13:32:20 +00006494 SourceLocation ToBeginLoc, ToEndLoc;
6495 TypeSourceInfo *ToQueriedTypeSourceInfo;
6496 Expr *ToDimensionExpression;
6497 QualType ToType;
6498 std::tie(
6499 ToBeginLoc, ToQueriedTypeSourceInfo, ToDimensionExpression, ToEndLoc,
6500 ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006501
6502 return new (Importer.getToContext()) ArrayTypeTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006503 ToBeginLoc, E->getTrait(), ToQueriedTypeSourceInfo, E->getValue(),
6504 ToDimensionExpression, ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006505}
6506
Balazs Keri3b30d652018-10-19 13:32:20 +00006507ExpectedStmt ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
6508 auto Imp = importSeq(
6509 E->getBeginLoc(), E->getQueriedExpression(), E->getEndLoc(), E->getType());
6510 if (!Imp)
6511 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006512
Balazs Keri3b30d652018-10-19 13:32:20 +00006513 SourceLocation ToBeginLoc, ToEndLoc;
6514 Expr *ToQueriedExpression;
6515 QualType ToType;
6516 std::tie(ToBeginLoc, ToQueriedExpression, ToEndLoc, ToType) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006517
6518 return new (Importer.getToContext()) ExpressionTraitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006519 ToBeginLoc, E->getTrait(), ToQueriedExpression, E->getValue(),
6520 ToEndLoc, ToType);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006521}
6522
Balazs Keri3b30d652018-10-19 13:32:20 +00006523ExpectedStmt ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
6524 auto Imp = importSeq(
6525 E->getLocation(), E->getType(), E->getSourceExpr());
6526 if (!Imp)
6527 return Imp.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006528
Balazs Keri3b30d652018-10-19 13:32:20 +00006529 SourceLocation ToLocation;
6530 QualType ToType;
6531 Expr *ToSourceExpr;
6532 std::tie(ToLocation, ToType, ToSourceExpr) = *Imp;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006533
6534 return new (Importer.getToContext()) OpaqueValueExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006535 ToLocation, ToType, E->getValueKind(), E->getObjectKind(), ToSourceExpr);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006536}
6537
Balazs Keri3b30d652018-10-19 13:32:20 +00006538ExpectedStmt ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
6539 auto Imp = importSeq(
6540 E->getLHS(), E->getRHS(), E->getType(), E->getRBracketLoc());
6541 if (!Imp)
6542 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006543
Balazs Keri3b30d652018-10-19 13:32:20 +00006544 Expr *ToLHS, *ToRHS;
6545 SourceLocation ToRBracketLoc;
6546 QualType ToType;
6547 std::tie(ToLHS, ToRHS, ToType, ToRBracketLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006548
6549 return new (Importer.getToContext()) ArraySubscriptExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006550 ToLHS, ToRHS, ToType, E->getValueKind(), E->getObjectKind(),
6551 ToRBracketLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006552}
6553
Balazs Keri3b30d652018-10-19 13:32:20 +00006554ExpectedStmt
6555ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
6556 auto Imp = importSeq(
6557 E->getLHS(), E->getRHS(), E->getType(), E->getComputationLHSType(),
6558 E->getComputationResultType(), E->getOperatorLoc());
6559 if (!Imp)
6560 return Imp.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006561
Balazs Keri3b30d652018-10-19 13:32:20 +00006562 Expr *ToLHS, *ToRHS;
6563 QualType ToType, ToComputationLHSType, ToComputationResultType;
6564 SourceLocation ToOperatorLoc;
6565 std::tie(ToLHS, ToRHS, ToType, ToComputationLHSType, ToComputationResultType,
6566 ToOperatorLoc) = *Imp;
Craig Topper36250ad2014-05-12 05:36:57 +00006567
Balazs Keri3b30d652018-10-19 13:32:20 +00006568 return new (Importer.getToContext()) CompoundAssignOperator(
6569 ToLHS, ToRHS, E->getOpcode(), ToType, E->getValueKind(),
6570 E->getObjectKind(), ToComputationLHSType, ToComputationResultType,
6571 ToOperatorLoc, E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00006572}
6573
Balazs Keri3b30d652018-10-19 13:32:20 +00006574Expected<CXXCastPath>
6575ASTNodeImporter::ImportCastPath(CastExpr *CE) {
6576 CXXCastPath Path;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006577 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006578 if (auto SpecOrErr = import(*I))
6579 Path.push_back(*SpecOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006580 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006581 return SpecOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006582 }
Balazs Keri3b30d652018-10-19 13:32:20 +00006583 return Path;
John McCallcf142162010-08-07 06:22:56 +00006584}
6585
Balazs Keri3b30d652018-10-19 13:32:20 +00006586ExpectedStmt ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
6587 ExpectedType ToTypeOrErr = import(E->getType());
6588 if (!ToTypeOrErr)
6589 return ToTypeOrErr.takeError();
Douglas Gregor98c10182010-02-12 22:17:39 +00006590
Balazs Keri3b30d652018-10-19 13:32:20 +00006591 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6592 if (!ToSubExprOrErr)
6593 return ToSubExprOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006594
Balazs Keri3b30d652018-10-19 13:32:20 +00006595 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6596 if (!ToBasePathOrErr)
6597 return ToBasePathOrErr.takeError();
John McCallcf142162010-08-07 06:22:56 +00006598
Balazs Keri3b30d652018-10-19 13:32:20 +00006599 return ImplicitCastExpr::Create(
6600 Importer.getToContext(), *ToTypeOrErr, E->getCastKind(), *ToSubExprOrErr,
6601 &(*ToBasePathOrErr), E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00006602}
6603
Balazs Keri3b30d652018-10-19 13:32:20 +00006604ExpectedStmt ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
6605 auto Imp1 = importSeq(
6606 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten());
6607 if (!Imp1)
6608 return Imp1.takeError();
Craig Topper36250ad2014-05-12 05:36:57 +00006609
Balazs Keri3b30d652018-10-19 13:32:20 +00006610 QualType ToType;
6611 Expr *ToSubExpr;
6612 TypeSourceInfo *ToTypeInfoAsWritten;
6613 std::tie(ToType, ToSubExpr, ToTypeInfoAsWritten) = *Imp1;
Douglas Gregor5481d322010-02-19 01:32:14 +00006614
Balazs Keri3b30d652018-10-19 13:32:20 +00006615 Expected<CXXCastPath> ToBasePathOrErr = ImportCastPath(E);
6616 if (!ToBasePathOrErr)
6617 return ToBasePathOrErr.takeError();
6618 CXXCastPath *ToBasePath = &(*ToBasePathOrErr);
John McCallcf142162010-08-07 06:22:56 +00006619
Aleksei Sidorina693b372016-09-28 10:16:56 +00006620 switch (E->getStmtClass()) {
6621 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006622 auto *CCE = cast<CStyleCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006623 ExpectedSLoc ToLParenLocOrErr = import(CCE->getLParenLoc());
6624 if (!ToLParenLocOrErr)
6625 return ToLParenLocOrErr.takeError();
6626 ExpectedSLoc ToRParenLocOrErr = import(CCE->getRParenLoc());
6627 if (!ToRParenLocOrErr)
6628 return ToRParenLocOrErr.takeError();
6629 return CStyleCastExpr::Create(
6630 Importer.getToContext(), ToType, E->getValueKind(), E->getCastKind(),
6631 ToSubExpr, ToBasePath, ToTypeInfoAsWritten, *ToLParenLocOrErr,
6632 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006633 }
6634
6635 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006636 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Balazs Keri3b30d652018-10-19 13:32:20 +00006637 ExpectedSLoc ToLParenLocOrErr = import(FCE->getLParenLoc());
6638 if (!ToLParenLocOrErr)
6639 return ToLParenLocOrErr.takeError();
6640 ExpectedSLoc ToRParenLocOrErr = import(FCE->getRParenLoc());
6641 if (!ToRParenLocOrErr)
6642 return ToRParenLocOrErr.takeError();
6643 return CXXFunctionalCastExpr::Create(
6644 Importer.getToContext(), ToType, E->getValueKind(), ToTypeInfoAsWritten,
6645 E->getCastKind(), ToSubExpr, ToBasePath, *ToLParenLocOrErr,
6646 *ToRParenLocOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006647 }
6648
6649 case Stmt::ObjCBridgedCastExprClass: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006650 auto *OCE = cast<ObjCBridgedCastExpr>(E);
6651 ExpectedSLoc ToLParenLocOrErr = import(OCE->getLParenLoc());
6652 if (!ToLParenLocOrErr)
6653 return ToLParenLocOrErr.takeError();
6654 ExpectedSLoc ToBridgeKeywordLocOrErr = import(OCE->getBridgeKeywordLoc());
6655 if (!ToBridgeKeywordLocOrErr)
6656 return ToBridgeKeywordLocOrErr.takeError();
6657 return new (Importer.getToContext()) ObjCBridgedCastExpr(
6658 *ToLParenLocOrErr, OCE->getBridgeKind(), E->getCastKind(),
6659 *ToBridgeKeywordLocOrErr, ToTypeInfoAsWritten, ToSubExpr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006660 }
6661 default:
Aleksei Sidorina693b372016-09-28 10:16:56 +00006662 llvm_unreachable("Cast expression of unsupported type!");
Balazs Keri3b30d652018-10-19 13:32:20 +00006663 return make_error<ImportError>(ImportError::UnsupportedConstruct);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006664 }
6665}
6666
Balazs Keri3b30d652018-10-19 13:32:20 +00006667ExpectedStmt ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *E) {
6668 SmallVector<OffsetOfNode, 4> ToNodes;
6669 for (int I = 0, N = E->getNumComponents(); I < N; ++I) {
6670 const OffsetOfNode &FromNode = E->getComponent(I);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006671
Balazs Keri3b30d652018-10-19 13:32:20 +00006672 SourceLocation ToBeginLoc, ToEndLoc;
6673 if (FromNode.getKind() != OffsetOfNode::Base) {
6674 auto Imp = importSeq(FromNode.getBeginLoc(), FromNode.getEndLoc());
6675 if (!Imp)
6676 return Imp.takeError();
6677 std::tie(ToBeginLoc, ToEndLoc) = *Imp;
6678 }
Aleksei Sidorina693b372016-09-28 10:16:56 +00006679
Balazs Keri3b30d652018-10-19 13:32:20 +00006680 switch (FromNode.getKind()) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00006681 case OffsetOfNode::Array:
Balazs Keri3b30d652018-10-19 13:32:20 +00006682 ToNodes.push_back(
6683 OffsetOfNode(ToBeginLoc, FromNode.getArrayExprIndex(), ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006684 break;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006685 case OffsetOfNode::Base: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006686 auto ToBSOrErr = import(FromNode.getBase());
6687 if (!ToBSOrErr)
6688 return ToBSOrErr.takeError();
6689 ToNodes.push_back(OffsetOfNode(*ToBSOrErr));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006690 break;
6691 }
6692 case OffsetOfNode::Field: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006693 auto ToFieldOrErr = import(FromNode.getField());
6694 if (!ToFieldOrErr)
6695 return ToFieldOrErr.takeError();
6696 ToNodes.push_back(OffsetOfNode(ToBeginLoc, *ToFieldOrErr, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006697 break;
6698 }
6699 case OffsetOfNode::Identifier: {
Balazs Keri3b30d652018-10-19 13:32:20 +00006700 IdentifierInfo *ToII = Importer.Import(FromNode.getFieldName());
6701 ToNodes.push_back(OffsetOfNode(ToBeginLoc, ToII, ToEndLoc));
Aleksei Sidorina693b372016-09-28 10:16:56 +00006702 break;
6703 }
6704 }
6705 }
6706
Balazs Keri3b30d652018-10-19 13:32:20 +00006707 SmallVector<Expr *, 4> ToExprs(E->getNumExpressions());
6708 for (int I = 0, N = E->getNumExpressions(); I < N; ++I) {
6709 ExpectedExpr ToIndexExprOrErr = import(E->getIndexExpr(I));
6710 if (!ToIndexExprOrErr)
6711 return ToIndexExprOrErr.takeError();
6712 ToExprs[I] = *ToIndexExprOrErr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006713 }
6714
Balazs Keri3b30d652018-10-19 13:32:20 +00006715 auto Imp = importSeq(
6716 E->getType(), E->getTypeSourceInfo(), E->getOperatorLoc(),
6717 E->getRParenLoc());
6718 if (!Imp)
6719 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006720
Balazs Keri3b30d652018-10-19 13:32:20 +00006721 QualType ToType;
6722 TypeSourceInfo *ToTypeSourceInfo;
6723 SourceLocation ToOperatorLoc, ToRParenLoc;
6724 std::tie(ToType, ToTypeSourceInfo, ToOperatorLoc, ToRParenLoc) = *Imp;
6725
6726 return OffsetOfExpr::Create(
6727 Importer.getToContext(), ToType, ToOperatorLoc, ToTypeSourceInfo, ToNodes,
6728 ToExprs, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006729}
6730
Balazs Keri3b30d652018-10-19 13:32:20 +00006731ExpectedStmt ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
6732 auto Imp = importSeq(
6733 E->getType(), E->getOperand(), E->getBeginLoc(), E->getEndLoc());
6734 if (!Imp)
6735 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006736
Balazs Keri3b30d652018-10-19 13:32:20 +00006737 QualType ToType;
6738 Expr *ToOperand;
6739 SourceLocation ToBeginLoc, ToEndLoc;
6740 std::tie(ToType, ToOperand, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006741
Balazs Keri3b30d652018-10-19 13:32:20 +00006742 CanThrowResult ToCanThrow;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006743 if (E->isValueDependent())
Balazs Keri3b30d652018-10-19 13:32:20 +00006744 ToCanThrow = CT_Dependent;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006745 else
Balazs Keri3b30d652018-10-19 13:32:20 +00006746 ToCanThrow = E->getValue() ? CT_Can : CT_Cannot;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006747
Balazs Keri3b30d652018-10-19 13:32:20 +00006748 return new (Importer.getToContext()) CXXNoexceptExpr(
6749 ToType, ToOperand, ToCanThrow, ToBeginLoc, ToEndLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006750}
6751
Balazs Keri3b30d652018-10-19 13:32:20 +00006752ExpectedStmt ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
6753 auto Imp = importSeq(E->getSubExpr(), E->getType(), E->getThrowLoc());
6754 if (!Imp)
6755 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006756
Balazs Keri3b30d652018-10-19 13:32:20 +00006757 Expr *ToSubExpr;
6758 QualType ToType;
6759 SourceLocation ToThrowLoc;
6760 std::tie(ToSubExpr, ToType, ToThrowLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006761
6762 return new (Importer.getToContext()) CXXThrowExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006763 ToSubExpr, ToType, ToThrowLoc, E->isThrownVariableInScope());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006764}
6765
Balazs Keri3b30d652018-10-19 13:32:20 +00006766ExpectedStmt ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
6767 ExpectedSLoc ToUsedLocOrErr = import(E->getUsedLocation());
6768 if (!ToUsedLocOrErr)
6769 return ToUsedLocOrErr.takeError();
6770
6771 auto ToParamOrErr = import(E->getParam());
6772 if (!ToParamOrErr)
6773 return ToParamOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006774
6775 return CXXDefaultArgExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006776 Importer.getToContext(), *ToUsedLocOrErr, *ToParamOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006777}
6778
Balazs Keri3b30d652018-10-19 13:32:20 +00006779ExpectedStmt
6780ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
6781 auto Imp = importSeq(
6782 E->getType(), E->getTypeSourceInfo(), E->getRParenLoc());
6783 if (!Imp)
6784 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006785
Balazs Keri3b30d652018-10-19 13:32:20 +00006786 QualType ToType;
6787 TypeSourceInfo *ToTypeSourceInfo;
6788 SourceLocation ToRParenLoc;
6789 std::tie(ToType, ToTypeSourceInfo, ToRParenLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006790
6791 return new (Importer.getToContext()) CXXScalarValueInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006792 ToType, ToTypeSourceInfo, ToRParenLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006793}
6794
Balazs Keri3b30d652018-10-19 13:32:20 +00006795ExpectedStmt
6796ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
6797 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6798 if (!ToSubExprOrErr)
6799 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006800
Balazs Keri3b30d652018-10-19 13:32:20 +00006801 auto ToDtorOrErr = import(E->getTemporary()->getDestructor());
6802 if (!ToDtorOrErr)
6803 return ToDtorOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006804
6805 ASTContext &ToCtx = Importer.getToContext();
Balazs Keri3b30d652018-10-19 13:32:20 +00006806 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, *ToDtorOrErr);
6807 return CXXBindTemporaryExpr::Create(ToCtx, Temp, *ToSubExprOrErr);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006808}
6809
Balazs Keri3b30d652018-10-19 13:32:20 +00006810ExpectedStmt
6811ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
6812 auto Imp = importSeq(
6813 E->getConstructor(), E->getType(), E->getTypeSourceInfo(),
6814 E->getParenOrBraceRange());
6815 if (!Imp)
6816 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006817
Balazs Keri3b30d652018-10-19 13:32:20 +00006818 CXXConstructorDecl *ToConstructor;
6819 QualType ToType;
6820 TypeSourceInfo *ToTypeSourceInfo;
6821 SourceRange ToParenOrBraceRange;
6822 std::tie(ToConstructor, ToType, ToTypeSourceInfo, ToParenOrBraceRange) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006823
Balazs Keri3b30d652018-10-19 13:32:20 +00006824 SmallVector<Expr *, 8> ToArgs(E->getNumArgs());
6825 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6826 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006827
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00006828 return CXXTemporaryObjectExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006829 Importer.getToContext(), ToConstructor, ToType, ToTypeSourceInfo, ToArgs,
6830 ToParenOrBraceRange, E->hadMultipleCandidates(),
6831 E->isListInitialization(), E->isStdInitListInitialization(),
6832 E->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006833}
6834
Balazs Keri3b30d652018-10-19 13:32:20 +00006835ExpectedStmt
Aleksei Sidorina693b372016-09-28 10:16:56 +00006836ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006837 auto Imp = importSeq(
6838 E->getType(), E->GetTemporaryExpr(), E->getExtendingDecl());
6839 if (!Imp)
6840 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006841
Balazs Keri3b30d652018-10-19 13:32:20 +00006842 QualType ToType;
6843 Expr *ToTemporaryExpr;
6844 const ValueDecl *ToExtendingDecl;
6845 std::tie(ToType, ToTemporaryExpr, ToExtendingDecl) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006846
6847 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006848 ToType, ToTemporaryExpr, E->isBoundToLvalueReference());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006849
6850 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
Balazs Keri3b30d652018-10-19 13:32:20 +00006851 ToMTE->setExtendingDecl(ToExtendingDecl, E->getManglingNumber());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006852 return ToMTE;
6853}
6854
Balazs Keri3b30d652018-10-19 13:32:20 +00006855ExpectedStmt ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
6856 auto Imp = importSeq(
6857 E->getType(), E->getPattern(), E->getEllipsisLoc());
6858 if (!Imp)
6859 return Imp.takeError();
Gabor Horvath7a91c082017-11-14 11:30:38 +00006860
Balazs Keri3b30d652018-10-19 13:32:20 +00006861 QualType ToType;
6862 Expr *ToPattern;
6863 SourceLocation ToEllipsisLoc;
6864 std::tie(ToType, ToPattern, ToEllipsisLoc) = *Imp;
Gabor Horvath7a91c082017-11-14 11:30:38 +00006865
6866 return new (Importer.getToContext()) PackExpansionExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006867 ToType, ToPattern, ToEllipsisLoc, E->getNumExpansions());
Gabor Horvath7a91c082017-11-14 11:30:38 +00006868}
6869
Balazs Keri3b30d652018-10-19 13:32:20 +00006870ExpectedStmt ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
6871 auto Imp = importSeq(
6872 E->getOperatorLoc(), E->getPack(), E->getPackLoc(), E->getRParenLoc());
6873 if (!Imp)
6874 return Imp.takeError();
6875
6876 SourceLocation ToOperatorLoc, ToPackLoc, ToRParenLoc;
6877 NamedDecl *ToPack;
6878 std::tie(ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc) = *Imp;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006879
6880 Optional<unsigned> Length;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006881 if (!E->isValueDependent())
6882 Length = E->getPackLength();
6883
Balazs Keri3b30d652018-10-19 13:32:20 +00006884 SmallVector<TemplateArgument, 8> ToPartialArguments;
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006885 if (E->isPartiallySubstituted()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00006886 if (Error Err = ImportTemplateArguments(
6887 E->getPartialArguments().data(),
6888 E->getPartialArguments().size(),
6889 ToPartialArguments))
6890 return std::move(Err);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006891 }
6892
6893 return SizeOfPackExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006894 Importer.getToContext(), ToOperatorLoc, ToPack, ToPackLoc, ToRParenLoc,
6895 Length, ToPartialArguments);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006896}
6897
Aleksei Sidorina693b372016-09-28 10:16:56 +00006898
Balazs Keri3b30d652018-10-19 13:32:20 +00006899ExpectedStmt ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *E) {
6900 auto Imp = importSeq(
6901 E->getOperatorNew(), E->getOperatorDelete(), E->getTypeIdParens(),
6902 E->getArraySize(), E->getInitializer(), E->getType(),
6903 E->getAllocatedTypeSourceInfo(), E->getSourceRange(),
6904 E->getDirectInitRange());
6905 if (!Imp)
6906 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006907
Balazs Keri3b30d652018-10-19 13:32:20 +00006908 FunctionDecl *ToOperatorNew, *ToOperatorDelete;
6909 SourceRange ToTypeIdParens, ToSourceRange, ToDirectInitRange;
Richard Smithb9fb1212019-05-06 03:47:15 +00006910 Optional<Expr *> ToArraySize;
6911 Expr *ToInitializer;
Balazs Keri3b30d652018-10-19 13:32:20 +00006912 QualType ToType;
6913 TypeSourceInfo *ToAllocatedTypeSourceInfo;
6914 std::tie(
6915 ToOperatorNew, ToOperatorDelete, ToTypeIdParens, ToArraySize, ToInitializer,
6916 ToType, ToAllocatedTypeSourceInfo, ToSourceRange, ToDirectInitRange) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006917
Balazs Keri3b30d652018-10-19 13:32:20 +00006918 SmallVector<Expr *, 4> ToPlacementArgs(E->getNumPlacementArgs());
6919 if (Error Err =
6920 ImportContainerChecked(E->placement_arguments(), ToPlacementArgs))
6921 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006922
Bruno Ricci9b6dfac2019-01-07 15:04:45 +00006923 return CXXNewExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00006924 Importer.getToContext(), E->isGlobalNew(), ToOperatorNew,
6925 ToOperatorDelete, E->passAlignment(), E->doesUsualArrayDeleteWantSize(),
6926 ToPlacementArgs, ToTypeIdParens, ToArraySize, E->getInitializationStyle(),
6927 ToInitializer, ToType, ToAllocatedTypeSourceInfo, ToSourceRange,
6928 ToDirectInitRange);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006929}
6930
Balazs Keri3b30d652018-10-19 13:32:20 +00006931ExpectedStmt ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6932 auto Imp = importSeq(
6933 E->getType(), E->getOperatorDelete(), E->getArgument(), E->getBeginLoc());
6934 if (!Imp)
6935 return Imp.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006936
Balazs Keri3b30d652018-10-19 13:32:20 +00006937 QualType ToType;
6938 FunctionDecl *ToOperatorDelete;
6939 Expr *ToArgument;
6940 SourceLocation ToBeginLoc;
6941 std::tie(ToType, ToOperatorDelete, ToArgument, ToBeginLoc) = *Imp;
Aleksei Sidorina693b372016-09-28 10:16:56 +00006942
6943 return new (Importer.getToContext()) CXXDeleteExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00006944 ToType, E->isGlobalDelete(), E->isArrayForm(), E->isArrayFormAsWritten(),
6945 E->doesUsualArrayDeleteWantSize(), ToOperatorDelete, ToArgument,
6946 ToBeginLoc);
Douglas Gregor5481d322010-02-19 01:32:14 +00006947}
6948
Balazs Keri3b30d652018-10-19 13:32:20 +00006949ExpectedStmt ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6950 auto Imp = importSeq(
6951 E->getType(), E->getLocation(), E->getConstructor(),
6952 E->getParenOrBraceRange());
6953 if (!Imp)
6954 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00006955
Balazs Keri3b30d652018-10-19 13:32:20 +00006956 QualType ToType;
6957 SourceLocation ToLocation;
6958 CXXConstructorDecl *ToConstructor;
6959 SourceRange ToParenOrBraceRange;
6960 std::tie(ToType, ToLocation, ToConstructor, ToParenOrBraceRange) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00006961
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006962 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00006963 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
6964 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00006965
Balazs Keri3b30d652018-10-19 13:32:20 +00006966 return CXXConstructExpr::Create(
6967 Importer.getToContext(), ToType, ToLocation, ToConstructor,
6968 E->isElidable(), ToArgs, E->hadMultipleCandidates(),
6969 E->isListInitialization(), E->isStdInitListInitialization(),
6970 E->requiresZeroInitialization(), E->getConstructionKind(),
6971 ToParenOrBraceRange);
Sean Callanan59721b32015-04-28 18:41:46 +00006972}
6973
Balazs Keri3b30d652018-10-19 13:32:20 +00006974ExpectedStmt ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *E) {
6975 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
6976 if (!ToSubExprOrErr)
6977 return ToSubExprOrErr.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00006978
Balazs Keri3b30d652018-10-19 13:32:20 +00006979 SmallVector<ExprWithCleanups::CleanupObject, 8> ToObjects(E->getNumObjects());
6980 if (Error Err = ImportContainerChecked(E->getObjects(), ToObjects))
6981 return std::move(Err);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006982
Balazs Keri3b30d652018-10-19 13:32:20 +00006983 return ExprWithCleanups::Create(
6984 Importer.getToContext(), *ToSubExprOrErr, E->cleanupsHaveSideEffects(),
6985 ToObjects);
Aleksei Sidorina693b372016-09-28 10:16:56 +00006986}
6987
Balazs Keri3b30d652018-10-19 13:32:20 +00006988ExpectedStmt ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6989 auto Imp = importSeq(
6990 E->getCallee(), E->getType(), E->getRParenLoc());
6991 if (!Imp)
6992 return Imp.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00006993
Balazs Keri3b30d652018-10-19 13:32:20 +00006994 Expr *ToCallee;
6995 QualType ToType;
6996 SourceLocation ToRParenLoc;
6997 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Fangrui Song6907ce22018-07-30 19:24:48 +00006998
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006999 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007000 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7001 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007002
Bruno Riccic5885cf2018-12-21 15:20:32 +00007003 return CXXMemberCallExpr::Create(Importer.getToContext(), ToCallee, ToArgs,
7004 ToType, E->getValueKind(), ToRParenLoc);
Sean Callanan8bca9962016-03-28 21:43:01 +00007005}
7006
Balazs Keri3b30d652018-10-19 13:32:20 +00007007ExpectedStmt ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
7008 ExpectedType ToTypeOrErr = import(E->getType());
7009 if (!ToTypeOrErr)
7010 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007011
Balazs Keri3b30d652018-10-19 13:32:20 +00007012 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7013 if (!ToLocationOrErr)
7014 return ToLocationOrErr.takeError();
7015
7016 return new (Importer.getToContext()) CXXThisExpr(
7017 *ToLocationOrErr, *ToTypeOrErr, E->isImplicit());
Sean Callanan8bca9962016-03-28 21:43:01 +00007018}
7019
Balazs Keri3b30d652018-10-19 13:32:20 +00007020ExpectedStmt ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
7021 ExpectedType ToTypeOrErr = import(E->getType());
7022 if (!ToTypeOrErr)
7023 return ToTypeOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007024
Balazs Keri3b30d652018-10-19 13:32:20 +00007025 ExpectedSLoc ToLocationOrErr = import(E->getLocation());
7026 if (!ToLocationOrErr)
7027 return ToLocationOrErr.takeError();
7028
7029 return new (Importer.getToContext()) CXXBoolLiteralExpr(
7030 E->getValue(), *ToTypeOrErr, *ToLocationOrErr);
Sean Callanan8bca9962016-03-28 21:43:01 +00007031}
7032
Balazs Keri3b30d652018-10-19 13:32:20 +00007033ExpectedStmt ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
7034 auto Imp1 = importSeq(
7035 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7036 E->getTemplateKeywordLoc(), E->getMemberDecl(), E->getType());
7037 if (!Imp1)
7038 return Imp1.takeError();
Sean Callanan8bca9962016-03-28 21:43:01 +00007039
Balazs Keri3b30d652018-10-19 13:32:20 +00007040 Expr *ToBase;
7041 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7042 NestedNameSpecifierLoc ToQualifierLoc;
7043 ValueDecl *ToMemberDecl;
7044 QualType ToType;
7045 std::tie(
7046 ToBase, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl,
7047 ToType) = *Imp1;
Sean Callanan59721b32015-04-28 18:41:46 +00007048
Balazs Keri3b30d652018-10-19 13:32:20 +00007049 auto Imp2 = importSeq(
7050 E->getFoundDecl().getDecl(), E->getMemberNameInfo().getName(),
7051 E->getMemberNameInfo().getLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7052 if (!Imp2)
7053 return Imp2.takeError();
7054 NamedDecl *ToDecl;
7055 DeclarationName ToName;
7056 SourceLocation ToLoc, ToLAngleLoc, ToRAngleLoc;
7057 std::tie(ToDecl, ToName, ToLoc, ToLAngleLoc, ToRAngleLoc) = *Imp2;
Peter Szecsief972522018-05-02 11:52:54 +00007058
7059 DeclAccessPair ToFoundDecl =
7060 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00007061
Balazs Keri3b30d652018-10-19 13:32:20 +00007062 DeclarationNameInfo ToMemberNameInfo(ToName, ToLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00007063
Gabor Marton5caba302019-03-07 13:38:20 +00007064 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007065 if (E->hasExplicitTemplateArgs()) {
Gabor Marton5caba302019-03-07 13:38:20 +00007066 if (Error Err =
7067 ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
7068 E->template_arguments(), ToTAInfo))
7069 return std::move(Err);
7070 ResInfo = &ToTAInfo;
Sean Callanan59721b32015-04-28 18:41:46 +00007071 }
7072
Balazs Keri3b30d652018-10-19 13:32:20 +00007073 return MemberExpr::Create(
7074 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7075 ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl, ToFoundDecl,
Gabor Marton5caba302019-03-07 13:38:20 +00007076 ToMemberNameInfo, ResInfo, ToType, E->getValueKind(), E->getObjectKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007077}
7078
Balazs Keri3b30d652018-10-19 13:32:20 +00007079ExpectedStmt
7080ASTNodeImporter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
7081 auto Imp = importSeq(
7082 E->getBase(), E->getOperatorLoc(), E->getQualifierLoc(),
7083 E->getScopeTypeInfo(), E->getColonColonLoc(), E->getTildeLoc());
7084 if (!Imp)
7085 return Imp.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007086
Balazs Keri3b30d652018-10-19 13:32:20 +00007087 Expr *ToBase;
7088 SourceLocation ToOperatorLoc, ToColonColonLoc, ToTildeLoc;
7089 NestedNameSpecifierLoc ToQualifierLoc;
7090 TypeSourceInfo *ToScopeTypeInfo;
7091 std::tie(
7092 ToBase, ToOperatorLoc, ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc,
7093 ToTildeLoc) = *Imp;
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007094
7095 PseudoDestructorTypeStorage Storage;
7096 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
7097 IdentifierInfo *ToII = Importer.Import(FromII);
Balazs Keri3b30d652018-10-19 13:32:20 +00007098 ExpectedSLoc ToDestroyedTypeLocOrErr = import(E->getDestroyedTypeLoc());
7099 if (!ToDestroyedTypeLocOrErr)
7100 return ToDestroyedTypeLocOrErr.takeError();
7101 Storage = PseudoDestructorTypeStorage(ToII, *ToDestroyedTypeLocOrErr);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007102 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007103 if (auto ToTIOrErr = import(E->getDestroyedTypeInfo()))
7104 Storage = PseudoDestructorTypeStorage(*ToTIOrErr);
7105 else
7106 return ToTIOrErr.takeError();
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007107 }
7108
7109 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007110 Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc,
7111 ToQualifierLoc, ToScopeTypeInfo, ToColonColonLoc, ToTildeLoc, Storage);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00007112}
7113
Balazs Keri3b30d652018-10-19 13:32:20 +00007114ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007115 CXXDependentScopeMemberExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007116 auto Imp = importSeq(
7117 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7118 E->getTemplateKeywordLoc(), E->getFirstQualifierFoundInScope());
7119 if (!Imp)
7120 return Imp.takeError();
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007121
Balazs Keri3b30d652018-10-19 13:32:20 +00007122 QualType ToType;
7123 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7124 NestedNameSpecifierLoc ToQualifierLoc;
7125 NamedDecl *ToFirstQualifierFoundInScope;
7126 std::tie(
7127 ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7128 ToFirstQualifierFoundInScope) = *Imp;
7129
7130 Expr *ToBase = nullptr;
7131 if (!E->isImplicitAccess()) {
7132 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7133 ToBase = *ToBaseOrErr;
7134 else
7135 return ToBaseOrErr.takeError();
7136 }
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007137
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007138 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007139 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007140 if (Error Err = ImportTemplateArgumentListInfo(
7141 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7142 ToTAInfo))
7143 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007144 ResInfo = &ToTAInfo;
7145 }
7146
Balazs Keri3b30d652018-10-19 13:32:20 +00007147 auto ToMemberNameInfoOrErr = importSeq(E->getMember(), E->getMemberLoc());
7148 if (!ToMemberNameInfoOrErr)
7149 return ToMemberNameInfoOrErr.takeError();
7150 DeclarationNameInfo ToMemberNameInfo(
7151 std::get<0>(*ToMemberNameInfoOrErr), std::get<1>(*ToMemberNameInfoOrErr));
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007152 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007153 if (Error Err = ImportDeclarationNameLoc(
7154 E->getMemberNameInfo(), ToMemberNameInfo))
7155 return std::move(Err);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007156
7157 return CXXDependentScopeMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007158 Importer.getToContext(), ToBase, ToType, E->isArrow(), ToOperatorLoc,
7159 ToQualifierLoc, ToTemplateKeywordLoc, ToFirstQualifierFoundInScope,
7160 ToMemberNameInfo, ResInfo);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00007161}
7162
Balazs Keri3b30d652018-10-19 13:32:20 +00007163ExpectedStmt
Peter Szecsice7f3182018-05-07 12:08:27 +00007164ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007165 auto Imp = importSeq(
7166 E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
7167 E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7168 if (!Imp)
7169 return Imp.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007170
Balazs Keri3b30d652018-10-19 13:32:20 +00007171 NestedNameSpecifierLoc ToQualifierLoc;
7172 SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
7173 DeclarationName ToDeclName;
7174 std::tie(
7175 ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
7176 ToLAngleLoc, ToRAngleLoc) = *Imp;
Peter Szecsice7f3182018-05-07 12:08:27 +00007177
Balazs Keri3b30d652018-10-19 13:32:20 +00007178 DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
7179 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7180 return std::move(Err);
7181
7182 TemplateArgumentListInfo ToTAInfo(ToLAngleLoc, ToRAngleLoc);
Peter Szecsice7f3182018-05-07 12:08:27 +00007183 TemplateArgumentListInfo *ResInfo = nullptr;
7184 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007185 if (Error Err =
7186 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7187 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007188 ResInfo = &ToTAInfo;
7189 }
7190
7191 return DependentScopeDeclRefExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007192 Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc,
7193 ToNameInfo, ResInfo);
Peter Szecsice7f3182018-05-07 12:08:27 +00007194}
7195
Balazs Keri3b30d652018-10-19 13:32:20 +00007196ExpectedStmt ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
7197 CXXUnresolvedConstructExpr *E) {
7198 auto Imp = importSeq(
7199 E->getLParenLoc(), E->getRParenLoc(), E->getTypeSourceInfo());
7200 if (!Imp)
7201 return Imp.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007202
Balazs Keri3b30d652018-10-19 13:32:20 +00007203 SourceLocation ToLParenLoc, ToRParenLoc;
7204 TypeSourceInfo *ToTypeSourceInfo;
7205 std::tie(ToLParenLoc, ToRParenLoc, ToTypeSourceInfo) = *Imp;
7206
7207 SmallVector<Expr *, 8> ToArgs(E->arg_size());
7208 if (Error Err =
7209 ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
7210 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007211
7212 return CXXUnresolvedConstructExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007213 Importer.getToContext(), ToTypeSourceInfo, ToLParenLoc,
7214 llvm::makeArrayRef(ToArgs), ToRParenLoc);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007215}
7216
Balazs Keri3b30d652018-10-19 13:32:20 +00007217ExpectedStmt
7218ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
7219 Expected<CXXRecordDecl *> ToNamingClassOrErr = import(E->getNamingClass());
7220 if (!ToNamingClassOrErr)
7221 return ToNamingClassOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007222
Balazs Keri3b30d652018-10-19 13:32:20 +00007223 auto ToQualifierLocOrErr = import(E->getQualifierLoc());
7224 if (!ToQualifierLocOrErr)
7225 return ToQualifierLocOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007226
Balazs Keri3b30d652018-10-19 13:32:20 +00007227 auto ToNameInfoOrErr = importSeq(E->getName(), E->getNameLoc());
7228 if (!ToNameInfoOrErr)
7229 return ToNameInfoOrErr.takeError();
7230 DeclarationNameInfo ToNameInfo(
7231 std::get<0>(*ToNameInfoOrErr), std::get<1>(*ToNameInfoOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007232 // Import additional name location/type info.
Balazs Keri3b30d652018-10-19 13:32:20 +00007233 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7234 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007235
7236 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007237 for (auto *D : E->decls())
7238 if (auto ToDOrErr = import(D))
7239 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007240 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007241 return ToDOrErr.takeError();
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007242
Balazs Keri3b30d652018-10-19 13:32:20 +00007243 if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
7244 TemplateArgumentListInfo ToTAInfo;
7245 if (Error Err = ImportTemplateArgumentListInfo(
7246 E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
7247 ToTAInfo))
7248 return std::move(Err);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007249
Balazs Keri3b30d652018-10-19 13:32:20 +00007250 ExpectedSLoc ToTemplateKeywordLocOrErr = import(E->getTemplateKeywordLoc());
7251 if (!ToTemplateKeywordLocOrErr)
7252 return ToTemplateKeywordLocOrErr.takeError();
7253
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007254 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007255 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7256 *ToTemplateKeywordLocOrErr, ToNameInfo, E->requiresADL(), &ToTAInfo,
7257 ToDecls.begin(), ToDecls.end());
7258 }
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007259
7260 return UnresolvedLookupExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007261 Importer.getToContext(), *ToNamingClassOrErr, *ToQualifierLocOrErr,
7262 ToNameInfo, E->requiresADL(), E->isOverloaded(), ToDecls.begin(),
7263 ToDecls.end());
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00007264}
7265
Balazs Keri3b30d652018-10-19 13:32:20 +00007266ExpectedStmt
7267ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
7268 auto Imp1 = importSeq(
7269 E->getType(), E->getOperatorLoc(), E->getQualifierLoc(),
7270 E->getTemplateKeywordLoc());
7271 if (!Imp1)
7272 return Imp1.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007273
Balazs Keri3b30d652018-10-19 13:32:20 +00007274 QualType ToType;
7275 SourceLocation ToOperatorLoc, ToTemplateKeywordLoc;
7276 NestedNameSpecifierLoc ToQualifierLoc;
7277 std::tie(ToType, ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc) = *Imp1;
7278
7279 auto Imp2 = importSeq(E->getName(), E->getNameLoc());
7280 if (!Imp2)
7281 return Imp2.takeError();
7282 DeclarationNameInfo ToNameInfo(std::get<0>(*Imp2), std::get<1>(*Imp2));
7283 // Import additional name location/type info.
7284 if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
7285 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007286
7287 UnresolvedSet<8> ToDecls;
Balazs Keri3b30d652018-10-19 13:32:20 +00007288 for (Decl *D : E->decls())
7289 if (auto ToDOrErr = import(D))
7290 ToDecls.addDecl(cast<NamedDecl>(*ToDOrErr));
Peter Szecsice7f3182018-05-07 12:08:27 +00007291 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007292 return ToDOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007293
7294 TemplateArgumentListInfo ToTAInfo;
7295 TemplateArgumentListInfo *ResInfo = nullptr;
7296 if (E->hasExplicitTemplateArgs()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007297 if (Error Err =
7298 ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7299 return std::move(Err);
Peter Szecsice7f3182018-05-07 12:08:27 +00007300 ResInfo = &ToTAInfo;
7301 }
7302
Balazs Keri3b30d652018-10-19 13:32:20 +00007303 Expr *ToBase = nullptr;
7304 if (!E->isImplicitAccess()) {
7305 if (ExpectedExpr ToBaseOrErr = import(E->getBase()))
7306 ToBase = *ToBaseOrErr;
7307 else
7308 return ToBaseOrErr.takeError();
Peter Szecsice7f3182018-05-07 12:08:27 +00007309 }
7310
7311 return UnresolvedMemberExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007312 Importer.getToContext(), E->hasUnresolvedUsing(), ToBase, ToType,
7313 E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc,
7314 ToNameInfo, ResInfo, ToDecls.begin(), ToDecls.end());
Peter Szecsice7f3182018-05-07 12:08:27 +00007315}
7316
Balazs Keri3b30d652018-10-19 13:32:20 +00007317ExpectedStmt ASTNodeImporter::VisitCallExpr(CallExpr *E) {
7318 auto Imp = importSeq(E->getCallee(), E->getType(), E->getRParenLoc());
7319 if (!Imp)
7320 return Imp.takeError();
Sean Callanan59721b32015-04-28 18:41:46 +00007321
Balazs Keri3b30d652018-10-19 13:32:20 +00007322 Expr *ToCallee;
7323 QualType ToType;
7324 SourceLocation ToRParenLoc;
7325 std::tie(ToCallee, ToType, ToRParenLoc) = *Imp;
Sean Callanan59721b32015-04-28 18:41:46 +00007326
7327 unsigned NumArgs = E->getNumArgs();
Balazs Keri3b30d652018-10-19 13:32:20 +00007328 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
7329 if (Error Err = ImportContainerChecked(E->arguments(), ToArgs))
7330 return std::move(Err);
Sean Callanan59721b32015-04-28 18:41:46 +00007331
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007332 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
Bruno Riccic5885cf2018-12-21 15:20:32 +00007333 return CXXOperatorCallExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007334 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, ToType,
Eric Fiselier5cdc2cd2018-12-12 21:50:55 +00007335 OCE->getValueKind(), ToRParenLoc, OCE->getFPFeatures(),
7336 OCE->getADLCallKind());
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007337 }
7338
Bruno Riccic5885cf2018-12-21 15:20:32 +00007339 return CallExpr::Create(Importer.getToContext(), ToCallee, ToArgs, ToType,
7340 E->getValueKind(), ToRParenLoc, /*MinNumArgs=*/0,
7341 E->getADLCallKind());
Sean Callanan59721b32015-04-28 18:41:46 +00007342}
7343
Balazs Keri3b30d652018-10-19 13:32:20 +00007344ExpectedStmt ASTNodeImporter::VisitLambdaExpr(LambdaExpr *E) {
7345 CXXRecordDecl *FromClass = E->getLambdaClass();
7346 auto ToClassOrErr = import(FromClass);
7347 if (!ToClassOrErr)
7348 return ToClassOrErr.takeError();
7349 CXXRecordDecl *ToClass = *ToClassOrErr;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007350
7351 // NOTE: lambda classes are created with BeingDefined flag set up.
7352 // It means that ImportDefinition doesn't work for them and we should fill it
7353 // manually.
Gabor Marton302f3002019-02-15 12:04:05 +00007354 if (ToClass->isBeingDefined())
7355 if (Error Err = ImportDeclContext(FromClass, /*ForceImport = */ true))
7356 return std::move(Err);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007357
Balazs Keri3b30d652018-10-19 13:32:20 +00007358 auto ToCallOpOrErr = import(E->getCallOperator());
7359 if (!ToCallOpOrErr)
7360 return ToCallOpOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007361
7362 ToClass->completeDefinition();
7363
Balazs Keri3b30d652018-10-19 13:32:20 +00007364 SmallVector<LambdaCapture, 8> ToCaptures;
7365 ToCaptures.reserve(E->capture_size());
7366 for (const auto &FromCapture : E->captures()) {
7367 if (auto ToCaptureOrErr = import(FromCapture))
7368 ToCaptures.push_back(*ToCaptureOrErr);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007369 else
Balazs Keri3b30d652018-10-19 13:32:20 +00007370 return ToCaptureOrErr.takeError();
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007371 }
7372
Balazs Keri3b30d652018-10-19 13:32:20 +00007373 SmallVector<Expr *, 8> ToCaptureInits(E->capture_size());
7374 if (Error Err = ImportContainerChecked(E->capture_inits(), ToCaptureInits))
7375 return std::move(Err);
7376
7377 auto Imp = importSeq(
7378 E->getIntroducerRange(), E->getCaptureDefaultLoc(), E->getEndLoc());
7379 if (!Imp)
7380 return Imp.takeError();
7381
7382 SourceRange ToIntroducerRange;
7383 SourceLocation ToCaptureDefaultLoc, ToEndLoc;
7384 std::tie(ToIntroducerRange, ToCaptureDefaultLoc, ToEndLoc) = *Imp;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007385
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007386 return LambdaExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007387 Importer.getToContext(), ToClass, ToIntroducerRange,
7388 E->getCaptureDefault(), ToCaptureDefaultLoc, ToCaptures,
7389 E->hasExplicitParameters(), E->hasExplicitResultType(), ToCaptureInits,
7390 ToEndLoc, E->containsUnexpandedParameterPack());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00007391}
7392
Sean Callanan8bca9962016-03-28 21:43:01 +00007393
Balazs Keri3b30d652018-10-19 13:32:20 +00007394ExpectedStmt ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
7395 auto Imp = importSeq(E->getLBraceLoc(), E->getRBraceLoc(), E->getType());
7396 if (!Imp)
7397 return Imp.takeError();
7398
7399 SourceLocation ToLBraceLoc, ToRBraceLoc;
7400 QualType ToType;
7401 std::tie(ToLBraceLoc, ToRBraceLoc, ToType) = *Imp;
7402
7403 SmallVector<Expr *, 4> ToExprs(E->getNumInits());
7404 if (Error Err = ImportContainerChecked(E->inits(), ToExprs))
7405 return std::move(Err);
Sean Callanan8bca9962016-03-28 21:43:01 +00007406
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007407 ASTContext &ToCtx = Importer.getToContext();
7408 InitListExpr *To = new (ToCtx) InitListExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007409 ToCtx, ToLBraceLoc, ToExprs, ToRBraceLoc);
7410 To->setType(ToType);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007411
Balazs Keri3b30d652018-10-19 13:32:20 +00007412 if (E->hasArrayFiller()) {
7413 if (ExpectedExpr ToFillerOrErr = import(E->getArrayFiller()))
7414 To->setArrayFiller(*ToFillerOrErr);
7415 else
7416 return ToFillerOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007417 }
7418
Balazs Keri3b30d652018-10-19 13:32:20 +00007419 if (FieldDecl *FromFD = E->getInitializedFieldInUnion()) {
7420 if (auto ToFDOrErr = import(FromFD))
7421 To->setInitializedFieldInUnion(*ToFDOrErr);
7422 else
7423 return ToFDOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007424 }
7425
Balazs Keri3b30d652018-10-19 13:32:20 +00007426 if (InitListExpr *SyntForm = E->getSyntacticForm()) {
7427 if (auto ToSyntFormOrErr = import(SyntForm))
7428 To->setSyntacticForm(*ToSyntFormOrErr);
7429 else
7430 return ToSyntFormOrErr.takeError();
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007431 }
7432
Gabor Martona20ce602018-09-03 13:10:53 +00007433 // Copy InitListExprBitfields, which are not handled in the ctor of
7434 // InitListExpr.
Balazs Keri3b30d652018-10-19 13:32:20 +00007435 To->sawArrayRangeDesignator(E->hadArrayRangeDesignator());
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00007436
7437 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00007438}
7439
Balazs Keri3b30d652018-10-19 13:32:20 +00007440ExpectedStmt ASTNodeImporter::VisitCXXStdInitializerListExpr(
Gabor Marton07b01ff2018-06-29 12:17:34 +00007441 CXXStdInitializerListExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007442 ExpectedType ToTypeOrErr = import(E->getType());
7443 if (!ToTypeOrErr)
7444 return ToTypeOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007445
Balazs Keri3b30d652018-10-19 13:32:20 +00007446 ExpectedExpr ToSubExprOrErr = import(E->getSubExpr());
7447 if (!ToSubExprOrErr)
7448 return ToSubExprOrErr.takeError();
Gabor Marton07b01ff2018-06-29 12:17:34 +00007449
Balazs Keri3b30d652018-10-19 13:32:20 +00007450 return new (Importer.getToContext()) CXXStdInitializerListExpr(
7451 *ToTypeOrErr, *ToSubExprOrErr);
Gabor Marton07b01ff2018-06-29 12:17:34 +00007452}
7453
Balazs Keri3b30d652018-10-19 13:32:20 +00007454ExpectedStmt ASTNodeImporter::VisitCXXInheritedCtorInitExpr(
Balazs Keri95baa842018-07-25 10:21:06 +00007455 CXXInheritedCtorInitExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007456 auto Imp = importSeq(E->getLocation(), E->getType(), E->getConstructor());
7457 if (!Imp)
7458 return Imp.takeError();
Balazs Keri95baa842018-07-25 10:21:06 +00007459
Balazs Keri3b30d652018-10-19 13:32:20 +00007460 SourceLocation ToLocation;
7461 QualType ToType;
7462 CXXConstructorDecl *ToConstructor;
7463 std::tie(ToLocation, ToType, ToConstructor) = *Imp;
Balazs Keri95baa842018-07-25 10:21:06 +00007464
7465 return new (Importer.getToContext()) CXXInheritedCtorInitExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007466 ToLocation, ToType, ToConstructor, E->constructsVBase(),
7467 E->inheritedFromVBase());
Balazs Keri95baa842018-07-25 10:21:06 +00007468}
7469
Balazs Keri3b30d652018-10-19 13:32:20 +00007470ExpectedStmt ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
7471 auto Imp = importSeq(E->getType(), E->getCommonExpr(), E->getSubExpr());
7472 if (!Imp)
7473 return Imp.takeError();
Richard Smith30e304e2016-12-14 00:03:17 +00007474
Balazs Keri3b30d652018-10-19 13:32:20 +00007475 QualType ToType;
7476 Expr *ToCommonExpr, *ToSubExpr;
7477 std::tie(ToType, ToCommonExpr, ToSubExpr) = *Imp;
Richard Smith30e304e2016-12-14 00:03:17 +00007478
Balazs Keri3b30d652018-10-19 13:32:20 +00007479 return new (Importer.getToContext()) ArrayInitLoopExpr(
7480 ToType, ToCommonExpr, ToSubExpr);
Richard Smith30e304e2016-12-14 00:03:17 +00007481}
7482
Balazs Keri3b30d652018-10-19 13:32:20 +00007483ExpectedStmt ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
7484 ExpectedType ToTypeOrErr = import(E->getType());
7485 if (!ToTypeOrErr)
7486 return ToTypeOrErr.takeError();
7487 return new (Importer.getToContext()) ArrayInitIndexExpr(*ToTypeOrErr);
Richard Smith30e304e2016-12-14 00:03:17 +00007488}
7489
Balazs Keri3b30d652018-10-19 13:32:20 +00007490ExpectedStmt ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
7491 ExpectedSLoc ToBeginLocOrErr = import(E->getBeginLoc());
7492 if (!ToBeginLocOrErr)
7493 return ToBeginLocOrErr.takeError();
7494
7495 auto ToFieldOrErr = import(E->getField());
7496 if (!ToFieldOrErr)
7497 return ToFieldOrErr.takeError();
Sean Callanandd2c1742016-05-16 20:48:03 +00007498
7499 return CXXDefaultInitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007500 Importer.getToContext(), *ToBeginLocOrErr, *ToFieldOrErr);
Sean Callanandd2c1742016-05-16 20:48:03 +00007501}
7502
Balazs Keri3b30d652018-10-19 13:32:20 +00007503ExpectedStmt ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
7504 auto Imp = importSeq(
7505 E->getType(), E->getSubExpr(), E->getTypeInfoAsWritten(),
7506 E->getOperatorLoc(), E->getRParenLoc(), E->getAngleBrackets());
7507 if (!Imp)
7508 return Imp.takeError();
7509
7510 QualType ToType;
7511 Expr *ToSubExpr;
7512 TypeSourceInfo *ToTypeInfoAsWritten;
7513 SourceLocation ToOperatorLoc, ToRParenLoc;
7514 SourceRange ToAngleBrackets;
7515 std::tie(
7516 ToType, ToSubExpr, ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc,
7517 ToAngleBrackets) = *Imp;
7518
Sean Callanandd2c1742016-05-16 20:48:03 +00007519 ExprValueKind VK = E->getValueKind();
7520 CastKind CK = E->getCastKind();
Balazs Keri3b30d652018-10-19 13:32:20 +00007521 auto ToBasePathOrErr = ImportCastPath(E);
7522 if (!ToBasePathOrErr)
7523 return ToBasePathOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007524
Sean Callanandd2c1742016-05-16 20:48:03 +00007525 if (isa<CXXStaticCastExpr>(E)) {
7526 return CXXStaticCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007527 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7528 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007529 } else if (isa<CXXDynamicCastExpr>(E)) {
7530 return CXXDynamicCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007531 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7532 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007533 } else if (isa<CXXReinterpretCastExpr>(E)) {
7534 return CXXReinterpretCastExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007535 Importer.getToContext(), ToType, VK, CK, ToSubExpr, &(*ToBasePathOrErr),
7536 ToTypeInfoAsWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Raphael Isemannc705bb82018-08-20 16:20:01 +00007537 } else if (isa<CXXConstCastExpr>(E)) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007538 return CXXConstCastExpr::Create(
7539 Importer.getToContext(), ToType, VK, ToSubExpr, ToTypeInfoAsWritten,
7540 ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
Sean Callanandd2c1742016-05-16 20:48:03 +00007541 } else {
Balazs Keri3b30d652018-10-19 13:32:20 +00007542 llvm_unreachable("Unknown cast type");
7543 return make_error<ImportError>();
Sean Callanandd2c1742016-05-16 20:48:03 +00007544 }
7545}
7546
Balazs Keri3b30d652018-10-19 13:32:20 +00007547ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007548 SubstNonTypeTemplateParmExpr *E) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007549 auto Imp = importSeq(
7550 E->getType(), E->getExprLoc(), E->getParameter(), E->getReplacement());
7551 if (!Imp)
7552 return Imp.takeError();
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007553
Balazs Keri3b30d652018-10-19 13:32:20 +00007554 QualType ToType;
7555 SourceLocation ToExprLoc;
7556 NonTypeTemplateParmDecl *ToParameter;
7557 Expr *ToReplacement;
7558 std::tie(ToType, ToExprLoc, ToParameter, ToReplacement) = *Imp;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007559
7560 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
Balazs Keri3b30d652018-10-19 13:32:20 +00007561 ToType, E->getValueKind(), ToExprLoc, ToParameter, ToReplacement);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00007562}
7563
Balazs Keri3b30d652018-10-19 13:32:20 +00007564ExpectedStmt ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
7565 auto Imp = importSeq(
7566 E->getType(), E->getBeginLoc(), E->getEndLoc());
7567 if (!Imp)
7568 return Imp.takeError();
7569
7570 QualType ToType;
7571 SourceLocation ToBeginLoc, ToEndLoc;
7572 std::tie(ToType, ToBeginLoc, ToEndLoc) = *Imp;
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007573
7574 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
Balazs Keri3b30d652018-10-19 13:32:20 +00007575 if (Error Err = ImportContainerChecked(E->getArgs(), ToArgs))
7576 return std::move(Err);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007577
7578 // According to Sema::BuildTypeTrait(), if E is value-dependent,
7579 // Value is always false.
Balazs Keri3b30d652018-10-19 13:32:20 +00007580 bool ToValue = (E->isValueDependent() ? false : E->getValue());
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007581
7582 return TypeTraitExpr::Create(
Balazs Keri3b30d652018-10-19 13:32:20 +00007583 Importer.getToContext(), ToType, ToBeginLoc, E->getTrait(), ToArgs,
7584 ToEndLoc, ToValue);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00007585}
7586
Balazs Keri3b30d652018-10-19 13:32:20 +00007587ExpectedStmt ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
7588 ExpectedType ToTypeOrErr = import(E->getType());
7589 if (!ToTypeOrErr)
7590 return ToTypeOrErr.takeError();
7591
7592 auto ToSourceRangeOrErr = import(E->getSourceRange());
7593 if (!ToSourceRangeOrErr)
7594 return ToSourceRangeOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007595
7596 if (E->isTypeOperand()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007597 if (auto ToTSIOrErr = import(E->getTypeOperandSourceInfo()))
7598 return new (Importer.getToContext()) CXXTypeidExpr(
7599 *ToTypeOrErr, *ToTSIOrErr, *ToSourceRangeOrErr);
7600 else
7601 return ToTSIOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007602 }
7603
Balazs Keri3b30d652018-10-19 13:32:20 +00007604 ExpectedExpr ToExprOperandOrErr = import(E->getExprOperand());
7605 if (!ToExprOperandOrErr)
7606 return ToExprOperandOrErr.takeError();
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007607
Balazs Keri3b30d652018-10-19 13:32:20 +00007608 return new (Importer.getToContext()) CXXTypeidExpr(
7609 *ToTypeOrErr, *ToExprOperandOrErr, *ToSourceRangeOrErr);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00007610}
7611
Lang Hames19e07e12017-06-20 21:06:00 +00007612void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
7613 CXXMethodDecl *FromMethod) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007614 for (auto *FromOverriddenMethod : FromMethod->overridden_methods()) {
7615 if (auto ImportedOrErr = import(FromOverriddenMethod))
7616 ToMethod->getCanonicalDecl()->addOverriddenMethod(cast<CXXMethodDecl>(
7617 (*ImportedOrErr)->getCanonicalDecl()));
7618 else
7619 consumeError(ImportedOrErr.takeError());
7620 }
Lang Hames19e07e12017-06-20 21:06:00 +00007621}
7622
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007623ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00007624 ASTContext &FromContext, FileManager &FromFileManager,
Gabor Marton54058b52018-12-17 13:53:12 +00007625 bool MinimalImport,
7626 ASTImporterLookupTable *LookupTable)
7627 : LookupTable(LookupTable), ToContext(ToContext), FromContext(FromContext),
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007628 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
7629 Minimal(MinimalImport) {
Gabor Marton54058b52018-12-17 13:53:12 +00007630
7631 ImportedDecls[FromContext.getTranslationUnitDecl()] =
7632 ToContext.getTranslationUnitDecl();
Douglas Gregor62d311f2010-02-09 19:21:46 +00007633}
7634
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007635ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007636
Gabor Marton54058b52018-12-17 13:53:12 +00007637Optional<unsigned> ASTImporter::getFieldIndex(Decl *F) {
7638 assert(F && (isa<FieldDecl>(*F) || isa<IndirectFieldDecl>(*F)) &&
7639 "Try to get field index for non-field.");
7640
7641 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
7642 if (!Owner)
7643 return None;
7644
7645 unsigned Index = 0;
7646 for (const auto *D : Owner->decls()) {
7647 if (D == F)
7648 return Index;
7649
7650 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
7651 ++Index;
7652 }
7653
7654 llvm_unreachable("Field was not found in its parent context.");
7655
7656 return None;
7657}
7658
7659ASTImporter::FoundDeclsTy
7660ASTImporter::findDeclsInToCtx(DeclContext *DC, DeclarationName Name) {
7661 // We search in the redecl context because of transparent contexts.
7662 // E.g. a simple C language enum is a transparent context:
7663 // enum E { A, B };
7664 // Now if we had a global variable in the TU
7665 // int A;
7666 // then the enum constant 'A' and the variable 'A' violates ODR.
7667 // We can diagnose this only if we search in the redecl context.
7668 DeclContext *ReDC = DC->getRedeclContext();
7669 if (LookupTable) {
7670 ASTImporterLookupTable::LookupResult LookupResult =
7671 LookupTable->lookup(ReDC, Name);
7672 return FoundDeclsTy(LookupResult.begin(), LookupResult.end());
7673 } else {
7674 // FIXME Can we remove this kind of lookup?
7675 // Or lldb really needs this C/C++ lookup?
7676 FoundDeclsTy Result;
7677 ReDC->localUncachedLookup(Name, Result);
7678 return Result;
7679 }
7680}
7681
7682void ASTImporter::AddToLookupTable(Decl *ToD) {
7683 if (LookupTable)
7684 if (auto *ToND = dyn_cast<NamedDecl>(ToD))
7685 LookupTable->add(ToND);
7686}
7687
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007688Expected<Decl *> ASTImporter::ImportImpl(Decl *FromD) {
7689 // Import the decl using ASTNodeImporter.
7690 ASTNodeImporter Importer(*this);
7691 return Importer.Visit(FromD);
7692}
7693
7694void ASTImporter::RegisterImportedDecl(Decl *FromD, Decl *ToD) {
7695 MapImported(FromD, ToD);
7696 AddToLookupTable(ToD);
7697}
7698
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007699Expected<QualType> ASTImporter::Import_New(QualType FromT) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007700 if (FromT.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007701 return QualType{};
John McCall424cec92011-01-19 06:33:43 +00007702
Balazs Keri3b30d652018-10-19 13:32:20 +00007703 const Type *FromTy = FromT.getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007704
7705 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00007706 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Balazs Keri3b30d652018-10-19 13:32:20 +00007707 = ImportedTypes.find(FromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007708 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00007709 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Fangrui Song6907ce22018-07-30 19:24:48 +00007710
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007711 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00007712 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007713 ExpectedType ToTOrErr = Importer.Visit(FromTy);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007714 if (!ToTOrErr)
7715 return ToTOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00007716
Douglas Gregorf65bbb32010-02-08 15:18:58 +00007717 // Record the imported type.
Balazs Keri3b30d652018-10-19 13:32:20 +00007718 ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
Fangrui Song6907ce22018-07-30 19:24:48 +00007719
Balazs Keri3b30d652018-10-19 13:32:20 +00007720 return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00007721}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007722QualType ASTImporter::Import(QualType From) {
7723 llvm::Expected<QualType> To = Import_New(From);
7724 if (To)
7725 return *To;
7726 else
7727 llvm::consumeError(To.takeError());
7728 return {};
7729}
Douglas Gregor96e578d2010-02-05 17:54:41 +00007730
Balazs Keri4a3d7582018-11-27 18:36:31 +00007731Expected<TypeSourceInfo *> ASTImporter::Import_New(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007732 if (!FromTSI)
7733 return FromTSI;
7734
7735 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00007736 // on the type and a single location. Implement a real version of this.
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007737 ExpectedType TOrErr = Import_New(FromTSI->getType());
7738 if (!TOrErr)
7739 return TOrErr.takeError();
7740 ExpectedSLoc BeginLocOrErr = Import_New(FromTSI->getTypeLoc().getBeginLoc());
7741 if (!BeginLocOrErr)
7742 return BeginLocOrErr.takeError();
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00007743
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007744 return ToContext.getTrivialTypeSourceInfo(*TOrErr, *BeginLocOrErr);
7745}
7746TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *From) {
7747 llvm::Expected<TypeSourceInfo *> To = Import_New(From);
7748 if (To)
7749 return *To;
7750 else
7751 llvm::consumeError(To.takeError());
7752 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007753}
7754
Balazs Keri4a3d7582018-11-27 18:36:31 +00007755Expected<Attr *> ASTImporter::Import_New(const Attr *FromAttr) {
Davide Italianofaee83d2018-11-28 19:15:23 +00007756 Attr *ToAttr = FromAttr->clone(ToContext);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007757 if (auto ToRangeOrErr = Import_New(FromAttr->getRange()))
7758 ToAttr->setRange(*ToRangeOrErr);
7759 else
7760 return ToRangeOrErr.takeError();
7761
Davide Italianofaee83d2018-11-28 19:15:23 +00007762 return ToAttr;
Balazs Kerideaf7ab2018-11-28 13:21:26 +00007763}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007764Attr *ASTImporter::Import(const Attr *From) {
7765 llvm::Expected<Attr *> To = Import_New(From);
7766 if (To)
7767 return *To;
7768 else
7769 llvm::consumeError(To.takeError());
7770 return nullptr;
7771}
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007772
Gabor Martonbe77a982018-12-12 11:22:55 +00007773Decl *ASTImporter::GetAlreadyImportedOrNull(const Decl *FromD) const {
7774 auto Pos = ImportedDecls.find(FromD);
7775 if (Pos != ImportedDecls.end())
7776 return Pos->second;
7777 else
Sean Callanan59721b32015-04-28 18:41:46 +00007778 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00007779}
7780
Gabor Marton458d1452019-02-14 13:07:03 +00007781TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) {
7782 auto FromDPos = ImportedFromDecls.find(ToD);
7783 if (FromDPos == ImportedFromDecls.end())
7784 return nullptr;
7785 return FromDPos->second->getTranslationUnitDecl();
7786}
7787
Balazs Keri4a3d7582018-11-27 18:36:31 +00007788Expected<Decl *> ASTImporter::Import_New(Decl *FromD) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007789 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00007790 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007791
Douglas Gregord451ea92011-07-29 23:31:30 +00007792
Gabor Marton26f72a92018-07-12 09:42:05 +00007793 // Check whether we've already imported this declaration.
7794 Decl *ToD = GetAlreadyImportedOrNull(FromD);
7795 if (ToD) {
7796 // If FromD has some updated flags after last import, apply it
7797 updateFlags(FromD, ToD);
Douglas Gregord451ea92011-07-29 23:31:30 +00007798 return ToD;
7799 }
Gabor Marton26f72a92018-07-12 09:42:05 +00007800
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007801 // Import the declaration.
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007802 ExpectedDecl ToDOrErr = ImportImpl(FromD);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007803 if (!ToDOrErr)
7804 return ToDOrErr;
Balazs Keri3b30d652018-10-19 13:32:20 +00007805 ToD = *ToDOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007806
Gabor Marton7f8c4002019-03-19 13:34:10 +00007807 // FIXME Use getImportDeclErrorIfAny() here (and return with the error) once
7808 // the error handling is finished in GetImportedOrCreateSpecialDecl().
7809 if (!ToD) {
7810 return nullptr;
7811 }
7812
Raphael Isemanne9bc35f2019-04-29 21:02:35 +00007813 // Make sure that ImportImpl registered the imported decl.
7814 assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?");
7815
Gabor Marton54058b52018-12-17 13:53:12 +00007816 // Once the decl is connected to the existing declarations, i.e. when the
7817 // redecl chain is properly set then we populate the lookup again.
7818 // This way the primary context will be able to find all decls.
7819 AddToLookupTable(ToD);
7820
Gabor Marton26f72a92018-07-12 09:42:05 +00007821 // Notify subclasses.
7822 Imported(FromD, ToD);
7823
Gabor Martonac3a5d62018-09-17 12:04:52 +00007824 updateFlags(FromD, ToD);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007825 return ToDOrErr;
7826}
7827Decl *ASTImporter::Import(Decl *From) {
7828 llvm::Expected<Decl *> To = Import_New(From);
7829 if (To)
7830 return *To;
7831 else
7832 llvm::consumeError(To.takeError());
7833 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007834}
7835
Balazs Keri3b30d652018-10-19 13:32:20 +00007836Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007837 if (!FromDC)
7838 return FromDC;
7839
Balazs Keria1f6b102019-04-08 13:59:15 +00007840 ExpectedDecl ToDCOrErr = Import_New(cast<Decl>(FromDC));
7841 if (!ToDCOrErr)
7842 return ToDCOrErr.takeError();
7843 auto *ToDC = cast<DeclContext>(*ToDCOrErr);
Craig Topper36250ad2014-05-12 05:36:57 +00007844
Fangrui Song6907ce22018-07-30 19:24:48 +00007845 // When we're using a record/enum/Objective-C class/protocol as a context, we
Douglas Gregor2e15c842012-02-01 21:00:38 +00007846 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007847 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
7848 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007849 if (ToRecord->isCompleteDefinition()) {
7850 // Do nothing.
7851 } else if (FromRecord->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007852 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7853 FromRecord, ToRecord, ASTNodeImporter::IDK_Basic))
7854 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007855 } else {
7856 CompleteDecl(ToRecord);
7857 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007858 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
7859 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007860 if (ToEnum->isCompleteDefinition()) {
7861 // Do nothing.
7862 } else if (FromEnum->isCompleteDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007863 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7864 FromEnum, ToEnum, ASTNodeImporter::IDK_Basic))
7865 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007866 } else {
7867 CompleteDecl(ToEnum);
Fangrui Song6907ce22018-07-30 19:24:48 +00007868 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007869 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
7870 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007871 if (ToClass->getDefinition()) {
7872 // Do nothing.
7873 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007874 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7875 FromDef, ToClass, ASTNodeImporter::IDK_Basic))
7876 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007877 } else {
7878 CompleteDecl(ToClass);
7879 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007880 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
7881 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007882 if (ToProto->getDefinition()) {
7883 // Do nothing.
7884 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
Balazs Keri3b30d652018-10-19 13:32:20 +00007885 if (Error Err = ASTNodeImporter(*this).ImportDefinition(
7886 FromDef, ToProto, ASTNodeImporter::IDK_Basic))
7887 return std::move(Err);
Douglas Gregor2e15c842012-02-01 21:00:38 +00007888 } else {
7889 CompleteDecl(ToProto);
Fangrui Song6907ce22018-07-30 19:24:48 +00007890 }
Douglas Gregor95d82832012-01-24 18:36:04 +00007891 }
Fangrui Song6907ce22018-07-30 19:24:48 +00007892
Douglas Gregor95d82832012-01-24 18:36:04 +00007893 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007894}
7895
Balazs Keri4a3d7582018-11-27 18:36:31 +00007896Expected<Expr *> ASTImporter::Import_New(Expr *FromE) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007897 if (ExpectedStmt ToSOrErr = Import_New(cast_or_null<Stmt>(FromE)))
7898 return cast_or_null<Expr>(*ToSOrErr);
7899 else
7900 return ToSOrErr.takeError();
Balazs Keri4a3d7582018-11-27 18:36:31 +00007901}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007902Expr *ASTImporter::Import(Expr *From) {
7903 llvm::Expected<Expr *> To = Import_New(From);
7904 if (To)
7905 return *To;
7906 else
7907 llvm::consumeError(To.takeError());
7908 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007909}
7910
Balazs Keri4a3d7582018-11-27 18:36:31 +00007911Expected<Stmt *> ASTImporter::Import_New(Stmt *FromS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007912 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00007913 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007914
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007915 // Check whether we've already imported this statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007916 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
7917 if (Pos != ImportedStmts.end())
7918 return Pos->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00007919
Balazs Keri3b30d652018-10-19 13:32:20 +00007920 // Import the statement.
Douglas Gregor7eeb5972010-02-11 19:21:55 +00007921 ASTNodeImporter Importer(*this);
Balazs Keri3b30d652018-10-19 13:32:20 +00007922 ExpectedStmt ToSOrErr = Importer.Visit(FromS);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007923 if (!ToSOrErr)
7924 return ToSOrErr;
Craig Topper36250ad2014-05-12 05:36:57 +00007925
Balazs Keri3b30d652018-10-19 13:32:20 +00007926 if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
Gabor Martona20ce602018-09-03 13:10:53 +00007927 auto *FromE = cast<Expr>(FromS);
7928 // Copy ExprBitfields, which may not be handled in Expr subclasses
7929 // constructors.
7930 ToE->setValueKind(FromE->getValueKind());
7931 ToE->setObjectKind(FromE->getObjectKind());
7932 ToE->setTypeDependent(FromE->isTypeDependent());
7933 ToE->setValueDependent(FromE->isValueDependent());
7934 ToE->setInstantiationDependent(FromE->isInstantiationDependent());
7935 ToE->setContainsUnexpandedParameterPack(
7936 FromE->containsUnexpandedParameterPack());
7937 }
7938
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007939 // Record the imported statement object.
Balazs Keri3b30d652018-10-19 13:32:20 +00007940 ImportedStmts[FromS] = *ToSOrErr;
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007941 return ToSOrErr;
7942}
7943Stmt *ASTImporter::Import(Stmt *From) {
7944 llvm::Expected<Stmt *> To = Import_New(From);
7945 if (To)
7946 return *To;
7947 else
7948 llvm::consumeError(To.takeError());
7949 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007950}
7951
Balazs Keri4a3d7582018-11-27 18:36:31 +00007952Expected<NestedNameSpecifier *>
7953ASTImporter::Import_New(NestedNameSpecifier *FromNNS) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00007954 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00007955 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007956
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007957 NestedNameSpecifier *Prefix;
7958 if (Error Err = importInto(Prefix, FromNNS->getPrefix()))
7959 return std::move(Err);
Douglas Gregor90ebf252011-04-27 16:48:40 +00007960
7961 switch (FromNNS->getKind()) {
7962 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007963 assert(FromNNS->getAsIdentifier() && "NNS should contain identifier.");
7964 return NestedNameSpecifier::Create(ToContext, Prefix,
7965 Import(FromNNS->getAsIdentifier()));
Douglas Gregor90ebf252011-04-27 16:48:40 +00007966
7967 case NestedNameSpecifier::Namespace:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007968 if (ExpectedDecl NSOrErr = Import_New(FromNNS->getAsNamespace())) {
7969 return NestedNameSpecifier::Create(ToContext, Prefix,
7970 cast<NamespaceDecl>(*NSOrErr));
7971 } else
7972 return NSOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00007973
7974 case NestedNameSpecifier::NamespaceAlias:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007975 if (ExpectedDecl NSADOrErr = Import_New(FromNNS->getAsNamespaceAlias()))
7976 return NestedNameSpecifier::Create(ToContext, Prefix,
7977 cast<NamespaceAliasDecl>(*NSADOrErr));
7978 else
7979 return NSADOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00007980
7981 case NestedNameSpecifier::Global:
7982 return NestedNameSpecifier::GlobalSpecifier(ToContext);
7983
Nikola Smiljanic67860242014-09-26 00:28:20 +00007984 case NestedNameSpecifier::Super:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007985 if (ExpectedDecl RDOrErr = Import_New(FromNNS->getAsRecordDecl()))
7986 return NestedNameSpecifier::SuperSpecifier(ToContext,
7987 cast<CXXRecordDecl>(*RDOrErr));
7988 else
7989 return RDOrErr.takeError();
Nikola Smiljanic67860242014-09-26 00:28:20 +00007990
Douglas Gregor90ebf252011-04-27 16:48:40 +00007991 case NestedNameSpecifier::TypeSpec:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00007992 case NestedNameSpecifier::TypeSpecWithTemplate:
7993 if (Expected<QualType> TyOrErr =
7994 Import_New(QualType(FromNNS->getAsType(), 0u))) {
7995 bool TSTemplate =
7996 FromNNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate;
7997 return NestedNameSpecifier::Create(ToContext, Prefix, TSTemplate,
7998 TyOrErr->getTypePtr());
7999 } else {
8000 return TyOrErr.takeError();
Douglas Gregor90ebf252011-04-27 16:48:40 +00008001 }
Douglas Gregor90ebf252011-04-27 16:48:40 +00008002 }
8003
8004 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00008005}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008006NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *From) {
8007 llvm::Expected<NestedNameSpecifier *> To = Import_New(From);
8008 if (To)
8009 return *To;
8010 else
8011 llvm::consumeError(To.takeError());
8012 return nullptr;
8013}
Douglas Gregor62d311f2010-02-09 19:21:46 +00008014
Balazs Keri4a3d7582018-11-27 18:36:31 +00008015Expected<NestedNameSpecifierLoc>
8016ASTImporter::Import_New(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008017 // Copied from NestedNameSpecifier mostly.
8018 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
8019 NestedNameSpecifierLoc NNS = FromNNS;
8020
8021 // Push each of the nested-name-specifiers's onto a stack for
8022 // serialization in reverse order.
8023 while (NNS) {
8024 NestedNames.push_back(NNS);
8025 NNS = NNS.getPrefix();
8026 }
8027
8028 NestedNameSpecifierLocBuilder Builder;
8029
8030 while (!NestedNames.empty()) {
8031 NNS = NestedNames.pop_back_val();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008032 NestedNameSpecifier *Spec;
8033 if (Error Err = importInto(Spec, NNS.getNestedNameSpecifier()))
8034 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008035
8036 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008037
8038 SourceLocation ToLocalBeginLoc, ToLocalEndLoc;
8039 if (Kind != NestedNameSpecifier::Super) {
8040 if (Error Err = importInto(ToLocalBeginLoc, NNS.getLocalBeginLoc()))
8041 return std::move(Err);
8042
8043 if (Kind != NestedNameSpecifier::Global)
8044 if (Error Err = importInto(ToLocalEndLoc, NNS.getLocalEndLoc()))
8045 return std::move(Err);
8046 }
8047
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008048 switch (Kind) {
8049 case NestedNameSpecifier::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008050 Builder.Extend(getToContext(), Spec->getAsIdentifier(), ToLocalBeginLoc,
8051 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008052 break;
8053
8054 case NestedNameSpecifier::Namespace:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008055 Builder.Extend(getToContext(), Spec->getAsNamespace(), ToLocalBeginLoc,
8056 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008057 break;
8058
8059 case NestedNameSpecifier::NamespaceAlias:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008060 Builder.Extend(getToContext(), Spec->getAsNamespaceAlias(),
8061 ToLocalBeginLoc, ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008062 break;
8063
8064 case NestedNameSpecifier::TypeSpec:
8065 case NestedNameSpecifier::TypeSpecWithTemplate: {
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008066 SourceLocation ToTLoc;
8067 if (Error Err = importInto(ToTLoc, NNS.getTypeLoc().getBeginLoc()))
8068 return std::move(Err);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008069 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
Balazs Keri5f4fd8b2019-03-14 14:20:23 +00008070 QualType(Spec->getAsType(), 0), ToTLoc);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008071 Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(),
8072 ToLocalEndLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008073 break;
8074 }
8075
8076 case NestedNameSpecifier::Global:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008077 Builder.MakeGlobal(getToContext(), ToLocalBeginLoc);
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008078 break;
8079
8080 case NestedNameSpecifier::Super: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008081 auto ToSourceRangeOrErr = Import_New(NNS.getSourceRange());
8082 if (!ToSourceRangeOrErr)
8083 return ToSourceRangeOrErr.takeError();
8084
8085 Builder.MakeSuper(getToContext(), Spec->getAsRecordDecl(),
8086 ToSourceRangeOrErr->getBegin(),
8087 ToSourceRangeOrErr->getEnd());
Aleksei Sidorin855086d2017-01-23 09:30:36 +00008088 }
8089 }
8090 }
8091
8092 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00008093}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008094NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc From) {
8095 llvm::Expected<NestedNameSpecifierLoc> To = Import_New(From);
8096 if (To)
8097 return *To;
8098 else
8099 llvm::consumeError(To.takeError());
8100 return {};
8101}
Douglas Gregor14454802011-02-25 02:25:35 +00008102
Balazs Keri4a3d7582018-11-27 18:36:31 +00008103Expected<TemplateName> ASTImporter::Import_New(TemplateName From) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00008104 switch (From.getKind()) {
8105 case TemplateName::Template:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008106 if (ExpectedDecl ToTemplateOrErr = Import_New(From.getAsTemplateDecl()))
8107 return TemplateName(cast<TemplateDecl>(*ToTemplateOrErr));
8108 else
8109 return ToTemplateOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008110
Douglas Gregore2e50d332010-12-01 01:36:18 +00008111 case TemplateName::OverloadedTemplate: {
8112 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
8113 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008114 for (auto *I : *FromStorage) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008115 if (auto ToOrErr = Import_New(I))
8116 ToTemplates.addDecl(cast<NamedDecl>(*ToOrErr));
Douglas Gregore2e50d332010-12-01 01:36:18 +00008117 else
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008118 return ToOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008119 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008120 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
Douglas Gregore2e50d332010-12-01 01:36:18 +00008121 ToTemplates.end());
8122 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008123
Douglas Gregore2e50d332010-12-01 01:36:18 +00008124 case TemplateName::QualifiedTemplate: {
8125 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008126 auto QualifierOrErr = Import_New(QTN->getQualifier());
8127 if (!QualifierOrErr)
8128 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008129
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008130 if (ExpectedDecl ToTemplateOrErr = Import_New(From.getAsTemplateDecl()))
8131 return ToContext.getQualifiedTemplateName(
8132 *QualifierOrErr, QTN->hasTemplateKeyword(),
8133 cast<TemplateDecl>(*ToTemplateOrErr));
8134 else
8135 return ToTemplateOrErr.takeError();
Douglas Gregore2e50d332010-12-01 01:36:18 +00008136 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008137
Douglas Gregore2e50d332010-12-01 01:36:18 +00008138 case TemplateName::DependentTemplate: {
8139 DependentTemplateName *DTN = From.getAsDependentTemplateName();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008140 auto QualifierOrErr = Import_New(DTN->getQualifier());
8141 if (!QualifierOrErr)
8142 return QualifierOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008143
Douglas Gregore2e50d332010-12-01 01:36:18 +00008144 if (DTN->isIdentifier()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008145 return ToContext.getDependentTemplateName(*QualifierOrErr,
Douglas Gregore2e50d332010-12-01 01:36:18 +00008146 Import(DTN->getIdentifier()));
8147 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008148
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008149 return ToContext.getDependentTemplateName(*QualifierOrErr,
8150 DTN->getOperator());
Douglas Gregore2e50d332010-12-01 01:36:18 +00008151 }
John McCalld9dfe3a2011-06-30 08:33:18 +00008152
8153 case TemplateName::SubstTemplateTemplateParm: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008154 SubstTemplateTemplateParmStorage *Subst =
8155 From.getAsSubstTemplateTemplateParm();
8156 ExpectedDecl ParamOrErr = Import_New(Subst->getParameter());
8157 if (!ParamOrErr)
8158 return ParamOrErr.takeError();
John McCalld9dfe3a2011-06-30 08:33:18 +00008159
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008160 auto ReplacementOrErr = Import_New(Subst->getReplacement());
8161 if (!ReplacementOrErr)
8162 return ReplacementOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008163
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008164 return ToContext.getSubstTemplateTemplateParm(
8165 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ReplacementOrErr);
John McCalld9dfe3a2011-06-30 08:33:18 +00008166 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008167
Douglas Gregor5590be02011-01-15 06:45:20 +00008168 case TemplateName::SubstTemplateTemplateParmPack: {
8169 SubstTemplateTemplateParmPackStorage *SubstPack
8170 = From.getAsSubstTemplateTemplateParmPack();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008171 ExpectedDecl ParamOrErr = Import_New(SubstPack->getParameterPack());
8172 if (!ParamOrErr)
8173 return ParamOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008174
Douglas Gregor5590be02011-01-15 06:45:20 +00008175 ASTNodeImporter Importer(*this);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008176 auto ArgPackOrErr =
8177 Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
8178 if (!ArgPackOrErr)
8179 return ArgPackOrErr.takeError();
Fangrui Song6907ce22018-07-30 19:24:48 +00008180
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008181 return ToContext.getSubstTemplateTemplateParmPack(
8182 cast<TemplateTemplateParmDecl>(*ParamOrErr), *ArgPackOrErr);
Douglas Gregor5590be02011-01-15 06:45:20 +00008183 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00008184 }
Fangrui Song6907ce22018-07-30 19:24:48 +00008185
Douglas Gregore2e50d332010-12-01 01:36:18 +00008186 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00008187}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008188TemplateName ASTImporter::Import(TemplateName From) {
8189 llvm::Expected<TemplateName> To = Import_New(From);
8190 if (To)
8191 return *To;
8192 else
8193 llvm::consumeError(To.takeError());
8194 return {};
8195}
Douglas Gregore2e50d332010-12-01 01:36:18 +00008196
Balazs Keri4a3d7582018-11-27 18:36:31 +00008197Expected<SourceLocation> ASTImporter::Import_New(SourceLocation FromLoc) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00008198 if (FromLoc.isInvalid())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008199 return SourceLocation{};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008200
Douglas Gregor811663e2010-02-10 00:15:17 +00008201 SourceManager &FromSM = FromContext.getSourceManager();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008202 bool IsBuiltin = FromSM.isWrittenInBuiltinFile(FromLoc);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008203
Douglas Gregor811663e2010-02-10 00:15:17 +00008204 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008205 Expected<FileID> ToFileIDOrErr = Import_New(Decomposed.first, IsBuiltin);
8206 if (!ToFileIDOrErr)
8207 return ToFileIDOrErr.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008208 SourceManager &ToSM = ToContext.getSourceManager();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008209 return ToSM.getComposedLoc(*ToFileIDOrErr, Decomposed.second);
8210}
8211SourceLocation ASTImporter::Import(SourceLocation From) {
8212 llvm::Expected<SourceLocation> To = Import_New(From);
8213 if (To)
8214 return *To;
8215 else
8216 llvm::consumeError(To.takeError());
8217 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008218}
8219
Balazs Keri4a3d7582018-11-27 18:36:31 +00008220Expected<SourceRange> ASTImporter::Import_New(SourceRange FromRange) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008221 SourceLocation ToBegin, ToEnd;
8222 if (Error Err = importInto(ToBegin, FromRange.getBegin()))
8223 return std::move(Err);
8224 if (Error Err = importInto(ToEnd, FromRange.getEnd()))
8225 return std::move(Err);
8226
8227 return SourceRange(ToBegin, ToEnd);
Balazs Keri4a3d7582018-11-27 18:36:31 +00008228}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008229SourceRange ASTImporter::Import(SourceRange From) {
8230 llvm::Expected<SourceRange> To = Import_New(From);
8231 if (To)
8232 return *To;
8233 else
8234 llvm::consumeError(To.takeError());
8235 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00008236}
8237
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008238Expected<FileID> ASTImporter::Import_New(FileID FromID, bool IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008239 llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00008240 if (Pos != ImportedFileIDs.end())
8241 return Pos->second;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008242
Douglas Gregor811663e2010-02-10 00:15:17 +00008243 SourceManager &FromSM = FromContext.getSourceManager();
8244 SourceManager &ToSM = ToContext.getSourceManager();
8245 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008246
8247 // Map the FromID to the "to" source manager.
Douglas Gregor811663e2010-02-10 00:15:17 +00008248 FileID ToID;
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008249 if (FromSLoc.isExpansion()) {
8250 const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008251 ExpectedSLoc ToSpLoc = Import_New(FromEx.getSpellingLoc());
8252 if (!ToSpLoc)
8253 return ToSpLoc.takeError();
8254 ExpectedSLoc ToExLocS = Import_New(FromEx.getExpansionLocStart());
8255 if (!ToExLocS)
8256 return ToExLocS.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008257 unsigned TokenLen = FromSM.getFileIDSize(FromID);
8258 SourceLocation MLoc;
8259 if (FromEx.isMacroArgExpansion()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008260 MLoc = ToSM.createMacroArgExpansionLoc(*ToSpLoc, *ToExLocS, TokenLen);
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008261 } else {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008262 if (ExpectedSLoc ToExLocE = Import_New(FromEx.getExpansionLocEnd()))
8263 MLoc = ToSM.createExpansionLoc(*ToSpLoc, *ToExLocS, *ToExLocE, TokenLen,
8264 FromEx.isExpansionTokenRange());
8265 else
8266 return ToExLocE.takeError();
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008267 }
8268 ToID = ToSM.getFileID(MLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00008269 } else {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008270 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008271
8272 if (!IsBuiltin) {
8273 // Include location of this file.
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008274 ExpectedSLoc ToIncludeLoc =
8275 Import_New(FromSLoc.getFile().getIncludeLoc());
8276 if (!ToIncludeLoc)
8277 return ToIncludeLoc.takeError();
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008278
8279 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
8280 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
8281 // disk again
8282 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
8283 // than mmap the files several times.
8284 const FileEntry *Entry =
8285 ToFileManager.getFile(Cache->OrigEntry->getName());
8286 // FIXME: The filename may be a virtual name that does probably not
8287 // point to a valid file and we get no Entry here. In this case try with
8288 // the memory buffer below.
8289 if (Entry)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008290 ToID = ToSM.createFileID(Entry, *ToIncludeLoc,
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008291 FromSLoc.getFile().getFileCharacteristic());
8292 }
Balazs Keri9cf39df2019-02-27 16:31:48 +00008293 }
Shafik Yaghmour9adbbcb2019-03-04 20:25:54 +00008294
8295 if (ToID.isInvalid() || IsBuiltin) {
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008296 // FIXME: We want to re-use the existing MemoryBuffer!
Balazs Keri9cf39df2019-02-27 16:31:48 +00008297 bool Invalid = true;
8298 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(
8299 FromContext.getDiagnostics(), FromSM, SourceLocation{}, &Invalid);
8300 if (!FromBuf || Invalid)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008301 // FIXME: Use a new error kind?
8302 return llvm::make_error<ImportError>(ImportError::Unknown);
Balazs Keri9cf39df2019-02-27 16:31:48 +00008303
Rafael Stahl29f37fb2018-07-04 13:34:05 +00008304 std::unique_ptr<llvm::MemoryBuffer> ToBuf =
8305 llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
8306 FromBuf->getBufferIdentifier());
8307 ToID = ToSM.createFileID(std::move(ToBuf),
8308 FromSLoc.getFile().getFileCharacteristic());
8309 }
Douglas Gregor811663e2010-02-10 00:15:17 +00008310 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008311
Balazs Keri9cf39df2019-02-27 16:31:48 +00008312 assert(ToID.isValid() && "Unexpected invalid fileID was created.");
8313
Sebastian Redl99219f12010-09-30 01:03:06 +00008314 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00008315 return ToID;
8316}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008317FileID ASTImporter::Import(FileID From, bool IsBuiltin) {
8318 llvm::Expected<FileID> To = Import_New(From, IsBuiltin);
8319 if (To)
8320 return *To;
8321 else
8322 llvm::consumeError(To.takeError());
8323 return {};
8324}
Douglas Gregor811663e2010-02-10 00:15:17 +00008325
Balazs Keri4a3d7582018-11-27 18:36:31 +00008326Expected<CXXCtorInitializer *>
8327ASTImporter::Import_New(CXXCtorInitializer *From) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008328 ExpectedExpr ToExprOrErr = Import_New(From->getInit());
8329 if (!ToExprOrErr)
8330 return ToExprOrErr.takeError();
8331
8332 auto LParenLocOrErr = Import_New(From->getLParenLoc());
8333 if (!LParenLocOrErr)
8334 return LParenLocOrErr.takeError();
8335
8336 auto RParenLocOrErr = Import_New(From->getRParenLoc());
8337 if (!RParenLocOrErr)
8338 return RParenLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008339
8340 if (From->isBaseInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008341 auto ToTInfoOrErr = Import_New(From->getTypeSourceInfo());
8342 if (!ToTInfoOrErr)
8343 return ToTInfoOrErr.takeError();
8344
8345 SourceLocation EllipsisLoc;
8346 if (From->isPackExpansion())
8347 if (Error Err = importInto(EllipsisLoc, From->getEllipsisLoc()))
8348 return std::move(Err);
Davide Italianofaee83d2018-11-28 19:15:23 +00008349
8350 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008351 ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr,
8352 *ToExprOrErr, *RParenLocOrErr, EllipsisLoc);
Davide Italianofaee83d2018-11-28 19:15:23 +00008353 } else if (From->isMemberInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008354 ExpectedDecl ToFieldOrErr = Import_New(From->getMember());
8355 if (!ToFieldOrErr)
8356 return ToFieldOrErr.takeError();
8357
8358 auto MemberLocOrErr = Import_New(From->getMemberLocation());
8359 if (!MemberLocOrErr)
8360 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008361
8362 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008363 ToContext, cast_or_null<FieldDecl>(*ToFieldOrErr), *MemberLocOrErr,
8364 *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008365 } else if (From->isIndirectMemberInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008366 ExpectedDecl ToIFieldOrErr = Import_New(From->getIndirectMember());
8367 if (!ToIFieldOrErr)
8368 return ToIFieldOrErr.takeError();
8369
8370 auto MemberLocOrErr = Import_New(From->getMemberLocation());
8371 if (!MemberLocOrErr)
8372 return MemberLocOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008373
8374 return new (ToContext) CXXCtorInitializer(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008375 ToContext, cast_or_null<IndirectFieldDecl>(*ToIFieldOrErr),
8376 *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008377 } else if (From->isDelegatingInitializer()) {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008378 auto ToTInfoOrErr = Import_New(From->getTypeSourceInfo());
8379 if (!ToTInfoOrErr)
8380 return ToTInfoOrErr.takeError();
Davide Italianofaee83d2018-11-28 19:15:23 +00008381
8382 return new (ToContext)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008383 CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr,
8384 *ToExprOrErr, *RParenLocOrErr);
Davide Italianofaee83d2018-11-28 19:15:23 +00008385 } else {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008386 // FIXME: assert?
8387 return make_error<ImportError>();
Davide Italianofaee83d2018-11-28 19:15:23 +00008388 }
Balazs Kerideaf7ab2018-11-28 13:21:26 +00008389}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008390CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
8391 llvm::Expected<CXXCtorInitializer *> To = Import_New(From);
8392 if (To)
8393 return *To;
8394 else
8395 llvm::consumeError(To.takeError());
8396 return nullptr;
8397}
Sean Callanandd2c1742016-05-16 20:48:03 +00008398
Balazs Keri4a3d7582018-11-27 18:36:31 +00008399Expected<CXXBaseSpecifier *>
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008400ASTImporter::Import_New(const CXXBaseSpecifier *BaseSpec) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00008401 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
8402 if (Pos != ImportedCXXBaseSpecifiers.end())
8403 return Pos->second;
8404
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008405 Expected<SourceRange> ToSourceRange = Import_New(BaseSpec->getSourceRange());
8406 if (!ToSourceRange)
8407 return ToSourceRange.takeError();
8408 Expected<TypeSourceInfo *> ToTSI = Import_New(BaseSpec->getTypeSourceInfo());
8409 if (!ToTSI)
8410 return ToTSI.takeError();
8411 ExpectedSLoc ToEllipsisLoc = Import_New(BaseSpec->getEllipsisLoc());
8412 if (!ToEllipsisLoc)
8413 return ToEllipsisLoc.takeError();
Aleksei Sidorina693b372016-09-28 10:16:56 +00008414 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008415 *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
8416 BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc);
Aleksei Sidorina693b372016-09-28 10:16:56 +00008417 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
8418 return Imported;
8419}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008420CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *From) {
8421 llvm::Expected<CXXBaseSpecifier *> To = Import_New(From);
8422 if (To)
8423 return *To;
8424 else
8425 llvm::consumeError(To.takeError());
8426 return nullptr;
8427}
Aleksei Sidorina693b372016-09-28 10:16:56 +00008428
Balazs Keri3b30d652018-10-19 13:32:20 +00008429Error ASTImporter::ImportDefinition_New(Decl *From) {
Douglas Gregor0a791672011-01-18 03:11:38 +00008430 Decl *To = Import(From);
8431 if (!To)
Balazs Keri3b30d652018-10-19 13:32:20 +00008432 return llvm::make_error<ImportError>();
Fangrui Song6907ce22018-07-30 19:24:48 +00008433
Don Hintonf170dff2019-03-19 06:14:14 +00008434 auto *FromDC = cast<DeclContext>(From);
8435 ASTNodeImporter Importer(*this);
Fangrui Song6907ce22018-07-30 19:24:48 +00008436
Don Hintonf170dff2019-03-19 06:14:14 +00008437 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
8438 if (!ToRecord->getDefinition()) {
8439 return Importer.ImportDefinition(
8440 cast<RecordDecl>(FromDC), ToRecord,
8441 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00008442 }
Douglas Gregor0a791672011-01-18 03:11:38 +00008443 }
Balazs Keri3b30d652018-10-19 13:32:20 +00008444
Don Hintonf170dff2019-03-19 06:14:14 +00008445 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
8446 if (!ToEnum->getDefinition()) {
8447 return Importer.ImportDefinition(
8448 cast<EnumDecl>(FromDC), ToEnum, ASTNodeImporter::IDK_Everything);
8449 }
8450 }
8451
8452 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
8453 if (!ToIFace->getDefinition()) {
8454 return Importer.ImportDefinition(
8455 cast<ObjCInterfaceDecl>(FromDC), ToIFace,
8456 ASTNodeImporter::IDK_Everything);
8457 }
8458 }
8459
8460 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
8461 if (!ToProto->getDefinition()) {
8462 return Importer.ImportDefinition(
8463 cast<ObjCProtocolDecl>(FromDC), ToProto,
8464 ASTNodeImporter::IDK_Everything);
8465 }
8466 }
8467
8468 return Importer.ImportDeclContext(FromDC, true);
Balazs Keri3b30d652018-10-19 13:32:20 +00008469}
8470
8471void ASTImporter::ImportDefinition(Decl *From) {
8472 Error Err = ImportDefinition_New(From);
8473 llvm::consumeError(std::move(Err));
Douglas Gregor0a791672011-01-18 03:11:38 +00008474}
8475
Balazs Keri4a3d7582018-11-27 18:36:31 +00008476Expected<DeclarationName> ASTImporter::Import_New(DeclarationName FromName) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008477 if (!FromName)
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008478 return DeclarationName{};
Douglas Gregor96e578d2010-02-05 17:54:41 +00008479
8480 switch (FromName.getNameKind()) {
8481 case DeclarationName::Identifier:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008482 return DeclarationName(Import(FromName.getAsIdentifierInfo()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008483
8484 case DeclarationName::ObjCZeroArgSelector:
8485 case DeclarationName::ObjCOneArgSelector:
8486 case DeclarationName::ObjCMultiArgSelector:
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008487 if (auto ToSelOrErr = Import_New(FromName.getObjCSelector()))
8488 return DeclarationName(*ToSelOrErr);
8489 else
8490 return ToSelOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008491
8492 case DeclarationName::CXXConstructorName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008493 if (auto ToTyOrErr = Import_New(FromName.getCXXNameType()))
8494 return ToContext.DeclarationNames.getCXXConstructorName(
8495 ToContext.getCanonicalType(*ToTyOrErr));
8496 else
8497 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008498 }
8499
8500 case DeclarationName::CXXDestructorName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008501 if (auto ToTyOrErr = Import_New(FromName.getCXXNameType()))
8502 return ToContext.DeclarationNames.getCXXDestructorName(
8503 ToContext.getCanonicalType(*ToTyOrErr));
8504 else
8505 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008506 }
8507
Richard Smith35845152017-02-07 01:37:30 +00008508 case DeclarationName::CXXDeductionGuideName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008509 if (auto ToTemplateOrErr =
8510 Import_New(FromName.getCXXDeductionGuideTemplate()))
8511 return ToContext.DeclarationNames.getCXXDeductionGuideName(
8512 cast<TemplateDecl>(*ToTemplateOrErr));
8513 else
8514 return ToTemplateOrErr.takeError();
Richard Smith35845152017-02-07 01:37:30 +00008515 }
8516
Douglas Gregor96e578d2010-02-05 17:54:41 +00008517 case DeclarationName::CXXConversionFunctionName: {
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008518 if (auto ToTyOrErr = Import_New(FromName.getCXXNameType()))
8519 return ToContext.DeclarationNames.getCXXConversionFunctionName(
8520 ToContext.getCanonicalType(*ToTyOrErr));
8521 else
8522 return ToTyOrErr.takeError();
Douglas Gregor96e578d2010-02-05 17:54:41 +00008523 }
8524
8525 case DeclarationName::CXXOperatorName:
8526 return ToContext.DeclarationNames.getCXXOperatorName(
8527 FromName.getCXXOverloadedOperator());
8528
8529 case DeclarationName::CXXLiteralOperatorName:
8530 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008531 Import(FromName.getCXXLiteralIdentifier()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00008532
8533 case DeclarationName::CXXUsingDirective:
8534 // FIXME: STATICS!
8535 return DeclarationName::getUsingDirectiveName();
8536 }
8537
David Blaikiee4d798f2012-01-20 21:50:17 +00008538 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00008539}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008540DeclarationName ASTImporter::Import(DeclarationName From) {
8541 llvm::Expected<DeclarationName> To = Import_New(From);
8542 if (To)
8543 return *To;
8544 else
8545 llvm::consumeError(To.takeError());
8546 return {};
8547}
Douglas Gregor96e578d2010-02-05 17:54:41 +00008548
Douglas Gregore2e50d332010-12-01 01:36:18 +00008549IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00008550 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00008551 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008552
Sean Callananf94ef1d2016-05-14 06:11:19 +00008553 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
8554
8555 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
8556 ToId->setBuiltinID(FromId->getBuiltinID());
8557
8558 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00008559}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008560
Balazs Keri4a3d7582018-11-27 18:36:31 +00008561Expected<Selector> ASTImporter::Import_New(Selector FromSel) {
Douglas Gregor43f54792010-02-17 02:12:47 +00008562 if (FromSel.isNull())
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008563 return Selector{};
Douglas Gregor43f54792010-02-17 02:12:47 +00008564
Chris Lattner0e62c1c2011-07-23 10:55:15 +00008565 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00008566 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
8567 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
8568 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
8569 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
8570}
8571
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008572DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
8573 DeclContext *DC,
8574 unsigned IDNS,
8575 NamedDecl **Decls,
8576 unsigned NumDecls) {
8577 return Name;
8578}
Balazs Kerie2ddb2a2019-03-07 14:09:18 +00008579Selector ASTImporter::Import(Selector From) {
8580 llvm::Expected<Selector> To = Import_New(From);
8581 if (To)
8582 return *To;
8583 else
8584 llvm::consumeError(To.takeError());
8585 return {};
8586}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008587
8588DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008589 if (LastDiagFromFrom)
8590 ToContext.getDiagnostics().notePriorDiagnosticFrom(
8591 FromContext.getDiagnostics());
8592 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008593 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008594}
8595
8596DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00008597 if (!LastDiagFromFrom)
8598 FromContext.getDiagnostics().notePriorDiagnosticFrom(
8599 ToContext.getDiagnostics());
8600 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00008601 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00008602}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008603
Douglas Gregor2e15c842012-02-01 21:00:38 +00008604void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008605 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008606 if (!ID->getDefinition())
8607 ID->startDefinition();
8608 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008609 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008610 if (!PD->getDefinition())
8611 PD->startDefinition();
8612 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008613 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00008614 if (!TD->getDefinition() && !TD->isBeingDefined()) {
8615 TD->startDefinition();
8616 TD->setCompleteDefinition(true);
8617 }
8618 }
8619 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00008620 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00008621 }
8622}
8623
Gabor Marton26f72a92018-07-12 09:42:05 +00008624Decl *ASTImporter::MapImported(Decl *From, Decl *To) {
8625 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(From);
8626 assert((Pos == ImportedDecls.end() || Pos->second == To) &&
8627 "Try to import an already imported Decl");
8628 if (Pos != ImportedDecls.end())
8629 return Pos->second;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008630 ImportedDecls[From] = To;
Gabor Marton458d1452019-02-14 13:07:03 +00008631 // This mapping should be maintained only in this function. Therefore do not
8632 // check for additional consistency.
8633 ImportedFromDecls[To] = From;
Douglas Gregor8cdbe642010-02-12 23:44:20 +00008634 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00008635}
Douglas Gregorb4964f72010-02-15 23:54:17 +00008636
Douglas Gregordd6006f2012-07-17 21:16:27 +00008637bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
8638 bool Complain) {
Balazs Keria1f6b102019-04-08 13:59:15 +00008639 llvm::DenseMap<const Type *, const Type *>::iterator Pos =
8640 ImportedTypes.find(From.getTypePtr());
8641 if (Pos != ImportedTypes.end()) {
8642 if (ExpectedType ToFromOrErr = Import_New(From)) {
8643 if (ToContext.hasSameType(*ToFromOrErr, To))
8644 return true;
8645 } else {
8646 llvm::consumeError(ToFromOrErr.takeError());
8647 }
8648 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00008649
Douglas Gregordd6006f2012-07-17 21:16:27 +00008650 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
Gabor Marton26f72a92018-07-12 09:42:05 +00008651 getStructuralEquivalenceKind(*this), false,
8652 Complain);
Gabor Marton950fb572018-07-17 12:39:27 +00008653 return Ctx.IsEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00008654}